blob: 1067d1fb808a0fde85830a75a771110167c3a1f7 [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;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000150 if (!strcmp(receiverName->getName(), "super") && getCurMethodDecl()) {
151 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
Steve Naroff9fcf5b62008-03-28 21:37:05 +0000152 if (!ClassDecl)
153 return Diag(lbrac, diag::error_no_super_class,
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000154 getCurMethodDecl()->getClassInterface()->getName());
155 if (getCurMethodDecl()->isInstance()) {
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);
Chris Lattner4423d372008-06-21 18:04:54 +0000158 ExprResult ReceiverExpr = new PreDefinedExpr(SourceLocation(), superTy,
159 PreDefinedExpr::ObjCSuper);
Chris Lattner09493052008-01-04 22:32:30 +0000160 // 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
Steve Naroff51c6cff2008-06-04 23:08:38 +0000169 // ClassDecl is null in the following case.
170 //
171 // typedef XCElementDisplayRect XCElementGraphicsRect;
172 //
173 // @implementation XCRASlice
174 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
175 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
176 // }
177 //
178 // FIXME: Investigate why GCC allows the above.
179 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000180 QualType returnType;
Steve Naroff51c6cff2008-06-04 23:08:38 +0000181 if (ClassDecl) {
182 Method = ClassDecl->lookupClassMethod(Sel);
183
184 // If we have an implementation in scope, check "private" methods.
185 if (!Method) {
186 if (ObjCImplementationDecl *ImpDecl =
187 ObjCImplementations[ClassDecl->getIdentifier()])
188 Method = ImpDecl->getClassMethod(Sel);
189 }
190 // Before we give up, check if the selector is an instance method.
191 if (!Method)
192 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff616c55c2008-06-04 14:43:54 +0000193 }
Chris Lattner09493052008-01-04 22:32:30 +0000194 if (!Method) {
195 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
196 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000197 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000198 } else {
199 returnType = Method->getResultType();
200 if (Sel.getNumArgs()) {
201 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
202 return true;
203 }
204 }
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000205
206 // If we have the ObjCInterfaceDecl* for the class that is receiving
207 // the message, use that to construct the ObjCMessageExpr. Otherwise
208 // pass on the IdentifierInfo* for the class.
209 if (ClassDecl)
210 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
211 lbrac, rbrac, ArgExprs, NumArgs);
212 else
213 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
214 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000215}
216
217// ActOnInstanceMessage - used for both unary and keyword messages.
218// ArgExprs is optional - if it is present, the number of expressions
219// is obtained from Sel.getNumArgs().
220Sema::ExprResult Sema::ActOnInstanceMessage(
221 ExprTy *receiver, Selector Sel,
222 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
223{
224 assert(receiver && "missing receiver expression");
225
226 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
227 Expr *RExpr = static_cast<Expr *>(receiver);
Steve Naroff7c7817e2008-05-31 02:19:15 +0000228 QualType receiverType;
Chris Lattner09493052008-01-04 22:32:30 +0000229 QualType returnType;
Ted Kremenek42730c52008-01-07 19:49:32 +0000230 ObjCMethodDecl *Method = 0;
Steve Naroff7c7817e2008-05-31 02:19:15 +0000231
232 receiverType = RExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner09493052008-01-04 22:32:30 +0000233
Steve Naroff1a5027c2008-06-05 14:49:39 +0000234 if (receiverType == Context.getObjCIdType().getCanonicalType()) {
Chris Lattner09493052008-01-04 22:32:30 +0000235 Method = InstanceMethodPool[Sel].Method;
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000236 if (!Method)
237 Method = FactoryMethodPool[Sel].Method;
Chris Lattner09493052008-01-04 22:32:30 +0000238 if (!Method) {
239 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
240 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000241 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000242 } else {
243 returnType = Method->getResultType();
244 if (Sel.getNumArgs())
245 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
246 return true;
247 }
Steve Naroff1a5027c2008-06-05 14:49:39 +0000248 } else if (receiverType == Context.getObjCClassType().getCanonicalType()) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000249 if (getCurMethodDecl()) {
250 ObjCInterfaceDecl* ClassDecl = getCurMethodDecl()->getClassInterface();
Steve Naroff1a5027c2008-06-05 14:49:39 +0000251 // If we have an implementation in scope, check "private" methods.
252 if (ClassDecl)
253 if (ObjCImplementationDecl *ImpDecl =
254 ObjCImplementations[ClassDecl->getIdentifier()])
255 Method = ImpDecl->getClassMethod(Sel);
256 }
257 if (!Method)
258 Method = FactoryMethodPool[Sel].Method;
259 if (!Method)
260 Method = InstanceMethodPool[Sel].Method;
261 if (!Method) {
262 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
263 SourceRange(lbrac, rbrac));
264 returnType = Context.getObjCIdType();
265 } else {
266 returnType = Method->getResultType();
267 if (Sel.getNumArgs())
268 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
269 return true;
270 }
Chris Lattner09493052008-01-04 22:32:30 +0000271 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType);
Chris Lattner09493052008-01-04 22:32:30 +0000273 // FIXME (snaroff): checking in this code from Patrick. Needs to be
274 // revisited. how do we get the ClassDecl from the receiver expression?
275 if (!receiverIsQualId)
Chris Lattnere1b957d2008-02-01 06:43:02 +0000276 while (const PointerType *PTy = receiverType->getAsPointerType())
277 receiverType = PTy->getPointeeType();
278
Ted Kremenek42730c52008-01-07 19:49:32 +0000279 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner00cb97b2008-07-21 05:27:51 +0000280 if (ObjCQualifiedIdType *QIT =
Ted Kremenek42730c52008-01-07 19:49:32 +0000281 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattner09493052008-01-04 22:32:30 +0000282 // search protocols
283 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000284 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner09493052008-01-04 22:32:30 +0000285 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
286 break;
287 }
288 if (!Method)
289 Diag(lbrac, diag::warn_method_not_found_in_protocol,
290 std::string("-"), Sel.getName(),
291 SourceRange(lbrac, rbrac));
Chris Lattner00cb97b2008-07-21 05:27:51 +0000292 } else {
Chris Lattnere1b957d2008-02-01 06:43:02 +0000293 ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType);
294 if (OCIReceiver == 0) {
Chris Lattner00cb97b2008-07-21 05:27:51 +0000295 Diag(lbrac, diag::error_bad_receiver_type,
296 RExpr->getType().getAsString());
297 return true;
Fariborz Jahanianbc662012008-01-25 17:43:39 +0000298 }
Chris Lattner00cb97b2008-07-21 05:27:51 +0000299
Chris Lattnere1b957d2008-02-01 06:43:02 +0000300 ClassDecl = OCIReceiver->getDecl();
Chris Lattner09493052008-01-04 22:32:30 +0000301 // FIXME: consider using InstanceMethodPool, since it will be faster
302 // than the following method (which can do *many* linear searches). The
Chris Lattner00cb97b2008-07-21 05:27:51 +0000303 // idea is to add class info to InstanceMethodPool.
Chris Lattner09493052008-01-04 22:32:30 +0000304 Method = ClassDecl->lookupInstanceMethod(Sel);
Chris Lattner00cb97b2008-07-21 05:27:51 +0000305
306 if (!Method) {
307 // Search protocol qualifiers.
308 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
309 E = OCIReceiver->qual_end(); QI != E; ++QI) {
310 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
311 break;
312 }
313 }
314
315 if (!Method && !OCIReceiver->qual_empty())
316 Diag(lbrac, diag::warn_method_not_found_in_protocol,
317 std::string("-"), Sel.getName(),
318 SourceRange(lbrac, rbrac));
Chris Lattner09493052008-01-04 22:32:30 +0000319 }
Chris Lattner00cb97b2008-07-21 05:27:51 +0000320
Chris Lattner09493052008-01-04 22:32:30 +0000321 if (!Method) {
322 // If we have an implementation in scope, check "private" methods.
323 if (ClassDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +0000324 if (ObjCImplementationDecl *ImpDecl =
325 ObjCImplementations[ClassDecl->getIdentifier()])
Chris Lattner09493052008-01-04 22:32:30 +0000326 Method = ImpDecl->getInstanceMethod(Sel);
Chris Lattner43b885f2008-02-25 21:04:36 +0000327 // If we still haven't found a method, look in the global pool. This
328 // behavior isn't very desirable, however we need it for GCC
Chris Lattner09493052008-01-04 22:32:30 +0000329 // compatibility.
Chris Lattner43b885f2008-02-25 21:04:36 +0000330 if (!Method)
331 Method = InstanceMethodPool[Sel].Method;
Chris Lattner09493052008-01-04 22:32:30 +0000332 }
333 if (!Method) {
334 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
335 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000336 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000337 } else {
338 returnType = Method->getResultType();
339 if (Sel.getNumArgs())
340 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
341 return true;
342 }
343 }
344 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
345 ArgExprs, NumArgs);
346}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000347
348//===----------------------------------------------------------------------===//
349// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
350//===----------------------------------------------------------------------===//
351
352/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
353/// inheritance hierarchy of 'rProto'.
354static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
355 ObjCProtocolDecl *rProto) {
356 if (lProto == rProto)
357 return true;
358 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
359 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
360 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
361 return true;
362 return false;
363}
364
365/// ClassImplementsProtocol - Checks that 'lProto' protocol
366/// has been implemented in IDecl class, its super class or categories (if
367/// lookupCategory is true).
368static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
369 ObjCInterfaceDecl *IDecl,
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000370 bool lookupCategory,
371 bool RHSIsQualifiedID = false) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000372
373 // 1st, look up the class.
374 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
375 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
376 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
377 return true;
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000378 // This is dubious and is added to be compatible with gcc.
379 // In gcc, it is also allowed assigning a protocol-qualified 'id'
380 // type to a LHS object when protocol in qualified LHS is in list
381 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenek5a5cd832008-06-04 20:48:08 +0000382 // FIXME: Treat this as an extension, and flag this as an error when
383 // GCC extensions are not enabled.
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000384 else if (RHSIsQualifiedID &&
385 ProtocolCompatibleWithProtocol(protoList[i], lProto))
386 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000387 }
388
389 // 2nd, look up the category.
390 if (lookupCategory)
391 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
392 CDecl = CDecl->getNextClassCategory()) {
393 protoList = CDecl->getReferencedProtocols();
394 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
395 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
396 return true;
397 }
398 }
399
400 // 3rd, look up the super class(s)
401 if (IDecl->getSuperClass())
402 return
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000403 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
404 RHSIsQualifiedID);
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000405
406 return false;
407}
408
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000409/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
410/// ObjCQualifiedIDType.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000411bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
412 bool compare) {
413 // Allow id<P..> and an 'id' or void* type in all cases.
414 if (const PointerType *PT = lhs->getAsPointerType()) {
415 QualType PointeeTy = PT->getPointeeType();
416 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
417 return true;
418 } else if (const PointerType *PT = rhs->getAsPointerType()) {
419 QualType PointeeTy = PT->getPointeeType();
420 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
421 return true;
422 }
423
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000424 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
425 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
426 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000427 QualType rtype;
428
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000429 if (!rhsQID) {
430 // Not comparing two ObjCQualifiedIdType's?
431 if (!rhs->isPointerType()) return false;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000432
433 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000434 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000435 if (rhsQI == 0) {
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000436 // If the RHS is a unqualified interface pointer "NSString*",
437 // make sure we check the class hierarchy.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000438 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
439 ObjCInterfaceDecl *rhsID = IT->getDecl();
440 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
441 // when comparing an id<P> on lhs with a static type on rhs,
442 // see if static class implements all of id's protocols, directly or
443 // through its super class and categories.
444 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
445 return false;
446 }
447 return true;
448 }
449 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000450 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000451
452 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000453 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000454 RHSProtoI = rhsQI->qual_begin();
455 RHSProtoE = rhsQI->qual_end();
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000456 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000457 RHSProtoI = rhsQID->qual_begin();
458 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000459 } else {
460 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000461 }
462
463 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
464 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
465 bool match = false;
466
467 // when comparing an id<P> on lhs with a static type on rhs,
468 // see if static class implements all of id's protocols, directly or
469 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000470 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
471 ObjCProtocolDecl *rhsProto = *RHSProtoI;
472 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
473 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000474 match = true;
475 break;
476 }
477 }
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000478 if (rhsQI) {
479 // If the RHS is a qualified interface pointer "NSString<P>*",
480 // make sure we check the class hierarchy.
481 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
482 ObjCInterfaceDecl *rhsID = IT->getDecl();
483 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
484 // when comparing an id<P> on lhs with a static type on rhs,
485 // see if static class implements all of id's protocols, directly or
486 // through its super class and categories.
487 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
488 match = true;
489 break;
490 }
491 }
492 }
493 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000494 if (!match)
495 return false;
496 }
497
498 return true;
499 }
500
501 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
502 assert(rhsQID && "One of the LHS/RHS should be id<x>");
503
504 if (!lhs->isPointerType())
505 return false;
506
507 QualType ltype = lhs->getAsPointerType()->getPointeeType();
508 if (const ObjCQualifiedInterfaceType *lhsQI =
509 ltype->getAsObjCQualifiedInterfaceType()) {
510 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
511 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
512 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
513 bool match = false;
514 ObjCProtocolDecl *lhsProto = *LHSProtoI;
515 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
516 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
517 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000518 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000519 match = true;
520 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000521 }
522 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000523 if (!match)
524 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000525 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000526 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000527 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000528
529 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
530 // for static type vs. qualified 'id' type, check that class implements
531 // all of 'id's protocols.
532 ObjCInterfaceDecl *lhsID = IT->getDecl();
533 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
534 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000535 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000536 return false;
537 }
538 return true;
539 }
540 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000541}
542