]> granicus.if.org Git - clang/commitdiff
[AST] Add a method to get a call type from an ObjCMessageExpr
authorGeorge Karpenkov <ekarpenkov@apple.com>
Fri, 25 Jan 2019 01:23:37 +0000 (01:23 +0000)
committerGeorge Karpenkov <ekarpenkov@apple.com>
Fri, 25 Jan 2019 01:23:37 +0000 (01:23 +0000)
Due to references, expression type does not always correspond to an
expected method return type (e.g. for a method returning int & the
expression type of the call would still be int).
We have a helper method for getting the expected type on CallExpr, but
not on ObjCMessageExpr.

Differential Revision: https://reviews.llvm.org/D57204

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352147 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ExprObjC.h
lib/AST/ExprObjC.cpp

index debe5acebfd1f234b052178fde03801f9d8dcc9d..6719169f0e97088562b2a3fdddb6ed0e2d49a76f 100644 (file)
@@ -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;
 
index c31b5de2a5b91be1a91caee7f2d4820fc6d6a605..584aa8c1fbeb44db6fab26818af101be7674084b 100644 (file)
@@ -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: