blob: 99a7afdc1af3250e7a29b1f34dbe8d0eb36f6944 [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 Lattner13fd7e52008-06-21 21:44:18 +000057 if (strIFace)
58 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000059 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-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 Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +000087 QualType t = Context.getObjCSelType();
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +000096 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000097 if (!PDecl) {
98 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
99 return true;
100 }
101
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000110 ObjCMethodDecl *Method) {
Chris Lattner85a932e2008-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 Lattner987798a2008-04-02 17:17:33 +0000121 if (lhsType->isArrayType())
122 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000123 else if (lhsType->isFunctionType())
124 lhsType = Context.getPointerType(lhsType);
125
Chris Lattner987798a2008-04-02 17:17:33 +0000126 AssignConvertType Result =
127 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000149 ObjCInterfaceDecl* ClassDecl = 0;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000150 if (!strcmp(receiverName->getName(), "super") && getCurMethodDecl()) {
151 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
Steve Naroff95110962008-03-28 21:37:05 +0000152 if (!ClassDecl)
153 return Diag(lbrac, diag::error_no_super_class,
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000154 getCurMethodDecl()->getClassInterface()->getName());
155 if (getCurMethodDecl()->isInstance()) {
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);
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000158 ExprResult ReceiverExpr = new PreDefinedExpr(SourceLocation(), superTy,
159 PreDefinedExpr::ObjCSuper);
Chris Lattner85a932e2008-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 Naroffcb28be62008-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 Lattner85a932e2008-01-04 22:32:30 +0000180 QualType returnType;
Steve Naroffcb28be62008-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 Naroff9ad23d62008-06-04 14:43:54 +0000193 }
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000197 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-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 Kremenek4df728e2008-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 Lattner85a932e2008-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().
Chris Lattner1565e032008-07-21 06:31:05 +0000220Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattner85a932e2008-01-04 22:32:30 +0000221 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
222{
223 assert(receiver && "missing receiver expression");
224
225 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
226 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000227 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000228
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000229 QualType receiverType =
230 RExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000231
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000232 // Handle messages to id.
Steve Naroffe2af8b12008-06-05 14:49:39 +0000233 if (receiverType == Context.getObjCIdType().getCanonicalType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000234 ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method;
Chris Lattner6e10a082008-02-01 06:57:39 +0000235 if (!Method)
236 Method = FactoryMethodPool[Sel].Method;
Chris Lattner85a932e2008-01-04 22:32:30 +0000237 if (!Method) {
238 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
239 SourceRange(lbrac, rbrac));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000241 } else {
242 returnType = Method->getResultType();
243 if (Sel.getNumArgs())
244 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
245 return true;
246 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000247 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
248 ArgExprs, NumArgs);
249 }
250
251 // Handle messages to Class.
252 if (receiverType == Context.getObjCClassType().getCanonicalType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000253 ObjCMethodDecl *Method = 0;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000254 if (getCurMethodDecl()) {
255 ObjCInterfaceDecl* ClassDecl = getCurMethodDecl()->getClassInterface();
Steve Naroffe2af8b12008-06-05 14:49:39 +0000256 // If we have an implementation in scope, check "private" methods.
257 if (ClassDecl)
258 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000259 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000260 Method = ImpDecl->getClassMethod(Sel);
261 }
262 if (!Method)
263 Method = FactoryMethodPool[Sel].Method;
264 if (!Method)
265 Method = InstanceMethodPool[Sel].Method;
266 if (!Method) {
267 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
Chris Lattner0b2f7ea2008-07-21 06:16:07 +0000268 RExpr->getSourceRange());
Steve Naroffe2af8b12008-06-05 14:49:39 +0000269 returnType = Context.getObjCIdType();
270 } else {
271 returnType = Method->getResultType();
272 if (Sel.getNumArgs())
273 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
274 return true;
275 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000276
277 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
278 ArgExprs, NumArgs);
279 }
280
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000281 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000282 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000283
284 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
285 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000286 if (ObjCQualifiedIdType *QIT =
287 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000288 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000289 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
290 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
291 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
292 break;
293 }
294 if (!Method)
295 Diag(lbrac, diag::warn_method_not_found_in_protocol,
296 std::string("-"), Sel.getName(),
Chris Lattner0b2f7ea2008-07-21 06:16:07 +0000297 RExpr->getSourceRange());
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000298 } else if (const ObjCInterfaceType *OCIReceiver =
299 receiverType->getAsPointerToObjCInterfaceType()) {
300 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000301
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000302 ClassDecl = OCIReceiver->getDecl();
303 // FIXME: consider using InstanceMethodPool, since it will be faster
304 // than the following method (which can do *many* linear searches). The
305 // idea is to add class info to InstanceMethodPool.
306 Method = ClassDecl->lookupInstanceMethod(Sel);
307
308 if (!Method) {
309 // Search protocol qualifiers.
310 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
311 E = OCIReceiver->qual_end(); QI != E; ++QI) {
312 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000313 break;
314 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000315 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000316
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000317 if (!Method && !OCIReceiver->qual_empty())
318 Diag(lbrac, diag::warn_method_not_found_in_protocol,
319 std::string("-"), Sel.getName(),
Chris Lattner85a932e2008-01-04 22:32:30 +0000320 SourceRange(lbrac, rbrac));
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000321 } else {
322 Diag(lbrac, diag::error_bad_receiver_type,
Chris Lattner0b2f7ea2008-07-21 06:16:07 +0000323 RExpr->getType().getAsString(), RExpr->getSourceRange());
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000324 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000325 }
326
327 if (!Method) {
328 // If we have an implementation in scope, check "private" methods.
329 if (ClassDecl)
330 if (ObjCImplementationDecl *ImpDecl =
331 ObjCImplementations[ClassDecl->getIdentifier()])
332 Method = ImpDecl->getInstanceMethod(Sel);
333 // If we still haven't found a method, look in the global pool. This
334 // behavior isn't very desirable, however we need it for GCC
335 // compatibility.
336 if (!Method)
337 Method = InstanceMethodPool[Sel].Method;
338 }
339 if (!Method) {
340 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
341 SourceRange(lbrac, rbrac));
342 returnType = Context.getObjCIdType();
343 } else {
344 returnType = Method->getResultType();
345 if (Sel.getNumArgs())
346 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
347 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000348 }
349 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
350 ArgExprs, NumArgs);
351}
Chris Lattnereca7be62008-04-07 05:30:13 +0000352
353//===----------------------------------------------------------------------===//
354// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
355//===----------------------------------------------------------------------===//
356
357/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
358/// inheritance hierarchy of 'rProto'.
359static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
360 ObjCProtocolDecl *rProto) {
361 if (lProto == rProto)
362 return true;
363 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
364 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
365 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
366 return true;
367 return false;
368}
369
370/// ClassImplementsProtocol - Checks that 'lProto' protocol
371/// has been implemented in IDecl class, its super class or categories (if
372/// lookupCategory is true).
373static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
374 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000375 bool lookupCategory,
376 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000377
378 // 1st, look up the class.
379 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
380 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
381 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
382 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000383 // This is dubious and is added to be compatible with gcc.
384 // In gcc, it is also allowed assigning a protocol-qualified 'id'
385 // type to a LHS object when protocol in qualified LHS is in list
386 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000387 // FIXME: Treat this as an extension, and flag this as an error when
388 // GCC extensions are not enabled.
Fariborz Jahanian26631702008-06-04 19:00:03 +0000389 else if (RHSIsQualifiedID &&
390 ProtocolCompatibleWithProtocol(protoList[i], lProto))
391 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000392 }
393
394 // 2nd, look up the category.
395 if (lookupCategory)
396 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
397 CDecl = CDecl->getNextClassCategory()) {
398 protoList = CDecl->getReferencedProtocols();
399 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
400 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
401 return true;
402 }
403 }
404
405 // 3rd, look up the super class(s)
406 if (IDecl->getSuperClass())
407 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000408 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
409 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000410
411 return false;
412}
413
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000414/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
415/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000416bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
417 bool compare) {
418 // Allow id<P..> and an 'id' or void* type in all cases.
419 if (const PointerType *PT = lhs->getAsPointerType()) {
420 QualType PointeeTy = PT->getPointeeType();
421 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
422 return true;
423 } else if (const PointerType *PT = rhs->getAsPointerType()) {
424 QualType PointeeTy = PT->getPointeeType();
425 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
426 return true;
427 }
428
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000429 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
430 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
431 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000432 QualType rtype;
433
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000434 if (!rhsQID) {
435 // Not comparing two ObjCQualifiedIdType's?
436 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000437
438 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000439 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000440 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000441 // If the RHS is a unqualified interface pointer "NSString*",
442 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000443 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
444 ObjCInterfaceDecl *rhsID = IT->getDecl();
445 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
446 // when comparing an id<P> on lhs with a static type on rhs,
447 // see if static class implements all of id's protocols, directly or
448 // through its super class and categories.
449 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
450 return false;
451 }
452 return true;
453 }
454 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000455 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000456
457 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000458 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000459 RHSProtoI = rhsQI->qual_begin();
460 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000461 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000462 RHSProtoI = rhsQID->qual_begin();
463 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000464 } else {
465 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000466 }
467
468 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
469 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
470 bool match = false;
471
472 // when comparing an id<P> on lhs with a static type on rhs,
473 // see if static class implements all of id's protocols, directly or
474 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000475 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
476 ObjCProtocolDecl *rhsProto = *RHSProtoI;
477 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
478 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000479 match = true;
480 break;
481 }
482 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000483 if (rhsQI) {
484 // If the RHS is a qualified interface pointer "NSString<P>*",
485 // make sure we check the class hierarchy.
486 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
487 ObjCInterfaceDecl *rhsID = IT->getDecl();
488 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
489 // when comparing an id<P> on lhs with a static type on rhs,
490 // see if static class implements all of id's protocols, directly or
491 // through its super class and categories.
492 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
493 match = true;
494 break;
495 }
496 }
497 }
498 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000499 if (!match)
500 return false;
501 }
502
503 return true;
504 }
505
506 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
507 assert(rhsQID && "One of the LHS/RHS should be id<x>");
508
509 if (!lhs->isPointerType())
510 return false;
511
512 QualType ltype = lhs->getAsPointerType()->getPointeeType();
513 if (const ObjCQualifiedInterfaceType *lhsQI =
514 ltype->getAsObjCQualifiedInterfaceType()) {
515 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
516 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
517 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
518 bool match = false;
519 ObjCProtocolDecl *lhsProto = *LHSProtoI;
520 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
521 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
522 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnereca7be62008-04-07 05:30:13 +0000523 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000524 match = true;
525 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000526 }
527 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000528 if (!match)
529 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000530 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000531 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000532 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000533
534 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
535 // for static type vs. qualified 'id' type, check that class implements
536 // all of 'id's protocols.
537 ObjCInterfaceDecl *lhsID = IT->getDecl();
538 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
539 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000540 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000541 return false;
542 }
543 return true;
544 }
545 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000546}
547