blob: 97e1579548c5a86fa731d0023dfde9c9937adb10 [file] [log] [blame]
Chris Lattner09493052008-01-04 22:32:30 +00001//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective-C expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Chris Lattner09493052008-01-04 22:32:30 +000018using namespace clang;
19
20Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
21 ExprTy **Strings,
22 unsigned NumStrings) {
23 SourceLocation AtLoc = AtLocs[0];
24 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
25 if (NumStrings > 1) {
26 // Concatenate objc strings.
27 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
28 SourceLocation EndLoc = ES->getSourceRange().getEnd();
29 unsigned Length = 0;
30 for (unsigned i = 0; i < NumStrings; i++)
31 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
32 char *strBuf = new char [Length];
33 char *p = strBuf;
34 bool isWide = false;
35 for (unsigned i = 0; i < NumStrings; i++) {
36 S = static_cast<StringLiteral *>(Strings[i]);
37 if (S->isWide())
38 isWide = true;
39 memcpy(p, S->getStrData(), S->getByteLength());
40 p += S->getByteLength();
41 delete S;
42 }
43 S = new StringLiteral(strBuf, Length,
44 isWide, Context.getPointerType(Context.CharTy),
45 AtLoc, EndLoc);
46 }
47
48 if (CheckBuiltinCFStringArgument(S))
49 return true;
50
Ted Kremenek42730c52008-01-07 19:49:32 +000051 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner09493052008-01-04 22:32:30 +000052 // Initialize the constant string interface lazily. This assumes
53 // the NSConstantString interface is seen in this translation unit.
54 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Steve Naroff6384a012008-04-02 14:35:35 +000055 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000056 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattnerbac12452008-06-21 21:44:18 +000057 if (strIFace)
58 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner09493052008-01-04 22:32:30 +000059 }
Ted Kremenek42730c52008-01-07 19:49:32 +000060 QualType t = Context.getObjCConstantStringInterface();
Chris Lattnerbac12452008-06-21 21:44:18 +000061 // If there is no NSConstantString interface defined then treat constant
62 // strings as untyped objects and let the runtime figure it out later.
63 if (t == QualType()) {
64 t = Context.getObjCIdType();
65 } else {
66 t = Context.getPointerType(t);
67 }
Chris Lattner09493052008-01-04 22:32:30 +000068 return new ObjCStringLiteral(S, t, AtLoc);
69}
70
71Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
72 SourceLocation EncodeLoc,
73 SourceLocation LParenLoc,
74 TypeTy *Ty,
75 SourceLocation RParenLoc) {
76 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
77
78 QualType t = Context.getPointerType(Context.CharTy);
79 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
80}
81
82Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
83 SourceLocation AtLoc,
84 SourceLocation SelLoc,
85 SourceLocation LParenLoc,
86 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000087 QualType t = Context.getObjCSelType();
Chris Lattner09493052008-01-04 22:32:30 +000088 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
89}
90
91Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
92 SourceLocation AtLoc,
93 SourceLocation ProtoLoc,
94 SourceLocation LParenLoc,
95 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner09493052008-01-04 22:32:30 +000097 if (!PDecl) {
98 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
99 return true;
100 }
101
Ted Kremenek42730c52008-01-07 19:49:32 +0000102 QualType t = Context.getObjCProtoType();
Chris Lattner09493052008-01-04 22:32:30 +0000103 if (t.isNull())
104 return true;
105 t = Context.getPointerType(t);
106 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
107}
108
109bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 ObjCMethodDecl *Method) {
Chris Lattner09493052008-01-04 22:32:30 +0000111 bool anyIncompatibleArgs = false;
112
113 for (unsigned i = 0; i < NumArgs; i++) {
114 Expr *argExpr = Args[i];
115 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
116
117 QualType lhsType = Method->getParamDecl(i)->getType();
118 QualType rhsType = argExpr->getType();
119
120 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnera2ef0822008-04-02 17:17:33 +0000121 if (lhsType->isArrayType())
122 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner09493052008-01-04 22:32:30 +0000123 else if (lhsType->isFunctionType())
124 lhsType = Context.getPointerType(lhsType);
125
Chris Lattnera2ef0822008-04-02 17:17:33 +0000126 AssignConvertType Result =
127 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner09493052008-01-04 22:32:30 +0000128 if (Args[i] != argExpr) // The expression was converted.
129 Args[i] = argExpr; // Make sure we store the converted expression.
130
131 anyIncompatibleArgs |=
132 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
133 argExpr, "sending");
134 }
135 return anyIncompatibleArgs;
136}
137
138// ActOnClassMessage - used for both unary and keyword messages.
139// ArgExprs is optional - if it is present, the number of expressions
140// is obtained from Sel.getNumArgs().
141Sema::ExprResult Sema::ActOnClassMessage(
142 Scope *S,
143 IdentifierInfo *receiverName, Selector Sel,
144 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
145{
146 assert(receiverName && "missing receiver class name");
147
148 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremenek42730c52008-01-07 19:49:32 +0000149 ObjCInterfaceDecl* ClassDecl = 0;
Steve Naroff91a69202008-07-24 19:44:33 +0000150 bool isSuper = false;
151
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000152 if (!strcmp(receiverName->getName(), "super") && getCurMethodDecl()) {
Steve Naroff91a69202008-07-24 19:44:33 +0000153 isSuper = true;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000154 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
Steve Naroff9fcf5b62008-03-28 21:37:05 +0000155 if (!ClassDecl)
156 return Diag(lbrac, diag::error_no_super_class,
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000157 getCurMethodDecl()->getClassInterface()->getName());
158 if (getCurMethodDecl()->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000159 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner09493052008-01-04 22:32:30 +0000160 superTy = Context.getPointerType(superTy);
Chris Lattner4423d372008-06-21 18:04:54 +0000161 ExprResult ReceiverExpr = new PreDefinedExpr(SourceLocation(), superTy,
162 PreDefinedExpr::ObjCSuper);
Chris Lattner09493052008-01-04 22:32:30 +0000163 // We are really in an instance method, redirect.
164 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
165 Args, NumArgs);
166 }
167 // We are sending a message to 'super' within a class method. Do nothing,
168 // the receiver will pass through as 'super' (how convenient:-).
169 } else
170 ClassDecl = getObjCInterfaceDecl(receiverName);
171
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000172 // The following code allows for the following GCC-ism:
Steve Naroff51c6cff2008-06-04 23:08:38 +0000173 //
174 // typedef XCElementDisplayRect XCElementGraphicsRect;
175 //
176 // @implementation XCRASlice
177 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
178 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
179 // }
180 //
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000181 // If necessary, the following lookup could move to getObjCInterfaceDecl().
182 if (!ClassDecl) {
183 Decl *IDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, 0, false);
184 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
185 const ObjCInterfaceType *OCIT;
186 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
187 if (OCIT)
188 ClassDecl = OCIT->getDecl();
189 }
190 }
191 assert(ClassDecl && "missing interface declaration");
Steve Naroff51c6cff2008-06-04 23:08:38 +0000192 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000193 QualType returnType;
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000194 Method = ClassDecl->lookupClassMethod(Sel);
195
196 // If we have an implementation in scope, check "private" methods.
197 if (!Method) {
198 if (ObjCImplementationDecl *ImpDecl =
199 ObjCImplementations[ClassDecl->getIdentifier()])
200 Method = ImpDecl->getClassMethod(Sel);
Steve Naroff616c55c2008-06-04 14:43:54 +0000201 }
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000202 // Before we give up, check if the selector is an instance method.
203 if (!Method)
204 Method = ClassDecl->lookupInstanceMethod(Sel);
205
Chris Lattner09493052008-01-04 22:32:30 +0000206 if (!Method) {
207 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
208 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000209 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000210 } else {
211 returnType = Method->getResultType();
212 if (Sel.getNumArgs()) {
213 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
214 return true;
215 }
216 }
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000217
218 // If we have the ObjCInterfaceDecl* for the class that is receiving
219 // the message, use that to construct the ObjCMessageExpr. Otherwise
220 // pass on the IdentifierInfo* for the class.
Steve Naroff91a69202008-07-24 19:44:33 +0000221 // FIXME: need to do a better job handling 'super' usage within a class
222 // For now, we simply pass the "super" identifier through (which isn't
223 // consistent with instance methods.
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000224 if (isSuper)
Steve Naroff91a69202008-07-24 19:44:33 +0000225 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000226 lbrac, rbrac, ArgExprs, NumArgs);
227 else
Steve Naroff91a69202008-07-24 19:44:33 +0000228 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000229 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000230}
231
232// ActOnInstanceMessage - used for both unary and keyword messages.
233// ArgExprs is optional - if it is present, the number of expressions
234// is obtained from Sel.getNumArgs().
Chris Lattner4d018542008-07-21 06:31:05 +0000235Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattner09493052008-01-04 22:32:30 +0000236 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
237{
238 assert(receiver && "missing receiver expression");
239
240 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
241 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner09493052008-01-04 22:32:30 +0000242 QualType returnType;
Steve Naroff7c7817e2008-05-31 02:19:15 +0000243
Chris Lattner4cff4b72008-07-21 05:57:44 +0000244 QualType receiverType =
245 RExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner09493052008-01-04 22:32:30 +0000246
Chris Lattner4cff4b72008-07-21 05:57:44 +0000247 // Handle messages to id.
Steve Naroff1a5027c2008-06-05 14:49:39 +0000248 if (receiverType == Context.getObjCIdType().getCanonicalType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000249 ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method;
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000250 if (!Method)
251 Method = FactoryMethodPool[Sel].Method;
Chris Lattner09493052008-01-04 22:32:30 +0000252 if (!Method) {
253 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
254 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000255 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000256 } else {
257 returnType = Method->getResultType();
258 if (Sel.getNumArgs())
259 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
260 return true;
261 }
Chris Lattner4cff4b72008-07-21 05:57:44 +0000262 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
263 ArgExprs, NumArgs);
264 }
265
266 // Handle messages to Class.
267 if (receiverType == Context.getObjCClassType().getCanonicalType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000268 ObjCMethodDecl *Method = 0;
Chris Lattner55a24332008-07-21 06:44:27 +0000269 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +0000270 // If we have an implementation in scope, check "private" methods.
Chris Lattner55a24332008-07-21 06:44:27 +0000271 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff1a5027c2008-06-05 14:49:39 +0000272 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner4cff4b72008-07-21 05:57:44 +0000273 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroff1a5027c2008-06-05 14:49:39 +0000274 Method = ImpDecl->getClassMethod(Sel);
275 }
276 if (!Method)
277 Method = FactoryMethodPool[Sel].Method;
278 if (!Method)
279 Method = InstanceMethodPool[Sel].Method;
280 if (!Method) {
281 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
Chris Lattner3977eb72008-07-21 06:16:07 +0000282 RExpr->getSourceRange());
Steve Naroff1a5027c2008-06-05 14:49:39 +0000283 returnType = Context.getObjCIdType();
284 } else {
285 returnType = Method->getResultType();
286 if (Sel.getNumArgs())
287 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
288 return true;
289 }
Chris Lattner4cff4b72008-07-21 05:57:44 +0000290
291 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
292 ArgExprs, NumArgs);
293 }
294
Chris Lattnerf9147652008-07-21 06:12:56 +0000295 ObjCMethodDecl *Method = 0;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000296 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattnerf9147652008-07-21 06:12:56 +0000297
298 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
299 // long as one of the protocols implements the selector (if not, warn).
Chris Lattner4cff4b72008-07-21 05:57:44 +0000300 if (ObjCQualifiedIdType *QIT =
301 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000302 // Search protocols
Chris Lattner4cff4b72008-07-21 05:57:44 +0000303 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
304 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
305 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
306 break;
307 }
308 if (!Method)
309 Diag(lbrac, diag::warn_method_not_found_in_protocol,
310 std::string("-"), Sel.getName(),
Chris Lattner3977eb72008-07-21 06:16:07 +0000311 RExpr->getSourceRange());
Chris Lattnerf9147652008-07-21 06:12:56 +0000312 } else if (const ObjCInterfaceType *OCIReceiver =
313 receiverType->getAsPointerToObjCInterfaceType()) {
314 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnere1b957d2008-02-01 06:43:02 +0000315
Chris Lattner4cff4b72008-07-21 05:57:44 +0000316 ClassDecl = OCIReceiver->getDecl();
317 // FIXME: consider using InstanceMethodPool, since it will be faster
318 // than the following method (which can do *many* linear searches). The
319 // idea is to add class info to InstanceMethodPool.
320 Method = ClassDecl->lookupInstanceMethod(Sel);
321
322 if (!Method) {
323 // Search protocol qualifiers.
324 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
325 E = OCIReceiver->qual_end(); QI != E; ++QI) {
326 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner09493052008-01-04 22:32:30 +0000327 break;
328 }
Chris Lattner09493052008-01-04 22:32:30 +0000329 }
Chris Lattner00cb97b2008-07-21 05:27:51 +0000330
Chris Lattner4cff4b72008-07-21 05:57:44 +0000331 if (!Method && !OCIReceiver->qual_empty())
332 Diag(lbrac, diag::warn_method_not_found_in_protocol,
333 std::string("-"), Sel.getName(),
Chris Lattner09493052008-01-04 22:32:30 +0000334 SourceRange(lbrac, rbrac));
Chris Lattnerf9147652008-07-21 06:12:56 +0000335 } else {
336 Diag(lbrac, diag::error_bad_receiver_type,
Chris Lattner3977eb72008-07-21 06:16:07 +0000337 RExpr->getType().getAsString(), RExpr->getSourceRange());
Chris Lattnerf9147652008-07-21 06:12:56 +0000338 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000339 }
340
341 if (!Method) {
342 // If we have an implementation in scope, check "private" methods.
343 if (ClassDecl)
344 if (ObjCImplementationDecl *ImpDecl =
345 ObjCImplementations[ClassDecl->getIdentifier()])
346 Method = ImpDecl->getInstanceMethod(Sel);
347 // If we still haven't found a method, look in the global pool. This
348 // behavior isn't very desirable, however we need it for GCC
349 // compatibility.
350 if (!Method)
351 Method = InstanceMethodPool[Sel].Method;
352 }
353 if (!Method) {
354 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
355 SourceRange(lbrac, rbrac));
356 returnType = Context.getObjCIdType();
357 } else {
358 returnType = Method->getResultType();
359 if (Sel.getNumArgs())
360 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
361 return true;
Chris Lattner09493052008-01-04 22:32:30 +0000362 }
363 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
364 ArgExprs, NumArgs);
365}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000366
367//===----------------------------------------------------------------------===//
368// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
369//===----------------------------------------------------------------------===//
370
371/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
372/// inheritance hierarchy of 'rProto'.
373static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
374 ObjCProtocolDecl *rProto) {
375 if (lProto == rProto)
376 return true;
Chris Lattner0be08822008-07-21 21:32:27 +0000377 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
378 E = rProto->protocol_end(); PI != E; ++PI)
379 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000380 return true;
381 return false;
382}
383
384/// ClassImplementsProtocol - Checks that 'lProto' protocol
385/// has been implemented in IDecl class, its super class or categories (if
386/// lookupCategory is true).
387static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
388 ObjCInterfaceDecl *IDecl,
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000389 bool lookupCategory,
390 bool RHSIsQualifiedID = false) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000391
392 // 1st, look up the class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000393 const ObjCList<ObjCProtocolDecl> &Protocols =
394 IDecl->getReferencedProtocols();
395
396 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
397 E = Protocols.end(); PI != E; ++PI) {
398 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000399 return true;
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000400 // This is dubious and is added to be compatible with gcc.
401 // In gcc, it is also allowed assigning a protocol-qualified 'id'
402 // type to a LHS object when protocol in qualified LHS is in list
403 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenek5a5cd832008-06-04 20:48:08 +0000404 // FIXME: Treat this as an extension, and flag this as an error when
405 // GCC extensions are not enabled.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000406 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000407 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000408 }
409
410 // 2nd, look up the category.
411 if (lookupCategory)
412 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
413 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner0be08822008-07-21 21:32:27 +0000414 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
415 E = CDecl->protocol_end(); PI != E; ++PI)
416 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000417 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000418 }
419
420 // 3rd, look up the super class(s)
421 if (IDecl->getSuperClass())
422 return
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000423 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
424 RHSIsQualifiedID);
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000425
426 return false;
427}
428
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000429/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
430/// ObjCQualifiedIDType.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000431bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
432 bool compare) {
433 // Allow id<P..> and an 'id' or void* type in all cases.
434 if (const PointerType *PT = lhs->getAsPointerType()) {
435 QualType PointeeTy = PT->getPointeeType();
436 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
437 return true;
438 } else if (const PointerType *PT = rhs->getAsPointerType()) {
439 QualType PointeeTy = PT->getPointeeType();
440 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
441 return true;
442 }
443
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000444 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
445 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
446 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000447 QualType rtype;
448
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000449 if (!rhsQID) {
450 // Not comparing two ObjCQualifiedIdType's?
451 if (!rhs->isPointerType()) return false;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000452
453 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000454 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000455 if (rhsQI == 0) {
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000456 // If the RHS is a unqualified interface pointer "NSString*",
457 // make sure we check the class hierarchy.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000458 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
459 ObjCInterfaceDecl *rhsID = IT->getDecl();
460 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
461 // when comparing an id<P> on lhs with a static type on rhs,
462 // see if static class implements all of id's protocols, directly or
463 // through its super class and categories.
464 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
465 return false;
466 }
467 return true;
468 }
469 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000470 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000471
472 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000473 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000474 RHSProtoI = rhsQI->qual_begin();
475 RHSProtoE = rhsQI->qual_end();
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000476 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000477 RHSProtoI = rhsQID->qual_begin();
478 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000479 } else {
480 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000481 }
482
483 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
484 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
485 bool match = false;
486
487 // when comparing an id<P> on lhs with a static type on rhs,
488 // see if static class implements all of id's protocols, directly or
489 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000490 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
491 ObjCProtocolDecl *rhsProto = *RHSProtoI;
492 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
493 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000494 match = true;
495 break;
496 }
497 }
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000498 if (rhsQI) {
499 // If the RHS is a qualified interface pointer "NSString<P>*",
500 // make sure we check the class hierarchy.
501 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
502 ObjCInterfaceDecl *rhsID = IT->getDecl();
503 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
504 // when comparing an id<P> on lhs with a static type on rhs,
505 // see if static class implements all of id's protocols, directly or
506 // through its super class and categories.
507 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
508 match = true;
509 break;
510 }
511 }
512 }
513 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000514 if (!match)
515 return false;
516 }
517
518 return true;
519 }
520
521 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
522 assert(rhsQID && "One of the LHS/RHS should be id<x>");
523
524 if (!lhs->isPointerType())
525 return false;
526
527 QualType ltype = lhs->getAsPointerType()->getPointeeType();
528 if (const ObjCQualifiedInterfaceType *lhsQI =
529 ltype->getAsObjCQualifiedInterfaceType()) {
530 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
531 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
532 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
533 bool match = false;
534 ObjCProtocolDecl *lhsProto = *LHSProtoI;
535 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
536 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
537 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000538 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000539 match = true;
540 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000541 }
542 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000543 if (!match)
544 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000545 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000546 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000547 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000548
549 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
550 // for static type vs. qualified 'id' type, check that class implements
551 // all of 'id's protocols.
552 ObjCInterfaceDecl *lhsID = IT->getDecl();
553 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
554 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000555 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000556 return false;
557 }
558 return true;
559 }
560 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000561}
562