blob: 0d3bc298400b966f4e28f7f63bbfc9f98e6fe2a1 [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"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000019using namespace clang;
20
21Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
22 ExprTy **Strings,
23 unsigned NumStrings) {
24 SourceLocation AtLoc = AtLocs[0];
25 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
26 if (NumStrings > 1) {
27 // Concatenate objc strings.
28 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
29 SourceLocation EndLoc = ES->getSourceRange().getEnd();
30 unsigned Length = 0;
31 for (unsigned i = 0; i < NumStrings; i++)
32 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
33 char *strBuf = new char [Length];
34 char *p = strBuf;
35 bool isWide = false;
36 for (unsigned i = 0; i < NumStrings; i++) {
37 S = static_cast<StringLiteral *>(Strings[i]);
38 if (S->isWide())
39 isWide = true;
40 memcpy(p, S->getStrData(), S->getByteLength());
41 p += S->getByteLength();
42 delete S;
43 }
44 S = new StringLiteral(strBuf, Length,
45 isWide, Context.getPointerType(Context.CharTy),
46 AtLoc, EndLoc);
47 }
48
49 if (CheckBuiltinCFStringArgument(S))
50 return true;
51
Ted Kremeneka526c5c2008-01-07 19:49:32 +000052 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-01-04 22:32:30 +000053 // Initialize the constant string interface lazily. This assumes
54 // the NSConstantString interface is seen in this translation unit.
55 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Steve Naroffb327ce02008-04-02 14:35:35 +000056 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner13fd7e52008-06-21 21:44:18 +000058 if (strIFace)
59 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000060 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-06-21 21:44:18 +000062 // If there is no NSConstantString interface defined then treat constant
63 // strings as untyped objects and let the runtime figure it out later.
64 if (t == QualType()) {
65 t = Context.getObjCIdType();
66 } else {
67 t = Context.getPointerType(t);
68 }
Chris Lattner85a932e2008-01-04 22:32:30 +000069 return new ObjCStringLiteral(S, t, AtLoc);
70}
71
72Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
73 SourceLocation EncodeLoc,
74 SourceLocation LParenLoc,
75 TypeTy *Ty,
76 SourceLocation RParenLoc) {
77 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
78
79 QualType t = Context.getPointerType(Context.CharTy);
80 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
81}
82
83Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
84 SourceLocation AtLoc,
85 SourceLocation SelLoc,
86 SourceLocation LParenLoc,
87 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 QualType t = Context.getObjCSelType();
Chris Lattner85a932e2008-01-04 22:32:30 +000089 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
90}
91
92Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
93 SourceLocation AtLoc,
94 SourceLocation ProtoLoc,
95 SourceLocation LParenLoc,
96 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000098 if (!PDecl) {
99 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
100 return true;
101 }
102
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000104 if (t.isNull())
105 return true;
106 t = Context.getPointerType(t);
107 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
108}
109
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000110bool Sema::CheckMessageArgumentTypes(Expr **Args, Selector Sel,
111 ObjCMethodDecl *Method,
112 const char *PrefixStr,
113 SourceLocation lbrac, SourceLocation rbrac,
114 QualType &ReturnType) {
115 unsigned NumArgs = Sel.getNumArgs();
116 if (!Method) {
117 Diag(lbrac, diag::warn_method_not_found, std::string(PrefixStr),
118 Sel.getName(), SourceRange(lbrac, rbrac));
119 ReturnType = Context.getObjCIdType();
120 return false;
121 } else {
122 ReturnType = Method->getResultType();
123 }
124
Chris Lattner85a932e2008-01-04 22:32:30 +0000125 bool anyIncompatibleArgs = false;
Chris Lattner85a932e2008-01-04 22:32:30 +0000126 for (unsigned i = 0; i < NumArgs; i++) {
127 Expr *argExpr = Args[i];
128 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
129
130 QualType lhsType = Method->getParamDecl(i)->getType();
131 QualType rhsType = argExpr->getType();
132
133 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000134 if (lhsType->isArrayType())
135 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000136 else if (lhsType->isFunctionType())
137 lhsType = Context.getPointerType(lhsType);
138
Chris Lattner987798a2008-04-02 17:17:33 +0000139 AssignConvertType Result =
140 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000141 if (Args[i] != argExpr) // The expression was converted.
142 Args[i] = argExpr; // Make sure we store the converted expression.
143
144 anyIncompatibleArgs |=
145 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
146 argExpr, "sending");
147 }
148 return anyIncompatibleArgs;
149}
150
151// ActOnClassMessage - used for both unary and keyword messages.
152// ArgExprs is optional - if it is present, the number of expressions
153// is obtained from Sel.getNumArgs().
154Sema::ExprResult Sema::ActOnClassMessage(
155 Scope *S,
156 IdentifierInfo *receiverName, Selector Sel,
157 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
158{
159 assert(receiverName && "missing receiver class name");
160
161 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000162 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000163 bool isSuper = false;
164
Daniel Dunbar662e8b52008-08-14 22:04:54 +0000165 if (receiverName == SuperID && getCurMethodDecl()) {
Steve Narofffc93d522008-07-24 19:44:33 +0000166 isSuper = true;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000167 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
Steve Naroff95110962008-03-28 21:37:05 +0000168 if (!ClassDecl)
169 return Diag(lbrac, diag::error_no_super_class,
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000170 getCurMethodDecl()->getClassInterface()->getName());
171 if (getCurMethodDecl()->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000172 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner85a932e2008-01-04 22:32:30 +0000173 superTy = Context.getPointerType(superTy);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000174 ExprResult ReceiverExpr = new PredefinedExpr(SourceLocation(), superTy,
175 PredefinedExpr::ObjCSuper);
Chris Lattner85a932e2008-01-04 22:32:30 +0000176 // We are really in an instance method, redirect.
177 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
178 Args, NumArgs);
179 }
180 // We are sending a message to 'super' within a class method. Do nothing,
181 // the receiver will pass through as 'super' (how convenient:-).
182 } else
183 ClassDecl = getObjCInterfaceDecl(receiverName);
184
Steve Naroff7c778f12008-07-25 19:39:00 +0000185 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000186 //
187 // typedef XCElementDisplayRect XCElementGraphicsRect;
188 //
189 // @implementation XCRASlice
190 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
191 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
192 // }
193 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000194 // If necessary, the following lookup could move to getObjCInterfaceDecl().
195 if (!ClassDecl) {
196 Decl *IDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, 0, false);
197 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
198 const ObjCInterfaceType *OCIT;
199 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
200 if (OCIT)
201 ClassDecl = OCIT->getDecl();
202 }
203 }
204 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000205 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000206 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000207 Method = ClassDecl->lookupClassMethod(Sel);
208
209 // If we have an implementation in scope, check "private" methods.
210 if (!Method) {
211 if (ObjCImplementationDecl *ImpDecl =
212 ObjCImplementations[ClassDecl->getIdentifier()])
213 Method = ImpDecl->getClassMethod(Sel);
Steve Naroff9ad23d62008-06-04 14:43:54 +0000214 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000215 // Before we give up, check if the selector is an instance method.
216 if (!Method)
217 Method = ClassDecl->lookupInstanceMethod(Sel);
218
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000219 if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "+",
220 lbrac, rbrac, returnType))
221 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000222
223 // If we have the ObjCInterfaceDecl* for the class that is receiving
224 // the message, use that to construct the ObjCMessageExpr. Otherwise
225 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000226 // FIXME: need to do a better job handling 'super' usage within a class
227 // For now, we simply pass the "super" identifier through (which isn't
228 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000229 if (isSuper)
Steve Narofffc93d522008-07-24 19:44:33 +0000230 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenek4df728e2008-06-24 15:50:53 +0000231 lbrac, rbrac, ArgExprs, NumArgs);
232 else
Steve Narofffc93d522008-07-24 19:44:33 +0000233 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenek4df728e2008-06-24 15:50:53 +0000234 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000235}
236
237// ActOnInstanceMessage - used for both unary and keyword messages.
238// ArgExprs is optional - if it is present, the number of expressions
239// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000240Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000241 SourceLocation lbrac,
242 SourceLocation rbrac,
243 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000244 assert(receiver && "missing receiver expression");
245
246 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
247 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000248 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000249
Chris Lattnerb77792e2008-07-26 22:17:49 +0000250 QualType ReceiverCType =
251 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000252
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000253 // Handle messages to id.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000254 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000255 ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method;
Chris Lattner6e10a082008-02-01 06:57:39 +0000256 if (!Method)
257 Method = FactoryMethodPool[Sel].Method;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000258 if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "-",
259 lbrac, rbrac, returnType))
260 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000261 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
262 ArgExprs, NumArgs);
263 }
264
265 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000266 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000267 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000268 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000269 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000270 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000271 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000272 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000273 Method = ImpDecl->getClassMethod(Sel);
274 }
275 if (!Method)
276 Method = FactoryMethodPool[Sel].Method;
277 if (!Method)
278 Method = InstanceMethodPool[Sel].Method;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000279 if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "-",
280 lbrac, rbrac, returnType))
281 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000282 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
283 ArgExprs, NumArgs);
284 }
285
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000286 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000287 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000288
289 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
290 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000291 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000292 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000293 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
294 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
295 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
296 break;
297 }
298 if (!Method)
299 Diag(lbrac, diag::warn_method_not_found_in_protocol,
300 std::string("-"), Sel.getName(),
Chris Lattner0b2f7ea2008-07-21 06:16:07 +0000301 RExpr->getSourceRange());
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000302 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000303 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000304 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000305
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000306 ClassDecl = OCIReceiver->getDecl();
307 // FIXME: consider using InstanceMethodPool, since it will be faster
308 // than the following method (which can do *many* linear searches). The
309 // idea is to add class info to InstanceMethodPool.
310 Method = ClassDecl->lookupInstanceMethod(Sel);
311
312 if (!Method) {
313 // Search protocol qualifiers.
314 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
315 E = OCIReceiver->qual_end(); QI != E; ++QI) {
316 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000317 break;
318 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000319 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000320
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000321 if (!Method && !OCIReceiver->qual_empty())
322 Diag(lbrac, diag::warn_method_not_found_in_protocol,
323 std::string("-"), Sel.getName(),
Chris Lattner85a932e2008-01-04 22:32:30 +0000324 SourceRange(lbrac, rbrac));
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000325 } else {
326 Diag(lbrac, diag::error_bad_receiver_type,
Chris Lattner0b2f7ea2008-07-21 06:16:07 +0000327 RExpr->getType().getAsString(), RExpr->getSourceRange());
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000328 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000329 }
330
331 if (!Method) {
332 // If we have an implementation in scope, check "private" methods.
333 if (ClassDecl)
334 if (ObjCImplementationDecl *ImpDecl =
335 ObjCImplementations[ClassDecl->getIdentifier()])
336 Method = ImpDecl->getInstanceMethod(Sel);
337 // If we still haven't found a method, look in the global pool. This
338 // behavior isn't very desirable, however we need it for GCC
339 // compatibility.
340 if (!Method)
341 Method = InstanceMethodPool[Sel].Method;
342 }
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000343 if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "-",
344 lbrac, rbrac, returnType))
345 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000346 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
347 ArgExprs, NumArgs);
348}
Chris Lattnereca7be62008-04-07 05:30:13 +0000349
350//===----------------------------------------------------------------------===//
351// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
352//===----------------------------------------------------------------------===//
353
354/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
355/// inheritance hierarchy of 'rProto'.
356static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
357 ObjCProtocolDecl *rProto) {
358 if (lProto == rProto)
359 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000360 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
361 E = rProto->protocol_end(); PI != E; ++PI)
362 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000363 return true;
364 return false;
365}
366
367/// ClassImplementsProtocol - Checks that 'lProto' protocol
368/// has been implemented in IDecl class, its super class or categories (if
369/// lookupCategory is true).
370static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
371 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000372 bool lookupCategory,
373 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000374
375 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000376 const ObjCList<ObjCProtocolDecl> &Protocols =
377 IDecl->getReferencedProtocols();
378
379 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
380 E = Protocols.end(); PI != E; ++PI) {
381 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000382 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.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000389 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000390 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000391 }
392
393 // 2nd, look up the category.
394 if (lookupCategory)
395 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
396 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000397 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
398 E = CDecl->protocol_end(); PI != E; ++PI)
399 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000400 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000401 }
402
403 // 3rd, look up the super class(s)
404 if (IDecl->getSuperClass())
405 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000406 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
407 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000408
409 return false;
410}
411
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000412/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
413/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000414bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
415 bool compare) {
416 // Allow id<P..> and an 'id' or void* type in all cases.
417 if (const PointerType *PT = lhs->getAsPointerType()) {
418 QualType PointeeTy = PT->getPointeeType();
419 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
420 return true;
421 } else if (const PointerType *PT = rhs->getAsPointerType()) {
422 QualType PointeeTy = PT->getPointeeType();
423 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
424 return true;
425 }
426
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000427 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
428 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
429 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000430 QualType rtype;
431
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000432 if (!rhsQID) {
433 // Not comparing two ObjCQualifiedIdType's?
434 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000435
436 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000437 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000438 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000439 // If the RHS is a unqualified interface pointer "NSString*",
440 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000441 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
442 ObjCInterfaceDecl *rhsID = IT->getDecl();
443 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
444 // when comparing an id<P> on lhs with a static type on rhs,
445 // see if static class implements all of id's protocols, directly or
446 // through its super class and categories.
447 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
448 return false;
449 }
450 return true;
451 }
452 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000453 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000454
455 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000456 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000457 RHSProtoI = rhsQI->qual_begin();
458 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000459 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000460 RHSProtoI = rhsQID->qual_begin();
461 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000462 } else {
463 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000464 }
465
466 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
467 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
468 bool match = false;
469
470 // when comparing an id<P> on lhs with a static type on rhs,
471 // see if static class implements all of id's protocols, directly or
472 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000473 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
474 ObjCProtocolDecl *rhsProto = *RHSProtoI;
475 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
476 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000477 match = true;
478 break;
479 }
480 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000481 if (rhsQI) {
482 // If the RHS is a qualified interface pointer "NSString<P>*",
483 // make sure we check the class hierarchy.
484 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
485 ObjCInterfaceDecl *rhsID = IT->getDecl();
486 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
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.
490 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
491 match = true;
492 break;
493 }
494 }
495 }
496 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000497 if (!match)
498 return false;
499 }
500
501 return true;
502 }
503
504 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
505 assert(rhsQID && "One of the LHS/RHS should be id<x>");
506
507 if (!lhs->isPointerType())
508 return false;
509
510 QualType ltype = lhs->getAsPointerType()->getPointeeType();
511 if (const ObjCQualifiedInterfaceType *lhsQI =
512 ltype->getAsObjCQualifiedInterfaceType()) {
513 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
514 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
515 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
516 bool match = false;
517 ObjCProtocolDecl *lhsProto = *LHSProtoI;
518 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
519 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
520 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnereca7be62008-04-07 05:30:13 +0000521 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000522 match = true;
523 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000524 }
525 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000526 if (!match)
527 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000528 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000529 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000530 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000531
532 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
533 // for static type vs. qualified 'id' type, check that class implements
534 // all of 'id's protocols.
535 ObjCInterfaceDecl *lhsID = IT->getDecl();
536 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
537 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000538 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000539 return false;
540 }
541 return true;
542 }
543 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000544}
545