blob: ce06abec4d45a2f244771936ec0495af1542b8cf [file] [log] [blame]
Chris Lattner85a932e2008-01-04 22:32:30 +00001//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective-C expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattner7f816522010-04-11 07:45:24 +000015#include "Lookup.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Steve Narofff494b572008-05-29 21:12:08 +000018#include "clang/AST/ExprObjC.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000019#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000020#include "clang/Lex/Preprocessor.h"
21
Chris Lattner85a932e2008-01-04 22:32:30 +000022using namespace clang;
23
Mike Stump1eb44332009-09-09 15:08:12 +000024Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner39c28bb2009-02-18 06:48:40 +000025 ExprTy **strings,
Chris Lattner85a932e2008-01-04 22:32:30 +000026 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000027 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
28
Chris Lattnerf4b136f2009-02-18 06:13:04 +000029 // Most ObjC strings are formed out of a single piece. However, we *can*
30 // have strings formed out of multiple @ strings with multiple pptokens in
31 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
32 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000033 StringLiteral *S = Strings[0];
Mike Stump1eb44332009-09-09 15:08:12 +000034
Chris Lattnerf4b136f2009-02-18 06:13:04 +000035 // If we have a multi-part string, merge it all together.
36 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000037 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000038 llvm::SmallString<128> StrBuf;
39 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump1eb44332009-09-09 15:08:12 +000040
Chris Lattner726e1682009-02-18 05:49:11 +000041 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000042 S = Strings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner39c28bb2009-02-18 06:48:40 +000044 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000045 if (S->isWide()) {
46 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
47 << S->getSourceRange();
48 return true;
49 }
Mike Stump1eb44332009-09-09 15:08:12 +000050
Chris Lattner39c28bb2009-02-18 06:48:40 +000051 // Get the string data.
52 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
Mike Stump1eb44332009-09-09 15:08:12 +000053
Chris Lattner39c28bb2009-02-18 06:48:40 +000054 // Get the locations of the string tokens.
55 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner39c28bb2009-02-18 06:48:40 +000057 // Free the temporary string.
Ted Kremenek8189cde2009-02-07 01:47:29 +000058 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000059 }
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner39c28bb2009-02-18 06:48:40 +000061 // Create the aggregate string with the appropriate content and location
62 // information.
63 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000064 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000065 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000066 }
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner69039812009-02-18 06:01:06 +000068 // Verify that this composite string is acceptable for ObjC strings.
69 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000070 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000071
72 // Initialize the constant string interface lazily. This assumes
Steve Naroffd9fd7642009-04-07 14:18:33 +000073 // the NSString interface is seen in this translation unit. Note: We
74 // don't use NSConstantString, since the runtime team considers this
75 // interface private (even though it appears in the header files).
Chris Lattnera0af1fe2009-02-18 06:06:56 +000076 QualType Ty = Context.getObjCConstantStringInterface();
77 if (!Ty.isNull()) {
Steve Naroff14108da2009-07-10 23:34:53 +000078 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000079 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000080 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorc83c6872010-04-15 22:33:43 +000081 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
82 LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000083 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
84 Context.setObjCConstantStringInterface(StrIF);
85 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +000086 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000087 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000088 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +000089 // strings as untyped objects and let the runtime figure it out later.
90 Ty = Context.getObjCIdType();
91 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattnerf4b136f2009-02-18 06:13:04 +000094 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000095}
96
Mike Stump1eb44332009-09-09 15:08:12 +000097Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +000098 TypeSourceInfo *EncodedTypeInfo,
Anders Carlssonfc0f0212009-06-07 18:45:35 +000099 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +0000100 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000101 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000102 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000103 StrTy = Context.DependentTy;
104 else {
105 std::string Str;
106 Context.getObjCEncodingForType(EncodedType, Str);
107
108 // The type of @encode is the same as the type of the corresponding string,
109 // which is an array type.
110 StrTy = Context.CharTy;
111 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000112 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000113 StrTy.addConst();
114 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
115 ArrayType::Normal, 0);
116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor81d34662010-04-20 15:39:42 +0000118 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000119}
120
Chris Lattner85a932e2008-01-04 22:32:30 +0000121Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
122 SourceLocation EncodeLoc,
123 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000124 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +0000125 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000126 // FIXME: Preserve type source info ?
Douglas Gregor81d34662010-04-20 15:39:42 +0000127 TypeSourceInfo *TInfo;
128 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
129 if (!TInfo)
130 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
131 PP.getLocForEndOfToken(LParenLoc));
Chris Lattner85a932e2008-01-04 22:32:30 +0000132
Douglas Gregor81d34662010-04-20 15:39:42 +0000133 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000134}
135
136Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
137 SourceLocation AtLoc,
138 SourceLocation SelLoc,
139 SourceLocation LParenLoc,
140 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000141 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +0000142 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000143 if (!Method)
144 Method = LookupFactoryMethodInGlobalPool(Sel,
145 SourceRange(LParenLoc, RParenLoc));
146 if (!Method)
147 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
148
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000149 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000150 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000151}
152
153Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
154 SourceLocation AtLoc,
155 SourceLocation ProtoLoc,
156 SourceLocation LParenLoc,
157 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000158 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000159 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000160 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000161 return true;
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000164 QualType Ty = Context.getObjCProtoType();
165 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000166 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000167 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000168 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000169}
170
Mike Stump1eb44332009-09-09 15:08:12 +0000171bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
172 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000173 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000174 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump1eb44332009-09-09 15:08:12 +0000175 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000176 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000177 // Apply default argument promotion as for (C99 6.5.2.2p6).
178 for (unsigned i = 0; i != NumArgs; i++)
179 DefaultArgumentPromotion(Args[i]);
180
Chris Lattner077bf5e2008-11-24 03:33:13 +0000181 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
182 diag::warn_inst_method_not_found;
183 Diag(lbrac, DiagID)
184 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000185 ReturnType = Context.getObjCIdType();
186 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner077bf5e2008-11-24 03:33:13 +0000189 ReturnType = Method->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000191 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000192 // Method might have more arguments than selector indicates. This is due
193 // to addition of c-style arguments in method.
194 if (Method->param_size() > Sel.getNumArgs())
195 NumNamedArgs = Method->param_size();
196 // FIXME. This need be cleaned up.
197 if (NumArgs < NumNamedArgs) {
Eric Christopherd77b9a22010-04-16 04:48:22 +0000198 Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2
199 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000200 return false;
201 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000202
Chris Lattner312531a2009-04-12 08:11:20 +0000203 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000204 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000205 Expr *argExpr = Args[i];
206 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Chris Lattner89951a82009-02-20 18:43:26 +0000208 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000209 QualType rhsType = argExpr->getType();
210
Mike Stump1eb44332009-09-09 15:08:12 +0000211 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000212 if (lhsType->isArrayType())
213 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000214 else if (lhsType->isFunctionType())
215 lhsType = Context.getPointerType(lhsType);
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217 AssignConvertType Result =
Chris Lattner987798a2008-04-02 17:17:33 +0000218 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000219 if (Args[i] != argExpr) // The expression was converted.
220 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000221
222 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000223 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor68647482009-12-16 03:45:30 +0000224 argExpr, AA_Sending);
Chris Lattner85a932e2008-01-04 22:32:30 +0000225 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000226
227 // Promote additional arguments to variadic methods.
228 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000229 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000230 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000231 } else {
232 // Check for extra arguments to non-variadic methods.
233 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000234 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000235 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000236 << 2 /*method*/ << NumNamedArgs << NumArgs
237 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000238 << SourceRange(Args[NumNamedArgs]->getLocStart(),
239 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000240 }
241 }
242
Chris Lattner312531a2009-04-12 08:11:20 +0000243 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000244}
245
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000246bool Sema::isSelfExpr(Expr *RExpr) {
247 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
248 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
249 return true;
250 return false;
251}
252
Steve Narofff1afaf62009-02-26 15:55:06 +0000253// Helper method for ActOnClassMethod/ActOnInstanceMethod.
254// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000255// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000256// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000257ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000258 ObjCInterfaceDecl *ClassDecl) {
259 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000260 // lookup in class and all superclasses
261 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000262 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000263 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Steve Naroff5609ec02009-03-08 18:56:13 +0000265 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000266 if (!Method)
267 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Steve Naroff5609ec02009-03-08 18:56:13 +0000269 // Before we give up, check if the selector is an instance method.
270 // But only in the root. This matches gcc's behaviour and what the
271 // runtime expects.
272 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000273 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000274 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000275 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000276 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000277 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Steve Naroff5609ec02009-03-08 18:56:13 +0000280 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000281 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000282 return Method;
283}
284
285ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
286 ObjCInterfaceDecl *ClassDecl) {
287 ObjCMethodDecl *Method = 0;
288 while (ClassDecl && !Method) {
289 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000290 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000291 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Steve Naroff5609ec02009-03-08 18:56:13 +0000293 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000294 if (!Method)
295 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000296 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000297 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000298 return Method;
299}
300
Chris Lattner7f816522010-04-11 07:45:24 +0000301/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
302/// objective C interface. This is a property reference expression.
303Action::OwningExprResult Sema::
304HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000305 Expr *BaseExpr, DeclarationName MemberName,
306 SourceLocation MemberLoc) {
Chris Lattner7f816522010-04-11 07:45:24 +0000307 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
308 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
309 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
310
311 // Search for a declared property first.
312 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
313 // Check whether we can reference this property.
314 if (DiagnoseUseOfDecl(PD, MemberLoc))
315 return ExprError();
316 QualType ResTy = PD->getType();
317 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
318 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
319 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
320 ResTy = Getter->getResultType();
321 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
322 MemberLoc, BaseExpr));
323 }
324 // Check protocols on qualified interfaces.
325 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
326 E = OPT->qual_end(); I != E; ++I)
327 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
328 // Check whether we can reference this property.
329 if (DiagnoseUseOfDecl(PD, MemberLoc))
330 return ExprError();
331
332 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
333 MemberLoc, BaseExpr));
334 }
335 // If that failed, look for an "implicit" property by seeing if the nullary
336 // selector is implemented.
337
338 // FIXME: The logic for looking up nullary and unary selectors should be
339 // shared with the code in ActOnInstanceMessage.
340
341 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
342 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
343
344 // If this reference is in an @implementation, check for 'private' methods.
345 if (!Getter)
346 Getter = IFace->lookupPrivateInstanceMethod(Sel);
347
348 // Look through local category implementations associated with the class.
349 if (!Getter)
350 Getter = IFace->getCategoryInstanceMethod(Sel);
351 if (Getter) {
352 // Check if we can reference this property.
353 if (DiagnoseUseOfDecl(Getter, MemberLoc))
354 return ExprError();
355 }
356 // If we found a getter then this may be a valid dot-reference, we
357 // will look for the matching setter, in case it is needed.
358 Selector SetterSel =
359 SelectorTable::constructSetterName(PP.getIdentifierTable(),
360 PP.getSelectorTable(), Member);
361 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
362 if (!Setter) {
363 // If this reference is in an @implementation, also check for 'private'
364 // methods.
365 Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
366 }
367 // Look through local category implementations associated with the class.
368 if (!Setter)
369 Setter = IFace->getCategoryInstanceMethod(SetterSel);
370
371 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
372 return ExprError();
373
374 if (Getter) {
375 QualType PType;
376 PType = Getter->getResultType();
377 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType,
378 Setter, MemberLoc, BaseExpr));
379 }
380
381 // Attempt to correct for typos in property names.
382 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000383 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000384 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000385 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000386 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000387 << MemberName << QualType(OPT, 0) << TypoResult
388 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000389 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
390 Diag(Property->getLocation(), diag::note_previous_decl)
391 << Property->getDeclName();
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000392 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc);
Chris Lattner7f816522010-04-11 07:45:24 +0000393 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000394
Chris Lattner7f816522010-04-11 07:45:24 +0000395 Diag(MemberLoc, diag::err_property_not_found)
396 << MemberName << QualType(OPT, 0);
397 if (Setter && !Getter)
398 Diag(Setter->getLocation(), diag::note_getter_unavailable)
399 << MemberName << BaseExpr->getSourceRange();
400 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000401}
402
403
404
Chris Lattnereb483eb2010-04-11 08:28:14 +0000405Action::OwningExprResult Sema::
406ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
407 IdentifierInfo &propertyName,
408 SourceLocation receiverNameLoc,
409 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000411 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000412 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
413 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000414 if (IFace == 0) {
415 // If the "receiver" is 'super' in a method, handle it as an expression-like
416 // property reference.
417 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
418 if (receiverNamePtr->isStr("super")) {
419 if (CurMethod->isInstanceMethod()) {
420 QualType T =
421 Context.getObjCInterfaceType(CurMethod->getClassInterface());
422 T = Context.getObjCObjectPointerType(T);
423 Expr *SuperExpr = new (Context) ObjCSuperExpr(receiverNameLoc, T);
424
425 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
426 SuperExpr, &propertyName,
427 propertyNameLoc);
428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnereb483eb2010-04-11 08:28:14 +0000430 // Otherwise, if this is a class method, try dispatching to our
431 // superclass.
432 IFace = CurMethod->getClassInterface()->getSuperClass();
433 }
434
435 if (IFace == 0) {
436 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
437 return ExprError();
438 }
439 }
440
441 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000442 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000443 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000444
445 // If this reference is in an @implementation, check for 'private' methods.
446 if (!Getter)
447 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
448 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000449 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000450 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000451
452 if (Getter) {
453 // FIXME: refactor/share with ActOnMemberReference().
454 // Check if we can reference this property.
455 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
456 return ExprError();
457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Steve Naroff61f72cb2009-03-09 21:12:44 +0000459 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000460 Selector SetterSel =
461 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000462 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000464 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000465 if (!Setter) {
466 // If this reference is in an @implementation, also check for 'private'
467 // methods.
468 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
469 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000470 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000471 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000472 }
473 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000474 if (!Setter)
475 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000476
477 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
478 return ExprError();
479
480 if (Getter || Setter) {
481 QualType PType;
482
483 if (Getter)
484 PType = Getter->getResultType();
485 else {
486 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
487 E = Setter->param_end(); PI != E; ++PI)
488 PType = (*PI)->getType();
489 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000490 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump1eb44332009-09-09 15:08:12 +0000491 Getter, PType, Setter,
Steve Naroff61f72cb2009-03-09 21:12:44 +0000492 propertyNameLoc, IFace, receiverNameLoc));
493 }
494 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
495 << &propertyName << Context.getObjCInterfaceType(IFace));
496}
497
Douglas Gregor47bd5432010-04-14 02:46:37 +0000498Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
499 IdentifierInfo *&Name,
500 SourceLocation NameLoc,
501 bool IsSuper,
502 bool HasTrailingDot) {
503 // If the identifier is "super" and there is no trailing dot, we're
504 // messaging super.
505 if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
506 return ObjCSuperMessage;
507
508 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
509 LookupName(Result, S);
510
511 switch (Result.getResultKind()) {
512 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000513 // Normal name lookup didn't find anything. If we're in an
514 // Objective-C method, look for ivars. If we find one, we're done!
515 // FIXME: This is a hack. Ivar lookup should be part of normal lookup.
516 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
517 ObjCInterfaceDecl *ClassDeclared;
518 if (Method->getClassInterface()->lookupInstanceVariable(Name,
519 ClassDeclared))
520 return ObjCInstanceMessage;
521 }
522
Douglas Gregor47bd5432010-04-14 02:46:37 +0000523 // Break out; we'll perform typo correction below.
524 break;
525
526 case LookupResult::NotFoundInCurrentInstantiation:
527 case LookupResult::FoundOverloaded:
528 case LookupResult::FoundUnresolvedValue:
529 case LookupResult::Ambiguous:
530 Result.suppressDiagnostics();
531 return ObjCInstanceMessage;
532
533 case LookupResult::Found: {
534 // We found something. If it's a type, then we have a class
535 // message. Otherwise, it's an instance message.
536 NamedDecl *ND = Result.getFoundDecl();
537 if (isa<ObjCInterfaceDecl>(ND) || isa<TypeDecl>(ND) ||
538 isa<UnresolvedUsingTypenameDecl>(ND))
539 return ObjCClassMessage;
540
541 return ObjCInstanceMessage;
542 }
543 }
544
Douglas Gregoraaf87162010-04-14 20:04:41 +0000545 // Determine our typo-correction context.
546 CorrectTypoContext CTC = CTC_Expression;
547 if (ObjCMethodDecl *Method = getCurMethodDecl())
548 if (Method->getClassInterface() &&
549 Method->getClassInterface()->getSuperClass())
550 CTC = CTC_ObjCMessageReceiver;
551
552 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
553 if (Result.isSingleResult()) {
554 // If we found a declaration, correct when it refers to an Objective-C
555 // class.
556 NamedDecl *ND = Result.getFoundDecl();
557 if (isa<ObjCInterfaceDecl>(ND)) {
558 Diag(NameLoc, diag::err_unknown_receiver_suggest)
559 << Name << Result.getLookupName()
560 << FixItHint::CreateReplacement(SourceRange(NameLoc),
561 ND->getNameAsString());
562 Diag(ND->getLocation(), diag::note_previous_decl)
563 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000564
Douglas Gregoraaf87162010-04-14 20:04:41 +0000565 Name = ND->getIdentifier();
566 return ObjCClassMessage;
567 }
568 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
569 Corrected.getAsIdentifierInfo()->isStr("super")) {
570 // If we've found the keyword "super", this is a send to super.
571 Diag(NameLoc, diag::err_unknown_receiver_suggest)
572 << Name << Corrected
573 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
574 Name = Corrected.getAsIdentifierInfo();
575 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000576 }
577 }
578
579 // Fall back: let the parser try to parse it as an instance message.
580 return ObjCInstanceMessage;
581}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000582
Chris Lattner85a932e2008-01-04 22:32:30 +0000583// ActOnClassMessage - used for both unary and keyword messages.
584// ArgExprs is optional - if it is present, the number of expressions
585// is obtained from Sel.getNumArgs().
Chris Lattnereb483eb2010-04-11 08:28:14 +0000586Sema::ExprResult Sema::
587ActOnClassMessage(Scope *S, IdentifierInfo *receiverName, Selector Sel,
588 SourceLocation lbrac, SourceLocation receiverLoc,
589 SourceLocation selectorLoc, SourceLocation rbrac,
590 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000591 assert(receiverName && "missing receiver class name");
592
593 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Chris Lattner15faee12010-04-12 05:38:43 +0000594 ObjCInterfaceDecl *ClassDecl = 0;
Chris Lattnera7a98c92010-04-12 17:25:51 +0000595 bool isSuper = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Chris Lattner15faee12010-04-12 05:38:43 +0000597 // Special case a message to super, which can be either a class message or an
598 // instance message, depending on what CurMethodDecl is.
Chris Lattner84692652008-11-20 05:35:30 +0000599 if (receiverName->isStr("super")) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000600 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000601 ObjCInterfaceDecl *OID = CurMethod->getClassInterface();
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000602 if (!OID)
Mike Stump1eb44332009-09-09 15:08:12 +0000603 return Diag(lbrac, diag::error_no_super_class_message)
Chris Lattnereb483eb2010-04-11 08:28:14 +0000604 << CurMethod->getDeclName();
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000605 ClassDecl = OID->getSuperClass();
Chris Lattner15faee12010-04-12 05:38:43 +0000606 if (ClassDecl == 0)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000607 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Chris Lattnereb483eb2010-04-11 08:28:14 +0000608 if (CurMethod->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000609 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000610 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000611 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
612 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000613 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000614 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000615 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000616 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000617
Chris Lattner15faee12010-04-12 05:38:43 +0000618 // Otherwise, if this is a class method, try dispatching to our
619 // superclass, which is in ClassDecl.
Chris Lattnera7a98c92010-04-12 17:25:51 +0000620 isSuper = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000621 }
Chris Lattner15faee12010-04-12 05:38:43 +0000622 }
623
624 if (ClassDecl == 0)
Douglas Gregorc83c6872010-04-15 22:33:43 +0000625 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Steve Naroff7c778f12008-07-25 19:39:00 +0000627 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000628 //
629 // typedef XCElementDisplayRect XCElementGraphicsRect;
630 //
631 // @implementation XCRASlice
632 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
633 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
634 // }
635 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000636 // If necessary, the following lookup could move to getObjCInterfaceDecl().
637 if (!ClassDecl) {
John McCallf36e02d2009-10-09 21:13:30 +0000638 NamedDecl *IDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +0000639 = LookupSingleName(TUScope, receiverName, receiverLoc,
640 LookupOrdinaryName);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000641 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl))
642 if (const ObjCInterfaceType *OCIT
643 = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>())
644 ClassDecl = OCIT->getDecl();
645
646 if (!ClassDecl) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000647 // Give a better error message for invalid use of super.
648 if (receiverName->isStr("super"))
649 Diag(receiverLoc, diag::err_invalid_receiver_to_message_super);
650 else
651 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000652 return true;
Steve Naroff7c778f12008-07-25 19:39:00 +0000653 }
654 }
655 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000656 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000657 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000658 if (ClassDecl->isForwardDecl()) {
659 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000660 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000661 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
662 if (Method)
Mike Stump1eb44332009-09-09 15:08:12 +0000663 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000664 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000665 }
666 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000667 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Steve Naroff7c778f12008-07-25 19:39:00 +0000669 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000670 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000671 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000672
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000673 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
674 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
676 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000677 lbrac, rbrac, returnType))
678 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000679
Anders Carlsson187ba152009-05-26 15:22:25 +0000680 returnType = returnType.getNonReferenceType();
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattnera7a98c92010-04-12 17:25:51 +0000682 // FIXME: need to do a better job handling 'super' usage within a class. For
683 // now, we simply pass the "super" identifier through (which isn't consistent
684 // with instance methods.
685 if (isSuper)
686 return new (Context) ObjCMessageExpr(Context, receiverName, receiverLoc,
687 Sel, returnType, Method, lbrac, rbrac,
688 ArgExprs, NumArgs);
689
Mike Stump390b4cc2009-05-16 07:39:55 +0000690 // If we have the ObjCInterfaceDecl* for the class that is receiving the
691 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
692 // IdentifierInfo* for the class.
Chris Lattner15faee12010-04-12 05:38:43 +0000693 return new (Context) ObjCMessageExpr(Context, ClassDecl, receiverLoc,
694 Sel, returnType, Method, lbrac, rbrac,
695 ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000696}
697
698// ActOnInstanceMessage - used for both unary and keyword messages.
699// ArgExprs is optional - if it is present, the number of expressions
700// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000701Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump1eb44332009-09-09 15:08:12 +0000702 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000703 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000704 SourceLocation rbrac,
705 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000706 assert(receiver && "missing receiver expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattner85a932e2008-01-04 22:32:30 +0000708 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
709 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattnerd0d45992009-04-29 05:48:32 +0000711 // If necessary, apply function/array conversion to the receiver.
712 // C99 6.7.5.3p[7,8].
Douglas Gregora873dfc2010-02-03 00:27:59 +0000713 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner85a932e2008-01-04 22:32:30 +0000715 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000716 QualType ReceiverCType =
717 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000718
Mike Stump1eb44332009-09-09 15:08:12 +0000719 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000720 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000721 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000722 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
723 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000724 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000725 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump1eb44332009-09-09 15:08:12 +0000726 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000727 lbrac, rbrac, returnType))
728 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000729 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000730 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
731 Method, lbrac, rbrac,
732 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000735 // Handle messages to Class.
Mike Stump1eb44332009-09-09 15:08:12 +0000736 if (ReceiverCType->isObjCClassType() ||
Steve Naroff470301b2009-07-22 16:07:01 +0000737 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000738 ObjCMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattner6562fda2008-07-21 06:44:27 +0000740 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000741 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
742 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000743 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Steve Narofff1afaf62009-02-26 15:55:06 +0000745 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000746 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
748 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff470301b2009-07-22 16:07:01 +0000749 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000750 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000751 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
752 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000753 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000754 if (!Method) {
755 // If not messaging 'self', look for any factory method named 'Sel'.
756 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000757 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000758 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000759 // If no class (factory) method was found, check if an _instance_
760 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000761 Method = LookupInstanceMethodInGlobalPool(
762 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000763 if (Method)
764 if (const ObjCInterfaceDecl *ID =
765 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
766 if (ID->getSuperClass())
767 Diag(lbrac, diag::warn_root_inst_method_not_found)
768 << Sel << SourceRange(lbrac, rbrac);
769 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000770 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000771 }
772 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000773 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000774 lbrac, rbrac, returnType))
775 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000776 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000777 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
778 Method, lbrac, rbrac,
779 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000782 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000783 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000784
785 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000786 // long as one of the protocols implements the selector (if not, warn).
Mike Stump1eb44332009-09-09 15:08:12 +0000787 if (const ObjCObjectPointerType *QIdTy =
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000788 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000789 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000790 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000791 E = QIdTy->qual_end(); I != E; ++I) {
792 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000793 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000794 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000795 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000796 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000797 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff14108da2009-07-10 23:34:53 +0000800 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000801 // We allow sending a message to a pointer to an interface (an object).
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Steve Naroff14108da2009-07-10 23:34:53 +0000803 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000804 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000805 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000806 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000807 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000809 if (!Method) {
810 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000811 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000812 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000813 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000814 break;
815 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000816 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000817 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000818 // If we have implementations in scope, check "private" methods.
819 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Steve Naroff5609ec02009-03-08 18:56:13 +0000821 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000822 // If we still haven't found a method, look in the global pool. This
823 // behavior isn't very desirable, however we need it for GCC
824 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000825 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000826 Method = LookupInstanceMethodInGlobalPool(
827 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000828 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump1eb44332009-09-09 15:08:12 +0000829 Diag(lbrac, diag::warn_maynot_respond)
Chris Lattner0784fcd2010-04-11 07:04:01 +0000830 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000831 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000832 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000833 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000834 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
835 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000836 } else if (!Context.getObjCIdType().isNull() &&
837 (ReceiverCType->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000838 (ReceiverCType->isIntegerType() &&
Chris Lattner0c73f372009-03-09 21:19:16 +0000839 ReceiverCType->isScalarType()))) {
840 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000841 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000842 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman73c39ab2009-10-20 08:27:19 +0000843 if (ReceiverCType->isPointerType())
844 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
845 else
846 ImpCastExprToType(RExpr, Context.getObjCIdType(),
847 CastExpr::CK_IntegralToPointer);
Chris Lattner0c73f372009-03-09 21:19:16 +0000848 } else {
849 // Reject other random receiver types (e.g. structs).
850 Diag(lbrac, diag::err_bad_receiver_type)
851 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000852 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000855 if (Method)
856 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000857 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000858 lbrac, rbrac, returnType))
859 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000860 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000861 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType, Method,
862 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000863}
Chris Lattnereca7be62008-04-07 05:30:13 +0000864