blob: 26d115667c5cad4c6eed1a1dc7c504f905dc5acc [file] [log] [blame]
Chris Lattnera3fc41d2008-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 Lattner2b1ca5f2010-04-11 07:45:24 +000015#include "Lookup.h"
Chris Lattnera3fc41d2008-01-04 22:32:30 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Steve Naroff021ca182008-05-29 21:12:08 +000018#include "clang/AST/ExprObjC.h"
Chris Lattner163ffd22009-02-18 06:48:40 +000019#include "llvm/ADT/SmallString.h"
Steve Naroff9527bbf2009-03-09 21:12:44 +000020#include "clang/Lex/Preprocessor.h"
21
Chris Lattnera3fc41d2008-01-04 22:32:30 +000022using namespace clang;
23
Mike Stump11289f42009-09-09 15:08:12 +000024Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner163ffd22009-02-18 06:48:40 +000025 ExprTy **strings,
Chris Lattnera3fc41d2008-01-04 22:32:30 +000026 unsigned NumStrings) {
Chris Lattner163ffd22009-02-18 06:48:40 +000027 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
28
Chris Lattnerd7670d92009-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 Lattner163ffd22009-02-18 06:48:40 +000033 StringLiteral *S = Strings[0];
Mike Stump11289f42009-09-09 15:08:12 +000034
Chris Lattnerd7670d92009-02-18 06:13:04 +000035 // If we have a multi-part string, merge it all together.
36 if (NumStrings != 1) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +000037 // Concatenate objc strings.
Chris Lattner163ffd22009-02-18 06:48:40 +000038 llvm::SmallString<128> StrBuf;
39 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump11289f42009-09-09 15:08:12 +000040
Chris Lattner630970d2009-02-18 05:49:11 +000041 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner163ffd22009-02-18 06:48:40 +000042 S = Strings[i];
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattner163ffd22009-02-18 06:48:40 +000044 // ObjC strings can't be wide.
Chris Lattnerd7670d92009-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 Stump11289f42009-09-09 15:08:12 +000050
Chris Lattner163ffd22009-02-18 06:48:40 +000051 // Get the string data.
52 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
Mike Stump11289f42009-09-09 15:08:12 +000053
Chris Lattner163ffd22009-02-18 06:48:40 +000054 // Get the locations of the string tokens.
55 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Mike Stump11289f42009-09-09 15:08:12 +000056
Chris Lattner163ffd22009-02-18 06:48:40 +000057 // Free the temporary string.
Ted Kremenek5a201952009-02-07 01:47:29 +000058 S->Destroy(Context);
Chris Lattnera3fc41d2008-01-04 22:32:30 +000059 }
Mike Stump11289f42009-09-09 15:08:12 +000060
Chris Lattner163ffd22009-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 Lattnerf83b5af2009-02-18 06:40:38 +000064 Context.getPointerType(Context.CharTy),
Chris Lattner163ffd22009-02-18 06:48:40 +000065 &StrLocs[0], StrLocs.size());
Chris Lattnera3fc41d2008-01-04 22:32:30 +000066 }
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattner6436fb62009-02-18 06:01:06 +000068 // Verify that this composite string is acceptable for ObjC strings.
69 if (CheckObjCString(S))
Chris Lattnera3fc41d2008-01-04 22:32:30 +000070 return true;
Chris Lattnerfffd6a72009-02-18 06:06:56 +000071
72 // Initialize the constant string interface lazily. This assumes
Steve Naroff54e59452009-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 Lattnerfffd6a72009-02-18 06:06:56 +000076 QualType Ty = Context.getObjCConstantStringInterface();
77 if (!Ty.isNull()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +000078 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattner091f6982008-06-21 21:44:18 +000079 } else {
Steve Naroff54e59452009-04-07 14:18:33 +000080 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
John McCall9f3059a2009-10-09 21:13:30 +000081 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, LookupOrdinaryName);
Chris Lattnerfffd6a72009-02-18 06:06:56 +000082 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
83 Context.setObjCConstantStringInterface(StrIF);
84 Ty = Context.getObjCConstantStringInterface();
Steve Naroff7cae42b2009-07-10 23:34:53 +000085 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +000086 } else {
Steve Naroff54e59452009-04-07 14:18:33 +000087 // If there is no NSString interface defined then treat constant
Chris Lattnerfffd6a72009-02-18 06:06:56 +000088 // strings as untyped objects and let the runtime figure it out later.
89 Ty = Context.getObjCIdType();
90 }
Chris Lattner091f6982008-06-21 21:44:18 +000091 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattnerd7670d92009-02-18 06:13:04 +000093 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattnera3fc41d2008-01-04 22:32:30 +000094}
95
Mike Stump11289f42009-09-09 15:08:12 +000096Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Anders Carlsson315d2292009-06-07 18:45:35 +000097 QualType EncodedType,
98 SourceLocation RParenLoc) {
99 QualType StrTy;
Mike Stump11289f42009-09-09 15:08:12 +0000100 if (EncodedType->isDependentType())
Anders Carlsson315d2292009-06-07 18:45:35 +0000101 StrTy = Context.DependentTy;
102 else {
103 std::string Str;
104 Context.getObjCEncodingForType(EncodedType, Str);
105
106 // The type of @encode is the same as the type of the corresponding string,
107 // which is an array type.
108 StrTy = Context.CharTy;
109 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCallc33dec32010-03-15 10:54:44 +0000110 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlsson315d2292009-06-07 18:45:35 +0000111 StrTy.addConst();
112 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
113 ArrayType::Normal, 0);
114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Anders Carlsson315d2292009-06-07 18:45:35 +0000116 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
117}
118
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000119Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
120 SourceLocation EncodeLoc,
121 SourceLocation LParenLoc,
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000122 TypeTy *ty,
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000123 SourceLocation RParenLoc) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +0000124 // FIXME: Preserve type source info ?
125 QualType EncodedType = GetTypeFromParser(ty);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000126
Anders Carlsson315d2292009-06-07 18:45:35 +0000127 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000128}
129
130Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
131 SourceLocation AtLoc,
132 SourceLocation SelLoc,
133 SourceLocation LParenLoc,
134 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000135 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +0000136 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian0571d9b2009-06-16 16:25:00 +0000137 if (!Method)
138 Method = LookupFactoryMethodInGlobalPool(Sel,
139 SourceRange(LParenLoc, RParenLoc));
140 if (!Method)
141 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
142
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000143 QualType Ty = Context.getObjCSelType();
Daniel Dunbar45858d22010-02-03 20:11:42 +0000144 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000145}
146
147Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
148 SourceLocation AtLoc,
149 SourceLocation ProtoLoc,
150 SourceLocation LParenLoc,
151 SourceLocation RParenLoc) {
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000152 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000153 if (!PDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000154 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000155 return true;
156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000158 QualType Ty = Context.getObjCProtoType();
159 if (Ty.isNull())
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000160 return true;
Steve Naroff7cae42b2009-07-10 23:34:53 +0000161 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000162 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000163}
164
Mike Stump11289f42009-09-09 15:08:12 +0000165bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
166 Selector Sel, ObjCMethodDecl *Method,
Chris Lattnere4b95692008-11-24 03:33:13 +0000167 bool isClassMessage,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000168 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump11289f42009-09-09 15:08:12 +0000169 QualType &ReturnType) {
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000170 if (!Method) {
Daniel Dunbar83876b42008-09-11 00:04:36 +0000171 // Apply default argument promotion as for (C99 6.5.2.2p6).
172 for (unsigned i = 0; i != NumArgs; i++)
173 DefaultArgumentPromotion(Args[i]);
174
Chris Lattnere4b95692008-11-24 03:33:13 +0000175 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
176 diag::warn_inst_method_not_found;
177 Diag(lbrac, DiagID)
178 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000179 ReturnType = Context.getObjCIdType();
180 return false;
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattnere4b95692008-11-24 03:33:13 +0000183 ReturnType = Method->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000184
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000185 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian60462092010-04-08 00:30:06 +0000186 // Method might have more arguments than selector indicates. This is due
187 // to addition of c-style arguments in method.
188 if (Method->param_size() > Sel.getNumArgs())
189 NumNamedArgs = Method->param_size();
190 // FIXME. This need be cleaned up.
191 if (NumArgs < NumNamedArgs) {
192 Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2;
193 return false;
194 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000195
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000196 bool IsError = false;
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000197 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000198 Expr *argExpr = Args[i];
199 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattnera4997152009-02-20 18:43:26 +0000201 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000202 QualType rhsType = argExpr->getType();
203
Mike Stump11289f42009-09-09 15:08:12 +0000204 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnerb1f73982008-04-02 17:17:33 +0000205 if (lhsType->isArrayType())
206 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000207 else if (lhsType->isFunctionType())
208 lhsType = Context.getPointerType(lhsType);
209
Mike Stump11289f42009-09-09 15:08:12 +0000210 AssignConvertType Result =
Chris Lattnerb1f73982008-04-02 17:17:33 +0000211 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000212 if (Args[i] != argExpr) // The expression was converted.
213 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump11289f42009-09-09 15:08:12 +0000214
215 IsError |=
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000216 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor7c3bbdf2009-12-16 03:45:30 +0000217 argExpr, AA_Sending);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000218 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000219
220 // Promote additional arguments to variadic methods.
221 if (Method->isVariadic()) {
Anders Carlssona7d069d2009-01-16 16:48:51 +0000222 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000223 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000224 } else {
225 // Check for extra arguments to non-variadic methods.
226 if (NumArgs != NumNamedArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000227 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +0000228 diag::err_typecheck_call_too_many_args)
Chris Lattnercedef8d2008-11-21 18:44:24 +0000229 << 2 /*method*/ << Method->getSourceRange()
Chris Lattner3b054132008-11-19 05:08:23 +0000230 << SourceRange(Args[NumNamedArgs]->getLocStart(),
231 Args[NumArgs-1]->getLocEnd());
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000232 }
233 }
234
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000235 return IsError;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000236}
237
Steve Naroff3f49fee2009-03-04 15:11:40 +0000238bool Sema::isSelfExpr(Expr *RExpr) {
239 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
240 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
241 return true;
242 return false;
243}
244
Steve Naroffb162f172009-02-26 15:55:06 +0000245// Helper method for ActOnClassMethod/ActOnInstanceMethod.
246// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000247// If failed, then we search in class's root for an instance method.
Steve Naroffb162f172009-02-26 15:55:06 +0000248// Returns 0 if no method is found.
Steve Naroffed031702009-03-08 18:56:13 +0000249ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Naroffb162f172009-02-26 15:55:06 +0000250 ObjCInterfaceDecl *ClassDecl) {
251 ObjCMethodDecl *Method = 0;
Steve Naroffed031702009-03-08 18:56:13 +0000252 // lookup in class and all superclasses
253 while (ClassDecl && !Method) {
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000254 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000255 Method = ImpDecl->getClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000256
Steve Naroffed031702009-03-08 18:56:13 +0000257 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000258 if (!Method)
259 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000260
Steve Naroffed031702009-03-08 18:56:13 +0000261 // Before we give up, check if the selector is an instance method.
262 // But only in the root. This matches gcc's behaviour and what the
263 // runtime expects.
264 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000265 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000266 // Look through local category implementations associated
Steve Naroffed031702009-03-08 18:56:13 +0000267 // with the root class.
Mike Stump11289f42009-09-09 15:08:12 +0000268 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000269 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
270 }
Mike Stump11289f42009-09-09 15:08:12 +0000271
Steve Naroffed031702009-03-08 18:56:13 +0000272 ClassDecl = ClassDecl->getSuperClass();
Steve Naroffb162f172009-02-26 15:55:06 +0000273 }
Steve Naroffed031702009-03-08 18:56:13 +0000274 return Method;
275}
276
277ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
278 ObjCInterfaceDecl *ClassDecl) {
279 ObjCMethodDecl *Method = 0;
280 while (ClassDecl && !Method) {
281 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000282 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000283 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000284
Steve Naroffed031702009-03-08 18:56:13 +0000285 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000286 if (!Method)
287 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroffed031702009-03-08 18:56:13 +0000288 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000289 }
Steve Naroffb162f172009-02-26 15:55:06 +0000290 return Method;
291}
292
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000293/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
294/// objective C interface. This is a property reference expression.
295Action::OwningExprResult Sema::
296HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattner90c58fa2010-04-11 07:51:10 +0000297 Expr *BaseExpr, DeclarationName MemberName,
298 SourceLocation MemberLoc) {
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000299 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
300 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
301 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
302
303 // Search for a declared property first.
304 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
305 // Check whether we can reference this property.
306 if (DiagnoseUseOfDecl(PD, MemberLoc))
307 return ExprError();
308 QualType ResTy = PD->getType();
309 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
310 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
311 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
312 ResTy = Getter->getResultType();
313 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
314 MemberLoc, BaseExpr));
315 }
316 // Check protocols on qualified interfaces.
317 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
318 E = OPT->qual_end(); I != E; ++I)
319 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
320 // Check whether we can reference this property.
321 if (DiagnoseUseOfDecl(PD, MemberLoc))
322 return ExprError();
323
324 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
325 MemberLoc, BaseExpr));
326 }
327 // If that failed, look for an "implicit" property by seeing if the nullary
328 // selector is implemented.
329
330 // FIXME: The logic for looking up nullary and unary selectors should be
331 // shared with the code in ActOnInstanceMessage.
332
333 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
334 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
335
336 // If this reference is in an @implementation, check for 'private' methods.
337 if (!Getter)
338 Getter = IFace->lookupPrivateInstanceMethod(Sel);
339
340 // Look through local category implementations associated with the class.
341 if (!Getter)
342 Getter = IFace->getCategoryInstanceMethod(Sel);
343 if (Getter) {
344 // Check if we can reference this property.
345 if (DiagnoseUseOfDecl(Getter, MemberLoc))
346 return ExprError();
347 }
348 // If we found a getter then this may be a valid dot-reference, we
349 // will look for the matching setter, in case it is needed.
350 Selector SetterSel =
351 SelectorTable::constructSetterName(PP.getIdentifierTable(),
352 PP.getSelectorTable(), Member);
353 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
354 if (!Setter) {
355 // If this reference is in an @implementation, also check for 'private'
356 // methods.
357 Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
358 }
359 // Look through local category implementations associated with the class.
360 if (!Setter)
361 Setter = IFace->getCategoryInstanceMethod(SetterSel);
362
363 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
364 return ExprError();
365
366 if (Getter) {
367 QualType PType;
368 PType = Getter->getResultType();
369 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType,
370 Setter, MemberLoc, BaseExpr));
371 }
372
373 // Attempt to correct for typos in property names.
374 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
375 if (CorrectTypo(Res, 0, 0, IFace, false, OPT) &&
376 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattner90c58fa2010-04-11 07:51:10 +0000377 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000378 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattner90c58fa2010-04-11 07:51:10 +0000379 << MemberName << QualType(OPT, 0) << TypoResult
380 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000381 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
382 Diag(Property->getLocation(), diag::note_previous_decl)
383 << Property->getDeclName();
Chris Lattner90c58fa2010-04-11 07:51:10 +0000384 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000385 }
Chris Lattner90c58fa2010-04-11 07:51:10 +0000386
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000387 Diag(MemberLoc, diag::err_property_not_found)
388 << MemberName << QualType(OPT, 0);
389 if (Setter && !Getter)
390 Diag(Setter->getLocation(), diag::note_getter_unavailable)
391 << MemberName << BaseExpr->getSourceRange();
392 return ExprError();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000393}
394
395
396
Chris Lattnera36ec422010-04-11 08:28:14 +0000397Action::OwningExprResult Sema::
398ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
399 IdentifierInfo &propertyName,
400 SourceLocation receiverNameLoc,
401 SourceLocation propertyNameLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000402
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000403 IdentifierInfo *receiverNamePtr = &receiverName;
404 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr);
Chris Lattnera36ec422010-04-11 08:28:14 +0000405 if (IFace == 0) {
406 // If the "receiver" is 'super' in a method, handle it as an expression-like
407 // property reference.
408 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
409 if (receiverNamePtr->isStr("super")) {
410 if (CurMethod->isInstanceMethod()) {
411 QualType T =
412 Context.getObjCInterfaceType(CurMethod->getClassInterface());
413 T = Context.getObjCObjectPointerType(T);
414 Expr *SuperExpr = new (Context) ObjCSuperExpr(receiverNameLoc, T);
415
416 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
417 SuperExpr, &propertyName,
418 propertyNameLoc);
419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattnera36ec422010-04-11 08:28:14 +0000421 // Otherwise, if this is a class method, try dispatching to our
422 // superclass.
423 IFace = CurMethod->getClassInterface()->getSuperClass();
424 }
425
426 if (IFace == 0) {
427 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
428 return ExprError();
429 }
430 }
431
432 // Search for a declared property first.
Steve Naroff9527bbf2009-03-09 21:12:44 +0000433 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000434 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000435
436 // If this reference is in an @implementation, check for 'private' methods.
437 if (!Getter)
438 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
439 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000440 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000441 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000442
443 if (Getter) {
444 // FIXME: refactor/share with ActOnMemberReference().
445 // Check if we can reference this property.
446 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
447 return ExprError();
448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Steve Naroff9527bbf2009-03-09 21:12:44 +0000450 // Look for the matching setter, in case it is needed.
Mike Stump11289f42009-09-09 15:08:12 +0000451 Selector SetterSel =
452 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Naroffc7597f82009-03-10 17:24:38 +0000453 PP.getSelectorTable(), &propertyName);
Mike Stump11289f42009-09-09 15:08:12 +0000454
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000455 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000456 if (!Setter) {
457 // If this reference is in an @implementation, also check for 'private'
458 // methods.
459 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
460 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000461 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000462 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000463 }
464 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000465 if (!Setter)
466 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000467
468 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
469 return ExprError();
470
471 if (Getter || Setter) {
472 QualType PType;
473
474 if (Getter)
475 PType = Getter->getResultType();
476 else {
477 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
478 E = Setter->param_end(); PI != E; ++PI)
479 PType = (*PI)->getType();
480 }
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000481 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump11289f42009-09-09 15:08:12 +0000482 Getter, PType, Setter,
Steve Naroff9527bbf2009-03-09 21:12:44 +0000483 propertyNameLoc, IFace, receiverNameLoc));
484 }
485 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
486 << &propertyName << Context.getObjCInterfaceType(IFace));
487}
488
489
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000490// ActOnClassMessage - used for both unary and keyword messages.
491// ArgExprs is optional - if it is present, the number of expressions
492// is obtained from Sel.getNumArgs().
Chris Lattnera36ec422010-04-11 08:28:14 +0000493Sema::ExprResult Sema::
494ActOnClassMessage(Scope *S, IdentifierInfo *receiverName, Selector Sel,
495 SourceLocation lbrac, SourceLocation receiverLoc,
496 SourceLocation selectorLoc, SourceLocation rbrac,
497 ExprTy **Args, unsigned NumArgs) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000498 assert(receiverName && "missing receiver class name");
499
500 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000501 ObjCInterfaceDecl *ClassDecl = 0;
Chris Lattner509a32d2010-04-12 17:25:51 +0000502 bool isSuper = false;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000504 // Special case a message to super, which can be either a class message or an
505 // instance message, depending on what CurMethodDecl is.
Chris Lattner4500f722008-11-20 05:35:30 +0000506 if (receiverName->isStr("super")) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000507 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000508 ObjCInterfaceDecl *OID = CurMethod->getClassInterface();
Fariborz Jahaniandd2d75b2009-01-07 21:01:41 +0000509 if (!OID)
Mike Stump11289f42009-09-09 15:08:12 +0000510 return Diag(lbrac, diag::error_no_super_class_message)
Chris Lattnera36ec422010-04-11 08:28:14 +0000511 << CurMethod->getDeclName();
Fariborz Jahaniandd2d75b2009-01-07 21:01:41 +0000512 ClassDecl = OID->getSuperClass();
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000513 if (ClassDecl == 0)
Fariborz Jahaniandd2d75b2009-01-07 21:01:41 +0000514 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Chris Lattnera36ec422010-04-11 08:28:14 +0000515 if (CurMethod->isInstanceMethod()) {
Steve Naroff9e4ac112008-11-19 15:54:23 +0000516 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000517 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek5a201952009-02-07 01:47:29 +0000518 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
519 superTy);
Steve Naroff9e4ac112008-11-19 15:54:23 +0000520 // We are really in an instance method, redirect.
Mike Stump11289f42009-09-09 15:08:12 +0000521 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlsson978f08d2009-02-14 18:21:46 +0000522 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff9e4ac112008-11-19 15:54:23 +0000523 }
Chris Lattnera36ec422010-04-11 08:28:14 +0000524
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000525 // Otherwise, if this is a class method, try dispatching to our
526 // superclass, which is in ClassDecl.
Chris Lattner509a32d2010-04-12 17:25:51 +0000527 isSuper = true;
Mike Stump11289f42009-09-09 15:08:12 +0000528 }
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000529 }
530
531 if (ClassDecl == 0)
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000532 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000533
Steve Naroffe2177fb2008-07-25 19:39:00 +0000534 // The following code allows for the following GCC-ism:
Steve Naroffd5bf26f2008-06-04 23:08:38 +0000535 //
536 // typedef XCElementDisplayRect XCElementGraphicsRect;
537 //
538 // @implementation XCRASlice
539 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
540 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
541 // }
542 //
Steve Naroffe2177fb2008-07-25 19:39:00 +0000543 // If necessary, the following lookup could move to getObjCInterfaceDecl().
544 if (!ClassDecl) {
John McCall9f3059a2009-10-09 21:13:30 +0000545 NamedDecl *IDecl
546 = LookupSingleName(TUScope, receiverName, LookupOrdinaryName);
Douglas Gregorcfd70242010-02-19 15:18:45 +0000547 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl))
548 if (const ObjCInterfaceType *OCIT
549 = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>())
550 ClassDecl = OCIT->getDecl();
551
552 if (!ClassDecl) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000553 // Give a better error message for invalid use of super.
554 if (receiverName->isStr("super"))
555 Diag(receiverLoc, diag::err_invalid_receiver_to_message_super);
556 else
557 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
Douglas Gregorcfd70242010-02-19 15:18:45 +0000558 return true;
Steve Naroffe2177fb2008-07-25 19:39:00 +0000559 }
560 }
561 assert(ClassDecl && "missing interface declaration");
Steve Naroffd5bf26f2008-06-04 23:08:38 +0000562 ObjCMethodDecl *Method = 0;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000563 QualType returnType;
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000564 if (ClassDecl->isForwardDecl()) {
565 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian52760142009-05-08 23:45:49 +0000566 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000567 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
568 if (Method)
Mike Stump11289f42009-09-09 15:08:12 +0000569 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian52760142009-05-08 23:45:49 +0000570 << Method->getDeclName();
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000571 }
572 if (!Method)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000573 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Steve Naroffe2177fb2008-07-25 19:39:00 +0000575 // If we have an implementation in scope, check "private" methods.
Steve Naroffb162f172009-02-26 15:55:06 +0000576 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000577 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffe2177fb2008-07-25 19:39:00 +0000578
Douglas Gregor171c45a2009-02-18 21:56:37 +0000579 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
580 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000581
582 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000583 lbrac, rbrac, returnType))
584 return true;
Ted Kremeneka3a37ae2008-06-24 15:50:53 +0000585
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000586 returnType = returnType.getNonReferenceType();
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner509a32d2010-04-12 17:25:51 +0000588 // FIXME: need to do a better job handling 'super' usage within a class. For
589 // now, we simply pass the "super" identifier through (which isn't consistent
590 // with instance methods.
591 if (isSuper)
592 return new (Context) ObjCMessageExpr(Context, receiverName, receiverLoc,
593 Sel, returnType, Method, lbrac, rbrac,
594 ArgExprs, NumArgs);
595
Mike Stump87c57ac2009-05-16 07:39:55 +0000596 // If we have the ObjCInterfaceDecl* for the class that is receiving the
597 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
598 // IdentifierInfo* for the class.
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000599 return new (Context) ObjCMessageExpr(Context, ClassDecl, receiverLoc,
600 Sel, returnType, Method, lbrac, rbrac,
601 ArgExprs, NumArgs);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000602}
603
604// ActOnInstanceMessage - used for both unary and keyword messages.
605// ArgExprs is optional - if it is present, the number of expressions
606// is obtained from Sel.getNumArgs().
Chris Lattnerf1c149d2008-07-21 06:31:05 +0000607Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump11289f42009-09-09 15:08:12 +0000608 SourceLocation lbrac,
Anders Carlsson978f08d2009-02-14 18:21:46 +0000609 SourceLocation receiverLoc,
Chris Lattner574dee62008-07-26 22:17:49 +0000610 SourceLocation rbrac,
611 ExprTy **Args, unsigned NumArgs) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000612 assert(receiver && "missing receiver expression");
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000614 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
615 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump11289f42009-09-09 15:08:12 +0000616
Chris Lattner11a82742009-04-29 05:48:32 +0000617 // If necessary, apply function/array conversion to the receiver.
618 // C99 6.7.5.3p[7,8].
Douglas Gregorb92a1562010-02-03 00:27:59 +0000619 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000621 QualType returnType;
Chris Lattner574dee62008-07-26 22:17:49 +0000622 QualType ReceiverCType =
623 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff773cdc72008-11-17 22:29:32 +0000624
Chris Lattnera36ec422010-04-11 08:28:14 +0000625#if 0
Steve Naroff773cdc72008-11-17 22:29:32 +0000626 // Handle messages to 'super'.
Steve Naroff59bf35e2009-02-23 15:40:48 +0000627 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff773cdc72008-11-17 22:29:32 +0000628 ObjCMethodDecl *Method = 0;
629 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
630 // If we have an interface in scope, check 'super' methods.
631 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffed031702009-03-08 18:56:13 +0000632 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000633 Method = SuperDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000634
635 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000636 // If we have implementations in scope, check "private" methods.
637 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
638 }
Steve Naroff773cdc72008-11-17 22:29:32 +0000639 }
Anders Carlsson978f08d2009-02-14 18:21:46 +0000640
Douglas Gregor171c45a2009-02-18 21:56:37 +0000641 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
642 return true;
Anders Carlssonb6ba4682009-02-14 19:08:58 +0000643
Chris Lattnere4b95692008-11-24 03:33:13 +0000644 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff773cdc72008-11-17 22:29:32 +0000645 lbrac, rbrac, returnType))
646 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000648 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000649 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
650 Method, lbrac, rbrac,
651 ArgExprs, NumArgs);
Steve Naroff773cdc72008-11-17 22:29:32 +0000652 }
Chris Lattnera36ec422010-04-11 08:28:14 +0000653#endif
Steve Naroff773cdc72008-11-17 22:29:32 +0000654
Mike Stump11289f42009-09-09 15:08:12 +0000655 // Handle messages to id.
Steve Naroff51d4f792009-07-22 16:07:01 +0000656 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian8c46fec2009-05-21 21:04:28 +0000657 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff4a82d812008-09-30 14:38:43 +0000658 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
659 Sel, SourceRange(lbrac,rbrac));
Chris Lattnera3478342008-02-01 06:57:39 +0000660 if (!Method)
Douglas Gregorc78d3462009-04-24 21:10:55 +0000661 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump11289f42009-09-09 15:08:12 +0000662 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000663 lbrac, rbrac, returnType))
664 return true;
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000665 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000666 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
667 Method, lbrac, rbrac,
668 ArgExprs, NumArgs);
Chris Lattner6b946cc2008-07-21 05:57:44 +0000669 }
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner6b946cc2008-07-21 05:57:44 +0000671 // Handle messages to Class.
Mike Stump11289f42009-09-09 15:08:12 +0000672 if (ReceiverCType->isObjCClassType() ||
Steve Naroff51d4f792009-07-22 16:07:01 +0000673 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000674 ObjCMethodDecl *Method = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000675
Chris Lattner87899be2008-07-21 06:44:27 +0000676 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroff77170dc2009-02-23 02:25:40 +0000677 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
678 // First check the public methods in the class interface.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000679 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000680
Steve Naroffb162f172009-02-26 15:55:06 +0000681 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000682 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000683
684 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff51d4f792009-07-22 16:07:01 +0000685 // protocols (if we have qualifiers).
Steve Naroff77170dc2009-02-23 02:25:40 +0000686 }
Douglas Gregor171c45a2009-02-18 21:56:37 +0000687 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
688 return true;
Steve Naroff77170dc2009-02-23 02:25:40 +0000689 }
Steve Naroff3f49fee2009-03-04 15:11:40 +0000690 if (!Method) {
691 // If not messaging 'self', look for any factory method named 'Sel'.
692 if (!isSelfExpr(RExpr)) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000693 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianc2371ea2009-03-04 17:50:39 +0000694 if (!Method) {
Fariborz Jahanian3baaffb2009-05-05 18:34:37 +0000695 // If no class (factory) method was found, check if an _instance_
696 // method of the same name exists in the root class only.
Steve Naroff3f49fee2009-03-04 15:11:40 +0000697 Method = LookupInstanceMethodInGlobalPool(
698 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian3baaffb2009-05-05 18:34:37 +0000699 if (Method)
700 if (const ObjCInterfaceDecl *ID =
701 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
702 if (ID->getSuperClass())
703 Diag(lbrac, diag::warn_root_inst_method_not_found)
704 << Sel << SourceRange(lbrac, rbrac);
705 }
Fariborz Jahanianc2371ea2009-03-04 17:50:39 +0000706 }
Steve Naroff3f49fee2009-03-04 15:11:40 +0000707 }
708 }
Chris Lattnere4b95692008-11-24 03:33:13 +0000709 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000710 lbrac, rbrac, returnType))
711 return true;
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000712 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000713 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
714 Method, lbrac, rbrac,
715 ArgExprs, NumArgs);
Chris Lattner6b946cc2008-07-21 05:57:44 +0000716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000718 ObjCMethodDecl *Method = 0;
Chris Lattner6b946cc2008-07-21 05:57:44 +0000719 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000720
721 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000722 // long as one of the protocols implements the selector (if not, warn).
Mike Stump11289f42009-09-09 15:08:12 +0000723 if (const ObjCObjectPointerType *QIdTy =
Steve Narofffb4330f2009-06-17 22:40:22 +0000724 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Naroff8487e3e2009-02-21 21:17:01 +0000725 // Search protocols for instance methods.
Steve Narofffb4330f2009-06-17 22:40:22 +0000726 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000727 E = QIdTy->qual_end(); I != E; ++I) {
728 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000729 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattner6b946cc2008-07-21 05:57:44 +0000730 break;
Steve Naroffebc790d2009-04-07 15:07:57 +0000731 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000732 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebc790d2009-04-07 15:07:57 +0000733 break;
Chris Lattner6b946cc2008-07-21 05:57:44 +0000734 }
Mike Stump11289f42009-09-09 15:08:12 +0000735 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff7cae42b2009-07-10 23:34:53 +0000736 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000737 // We allow sending a message to a pointer to an interface (an object).
Mike Stump11289f42009-09-09 15:08:12 +0000738
Steve Naroff7cae42b2009-07-10 23:34:53 +0000739 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff4a82d812008-09-30 14:38:43 +0000740 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump87c57ac2009-05-16 07:39:55 +0000741 // faster than the following method (which can do *many* linear searches).
Steve Naroff4a82d812008-09-30 14:38:43 +0000742 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000743 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000744
Chris Lattner6b946cc2008-07-21 05:57:44 +0000745 if (!Method) {
746 // Search protocol qualifiers.
Steve Naroff7cae42b2009-07-10 23:34:53 +0000747 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff59bf35e2009-02-23 15:40:48 +0000748 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000749 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000750 break;
751 }
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000752 }
Steve Naroffc4173fa2009-02-22 19:35:57 +0000753 if (!Method) {
Steve Naroffed031702009-03-08 18:56:13 +0000754 // If we have implementations in scope, check "private" methods.
755 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000756
Steve Naroffed031702009-03-08 18:56:13 +0000757 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff3f49fee2009-03-04 15:11:40 +0000758 // If we still haven't found a method, look in the global pool. This
759 // behavior isn't very desirable, however we need it for GCC
760 // compatibility. FIXME: should we deviate??
Steve Naroffed031702009-03-08 18:56:13 +0000761 if (OCIType->qual_empty()) {
Steve Naroff3f49fee2009-03-04 15:11:40 +0000762 Method = LookupInstanceMethodInGlobalPool(
763 Sel, SourceRange(lbrac,rbrac));
Steve Naroff7cae42b2009-07-10 23:34:53 +0000764 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump11289f42009-09-09 15:08:12 +0000765 Diag(lbrac, diag::warn_maynot_respond)
Chris Lattner00dcffd2010-04-11 07:04:01 +0000766 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
Steve Naroff3f49fee2009-03-04 15:11:40 +0000767 }
Fariborz Jahanian9a8e7592009-03-03 22:19:15 +0000768 }
Steve Naroffc4173fa2009-02-22 19:35:57 +0000769 }
Douglas Gregor171c45a2009-02-18 21:56:37 +0000770 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
771 return true;
Chris Lattner1897de12009-03-09 21:19:16 +0000772 } else if (!Context.getObjCIdType().isNull() &&
773 (ReceiverCType->isPointerType() ||
Mike Stump11289f42009-09-09 15:08:12 +0000774 (ReceiverCType->isIntegerType() &&
Chris Lattner1897de12009-03-09 21:19:16 +0000775 ReceiverCType->isScalarType()))) {
776 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8676e082009-03-01 17:14:31 +0000777 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000778 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman06ed2a52009-10-20 08:27:19 +0000779 if (ReceiverCType->isPointerType())
780 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
781 else
782 ImpCastExprToType(RExpr, Context.getObjCIdType(),
783 CastExpr::CK_IntegralToPointer);
Chris Lattner1897de12009-03-09 21:19:16 +0000784 } else {
785 // Reject other random receiver types (e.g. structs).
786 Diag(lbrac, diag::err_bad_receiver_type)
787 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000788 return true;
Chris Lattner6b946cc2008-07-21 05:57:44 +0000789 }
Mike Stump11289f42009-09-09 15:08:12 +0000790
Fariborz Jahanian027b8862009-05-13 18:09:35 +0000791 if (Method)
792 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattnere4b95692008-11-24 03:33:13 +0000793 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000794 lbrac, rbrac, returnType))
795 return true;
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000796 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000797 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType, Method,
798 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000799}
Chris Lattner2a3569b2008-04-07 05:30:13 +0000800