blob: cf38225de0cf9bd3c5f08972d0a32f8c590d2502 [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
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
John McCallcc14d1f2010-08-24 08:50:51 +000016#include "clang/Sema/Scope.h"
John McCall5f2d5562011-02-03 09:00:02 +000017#include "clang/Sema/ScopeInfo.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000018#include "clang/Sema/Initialization.h"
Chris Lattnera3fc41d2008-01-04 22:32:30 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/DeclObjC.h"
Steve Naroff021ca182008-05-29 21:12:08 +000021#include "clang/AST/ExprObjC.h"
Douglas Gregor0c78ad92010-04-21 19:57:20 +000022#include "clang/AST/TypeLoc.h"
Chris Lattner163ffd22009-02-18 06:48:40 +000023#include "llvm/ADT/SmallString.h"
Steve Naroff9527bbf2009-03-09 21:12:44 +000024#include "clang/Lex/Preprocessor.h"
25
Chris Lattnera3fc41d2008-01-04 22:32:30 +000026using namespace clang;
John McCall5f2d5562011-02-03 09:00:02 +000027using namespace sema;
Chris Lattnera3fc41d2008-01-04 22:32:30 +000028
John McCallfaf5fb42010-08-26 23:41:50 +000029ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
30 Expr **strings,
31 unsigned NumStrings) {
Chris Lattner163ffd22009-02-18 06:48:40 +000032 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
33
Chris Lattnerd7670d92009-02-18 06:13:04 +000034 // Most ObjC strings are formed out of a single piece. However, we *can*
35 // have strings formed out of multiple @ strings with multiple pptokens in
36 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
37 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner163ffd22009-02-18 06:48:40 +000038 StringLiteral *S = Strings[0];
Mike Stump11289f42009-09-09 15:08:12 +000039
Chris Lattnerd7670d92009-02-18 06:13:04 +000040 // If we have a multi-part string, merge it all together.
41 if (NumStrings != 1) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +000042 // Concatenate objc strings.
Chris Lattner163ffd22009-02-18 06:48:40 +000043 llvm::SmallString<128> StrBuf;
44 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump11289f42009-09-09 15:08:12 +000045
Chris Lattner630970d2009-02-18 05:49:11 +000046 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner163ffd22009-02-18 06:48:40 +000047 S = Strings[i];
Mike Stump11289f42009-09-09 15:08:12 +000048
Chris Lattner163ffd22009-02-18 06:48:40 +000049 // ObjC strings can't be wide.
Chris Lattnerd7670d92009-02-18 06:13:04 +000050 if (S->isWide()) {
51 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
52 << S->getSourceRange();
53 return true;
54 }
Mike Stump11289f42009-09-09 15:08:12 +000055
Benjamin Kramer35b077e2010-08-17 12:54:38 +000056 // Append the string.
57 StrBuf += S->getString();
Mike Stump11289f42009-09-09 15:08:12 +000058
Chris Lattner163ffd22009-02-18 06:48:40 +000059 // Get the locations of the string tokens.
60 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Chris Lattnera3fc41d2008-01-04 22:32:30 +000061 }
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattner163ffd22009-02-18 06:48:40 +000063 // Create the aggregate string with the appropriate content and location
64 // information.
65 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattnerf83b5af2009-02-18 06:40:38 +000066 Context.getPointerType(Context.CharTy),
Chris Lattner163ffd22009-02-18 06:48:40 +000067 &StrLocs[0], StrLocs.size());
Chris Lattnera3fc41d2008-01-04 22:32:30 +000068 }
Mike Stump11289f42009-09-09 15:08:12 +000069
Chris Lattner6436fb62009-02-18 06:01:06 +000070 // Verify that this composite string is acceptable for ObjC strings.
71 if (CheckObjCString(S))
Chris Lattnera3fc41d2008-01-04 22:32:30 +000072 return true;
Chris Lattnerfffd6a72009-02-18 06:06:56 +000073
74 // Initialize the constant string interface lazily. This assumes
Steve Naroff54e59452009-04-07 14:18:33 +000075 // the NSString interface is seen in this translation unit. Note: We
76 // don't use NSConstantString, since the runtime team considers this
77 // interface private (even though it appears in the header files).
Chris Lattnerfffd6a72009-02-18 06:06:56 +000078 QualType Ty = Context.getObjCConstantStringInterface();
79 if (!Ty.isNull()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +000080 Ty = Context.getObjCObjectPointerType(Ty);
Fariborz Jahanian07317632010-04-23 23:19:04 +000081 } else if (getLangOptions().NoConstantCFStrings) {
Fariborz Jahanian50c925f2010-10-19 17:19:29 +000082 IdentifierInfo *NSIdent=0;
83 std::string StringClass(getLangOptions().ObjCConstantStringClass);
84
85 if (StringClass.empty())
86 NSIdent = &Context.Idents.get("NSConstantString");
87 else
88 NSIdent = &Context.Idents.get(StringClass);
89
Fariborz Jahanian07317632010-04-23 23:19:04 +000090 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
91 LookupOrdinaryName);
92 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
93 Context.setObjCConstantStringInterface(StrIF);
94 Ty = Context.getObjCConstantStringInterface();
95 Ty = Context.getObjCObjectPointerType(Ty);
96 } else {
97 // If there is no NSConstantString interface defined then treat this
98 // as error and recover from it.
99 Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent
100 << S->getSourceRange();
101 Ty = Context.getObjCIdType();
102 }
Chris Lattner091f6982008-06-21 21:44:18 +0000103 } else {
Steve Naroff54e59452009-04-07 14:18:33 +0000104 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000105 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
106 LookupOrdinaryName);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000107 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
108 Context.setObjCConstantStringInterface(StrIF);
109 Ty = Context.getObjCConstantStringInterface();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000110 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000111 } else {
Steve Naroff54e59452009-04-07 14:18:33 +0000112 // If there is no NSString interface defined then treat constant
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000113 // strings as untyped objects and let the runtime figure it out later.
114 Ty = Context.getObjCIdType();
115 }
Chris Lattner091f6982008-06-21 21:44:18 +0000116 }
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattnerd7670d92009-02-18 06:13:04 +0000118 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000119}
120
Mike Stump11289f42009-09-09 15:08:12 +0000121Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregorabd9e962010-04-20 15:39:42 +0000122 TypeSourceInfo *EncodedTypeInfo,
Anders Carlsson315d2292009-06-07 18:45:35 +0000123 SourceLocation RParenLoc) {
Douglas Gregorabd9e962010-04-20 15:39:42 +0000124 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlsson315d2292009-06-07 18:45:35 +0000125 QualType StrTy;
Mike Stump11289f42009-09-09 15:08:12 +0000126 if (EncodedType->isDependentType())
Anders Carlsson315d2292009-06-07 18:45:35 +0000127 StrTy = Context.DependentTy;
128 else {
129 std::string Str;
130 Context.getObjCEncodingForType(EncodedType, Str);
131
132 // The type of @encode is the same as the type of the corresponding string,
133 // which is an array type.
134 StrTy = Context.CharTy;
135 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCallc33dec32010-03-15 10:54:44 +0000136 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlsson315d2292009-06-07 18:45:35 +0000137 StrTy.addConst();
138 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
139 ArrayType::Normal, 0);
140 }
Mike Stump11289f42009-09-09 15:08:12 +0000141
Douglas Gregorabd9e962010-04-20 15:39:42 +0000142 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlsson315d2292009-06-07 18:45:35 +0000143}
144
John McCallfaf5fb42010-08-26 23:41:50 +0000145ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
146 SourceLocation EncodeLoc,
147 SourceLocation LParenLoc,
148 ParsedType ty,
149 SourceLocation RParenLoc) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +0000150 // FIXME: Preserve type source info ?
Douglas Gregorabd9e962010-04-20 15:39:42 +0000151 TypeSourceInfo *TInfo;
152 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
153 if (!TInfo)
154 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
155 PP.getLocForEndOfToken(LParenLoc));
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000156
Douglas Gregorabd9e962010-04-20 15:39:42 +0000157 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000158}
159
John McCallfaf5fb42010-08-26 23:41:50 +0000160ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
161 SourceLocation AtLoc,
162 SourceLocation SelLoc,
163 SourceLocation LParenLoc,
164 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000165 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +0000166 SourceRange(LParenLoc, RParenLoc), false, false);
Fariborz Jahanian0571d9b2009-06-16 16:25:00 +0000167 if (!Method)
168 Method = LookupFactoryMethodInGlobalPool(Sel,
169 SourceRange(LParenLoc, RParenLoc));
170 if (!Method)
171 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
172
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000173 llvm::DenseMap<Selector, SourceLocation>::iterator Pos
174 = ReferencedSelectors.find(Sel);
175 if (Pos == ReferencedSelectors.end())
176 ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
177
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000178 QualType Ty = Context.getObjCSelType();
Daniel Dunbar45858d22010-02-03 20:11:42 +0000179 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000180}
181
John McCallfaf5fb42010-08-26 23:41:50 +0000182ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
183 SourceLocation AtLoc,
184 SourceLocation ProtoLoc,
185 SourceLocation LParenLoc,
186 SourceLocation RParenLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000187 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000188 if (!PDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000189 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000190 return true;
191 }
Mike Stump11289f42009-09-09 15:08:12 +0000192
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000193 QualType Ty = Context.getObjCProtoType();
194 if (Ty.isNull())
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000195 return true;
Steve Naroff7cae42b2009-07-10 23:34:53 +0000196 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000197 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000198}
199
John McCall5f2d5562011-02-03 09:00:02 +0000200/// Try to capture an implicit reference to 'self'.
201ObjCMethodDecl *Sema::tryCaptureObjCSelf() {
202 // Ignore block scopes: we can capture through them.
203 DeclContext *DC = CurContext;
204 while (true) {
205 if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext();
206 else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext();
207 else break;
208 }
209
210 // If we're not in an ObjC method, error out. Note that, unlike the
211 // C++ case, we don't require an instance method --- class methods
212 // still have a 'self', and we really do still need to capture it!
213 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
214 if (!method)
215 return 0;
216
217 ImplicitParamDecl *self = method->getSelfDecl();
218 assert(self && "capturing 'self' in non-definition?");
219
220 // Mark that we're closing on 'this' in all the block scopes, if applicable.
221 for (unsigned idx = FunctionScopes.size() - 1;
222 isa<BlockScopeInfo>(FunctionScopes[idx]);
John McCall351762c2011-02-07 10:33:21 +0000223 --idx) {
224 BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]);
225 unsigned &captureIndex = blockScope->CaptureMap[self];
226 if (captureIndex) break;
227
228 bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]);
229 blockScope->Captures.push_back(
230 BlockDecl::Capture(self, /*byref*/ false, nested, /*copy*/ 0));
231 captureIndex = blockScope->Captures.size(); // +1
232 }
John McCall5f2d5562011-02-03 09:00:02 +0000233
234 return method;
235}
236
237
Mike Stump11289f42009-09-09 15:08:12 +0000238bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
239 Selector Sel, ObjCMethodDecl *Method,
Chris Lattnere4b95692008-11-24 03:33:13 +0000240 bool isClassMessage,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000241 SourceLocation lbrac, SourceLocation rbrac,
John McCall7decc9e2010-11-18 06:31:45 +0000242 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000243 if (!Method) {
Daniel Dunbar83876b42008-09-11 00:04:36 +0000244 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000245 for (unsigned i = 0; i != NumArgs; i++) {
246 if (Args[i]->isTypeDependent())
247 continue;
248
Daniel Dunbar83876b42008-09-11 00:04:36 +0000249 DefaultArgumentPromotion(Args[i]);
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000250 }
Daniel Dunbar83876b42008-09-11 00:04:36 +0000251
Chris Lattnere4b95692008-11-24 03:33:13 +0000252 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
253 diag::warn_inst_method_not_found;
254 Diag(lbrac, DiagID)
255 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000256 ReturnType = Context.getObjCIdType();
John McCall7decc9e2010-11-18 06:31:45 +0000257 VK = VK_RValue;
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000258 return false;
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000259 }
Mike Stump11289f42009-09-09 15:08:12 +0000260
Douglas Gregor603d81b2010-07-13 08:18:22 +0000261 ReturnType = Method->getSendResultType();
John McCall7decc9e2010-11-18 06:31:45 +0000262 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +0000263
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000264 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian60462092010-04-08 00:30:06 +0000265 // Method might have more arguments than selector indicates. This is due
266 // to addition of c-style arguments in method.
267 if (Method->param_size() > Sel.getNumArgs())
268 NumNamedArgs = Method->param_size();
269 // FIXME. This need be cleaned up.
270 if (NumArgs < NumNamedArgs) {
John McCall7decc9e2010-11-18 06:31:45 +0000271 Diag(lbrac, diag::err_typecheck_call_too_few_args)
272 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian60462092010-04-08 00:30:06 +0000273 return false;
274 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000275
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000276 bool IsError = false;
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000277 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000278 // We can't do any type-checking on a type-dependent argument.
279 if (Args[i]->isTypeDependent())
280 continue;
281
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000282 Expr *argExpr = Args[i];
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000283
Douglas Gregor6b7f12c2010-04-21 23:24:10 +0000284 ParmVarDecl *Param = Method->param_begin()[i];
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000285 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump11289f42009-09-09 15:08:12 +0000286
Douglas Gregor6b7f12c2010-04-21 23:24:10 +0000287 if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
288 Param->getType(),
289 PDiag(diag::err_call_incomplete_argument)
290 << argExpr->getSourceRange()))
291 return true;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000292
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +0000293 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
294 Param);
John McCallc3007a22010-10-26 07:05:15 +0000295 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor6b7f12c2010-04-21 23:24:10 +0000296 if (ArgE.isInvalid())
297 IsError = true;
298 else
299 Args[i] = ArgE.takeAs<Expr>();
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000300 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000301
302 // Promote additional arguments to variadic methods.
303 if (Method->isVariadic()) {
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000304 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
305 if (Args[i]->isTypeDependent())
306 continue;
307
Chris Lattnerbb53efb2010-05-16 04:01:30 +0000308 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000309 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000310 } else {
311 // Check for extra arguments to non-variadic methods.
312 if (NumArgs != NumNamedArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000313 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +0000314 diag::err_typecheck_call_too_many_args)
Eric Christopher2a5aaff2010-04-16 04:56:46 +0000315 << 2 /*method*/ << NumNamedArgs << NumArgs
316 << Method->getSourceRange()
Chris Lattner3b054132008-11-19 05:08:23 +0000317 << SourceRange(Args[NumNamedArgs]->getLocStart(),
318 Args[NumArgs-1]->getLocEnd());
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000319 }
320 }
321
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000322 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000323 return IsError;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000324}
325
Steve Naroff3f49fee2009-03-04 15:11:40 +0000326bool Sema::isSelfExpr(Expr *RExpr) {
Fariborz Jahanianb3b1e172011-03-27 19:53:47 +0000327 // 'self' is objc 'self' in an objc method only.
Fariborz Jahaniand0d31bf2011-03-28 16:23:34 +0000328 DeclContext *DC = CurContext;
329 while (isa<BlockDecl>(DC))
330 DC = DC->getParent();
331 if (DC && !isa<ObjCMethodDecl>(DC))
Fariborz Jahanianb3b1e172011-03-27 19:53:47 +0000332 return false;
John McCall34376a62010-12-04 03:47:34 +0000333 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
334 if (ICE->getCastKind() == CK_LValueToRValue)
335 RExpr = ICE->getSubExpr();
Steve Naroff3f49fee2009-03-04 15:11:40 +0000336 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
337 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
338 return true;
339 return false;
340}
341
Steve Naroffb162f172009-02-26 15:55:06 +0000342// Helper method for ActOnClassMethod/ActOnInstanceMethod.
343// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000344// If failed, then we search in class's root for an instance method.
Steve Naroffb162f172009-02-26 15:55:06 +0000345// Returns 0 if no method is found.
Steve Naroffed031702009-03-08 18:56:13 +0000346ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Naroffb162f172009-02-26 15:55:06 +0000347 ObjCInterfaceDecl *ClassDecl) {
348 ObjCMethodDecl *Method = 0;
Steve Naroffed031702009-03-08 18:56:13 +0000349 // lookup in class and all superclasses
350 while (ClassDecl && !Method) {
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000351 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000352 Method = ImpDecl->getClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000353
Steve Naroffed031702009-03-08 18:56:13 +0000354 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000355 if (!Method)
356 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000357
Steve Naroffed031702009-03-08 18:56:13 +0000358 // Before we give up, check if the selector is an instance method.
359 // But only in the root. This matches gcc's behaviour and what the
360 // runtime expects.
361 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000362 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000363 // Look through local category implementations associated
Steve Naroffed031702009-03-08 18:56:13 +0000364 // with the root class.
Mike Stump11289f42009-09-09 15:08:12 +0000365 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000366 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Steve Naroffed031702009-03-08 18:56:13 +0000369 ClassDecl = ClassDecl->getSuperClass();
Steve Naroffb162f172009-02-26 15:55:06 +0000370 }
Steve Naroffed031702009-03-08 18:56:13 +0000371 return Method;
372}
373
374ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
375 ObjCInterfaceDecl *ClassDecl) {
376 ObjCMethodDecl *Method = 0;
377 while (ClassDecl && !Method) {
378 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000379 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000380 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000381
Steve Naroffed031702009-03-08 18:56:13 +0000382 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000383 if (!Method)
384 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroffed031702009-03-08 18:56:13 +0000385 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000386 }
Steve Naroffb162f172009-02-26 15:55:06 +0000387 return Method;
388}
389
Fariborz Jahanian3dc11ad2011-03-09 20:18:06 +0000390/// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
391/// list of a qualified objective pointer type.
392ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
393 const ObjCObjectPointerType *OPT,
394 bool Instance)
395{
396 ObjCMethodDecl *MD = 0;
397 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
398 E = OPT->qual_end(); I != E; ++I) {
399 ObjCProtocolDecl *PROTO = (*I);
400 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
401 return MD;
402 }
403 }
404 return 0;
405}
406
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000407/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
408/// objective C interface. This is a property reference expression.
John McCalldadc5752010-08-24 06:29:42 +0000409ExprResult Sema::
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000410HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattner90c58fa2010-04-11 07:51:10 +0000411 Expr *BaseExpr, DeclarationName MemberName,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000412 SourceLocation MemberLoc,
413 SourceLocation SuperLoc, QualType SuperType,
414 bool Super) {
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000415 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
416 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
417 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
418
Fariborz Jahanian7cabbe02010-12-16 00:56:28 +0000419 if (IFace->isForwardDecl()) {
420 Diag(MemberLoc, diag::err_property_not_found_forward_class)
421 << MemberName << QualType(OPT, 0);
422 Diag(IFace->getLocation(), diag::note_forward_class);
423 return ExprError();
424 }
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000425 // Search for a declared property first.
426 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
427 // Check whether we can reference this property.
428 if (DiagnoseUseOfDecl(PD, MemberLoc))
429 return ExprError();
430 QualType ResTy = PD->getType();
431 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
432 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
433 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
John McCall7decc9e2010-11-18 06:31:45 +0000434 ResTy = Getter->getResultType();
435
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000436 if (Super)
437 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCall7decc9e2010-11-18 06:31:45 +0000438 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000439 MemberLoc,
440 SuperLoc, SuperType));
441 else
442 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCall7decc9e2010-11-18 06:31:45 +0000443 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000444 MemberLoc, BaseExpr));
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000445 }
446 // Check protocols on qualified interfaces.
447 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
448 E = OPT->qual_end(); I != E; ++I)
449 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
450 // Check whether we can reference this property.
451 if (DiagnoseUseOfDecl(PD, MemberLoc))
452 return ExprError();
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000453 if (Super)
John McCall7decc9e2010-11-18 06:31:45 +0000454 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
455 VK_LValue,
456 OK_ObjCProperty,
457 MemberLoc,
458 SuperLoc, SuperType));
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000459 else
460 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000461 VK_LValue,
462 OK_ObjCProperty,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000463 MemberLoc,
464 BaseExpr));
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000465 }
466 // If that failed, look for an "implicit" property by seeing if the nullary
467 // selector is implemented.
468
469 // FIXME: The logic for looking up nullary and unary selectors should be
470 // shared with the code in ActOnInstanceMessage.
471
472 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
473 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
Fariborz Jahanianb296e332011-03-09 22:17:12 +0000474
475 // May be founf in property's qualified list.
476 if (!Getter)
477 Getter = LookupMethodInQualifiedType(Sel, OPT, true);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000478
479 // If this reference is in an @implementation, check for 'private' methods.
480 if (!Getter)
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000481 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000482
483 // Look through local category implementations associated with the class.
484 if (!Getter)
485 Getter = IFace->getCategoryInstanceMethod(Sel);
486 if (Getter) {
487 // Check if we can reference this property.
488 if (DiagnoseUseOfDecl(Getter, MemberLoc))
489 return ExprError();
490 }
491 // If we found a getter then this may be a valid dot-reference, we
492 // will look for the matching setter, in case it is needed.
493 Selector SetterSel =
494 SelectorTable::constructSetterName(PP.getIdentifierTable(),
495 PP.getSelectorTable(), Member);
496 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
Fariborz Jahanianb296e332011-03-09 22:17:12 +0000497
498 // May be founf in property's qualified list.
499 if (!Setter)
500 Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
501
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000502 if (!Setter) {
503 // If this reference is in an @implementation, also check for 'private'
504 // methods.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000505 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000506 }
507 // Look through local category implementations associated with the class.
508 if (!Setter)
509 Setter = IFace->getCategoryInstanceMethod(SetterSel);
Fariborz Jahanianb296e332011-03-09 22:17:12 +0000510
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000511 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
512 return ExprError();
513
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000514 if (Getter || Setter) {
515 QualType PType;
516 if (Getter)
517 PType = Getter->getSendResultType();
518 else {
519 ParmVarDecl *ArgDecl = *Setter->param_begin();
520 PType = ArgDecl->getType();
521 }
522
John McCall4bc41ae2010-11-18 19:01:18 +0000523 ExprValueKind VK = VK_LValue;
524 ExprObjectKind OK = OK_ObjCProperty;
525 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
526 PType->isVoidType())
527 VK = VK_RValue, OK = OK_Ordinary;
528
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000529 if (Super)
John McCallb7bd14f2010-12-02 01:19:52 +0000530 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
531 PType, VK, OK,
532 MemberLoc,
533 SuperLoc, SuperType));
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000534 else
John McCallb7bd14f2010-12-02 01:19:52 +0000535 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
536 PType, VK, OK,
537 MemberLoc, BaseExpr));
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000538
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000539 }
540
541 // Attempt to correct for typos in property names.
542 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000543 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000544 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattner90c58fa2010-04-11 07:51:10 +0000545 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000546 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattner90c58fa2010-04-11 07:51:10 +0000547 << MemberName << QualType(OPT, 0) << TypoResult
548 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000549 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
550 Diag(Property->getLocation(), diag::note_previous_decl)
551 << Property->getDeclName();
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000552 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
553 SuperLoc, SuperType, Super);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000554 }
Fariborz Jahanian05d389f2011-02-17 01:26:14 +0000555 ObjCInterfaceDecl *ClassDeclared;
556 if (ObjCIvarDecl *Ivar =
557 IFace->lookupInstanceVariable(Member, ClassDeclared)) {
558 QualType T = Ivar->getType();
559 if (const ObjCObjectPointerType * OBJPT =
560 T->getAsObjCInterfacePointerType()) {
561 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
562 if (ObjCInterfaceDecl *IFace = IFaceT->getDecl())
563 if (IFace->isForwardDecl()) {
564 Diag(MemberLoc, diag::err_property_not_as_forward_class)
Fariborz Jahanian5fc74802011-02-17 17:30:05 +0000565 << MemberName << IFace;
Fariborz Jahanian05d389f2011-02-17 01:26:14 +0000566 Diag(IFace->getLocation(), diag::note_forward_class);
567 return ExprError();
568 }
569 }
570 }
Chris Lattner90c58fa2010-04-11 07:51:10 +0000571
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000572 Diag(MemberLoc, diag::err_property_not_found)
573 << MemberName << QualType(OPT, 0);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000574 if (Setter)
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000575 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000576 << MemberName << BaseExpr->getSourceRange();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000577 return ExprError();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000578}
579
580
581
John McCalldadc5752010-08-24 06:29:42 +0000582ExprResult Sema::
Chris Lattnera36ec422010-04-11 08:28:14 +0000583ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
584 IdentifierInfo &propertyName,
585 SourceLocation receiverNameLoc,
586 SourceLocation propertyNameLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000588 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000589 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
590 receiverNameLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +0000591 if (IFace == 0) {
592 // If the "receiver" is 'super' in a method, handle it as an expression-like
593 // property reference.
John McCall5f2d5562011-02-03 09:00:02 +0000594 if (receiverNamePtr->isStr("super")) {
595 if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000596 if (CurMethod->isInstanceMethod()) {
597 QualType T =
598 Context.getObjCInterfaceType(CurMethod->getClassInterface());
599 T = Context.getObjCObjectPointerType(T);
Chris Lattnera36ec422010-04-11 08:28:14 +0000600
601 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000602 /*BaseExpr*/0, &propertyName,
603 propertyNameLoc,
604 receiverNameLoc, T, true);
Chris Lattnera36ec422010-04-11 08:28:14 +0000605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattnera36ec422010-04-11 08:28:14 +0000607 // Otherwise, if this is a class method, try dispatching to our
608 // superclass.
609 IFace = CurMethod->getClassInterface()->getSuperClass();
610 }
John McCall5f2d5562011-02-03 09:00:02 +0000611 }
Chris Lattnera36ec422010-04-11 08:28:14 +0000612
613 if (IFace == 0) {
614 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
615 return ExprError();
616 }
617 }
618
619 // Search for a declared property first.
Steve Naroff9527bbf2009-03-09 21:12:44 +0000620 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000621 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000622
623 // If this reference is in an @implementation, check for 'private' methods.
624 if (!Getter)
625 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
626 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000627 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000628 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000629
630 if (Getter) {
631 // FIXME: refactor/share with ActOnMemberReference().
632 // Check if we can reference this property.
633 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
634 return ExprError();
635 }
Mike Stump11289f42009-09-09 15:08:12 +0000636
Steve Naroff9527bbf2009-03-09 21:12:44 +0000637 // Look for the matching setter, in case it is needed.
Mike Stump11289f42009-09-09 15:08:12 +0000638 Selector SetterSel =
639 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Naroffc7597f82009-03-10 17:24:38 +0000640 PP.getSelectorTable(), &propertyName);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000642 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000643 if (!Setter) {
644 // If this reference is in an @implementation, also check for 'private'
645 // methods.
646 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
647 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000648 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000649 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000650 }
651 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000652 if (!Setter)
653 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000654
655 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
656 return ExprError();
657
658 if (Getter || Setter) {
659 QualType PType;
660
John McCall4bc41ae2010-11-18 19:01:18 +0000661 ExprValueKind VK = VK_LValue;
662 if (Getter) {
Douglas Gregor603d81b2010-07-13 08:18:22 +0000663 PType = Getter->getSendResultType();
John McCall4bc41ae2010-11-18 19:01:18 +0000664 if (!getLangOptions().CPlusPlus &&
665 !PType.hasQualifiers() && PType->isVoidType())
666 VK = VK_RValue;
667 } else {
Steve Naroff9527bbf2009-03-09 21:12:44 +0000668 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
669 E = Setter->param_end(); PI != E; ++PI)
670 PType = (*PI)->getType();
John McCall4bc41ae2010-11-18 19:01:18 +0000671 VK = VK_LValue;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000672 }
John McCall4bc41ae2010-11-18 19:01:18 +0000673
674 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
675
John McCallb7bd14f2010-12-02 01:19:52 +0000676 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
677 PType, VK, OK,
678 propertyNameLoc,
679 receiverNameLoc, IFace));
Steve Naroff9527bbf2009-03-09 21:12:44 +0000680 }
681 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
682 << &propertyName << Context.getObjCInterfaceType(IFace));
683}
684
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000685Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregore5798dc2010-04-21 20:38:13 +0000686 IdentifierInfo *Name,
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000687 SourceLocation NameLoc,
688 bool IsSuper,
Douglas Gregore5798dc2010-04-21 20:38:13 +0000689 bool HasTrailingDot,
John McCallba7bf592010-08-24 05:47:05 +0000690 ParsedType &ReceiverType) {
691 ReceiverType = ParsedType();
Douglas Gregore5798dc2010-04-21 20:38:13 +0000692
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000693 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor57756ea2010-10-14 22:11:03 +0000694 // messaging super. If the identifier is "super" and there is a
695 // trailing dot, it's an instance message.
696 if (IsSuper && S->isInObjcMethodScope())
697 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000698
699 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
700 LookupName(Result, S);
701
702 switch (Result.getResultKind()) {
703 case LookupResult::NotFound:
Douglas Gregorca7136b2010-04-19 20:09:36 +0000704 // Normal name lookup didn't find anything. If we're in an
705 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor57756ea2010-10-14 22:11:03 +0000706 // FIXME: This is a hack. Ivar lookup should be part of normal
707 // lookup.
Douglas Gregorca7136b2010-04-19 20:09:36 +0000708 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
709 ObjCInterfaceDecl *ClassDeclared;
710 if (Method->getClassInterface()->lookupInstanceVariable(Name,
711 ClassDeclared))
712 return ObjCInstanceMessage;
713 }
Douglas Gregor57756ea2010-10-14 22:11:03 +0000714
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000715 // Break out; we'll perform typo correction below.
716 break;
717
718 case LookupResult::NotFoundInCurrentInstantiation:
719 case LookupResult::FoundOverloaded:
720 case LookupResult::FoundUnresolvedValue:
721 case LookupResult::Ambiguous:
722 Result.suppressDiagnostics();
723 return ObjCInstanceMessage;
724
725 case LookupResult::Found: {
Fariborz Jahanian14889fc2011-02-08 00:23:07 +0000726 // If the identifier is a class or not, and there is a trailing dot,
727 // it's an instance message.
728 if (HasTrailingDot)
729 return ObjCInstanceMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000730 // We found something. If it's a type, then we have a class
731 // message. Otherwise, it's an instance message.
732 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregore5798dc2010-04-21 20:38:13 +0000733 QualType T;
734 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
735 T = Context.getObjCInterfaceType(Class);
736 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
737 T = Context.getTypeDeclType(Type);
738 else
739 return ObjCInstanceMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000740
Douglas Gregore5798dc2010-04-21 20:38:13 +0000741 // We have a class message, and T is the type we're
742 // messaging. Build source-location information for it.
743 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallba7bf592010-08-24 05:47:05 +0000744 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000745 return ObjCClassMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000746 }
747 }
748
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000749 // Determine our typo-correction context.
750 CorrectTypoContext CTC = CTC_Expression;
751 if (ObjCMethodDecl *Method = getCurMethodDecl())
752 if (Method->getClassInterface() &&
753 Method->getClassInterface()->getSuperClass())
754 CTC = CTC_ObjCMessageReceiver;
755
756 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
757 if (Result.isSingleResult()) {
758 // If we found a declaration, correct when it refers to an Objective-C
759 // class.
760 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregore5798dc2010-04-21 20:38:13 +0000761 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000762 Diag(NameLoc, diag::err_unknown_receiver_suggest)
763 << Name << Result.getLookupName()
764 << FixItHint::CreateReplacement(SourceRange(NameLoc),
765 ND->getNameAsString());
766 Diag(ND->getLocation(), diag::note_previous_decl)
767 << Corrected;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000768
Douglas Gregore5798dc2010-04-21 20:38:13 +0000769 QualType T = Context.getObjCInterfaceType(Class);
770 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallba7bf592010-08-24 05:47:05 +0000771 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000772 return ObjCClassMessage;
773 }
774 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
775 Corrected.getAsIdentifierInfo()->isStr("super")) {
776 // If we've found the keyword "super", this is a send to super.
777 Diag(NameLoc, diag::err_unknown_receiver_suggest)
778 << Name << Corrected
779 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000780 return ObjCSuperMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000781 }
782 }
783
784 // Fall back: let the parser try to parse it as an instance message.
785 return ObjCInstanceMessage;
786}
Steve Naroff9527bbf2009-03-09 21:12:44 +0000787
John McCalldadc5752010-08-24 06:29:42 +0000788ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000789 SourceLocation SuperLoc,
790 Selector Sel,
791 SourceLocation LBracLoc,
792 SourceLocation SelectorLoc,
793 SourceLocation RBracLoc,
794 MultiExprArg Args) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000795 // Determine whether we are inside a method or not.
John McCall5f2d5562011-02-03 09:00:02 +0000796 ObjCMethodDecl *Method = tryCaptureObjCSelf();
Douglas Gregor4fdba132010-04-21 20:01:04 +0000797 if (!Method) {
798 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
799 return ExprError();
800 }
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000801
Douglas Gregor4fdba132010-04-21 20:01:04 +0000802 ObjCInterfaceDecl *Class = Method->getClassInterface();
803 if (!Class) {
804 Diag(SuperLoc, diag::error_no_super_class_message)
805 << Method->getDeclName();
806 return ExprError();
807 }
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000808
Douglas Gregor4fdba132010-04-21 20:01:04 +0000809 ObjCInterfaceDecl *Super = Class->getSuperClass();
810 if (!Super) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000811 // The current class does not have a superclass.
Ted Kremenek499897b2011-01-23 17:21:34 +0000812 Diag(SuperLoc, diag::error_root_class_cannot_use_super)
813 << Class->getIdentifier();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000814 return ExprError();
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000815 }
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000816
Douglas Gregor4fdba132010-04-21 20:01:04 +0000817 // We are in a method whose class has a superclass, so 'super'
818 // is acting as a keyword.
819 if (Method->isInstanceMethod()) {
820 // Since we are in an instance method, this is an instance
821 // message to the superclass instance.
822 QualType SuperTy = Context.getObjCInterfaceType(Super);
823 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCallb268a282010-08-23 23:25:46 +0000824 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000825 Sel, /*Method=*/0,
826 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000827 }
Douglas Gregor4fdba132010-04-21 20:01:04 +0000828
829 // Since we are in a class method, this is a class message to
830 // the superclass.
831 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
832 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000833 SuperLoc, Sel, /*Method=*/0,
834 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000835}
836
837/// \brief Build an Objective-C class message expression.
838///
839/// This routine takes care of both normal class messages and
840/// class messages to the superclass.
841///
842/// \param ReceiverTypeInfo Type source information that describes the
843/// receiver of this message. This may be NULL, in which case we are
844/// sending to the superclass and \p SuperLoc must be a valid source
845/// location.
846
847/// \param ReceiverType The type of the object receiving the
848/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
849/// type as that refers to. For a superclass send, this is the type of
850/// the superclass.
851///
852/// \param SuperLoc The location of the "super" keyword in a
853/// superclass message.
854///
855/// \param Sel The selector to which the message is being sent.
856///
Douglas Gregorb5186b12010-04-22 17:01:48 +0000857/// \param Method The method that this class message is invoking, if
858/// already known.
859///
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000860/// \param LBracLoc The location of the opening square bracket ']'.
861///
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000862/// \param RBrac The location of the closing square bracket ']'.
863///
864/// \param Args The message arguments.
John McCalldadc5752010-08-24 06:29:42 +0000865ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000866 QualType ReceiverType,
867 SourceLocation SuperLoc,
868 Selector Sel,
869 ObjCMethodDecl *Method,
870 SourceLocation LBracLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000871 SourceLocation SelectorLoc,
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000872 SourceLocation RBracLoc,
873 MultiExprArg ArgsIn) {
874 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000875 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000876 if (LBracLoc.isInvalid()) {
877 Diag(Loc, diag::err_missing_open_square_message_send)
878 << FixItHint::CreateInsertion(Loc, "[");
879 LBracLoc = Loc;
880 }
881
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000882 if (ReceiverType->isDependentType()) {
883 // If the receiver type is dependent, we can't type-check anything
884 // at this point. Build a dependent expression.
885 unsigned NumArgs = ArgsIn.size();
886 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
887 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCall7decc9e2010-11-18 06:31:45 +0000888 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
889 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000890 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000891 Args, NumArgs, RBracLoc));
892 }
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000893
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000894 // Find the class to which we are sending this message.
895 ObjCInterfaceDecl *Class = 0;
John McCall8b07ec22010-05-15 11:32:37 +0000896 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
897 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000898 Diag(Loc, diag::err_invalid_receiver_class_message)
899 << ReceiverType;
900 return ExprError();
Steve Naroffe2177fb2008-07-25 19:39:00 +0000901 }
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000902 assert(Class && "We don't know which class we're messaging?");
Fariborz Jahanian08891f52011-03-08 19:12:46 +0000903 (void)DiagnoseUseOfDecl(Class, Loc);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000904 // Find the method we are messaging.
Douglas Gregorb5186b12010-04-22 17:01:48 +0000905 if (!Method) {
906 if (Class->isForwardDecl()) {
907 // A forward class used in messaging is treated as a 'Class'
908 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
909 Method = LookupFactoryMethodInGlobalPool(Sel,
910 SourceRange(LBracLoc, RBracLoc));
911 if (Method)
912 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
913 << Method->getDeclName();
914 }
915 if (!Method)
916 Method = Class->lookupClassMethod(Sel);
917
918 // If we have an implementation in scope, check "private" methods.
919 if (!Method)
920 Method = LookupPrivateClassMethod(Sel, Class);
921
922 if (Method && DiagnoseUseOfDecl(Method, Loc))
923 return ExprError();
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000926 // Check the argument types and determine the result type.
927 QualType ReturnType;
John McCall7decc9e2010-11-18 06:31:45 +0000928 ExprValueKind VK = VK_RValue;
929
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000930 unsigned NumArgs = ArgsIn.size();
931 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
932 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCall7decc9e2010-11-18 06:31:45 +0000933 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000934 return ExprError();
Ted Kremeneka3a37ae2008-06-24 15:50:53 +0000935
Douglas Gregoraec93c62011-01-11 03:23:19 +0000936 if (Method && !Method->getResultType()->isVoidType() &&
937 RequireCompleteType(LBracLoc, Method->getResultType(),
938 diag::err_illegal_message_expr_incomplete_type))
939 return ExprError();
940
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000941 // Construct the appropriate ObjCMessageExpr.
Douglas Gregoraae38d62010-05-22 05:17:18 +0000942 Expr *Result;
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000943 if (SuperLoc.isValid())
John McCall7decc9e2010-11-18 06:31:45 +0000944 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregoraae38d62010-05-22 05:17:18 +0000945 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000946 ReceiverType, Sel, SelectorLoc,
947 Method, Args, NumArgs, RBracLoc);
Douglas Gregoraae38d62010-05-22 05:17:18 +0000948 else
John McCall7decc9e2010-11-18 06:31:45 +0000949 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000950 ReceiverTypeInfo, Sel, SelectorLoc,
951 Method, Args, NumArgs, RBracLoc);
Douglas Gregoraae38d62010-05-22 05:17:18 +0000952 return MaybeBindToTemporary(Result);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000953}
954
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000955// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000956// ArgExprs is optional - if it is present, the number of expressions
957// is obtained from Sel.getNumArgs().
John McCalldadc5752010-08-24 06:29:42 +0000958ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor3e972002010-09-15 23:19:31 +0000959 ParsedType Receiver,
960 Selector Sel,
961 SourceLocation LBracLoc,
962 SourceLocation SelectorLoc,
963 SourceLocation RBracLoc,
964 MultiExprArg Args) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000965 TypeSourceInfo *ReceiverTypeInfo;
966 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
967 if (ReceiverType.isNull())
968 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000969
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000971 if (!ReceiverTypeInfo)
972 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
973
974 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorb5186b12010-04-22 17:01:48 +0000975 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000976 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000977}
978
979/// \brief Build an Objective-C instance message expression.
980///
981/// This routine takes care of both normal instance messages and
982/// instance messages to the superclass instance.
983///
984/// \param Receiver The expression that computes the object that will
985/// receive this message. This may be empty, in which case we are
986/// sending to the superclass instance and \p SuperLoc must be a valid
987/// source location.
988///
989/// \param ReceiverType The (static) type of the object receiving the
990/// message. When a \p Receiver expression is provided, this is the
991/// same type as that expression. For a superclass instance send, this
992/// is a pointer to the type of the superclass.
993///
994/// \param SuperLoc The location of the "super" keyword in a
995/// superclass instance message.
996///
997/// \param Sel The selector to which the message is being sent.
998///
Douglas Gregorb5186b12010-04-22 17:01:48 +0000999/// \param Method The method that this instance message is invoking, if
1000/// already known.
1001///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001002/// \param LBracLoc The location of the opening square bracket ']'.
1003///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001004/// \param RBrac The location of the closing square bracket ']'.
1005///
1006/// \param Args The message arguments.
John McCalldadc5752010-08-24 06:29:42 +00001007ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001008 QualType ReceiverType,
1009 SourceLocation SuperLoc,
1010 Selector Sel,
1011 ObjCMethodDecl *Method,
1012 SourceLocation LBracLoc,
1013 SourceLocation SelectorLoc,
1014 SourceLocation RBracLoc,
1015 MultiExprArg ArgsIn) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001016 // The location of the receiver.
1017 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
1018
1019 if (LBracLoc.isInvalid()) {
1020 Diag(Loc, diag::err_missing_open_square_message_send)
1021 << FixItHint::CreateInsertion(Loc, "[");
1022 LBracLoc = Loc;
1023 }
1024
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001025 // If we have a receiver expression, perform appropriate promotions
1026 // and determine receiver type.
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001027 if (Receiver) {
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001028 if (Receiver->isTypeDependent()) {
1029 // If the receiver is type-dependent, we can't type-check anything
1030 // at this point. Build a dependent expression.
1031 unsigned NumArgs = ArgsIn.size();
1032 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1033 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
1034 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCall7decc9e2010-11-18 06:31:45 +00001035 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001036 SelectorLoc, /*Method=*/0,
1037 Args, NumArgs, RBracLoc));
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001038 }
1039
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001040 // If necessary, apply function/array conversion to the receiver.
1041 // C99 6.7.5.3p[7,8].
1042 DefaultFunctionArrayLvalueConversion(Receiver);
1043 ReceiverType = Receiver->getType();
1044 }
1045
Douglas Gregorb5186b12010-04-22 17:01:48 +00001046 if (!Method) {
1047 // Handle messages to id.
Fariborz Jahanian32e59ba2010-08-10 18:10:50 +00001048 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001049 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorb5186b12010-04-22 17:01:48 +00001050 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
1051 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001052 SourceRange(LBracLoc, RBracLoc),
1053 receiverIsId);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001054 if (!Method)
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001055 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001056 SourceRange(LBracLoc, RBracLoc),
1057 receiverIsId);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001058 } else if (ReceiverType->isObjCClassType() ||
1059 ReceiverType->isObjCQualifiedClassType()) {
1060 // Handle messages to Class.
1061 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1062 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1063 // First check the public methods in the class interface.
1064 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +00001065
Douglas Gregorb5186b12010-04-22 17:01:48 +00001066 if (!Method)
1067 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Douglas Gregor9a129192010-04-21 00:45:42 +00001068
Douglas Gregorb5186b12010-04-22 17:01:48 +00001069 // FIXME: if we still haven't found a method, we need to look in
1070 // protocols (if we have qualifiers).
Steve Naroff3f49fee2009-03-04 15:11:40 +00001071 }
Douglas Gregorb5186b12010-04-22 17:01:48 +00001072 if (Method && DiagnoseUseOfDecl(Method, Loc))
1073 return ExprError();
Fariborz Jahanian9a8e7592009-03-03 22:19:15 +00001074 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001075 if (!Method) {
Douglas Gregorb5186b12010-04-22 17:01:48 +00001076 // If not messaging 'self', look for any factory method named 'Sel'.
1077 if (!Receiver || !isSelfExpr(Receiver)) {
1078 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001079 SourceRange(LBracLoc, RBracLoc),
1080 true);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001081 if (!Method) {
1082 // If no class (factory) method was found, check if an _instance_
1083 // method of the same name exists in the root class only.
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001084 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001085 SourceRange(LBracLoc, RBracLoc),
1086 true);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001087 if (Method)
1088 if (const ObjCInterfaceDecl *ID =
1089 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1090 if (ID->getSuperClass())
1091 Diag(Loc, diag::warn_root_inst_method_not_found)
1092 << Sel << SourceRange(LBracLoc, RBracLoc);
1093 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001094 }
1095 }
1096 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001097 } else {
Douglas Gregorb5186b12010-04-22 17:01:48 +00001098 ObjCInterfaceDecl* ClassDecl = 0;
1099
1100 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1101 // long as one of the protocols implements the selector (if not, warn).
1102 if (const ObjCObjectPointerType *QIdTy
1103 = ReceiverType->getAsObjCQualifiedIdType()) {
1104 // Search protocols for instance methods.
Fariborz Jahanianb296e332011-03-09 22:17:12 +00001105 Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
1106 if (!Method)
1107 Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001108 } else if (const ObjCObjectPointerType *OCIType
1109 = ReceiverType->getAsObjCInterfacePointerType()) {
1110 // We allow sending a message to a pointer to an interface (an object).
1111 ClassDecl = OCIType->getInterfaceDecl();
1112 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1113 // faster than the following method (which can do *many* linear searches).
Sebastian Redl75d8a322010-08-02 23:18:59 +00001114 // The idea is to add class info to MethodPool.
Douglas Gregorb5186b12010-04-22 17:01:48 +00001115 Method = ClassDecl->lookupInstanceMethod(Sel);
1116
Fariborz Jahanianb296e332011-03-09 22:17:12 +00001117 if (!Method)
Douglas Gregorb5186b12010-04-22 17:01:48 +00001118 // Search protocol qualifiers.
Fariborz Jahanianb296e332011-03-09 22:17:12 +00001119 Method = LookupMethodInQualifiedType(Sel, OCIType, true);
1120
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00001121 bool forwardClass = false;
Douglas Gregorb5186b12010-04-22 17:01:48 +00001122 if (!Method) {
1123 // If we have implementations in scope, check "private" methods.
1124 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1125
1126 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1127 // If we still haven't found a method, look in the global pool. This
1128 // behavior isn't very desirable, however we need it for GCC
1129 // compatibility. FIXME: should we deviate??
1130 if (OCIType->qual_empty()) {
1131 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00001132 SourceRange(LBracLoc, RBracLoc));
1133 forwardClass = OCIType->getInterfaceDecl()->isForwardDecl();
1134 if (Method && !forwardClass)
Douglas Gregorb5186b12010-04-22 17:01:48 +00001135 Diag(Loc, diag::warn_maynot_respond)
1136 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1137 }
1138 }
1139 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00001140 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorb5186b12010-04-22 17:01:48 +00001141 return ExprError();
1142 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00001143 (ReceiverType->isPointerType() ||
1144 ReceiverType->isIntegerType())) {
Douglas Gregorb5186b12010-04-22 17:01:48 +00001145 // Implicitly convert integers and pointers to 'id' but emit a warning.
1146 Diag(Loc, diag::warn_bad_receiver_type)
1147 << ReceiverType
1148 << Receiver->getSourceRange();
1149 if (ReceiverType->isPointerType())
1150 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCalle3027922010-08-25 11:45:40 +00001151 CK_BitCast);
John McCalle84af4e2010-11-13 01:35:44 +00001152 else {
1153 // TODO: specialized warning on null receivers?
1154 bool IsNull = Receiver->isNullPointerConstant(Context,
1155 Expr::NPC_ValueDependentIsNull);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001156 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCalle84af4e2010-11-13 01:35:44 +00001157 IsNull ? CK_NullToPointer : CK_IntegralToPointer);
1158 }
Douglas Gregorb5186b12010-04-22 17:01:48 +00001159 ReceiverType = Receiver->getType();
Fariborz Jahaniancac49a82010-05-12 23:29:11 +00001160 }
Fariborz Jahanian1bd96d12010-05-13 17:19:25 +00001161 else if (getLangOptions().CPlusPlus &&
1162 !PerformContextuallyConvertToObjCId(Receiver)) {
1163 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1164 Receiver = ICE->getSubExpr();
1165 ReceiverType = Receiver->getType();
1166 }
John McCallb268a282010-08-23 23:25:46 +00001167 return BuildInstanceMessage(Receiver,
Fariborz Jahaniancac49a82010-05-12 23:29:11 +00001168 ReceiverType,
1169 SuperLoc,
1170 Sel,
1171 Method,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001172 LBracLoc,
1173 SelectorLoc,
Fariborz Jahaniancac49a82010-05-12 23:29:11 +00001174 RBracLoc,
1175 move(ArgsIn));
Douglas Gregorb5186b12010-04-22 17:01:48 +00001176 } else {
1177 // Reject other random receiver types (e.g. structs).
1178 Diag(Loc, diag::err_bad_receiver_type)
1179 << ReceiverType << Receiver->getSourceRange();
1180 return ExprError();
1181 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001182 }
Chris Lattner6b946cc2008-07-21 05:57:44 +00001183 }
Mike Stump11289f42009-09-09 15:08:12 +00001184
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001185 // Check the message arguments.
1186 unsigned NumArgs = ArgsIn.size();
1187 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1188 QualType ReturnType;
John McCall7decc9e2010-11-18 06:31:45 +00001189 ExprValueKind VK = VK_RValue;
Fariborz Jahanian68500912010-12-01 01:07:24 +00001190 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1191 ReceiverType->isObjCQualifiedClassType());
1192 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCall7decc9e2010-11-18 06:31:45 +00001193 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001194 return ExprError();
Fariborz Jahanian18e02752010-06-16 19:56:08 +00001195
Douglas Gregoraec93c62011-01-11 03:23:19 +00001196 if (Method && !Method->getResultType()->isVoidType() &&
1197 RequireCompleteType(LBracLoc, Method->getResultType(),
1198 diag::err_illegal_message_expr_incomplete_type))
1199 return ExprError();
Douglas Gregor9a129192010-04-21 00:45:42 +00001200
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001201 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregoraae38d62010-05-22 05:17:18 +00001202 Expr *Result;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001203 if (SuperLoc.isValid())
John McCall7decc9e2010-11-18 06:31:45 +00001204 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregoraae38d62010-05-22 05:17:18 +00001205 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001206 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregoraae38d62010-05-22 05:17:18 +00001207 Args, NumArgs, RBracLoc);
1208 else
John McCall7decc9e2010-11-18 06:31:45 +00001209 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001210 Receiver, Sel, SelectorLoc, Method,
1211 Args, NumArgs, RBracLoc);
Douglas Gregoraae38d62010-05-22 05:17:18 +00001212 return MaybeBindToTemporary(Result);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001213}
1214
1215// ActOnInstanceMessage - used for both unary and keyword messages.
1216// ArgExprs is optional - if it is present, the number of expressions
1217// is obtained from Sel.getNumArgs().
John McCalldadc5752010-08-24 06:29:42 +00001218ExprResult Sema::ActOnInstanceMessage(Scope *S,
1219 Expr *Receiver,
1220 Selector Sel,
1221 SourceLocation LBracLoc,
1222 SourceLocation SelectorLoc,
1223 SourceLocation RBracLoc,
1224 MultiExprArg Args) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001225 if (!Receiver)
1226 return ExprError();
1227
John McCallb268a282010-08-23 23:25:46 +00001228 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorb5186b12010-04-22 17:01:48 +00001229 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001230 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattnera3fc41d2008-01-04 22:32:30 +00001231}
Chris Lattner2a3569b2008-04-07 05:30:13 +00001232