blob: 20fe6aba884ec675a5bfc866441721297d783fbd [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]);
223 --idx)
224 if (!cast<BlockScopeInfo>(FunctionScopes[idx])->Captures.insert(self))
225 break;
226
227 return method;
228}
229
230
Mike Stump1eb44332009-09-09 15:08:12 +0000231bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
232 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000233 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000234 SourceLocation lbrac, SourceLocation rbrac,
John McCallf89e55a2010-11-18 06:31:45 +0000235 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000236 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000237 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregor92e986e2010-04-22 16:44:27 +0000238 for (unsigned i = 0; i != NumArgs; i++) {
239 if (Args[i]->isTypeDependent())
240 continue;
241
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000242 DefaultArgumentPromotion(Args[i]);
Douglas Gregor92e986e2010-04-22 16:44:27 +0000243 }
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000244
Chris Lattner077bf5e2008-11-24 03:33:13 +0000245 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
246 diag::warn_inst_method_not_found;
247 Diag(lbrac, DiagID)
248 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000249 ReturnType = Context.getObjCIdType();
John McCallf89e55a2010-11-18 06:31:45 +0000250 VK = VK_RValue;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000251 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000254 ReturnType = Method->getSendResultType();
John McCallf89e55a2010-11-18 06:31:45 +0000255 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000257 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000258 // Method might have more arguments than selector indicates. This is due
259 // to addition of c-style arguments in method.
260 if (Method->param_size() > Sel.getNumArgs())
261 NumNamedArgs = Method->param_size();
262 // FIXME. This need be cleaned up.
263 if (NumArgs < NumNamedArgs) {
John McCallf89e55a2010-11-18 06:31:45 +0000264 Diag(lbrac, diag::err_typecheck_call_too_few_args)
265 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000266 return false;
267 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000268
Chris Lattner312531a2009-04-12 08:11:20 +0000269 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000270 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000271 // We can't do any type-checking on a type-dependent argument.
272 if (Args[i]->isTypeDependent())
273 continue;
274
Chris Lattner85a932e2008-01-04 22:32:30 +0000275 Expr *argExpr = Args[i];
Douglas Gregor92e986e2010-04-22 16:44:27 +0000276
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000277 ParmVarDecl *Param = Method->param_begin()[i];
Chris Lattner85a932e2008-01-04 22:32:30 +0000278 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000280 if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
281 Param->getType(),
282 PDiag(diag::err_call_incomplete_argument)
283 << argExpr->getSourceRange()))
284 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000285
Fariborz Jahanian745da3a2010-09-24 17:30:16 +0000286 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
287 Param);
John McCall3fa5cae2010-10-26 07:05:15 +0000288 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000289 if (ArgE.isInvalid())
290 IsError = true;
291 else
292 Args[i] = ArgE.takeAs<Expr>();
Chris Lattner85a932e2008-01-04 22:32:30 +0000293 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000294
295 // Promote additional arguments to variadic methods.
296 if (Method->isVariadic()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000297 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
298 if (Args[i]->isTypeDependent())
299 continue;
300
Chris Lattner40378332010-05-16 04:01:30 +0000301 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
Douglas Gregor92e986e2010-04-22 16:44:27 +0000302 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000303 } else {
304 // Check for extra arguments to non-variadic methods.
305 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000306 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000307 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000308 << 2 /*method*/ << NumNamedArgs << NumArgs
309 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000310 << SourceRange(Args[NumNamedArgs]->getLocStart(),
311 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000312 }
313 }
314
Douglas Gregor2725ca82010-04-21 19:57:20 +0000315 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Chris Lattner312531a2009-04-12 08:11:20 +0000316 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000317}
318
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000319bool Sema::isSelfExpr(Expr *RExpr) {
John McCallf6a16482010-12-04 03:47:34 +0000320 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
321 if (ICE->getCastKind() == CK_LValueToRValue)
322 RExpr = ICE->getSubExpr();
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000323 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
324 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
325 return true;
326 return false;
327}
328
Steve Narofff1afaf62009-02-26 15:55:06 +0000329// Helper method for ActOnClassMethod/ActOnInstanceMethod.
330// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000331// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000332// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000333ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000334 ObjCInterfaceDecl *ClassDecl) {
335 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000336 // lookup in class and all superclasses
337 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000338 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000339 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Steve Naroff5609ec02009-03-08 18:56:13 +0000341 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000342 if (!Method)
343 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Steve Naroff5609ec02009-03-08 18:56:13 +0000345 // Before we give up, check if the selector is an instance method.
346 // But only in the root. This matches gcc's behaviour and what the
347 // runtime expects.
348 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000349 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000351 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000352 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000353 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Steve Naroff5609ec02009-03-08 18:56:13 +0000356 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000357 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000358 return Method;
359}
360
361ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
362 ObjCInterfaceDecl *ClassDecl) {
363 ObjCMethodDecl *Method = 0;
364 while (ClassDecl && !Method) {
365 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000366 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000367 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Steve Naroff5609ec02009-03-08 18:56:13 +0000369 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000370 if (!Method)
371 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000372 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000373 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000374 return Method;
375}
376
Chris Lattner7f816522010-04-11 07:45:24 +0000377/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
378/// objective C interface. This is a property reference expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000379ExprResult Sema::
Chris Lattner7f816522010-04-11 07:45:24 +0000380HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000381 Expr *BaseExpr, DeclarationName MemberName,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000382 SourceLocation MemberLoc,
383 SourceLocation SuperLoc, QualType SuperType,
384 bool Super) {
Chris Lattner7f816522010-04-11 07:45:24 +0000385 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
386 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
387 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
388
Fariborz Jahanian8b1aba42010-12-16 00:56:28 +0000389 if (IFace->isForwardDecl()) {
390 Diag(MemberLoc, diag::err_property_not_found_forward_class)
391 << MemberName << QualType(OPT, 0);
392 Diag(IFace->getLocation(), diag::note_forward_class);
393 return ExprError();
394 }
Chris Lattner7f816522010-04-11 07:45:24 +0000395 // Search for a declared property first.
396 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
397 // Check whether we can reference this property.
398 if (DiagnoseUseOfDecl(PD, MemberLoc))
399 return ExprError();
400 QualType ResTy = PD->getType();
401 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
402 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
403 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
John McCallf89e55a2010-11-18 06:31:45 +0000404 ResTy = Getter->getResultType();
405
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000406 if (Super)
407 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000408 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000409 MemberLoc,
410 SuperLoc, SuperType));
411 else
412 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000413 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000414 MemberLoc, BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000415 }
416 // Check protocols on qualified interfaces.
417 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
418 E = OPT->qual_end(); I != E; ++I)
419 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
420 // Check whether we can reference this property.
421 if (DiagnoseUseOfDecl(PD, MemberLoc))
422 return ExprError();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000423 if (Super)
John McCallf89e55a2010-11-18 06:31:45 +0000424 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
425 VK_LValue,
426 OK_ObjCProperty,
427 MemberLoc,
428 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000429 else
430 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000431 VK_LValue,
432 OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000433 MemberLoc,
434 BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000435 }
436 // If that failed, look for an "implicit" property by seeing if the nullary
437 // selector is implemented.
438
439 // FIXME: The logic for looking up nullary and unary selectors should be
440 // shared with the code in ActOnInstanceMessage.
441
442 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
443 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
444
445 // If this reference is in an @implementation, check for 'private' methods.
446 if (!Getter)
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000447 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner7f816522010-04-11 07:45:24 +0000448
449 // Look through local category implementations associated with the class.
450 if (!Getter)
451 Getter = IFace->getCategoryInstanceMethod(Sel);
452 if (Getter) {
453 // Check if we can reference this property.
454 if (DiagnoseUseOfDecl(Getter, MemberLoc))
455 return ExprError();
456 }
457 // If we found a getter then this may be a valid dot-reference, we
458 // will look for the matching setter, in case it is needed.
459 Selector SetterSel =
460 SelectorTable::constructSetterName(PP.getIdentifierTable(),
461 PP.getSelectorTable(), Member);
462 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
463 if (!Setter) {
464 // If this reference is in an @implementation, also check for 'private'
465 // methods.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000466 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner7f816522010-04-11 07:45:24 +0000467 }
468 // Look through local category implementations associated with the class.
469 if (!Setter)
470 Setter = IFace->getCategoryInstanceMethod(SetterSel);
471
472 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
473 return ExprError();
474
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000475 if (Getter || Setter) {
476 QualType PType;
477 if (Getter)
478 PType = Getter->getSendResultType();
479 else {
480 ParmVarDecl *ArgDecl = *Setter->param_begin();
481 PType = ArgDecl->getType();
482 }
483
John McCall09431682010-11-18 19:01:18 +0000484 ExprValueKind VK = VK_LValue;
485 ExprObjectKind OK = OK_ObjCProperty;
486 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
487 PType->isVoidType())
488 VK = VK_RValue, OK = OK_Ordinary;
489
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000490 if (Super)
John McCall12f78a62010-12-02 01:19:52 +0000491 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
492 PType, VK, OK,
493 MemberLoc,
494 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000495 else
John McCall12f78a62010-12-02 01:19:52 +0000496 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
497 PType, VK, OK,
498 MemberLoc, BaseExpr));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000499
Chris Lattner7f816522010-04-11 07:45:24 +0000500 }
501
502 // Attempt to correct for typos in property names.
503 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000504 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000505 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000506 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000507 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000508 << MemberName << QualType(OPT, 0) << TypoResult
509 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000510 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
511 Diag(Property->getLocation(), diag::note_previous_decl)
512 << Property->getDeclName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000513 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
514 SuperLoc, SuperType, Super);
Chris Lattner7f816522010-04-11 07:45:24 +0000515 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000516
Chris Lattner7f816522010-04-11 07:45:24 +0000517 Diag(MemberLoc, diag::err_property_not_found)
518 << MemberName << QualType(OPT, 0);
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000519 if (Setter)
Chris Lattner7f816522010-04-11 07:45:24 +0000520 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000521 << MemberName << BaseExpr->getSourceRange();
Chris Lattner7f816522010-04-11 07:45:24 +0000522 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000523}
524
525
526
John McCall60d7b3a2010-08-24 06:29:42 +0000527ExprResult Sema::
Chris Lattnereb483eb2010-04-11 08:28:14 +0000528ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
529 IdentifierInfo &propertyName,
530 SourceLocation receiverNameLoc,
531 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000533 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000534 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
535 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000536 if (IFace == 0) {
537 // If the "receiver" is 'super' in a method, handle it as an expression-like
538 // property reference.
John McCall26743b22011-02-03 09:00:02 +0000539 if (receiverNamePtr->isStr("super")) {
540 if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000541 if (CurMethod->isInstanceMethod()) {
542 QualType T =
543 Context.getObjCInterfaceType(CurMethod->getClassInterface());
544 T = Context.getObjCObjectPointerType(T);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000545
546 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000547 /*BaseExpr*/0, &propertyName,
548 propertyNameLoc,
549 receiverNameLoc, T, true);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattnereb483eb2010-04-11 08:28:14 +0000552 // Otherwise, if this is a class method, try dispatching to our
553 // superclass.
554 IFace = CurMethod->getClassInterface()->getSuperClass();
555 }
John McCall26743b22011-02-03 09:00:02 +0000556 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000557
558 if (IFace == 0) {
559 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
560 return ExprError();
561 }
562 }
563
564 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000565 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000566 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000567
568 // If this reference is in an @implementation, check for 'private' methods.
569 if (!Getter)
570 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
571 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000572 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000573 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000574
575 if (Getter) {
576 // FIXME: refactor/share with ActOnMemberReference().
577 // Check if we can reference this property.
578 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
579 return ExprError();
580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Steve Naroff61f72cb2009-03-09 21:12:44 +0000582 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000583 Selector SetterSel =
584 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000585 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000587 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000588 if (!Setter) {
589 // If this reference is in an @implementation, also check for 'private'
590 // methods.
591 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
592 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000593 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000595 }
596 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000597 if (!Setter)
598 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000599
600 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
601 return ExprError();
602
603 if (Getter || Setter) {
604 QualType PType;
605
John McCall09431682010-11-18 19:01:18 +0000606 ExprValueKind VK = VK_LValue;
607 if (Getter) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000608 PType = Getter->getSendResultType();
John McCall09431682010-11-18 19:01:18 +0000609 if (!getLangOptions().CPlusPlus &&
610 !PType.hasQualifiers() && PType->isVoidType())
611 VK = VK_RValue;
612 } else {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000613 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
614 E = Setter->param_end(); PI != E; ++PI)
615 PType = (*PI)->getType();
John McCall09431682010-11-18 19:01:18 +0000616 VK = VK_LValue;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000617 }
John McCall09431682010-11-18 19:01:18 +0000618
619 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
620
John McCall12f78a62010-12-02 01:19:52 +0000621 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
622 PType, VK, OK,
623 propertyNameLoc,
624 receiverNameLoc, IFace));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000625 }
626 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
627 << &propertyName << Context.getObjCInterfaceType(IFace));
628}
629
Douglas Gregor47bd5432010-04-14 02:46:37 +0000630Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +0000631 IdentifierInfo *Name,
Douglas Gregor47bd5432010-04-14 02:46:37 +0000632 SourceLocation NameLoc,
633 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +0000634 bool HasTrailingDot,
John McCallb3d87482010-08-24 05:47:05 +0000635 ParsedType &ReceiverType) {
636 ReceiverType = ParsedType();
Douglas Gregor1569f952010-04-21 20:38:13 +0000637
Douglas Gregor47bd5432010-04-14 02:46:37 +0000638 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor95f42922010-10-14 22:11:03 +0000639 // messaging super. If the identifier is "super" and there is a
640 // trailing dot, it's an instance message.
641 if (IsSuper && S->isInObjcMethodScope())
642 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000643
644 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
645 LookupName(Result, S);
646
647 switch (Result.getResultKind()) {
648 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000649 // Normal name lookup didn't find anything. If we're in an
650 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor95f42922010-10-14 22:11:03 +0000651 // FIXME: This is a hack. Ivar lookup should be part of normal
652 // lookup.
Douglas Gregored464422010-04-19 20:09:36 +0000653 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
654 ObjCInterfaceDecl *ClassDeclared;
655 if (Method->getClassInterface()->lookupInstanceVariable(Name,
656 ClassDeclared))
657 return ObjCInstanceMessage;
658 }
Douglas Gregor95f42922010-10-14 22:11:03 +0000659
Douglas Gregor47bd5432010-04-14 02:46:37 +0000660 // Break out; we'll perform typo correction below.
661 break;
662
663 case LookupResult::NotFoundInCurrentInstantiation:
664 case LookupResult::FoundOverloaded:
665 case LookupResult::FoundUnresolvedValue:
666 case LookupResult::Ambiguous:
667 Result.suppressDiagnostics();
668 return ObjCInstanceMessage;
669
670 case LookupResult::Found: {
671 // We found something. If it's a type, then we have a class
672 // message. Otherwise, it's an instance message.
673 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000674 QualType T;
675 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
676 T = Context.getObjCInterfaceType(Class);
677 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
678 T = Context.getTypeDeclType(Type);
679 else
680 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000681
Douglas Gregor1569f952010-04-21 20:38:13 +0000682 // We have a class message, and T is the type we're
683 // messaging. Build source-location information for it.
684 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000685 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor1569f952010-04-21 20:38:13 +0000686 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000687 }
688 }
689
Douglas Gregoraaf87162010-04-14 20:04:41 +0000690 // Determine our typo-correction context.
691 CorrectTypoContext CTC = CTC_Expression;
692 if (ObjCMethodDecl *Method = getCurMethodDecl())
693 if (Method->getClassInterface() &&
694 Method->getClassInterface()->getSuperClass())
695 CTC = CTC_ObjCMessageReceiver;
696
697 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
698 if (Result.isSingleResult()) {
699 // If we found a declaration, correct when it refers to an Objective-C
700 // class.
701 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000702 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregoraaf87162010-04-14 20:04:41 +0000703 Diag(NameLoc, diag::err_unknown_receiver_suggest)
704 << Name << Result.getLookupName()
705 << FixItHint::CreateReplacement(SourceRange(NameLoc),
706 ND->getNameAsString());
707 Diag(ND->getLocation(), diag::note_previous_decl)
708 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000709
Douglas Gregor1569f952010-04-21 20:38:13 +0000710 QualType T = Context.getObjCInterfaceType(Class);
711 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000712 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000713 return ObjCClassMessage;
714 }
715 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
716 Corrected.getAsIdentifierInfo()->isStr("super")) {
717 // If we've found the keyword "super", this is a send to super.
718 Diag(NameLoc, diag::err_unknown_receiver_suggest)
719 << Name << Corrected
720 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
Douglas Gregoraaf87162010-04-14 20:04:41 +0000721 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000722 }
723 }
724
725 // Fall back: let the parser try to parse it as an instance message.
726 return ObjCInstanceMessage;
727}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000728
John McCall60d7b3a2010-08-24 06:29:42 +0000729ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000730 SourceLocation SuperLoc,
731 Selector Sel,
732 SourceLocation LBracLoc,
733 SourceLocation SelectorLoc,
734 SourceLocation RBracLoc,
735 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000736 // Determine whether we are inside a method or not.
John McCall26743b22011-02-03 09:00:02 +0000737 ObjCMethodDecl *Method = tryCaptureObjCSelf();
Douglas Gregorf95861a2010-04-21 20:01:04 +0000738 if (!Method) {
739 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
740 return ExprError();
741 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000742
Douglas Gregorf95861a2010-04-21 20:01:04 +0000743 ObjCInterfaceDecl *Class = Method->getClassInterface();
744 if (!Class) {
745 Diag(SuperLoc, diag::error_no_super_class_message)
746 << Method->getDeclName();
747 return ExprError();
748 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000749
Douglas Gregorf95861a2010-04-21 20:01:04 +0000750 ObjCInterfaceDecl *Super = Class->getSuperClass();
751 if (!Super) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000752 // The current class does not have a superclass.
Ted Kremeneke00909a2011-01-23 17:21:34 +0000753 Diag(SuperLoc, diag::error_root_class_cannot_use_super)
754 << Class->getIdentifier();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000755 return ExprError();
Chris Lattner15faee12010-04-12 05:38:43 +0000756 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000757
Douglas Gregorf95861a2010-04-21 20:01:04 +0000758 // We are in a method whose class has a superclass, so 'super'
759 // is acting as a keyword.
760 if (Method->isInstanceMethod()) {
761 // Since we are in an instance method, this is an instance
762 // message to the superclass instance.
763 QualType SuperTy = Context.getObjCInterfaceType(Super);
764 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCall9ae2f072010-08-23 23:25:46 +0000765 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000766 Sel, /*Method=*/0,
767 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000768 }
Douglas Gregorf95861a2010-04-21 20:01:04 +0000769
770 // Since we are in a class method, this is a class message to
771 // the superclass.
772 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
773 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000774 SuperLoc, Sel, /*Method=*/0,
775 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000776}
777
778/// \brief Build an Objective-C class message expression.
779///
780/// This routine takes care of both normal class messages and
781/// class messages to the superclass.
782///
783/// \param ReceiverTypeInfo Type source information that describes the
784/// receiver of this message. This may be NULL, in which case we are
785/// sending to the superclass and \p SuperLoc must be a valid source
786/// location.
787
788/// \param ReceiverType The type of the object receiving the
789/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
790/// type as that refers to. For a superclass send, this is the type of
791/// the superclass.
792///
793/// \param SuperLoc The location of the "super" keyword in a
794/// superclass message.
795///
796/// \param Sel The selector to which the message is being sent.
797///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000798/// \param Method The method that this class message is invoking, if
799/// already known.
800///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000801/// \param LBracLoc The location of the opening square bracket ']'.
802///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000803/// \param RBrac The location of the closing square bracket ']'.
804///
805/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000806ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000807 QualType ReceiverType,
808 SourceLocation SuperLoc,
809 Selector Sel,
810 ObjCMethodDecl *Method,
811 SourceLocation LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000812 SourceLocation SelectorLoc,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000813 SourceLocation RBracLoc,
814 MultiExprArg ArgsIn) {
815 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregor9497a732010-09-16 01:51:54 +0000816 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregor0fbda682010-09-15 14:51:05 +0000817 if (LBracLoc.isInvalid()) {
818 Diag(Loc, diag::err_missing_open_square_message_send)
819 << FixItHint::CreateInsertion(Loc, "[");
820 LBracLoc = Loc;
821 }
822
Douglas Gregor92e986e2010-04-22 16:44:27 +0000823 if (ReceiverType->isDependentType()) {
824 // If the receiver type is dependent, we can't type-check anything
825 // at this point. Build a dependent expression.
826 unsigned NumArgs = ArgsIn.size();
827 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
828 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCallf89e55a2010-11-18 06:31:45 +0000829 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
830 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000831 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregor92e986e2010-04-22 16:44:27 +0000832 Args, NumArgs, RBracLoc));
833 }
Chris Lattner15faee12010-04-12 05:38:43 +0000834
Douglas Gregor2725ca82010-04-21 19:57:20 +0000835 // Find the class to which we are sending this message.
836 ObjCInterfaceDecl *Class = 0;
John McCallc12c5bb2010-05-15 11:32:37 +0000837 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
838 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000839 Diag(Loc, diag::err_invalid_receiver_class_message)
840 << ReceiverType;
841 return ExprError();
Steve Naroff7c778f12008-07-25 19:39:00 +0000842 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000843 assert(Class && "We don't know which class we're messaging?");
844
845 // Find the method we are messaging.
Douglas Gregorf49bb082010-04-22 17:01:48 +0000846 if (!Method) {
847 if (Class->isForwardDecl()) {
848 // A forward class used in messaging is treated as a 'Class'
849 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
850 Method = LookupFactoryMethodInGlobalPool(Sel,
851 SourceRange(LBracLoc, RBracLoc));
852 if (Method)
853 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
854 << Method->getDeclName();
855 }
856 if (!Method)
857 Method = Class->lookupClassMethod(Sel);
858
859 // If we have an implementation in scope, check "private" methods.
860 if (!Method)
861 Method = LookupPrivateClassMethod(Sel, Class);
862
863 if (Method && DiagnoseUseOfDecl(Method, Loc))
864 return ExprError();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Douglas Gregor2725ca82010-04-21 19:57:20 +0000867 // Check the argument types and determine the result type.
868 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +0000869 ExprValueKind VK = VK_RValue;
870
Douglas Gregor2725ca82010-04-21 19:57:20 +0000871 unsigned NumArgs = ArgsIn.size();
872 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
873 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCallf89e55a2010-11-18 06:31:45 +0000874 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +0000875 return ExprError();
Ted Kremenek4df728e2008-06-24 15:50:53 +0000876
Douglas Gregor483dd2f2011-01-11 03:23:19 +0000877 if (Method && !Method->getResultType()->isVoidType() &&
878 RequireCompleteType(LBracLoc, Method->getResultType(),
879 diag::err_illegal_message_expr_incomplete_type))
880 return ExprError();
881
Douglas Gregor2725ca82010-04-21 19:57:20 +0000882 // Construct the appropriate ObjCMessageExpr.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000883 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +0000884 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +0000885 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000886 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000887 ReceiverType, Sel, SelectorLoc,
888 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000889 else
John McCallf89e55a2010-11-18 06:31:45 +0000890 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000891 ReceiverTypeInfo, Sel, SelectorLoc,
892 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000893 return MaybeBindToTemporary(Result);
Chris Lattner85a932e2008-01-04 22:32:30 +0000894}
895
Douglas Gregor2725ca82010-04-21 19:57:20 +0000896// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattner85a932e2008-01-04 22:32:30 +0000897// ArgExprs is optional - if it is present, the number of expressions
898// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +0000899ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor77328d12010-09-15 23:19:31 +0000900 ParsedType Receiver,
901 Selector Sel,
902 SourceLocation LBracLoc,
903 SourceLocation SelectorLoc,
904 SourceLocation RBracLoc,
905 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000906 TypeSourceInfo *ReceiverTypeInfo;
907 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
908 if (ReceiverType.isNull())
909 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Douglas Gregor2725ca82010-04-21 19:57:20 +0000912 if (!ReceiverTypeInfo)
913 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
914
915 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorf49bb082010-04-22 17:01:48 +0000916 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000917 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000918}
919
920/// \brief Build an Objective-C instance message expression.
921///
922/// This routine takes care of both normal instance messages and
923/// instance messages to the superclass instance.
924///
925/// \param Receiver The expression that computes the object that will
926/// receive this message. This may be empty, in which case we are
927/// sending to the superclass instance and \p SuperLoc must be a valid
928/// source location.
929///
930/// \param ReceiverType The (static) type of the object receiving the
931/// message. When a \p Receiver expression is provided, this is the
932/// same type as that expression. For a superclass instance send, this
933/// is a pointer to the type of the superclass.
934///
935/// \param SuperLoc The location of the "super" keyword in a
936/// superclass instance message.
937///
938/// \param Sel The selector to which the message is being sent.
939///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000940/// \param Method The method that this instance message is invoking, if
941/// already known.
942///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000943/// \param LBracLoc The location of the opening square bracket ']'.
944///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000945/// \param RBrac The location of the closing square bracket ']'.
946///
947/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000948ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000949 QualType ReceiverType,
950 SourceLocation SuperLoc,
951 Selector Sel,
952 ObjCMethodDecl *Method,
953 SourceLocation LBracLoc,
954 SourceLocation SelectorLoc,
955 SourceLocation RBracLoc,
956 MultiExprArg ArgsIn) {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000957 // The location of the receiver.
958 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
959
960 if (LBracLoc.isInvalid()) {
961 Diag(Loc, diag::err_missing_open_square_message_send)
962 << FixItHint::CreateInsertion(Loc, "[");
963 LBracLoc = Loc;
964 }
965
Douglas Gregor2725ca82010-04-21 19:57:20 +0000966 // If we have a receiver expression, perform appropriate promotions
967 // and determine receiver type.
Douglas Gregor2725ca82010-04-21 19:57:20 +0000968 if (Receiver) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000969 if (Receiver->isTypeDependent()) {
970 // If the receiver is type-dependent, we can't type-check anything
971 // at this point. Build a dependent expression.
972 unsigned NumArgs = ArgsIn.size();
973 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
974 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
975 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCallf89e55a2010-11-18 06:31:45 +0000976 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000977 SelectorLoc, /*Method=*/0,
978 Args, NumArgs, RBracLoc));
Douglas Gregor92e986e2010-04-22 16:44:27 +0000979 }
980
Douglas Gregor2725ca82010-04-21 19:57:20 +0000981 // If necessary, apply function/array conversion to the receiver.
982 // C99 6.7.5.3p[7,8].
983 DefaultFunctionArrayLvalueConversion(Receiver);
984 ReceiverType = Receiver->getType();
985 }
986
Douglas Gregorf49bb082010-04-22 17:01:48 +0000987 if (!Method) {
988 // Handle messages to id.
Fariborz Jahanianba551982010-08-10 18:10:50 +0000989 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000990 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorf49bb082010-04-22 17:01:48 +0000991 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
992 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000993 SourceRange(LBracLoc, RBracLoc),
994 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000995 if (!Method)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000996 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000997 SourceRange(LBracLoc, RBracLoc),
998 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000999 } else if (ReceiverType->isObjCClassType() ||
1000 ReceiverType->isObjCQualifiedClassType()) {
1001 // Handle messages to Class.
1002 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1003 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1004 // First check the public methods in the class interface.
1005 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Douglas Gregorf49bb082010-04-22 17:01:48 +00001007 if (!Method)
1008 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Douglas Gregor04badcf2010-04-21 00:45:42 +00001009
Douglas Gregorf49bb082010-04-22 17:01:48 +00001010 // FIXME: if we still haven't found a method, we need to look in
1011 // protocols (if we have qualifiers).
Steve Naroff6b9dfd42009-03-04 15:11:40 +00001012 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001013 if (Method && DiagnoseUseOfDecl(Method, Loc))
1014 return ExprError();
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +00001015 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001016 if (!Method) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001017 // If not messaging 'self', look for any factory method named 'Sel'.
1018 if (!Receiver || !isSelfExpr(Receiver)) {
1019 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001020 SourceRange(LBracLoc, RBracLoc),
1021 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001022 if (!Method) {
1023 // If no class (factory) method was found, check if an _instance_
1024 // method of the same name exists in the root class only.
Douglas Gregor2725ca82010-04-21 19:57:20 +00001025 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001026 SourceRange(LBracLoc, RBracLoc),
1027 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001028 if (Method)
1029 if (const ObjCInterfaceDecl *ID =
1030 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1031 if (ID->getSuperClass())
1032 Diag(Loc, diag::warn_root_inst_method_not_found)
1033 << Sel << SourceRange(LBracLoc, RBracLoc);
1034 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001035 }
1036 }
1037 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001038 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001039 ObjCInterfaceDecl* ClassDecl = 0;
1040
1041 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1042 // long as one of the protocols implements the selector (if not, warn).
1043 if (const ObjCObjectPointerType *QIdTy
1044 = ReceiverType->getAsObjCQualifiedIdType()) {
1045 // Search protocols for instance methods.
1046 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
1047 E = QIdTy->qual_end(); I != E; ++I) {
1048 ObjCProtocolDecl *PDecl = *I;
1049 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
1050 break;
1051 // Since we aren't supporting "Class<foo>", look for a class method.
1052 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
1053 break;
1054 }
1055 } else if (const ObjCObjectPointerType *OCIType
1056 = ReceiverType->getAsObjCInterfacePointerType()) {
1057 // We allow sending a message to a pointer to an interface (an object).
1058 ClassDecl = OCIType->getInterfaceDecl();
1059 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1060 // faster than the following method (which can do *many* linear searches).
Sebastian Redldb9d2142010-08-02 23:18:59 +00001061 // The idea is to add class info to MethodPool.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001062 Method = ClassDecl->lookupInstanceMethod(Sel);
1063
1064 if (!Method) {
1065 // Search protocol qualifiers.
1066 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
1067 E = OCIType->qual_end(); QI != E; ++QI) {
1068 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
1069 break;
1070 }
1071 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001072 bool forwardClass = false;
Douglas Gregorf49bb082010-04-22 17:01:48 +00001073 if (!Method) {
1074 // If we have implementations in scope, check "private" methods.
1075 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1076
1077 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1078 // If we still haven't found a method, look in the global pool. This
1079 // behavior isn't very desirable, however we need it for GCC
1080 // compatibility. FIXME: should we deviate??
1081 if (OCIType->qual_empty()) {
1082 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001083 SourceRange(LBracLoc, RBracLoc));
1084 forwardClass = OCIType->getInterfaceDecl()->isForwardDecl();
1085 if (Method && !forwardClass)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001086 Diag(Loc, diag::warn_maynot_respond)
1087 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1088 }
1089 }
1090 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001091 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorf49bb082010-04-22 17:01:48 +00001092 return ExprError();
1093 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001094 (ReceiverType->isPointerType() ||
1095 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001096 // Implicitly convert integers and pointers to 'id' but emit a warning.
1097 Diag(Loc, diag::warn_bad_receiver_type)
1098 << ReceiverType
1099 << Receiver->getSourceRange();
1100 if (ReceiverType->isPointerType())
1101 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall2de56d12010-08-25 11:45:40 +00001102 CK_BitCast);
John McCall404cd162010-11-13 01:35:44 +00001103 else {
1104 // TODO: specialized warning on null receivers?
1105 bool IsNull = Receiver->isNullPointerConstant(Context,
1106 Expr::NPC_ValueDependentIsNull);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001107 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall404cd162010-11-13 01:35:44 +00001108 IsNull ? CK_NullToPointer : CK_IntegralToPointer);
1109 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001110 ReceiverType = Receiver->getType();
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001111 }
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001112 else if (getLangOptions().CPlusPlus &&
1113 !PerformContextuallyConvertToObjCId(Receiver)) {
1114 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1115 Receiver = ICE->getSubExpr();
1116 ReceiverType = Receiver->getType();
1117 }
John McCall9ae2f072010-08-23 23:25:46 +00001118 return BuildInstanceMessage(Receiver,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001119 ReceiverType,
1120 SuperLoc,
1121 Sel,
1122 Method,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001123 LBracLoc,
1124 SelectorLoc,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001125 RBracLoc,
1126 move(ArgsIn));
Douglas Gregorf49bb082010-04-22 17:01:48 +00001127 } else {
1128 // Reject other random receiver types (e.g. structs).
1129 Diag(Loc, diag::err_bad_receiver_type)
1130 << ReceiverType << Receiver->getSourceRange();
1131 return ExprError();
1132 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001133 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor2725ca82010-04-21 19:57:20 +00001136 // Check the message arguments.
1137 unsigned NumArgs = ArgsIn.size();
1138 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1139 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001140 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001141 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1142 ReceiverType->isObjCQualifiedClassType());
1143 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCallf89e55a2010-11-18 06:31:45 +00001144 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001145 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001146
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001147 if (Method && !Method->getResultType()->isVoidType() &&
1148 RequireCompleteType(LBracLoc, Method->getResultType(),
1149 diag::err_illegal_message_expr_incomplete_type))
1150 return ExprError();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001151
Douglas Gregor2725ca82010-04-21 19:57:20 +00001152 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001153 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001154 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001155 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001156 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001157 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001158 Args, NumArgs, RBracLoc);
1159 else
John McCallf89e55a2010-11-18 06:31:45 +00001160 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001161 Receiver, Sel, SelectorLoc, Method,
1162 Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001163 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001164}
1165
1166// ActOnInstanceMessage - used for both unary and keyword messages.
1167// ArgExprs is optional - if it is present, the number of expressions
1168// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001169ExprResult Sema::ActOnInstanceMessage(Scope *S,
1170 Expr *Receiver,
1171 Selector Sel,
1172 SourceLocation LBracLoc,
1173 SourceLocation SelectorLoc,
1174 SourceLocation RBracLoc,
1175 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001176 if (!Receiver)
1177 return ExprError();
1178
John McCall9ae2f072010-08-23 23:25:46 +00001179 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001180 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001181 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001182}
Chris Lattnereca7be62008-04-07 05:30:13 +00001183