SemaExprObjC.cpp revision 48218c60d6b53b7880917d1366ee716dec2145ca
185a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// 285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// 385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// The LLVM Compiler Infrastructure 485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// 585a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// This file is distributed under the University of Illinois Open Source 685a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// License. See LICENSE.TXT for details. 785a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// 885a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner//===----------------------------------------------------------------------===// 985a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// 1085a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// This file implements semantic analysis for Objective-C expressions. 1185a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// 1285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner//===----------------------------------------------------------------------===// 1385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h" 15e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h" 165f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h" 1726743b20e4a8c2a986e6453f0c38beba0afef633John McCall#include "clang/Sema/ScopeInfo.h" 18e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h" 1985a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner#include "clang/AST/ASTContext.h" 2085a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner#include "clang/AST/DeclObjC.h" 21f494b579b22f9950f5af021f0bf9879a91bb8b41Steve Naroff#include "clang/AST/ExprObjC.h" 22f85e193739c953358c865005855253af4f68a497John McCall#include "clang/AST/StmtVisitor.h" 232725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor#include "clang/AST/TypeLoc.h" 2439c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner#include "llvm/ADT/SmallString.h" 2561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff#include "clang/Lex/Preprocessor.h" 2661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 2785a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattnerusing namespace clang; 2826743b20e4a8c2a986e6453f0c38beba0afef633John McCallusing namespace sema; 2985a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 30f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, 31f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall Expr **strings, 32f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall unsigned NumStrings) { 3339c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); 3439c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner 35f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner // Most ObjC strings are formed out of a single piece. However, we *can* 36f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner // have strings formed out of multiple @ strings with multiple pptokens in 37f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one 38f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner // StringLiteral for ObjCStringLiteral to hold onto. 3939c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner StringLiteral *S = Strings[0]; 401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 41f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner // If we have a multi-part string, merge it all together. 42f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner if (NumStrings != 1) { 4385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner // Concatenate objc strings. 4439c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner llvm::SmallString<128> StrBuf; 4539c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner llvm::SmallVector<SourceLocation, 8> StrLocs; 461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 47726e168dc09fb23f53c7b004f8e919421ee91806Chris Lattner for (unsigned i = 0; i != NumStrings; ++i) { 4839c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner S = Strings[i]; 491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5039c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner // ObjC strings can't be wide. 51f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner if (S->isWide()) { 52f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) 53f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner << S->getSourceRange(); 54f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner return true; 55f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner } 561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 572f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer // Append the string. 582f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer StrBuf += S->getString(); 591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6039c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner // Get the locations of the string tokens. 6139c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner StrLocs.append(S->tokloc_begin(), S->tokloc_end()); 6285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner } 631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6439c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner // Create the aggregate string with the appropriate content and location 6539c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner // information. 6665aa6885818d4b4eea2e5a9d12085b2398148662Jay Foad S = StringLiteral::Create(Context, StrBuf, 673e2193ce5feb2feb092e5ae615e85148e06e9fd2Anders Carlsson /*Wide=*/false, /*Pascal=*/false, 682085fd6cd22ec5c268175251db10d7c60caf7aaaChris Lattner Context.getPointerType(Context.CharTy), 6939c28bbbf235533e9ae7d06fb9b13371dfcc542dChris Lattner &StrLocs[0], StrLocs.size()); 7085a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner } 711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 72690398188ea5b428f06aa13c7d4ce6eb741ad4f9Chris Lattner // Verify that this composite string is acceptable for ObjC strings. 73690398188ea5b428f06aa13c7d4ce6eb741ad4f9Chris Lattner if (CheckObjCString(S)) 7485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner return true; 75a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner 76a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner // Initialize the constant string interface lazily. This assumes 77d9fd7647e286723d100db4cfeab31ec022eec629Steve Naroff // the NSString interface is seen in this translation unit. Note: We 78d9fd7647e286723d100db4cfeab31ec022eec629Steve Naroff // don't use NSConstantString, since the runtime team considers this 79d9fd7647e286723d100db4cfeab31ec022eec629Steve Naroff // interface private (even though it appears in the header files). 80a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner QualType Ty = Context.getObjCConstantStringInterface(); 81a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner if (!Ty.isNull()) { 8214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff Ty = Context.getObjCObjectPointerType(Ty); 838a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian } else if (getLangOptions().NoConstantCFStrings) { 844c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian IdentifierInfo *NSIdent=0; 854c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian std::string StringClass(getLangOptions().ObjCConstantStringClass); 864c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian 874c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian if (StringClass.empty()) 884c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian NSIdent = &Context.Idents.get("NSConstantString"); 894c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian else 904c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian NSIdent = &Context.Idents.get(StringClass); 914c73307c74764ba99e1379677fe92af72f676531Fariborz Jahanian 928a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], 938a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian LookupOrdinaryName); 948a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { 958a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian Context.setObjCConstantStringInterface(StrIF); 968a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian Ty = Context.getObjCConstantStringInterface(); 978a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian Ty = Context.getObjCObjectPointerType(Ty); 988a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian } else { 998a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian // If there is no NSConstantString interface defined then treat this 1008a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian // as error and recover from it. 1018a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent 1028a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian << S->getSourceRange(); 1038a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian Ty = Context.getObjCIdType(); 1048a4377697161c5087e27cc40d6e0682f0cd1fa20Fariborz Jahanian } 10513fd7e5111032f54b538dd66d035b0ccc1f82467Chris Lattner } else { 106d9fd7647e286723d100db4cfeab31ec022eec629Steve Naroff IdentifierInfo *NSIdent = &Context.Idents.get("NSString"); 107c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], 108c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor LookupOrdinaryName); 109a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { 110a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner Context.setObjCConstantStringInterface(StrIF); 111a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner Ty = Context.getObjCConstantStringInterface(); 11214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff Ty = Context.getObjCObjectPointerType(Ty); 113a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner } else { 114d9fd7647e286723d100db4cfeab31ec022eec629Steve Naroff // If there is no NSString interface defined then treat constant 115a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner // strings as untyped objects and let the runtime figure it out later. 116a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner Ty = Context.getObjCIdType(); 117a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner } 11813fd7e5111032f54b538dd66d035b0ccc1f82467Chris Lattner } 1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120f4b136fb40aeedeaaa6ce7cdff22f375eb76c47bChris Lattner return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); 12185a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 12285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 1233b5904ba83fc9b968b1cd6f1ce78d01bfd6e8686Argyrios KyrtzidisExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, 12481d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor TypeSourceInfo *EncodedTypeInfo, 125fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson SourceLocation RParenLoc) { 12681d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor QualType EncodedType = EncodedTypeInfo->getType(); 127fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson QualType StrTy; 1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (EncodedType->isDependentType()) 129fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson StrTy = Context.DependentTy; 130fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson else { 1316c91615e5d67d59e752037beb475c4b827431172Fariborz Jahanian if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled. 1326c91615e5d67d59e752037beb475c4b827431172Fariborz Jahanian !EncodedType->isVoidType()) // void is handled too. 1333b5904ba83fc9b968b1cd6f1ce78d01bfd6e8686Argyrios Kyrtzidis if (RequireCompleteType(AtLoc, EncodedType, 1343b5904ba83fc9b968b1cd6f1ce78d01bfd6e8686Argyrios Kyrtzidis PDiag(diag::err_incomplete_type_objc_at_encode) 1353b5904ba83fc9b968b1cd6f1ce78d01bfd6e8686Argyrios Kyrtzidis << EncodedTypeInfo->getTypeLoc().getSourceRange())) 1363b5904ba83fc9b968b1cd6f1ce78d01bfd6e8686Argyrios Kyrtzidis return ExprError(); 1373b5904ba83fc9b968b1cd6f1ce78d01bfd6e8686Argyrios Kyrtzidis 138fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson std::string Str; 139fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson Context.getObjCEncodingForType(EncodedType, Str); 140fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson 141fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson // The type of @encode is the same as the type of the corresponding string, 142fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson // which is an array type. 143fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson StrTy = Context.CharTy; 144fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). 1454b7a834e0fecddd9eaf1f4567867c718e4eebf50John McCall if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) 146fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson StrTy.addConst(); 147fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), 148fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson ArrayType::Normal, 0); 149fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson } 1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15181d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); 152fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson} 153fc0f021b492cf28ee7b3a6bd4445ae569e6f15deAnders Carlsson 154f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, 155f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation EncodeLoc, 156f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation LParenLoc, 157f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall ParsedType ty, 158f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation RParenLoc) { 159e8661906d49ef6c9694a9cc845ca62a85dbc016dArgyrios Kyrtzidis // FIXME: Preserve type source info ? 16081d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor TypeSourceInfo *TInfo; 16181d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor QualType EncodedType = GetTypeFromParser(ty, &TInfo); 16281d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor if (!TInfo) 16381d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor TInfo = Context.getTrivialTypeSourceInfo(EncodedType, 16481d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor PP.getLocForEndOfToken(LParenLoc)); 16585a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 16681d3466d037dc5844234c7a93dab21a6ad986e7dDouglas Gregor return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); 16785a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 16885a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 169f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallExprResult Sema::ParseObjCSelectorExpression(Selector Sel, 170f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation AtLoc, 171f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation SelLoc, 172f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation LParenLoc, 173f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation RParenLoc) { 1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, 1756b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian SourceRange(LParenLoc, RParenLoc), false, false); 1767ff22ded2221f442b1f8ff78172938d04ec8c926Fariborz Jahanian if (!Method) 1777ff22ded2221f442b1f8ff78172938d04ec8c926Fariborz Jahanian Method = LookupFactoryMethodInGlobalPool(Sel, 1787ff22ded2221f442b1f8ff78172938d04ec8c926Fariborz Jahanian SourceRange(LParenLoc, RParenLoc)); 1797ff22ded2221f442b1f8ff78172938d04ec8c926Fariborz Jahanian if (!Method) 1807ff22ded2221f442b1f8ff78172938d04ec8c926Fariborz Jahanian Diag(SelLoc, diag::warn_undeclared_selector) << Sel; 1817ff22ded2221f442b1f8ff78172938d04ec8c926Fariborz Jahanian 1823fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian llvm::DenseMap<Selector, SourceLocation>::iterator Pos 1833fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian = ReferencedSelectors.find(Sel); 1843fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian if (Pos == ReferencedSelectors.end()) 1853fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); 1863fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian 187f85e193739c953358c865005855253af4f68a497John McCall // In ARC, forbid the user from using @selector for 188f85e193739c953358c865005855253af4f68a497John McCall // retain/release/autorelease/dealloc/retainCount. 189f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount) { 190f85e193739c953358c865005855253af4f68a497John McCall switch (Sel.getMethodFamily()) { 191f85e193739c953358c865005855253af4f68a497John McCall case OMF_retain: 192f85e193739c953358c865005855253af4f68a497John McCall case OMF_release: 193f85e193739c953358c865005855253af4f68a497John McCall case OMF_autorelease: 194f85e193739c953358c865005855253af4f68a497John McCall case OMF_retainCount: 195f85e193739c953358c865005855253af4f68a497John McCall case OMF_dealloc: 196f85e193739c953358c865005855253af4f68a497John McCall Diag(AtLoc, diag::err_arc_illegal_selector) << 197f85e193739c953358c865005855253af4f68a497John McCall Sel << SourceRange(LParenLoc, RParenLoc); 198f85e193739c953358c865005855253af4f68a497John McCall break; 199f85e193739c953358c865005855253af4f68a497John McCall 200f85e193739c953358c865005855253af4f68a497John McCall case OMF_None: 201f85e193739c953358c865005855253af4f68a497John McCall case OMF_alloc: 202f85e193739c953358c865005855253af4f68a497John McCall case OMF_copy: 203f85e193739c953358c865005855253af4f68a497John McCall case OMF_init: 204f85e193739c953358c865005855253af4f68a497John McCall case OMF_mutableCopy: 205f85e193739c953358c865005855253af4f68a497John McCall case OMF_new: 206f85e193739c953358c865005855253af4f68a497John McCall case OMF_self: 2079670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_performSelector: 208f85e193739c953358c865005855253af4f68a497John McCall break; 209f85e193739c953358c865005855253af4f68a497John McCall } 210f85e193739c953358c865005855253af4f68a497John McCall } 211a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner QualType Ty = Context.getObjCSelType(); 2126d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); 21385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 21485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 215f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, 216f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation AtLoc, 217f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation ProtoLoc, 218f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation LParenLoc, 219f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall SourceLocation RParenLoc) { 220c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc); 22185a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner if (!PDecl) { 2223c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; 22385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner return true; 22485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner } 2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 226a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner QualType Ty = Context.getObjCProtoType(); 227a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner if (Ty.isNull()) 22885a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner return true; 22914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff Ty = Context.getObjCObjectPointerType(Ty); 230a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); 23185a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 23285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 23326743b20e4a8c2a986e6453f0c38beba0afef633John McCall/// Try to capture an implicit reference to 'self'. 23426743b20e4a8c2a986e6453f0c38beba0afef633John McCallObjCMethodDecl *Sema::tryCaptureObjCSelf() { 23526743b20e4a8c2a986e6453f0c38beba0afef633John McCall // Ignore block scopes: we can capture through them. 23626743b20e4a8c2a986e6453f0c38beba0afef633John McCall DeclContext *DC = CurContext; 23726743b20e4a8c2a986e6453f0c38beba0afef633John McCall while (true) { 23826743b20e4a8c2a986e6453f0c38beba0afef633John McCall if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext(); 23926743b20e4a8c2a986e6453f0c38beba0afef633John McCall else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext(); 24026743b20e4a8c2a986e6453f0c38beba0afef633John McCall else break; 24126743b20e4a8c2a986e6453f0c38beba0afef633John McCall } 24226743b20e4a8c2a986e6453f0c38beba0afef633John McCall 24326743b20e4a8c2a986e6453f0c38beba0afef633John McCall // If we're not in an ObjC method, error out. Note that, unlike the 24426743b20e4a8c2a986e6453f0c38beba0afef633John McCall // C++ case, we don't require an instance method --- class methods 24526743b20e4a8c2a986e6453f0c38beba0afef633John McCall // still have a 'self', and we really do still need to capture it! 24626743b20e4a8c2a986e6453f0c38beba0afef633John McCall ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC); 24726743b20e4a8c2a986e6453f0c38beba0afef633John McCall if (!method) 24826743b20e4a8c2a986e6453f0c38beba0afef633John McCall return 0; 24926743b20e4a8c2a986e6453f0c38beba0afef633John McCall 25026743b20e4a8c2a986e6453f0c38beba0afef633John McCall ImplicitParamDecl *self = method->getSelfDecl(); 25126743b20e4a8c2a986e6453f0c38beba0afef633John McCall assert(self && "capturing 'self' in non-definition?"); 25226743b20e4a8c2a986e6453f0c38beba0afef633John McCall 25326743b20e4a8c2a986e6453f0c38beba0afef633John McCall // Mark that we're closing on 'this' in all the block scopes, if applicable. 25426743b20e4a8c2a986e6453f0c38beba0afef633John McCall for (unsigned idx = FunctionScopes.size() - 1; 25526743b20e4a8c2a986e6453f0c38beba0afef633John McCall isa<BlockScopeInfo>(FunctionScopes[idx]); 2566b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall --idx) { 2576b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]); 2586b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall unsigned &captureIndex = blockScope->CaptureMap[self]; 2596b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall if (captureIndex) break; 2606b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall 2616b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]); 2626b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall blockScope->Captures.push_back( 2636b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall BlockDecl::Capture(self, /*byref*/ false, nested, /*copy*/ 0)); 2646b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall captureIndex = blockScope->Captures.size(); // +1 2656b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall } 26626743b20e4a8c2a986e6453f0c38beba0afef633John McCall 26726743b20e4a8c2a986e6453f0c38beba0afef633John McCall return method; 26826743b20e4a8c2a986e6453f0c38beba0afef633John McCall} 26926743b20e4a8c2a986e6453f0c38beba0afef633John McCall 270926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas GregorQualType Sema::getMessageSendResultType(QualType ReceiverType, 271926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ObjCMethodDecl *Method, 272926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor bool isClassMessage, bool isSuperMessage) { 273926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor assert(Method && "Must have a method"); 274926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method->hasRelatedResultType()) 275926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Method->getSendResultType(); 276926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 277926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // If a method has a related return type: 278926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // - if the method found is an instance method, but the message send 279926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // was a class message send, T is the declared return type of the method 280926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // found 281926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (Method->isInstanceMethod() && isClassMessage) 282926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Method->getSendResultType(); 283926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 284926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // - if the receiver is super, T is a pointer to the class of the 285926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // enclosing method definition 286926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (isSuperMessage) { 287926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) 288926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) 289926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Context.getObjCObjectPointerType( 290926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Context.getObjCInterfaceType(Class)); 291926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } 292926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 293926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // - if the receiver is the name of a class U, T is a pointer to U 294926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (ReceiverType->getAs<ObjCInterfaceType>() || 295926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType->isObjCQualifiedInterfaceType()) 296926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Context.getObjCObjectPointerType(ReceiverType); 297926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // - if the receiver is of type Class or qualified Class type, 298926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // T is the declared return type of the method. 299926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (ReceiverType->isObjCClassType() || 300926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType->isObjCQualifiedClassType()) 301926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Method->getSendResultType(); 302926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 303926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // - if the receiver is id, qualified id, Class, or qualified Class, T 304926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // is the receiver type, otherwise 305926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // - T is the type of the receiver expression. 306926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return ReceiverType; 307926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor} 308926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 309926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorvoid Sema::EmitRelatedResultTypeNote(const Expr *E) { 310926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E = E->IgnoreParenImpCasts(); 311926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E); 312926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!MsgSend) 313926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return; 314926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 315926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCMethodDecl *Method = MsgSend->getMethodDecl(); 316926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method) 317926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return; 318926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 319926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method->hasRelatedResultType()) 320926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return; 321926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 322926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (Context.hasSameUnqualifiedType(Method->getResultType() 323926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor .getNonReferenceType(), 324926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor MsgSend->getType())) 325926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return; 326926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 327926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Diag(Method->getLocation(), diag::note_related_result_type_inferred) 328926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor << Method->isInstanceMethod() << Method->getSelector() 329926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor << MsgSend->getType(); 330926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor} 33126743b20e4a8c2a986e6453f0c38beba0afef633John McCall 332926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorbool Sema::CheckMessageArgumentTypes(QualType ReceiverType, 333926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Expr **Args, unsigned NumArgs, 3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Selector Sel, ObjCMethodDecl *Method, 335926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor bool isClassMessage, bool isSuperMessage, 336637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar SourceLocation lbrac, SourceLocation rbrac, 337f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall QualType &ReturnType, ExprValueKind &VK) { 338637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar if (!Method) { 3396660c8a4cc2115929d92be83bbc54c307002a321Daniel Dunbar // Apply default argument promotion as for (C99 6.5.2.2p6). 34092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor for (unsigned i = 0; i != NumArgs; i++) { 34192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor if (Args[i]->isTypeDependent()) 34292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor continue; 34392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor 344429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultArgumentPromotion(Args[i]); 345429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 346429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return true; 347429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Args[i] = Result.take(); 34892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor } 3496660c8a4cc2115929d92be83bbc54c307002a321Daniel Dunbar 350f85e193739c953358c865005855253af4f68a497John McCall unsigned DiagID; 351f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount) 352f85e193739c953358c865005855253af4f68a497John McCall DiagID = diag::err_arc_method_not_found; 353f85e193739c953358c865005855253af4f68a497John McCall else 354f85e193739c953358c865005855253af4f68a497John McCall DiagID = isClassMessage ? diag::warn_class_method_not_found 355f85e193739c953358c865005855253af4f68a497John McCall : diag::warn_inst_method_not_found; 356077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner Diag(lbrac, DiagID) 357077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner << Sel << isClassMessage << SourceRange(lbrac, rbrac); 35848218c60d6b53b7880917d1366ee716dec2145caJohn McCall 35948218c60d6b53b7880917d1366ee716dec2145caJohn McCall // In debuggers, we want to use __unknown_anytype for these 36048218c60d6b53b7880917d1366ee716dec2145caJohn McCall // results so that clients can cast them. 36148218c60d6b53b7880917d1366ee716dec2145caJohn McCall if (getLangOptions().DebuggerSupport) { 36248218c60d6b53b7880917d1366ee716dec2145caJohn McCall ReturnType = Context.UnknownAnyTy; 36348218c60d6b53b7880917d1366ee716dec2145caJohn McCall } else { 36448218c60d6b53b7880917d1366ee716dec2145caJohn McCall ReturnType = Context.getObjCIdType(); 36548218c60d6b53b7880917d1366ee716dec2145caJohn McCall } 366f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK = VK_RValue; 367637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar return false; 368637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar } 3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 370926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage, 371926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isSuperMessage); 372f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK = Expr::getValueKindForType(Method->getResultType()); 3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 37491e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar unsigned NumNamedArgs = Sel.getNumArgs(); 3754f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian // Method might have more arguments than selector indicates. This is due 3764f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian // to addition of c-style arguments in method. 3774f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian if (Method->param_size() > Sel.getNumArgs()) 3784f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian NumNamedArgs = Method->param_size(); 3794f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian // FIXME. This need be cleaned up. 3804f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian if (NumArgs < NumNamedArgs) { 381f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall Diag(lbrac, diag::err_typecheck_call_too_few_args) 382f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall << 2 << NumNamedArgs << NumArgs; 3834f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian return false; 3844f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian } 38591e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar 386312531a8cd69c562d5687bd69fd334be99d87320Chris Lattner bool IsError = false; 38791e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar for (unsigned i = 0; i < NumNamedArgs; i++) { 38892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor // We can't do any type-checking on a type-dependent argument. 38992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor if (Args[i]->isTypeDependent()) 39092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor continue; 39192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor 39285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner Expr *argExpr = Args[i]; 39392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor 394688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor ParmVarDecl *Param = Method->param_begin()[i]; 39585a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); 3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 397688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor if (RequireCompleteType(argExpr->getSourceRange().getBegin(), 398688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor Param->getType(), 399688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor PDiag(diag::err_call_incomplete_argument) 400688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor << argExpr->getSourceRange())) 401688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor return true; 402688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor 403745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 404745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian Param); 4053fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr)); 406688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor if (ArgE.isInvalid()) 407688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor IsError = true; 408688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor else 409688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor Args[i] = ArgE.takeAs<Expr>(); 41085a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner } 41191e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar 41291e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar // Promote additional arguments to variadic methods. 41391e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar if (Method->isVariadic()) { 41492e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor for (unsigned i = NumNamedArgs; i < NumArgs; ++i) { 41592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor if (Args[i]->isTypeDependent()) 41692e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor continue; 41792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor 418429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); 419429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley IsError |= Arg.isInvalid(); 420429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Args[i] = Arg.take(); 42192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor } 42291e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar } else { 42391e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar // Check for extra arguments to non-variadic methods. 42491e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar if (NumArgs != NumNamedArgs) { 4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(Args[NumNamedArgs]->getLocStart(), 426fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner diag::err_typecheck_call_too_many_args) 427ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << 2 /*method*/ << NumNamedArgs << NumArgs 428ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << Method->getSourceRange() 429fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner << SourceRange(Args[NumNamedArgs]->getLocStart(), 430fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner Args[NumArgs-1]->getLocEnd()); 43191e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar } 43291e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar } 4335272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian // diagnose nonnull arguments. 4345272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian for (specific_attr_iterator<NonNullAttr> 4355272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian i = Method->specific_attr_begin<NonNullAttr>(), 4365272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) { 4375272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian CheckNonNullArguments(*i, Args, lbrac); 4385272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian } 43991e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar 4402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs); 441312531a8cd69c562d5687bd69fd334be99d87320Chris Lattner return IsError; 44285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 44385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 444f85e193739c953358c865005855253af4f68a497John McCallbool Sema::isSelfExpr(Expr *receiver) { 445f2d74cc083e1a83838acf99d2e1ca260d1c54742Fariborz Jahanian // 'self' is objc 'self' in an objc method only. 446b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian DeclContext *DC = CurContext; 447b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian while (isa<BlockDecl>(DC)) 448b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian DC = DC->getParent(); 449b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian if (DC && !isa<ObjCMethodDecl>(DC)) 450f2d74cc083e1a83838acf99d2e1ca260d1c54742Fariborz Jahanian return false; 451f85e193739c953358c865005855253af4f68a497John McCall receiver = receiver->IgnoreParenLValueCasts(); 452f85e193739c953358c865005855253af4f68a497John McCall if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver)) 4536b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self")) 4546b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff return true; 4556b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff return false; 4566b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff} 4576b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff 458f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff// Helper method for ActOnClassMethod/ActOnInstanceMethod. 459f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff// Will search "local" class/category implementations for a method decl. 460175ba1e8180083927aabd7cc8137baa16be75646Fariborz Jahanian// If failed, then we search in class's root for an instance method. 461f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff// Returns 0 if no method is found. 4625609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve NaroffObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, 463f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff ObjCInterfaceDecl *ClassDecl) { 464f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff ObjCMethodDecl *Method = 0; 4655609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // lookup in class and all superclasses 4665609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff while (ClassDecl && !Method) { 46787018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 46817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Method = ImpDecl->getClassMethod(Sel); 4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4705609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // Look through local category implementations associated with the class. 4711cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis if (!Method) 4721cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis Method = ClassDecl->getCategoryClassMethod(Sel); 4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4745609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // Before we give up, check if the selector is an instance method. 4755609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // But only in the root. This matches gcc's behaviour and what the 4765609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // runtime expects. 4775609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff if (!Method && !ClassDecl->getSuperClass()) { 47817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Method = ClassDecl->lookupInstanceMethod(Sel); 4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Look through local category implementations associated 4805609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // with the root class. 4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (!Method) 4825609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff Method = LookupPrivateInstanceMethod(Sel, ClassDecl); 483f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff } 4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4855609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff ClassDecl = ClassDecl->getSuperClass(); 486f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff } 4875609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff return Method; 4885609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff} 4895609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff 4905609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve NaroffObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, 4915609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff ObjCInterfaceDecl *ClassDecl) { 4925609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff ObjCMethodDecl *Method = 0; 4935609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff while (ClassDecl && !Method) { 4945609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // If we have implementations in scope, check "private" methods. 49587018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 49617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Method = ImpDecl->getInstanceMethod(Sel); 4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4985609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff // Look through local category implementations associated with the class. 4991cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis if (!Method) 5001cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis Method = ClassDecl->getCategoryInstanceMethod(Sel); 5015609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff ClassDecl = ClassDecl->getSuperClass(); 502175ba1e8180083927aabd7cc8137baa16be75646Fariborz Jahanian } 503f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff return Method; 504f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff} 505f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff 50661478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian/// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier 50761478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian/// list of a qualified objective pointer type. 50861478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz JahanianObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel, 50961478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian const ObjCObjectPointerType *OPT, 51061478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian bool Instance) 51161478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian{ 51261478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian ObjCMethodDecl *MD = 0; 51361478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 51461478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian E = OPT->qual_end(); I != E; ++I) { 51561478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian ObjCProtocolDecl *PROTO = (*I); 51661478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian if ((MD = PROTO->lookupMethod(Sel, Instance))) { 51761478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian return MD; 51861478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian } 51961478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian } 52061478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian return 0; 52161478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian} 52261478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian 5237f81652f97a69ae8b514893a69c0245253687e55Chris Lattner/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an 5247f81652f97a69ae8b514893a69c0245253687e55Chris Lattner/// objective C interface. This is a property reference expression. 52560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema:: 5267f81652f97a69ae8b514893a69c0245253687e55Chris LattnerHandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, 5276326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian Expr *BaseExpr, SourceLocation OpLoc, 5286326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian DeclarationName MemberName, 5298ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian SourceLocation MemberLoc, 5308ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian SourceLocation SuperLoc, QualType SuperType, 5318ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian bool Super) { 5327f81652f97a69ae8b514893a69c0245253687e55Chris Lattner const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); 5337f81652f97a69ae8b514893a69c0245253687e55Chris Lattner ObjCInterfaceDecl *IFace = IFaceT->getDecl(); 534109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor 535109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor if (MemberName.getNameKind() != DeclarationName::Identifier) { 536109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor Diag(MemberLoc, diag::err_invalid_property_name) 537109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor << MemberName << QualType(OPT, 0); 538109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor return ExprError(); 539109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor } 540109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor 5417f81652f97a69ae8b514893a69c0245253687e55Chris Lattner IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); 5427f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 5438b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian if (IFace->isForwardDecl()) { 5448b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian Diag(MemberLoc, diag::err_property_not_found_forward_class) 5458b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian << MemberName << QualType(OPT, 0); 5468b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian Diag(IFace->getLocation(), diag::note_forward_class); 5478b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian return ExprError(); 5488b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian } 5497f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Search for a declared property first. 5507f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { 5517f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Check whether we can reference this property. 5527f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (DiagnoseUseOfDecl(PD, MemberLoc)) 5537f81652f97a69ae8b514893a69c0245253687e55Chris Lattner return ExprError(); 5547f81652f97a69ae8b514893a69c0245253687e55Chris Lattner QualType ResTy = PD->getType(); 55514086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian ResTy = ResTy.getNonLValueExprType(Context); 5567f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Selector Sel = PP.getSelectorTable().getNullarySelector(Member); 5577f81652f97a69ae8b514893a69c0245253687e55Chris Lattner ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); 558926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (Getter && 559926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor (Getter->hasRelatedResultType() 560926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor || DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))) 561926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ResTy = getMessageSendResultType(QualType(OPT, 0), Getter, false, 562926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Super); 563926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 5648ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian if (Super) 5658ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, 566f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK_LValue, OK_ObjCProperty, 5678ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian MemberLoc, 5688ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian SuperLoc, SuperType)); 5698ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian else 5708ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, 571f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK_LValue, OK_ObjCProperty, 5728ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian MemberLoc, BaseExpr)); 5737f81652f97a69ae8b514893a69c0245253687e55Chris Lattner } 5747f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Check protocols on qualified interfaces. 5757f81652f97a69ae8b514893a69c0245253687e55Chris Lattner for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 5767f81652f97a69ae8b514893a69c0245253687e55Chris Lattner E = OPT->qual_end(); I != E; ++I) 5777f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { 5787f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Check whether we can reference this property. 5797f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (DiagnoseUseOfDecl(PD, MemberLoc)) 5807f81652f97a69ae8b514893a69c0245253687e55Chris Lattner return ExprError(); 581926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 582926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor QualType T = PD->getType(); 583926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) 584926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor T = getMessageSendResultType(QualType(OPT, 0), Getter, false, Super); 5858ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian if (Super) 586926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Owned(new (Context) ObjCPropertyRefExpr(PD, T, 587f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK_LValue, 588f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall OK_ObjCProperty, 589f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall MemberLoc, 590f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall SuperLoc, SuperType)); 5918ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian else 592926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Owned(new (Context) ObjCPropertyRefExpr(PD, T, 593f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK_LValue, 594f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall OK_ObjCProperty, 5958ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian MemberLoc, 5968ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian BaseExpr)); 5977f81652f97a69ae8b514893a69c0245253687e55Chris Lattner } 5987f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // If that failed, look for an "implicit" property by seeing if the nullary 5997f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // selector is implemented. 6007f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 6017f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // FIXME: The logic for looking up nullary and unary selectors should be 6027f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // shared with the code in ActOnInstanceMessage. 6037f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 6047f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Selector Sel = PP.getSelectorTable().getNullarySelector(Member); 6057f81652f97a69ae8b514893a69c0245253687e55Chris Lattner ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); 60627569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian 60727569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian // May be founf in property's qualified list. 60827569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian if (!Getter) 60927569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian Getter = LookupMethodInQualifiedType(Sel, OPT, true); 6107f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 6117f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // If this reference is in an @implementation, check for 'private' methods. 6127f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (!Getter) 61374b2756bc1f1f5f7c189996fe7e4cd3efef70263Fariborz Jahanian Getter = IFace->lookupPrivateMethod(Sel); 6147f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 6157f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Look through local category implementations associated with the class. 6167f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (!Getter) 6177f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Getter = IFace->getCategoryInstanceMethod(Sel); 6187f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (Getter) { 6197f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Check if we can reference this property. 6207f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (DiagnoseUseOfDecl(Getter, MemberLoc)) 6217f81652f97a69ae8b514893a69c0245253687e55Chris Lattner return ExprError(); 6227f81652f97a69ae8b514893a69c0245253687e55Chris Lattner } 6237f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // If we found a getter then this may be a valid dot-reference, we 6247f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // will look for the matching setter, in case it is needed. 6257f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Selector SetterSel = 6267f81652f97a69ae8b514893a69c0245253687e55Chris Lattner SelectorTable::constructSetterName(PP.getIdentifierTable(), 6277f81652f97a69ae8b514893a69c0245253687e55Chris Lattner PP.getSelectorTable(), Member); 6287f81652f97a69ae8b514893a69c0245253687e55Chris Lattner ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); 62927569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian 63027569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian // May be founf in property's qualified list. 63127569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian if (!Setter) 63227569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian Setter = LookupMethodInQualifiedType(SetterSel, OPT, true); 63327569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian 6347f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (!Setter) { 6357f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // If this reference is in an @implementation, also check for 'private' 6367f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // methods. 63774b2756bc1f1f5f7c189996fe7e4cd3efef70263Fariborz Jahanian Setter = IFace->lookupPrivateMethod(SetterSel); 6387f81652f97a69ae8b514893a69c0245253687e55Chris Lattner } 6397f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Look through local category implementations associated with the class. 6407f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (!Setter) 6417f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Setter = IFace->getCategoryInstanceMethod(SetterSel); 64227569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian 6437f81652f97a69ae8b514893a69c0245253687e55Chris Lattner if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) 6447f81652f97a69ae8b514893a69c0245253687e55Chris Lattner return ExprError(); 6457f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 64699130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian if (Getter || Setter) { 64799130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian QualType PType; 64899130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian if (Getter) 649926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor PType = getMessageSendResultType(QualType(OPT, 0), Getter, false, Super); 65099130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian else { 65199130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian ParmVarDecl *ArgDecl = *Setter->param_begin(); 65299130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian PType = ArgDecl->getType(); 65399130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian } 65499130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian 6550943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall ExprValueKind VK = VK_LValue; 6560943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall ExprObjectKind OK = OK_ObjCProperty; 6570943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() && 6580943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall PType->isVoidType()) 6590943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall VK = VK_RValue, OK = OK_Ordinary; 6600943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall 6618ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian if (Super) 66212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, 66312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall PType, VK, OK, 66412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall MemberLoc, 66512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall SuperLoc, SuperType)); 6668ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian else 66712f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, 66812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall PType, VK, OK, 66912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall MemberLoc, BaseExpr)); 6708ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian 6717f81652f97a69ae8b514893a69c0245253687e55Chris Lattner } 6727f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 6737f81652f97a69ae8b514893a69c0245253687e55Chris Lattner // Attempt to correct for typos in property names. 674d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor TypoCorrection Corrected = CorrectTypo( 675d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, NULL, 676d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor NULL, IFace, false, CTC_NoKeywords, OPT); 677d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (ObjCPropertyDecl *Property = 678d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Corrected.getCorrectionDeclAs<ObjCPropertyDecl>()) { 679d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor DeclarationName TypoResult = Corrected.getCorrection(); 6807f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Diag(MemberLoc, diag::err_property_not_found_suggest) 681b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner << MemberName << QualType(OPT, 0) << TypoResult 682b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString()); 6837f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Diag(Property->getLocation(), diag::note_previous_decl) 6847f81652f97a69ae8b514893a69c0245253687e55Chris Lattner << Property->getDeclName(); 6856326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc, 6866326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian TypoResult, MemberLoc, 6878ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian SuperLoc, SuperType, Super); 6887f81652f97a69ae8b514893a69c0245253687e55Chris Lattner } 68941aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian ObjCInterfaceDecl *ClassDeclared; 69041aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian if (ObjCIvarDecl *Ivar = 69141aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian IFace->lookupInstanceVariable(Member, ClassDeclared)) { 69241aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian QualType T = Ivar->getType(); 69341aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian if (const ObjCObjectPointerType * OBJPT = 69441aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian T->getAsObjCInterfacePointerType()) { 69541aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); 69641aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian if (ObjCInterfaceDecl *IFace = IFaceT->getDecl()) 69741aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian if (IFace->isForwardDecl()) { 69841aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian Diag(MemberLoc, diag::err_property_not_as_forward_class) 6992a96bf5e66731bb54dff3e4aadfbbced83377530Fariborz Jahanian << MemberName << IFace; 70041aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian Diag(IFace->getLocation(), diag::note_forward_class); 70141aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian return ExprError(); 70241aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian } 70341aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian } 7046326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian Diag(MemberLoc, 7056326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian diag::err_ivar_access_using_property_syntax_suggest) 7066326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian << MemberName << QualType(OPT, 0) << Ivar->getDeclName() 7076326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian << FixItHint::CreateReplacement(OpLoc, "->"); 7086326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian return ExprError(); 70941aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian } 710b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner 7117f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Diag(MemberLoc, diag::err_property_not_found) 7127f81652f97a69ae8b514893a69c0245253687e55Chris Lattner << MemberName << QualType(OPT, 0); 71399130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian if (Setter) 7147f81652f97a69ae8b514893a69c0245253687e55Chris Lattner Diag(Setter->getLocation(), diag::note_getter_unavailable) 71599130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian << MemberName << BaseExpr->getSourceRange(); 7167f81652f97a69ae8b514893a69c0245253687e55Chris Lattner return ExprError(); 7177f81652f97a69ae8b514893a69c0245253687e55Chris Lattner} 7187f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 7197f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 7207f81652f97a69ae8b514893a69c0245253687e55Chris Lattner 72160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema:: 722eb483eb3ee80300f15d6d13573d82493c2194461Chris LattnerActOnClassPropertyRefExpr(IdentifierInfo &receiverName, 723eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner IdentifierInfo &propertyName, 724eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner SourceLocation receiverNameLoc, 725eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner SourceLocation propertyNameLoc) { 7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 727f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor IdentifierInfo *receiverNamePtr = &receiverName; 728c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, 729c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor receiverNameLoc); 730926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 731926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor bool IsSuper = false; 732eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner if (IFace == 0) { 733eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner // If the "receiver" is 'super' in a method, handle it as an expression-like 734eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner // property reference. 73526743b20e4a8c2a986e6453f0c38beba0afef633John McCall if (receiverNamePtr->isStr("super")) { 736926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor IsSuper = true; 737926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 73826743b20e4a8c2a986e6453f0c38beba0afef633John McCall if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) { 739eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner if (CurMethod->isInstanceMethod()) { 740eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner QualType T = 741eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner Context.getObjCInterfaceType(CurMethod->getClassInterface()); 742eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner T = Context.getObjCObjectPointerType(T); 743eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner 744eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(), 7456326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian /*BaseExpr*/0, 7466326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian SourceLocation()/*OpLoc*/, 7476326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian &propertyName, 7488ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian propertyNameLoc, 7498ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian receiverNameLoc, T, true); 750eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner } 751eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner 752eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner // Otherwise, if this is a class method, try dispatching to our 753eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner // superclass. 754eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner IFace = CurMethod->getClassInterface()->getSuperClass(); 755eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner } 75626743b20e4a8c2a986e6453f0c38beba0afef633John McCall } 757eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner 758eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner if (IFace == 0) { 759eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner Diag(receiverNameLoc, diag::err_expected_ident_or_lparen); 760eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner return ExprError(); 761eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner } 7628149a5786def747af783a9e3c22714bb7ab42b9cFariborz Jahanian } 7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 764eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner // Search for a declared property first. 76561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); 76617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); 76761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 76861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // If this reference is in an @implementation, check for 'private' methods. 76961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (!Getter) 77061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) 77161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) 77287018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 77317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Getter = ImpDecl->getClassMethod(Sel); 77461f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 77561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (Getter) { 77661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // FIXME: refactor/share with ActOnMemberReference(). 77761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // Check if we can reference this property. 77861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) 77961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff return ExprError(); 78061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff } 7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 78261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // Look for the matching setter, in case it is needed. 7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Selector SetterSel = 7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SelectorTable::constructSetterName(PP.getIdentifierTable(), 785fdc92b7877535e6264fe43cfbdc8f01e9b224f81Steve Naroff PP.getSelectorTable(), &propertyName); 7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 78717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); 78861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (!Setter) { 78961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // If this reference is in an @implementation, also check for 'private' 79061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // methods. 79161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) 79261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) 79387018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 79417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis Setter = ImpDecl->getClassMethod(SetterSel); 79561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff } 79661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff // Look through local category implementations associated with the class. 7971cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis if (!Setter) 7981cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis Setter = IFace->getCategoryClassMethod(SetterSel); 79961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 80061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) 80161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff return ExprError(); 80261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 80361f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff if (Getter || Setter) { 80461f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff QualType PType; 80561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 8060943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall ExprValueKind VK = VK_LValue; 8070943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall if (Getter) { 808926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor PType = getMessageSendResultType(Context.getObjCInterfaceType(IFace), 809926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Getter, true, 810926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor receiverNamePtr->isStr("super")); 8110943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall if (!getLangOptions().CPlusPlus && 8120943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall !PType.hasQualifiers() && PType->isVoidType()) 8130943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall VK = VK_RValue; 8140943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall } else { 81561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), 81661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff E = Setter->param_end(); PI != E; ++PI) 81761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff PType = (*PI)->getType(); 8180943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall VK = VK_LValue; 81961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff } 8200943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall 8210943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); 8220943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall 823926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (IsSuper) 824926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, 825926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor PType, VK, OK, 826926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor propertyNameLoc, 827926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor receiverNameLoc, 828926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Context.getObjCInterfaceType(IFace))); 829926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 83012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, 83112f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall PType, VK, OK, 83212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall propertyNameLoc, 83312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall receiverNameLoc, IFace)); 83461f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff } 83561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) 83661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff << &propertyName << Context.getObjCInterfaceType(IFace)); 83761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff} 83861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 83947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas GregorSema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, 8401569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor IdentifierInfo *Name, 84147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor SourceLocation NameLoc, 84247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor bool IsSuper, 8431569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor bool HasTrailingDot, 844b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ParsedType &ReceiverType) { 845b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ReceiverType = ParsedType(); 8461569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor 84747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor // If the identifier is "super" and there is no trailing dot, we're 84895f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor // messaging super. If the identifier is "super" and there is a 84995f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor // trailing dot, it's an instance message. 85095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor if (IsSuper && S->isInObjcMethodScope()) 85195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage; 85247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor 85347bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); 85447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor LookupName(Result, S); 85547bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor 85647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor switch (Result.getResultKind()) { 85747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor case LookupResult::NotFound: 858ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor // Normal name lookup didn't find anything. If we're in an 859ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor // Objective-C method, look for ivars. If we find one, we're done! 86095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor // FIXME: This is a hack. Ivar lookup should be part of normal 86195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor // lookup. 862ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor if (ObjCMethodDecl *Method = getCurMethodDecl()) { 863ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor ObjCInterfaceDecl *ClassDeclared; 864ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor if (Method->getClassInterface()->lookupInstanceVariable(Name, 865ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor ClassDeclared)) 866ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor return ObjCInstanceMessage; 867ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor } 86895f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor 86947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor // Break out; we'll perform typo correction below. 87047bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor break; 87147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor 87247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor case LookupResult::NotFoundInCurrentInstantiation: 87347bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor case LookupResult::FoundOverloaded: 87447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor case LookupResult::FoundUnresolvedValue: 87547bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor case LookupResult::Ambiguous: 87647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor Result.suppressDiagnostics(); 87747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor return ObjCInstanceMessage; 87847bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor 87947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor case LookupResult::Found: { 8808348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian // If the identifier is a class or not, and there is a trailing dot, 8818348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian // it's an instance message. 8828348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian if (HasTrailingDot) 8838348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian return ObjCInstanceMessage; 88447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor // We found something. If it's a type, then we have a class 88547bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor // message. Otherwise, it's an instance message. 88647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor NamedDecl *ND = Result.getFoundDecl(); 8871569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor QualType T; 8881569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) 8891569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor T = Context.getObjCInterfaceType(Class); 8901569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) 8911569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor T = Context.getTypeDeclType(Type); 8921569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor else 8931569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor return ObjCInstanceMessage; 8941569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor 8951569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor // We have a class message, and T is the type we're 8961569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor // messaging. Build source-location information for it. 8971569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); 898b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ReceiverType = CreateParsedType(T, TSInfo); 8991569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor return ObjCClassMessage; 90047bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor } 90147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor } 90247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor 903aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor // Determine our typo-correction context. 904aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor CorrectTypoContext CTC = CTC_Expression; 905aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor if (ObjCMethodDecl *Method = getCurMethodDecl()) 906aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor if (Method->getClassInterface() && 907aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor Method->getClassInterface()->getSuperClass()) 908aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor CTC = CTC_ObjCMessageReceiver; 909aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor 910d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), 911d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Result.getLookupKind(), S, NULL, 912d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor NULL, false, CTC)) { 913d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor if (NamedDecl *ND = Corrected.getCorrectionDecl()) { 914aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor // If we found a declaration, correct when it refers to an Objective-C 915aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor // class. 9161569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) { 917aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor Diag(NameLoc, diag::err_unknown_receiver_suggest) 918d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor << Name << Corrected.getCorrection() 919aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor << FixItHint::CreateReplacement(SourceRange(NameLoc), 920aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor ND->getNameAsString()); 921aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor Diag(ND->getLocation(), diag::note_previous_decl) 922d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor << Corrected.getCorrection(); 923aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor 9241569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor QualType T = Context.getObjCInterfaceType(Class); 9251569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); 926b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall ReceiverType = CreateParsedType(T, TSInfo); 927aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor return ObjCClassMessage; 928aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor } 929d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor } else if (Corrected.isKeyword() && 930d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Corrected.getCorrectionAsIdentifierInfo()->isStr("super")) { 931aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor // If we've found the keyword "super", this is a send to super. 93247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor Diag(NameLoc, diag::err_unknown_receiver_suggest) 933d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor << Name << Corrected.getCorrection() 934aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor << FixItHint::CreateReplacement(SourceRange(NameLoc), "super"); 935aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor return ObjCSuperMessage; 93647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor } 93747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor } 93847bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor 93947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor // Fall back: let the parser try to parse it as an instance message. 94047bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor return ObjCInstanceMessage; 94147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor} 94261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff 94360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnSuperMessage(Scope *S, 9440fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation SuperLoc, 9450fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor Selector Sel, 9460fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation LBracLoc, 9470fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation SelectorLoc, 9480fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation RBracLoc, 9490fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor MultiExprArg Args) { 9502725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Determine whether we are inside a method or not. 95126743b20e4a8c2a986e6453f0c38beba0afef633John McCall ObjCMethodDecl *Method = tryCaptureObjCSelf(); 952f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor if (!Method) { 953f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); 954f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor return ExprError(); 955f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor } 9562725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor 957f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor ObjCInterfaceDecl *Class = Method->getClassInterface(); 958f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor if (!Class) { 959f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor Diag(SuperLoc, diag::error_no_super_class_message) 960f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor << Method->getDeclName(); 9612725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 96215faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner } 9632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor 964f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor ObjCInterfaceDecl *Super = Class->getSuperClass(); 9652725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (!Super) { 966f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // The current class does not have a superclass. 967e00909a6e997ec33d9baa5312e9d27b52a3da770Ted Kremenek Diag(SuperLoc, diag::error_root_class_cannot_use_super) 968e00909a6e997ec33d9baa5312e9d27b52a3da770Ted Kremenek << Class->getIdentifier(); 9692725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 9702725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor } 9712725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor 972f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // We are in a method whose class has a superclass, so 'super' 973f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // is acting as a keyword. 974f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor if (Method->isInstanceMethod()) { 975f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // Since we are in an instance method, this is an instance 976f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // message to the superclass instance. 977f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor QualType SuperTy = Context.getObjCInterfaceType(Super); 978f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor SuperTy = Context.getObjCObjectPointerType(SuperTy); 9799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return BuildInstanceMessage(0, SuperTy, SuperLoc, 980f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Sel, /*Method=*/0, 981f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis LBracLoc, SelectorLoc, RBracLoc, move(Args)); 9822725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor } 983f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor 984f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // Since we are in a class method, this is a class message to 985f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor // the superclass. 986f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor return BuildClassMessage(/*ReceiverTypeInfo=*/0, 987f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor Context.getObjCInterfaceType(Super), 988f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SuperLoc, Sel, /*Method=*/0, 989f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis LBracLoc, SelectorLoc, RBracLoc, move(Args)); 9902725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor} 9912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor 9922725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Build an Objective-C class message expression. 9932725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 9942725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine takes care of both normal class messages and 9952725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class messages to the superclass. 9962725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 9972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverTypeInfo Type source information that describes the 9982725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// receiver of this message. This may be NULL, in which case we are 9992725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// sending to the superclass and \p SuperLoc must be a valid source 10002725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// location. 10012725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor 10022725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType The type of the object receiving the 10032725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// message. When \p ReceiverTypeInfo is non-NULL, this is the same 10042725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// type as that refers to. For a superclass send, this is the type of 10052725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// the superclass. 10062725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 10072725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc The location of the "super" keyword in a 10082725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// superclass message. 10092725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 10102725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Sel The selector to which the message is being sent. 10112725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 1012f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// \param Method The method that this class message is invoking, if 1013f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// already known. 1014f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// 10152725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening square bracket ']'. 10162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 10172725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param RBrac The location of the closing square bracket ']'. 10182725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 10192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Args The message arguments. 102060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, 10210fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor QualType ReceiverType, 10220fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation SuperLoc, 10230fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor Selector Sel, 10240fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor ObjCMethodDecl *Method, 10250fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation LBracLoc, 1026f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SourceLocation SelectorLoc, 10270fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation RBracLoc, 10280fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor MultiExprArg ArgsIn) { 10290fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation Loc = SuperLoc.isValid()? SuperLoc 10309497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); 10310fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor if (LBracLoc.isInvalid()) { 10320fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor Diag(Loc, diag::err_missing_open_square_message_send) 10330fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor << FixItHint::CreateInsertion(Loc, "["); 10340fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor LBracLoc = Loc; 10350fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor } 10360fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor 103792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor if (ReceiverType->isDependentType()) { 103892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor // If the receiver type is dependent, we can't type-check anything 103992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor // at this point. Build a dependent expression. 104092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor unsigned NumArgs = ArgsIn.size(); 104192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); 104292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor assert(SuperLoc.isInvalid() && "Message to super with dependent type"); 1043f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall return Owned(ObjCMessageExpr::Create(Context, ReceiverType, 1044f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK_RValue, LBracLoc, ReceiverTypeInfo, 1045f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Sel, SelectorLoc, /*Method=*/0, 104692e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor Args, NumArgs, RBracLoc)); 104792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor } 104815faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner 10492725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Find the class to which we are sending this message. 10502725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor ObjCInterfaceDecl *Class = 0; 1051c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); 1052c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall if (!ClassType || !(Class = ClassType->getInterface())) { 10532725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor Diag(Loc, diag::err_invalid_receiver_class_message) 10542725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor << ReceiverType; 10552725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 10567c778f1c549a8ae95d50a819fd537df78da16426Steve Naroff } 10572725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor assert(Class && "We don't know which class we're messaging?"); 105802b0d651ecd6a4d5171dd0208c0c570c3d7103b7Fariborz Jahanian (void)DiagnoseUseOfDecl(Class, Loc); 10592725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Find the method we are messaging. 1060f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method) { 1061f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (Class->isForwardDecl()) { 1062f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount) { 1063f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_receiver_forward_class) << ReceiverType; 1064f85e193739c953358c865005855253af4f68a497John McCall } else { 1065f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName(); 1066f85e193739c953358c865005855253af4f68a497John McCall } 1067f85e193739c953358c865005855253af4f68a497John McCall 1068f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // A forward class used in messaging is treated as a 'Class' 1069f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = LookupFactoryMethodInGlobalPool(Sel, 1070f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor SourceRange(LBracLoc, RBracLoc)); 1071f85e193739c953358c865005855253af4f68a497John McCall if (Method && !getLangOptions().ObjCAutoRefCount) 1072f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Diag(Method->getLocation(), diag::note_method_sent_forward_class) 1073f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor << Method->getDeclName(); 1074f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } 1075f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method) 1076f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = Class->lookupClassMethod(Sel); 10771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1078f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // If we have an implementation in scope, check "private" methods. 1079f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method) 1080f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = LookupPrivateClassMethod(Sel, Class); 10817c778f1c549a8ae95d50a819fd537df78da16426Steve Naroff 1082f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (Method && DiagnoseUseOfDecl(Method, Loc)) 1083f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor return ExprError(); 1084f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } 10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10862725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Check the argument types and determine the result type. 10872725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor QualType ReturnType; 1088f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall ExprValueKind VK = VK_RValue; 1089f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall 10902725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor unsigned NumArgs = ArgsIn.size(); 10912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); 1092926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, true, 1093926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor SuperLoc.isValid(), LBracLoc, RBracLoc, 1094926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReturnType, VK)) 10952725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 10964df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek 1097483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor if (Method && !Method->getResultType()->isVoidType() && 1098483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor RequireCompleteType(LBracLoc, Method->getResultType(), 1099483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor diag::err_illegal_message_expr_incomplete_type)) 1100483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor return ExprError(); 1101483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor 11022725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Construct the appropriate ObjCMessageExpr. 11032d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor Expr *Result; 11042725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (SuperLoc.isValid()) 1105f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, 11062d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor SuperLoc, /*IsInstanceSuper=*/false, 1107f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis ReceiverType, Sel, SelectorLoc, 1108f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Method, Args, NumArgs, RBracLoc); 11092d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor else 1110f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, 1111f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis ReceiverTypeInfo, Sel, SelectorLoc, 1112f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Method, Args, NumArgs, RBracLoc); 11132d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor return MaybeBindToTemporary(Result); 111485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 111585a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner 11162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// ActOnClassMessage - used for both unary and keyword messages. 111785a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// ArgExprs is optional - if it is present, the number of expressions 111885a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// is obtained from Sel.getNumArgs(). 111960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnClassMessage(Scope *S, 112077328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor ParsedType Receiver, 112177328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor Selector Sel, 112277328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor SourceLocation LBracLoc, 112377328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor SourceLocation SelectorLoc, 112477328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor SourceLocation RBracLoc, 112577328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor MultiExprArg Args) { 11262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor TypeSourceInfo *ReceiverTypeInfo; 11272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); 11282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (ReceiverType.isNull()) 11292725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (!ReceiverTypeInfo) 11332725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); 11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return BuildClassMessage(ReceiverTypeInfo, ReceiverType, 1136f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, 1137f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis LBracLoc, SelectorLoc, RBracLoc, move(Args)); 11382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor} 113987d3ef08d892df8264bd51adb6ddd4a22422cd29Steve Naroff 11402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Build an Objective-C instance message expression. 11412725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11422725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine takes care of both normal instance messages and 11432725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// instance messages to the superclass instance. 11442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11452725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Receiver The expression that computes the object that will 11462725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// receive this message. This may be empty, in which case we are 11472725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// sending to the superclass instance and \p SuperLoc must be a valid 11482725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// source location. 11492725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11502725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType The (static) type of the object receiving the 11512725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// message. When a \p Receiver expression is provided, this is the 11522725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// same type as that expression. For a superclass instance send, this 11532725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// is a pointer to the type of the superclass. 11542725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11552725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc The location of the "super" keyword in a 11562725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// superclass instance message. 11572725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11582725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Sel The selector to which the message is being sent. 11592725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 1160f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// \param Method The method that this instance message is invoking, if 1161f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// already known. 1162f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// 11632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening square bracket ']'. 11642725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11652725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param RBrac The location of the closing square bracket ']'. 11662725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 11672725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Args The message arguments. 116860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::BuildInstanceMessage(Expr *Receiver, 1169f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis QualType ReceiverType, 1170f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SourceLocation SuperLoc, 1171f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Selector Sel, 1172f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis ObjCMethodDecl *Method, 1173f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SourceLocation LBracLoc, 1174f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SourceLocation SelectorLoc, 1175f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SourceLocation RBracLoc, 1176f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis MultiExprArg ArgsIn) { 11770fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor // The location of the receiver. 11780fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart(); 11790fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor 11800fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor if (LBracLoc.isInvalid()) { 11810fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor Diag(Loc, diag::err_missing_open_square_message_send) 11820fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor << FixItHint::CreateInsertion(Loc, "["); 11830fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor LBracLoc = Loc; 11840fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor } 11850fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor 11862725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // If we have a receiver expression, perform appropriate promotions 11872725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // and determine receiver type. 11882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (Receiver) { 118992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor if (Receiver->isTypeDependent()) { 119092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor // If the receiver is type-dependent, we can't type-check anything 119192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor // at this point. Build a dependent expression. 119292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor unsigned NumArgs = ArgsIn.size(); 119392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); 119492e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor assert(SuperLoc.isInvalid() && "Message to super with dependent type"); 119592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy, 1196f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall VK_RValue, LBracLoc, Receiver, Sel, 1197f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis SelectorLoc, /*Method=*/0, 1198f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Args, NumArgs, RBracLoc)); 119992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor } 120092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor 12012725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // If necessary, apply function/array conversion to the receiver. 12022725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // C99 6.7.5.3p[7,8]. 1203429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver); 1204429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 1205429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 1206429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Receiver = Result.take(); 12072725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor ReceiverType = Receiver->getType(); 12082725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor } 120904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 1210f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method) { 1211f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // Handle messages to id. 1212ba551983016ee3eac5421255d2ebe6723e61befbFariborz Jahanian bool receiverIsId = ReceiverType->isObjCIdType(); 12136b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian if (receiverIsId || ReceiverType->isBlockPointerType() || 1214f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { 1215f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = LookupInstanceMethodInGlobalPool(Sel, 12166b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian SourceRange(LBracLoc, RBracLoc), 12176b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian receiverIsId); 1218f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method) 12192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor Method = LookupFactoryMethodInGlobalPool(Sel, 12206b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian SourceRange(LBracLoc, RBracLoc), 12216b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian receiverIsId); 1222f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } else if (ReceiverType->isObjCClassType() || 1223f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor ReceiverType->isObjCQualifiedClassType()) { 1224f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // Handle messages to Class. 1225759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // We allow sending a message to a qualified Class ("Class<foo>"), which 1226759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // is ok as long as one of the protocols implements the selector (if not, warn). 1227759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (const ObjCObjectPointerType *QClassTy 1228759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian = ReceiverType->getAsObjCQualifiedClassType()) { 1229759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // Search protocols for class methods. 1230759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Method = LookupMethodInQualifiedType(Sel, QClassTy, false); 1231759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (!Method) { 1232759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Method = LookupMethodInQualifiedType(Sel, QClassTy, true); 1233759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // warn if instance method found for a Class message. 1234759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (Method) { 1235759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Diag(Loc, diag::warn_instance_method_on_class_found) 1236759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian << Method->getSelector() << Sel; 1237759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Diag(Method->getLocation(), diag::note_method_declared_at); 1238759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian } 12396b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff } 1240759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian } else { 1241759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { 1242759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { 1243759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // First check the public methods in the class interface. 1244759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Method = ClassDecl->lookupClassMethod(Sel); 1245759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian 1246759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (!Method) 1247759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Method = LookupPrivateClassMethod(Sel, ClassDecl); 1248759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian } 1249759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (Method && DiagnoseUseOfDecl(Method, Loc)) 1250759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian return ExprError(); 1251759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian } 1252759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (!Method) { 1253759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // If not messaging 'self', look for any factory method named 'Sel'. 1254759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (!Receiver || !isSelfExpr(Receiver)) { 1255759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Method = LookupFactoryMethodInGlobalPool(Sel, 1256759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian SourceRange(LBracLoc, RBracLoc), 1257759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian true); 1258759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (!Method) { 1259759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // If no class (factory) method was found, check if an _instance_ 1260759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian // method of the same name exists in the root class only. 1261759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Method = LookupInstanceMethodInGlobalPool(Sel, 12626b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian SourceRange(LBracLoc, RBracLoc), 1263759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian true); 1264759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (Method) 1265759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (const ObjCInterfaceDecl *ID = 1266759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { 1267759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian if (ID->getSuperClass()) 1268759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian Diag(Loc, diag::warn_root_inst_method_not_found) 1269759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian << Sel << SourceRange(LBracLoc, RBracLoc); 1270759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian } 1271759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian } 127204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 127304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 127404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 127504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } else { 1276f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor ObjCInterfaceDecl* ClassDecl = 0; 1277f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor 1278f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // We allow sending a message to a qualified ID ("id<foo>"), which is ok as 1279f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // long as one of the protocols implements the selector (if not, warn). 1280f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (const ObjCObjectPointerType *QIdTy 1281f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor = ReceiverType->getAsObjCQualifiedIdType()) { 1282f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // Search protocols for instance methods. 128327569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian Method = LookupMethodInQualifiedType(Sel, QIdTy, true); 128427569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian if (!Method) 128527569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian Method = LookupMethodInQualifiedType(Sel, QIdTy, false); 1286f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } else if (const ObjCObjectPointerType *OCIType 1287f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor = ReceiverType->getAsObjCInterfacePointerType()) { 1288f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // We allow sending a message to a pointer to an interface (an object). 1289f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor ClassDecl = OCIType->getInterfaceDecl(); 1290f85e193739c953358c865005855253af4f68a497John McCall 1291f85e193739c953358c865005855253af4f68a497John McCall if (ClassDecl->isForwardDecl() && getLangOptions().ObjCAutoRefCount) { 1292f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_receiver_forward_instance) 1293f85e193739c953358c865005855253af4f68a497John McCall << OCIType->getPointeeType() 1294f85e193739c953358c865005855253af4f68a497John McCall << (Receiver ? Receiver->getSourceRange() : SourceRange(SuperLoc)); 1295f85e193739c953358c865005855253af4f68a497John McCall return ExprError(); 1296f85e193739c953358c865005855253af4f68a497John McCall } 1297f85e193739c953358c865005855253af4f68a497John McCall 1298f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be 1299f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // faster than the following method (which can do *many* linear searches). 1300db9d2145f1d85f64dba2c9b92416621ce01090a6Sebastian Redl // The idea is to add class info to MethodPool. 1301f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = ClassDecl->lookupInstanceMethod(Sel); 1302f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor 130327569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian if (!Method) 1304f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // Search protocol qualifiers. 130527569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian Method = LookupMethodInQualifiedType(Sel, OCIType, true); 130627569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian 130789ebaed91cca7fd296ec7804e4e9fb68949c1d0eFariborz Jahanian const ObjCInterfaceDecl *forwardClass = 0; 1308f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method) { 1309f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // If we have implementations in scope, check "private" methods. 1310f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = LookupPrivateInstanceMethod(Sel, ClassDecl); 1311f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor 1312f85e193739c953358c865005855253af4f68a497John McCall if (!Method && getLangOptions().ObjCAutoRefCount) { 1313f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_may_not_respond) 1314f85e193739c953358c865005855253af4f68a497John McCall << OCIType->getPointeeType() << Sel; 1315f85e193739c953358c865005855253af4f68a497John McCall return ExprError(); 1316f85e193739c953358c865005855253af4f68a497John McCall } 1317f85e193739c953358c865005855253af4f68a497John McCall 1318f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (!Method && (!Receiver || !isSelfExpr(Receiver))) { 1319f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // If we still haven't found a method, look in the global pool. This 1320f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // behavior isn't very desirable, however we need it for GCC 1321f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // compatibility. FIXME: should we deviate?? 1322f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (OCIType->qual_empty()) { 1323f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Method = LookupInstanceMethodInGlobalPool(Sel, 13248e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian SourceRange(LBracLoc, RBracLoc)); 132589ebaed91cca7fd296ec7804e4e9fb68949c1d0eFariborz Jahanian if (OCIType->getInterfaceDecl()->isForwardDecl()) 132689ebaed91cca7fd296ec7804e4e9fb68949c1d0eFariborz Jahanian forwardClass = OCIType->getInterfaceDecl(); 13278e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian if (Method && !forwardClass) 1328f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Diag(Loc, diag::warn_maynot_respond) 1329f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor << OCIType->getInterfaceDecl()->getIdentifier() << Sel; 1330f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } 1331f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } 1332f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } 13338e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass)) 1334f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor return ExprError(); 1335f85e193739c953358c865005855253af4f68a497John McCall } else if (!getLangOptions().ObjCAutoRefCount && 1336f85e193739c953358c865005855253af4f68a497John McCall !Context.getObjCIdType().isNull() && 1337f60946222721d9ba3c059563935c17b84703187aDouglas Gregor (ReceiverType->isPointerType() || 1338f60946222721d9ba3c059563935c17b84703187aDouglas Gregor ReceiverType->isIntegerType())) { 1339f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor // Implicitly convert integers and pointers to 'id' but emit a warning. 1340f85e193739c953358c865005855253af4f68a497John McCall // But not in ARC. 1341f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor Diag(Loc, diag::warn_bad_receiver_type) 1342f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor << ReceiverType 1343f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor << Receiver->getSourceRange(); 1344f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor if (ReceiverType->isPointerType()) 1345429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), 1346429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CK_BitCast).take(); 1347404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall else { 1348404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall // TODO: specialized warning on null receivers? 1349404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall bool IsNull = Receiver->isNullPointerConstant(Context, 1350404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall Expr::NPC_ValueDependentIsNull); 1351429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), 1352429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley IsNull ? CK_NullToPointer : CK_IntegralToPointer).take(); 1353404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall } 1354f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor ReceiverType = Receiver->getType(); 135579d3f04688497857d29fe1f009bbc7b4b830a21bFariborz Jahanian } 1356429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley else { 1357429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult ReceiverRes; 1358429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (getLangOptions().CPlusPlus) 1359429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ReceiverRes = PerformContextuallyConvertToObjCId(Receiver); 1360429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (ReceiverRes.isUsable()) { 1361429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Receiver = ReceiverRes.take(); 1362429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) { 1363429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Receiver = ICE->getSubExpr(); 1364429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ReceiverType = Receiver->getType(); 1365429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } 1366429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return BuildInstanceMessage(Receiver, 1367429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ReceiverType, 1368429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SuperLoc, 1369429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Sel, 1370429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Method, 1371429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley LBracLoc, 1372429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley SelectorLoc, 1373429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RBracLoc, 1374429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley move(ArgsIn)); 1375429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley } else { 1376429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley // Reject other random receiver types (e.g. structs). 1377429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Diag(Loc, diag::err_bad_receiver_type) 1378429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << ReceiverType << Receiver->getSourceRange(); 1379429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 13803ba606199be8056ae83596260bd6fd5872942905Fariborz Jahanian } 1381f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor } 138204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 1383fe1a553256b46fad3c0a9e9967481bcf571339ecChris Lattner } 13841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13852725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Check the message arguments. 13862725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor unsigned NumArgs = ArgsIn.size(); 13872725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); 13882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor QualType ReturnType; 1389f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall ExprValueKind VK = VK_RValue; 13902600503170c8366af2587408c50e2beedee5d1f1Fariborz Jahanian bool ClassMessage = (ReceiverType->isObjCClassType() || 13912600503170c8366af2587408c50e2beedee5d1f1Fariborz Jahanian ReceiverType->isObjCQualifiedClassType()); 1392926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, 1393926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ClassMessage, SuperLoc.isValid(), 1394f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall LBracLoc, RBracLoc, ReturnType, VK)) 13952725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 1396da59e09f1fd53350fcd949adbadd8148117f21b2Fariborz Jahanian 1397483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor if (Method && !Method->getResultType()->isVoidType() && 1398483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor RequireCompleteType(LBracLoc, Method->getResultType(), 1399483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor diag::err_illegal_message_expr_incomplete_type)) 1400483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor return ExprError(); 140104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 1402f85e193739c953358c865005855253af4f68a497John McCall // In ARC, forbid the user from sending messages to 1403f85e193739c953358c865005855253af4f68a497John McCall // retain/release/autorelease/dealloc/retainCount explicitly. 1404f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount) { 1405f85e193739c953358c865005855253af4f68a497John McCall ObjCMethodFamily family = 1406f85e193739c953358c865005855253af4f68a497John McCall (Method ? Method->getMethodFamily() : Sel.getMethodFamily()); 1407f85e193739c953358c865005855253af4f68a497John McCall switch (family) { 1408f85e193739c953358c865005855253af4f68a497John McCall case OMF_init: 1409f85e193739c953358c865005855253af4f68a497John McCall if (Method) 1410f85e193739c953358c865005855253af4f68a497John McCall checkInitMethod(Method, ReceiverType); 1411f85e193739c953358c865005855253af4f68a497John McCall 1412f85e193739c953358c865005855253af4f68a497John McCall case OMF_None: 1413f85e193739c953358c865005855253af4f68a497John McCall case OMF_alloc: 1414f85e193739c953358c865005855253af4f68a497John McCall case OMF_copy: 1415f85e193739c953358c865005855253af4f68a497John McCall case OMF_mutableCopy: 1416f85e193739c953358c865005855253af4f68a497John McCall case OMF_new: 1417f85e193739c953358c865005855253af4f68a497John McCall case OMF_self: 1418f85e193739c953358c865005855253af4f68a497John McCall break; 1419f85e193739c953358c865005855253af4f68a497John McCall 1420f85e193739c953358c865005855253af4f68a497John McCall case OMF_dealloc: 1421f85e193739c953358c865005855253af4f68a497John McCall case OMF_retain: 1422f85e193739c953358c865005855253af4f68a497John McCall case OMF_release: 1423f85e193739c953358c865005855253af4f68a497John McCall case OMF_autorelease: 1424f85e193739c953358c865005855253af4f68a497John McCall case OMF_retainCount: 1425f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_illegal_explicit_message) 1426f85e193739c953358c865005855253af4f68a497John McCall << Sel << SelectorLoc; 1427f85e193739c953358c865005855253af4f68a497John McCall break; 14289670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian 14299670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_performSelector: 14309670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian if (Method && NumArgs >= 1) { 14319670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) { 14329670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Selector ArgSel = SelExp->getSelector(); 14339670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian ObjCMethodDecl *SelMethod = 14349670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian LookupInstanceMethodInGlobalPool(ArgSel, 14359670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian SelExp->getSourceRange()); 14369670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian if (!SelMethod) 14379670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian SelMethod = 14389670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian LookupFactoryMethodInGlobalPool(ArgSel, 14399670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian SelExp->getSourceRange()); 14409670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian if (SelMethod) { 14419670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian ObjCMethodFamily SelFamily = SelMethod->getMethodFamily(); 14429670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian switch (SelFamily) { 14439670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_alloc: 14449670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_copy: 14459670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_mutableCopy: 14469670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_new: 14479670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_self: 14489670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian case OMF_init: 14499670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian // Issue error, unless ns_returns_not_retained. 14509670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) { 14519670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian // selector names a +1 method 14529670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Diag(SelectorLoc, 14539670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian diag::err_arc_perform_selector_retains); 14549670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Diag(SelMethod->getLocation(), diag::note_method_declared_at); 14559670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } 14569670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian break; 14579670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian default: 14589670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian // +0 call. OK. unless ns_returns_retained. 14599670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) { 14609670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian // selector names a +1 method 14619670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Diag(SelectorLoc, 14629670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian diag::err_arc_perform_selector_retains); 14639670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Diag(SelMethod->getLocation(), diag::note_method_declared_at); 14649670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } 14659670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian break; 14669670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } 14679670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } 14689670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } else { 14699670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian // error (may leak). 14709670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Diag(SelectorLoc, diag::warn_arc_perform_selector_leaks); 14719670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian Diag(Args[0]->getExprLoc(), diag::note_used_here); 14729670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } 14739670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian } 14749670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian break; 1475f85e193739c953358c865005855253af4f68a497John McCall } 1476f85e193739c953358c865005855253af4f68a497John McCall } 1477f85e193739c953358c865005855253af4f68a497John McCall 14782725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor // Construct the appropriate ObjCMessageExpr instance. 1479f85e193739c953358c865005855253af4f68a497John McCall ObjCMessageExpr *Result; 14802725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (SuperLoc.isValid()) 1481f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, 14822d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor SuperLoc, /*IsInstanceSuper=*/true, 1483f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis ReceiverType, Sel, SelectorLoc, Method, 14842d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor Args, NumArgs, RBracLoc); 14852d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor else 1486f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, 1487f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Receiver, Sel, SelectorLoc, Method, 1488f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis Args, NumArgs, RBracLoc); 1489f85e193739c953358c865005855253af4f68a497John McCall 1490f85e193739c953358c865005855253af4f68a497John McCall if (getLangOptions().ObjCAutoRefCount) { 1491f85e193739c953358c865005855253af4f68a497John McCall // In ARC, annotate delegate init calls. 1492f85e193739c953358c865005855253af4f68a497John McCall if (Result->getMethodFamily() == OMF_init && 1493f85e193739c953358c865005855253af4f68a497John McCall (SuperLoc.isValid() || isSelfExpr(Receiver))) { 1494f85e193739c953358c865005855253af4f68a497John McCall // Only consider init calls *directly* in init implementations, 1495f85e193739c953358c865005855253af4f68a497John McCall // not within blocks. 1496f85e193739c953358c865005855253af4f68a497John McCall ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext); 1497f85e193739c953358c865005855253af4f68a497John McCall if (method && method->getMethodFamily() == OMF_init) { 1498f85e193739c953358c865005855253af4f68a497John McCall // The implicit assignment to self means we also don't want to 1499f85e193739c953358c865005855253af4f68a497John McCall // consume the result. 1500f85e193739c953358c865005855253af4f68a497John McCall Result->setDelegateInitCall(true); 1501f85e193739c953358c865005855253af4f68a497John McCall return Owned(Result); 1502f85e193739c953358c865005855253af4f68a497John McCall } 1503f85e193739c953358c865005855253af4f68a497John McCall } 1504f85e193739c953358c865005855253af4f68a497John McCall 1505f85e193739c953358c865005855253af4f68a497John McCall // In ARC, check for message sends which are likely to introduce 1506f85e193739c953358c865005855253af4f68a497John McCall // retain cycles. 1507f85e193739c953358c865005855253af4f68a497John McCall checkRetainCycles(Result); 1508f85e193739c953358c865005855253af4f68a497John McCall } 1509f85e193739c953358c865005855253af4f68a497John McCall 15102d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor return MaybeBindToTemporary(Result); 15112725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor} 15122725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor 15132725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// ActOnInstanceMessage - used for both unary and keyword messages. 15142725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// ArgExprs is optional - if it is present, the number of expressions 15152725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// is obtained from Sel.getNumArgs(). 151660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnInstanceMessage(Scope *S, 151760d7b3a319d84d688752be3870615ac0f111fb16John McCall Expr *Receiver, 151860d7b3a319d84d688752be3870615ac0f111fb16John McCall Selector Sel, 151960d7b3a319d84d688752be3870615ac0f111fb16John McCall SourceLocation LBracLoc, 152060d7b3a319d84d688752be3870615ac0f111fb16John McCall SourceLocation SelectorLoc, 152160d7b3a319d84d688752be3870615ac0f111fb16John McCall SourceLocation RBracLoc, 152260d7b3a319d84d688752be3870615ac0f111fb16John McCall MultiExprArg Args) { 15232725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor if (!Receiver) 15242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor return ExprError(); 152504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 15269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return BuildInstanceMessage(Receiver, Receiver->getType(), 1527f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, 1528f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis LBracLoc, SelectorLoc, RBracLoc, move(Args)); 152985a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner} 1530eca7be6b7ebd93682eeaab2c71d59f2995dacdccChris Lattner 1531f85e193739c953358c865005855253af4f68a497John McCallenum ARCConversionTypeClass { 1532f85e193739c953358c865005855253af4f68a497John McCall ACTC_none, 1533f85e193739c953358c865005855253af4f68a497John McCall ACTC_retainable, 1534f85e193739c953358c865005855253af4f68a497John McCall ACTC_indirectRetainable 1535f85e193739c953358c865005855253af4f68a497John McCall}; 1536f85e193739c953358c865005855253af4f68a497John McCallstatic ARCConversionTypeClass classifyTypeForARCConversion(QualType type) { 1537f85e193739c953358c865005855253af4f68a497John McCall ARCConversionTypeClass ACTC = ACTC_retainable; 1538f85e193739c953358c865005855253af4f68a497John McCall 1539f85e193739c953358c865005855253af4f68a497John McCall // Ignore an outermost reference type. 1540f85e193739c953358c865005855253af4f68a497John McCall if (const ReferenceType *ref = type->getAs<ReferenceType>()) 1541f85e193739c953358c865005855253af4f68a497John McCall type = ref->getPointeeType(); 1542f85e193739c953358c865005855253af4f68a497John McCall 1543f85e193739c953358c865005855253af4f68a497John McCall // Drill through pointers and arrays recursively. 1544f85e193739c953358c865005855253af4f68a497John McCall while (true) { 1545f85e193739c953358c865005855253af4f68a497John McCall if (const PointerType *ptr = type->getAs<PointerType>()) { 1546f85e193739c953358c865005855253af4f68a497John McCall type = ptr->getPointeeType(); 1547f85e193739c953358c865005855253af4f68a497John McCall } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) { 1548f85e193739c953358c865005855253af4f68a497John McCall type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0); 1549f85e193739c953358c865005855253af4f68a497John McCall } else { 1550f85e193739c953358c865005855253af4f68a497John McCall break; 1551f85e193739c953358c865005855253af4f68a497John McCall } 1552f85e193739c953358c865005855253af4f68a497John McCall ACTC = ACTC_indirectRetainable; 1553f85e193739c953358c865005855253af4f68a497John McCall } 1554f85e193739c953358c865005855253af4f68a497John McCall 1555f85e193739c953358c865005855253af4f68a497John McCall if (!type->isObjCRetainableType()) return ACTC_none; 1556f85e193739c953358c865005855253af4f68a497John McCall return ACTC; 1557f85e193739c953358c865005855253af4f68a497John McCall} 1558f85e193739c953358c865005855253af4f68a497John McCall 1559f85e193739c953358c865005855253af4f68a497John McCallnamespace { 1560f85e193739c953358c865005855253af4f68a497John McCall /// Return true if the given expression can be reasonably converted 1561f85e193739c953358c865005855253af4f68a497John McCall /// between a retainable pointer type and a C pointer type. 1562f85e193739c953358c865005855253af4f68a497John McCall struct ARCCastChecker : StmtVisitor<ARCCastChecker, bool> { 1563f85e193739c953358c865005855253af4f68a497John McCall ASTContext &Context; 1564f85e193739c953358c865005855253af4f68a497John McCall ARCCastChecker(ASTContext &Context) : Context(Context) {} 1565f85e193739c953358c865005855253af4f68a497John McCall bool VisitStmt(Stmt *s) { 1566f85e193739c953358c865005855253af4f68a497John McCall return false; 1567f85e193739c953358c865005855253af4f68a497John McCall } 1568f85e193739c953358c865005855253af4f68a497John McCall bool VisitExpr(Expr *e) { 1569f85e193739c953358c865005855253af4f68a497John McCall return e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); 1570f85e193739c953358c865005855253af4f68a497John McCall } 1571f85e193739c953358c865005855253af4f68a497John McCall 1572f85e193739c953358c865005855253af4f68a497John McCall bool VisitParenExpr(ParenExpr *e) { 1573f85e193739c953358c865005855253af4f68a497John McCall return Visit(e->getSubExpr()); 1574f85e193739c953358c865005855253af4f68a497John McCall } 1575f85e193739c953358c865005855253af4f68a497John McCall bool VisitCastExpr(CastExpr *e) { 1576f85e193739c953358c865005855253af4f68a497John McCall switch (e->getCastKind()) { 1577f85e193739c953358c865005855253af4f68a497John McCall case CK_NullToPointer: 1578f85e193739c953358c865005855253af4f68a497John McCall return true; 1579f85e193739c953358c865005855253af4f68a497John McCall case CK_NoOp: 1580f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueToRValue: 1581f85e193739c953358c865005855253af4f68a497John McCall case CK_BitCast: 1582f85e193739c953358c865005855253af4f68a497John McCall case CK_AnyPointerToObjCPointerCast: 1583f85e193739c953358c865005855253af4f68a497John McCall case CK_AnyPointerToBlockPointerCast: 1584f85e193739c953358c865005855253af4f68a497John McCall return Visit(e->getSubExpr()); 1585f85e193739c953358c865005855253af4f68a497John McCall default: 1586f85e193739c953358c865005855253af4f68a497John McCall return false; 1587f85e193739c953358c865005855253af4f68a497John McCall } 1588f85e193739c953358c865005855253af4f68a497John McCall } 1589f85e193739c953358c865005855253af4f68a497John McCall bool VisitUnaryExtension(UnaryOperator *e) { 1590f85e193739c953358c865005855253af4f68a497John McCall return Visit(e->getSubExpr()); 1591f85e193739c953358c865005855253af4f68a497John McCall } 1592f85e193739c953358c865005855253af4f68a497John McCall bool VisitBinComma(BinaryOperator *e) { 1593f85e193739c953358c865005855253af4f68a497John McCall return Visit(e->getRHS()); 1594f85e193739c953358c865005855253af4f68a497John McCall } 1595f85e193739c953358c865005855253af4f68a497John McCall bool VisitConditionalOperator(ConditionalOperator *e) { 1596f85e193739c953358c865005855253af4f68a497John McCall // Conditional operators are okay if both sides are okay. 1597f85e193739c953358c865005855253af4f68a497John McCall return Visit(e->getTrueExpr()) && Visit(e->getFalseExpr()); 1598f85e193739c953358c865005855253af4f68a497John McCall } 1599f85e193739c953358c865005855253af4f68a497John McCall bool VisitObjCStringLiteral(ObjCStringLiteral *e) { 1600f85e193739c953358c865005855253af4f68a497John McCall // Always white-list Objective-C string literals. 1601f85e193739c953358c865005855253af4f68a497John McCall return true; 1602f85e193739c953358c865005855253af4f68a497John McCall } 1603f85e193739c953358c865005855253af4f68a497John McCall bool VisitStmtExpr(StmtExpr *e) { 1604f85e193739c953358c865005855253af4f68a497John McCall return Visit(e->getSubStmt()->body_back()); 1605f85e193739c953358c865005855253af4f68a497John McCall } 1606f85e193739c953358c865005855253af4f68a497John McCall bool VisitDeclRefExpr(DeclRefExpr *e) { 1607f85e193739c953358c865005855253af4f68a497John McCall // White-list references to global extern strings from system 1608f85e193739c953358c865005855253af4f68a497John McCall // headers. 1609f85e193739c953358c865005855253af4f68a497John McCall if (VarDecl *var = dyn_cast<VarDecl>(e->getDecl())) 1610f85e193739c953358c865005855253af4f68a497John McCall if (var->getStorageClass() == SC_Extern && 1611f85e193739c953358c865005855253af4f68a497John McCall var->getType().isConstQualified() && 1612f85e193739c953358c865005855253af4f68a497John McCall Context.getSourceManager().isInSystemHeader(var->getLocation())) 1613f85e193739c953358c865005855253af4f68a497John McCall return true; 1614f85e193739c953358c865005855253af4f68a497John McCall return false; 1615f85e193739c953358c865005855253af4f68a497John McCall } 1616f85e193739c953358c865005855253af4f68a497John McCall }; 1617f85e193739c953358c865005855253af4f68a497John McCall} 1618f85e193739c953358c865005855253af4f68a497John McCall 16191522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanianbool 1620af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz JahanianSema::ValidObjCARCNoBridgeCastExpr(Expr *&Exp, QualType castType) { 1621c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian Expr *NewExp = Exp->IgnoreParenCasts(); 1622af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian 1623c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (!isa<ObjCMessageExpr>(NewExp) && !isa<ObjCPropertyRefExpr>(NewExp) 1624c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian && !isa<CallExpr>(NewExp)) 1625af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian return false; 1626af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian ObjCMethodDecl *method = 0; 1627c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian bool MethodReturnsPlusOne = false; 1628c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian 1629af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(NewExp)) { 1630af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian method = PRE->getExplicitProperty()->getGetterMethodDecl(); 1631af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian } 1632c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(NewExp)) 1633af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian method = ME->getMethodDecl(); 1634c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian else { 1635c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian CallExpr *CE = cast<CallExpr>(NewExp); 1636c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian Decl *CallDecl = CE->getCalleeDecl(); 1637c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (!CallDecl) 1638c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian return false; 1639c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (CallDecl->hasAttr<CFReturnsNotRetainedAttr>()) 1640c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian return true; 1641c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian MethodReturnsPlusOne = CallDecl->hasAttr<CFReturnsRetainedAttr>(); 1642c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (!MethodReturnsPlusOne) { 1643c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (NamedDecl *ND = dyn_cast<NamedDecl>(CallDecl)) 1644c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (const IdentifierInfo *Id = ND->getIdentifier()) 1645c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (Id->isStr("__builtin___CFStringMakeConstantString")) 1646c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian return true; 1647c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian } 1648af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian } 1649c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian 1650af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian if (!MethodReturnsPlusOne) { 1651c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (!method) 1652c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian return false; 1653c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (method->hasAttr<CFReturnsNotRetainedAttr>()) 1654c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian return true; 1655c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian MethodReturnsPlusOne = method->hasAttr<CFReturnsRetainedAttr>(); 1656c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian if (!MethodReturnsPlusOne) { 1657c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian ObjCMethodFamily family = method->getSelector().getMethodFamily(); 1658c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian switch (family) { 1659c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian case OMF_alloc: 1660c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian case OMF_copy: 1661c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian case OMF_mutableCopy: 1662c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian case OMF_new: 1663c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian MethodReturnsPlusOne = true; 1664c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian break; 1665c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian default: 1666c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian break; 1667c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian } 1668af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian } 1669af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian } 1670c8505ad9182c3ddcfda42bee250b2c32dd1f3219Fariborz Jahanian 1671af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian if (MethodReturnsPlusOne) { 1672af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian TypeSourceInfo *TSInfo = 1673af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian Context.getTrivialTypeSourceInfo(castType, SourceLocation()); 1674af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian ExprResult ExpRes = BuildObjCBridgedCast(SourceLocation(), OBC_BridgeTransfer, 1675af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian SourceLocation(), TSInfo, Exp); 1676af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian Exp = ExpRes.take(); 1677af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian } 1678af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian return true; 16791522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian} 16801522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian 1681f85e193739c953358c865005855253af4f68a497John McCallvoid 1682f85e193739c953358c865005855253af4f68a497John McCallSema::CheckObjCARCConversion(SourceRange castRange, QualType castType, 1683af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian Expr *&castExpr, CheckedConversionKind CCK) { 1684f85e193739c953358c865005855253af4f68a497John McCall QualType castExprType = castExpr->getType(); 1685f85e193739c953358c865005855253af4f68a497John McCall 1686f85e193739c953358c865005855253af4f68a497John McCall ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType); 1687f85e193739c953358c865005855253af4f68a497John McCall ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType); 1688f85e193739c953358c865005855253af4f68a497John McCall if (exprACTC == castACTC) return; 16898295b7b51b13add75518bb9e2d9db00711a20166Fariborz Jahanian if (exprACTC && castType->isIntegralType(Context)) return; 1690f85e193739c953358c865005855253af4f68a497John McCall 1691f85e193739c953358c865005855253af4f68a497John McCall // Allow casts between pointers to lifetime types (e.g., __strong id*) 1692f85e193739c953358c865005855253af4f68a497John McCall // and pointers to void (e.g., cv void *). Casting from void* to lifetime* 1693f85e193739c953358c865005855253af4f68a497John McCall // must be explicit. 1694f85e193739c953358c865005855253af4f68a497John McCall if (const PointerType *CastPtr = castType->getAs<PointerType>()) { 1695f85e193739c953358c865005855253af4f68a497John McCall if (const PointerType *CastExprPtr = castExprType->getAs<PointerType>()) { 1696f85e193739c953358c865005855253af4f68a497John McCall QualType CastPointee = CastPtr->getPointeeType(); 1697f85e193739c953358c865005855253af4f68a497John McCall QualType CastExprPointee = CastExprPtr->getPointeeType(); 1698f85e193739c953358c865005855253af4f68a497John McCall if ((CCK != CCK_ImplicitConversion && 1699f85e193739c953358c865005855253af4f68a497John McCall CastPointee->isObjCIndirectLifetimeType() && 1700f85e193739c953358c865005855253af4f68a497John McCall CastExprPointee->isVoidType()) || 1701f85e193739c953358c865005855253af4f68a497John McCall (CastPointee->isVoidType() && 1702f85e193739c953358c865005855253af4f68a497John McCall CastExprPointee->isObjCIndirectLifetimeType())) 1703f85e193739c953358c865005855253af4f68a497John McCall return; 1704f85e193739c953358c865005855253af4f68a497John McCall } 1705f85e193739c953358c865005855253af4f68a497John McCall } 1706f85e193739c953358c865005855253af4f68a497John McCall 1707f85e193739c953358c865005855253af4f68a497John McCall if (ARCCastChecker(Context).Visit(castExpr)) 1708f85e193739c953358c865005855253af4f68a497John McCall return; 1709f85e193739c953358c865005855253af4f68a497John McCall 1710f85e193739c953358c865005855253af4f68a497John McCall SourceLocation loc = 1711f85e193739c953358c865005855253af4f68a497John McCall (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc()); 1712f85e193739c953358c865005855253af4f68a497John McCall 1713f85e193739c953358c865005855253af4f68a497John McCall if (makeUnavailableInSystemHeader(loc, 1714f85e193739c953358c865005855253af4f68a497John McCall "converts between Objective-C and C pointers in -fobjc-arc")) 1715f85e193739c953358c865005855253af4f68a497John McCall return; 1716f85e193739c953358c865005855253af4f68a497John McCall 171771c482c76c67ad4240757c4e319ffdb72272b8eeJohn McCall unsigned srcKind = 0; 1718f85e193739c953358c865005855253af4f68a497John McCall switch (exprACTC) { 1719f85e193739c953358c865005855253af4f68a497John McCall case ACTC_none: 1720f85e193739c953358c865005855253af4f68a497John McCall srcKind = (castExprType->isPointerType() ? 1 : 0); 1721f85e193739c953358c865005855253af4f68a497John McCall break; 1722f85e193739c953358c865005855253af4f68a497John McCall case ACTC_retainable: 1723f85e193739c953358c865005855253af4f68a497John McCall srcKind = (castExprType->isBlockPointerType() ? 2 : 3); 1724f85e193739c953358c865005855253af4f68a497John McCall break; 1725f85e193739c953358c865005855253af4f68a497John McCall case ACTC_indirectRetainable: 1726f85e193739c953358c865005855253af4f68a497John McCall srcKind = 4; 1727f85e193739c953358c865005855253af4f68a497John McCall break; 1728f85e193739c953358c865005855253af4f68a497John McCall } 1729f85e193739c953358c865005855253af4f68a497John McCall 1730f85e193739c953358c865005855253af4f68a497John McCall if (CCK == CCK_CStyleCast) { 1731f85e193739c953358c865005855253af4f68a497John McCall // Check whether this could be fixed with a bridge cast. 1732f85e193739c953358c865005855253af4f68a497John McCall SourceLocation AfterLParen = PP.getLocForEndOfToken(castRange.getBegin()); 1733f85e193739c953358c865005855253af4f68a497John McCall SourceLocation NoteLoc = AfterLParen.isValid()? AfterLParen : loc; 1734f85e193739c953358c865005855253af4f68a497John McCall 1735f85e193739c953358c865005855253af4f68a497John McCall if (castType->isObjCARCBridgableType() && 1736f85e193739c953358c865005855253af4f68a497John McCall castExprType->isCARCBridgableType()) { 17371522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian // explicit unbridged casts are allowed if the source of the cast is a 17381522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian // message sent to an objc method (or property access) 1739af9751747b098d77052cb71f1e648f29cfbadcc1Fariborz Jahanian if (ValidObjCARCNoBridgeCastExpr(castExpr, castType)) 17401522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian return; 1741f85e193739c953358c865005855253af4f68a497John McCall Diag(loc, diag::err_arc_cast_requires_bridge) 1742f85e193739c953358c865005855253af4f68a497John McCall << 2 1743f85e193739c953358c865005855253af4f68a497John McCall << castExprType 1744f85e193739c953358c865005855253af4f68a497John McCall << (castType->isBlockPointerType()? 1 : 0) 1745f85e193739c953358c865005855253af4f68a497John McCall << castType 1746f85e193739c953358c865005855253af4f68a497John McCall << castRange 1747f85e193739c953358c865005855253af4f68a497John McCall << castExpr->getSourceRange(); 1748f85e193739c953358c865005855253af4f68a497John McCall Diag(NoteLoc, diag::note_arc_bridge) 1749f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateInsertion(AfterLParen, "__bridge "); 1750f85e193739c953358c865005855253af4f68a497John McCall Diag(NoteLoc, diag::note_arc_bridge_transfer) 1751f85e193739c953358c865005855253af4f68a497John McCall << castExprType 1752f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateInsertion(AfterLParen, "__bridge_transfer "); 1753f85e193739c953358c865005855253af4f68a497John McCall 1754f85e193739c953358c865005855253af4f68a497John McCall return; 1755f85e193739c953358c865005855253af4f68a497John McCall } 1756f85e193739c953358c865005855253af4f68a497John McCall 1757f85e193739c953358c865005855253af4f68a497John McCall if (castType->isCARCBridgableType() && 1758f85e193739c953358c865005855253af4f68a497John McCall castExprType->isObjCARCBridgableType()){ 1759f85e193739c953358c865005855253af4f68a497John McCall Diag(loc, diag::err_arc_cast_requires_bridge) 1760f85e193739c953358c865005855253af4f68a497John McCall << (castExprType->isBlockPointerType()? 1 : 0) 1761f85e193739c953358c865005855253af4f68a497John McCall << castExprType 1762f85e193739c953358c865005855253af4f68a497John McCall << 2 1763f85e193739c953358c865005855253af4f68a497John McCall << castType 1764f85e193739c953358c865005855253af4f68a497John McCall << castRange 1765f85e193739c953358c865005855253af4f68a497John McCall << castExpr->getSourceRange(); 1766f85e193739c953358c865005855253af4f68a497John McCall 1767f85e193739c953358c865005855253af4f68a497John McCall Diag(NoteLoc, diag::note_arc_bridge) 1768f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateInsertion(AfterLParen, "__bridge "); 1769f85e193739c953358c865005855253af4f68a497John McCall Diag(NoteLoc, diag::note_arc_bridge_retained) 1770f85e193739c953358c865005855253af4f68a497John McCall << castType 1771f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateInsertion(AfterLParen, "__bridge_retained "); 1772f85e193739c953358c865005855253af4f68a497John McCall return; 1773f85e193739c953358c865005855253af4f68a497John McCall } 1774f85e193739c953358c865005855253af4f68a497John McCall } 1775f85e193739c953358c865005855253af4f68a497John McCall 1776f85e193739c953358c865005855253af4f68a497John McCall Diag(loc, diag::err_arc_mismatched_cast) 1777f85e193739c953358c865005855253af4f68a497John McCall << (CCK != CCK_ImplicitConversion) << srcKind << castExprType << castType 1778f85e193739c953358c865005855253af4f68a497John McCall << castRange << castExpr->getSourceRange(); 1779f85e193739c953358c865005855253af4f68a497John McCall} 1780f85e193739c953358c865005855253af4f68a497John McCall 17817a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanianbool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType, 17827a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian QualType exprType) { 17837a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian QualType canCastType = 17847a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian Context.getCanonicalType(castType).getUnqualifiedType(); 17857a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian QualType canExprType = 17867a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian Context.getCanonicalType(exprType).getUnqualifiedType(); 17877a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian if (isa<ObjCObjectPointerType>(canCastType) && 17887a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian castType.getObjCLifetime() == Qualifiers::OCL_Weak && 17897a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian canExprType->isObjCObjectPointerType()) { 17907a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian if (const ObjCObjectPointerType *ObjT = 17917a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian canExprType->getAs<ObjCObjectPointerType>()) 17927a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) 17937a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian return false; 17947a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian } 17957a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian return true; 17967a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian} 17977a084ec568f8336ec6f10011d0118a6b19e253cbFariborz Jahanian 17987e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall/// Look for an ObjCReclaimReturnedObject cast and destroy it. 17997e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCallstatic Expr *maybeUndoReclaimObject(Expr *e) { 18007e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // For now, we just undo operands that are *immediately* reclaim 18017e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // expressions, which prevents the vast majority of potential 18027e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // problems here. To catch them all, we'd need to rebuild arbitrary 18037e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // value-propagating subexpressions --- we can't reliably rebuild 18047e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // in-place because of expression sharing. 18057e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) 18067e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall if (ice->getCastKind() == CK_ObjCReclaimReturnedObject) 18077e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall return ice->getSubExpr(); 18087e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 18097e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall return e; 18107e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall} 18117e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 1812f85e193739c953358c865005855253af4f68a497John McCallExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc, 1813f85e193739c953358c865005855253af4f68a497John McCall ObjCBridgeCastKind Kind, 1814f85e193739c953358c865005855253af4f68a497John McCall SourceLocation BridgeKeywordLoc, 1815f85e193739c953358c865005855253af4f68a497John McCall TypeSourceInfo *TSInfo, 1816f85e193739c953358c865005855253af4f68a497John McCall Expr *SubExpr) { 1817f85e193739c953358c865005855253af4f68a497John McCall QualType T = TSInfo->getType(); 1818f85e193739c953358c865005855253af4f68a497John McCall QualType FromType = SubExpr->getType(); 1819f85e193739c953358c865005855253af4f68a497John McCall 1820f85e193739c953358c865005855253af4f68a497John McCall bool MustConsume = false; 1821f85e193739c953358c865005855253af4f68a497John McCall if (T->isDependentType() || SubExpr->isTypeDependent()) { 1822f85e193739c953358c865005855253af4f68a497John McCall // Okay: we'll build a dependent expression type. 1823f85e193739c953358c865005855253af4f68a497John McCall } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) { 1824f85e193739c953358c865005855253af4f68a497John McCall // Casting CF -> id 1825f85e193739c953358c865005855253af4f68a497John McCall switch (Kind) { 1826f85e193739c953358c865005855253af4f68a497John McCall case OBC_Bridge: 1827f85e193739c953358c865005855253af4f68a497John McCall break; 1828f85e193739c953358c865005855253af4f68a497John McCall 1829f85e193739c953358c865005855253af4f68a497John McCall case OBC_BridgeRetained: 1830f85e193739c953358c865005855253af4f68a497John McCall Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) 1831f85e193739c953358c865005855253af4f68a497John McCall << 2 1832f85e193739c953358c865005855253af4f68a497John McCall << FromType 1833f85e193739c953358c865005855253af4f68a497John McCall << (T->isBlockPointerType()? 1 : 0) 1834f85e193739c953358c865005855253af4f68a497John McCall << T 1835f85e193739c953358c865005855253af4f68a497John McCall << SubExpr->getSourceRange() 1836f85e193739c953358c865005855253af4f68a497John McCall << Kind; 1837f85e193739c953358c865005855253af4f68a497John McCall Diag(BridgeKeywordLoc, diag::note_arc_bridge) 1838f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge"); 1839f85e193739c953358c865005855253af4f68a497John McCall Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer) 1840f85e193739c953358c865005855253af4f68a497John McCall << FromType 1841f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateReplacement(BridgeKeywordLoc, 1842f85e193739c953358c865005855253af4f68a497John McCall "__bridge_transfer "); 1843f85e193739c953358c865005855253af4f68a497John McCall 1844f85e193739c953358c865005855253af4f68a497John McCall Kind = OBC_Bridge; 1845f85e193739c953358c865005855253af4f68a497John McCall break; 1846f85e193739c953358c865005855253af4f68a497John McCall 1847f85e193739c953358c865005855253af4f68a497John McCall case OBC_BridgeTransfer: 1848f85e193739c953358c865005855253af4f68a497John McCall // We must consume the Objective-C object produced by the cast. 1849f85e193739c953358c865005855253af4f68a497John McCall MustConsume = true; 1850f85e193739c953358c865005855253af4f68a497John McCall break; 1851f85e193739c953358c865005855253af4f68a497John McCall } 1852f85e193739c953358c865005855253af4f68a497John McCall } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) { 1853f85e193739c953358c865005855253af4f68a497John McCall // Okay: id -> CF 1854f85e193739c953358c865005855253af4f68a497John McCall switch (Kind) { 1855f85e193739c953358c865005855253af4f68a497John McCall case OBC_Bridge: 18567e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // Reclaiming a value that's going to be __bridge-casted to CF 18577e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // is very dangerous, so we don't do it. 18587e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall SubExpr = maybeUndoReclaimObject(SubExpr); 1859f85e193739c953358c865005855253af4f68a497John McCall break; 1860f85e193739c953358c865005855253af4f68a497John McCall 1861f85e193739c953358c865005855253af4f68a497John McCall case OBC_BridgeRetained: 1862f85e193739c953358c865005855253af4f68a497John McCall // Produce the object before casting it. 1863f85e193739c953358c865005855253af4f68a497John McCall SubExpr = ImplicitCastExpr::Create(Context, FromType, 1864f85e193739c953358c865005855253af4f68a497John McCall CK_ObjCProduceObject, 1865f85e193739c953358c865005855253af4f68a497John McCall SubExpr, 0, VK_RValue); 1866f85e193739c953358c865005855253af4f68a497John McCall break; 1867f85e193739c953358c865005855253af4f68a497John McCall 1868f85e193739c953358c865005855253af4f68a497John McCall case OBC_BridgeTransfer: 1869f85e193739c953358c865005855253af4f68a497John McCall Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) 1870f85e193739c953358c865005855253af4f68a497John McCall << (FromType->isBlockPointerType()? 1 : 0) 1871f85e193739c953358c865005855253af4f68a497John McCall << FromType 1872f85e193739c953358c865005855253af4f68a497John McCall << 2 1873f85e193739c953358c865005855253af4f68a497John McCall << T 1874f85e193739c953358c865005855253af4f68a497John McCall << SubExpr->getSourceRange() 1875f85e193739c953358c865005855253af4f68a497John McCall << Kind; 1876f85e193739c953358c865005855253af4f68a497John McCall 1877f85e193739c953358c865005855253af4f68a497John McCall Diag(BridgeKeywordLoc, diag::note_arc_bridge) 1878f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge "); 1879f85e193739c953358c865005855253af4f68a497John McCall Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained) 1880f85e193739c953358c865005855253af4f68a497John McCall << T 1881f85e193739c953358c865005855253af4f68a497John McCall << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge_retained "); 1882f85e193739c953358c865005855253af4f68a497John McCall 1883f85e193739c953358c865005855253af4f68a497John McCall Kind = OBC_Bridge; 1884f85e193739c953358c865005855253af4f68a497John McCall break; 1885f85e193739c953358c865005855253af4f68a497John McCall } 1886f85e193739c953358c865005855253af4f68a497John McCall } else { 1887f85e193739c953358c865005855253af4f68a497John McCall Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible) 1888f85e193739c953358c865005855253af4f68a497John McCall << FromType << T << Kind 1889f85e193739c953358c865005855253af4f68a497John McCall << SubExpr->getSourceRange() 1890f85e193739c953358c865005855253af4f68a497John McCall << TSInfo->getTypeLoc().getSourceRange(); 1891f85e193739c953358c865005855253af4f68a497John McCall return ExprError(); 1892f85e193739c953358c865005855253af4f68a497John McCall } 1893f85e193739c953358c865005855253af4f68a497John McCall 1894f85e193739c953358c865005855253af4f68a497John McCall Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, 1895f85e193739c953358c865005855253af4f68a497John McCall BridgeKeywordLoc, 1896f85e193739c953358c865005855253af4f68a497John McCall TSInfo, SubExpr); 1897f85e193739c953358c865005855253af4f68a497John McCall 1898f85e193739c953358c865005855253af4f68a497John McCall if (MustConsume) { 1899f85e193739c953358c865005855253af4f68a497John McCall ExprNeedsCleanups = true; 1900f85e193739c953358c865005855253af4f68a497John McCall Result = ImplicitCastExpr::Create(Context, T, CK_ObjCConsumeObject, Result, 1901f85e193739c953358c865005855253af4f68a497John McCall 0, VK_RValue); 1902f85e193739c953358c865005855253af4f68a497John McCall } 1903f85e193739c953358c865005855253af4f68a497John McCall 1904f85e193739c953358c865005855253af4f68a497John McCall return Result; 1905f85e193739c953358c865005855253af4f68a497John McCall} 1906f85e193739c953358c865005855253af4f68a497John McCall 1907f85e193739c953358c865005855253af4f68a497John McCallExprResult Sema::ActOnObjCBridgedCast(Scope *S, 1908f85e193739c953358c865005855253af4f68a497John McCall SourceLocation LParenLoc, 1909f85e193739c953358c865005855253af4f68a497John McCall ObjCBridgeCastKind Kind, 1910f85e193739c953358c865005855253af4f68a497John McCall SourceLocation BridgeKeywordLoc, 1911f85e193739c953358c865005855253af4f68a497John McCall ParsedType Type, 1912f85e193739c953358c865005855253af4f68a497John McCall SourceLocation RParenLoc, 1913f85e193739c953358c865005855253af4f68a497John McCall Expr *SubExpr) { 1914f85e193739c953358c865005855253af4f68a497John McCall TypeSourceInfo *TSInfo = 0; 1915f85e193739c953358c865005855253af4f68a497John McCall QualType T = GetTypeFromParser(Type, &TSInfo); 1916f85e193739c953358c865005855253af4f68a497John McCall if (!TSInfo) 1917f85e193739c953358c865005855253af4f68a497John McCall TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc); 1918f85e193739c953358c865005855253af4f68a497John McCall return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, 1919f85e193739c953358c865005855253af4f68a497John McCall SubExpr); 1920f85e193739c953358c865005855253af4f68a497John McCall} 1921