blob: eeaa45163d54c8761c2a992ab31ebf43596a4009 [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.
65 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000066 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000067 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000068 }
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner69039812009-02-18 06:01:06 +000070 // Verify that this composite string is acceptable for ObjC strings.
71 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000072 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000073
74 // Initialize the constant string interface lazily. This assumes
Steve Naroffd9fd7642009-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 Lattnera0af1fe2009-02-18 06:06:56 +000078 QualType Ty = Context.getObjCConstantStringInterface();
79 if (!Ty.isNull()) {
Steve Naroff14108da2009-07-10 23:34:53 +000080 Ty = Context.getObjCObjectPointerType(Ty);
Fariborz Jahanian8a437762010-04-23 23:19:04 +000081 } else if (getLangOptions().NoConstantCFStrings) {
Fariborz Jahanian4c733072010-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 Jahanian8a437762010-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 Lattner13fd7e52008-06-21 21:44:18 +0000103 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000104 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000105 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
106 LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000107 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
108 Context.setObjCConstantStringInterface(StrIF);
109 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +0000110 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000111 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000112 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000113 // strings as untyped objects and let the runtime figure it out later.
114 Ty = Context.getObjCIdType();
115 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattnerf4b136f2009-02-18 06:13:04 +0000118 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +0000119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +0000122 TypeSourceInfo *EncodedTypeInfo,
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000123 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +0000124 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000125 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000126 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-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 McCall4b7a8342010-03-15 10:54:44 +0000136 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-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 Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor81d34662010-04-20 15:39:42 +0000142 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000143}
144
John McCallf312b1e2010-08-26 23:41:50 +0000145ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
146 SourceLocation EncodeLoc,
147 SourceLocation LParenLoc,
148 ParsedType ty,
149 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000150 // FIXME: Preserve type source info ?
Douglas Gregor81d34662010-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 Lattner85a932e2008-01-04 22:32:30 +0000156
Douglas Gregor81d34662010-04-20 15:39:42 +0000157 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000158}
159
John McCallf312b1e2010-08-26 23:41:50 +0000160ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
161 SourceLocation AtLoc,
162 SourceLocation SelLoc,
163 SourceLocation LParenLoc,
164 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000165 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000166 SourceRange(LParenLoc, RParenLoc), false, false);
Fariborz Jahanian7ff22de2009-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 Jahanian3fe10412010-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 Lattnera0af1fe2009-02-18 06:06:56 +0000178 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000179 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000180}
181
John McCallf312b1e2010-08-26 23:41:50 +0000182ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
183 SourceLocation AtLoc,
184 SourceLocation ProtoLoc,
185 SourceLocation LParenLoc,
186 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000187 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000188 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000189 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000190 return true;
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000193 QualType Ty = Context.getObjCProtoType();
194 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000195 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000196 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000197 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000198}
199
John McCall26743b22011-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 McCall6b5a61b2011-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 McCall26743b22011-02-03 09:00:02 +0000233
234 return method;
235}
236
237
Mike Stump1eb44332009-09-09 15:08:12 +0000238bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
239 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000240 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000241 SourceLocation lbrac, SourceLocation rbrac,
John McCallf89e55a2010-11-18 06:31:45 +0000242 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000243 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000244 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregor92e986e2010-04-22 16:44:27 +0000245 for (unsigned i = 0; i != NumArgs; i++) {
246 if (Args[i]->isTypeDependent())
247 continue;
248
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000249 DefaultArgumentPromotion(Args[i]);
Douglas Gregor92e986e2010-04-22 16:44:27 +0000250 }
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000251
Chris Lattner077bf5e2008-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 Dunbar637cebb2008-09-11 00:01:56 +0000256 ReturnType = Context.getObjCIdType();
John McCallf89e55a2010-11-18 06:31:45 +0000257 VK = VK_RValue;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000258 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000261 ReturnType = Method->getSendResultType();
John McCallf89e55a2010-11-18 06:31:45 +0000262 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000264 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-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 McCallf89e55a2010-11-18 06:31:45 +0000271 Diag(lbrac, diag::err_typecheck_call_too_few_args)
272 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000273 return false;
274 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000275
Chris Lattner312531a2009-04-12 08:11:20 +0000276 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000277 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregor92e986e2010-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 Lattner85a932e2008-01-04 22:32:30 +0000282 Expr *argExpr = Args[i];
Douglas Gregor92e986e2010-04-22 16:44:27 +0000283
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000284 ParmVarDecl *Param = Method->param_begin()[i];
Chris Lattner85a932e2008-01-04 22:32:30 +0000285 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregor688fc9b2010-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 Lattner85a932e2008-01-04 22:32:30 +0000292
Fariborz Jahanian745da3a2010-09-24 17:30:16 +0000293 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
294 Param);
John McCall3fa5cae2010-10-26 07:05:15 +0000295 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000296 if (ArgE.isInvalid())
297 IsError = true;
298 else
299 Args[i] = ArgE.takeAs<Expr>();
Chris Lattner85a932e2008-01-04 22:32:30 +0000300 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000301
302 // Promote additional arguments to variadic methods.
303 if (Method->isVariadic()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000304 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
305 if (Args[i]->isTypeDependent())
306 continue;
307
Chris Lattner40378332010-05-16 04:01:30 +0000308 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
Douglas Gregor92e986e2010-04-22 16:44:27 +0000309 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000310 } else {
311 // Check for extra arguments to non-variadic methods.
312 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000313 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000314 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000315 << 2 /*method*/ << NumNamedArgs << NumArgs
316 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000317 << SourceRange(Args[NumNamedArgs]->getLocStart(),
318 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000319 }
320 }
321
Douglas Gregor2725ca82010-04-21 19:57:20 +0000322 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Chris Lattner312531a2009-04-12 08:11:20 +0000323 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000324}
325
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000326bool Sema::isSelfExpr(Expr *RExpr) {
John McCallf6a16482010-12-04 03:47:34 +0000327 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
328 if (ICE->getCastKind() == CK_LValueToRValue)
329 RExpr = ICE->getSubExpr();
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000330 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
331 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
332 return true;
333 return false;
334}
335
Steve Narofff1afaf62009-02-26 15:55:06 +0000336// Helper method for ActOnClassMethod/ActOnInstanceMethod.
337// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000338// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000339// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000340ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000341 ObjCInterfaceDecl *ClassDecl) {
342 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000343 // lookup in class and all superclasses
344 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000345 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000346 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Steve Naroff5609ec02009-03-08 18:56:13 +0000348 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000349 if (!Method)
350 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Steve Naroff5609ec02009-03-08 18:56:13 +0000352 // Before we give up, check if the selector is an instance method.
353 // But only in the root. This matches gcc's behaviour and what the
354 // runtime expects.
355 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000356 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000357 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000358 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000359 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000360 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Steve Naroff5609ec02009-03-08 18:56:13 +0000363 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000364 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000365 return Method;
366}
367
368ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
369 ObjCInterfaceDecl *ClassDecl) {
370 ObjCMethodDecl *Method = 0;
371 while (ClassDecl && !Method) {
372 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000373 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000374 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Steve Naroff5609ec02009-03-08 18:56:13 +0000376 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000377 if (!Method)
378 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000379 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000380 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000381 return Method;
382}
383
Chris Lattner7f816522010-04-11 07:45:24 +0000384/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
385/// objective C interface. This is a property reference expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000386ExprResult Sema::
Chris Lattner7f816522010-04-11 07:45:24 +0000387HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000388 Expr *BaseExpr, DeclarationName MemberName,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000389 SourceLocation MemberLoc,
390 SourceLocation SuperLoc, QualType SuperType,
391 bool Super) {
Chris Lattner7f816522010-04-11 07:45:24 +0000392 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
393 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
394 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
395
Fariborz Jahanian8b1aba42010-12-16 00:56:28 +0000396 if (IFace->isForwardDecl()) {
397 Diag(MemberLoc, diag::err_property_not_found_forward_class)
398 << MemberName << QualType(OPT, 0);
399 Diag(IFace->getLocation(), diag::note_forward_class);
400 return ExprError();
401 }
Chris Lattner7f816522010-04-11 07:45:24 +0000402 // Search for a declared property first.
403 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
404 // Check whether we can reference this property.
405 if (DiagnoseUseOfDecl(PD, MemberLoc))
406 return ExprError();
407 QualType ResTy = PD->getType();
408 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
409 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
410 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
John McCallf89e55a2010-11-18 06:31:45 +0000411 ResTy = Getter->getResultType();
412
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000413 if (Super)
414 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000415 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000416 MemberLoc,
417 SuperLoc, SuperType));
418 else
419 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000420 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000421 MemberLoc, BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000422 }
423 // Check protocols on qualified interfaces.
424 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
425 E = OPT->qual_end(); I != E; ++I)
426 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
427 // Check whether we can reference this property.
428 if (DiagnoseUseOfDecl(PD, MemberLoc))
429 return ExprError();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000430 if (Super)
John McCallf89e55a2010-11-18 06:31:45 +0000431 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
432 VK_LValue,
433 OK_ObjCProperty,
434 MemberLoc,
435 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000436 else
437 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000438 VK_LValue,
439 OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000440 MemberLoc,
441 BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000442 }
443 // If that failed, look for an "implicit" property by seeing if the nullary
444 // selector is implemented.
445
446 // FIXME: The logic for looking up nullary and unary selectors should be
447 // shared with the code in ActOnInstanceMessage.
448
449 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
450 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
451
452 // If this reference is in an @implementation, check for 'private' methods.
453 if (!Getter)
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000454 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner7f816522010-04-11 07:45:24 +0000455
456 // Look through local category implementations associated with the class.
457 if (!Getter)
458 Getter = IFace->getCategoryInstanceMethod(Sel);
459 if (Getter) {
460 // Check if we can reference this property.
461 if (DiagnoseUseOfDecl(Getter, MemberLoc))
462 return ExprError();
463 }
464 // If we found a getter then this may be a valid dot-reference, we
465 // will look for the matching setter, in case it is needed.
466 Selector SetterSel =
467 SelectorTable::constructSetterName(PP.getIdentifierTable(),
468 PP.getSelectorTable(), Member);
469 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
470 if (!Setter) {
471 // If this reference is in an @implementation, also check for 'private'
472 // methods.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000473 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner7f816522010-04-11 07:45:24 +0000474 }
475 // Look through local category implementations associated with the class.
476 if (!Setter)
477 Setter = IFace->getCategoryInstanceMethod(SetterSel);
478
479 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
480 return ExprError();
481
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000482 if (Getter || Setter) {
483 QualType PType;
484 if (Getter)
485 PType = Getter->getSendResultType();
486 else {
487 ParmVarDecl *ArgDecl = *Setter->param_begin();
488 PType = ArgDecl->getType();
489 }
490
John McCall09431682010-11-18 19:01:18 +0000491 ExprValueKind VK = VK_LValue;
492 ExprObjectKind OK = OK_ObjCProperty;
493 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
494 PType->isVoidType())
495 VK = VK_RValue, OK = OK_Ordinary;
496
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000497 if (Super)
John McCall12f78a62010-12-02 01:19:52 +0000498 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
499 PType, VK, OK,
500 MemberLoc,
501 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000502 else
John McCall12f78a62010-12-02 01:19:52 +0000503 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
504 PType, VK, OK,
505 MemberLoc, BaseExpr));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000506
Chris Lattner7f816522010-04-11 07:45:24 +0000507 }
508
509 // Attempt to correct for typos in property names.
510 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000511 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000512 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000513 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000514 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000515 << MemberName << QualType(OPT, 0) << TypoResult
516 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000517 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
518 Diag(Property->getLocation(), diag::note_previous_decl)
519 << Property->getDeclName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000520 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
521 SuperLoc, SuperType, Super);
Chris Lattner7f816522010-04-11 07:45:24 +0000522 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000523
Chris Lattner7f816522010-04-11 07:45:24 +0000524 Diag(MemberLoc, diag::err_property_not_found)
525 << MemberName << QualType(OPT, 0);
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000526 if (Setter)
Chris Lattner7f816522010-04-11 07:45:24 +0000527 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000528 << MemberName << BaseExpr->getSourceRange();
Chris Lattner7f816522010-04-11 07:45:24 +0000529 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000530}
531
532
533
John McCall60d7b3a2010-08-24 06:29:42 +0000534ExprResult Sema::
Chris Lattnereb483eb2010-04-11 08:28:14 +0000535ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
536 IdentifierInfo &propertyName,
537 SourceLocation receiverNameLoc,
538 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000540 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000541 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
542 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000543 if (IFace == 0) {
544 // If the "receiver" is 'super' in a method, handle it as an expression-like
545 // property reference.
John McCall26743b22011-02-03 09:00:02 +0000546 if (receiverNamePtr->isStr("super")) {
547 if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000548 if (CurMethod->isInstanceMethod()) {
549 QualType T =
550 Context.getObjCInterfaceType(CurMethod->getClassInterface());
551 T = Context.getObjCObjectPointerType(T);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000552
553 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000554 /*BaseExpr*/0, &propertyName,
555 propertyNameLoc,
556 receiverNameLoc, T, true);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattnereb483eb2010-04-11 08:28:14 +0000559 // Otherwise, if this is a class method, try dispatching to our
560 // superclass.
561 IFace = CurMethod->getClassInterface()->getSuperClass();
562 }
John McCall26743b22011-02-03 09:00:02 +0000563 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000564
565 if (IFace == 0) {
566 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
567 return ExprError();
568 }
569 }
570
571 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000572 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000573 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000574
575 // If this reference is in an @implementation, check for 'private' methods.
576 if (!Getter)
577 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
578 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000579 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000580 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000581
582 if (Getter) {
583 // FIXME: refactor/share with ActOnMemberReference().
584 // Check if we can reference this property.
585 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
586 return ExprError();
587 }
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Steve Naroff61f72cb2009-03-09 21:12:44 +0000589 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000590 Selector SetterSel =
591 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000592 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000595 if (!Setter) {
596 // If this reference is in an @implementation, also check for 'private'
597 // methods.
598 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
599 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000600 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000602 }
603 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000604 if (!Setter)
605 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000606
607 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
608 return ExprError();
609
610 if (Getter || Setter) {
611 QualType PType;
612
John McCall09431682010-11-18 19:01:18 +0000613 ExprValueKind VK = VK_LValue;
614 if (Getter) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000615 PType = Getter->getSendResultType();
John McCall09431682010-11-18 19:01:18 +0000616 if (!getLangOptions().CPlusPlus &&
617 !PType.hasQualifiers() && PType->isVoidType())
618 VK = VK_RValue;
619 } else {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000620 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
621 E = Setter->param_end(); PI != E; ++PI)
622 PType = (*PI)->getType();
John McCall09431682010-11-18 19:01:18 +0000623 VK = VK_LValue;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000624 }
John McCall09431682010-11-18 19:01:18 +0000625
626 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
627
John McCall12f78a62010-12-02 01:19:52 +0000628 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
629 PType, VK, OK,
630 propertyNameLoc,
631 receiverNameLoc, IFace));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000632 }
633 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
634 << &propertyName << Context.getObjCInterfaceType(IFace));
635}
636
Douglas Gregor47bd5432010-04-14 02:46:37 +0000637Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +0000638 IdentifierInfo *Name,
Douglas Gregor47bd5432010-04-14 02:46:37 +0000639 SourceLocation NameLoc,
640 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +0000641 bool HasTrailingDot,
John McCallb3d87482010-08-24 05:47:05 +0000642 ParsedType &ReceiverType) {
643 ReceiverType = ParsedType();
Douglas Gregor1569f952010-04-21 20:38:13 +0000644
Douglas Gregor47bd5432010-04-14 02:46:37 +0000645 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor95f42922010-10-14 22:11:03 +0000646 // messaging super. If the identifier is "super" and there is a
647 // trailing dot, it's an instance message.
648 if (IsSuper && S->isInObjcMethodScope())
649 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000650
651 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
652 LookupName(Result, S);
653
654 switch (Result.getResultKind()) {
655 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000656 // Normal name lookup didn't find anything. If we're in an
657 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor95f42922010-10-14 22:11:03 +0000658 // FIXME: This is a hack. Ivar lookup should be part of normal
659 // lookup.
Douglas Gregored464422010-04-19 20:09:36 +0000660 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
661 ObjCInterfaceDecl *ClassDeclared;
662 if (Method->getClassInterface()->lookupInstanceVariable(Name,
663 ClassDeclared))
664 return ObjCInstanceMessage;
665 }
Douglas Gregor95f42922010-10-14 22:11:03 +0000666
Douglas Gregor47bd5432010-04-14 02:46:37 +0000667 // Break out; we'll perform typo correction below.
668 break;
669
670 case LookupResult::NotFoundInCurrentInstantiation:
671 case LookupResult::FoundOverloaded:
672 case LookupResult::FoundUnresolvedValue:
673 case LookupResult::Ambiguous:
674 Result.suppressDiagnostics();
675 return ObjCInstanceMessage;
676
677 case LookupResult::Found: {
678 // We found something. If it's a type, then we have a class
679 // message. Otherwise, it's an instance message.
680 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000681 QualType T;
682 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
683 T = Context.getObjCInterfaceType(Class);
684 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
685 T = Context.getTypeDeclType(Type);
686 else
687 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000688
Douglas Gregor1569f952010-04-21 20:38:13 +0000689 // We have a class message, and T is the type we're
690 // messaging. Build source-location information for it.
691 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000692 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor1569f952010-04-21 20:38:13 +0000693 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000694 }
695 }
696
Douglas Gregoraaf87162010-04-14 20:04:41 +0000697 // Determine our typo-correction context.
698 CorrectTypoContext CTC = CTC_Expression;
699 if (ObjCMethodDecl *Method = getCurMethodDecl())
700 if (Method->getClassInterface() &&
701 Method->getClassInterface()->getSuperClass())
702 CTC = CTC_ObjCMessageReceiver;
703
704 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
705 if (Result.isSingleResult()) {
706 // If we found a declaration, correct when it refers to an Objective-C
707 // class.
708 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000709 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregoraaf87162010-04-14 20:04:41 +0000710 Diag(NameLoc, diag::err_unknown_receiver_suggest)
711 << Name << Result.getLookupName()
712 << FixItHint::CreateReplacement(SourceRange(NameLoc),
713 ND->getNameAsString());
714 Diag(ND->getLocation(), diag::note_previous_decl)
715 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000716
Douglas Gregor1569f952010-04-21 20:38:13 +0000717 QualType T = Context.getObjCInterfaceType(Class);
718 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000719 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000720 return ObjCClassMessage;
721 }
722 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
723 Corrected.getAsIdentifierInfo()->isStr("super")) {
724 // If we've found the keyword "super", this is a send to super.
725 Diag(NameLoc, diag::err_unknown_receiver_suggest)
726 << Name << Corrected
727 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
Douglas Gregoraaf87162010-04-14 20:04:41 +0000728 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000729 }
730 }
731
732 // Fall back: let the parser try to parse it as an instance message.
733 return ObjCInstanceMessage;
734}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000735
John McCall60d7b3a2010-08-24 06:29:42 +0000736ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000737 SourceLocation SuperLoc,
738 Selector Sel,
739 SourceLocation LBracLoc,
740 SourceLocation SelectorLoc,
741 SourceLocation RBracLoc,
742 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000743 // Determine whether we are inside a method or not.
John McCall26743b22011-02-03 09:00:02 +0000744 ObjCMethodDecl *Method = tryCaptureObjCSelf();
Douglas Gregorf95861a2010-04-21 20:01:04 +0000745 if (!Method) {
746 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
747 return ExprError();
748 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000749
Douglas Gregorf95861a2010-04-21 20:01:04 +0000750 ObjCInterfaceDecl *Class = Method->getClassInterface();
751 if (!Class) {
752 Diag(SuperLoc, diag::error_no_super_class_message)
753 << Method->getDeclName();
754 return ExprError();
755 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000756
Douglas Gregorf95861a2010-04-21 20:01:04 +0000757 ObjCInterfaceDecl *Super = Class->getSuperClass();
758 if (!Super) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000759 // The current class does not have a superclass.
Ted Kremeneke00909a2011-01-23 17:21:34 +0000760 Diag(SuperLoc, diag::error_root_class_cannot_use_super)
761 << Class->getIdentifier();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000762 return ExprError();
Chris Lattner15faee12010-04-12 05:38:43 +0000763 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000764
Douglas Gregorf95861a2010-04-21 20:01:04 +0000765 // We are in a method whose class has a superclass, so 'super'
766 // is acting as a keyword.
767 if (Method->isInstanceMethod()) {
768 // Since we are in an instance method, this is an instance
769 // message to the superclass instance.
770 QualType SuperTy = Context.getObjCInterfaceType(Super);
771 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCall9ae2f072010-08-23 23:25:46 +0000772 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000773 Sel, /*Method=*/0,
774 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000775 }
Douglas Gregorf95861a2010-04-21 20:01:04 +0000776
777 // Since we are in a class method, this is a class message to
778 // the superclass.
779 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
780 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000781 SuperLoc, Sel, /*Method=*/0,
782 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000783}
784
785/// \brief Build an Objective-C class message expression.
786///
787/// This routine takes care of both normal class messages and
788/// class messages to the superclass.
789///
790/// \param ReceiverTypeInfo Type source information that describes the
791/// receiver of this message. This may be NULL, in which case we are
792/// sending to the superclass and \p SuperLoc must be a valid source
793/// location.
794
795/// \param ReceiverType The type of the object receiving the
796/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
797/// type as that refers to. For a superclass send, this is the type of
798/// the superclass.
799///
800/// \param SuperLoc The location of the "super" keyword in a
801/// superclass message.
802///
803/// \param Sel The selector to which the message is being sent.
804///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000805/// \param Method The method that this class message is invoking, if
806/// already known.
807///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000808/// \param LBracLoc The location of the opening square bracket ']'.
809///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000810/// \param RBrac The location of the closing square bracket ']'.
811///
812/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000813ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000814 QualType ReceiverType,
815 SourceLocation SuperLoc,
816 Selector Sel,
817 ObjCMethodDecl *Method,
818 SourceLocation LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000819 SourceLocation SelectorLoc,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000820 SourceLocation RBracLoc,
821 MultiExprArg ArgsIn) {
822 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregor9497a732010-09-16 01:51:54 +0000823 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregor0fbda682010-09-15 14:51:05 +0000824 if (LBracLoc.isInvalid()) {
825 Diag(Loc, diag::err_missing_open_square_message_send)
826 << FixItHint::CreateInsertion(Loc, "[");
827 LBracLoc = Loc;
828 }
829
Douglas Gregor92e986e2010-04-22 16:44:27 +0000830 if (ReceiverType->isDependentType()) {
831 // If the receiver type is dependent, we can't type-check anything
832 // at this point. Build a dependent expression.
833 unsigned NumArgs = ArgsIn.size();
834 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
835 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCallf89e55a2010-11-18 06:31:45 +0000836 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
837 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000838 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregor92e986e2010-04-22 16:44:27 +0000839 Args, NumArgs, RBracLoc));
840 }
Chris Lattner15faee12010-04-12 05:38:43 +0000841
Douglas Gregor2725ca82010-04-21 19:57:20 +0000842 // Find the class to which we are sending this message.
843 ObjCInterfaceDecl *Class = 0;
John McCallc12c5bb2010-05-15 11:32:37 +0000844 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
845 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000846 Diag(Loc, diag::err_invalid_receiver_class_message)
847 << ReceiverType;
848 return ExprError();
Steve Naroff7c778f12008-07-25 19:39:00 +0000849 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000850 assert(Class && "We don't know which class we're messaging?");
851
852 // Find the method we are messaging.
Douglas Gregorf49bb082010-04-22 17:01:48 +0000853 if (!Method) {
854 if (Class->isForwardDecl()) {
855 // A forward class used in messaging is treated as a 'Class'
856 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
857 Method = LookupFactoryMethodInGlobalPool(Sel,
858 SourceRange(LBracLoc, RBracLoc));
859 if (Method)
860 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
861 << Method->getDeclName();
862 }
863 if (!Method)
864 Method = Class->lookupClassMethod(Sel);
865
866 // If we have an implementation in scope, check "private" methods.
867 if (!Method)
868 Method = LookupPrivateClassMethod(Sel, Class);
869
870 if (Method && DiagnoseUseOfDecl(Method, Loc))
871 return ExprError();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor2725ca82010-04-21 19:57:20 +0000874 // Check the argument types and determine the result type.
875 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +0000876 ExprValueKind VK = VK_RValue;
877
Douglas Gregor2725ca82010-04-21 19:57:20 +0000878 unsigned NumArgs = ArgsIn.size();
879 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
880 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCallf89e55a2010-11-18 06:31:45 +0000881 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +0000882 return ExprError();
Ted Kremenek4df728e2008-06-24 15:50:53 +0000883
Douglas Gregor483dd2f2011-01-11 03:23:19 +0000884 if (Method && !Method->getResultType()->isVoidType() &&
885 RequireCompleteType(LBracLoc, Method->getResultType(),
886 diag::err_illegal_message_expr_incomplete_type))
887 return ExprError();
888
Douglas Gregor2725ca82010-04-21 19:57:20 +0000889 // Construct the appropriate ObjCMessageExpr.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000890 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +0000891 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +0000892 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000893 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000894 ReceiverType, Sel, SelectorLoc,
895 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000896 else
John McCallf89e55a2010-11-18 06:31:45 +0000897 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000898 ReceiverTypeInfo, Sel, SelectorLoc,
899 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000900 return MaybeBindToTemporary(Result);
Chris Lattner85a932e2008-01-04 22:32:30 +0000901}
902
Douglas Gregor2725ca82010-04-21 19:57:20 +0000903// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattner85a932e2008-01-04 22:32:30 +0000904// ArgExprs is optional - if it is present, the number of expressions
905// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +0000906ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor77328d12010-09-15 23:19:31 +0000907 ParsedType Receiver,
908 Selector Sel,
909 SourceLocation LBracLoc,
910 SourceLocation SelectorLoc,
911 SourceLocation RBracLoc,
912 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000913 TypeSourceInfo *ReceiverTypeInfo;
914 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
915 if (ReceiverType.isNull())
916 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor2725ca82010-04-21 19:57:20 +0000919 if (!ReceiverTypeInfo)
920 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
921
922 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorf49bb082010-04-22 17:01:48 +0000923 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000924 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000925}
926
927/// \brief Build an Objective-C instance message expression.
928///
929/// This routine takes care of both normal instance messages and
930/// instance messages to the superclass instance.
931///
932/// \param Receiver The expression that computes the object that will
933/// receive this message. This may be empty, in which case we are
934/// sending to the superclass instance and \p SuperLoc must be a valid
935/// source location.
936///
937/// \param ReceiverType The (static) type of the object receiving the
938/// message. When a \p Receiver expression is provided, this is the
939/// same type as that expression. For a superclass instance send, this
940/// is a pointer to the type of the superclass.
941///
942/// \param SuperLoc The location of the "super" keyword in a
943/// superclass instance message.
944///
945/// \param Sel The selector to which the message is being sent.
946///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000947/// \param Method The method that this instance message is invoking, if
948/// already known.
949///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000950/// \param LBracLoc The location of the opening square bracket ']'.
951///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000952/// \param RBrac The location of the closing square bracket ']'.
953///
954/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000955ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000956 QualType ReceiverType,
957 SourceLocation SuperLoc,
958 Selector Sel,
959 ObjCMethodDecl *Method,
960 SourceLocation LBracLoc,
961 SourceLocation SelectorLoc,
962 SourceLocation RBracLoc,
963 MultiExprArg ArgsIn) {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000964 // The location of the receiver.
965 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
966
967 if (LBracLoc.isInvalid()) {
968 Diag(Loc, diag::err_missing_open_square_message_send)
969 << FixItHint::CreateInsertion(Loc, "[");
970 LBracLoc = Loc;
971 }
972
Douglas Gregor2725ca82010-04-21 19:57:20 +0000973 // If we have a receiver expression, perform appropriate promotions
974 // and determine receiver type.
Douglas Gregor2725ca82010-04-21 19:57:20 +0000975 if (Receiver) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000976 if (Receiver->isTypeDependent()) {
977 // If the receiver is type-dependent, we can't type-check anything
978 // at this point. Build a dependent expression.
979 unsigned NumArgs = ArgsIn.size();
980 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
981 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
982 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCallf89e55a2010-11-18 06:31:45 +0000983 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000984 SelectorLoc, /*Method=*/0,
985 Args, NumArgs, RBracLoc));
Douglas Gregor92e986e2010-04-22 16:44:27 +0000986 }
987
Douglas Gregor2725ca82010-04-21 19:57:20 +0000988 // If necessary, apply function/array conversion to the receiver.
989 // C99 6.7.5.3p[7,8].
990 DefaultFunctionArrayLvalueConversion(Receiver);
991 ReceiverType = Receiver->getType();
992 }
993
Douglas Gregorf49bb082010-04-22 17:01:48 +0000994 if (!Method) {
995 // Handle messages to id.
Fariborz Jahanianba551982010-08-10 18:10:50 +0000996 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000997 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorf49bb082010-04-22 17:01:48 +0000998 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
999 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001000 SourceRange(LBracLoc, RBracLoc),
1001 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001002 if (!Method)
Douglas Gregor2725ca82010-04-21 19:57:20 +00001003 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001004 SourceRange(LBracLoc, RBracLoc),
1005 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001006 } else if (ReceiverType->isObjCClassType() ||
1007 ReceiverType->isObjCQualifiedClassType()) {
1008 // Handle messages to Class.
1009 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1010 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1011 // First check the public methods in the class interface.
1012 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Douglas Gregorf49bb082010-04-22 17:01:48 +00001014 if (!Method)
1015 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Douglas Gregor04badcf2010-04-21 00:45:42 +00001016
Douglas Gregorf49bb082010-04-22 17:01:48 +00001017 // FIXME: if we still haven't found a method, we need to look in
1018 // protocols (if we have qualifiers).
Steve Naroff6b9dfd42009-03-04 15:11:40 +00001019 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001020 if (Method && DiagnoseUseOfDecl(Method, Loc))
1021 return ExprError();
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +00001022 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001023 if (!Method) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001024 // If not messaging 'self', look for any factory method named 'Sel'.
1025 if (!Receiver || !isSelfExpr(Receiver)) {
1026 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001027 SourceRange(LBracLoc, RBracLoc),
1028 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001029 if (!Method) {
1030 // If no class (factory) method was found, check if an _instance_
1031 // method of the same name exists in the root class only.
Douglas Gregor2725ca82010-04-21 19:57:20 +00001032 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001033 SourceRange(LBracLoc, RBracLoc),
1034 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001035 if (Method)
1036 if (const ObjCInterfaceDecl *ID =
1037 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1038 if (ID->getSuperClass())
1039 Diag(Loc, diag::warn_root_inst_method_not_found)
1040 << Sel << SourceRange(LBracLoc, RBracLoc);
1041 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001042 }
1043 }
1044 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001045 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001046 ObjCInterfaceDecl* ClassDecl = 0;
1047
1048 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1049 // long as one of the protocols implements the selector (if not, warn).
1050 if (const ObjCObjectPointerType *QIdTy
1051 = ReceiverType->getAsObjCQualifiedIdType()) {
1052 // Search protocols for instance methods.
1053 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
1054 E = QIdTy->qual_end(); I != E; ++I) {
1055 ObjCProtocolDecl *PDecl = *I;
1056 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
1057 break;
1058 // Since we aren't supporting "Class<foo>", look for a class method.
1059 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
1060 break;
1061 }
1062 } else if (const ObjCObjectPointerType *OCIType
1063 = ReceiverType->getAsObjCInterfacePointerType()) {
1064 // We allow sending a message to a pointer to an interface (an object).
1065 ClassDecl = OCIType->getInterfaceDecl();
1066 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1067 // faster than the following method (which can do *many* linear searches).
Sebastian Redldb9d2142010-08-02 23:18:59 +00001068 // The idea is to add class info to MethodPool.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001069 Method = ClassDecl->lookupInstanceMethod(Sel);
1070
1071 if (!Method) {
1072 // Search protocol qualifiers.
1073 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
1074 E = OCIType->qual_end(); QI != E; ++QI) {
1075 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
1076 break;
1077 }
1078 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001079 bool forwardClass = false;
Douglas Gregorf49bb082010-04-22 17:01:48 +00001080 if (!Method) {
1081 // If we have implementations in scope, check "private" methods.
1082 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1083
1084 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1085 // If we still haven't found a method, look in the global pool. This
1086 // behavior isn't very desirable, however we need it for GCC
1087 // compatibility. FIXME: should we deviate??
1088 if (OCIType->qual_empty()) {
1089 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001090 SourceRange(LBracLoc, RBracLoc));
1091 forwardClass = OCIType->getInterfaceDecl()->isForwardDecl();
1092 if (Method && !forwardClass)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001093 Diag(Loc, diag::warn_maynot_respond)
1094 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1095 }
1096 }
1097 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001098 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorf49bb082010-04-22 17:01:48 +00001099 return ExprError();
1100 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001101 (ReceiverType->isPointerType() ||
1102 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001103 // Implicitly convert integers and pointers to 'id' but emit a warning.
1104 Diag(Loc, diag::warn_bad_receiver_type)
1105 << ReceiverType
1106 << Receiver->getSourceRange();
1107 if (ReceiverType->isPointerType())
1108 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall2de56d12010-08-25 11:45:40 +00001109 CK_BitCast);
John McCall404cd162010-11-13 01:35:44 +00001110 else {
1111 // TODO: specialized warning on null receivers?
1112 bool IsNull = Receiver->isNullPointerConstant(Context,
1113 Expr::NPC_ValueDependentIsNull);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001114 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall404cd162010-11-13 01:35:44 +00001115 IsNull ? CK_NullToPointer : CK_IntegralToPointer);
1116 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001117 ReceiverType = Receiver->getType();
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001118 }
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001119 else if (getLangOptions().CPlusPlus &&
1120 !PerformContextuallyConvertToObjCId(Receiver)) {
1121 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1122 Receiver = ICE->getSubExpr();
1123 ReceiverType = Receiver->getType();
1124 }
John McCall9ae2f072010-08-23 23:25:46 +00001125 return BuildInstanceMessage(Receiver,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001126 ReceiverType,
1127 SuperLoc,
1128 Sel,
1129 Method,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001130 LBracLoc,
1131 SelectorLoc,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001132 RBracLoc,
1133 move(ArgsIn));
Douglas Gregorf49bb082010-04-22 17:01:48 +00001134 } else {
1135 // Reject other random receiver types (e.g. structs).
1136 Diag(Loc, diag::err_bad_receiver_type)
1137 << ReceiverType << Receiver->getSourceRange();
1138 return ExprError();
1139 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001140 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregor2725ca82010-04-21 19:57:20 +00001143 // Check the message arguments.
1144 unsigned NumArgs = ArgsIn.size();
1145 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1146 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001147 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001148 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1149 ReceiverType->isObjCQualifiedClassType());
1150 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCallf89e55a2010-11-18 06:31:45 +00001151 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001152 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001153
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001154 if (Method && !Method->getResultType()->isVoidType() &&
1155 RequireCompleteType(LBracLoc, Method->getResultType(),
1156 diag::err_illegal_message_expr_incomplete_type))
1157 return ExprError();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001158
Douglas Gregor2725ca82010-04-21 19:57:20 +00001159 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001160 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001161 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001162 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001163 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001164 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001165 Args, NumArgs, RBracLoc);
1166 else
John McCallf89e55a2010-11-18 06:31:45 +00001167 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001168 Receiver, Sel, SelectorLoc, Method,
1169 Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001170 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001171}
1172
1173// ActOnInstanceMessage - used for both unary and keyword messages.
1174// ArgExprs is optional - if it is present, the number of expressions
1175// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001176ExprResult Sema::ActOnInstanceMessage(Scope *S,
1177 Expr *Receiver,
1178 Selector Sel,
1179 SourceLocation LBracLoc,
1180 SourceLocation SelectorLoc,
1181 SourceLocation RBracLoc,
1182 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001183 if (!Receiver)
1184 return ExprError();
1185
John McCall9ae2f072010-08-23 23:25:46 +00001186 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001187 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001188 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001189}
Chris Lattnereca7be62008-04-07 05:30:13 +00001190