blob: 5d168232fe0bc1510abd4d0ce928ba7402d66252 [file] [log] [blame]
Chris Lattner09493052008-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 Naroff9ed3e772008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Chris Lattner09493052008-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();
32 char *strBuf = new char [Length];
33 char *p = strBuf;
34 bool isWide = false;
35 for (unsigned i = 0; i < NumStrings; i++) {
36 S = static_cast<StringLiteral *>(Strings[i]);
37 if (S->isWide())
38 isWide = true;
39 memcpy(p, S->getStrData(), S->getByteLength());
40 p += S->getByteLength();
41 delete S;
42 }
43 S = new StringLiteral(strBuf, Length,
44 isWide, Context.getPointerType(Context.CharTy),
45 AtLoc, EndLoc);
46 }
47
48 if (CheckBuiltinCFStringArgument(S))
49 return true;
50
Ted Kremenek42730c52008-01-07 19:49:32 +000051 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner09493052008-01-04 22:32:30 +000052 // Initialize the constant string interface lazily. This assumes
53 // the NSConstantString interface is seen in this translation unit.
54 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Steve Naroff6384a012008-04-02 14:35:35 +000055 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000056 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattnerbac12452008-06-21 21:44:18 +000057 if (strIFace)
58 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner09493052008-01-04 22:32:30 +000059 }
Ted Kremenek42730c52008-01-07 19:49:32 +000060 QualType t = Context.getObjCConstantStringInterface();
Chris Lattnerbac12452008-06-21 21:44:18 +000061 // If there is no NSConstantString interface defined then treat constant
62 // strings as untyped objects and let the runtime figure it out later.
63 if (t == QualType()) {
64 t = Context.getObjCIdType();
65 } else {
66 t = Context.getPointerType(t);
67 }
Chris Lattner09493052008-01-04 22:32:30 +000068 return new ObjCStringLiteral(S, t, AtLoc);
69}
70
71Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
72 SourceLocation EncodeLoc,
73 SourceLocation LParenLoc,
74 TypeTy *Ty,
75 SourceLocation RParenLoc) {
76 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
77
78 QualType t = Context.getPointerType(Context.CharTy);
79 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
80}
81
82Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
83 SourceLocation AtLoc,
84 SourceLocation SelLoc,
85 SourceLocation LParenLoc,
86 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000087 QualType t = Context.getObjCSelType();
Chris Lattner09493052008-01-04 22:32:30 +000088 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
89}
90
91Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
92 SourceLocation AtLoc,
93 SourceLocation ProtoLoc,
94 SourceLocation LParenLoc,
95 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner09493052008-01-04 22:32:30 +000097 if (!PDecl) {
98 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
99 return true;
100 }
101
Ted Kremenek42730c52008-01-07 19:49:32 +0000102 QualType t = Context.getObjCProtoType();
Chris Lattner09493052008-01-04 22:32:30 +0000103 if (t.isNull())
104 return true;
105 t = Context.getPointerType(t);
106 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
107}
108
109bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 ObjCMethodDecl *Method) {
Chris Lattner09493052008-01-04 22:32:30 +0000111 bool anyIncompatibleArgs = false;
112
113 for (unsigned i = 0; i < NumArgs; i++) {
114 Expr *argExpr = Args[i];
115 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
116
117 QualType lhsType = Method->getParamDecl(i)->getType();
118 QualType rhsType = argExpr->getType();
119
120 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnera2ef0822008-04-02 17:17:33 +0000121 if (lhsType->isArrayType())
122 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner09493052008-01-04 22:32:30 +0000123 else if (lhsType->isFunctionType())
124 lhsType = Context.getPointerType(lhsType);
125
Chris Lattnera2ef0822008-04-02 17:17:33 +0000126 AssignConvertType Result =
127 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner09493052008-01-04 22:32:30 +0000128 if (Args[i] != argExpr) // The expression was converted.
129 Args[i] = argExpr; // Make sure we store the converted expression.
130
131 anyIncompatibleArgs |=
132 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
133 argExpr, "sending");
134 }
135 return anyIncompatibleArgs;
136}
137
138// ActOnClassMessage - used for both unary and keyword messages.
139// ArgExprs is optional - if it is present, the number of expressions
140// is obtained from Sel.getNumArgs().
141Sema::ExprResult Sema::ActOnClassMessage(
142 Scope *S,
143 IdentifierInfo *receiverName, Selector Sel,
144 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
145{
146 assert(receiverName && "missing receiver class name");
147
148 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremenek42730c52008-01-07 19:49:32 +0000149 ObjCInterfaceDecl* ClassDecl = 0;
Steve Naroff91a69202008-07-24 19:44:33 +0000150 bool isSuper = false;
151
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000152 if (!strcmp(receiverName->getName(), "super") && getCurMethodDecl()) {
Steve Naroff91a69202008-07-24 19:44:33 +0000153 isSuper = true;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000154 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
Steve Naroff9fcf5b62008-03-28 21:37:05 +0000155 if (!ClassDecl)
156 return Diag(lbrac, diag::error_no_super_class,
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000157 getCurMethodDecl()->getClassInterface()->getName());
158 if (getCurMethodDecl()->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000159 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner09493052008-01-04 22:32:30 +0000160 superTy = Context.getPointerType(superTy);
Chris Lattner4423d372008-06-21 18:04:54 +0000161 ExprResult ReceiverExpr = new PreDefinedExpr(SourceLocation(), superTy,
162 PreDefinedExpr::ObjCSuper);
Chris Lattner09493052008-01-04 22:32:30 +0000163 // We are really in an instance method, redirect.
164 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
165 Args, NumArgs);
166 }
167 // We are sending a message to 'super' within a class method. Do nothing,
168 // the receiver will pass through as 'super' (how convenient:-).
169 } else
170 ClassDecl = getObjCInterfaceDecl(receiverName);
171
Steve Naroff51c6cff2008-06-04 23:08:38 +0000172 // ClassDecl is null in the following case.
173 //
174 // typedef XCElementDisplayRect XCElementGraphicsRect;
175 //
176 // @implementation XCRASlice
177 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
178 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
179 // }
180 //
181 // FIXME: Investigate why GCC allows the above.
182 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000183 QualType returnType;
Steve Naroff51c6cff2008-06-04 23:08:38 +0000184 if (ClassDecl) {
185 Method = ClassDecl->lookupClassMethod(Sel);
186
187 // If we have an implementation in scope, check "private" methods.
188 if (!Method) {
189 if (ObjCImplementationDecl *ImpDecl =
190 ObjCImplementations[ClassDecl->getIdentifier()])
191 Method = ImpDecl->getClassMethod(Sel);
192 }
193 // Before we give up, check if the selector is an instance method.
194 if (!Method)
195 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff616c55c2008-06-04 14:43:54 +0000196 }
Chris Lattner09493052008-01-04 22:32:30 +0000197 if (!Method) {
198 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
199 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000200 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000201 } else {
202 returnType = Method->getResultType();
203 if (Sel.getNumArgs()) {
204 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
205 return true;
206 }
207 }
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000208
209 // If we have the ObjCInterfaceDecl* for the class that is receiving
210 // the message, use that to construct the ObjCMessageExpr. Otherwise
211 // pass on the IdentifierInfo* for the class.
Steve Naroff91a69202008-07-24 19:44:33 +0000212 // FIXME: need to do a better job handling 'super' usage within a class
213 // For now, we simply pass the "super" identifier through (which isn't
214 // consistent with instance methods.
215 if (isSuper || !ClassDecl)
216 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000217 lbrac, rbrac, ArgExprs, NumArgs);
218 else
Steve Naroff91a69202008-07-24 19:44:33 +0000219 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000220 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000221}
222
223// ActOnInstanceMessage - used for both unary and keyword messages.
224// ArgExprs is optional - if it is present, the number of expressions
225// is obtained from Sel.getNumArgs().
Chris Lattner4d018542008-07-21 06:31:05 +0000226Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattner09493052008-01-04 22:32:30 +0000227 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
228{
229 assert(receiver && "missing receiver expression");
230
231 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
232 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner09493052008-01-04 22:32:30 +0000233 QualType returnType;
Steve Naroff7c7817e2008-05-31 02:19:15 +0000234
Chris Lattner4cff4b72008-07-21 05:57:44 +0000235 QualType receiverType =
236 RExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner09493052008-01-04 22:32:30 +0000237
Chris Lattner4cff4b72008-07-21 05:57:44 +0000238 // Handle messages to id.
Steve Naroff1a5027c2008-06-05 14:49:39 +0000239 if (receiverType == Context.getObjCIdType().getCanonicalType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000240 ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method;
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000241 if (!Method)
242 Method = FactoryMethodPool[Sel].Method;
Chris Lattner09493052008-01-04 22:32:30 +0000243 if (!Method) {
244 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
245 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000246 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000247 } else {
248 returnType = Method->getResultType();
249 if (Sel.getNumArgs())
250 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
251 return true;
252 }
Chris Lattner4cff4b72008-07-21 05:57:44 +0000253 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
254 ArgExprs, NumArgs);
255 }
256
257 // Handle messages to Class.
258 if (receiverType == Context.getObjCClassType().getCanonicalType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000259 ObjCMethodDecl *Method = 0;
Chris Lattner55a24332008-07-21 06:44:27 +0000260 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +0000261 // If we have an implementation in scope, check "private" methods.
Chris Lattner55a24332008-07-21 06:44:27 +0000262 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff1a5027c2008-06-05 14:49:39 +0000263 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner4cff4b72008-07-21 05:57:44 +0000264 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroff1a5027c2008-06-05 14:49:39 +0000265 Method = ImpDecl->getClassMethod(Sel);
266 }
267 if (!Method)
268 Method = FactoryMethodPool[Sel].Method;
269 if (!Method)
270 Method = InstanceMethodPool[Sel].Method;
271 if (!Method) {
272 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
Chris Lattner3977eb72008-07-21 06:16:07 +0000273 RExpr->getSourceRange());
Steve Naroff1a5027c2008-06-05 14:49:39 +0000274 returnType = Context.getObjCIdType();
275 } else {
276 returnType = Method->getResultType();
277 if (Sel.getNumArgs())
278 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
279 return true;
280 }
Chris Lattner4cff4b72008-07-21 05:57:44 +0000281
282 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
283 ArgExprs, NumArgs);
284 }
285
Chris Lattnerf9147652008-07-21 06:12:56 +0000286 ObjCMethodDecl *Method = 0;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000287 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattnerf9147652008-07-21 06:12:56 +0000288
289 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
290 // long as one of the protocols implements the selector (if not, warn).
Chris Lattner4cff4b72008-07-21 05:57:44 +0000291 if (ObjCQualifiedIdType *QIT =
292 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000293 // Search protocols
Chris Lattner4cff4b72008-07-21 05:57:44 +0000294 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
295 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
296 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
297 break;
298 }
299 if (!Method)
300 Diag(lbrac, diag::warn_method_not_found_in_protocol,
301 std::string("-"), Sel.getName(),
Chris Lattner3977eb72008-07-21 06:16:07 +0000302 RExpr->getSourceRange());
Chris Lattnerf9147652008-07-21 06:12:56 +0000303 } else if (const ObjCInterfaceType *OCIReceiver =
304 receiverType->getAsPointerToObjCInterfaceType()) {
305 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnere1b957d2008-02-01 06:43:02 +0000306
Chris Lattner4cff4b72008-07-21 05:57:44 +0000307 ClassDecl = OCIReceiver->getDecl();
308 // FIXME: consider using InstanceMethodPool, since it will be faster
309 // than the following method (which can do *many* linear searches). The
310 // idea is to add class info to InstanceMethodPool.
311 Method = ClassDecl->lookupInstanceMethod(Sel);
312
313 if (!Method) {
314 // Search protocol qualifiers.
315 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
316 E = OCIReceiver->qual_end(); QI != E; ++QI) {
317 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner09493052008-01-04 22:32:30 +0000318 break;
319 }
Chris Lattner09493052008-01-04 22:32:30 +0000320 }
Chris Lattner00cb97b2008-07-21 05:27:51 +0000321
Chris Lattner4cff4b72008-07-21 05:57:44 +0000322 if (!Method && !OCIReceiver->qual_empty())
323 Diag(lbrac, diag::warn_method_not_found_in_protocol,
324 std::string("-"), Sel.getName(),
Chris Lattner09493052008-01-04 22:32:30 +0000325 SourceRange(lbrac, rbrac));
Chris Lattnerf9147652008-07-21 06:12:56 +0000326 } else {
327 Diag(lbrac, diag::error_bad_receiver_type,
Chris Lattner3977eb72008-07-21 06:16:07 +0000328 RExpr->getType().getAsString(), RExpr->getSourceRange());
Chris Lattnerf9147652008-07-21 06:12:56 +0000329 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000330 }
331
332 if (!Method) {
333 // If we have an implementation in scope, check "private" methods.
334 if (ClassDecl)
335 if (ObjCImplementationDecl *ImpDecl =
336 ObjCImplementations[ClassDecl->getIdentifier()])
337 Method = ImpDecl->getInstanceMethod(Sel);
338 // If we still haven't found a method, look in the global pool. This
339 // behavior isn't very desirable, however we need it for GCC
340 // compatibility.
341 if (!Method)
342 Method = InstanceMethodPool[Sel].Method;
343 }
344 if (!Method) {
345 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
346 SourceRange(lbrac, rbrac));
347 returnType = Context.getObjCIdType();
348 } else {
349 returnType = Method->getResultType();
350 if (Sel.getNumArgs())
351 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
352 return true;
Chris Lattner09493052008-01-04 22:32:30 +0000353 }
354 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
355 ArgExprs, NumArgs);
356}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000357
358//===----------------------------------------------------------------------===//
359// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
360//===----------------------------------------------------------------------===//
361
362/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
363/// inheritance hierarchy of 'rProto'.
364static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
365 ObjCProtocolDecl *rProto) {
366 if (lProto == rProto)
367 return true;
Chris Lattner0be08822008-07-21 21:32:27 +0000368 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
369 E = rProto->protocol_end(); PI != E; ++PI)
370 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000371 return true;
372 return false;
373}
374
375/// ClassImplementsProtocol - Checks that 'lProto' protocol
376/// has been implemented in IDecl class, its super class or categories (if
377/// lookupCategory is true).
378static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
379 ObjCInterfaceDecl *IDecl,
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000380 bool lookupCategory,
381 bool RHSIsQualifiedID = false) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000382
383 // 1st, look up the class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000384 const ObjCList<ObjCProtocolDecl> &Protocols =
385 IDecl->getReferencedProtocols();
386
387 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
388 E = Protocols.end(); PI != E; ++PI) {
389 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000390 return true;
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000391 // This is dubious and is added to be compatible with gcc.
392 // In gcc, it is also allowed assigning a protocol-qualified 'id'
393 // type to a LHS object when protocol in qualified LHS is in list
394 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenek5a5cd832008-06-04 20:48:08 +0000395 // FIXME: Treat this as an extension, and flag this as an error when
396 // GCC extensions are not enabled.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000397 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000398 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000399 }
400
401 // 2nd, look up the category.
402 if (lookupCategory)
403 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
404 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner0be08822008-07-21 21:32:27 +0000405 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
406 E = CDecl->protocol_end(); PI != E; ++PI)
407 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000408 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000409 }
410
411 // 3rd, look up the super class(s)
412 if (IDecl->getSuperClass())
413 return
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000414 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
415 RHSIsQualifiedID);
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000416
417 return false;
418}
419
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000420/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
421/// ObjCQualifiedIDType.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000422bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
423 bool compare) {
424 // Allow id<P..> and an 'id' or void* type in all cases.
425 if (const PointerType *PT = lhs->getAsPointerType()) {
426 QualType PointeeTy = PT->getPointeeType();
427 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
428 return true;
429 } else if (const PointerType *PT = rhs->getAsPointerType()) {
430 QualType PointeeTy = PT->getPointeeType();
431 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
432 return true;
433 }
434
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000435 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
436 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
437 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000438 QualType rtype;
439
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000440 if (!rhsQID) {
441 // Not comparing two ObjCQualifiedIdType's?
442 if (!rhs->isPointerType()) return false;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000443
444 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000445 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000446 if (rhsQI == 0) {
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000447 // If the RHS is a unqualified interface pointer "NSString*",
448 // make sure we check the class hierarchy.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000449 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
450 ObjCInterfaceDecl *rhsID = IT->getDecl();
451 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
452 // when comparing an id<P> on lhs with a static type on rhs,
453 // see if static class implements all of id's protocols, directly or
454 // through its super class and categories.
455 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
456 return false;
457 }
458 return true;
459 }
460 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000461 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000462
463 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000464 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000465 RHSProtoI = rhsQI->qual_begin();
466 RHSProtoE = rhsQI->qual_end();
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000467 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000468 RHSProtoI = rhsQID->qual_begin();
469 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000470 } else {
471 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000472 }
473
474 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
475 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
476 bool match = false;
477
478 // when comparing an id<P> on lhs with a static type on rhs,
479 // see if static class implements all of id's protocols, directly or
480 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000481 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
482 ObjCProtocolDecl *rhsProto = *RHSProtoI;
483 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
484 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000485 match = true;
486 break;
487 }
488 }
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000489 if (rhsQI) {
490 // If the RHS is a qualified interface pointer "NSString<P>*",
491 // make sure we check the class hierarchy.
492 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
493 ObjCInterfaceDecl *rhsID = IT->getDecl();
494 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
495 // when comparing an id<P> on lhs with a static type on rhs,
496 // see if static class implements all of id's protocols, directly or
497 // through its super class and categories.
498 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
499 match = true;
500 break;
501 }
502 }
503 }
504 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000505 if (!match)
506 return false;
507 }
508
509 return true;
510 }
511
512 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
513 assert(rhsQID && "One of the LHS/RHS should be id<x>");
514
515 if (!lhs->isPointerType())
516 return false;
517
518 QualType ltype = lhs->getAsPointerType()->getPointeeType();
519 if (const ObjCQualifiedInterfaceType *lhsQI =
520 ltype->getAsObjCQualifiedInterfaceType()) {
521 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
522 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
523 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
524 bool match = false;
525 ObjCProtocolDecl *lhsProto = *LHSProtoI;
526 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
527 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
528 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000529 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000530 match = true;
531 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000532 }
533 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000534 if (!match)
535 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000536 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000537 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000538 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000539
540 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
541 // for static type vs. qualified 'id' type, check that class implements
542 // all of 'id's protocols.
543 ObjCInterfaceDecl *lhsID = IT->getDecl();
544 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
545 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000546 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000547 return false;
548 }
549 return true;
550 }
551 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000552}
553