blob: c9cfc92f4d6b9b69a547371894d5fcb072011bd7 [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();
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 Kremeneka526c5c2008-01-07 19:49:32 +000051 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-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 Naroffb327ce02008-04-02 14:35:35 +000055 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000056 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000057 if (!strIFace)
58 return Diag(S->getLocStart(), diag::err_undef_interface,
59 NSIdent->getName());
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000061 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000062 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner85a932e2008-01-04 22:32:30 +000063 t = Context.getPointerType(t);
64 return new ObjCStringLiteral(S, t, AtLoc);
65}
66
67Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
68 SourceLocation EncodeLoc,
69 SourceLocation LParenLoc,
70 TypeTy *Ty,
71 SourceLocation RParenLoc) {
72 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
73
74 QualType t = Context.getPointerType(Context.CharTy);
75 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
76}
77
78Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
79 SourceLocation AtLoc,
80 SourceLocation SelLoc,
81 SourceLocation LParenLoc,
82 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 QualType t = Context.getObjCSelType();
Chris Lattner85a932e2008-01-04 22:32:30 +000084 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
85}
86
87Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
88 SourceLocation AtLoc,
89 SourceLocation ProtoLoc,
90 SourceLocation LParenLoc,
91 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000093 if (!PDecl) {
94 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
95 return true;
96 }
97
Ted Kremeneka526c5c2008-01-07 19:49:32 +000098 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +000099 if (t.isNull())
100 return true;
101 t = Context.getPointerType(t);
102 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
103}
104
105bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 ObjCMethodDecl *Method) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000107 bool anyIncompatibleArgs = false;
108
109 for (unsigned i = 0; i < NumArgs; i++) {
110 Expr *argExpr = Args[i];
111 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
112
113 QualType lhsType = Method->getParamDecl(i)->getType();
114 QualType rhsType = argExpr->getType();
115
116 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000117 if (lhsType->isArrayType())
118 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000119 else if (lhsType->isFunctionType())
120 lhsType = Context.getPointerType(lhsType);
121
Chris Lattner987798a2008-04-02 17:17:33 +0000122 AssignConvertType Result =
123 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000124 if (Args[i] != argExpr) // The expression was converted.
125 Args[i] = argExpr; // Make sure we store the converted expression.
126
127 anyIncompatibleArgs |=
128 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
129 argExpr, "sending");
130 }
131 return anyIncompatibleArgs;
132}
133
134// ActOnClassMessage - used for both unary and keyword messages.
135// ArgExprs is optional - if it is present, the number of expressions
136// is obtained from Sel.getNumArgs().
137Sema::ExprResult Sema::ActOnClassMessage(
138 Scope *S,
139 IdentifierInfo *receiverName, Selector Sel,
140 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
141{
142 assert(receiverName && "missing receiver class name");
143
144 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000145 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000146 if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) {
147 ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass();
Steve Naroff95110962008-03-28 21:37:05 +0000148 if (!ClassDecl)
149 return Diag(lbrac, diag::error_no_super_class,
150 CurMethodDecl->getClassInterface()->getName());
151 if (CurMethodDecl->isInstance()) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000152 // Synthesize a cast to the super class. This hack allows us to loosely
153 // represent super without creating a special expression node.
154 IdentifierInfo &II = Context.Idents.get("self");
155 ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000156 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner85a932e2008-01-04 22:32:30 +0000157 superTy = Context.getPointerType(superTy);
158 ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(),
159 SourceLocation(), ReceiverExpr.Val);
160 // We are really in an instance method, redirect.
161 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
162 Args, NumArgs);
163 }
164 // We are sending a message to 'super' within a class method. Do nothing,
165 // the receiver will pass through as 'super' (how convenient:-).
166 } else
167 ClassDecl = getObjCInterfaceDecl(receiverName);
168
169 // FIXME: can ClassDecl ever be null?
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170 ObjCMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Chris Lattner85a932e2008-01-04 22:32:30 +0000171 QualType returnType;
172
Steve Naroff9ad23d62008-06-04 14:43:54 +0000173 // If we have an implementation in scope, check "private" methods.
174 if (!Method) {
175 if (ObjCImplementationDecl *ImpDecl =
176 ObjCImplementations[ClassDecl->getIdentifier()])
177 Method = ImpDecl->getClassMethod(Sel);
178 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000179 // Before we give up, check if the selector is an instance method.
180 if (!Method)
181 Method = ClassDecl->lookupInstanceMethod(Sel);
182 if (!Method) {
183 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
184 SourceRange(lbrac, rbrac));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000186 } else {
187 returnType = Method->getResultType();
188 if (Sel.getNumArgs()) {
189 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
190 return true;
191 }
192 }
193 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
194 lbrac, rbrac, ArgExprs, NumArgs);
195}
196
197// ActOnInstanceMessage - used for both unary and keyword messages.
198// ArgExprs is optional - if it is present, the number of expressions
199// is obtained from Sel.getNumArgs().
200Sema::ExprResult Sema::ActOnInstanceMessage(
201 ExprTy *receiver, Selector Sel,
202 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
203{
204 assert(receiver && "missing receiver expression");
205
206 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
207 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroff94a82c92008-05-31 02:19:15 +0000208 QualType receiverType;
Chris Lattner85a932e2008-01-04 22:32:30 +0000209 QualType returnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000210 ObjCMethodDecl *Method = 0;
Steve Naroff94a82c92008-05-31 02:19:15 +0000211
212 receiverType = RExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000213
Chris Lattner6e10a082008-02-01 06:57:39 +0000214 if (receiverType == Context.getObjCIdType().getCanonicalType() ||
215 receiverType == Context.getObjCClassType().getCanonicalType()) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000216 Method = InstanceMethodPool[Sel].Method;
Chris Lattner6e10a082008-02-01 06:57:39 +0000217 if (!Method)
218 Method = FactoryMethodPool[Sel].Method;
Chris Lattner85a932e2008-01-04 22:32:30 +0000219 if (!Method) {
220 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
221 SourceRange(lbrac, rbrac));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000222 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000223 } else {
224 returnType = Method->getResultType();
225 if (Sel.getNumArgs())
226 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
227 return true;
228 }
229 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000230 bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000231 // FIXME (snaroff): checking in this code from Patrick. Needs to be
232 // revisited. how do we get the ClassDecl from the receiver expression?
233 if (!receiverIsQualId)
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000234 while (const PointerType *PTy = receiverType->getAsPointerType())
235 receiverType = PTy->getPointeeType();
236
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000237 ObjCInterfaceDecl* ClassDecl = 0;
238 if (ObjCQualifiedInterfaceType *QIT =
239 dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000240 ClassDecl = QIT->getDecl();
241 Method = ClassDecl->lookupInstanceMethod(Sel);
242 if (!Method) {
243 // search protocols
244 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000245 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner85a932e2008-01-04 22:32:30 +0000246 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
247 break;
248 }
249 }
250 if (!Method)
251 Diag(lbrac, diag::warn_method_not_found_in_protocol,
252 std::string("-"), Sel.getName(),
253 SourceRange(lbrac, rbrac));
254 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000255 else if (ObjCQualifiedIdType *QIT =
256 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000257 // search protocols
258 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner85a932e2008-01-04 22:32:30 +0000260 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
261 break;
262 }
263 if (!Method)
264 Diag(lbrac, diag::warn_method_not_found_in_protocol,
265 std::string("-"), Sel.getName(),
266 SourceRange(lbrac, rbrac));
267 }
268 else {
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000269 ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType);
270 if (OCIReceiver == 0) {
Chris Lattner6e10a082008-02-01 06:57:39 +0000271 Diag(lbrac, diag::error_bad_receiver_type,
272 RExpr->getType().getAsString());
Fariborz Jahanian1dad5b22008-01-25 17:43:39 +0000273 return true;
274 }
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000275 ClassDecl = OCIReceiver->getDecl();
Chris Lattner85a932e2008-01-04 22:32:30 +0000276 // FIXME: consider using InstanceMethodPool, since it will be faster
277 // than the following method (which can do *many* linear searches). The
278 // idea is to add class info to InstanceMethodPool...
279 Method = ClassDecl->lookupInstanceMethod(Sel);
280 }
281 if (!Method) {
282 // If we have an implementation in scope, check "private" methods.
283 if (ClassDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000284 if (ObjCImplementationDecl *ImpDecl =
285 ObjCImplementations[ClassDecl->getIdentifier()])
Chris Lattner85a932e2008-01-04 22:32:30 +0000286 Method = ImpDecl->getInstanceMethod(Sel);
Chris Lattnerc81c8142008-02-25 21:04:36 +0000287 // If we still haven't found a method, look in the global pool. This
288 // behavior isn't very desirable, however we need it for GCC
Chris Lattner85a932e2008-01-04 22:32:30 +0000289 // compatibility.
Chris Lattnerc81c8142008-02-25 21:04:36 +0000290 if (!Method)
291 Method = InstanceMethodPool[Sel].Method;
Chris Lattner85a932e2008-01-04 22:32:30 +0000292 }
293 if (!Method) {
294 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
295 SourceRange(lbrac, rbrac));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000296 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000297 } else {
298 returnType = Method->getResultType();
299 if (Sel.getNumArgs())
300 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
301 return true;
302 }
303 }
304 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
305 ArgExprs, NumArgs);
306}
Chris Lattnereca7be62008-04-07 05:30:13 +0000307
308//===----------------------------------------------------------------------===//
309// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
310//===----------------------------------------------------------------------===//
311
312/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
313/// inheritance hierarchy of 'rProto'.
314static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
315 ObjCProtocolDecl *rProto) {
316 if (lProto == rProto)
317 return true;
318 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
319 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
320 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
321 return true;
322 return false;
323}
324
325/// ClassImplementsProtocol - Checks that 'lProto' protocol
326/// has been implemented in IDecl class, its super class or categories (if
327/// lookupCategory is true).
328static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
329 ObjCInterfaceDecl *IDecl,
330 bool lookupCategory) {
331
332 // 1st, look up the class.
333 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
334 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
335 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
336 return true;
337 }
338
339 // 2nd, look up the category.
340 if (lookupCategory)
341 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
342 CDecl = CDecl->getNextClassCategory()) {
343 protoList = CDecl->getReferencedProtocols();
344 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
345 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
346 return true;
347 }
348 }
349
350 // 3rd, look up the super class(s)
351 if (IDecl->getSuperClass())
352 return
353 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
354
355 return false;
356}
357
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000358/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
359/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000360bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
361 bool compare) {
362 // Allow id<P..> and an 'id' or void* type in all cases.
363 if (const PointerType *PT = lhs->getAsPointerType()) {
364 QualType PointeeTy = PT->getPointeeType();
365 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
366 return true;
367 } else if (const PointerType *PT = rhs->getAsPointerType()) {
368 QualType PointeeTy = PT->getPointeeType();
369 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
370 return true;
371 }
372
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000373 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
374 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
375 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000376 QualType rtype;
377
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000378 if (!rhsQID) {
379 // Not comparing two ObjCQualifiedIdType's?
380 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000381
382 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000383 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000384 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000385 // If the RHS is a unqualified interface pointer "NSString*",
386 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000387 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
388 ObjCInterfaceDecl *rhsID = IT->getDecl();
389 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
390 // when comparing an id<P> on lhs with a static type on rhs,
391 // see if static class implements all of id's protocols, directly or
392 // through its super class and categories.
393 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
394 return false;
395 }
396 return true;
397 }
398 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000399 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000400
401 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000402 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000403 RHSProtoI = rhsQI->qual_begin();
404 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000405 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000406 RHSProtoI = rhsQID->qual_begin();
407 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000408 } else {
409 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000410 }
411
412 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
413 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
414 bool match = false;
415
416 // when comparing an id<P> on lhs with a static type on rhs,
417 // see if static class implements all of id's protocols, directly or
418 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000419 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
420 ObjCProtocolDecl *rhsProto = *RHSProtoI;
421 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
422 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000423 match = true;
424 break;
425 }
426 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000427 if (rhsQI) {
428 // If the RHS is a qualified interface pointer "NSString<P>*",
429 // make sure we check the class hierarchy.
430 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
431 ObjCInterfaceDecl *rhsID = IT->getDecl();
432 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
433 // when comparing an id<P> on lhs with a static type on rhs,
434 // see if static class implements all of id's protocols, directly or
435 // through its super class and categories.
436 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
437 match = true;
438 break;
439 }
440 }
441 }
442 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000443 if (!match)
444 return false;
445 }
446
447 return true;
448 }
449
450 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
451 assert(rhsQID && "One of the LHS/RHS should be id<x>");
452
453 if (!lhs->isPointerType())
454 return false;
455
456 QualType ltype = lhs->getAsPointerType()->getPointeeType();
457 if (const ObjCQualifiedInterfaceType *lhsQI =
458 ltype->getAsObjCQualifiedInterfaceType()) {
459 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
460 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
461 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
462 bool match = false;
463 ObjCProtocolDecl *lhsProto = *LHSProtoI;
464 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
465 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
466 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnereca7be62008-04-07 05:30:13 +0000467 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000468 match = true;
469 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000470 }
471 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000472 if (!match)
473 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000474 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000475 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000476 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000477
478 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
479 // for static type vs. qualified 'id' type, check that class implements
480 // all of 'id's protocols.
481 ObjCInterfaceDecl *lhsID = IT->getDecl();
482 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
483 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
484 if (!ClassImplementsProtocol(rhsProto, lhsID, compare))
485 return false;
486 }
487 return true;
488 }
489 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000490}
491