summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/include/clang/AST/ExprObjC.h7
-rw-r--r--clang/lib/AST/ExprObjC.cpp25
2 files changed, 32 insertions, 0 deletions
diff --git a/clang/include/clang/AST/ExprObjC.h b/clang/include/clang/AST/ExprObjC.h
index debe5acebfd..6719169f0e9 100644
--- a/clang/include/clang/AST/ExprObjC.h
+++ b/clang/include/clang/AST/ExprObjC.h
@@ -1180,6 +1180,13 @@ public:
/// sent to.
ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; }
+ /// \return the return type of the message being sent.
+ /// This is not always the type of the message expression itself because
+ /// of references (the expression would not have a reference type).
+ /// It is also not always the declared return type of the method because
+ /// of `instancetype` (in that case it's an expression type).
+ QualType getCallReturnType(ASTContext &Ctx) const;
+
/// Source range of the receiver.
SourceRange getReceiverRange() const;
diff --git a/clang/lib/AST/ExprObjC.cpp b/clang/lib/AST/ExprObjC.cpp
index c31b5de2a5b..584aa8c1fbe 100644
--- a/clang/lib/AST/ExprObjC.cpp
+++ b/clang/lib/AST/ExprObjC.cpp
@@ -292,6 +292,31 @@ void ObjCMessageExpr::getSelectorLocs(
SelLocs.push_back(getSelectorLoc(i));
}
+
+QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const {
+ if (const ObjCMethodDecl *MD = getMethodDecl()) {
+ QualType QT = MD->getReturnType();
+ if (QT == Ctx.getObjCInstanceType()) {
+ // instancetype corresponds to expression types.
+ return getType();
+ }
+ return QT;
+ }
+
+ // Expression type might be different from an expected call return type,
+ // as expression type would never be a reference even if call returns a
+ // reference. Reconstruct the original expression type.
+ QualType QT = getType();
+ switch (getValueKind()) {
+ case VK_LValue:
+ return Ctx.getLValueReferenceType(QT);
+ case VK_XValue:
+ return Ctx.getRValueReferenceType(QT);
+ case VK_RValue:
+ return QT;
+ }
+}
+
SourceRange ObjCMessageExpr::getReceiverRange() const {
switch (getReceiverKind()) {
case Instance:
OpenPOWER on IntegriCloud