blob: a19a1d16d0e67cd02598e6685916f88185018f4e [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 Lattner20c6b3b2009-01-27 18:30:58 +000018#include "clang/Basic/DiagnosticSema.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000019using namespace clang;
20
21Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
22 ExprTy **Strings,
23 unsigned NumStrings) {
24 SourceLocation AtLoc = AtLocs[0];
25 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
26 if (NumStrings > 1) {
27 // Concatenate objc strings.
28 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
29 SourceLocation EndLoc = ES->getSourceRange().getEnd();
30 unsigned Length = 0;
31 for (unsigned i = 0; i < NumStrings; i++)
32 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
33 char *strBuf = new char [Length];
34 char *p = strBuf;
35 bool isWide = false;
36 for (unsigned i = 0; i < NumStrings; i++) {
37 S = static_cast<StringLiteral *>(Strings[i]);
38 if (S->isWide())
39 isWide = true;
40 memcpy(p, S->getStrData(), S->getByteLength());
41 p += S->getByteLength();
42 delete S;
43 }
44 S = new StringLiteral(strBuf, Length,
45 isWide, Context.getPointerType(Context.CharTy),
46 AtLoc, EndLoc);
47 }
48
49 if (CheckBuiltinCFStringArgument(S))
50 return true;
51
Ted Kremeneka526c5c2008-01-07 19:49:32 +000052 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-01-04 22:32:30 +000053 // Initialize the constant string interface lazily. This assumes
54 // the NSConstantString interface is seen in this translation unit.
55 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Steve Naroff3e8ffd22009-01-29 00:07:50 +000056 Decl *IFace = LookupDeclInScope(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner13fd7e52008-06-21 21:44:18 +000058 if (strIFace)
59 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000060 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-06-21 21:44:18 +000062 // If there is no NSConstantString interface defined then treat constant
63 // strings as untyped objects and let the runtime figure it out later.
64 if (t == QualType()) {
65 t = Context.getObjCIdType();
66 } else {
67 t = Context.getPointerType(t);
68 }
Chris Lattner85a932e2008-01-04 22:32:30 +000069 return new ObjCStringLiteral(S, t, AtLoc);
70}
71
72Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
73 SourceLocation EncodeLoc,
74 SourceLocation LParenLoc,
75 TypeTy *Ty,
76 SourceLocation RParenLoc) {
77 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
78
79 QualType t = Context.getPointerType(Context.CharTy);
80 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
81}
82
83Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
84 SourceLocation AtLoc,
85 SourceLocation SelLoc,
86 SourceLocation LParenLoc,
87 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 QualType t = Context.getObjCSelType();
Chris Lattner85a932e2008-01-04 22:32:30 +000089 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
90}
91
92Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
93 SourceLocation AtLoc,
94 SourceLocation ProtoLoc,
95 SourceLocation LParenLoc,
96 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000098 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +000099 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000100 return true;
101 }
102
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000104 if (t.isNull())
105 return true;
106 t = Context.getPointerType(t);
107 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
108}
109
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000110bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
111 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000112 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000113 SourceLocation lbrac, SourceLocation rbrac,
114 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000115 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000116 // Apply default argument promotion as for (C99 6.5.2.2p6).
117 for (unsigned i = 0; i != NumArgs; i++)
118 DefaultArgumentPromotion(Args[i]);
119
Chris Lattner077bf5e2008-11-24 03:33:13 +0000120 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
121 diag::warn_inst_method_not_found;
122 Diag(lbrac, DiagID)
123 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000124 ReturnType = Context.getObjCIdType();
125 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000126 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000127
128 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000129
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000130 unsigned NumNamedArgs = Sel.getNumArgs();
131 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
132
Chris Lattner85a932e2008-01-04 22:32:30 +0000133 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000134 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000135 Expr *argExpr = Args[i];
136 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
137
138 QualType lhsType = Method->getParamDecl(i)->getType();
139 QualType rhsType = argExpr->getType();
140
141 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000142 if (lhsType->isArrayType())
143 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000144 else if (lhsType->isFunctionType())
145 lhsType = Context.getPointerType(lhsType);
146
Chris Lattner987798a2008-04-02 17:17:33 +0000147 AssignConvertType Result =
148 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000149 if (Args[i] != argExpr) // The expression was converted.
150 Args[i] = argExpr; // Make sure we store the converted expression.
151
152 anyIncompatibleArgs |=
153 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
154 argExpr, "sending");
155 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000156
157 // Promote additional arguments to variadic methods.
158 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000159 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
160 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000161 } else {
162 // Check for extra arguments to non-variadic methods.
163 if (NumArgs != NumNamedArgs) {
164 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000165 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000166 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000167 << SourceRange(Args[NumNamedArgs]->getLocStart(),
168 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000169 }
170 }
171
Chris Lattner85a932e2008-01-04 22:32:30 +0000172 return anyIncompatibleArgs;
173}
174
175// ActOnClassMessage - used for both unary and keyword messages.
176// ArgExprs is optional - if it is present, the number of expressions
177// is obtained from Sel.getNumArgs().
178Sema::ExprResult Sema::ActOnClassMessage(
179 Scope *S,
180 IdentifierInfo *receiverName, Selector Sel,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000181 SourceLocation lbrac, SourceLocation receiverLoc, SourceLocation rbrac,
182 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000183{
184 assert(receiverName && "missing receiver class name");
185
186 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000187 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000188 bool isSuper = false;
189
Chris Lattner84692652008-11-20 05:35:30 +0000190 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000191 if (getCurMethodDecl()) {
192 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000193 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
194 if (!OID)
195 return Diag(lbrac, diag::error_no_super_class_message)
196 << getCurMethodDecl()->getDeclName();
197 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000198 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000199 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000200 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000201 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
202 superTy = Context.getPointerType(superTy);
203 ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy);
204 // We are really in an instance method, redirect.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000205 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000206 Args, NumArgs);
207 }
208 // We are sending a message to 'super' within a class method. Do nothing,
209 // the receiver will pass through as 'super' (how convenient:-).
210 } else {
211 // 'super' has been used outside a method context. If a variable named
212 // 'super' has been declared, redirect. If not, produce a diagnostic.
Steve Naroff3e8ffd22009-01-29 00:07:50 +0000213 Decl *SuperDecl = LookupDeclInScope(receiverName, Decl::IDNS_Ordinary, S);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000214 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
215 if (VD) {
216 ExprResult ReceiverExpr = new DeclRefExpr(VD, VD->getType(),
217 receiverLoc);
218 // We are really in an instance method, redirect.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000219 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000220 Args, NumArgs);
221 }
Chris Lattner08631c52008-11-23 21:45:46 +0000222 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000223 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000224 } else
225 ClassDecl = getObjCInterfaceDecl(receiverName);
226
Steve Naroff7c778f12008-07-25 19:39:00 +0000227 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000228 //
229 // typedef XCElementDisplayRect XCElementGraphicsRect;
230 //
231 // @implementation XCRASlice
232 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
233 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
234 // }
235 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000236 // If necessary, the following lookup could move to getObjCInterfaceDecl().
237 if (!ClassDecl) {
Steve Naroff3e8ffd22009-01-29 00:07:50 +0000238 Decl *IDecl = LookupDeclInScope(receiverName, Decl::IDNS_Ordinary, 0);
Steve Naroff7c778f12008-07-25 19:39:00 +0000239 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
240 const ObjCInterfaceType *OCIT;
241 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000242 if (!OCIT)
243 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
244 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000245 }
246 }
247 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000248 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000249 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000250 Method = ClassDecl->lookupClassMethod(Sel);
251
252 // If we have an implementation in scope, check "private" methods.
253 if (!Method) {
254 if (ObjCImplementationDecl *ImpDecl =
255 ObjCImplementations[ClassDecl->getIdentifier()])
256 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000257
258 // Look through local category implementations associated with the class.
259 if (!Method) {
260 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
261 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
262 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
263 }
264 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000265 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000266 // Before we give up, check if the selector is an instance method.
267 if (!Method)
268 Method = ClassDecl->lookupInstanceMethod(Sel);
269
Chris Lattner077bf5e2008-11-24 03:33:13 +0000270 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000271 lbrac, rbrac, returnType))
272 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000273
274 // If we have the ObjCInterfaceDecl* for the class that is receiving
275 // the message, use that to construct the ObjCMessageExpr. Otherwise
276 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000277 // FIXME: need to do a better job handling 'super' usage within a class
278 // For now, we simply pass the "super" identifier through (which isn't
279 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000280 if (isSuper)
Steve Narofffc93d522008-07-24 19:44:33 +0000281 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenek4df728e2008-06-24 15:50:53 +0000282 lbrac, rbrac, ArgExprs, NumArgs);
283 else
Steve Narofffc93d522008-07-24 19:44:33 +0000284 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenek4df728e2008-06-24 15:50:53 +0000285 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000286}
287
288// ActOnInstanceMessage - used for both unary and keyword messages.
289// ArgExprs is optional - if it is present, the number of expressions
290// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000291Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000292 SourceLocation lbrac,
293 SourceLocation rbrac,
294 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000295 assert(receiver && "missing receiver expression");
296
297 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
298 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000299 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000300
Chris Lattnerb77792e2008-07-26 22:17:49 +0000301 QualType ReceiverCType =
302 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000303
304 // Handle messages to 'super'.
305 if (isa<ObjCSuperExpr>(RExpr)) {
306 ObjCMethodDecl *Method = 0;
307 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
308 // If we have an interface in scope, check 'super' methods.
309 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
310 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
311 Method = SuperDecl->lookupInstanceMethod(Sel);
312 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000313 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000314 lbrac, rbrac, returnType))
315 return true;
316 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
317 ArgExprs, NumArgs);
318 }
319
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000320 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000321 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
322 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000323 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
324 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000325 if (!Method)
326 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000327 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000328 lbrac, rbrac, returnType))
329 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000330 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
331 ArgExprs, NumArgs);
332 }
333
334 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000335 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000336 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000337 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000338 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000339 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000340 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000341 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000342 Method = ImpDecl->getClassMethod(Sel);
343 }
344 if (!Method)
345 Method = FactoryMethodPool[Sel].Method;
346 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000347 Method = LookupInstanceMethodInGlobalPool(
348 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000349 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000350 lbrac, rbrac, returnType))
351 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000352 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
353 ArgExprs, NumArgs);
354 }
355
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000356 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000357 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000358
359 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
360 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000361 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000362 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000363 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
364 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
365 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
366 break;
367 }
368 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000369 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000370 << Sel << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000371 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000372 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000373 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000374
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000375 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000376 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
377 // faster than the following method (which can do *many* linear searches).
378 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000379 Method = ClassDecl->lookupInstanceMethod(Sel);
380
381 if (!Method) {
382 // Search protocol qualifiers.
383 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
384 E = OCIReceiver->qual_end(); QI != E; ++QI) {
385 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000386 break;
387 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000388 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000389
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000390 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000391 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000392 << Sel << SourceRange(lbrac, rbrac);
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000393 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000394 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000395 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000396 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000397 }
398
399 if (!Method) {
400 // If we have an implementation in scope, check "private" methods.
401 if (ClassDecl)
402 if (ObjCImplementationDecl *ImpDecl =
403 ObjCImplementations[ClassDecl->getIdentifier()])
404 Method = ImpDecl->getInstanceMethod(Sel);
405 // If we still haven't found a method, look in the global pool. This
406 // behavior isn't very desirable, however we need it for GCC
407 // compatibility.
408 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000409 Method = LookupInstanceMethodInGlobalPool(
410 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000411 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000412 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000413 lbrac, rbrac, returnType))
414 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000415 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
416 ArgExprs, NumArgs);
417}
Chris Lattnereca7be62008-04-07 05:30:13 +0000418
419//===----------------------------------------------------------------------===//
420// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
421//===----------------------------------------------------------------------===//
422
423/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
424/// inheritance hierarchy of 'rProto'.
425static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
426 ObjCProtocolDecl *rProto) {
427 if (lProto == rProto)
428 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000429 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
430 E = rProto->protocol_end(); PI != E; ++PI)
431 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000432 return true;
433 return false;
434}
435
436/// ClassImplementsProtocol - Checks that 'lProto' protocol
437/// has been implemented in IDecl class, its super class or categories (if
438/// lookupCategory is true).
439static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
440 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000441 bool lookupCategory,
442 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000443
444 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000445 const ObjCList<ObjCProtocolDecl> &Protocols =
446 IDecl->getReferencedProtocols();
447
448 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
449 E = Protocols.end(); PI != E; ++PI) {
450 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000451 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000452 // This is dubious and is added to be compatible with gcc.
453 // In gcc, it is also allowed assigning a protocol-qualified 'id'
454 // type to a LHS object when protocol in qualified LHS is in list
455 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000456 // FIXME: Treat this as an extension, and flag this as an error when
457 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000458 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000459 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000460 }
461
462 // 2nd, look up the category.
463 if (lookupCategory)
464 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
465 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000466 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
467 E = CDecl->protocol_end(); PI != E; ++PI)
468 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000469 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000470 }
471
472 // 3rd, look up the super class(s)
473 if (IDecl->getSuperClass())
474 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000475 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
476 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000477
478 return false;
479}
480
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000481/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
482/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000483bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
484 bool compare) {
485 // Allow id<P..> and an 'id' or void* type in all cases.
486 if (const PointerType *PT = lhs->getAsPointerType()) {
487 QualType PointeeTy = PT->getPointeeType();
488 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
489 return true;
490 } else if (const PointerType *PT = rhs->getAsPointerType()) {
491 QualType PointeeTy = PT->getPointeeType();
492 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
493 return true;
494 }
495
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000496 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
497 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
498 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000499 QualType rtype;
500
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000501 if (!rhsQID) {
502 // Not comparing two ObjCQualifiedIdType's?
503 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000504
505 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000506 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000507 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000508 // If the RHS is a unqualified interface pointer "NSString*",
509 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000510 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
511 ObjCInterfaceDecl *rhsID = IT->getDecl();
512 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
513 // when comparing an id<P> on lhs with a static type on rhs,
514 // see if static class implements all of id's protocols, directly or
515 // through its super class and categories.
516 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
517 return false;
518 }
519 return true;
520 }
521 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000522 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000523
524 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000525 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000526 RHSProtoI = rhsQI->qual_begin();
527 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000528 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000529 RHSProtoI = rhsQID->qual_begin();
530 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000531 } else {
532 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000533 }
534
535 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
536 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
537 bool match = false;
538
539 // when comparing an id<P> on lhs with a static type on rhs,
540 // see if static class implements all of id's protocols, directly or
541 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000542 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
543 ObjCProtocolDecl *rhsProto = *RHSProtoI;
544 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000545 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000546 match = true;
547 break;
548 }
549 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000550 if (rhsQI) {
551 // If the RHS is a qualified interface pointer "NSString<P>*",
552 // make sure we check the class hierarchy.
553 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
554 ObjCInterfaceDecl *rhsID = IT->getDecl();
555 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
556 // when comparing an id<P> on lhs with a static type on rhs,
557 // see if static class implements all of id's protocols, directly or
558 // through its super class and categories.
559 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
560 match = true;
561 break;
562 }
563 }
564 }
565 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000566 if (!match)
567 return false;
568 }
569
570 return true;
571 }
572
573 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
574 assert(rhsQID && "One of the LHS/RHS should be id<x>");
575
576 if (!lhs->isPointerType())
577 return false;
578
579 QualType ltype = lhs->getAsPointerType()->getPointeeType();
580 if (const ObjCQualifiedInterfaceType *lhsQI =
581 ltype->getAsObjCQualifiedInterfaceType()) {
582 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
583 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
584 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
585 bool match = false;
586 ObjCProtocolDecl *lhsProto = *LHSProtoI;
587 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
588 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
589 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000590 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000591 match = true;
592 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000593 }
594 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000595 if (!match)
596 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000597 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000598 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000599 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000600
601 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
602 // for static type vs. qualified 'id' type, check that class implements
603 // all of 'id's protocols.
604 ObjCInterfaceDecl *lhsID = IT->getDecl();
605 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
606 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000607 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000608 return false;
609 }
610 return true;
611 }
612 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000613}
614