r352147 - [AST] Add a method to get a call type from an ObjCMessageExpr

George Karpenkov via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 24 17:23:37 PST 2019


Author: george.karpenkov
Date: Thu Jan 24 17:23:37 2019
New Revision: 352147

URL: http://llvm.org/viewvc/llvm-project?rev=352147&view=rev
Log:
[AST] Add a method to get a call type from an ObjCMessageExpr

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

Modified:
    cfe/trunk/include/clang/AST/ExprObjC.h
    cfe/trunk/lib/AST/ExprObjC.cpp

Modified: cfe/trunk/include/clang/AST/ExprObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprObjC.h?rev=352147&r1=352146&r2=352147&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprObjC.h (original)
+++ cfe/trunk/include/clang/AST/ExprObjC.h Thu Jan 24 17:23:37 2019
@@ -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;
 

Modified: cfe/trunk/lib/AST/ExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprObjC.cpp?rev=352147&r1=352146&r2=352147&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprObjC.cpp (original)
+++ cfe/trunk/lib/AST/ExprObjC.cpp Thu Jan 24 17:23:37 2019
@@ -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:




More information about the cfe-commits mailing list