blob: dd90b9ca19d6807238f14438cad275bf15341fe0 [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 Lattner09493052008-01-04 22:32:30 +000057 if (!strIFace)
58 return Diag(S->getLocStart(), diag::err_undef_interface,
59 NSIdent->getName());
Ted Kremenek42730c52008-01-07 19:49:32 +000060 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner09493052008-01-04 22:32:30 +000061 }
Ted Kremenek42730c52008-01-07 19:49:32 +000062 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +000083 QualType t = Context.getObjCSelType();
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +000092 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner09493052008-01-04 22:32:30 +000093 if (!PDecl) {
94 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
95 return true;
96 }
97
Ted Kremenek42730c52008-01-07 19:49:32 +000098 QualType t = Context.getObjCProtoType();
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +0000106 ObjCMethodDecl *Method) {
Chris Lattner09493052008-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 Lattnera2ef0822008-04-02 17:17:33 +0000117 if (lhsType->isArrayType())
118 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner09493052008-01-04 22:32:30 +0000119 else if (lhsType->isFunctionType())
120 lhsType = Context.getPointerType(lhsType);
121
Chris Lattnera2ef0822008-04-02 17:17:33 +0000122 AssignConvertType Result =
123 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +0000145 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000146 if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) {
147 ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass();
Steve Naroff9fcf5b62008-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 Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +0000156 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Chris Lattner09493052008-01-04 22:32:30 +0000171 QualType returnType;
172
173 // Before we give up, check if the selector is an instance method.
174 if (!Method)
175 Method = ClassDecl->lookupInstanceMethod(Sel);
176 if (!Method) {
177 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
178 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000179 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000180 } else {
181 returnType = Method->getResultType();
182 if (Sel.getNumArgs()) {
183 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
184 return true;
185 }
186 }
187 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
188 lbrac, rbrac, ArgExprs, NumArgs);
189}
190
191// ActOnInstanceMessage - used for both unary and keyword messages.
192// ArgExprs is optional - if it is present, the number of expressions
193// is obtained from Sel.getNumArgs().
194Sema::ExprResult Sema::ActOnInstanceMessage(
195 ExprTy *receiver, Selector Sel,
196 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
197{
198 assert(receiver && "missing receiver expression");
199
200 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
201 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroff7c7817e2008-05-31 02:19:15 +0000202 QualType receiverType;
Chris Lattner09493052008-01-04 22:32:30 +0000203 QualType returnType;
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCMethodDecl *Method = 0;
Steve Naroff7c7817e2008-05-31 02:19:15 +0000205
206 receiverType = RExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner09493052008-01-04 22:32:30 +0000207
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000208 if (receiverType == Context.getObjCIdType().getCanonicalType() ||
209 receiverType == Context.getObjCClassType().getCanonicalType()) {
Chris Lattner09493052008-01-04 22:32:30 +0000210 Method = InstanceMethodPool[Sel].Method;
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000211 if (!Method)
212 Method = FactoryMethodPool[Sel].Method;
Chris Lattner09493052008-01-04 22:32:30 +0000213 if (!Method) {
214 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
215 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000217 } else {
218 returnType = Method->getResultType();
219 if (Sel.getNumArgs())
220 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
221 return true;
222 }
223 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType);
Chris Lattner09493052008-01-04 22:32:30 +0000225 // FIXME (snaroff): checking in this code from Patrick. Needs to be
226 // revisited. how do we get the ClassDecl from the receiver expression?
227 if (!receiverIsQualId)
Chris Lattnere1b957d2008-02-01 06:43:02 +0000228 while (const PointerType *PTy = receiverType->getAsPointerType())
229 receiverType = PTy->getPointeeType();
230
Ted Kremenek42730c52008-01-07 19:49:32 +0000231 ObjCInterfaceDecl* ClassDecl = 0;
232 if (ObjCQualifiedInterfaceType *QIT =
233 dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) {
Chris Lattner09493052008-01-04 22:32:30 +0000234 ClassDecl = QIT->getDecl();
235 Method = ClassDecl->lookupInstanceMethod(Sel);
236 if (!Method) {
237 // search protocols
238 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000239 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner09493052008-01-04 22:32:30 +0000240 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
241 break;
242 }
243 }
244 if (!Method)
245 Diag(lbrac, diag::warn_method_not_found_in_protocol,
246 std::string("-"), Sel.getName(),
247 SourceRange(lbrac, rbrac));
248 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000249 else if (ObjCQualifiedIdType *QIT =
250 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattner09493052008-01-04 22:32:30 +0000251 // search protocols
252 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000253 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner09493052008-01-04 22:32:30 +0000254 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
255 break;
256 }
257 if (!Method)
258 Diag(lbrac, diag::warn_method_not_found_in_protocol,
259 std::string("-"), Sel.getName(),
260 SourceRange(lbrac, rbrac));
261 }
262 else {
Chris Lattnere1b957d2008-02-01 06:43:02 +0000263 ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType);
264 if (OCIReceiver == 0) {
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000265 Diag(lbrac, diag::error_bad_receiver_type,
266 RExpr->getType().getAsString());
Fariborz Jahanianbc662012008-01-25 17:43:39 +0000267 return true;
268 }
Chris Lattnere1b957d2008-02-01 06:43:02 +0000269 ClassDecl = OCIReceiver->getDecl();
Chris Lattner09493052008-01-04 22:32:30 +0000270 // FIXME: consider using InstanceMethodPool, since it will be faster
271 // than the following method (which can do *many* linear searches). The
272 // idea is to add class info to InstanceMethodPool...
273 Method = ClassDecl->lookupInstanceMethod(Sel);
274 }
275 if (!Method) {
276 // If we have an implementation in scope, check "private" methods.
277 if (ClassDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +0000278 if (ObjCImplementationDecl *ImpDecl =
279 ObjCImplementations[ClassDecl->getIdentifier()])
Chris Lattner09493052008-01-04 22:32:30 +0000280 Method = ImpDecl->getInstanceMethod(Sel);
Chris Lattner43b885f2008-02-25 21:04:36 +0000281 // If we still haven't found a method, look in the global pool. This
282 // behavior isn't very desirable, however we need it for GCC
Chris Lattner09493052008-01-04 22:32:30 +0000283 // compatibility.
Chris Lattner43b885f2008-02-25 21:04:36 +0000284 if (!Method)
285 Method = InstanceMethodPool[Sel].Method;
Chris Lattner09493052008-01-04 22:32:30 +0000286 }
287 if (!Method) {
288 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
289 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000290 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000291 } else {
292 returnType = Method->getResultType();
293 if (Sel.getNumArgs())
294 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
295 return true;
296 }
297 }
298 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
299 ArgExprs, NumArgs);
300}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000301
302//===----------------------------------------------------------------------===//
303// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
304//===----------------------------------------------------------------------===//
305
306/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
307/// inheritance hierarchy of 'rProto'.
308static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
309 ObjCProtocolDecl *rProto) {
310 if (lProto == rProto)
311 return true;
312 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
313 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
314 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
315 return true;
316 return false;
317}
318
319/// ClassImplementsProtocol - Checks that 'lProto' protocol
320/// has been implemented in IDecl class, its super class or categories (if
321/// lookupCategory is true).
322static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
323 ObjCInterfaceDecl *IDecl,
324 bool lookupCategory) {
325
326 // 1st, look up the class.
327 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
328 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
329 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
330 return true;
331 }
332
333 // 2nd, look up the category.
334 if (lookupCategory)
335 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
336 CDecl = CDecl->getNextClassCategory()) {
337 protoList = CDecl->getReferencedProtocols();
338 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
339 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
340 return true;
341 }
342 }
343
344 // 3rd, look up the super class(s)
345 if (IDecl->getSuperClass())
346 return
347 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
348
349 return false;
350}
351
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000352/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
353/// ObjCQualifiedIDType.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000354bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
355 bool compare) {
356 // Allow id<P..> and an 'id' or void* type in all cases.
357 if (const PointerType *PT = lhs->getAsPointerType()) {
358 QualType PointeeTy = PT->getPointeeType();
359 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
360 return true;
361 } else if (const PointerType *PT = rhs->getAsPointerType()) {
362 QualType PointeeTy = PT->getPointeeType();
363 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
364 return true;
365 }
366
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000367 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
368 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
369 const ObjCQualifiedInterfaceType *rhsQI = 0;
370 if (!rhsQID) {
371 // Not comparing two ObjCQualifiedIdType's?
372 if (!rhs->isPointerType()) return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000373 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000374
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000375 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000376 if (rhsQI == 0) {
377 // If the RHS is an interface pointer ('NSString*'), handle it.
378 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
379 ObjCInterfaceDecl *rhsID = IT->getDecl();
380 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
381 // when comparing an id<P> on lhs with a static type on rhs,
382 // see if static class implements all of id's protocols, directly or
383 // through its super class and categories.
384 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
385 return false;
386 }
387 return true;
388 }
389 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000390 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000391
392 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
393 if (rhsQI) {
394 RHSProtoI = rhsQI->qual_begin();
395 RHSProtoE = rhsQI->qual_end();
396 } else if (rhsQID) {
397 RHSProtoI = rhsQID->qual_begin();
398 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000399 } else {
400 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000401 }
402
403 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
404 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
405 bool match = false;
406
407 // when comparing an id<P> on lhs with a static type on rhs,
408 // see if static class implements all of id's protocols, directly or
409 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000410 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
411 ObjCProtocolDecl *rhsProto = *RHSProtoI;
412 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
413 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000414 match = true;
415 break;
416 }
417 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000418 if (!match)
419 return false;
420 }
421
422 return true;
423 }
424
425 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
426 assert(rhsQID && "One of the LHS/RHS should be id<x>");
427
428 if (!lhs->isPointerType())
429 return false;
430
431 QualType ltype = lhs->getAsPointerType()->getPointeeType();
432 if (const ObjCQualifiedInterfaceType *lhsQI =
433 ltype->getAsObjCQualifiedInterfaceType()) {
434 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
435 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
436 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
437 bool match = false;
438 ObjCProtocolDecl *lhsProto = *LHSProtoI;
439 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
440 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
441 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000442 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000443 match = true;
444 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000445 }
446 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000447 if (!match)
448 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000449 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000450 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000451 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000452
453 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
454 // for static type vs. qualified 'id' type, check that class implements
455 // all of 'id's protocols.
456 ObjCInterfaceDecl *lhsID = IT->getDecl();
457 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
458 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
459 if (!ClassImplementsProtocol(rhsProto, lhsID, compare))
460 return false;
461 }
462 return true;
463 }
464 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000465}
466