SemaExprObjC.cpp revision 1522a7c35e9872c5767721350fc8050be5b14fd2
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.
663e2193ce5feb2feb092e5ae615e85148e06e9fd2Anders Carlsson    S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(),
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:
207f85e193739c953358c865005855253af4f68a497John McCall      break;
208f85e193739c953358c865005855253af4f68a497John McCall    }
209f85e193739c953358c865005855253af4f68a497John McCall  }
210a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner  QualType Ty = Context.getObjCSelType();
2116d5a1c28593443f3973ef38f8fa042d59182412dDaniel Dunbar  return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
21285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner}
21385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner
214f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCallExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
215f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             SourceLocation AtLoc,
216f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             SourceLocation ProtoLoc,
217f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             SourceLocation LParenLoc,
218f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             SourceLocation RParenLoc) {
219c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor  ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
22085a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner  if (!PDecl) {
2213c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner    Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
22285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner    return true;
22385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner  }
2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
225a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner  QualType Ty = Context.getObjCProtoType();
226a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner  if (Ty.isNull())
22785a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner    return true;
22814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  Ty = Context.getObjCObjectPointerType(Ty);
229a0af1fe67da29343cd182c51cd48d91b740ecef2Chris Lattner  return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
23085a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner}
23185a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner
23226743b20e4a8c2a986e6453f0c38beba0afef633John McCall/// Try to capture an implicit reference to 'self'.
23326743b20e4a8c2a986e6453f0c38beba0afef633John McCallObjCMethodDecl *Sema::tryCaptureObjCSelf() {
23426743b20e4a8c2a986e6453f0c38beba0afef633John McCall  // Ignore block scopes: we can capture through them.
23526743b20e4a8c2a986e6453f0c38beba0afef633John McCall  DeclContext *DC = CurContext;
23626743b20e4a8c2a986e6453f0c38beba0afef633John McCall  while (true) {
23726743b20e4a8c2a986e6453f0c38beba0afef633John McCall    if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext();
23826743b20e4a8c2a986e6453f0c38beba0afef633John McCall    else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext();
23926743b20e4a8c2a986e6453f0c38beba0afef633John McCall    else break;
24026743b20e4a8c2a986e6453f0c38beba0afef633John McCall  }
24126743b20e4a8c2a986e6453f0c38beba0afef633John McCall
24226743b20e4a8c2a986e6453f0c38beba0afef633John McCall  // If we're not in an ObjC method, error out.  Note that, unlike the
24326743b20e4a8c2a986e6453f0c38beba0afef633John McCall  // C++ case, we don't require an instance method --- class methods
24426743b20e4a8c2a986e6453f0c38beba0afef633John McCall  // still have a 'self', and we really do still need to capture it!
24526743b20e4a8c2a986e6453f0c38beba0afef633John McCall  ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
24626743b20e4a8c2a986e6453f0c38beba0afef633John McCall  if (!method)
24726743b20e4a8c2a986e6453f0c38beba0afef633John McCall    return 0;
24826743b20e4a8c2a986e6453f0c38beba0afef633John McCall
24926743b20e4a8c2a986e6453f0c38beba0afef633John McCall  ImplicitParamDecl *self = method->getSelfDecl();
25026743b20e4a8c2a986e6453f0c38beba0afef633John McCall  assert(self && "capturing 'self' in non-definition?");
25126743b20e4a8c2a986e6453f0c38beba0afef633John McCall
25226743b20e4a8c2a986e6453f0c38beba0afef633John McCall  // Mark that we're closing on 'this' in all the block scopes, if applicable.
25326743b20e4a8c2a986e6453f0c38beba0afef633John McCall  for (unsigned idx = FunctionScopes.size() - 1;
25426743b20e4a8c2a986e6453f0c38beba0afef633John McCall       isa<BlockScopeInfo>(FunctionScopes[idx]);
2556b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall       --idx) {
2566b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]);
2576b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    unsigned &captureIndex = blockScope->CaptureMap[self];
2586b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    if (captureIndex) break;
2596b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
2606b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]);
2616b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    blockScope->Captures.push_back(
2626b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall              BlockDecl::Capture(self, /*byref*/ false, nested, /*copy*/ 0));
2636b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    captureIndex = blockScope->Captures.size(); // +1
2646b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  }
26526743b20e4a8c2a986e6453f0c38beba0afef633John McCall
26626743b20e4a8c2a986e6453f0c38beba0afef633John McCall  return method;
26726743b20e4a8c2a986e6453f0c38beba0afef633John McCall}
26826743b20e4a8c2a986e6453f0c38beba0afef633John McCall
269926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas GregorQualType Sema::getMessageSendResultType(QualType ReceiverType,
270926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                        ObjCMethodDecl *Method,
271926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                    bool isClassMessage, bool isSuperMessage) {
272926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  assert(Method && "Must have a method");
273926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method->hasRelatedResultType())
274926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Method->getSendResultType();
275926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
276926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  // If a method has a related return type:
277926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //   - if the method found is an instance method, but the message send
278926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //     was a class message send, T is the declared return type of the method
279926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //     found
280926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (Method->isInstanceMethod() && isClassMessage)
281926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Method->getSendResultType();
282926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
283926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //   - if the receiver is super, T is a pointer to the class of the
284926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //     enclosing method definition
285926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (isSuperMessage) {
286926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
287926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface())
288926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        return Context.getObjCObjectPointerType(
289926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                        Context.getObjCInterfaceType(Class));
290926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  }
291926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
292926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //   - if the receiver is the name of a class U, T is a pointer to U
293926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (ReceiverType->getAs<ObjCInterfaceType>() ||
294926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      ReceiverType->isObjCQualifiedInterfaceType())
295926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Context.getObjCObjectPointerType(ReceiverType);
296926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //   - if the receiver is of type Class or qualified Class type,
297926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //     T is the declared return type of the method.
298926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (ReceiverType->isObjCClassType() ||
299926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      ReceiverType->isObjCQualifiedClassType())
300926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return  Method->getSendResultType();
301926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
302926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //   - if the receiver is id, qualified id, Class, or qualified Class, T
303926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //     is the receiver type, otherwise
304926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  //   - T is the type of the receiver expression.
305926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  return ReceiverType;
306926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor}
307926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
308926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorvoid Sema::EmitRelatedResultTypeNote(const Expr *E) {
309926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  E = E->IgnoreParenImpCasts();
310926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E);
311926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!MsgSend)
312926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return;
313926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
314926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  const ObjCMethodDecl *Method = MsgSend->getMethodDecl();
315926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method)
316926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return;
317926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
318926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (!Method->hasRelatedResultType())
319926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return;
320926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
321926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (Context.hasSameUnqualifiedType(Method->getResultType()
322926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                        .getNonReferenceType(),
323926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                     MsgSend->getType()))
324926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return;
325926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
326926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  Diag(Method->getLocation(), diag::note_related_result_type_inferred)
327926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    << Method->isInstanceMethod() << Method->getSelector()
328926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    << MsgSend->getType();
329926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor}
33026743b20e4a8c2a986e6453f0c38beba0afef633John McCall
331926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorbool Sema::CheckMessageArgumentTypes(QualType ReceiverType,
332926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                     Expr **Args, unsigned NumArgs,
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     Selector Sel, ObjCMethodDecl *Method,
334926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                     bool isClassMessage, bool isSuperMessage,
335637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar                                     SourceLocation lbrac, SourceLocation rbrac,
336f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                     QualType &ReturnType, ExprValueKind &VK) {
337637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar  if (!Method) {
3386660c8a4cc2115929d92be83bbc54c307002a321Daniel Dunbar    // Apply default argument promotion as for (C99 6.5.2.2p6).
33992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    for (unsigned i = 0; i != NumArgs; i++) {
34092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      if (Args[i]->isTypeDependent())
34192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor        continue;
34292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
343429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult Result = DefaultArgumentPromotion(Args[i]);
344429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (Result.isInvalid())
345429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        return true;
346429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      Args[i] = Result.take();
34792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    }
3486660c8a4cc2115929d92be83bbc54c307002a321Daniel Dunbar
349f85e193739c953358c865005855253af4f68a497John McCall    unsigned DiagID;
350f85e193739c953358c865005855253af4f68a497John McCall    if (getLangOptions().ObjCAutoRefCount)
351f85e193739c953358c865005855253af4f68a497John McCall      DiagID = diag::err_arc_method_not_found;
352f85e193739c953358c865005855253af4f68a497John McCall    else
353f85e193739c953358c865005855253af4f68a497John McCall      DiagID = isClassMessage ? diag::warn_class_method_not_found
354f85e193739c953358c865005855253af4f68a497John McCall                              : diag::warn_inst_method_not_found;
355077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    Diag(lbrac, DiagID)
356077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner      << Sel << isClassMessage << SourceRange(lbrac, rbrac);
357637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar    ReturnType = Context.getObjCIdType();
358f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    VK = VK_RValue;
359637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar    return false;
360637cebb67c59765e1412c589550c8c9ba001baebDaniel Dunbar  }
3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
362926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage,
363926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                        isSuperMessage);
364f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  VK = Expr::getValueKindForType(Method->getResultType());
3651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36691e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar  unsigned NumNamedArgs = Sel.getNumArgs();
3674f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  // Method might have more arguments than selector indicates. This is due
3684f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  // to addition of c-style arguments in method.
3694f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  if (Method->param_size() > Sel.getNumArgs())
3704f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    NumNamedArgs = Method->param_size();
3714f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  // FIXME. This need be cleaned up.
3724f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  if (NumArgs < NumNamedArgs) {
373f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    Diag(lbrac, diag::err_typecheck_call_too_few_args)
374f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall      << 2 << NumNamedArgs << NumArgs;
3754f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    return false;
3764f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  }
37791e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar
378312531a8cd69c562d5687bd69fd334be99d87320Chris Lattner  bool IsError = false;
37991e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar  for (unsigned i = 0; i < NumNamedArgs; i++) {
38092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    // We can't do any type-checking on a type-dependent argument.
38192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    if (Args[i]->isTypeDependent())
38292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      continue;
38392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
38485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner    Expr *argExpr = Args[i];
38592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
386688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    ParmVarDecl *Param = Method->param_begin()[i];
38785a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner    assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
389688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
390688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor                            Param->getType(),
391688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor                            PDiag(diag::err_call_incomplete_argument)
392688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor                              << argExpr->getSourceRange()))
393688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor      return true;
394688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor
395745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian    InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
396745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian                                                                      Param);
3973fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall    ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
398688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    if (ArgE.isInvalid())
399688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor      IsError = true;
400688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor    else
401688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor      Args[i] = ArgE.takeAs<Expr>();
40285a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner  }
40391e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar
40491e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar  // Promote additional arguments to variadic methods.
40591e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar  if (Method->isVariadic()) {
40692e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
40792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      if (Args[i]->isTypeDependent())
40892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor        continue;
40992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
410429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
411429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      IsError |= Arg.isInvalid();
412429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      Args[i] = Arg.take();
41392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    }
41491e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar  } else {
41591e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar    // Check for extra arguments to non-variadic methods.
41691e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar    if (NumArgs != NumNamedArgs) {
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Diag(Args[NumNamedArgs]->getLocStart(),
418fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner           diag::err_typecheck_call_too_many_args)
419ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher        << 2 /*method*/ << NumNamedArgs << NumArgs
420ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher        << Method->getSourceRange()
421fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner        << SourceRange(Args[NumNamedArgs]->getLocStart(),
422fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner                       Args[NumArgs-1]->getLocEnd());
42391e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar    }
42491e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar  }
4255272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian  // diagnose nonnull arguments.
4265272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian  for (specific_attr_iterator<NonNullAttr>
4275272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian       i = Method->specific_attr_begin<NonNullAttr>(),
4285272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian       e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) {
4295272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian    CheckNonNullArguments(*i, Args, lbrac);
4305272adfe7066c4847b7339a75777252de64e79c2Fariborz Jahanian  }
43191e19b2029447e75d2c7730ff888cc396874685bDaniel Dunbar
4322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
433312531a8cd69c562d5687bd69fd334be99d87320Chris Lattner  return IsError;
43485a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner}
43585a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner
436f85e193739c953358c865005855253af4f68a497John McCallbool Sema::isSelfExpr(Expr *receiver) {
437f2d74cc083e1a83838acf99d2e1ca260d1c54742Fariborz Jahanian  // 'self' is objc 'self' in an objc method only.
438b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian  DeclContext *DC = CurContext;
439b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian  while (isa<BlockDecl>(DC))
440b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian    DC = DC->getParent();
441b460210fe0dec08971edfe33c294323cf79cb894Fariborz Jahanian  if (DC && !isa<ObjCMethodDecl>(DC))
442f2d74cc083e1a83838acf99d2e1ca260d1c54742Fariborz Jahanian    return false;
443f85e193739c953358c865005855253af4f68a497John McCall  receiver = receiver->IgnoreParenLValueCasts();
444f85e193739c953358c865005855253af4f68a497John McCall  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
4456b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff    if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
4466b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff      return true;
4476b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff  return false;
4486b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff}
4496b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff
450f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff// Helper method for ActOnClassMethod/ActOnInstanceMethod.
451f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff// Will search "local" class/category implementations for a method decl.
452175ba1e8180083927aabd7cc8137baa16be75646Fariborz Jahanian// If failed, then we search in class's root for an instance method.
453f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff// Returns 0 if no method is found.
4545609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve NaroffObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
455f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff                                          ObjCInterfaceDecl *ClassDecl) {
456f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff  ObjCMethodDecl *Method = 0;
4575609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff  // lookup in class and all superclasses
4585609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff  while (ClassDecl && !Method) {
45987018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis    if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
46017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Method = ImpDecl->getClassMethod(Sel);
4611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4625609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    // Look through local category implementations associated with the class.
4631cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis    if (!Method)
4641cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis      Method = ClassDecl->getCategoryClassMethod(Sel);
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4665609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    // Before we give up, check if the selector is an instance method.
4675609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    // But only in the root. This matches gcc's behaviour and what the
4685609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    // runtime expects.
4695609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    if (!Method && !ClassDecl->getSuperClass()) {
47017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Method = ClassDecl->lookupInstanceMethod(Sel);
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Look through local category implementations associated
4725609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff      // with the root class.
4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (!Method)
4745609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff        Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
475f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff    }
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4775609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    ClassDecl = ClassDecl->getSuperClass();
478f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff  }
4795609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff  return Method;
4805609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff}
4815609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff
4825609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve NaroffObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
4835609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff                                              ObjCInterfaceDecl *ClassDecl) {
4845609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff  ObjCMethodDecl *Method = 0;
4855609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff  while (ClassDecl && !Method) {
4865609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    // If we have implementations in scope, check "private" methods.
48787018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis    if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
48817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Method = ImpDecl->getInstanceMethod(Sel);
4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4905609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    // Look through local category implementations associated with the class.
4911cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis    if (!Method)
4921cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis      Method = ClassDecl->getCategoryInstanceMethod(Sel);
4935609ec04ed9a4fd58c3203d210cf32e9283feb5eSteve Naroff    ClassDecl = ClassDecl->getSuperClass();
494175ba1e8180083927aabd7cc8137baa16be75646Fariborz Jahanian  }
495f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff  return Method;
496f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff}
497f1afaf6fe2d94ab265299853f288b676694f7554Steve Naroff
49861478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian/// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
49961478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian/// list of a qualified objective pointer type.
50061478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz JahanianObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
50161478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian                                              const ObjCObjectPointerType *OPT,
50261478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian                                              bool Instance)
50361478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian{
50461478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian  ObjCMethodDecl *MD = 0;
50561478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
50661478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian       E = OPT->qual_end(); I != E; ++I) {
50761478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian    ObjCProtocolDecl *PROTO = (*I);
50861478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian    if ((MD = PROTO->lookupMethod(Sel, Instance))) {
50961478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian      return MD;
51061478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian    }
51161478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian  }
51261478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian  return 0;
51361478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian}
51461478065fbcafcf5295bb0fb796c9a92f2d861e0Fariborz Jahanian
5157f81652f97a69ae8b514893a69c0245253687e55Chris Lattner/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
5167f81652f97a69ae8b514893a69c0245253687e55Chris Lattner/// objective C interface.  This is a property reference expression.
51760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::
5187f81652f97a69ae8b514893a69c0245253687e55Chris LattnerHandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
519b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner                          Expr *BaseExpr, DeclarationName MemberName,
5208ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                          SourceLocation MemberLoc,
5218ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                          SourceLocation SuperLoc, QualType SuperType,
5228ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                          bool Super) {
5237f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
5247f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  ObjCInterfaceDecl *IFace = IFaceT->getDecl();
525109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor
526109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor  if (MemberName.getNameKind() != DeclarationName::Identifier) {
527109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor    Diag(MemberLoc, diag::err_invalid_property_name)
528109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor      << MemberName << QualType(OPT, 0);
529109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor    return ExprError();
530109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor  }
531109ec1b05664e35e710785314c12552307f39a7dDouglas Gregor
5327f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
5337f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
5348b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian  if (IFace->isForwardDecl()) {
5358b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian    Diag(MemberLoc, diag::err_property_not_found_forward_class)
5368b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian         << MemberName << QualType(OPT, 0);
5378b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian    Diag(IFace->getLocation(), diag::note_forward_class);
5388b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian    return ExprError();
5398b1aba495744bea7093899a65f08c3987263061cFariborz Jahanian  }
5407f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // Search for a declared property first.
5417f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
5427f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    // Check whether we can reference this property.
5437f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    if (DiagnoseUseOfDecl(PD, MemberLoc))
5447f81652f97a69ae8b514893a69c0245253687e55Chris Lattner      return ExprError();
5457f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    QualType ResTy = PD->getType();
54614086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian    ResTy = ResTy.getNonLValueExprType(Context);
5477f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
5487f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
549926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    if (Getter &&
550926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        (Getter->hasRelatedResultType()
551926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor         || DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)))
552926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        ResTy = getMessageSendResultType(QualType(OPT, 0), Getter, false,
553926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                         Super);
554926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
5558ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian    if (Super)
5568ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian      return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
557f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     VK_LValue, OK_ObjCProperty,
5588ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                                     MemberLoc,
5598ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                                     SuperLoc, SuperType));
5608ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian    else
5618ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian      return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
562f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     VK_LValue, OK_ObjCProperty,
5638ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                                     MemberLoc, BaseExpr));
5647f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  }
5657f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // Check protocols on qualified interfaces.
5667f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
5677f81652f97a69ae8b514893a69c0245253687e55Chris Lattner       E = OPT->qual_end(); I != E; ++I)
5687f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
5697f81652f97a69ae8b514893a69c0245253687e55Chris Lattner      // Check whether we can reference this property.
5707f81652f97a69ae8b514893a69c0245253687e55Chris Lattner      if (DiagnoseUseOfDecl(PD, MemberLoc))
5717f81652f97a69ae8b514893a69c0245253687e55Chris Lattner        return ExprError();
572926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
573926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      QualType T = PD->getType();
574926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
575926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        T = getMessageSendResultType(QualType(OPT, 0), Getter, false, Super);
5768ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian      if (Super)
577926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        return Owned(new (Context) ObjCPropertyRefExpr(PD, T,
578f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                       VK_LValue,
579f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                       OK_ObjCProperty,
580f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                       MemberLoc,
581f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                       SuperLoc, SuperType));
5828ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian      else
583926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        return Owned(new (Context) ObjCPropertyRefExpr(PD, T,
584f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                       VK_LValue,
585f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                       OK_ObjCProperty,
5868ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                                       MemberLoc,
5878ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                                       BaseExpr));
5887f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    }
5897f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // If that failed, look for an "implicit" property by seeing if the nullary
5907f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // selector is implemented.
5917f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
5927f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // FIXME: The logic for looking up nullary and unary selectors should be
5937f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // shared with the code in ActOnInstanceMessage.
5947f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
5957f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
5967f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
59727569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian
59827569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian  // May be founf in property's qualified list.
59927569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian  if (!Getter)
60027569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian    Getter = LookupMethodInQualifiedType(Sel, OPT, true);
6017f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
6027f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // If this reference is in an @implementation, check for 'private' methods.
6037f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (!Getter)
60474b2756bc1f1f5f7c189996fe7e4cd3efef70263Fariborz Jahanian    Getter = IFace->lookupPrivateMethod(Sel);
6057f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
6067f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // Look through local category implementations associated with the class.
6077f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (!Getter)
6087f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    Getter = IFace->getCategoryInstanceMethod(Sel);
6097f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (Getter) {
6107f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    // Check if we can reference this property.
6117f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    if (DiagnoseUseOfDecl(Getter, MemberLoc))
6127f81652f97a69ae8b514893a69c0245253687e55Chris Lattner      return ExprError();
6137f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  }
6147f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // If we found a getter then this may be a valid dot-reference, we
6157f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // will look for the matching setter, in case it is needed.
6167f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  Selector SetterSel =
6177f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    SelectorTable::constructSetterName(PP.getIdentifierTable(),
6187f81652f97a69ae8b514893a69c0245253687e55Chris Lattner                                       PP.getSelectorTable(), Member);
6197f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
62027569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian
62127569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian  // May be founf in property's qualified list.
62227569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian  if (!Setter)
62327569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian    Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
62427569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian
6257f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (!Setter) {
6267f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    // If this reference is in an @implementation, also check for 'private'
6277f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    // methods.
62874b2756bc1f1f5f7c189996fe7e4cd3efef70263Fariborz Jahanian    Setter = IFace->lookupPrivateMethod(SetterSel);
6297f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  }
6307f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // Look through local category implementations associated with the class.
6317f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (!Setter)
6327f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    Setter = IFace->getCategoryInstanceMethod(SetterSel);
63327569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian
6347f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
6357f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    return ExprError();
6367f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
63799130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian  if (Getter || Setter) {
63899130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian    QualType PType;
63999130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian    if (Getter)
640926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      PType = getMessageSendResultType(QualType(OPT, 0), Getter, false, Super);
64199130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian    else {
64299130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian      ParmVarDecl *ArgDecl = *Setter->param_begin();
64399130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian      PType = ArgDecl->getType();
64499130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian    }
64599130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian
6460943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    ExprValueKind VK = VK_LValue;
6470943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    ExprObjectKind OK = OK_ObjCProperty;
6480943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
6490943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall        PType->isVoidType())
6500943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall      VK = VK_RValue, OK = OK_Ordinary;
6510943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall
6528ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian    if (Super)
65312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall      return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
65412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                     PType, VK, OK,
65512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                     MemberLoc,
65612f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                     SuperLoc, SuperType));
6578ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian    else
65812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall      return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
65912f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                     PType, VK, OK,
66012f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                     MemberLoc, BaseExpr));
6618ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian
6627f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  }
6637f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
6647f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  // Attempt to correct for typos in property names.
6657f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
666aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor  if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
6677f81652f97a69ae8b514893a69c0245253687e55Chris Lattner      Res.getAsSingle<ObjCPropertyDecl>()) {
668b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner    DeclarationName TypoResult = Res.getLookupName();
6697f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    Diag(MemberLoc, diag::err_property_not_found_suggest)
670b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner      << MemberName << QualType(OPT, 0) << TypoResult
671b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner      << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
6727f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
6737f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    Diag(Property->getLocation(), diag::note_previous_decl)
6747f81652f97a69ae8b514893a69c0245253687e55Chris Lattner      << Property->getDeclName();
6758ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian    return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
6768ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                     SuperLoc, SuperType, Super);
6777f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  }
67841aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian  ObjCInterfaceDecl *ClassDeclared;
67941aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian  if (ObjCIvarDecl *Ivar =
68041aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian      IFace->lookupInstanceVariable(Member, ClassDeclared)) {
68141aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian    QualType T = Ivar->getType();
68241aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian    if (const ObjCObjectPointerType * OBJPT =
68341aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian        T->getAsObjCInterfacePointerType()) {
68441aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian      const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
68541aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian      if (ObjCInterfaceDecl *IFace = IFaceT->getDecl())
68641aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian        if (IFace->isForwardDecl()) {
68741aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian          Diag(MemberLoc, diag::err_property_not_as_forward_class)
6882a96bf5e66731bb54dff3e4aadfbbced83377530Fariborz Jahanian          << MemberName << IFace;
68941aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian          Diag(IFace->getLocation(), diag::note_forward_class);
69041aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian          return ExprError();
69141aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian        }
69241aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian    }
69341aadbc530f071fe5ccbfc2560899a4a2e74c057Fariborz Jahanian  }
694b9d4fc1f54924a7b242fb763192a40c19fa6103dChris Lattner
6957f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  Diag(MemberLoc, diag::err_property_not_found)
6967f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    << MemberName << QualType(OPT, 0);
69799130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian  if (Setter)
6987f81652f97a69ae8b514893a69c0245253687e55Chris Lattner    Diag(Setter->getLocation(), diag::note_getter_unavailable)
69999130e5a02e93282cb393d2cba0d3dffc10abc01Fariborz Jahanian          << MemberName << BaseExpr->getSourceRange();
7007f81652f97a69ae8b514893a69c0245253687e55Chris Lattner  return ExprError();
7017f81652f97a69ae8b514893a69c0245253687e55Chris Lattner}
7027f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
7037f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
7047f81652f97a69ae8b514893a69c0245253687e55Chris Lattner
70560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::
706eb483eb3ee80300f15d6d13573d82493c2194461Chris LattnerActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
707eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner                          IdentifierInfo &propertyName,
708eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner                          SourceLocation receiverNameLoc,
709eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner                          SourceLocation propertyNameLoc) {
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
711f06cdae9c68dfc4191fbf6b9e5ea0fd748488d88Douglas Gregor  IdentifierInfo *receiverNamePtr = &receiverName;
712c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor  ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
713c83c6874e3bf1432d3df5e8d3530f8561ff5441fDouglas Gregor                                                  receiverNameLoc);
714926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
715926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  bool IsSuper = false;
716eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  if (IFace == 0) {
717eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner    // If the "receiver" is 'super' in a method, handle it as an expression-like
718eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner    // property reference.
71926743b20e4a8c2a986e6453f0c38beba0afef633John McCall    if (receiverNamePtr->isStr("super")) {
720926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      IsSuper = true;
721926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
72226743b20e4a8c2a986e6453f0c38beba0afef633John McCall      if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
723eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner        if (CurMethod->isInstanceMethod()) {
724eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner          QualType T =
725eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner            Context.getObjCInterfaceType(CurMethod->getClassInterface());
726eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner          T = Context.getObjCObjectPointerType(T);
727eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
728eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner          return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
7298ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                           /*BaseExpr*/0, &propertyName,
7308ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                           propertyNameLoc,
7318ac2d449820fd0df00fcbde5bf82165c1f49854dFariborz Jahanian                                           receiverNameLoc, T, true);
732eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner        }
733eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
734eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner        // Otherwise, if this is a class method, try dispatching to our
735eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner        // superclass.
736eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner        IFace = CurMethod->getClassInterface()->getSuperClass();
737eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner      }
73826743b20e4a8c2a986e6453f0c38beba0afef633John McCall    }
739eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
740eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner    if (IFace == 0) {
741eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner      Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
742eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner      return ExprError();
743eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner    }
7448149a5786def747af783a9e3c22714bb7ab42b9cFariborz Jahanian  }
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
746eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Search for a declared property first.
74761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
74817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
74961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
75061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  // If this reference is in an @implementation, check for 'private' methods.
75161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  if (!Getter)
75261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
75361f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff      if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
75487018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis        if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
75517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis          Getter = ImpDecl->getClassMethod(Sel);
75661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
75761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  if (Getter) {
75861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    // FIXME: refactor/share with ActOnMemberReference().
75961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    // Check if we can reference this property.
76061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
76161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff      return ExprError();
76261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  }
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76461f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  // Look for the matching setter, in case it is needed.
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Selector SetterSel =
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SelectorTable::constructSetterName(PP.getIdentifierTable(),
767fdc92b7877535e6264fe43cfbdc8f01e9b224f81Steve Naroff                                       PP.getSelectorTable(), &propertyName);
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
77061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  if (!Setter) {
77161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    // If this reference is in an @implementation, also check for 'private'
77261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    // methods.
77361f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
77461f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff      if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
77587018775ed689d0a67357cf767747166044b3a27Argyrios Kyrtzidis        if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
77617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis          Setter = ImpDecl->getClassMethod(SetterSel);
77761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  }
77861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  // Look through local category implementations associated with the class.
7791cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis  if (!Setter)
7801cb35dd4840d21cec58648361180d5688446a9caArgyrios Kyrtzidis    Setter = IFace->getCategoryClassMethod(SetterSel);
78161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
78261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
78361f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    return ExprError();
78461f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
78561f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  if (Getter || Setter) {
78661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    QualType PType;
78761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
7880943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    ExprValueKind VK = VK_LValue;
7890943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    if (Getter) {
790926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      PType = getMessageSendResultType(Context.getObjCInterfaceType(IFace),
791926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                       Getter, true,
792926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                       receiverNamePtr->isStr("super"));
7930943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall      if (!getLangOptions().CPlusPlus &&
7940943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall          !PType.hasQualifiers() && PType->isVoidType())
7950943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall        VK = VK_RValue;
7960943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    } else {
79761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff      for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
79861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff           E = Setter->param_end(); PI != E; ++PI)
79961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff        PType = (*PI)->getType();
8000943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall      VK = VK_LValue;
80161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    }
8020943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall
8030943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall    ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
8040943168ac126b8047f30f6bd215fbe7db14d61baJohn McCall
805926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    if (IsSuper)
806926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
807926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                   PType, VK, OK,
808926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                   propertyNameLoc,
809926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                                   receiverNameLoc,
810926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                          Context.getObjCInterfaceType(IFace)));
811926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor
81212f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall    return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
81312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                   PType, VK, OK,
81412f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                   propertyNameLoc,
81512f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall                                                   receiverNameLoc, IFace));
81661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  }
81761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
81861f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff                     << &propertyName << Context.getObjCInterfaceType(IFace));
81961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff}
82061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
82147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas GregorSema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
8221569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                               IdentifierInfo *Name,
82347bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor                                               SourceLocation NameLoc,
82447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor                                               bool IsSuper,
8251569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                               bool HasTrailingDot,
826b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                               ParsedType &ReceiverType) {
827b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ReceiverType = ParsedType();
8281569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
82947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  // If the identifier is "super" and there is no trailing dot, we're
83095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  // messaging super. If the identifier is "super" and there is a
83195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  // trailing dot, it's an instance message.
83295f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  if (IsSuper && S->isInObjcMethodScope())
83395f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor    return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
83447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor
83547bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
83647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  LookupName(Result, S);
83747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor
83847bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  switch (Result.getResultKind()) {
83947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  case LookupResult::NotFound:
840ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor    // Normal name lookup didn't find anything. If we're in an
841ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor    // Objective-C method, look for ivars. If we find one, we're done!
84295f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor    // FIXME: This is a hack. Ivar lookup should be part of normal
84395f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor    // lookup.
844ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor    if (ObjCMethodDecl *Method = getCurMethodDecl()) {
845ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor      ObjCInterfaceDecl *ClassDeclared;
846ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor      if (Method->getClassInterface()->lookupInstanceVariable(Name,
847ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor                                                              ClassDeclared))
848ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor        return ObjCInstanceMessage;
849ed46442adea496dfb01dbbe53ace583d5614e79aDouglas Gregor    }
85095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
85147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    // Break out; we'll perform typo correction below.
85247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    break;
85347bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor
85447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  case LookupResult::NotFoundInCurrentInstantiation:
85547bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  case LookupResult::FoundOverloaded:
85647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  case LookupResult::FoundUnresolvedValue:
85747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  case LookupResult::Ambiguous:
85847bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    Result.suppressDiagnostics();
85947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    return ObjCInstanceMessage;
86047bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor
86147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  case LookupResult::Found: {
8628348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian    // If the identifier is a class or not, and there is a trailing dot,
8638348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian    // it's an instance message.
8648348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian    if (HasTrailingDot)
8658348de33e92b8278a07a7573eb7ad3c166c02b0dFariborz Jahanian      return ObjCInstanceMessage;
86647bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    // We found something. If it's a type, then we have a class
86747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    // message. Otherwise, it's an instance message.
86847bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    NamedDecl *ND = Result.getFoundDecl();
8691569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    QualType T;
8701569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
8711569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      T = Context.getObjCInterfaceType(Class);
8721569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
8731569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      T = Context.getTypeDeclType(Type);
8741569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    else
8751569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ObjCInstanceMessage;
8761569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
8771569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    //  We have a class message, and T is the type we're
8781569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    //  messaging. Build source-location information for it.
8791569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
880b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ReceiverType = CreateParsedType(T, TSInfo);
8811569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    return ObjCClassMessage;
88247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  }
88347bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  }
88447bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor
885aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor  // Determine our typo-correction context.
886aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor  CorrectTypoContext CTC = CTC_Expression;
887aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor  if (ObjCMethodDecl *Method = getCurMethodDecl())
888aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor    if (Method->getClassInterface() &&
889aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor        Method->getClassInterface()->getSuperClass())
890aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      CTC = CTC_ObjCMessageReceiver;
891aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor
892aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor  if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
893aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor    if (Result.isSingleResult()) {
894aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      // If we found a declaration, correct when it refers to an Objective-C
895aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      // class.
896aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      NamedDecl *ND = Result.getFoundDecl();
8971569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
898aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor        Diag(NameLoc, diag::err_unknown_receiver_suggest)
899aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor          << Name << Result.getLookupName()
900aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor          << FixItHint::CreateReplacement(SourceRange(NameLoc),
901aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor                                          ND->getNameAsString());
902aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor        Diag(ND->getLocation(), diag::note_previous_decl)
903aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor          << Corrected;
904aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor
9051569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor        QualType T = Context.getObjCInterfaceType(Class);
9061569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor        TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
907b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall        ReceiverType = CreateParsedType(T, TSInfo);
908aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor        return ObjCClassMessage;
909aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      }
910aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor    } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
911aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor               Corrected.getAsIdentifierInfo()->isStr("super")) {
912aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      // If we've found the keyword "super", this is a send to super.
91347bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor      Diag(NameLoc, diag::err_unknown_receiver_suggest)
914aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor        << Name << Corrected
915aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor        << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
916aaf87162c5fbfbf320072da3a8e83392e1bbf041Douglas Gregor      return ObjCSuperMessage;
91747bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor    }
91847bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  }
91947bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor
92047bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  // Fall back: let the parser try to parse it as an instance message.
92147bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor  return ObjCInstanceMessage;
92247bd54392a4fd0f10e04de6a0420fd4838caaa0eDouglas Gregor}
92361f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
92460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnSuperMessage(Scope *S,
9250fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation SuperLoc,
9260fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   Selector Sel,
9270fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation LBracLoc,
9280fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation SelectorLoc,
9290fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation RBracLoc,
9300fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   MultiExprArg Args) {
9312725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Determine whether we are inside a method or not.
93226743b20e4a8c2a986e6453f0c38beba0afef633John McCall  ObjCMethodDecl *Method = tryCaptureObjCSelf();
933f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  if (!Method) {
934f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
935f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    return ExprError();
936f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  }
9372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
938f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  ObjCInterfaceDecl *Class = Method->getClassInterface();
939f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  if (!Class) {
940f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    Diag(SuperLoc, diag::error_no_super_class_message)
941f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor      << Method->getDeclName();
9422725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
94315faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner  }
9442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
945f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  ObjCInterfaceDecl *Super = Class->getSuperClass();
9462725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (!Super) {
947f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    // The current class does not have a superclass.
948e00909a6e997ec33d9baa5312e9d27b52a3da770Ted Kremenek    Diag(SuperLoc, diag::error_root_class_cannot_use_super)
949e00909a6e997ec33d9baa5312e9d27b52a3da770Ted Kremenek      << Class->getIdentifier();
9502725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
9512725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  }
9522725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
953f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  // We are in a method whose class has a superclass, so 'super'
954f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  // is acting as a keyword.
955f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  if (Method->isInstanceMethod()) {
956f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    // Since we are in an instance method, this is an instance
957f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    // message to the superclass instance.
958f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    QualType SuperTy = Context.getObjCInterfaceType(Super);
959f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor    SuperTy = Context.getObjCObjectPointerType(SuperTy);
9609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return BuildInstanceMessage(0, SuperTy, SuperLoc,
961f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                Sel, /*Method=*/0,
962f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                LBracLoc, SelectorLoc, RBracLoc, move(Args));
9632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  }
964f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor
965f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  // Since we are in a class method, this is a class message to
966f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  // the superclass.
967f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor  return BuildClassMessage(/*ReceiverTypeInfo=*/0,
968f95861a4ab6bbd6a975ed079dd70eb1cc22f4467Douglas Gregor                           Context.getObjCInterfaceType(Super),
969f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                           SuperLoc, Sel, /*Method=*/0,
970f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                           LBracLoc, SelectorLoc, RBracLoc, move(Args));
9712725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor}
9722725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
9732725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Build an Objective-C class message expression.
9742725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
9752725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine takes care of both normal class messages and
9762725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class messages to the superclass.
9772725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
9782725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverTypeInfo Type source information that describes the
9792725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// receiver of this message. This may be NULL, in which case we are
9802725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// sending to the superclass and \p SuperLoc must be a valid source
9812725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// location.
9822725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
9832725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType The type of the object receiving the
9842725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
9852725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// type as that refers to. For a superclass send, this is the type of
9862725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// the superclass.
9872725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
9882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc The location of the "super" keyword in a
9892725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// superclass message.
9902725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
9912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Sel The selector to which the message is being sent.
9922725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
993f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// \param Method The method that this class message is invoking, if
994f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// already known.
995f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor///
9962725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening square bracket ']'.
9972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
9982725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param RBrac The location of the closing square bracket ']'.
9992725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
10002725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Args The message arguments.
100160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
10020fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   QualType ReceiverType,
10030fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation SuperLoc,
10040fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   Selector Sel,
10050fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   ObjCMethodDecl *Method,
10060fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation LBracLoc,
1007f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                   SourceLocation SelectorLoc,
10080fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   SourceLocation RBracLoc,
10090fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor                                   MultiExprArg ArgsIn) {
10100fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  SourceLocation Loc = SuperLoc.isValid()? SuperLoc
10119497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
10120fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  if (LBracLoc.isInvalid()) {
10130fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    Diag(Loc, diag::err_missing_open_square_message_send)
10140fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor      << FixItHint::CreateInsertion(Loc, "[");
10150fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    LBracLoc = Loc;
10160fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  }
10170fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
101892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor  if (ReceiverType->isDependentType()) {
101992e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    // If the receiver type is dependent, we can't type-check anything
102092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    // at this point. Build a dependent expression.
102192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    unsigned NumArgs = ArgsIn.size();
102292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
102392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    assert(SuperLoc.isInvalid() && "Message to super with dependent type");
1024f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
1025f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                         VK_RValue, LBracLoc, ReceiverTypeInfo,
1026f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                         Sel, SelectorLoc, /*Method=*/0,
102792e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor                                         Args, NumArgs, RBracLoc));
102892e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor  }
102915faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner
10302725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Find the class to which we are sending this message.
10312725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  ObjCInterfaceDecl *Class = 0;
1032c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
1033c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ClassType || !(Class = ClassType->getInterface())) {
10342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    Diag(Loc, diag::err_invalid_receiver_class_message)
10352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      << ReceiverType;
10362725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
10377c778f1c549a8ae95d50a819fd537df78da16426Steve Naroff  }
10382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  assert(Class && "We don't know which class we're messaging?");
103902b0d651ecd6a4d5171dd0208c0c570c3d7103b7Fariborz Jahanian  (void)DiagnoseUseOfDecl(Class, Loc);
10402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Find the method we are messaging.
1041f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor  if (!Method) {
1042f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    if (Class->isForwardDecl()) {
1043f85e193739c953358c865005855253af4f68a497John McCall      if (getLangOptions().ObjCAutoRefCount) {
1044f85e193739c953358c865005855253af4f68a497John McCall        Diag(Loc, diag::err_arc_receiver_forward_class) << ReceiverType;
1045f85e193739c953358c865005855253af4f68a497John McCall      } else {
1046f85e193739c953358c865005855253af4f68a497John McCall        Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
1047f85e193739c953358c865005855253af4f68a497John McCall      }
1048f85e193739c953358c865005855253af4f68a497John McCall
1049f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      // A forward class used in messaging is treated as a 'Class'
1050f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      Method = LookupFactoryMethodInGlobalPool(Sel,
1051f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                                               SourceRange(LBracLoc, RBracLoc));
1052f85e193739c953358c865005855253af4f68a497John McCall      if (Method && !getLangOptions().ObjCAutoRefCount)
1053f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        Diag(Method->getLocation(), diag::note_method_sent_forward_class)
1054f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          << Method->getDeclName();
1055f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    }
1056f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    if (!Method)
1057f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      Method = Class->lookupClassMethod(Sel);
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1059f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    // If we have an implementation in scope, check "private" methods.
1060f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    if (!Method)
1061f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      Method = LookupPrivateClassMethod(Sel, Class);
10627c778f1c549a8ae95d50a819fd537df78da16426Steve Naroff
1063f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    if (Method && DiagnoseUseOfDecl(Method, Loc))
1064f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      return ExprError();
1065f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor  }
10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10672725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Check the argument types and determine the result type.
10682725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  QualType ReturnType;
1069f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  ExprValueKind VK = VK_RValue;
1070f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall
10712725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  unsigned NumArgs = ArgsIn.size();
10722725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1073926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, true,
1074926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                SuperLoc.isValid(), LBracLoc, RBracLoc,
1075926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                ReturnType, VK))
10762725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
10774df728e368fa1f65ffc57572fed613dcca5b4fe8Ted Kremenek
1078483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor  if (Method && !Method->getResultType()->isVoidType() &&
1079483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor      RequireCompleteType(LBracLoc, Method->getResultType(),
1080483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor                          diag::err_illegal_message_expr_incomplete_type))
1081483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor    return ExprError();
1082483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor
10832725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Construct the appropriate ObjCMessageExpr.
10842d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor  Expr *Result;
10852725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
1086f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
10872d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor                                     SuperLoc, /*IsInstanceSuper=*/false,
1088f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     ReceiverType, Sel, SelectorLoc,
1089f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     Method, Args, NumArgs, RBracLoc);
10902d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor  else
1091f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
1092f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     ReceiverTypeInfo, Sel, SelectorLoc,
1093f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     Method, Args, NumArgs, RBracLoc);
10942d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor  return MaybeBindToTemporary(Result);
109585a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner}
109685a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner
10972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// ActOnClassMessage - used for both unary and keyword messages.
109885a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// ArgExprs is optional - if it is present, the number of expressions
109985a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner// is obtained from Sel.getNumArgs().
110060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnClassMessage(Scope *S,
110177328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor                                   ParsedType Receiver,
110277328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor                                   Selector Sel,
110377328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor                                   SourceLocation LBracLoc,
110477328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor                                   SourceLocation SelectorLoc,
110577328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor                                   SourceLocation RBracLoc,
110677328d1bb92c2c46bc3e4badc4b4b97c517903b7Douglas Gregor                                   MultiExprArg Args) {
11072725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  TypeSourceInfo *ReceiverTypeInfo;
11082725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
11092725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (ReceiverType.isNull())
11102725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11132725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (!ReceiverTypeInfo)
11142725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
1117f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                           /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
1118f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                           LBracLoc, SelectorLoc, RBracLoc, move(Args));
11192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor}
112087d3ef08d892df8264bd51adb6ddd4a22422cd29Steve Naroff
11212725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Build an Objective-C instance message expression.
11222725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11232725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine takes care of both normal instance messages and
11242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// instance messages to the superclass instance.
11252725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Receiver The expression that computes the object that will
11272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// receive this message. This may be empty, in which case we are
11282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// sending to the superclass instance and \p SuperLoc must be a valid
11292725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// source location.
11302725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11312725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType The (static) type of the object receiving the
11322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// message. When a \p Receiver expression is provided, this is the
11332725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// same type as that expression. For a superclass instance send, this
11342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// is a pointer to the type of the superclass.
11352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11362725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc The location of the "super" keyword in a
11372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// superclass instance message.
11382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Sel The selector to which the message is being sent.
11402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
1141f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// \param Method The method that this instance message is invoking, if
1142f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor/// already known.
1143f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor///
11442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening square bracket ']'.
11452725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11462725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param RBrac The location of the closing square bracket ']'.
11472725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
11482725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param Args The message arguments.
114960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::BuildInstanceMessage(Expr *Receiver,
1150f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      QualType ReceiverType,
1151f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      SourceLocation SuperLoc,
1152f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      Selector Sel,
1153f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      ObjCMethodDecl *Method,
1154f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      SourceLocation LBracLoc,
1155f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      SourceLocation SelectorLoc,
1156f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      SourceLocation RBracLoc,
1157f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                      MultiExprArg ArgsIn) {
11580fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  // The location of the receiver.
11590fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
11600fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
11610fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  if (LBracLoc.isInvalid()) {
11620fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    Diag(Loc, diag::err_missing_open_square_message_send)
11630fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor      << FixItHint::CreateInsertion(Loc, "[");
11640fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor    LBracLoc = Loc;
11650fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  }
11660fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
11672725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // If we have a receiver expression, perform appropriate promotions
11682725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // and determine receiver type.
11692725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (Receiver) {
117092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    if (Receiver->isTypeDependent()) {
117192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      // If the receiver is type-dependent, we can't type-check anything
117292e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      // at this point. Build a dependent expression.
117392e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      unsigned NumArgs = ArgsIn.size();
117492e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
117592e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      assert(SuperLoc.isInvalid() && "Message to super with dependent type");
117692e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor      return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
1177f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                           VK_RValue, LBracLoc, Receiver, Sel,
1178f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                           SelectorLoc, /*Method=*/0,
1179f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                           Args, NumArgs, RBracLoc));
118092e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor    }
118192e986e0adb79e8a47f738bd608e6c97c547641dDouglas Gregor
11822725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    // If necessary, apply function/array conversion to the receiver.
11832725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    // C99 6.7.5.3p[7,8].
1184429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
1185429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (Result.isInvalid())
1186429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      return ExprError();
1187429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    Receiver = Result.take();
11882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    ReceiverType = Receiver->getType();
11892725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  }
119004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
1191f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor  if (!Method) {
1192f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    // Handle messages to id.
1193ba551983016ee3eac5421255d2ebe6723e61befbFariborz Jahanian    bool receiverIsId = ReceiverType->isObjCIdType();
11946b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian    if (receiverIsId || ReceiverType->isBlockPointerType() ||
1195f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
1196f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      Method = LookupInstanceMethodInGlobalPool(Sel,
11976b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian                                                SourceRange(LBracLoc, RBracLoc),
11986b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian                                                receiverIsId);
1199f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      if (!Method)
12002725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        Method = LookupFactoryMethodInGlobalPool(Sel,
12016b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian                                                 SourceRange(LBracLoc, RBracLoc),
12026b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian                                                 receiverIsId);
1203f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor    } else if (ReceiverType->isObjCClassType() ||
1204f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor               ReceiverType->isObjCQualifiedClassType()) {
1205f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      // Handle messages to Class.
1206759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian      // We allow sending a message to a qualified Class ("Class<foo>"), which
1207759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian      // is ok as long as one of the protocols implements the selector (if not, warn).
1208759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian      if (const ObjCObjectPointerType *QClassTy
1209759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            = ReceiverType->getAsObjCQualifiedClassType()) {
1210759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian        // Search protocols for class methods.
1211759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian        Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
1212759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian        if (!Method) {
1213759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
1214759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          // warn if instance method found for a Class message.
1215759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          if (Method) {
1216759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            Diag(Loc, diag::warn_instance_method_on_class_found)
1217759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian              << Method->getSelector() << Sel;
1218759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            Diag(Method->getLocation(), diag::note_method_declared_at);
1219759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          }
12206b9dfd4257b85c388a9e3cd345cf28acb7351006Steve Naroff        }
1221759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian      } else {
1222759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian        if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1223759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1224759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            // First check the public methods in the class interface.
1225759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            Method = ClassDecl->lookupClassMethod(Sel);
1226759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian
1227759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            if (!Method)
1228759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian              Method = LookupPrivateClassMethod(Sel, ClassDecl);
1229759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          }
1230759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          if (Method && DiagnoseUseOfDecl(Method, Loc))
1231759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            return ExprError();
1232759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian        }
1233759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian        if (!Method) {
1234759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          // If not messaging 'self', look for any factory method named 'Sel'.
1235759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian          if (!Receiver || !isSelfExpr(Receiver)) {
1236759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            Method = LookupFactoryMethodInGlobalPool(Sel,
1237759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                                                SourceRange(LBracLoc, RBracLoc),
1238759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                                                     true);
1239759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            if (!Method) {
1240759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian              // If no class (factory) method was found, check if an _instance_
1241759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian              // method of the same name exists in the root class only.
1242759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian              Method = LookupInstanceMethodInGlobalPool(Sel,
12436b308f6dc7d8f1581c52095f435c0e1284b111d8Fariborz Jahanian                                               SourceRange(LBracLoc, RBracLoc),
1244759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                                                        true);
1245759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian              if (Method)
1246759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                  if (const ObjCInterfaceDecl *ID =
1247759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                      dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1248759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                    if (ID->getSuperClass())
1249759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                      Diag(Loc, diag::warn_root_inst_method_not_found)
1250759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                      << Sel << SourceRange(LBracLoc, RBracLoc);
1251759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian                  }
1252759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian            }
125304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor          }
125404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor        }
125504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor      }
125604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    } else {
1257f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      ObjCInterfaceDecl* ClassDecl = 0;
1258f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor
1259f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1260f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      // long as one of the protocols implements the selector (if not, warn).
1261f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      if (const ObjCObjectPointerType *QIdTy
1262f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                                   = ReceiverType->getAsObjCQualifiedIdType()) {
1263f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        // Search protocols for instance methods.
126427569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian        Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
126527569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian        if (!Method)
126627569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian          Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
1267f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      } else if (const ObjCObjectPointerType *OCIType
1268f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                   = ReceiverType->getAsObjCInterfacePointerType()) {
1269f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        // We allow sending a message to a pointer to an interface (an object).
1270f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        ClassDecl = OCIType->getInterfaceDecl();
1271f85e193739c953358c865005855253af4f68a497John McCall
1272f85e193739c953358c865005855253af4f68a497John McCall        if (ClassDecl->isForwardDecl() && getLangOptions().ObjCAutoRefCount) {
1273f85e193739c953358c865005855253af4f68a497John McCall          Diag(Loc, diag::err_arc_receiver_forward_instance)
1274f85e193739c953358c865005855253af4f68a497John McCall            << OCIType->getPointeeType()
1275f85e193739c953358c865005855253af4f68a497John McCall            << (Receiver ? Receiver->getSourceRange() : SourceRange(SuperLoc));
1276f85e193739c953358c865005855253af4f68a497John McCall          return ExprError();
1277f85e193739c953358c865005855253af4f68a497John McCall        }
1278f85e193739c953358c865005855253af4f68a497John McCall
1279f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1280f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        // faster than the following method (which can do *many* linear searches).
1281db9d2145f1d85f64dba2c9b92416621ce01090a6Sebastian Redl        // The idea is to add class info to MethodPool.
1282f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        Method = ClassDecl->lookupInstanceMethod(Sel);
1283f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor
128427569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian        if (!Method)
1285f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          // Search protocol qualifiers.
128627569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian          Method = LookupMethodInQualifiedType(Sel, OCIType, true);
128727569b0f3e1b27f609a2e104b39bad077850e210Fariborz Jahanian
128889ebaed91cca7fd296ec7804e4e9fb68949c1d0eFariborz Jahanian        const ObjCInterfaceDecl *forwardClass = 0;
1289f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        if (!Method) {
1290f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          // If we have implementations in scope, check "private" methods.
1291f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1292f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor
1293f85e193739c953358c865005855253af4f68a497John McCall          if (!Method && getLangOptions().ObjCAutoRefCount) {
1294f85e193739c953358c865005855253af4f68a497John McCall            Diag(Loc, diag::err_arc_may_not_respond)
1295f85e193739c953358c865005855253af4f68a497John McCall              << OCIType->getPointeeType() << Sel;
1296f85e193739c953358c865005855253af4f68a497John McCall            return ExprError();
1297f85e193739c953358c865005855253af4f68a497John McCall          }
1298f85e193739c953358c865005855253af4f68a497John McCall
1299f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1300f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor            // If we still haven't found a method, look in the global pool. This
1301f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor            // behavior isn't very desirable, however we need it for GCC
1302f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor            // compatibility. FIXME: should we deviate??
1303f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor            if (OCIType->qual_empty()) {
1304f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor              Method = LookupInstanceMethodInGlobalPool(Sel,
13058e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian                                                 SourceRange(LBracLoc, RBracLoc));
130689ebaed91cca7fd296ec7804e4e9fb68949c1d0eFariborz Jahanian              if (OCIType->getInterfaceDecl()->isForwardDecl())
130789ebaed91cca7fd296ec7804e4e9fb68949c1d0eFariborz Jahanian                forwardClass = OCIType->getInterfaceDecl();
13088e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian              if (Method && !forwardClass)
1309f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                Diag(Loc, diag::warn_maynot_respond)
1310f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                  << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1311f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor            }
1312f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          }
1313f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        }
13148e5fc9be37c6828ad008f22730e3baac1bef1686Fariborz Jahanian        if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
1315f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          return ExprError();
1316f85e193739c953358c865005855253af4f68a497John McCall      } else if (!getLangOptions().ObjCAutoRefCount &&
1317f85e193739c953358c865005855253af4f68a497John McCall                 !Context.getObjCIdType().isNull() &&
1318f60946222721d9ba3c059563935c17b84703187aDouglas Gregor                 (ReceiverType->isPointerType() ||
1319f60946222721d9ba3c059563935c17b84703187aDouglas Gregor                  ReceiverType->isIntegerType())) {
1320f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        // Implicitly convert integers and pointers to 'id' but emit a warning.
1321f85e193739c953358c865005855253af4f68a497John McCall        // But not in ARC.
1322f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        Diag(Loc, diag::warn_bad_receiver_type)
1323f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          << ReceiverType
1324f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor          << Receiver->getSourceRange();
1325f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        if (ReceiverType->isPointerType())
1326429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1327429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                            CK_BitCast).take();
1328404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall        else {
1329404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall          // TODO: specialized warning on null receivers?
1330404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall          bool IsNull = Receiver->isNullPointerConstant(Context,
1331404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall                                              Expr::NPC_ValueDependentIsNull);
1332429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1333429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                            IsNull ? CK_NullToPointer : CK_IntegralToPointer).take();
1334404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall        }
1335f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor        ReceiverType = Receiver->getType();
133679d3f04688497857d29fe1f009bbc7b4b830a21bFariborz Jahanian      }
1337429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      else {
1338429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        ExprResult ReceiverRes;
1339429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        if (getLangOptions().CPlusPlus)
1340429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          ReceiverRes = PerformContextuallyConvertToObjCId(Receiver);
1341429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        if (ReceiverRes.isUsable()) {
1342429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          Receiver = ReceiverRes.take();
1343429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1344429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley            Receiver = ICE->getSubExpr();
1345429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley            ReceiverType = Receiver->getType();
1346429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          }
1347429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          return BuildInstanceMessage(Receiver,
1348429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      ReceiverType,
1349429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      SuperLoc,
1350429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      Sel,
1351429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      Method,
1352429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      LBracLoc,
1353429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      SelectorLoc,
1354429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      RBracLoc,
1355429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      move(ArgsIn));
1356429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        } else {
1357429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          // Reject other random receiver types (e.g. structs).
1358429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          Diag(Loc, diag::err_bad_receiver_type)
1359429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley            << ReceiverType << Receiver->getSourceRange();
1360429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          return ExprError();
13613ba606199be8056ae83596260bd6fd5872942905Fariborz Jahanian        }
1362f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor      }
136304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor    }
1364fe1a553256b46fad3c0a9e9967481bcf571339ecChris Lattner  }
13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13662725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Check the message arguments.
13672725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  unsigned NumArgs = ArgsIn.size();
13682725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
13692725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  QualType ReturnType;
1370f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  ExprValueKind VK = VK_RValue;
13712600503170c8366af2587408c50e2beedee5d1f1Fariborz Jahanian  bool ClassMessage = (ReceiverType->isObjCClassType() ||
13722600503170c8366af2587408c50e2beedee5d1f1Fariborz Jahanian                       ReceiverType->isObjCQualifiedClassType());
1373926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method,
1374926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                ClassMessage, SuperLoc.isValid(),
1375f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                LBracLoc, RBracLoc, ReturnType, VK))
13762725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
1377da59e09f1fd53350fcd949adbadd8148117f21b2Fariborz Jahanian
1378483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor  if (Method && !Method->getResultType()->isVoidType() &&
1379483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor      RequireCompleteType(LBracLoc, Method->getResultType(),
1380483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor                          diag::err_illegal_message_expr_incomplete_type))
1381483dd2f55ee53bc765123ead3428aa4ad2682cf3Douglas Gregor    return ExprError();
138204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
1383f85e193739c953358c865005855253af4f68a497John McCall  // In ARC, forbid the user from sending messages to
1384f85e193739c953358c865005855253af4f68a497John McCall  // retain/release/autorelease/dealloc/retainCount explicitly.
1385f85e193739c953358c865005855253af4f68a497John McCall  if (getLangOptions().ObjCAutoRefCount) {
1386f85e193739c953358c865005855253af4f68a497John McCall    ObjCMethodFamily family =
1387f85e193739c953358c865005855253af4f68a497John McCall      (Method ? Method->getMethodFamily() : Sel.getMethodFamily());
1388f85e193739c953358c865005855253af4f68a497John McCall    switch (family) {
1389f85e193739c953358c865005855253af4f68a497John McCall    case OMF_init:
1390f85e193739c953358c865005855253af4f68a497John McCall      if (Method)
1391f85e193739c953358c865005855253af4f68a497John McCall        checkInitMethod(Method, ReceiverType);
1392f85e193739c953358c865005855253af4f68a497John McCall
1393f85e193739c953358c865005855253af4f68a497John McCall    case OMF_None:
1394f85e193739c953358c865005855253af4f68a497John McCall    case OMF_alloc:
1395f85e193739c953358c865005855253af4f68a497John McCall    case OMF_copy:
1396f85e193739c953358c865005855253af4f68a497John McCall    case OMF_mutableCopy:
1397f85e193739c953358c865005855253af4f68a497John McCall    case OMF_new:
1398f85e193739c953358c865005855253af4f68a497John McCall    case OMF_self:
1399f85e193739c953358c865005855253af4f68a497John McCall      break;
1400f85e193739c953358c865005855253af4f68a497John McCall
1401f85e193739c953358c865005855253af4f68a497John McCall    case OMF_dealloc:
1402f85e193739c953358c865005855253af4f68a497John McCall    case OMF_retain:
1403f85e193739c953358c865005855253af4f68a497John McCall    case OMF_release:
1404f85e193739c953358c865005855253af4f68a497John McCall    case OMF_autorelease:
1405f85e193739c953358c865005855253af4f68a497John McCall    case OMF_retainCount:
1406f85e193739c953358c865005855253af4f68a497John McCall      Diag(Loc, diag::err_arc_illegal_explicit_message)
1407f85e193739c953358c865005855253af4f68a497John McCall        << Sel << SelectorLoc;
1408f85e193739c953358c865005855253af4f68a497John McCall      break;
1409f85e193739c953358c865005855253af4f68a497John McCall    }
1410f85e193739c953358c865005855253af4f68a497John McCall  }
1411f85e193739c953358c865005855253af4f68a497John McCall
14122725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  // Construct the appropriate ObjCMessageExpr instance.
1413f85e193739c953358c865005855253af4f68a497John McCall  ObjCMessageExpr *Result;
14142725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
1415f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
14162d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor                                     SuperLoc,  /*IsInstanceSuper=*/true,
1417f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     ReceiverType, Sel, SelectorLoc, Method,
14182d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor                                     Args, NumArgs, RBracLoc);
14192d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor  else
1420f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall    Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
1421f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     Receiver, Sel, SelectorLoc, Method,
1422f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                                     Args, NumArgs, RBracLoc);
1423f85e193739c953358c865005855253af4f68a497John McCall
1424f85e193739c953358c865005855253af4f68a497John McCall  if (getLangOptions().ObjCAutoRefCount) {
1425f85e193739c953358c865005855253af4f68a497John McCall    // In ARC, annotate delegate init calls.
1426f85e193739c953358c865005855253af4f68a497John McCall    if (Result->getMethodFamily() == OMF_init &&
1427f85e193739c953358c865005855253af4f68a497John McCall        (SuperLoc.isValid() || isSelfExpr(Receiver))) {
1428f85e193739c953358c865005855253af4f68a497John McCall      // Only consider init calls *directly* in init implementations,
1429f85e193739c953358c865005855253af4f68a497John McCall      // not within blocks.
1430f85e193739c953358c865005855253af4f68a497John McCall      ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext);
1431f85e193739c953358c865005855253af4f68a497John McCall      if (method && method->getMethodFamily() == OMF_init) {
1432f85e193739c953358c865005855253af4f68a497John McCall        // The implicit assignment to self means we also don't want to
1433f85e193739c953358c865005855253af4f68a497John McCall        // consume the result.
1434f85e193739c953358c865005855253af4f68a497John McCall        Result->setDelegateInitCall(true);
1435f85e193739c953358c865005855253af4f68a497John McCall        return Owned(Result);
1436f85e193739c953358c865005855253af4f68a497John McCall      }
1437f85e193739c953358c865005855253af4f68a497John McCall    }
1438f85e193739c953358c865005855253af4f68a497John McCall
1439f85e193739c953358c865005855253af4f68a497John McCall    // In ARC, check for message sends which are likely to introduce
1440f85e193739c953358c865005855253af4f68a497John McCall    // retain cycles.
1441f85e193739c953358c865005855253af4f68a497John McCall    checkRetainCycles(Result);
1442f85e193739c953358c865005855253af4f68a497John McCall  }
1443f85e193739c953358c865005855253af4f68a497John McCall
14442d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor  return MaybeBindToTemporary(Result);
14452725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor}
14462725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
14472725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// ActOnInstanceMessage - used for both unary and keyword messages.
14482725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// ArgExprs is optional - if it is present, the number of expressions
14492725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor// is obtained from Sel.getNumArgs().
145060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnInstanceMessage(Scope *S,
145160d7b3a319d84d688752be3870615ac0f111fb16John McCall                                      Expr *Receiver,
145260d7b3a319d84d688752be3870615ac0f111fb16John McCall                                      Selector Sel,
145360d7b3a319d84d688752be3870615ac0f111fb16John McCall                                      SourceLocation LBracLoc,
145460d7b3a319d84d688752be3870615ac0f111fb16John McCall                                      SourceLocation SelectorLoc,
145560d7b3a319d84d688752be3870615ac0f111fb16John McCall                                      SourceLocation RBracLoc,
145660d7b3a319d84d688752be3870615ac0f111fb16John McCall                                      MultiExprArg Args) {
14572725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (!Receiver)
14582725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    return ExprError();
145904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor
14609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return BuildInstanceMessage(Receiver, Receiver->getType(),
1461f49bb082ebf6413b2d3cb956e9c78dbb8a978c58Douglas Gregor                              /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
1462f40f0d5a382395e0301d7dcbeaa2b8e90b8973b1Argyrios Kyrtzidis                              LBracLoc, SelectorLoc, RBracLoc, move(Args));
146385a932e26f3c3faae6bad639a6d32e92794dfda9Chris Lattner}
1464eca7be6b7ebd93682eeaab2c71d59f2995dacdccChris Lattner
1465f85e193739c953358c865005855253af4f68a497John McCallenum ARCConversionTypeClass {
1466f85e193739c953358c865005855253af4f68a497John McCall  ACTC_none,
1467f85e193739c953358c865005855253af4f68a497John McCall  ACTC_retainable,
1468f85e193739c953358c865005855253af4f68a497John McCall  ACTC_indirectRetainable
1469f85e193739c953358c865005855253af4f68a497John McCall};
1470f85e193739c953358c865005855253af4f68a497John McCallstatic ARCConversionTypeClass classifyTypeForARCConversion(QualType type) {
1471f85e193739c953358c865005855253af4f68a497John McCall  ARCConversionTypeClass ACTC = ACTC_retainable;
1472f85e193739c953358c865005855253af4f68a497John McCall
1473f85e193739c953358c865005855253af4f68a497John McCall  // Ignore an outermost reference type.
1474f85e193739c953358c865005855253af4f68a497John McCall  if (const ReferenceType *ref = type->getAs<ReferenceType>())
1475f85e193739c953358c865005855253af4f68a497John McCall    type = ref->getPointeeType();
1476f85e193739c953358c865005855253af4f68a497John McCall
1477f85e193739c953358c865005855253af4f68a497John McCall  // Drill through pointers and arrays recursively.
1478f85e193739c953358c865005855253af4f68a497John McCall  while (true) {
1479f85e193739c953358c865005855253af4f68a497John McCall    if (const PointerType *ptr = type->getAs<PointerType>()) {
1480f85e193739c953358c865005855253af4f68a497John McCall      type = ptr->getPointeeType();
1481f85e193739c953358c865005855253af4f68a497John McCall    } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) {
1482f85e193739c953358c865005855253af4f68a497John McCall      type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
1483f85e193739c953358c865005855253af4f68a497John McCall    } else {
1484f85e193739c953358c865005855253af4f68a497John McCall      break;
1485f85e193739c953358c865005855253af4f68a497John McCall    }
1486f85e193739c953358c865005855253af4f68a497John McCall    ACTC = ACTC_indirectRetainable;
1487f85e193739c953358c865005855253af4f68a497John McCall  }
1488f85e193739c953358c865005855253af4f68a497John McCall
1489f85e193739c953358c865005855253af4f68a497John McCall  if (!type->isObjCRetainableType()) return ACTC_none;
1490f85e193739c953358c865005855253af4f68a497John McCall  return ACTC;
1491f85e193739c953358c865005855253af4f68a497John McCall}
1492f85e193739c953358c865005855253af4f68a497John McCall
1493f85e193739c953358c865005855253af4f68a497John McCallnamespace {
1494f85e193739c953358c865005855253af4f68a497John McCall  /// Return true if the given expression can be reasonably converted
1495f85e193739c953358c865005855253af4f68a497John McCall  /// between a retainable pointer type and a C pointer type.
1496f85e193739c953358c865005855253af4f68a497John McCall  struct ARCCastChecker : StmtVisitor<ARCCastChecker, bool> {
1497f85e193739c953358c865005855253af4f68a497John McCall    ASTContext &Context;
1498f85e193739c953358c865005855253af4f68a497John McCall    ARCCastChecker(ASTContext &Context) : Context(Context) {}
1499f85e193739c953358c865005855253af4f68a497John McCall    bool VisitStmt(Stmt *s) {
1500f85e193739c953358c865005855253af4f68a497John McCall      return false;
1501f85e193739c953358c865005855253af4f68a497John McCall    }
1502f85e193739c953358c865005855253af4f68a497John McCall    bool VisitExpr(Expr *e) {
1503f85e193739c953358c865005855253af4f68a497John McCall      return e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull);
1504f85e193739c953358c865005855253af4f68a497John McCall    }
1505f85e193739c953358c865005855253af4f68a497John McCall
1506f85e193739c953358c865005855253af4f68a497John McCall    bool VisitParenExpr(ParenExpr *e) {
1507f85e193739c953358c865005855253af4f68a497John McCall      return Visit(e->getSubExpr());
1508f85e193739c953358c865005855253af4f68a497John McCall    }
1509f85e193739c953358c865005855253af4f68a497John McCall    bool VisitCastExpr(CastExpr *e) {
1510f85e193739c953358c865005855253af4f68a497John McCall      switch (e->getCastKind()) {
1511f85e193739c953358c865005855253af4f68a497John McCall        case CK_NullToPointer:
1512f85e193739c953358c865005855253af4f68a497John McCall          return true;
1513f85e193739c953358c865005855253af4f68a497John McCall        case CK_NoOp:
1514f85e193739c953358c865005855253af4f68a497John McCall        case CK_LValueToRValue:
1515f85e193739c953358c865005855253af4f68a497John McCall        case CK_BitCast:
1516f85e193739c953358c865005855253af4f68a497John McCall        case CK_AnyPointerToObjCPointerCast:
1517f85e193739c953358c865005855253af4f68a497John McCall        case CK_AnyPointerToBlockPointerCast:
1518f85e193739c953358c865005855253af4f68a497John McCall          return Visit(e->getSubExpr());
1519f85e193739c953358c865005855253af4f68a497John McCall        default:
1520f85e193739c953358c865005855253af4f68a497John McCall          return false;
1521f85e193739c953358c865005855253af4f68a497John McCall      }
1522f85e193739c953358c865005855253af4f68a497John McCall    }
1523f85e193739c953358c865005855253af4f68a497John McCall    bool VisitUnaryExtension(UnaryOperator *e) {
1524f85e193739c953358c865005855253af4f68a497John McCall      return Visit(e->getSubExpr());
1525f85e193739c953358c865005855253af4f68a497John McCall    }
1526f85e193739c953358c865005855253af4f68a497John McCall    bool VisitBinComma(BinaryOperator *e) {
1527f85e193739c953358c865005855253af4f68a497John McCall      return Visit(e->getRHS());
1528f85e193739c953358c865005855253af4f68a497John McCall    }
1529f85e193739c953358c865005855253af4f68a497John McCall    bool VisitConditionalOperator(ConditionalOperator *e) {
1530f85e193739c953358c865005855253af4f68a497John McCall      // Conditional operators are okay if both sides are okay.
1531f85e193739c953358c865005855253af4f68a497John McCall      return Visit(e->getTrueExpr()) && Visit(e->getFalseExpr());
1532f85e193739c953358c865005855253af4f68a497John McCall    }
1533f85e193739c953358c865005855253af4f68a497John McCall    bool VisitObjCStringLiteral(ObjCStringLiteral *e) {
1534f85e193739c953358c865005855253af4f68a497John McCall      // Always white-list Objective-C string literals.
1535f85e193739c953358c865005855253af4f68a497John McCall      return true;
1536f85e193739c953358c865005855253af4f68a497John McCall    }
1537f85e193739c953358c865005855253af4f68a497John McCall    bool VisitStmtExpr(StmtExpr *e) {
1538f85e193739c953358c865005855253af4f68a497John McCall      return Visit(e->getSubStmt()->body_back());
1539f85e193739c953358c865005855253af4f68a497John McCall    }
1540f85e193739c953358c865005855253af4f68a497John McCall    bool VisitDeclRefExpr(DeclRefExpr *e) {
1541f85e193739c953358c865005855253af4f68a497John McCall      // White-list references to global extern strings from system
1542f85e193739c953358c865005855253af4f68a497John McCall      // headers.
1543f85e193739c953358c865005855253af4f68a497John McCall      if (VarDecl *var = dyn_cast<VarDecl>(e->getDecl()))
1544f85e193739c953358c865005855253af4f68a497John McCall        if (var->getStorageClass() == SC_Extern &&
1545f85e193739c953358c865005855253af4f68a497John McCall            var->getType().isConstQualified() &&
1546f85e193739c953358c865005855253af4f68a497John McCall            Context.getSourceManager().isInSystemHeader(var->getLocation()))
1547f85e193739c953358c865005855253af4f68a497John McCall          return true;
1548f85e193739c953358c865005855253af4f68a497John McCall      return false;
1549f85e193739c953358c865005855253af4f68a497John McCall    }
1550f85e193739c953358c865005855253af4f68a497John McCall  };
1551f85e193739c953358c865005855253af4f68a497John McCall}
1552f85e193739c953358c865005855253af4f68a497John McCall
15531522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanianbool
15541522a7c35e9872c5767721350fc8050be5b14fd2Fariborz JahanianSema::ValidObjCARCNoBridgeCastExpr(const Expr *Exp) {
15551522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian  Exp = Exp->IgnoreParenImpCasts();
15561522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian  return isa<ObjCMessageExpr>(Exp) || isa<ObjCPropertyRefExpr>(Exp);
15571522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian}
15581522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian
1559f85e193739c953358c865005855253af4f68a497John McCallvoid
1560f85e193739c953358c865005855253af4f68a497John McCallSema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
1561f85e193739c953358c865005855253af4f68a497John McCall                             Expr *castExpr, CheckedConversionKind CCK) {
1562f85e193739c953358c865005855253af4f68a497John McCall  QualType castExprType = castExpr->getType();
1563f85e193739c953358c865005855253af4f68a497John McCall
1564f85e193739c953358c865005855253af4f68a497John McCall  ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType);
1565f85e193739c953358c865005855253af4f68a497John McCall  ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType);
1566f85e193739c953358c865005855253af4f68a497John McCall  if (exprACTC == castACTC) return;
1567f85e193739c953358c865005855253af4f68a497John McCall  if (exprACTC && castType->isBooleanType()) return;
1568f85e193739c953358c865005855253af4f68a497John McCall
1569f85e193739c953358c865005855253af4f68a497John McCall  // Allow casts between pointers to lifetime types (e.g., __strong id*)
1570f85e193739c953358c865005855253af4f68a497John McCall  // and pointers to void (e.g., cv void *). Casting from void* to lifetime*
1571f85e193739c953358c865005855253af4f68a497John McCall  // must be explicit.
1572f85e193739c953358c865005855253af4f68a497John McCall  if (const PointerType *CastPtr = castType->getAs<PointerType>()) {
1573f85e193739c953358c865005855253af4f68a497John McCall    if (const PointerType *CastExprPtr = castExprType->getAs<PointerType>()) {
1574f85e193739c953358c865005855253af4f68a497John McCall      QualType CastPointee = CastPtr->getPointeeType();
1575f85e193739c953358c865005855253af4f68a497John McCall      QualType CastExprPointee = CastExprPtr->getPointeeType();
1576f85e193739c953358c865005855253af4f68a497John McCall      if ((CCK != CCK_ImplicitConversion &&
1577f85e193739c953358c865005855253af4f68a497John McCall           CastPointee->isObjCIndirectLifetimeType() &&
1578f85e193739c953358c865005855253af4f68a497John McCall           CastExprPointee->isVoidType()) ||
1579f85e193739c953358c865005855253af4f68a497John McCall          (CastPointee->isVoidType() &&
1580f85e193739c953358c865005855253af4f68a497John McCall           CastExprPointee->isObjCIndirectLifetimeType()))
1581f85e193739c953358c865005855253af4f68a497John McCall        return;
1582f85e193739c953358c865005855253af4f68a497John McCall    }
1583f85e193739c953358c865005855253af4f68a497John McCall  }
1584f85e193739c953358c865005855253af4f68a497John McCall
1585f85e193739c953358c865005855253af4f68a497John McCall  if (ARCCastChecker(Context).Visit(castExpr))
1586f85e193739c953358c865005855253af4f68a497John McCall    return;
1587f85e193739c953358c865005855253af4f68a497John McCall
1588f85e193739c953358c865005855253af4f68a497John McCall  SourceLocation loc =
1589f85e193739c953358c865005855253af4f68a497John McCall  (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc());
1590f85e193739c953358c865005855253af4f68a497John McCall
1591f85e193739c953358c865005855253af4f68a497John McCall  if (makeUnavailableInSystemHeader(loc,
1592f85e193739c953358c865005855253af4f68a497John McCall                                    "converts between Objective-C and C pointers in -fobjc-arc"))
1593f85e193739c953358c865005855253af4f68a497John McCall    return;
1594f85e193739c953358c865005855253af4f68a497John McCall
159571c482c76c67ad4240757c4e319ffdb72272b8eeJohn McCall  unsigned srcKind = 0;
1596f85e193739c953358c865005855253af4f68a497John McCall  switch (exprACTC) {
1597f85e193739c953358c865005855253af4f68a497John McCall    case ACTC_none:
1598f85e193739c953358c865005855253af4f68a497John McCall      srcKind = (castExprType->isPointerType() ? 1 : 0);
1599f85e193739c953358c865005855253af4f68a497John McCall      break;
1600f85e193739c953358c865005855253af4f68a497John McCall    case ACTC_retainable:
1601f85e193739c953358c865005855253af4f68a497John McCall      srcKind = (castExprType->isBlockPointerType() ? 2 : 3);
1602f85e193739c953358c865005855253af4f68a497John McCall      break;
1603f85e193739c953358c865005855253af4f68a497John McCall    case ACTC_indirectRetainable:
1604f85e193739c953358c865005855253af4f68a497John McCall      srcKind = 4;
1605f85e193739c953358c865005855253af4f68a497John McCall      break;
1606f85e193739c953358c865005855253af4f68a497John McCall  }
1607f85e193739c953358c865005855253af4f68a497John McCall
1608f85e193739c953358c865005855253af4f68a497John McCall  if (CCK == CCK_CStyleCast) {
1609f85e193739c953358c865005855253af4f68a497John McCall    // Check whether this could be fixed with a bridge cast.
1610f85e193739c953358c865005855253af4f68a497John McCall    SourceLocation AfterLParen = PP.getLocForEndOfToken(castRange.getBegin());
1611f85e193739c953358c865005855253af4f68a497John McCall    SourceLocation NoteLoc = AfterLParen.isValid()? AfterLParen : loc;
1612f85e193739c953358c865005855253af4f68a497John McCall
1613f85e193739c953358c865005855253af4f68a497John McCall    if (castType->isObjCARCBridgableType() &&
1614f85e193739c953358c865005855253af4f68a497John McCall        castExprType->isCARCBridgableType()) {
16151522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian      // explicit unbridged casts are allowed if the source of the cast is a
16161522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian      // message sent to an objc method (or property access)
16171522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian      if (ValidObjCARCNoBridgeCastExpr(castExpr))
16181522a7c35e9872c5767721350fc8050be5b14fd2Fariborz Jahanian        return;
1619f85e193739c953358c865005855253af4f68a497John McCall      Diag(loc, diag::err_arc_cast_requires_bridge)
1620f85e193739c953358c865005855253af4f68a497John McCall        << 2
1621f85e193739c953358c865005855253af4f68a497John McCall        << castExprType
1622f85e193739c953358c865005855253af4f68a497John McCall        << (castType->isBlockPointerType()? 1 : 0)
1623f85e193739c953358c865005855253af4f68a497John McCall        << castType
1624f85e193739c953358c865005855253af4f68a497John McCall        << castRange
1625f85e193739c953358c865005855253af4f68a497John McCall        << castExpr->getSourceRange();
1626f85e193739c953358c865005855253af4f68a497John McCall      Diag(NoteLoc, diag::note_arc_bridge)
1627f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateInsertion(AfterLParen, "__bridge ");
1628f85e193739c953358c865005855253af4f68a497John McCall      Diag(NoteLoc, diag::note_arc_bridge_transfer)
1629f85e193739c953358c865005855253af4f68a497John McCall        << castExprType
1630f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateInsertion(AfterLParen, "__bridge_transfer ");
1631f85e193739c953358c865005855253af4f68a497John McCall
1632f85e193739c953358c865005855253af4f68a497John McCall      return;
1633f85e193739c953358c865005855253af4f68a497John McCall    }
1634f85e193739c953358c865005855253af4f68a497John McCall
1635f85e193739c953358c865005855253af4f68a497John McCall    if (castType->isCARCBridgableType() &&
1636f85e193739c953358c865005855253af4f68a497John McCall        castExprType->isObjCARCBridgableType()){
1637f85e193739c953358c865005855253af4f68a497John McCall      Diag(loc, diag::err_arc_cast_requires_bridge)
1638f85e193739c953358c865005855253af4f68a497John McCall        << (castExprType->isBlockPointerType()? 1 : 0)
1639f85e193739c953358c865005855253af4f68a497John McCall        << castExprType
1640f85e193739c953358c865005855253af4f68a497John McCall        << 2
1641f85e193739c953358c865005855253af4f68a497John McCall        << castType
1642f85e193739c953358c865005855253af4f68a497John McCall        << castRange
1643f85e193739c953358c865005855253af4f68a497John McCall        << castExpr->getSourceRange();
1644f85e193739c953358c865005855253af4f68a497John McCall
1645f85e193739c953358c865005855253af4f68a497John McCall      Diag(NoteLoc, diag::note_arc_bridge)
1646f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateInsertion(AfterLParen, "__bridge ");
1647f85e193739c953358c865005855253af4f68a497John McCall      Diag(NoteLoc, diag::note_arc_bridge_retained)
1648f85e193739c953358c865005855253af4f68a497John McCall        << castType
1649f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateInsertion(AfterLParen, "__bridge_retained ");
1650f85e193739c953358c865005855253af4f68a497John McCall      return;
1651f85e193739c953358c865005855253af4f68a497John McCall    }
1652f85e193739c953358c865005855253af4f68a497John McCall  }
1653f85e193739c953358c865005855253af4f68a497John McCall
1654f85e193739c953358c865005855253af4f68a497John McCall  Diag(loc, diag::err_arc_mismatched_cast)
1655f85e193739c953358c865005855253af4f68a497John McCall    << (CCK != CCK_ImplicitConversion) << srcKind << castExprType << castType
1656f85e193739c953358c865005855253af4f68a497John McCall    << castRange << castExpr->getSourceRange();
1657f85e193739c953358c865005855253af4f68a497John McCall}
1658f85e193739c953358c865005855253af4f68a497John McCall
1659f85e193739c953358c865005855253af4f68a497John McCallExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc,
1660f85e193739c953358c865005855253af4f68a497John McCall                                      ObjCBridgeCastKind Kind,
1661f85e193739c953358c865005855253af4f68a497John McCall                                      SourceLocation BridgeKeywordLoc,
1662f85e193739c953358c865005855253af4f68a497John McCall                                      TypeSourceInfo *TSInfo,
1663f85e193739c953358c865005855253af4f68a497John McCall                                      Expr *SubExpr) {
1664f85e193739c953358c865005855253af4f68a497John McCall  QualType T = TSInfo->getType();
1665f85e193739c953358c865005855253af4f68a497John McCall  QualType FromType = SubExpr->getType();
1666f85e193739c953358c865005855253af4f68a497John McCall
1667f85e193739c953358c865005855253af4f68a497John McCall  bool MustConsume = false;
1668f85e193739c953358c865005855253af4f68a497John McCall  if (T->isDependentType() || SubExpr->isTypeDependent()) {
1669f85e193739c953358c865005855253af4f68a497John McCall    // Okay: we'll build a dependent expression type.
1670f85e193739c953358c865005855253af4f68a497John McCall  } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) {
1671f85e193739c953358c865005855253af4f68a497John McCall    // Casting CF -> id
1672f85e193739c953358c865005855253af4f68a497John McCall    switch (Kind) {
1673f85e193739c953358c865005855253af4f68a497John McCall    case OBC_Bridge:
1674f85e193739c953358c865005855253af4f68a497John McCall      break;
1675f85e193739c953358c865005855253af4f68a497John McCall
1676f85e193739c953358c865005855253af4f68a497John McCall    case OBC_BridgeRetained:
1677f85e193739c953358c865005855253af4f68a497John McCall      Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
1678f85e193739c953358c865005855253af4f68a497John McCall        << 2
1679f85e193739c953358c865005855253af4f68a497John McCall        << FromType
1680f85e193739c953358c865005855253af4f68a497John McCall        << (T->isBlockPointerType()? 1 : 0)
1681f85e193739c953358c865005855253af4f68a497John McCall        << T
1682f85e193739c953358c865005855253af4f68a497John McCall        << SubExpr->getSourceRange()
1683f85e193739c953358c865005855253af4f68a497John McCall        << Kind;
1684f85e193739c953358c865005855253af4f68a497John McCall      Diag(BridgeKeywordLoc, diag::note_arc_bridge)
1685f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge");
1686f85e193739c953358c865005855253af4f68a497John McCall      Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
1687f85e193739c953358c865005855253af4f68a497John McCall        << FromType
1688f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateReplacement(BridgeKeywordLoc,
1689f85e193739c953358c865005855253af4f68a497John McCall                                        "__bridge_transfer ");
1690f85e193739c953358c865005855253af4f68a497John McCall
1691f85e193739c953358c865005855253af4f68a497John McCall      Kind = OBC_Bridge;
1692f85e193739c953358c865005855253af4f68a497John McCall      break;
1693f85e193739c953358c865005855253af4f68a497John McCall
1694f85e193739c953358c865005855253af4f68a497John McCall    case OBC_BridgeTransfer:
1695f85e193739c953358c865005855253af4f68a497John McCall      // We must consume the Objective-C object produced by the cast.
1696f85e193739c953358c865005855253af4f68a497John McCall      MustConsume = true;
1697f85e193739c953358c865005855253af4f68a497John McCall      break;
1698f85e193739c953358c865005855253af4f68a497John McCall    }
1699f85e193739c953358c865005855253af4f68a497John McCall  } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) {
1700f85e193739c953358c865005855253af4f68a497John McCall    // Okay: id -> CF
1701f85e193739c953358c865005855253af4f68a497John McCall    switch (Kind) {
1702f85e193739c953358c865005855253af4f68a497John McCall    case OBC_Bridge:
1703f85e193739c953358c865005855253af4f68a497John McCall      break;
1704f85e193739c953358c865005855253af4f68a497John McCall
1705f85e193739c953358c865005855253af4f68a497John McCall    case OBC_BridgeRetained:
1706f85e193739c953358c865005855253af4f68a497John McCall      // Produce the object before casting it.
1707f85e193739c953358c865005855253af4f68a497John McCall      SubExpr = ImplicitCastExpr::Create(Context, FromType,
1708f85e193739c953358c865005855253af4f68a497John McCall                                         CK_ObjCProduceObject,
1709f85e193739c953358c865005855253af4f68a497John McCall                                         SubExpr, 0, VK_RValue);
1710f85e193739c953358c865005855253af4f68a497John McCall      break;
1711f85e193739c953358c865005855253af4f68a497John McCall
1712f85e193739c953358c865005855253af4f68a497John McCall    case OBC_BridgeTransfer:
1713f85e193739c953358c865005855253af4f68a497John McCall      Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
1714f85e193739c953358c865005855253af4f68a497John McCall        << (FromType->isBlockPointerType()? 1 : 0)
1715f85e193739c953358c865005855253af4f68a497John McCall        << FromType
1716f85e193739c953358c865005855253af4f68a497John McCall        << 2
1717f85e193739c953358c865005855253af4f68a497John McCall        << T
1718f85e193739c953358c865005855253af4f68a497John McCall        << SubExpr->getSourceRange()
1719f85e193739c953358c865005855253af4f68a497John McCall        << Kind;
1720f85e193739c953358c865005855253af4f68a497John McCall
1721f85e193739c953358c865005855253af4f68a497John McCall      Diag(BridgeKeywordLoc, diag::note_arc_bridge)
1722f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge ");
1723f85e193739c953358c865005855253af4f68a497John McCall      Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
1724f85e193739c953358c865005855253af4f68a497John McCall        << T
1725f85e193739c953358c865005855253af4f68a497John McCall        << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge_retained ");
1726f85e193739c953358c865005855253af4f68a497John McCall
1727f85e193739c953358c865005855253af4f68a497John McCall      Kind = OBC_Bridge;
1728f85e193739c953358c865005855253af4f68a497John McCall      break;
1729f85e193739c953358c865005855253af4f68a497John McCall    }
1730f85e193739c953358c865005855253af4f68a497John McCall  } else {
1731f85e193739c953358c865005855253af4f68a497John McCall    Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
1732f85e193739c953358c865005855253af4f68a497John McCall      << FromType << T << Kind
1733f85e193739c953358c865005855253af4f68a497John McCall      << SubExpr->getSourceRange()
1734f85e193739c953358c865005855253af4f68a497John McCall      << TSInfo->getTypeLoc().getSourceRange();
1735f85e193739c953358c865005855253af4f68a497John McCall    return ExprError();
1736f85e193739c953358c865005855253af4f68a497John McCall  }
1737f85e193739c953358c865005855253af4f68a497John McCall
1738f85e193739c953358c865005855253af4f68a497John McCall  Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind,
1739f85e193739c953358c865005855253af4f68a497John McCall                                                   BridgeKeywordLoc,
1740f85e193739c953358c865005855253af4f68a497John McCall                                                   TSInfo, SubExpr);
1741f85e193739c953358c865005855253af4f68a497John McCall
1742f85e193739c953358c865005855253af4f68a497John McCall  if (MustConsume) {
1743f85e193739c953358c865005855253af4f68a497John McCall    ExprNeedsCleanups = true;
1744f85e193739c953358c865005855253af4f68a497John McCall    Result = ImplicitCastExpr::Create(Context, T, CK_ObjCConsumeObject, Result,
1745f85e193739c953358c865005855253af4f68a497John McCall                                      0, VK_RValue);
1746f85e193739c953358c865005855253af4f68a497John McCall  }
1747f85e193739c953358c865005855253af4f68a497John McCall
1748f85e193739c953358c865005855253af4f68a497John McCall  return Result;
1749f85e193739c953358c865005855253af4f68a497John McCall}
1750f85e193739c953358c865005855253af4f68a497John McCall
1751f85e193739c953358c865005855253af4f68a497John McCallExprResult Sema::ActOnObjCBridgedCast(Scope *S,
1752f85e193739c953358c865005855253af4f68a497John McCall                                      SourceLocation LParenLoc,
1753f85e193739c953358c865005855253af4f68a497John McCall                                      ObjCBridgeCastKind Kind,
1754f85e193739c953358c865005855253af4f68a497John McCall                                      SourceLocation BridgeKeywordLoc,
1755f85e193739c953358c865005855253af4f68a497John McCall                                      ParsedType Type,
1756f85e193739c953358c865005855253af4f68a497John McCall                                      SourceLocation RParenLoc,
1757f85e193739c953358c865005855253af4f68a497John McCall                                      Expr *SubExpr) {
1758f85e193739c953358c865005855253af4f68a497John McCall  TypeSourceInfo *TSInfo = 0;
1759f85e193739c953358c865005855253af4f68a497John McCall  QualType T = GetTypeFromParser(Type, &TSInfo);
1760f85e193739c953358c865005855253af4f68a497John McCall  if (!TSInfo)
1761f85e193739c953358c865005855253af4f68a497John McCall    TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc);
1762f85e193739c953358c865005855253af4f68a497John McCall  return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo,
1763f85e193739c953358c865005855253af4f68a497John McCall                              SubExpr);
1764f85e193739c953358c865005855253af4f68a497John McCall}
1765