blob: c769b856cd98e8751776ccf6a854a32621b552db [file] [log] [blame]
Chris Lattnera3fc41d2008-01-04 22:32:30 +00001//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective-C expressions.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
John McCallcc14d1f2010-08-24 08:50:51 +000016#include "clang/Sema/Scope.h"
John McCall5f2d5562011-02-03 09:00:02 +000017#include "clang/Sema/ScopeInfo.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000018#include "clang/Sema/Initialization.h"
Chris Lattnera3fc41d2008-01-04 22:32:30 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/DeclObjC.h"
Steve Naroff021ca182008-05-29 21:12:08 +000021#include "clang/AST/ExprObjC.h"
Douglas Gregor0c78ad92010-04-21 19:57:20 +000022#include "clang/AST/TypeLoc.h"
Chris Lattner163ffd22009-02-18 06:48:40 +000023#include "llvm/ADT/SmallString.h"
Steve Naroff9527bbf2009-03-09 21:12:44 +000024#include "clang/Lex/Preprocessor.h"
25
Chris Lattnera3fc41d2008-01-04 22:32:30 +000026using namespace clang;
John McCall5f2d5562011-02-03 09:00:02 +000027using namespace sema;
Chris Lattnera3fc41d2008-01-04 22:32:30 +000028
John McCallfaf5fb42010-08-26 23:41:50 +000029ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
30 Expr **strings,
31 unsigned NumStrings) {
Chris Lattner163ffd22009-02-18 06:48:40 +000032 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
33
Chris Lattnerd7670d92009-02-18 06:13:04 +000034 // Most ObjC strings are formed out of a single piece. However, we *can*
35 // have strings formed out of multiple @ strings with multiple pptokens in
36 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
37 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner163ffd22009-02-18 06:48:40 +000038 StringLiteral *S = Strings[0];
Mike Stump11289f42009-09-09 15:08:12 +000039
Chris Lattnerd7670d92009-02-18 06:13:04 +000040 // If we have a multi-part string, merge it all together.
41 if (NumStrings != 1) {
Chris Lattnera3fc41d2008-01-04 22:32:30 +000042 // Concatenate objc strings.
Chris Lattner163ffd22009-02-18 06:48:40 +000043 llvm::SmallString<128> StrBuf;
44 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump11289f42009-09-09 15:08:12 +000045
Chris Lattner630970d2009-02-18 05:49:11 +000046 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner163ffd22009-02-18 06:48:40 +000047 S = Strings[i];
Mike Stump11289f42009-09-09 15:08:12 +000048
Chris Lattner163ffd22009-02-18 06:48:40 +000049 // ObjC strings can't be wide.
Chris Lattnerd7670d92009-02-18 06:13:04 +000050 if (S->isWide()) {
51 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
52 << S->getSourceRange();
53 return true;
54 }
Mike Stump11289f42009-09-09 15:08:12 +000055
Benjamin Kramer35b077e2010-08-17 12:54:38 +000056 // Append the string.
57 StrBuf += S->getString();
Mike Stump11289f42009-09-09 15:08:12 +000058
Chris Lattner163ffd22009-02-18 06:48:40 +000059 // Get the locations of the string tokens.
60 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Chris Lattnera3fc41d2008-01-04 22:32:30 +000061 }
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattner163ffd22009-02-18 06:48:40 +000063 // Create the aggregate string with the appropriate content and location
64 // information.
Anders Carlsson75245402011-04-14 00:40:03 +000065 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(),
66 /*Wide=*/false, /*Pascal=*/false,
Chris Lattnerf83b5af2009-02-18 06:40:38 +000067 Context.getPointerType(Context.CharTy),
Chris Lattner163ffd22009-02-18 06:48:40 +000068 &StrLocs[0], StrLocs.size());
Chris Lattnera3fc41d2008-01-04 22:32:30 +000069 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Chris Lattner6436fb62009-02-18 06:01:06 +000071 // Verify that this composite string is acceptable for ObjC strings.
72 if (CheckObjCString(S))
Chris Lattnera3fc41d2008-01-04 22:32:30 +000073 return true;
Chris Lattnerfffd6a72009-02-18 06:06:56 +000074
75 // Initialize the constant string interface lazily. This assumes
Steve Naroff54e59452009-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 Lattnerfffd6a72009-02-18 06:06:56 +000079 QualType Ty = Context.getObjCConstantStringInterface();
80 if (!Ty.isNull()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +000081 Ty = Context.getObjCObjectPointerType(Ty);
Fariborz Jahanian07317632010-04-23 23:19:04 +000082 } else if (getLangOptions().NoConstantCFStrings) {
Fariborz Jahanian50c925f2010-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 Jahanian07317632010-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 Lattner091f6982008-06-21 21:44:18 +0000104 } else {
Steve Naroff54e59452009-04-07 14:18:33 +0000105 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000106 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
107 LookupOrdinaryName);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000108 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
109 Context.setObjCConstantStringInterface(StrIF);
110 Ty = Context.getObjCConstantStringInterface();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000111 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000112 } else {
Steve Naroff54e59452009-04-07 14:18:33 +0000113 // If there is no NSString interface defined then treat constant
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000114 // strings as untyped objects and let the runtime figure it out later.
115 Ty = Context.getObjCIdType();
116 }
Chris Lattner091f6982008-06-21 21:44:18 +0000117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattnerd7670d92009-02-18 06:13:04 +0000119 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000120}
121
Mike Stump11289f42009-09-09 15:08:12 +0000122Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregorabd9e962010-04-20 15:39:42 +0000123 TypeSourceInfo *EncodedTypeInfo,
Anders Carlsson315d2292009-06-07 18:45:35 +0000124 SourceLocation RParenLoc) {
Douglas Gregorabd9e962010-04-20 15:39:42 +0000125 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlsson315d2292009-06-07 18:45:35 +0000126 QualType StrTy;
Mike Stump11289f42009-09-09 15:08:12 +0000127 if (EncodedType->isDependentType())
Anders Carlsson315d2292009-06-07 18:45:35 +0000128 StrTy = Context.DependentTy;
129 else {
130 std::string Str;
131 Context.getObjCEncodingForType(EncodedType, Str);
132
133 // The type of @encode is the same as the type of the corresponding string,
134 // which is an array type.
135 StrTy = Context.CharTy;
136 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCallc33dec32010-03-15 10:54:44 +0000137 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlsson315d2292009-06-07 18:45:35 +0000138 StrTy.addConst();
139 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
140 ArrayType::Normal, 0);
141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Douglas Gregorabd9e962010-04-20 15:39:42 +0000143 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlsson315d2292009-06-07 18:45:35 +0000144}
145
John McCallfaf5fb42010-08-26 23:41:50 +0000146ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
147 SourceLocation EncodeLoc,
148 SourceLocation LParenLoc,
149 ParsedType ty,
150 SourceLocation RParenLoc) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +0000151 // FIXME: Preserve type source info ?
Douglas Gregorabd9e962010-04-20 15:39:42 +0000152 TypeSourceInfo *TInfo;
153 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
154 if (!TInfo)
155 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
156 PP.getLocForEndOfToken(LParenLoc));
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000157
Douglas Gregorabd9e962010-04-20 15:39:42 +0000158 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000159}
160
John McCallfaf5fb42010-08-26 23:41:50 +0000161ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
162 SourceLocation AtLoc,
163 SourceLocation SelLoc,
164 SourceLocation LParenLoc,
165 SourceLocation RParenLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000166 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +0000167 SourceRange(LParenLoc, RParenLoc), false, false);
Fariborz Jahanian0571d9b2009-06-16 16:25:00 +0000168 if (!Method)
169 Method = LookupFactoryMethodInGlobalPool(Sel,
170 SourceRange(LParenLoc, RParenLoc));
171 if (!Method)
172 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
173
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000174 llvm::DenseMap<Selector, SourceLocation>::iterator Pos
175 = ReferencedSelectors.find(Sel);
176 if (Pos == ReferencedSelectors.end())
177 ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
178
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000179 QualType Ty = Context.getObjCSelType();
Daniel Dunbar45858d22010-02-03 20:11:42 +0000180 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000181}
182
John McCallfaf5fb42010-08-26 23:41:50 +0000183ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
184 SourceLocation AtLoc,
185 SourceLocation ProtoLoc,
186 SourceLocation LParenLoc,
187 SourceLocation RParenLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000188 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000189 if (!PDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000190 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000191 return true;
192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000194 QualType Ty = Context.getObjCProtoType();
195 if (Ty.isNull())
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000196 return true;
Steve Naroff7cae42b2009-07-10 23:34:53 +0000197 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnerfffd6a72009-02-18 06:06:56 +0000198 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000199}
200
John McCall5f2d5562011-02-03 09:00:02 +0000201/// Try to capture an implicit reference to 'self'.
202ObjCMethodDecl *Sema::tryCaptureObjCSelf() {
203 // Ignore block scopes: we can capture through them.
204 DeclContext *DC = CurContext;
205 while (true) {
206 if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext();
207 else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext();
208 else break;
209 }
210
211 // If we're not in an ObjC method, error out. Note that, unlike the
212 // C++ case, we don't require an instance method --- class methods
213 // still have a 'self', and we really do still need to capture it!
214 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
215 if (!method)
216 return 0;
217
218 ImplicitParamDecl *self = method->getSelfDecl();
219 assert(self && "capturing 'self' in non-definition?");
220
221 // Mark that we're closing on 'this' in all the block scopes, if applicable.
222 for (unsigned idx = FunctionScopes.size() - 1;
223 isa<BlockScopeInfo>(FunctionScopes[idx]);
John McCall351762c2011-02-07 10:33:21 +0000224 --idx) {
225 BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]);
226 unsigned &captureIndex = blockScope->CaptureMap[self];
227 if (captureIndex) break;
228
229 bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]);
230 blockScope->Captures.push_back(
231 BlockDecl::Capture(self, /*byref*/ false, nested, /*copy*/ 0));
232 captureIndex = blockScope->Captures.size(); // +1
233 }
John McCall5f2d5562011-02-03 09:00:02 +0000234
235 return method;
236}
237
238
Mike Stump11289f42009-09-09 15:08:12 +0000239bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
240 Selector Sel, ObjCMethodDecl *Method,
Chris Lattnere4b95692008-11-24 03:33:13 +0000241 bool isClassMessage,
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000242 SourceLocation lbrac, SourceLocation rbrac,
John McCall7decc9e2010-11-18 06:31:45 +0000243 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000244 if (!Method) {
Daniel Dunbar83876b42008-09-11 00:04:36 +0000245 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000246 for (unsigned i = 0; i != NumArgs; i++) {
247 if (Args[i]->isTypeDependent())
248 continue;
249
John Wiegley01296292011-04-08 18:41:53 +0000250 ExprResult Result = DefaultArgumentPromotion(Args[i]);
251 if (Result.isInvalid())
252 return true;
253 Args[i] = Result.take();
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000254 }
Daniel Dunbar83876b42008-09-11 00:04:36 +0000255
Chris Lattnere4b95692008-11-24 03:33:13 +0000256 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
257 diag::warn_inst_method_not_found;
258 Diag(lbrac, DiagID)
259 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000260 ReturnType = Context.getObjCIdType();
John McCall7decc9e2010-11-18 06:31:45 +0000261 VK = VK_RValue;
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000262 return false;
Daniel Dunbaraa9326c2008-09-11 00:01:56 +0000263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Douglas Gregor603d81b2010-07-13 08:18:22 +0000265 ReturnType = Method->getSendResultType();
John McCall7decc9e2010-11-18 06:31:45 +0000266 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump11289f42009-09-09 15:08:12 +0000267
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000268 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian60462092010-04-08 00:30:06 +0000269 // Method might have more arguments than selector indicates. This is due
270 // to addition of c-style arguments in method.
271 if (Method->param_size() > Sel.getNumArgs())
272 NumNamedArgs = Method->param_size();
273 // FIXME. This need be cleaned up.
274 if (NumArgs < NumNamedArgs) {
John McCall7decc9e2010-11-18 06:31:45 +0000275 Diag(lbrac, diag::err_typecheck_call_too_few_args)
276 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian60462092010-04-08 00:30:06 +0000277 return false;
278 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000279
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000280 bool IsError = false;
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000281 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000282 // We can't do any type-checking on a type-dependent argument.
283 if (Args[i]->isTypeDependent())
284 continue;
285
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000286 Expr *argExpr = Args[i];
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000287
Douglas Gregor6b7f12c2010-04-21 23:24:10 +0000288 ParmVarDecl *Param = Method->param_begin()[i];
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000289 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump11289f42009-09-09 15:08:12 +0000290
Douglas Gregor6b7f12c2010-04-21 23:24:10 +0000291 if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
292 Param->getType(),
293 PDiag(diag::err_call_incomplete_argument)
294 << argExpr->getSourceRange()))
295 return true;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000296
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +0000297 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
298 Param);
John McCallc3007a22010-10-26 07:05:15 +0000299 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor6b7f12c2010-04-21 23:24:10 +0000300 if (ArgE.isInvalid())
301 IsError = true;
302 else
303 Args[i] = ArgE.takeAs<Expr>();
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000304 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000305
306 // Promote additional arguments to variadic methods.
307 if (Method->isVariadic()) {
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000308 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
309 if (Args[i]->isTypeDependent())
310 continue;
311
John Wiegley01296292011-04-08 18:41:53 +0000312 ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
313 IsError |= Arg.isInvalid();
314 Args[i] = Arg.take();
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000315 }
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000316 } else {
317 // Check for extra arguments to non-variadic methods.
318 if (NumArgs != NumNamedArgs) {
Mike Stump11289f42009-09-09 15:08:12 +0000319 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +0000320 diag::err_typecheck_call_too_many_args)
Eric Christopher2a5aaff2010-04-16 04:56:46 +0000321 << 2 /*method*/ << NumNamedArgs << NumArgs
322 << Method->getSourceRange()
Chris Lattner3b054132008-11-19 05:08:23 +0000323 << SourceRange(Args[NumNamedArgs]->getLocStart(),
324 Args[NumArgs-1]->getLocEnd());
Daniel Dunbarce05c8e2008-09-11 00:50:25 +0000325 }
326 }
327
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000328 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Chris Lattnera8a7d0f2009-04-12 08:11:20 +0000329 return IsError;
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000330}
331
Steve Naroff3f49fee2009-03-04 15:11:40 +0000332bool Sema::isSelfExpr(Expr *RExpr) {
Fariborz Jahanianb3b1e172011-03-27 19:53:47 +0000333 // 'self' is objc 'self' in an objc method only.
Fariborz Jahaniand0d31bf2011-03-28 16:23:34 +0000334 DeclContext *DC = CurContext;
335 while (isa<BlockDecl>(DC))
336 DC = DC->getParent();
337 if (DC && !isa<ObjCMethodDecl>(DC))
Fariborz Jahanianb3b1e172011-03-27 19:53:47 +0000338 return false;
John McCall34376a62010-12-04 03:47:34 +0000339 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
340 if (ICE->getCastKind() == CK_LValueToRValue)
341 RExpr = ICE->getSubExpr();
Steve Naroff3f49fee2009-03-04 15:11:40 +0000342 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
343 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
344 return true;
345 return false;
346}
347
Steve Naroffb162f172009-02-26 15:55:06 +0000348// Helper method for ActOnClassMethod/ActOnInstanceMethod.
349// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000350// If failed, then we search in class's root for an instance method.
Steve Naroffb162f172009-02-26 15:55:06 +0000351// Returns 0 if no method is found.
Steve Naroffed031702009-03-08 18:56:13 +0000352ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Naroffb162f172009-02-26 15:55:06 +0000353 ObjCInterfaceDecl *ClassDecl) {
354 ObjCMethodDecl *Method = 0;
Steve Naroffed031702009-03-08 18:56:13 +0000355 // lookup in class and all superclasses
356 while (ClassDecl && !Method) {
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000357 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000358 Method = ImpDecl->getClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000359
Steve Naroffed031702009-03-08 18:56:13 +0000360 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000361 if (!Method)
362 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000363
Steve Naroffed031702009-03-08 18:56:13 +0000364 // Before we give up, check if the selector is an instance method.
365 // But only in the root. This matches gcc's behaviour and what the
366 // runtime expects.
367 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000368 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000369 // Look through local category implementations associated
Steve Naroffed031702009-03-08 18:56:13 +0000370 // with the root class.
Mike Stump11289f42009-09-09 15:08:12 +0000371 if (!Method)
Steve Naroffed031702009-03-08 18:56:13 +0000372 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Steve Naroffed031702009-03-08 18:56:13 +0000375 ClassDecl = ClassDecl->getSuperClass();
Steve Naroffb162f172009-02-26 15:55:06 +0000376 }
Steve Naroffed031702009-03-08 18:56:13 +0000377 return Method;
378}
379
380ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
381 ObjCInterfaceDecl *ClassDecl) {
382 ObjCMethodDecl *Method = 0;
383 while (ClassDecl && !Method) {
384 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000385 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000386 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump11289f42009-09-09 15:08:12 +0000387
Steve Naroffed031702009-03-08 18:56:13 +0000388 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000389 if (!Method)
390 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroffed031702009-03-08 18:56:13 +0000391 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian4f4de6c2009-03-04 18:15:57 +0000392 }
Steve Naroffb162f172009-02-26 15:55:06 +0000393 return Method;
394}
395
Fariborz Jahanian3dc11ad2011-03-09 20:18:06 +0000396/// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
397/// list of a qualified objective pointer type.
398ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
399 const ObjCObjectPointerType *OPT,
400 bool Instance)
401{
402 ObjCMethodDecl *MD = 0;
403 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
404 E = OPT->qual_end(); I != E; ++I) {
405 ObjCProtocolDecl *PROTO = (*I);
406 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
407 return MD;
408 }
409 }
410 return 0;
411}
412
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000413/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
414/// objective C interface. This is a property reference expression.
John McCalldadc5752010-08-24 06:29:42 +0000415ExprResult Sema::
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000416HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattner90c58fa2010-04-11 07:51:10 +0000417 Expr *BaseExpr, DeclarationName MemberName,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000418 SourceLocation MemberLoc,
419 SourceLocation SuperLoc, QualType SuperType,
420 bool Super) {
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000421 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
422 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
423 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
424
Fariborz Jahanian7cabbe02010-12-16 00:56:28 +0000425 if (IFace->isForwardDecl()) {
426 Diag(MemberLoc, diag::err_property_not_found_forward_class)
427 << MemberName << QualType(OPT, 0);
428 Diag(IFace->getLocation(), diag::note_forward_class);
429 return ExprError();
430 }
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000431 // Search for a declared property first.
432 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
433 // Check whether we can reference this property.
434 if (DiagnoseUseOfDecl(PD, MemberLoc))
435 return ExprError();
436 QualType ResTy = PD->getType();
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000437 ResTy = ResTy.getNonLValueExprType(Context);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000438 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
439 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
440 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
John McCall7decc9e2010-11-18 06:31:45 +0000441 ResTy = Getter->getResultType();
442
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000443 if (Super)
444 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCall7decc9e2010-11-18 06:31:45 +0000445 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000446 MemberLoc,
447 SuperLoc, SuperType));
448 else
449 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCall7decc9e2010-11-18 06:31:45 +0000450 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000451 MemberLoc, BaseExpr));
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000452 }
453 // Check protocols on qualified interfaces.
454 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
455 E = OPT->qual_end(); I != E; ++I)
456 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
457 // Check whether we can reference this property.
458 if (DiagnoseUseOfDecl(PD, MemberLoc))
459 return ExprError();
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000460 if (Super)
John McCall7decc9e2010-11-18 06:31:45 +0000461 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
462 VK_LValue,
463 OK_ObjCProperty,
464 MemberLoc,
465 SuperLoc, SuperType));
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000466 else
467 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000468 VK_LValue,
469 OK_ObjCProperty,
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000470 MemberLoc,
471 BaseExpr));
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000472 }
473 // If that failed, look for an "implicit" property by seeing if the nullary
474 // selector is implemented.
475
476 // FIXME: The logic for looking up nullary and unary selectors should be
477 // shared with the code in ActOnInstanceMessage.
478
479 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
480 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
Fariborz Jahanianb296e332011-03-09 22:17:12 +0000481
482 // May be founf in property's qualified list.
483 if (!Getter)
484 Getter = LookupMethodInQualifiedType(Sel, OPT, true);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000485
486 // If this reference is in an @implementation, check for 'private' methods.
487 if (!Getter)
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000488 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000489
490 // Look through local category implementations associated with the class.
491 if (!Getter)
492 Getter = IFace->getCategoryInstanceMethod(Sel);
493 if (Getter) {
494 // Check if we can reference this property.
495 if (DiagnoseUseOfDecl(Getter, MemberLoc))
496 return ExprError();
497 }
498 // If we found a getter then this may be a valid dot-reference, we
499 // will look for the matching setter, in case it is needed.
500 Selector SetterSel =
501 SelectorTable::constructSetterName(PP.getIdentifierTable(),
502 PP.getSelectorTable(), Member);
503 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
Fariborz Jahanianb296e332011-03-09 22:17:12 +0000504
505 // May be founf in property's qualified list.
506 if (!Setter)
507 Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
508
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000509 if (!Setter) {
510 // If this reference is in an @implementation, also check for 'private'
511 // methods.
Fariborz Jahanianecbbb6e2010-12-03 23:37:08 +0000512 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000513 }
514 // Look through local category implementations associated with the class.
515 if (!Setter)
516 Setter = IFace->getCategoryInstanceMethod(SetterSel);
Fariborz Jahanianb296e332011-03-09 22:17:12 +0000517
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000518 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
519 return ExprError();
520
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000521 if (Getter || Setter) {
522 QualType PType;
523 if (Getter)
524 PType = Getter->getSendResultType();
525 else {
526 ParmVarDecl *ArgDecl = *Setter->param_begin();
527 PType = ArgDecl->getType();
528 }
529
John McCall4bc41ae2010-11-18 19:01:18 +0000530 ExprValueKind VK = VK_LValue;
531 ExprObjectKind OK = OK_ObjCProperty;
532 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
533 PType->isVoidType())
534 VK = VK_RValue, OK = OK_Ordinary;
535
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000536 if (Super)
John McCallb7bd14f2010-12-02 01:19:52 +0000537 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
538 PType, VK, OK,
539 MemberLoc,
540 SuperLoc, SuperType));
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000541 else
John McCallb7bd14f2010-12-02 01:19:52 +0000542 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
543 PType, VK, OK,
544 MemberLoc, BaseExpr));
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000545
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000546 }
547
548 // Attempt to correct for typos in property names.
549 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000550 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000551 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattner90c58fa2010-04-11 07:51:10 +0000552 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000553 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattner90c58fa2010-04-11 07:51:10 +0000554 << MemberName << QualType(OPT, 0) << TypoResult
555 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000556 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
557 Diag(Property->getLocation(), diag::note_previous_decl)
558 << Property->getDeclName();
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000559 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
560 SuperLoc, SuperType, Super);
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000561 }
Fariborz Jahanian05d389f2011-02-17 01:26:14 +0000562 ObjCInterfaceDecl *ClassDeclared;
563 if (ObjCIvarDecl *Ivar =
564 IFace->lookupInstanceVariable(Member, ClassDeclared)) {
565 QualType T = Ivar->getType();
566 if (const ObjCObjectPointerType * OBJPT =
567 T->getAsObjCInterfacePointerType()) {
568 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
569 if (ObjCInterfaceDecl *IFace = IFaceT->getDecl())
570 if (IFace->isForwardDecl()) {
571 Diag(MemberLoc, diag::err_property_not_as_forward_class)
Fariborz Jahanian5fc74802011-02-17 17:30:05 +0000572 << MemberName << IFace;
Fariborz Jahanian05d389f2011-02-17 01:26:14 +0000573 Diag(IFace->getLocation(), diag::note_forward_class);
574 return ExprError();
575 }
576 }
577 }
Chris Lattner90c58fa2010-04-11 07:51:10 +0000578
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000579 Diag(MemberLoc, diag::err_property_not_found)
580 << MemberName << QualType(OPT, 0);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000581 if (Setter)
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000582 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000583 << MemberName << BaseExpr->getSourceRange();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000584 return ExprError();
Chris Lattner2b1ca5f2010-04-11 07:45:24 +0000585}
586
587
588
John McCalldadc5752010-08-24 06:29:42 +0000589ExprResult Sema::
Chris Lattnera36ec422010-04-11 08:28:14 +0000590ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
591 IdentifierInfo &propertyName,
592 SourceLocation receiverNameLoc,
593 SourceLocation propertyNameLoc) {
Mike Stump11289f42009-09-09 15:08:12 +0000594
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000595 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000596 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
597 receiverNameLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +0000598 if (IFace == 0) {
599 // If the "receiver" is 'super' in a method, handle it as an expression-like
600 // property reference.
John McCall5f2d5562011-02-03 09:00:02 +0000601 if (receiverNamePtr->isStr("super")) {
602 if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
Chris Lattnera36ec422010-04-11 08:28:14 +0000603 if (CurMethod->isInstanceMethod()) {
604 QualType T =
605 Context.getObjCInterfaceType(CurMethod->getClassInterface());
606 T = Context.getObjCObjectPointerType(T);
Chris Lattnera36ec422010-04-11 08:28:14 +0000607
608 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000609 /*BaseExpr*/0, &propertyName,
610 propertyNameLoc,
611 receiverNameLoc, T, true);
Chris Lattnera36ec422010-04-11 08:28:14 +0000612 }
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattnera36ec422010-04-11 08:28:14 +0000614 // Otherwise, if this is a class method, try dispatching to our
615 // superclass.
616 IFace = CurMethod->getClassInterface()->getSuperClass();
617 }
John McCall5f2d5562011-02-03 09:00:02 +0000618 }
Chris Lattnera36ec422010-04-11 08:28:14 +0000619
620 if (IFace == 0) {
621 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
622 return ExprError();
623 }
624 }
625
626 // Search for a declared property first.
Steve Naroff9527bbf2009-03-09 21:12:44 +0000627 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000628 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000629
630 // If this reference is in an @implementation, check for 'private' methods.
631 if (!Getter)
632 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
633 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000634 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000635 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000636
637 if (Getter) {
638 // FIXME: refactor/share with ActOnMemberReference().
639 // Check if we can reference this property.
640 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
641 return ExprError();
642 }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Steve Naroff9527bbf2009-03-09 21:12:44 +0000644 // Look for the matching setter, in case it is needed.
Mike Stump11289f42009-09-09 15:08:12 +0000645 Selector SetterSel =
646 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Naroffc7597f82009-03-10 17:24:38 +0000647 PP.getSelectorTable(), &propertyName);
Mike Stump11289f42009-09-09 15:08:12 +0000648
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000649 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000650 if (!Setter) {
651 // If this reference is in an @implementation, also check for 'private'
652 // methods.
653 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
654 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +0000655 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000656 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000657 }
658 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1559d67b2009-07-21 00:06:20 +0000659 if (!Setter)
660 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000661
662 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
663 return ExprError();
664
665 if (Getter || Setter) {
666 QualType PType;
667
John McCall4bc41ae2010-11-18 19:01:18 +0000668 ExprValueKind VK = VK_LValue;
669 if (Getter) {
Douglas Gregor603d81b2010-07-13 08:18:22 +0000670 PType = Getter->getSendResultType();
John McCall4bc41ae2010-11-18 19:01:18 +0000671 if (!getLangOptions().CPlusPlus &&
672 !PType.hasQualifiers() && PType->isVoidType())
673 VK = VK_RValue;
674 } else {
Steve Naroff9527bbf2009-03-09 21:12:44 +0000675 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
676 E = Setter->param_end(); PI != E; ++PI)
677 PType = (*PI)->getType();
John McCall4bc41ae2010-11-18 19:01:18 +0000678 VK = VK_LValue;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000679 }
John McCall4bc41ae2010-11-18 19:01:18 +0000680
681 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
682
John McCallb7bd14f2010-12-02 01:19:52 +0000683 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
684 PType, VK, OK,
685 propertyNameLoc,
686 receiverNameLoc, IFace));
Steve Naroff9527bbf2009-03-09 21:12:44 +0000687 }
688 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
689 << &propertyName << Context.getObjCInterfaceType(IFace));
690}
691
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000692Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregore5798dc2010-04-21 20:38:13 +0000693 IdentifierInfo *Name,
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000694 SourceLocation NameLoc,
695 bool IsSuper,
Douglas Gregore5798dc2010-04-21 20:38:13 +0000696 bool HasTrailingDot,
John McCallba7bf592010-08-24 05:47:05 +0000697 ParsedType &ReceiverType) {
698 ReceiverType = ParsedType();
Douglas Gregore5798dc2010-04-21 20:38:13 +0000699
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000700 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor57756ea2010-10-14 22:11:03 +0000701 // messaging super. If the identifier is "super" and there is a
702 // trailing dot, it's an instance message.
703 if (IsSuper && S->isInObjcMethodScope())
704 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000705
706 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
707 LookupName(Result, S);
708
709 switch (Result.getResultKind()) {
710 case LookupResult::NotFound:
Douglas Gregorca7136b2010-04-19 20:09:36 +0000711 // Normal name lookup didn't find anything. If we're in an
712 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor57756ea2010-10-14 22:11:03 +0000713 // FIXME: This is a hack. Ivar lookup should be part of normal
714 // lookup.
Douglas Gregorca7136b2010-04-19 20:09:36 +0000715 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
716 ObjCInterfaceDecl *ClassDeclared;
717 if (Method->getClassInterface()->lookupInstanceVariable(Name,
718 ClassDeclared))
719 return ObjCInstanceMessage;
720 }
Douglas Gregor57756ea2010-10-14 22:11:03 +0000721
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000722 // Break out; we'll perform typo correction below.
723 break;
724
725 case LookupResult::NotFoundInCurrentInstantiation:
726 case LookupResult::FoundOverloaded:
727 case LookupResult::FoundUnresolvedValue:
728 case LookupResult::Ambiguous:
729 Result.suppressDiagnostics();
730 return ObjCInstanceMessage;
731
732 case LookupResult::Found: {
Fariborz Jahanian14889fc2011-02-08 00:23:07 +0000733 // If the identifier is a class or not, and there is a trailing dot,
734 // it's an instance message.
735 if (HasTrailingDot)
736 return ObjCInstanceMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000737 // We found something. If it's a type, then we have a class
738 // message. Otherwise, it's an instance message.
739 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregore5798dc2010-04-21 20:38:13 +0000740 QualType T;
741 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
742 T = Context.getObjCInterfaceType(Class);
743 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
744 T = Context.getTypeDeclType(Type);
745 else
746 return ObjCInstanceMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000747
Douglas Gregore5798dc2010-04-21 20:38:13 +0000748 // We have a class message, and T is the type we're
749 // messaging. Build source-location information for it.
750 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallba7bf592010-08-24 05:47:05 +0000751 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregore5798dc2010-04-21 20:38:13 +0000752 return ObjCClassMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000753 }
754 }
755
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000756 // Determine our typo-correction context.
757 CorrectTypoContext CTC = CTC_Expression;
758 if (ObjCMethodDecl *Method = getCurMethodDecl())
759 if (Method->getClassInterface() &&
760 Method->getClassInterface()->getSuperClass())
761 CTC = CTC_ObjCMessageReceiver;
762
763 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
764 if (Result.isSingleResult()) {
765 // If we found a declaration, correct when it refers to an Objective-C
766 // class.
767 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregore5798dc2010-04-21 20:38:13 +0000768 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000769 Diag(NameLoc, diag::err_unknown_receiver_suggest)
770 << Name << Result.getLookupName()
771 << FixItHint::CreateReplacement(SourceRange(NameLoc),
772 ND->getNameAsString());
773 Diag(ND->getLocation(), diag::note_previous_decl)
774 << Corrected;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000775
Douglas Gregore5798dc2010-04-21 20:38:13 +0000776 QualType T = Context.getObjCInterfaceType(Class);
777 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallba7bf592010-08-24 05:47:05 +0000778 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000779 return ObjCClassMessage;
780 }
781 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
782 Corrected.getAsIdentifierInfo()->isStr("super")) {
783 // If we've found the keyword "super", this is a send to super.
784 Diag(NameLoc, diag::err_unknown_receiver_suggest)
785 << Name << Corrected
786 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
Douglas Gregor280e1ee2010-04-14 20:04:41 +0000787 return ObjCSuperMessage;
Douglas Gregor8aa4ebf2010-04-14 02:46:37 +0000788 }
789 }
790
791 // Fall back: let the parser try to parse it as an instance message.
792 return ObjCInstanceMessage;
793}
Steve Naroff9527bbf2009-03-09 21:12:44 +0000794
John McCalldadc5752010-08-24 06:29:42 +0000795ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000796 SourceLocation SuperLoc,
797 Selector Sel,
798 SourceLocation LBracLoc,
799 SourceLocation SelectorLoc,
800 SourceLocation RBracLoc,
801 MultiExprArg Args) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000802 // Determine whether we are inside a method or not.
John McCall5f2d5562011-02-03 09:00:02 +0000803 ObjCMethodDecl *Method = tryCaptureObjCSelf();
Douglas Gregor4fdba132010-04-21 20:01:04 +0000804 if (!Method) {
805 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
806 return ExprError();
807 }
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000808
Douglas Gregor4fdba132010-04-21 20:01:04 +0000809 ObjCInterfaceDecl *Class = Method->getClassInterface();
810 if (!Class) {
811 Diag(SuperLoc, diag::error_no_super_class_message)
812 << Method->getDeclName();
813 return ExprError();
814 }
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000815
Douglas Gregor4fdba132010-04-21 20:01:04 +0000816 ObjCInterfaceDecl *Super = Class->getSuperClass();
817 if (!Super) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000818 // The current class does not have a superclass.
Ted Kremenek499897b2011-01-23 17:21:34 +0000819 Diag(SuperLoc, diag::error_root_class_cannot_use_super)
820 << Class->getIdentifier();
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000821 return ExprError();
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000822 }
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000823
Douglas Gregor4fdba132010-04-21 20:01:04 +0000824 // We are in a method whose class has a superclass, so 'super'
825 // is acting as a keyword.
826 if (Method->isInstanceMethod()) {
827 // Since we are in an instance method, this is an instance
828 // message to the superclass instance.
829 QualType SuperTy = Context.getObjCInterfaceType(Super);
830 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCallb268a282010-08-23 23:25:46 +0000831 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000832 Sel, /*Method=*/0,
833 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000834 }
Douglas Gregor4fdba132010-04-21 20:01:04 +0000835
836 // Since we are in a class method, this is a class message to
837 // the superclass.
838 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
839 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000840 SuperLoc, Sel, /*Method=*/0,
841 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000842}
843
844/// \brief Build an Objective-C class message expression.
845///
846/// This routine takes care of both normal class messages and
847/// class messages to the superclass.
848///
849/// \param ReceiverTypeInfo Type source information that describes the
850/// receiver of this message. This may be NULL, in which case we are
851/// sending to the superclass and \p SuperLoc must be a valid source
852/// location.
853
854/// \param ReceiverType The type of the object receiving the
855/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
856/// type as that refers to. For a superclass send, this is the type of
857/// the superclass.
858///
859/// \param SuperLoc The location of the "super" keyword in a
860/// superclass message.
861///
862/// \param Sel The selector to which the message is being sent.
863///
Douglas Gregorb5186b12010-04-22 17:01:48 +0000864/// \param Method The method that this class message is invoking, if
865/// already known.
866///
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000867/// \param LBracLoc The location of the opening square bracket ']'.
868///
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000869/// \param RBrac The location of the closing square bracket ']'.
870///
871/// \param Args The message arguments.
John McCalldadc5752010-08-24 06:29:42 +0000872ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000873 QualType ReceiverType,
874 SourceLocation SuperLoc,
875 Selector Sel,
876 ObjCMethodDecl *Method,
877 SourceLocation LBracLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000878 SourceLocation SelectorLoc,
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000879 SourceLocation RBracLoc,
880 MultiExprArg ArgsIn) {
881 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000882 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000883 if (LBracLoc.isInvalid()) {
884 Diag(Loc, diag::err_missing_open_square_message_send)
885 << FixItHint::CreateInsertion(Loc, "[");
886 LBracLoc = Loc;
887 }
888
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000889 if (ReceiverType->isDependentType()) {
890 // If the receiver type is dependent, we can't type-check anything
891 // at this point. Build a dependent expression.
892 unsigned NumArgs = ArgsIn.size();
893 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
894 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCall7decc9e2010-11-18 06:31:45 +0000895 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
896 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000897 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregorc298ffc2010-04-22 16:44:27 +0000898 Args, NumArgs, RBracLoc));
899 }
Chris Lattnerc2ebb032010-04-12 05:38:43 +0000900
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000901 // Find the class to which we are sending this message.
902 ObjCInterfaceDecl *Class = 0;
John McCall8b07ec22010-05-15 11:32:37 +0000903 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
904 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000905 Diag(Loc, diag::err_invalid_receiver_class_message)
906 << ReceiverType;
907 return ExprError();
Steve Naroffe2177fb2008-07-25 19:39:00 +0000908 }
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000909 assert(Class && "We don't know which class we're messaging?");
Fariborz Jahanian08891f52011-03-08 19:12:46 +0000910 (void)DiagnoseUseOfDecl(Class, Loc);
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000911 // Find the method we are messaging.
Douglas Gregorb5186b12010-04-22 17:01:48 +0000912 if (!Method) {
913 if (Class->isForwardDecl()) {
914 // A forward class used in messaging is treated as a 'Class'
915 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
916 Method = LookupFactoryMethodInGlobalPool(Sel,
917 SourceRange(LBracLoc, RBracLoc));
918 if (Method)
919 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
920 << Method->getDeclName();
921 }
922 if (!Method)
923 Method = Class->lookupClassMethod(Sel);
924
925 // If we have an implementation in scope, check "private" methods.
926 if (!Method)
927 Method = LookupPrivateClassMethod(Sel, Class);
928
929 if (Method && DiagnoseUseOfDecl(Method, Loc))
930 return ExprError();
Fariborz Jahanian1bd844d2009-05-08 23:02:36 +0000931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000933 // Check the argument types and determine the result type.
934 QualType ReturnType;
John McCall7decc9e2010-11-18 06:31:45 +0000935 ExprValueKind VK = VK_RValue;
936
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000937 unsigned NumArgs = ArgsIn.size();
938 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
939 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCall7decc9e2010-11-18 06:31:45 +0000940 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000941 return ExprError();
Ted Kremeneka3a37ae2008-06-24 15:50:53 +0000942
Douglas Gregoraec93c62011-01-11 03:23:19 +0000943 if (Method && !Method->getResultType()->isVoidType() &&
944 RequireCompleteType(LBracLoc, Method->getResultType(),
945 diag::err_illegal_message_expr_incomplete_type))
946 return ExprError();
947
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000948 // Construct the appropriate ObjCMessageExpr.
Douglas Gregoraae38d62010-05-22 05:17:18 +0000949 Expr *Result;
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000950 if (SuperLoc.isValid())
John McCall7decc9e2010-11-18 06:31:45 +0000951 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregoraae38d62010-05-22 05:17:18 +0000952 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000953 ReceiverType, Sel, SelectorLoc,
954 Method, Args, NumArgs, RBracLoc);
Douglas Gregoraae38d62010-05-22 05:17:18 +0000955 else
John McCall7decc9e2010-11-18 06:31:45 +0000956 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000957 ReceiverTypeInfo, Sel, SelectorLoc,
958 Method, Args, NumArgs, RBracLoc);
Douglas Gregoraae38d62010-05-22 05:17:18 +0000959 return MaybeBindToTemporary(Result);
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000960}
961
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000962// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattnera3fc41d2008-01-04 22:32:30 +0000963// ArgExprs is optional - if it is present, the number of expressions
964// is obtained from Sel.getNumArgs().
John McCalldadc5752010-08-24 06:29:42 +0000965ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor3e972002010-09-15 23:19:31 +0000966 ParsedType Receiver,
967 Selector Sel,
968 SourceLocation LBracLoc,
969 SourceLocation SelectorLoc,
970 SourceLocation RBracLoc,
971 MultiExprArg Args) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000972 TypeSourceInfo *ReceiverTypeInfo;
973 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
974 if (ReceiverType.isNull())
975 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000976
Mike Stump11289f42009-09-09 15:08:12 +0000977
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000978 if (!ReceiverTypeInfo)
979 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
980
981 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorb5186b12010-04-22 17:01:48 +0000982 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +0000983 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000984}
985
986/// \brief Build an Objective-C instance message expression.
987///
988/// This routine takes care of both normal instance messages and
989/// instance messages to the superclass instance.
990///
991/// \param Receiver The expression that computes the object that will
992/// receive this message. This may be empty, in which case we are
993/// sending to the superclass instance and \p SuperLoc must be a valid
994/// source location.
995///
996/// \param ReceiverType The (static) type of the object receiving the
997/// message. When a \p Receiver expression is provided, this is the
998/// same type as that expression. For a superclass instance send, this
999/// is a pointer to the type of the superclass.
1000///
1001/// \param SuperLoc The location of the "super" keyword in a
1002/// superclass instance message.
1003///
1004/// \param Sel The selector to which the message is being sent.
1005///
Douglas Gregorb5186b12010-04-22 17:01:48 +00001006/// \param Method The method that this instance message is invoking, if
1007/// already known.
1008///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001009/// \param LBracLoc The location of the opening square bracket ']'.
1010///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001011/// \param RBrac The location of the closing square bracket ']'.
1012///
1013/// \param Args The message arguments.
John McCalldadc5752010-08-24 06:29:42 +00001014ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001015 QualType ReceiverType,
1016 SourceLocation SuperLoc,
1017 Selector Sel,
1018 ObjCMethodDecl *Method,
1019 SourceLocation LBracLoc,
1020 SourceLocation SelectorLoc,
1021 SourceLocation RBracLoc,
1022 MultiExprArg ArgsIn) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001023 // The location of the receiver.
1024 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
1025
1026 if (LBracLoc.isInvalid()) {
1027 Diag(Loc, diag::err_missing_open_square_message_send)
1028 << FixItHint::CreateInsertion(Loc, "[");
1029 LBracLoc = Loc;
1030 }
1031
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001032 // If we have a receiver expression, perform appropriate promotions
1033 // and determine receiver type.
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001034 if (Receiver) {
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001035 if (Receiver->isTypeDependent()) {
1036 // If the receiver is type-dependent, we can't type-check anything
1037 // at this point. Build a dependent expression.
1038 unsigned NumArgs = ArgsIn.size();
1039 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1040 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
1041 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCall7decc9e2010-11-18 06:31:45 +00001042 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001043 SelectorLoc, /*Method=*/0,
1044 Args, NumArgs, RBracLoc));
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001045 }
1046
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001047 // If necessary, apply function/array conversion to the receiver.
1048 // C99 6.7.5.3p[7,8].
John Wiegley01296292011-04-08 18:41:53 +00001049 ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
1050 if (Result.isInvalid())
1051 return ExprError();
1052 Receiver = Result.take();
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001053 ReceiverType = Receiver->getType();
1054 }
1055
Douglas Gregorb5186b12010-04-22 17:01:48 +00001056 if (!Method) {
1057 // Handle messages to id.
Fariborz Jahanian32e59ba2010-08-10 18:10:50 +00001058 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001059 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorb5186b12010-04-22 17:01:48 +00001060 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
1061 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001062 SourceRange(LBracLoc, RBracLoc),
1063 receiverIsId);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001064 if (!Method)
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001065 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001066 SourceRange(LBracLoc, RBracLoc),
1067 receiverIsId);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001068 } else if (ReceiverType->isObjCClassType() ||
1069 ReceiverType->isObjCQualifiedClassType()) {
1070 // Handle messages to Class.
Fariborz Jahanian3b9819b2011-04-06 18:40:08 +00001071 // We allow sending a message to a qualified Class ("Class<foo>"), which
1072 // is ok as long as one of the protocols implements the selector (if not, warn).
1073 if (const ObjCObjectPointerType *QClassTy
1074 = ReceiverType->getAsObjCQualifiedClassType()) {
1075 // Search protocols for class methods.
1076 Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
1077 if (!Method) {
1078 Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
1079 // warn if instance method found for a Class message.
1080 if (Method) {
1081 Diag(Loc, diag::warn_instance_method_on_class_found)
1082 << Method->getSelector() << Sel;
1083 Diag(Method->getLocation(), diag::note_method_declared_at);
1084 }
Steve Naroff3f49fee2009-03-04 15:11:40 +00001085 }
Fariborz Jahanian3b9819b2011-04-06 18:40:08 +00001086 } else {
1087 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1088 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1089 // First check the public methods in the class interface.
1090 Method = ClassDecl->lookupClassMethod(Sel);
1091
1092 if (!Method)
1093 Method = LookupPrivateClassMethod(Sel, ClassDecl);
1094 }
1095 if (Method && DiagnoseUseOfDecl(Method, Loc))
1096 return ExprError();
1097 }
1098 if (!Method) {
1099 // If not messaging 'self', look for any factory method named 'Sel'.
1100 if (!Receiver || !isSelfExpr(Receiver)) {
1101 Method = LookupFactoryMethodInGlobalPool(Sel,
1102 SourceRange(LBracLoc, RBracLoc),
1103 true);
1104 if (!Method) {
1105 // If no class (factory) method was found, check if an _instance_
1106 // method of the same name exists in the root class only.
1107 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00001108 SourceRange(LBracLoc, RBracLoc),
Fariborz Jahanian3b9819b2011-04-06 18:40:08 +00001109 true);
1110 if (Method)
1111 if (const ObjCInterfaceDecl *ID =
1112 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1113 if (ID->getSuperClass())
1114 Diag(Loc, diag::warn_root_inst_method_not_found)
1115 << Sel << SourceRange(LBracLoc, RBracLoc);
1116 }
1117 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001118 }
1119 }
1120 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001121 } else {
Douglas Gregorb5186b12010-04-22 17:01:48 +00001122 ObjCInterfaceDecl* ClassDecl = 0;
1123
1124 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1125 // long as one of the protocols implements the selector (if not, warn).
1126 if (const ObjCObjectPointerType *QIdTy
1127 = ReceiverType->getAsObjCQualifiedIdType()) {
1128 // Search protocols for instance methods.
Fariborz Jahanianb296e332011-03-09 22:17:12 +00001129 Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
1130 if (!Method)
1131 Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
Douglas Gregorb5186b12010-04-22 17:01:48 +00001132 } else if (const ObjCObjectPointerType *OCIType
1133 = ReceiverType->getAsObjCInterfacePointerType()) {
1134 // We allow sending a message to a pointer to an interface (an object).
1135 ClassDecl = OCIType->getInterfaceDecl();
1136 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1137 // faster than the following method (which can do *many* linear searches).
Sebastian Redl75d8a322010-08-02 23:18:59 +00001138 // The idea is to add class info to MethodPool.
Douglas Gregorb5186b12010-04-22 17:01:48 +00001139 Method = ClassDecl->lookupInstanceMethod(Sel);
1140
Fariborz Jahanianb296e332011-03-09 22:17:12 +00001141 if (!Method)
Douglas Gregorb5186b12010-04-22 17:01:48 +00001142 // Search protocol qualifiers.
Fariborz Jahanianb296e332011-03-09 22:17:12 +00001143 Method = LookupMethodInQualifiedType(Sel, OCIType, true);
1144
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00001145 bool forwardClass = false;
Douglas Gregorb5186b12010-04-22 17:01:48 +00001146 if (!Method) {
1147 // If we have implementations in scope, check "private" methods.
1148 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1149
1150 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1151 // If we still haven't found a method, look in the global pool. This
1152 // behavior isn't very desirable, however we need it for GCC
1153 // compatibility. FIXME: should we deviate??
1154 if (OCIType->qual_empty()) {
1155 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00001156 SourceRange(LBracLoc, RBracLoc));
1157 forwardClass = OCIType->getInterfaceDecl()->isForwardDecl();
1158 if (Method && !forwardClass)
Douglas Gregorb5186b12010-04-22 17:01:48 +00001159 Diag(Loc, diag::warn_maynot_respond)
1160 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1161 }
1162 }
1163 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00001164 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorb5186b12010-04-22 17:01:48 +00001165 return ExprError();
1166 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00001167 (ReceiverType->isPointerType() ||
1168 ReceiverType->isIntegerType())) {
Douglas Gregorb5186b12010-04-22 17:01:48 +00001169 // Implicitly convert integers and pointers to 'id' but emit a warning.
1170 Diag(Loc, diag::warn_bad_receiver_type)
1171 << ReceiverType
1172 << Receiver->getSourceRange();
1173 if (ReceiverType->isPointerType())
John Wiegley01296292011-04-08 18:41:53 +00001174 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1175 CK_BitCast).take();
John McCalle84af4e2010-11-13 01:35:44 +00001176 else {
1177 // TODO: specialized warning on null receivers?
1178 bool IsNull = Receiver->isNullPointerConstant(Context,
1179 Expr::NPC_ValueDependentIsNull);
John Wiegley01296292011-04-08 18:41:53 +00001180 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1181 IsNull ? CK_NullToPointer : CK_IntegralToPointer).take();
John McCalle84af4e2010-11-13 01:35:44 +00001182 }
Douglas Gregorb5186b12010-04-22 17:01:48 +00001183 ReceiverType = Receiver->getType();
Fariborz Jahaniancac49a82010-05-12 23:29:11 +00001184 }
John Wiegley01296292011-04-08 18:41:53 +00001185 else {
1186 ExprResult ReceiverRes;
1187 if (getLangOptions().CPlusPlus)
1188 ReceiverRes = PerformContextuallyConvertToObjCId(Receiver);
1189 if (ReceiverRes.isUsable()) {
1190 Receiver = ReceiverRes.take();
1191 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1192 Receiver = ICE->getSubExpr();
1193 ReceiverType = Receiver->getType();
1194 }
1195 return BuildInstanceMessage(Receiver,
1196 ReceiverType,
1197 SuperLoc,
1198 Sel,
1199 Method,
1200 LBracLoc,
1201 SelectorLoc,
1202 RBracLoc,
1203 move(ArgsIn));
1204 } else {
1205 // Reject other random receiver types (e.g. structs).
1206 Diag(Loc, diag::err_bad_receiver_type)
1207 << ReceiverType << Receiver->getSourceRange();
1208 return ExprError();
Fariborz Jahanian1bd96d12010-05-13 17:19:25 +00001209 }
Douglas Gregorb5186b12010-04-22 17:01:48 +00001210 }
Douglas Gregor9a129192010-04-21 00:45:42 +00001211 }
Chris Lattner6b946cc2008-07-21 05:57:44 +00001212 }
Mike Stump11289f42009-09-09 15:08:12 +00001213
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001214 // Check the message arguments.
1215 unsigned NumArgs = ArgsIn.size();
1216 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1217 QualType ReturnType;
John McCall7decc9e2010-11-18 06:31:45 +00001218 ExprValueKind VK = VK_RValue;
Fariborz Jahanian68500912010-12-01 01:07:24 +00001219 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1220 ReceiverType->isObjCQualifiedClassType());
1221 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCall7decc9e2010-11-18 06:31:45 +00001222 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001223 return ExprError();
Fariborz Jahanian18e02752010-06-16 19:56:08 +00001224
Douglas Gregoraec93c62011-01-11 03:23:19 +00001225 if (Method && !Method->getResultType()->isVoidType() &&
1226 RequireCompleteType(LBracLoc, Method->getResultType(),
1227 diag::err_illegal_message_expr_incomplete_type))
1228 return ExprError();
Douglas Gregor9a129192010-04-21 00:45:42 +00001229
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001230 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregoraae38d62010-05-22 05:17:18 +00001231 Expr *Result;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001232 if (SuperLoc.isValid())
John McCall7decc9e2010-11-18 06:31:45 +00001233 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregoraae38d62010-05-22 05:17:18 +00001234 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001235 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregoraae38d62010-05-22 05:17:18 +00001236 Args, NumArgs, RBracLoc);
1237 else
John McCall7decc9e2010-11-18 06:31:45 +00001238 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001239 Receiver, Sel, SelectorLoc, Method,
1240 Args, NumArgs, RBracLoc);
Douglas Gregoraae38d62010-05-22 05:17:18 +00001241 return MaybeBindToTemporary(Result);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001242}
1243
1244// ActOnInstanceMessage - used for both unary and keyword messages.
1245// ArgExprs is optional - if it is present, the number of expressions
1246// is obtained from Sel.getNumArgs().
John McCalldadc5752010-08-24 06:29:42 +00001247ExprResult Sema::ActOnInstanceMessage(Scope *S,
1248 Expr *Receiver,
1249 Selector Sel,
1250 SourceLocation LBracLoc,
1251 SourceLocation SelectorLoc,
1252 SourceLocation RBracLoc,
1253 MultiExprArg Args) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001254 if (!Receiver)
1255 return ExprError();
1256
John McCallb268a282010-08-23 23:25:46 +00001257 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorb5186b12010-04-22 17:01:48 +00001258 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00001259 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattnera3fc41d2008-01-04 22:32:30 +00001260}
Chris Lattner2a3569b2008-04-07 05:30:13 +00001261