Index: clang/include/clang/AST/ExprObjC.h =================================================================== --- clang/include/clang/AST/ExprObjC.h +++ clang/include/clang/AST/ExprObjC.h @@ -980,6 +980,11 @@ SourceLocation RBracLoc, bool isImplicit); + /// getCallReturnType - Get the return type of the call expr. This is not + /// always the type of the expr itself, if the return type is a reference + /// type. + QualType getCallReturnType(ASTContext &Ctx) const; + size_t numTrailingObjects(OverloadToken) const { return NumArgs + 1; } void setNumArgs(unsigned Num) { Index: clang/lib/AST/ExprObjC.cpp =================================================================== --- clang/lib/AST/ExprObjC.cpp +++ clang/lib/AST/ExprObjC.cpp @@ -292,6 +292,31 @@ 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: