blob: 3444cb5a494506368644cee83f3d1af92d4daac3 [file] [log] [blame]
Chris Lattner85a932e2008-01-04 22:32:30 +00001//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective-C expressions.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000016#include "clang/Sema/Scope.h"
John McCall26743b22011-02-03 09:00:02 +000017#include "clang/Sema/ScopeInfo.h"
Douglas Gregore737f502010-08-12 20:07:10 +000018#include "clang/Sema/Initialization.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/DeclObjC.h"
Steve Narofff494b572008-05-29 21:12:08 +000021#include "clang/AST/ExprObjC.h"
Douglas Gregor2725ca82010-04-21 19:57:20 +000022#include "clang/AST/TypeLoc.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000023#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000024#include "clang/Lex/Preprocessor.h"
25
Chris Lattner85a932e2008-01-04 22:32:30 +000026using namespace clang;
John McCall26743b22011-02-03 09:00:02 +000027using namespace sema;
Chris Lattner85a932e2008-01-04 22:32:30 +000028
John McCallf312b1e2010-08-26 23:41:50 +000029ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
30 Expr **strings,
31 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000032 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
33
Chris Lattnerf4b136f2009-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 Lattner39c28bb2009-02-18 06:48:40 +000038 StringLiteral *S = Strings[0];
Mike Stump1eb44332009-09-09 15:08:12 +000039
Chris Lattnerf4b136f2009-02-18 06:13:04 +000040 // If we have a multi-part string, merge it all together.
41 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000042 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000043 llvm::SmallString<128> StrBuf;
44 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Chris Lattner726e1682009-02-18 05:49:11 +000046 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000047 S = Strings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner39c28bb2009-02-18 06:48:40 +000049 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-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 Stump1eb44332009-09-09 15:08:12 +000055
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +000056 // Append the string.
57 StrBuf += S->getString();
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner39c28bb2009-02-18 06:48:40 +000059 // Get the locations of the string tokens.
60 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Chris Lattner85a932e2008-01-04 22:32:30 +000061 }
Mike Stump1eb44332009-09-09 15:08:12 +000062
Chris Lattner39c28bb2009-02-18 06:48:40 +000063 // Create the aggregate string with the appropriate content and location
64 // information.
Anders Carlsson3e2193c2011-04-14 00:40:03 +000065 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(),
66 /*Wide=*/false, /*Pascal=*/false,
Chris Lattner2085fd62009-02-18 06:40:38 +000067 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000068 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000069 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner69039812009-02-18 06:01:06 +000071 // Verify that this composite string is acceptable for ObjC strings.
72 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000073 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000074
75 // Initialize the constant string interface lazily. This assumes
Steve Naroffd9fd7642009-04-07 14:18:33 +000076 // the NSString interface is seen in this translation unit. Note: We
77 // don't use NSConstantString, since the runtime team considers this
78 // interface private (even though it appears in the header files).
Chris Lattnera0af1fe2009-02-18 06:06:56 +000079 QualType Ty = Context.getObjCConstantStringInterface();
80 if (!Ty.isNull()) {
Steve Naroff14108da2009-07-10 23:34:53 +000081 Ty = Context.getObjCObjectPointerType(Ty);
Fariborz Jahanian8a437762010-04-23 23:19:04 +000082 } else if (getLangOptions().NoConstantCFStrings) {
Fariborz Jahanian4c733072010-10-19 17:19:29 +000083 IdentifierInfo *NSIdent=0;
84 std::string StringClass(getLangOptions().ObjCConstantStringClass);
85
86 if (StringClass.empty())
87 NSIdent = &Context.Idents.get("NSConstantString");
88 else
89 NSIdent = &Context.Idents.get(StringClass);
90
Fariborz Jahanian8a437762010-04-23 23:19:04 +000091 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
92 LookupOrdinaryName);
93 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
94 Context.setObjCConstantStringInterface(StrIF);
95 Ty = Context.getObjCConstantStringInterface();
96 Ty = Context.getObjCObjectPointerType(Ty);
97 } else {
98 // If there is no NSConstantString interface defined then treat this
99 // as error and recover from it.
100 Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent
101 << S->getSourceRange();
102 Ty = Context.getObjCIdType();
103 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000104 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000105 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000106 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
107 LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000108 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
109 Context.setObjCConstantStringInterface(StrIF);
110 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +0000111 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000112 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000113 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000114 // strings as untyped objects and let the runtime figure it out later.
115 Ty = Context.getObjCIdType();
116 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattnerf4b136f2009-02-18 06:13:04 +0000119 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +0000120}
121
Argyrios Kyrtzidis3b5904b2011-05-14 20:32:39 +0000122ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +0000123 TypeSourceInfo *EncodedTypeInfo,
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000124 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +0000125 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000126 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000127 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000128 StrTy = Context.DependentTy;
129 else {
Argyrios Kyrtzidis3b5904b2011-05-14 20:32:39 +0000130 if (!EncodedType->getAsArrayTypeUnsafe()) // Incomplete array is handled.
131 if (RequireCompleteType(AtLoc, EncodedType,
132 PDiag(diag::err_incomplete_type_objc_at_encode)
133 << EncodedTypeInfo->getTypeLoc().getSourceRange()))
134 return ExprError();
135
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000136 std::string Str;
137 Context.getObjCEncodingForType(EncodedType, Str);
138
139 // The type of @encode is the same as the type of the corresponding string,
140 // which is an array type.
141 StrTy = Context.CharTy;
142 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000143 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000144 StrTy.addConst();
145 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
146 ArrayType::Normal, 0);
147 }
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Douglas Gregor81d34662010-04-20 15:39:42 +0000149 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000150}
151
John McCallf312b1e2010-08-26 23:41:50 +0000152ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
153 SourceLocation EncodeLoc,
154 SourceLocation LParenLoc,
155 ParsedType ty,
156 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000157 // FIXME: Preserve type source info ?
Douglas Gregor81d34662010-04-20 15:39:42 +0000158 TypeSourceInfo *TInfo;
159 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
160 if (!TInfo)
161 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
162 PP.getLocForEndOfToken(LParenLoc));
Chris Lattner85a932e2008-01-04 22:32:30 +0000163
Douglas Gregor81d34662010-04-20 15:39:42 +0000164 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000165}
166
John McCallf312b1e2010-08-26 23:41:50 +0000167ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
168 SourceLocation AtLoc,
169 SourceLocation SelLoc,
170 SourceLocation LParenLoc,
171 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000172 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000173 SourceRange(LParenLoc, RParenLoc), false, false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000174 if (!Method)
175 Method = LookupFactoryMethodInGlobalPool(Sel,
176 SourceRange(LParenLoc, RParenLoc));
177 if (!Method)
178 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
179
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000180 llvm::DenseMap<Selector, SourceLocation>::iterator Pos
181 = ReferencedSelectors.find(Sel);
182 if (Pos == ReferencedSelectors.end())
183 ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
184
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000185 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000186 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000187}
188
John McCallf312b1e2010-08-26 23:41:50 +0000189ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
190 SourceLocation AtLoc,
191 SourceLocation ProtoLoc,
192 SourceLocation LParenLoc,
193 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000194 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000195 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000196 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000197 return true;
198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000200 QualType Ty = Context.getObjCProtoType();
201 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000202 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000203 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000204 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000205}
206
John McCall26743b22011-02-03 09:00:02 +0000207/// Try to capture an implicit reference to 'self'.
208ObjCMethodDecl *Sema::tryCaptureObjCSelf() {
209 // Ignore block scopes: we can capture through them.
210 DeclContext *DC = CurContext;
211 while (true) {
212 if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext();
213 else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext();
214 else break;
215 }
216
217 // If we're not in an ObjC method, error out. Note that, unlike the
218 // C++ case, we don't require an instance method --- class methods
219 // still have a 'self', and we really do still need to capture it!
220 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
221 if (!method)
222 return 0;
223
224 ImplicitParamDecl *self = method->getSelfDecl();
225 assert(self && "capturing 'self' in non-definition?");
226
227 // Mark that we're closing on 'this' in all the block scopes, if applicable.
228 for (unsigned idx = FunctionScopes.size() - 1;
229 isa<BlockScopeInfo>(FunctionScopes[idx]);
John McCall6b5a61b2011-02-07 10:33:21 +0000230 --idx) {
231 BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]);
232 unsigned &captureIndex = blockScope->CaptureMap[self];
233 if (captureIndex) break;
234
235 bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]);
236 blockScope->Captures.push_back(
237 BlockDecl::Capture(self, /*byref*/ false, nested, /*copy*/ 0));
238 captureIndex = blockScope->Captures.size(); // +1
239 }
John McCall26743b22011-02-03 09:00:02 +0000240
241 return method;
242}
243
244
Mike Stump1eb44332009-09-09 15:08:12 +0000245bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
246 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000247 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000248 SourceLocation lbrac, SourceLocation rbrac,
John McCallf89e55a2010-11-18 06:31:45 +0000249 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000250 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000251 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregor92e986e2010-04-22 16:44:27 +0000252 for (unsigned i = 0; i != NumArgs; i++) {
253 if (Args[i]->isTypeDependent())
254 continue;
255
John Wiegley429bb272011-04-08 18:41:53 +0000256 ExprResult Result = DefaultArgumentPromotion(Args[i]);
257 if (Result.isInvalid())
258 return true;
259 Args[i] = Result.take();
Douglas Gregor92e986e2010-04-22 16:44:27 +0000260 }
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000261
Chris Lattner077bf5e2008-11-24 03:33:13 +0000262 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
263 diag::warn_inst_method_not_found;
264 Diag(lbrac, DiagID)
265 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000266 ReturnType = Context.getObjCIdType();
John McCallf89e55a2010-11-18 06:31:45 +0000267 VK = VK_RValue;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000268 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000271 ReturnType = Method->getSendResultType();
John McCallf89e55a2010-11-18 06:31:45 +0000272 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000274 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000275 // Method might have more arguments than selector indicates. This is due
276 // to addition of c-style arguments in method.
277 if (Method->param_size() > Sel.getNumArgs())
278 NumNamedArgs = Method->param_size();
279 // FIXME. This need be cleaned up.
280 if (NumArgs < NumNamedArgs) {
John McCallf89e55a2010-11-18 06:31:45 +0000281 Diag(lbrac, diag::err_typecheck_call_too_few_args)
282 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000283 return false;
284 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000285
Chris Lattner312531a2009-04-12 08:11:20 +0000286 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000287 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000288 // We can't do any type-checking on a type-dependent argument.
289 if (Args[i]->isTypeDependent())
290 continue;
291
Chris Lattner85a932e2008-01-04 22:32:30 +0000292 Expr *argExpr = Args[i];
Douglas Gregor92e986e2010-04-22 16:44:27 +0000293
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000294 ParmVarDecl *Param = Method->param_begin()[i];
Chris Lattner85a932e2008-01-04 22:32:30 +0000295 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000297 if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
298 Param->getType(),
299 PDiag(diag::err_call_incomplete_argument)
300 << argExpr->getSourceRange()))
301 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000302
Fariborz Jahanian745da3a2010-09-24 17:30:16 +0000303 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
304 Param);
John McCall3fa5cae2010-10-26 07:05:15 +0000305 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000306 if (ArgE.isInvalid())
307 IsError = true;
308 else
309 Args[i] = ArgE.takeAs<Expr>();
Chris Lattner85a932e2008-01-04 22:32:30 +0000310 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000311
312 // Promote additional arguments to variadic methods.
313 if (Method->isVariadic()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000314 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
315 if (Args[i]->isTypeDependent())
316 continue;
317
John Wiegley429bb272011-04-08 18:41:53 +0000318 ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
319 IsError |= Arg.isInvalid();
320 Args[i] = Arg.take();
Douglas Gregor92e986e2010-04-22 16:44:27 +0000321 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000322 } else {
323 // Check for extra arguments to non-variadic methods.
324 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000325 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000326 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000327 << 2 /*method*/ << NumNamedArgs << NumArgs
328 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000329 << SourceRange(Args[NumNamedArgs]->getLocStart(),
330 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000331 }
332 }
Fariborz Jahanian5272adf2011-04-15 22:06:22 +0000333 // diagnose nonnull arguments.
334 for (specific_attr_iterator<NonNullAttr>
335 i = Method->specific_attr_begin<NonNullAttr>(),
336 e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) {
337 CheckNonNullArguments(*i, Args, lbrac);
338 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000339
Douglas Gregor2725ca82010-04-21 19:57:20 +0000340 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Chris Lattner312531a2009-04-12 08:11:20 +0000341 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000342}
343
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000344bool Sema::isSelfExpr(Expr *RExpr) {
Fariborz Jahanianf2d74cc2011-03-27 19:53:47 +0000345 // 'self' is objc 'self' in an objc method only.
Fariborz Jahanianb4602102011-03-28 16:23:34 +0000346 DeclContext *DC = CurContext;
347 while (isa<BlockDecl>(DC))
348 DC = DC->getParent();
349 if (DC && !isa<ObjCMethodDecl>(DC))
Fariborz Jahanianf2d74cc2011-03-27 19:53:47 +0000350 return false;
John McCallf6a16482010-12-04 03:47:34 +0000351 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
352 if (ICE->getCastKind() == CK_LValueToRValue)
353 RExpr = ICE->getSubExpr();
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000354 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
355 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
356 return true;
357 return false;
358}
359
Steve Narofff1afaf62009-02-26 15:55:06 +0000360// Helper method for ActOnClassMethod/ActOnInstanceMethod.
361// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000362// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000363// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000364ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000365 ObjCInterfaceDecl *ClassDecl) {
366 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000367 // lookup in class and all superclasses
368 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000369 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000370 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Steve Naroff5609ec02009-03-08 18:56:13 +0000372 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000373 if (!Method)
374 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Steve Naroff5609ec02009-03-08 18:56:13 +0000376 // Before we give up, check if the selector is an instance method.
377 // But only in the root. This matches gcc's behaviour and what the
378 // runtime expects.
379 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000380 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000381 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000382 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000383 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000384 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Steve Naroff5609ec02009-03-08 18:56:13 +0000387 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000388 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000389 return Method;
390}
391
392ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
393 ObjCInterfaceDecl *ClassDecl) {
394 ObjCMethodDecl *Method = 0;
395 while (ClassDecl && !Method) {
396 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000397 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000398 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Steve Naroff5609ec02009-03-08 18:56:13 +0000400 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000401 if (!Method)
402 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000403 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000404 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000405 return Method;
406}
407
Fariborz Jahanian61478062011-03-09 20:18:06 +0000408/// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
409/// list of a qualified objective pointer type.
410ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
411 const ObjCObjectPointerType *OPT,
412 bool Instance)
413{
414 ObjCMethodDecl *MD = 0;
415 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
416 E = OPT->qual_end(); I != E; ++I) {
417 ObjCProtocolDecl *PROTO = (*I);
418 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
419 return MD;
420 }
421 }
422 return 0;
423}
424
Chris Lattner7f816522010-04-11 07:45:24 +0000425/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
426/// objective C interface. This is a property reference expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000427ExprResult Sema::
Chris Lattner7f816522010-04-11 07:45:24 +0000428HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000429 Expr *BaseExpr, DeclarationName MemberName,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000430 SourceLocation MemberLoc,
431 SourceLocation SuperLoc, QualType SuperType,
432 bool Super) {
Chris Lattner7f816522010-04-11 07:45:24 +0000433 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
434 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
Douglas Gregor109ec1b2011-04-20 18:19:55 +0000435
436 if (MemberName.getNameKind() != DeclarationName::Identifier) {
437 Diag(MemberLoc, diag::err_invalid_property_name)
438 << MemberName << QualType(OPT, 0);
439 return ExprError();
440 }
441
Chris Lattner7f816522010-04-11 07:45:24 +0000442 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
443
Fariborz Jahanian8b1aba42010-12-16 00:56:28 +0000444 if (IFace->isForwardDecl()) {
445 Diag(MemberLoc, diag::err_property_not_found_forward_class)
446 << MemberName << QualType(OPT, 0);
447 Diag(IFace->getLocation(), diag::note_forward_class);
448 return ExprError();
449 }
Chris Lattner7f816522010-04-11 07:45:24 +0000450 // Search for a declared property first.
451 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
452 // Check whether we can reference this property.
453 if (DiagnoseUseOfDecl(PD, MemberLoc))
454 return ExprError();
455 QualType ResTy = PD->getType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000456 ResTy = ResTy.getNonLValueExprType(Context);
Chris Lattner7f816522010-04-11 07:45:24 +0000457 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
458 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
459 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
John McCallf89e55a2010-11-18 06:31:45 +0000460 ResTy = Getter->getResultType();
461
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000462 if (Super)
463 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000464 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000465 MemberLoc,
466 SuperLoc, SuperType));
467 else
468 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000469 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000470 MemberLoc, BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000471 }
472 // Check protocols on qualified interfaces.
473 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
474 E = OPT->qual_end(); I != E; ++I)
475 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
476 // Check whether we can reference this property.
477 if (DiagnoseUseOfDecl(PD, MemberLoc))
478 return ExprError();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000479 if (Super)
John McCallf89e55a2010-11-18 06:31:45 +0000480 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
481 VK_LValue,
482 OK_ObjCProperty,
483 MemberLoc,
484 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000485 else
486 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000487 VK_LValue,
488 OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000489 MemberLoc,
490 BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000491 }
492 // If that failed, look for an "implicit" property by seeing if the nullary
493 // selector is implemented.
494
495 // FIXME: The logic for looking up nullary and unary selectors should be
496 // shared with the code in ActOnInstanceMessage.
497
498 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
499 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
Fariborz Jahanian27569b02011-03-09 22:17:12 +0000500
501 // May be founf in property's qualified list.
502 if (!Getter)
503 Getter = LookupMethodInQualifiedType(Sel, OPT, true);
Chris Lattner7f816522010-04-11 07:45:24 +0000504
505 // If this reference is in an @implementation, check for 'private' methods.
506 if (!Getter)
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000507 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner7f816522010-04-11 07:45:24 +0000508
509 // Look through local category implementations associated with the class.
510 if (!Getter)
511 Getter = IFace->getCategoryInstanceMethod(Sel);
512 if (Getter) {
513 // Check if we can reference this property.
514 if (DiagnoseUseOfDecl(Getter, MemberLoc))
515 return ExprError();
516 }
517 // If we found a getter then this may be a valid dot-reference, we
518 // will look for the matching setter, in case it is needed.
519 Selector SetterSel =
520 SelectorTable::constructSetterName(PP.getIdentifierTable(),
521 PP.getSelectorTable(), Member);
522 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
Fariborz Jahanian27569b02011-03-09 22:17:12 +0000523
524 // May be founf in property's qualified list.
525 if (!Setter)
526 Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
527
Chris Lattner7f816522010-04-11 07:45:24 +0000528 if (!Setter) {
529 // If this reference is in an @implementation, also check for 'private'
530 // methods.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000531 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner7f816522010-04-11 07:45:24 +0000532 }
533 // Look through local category implementations associated with the class.
534 if (!Setter)
535 Setter = IFace->getCategoryInstanceMethod(SetterSel);
Fariborz Jahanian27569b02011-03-09 22:17:12 +0000536
Chris Lattner7f816522010-04-11 07:45:24 +0000537 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
538 return ExprError();
539
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000540 if (Getter || Setter) {
541 QualType PType;
542 if (Getter)
543 PType = Getter->getSendResultType();
544 else {
545 ParmVarDecl *ArgDecl = *Setter->param_begin();
546 PType = ArgDecl->getType();
547 }
548
John McCall09431682010-11-18 19:01:18 +0000549 ExprValueKind VK = VK_LValue;
550 ExprObjectKind OK = OK_ObjCProperty;
551 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
552 PType->isVoidType())
553 VK = VK_RValue, OK = OK_Ordinary;
554
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000555 if (Super)
John McCall12f78a62010-12-02 01:19:52 +0000556 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
557 PType, VK, OK,
558 MemberLoc,
559 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000560 else
John McCall12f78a62010-12-02 01:19:52 +0000561 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
562 PType, VK, OK,
563 MemberLoc, BaseExpr));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000564
Chris Lattner7f816522010-04-11 07:45:24 +0000565 }
566
567 // Attempt to correct for typos in property names.
568 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000569 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000570 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000571 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000572 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000573 << MemberName << QualType(OPT, 0) << TypoResult
574 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000575 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
576 Diag(Property->getLocation(), diag::note_previous_decl)
577 << Property->getDeclName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000578 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
579 SuperLoc, SuperType, Super);
Chris Lattner7f816522010-04-11 07:45:24 +0000580 }
Fariborz Jahanian41aadbc2011-02-17 01:26:14 +0000581 ObjCInterfaceDecl *ClassDeclared;
582 if (ObjCIvarDecl *Ivar =
583 IFace->lookupInstanceVariable(Member, ClassDeclared)) {
584 QualType T = Ivar->getType();
585 if (const ObjCObjectPointerType * OBJPT =
586 T->getAsObjCInterfacePointerType()) {
587 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
588 if (ObjCInterfaceDecl *IFace = IFaceT->getDecl())
589 if (IFace->isForwardDecl()) {
590 Diag(MemberLoc, diag::err_property_not_as_forward_class)
Fariborz Jahanian2a96bf52011-02-17 17:30:05 +0000591 << MemberName << IFace;
Fariborz Jahanian41aadbc2011-02-17 01:26:14 +0000592 Diag(IFace->getLocation(), diag::note_forward_class);
593 return ExprError();
594 }
595 }
596 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000597
Chris Lattner7f816522010-04-11 07:45:24 +0000598 Diag(MemberLoc, diag::err_property_not_found)
599 << MemberName << QualType(OPT, 0);
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000600 if (Setter)
Chris Lattner7f816522010-04-11 07:45:24 +0000601 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000602 << MemberName << BaseExpr->getSourceRange();
Chris Lattner7f816522010-04-11 07:45:24 +0000603 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000604}
605
606
607
John McCall60d7b3a2010-08-24 06:29:42 +0000608ExprResult Sema::
Chris Lattnereb483eb2010-04-11 08:28:14 +0000609ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
610 IdentifierInfo &propertyName,
611 SourceLocation receiverNameLoc,
612 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000614 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000615 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
616 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000617 if (IFace == 0) {
618 // If the "receiver" is 'super' in a method, handle it as an expression-like
619 // property reference.
John McCall26743b22011-02-03 09:00:02 +0000620 if (receiverNamePtr->isStr("super")) {
621 if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000622 if (CurMethod->isInstanceMethod()) {
623 QualType T =
624 Context.getObjCInterfaceType(CurMethod->getClassInterface());
625 T = Context.getObjCObjectPointerType(T);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000626
627 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000628 /*BaseExpr*/0, &propertyName,
629 propertyNameLoc,
630 receiverNameLoc, T, true);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Chris Lattnereb483eb2010-04-11 08:28:14 +0000633 // Otherwise, if this is a class method, try dispatching to our
634 // superclass.
635 IFace = CurMethod->getClassInterface()->getSuperClass();
636 }
John McCall26743b22011-02-03 09:00:02 +0000637 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000638
639 if (IFace == 0) {
640 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
641 return ExprError();
642 }
643 }
644
645 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000646 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000647 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000648
649 // If this reference is in an @implementation, check for 'private' methods.
650 if (!Getter)
651 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
652 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000653 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000654 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000655
656 if (Getter) {
657 // FIXME: refactor/share with ActOnMemberReference().
658 // Check if we can reference this property.
659 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
660 return ExprError();
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Steve Naroff61f72cb2009-03-09 21:12:44 +0000663 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000664 Selector SetterSel =
665 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000666 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000668 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000669 if (!Setter) {
670 // If this reference is in an @implementation, also check for 'private'
671 // methods.
672 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
673 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000674 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000675 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000676 }
677 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000678 if (!Setter)
679 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000680
681 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
682 return ExprError();
683
684 if (Getter || Setter) {
685 QualType PType;
686
John McCall09431682010-11-18 19:01:18 +0000687 ExprValueKind VK = VK_LValue;
688 if (Getter) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000689 PType = Getter->getSendResultType();
John McCall09431682010-11-18 19:01:18 +0000690 if (!getLangOptions().CPlusPlus &&
691 !PType.hasQualifiers() && PType->isVoidType())
692 VK = VK_RValue;
693 } else {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000694 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
695 E = Setter->param_end(); PI != E; ++PI)
696 PType = (*PI)->getType();
John McCall09431682010-11-18 19:01:18 +0000697 VK = VK_LValue;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000698 }
John McCall09431682010-11-18 19:01:18 +0000699
700 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
701
John McCall12f78a62010-12-02 01:19:52 +0000702 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
703 PType, VK, OK,
704 propertyNameLoc,
705 receiverNameLoc, IFace));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000706 }
707 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
708 << &propertyName << Context.getObjCInterfaceType(IFace));
709}
710
Douglas Gregor47bd5432010-04-14 02:46:37 +0000711Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +0000712 IdentifierInfo *Name,
Douglas Gregor47bd5432010-04-14 02:46:37 +0000713 SourceLocation NameLoc,
714 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +0000715 bool HasTrailingDot,
John McCallb3d87482010-08-24 05:47:05 +0000716 ParsedType &ReceiverType) {
717 ReceiverType = ParsedType();
Douglas Gregor1569f952010-04-21 20:38:13 +0000718
Douglas Gregor47bd5432010-04-14 02:46:37 +0000719 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor95f42922010-10-14 22:11:03 +0000720 // messaging super. If the identifier is "super" and there is a
721 // trailing dot, it's an instance message.
722 if (IsSuper && S->isInObjcMethodScope())
723 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000724
725 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
726 LookupName(Result, S);
727
728 switch (Result.getResultKind()) {
729 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000730 // Normal name lookup didn't find anything. If we're in an
731 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor95f42922010-10-14 22:11:03 +0000732 // FIXME: This is a hack. Ivar lookup should be part of normal
733 // lookup.
Douglas Gregored464422010-04-19 20:09:36 +0000734 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
735 ObjCInterfaceDecl *ClassDeclared;
736 if (Method->getClassInterface()->lookupInstanceVariable(Name,
737 ClassDeclared))
738 return ObjCInstanceMessage;
739 }
Douglas Gregor95f42922010-10-14 22:11:03 +0000740
Douglas Gregor47bd5432010-04-14 02:46:37 +0000741 // Break out; we'll perform typo correction below.
742 break;
743
744 case LookupResult::NotFoundInCurrentInstantiation:
745 case LookupResult::FoundOverloaded:
746 case LookupResult::FoundUnresolvedValue:
747 case LookupResult::Ambiguous:
748 Result.suppressDiagnostics();
749 return ObjCInstanceMessage;
750
751 case LookupResult::Found: {
Fariborz Jahanian8348de32011-02-08 00:23:07 +0000752 // If the identifier is a class or not, and there is a trailing dot,
753 // it's an instance message.
754 if (HasTrailingDot)
755 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000756 // We found something. If it's a type, then we have a class
757 // message. Otherwise, it's an instance message.
758 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000759 QualType T;
760 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
761 T = Context.getObjCInterfaceType(Class);
762 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
763 T = Context.getTypeDeclType(Type);
764 else
765 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000766
Douglas Gregor1569f952010-04-21 20:38:13 +0000767 // We have a class message, and T is the type we're
768 // messaging. Build source-location information for it.
769 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000770 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor1569f952010-04-21 20:38:13 +0000771 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000772 }
773 }
774
Douglas Gregoraaf87162010-04-14 20:04:41 +0000775 // Determine our typo-correction context.
776 CorrectTypoContext CTC = CTC_Expression;
777 if (ObjCMethodDecl *Method = getCurMethodDecl())
778 if (Method->getClassInterface() &&
779 Method->getClassInterface()->getSuperClass())
780 CTC = CTC_ObjCMessageReceiver;
781
782 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
783 if (Result.isSingleResult()) {
784 // If we found a declaration, correct when it refers to an Objective-C
785 // class.
786 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000787 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregoraaf87162010-04-14 20:04:41 +0000788 Diag(NameLoc, diag::err_unknown_receiver_suggest)
789 << Name << Result.getLookupName()
790 << FixItHint::CreateReplacement(SourceRange(NameLoc),
791 ND->getNameAsString());
792 Diag(ND->getLocation(), diag::note_previous_decl)
793 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000794
Douglas Gregor1569f952010-04-21 20:38:13 +0000795 QualType T = Context.getObjCInterfaceType(Class);
796 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000797 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000798 return ObjCClassMessage;
799 }
800 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
801 Corrected.getAsIdentifierInfo()->isStr("super")) {
802 // If we've found the keyword "super", this is a send to super.
803 Diag(NameLoc, diag::err_unknown_receiver_suggest)
804 << Name << Corrected
805 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
Douglas Gregoraaf87162010-04-14 20:04:41 +0000806 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000807 }
808 }
809
810 // Fall back: let the parser try to parse it as an instance message.
811 return ObjCInstanceMessage;
812}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000813
John McCall60d7b3a2010-08-24 06:29:42 +0000814ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000815 SourceLocation SuperLoc,
816 Selector Sel,
817 SourceLocation LBracLoc,
818 SourceLocation SelectorLoc,
819 SourceLocation RBracLoc,
820 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000821 // Determine whether we are inside a method or not.
John McCall26743b22011-02-03 09:00:02 +0000822 ObjCMethodDecl *Method = tryCaptureObjCSelf();
Douglas Gregorf95861a2010-04-21 20:01:04 +0000823 if (!Method) {
824 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
825 return ExprError();
826 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000827
Douglas Gregorf95861a2010-04-21 20:01:04 +0000828 ObjCInterfaceDecl *Class = Method->getClassInterface();
829 if (!Class) {
830 Diag(SuperLoc, diag::error_no_super_class_message)
831 << Method->getDeclName();
832 return ExprError();
833 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000834
Douglas Gregorf95861a2010-04-21 20:01:04 +0000835 ObjCInterfaceDecl *Super = Class->getSuperClass();
836 if (!Super) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000837 // The current class does not have a superclass.
Ted Kremeneke00909a2011-01-23 17:21:34 +0000838 Diag(SuperLoc, diag::error_root_class_cannot_use_super)
839 << Class->getIdentifier();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000840 return ExprError();
Chris Lattner15faee12010-04-12 05:38:43 +0000841 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000842
Douglas Gregorf95861a2010-04-21 20:01:04 +0000843 // We are in a method whose class has a superclass, so 'super'
844 // is acting as a keyword.
845 if (Method->isInstanceMethod()) {
846 // Since we are in an instance method, this is an instance
847 // message to the superclass instance.
848 QualType SuperTy = Context.getObjCInterfaceType(Super);
849 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCall9ae2f072010-08-23 23:25:46 +0000850 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000851 Sel, /*Method=*/0,
852 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000853 }
Douglas Gregorf95861a2010-04-21 20:01:04 +0000854
855 // Since we are in a class method, this is a class message to
856 // the superclass.
857 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
858 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000859 SuperLoc, Sel, /*Method=*/0,
860 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000861}
862
863/// \brief Build an Objective-C class message expression.
864///
865/// This routine takes care of both normal class messages and
866/// class messages to the superclass.
867///
868/// \param ReceiverTypeInfo Type source information that describes the
869/// receiver of this message. This may be NULL, in which case we are
870/// sending to the superclass and \p SuperLoc must be a valid source
871/// location.
872
873/// \param ReceiverType The type of the object receiving the
874/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
875/// type as that refers to. For a superclass send, this is the type of
876/// the superclass.
877///
878/// \param SuperLoc The location of the "super" keyword in a
879/// superclass message.
880///
881/// \param Sel The selector to which the message is being sent.
882///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000883/// \param Method The method that this class message is invoking, if
884/// already known.
885///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000886/// \param LBracLoc The location of the opening square bracket ']'.
887///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000888/// \param RBrac The location of the closing square bracket ']'.
889///
890/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000891ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000892 QualType ReceiverType,
893 SourceLocation SuperLoc,
894 Selector Sel,
895 ObjCMethodDecl *Method,
896 SourceLocation LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000897 SourceLocation SelectorLoc,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000898 SourceLocation RBracLoc,
899 MultiExprArg ArgsIn) {
900 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregor9497a732010-09-16 01:51:54 +0000901 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregor0fbda682010-09-15 14:51:05 +0000902 if (LBracLoc.isInvalid()) {
903 Diag(Loc, diag::err_missing_open_square_message_send)
904 << FixItHint::CreateInsertion(Loc, "[");
905 LBracLoc = Loc;
906 }
907
Douglas Gregor92e986e2010-04-22 16:44:27 +0000908 if (ReceiverType->isDependentType()) {
909 // If the receiver type is dependent, we can't type-check anything
910 // at this point. Build a dependent expression.
911 unsigned NumArgs = ArgsIn.size();
912 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
913 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCallf89e55a2010-11-18 06:31:45 +0000914 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
915 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000916 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregor92e986e2010-04-22 16:44:27 +0000917 Args, NumArgs, RBracLoc));
918 }
Chris Lattner15faee12010-04-12 05:38:43 +0000919
Douglas Gregor2725ca82010-04-21 19:57:20 +0000920 // Find the class to which we are sending this message.
921 ObjCInterfaceDecl *Class = 0;
John McCallc12c5bb2010-05-15 11:32:37 +0000922 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
923 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000924 Diag(Loc, diag::err_invalid_receiver_class_message)
925 << ReceiverType;
926 return ExprError();
Steve Naroff7c778f12008-07-25 19:39:00 +0000927 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000928 assert(Class && "We don't know which class we're messaging?");
Fariborz Jahanian02b0d652011-03-08 19:12:46 +0000929 (void)DiagnoseUseOfDecl(Class, Loc);
Douglas Gregor2725ca82010-04-21 19:57:20 +0000930 // Find the method we are messaging.
Douglas Gregorf49bb082010-04-22 17:01:48 +0000931 if (!Method) {
932 if (Class->isForwardDecl()) {
933 // A forward class used in messaging is treated as a 'Class'
934 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
935 Method = LookupFactoryMethodInGlobalPool(Sel,
936 SourceRange(LBracLoc, RBracLoc));
937 if (Method)
938 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
939 << Method->getDeclName();
940 }
941 if (!Method)
942 Method = Class->lookupClassMethod(Sel);
943
944 // If we have an implementation in scope, check "private" methods.
945 if (!Method)
946 Method = LookupPrivateClassMethod(Sel, Class);
947
948 if (Method && DiagnoseUseOfDecl(Method, Loc))
949 return ExprError();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregor2725ca82010-04-21 19:57:20 +0000952 // Check the argument types and determine the result type.
953 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +0000954 ExprValueKind VK = VK_RValue;
955
Douglas Gregor2725ca82010-04-21 19:57:20 +0000956 unsigned NumArgs = ArgsIn.size();
957 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
958 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCallf89e55a2010-11-18 06:31:45 +0000959 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +0000960 return ExprError();
Ted Kremenek4df728e2008-06-24 15:50:53 +0000961
Douglas Gregor483dd2f2011-01-11 03:23:19 +0000962 if (Method && !Method->getResultType()->isVoidType() &&
963 RequireCompleteType(LBracLoc, Method->getResultType(),
964 diag::err_illegal_message_expr_incomplete_type))
965 return ExprError();
966
Douglas Gregor2725ca82010-04-21 19:57:20 +0000967 // Construct the appropriate ObjCMessageExpr.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000968 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +0000969 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +0000970 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000971 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000972 ReceiverType, Sel, SelectorLoc,
973 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000974 else
John McCallf89e55a2010-11-18 06:31:45 +0000975 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000976 ReceiverTypeInfo, Sel, SelectorLoc,
977 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000978 return MaybeBindToTemporary(Result);
Chris Lattner85a932e2008-01-04 22:32:30 +0000979}
980
Douglas Gregor2725ca82010-04-21 19:57:20 +0000981// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattner85a932e2008-01-04 22:32:30 +0000982// ArgExprs is optional - if it is present, the number of expressions
983// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +0000984ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor77328d12010-09-15 23:19:31 +0000985 ParsedType Receiver,
986 Selector Sel,
987 SourceLocation LBracLoc,
988 SourceLocation SelectorLoc,
989 SourceLocation RBracLoc,
990 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000991 TypeSourceInfo *ReceiverTypeInfo;
992 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
993 if (ReceiverType.isNull())
994 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Douglas Gregor2725ca82010-04-21 19:57:20 +0000997 if (!ReceiverTypeInfo)
998 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
999
1000 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorf49bb082010-04-22 17:01:48 +00001001 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001002 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +00001003}
1004
1005/// \brief Build an Objective-C instance message expression.
1006///
1007/// This routine takes care of both normal instance messages and
1008/// instance messages to the superclass instance.
1009///
1010/// \param Receiver The expression that computes the object that will
1011/// receive this message. This may be empty, in which case we are
1012/// sending to the superclass instance and \p SuperLoc must be a valid
1013/// source location.
1014///
1015/// \param ReceiverType The (static) type of the object receiving the
1016/// message. When a \p Receiver expression is provided, this is the
1017/// same type as that expression. For a superclass instance send, this
1018/// is a pointer to the type of the superclass.
1019///
1020/// \param SuperLoc The location of the "super" keyword in a
1021/// superclass instance message.
1022///
1023/// \param Sel The selector to which the message is being sent.
1024///
Douglas Gregorf49bb082010-04-22 17:01:48 +00001025/// \param Method The method that this instance message is invoking, if
1026/// already known.
1027///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001028/// \param LBracLoc The location of the opening square bracket ']'.
1029///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001030/// \param RBrac The location of the closing square bracket ']'.
1031///
1032/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +00001033ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001034 QualType ReceiverType,
1035 SourceLocation SuperLoc,
1036 Selector Sel,
1037 ObjCMethodDecl *Method,
1038 SourceLocation LBracLoc,
1039 SourceLocation SelectorLoc,
1040 SourceLocation RBracLoc,
1041 MultiExprArg ArgsIn) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001042 // The location of the receiver.
1043 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
1044
1045 if (LBracLoc.isInvalid()) {
1046 Diag(Loc, diag::err_missing_open_square_message_send)
1047 << FixItHint::CreateInsertion(Loc, "[");
1048 LBracLoc = Loc;
1049 }
1050
Douglas Gregor2725ca82010-04-21 19:57:20 +00001051 // If we have a receiver expression, perform appropriate promotions
1052 // and determine receiver type.
Douglas Gregor2725ca82010-04-21 19:57:20 +00001053 if (Receiver) {
Douglas Gregor92e986e2010-04-22 16:44:27 +00001054 if (Receiver->isTypeDependent()) {
1055 // If the receiver is type-dependent, we can't type-check anything
1056 // at this point. Build a dependent expression.
1057 unsigned NumArgs = ArgsIn.size();
1058 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1059 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
1060 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCallf89e55a2010-11-18 06:31:45 +00001061 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001062 SelectorLoc, /*Method=*/0,
1063 Args, NumArgs, RBracLoc));
Douglas Gregor92e986e2010-04-22 16:44:27 +00001064 }
1065
Douglas Gregor2725ca82010-04-21 19:57:20 +00001066 // If necessary, apply function/array conversion to the receiver.
1067 // C99 6.7.5.3p[7,8].
John Wiegley429bb272011-04-08 18:41:53 +00001068 ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
1069 if (Result.isInvalid())
1070 return ExprError();
1071 Receiver = Result.take();
Douglas Gregor2725ca82010-04-21 19:57:20 +00001072 ReceiverType = Receiver->getType();
1073 }
1074
Douglas Gregorf49bb082010-04-22 17:01:48 +00001075 if (!Method) {
1076 // Handle messages to id.
Fariborz Jahanianba551982010-08-10 18:10:50 +00001077 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001078 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorf49bb082010-04-22 17:01:48 +00001079 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
1080 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001081 SourceRange(LBracLoc, RBracLoc),
1082 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001083 if (!Method)
Douglas Gregor2725ca82010-04-21 19:57:20 +00001084 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001085 SourceRange(LBracLoc, RBracLoc),
1086 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001087 } else if (ReceiverType->isObjCClassType() ||
1088 ReceiverType->isObjCQualifiedClassType()) {
1089 // Handle messages to Class.
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001090 // We allow sending a message to a qualified Class ("Class<foo>"), which
1091 // is ok as long as one of the protocols implements the selector (if not, warn).
1092 if (const ObjCObjectPointerType *QClassTy
1093 = ReceiverType->getAsObjCQualifiedClassType()) {
1094 // Search protocols for class methods.
1095 Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
1096 if (!Method) {
1097 Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
1098 // warn if instance method found for a Class message.
1099 if (Method) {
1100 Diag(Loc, diag::warn_instance_method_on_class_found)
1101 << Method->getSelector() << Sel;
1102 Diag(Method->getLocation(), diag::note_method_declared_at);
1103 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +00001104 }
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001105 } else {
1106 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1107 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1108 // First check the public methods in the class interface.
1109 Method = ClassDecl->lookupClassMethod(Sel);
1110
1111 if (!Method)
1112 Method = LookupPrivateClassMethod(Sel, ClassDecl);
1113 }
1114 if (Method && DiagnoseUseOfDecl(Method, Loc))
1115 return ExprError();
1116 }
1117 if (!Method) {
1118 // If not messaging 'self', look for any factory method named 'Sel'.
1119 if (!Receiver || !isSelfExpr(Receiver)) {
1120 Method = LookupFactoryMethodInGlobalPool(Sel,
1121 SourceRange(LBracLoc, RBracLoc),
1122 true);
1123 if (!Method) {
1124 // If no class (factory) method was found, check if an _instance_
1125 // method of the same name exists in the root class only.
1126 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001127 SourceRange(LBracLoc, RBracLoc),
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001128 true);
1129 if (Method)
1130 if (const ObjCInterfaceDecl *ID =
1131 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1132 if (ID->getSuperClass())
1133 Diag(Loc, diag::warn_root_inst_method_not_found)
1134 << Sel << SourceRange(LBracLoc, RBracLoc);
1135 }
1136 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001137 }
1138 }
1139 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001140 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001141 ObjCInterfaceDecl* ClassDecl = 0;
1142
1143 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1144 // long as one of the protocols implements the selector (if not, warn).
1145 if (const ObjCObjectPointerType *QIdTy
1146 = ReceiverType->getAsObjCQualifiedIdType()) {
1147 // Search protocols for instance methods.
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001148 Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
1149 if (!Method)
1150 Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001151 } else if (const ObjCObjectPointerType *OCIType
1152 = ReceiverType->getAsObjCInterfacePointerType()) {
1153 // We allow sending a message to a pointer to an interface (an object).
1154 ClassDecl = OCIType->getInterfaceDecl();
1155 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1156 // faster than the following method (which can do *many* linear searches).
Sebastian Redldb9d2142010-08-02 23:18:59 +00001157 // The idea is to add class info to MethodPool.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001158 Method = ClassDecl->lookupInstanceMethod(Sel);
1159
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001160 if (!Method)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001161 // Search protocol qualifiers.
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001162 Method = LookupMethodInQualifiedType(Sel, OCIType, true);
1163
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00001164 const ObjCInterfaceDecl *forwardClass = 0;
Douglas Gregorf49bb082010-04-22 17:01:48 +00001165 if (!Method) {
1166 // If we have implementations in scope, check "private" methods.
1167 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1168
1169 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1170 // If we still haven't found a method, look in the global pool. This
1171 // behavior isn't very desirable, however we need it for GCC
1172 // compatibility. FIXME: should we deviate??
1173 if (OCIType->qual_empty()) {
1174 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001175 SourceRange(LBracLoc, RBracLoc));
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00001176 if (OCIType->getInterfaceDecl()->isForwardDecl())
1177 forwardClass = OCIType->getInterfaceDecl();
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001178 if (Method && !forwardClass)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001179 Diag(Loc, diag::warn_maynot_respond)
1180 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1181 }
1182 }
1183 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001184 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorf49bb082010-04-22 17:01:48 +00001185 return ExprError();
1186 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001187 (ReceiverType->isPointerType() ||
1188 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001189 // Implicitly convert integers and pointers to 'id' but emit a warning.
1190 Diag(Loc, diag::warn_bad_receiver_type)
1191 << ReceiverType
1192 << Receiver->getSourceRange();
1193 if (ReceiverType->isPointerType())
John Wiegley429bb272011-04-08 18:41:53 +00001194 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1195 CK_BitCast).take();
John McCall404cd162010-11-13 01:35:44 +00001196 else {
1197 // TODO: specialized warning on null receivers?
1198 bool IsNull = Receiver->isNullPointerConstant(Context,
1199 Expr::NPC_ValueDependentIsNull);
John Wiegley429bb272011-04-08 18:41:53 +00001200 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1201 IsNull ? CK_NullToPointer : CK_IntegralToPointer).take();
John McCall404cd162010-11-13 01:35:44 +00001202 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001203 ReceiverType = Receiver->getType();
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001204 }
John Wiegley429bb272011-04-08 18:41:53 +00001205 else {
1206 ExprResult ReceiverRes;
1207 if (getLangOptions().CPlusPlus)
1208 ReceiverRes = PerformContextuallyConvertToObjCId(Receiver);
1209 if (ReceiverRes.isUsable()) {
1210 Receiver = ReceiverRes.take();
1211 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1212 Receiver = ICE->getSubExpr();
1213 ReceiverType = Receiver->getType();
1214 }
1215 return BuildInstanceMessage(Receiver,
1216 ReceiverType,
1217 SuperLoc,
1218 Sel,
1219 Method,
1220 LBracLoc,
1221 SelectorLoc,
1222 RBracLoc,
1223 move(ArgsIn));
1224 } else {
1225 // Reject other random receiver types (e.g. structs).
1226 Diag(Loc, diag::err_bad_receiver_type)
1227 << ReceiverType << Receiver->getSourceRange();
1228 return ExprError();
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001229 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001230 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001231 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001232 }
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor2725ca82010-04-21 19:57:20 +00001234 // Check the message arguments.
1235 unsigned NumArgs = ArgsIn.size();
1236 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1237 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001238 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001239 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1240 ReceiverType->isObjCQualifiedClassType());
1241 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCallf89e55a2010-11-18 06:31:45 +00001242 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001243 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001244
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001245 if (Method && !Method->getResultType()->isVoidType() &&
1246 RequireCompleteType(LBracLoc, Method->getResultType(),
1247 diag::err_illegal_message_expr_incomplete_type))
1248 return ExprError();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001249
Douglas Gregor2725ca82010-04-21 19:57:20 +00001250 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001251 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001252 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001253 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001254 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001255 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001256 Args, NumArgs, RBracLoc);
1257 else
John McCallf89e55a2010-11-18 06:31:45 +00001258 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001259 Receiver, Sel, SelectorLoc, Method,
1260 Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001261 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001262}
1263
1264// ActOnInstanceMessage - used for both unary and keyword messages.
1265// ArgExprs is optional - if it is present, the number of expressions
1266// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001267ExprResult Sema::ActOnInstanceMessage(Scope *S,
1268 Expr *Receiver,
1269 Selector Sel,
1270 SourceLocation LBracLoc,
1271 SourceLocation SelectorLoc,
1272 SourceLocation RBracLoc,
1273 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001274 if (!Receiver)
1275 return ExprError();
1276
John McCall9ae2f072010-08-23 23:25:46 +00001277 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001278 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001279 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001280}
Chris Lattnereca7be62008-04-07 05:30:13 +00001281