blob: ad95f00d246939e57d1f3b7631d6670f5b693f93 [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");
Douglas Gregorb2ccf012010-04-15 22:33:43 +000081 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
82 LookupOrdinaryName);
Chris Lattnerfffd6a72009-02-18 06:06:56 +000083 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
84 Context.setObjCConstantStringInterface(StrIF);
85 Ty = Context.getObjCConstantStringInterface();
Steve Naroff7cae42b2009-07-10 23:34:53 +000086 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +000087 } else {
Steve Naroff54e59452009-04-07 14:18:33 +000088 // If there is no NSString interface defined then treat constant
Chris Lattnerfffd6a72009-02-18 06:06:56 +000089 // strings as untyped objects and let the runtime figure it out later.
90 Ty = Context.getObjCIdType();
91 }
Chris Lattner091f6982008-06-21 21:44:18 +000092 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerd7670d92009-02-18 06:13:04 +000094 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattnera3fc41d2008-01-04 22:32:30 +000095}
96
Mike Stump11289f42009-09-09 15:08:12 +000097Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Anders Carlsson315d2292009-06-07 18:45:35 +000098 QualType EncodedType,
99 SourceLocation RParenLoc) {
100 QualType StrTy;
Mike Stump11289f42009-09-09 15:08:12 +0000101 if (EncodedType->isDependentType())
Anders Carlsson315d2292009-06-07 18:45:35 +0000102 StrTy = Context.DependentTy;
103 else {
104 std::string Str;
105 Context.getObjCEncodingForType(EncodedType, Str);
106
107 // The type of @encode is the same as the type of the corresponding string,
108 // which is an array type.
109 StrTy = Context.CharTy;
110 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCallc33dec32010-03-15 10:54:44 +0000111 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlsson315d2292009-06-07 18:45:35 +0000112 StrTy.addConst();
113 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
114 ArrayType::Normal, 0);
115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Anders Carlsson315d2292009-06-07 18:45:35 +0000117 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
118}
119
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000120Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
121 SourceLocation EncodeLoc,
122 SourceLocation LParenLoc,
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000123 TypeTy *ty,
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000124 SourceLocation RParenLoc) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +0000125 // FIXME: Preserve type source info ?
126 QualType EncodedType = GetTypeFromParser(ty);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000127
Anders Carlsson315d2292009-06-07 18:45:35 +0000128 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000129}
130
131Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
132 SourceLocation AtLoc,
133 SourceLocation SelLoc,
134 SourceLocation LParenLoc,
135 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000136 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahaniancbf10f52009-08-22 21:13:55 +0000137 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian0571d9b2009-06-16 16:25:00 +0000138 if (!Method)
139 Method = LookupFactoryMethodInGlobalPool(Sel,
140 SourceRange(LParenLoc, RParenLoc));
141 if (!Method)
142 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
143
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000144 QualType Ty = Context.getObjCSelType();
Daniel Dunbar45858d22010-02-03 20:11:42 +0000145 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000146}
147
148Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
149 SourceLocation AtLoc,
150 SourceLocation ProtoLoc,
151 SourceLocation LParenLoc,
152 SourceLocation RParenLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000153 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000154 if (!PDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000155 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000156 return true;
157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000159 QualType Ty = Context.getObjCProtoType();
160 if (Ty.isNull())
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000161 return true;
Steve Naroff7cae42b2009-07-10 23:34:53 +0000162 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000163 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000164}
165
Mike Stump11289f42009-09-09 15:08:12 +0000166bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
167 Selector Sel, ObjCMethodDecl *Method,
Chris Lattnere4b95692008-11-24 03:33:13 +0000168 bool isClassMessage,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000169 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump11289f42009-09-09 15:08:12 +0000170 QualType &ReturnType) {
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000171 if (!Method) {
Daniel Dunbar83876b42008-09-11 00:04:36 +0000172 // Apply default argument promotion as for (C99 6.5.2.2p6).
173 for (unsigned i = 0; i != NumArgs; i++)
174 DefaultArgumentPromotion(Args[i]);
175
Chris Lattnere4b95692008-11-24 03:33:13 +0000176 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
177 diag::warn_inst_method_not_found;
178 Diag(lbrac, DiagID)
179 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000180 ReturnType = Context.getObjCIdType();
181 return false;
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattnere4b95692008-11-24 03:33:13 +0000184 ReturnType = Method->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000185
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000186 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian60462092010-04-08 00:30:06 +0000187 // Method might have more arguments than selector indicates. This is due
188 // to addition of c-style arguments in method.
189 if (Method->param_size() > Sel.getNumArgs())
190 NumNamedArgs = Method->param_size();
191 // FIXME. This need be cleaned up.
192 if (NumArgs < NumNamedArgs) {
Eric Christopherabf1e182010-04-16 04:48:22 +0000193 Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2
194 << NumNamedArgs << NumArgs;
Fariborz Jahanian60462092010-04-08 00:30:06 +0000195 return false;
196 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000197
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000198 bool IsError = false;
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000199 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000200 Expr *argExpr = Args[i];
201 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattnera4997152009-02-20 18:43:26 +0000203 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000204 QualType rhsType = argExpr->getType();
205
Mike Stump11289f42009-09-09 15:08:12 +0000206 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnerb1f73982008-04-02 17:17:33 +0000207 if (lhsType->isArrayType())
208 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000209 else if (lhsType->isFunctionType())
210 lhsType = Context.getPointerType(lhsType);
211
Mike Stump11289f42009-09-09 15:08:12 +0000212 AssignConvertType Result =
Chris Lattnerb1f73982008-04-02 17:17:33 +0000213 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000214 if (Args[i] != argExpr) // The expression was converted.
215 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump11289f42009-09-09 15:08:12 +0000216
217 IsError |=
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000218 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor7c3bbdf2009-12-16 03:45:30 +0000219 argExpr, AA_Sending);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000220 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000221
222 // Promote additional arguments to variadic methods.
223 if (Method->isVariadic()) {
Anders Carlssona7d069d2009-01-16 16:48:51 +0000224 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000225 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000226 } else {
227 // Check for extra arguments to non-variadic methods.
228 if (NumArgs != NumNamedArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000229 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +0000230 diag::err_typecheck_call_too_many_args)
Eric Christopher2a5aaff2010-04-16 04:56:46 +0000231 << 2 /*method*/ << NumNamedArgs << NumArgs
232 << Method->getSourceRange()
Chris Lattner3b054132008-11-19 05:08:23 +0000233 << SourceRange(Args[NumNamedArgs]->getLocStart(),
234 Args[NumArgs-1]->getLocEnd());
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000235 }
236 }
237
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000238 return IsError;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000239}
240
Steve Naroff3f49fee2009-03-04 15:11:40 +0000241bool Sema::isSelfExpr(Expr *RExpr) {
242 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
243 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
244 return true;
245 return false;
246}
247
Steve Naroffb162f172009-02-26 15:55:06 +0000248// Helper method for ActOnClassMethod/ActOnInstanceMethod.
249// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000250// If failed, then we search in class's root for an instance method.
Steve Naroffb162f172009-02-26 15:55:06 +0000251// Returns 0 if no method is found.
Steve Naroffed031702009-03-08 18:56:13 +0000252ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Naroffb162f172009-02-26 15:55:06 +0000253 ObjCInterfaceDecl *ClassDecl) {
254 ObjCMethodDecl *Method = 0;
Steve Naroffed031702009-03-08 18:56:13 +0000255 // lookup in class and all superclasses
256 while (ClassDecl && !Method) {
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000257 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000258 Method = ImpDecl->getClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000259
Steve Naroffed031702009-03-08 18:56:13 +0000260 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000261 if (!Method)
262 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000263
Steve Naroffed031702009-03-08 18:56:13 +0000264 // Before we give up, check if the selector is an instance method.
265 // But only in the root. This matches gcc's behaviour and what the
266 // runtime expects.
267 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000268 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000269 // Look through local category implementations associated
Steve Naroffed031702009-03-08 18:56:13 +0000270 // with the root class.
Mike Stump11289f42009-09-09 15:08:12 +0000271 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000272 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Steve Naroffed031702009-03-08 18:56:13 +0000275 ClassDecl = ClassDecl->getSuperClass();
Steve Naroffb162f172009-02-26 15:55:06 +0000276 }
Steve Naroffed031702009-03-08 18:56:13 +0000277 return Method;
278}
279
280ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
281 ObjCInterfaceDecl *ClassDecl) {
282 ObjCMethodDecl *Method = 0;
283 while (ClassDecl && !Method) {
284 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000285 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000286 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000287
Steve Naroffed031702009-03-08 18:56:13 +0000288 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000289 if (!Method)
290 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroffed031702009-03-08 18:56:13 +0000291 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000292 }
Steve Naroffb162f172009-02-26 15:55:06 +0000293 return Method;
294}
295
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000296/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
297/// objective C interface. This is a property reference expression.
298Action::OwningExprResult Sema::
299HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattner90c58fa2010-04-11 07:51:10 +0000300 Expr *BaseExpr, DeclarationName MemberName,
301 SourceLocation MemberLoc) {
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000302 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
303 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
304 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
305
306 // Search for a declared property first.
307 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
308 // Check whether we can reference this property.
309 if (DiagnoseUseOfDecl(PD, MemberLoc))
310 return ExprError();
311 QualType ResTy = PD->getType();
312 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
313 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
314 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
315 ResTy = Getter->getResultType();
316 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
317 MemberLoc, BaseExpr));
318 }
319 // Check protocols on qualified interfaces.
320 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
321 E = OPT->qual_end(); I != E; ++I)
322 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
323 // Check whether we can reference this property.
324 if (DiagnoseUseOfDecl(PD, MemberLoc))
325 return ExprError();
326
327 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
328 MemberLoc, BaseExpr));
329 }
330 // If that failed, look for an "implicit" property by seeing if the nullary
331 // selector is implemented.
332
333 // FIXME: The logic for looking up nullary and unary selectors should be
334 // shared with the code in ActOnInstanceMessage.
335
336 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
337 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
338
339 // If this reference is in an @implementation, check for 'private' methods.
340 if (!Getter)
341 Getter = IFace->lookupPrivateInstanceMethod(Sel);
342
343 // Look through local category implementations associated with the class.
344 if (!Getter)
345 Getter = IFace->getCategoryInstanceMethod(Sel);
346 if (Getter) {
347 // Check if we can reference this property.
348 if (DiagnoseUseOfDecl(Getter, MemberLoc))
349 return ExprError();
350 }
351 // If we found a getter then this may be a valid dot-reference, we
352 // will look for the matching setter, in case it is needed.
353 Selector SetterSel =
354 SelectorTable::constructSetterName(PP.getIdentifierTable(),
355 PP.getSelectorTable(), Member);
356 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
357 if (!Setter) {
358 // If this reference is in an @implementation, also check for 'private'
359 // methods.
360 Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
361 }
362 // Look through local category implementations associated with the class.
363 if (!Setter)
364 Setter = IFace->getCategoryInstanceMethod(SetterSel);
365
366 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
367 return ExprError();
368
369 if (Getter) {
370 QualType PType;
371 PType = Getter->getResultType();
372 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType,
373 Setter, MemberLoc, BaseExpr));
374 }
375
376 // Attempt to correct for typos in property names.
377 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000378 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000379 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattner90c58fa2010-04-11 07:51:10 +0000380 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000381 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattner90c58fa2010-04-11 07:51:10 +0000382 << MemberName << QualType(OPT, 0) << TypoResult
383 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000384 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
385 Diag(Property->getLocation(), diag::note_previous_decl)
386 << Property->getDeclName();
Chris Lattner90c58fa2010-04-11 07:51:10 +0000387 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000388 }
Chris Lattner90c58fa2010-04-11 07:51:10 +0000389
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000390 Diag(MemberLoc, diag::err_property_not_found)
391 << MemberName << QualType(OPT, 0);
392 if (Setter && !Getter)
393 Diag(Setter->getLocation(), diag::note_getter_unavailable)
394 << MemberName << BaseExpr->getSourceRange();
395 return ExprError();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000396}
397
398
399
Chris Lattnera36ec422010-04-11 08:28:14 +0000400Action::OwningExprResult Sema::
401ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
402 IdentifierInfo &propertyName,
403 SourceLocation receiverNameLoc,
404 SourceLocation propertyNameLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000405
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000406 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000407 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
408 receiverNameLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +0000409 if (IFace == 0) {
410 // If the "receiver" is 'super' in a method, handle it as an expression-like
411 // property reference.
412 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
413 if (receiverNamePtr->isStr("super")) {
414 if (CurMethod->isInstanceMethod()) {
415 QualType T =
416 Context.getObjCInterfaceType(CurMethod->getClassInterface());
417 T = Context.getObjCObjectPointerType(T);
418 Expr *SuperExpr = new (Context) ObjCSuperExpr(receiverNameLoc, T);
419
420 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
421 SuperExpr, &propertyName,
422 propertyNameLoc);
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattnera36ec422010-04-11 08:28:14 +0000425 // Otherwise, if this is a class method, try dispatching to our
426 // superclass.
427 IFace = CurMethod->getClassInterface()->getSuperClass();
428 }
429
430 if (IFace == 0) {
431 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
432 return ExprError();
433 }
434 }
435
436 // Search for a declared property first.
Steve Naroff9527bbf2009-03-09 21:12:44 +0000437 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000438 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000439
440 // If this reference is in an @implementation, check for 'private' methods.
441 if (!Getter)
442 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
443 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000444 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000445 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000446
447 if (Getter) {
448 // FIXME: refactor/share with ActOnMemberReference().
449 // Check if we can reference this property.
450 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
451 return ExprError();
452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Steve Naroff9527bbf2009-03-09 21:12:44 +0000454 // Look for the matching setter, in case it is needed.
Mike Stump11289f42009-09-09 15:08:12 +0000455 Selector SetterSel =
456 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Naroffc7597f82009-03-10 17:24:38 +0000457 PP.getSelectorTable(), &propertyName);
Mike Stump11289f42009-09-09 15:08:12 +0000458
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000459 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000460 if (!Setter) {
461 // If this reference is in an @implementation, also check for 'private'
462 // methods.
463 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
464 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000465 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000466 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000467 }
468 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000469 if (!Setter)
470 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000471
472 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
473 return ExprError();
474
475 if (Getter || Setter) {
476 QualType PType;
477
478 if (Getter)
479 PType = Getter->getResultType();
480 else {
481 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
482 E = Setter->param_end(); PI != E; ++PI)
483 PType = (*PI)->getType();
484 }
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000485 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump11289f42009-09-09 15:08:12 +0000486 Getter, PType, Setter,
Steve Naroff9527bbf2009-03-09 21:12:44 +0000487 propertyNameLoc, IFace, receiverNameLoc));
488 }
489 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
490 << &propertyName << Context.getObjCInterfaceType(IFace));
491}
492
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000493Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
494 IdentifierInfo *&Name,
495 SourceLocation NameLoc,
496 bool IsSuper,
497 bool HasTrailingDot) {
498 // If the identifier is "super" and there is no trailing dot, we're
499 // messaging super.
500 if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
501 return ObjCSuperMessage;
502
503 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
504 LookupName(Result, S);
505
506 switch (Result.getResultKind()) {
507 case LookupResult::NotFound:
Douglas Gregorca7136b2010-04-19 20:09:36 +0000508 // Normal name lookup didn't find anything. If we're in an
509 // Objective-C method, look for ivars. If we find one, we're done!
510 // FIXME: This is a hack. Ivar lookup should be part of normal lookup.
511 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
512 ObjCInterfaceDecl *ClassDeclared;
513 if (Method->getClassInterface()->lookupInstanceVariable(Name,
514 ClassDeclared))
515 return ObjCInstanceMessage;
516 }
517
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000518 // Break out; we'll perform typo correction below.
519 break;
520
521 case LookupResult::NotFoundInCurrentInstantiation:
522 case LookupResult::FoundOverloaded:
523 case LookupResult::FoundUnresolvedValue:
524 case LookupResult::Ambiguous:
525 Result.suppressDiagnostics();
526 return ObjCInstanceMessage;
527
528 case LookupResult::Found: {
529 // We found something. If it's a type, then we have a class
530 // message. Otherwise, it's an instance message.
531 NamedDecl *ND = Result.getFoundDecl();
532 if (isa<ObjCInterfaceDecl>(ND) || isa<TypeDecl>(ND) ||
533 isa<UnresolvedUsingTypenameDecl>(ND))
534 return ObjCClassMessage;
535
536 return ObjCInstanceMessage;
537 }
538 }
539
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000540 // Determine our typo-correction context.
541 CorrectTypoContext CTC = CTC_Expression;
542 if (ObjCMethodDecl *Method = getCurMethodDecl())
543 if (Method->getClassInterface() &&
544 Method->getClassInterface()->getSuperClass())
545 CTC = CTC_ObjCMessageReceiver;
546
547 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
548 if (Result.isSingleResult()) {
549 // If we found a declaration, correct when it refers to an Objective-C
550 // class.
551 NamedDecl *ND = Result.getFoundDecl();
552 if (isa<ObjCInterfaceDecl>(ND)) {
553 Diag(NameLoc, diag::err_unknown_receiver_suggest)
554 << Name << Result.getLookupName()
555 << FixItHint::CreateReplacement(SourceRange(NameLoc),
556 ND->getNameAsString());
557 Diag(ND->getLocation(), diag::note_previous_decl)
558 << Corrected;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000559
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000560 Name = ND->getIdentifier();
561 return ObjCClassMessage;
562 }
563 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
564 Corrected.getAsIdentifierInfo()->isStr("super")) {
565 // If we've found the keyword "super", this is a send to super.
566 Diag(NameLoc, diag::err_unknown_receiver_suggest)
567 << Name << Corrected
568 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
569 Name = Corrected.getAsIdentifierInfo();
570 return ObjCSuperMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000571 }
572 }
573
574 // Fall back: let the parser try to parse it as an instance message.
575 return ObjCInstanceMessage;
576}
Steve Naroff9527bbf2009-03-09 21:12:44 +0000577
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000578// ActOnClassMessage - used for both unary and keyword messages.
579// ArgExprs is optional - if it is present, the number of expressions
580// is obtained from Sel.getNumArgs().
Chris Lattnera36ec422010-04-11 08:28:14 +0000581Sema::ExprResult Sema::
582ActOnClassMessage(Scope *S, IdentifierInfo *receiverName, Selector Sel,
583 SourceLocation lbrac, SourceLocation receiverLoc,
584 SourceLocation selectorLoc, SourceLocation rbrac,
585 ExprTy **Args, unsigned NumArgs) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000586 assert(receiverName && "missing receiver class name");
587
588 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000589 ObjCInterfaceDecl *ClassDecl = 0;
Chris Lattner509a32d2010-04-12 17:25:51 +0000590 bool isSuper = false;
Mike Stump11289f42009-09-09 15:08:12 +0000591
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000592 // Special case a message to super, which can be either a class message or an
593 // instance message, depending on what CurMethodDecl is.
Chris Lattner4500f722008-11-20 05:35:30 +0000594 if (receiverName->isStr("super")) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000595 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000596 ObjCInterfaceDecl *OID = CurMethod->getClassInterface();
Fariborz Jahaniandd2d75b2009-01-07 21:01:41 +0000597 if (!OID)
Mike Stump11289f42009-09-09 15:08:12 +0000598 return Diag(lbrac, diag::error_no_super_class_message)
Chris Lattnera36ec422010-04-11 08:28:14 +0000599 << CurMethod->getDeclName();
Fariborz Jahaniandd2d75b2009-01-07 21:01:41 +0000600 ClassDecl = OID->getSuperClass();
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000601 if (ClassDecl == 0)
Fariborz Jahaniandd2d75b2009-01-07 21:01:41 +0000602 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Chris Lattnera36ec422010-04-11 08:28:14 +0000603 if (CurMethod->isInstanceMethod()) {
Steve Naroff9e4ac112008-11-19 15:54:23 +0000604 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000605 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek5a201952009-02-07 01:47:29 +0000606 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
607 superTy);
Steve Naroff9e4ac112008-11-19 15:54:23 +0000608 // We are really in an instance method, redirect.
Mike Stump11289f42009-09-09 15:08:12 +0000609 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlsson978f08d2009-02-14 18:21:46 +0000610 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff9e4ac112008-11-19 15:54:23 +0000611 }
Chris Lattnera36ec422010-04-11 08:28:14 +0000612
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000613 // Otherwise, if this is a class method, try dispatching to our
614 // superclass, which is in ClassDecl.
Chris Lattner509a32d2010-04-12 17:25:51 +0000615 isSuper = true;
Mike Stump11289f42009-09-09 15:08:12 +0000616 }
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000617 }
618
619 if (ClassDecl == 0)
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000620 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc, true);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Steve Naroffe2177fb2008-07-25 19:39:00 +0000622 // The following code allows for the following GCC-ism:
Steve Naroffd5bf26f2008-06-04 23:08:38 +0000623 //
624 // typedef XCElementDisplayRect XCElementGraphicsRect;
625 //
626 // @implementation XCRASlice
627 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
628 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
629 // }
630 //
Steve Naroffe2177fb2008-07-25 19:39:00 +0000631 // If necessary, the following lookup could move to getObjCInterfaceDecl().
632 if (!ClassDecl) {
John McCall9f3059a2009-10-09 21:13:30 +0000633 NamedDecl *IDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000634 = LookupSingleName(TUScope, receiverName, receiverLoc,
635 LookupOrdinaryName);
Douglas Gregorcfd70242010-02-19 15:18:45 +0000636 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl))
637 if (const ObjCInterfaceType *OCIT
638 = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>())
639 ClassDecl = OCIT->getDecl();
640
641 if (!ClassDecl) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000642 // Give a better error message for invalid use of super.
643 if (receiverName->isStr("super"))
644 Diag(receiverLoc, diag::err_invalid_receiver_to_message_super);
645 else
646 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
Douglas Gregorcfd70242010-02-19 15:18:45 +0000647 return true;
Steve Naroffe2177fb2008-07-25 19:39:00 +0000648 }
649 }
650 assert(ClassDecl && "missing interface declaration");
Steve Naroffd5bf26f2008-06-04 23:08:38 +0000651 ObjCMethodDecl *Method = 0;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000652 QualType returnType;
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000653 if (ClassDecl->isForwardDecl()) {
654 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian52760142009-05-08 23:45:49 +0000655 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000656 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
657 if (Method)
Mike Stump11289f42009-09-09 15:08:12 +0000658 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian52760142009-05-08 23:45:49 +0000659 << Method->getDeclName();
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000660 }
661 if (!Method)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000662 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000663
Steve Naroffe2177fb2008-07-25 19:39:00 +0000664 // If we have an implementation in scope, check "private" methods.
Steve Naroffb162f172009-02-26 15:55:06 +0000665 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000666 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffe2177fb2008-07-25 19:39:00 +0000667
Douglas Gregor171c45a2009-02-18 21:56:37 +0000668 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
669 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000670
671 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000672 lbrac, rbrac, returnType))
673 return true;
Ted Kremeneka3a37ae2008-06-24 15:50:53 +0000674
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000675 returnType = returnType.getNonReferenceType();
Mike Stump11289f42009-09-09 15:08:12 +0000676
Chris Lattner509a32d2010-04-12 17:25:51 +0000677 // FIXME: need to do a better job handling 'super' usage within a class. For
678 // now, we simply pass the "super" identifier through (which isn't consistent
679 // with instance methods.
680 if (isSuper)
681 return new (Context) ObjCMessageExpr(Context, receiverName, receiverLoc,
682 Sel, returnType, Method, lbrac, rbrac,
683 ArgExprs, NumArgs);
684
Mike Stump87c57ac2009-05-16 07:39:55 +0000685 // If we have the ObjCInterfaceDecl* for the class that is receiving the
686 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
687 // IdentifierInfo* for the class.
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000688 return new (Context) ObjCMessageExpr(Context, ClassDecl, receiverLoc,
689 Sel, returnType, Method, lbrac, rbrac,
690 ArgExprs, NumArgs);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000691}
692
693// ActOnInstanceMessage - used for both unary and keyword messages.
694// ArgExprs is optional - if it is present, the number of expressions
695// is obtained from Sel.getNumArgs().
Chris Lattnerf1c149d2008-07-21 06:31:05 +0000696Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump11289f42009-09-09 15:08:12 +0000697 SourceLocation lbrac,
Anders Carlsson978f08d2009-02-14 18:21:46 +0000698 SourceLocation receiverLoc,
Chris Lattner574dee62008-07-26 22:17:49 +0000699 SourceLocation rbrac,
700 ExprTy **Args, unsigned NumArgs) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000701 assert(receiver && "missing receiver expression");
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000703 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
704 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattner11a82742009-04-29 05:48:32 +0000706 // If necessary, apply function/array conversion to the receiver.
707 // C99 6.7.5.3p[7,8].
Douglas Gregorb92a1562010-02-03 00:27:59 +0000708 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump11289f42009-09-09 15:08:12 +0000709
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000710 QualType returnType;
Chris Lattner574dee62008-07-26 22:17:49 +0000711 QualType ReceiverCType =
712 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff773cdc72008-11-17 22:29:32 +0000713
Mike Stump11289f42009-09-09 15:08:12 +0000714 // Handle messages to id.
Steve Naroff51d4f792009-07-22 16:07:01 +0000715 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian8c46fec2009-05-21 21:04:28 +0000716 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff4a82d812008-09-30 14:38:43 +0000717 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
718 Sel, SourceRange(lbrac,rbrac));
Chris Lattnera3478342008-02-01 06:57:39 +0000719 if (!Method)
Douglas Gregorc78d3462009-04-24 21:10:55 +0000720 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump11289f42009-09-09 15:08:12 +0000721 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000722 lbrac, rbrac, returnType))
723 return true;
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000724 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000725 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
726 Method, lbrac, rbrac,
727 ArgExprs, NumArgs);
Chris Lattner6b946cc2008-07-21 05:57:44 +0000728 }
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattner6b946cc2008-07-21 05:57:44 +0000730 // Handle messages to Class.
Mike Stump11289f42009-09-09 15:08:12 +0000731 if (ReceiverCType->isObjCClassType() ||
Steve Naroff51d4f792009-07-22 16:07:01 +0000732 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000733 ObjCMethodDecl *Method = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000734
Chris Lattner87899be2008-07-21 06:44:27 +0000735 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroff77170dc2009-02-23 02:25:40 +0000736 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
737 // First check the public methods in the class interface.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000738 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Steve Naroffb162f172009-02-26 15:55:06 +0000740 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000741 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000742
743 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff51d4f792009-07-22 16:07:01 +0000744 // protocols (if we have qualifiers).
Steve Naroff77170dc2009-02-23 02:25:40 +0000745 }
Douglas Gregor171c45a2009-02-18 21:56:37 +0000746 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
747 return true;
Steve Naroff77170dc2009-02-23 02:25:40 +0000748 }
Steve Naroff3f49fee2009-03-04 15:11:40 +0000749 if (!Method) {
750 // If not messaging 'self', look for any factory method named 'Sel'.
751 if (!isSelfExpr(RExpr)) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000752 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianc2371ea2009-03-04 17:50:39 +0000753 if (!Method) {
Fariborz Jahanian3baaffb2009-05-05 18:34:37 +0000754 // If no class (factory) method was found, check if an _instance_
755 // method of the same name exists in the root class only.
Steve Naroff3f49fee2009-03-04 15:11:40 +0000756 Method = LookupInstanceMethodInGlobalPool(
757 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian3baaffb2009-05-05 18:34:37 +0000758 if (Method)
759 if (const ObjCInterfaceDecl *ID =
760 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
761 if (ID->getSuperClass())
762 Diag(lbrac, diag::warn_root_inst_method_not_found)
763 << Sel << SourceRange(lbrac, rbrac);
764 }
Fariborz Jahanianc2371ea2009-03-04 17:50:39 +0000765 }
Steve Naroff3f49fee2009-03-04 15:11:40 +0000766 }
767 }
Chris Lattnere4b95692008-11-24 03:33:13 +0000768 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000769 lbrac, rbrac, returnType))
770 return true;
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000771 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000772 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
773 Method, lbrac, rbrac,
774 ArgExprs, NumArgs);
Chris Lattner6b946cc2008-07-21 05:57:44 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000777 ObjCMethodDecl *Method = 0;
Chris Lattner6b946cc2008-07-21 05:57:44 +0000778 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000779
780 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000781 // long as one of the protocols implements the selector (if not, warn).
Mike Stump11289f42009-09-09 15:08:12 +0000782 if (const ObjCObjectPointerType *QIdTy =
Steve Narofffb4330f2009-06-17 22:40:22 +0000783 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Naroff8487e3e2009-02-21 21:17:01 +0000784 // Search protocols for instance methods.
Steve Narofffb4330f2009-06-17 22:40:22 +0000785 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000786 E = QIdTy->qual_end(); I != E; ++I) {
787 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000788 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattner6b946cc2008-07-21 05:57:44 +0000789 break;
Steve Naroffebc790d2009-04-07 15:07:57 +0000790 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000791 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebc790d2009-04-07 15:07:57 +0000792 break;
Chris Lattner6b946cc2008-07-21 05:57:44 +0000793 }
Mike Stump11289f42009-09-09 15:08:12 +0000794 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff7cae42b2009-07-10 23:34:53 +0000795 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000796 // We allow sending a message to a pointer to an interface (an object).
Mike Stump11289f42009-09-09 15:08:12 +0000797
Steve Naroff7cae42b2009-07-10 23:34:53 +0000798 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff4a82d812008-09-30 14:38:43 +0000799 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump87c57ac2009-05-16 07:39:55 +0000800 // faster than the following method (which can do *many* linear searches).
Steve Naroff4a82d812008-09-30 14:38:43 +0000801 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000802 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000803
Chris Lattner6b946cc2008-07-21 05:57:44 +0000804 if (!Method) {
805 // Search protocol qualifiers.
Steve Naroff7cae42b2009-07-10 23:34:53 +0000806 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff59bf35e2009-02-23 15:40:48 +0000807 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000808 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000809 break;
810 }
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000811 }
Steve Naroffc4173fa2009-02-22 19:35:57 +0000812 if (!Method) {
Steve Naroffed031702009-03-08 18:56:13 +0000813 // If we have implementations in scope, check "private" methods.
814 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000815
Steve Naroffed031702009-03-08 18:56:13 +0000816 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff3f49fee2009-03-04 15:11:40 +0000817 // If we still haven't found a method, look in the global pool. This
818 // behavior isn't very desirable, however we need it for GCC
819 // compatibility. FIXME: should we deviate??
Steve Naroffed031702009-03-08 18:56:13 +0000820 if (OCIType->qual_empty()) {
Steve Naroff3f49fee2009-03-04 15:11:40 +0000821 Method = LookupInstanceMethodInGlobalPool(
822 Sel, SourceRange(lbrac,rbrac));
Steve Naroff7cae42b2009-07-10 23:34:53 +0000823 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump11289f42009-09-09 15:08:12 +0000824 Diag(lbrac, diag::warn_maynot_respond)
Chris Lattner00dcffd2010-04-11 07:04:01 +0000825 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
Steve Naroff3f49fee2009-03-04 15:11:40 +0000826 }
Fariborz Jahanian9a8e7592009-03-03 22:19:15 +0000827 }
Steve Naroffc4173fa2009-02-22 19:35:57 +0000828 }
Douglas Gregor171c45a2009-02-18 21:56:37 +0000829 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
830 return true;
Chris Lattner1897de12009-03-09 21:19:16 +0000831 } else if (!Context.getObjCIdType().isNull() &&
832 (ReceiverCType->isPointerType() ||
Mike Stump11289f42009-09-09 15:08:12 +0000833 (ReceiverCType->isIntegerType() &&
Chris Lattner1897de12009-03-09 21:19:16 +0000834 ReceiverCType->isScalarType()))) {
835 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8676e082009-03-01 17:14:31 +0000836 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattner1e5665e2008-11-24 06:25:27 +0000837 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman06ed2a52009-10-20 08:27:19 +0000838 if (ReceiverCType->isPointerType())
839 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
840 else
841 ImpCastExprToType(RExpr, Context.getObjCIdType(),
842 CastExpr::CK_IntegralToPointer);
Chris Lattner1897de12009-03-09 21:19:16 +0000843 } else {
844 // Reject other random receiver types (e.g. structs).
845 Diag(lbrac, diag::err_bad_receiver_type)
846 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattnerbd2d6342008-07-21 06:12:56 +0000847 return true;
Chris Lattner6b946cc2008-07-21 05:57:44 +0000848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Fariborz Jahanian027b8862009-05-13 18:09:35 +0000850 if (Method)
851 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattnere4b95692008-11-24 03:33:13 +0000852 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000853 lbrac, rbrac, returnType))
854 return true;
Anders Carlsson09ac6f22009-05-26 15:22:25 +0000855 returnType = returnType.getNonReferenceType();
Ted Kremenek2c809302010-02-11 22:41:21 +0000856 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType, Method,
857 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000858}
Chris Lattner2a3569b2008-04-07 05:30:13 +0000859