blob: d6bec762764bc073d7c97932d816d42729cc28b1 [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
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Steve Narofff494b572008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000019using namespace clang;
20
21Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner39c28bb2009-02-18 06:48:40 +000022 ExprTy **strings,
Chris Lattner85a932e2008-01-04 22:32:30 +000023 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000024 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
25
Chris Lattnerf4b136f2009-02-18 06:13:04 +000026 // Most ObjC strings are formed out of a single piece. However, we *can*
27 // have strings formed out of multiple @ strings with multiple pptokens in
28 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
29 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000030 StringLiteral *S = Strings[0];
Chris Lattnerf4b136f2009-02-18 06:13:04 +000031
32 // If we have a multi-part string, merge it all together.
33 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000034 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000035 llvm::SmallString<128> StrBuf;
36 llvm::SmallVector<SourceLocation, 8> StrLocs;
Chris Lattner726e1682009-02-18 05:49:11 +000037
Chris Lattner726e1682009-02-18 05:49:11 +000038 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000039 S = Strings[i];
40
41 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000042 if (S->isWide()) {
43 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
44 << S->getSourceRange();
45 return true;
46 }
47
Chris Lattner39c28bb2009-02-18 06:48:40 +000048 // Get the string data.
49 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
50
51 // Get the locations of the string tokens.
52 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
53
54 // Free the temporary string.
Ted Kremenek8189cde2009-02-07 01:47:29 +000055 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000056 }
Chris Lattner39c28bb2009-02-18 06:48:40 +000057
58 // Create the aggregate string with the appropriate content and location
59 // information.
60 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000061 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000062 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000063 }
64
Chris Lattner69039812009-02-18 06:01:06 +000065 // Verify that this composite string is acceptable for ObjC strings.
66 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000067 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000068
69 // Initialize the constant string interface lazily. This assumes
70 // the NSConstantString interface is seen in this translation unit.
71 QualType Ty = Context.getObjCConstantStringInterface();
72 if (!Ty.isNull()) {
73 Ty = Context.getPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000074 } else {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000075 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
76 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
77 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
78 Context.setObjCConstantStringInterface(StrIF);
79 Ty = Context.getObjCConstantStringInterface();
80 Ty = Context.getPointerType(Ty);
81 } else {
82 // If there is no NSConstantString interface defined then treat constant
83 // strings as untyped objects and let the runtime figure it out later.
84 Ty = Context.getObjCIdType();
85 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000086 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +000087
Chris Lattnerf4b136f2009-02-18 06:13:04 +000088 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000089}
90
91Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
92 SourceLocation EncodeLoc,
93 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +000094 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +000095 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000096 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +000097
Chris Lattnera0af1fe2009-02-18 06:06:56 +000098 QualType Ty = Context.getPointerType(Context.CharTy);
99 return new (Context) ObjCEncodeExpr(Ty, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000100}
101
102Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
103 SourceLocation AtLoc,
104 SourceLocation SelLoc,
105 SourceLocation LParenLoc,
106 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000107 QualType Ty = Context.getObjCSelType();
108 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000109}
110
111Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
112 SourceLocation AtLoc,
113 SourceLocation ProtoLoc,
114 SourceLocation LParenLoc,
115 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000116 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000117 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000118 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000119 return true;
120 }
121
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000122 QualType Ty = Context.getObjCProtoType();
123 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000124 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000125 Ty = Context.getPointerType(Ty);
126 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000127}
128
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000129bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
130 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000131 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000132 SourceLocation lbrac, SourceLocation rbrac,
133 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000134 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000135 // Apply default argument promotion as for (C99 6.5.2.2p6).
136 for (unsigned i = 0; i != NumArgs; i++)
137 DefaultArgumentPromotion(Args[i]);
138
Chris Lattner077bf5e2008-11-24 03:33:13 +0000139 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
140 diag::warn_inst_method_not_found;
141 Diag(lbrac, DiagID)
142 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000143 ReturnType = Context.getObjCIdType();
144 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000145 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000146
147 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000148
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000149 unsigned NumNamedArgs = Sel.getNumArgs();
150 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
151
Chris Lattner85a932e2008-01-04 22:32:30 +0000152 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000153 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000154 Expr *argExpr = Args[i];
155 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
156
Chris Lattner89951a82009-02-20 18:43:26 +0000157 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000158 QualType rhsType = argExpr->getType();
159
160 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000161 if (lhsType->isArrayType())
162 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000163 else if (lhsType->isFunctionType())
164 lhsType = Context.getPointerType(lhsType);
165
Chris Lattner987798a2008-04-02 17:17:33 +0000166 AssignConvertType Result =
167 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000168 if (Args[i] != argExpr) // The expression was converted.
169 Args[i] = argExpr; // Make sure we store the converted expression.
170
171 anyIncompatibleArgs |=
172 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
173 argExpr, "sending");
174 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000175
176 // Promote additional arguments to variadic methods.
177 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000178 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
179 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000180 } else {
181 // Check for extra arguments to non-variadic methods.
182 if (NumArgs != NumNamedArgs) {
183 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000184 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000185 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000186 << SourceRange(Args[NumNamedArgs]->getLocStart(),
187 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000188 }
189 }
190
Chris Lattner85a932e2008-01-04 22:32:30 +0000191 return anyIncompatibleArgs;
192}
193
194// ActOnClassMessage - used for both unary and keyword messages.
195// ArgExprs is optional - if it is present, the number of expressions
196// is obtained from Sel.getNumArgs().
197Sema::ExprResult Sema::ActOnClassMessage(
198 Scope *S,
199 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000200 SourceLocation lbrac, SourceLocation receiverLoc,
201 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000202 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000203{
204 assert(receiverName && "missing receiver class name");
205
206 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000207 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000208 bool isSuper = false;
209
Chris Lattner84692652008-11-20 05:35:30 +0000210 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000211 if (getCurMethodDecl()) {
212 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000213 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
214 if (!OID)
215 return Diag(lbrac, diag::error_no_super_class_message)
216 << getCurMethodDecl()->getDeclName();
217 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000218 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000219 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000220 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000221 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
222 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000223 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
224 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000225 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000226 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
227 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000228 }
229 // We are sending a message to 'super' within a class method. Do nothing,
230 // the receiver will pass through as 'super' (how convenient:-).
231 } else {
232 // 'super' has been used outside a method context. If a variable named
233 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000234 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000235 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
236 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000237 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
238 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000239 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000240 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
241 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000242 }
Chris Lattner08631c52008-11-23 21:45:46 +0000243 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000244 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000245 } else
246 ClassDecl = getObjCInterfaceDecl(receiverName);
247
Steve Naroff7c778f12008-07-25 19:39:00 +0000248 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000249 //
250 // typedef XCElementDisplayRect XCElementGraphicsRect;
251 //
252 // @implementation XCRASlice
253 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
254 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
255 // }
256 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000257 // If necessary, the following lookup could move to getObjCInterfaceDecl().
258 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000259 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000260 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
261 const ObjCInterfaceType *OCIT;
262 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000263 if (!OCIT)
264 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
265 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000266 }
267 }
268 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000269 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000270 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000271 Method = ClassDecl->lookupClassMethod(Sel);
272
273 // If we have an implementation in scope, check "private" methods.
274 if (!Method) {
275 if (ObjCImplementationDecl *ImpDecl =
276 ObjCImplementations[ClassDecl->getIdentifier()])
277 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000278
279 // Look through local category implementations associated with the class.
280 if (!Method) {
281 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
282 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
283 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
284 }
285 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000286 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000287 // Before we give up, check if the selector is an instance method.
288 if (!Method)
289 Method = ClassDecl->lookupInstanceMethod(Sel);
290
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000291 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
292 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000293
Chris Lattner077bf5e2008-11-24 03:33:13 +0000294 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000295 lbrac, rbrac, returnType))
296 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000297
298 // If we have the ObjCInterfaceDecl* for the class that is receiving
299 // the message, use that to construct the ObjCMessageExpr. Otherwise
300 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000301 // FIXME: need to do a better job handling 'super' usage within a class
302 // For now, we simply pass the "super" identifier through (which isn't
303 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000304 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000305 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
306 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000307 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000308 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
309 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000310}
311
Steve Naroffd526c2f2009-02-23 02:25:40 +0000312// This routine makes sure we handle the following:
313//
314// [(Object <Func> *)super class_func0];
315// [(id <Func>)super class_func0];
316//
317static bool isSuperExpr(Stmt *S) {
318 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
319 CI != E; ++CI) {
320 if (*CI)
321 return isSuperExpr(*CI);
322 }
323 if (isa<ObjCSuperExpr>(S))
324 return true;
325 return false;
326}
327
Chris Lattner85a932e2008-01-04 22:32:30 +0000328// ActOnInstanceMessage - used for both unary and keyword messages.
329// ArgExprs is optional - if it is present, the number of expressions
330// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000331Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000332 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000333 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000334 SourceLocation rbrac,
335 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000336 assert(receiver && "missing receiver expression");
337
338 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
339 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000340 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000341
Chris Lattnerb77792e2008-07-26 22:17:49 +0000342 QualType ReceiverCType =
343 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000344
345 // Handle messages to 'super'.
Steve Naroffd526c2f2009-02-23 02:25:40 +0000346 if (isSuperExpr(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000347 ObjCMethodDecl *Method = 0;
348 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
349 // If we have an interface in scope, check 'super' methods.
350 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
351 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
352 Method = SuperDecl->lookupInstanceMethod(Sel);
353 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000354
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000355 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
356 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000357
Chris Lattner077bf5e2008-11-24 03:33:13 +0000358 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000359 lbrac, rbrac, returnType))
360 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000361 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
362 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000363 }
364
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000365 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000366 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
367 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000368 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
369 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000370 if (!Method)
371 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000372 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000373 lbrac, rbrac, returnType))
374 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000375 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
376 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000377 }
378
379 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000380 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000381 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000382 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000383 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
384 // First check the public methods in the class interface.
385 Method = ClassDecl->lookupClassMethod(Sel);
386
387 if (!Method) {
388 // If we have an implementation in scope, check "private" methods.
389 if (ObjCImplementationDecl *ImpDecl =
390 ObjCImplementations[ClassDecl->getIdentifier()])
391 Method = ImpDecl->getClassMethod(Sel);
392 }
393 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000394 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
395 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000396 } else {
397 // We're not in a method context, look for any factory method named 'Sel'.
Steve Naroffe2af8b12008-06-05 14:49:39 +0000398 Method = FactoryMethodPool[Sel].Method;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000399 }
Steve Naroffe2af8b12008-06-05 14:49:39 +0000400 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000401 Method = LookupInstanceMethodInGlobalPool(
402 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000403 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000404 lbrac, rbrac, returnType))
405 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000406 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
407 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000408 }
409
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000410 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000411 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000412
413 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
414 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000415 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000416 // Search protocols for instance methods.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000417 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
418 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
419 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
420 break;
421 }
Steve Narofff7f52e72009-02-21 21:17:01 +0000422 // Check for GCC extension "Class<foo>".
423 } else if (ObjCQualifiedClassType *QIT =
424 dyn_cast<ObjCQualifiedClassType>(ReceiverCType)) {
425 // Search protocols for class methods.
426 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
427 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
428 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
429 break;
430 }
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000431 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000432 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000433 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000434
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000435 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000436 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
437 // faster than the following method (which can do *many* linear searches).
438 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000439 Method = ClassDecl->lookupInstanceMethod(Sel);
440
441 if (!Method) {
442 // Search protocol qualifiers.
443 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
444 E = OCIReceiver->qual_end(); QI != E; ++QI) {
445 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000446 break;
447 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000448 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000449 if (!Method) {
450 // If we have an implementation in scope, check "private" methods.
451 if (ClassDecl)
452 if (ObjCImplementationDecl *ImpDecl =
453 ObjCImplementations[ClassDecl->getIdentifier()])
454 Method = ImpDecl->getInstanceMethod(Sel);
455 // If we still haven't found a method, look in the global pool. This
456 // behavior isn't very desirable, however we need it for GCC
457 // compatibility. FIXME: should we deviate??
Steve Naroffac1337d2009-02-22 19:41:00 +0000458 if (!Method && OCIReceiver->qual_empty())
Steve Naroff0de21fd2009-02-22 19:35:57 +0000459 Method = LookupInstanceMethodInGlobalPool(
460 Sel, SourceRange(lbrac,rbrac));
461 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000462 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
463 return true;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000464 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000465 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000466 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000467 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000468 }
469
Chris Lattner077bf5e2008-11-24 03:33:13 +0000470 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000471 lbrac, rbrac, returnType))
472 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000473 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
474 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000475}
Chris Lattnereca7be62008-04-07 05:30:13 +0000476
477//===----------------------------------------------------------------------===//
478// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
479//===----------------------------------------------------------------------===//
480
481/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
482/// inheritance hierarchy of 'rProto'.
483static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
484 ObjCProtocolDecl *rProto) {
485 if (lProto == rProto)
486 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000487 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
488 E = rProto->protocol_end(); PI != E; ++PI)
489 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000490 return true;
491 return false;
492}
493
494/// ClassImplementsProtocol - Checks that 'lProto' protocol
495/// has been implemented in IDecl class, its super class or categories (if
496/// lookupCategory is true).
497static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
498 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000499 bool lookupCategory,
500 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000501
502 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000503 const ObjCList<ObjCProtocolDecl> &Protocols =
504 IDecl->getReferencedProtocols();
505
506 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
507 E = Protocols.end(); PI != E; ++PI) {
508 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000509 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000510 // This is dubious and is added to be compatible with gcc.
511 // In gcc, it is also allowed assigning a protocol-qualified 'id'
512 // type to a LHS object when protocol in qualified LHS is in list
513 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000514 // FIXME: Treat this as an extension, and flag this as an error when
515 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000516 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000517 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000518 }
519
520 // 2nd, look up the category.
521 if (lookupCategory)
522 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
523 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000524 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
525 E = CDecl->protocol_end(); PI != E; ++PI)
526 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000527 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000528 }
529
530 // 3rd, look up the super class(s)
531 if (IDecl->getSuperClass())
532 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000533 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
534 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000535
536 return false;
537}
538
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000539/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
540/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000541bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
542 bool compare) {
543 // Allow id<P..> and an 'id' or void* type in all cases.
544 if (const PointerType *PT = lhs->getAsPointerType()) {
545 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000546 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000547 return true;
548 } else if (const PointerType *PT = rhs->getAsPointerType()) {
549 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000550 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000551 return true;
552 }
553
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000554 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
555 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
556 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000557 QualType rtype;
558
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000559 if (!rhsQID) {
560 // Not comparing two ObjCQualifiedIdType's?
561 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000562
563 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000564 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000565 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000566 // If the RHS is a unqualified interface pointer "NSString*",
567 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000568 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
569 ObjCInterfaceDecl *rhsID = IT->getDecl();
570 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
571 // when comparing an id<P> on lhs with a static type on rhs,
572 // see if static class implements all of id's protocols, directly or
573 // through its super class and categories.
574 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
575 return false;
576 }
577 return true;
578 }
579 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000580 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000581
582 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000583 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000584 RHSProtoI = rhsQI->qual_begin();
585 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000586 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000587 RHSProtoI = rhsQID->qual_begin();
588 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000589 } else {
590 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000591 }
592
593 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
594 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
595 bool match = false;
596
597 // when comparing an id<P> on lhs with a static type on rhs,
598 // see if static class implements all of id's protocols, directly or
599 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000600 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
601 ObjCProtocolDecl *rhsProto = *RHSProtoI;
602 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000603 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000604 match = true;
605 break;
606 }
607 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000608 if (rhsQI) {
609 // If the RHS is a qualified interface pointer "NSString<P>*",
610 // make sure we check the class hierarchy.
611 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
612 ObjCInterfaceDecl *rhsID = IT->getDecl();
613 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
614 // when comparing an id<P> on lhs with a static type on rhs,
615 // see if static class implements all of id's protocols, directly or
616 // through its super class and categories.
617 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
618 match = true;
619 break;
620 }
621 }
622 }
623 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000624 if (!match)
625 return false;
626 }
627
628 return true;
629 }
630
631 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
632 assert(rhsQID && "One of the LHS/RHS should be id<x>");
633
634 if (!lhs->isPointerType())
635 return false;
636
637 QualType ltype = lhs->getAsPointerType()->getPointeeType();
638 if (const ObjCQualifiedInterfaceType *lhsQI =
639 ltype->getAsObjCQualifiedInterfaceType()) {
640 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
641 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
642 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
643 bool match = false;
644 ObjCProtocolDecl *lhsProto = *LHSProtoI;
645 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
646 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
647 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000648 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000649 match = true;
650 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000651 }
652 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000653 if (!match)
654 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000655 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000656 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000657 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000658
659 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
660 // for static type vs. qualified 'id' type, check that class implements
661 // all of 'id's protocols.
662 ObjCInterfaceDecl *lhsID = IT->getDecl();
663 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
664 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000665 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000666 return false;
667 }
668 return true;
669 }
670 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000671}
672