blob: b0b5367790817937df4877a5e191dd3e5793bbe9 [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 Lattner85a932e2008-01-04 22:32:30 +000018using namespace clang;
19
20Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
21 ExprTy **Strings,
22 unsigned NumStrings) {
23 SourceLocation AtLoc = AtLocs[0];
24 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
25 if (NumStrings > 1) {
26 // Concatenate objc strings.
27 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
28 SourceLocation EndLoc = ES->getSourceRange().getEnd();
29 unsigned Length = 0;
30 for (unsigned i = 0; i < NumStrings; i++)
31 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
Chris Lattner726e1682009-02-18 05:49:11 +000032
33 // FIXME: This should not be allocated by SEMA!
34 char *strBuf = new char[Length];
Chris Lattner85a932e2008-01-04 22:32:30 +000035 char *p = strBuf;
36 bool isWide = false;
Chris Lattner726e1682009-02-18 05:49:11 +000037 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner85a932e2008-01-04 22:32:30 +000038 S = static_cast<StringLiteral *>(Strings[i]);
39 if (S->isWide())
40 isWide = true;
41 memcpy(p, S->getStrData(), S->getByteLength());
42 p += S->getByteLength();
Ted Kremenek8189cde2009-02-07 01:47:29 +000043 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000044 }
Chris Lattner726e1682009-02-18 05:49:11 +000045 // FIXME: PASS LOCATIONS PROPERLY.
46 S = new (Context) StringLiteral(Context, strBuf, Length, isWide,
47 Context.getPointerType(Context.CharTy),
48 AtLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000049 }
50
51 if (CheckBuiltinCFStringArgument(S))
52 return true;
53
Ted Kremeneka526c5c2008-01-07 19:49:32 +000054 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-01-04 22:32:30 +000055 // Initialize the constant string interface lazily. This assumes
56 // the NSConstantString interface is seen in this translation unit.
57 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000058 NamedDecl *IFace = LookupName(TUScope, NSIdent, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000059 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner13fd7e52008-06-21 21:44:18 +000060 if (strIFace)
61 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000062 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000063 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-06-21 21:44:18 +000064 // If there is no NSConstantString interface defined then treat constant
65 // strings as untyped objects and let the runtime figure it out later.
66 if (t == QualType()) {
67 t = Context.getObjCIdType();
68 } else {
69 t = Context.getPointerType(t);
70 }
Ted Kremenek8189cde2009-02-07 01:47:29 +000071 return new (Context) ObjCStringLiteral(S, t, AtLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000072}
73
74Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
75 SourceLocation EncodeLoc,
76 SourceLocation LParenLoc,
77 TypeTy *Ty,
78 SourceLocation RParenLoc) {
79 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
80
81 QualType t = Context.getPointerType(Context.CharTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +000082 return new (Context) ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000083}
84
85Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
86 SourceLocation AtLoc,
87 SourceLocation SelLoc,
88 SourceLocation LParenLoc,
89 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000090 QualType t = Context.getObjCSelType();
Ted Kremenek8189cde2009-02-07 01:47:29 +000091 return new (Context) ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000092}
93
94Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
95 SourceLocation AtLoc,
96 SourceLocation ProtoLoc,
97 SourceLocation LParenLoc,
98 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000099 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000100 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000101 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000102 return true;
103 }
104
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000105 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000106 if (t.isNull())
107 return true;
108 t = Context.getPointerType(t);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000109 return new (Context) ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000110}
111
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000112bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
113 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000114 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000115 SourceLocation lbrac, SourceLocation rbrac,
116 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000117 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000118 // Apply default argument promotion as for (C99 6.5.2.2p6).
119 for (unsigned i = 0; i != NumArgs; i++)
120 DefaultArgumentPromotion(Args[i]);
121
Chris Lattner077bf5e2008-11-24 03:33:13 +0000122 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
123 diag::warn_inst_method_not_found;
124 Diag(lbrac, DiagID)
125 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000126 ReturnType = Context.getObjCIdType();
127 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000128 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000129
130 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000131
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000132 unsigned NumNamedArgs = Sel.getNumArgs();
133 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
134
Chris Lattner85a932e2008-01-04 22:32:30 +0000135 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000136 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000137 Expr *argExpr = Args[i];
138 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
139
140 QualType lhsType = Method->getParamDecl(i)->getType();
141 QualType rhsType = argExpr->getType();
142
143 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000144 if (lhsType->isArrayType())
145 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000146 else if (lhsType->isFunctionType())
147 lhsType = Context.getPointerType(lhsType);
148
Chris Lattner987798a2008-04-02 17:17:33 +0000149 AssignConvertType Result =
150 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000151 if (Args[i] != argExpr) // The expression was converted.
152 Args[i] = argExpr; // Make sure we store the converted expression.
153
154 anyIncompatibleArgs |=
155 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
156 argExpr, "sending");
157 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000158
159 // Promote additional arguments to variadic methods.
160 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000161 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
162 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000163 } else {
164 // Check for extra arguments to non-variadic methods.
165 if (NumArgs != NumNamedArgs) {
166 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000167 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000168 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000169 << SourceRange(Args[NumNamedArgs]->getLocStart(),
170 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000171 }
172 }
173
Chris Lattner85a932e2008-01-04 22:32:30 +0000174 return anyIncompatibleArgs;
175}
176
177// ActOnClassMessage - used for both unary and keyword messages.
178// ArgExprs is optional - if it is present, the number of expressions
179// is obtained from Sel.getNumArgs().
180Sema::ExprResult Sema::ActOnClassMessage(
181 Scope *S,
182 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000183 SourceLocation lbrac, SourceLocation receiverLoc,
184 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000185 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000186{
187 assert(receiverName && "missing receiver class name");
188
189 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000191 bool isSuper = false;
192
Chris Lattner84692652008-11-20 05:35:30 +0000193 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000194 if (getCurMethodDecl()) {
195 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000196 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
197 if (!OID)
198 return Diag(lbrac, diag::error_no_super_class_message)
199 << getCurMethodDecl()->getDeclName();
200 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000201 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000202 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000203 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000204 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
205 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000206 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
207 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000208 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000209 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
210 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000211 }
212 // We are sending a message to 'super' within a class method. Do nothing,
213 // the receiver will pass through as 'super' (how convenient:-).
214 } else {
215 // 'super' has been used outside a method context. If a variable named
216 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000217 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000218 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
219 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000220 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
221 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000222 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000223 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
224 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000225 }
Chris Lattner08631c52008-11-23 21:45:46 +0000226 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000227 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000228 } else
229 ClassDecl = getObjCInterfaceDecl(receiverName);
230
Steve Naroff7c778f12008-07-25 19:39:00 +0000231 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000232 //
233 // typedef XCElementDisplayRect XCElementGraphicsRect;
234 //
235 // @implementation XCRASlice
236 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
237 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
238 // }
239 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000240 // If necessary, the following lookup could move to getObjCInterfaceDecl().
241 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000242 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000243 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
244 const ObjCInterfaceType *OCIT;
245 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000246 if (!OCIT)
247 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
248 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000249 }
250 }
251 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000252 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000253 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000254 Method = ClassDecl->lookupClassMethod(Sel);
255
256 // If we have an implementation in scope, check "private" methods.
257 if (!Method) {
258 if (ObjCImplementationDecl *ImpDecl =
259 ObjCImplementations[ClassDecl->getIdentifier()])
260 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000261
262 // Look through local category implementations associated with the class.
263 if (!Method) {
264 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
265 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
266 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
267 }
268 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000269 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000270 // Before we give up, check if the selector is an instance method.
271 if (!Method)
272 Method = ClassDecl->lookupInstanceMethod(Sel);
273
Chris Lattner76a642f2009-02-15 22:43:40 +0000274 if (Method)
275 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000276
Chris Lattner077bf5e2008-11-24 03:33:13 +0000277 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000278 lbrac, rbrac, returnType))
279 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000280
281 // If we have the ObjCInterfaceDecl* for the class that is receiving
282 // the message, use that to construct the ObjCMessageExpr. Otherwise
283 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000284 // FIXME: need to do a better job handling 'super' usage within a class
285 // For now, we simply pass the "super" identifier through (which isn't
286 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000287 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000288 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
289 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000290 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000291 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
292 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000293}
294
295// ActOnInstanceMessage - used for both unary and keyword messages.
296// ArgExprs is optional - if it is present, the number of expressions
297// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000298Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000299 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000300 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000301 SourceLocation rbrac,
302 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000303 assert(receiver && "missing receiver expression");
304
305 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
306 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000307 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000308
Chris Lattnerb77792e2008-07-26 22:17:49 +0000309 QualType ReceiverCType =
310 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000311
312 // Handle messages to 'super'.
313 if (isa<ObjCSuperExpr>(RExpr)) {
314 ObjCMethodDecl *Method = 0;
315 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
316 // If we have an interface in scope, check 'super' methods.
317 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
318 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
319 Method = SuperDecl->lookupInstanceMethod(Sel);
320 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000321
Chris Lattner76a642f2009-02-15 22:43:40 +0000322 if (Method)
323 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000324
Chris Lattner077bf5e2008-11-24 03:33:13 +0000325 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000326 lbrac, rbrac, returnType))
327 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000328 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
329 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000330 }
331
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000332 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000333 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
334 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000335 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
336 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000337 if (!Method)
338 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000339 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000340 lbrac, rbrac, returnType))
341 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000342 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
343 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000344 }
345
346 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000347 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000348 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000349 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000350 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000351 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000352 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000353 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000354 Method = ImpDecl->getClassMethod(Sel);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000355
Chris Lattner76a642f2009-02-15 22:43:40 +0000356 if (Method)
357 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Steve Naroffe2af8b12008-06-05 14:49:39 +0000358 }
359 if (!Method)
360 Method = FactoryMethodPool[Sel].Method;
361 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000362 Method = LookupInstanceMethodInGlobalPool(
363 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000364 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000365 lbrac, rbrac, returnType))
366 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000367 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
368 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000369 }
370
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000371 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000372 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000373
374 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
375 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000376 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000377 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000378 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
379 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
380 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
381 break;
382 }
383 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000384 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000385 << Sel << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000386 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000387 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000388 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000389
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000390 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000391 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
392 // faster than the following method (which can do *many* linear searches).
393 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000394 Method = ClassDecl->lookupInstanceMethod(Sel);
395
396 if (!Method) {
397 // Search protocol qualifiers.
398 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
399 E = OCIReceiver->qual_end(); QI != E; ++QI) {
400 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000401 break;
402 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000403 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000404
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000405 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000406 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000407 << Sel << SourceRange(lbrac, rbrac);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000408
Chris Lattner76a642f2009-02-15 22:43:40 +0000409 if (Method)
410 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000411 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000412 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000413 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000414 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000415 }
416
417 if (!Method) {
418 // If we have an implementation in scope, check "private" methods.
419 if (ClassDecl)
420 if (ObjCImplementationDecl *ImpDecl =
421 ObjCImplementations[ClassDecl->getIdentifier()])
422 Method = ImpDecl->getInstanceMethod(Sel);
423 // If we still haven't found a method, look in the global pool. This
424 // behavior isn't very desirable, however we need it for GCC
425 // compatibility.
426 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000427 Method = LookupInstanceMethodInGlobalPool(
428 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000429 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000430 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000431 lbrac, rbrac, returnType))
432 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000433 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
434 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000435}
Chris Lattnereca7be62008-04-07 05:30:13 +0000436
437//===----------------------------------------------------------------------===//
438// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
439//===----------------------------------------------------------------------===//
440
441/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
442/// inheritance hierarchy of 'rProto'.
443static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
444 ObjCProtocolDecl *rProto) {
445 if (lProto == rProto)
446 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000447 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
448 E = rProto->protocol_end(); PI != E; ++PI)
449 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000450 return true;
451 return false;
452}
453
454/// ClassImplementsProtocol - Checks that 'lProto' protocol
455/// has been implemented in IDecl class, its super class or categories (if
456/// lookupCategory is true).
457static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
458 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000459 bool lookupCategory,
460 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000461
462 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000463 const ObjCList<ObjCProtocolDecl> &Protocols =
464 IDecl->getReferencedProtocols();
465
466 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
467 E = Protocols.end(); PI != E; ++PI) {
468 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000469 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000470 // This is dubious and is added to be compatible with gcc.
471 // In gcc, it is also allowed assigning a protocol-qualified 'id'
472 // type to a LHS object when protocol in qualified LHS is in list
473 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000474 // FIXME: Treat this as an extension, and flag this as an error when
475 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000476 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000477 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000478 }
479
480 // 2nd, look up the category.
481 if (lookupCategory)
482 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
483 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000484 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
485 E = CDecl->protocol_end(); PI != E; ++PI)
486 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000487 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000488 }
489
490 // 3rd, look up the super class(s)
491 if (IDecl->getSuperClass())
492 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000493 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
494 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000495
496 return false;
497}
498
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000499/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
500/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000501bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
502 bool compare) {
503 // Allow id<P..> and an 'id' or void* type in all cases.
504 if (const PointerType *PT = lhs->getAsPointerType()) {
505 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000506 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000507 return true;
508 } else if (const PointerType *PT = rhs->getAsPointerType()) {
509 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000510 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000511 return true;
512 }
513
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000514 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
515 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
516 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000517 QualType rtype;
518
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000519 if (!rhsQID) {
520 // Not comparing two ObjCQualifiedIdType's?
521 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000522
523 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000524 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000525 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000526 // If the RHS is a unqualified interface pointer "NSString*",
527 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000528 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
529 ObjCInterfaceDecl *rhsID = IT->getDecl();
530 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
531 // when comparing an id<P> on lhs with a static type on rhs,
532 // see if static class implements all of id's protocols, directly or
533 // through its super class and categories.
534 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
535 return false;
536 }
537 return true;
538 }
539 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000540 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000541
542 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000543 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000544 RHSProtoI = rhsQI->qual_begin();
545 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000546 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000547 RHSProtoI = rhsQID->qual_begin();
548 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000549 } else {
550 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000551 }
552
553 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
554 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
555 bool match = false;
556
557 // when comparing an id<P> on lhs with a static type on rhs,
558 // see if static class implements all of id's protocols, directly or
559 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000560 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
561 ObjCProtocolDecl *rhsProto = *RHSProtoI;
562 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000563 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000564 match = true;
565 break;
566 }
567 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000568 if (rhsQI) {
569 // If the RHS is a qualified interface pointer "NSString<P>*",
570 // make sure we check the class hierarchy.
571 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
572 ObjCInterfaceDecl *rhsID = IT->getDecl();
573 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
574 // when comparing an id<P> on lhs with a static type on rhs,
575 // see if static class implements all of id's protocols, directly or
576 // through its super class and categories.
577 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
578 match = true;
579 break;
580 }
581 }
582 }
583 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000584 if (!match)
585 return false;
586 }
587
588 return true;
589 }
590
591 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
592 assert(rhsQID && "One of the LHS/RHS should be id<x>");
593
594 if (!lhs->isPointerType())
595 return false;
596
597 QualType ltype = lhs->getAsPointerType()->getPointeeType();
598 if (const ObjCQualifiedInterfaceType *lhsQI =
599 ltype->getAsObjCQualifiedInterfaceType()) {
600 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
601 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
602 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
603 bool match = false;
604 ObjCProtocolDecl *lhsProto = *LHSProtoI;
605 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
606 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
607 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000608 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000609 match = true;
610 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000611 }
612 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000613 if (!match)
614 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000615 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000616 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000617 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000618
619 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
620 // for static type vs. qualified 'id' type, check that class implements
621 // all of 'id's protocols.
622 ObjCInterfaceDecl *lhsID = IT->getDecl();
623 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
624 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000625 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000626 return false;
627 }
628 return true;
629 }
630 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000631}
632