blob: 5dba6846a343e5744388055526aeb42931f5bafb [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
Chris Lattner69039812009-02-18 06:01:06 +000051 // Verify that this composite string is acceptable for ObjC strings.
52 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000053 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000054
55 // Initialize the constant string interface lazily. This assumes
56 // the NSConstantString interface is seen in this translation unit.
57 QualType Ty = Context.getObjCConstantStringInterface();
58 if (!Ty.isNull()) {
59 Ty = Context.getPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000060 } else {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000061 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
62 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
63 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
64 Context.setObjCConstantStringInterface(StrIF);
65 Ty = Context.getObjCConstantStringInterface();
66 Ty = Context.getPointerType(Ty);
67 } else {
68 // If there is no NSConstantString interface defined then treat constant
69 // strings as untyped objects and let the runtime figure it out later.
70 Ty = Context.getObjCIdType();
71 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000072 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +000073
74 return new (Context) ObjCStringLiteral(S, Ty, AtLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000075}
76
77Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
78 SourceLocation EncodeLoc,
79 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +000080 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +000081 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000082 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +000083
Chris Lattnera0af1fe2009-02-18 06:06:56 +000084 QualType Ty = Context.getPointerType(Context.CharTy);
85 return new (Context) ObjCEncodeExpr(Ty, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000086}
87
88Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
89 SourceLocation AtLoc,
90 SourceLocation SelLoc,
91 SourceLocation LParenLoc,
92 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000093 QualType Ty = Context.getObjCSelType();
94 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000095}
96
97Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
98 SourceLocation AtLoc,
99 SourceLocation ProtoLoc,
100 SourceLocation LParenLoc,
101 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000103 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000104 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000105 return true;
106 }
107
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000108 QualType Ty = Context.getObjCProtoType();
109 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000110 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000111 Ty = Context.getPointerType(Ty);
112 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000113}
114
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000115bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
116 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000117 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000118 SourceLocation lbrac, SourceLocation rbrac,
119 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000120 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000121 // Apply default argument promotion as for (C99 6.5.2.2p6).
122 for (unsigned i = 0; i != NumArgs; i++)
123 DefaultArgumentPromotion(Args[i]);
124
Chris Lattner077bf5e2008-11-24 03:33:13 +0000125 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
126 diag::warn_inst_method_not_found;
127 Diag(lbrac, DiagID)
128 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000129 ReturnType = Context.getObjCIdType();
130 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000131 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000132
133 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000134
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000135 unsigned NumNamedArgs = Sel.getNumArgs();
136 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
137
Chris Lattner85a932e2008-01-04 22:32:30 +0000138 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000139 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000140 Expr *argExpr = Args[i];
141 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
142
143 QualType lhsType = Method->getParamDecl(i)->getType();
144 QualType rhsType = argExpr->getType();
145
146 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000147 if (lhsType->isArrayType())
148 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000149 else if (lhsType->isFunctionType())
150 lhsType = Context.getPointerType(lhsType);
151
Chris Lattner987798a2008-04-02 17:17:33 +0000152 AssignConvertType Result =
153 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000154 if (Args[i] != argExpr) // The expression was converted.
155 Args[i] = argExpr; // Make sure we store the converted expression.
156
157 anyIncompatibleArgs |=
158 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
159 argExpr, "sending");
160 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000161
162 // Promote additional arguments to variadic methods.
163 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000164 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
165 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000166 } else {
167 // Check for extra arguments to non-variadic methods.
168 if (NumArgs != NumNamedArgs) {
169 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000170 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000171 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000172 << SourceRange(Args[NumNamedArgs]->getLocStart(),
173 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000174 }
175 }
176
Chris Lattner85a932e2008-01-04 22:32:30 +0000177 return anyIncompatibleArgs;
178}
179
180// ActOnClassMessage - used for both unary and keyword messages.
181// ArgExprs is optional - if it is present, the number of expressions
182// is obtained from Sel.getNumArgs().
183Sema::ExprResult Sema::ActOnClassMessage(
184 Scope *S,
185 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000186 SourceLocation lbrac, SourceLocation receiverLoc,
187 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000188 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000189{
190 assert(receiverName && "missing receiver class name");
191
192 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000193 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000194 bool isSuper = false;
195
Chris Lattner84692652008-11-20 05:35:30 +0000196 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000197 if (getCurMethodDecl()) {
198 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000199 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
200 if (!OID)
201 return Diag(lbrac, diag::error_no_super_class_message)
202 << getCurMethodDecl()->getDeclName();
203 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000204 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000205 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000206 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000207 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
208 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000209 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
210 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000211 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000212 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
213 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000214 }
215 // We are sending a message to 'super' within a class method. Do nothing,
216 // the receiver will pass through as 'super' (how convenient:-).
217 } else {
218 // 'super' has been used outside a method context. If a variable named
219 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000220 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000221 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
222 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000223 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
224 receiverLoc);
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 }
Chris Lattner08631c52008-11-23 21:45:46 +0000229 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000230 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000231 } else
232 ClassDecl = getObjCInterfaceDecl(receiverName);
233
Steve Naroff7c778f12008-07-25 19:39:00 +0000234 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000235 //
236 // typedef XCElementDisplayRect XCElementGraphicsRect;
237 //
238 // @implementation XCRASlice
239 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
240 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
241 // }
242 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000243 // If necessary, the following lookup could move to getObjCInterfaceDecl().
244 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000245 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000246 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
247 const ObjCInterfaceType *OCIT;
248 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000249 if (!OCIT)
250 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
251 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000252 }
253 }
254 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000255 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000256 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000257 Method = ClassDecl->lookupClassMethod(Sel);
258
259 // If we have an implementation in scope, check "private" methods.
260 if (!Method) {
261 if (ObjCImplementationDecl *ImpDecl =
262 ObjCImplementations[ClassDecl->getIdentifier()])
263 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000264
265 // Look through local category implementations associated with the class.
266 if (!Method) {
267 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
268 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
269 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
270 }
271 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000272 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000273 // Before we give up, check if the selector is an instance method.
274 if (!Method)
275 Method = ClassDecl->lookupInstanceMethod(Sel);
276
Chris Lattner76a642f2009-02-15 22:43:40 +0000277 if (Method)
278 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000279
Chris Lattner077bf5e2008-11-24 03:33:13 +0000280 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000281 lbrac, rbrac, returnType))
282 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000283
284 // If we have the ObjCInterfaceDecl* for the class that is receiving
285 // the message, use that to construct the ObjCMessageExpr. Otherwise
286 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000287 // FIXME: need to do a better job handling 'super' usage within a class
288 // For now, we simply pass the "super" identifier through (which isn't
289 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000290 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000291 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
292 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000293 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000294 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
295 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000296}
297
298// ActOnInstanceMessage - used for both unary and keyword messages.
299// ArgExprs is optional - if it is present, the number of expressions
300// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000301Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000302 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000303 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000304 SourceLocation rbrac,
305 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000306 assert(receiver && "missing receiver expression");
307
308 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
309 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000310 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000311
Chris Lattnerb77792e2008-07-26 22:17:49 +0000312 QualType ReceiverCType =
313 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000314
315 // Handle messages to 'super'.
316 if (isa<ObjCSuperExpr>(RExpr)) {
317 ObjCMethodDecl *Method = 0;
318 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
319 // If we have an interface in scope, check 'super' methods.
320 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
321 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
322 Method = SuperDecl->lookupInstanceMethod(Sel);
323 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000324
Chris Lattner76a642f2009-02-15 22:43:40 +0000325 if (Method)
326 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000327
Chris Lattner077bf5e2008-11-24 03:33:13 +0000328 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000329 lbrac, rbrac, returnType))
330 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000331 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
332 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000333 }
334
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000335 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000336 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
337 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000338 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
339 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000340 if (!Method)
341 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000342 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000343 lbrac, rbrac, returnType))
344 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000345 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
346 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000347 }
348
349 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000350 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000351 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000352 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000353 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000354 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000355 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000356 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000357 Method = ImpDecl->getClassMethod(Sel);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000358
Chris Lattner76a642f2009-02-15 22:43:40 +0000359 if (Method)
360 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Steve Naroffe2af8b12008-06-05 14:49:39 +0000361 }
362 if (!Method)
363 Method = FactoryMethodPool[Sel].Method;
364 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000365 Method = LookupInstanceMethodInGlobalPool(
366 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000367 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000368 lbrac, rbrac, returnType))
369 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000370 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
371 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000372 }
373
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000374 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000375 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000376
377 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
378 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000379 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000380 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000381 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
382 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
383 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
384 break;
385 }
386 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000387 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000388 << Sel << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000389 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000390 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000391 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000392
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000393 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000394 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
395 // faster than the following method (which can do *many* linear searches).
396 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000397 Method = ClassDecl->lookupInstanceMethod(Sel);
398
399 if (!Method) {
400 // Search protocol qualifiers.
401 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
402 E = OCIReceiver->qual_end(); QI != E; ++QI) {
403 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000404 break;
405 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000406 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000407
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000408 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000409 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000410 << Sel << SourceRange(lbrac, rbrac);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000411
Chris Lattner76a642f2009-02-15 22:43:40 +0000412 if (Method)
413 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000414 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000415 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000416 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000417 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000418 }
419
420 if (!Method) {
421 // If we have an implementation in scope, check "private" methods.
422 if (ClassDecl)
423 if (ObjCImplementationDecl *ImpDecl =
424 ObjCImplementations[ClassDecl->getIdentifier()])
425 Method = ImpDecl->getInstanceMethod(Sel);
426 // If we still haven't found a method, look in the global pool. This
427 // behavior isn't very desirable, however we need it for GCC
428 // compatibility.
429 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000430 Method = LookupInstanceMethodInGlobalPool(
431 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000432 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000433 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000434 lbrac, rbrac, returnType))
435 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000436 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
437 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000438}
Chris Lattnereca7be62008-04-07 05:30:13 +0000439
440//===----------------------------------------------------------------------===//
441// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
442//===----------------------------------------------------------------------===//
443
444/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
445/// inheritance hierarchy of 'rProto'.
446static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
447 ObjCProtocolDecl *rProto) {
448 if (lProto == rProto)
449 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000450 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
451 E = rProto->protocol_end(); PI != E; ++PI)
452 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000453 return true;
454 return false;
455}
456
457/// ClassImplementsProtocol - Checks that 'lProto' protocol
458/// has been implemented in IDecl class, its super class or categories (if
459/// lookupCategory is true).
460static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
461 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000462 bool lookupCategory,
463 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000464
465 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000466 const ObjCList<ObjCProtocolDecl> &Protocols =
467 IDecl->getReferencedProtocols();
468
469 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
470 E = Protocols.end(); PI != E; ++PI) {
471 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000472 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000473 // This is dubious and is added to be compatible with gcc.
474 // In gcc, it is also allowed assigning a protocol-qualified 'id'
475 // type to a LHS object when protocol in qualified LHS is in list
476 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000477 // FIXME: Treat this as an extension, and flag this as an error when
478 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000479 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000480 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000481 }
482
483 // 2nd, look up the category.
484 if (lookupCategory)
485 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
486 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000487 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
488 E = CDecl->protocol_end(); PI != E; ++PI)
489 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000490 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000491 }
492
493 // 3rd, look up the super class(s)
494 if (IDecl->getSuperClass())
495 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000496 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
497 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000498
499 return false;
500}
501
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000502/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
503/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000504bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
505 bool compare) {
506 // Allow id<P..> and an 'id' or void* type in all cases.
507 if (const PointerType *PT = lhs->getAsPointerType()) {
508 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000509 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000510 return true;
511 } else if (const PointerType *PT = rhs->getAsPointerType()) {
512 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000513 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000514 return true;
515 }
516
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000517 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
518 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
519 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000520 QualType rtype;
521
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000522 if (!rhsQID) {
523 // Not comparing two ObjCQualifiedIdType's?
524 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000525
526 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000527 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000528 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000529 // If the RHS is a unqualified interface pointer "NSString*",
530 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000531 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
532 ObjCInterfaceDecl *rhsID = IT->getDecl();
533 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
534 // when comparing an id<P> on lhs with a static type on rhs,
535 // see if static class implements all of id's protocols, directly or
536 // through its super class and categories.
537 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
538 return false;
539 }
540 return true;
541 }
542 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000543 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000544
545 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000546 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000547 RHSProtoI = rhsQI->qual_begin();
548 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000549 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000550 RHSProtoI = rhsQID->qual_begin();
551 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000552 } else {
553 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000554 }
555
556 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
557 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
558 bool match = false;
559
560 // when comparing an id<P> on lhs with a static type on rhs,
561 // see if static class implements all of id's protocols, directly or
562 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000563 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
564 ObjCProtocolDecl *rhsProto = *RHSProtoI;
565 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000566 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000567 match = true;
568 break;
569 }
570 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000571 if (rhsQI) {
572 // If the RHS is a qualified interface pointer "NSString<P>*",
573 // make sure we check the class hierarchy.
574 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
575 ObjCInterfaceDecl *rhsID = IT->getDecl();
576 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
577 // when comparing an id<P> on lhs with a static type on rhs,
578 // see if static class implements all of id's protocols, directly or
579 // through its super class and categories.
580 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
581 match = true;
582 break;
583 }
584 }
585 }
586 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000587 if (!match)
588 return false;
589 }
590
591 return true;
592 }
593
594 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
595 assert(rhsQID && "One of the LHS/RHS should be id<x>");
596
597 if (!lhs->isPointerType())
598 return false;
599
600 QualType ltype = lhs->getAsPointerType()->getPointeeType();
601 if (const ObjCQualifiedInterfaceType *lhsQI =
602 ltype->getAsObjCQualifiedInterfaceType()) {
603 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
604 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
605 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
606 bool match = false;
607 ObjCProtocolDecl *lhsProto = *LHSProtoI;
608 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
609 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
610 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000611 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000612 match = true;
613 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000614 }
615 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000616 if (!match)
617 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000618 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000619 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000620 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000621
622 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
623 // for static type vs. qualified 'id' type, check that class implements
624 // all of 'id's protocols.
625 ObjCInterfaceDecl *lhsID = IT->getDecl();
626 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
627 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000628 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000629 return false;
630 }
631 return true;
632 }
633 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000634}
635