blob: ce9fe8971932daff3dffc201efa74f7a67c6b87c [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 Lattner39c28bb2009-02-18 06:48:40 +000018#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000019#include "clang/Lex/Preprocessor.h"
20
Chris Lattner85a932e2008-01-04 22:32:30 +000021using namespace clang;
22
23Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner39c28bb2009-02-18 06:48:40 +000024 ExprTy **strings,
Chris Lattner85a932e2008-01-04 22:32:30 +000025 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000026 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
27
Chris Lattnerf4b136f2009-02-18 06:13:04 +000028 // Most ObjC strings are formed out of a single piece. However, we *can*
29 // have strings formed out of multiple @ strings with multiple pptokens in
30 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
31 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000032 StringLiteral *S = Strings[0];
Chris Lattnerf4b136f2009-02-18 06:13:04 +000033
34 // If we have a multi-part string, merge it all together.
35 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000036 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000037 llvm::SmallString<128> StrBuf;
38 llvm::SmallVector<SourceLocation, 8> StrLocs;
Chris Lattner726e1682009-02-18 05:49:11 +000039
Chris Lattner726e1682009-02-18 05:49:11 +000040 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000041 S = Strings[i];
42
43 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000044 if (S->isWide()) {
45 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
46 << S->getSourceRange();
47 return true;
48 }
49
Chris Lattner39c28bb2009-02-18 06:48:40 +000050 // Get the string data.
51 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
52
53 // Get the locations of the string tokens.
54 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
55
56 // Free the temporary string.
Ted Kremenek8189cde2009-02-07 01:47:29 +000057 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000058 }
Chris Lattner39c28bb2009-02-18 06:48:40 +000059
60 // Create the aggregate string with the appropriate content and location
61 // information.
62 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000063 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000064 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000065 }
66
Chris Lattner69039812009-02-18 06:01:06 +000067 // Verify that this composite string is acceptable for ObjC strings.
68 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000069 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000070
71 // Initialize the constant string interface lazily. This assumes
72 // the NSConstantString interface is seen in this translation unit.
73 QualType Ty = Context.getObjCConstantStringInterface();
74 if (!Ty.isNull()) {
75 Ty = Context.getPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000076 } else {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000077 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
78 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
79 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
80 Context.setObjCConstantStringInterface(StrIF);
81 Ty = Context.getObjCConstantStringInterface();
82 Ty = Context.getPointerType(Ty);
83 } else {
84 // If there is no NSConstantString interface defined then treat constant
85 // strings as untyped objects and let the runtime figure it out later.
86 Ty = Context.getObjCIdType();
87 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000088 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +000089
Chris Lattnerf4b136f2009-02-18 06:13:04 +000090 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000091}
92
93Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
94 SourceLocation EncodeLoc,
95 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +000096 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +000097 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000098 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +000099
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000100 std::string Str;
101 Context.getObjCEncodingForType(EncodedType, Str);
102
103 // The type of @encode is the same as the type of the corresponding string,
104 // which is an array type.
105 QualType StrTy = Context.CharTy;
106 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
107 if (getLangOptions().CPlusPlus)
108 StrTy.addConst();
109 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
110 ArrayType::Normal, 0);
111
112 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000113}
114
115Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
116 SourceLocation AtLoc,
117 SourceLocation SelLoc,
118 SourceLocation LParenLoc,
119 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000120 QualType Ty = Context.getObjCSelType();
121 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000122}
123
124Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
125 SourceLocation AtLoc,
126 SourceLocation ProtoLoc,
127 SourceLocation LParenLoc,
128 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000130 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000131 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000132 return true;
133 }
134
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000135 QualType Ty = Context.getObjCProtoType();
136 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000137 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000138 Ty = Context.getPointerType(Ty);
139 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000140}
141
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000142bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
143 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000144 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000145 SourceLocation lbrac, SourceLocation rbrac,
146 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000147 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000148 // Apply default argument promotion as for (C99 6.5.2.2p6).
149 for (unsigned i = 0; i != NumArgs; i++)
150 DefaultArgumentPromotion(Args[i]);
151
Chris Lattner077bf5e2008-11-24 03:33:13 +0000152 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
153 diag::warn_inst_method_not_found;
154 Diag(lbrac, DiagID)
155 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000156 ReturnType = Context.getObjCIdType();
157 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000158 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000159
160 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000161
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000162 unsigned NumNamedArgs = Sel.getNumArgs();
163 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
164
Chris Lattner85a932e2008-01-04 22:32:30 +0000165 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000166 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000167 Expr *argExpr = Args[i];
168 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
169
Chris Lattner89951a82009-02-20 18:43:26 +0000170 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000171 QualType rhsType = argExpr->getType();
172
173 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000174 if (lhsType->isArrayType())
175 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000176 else if (lhsType->isFunctionType())
177 lhsType = Context.getPointerType(lhsType);
178
Chris Lattner987798a2008-04-02 17:17:33 +0000179 AssignConvertType Result =
180 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000181 if (Args[i] != argExpr) // The expression was converted.
182 Args[i] = argExpr; // Make sure we store the converted expression.
183
184 anyIncompatibleArgs |=
185 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
186 argExpr, "sending");
187 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000188
189 // Promote additional arguments to variadic methods.
190 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000191 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
192 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000193 } else {
194 // Check for extra arguments to non-variadic methods.
195 if (NumArgs != NumNamedArgs) {
196 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000197 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000198 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000199 << SourceRange(Args[NumNamedArgs]->getLocStart(),
200 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000201 }
202 }
203
Chris Lattner85a932e2008-01-04 22:32:30 +0000204 return anyIncompatibleArgs;
205}
206
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000207bool Sema::isSelfExpr(Expr *RExpr) {
208 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
209 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
210 return true;
211 return false;
212}
213
Steve Narofff1afaf62009-02-26 15:55:06 +0000214// Helper method for ActOnClassMethod/ActOnInstanceMethod.
215// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000216// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000217// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000218ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000219 ObjCInterfaceDecl *ClassDecl) {
220 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000221 // lookup in class and all superclasses
222 while (ClassDecl && !Method) {
223 if (ObjCImplementationDecl *ImpDecl =
224 ObjCImplementations[ClassDecl->getIdentifier()])
225 Method = ImpDecl->getClassMethod(Sel);
Steve Narofff1afaf62009-02-26 15:55:06 +0000226
Steve Naroff5609ec02009-03-08 18:56:13 +0000227 // Look through local category implementations associated with the class.
228 if (!Method) {
229 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
230 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
231 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
232 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000233 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000234
235 // Before we give up, check if the selector is an instance method.
236 // But only in the root. This matches gcc's behaviour and what the
237 // runtime expects.
238 if (!Method && !ClassDecl->getSuperClass()) {
239 Method = ClassDecl->lookupInstanceMethod(Sel);
240 // Look through local category implementations associated
241 // with the root class.
242 if (!Method)
243 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
244 }
245
246 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000247 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000248 return Method;
249}
250
251ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
252 ObjCInterfaceDecl *ClassDecl) {
253 ObjCMethodDecl *Method = 0;
254 while (ClassDecl && !Method) {
255 // If we have implementations in scope, check "private" methods.
256 if (ObjCImplementationDecl *ImpDecl =
257 ObjCImplementations[ClassDecl->getIdentifier()])
258 Method = ImpDecl->getInstanceMethod(Sel);
259
260 // Look through local category implementations associated with the class.
261 if (!Method) {
262 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
263 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
264 Method = ObjCCategoryImpls[i]->getInstanceMethod(Sel);
265 }
266 }
267 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000268 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000269 return Method;
270}
271
Steve Naroff61f72cb2009-03-09 21:12:44 +0000272Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
273 IdentifierInfo &receiverName,
274 IdentifierInfo &propertyName,
275 SourceLocation &receiverNameLoc,
276 SourceLocation &propertyNameLoc) {
277
278 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
279
280 // Search for a declared property first.
281
282 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
283 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
284
285 // If this reference is in an @implementation, check for 'private' methods.
286 if (!Getter)
287 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
288 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
289 if (ObjCImplementationDecl *ImpDecl =
290 ObjCImplementations[ClassDecl->getIdentifier()])
291 Getter = ImpDecl->getClassMethod(Sel);
292
293 if (Getter) {
294 // FIXME: refactor/share with ActOnMemberReference().
295 // Check if we can reference this property.
296 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
297 return ExprError();
298 }
299
300 // Look for the matching setter, in case it is needed.
Steve Narofffdc92b72009-03-10 17:24:38 +0000301 Selector SetterSel =
302 SelectorTable::constructSetterName(PP.getIdentifierTable(),
303 PP.getSelectorTable(), &propertyName);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000304
Steve Naroff61f72cb2009-03-09 21:12:44 +0000305 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
306 if (!Setter) {
307 // If this reference is in an @implementation, also check for 'private'
308 // methods.
309 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
310 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
311 if (ObjCImplementationDecl *ImpDecl =
312 ObjCImplementations[ClassDecl->getIdentifier()])
313 Setter = ImpDecl->getClassMethod(SetterSel);
314 }
315 // Look through local category implementations associated with the class.
316 if (!Setter) {
317 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) {
318 if (ObjCCategoryImpls[i]->getClassInterface() == IFace)
319 Setter = ObjCCategoryImpls[i]->getClassMethod(SetterSel);
320 }
321 }
322
323 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
324 return ExprError();
325
326 if (Getter || Setter) {
327 QualType PType;
328
329 if (Getter)
330 PType = Getter->getResultType();
331 else {
332 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
333 E = Setter->param_end(); PI != E; ++PI)
334 PType = (*PI)->getType();
335 }
336 return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter,
337 propertyNameLoc, IFace, receiverNameLoc));
338 }
339 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
340 << &propertyName << Context.getObjCInterfaceType(IFace));
341}
342
343
Chris Lattner85a932e2008-01-04 22:32:30 +0000344// ActOnClassMessage - used for both unary and keyword messages.
345// ArgExprs is optional - if it is present, the number of expressions
346// is obtained from Sel.getNumArgs().
347Sema::ExprResult Sema::ActOnClassMessage(
348 Scope *S,
349 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000350 SourceLocation lbrac, SourceLocation receiverLoc,
351 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000352 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000353{
354 assert(receiverName && "missing receiver class name");
355
356 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000357 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000358 bool isSuper = false;
359
Chris Lattner84692652008-11-20 05:35:30 +0000360 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000361 if (getCurMethodDecl()) {
362 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000363 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
364 if (!OID)
365 return Diag(lbrac, diag::error_no_super_class_message)
366 << getCurMethodDecl()->getDeclName();
367 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000368 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000369 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000370 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000371 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
372 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000373 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
374 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000375 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000376 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
377 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000378 }
379 // We are sending a message to 'super' within a class method. Do nothing,
380 // the receiver will pass through as 'super' (how convenient:-).
381 } else {
382 // 'super' has been used outside a method context. If a variable named
383 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000384 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000385 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
386 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000387 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
388 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000389 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000390 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
391 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000392 }
Chris Lattner08631c52008-11-23 21:45:46 +0000393 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000394 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000395 } else
396 ClassDecl = getObjCInterfaceDecl(receiverName);
397
Steve Naroff7c778f12008-07-25 19:39:00 +0000398 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000399 //
400 // typedef XCElementDisplayRect XCElementGraphicsRect;
401 //
402 // @implementation XCRASlice
403 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
404 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
405 // }
406 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000407 // If necessary, the following lookup could move to getObjCInterfaceDecl().
408 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000409 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000410 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
411 const ObjCInterfaceType *OCIT;
412 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000413 if (!OCIT)
414 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
415 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000416 }
417 }
418 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000419 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000420 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000421 Method = ClassDecl->lookupClassMethod(Sel);
422
423 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000424 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000425 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000426
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000427 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
428 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000429
Chris Lattner077bf5e2008-11-24 03:33:13 +0000430 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000431 lbrac, rbrac, returnType))
432 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000433
434 // If we have the ObjCInterfaceDecl* for the class that is receiving
435 // the message, use that to construct the ObjCMessageExpr. Otherwise
436 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000437 // FIXME: need to do a better job handling 'super' usage within a class
438 // For now, we simply pass the "super" identifier through (which isn't
439 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000440 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000441 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
442 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000443 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000444 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
445 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000446}
447
448// ActOnInstanceMessage - used for both unary and keyword messages.
449// ArgExprs is optional - if it is present, the number of expressions
450// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000451Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000452 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000453 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000454 SourceLocation rbrac,
455 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000456 assert(receiver && "missing receiver expression");
457
458 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
459 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000460 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000461
Chris Lattnerb77792e2008-07-26 22:17:49 +0000462 QualType ReceiverCType =
463 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000464
465 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000466 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000467 ObjCMethodDecl *Method = 0;
468 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
469 // If we have an interface in scope, check 'super' methods.
470 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000471 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000472 Method = SuperDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000473
474 if (!Method)
475 // If we have implementations in scope, check "private" methods.
476 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
477 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000478 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000479
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000480 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
481 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000482
Chris Lattner077bf5e2008-11-24 03:33:13 +0000483 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000484 lbrac, rbrac, returnType))
485 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000486 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
487 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000488 }
489
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000490 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000491 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Chris Lattner0c73f372009-03-09 21:19:16 +0000492 ReceiverCType->isBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000493 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
494 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000495 if (!Method)
496 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000497 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000498 lbrac, rbrac, returnType))
499 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000500 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
501 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000502 }
503
504 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000505 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000506 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000507
Chris Lattner6562fda2008-07-21 06:44:27 +0000508 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000509 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
510 // First check the public methods in the class interface.
511 Method = ClassDecl->lookupClassMethod(Sel);
512
Steve Narofff1afaf62009-02-26 15:55:06 +0000513 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000514 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000515 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000516 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
517 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000518 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000519 if (!Method) {
520 // If not messaging 'self', look for any factory method named 'Sel'.
521 if (!isSelfExpr(RExpr)) {
522 Method = FactoryMethodPool[Sel].Method;
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000523 if (!Method) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000524 Method = LookupInstanceMethodInGlobalPool(
525 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000526 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000527 }
528 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000529 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000530 lbrac, rbrac, returnType))
531 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000532 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
533 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000534 }
535
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000536 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000537 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000538
539 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
540 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000541 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000542 // Search protocols for instance methods.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000543 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
544 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
545 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
546 break;
547 }
Steve Naroff279d8962009-02-23 15:40:48 +0000548 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000549 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000550 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000551
Steve Naroff279d8962009-02-23 15:40:48 +0000552 ClassDecl = OCIType->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000553 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
554 // faster than the following method (which can do *many* linear searches).
555 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000556 Method = ClassDecl->lookupInstanceMethod(Sel);
557
558 if (!Method) {
559 // Search protocol qualifiers.
Steve Naroff279d8962009-02-23 15:40:48 +0000560 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
561 E = OCIType->qual_end(); QI != E; ++QI) {
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000562 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000563 break;
564 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000565 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000566 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000567 // If we have implementations in scope, check "private" methods.
568 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
569
570 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000571 // If we still haven't found a method, look in the global pool. This
572 // behavior isn't very desirable, however we need it for GCC
573 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000574 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000575 Method = LookupInstanceMethodInGlobalPool(
576 Sel, SourceRange(lbrac,rbrac));
577 if (Method && !OCIType->getDecl()->isForwardDecl())
578 Diag(lbrac, diag::warn_maynot_respond)
579 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
580 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000581 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000582 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000583 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
584 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000585 } else if (!Context.getObjCIdType().isNull() &&
586 (ReceiverCType->isPointerType() ||
587 (ReceiverCType->isIntegerType() &&
588 ReceiverCType->isScalarType()))) {
589 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000590 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000591 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000592 ImpCastExprToType(RExpr, Context.getObjCIdType());
593 } else {
594 // Reject other random receiver types (e.g. structs).
595 Diag(lbrac, diag::err_bad_receiver_type)
596 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000597 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000598 }
599
Chris Lattner077bf5e2008-11-24 03:33:13 +0000600 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000601 lbrac, rbrac, returnType))
602 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000603 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
604 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000605}
Chris Lattnereca7be62008-04-07 05:30:13 +0000606
607//===----------------------------------------------------------------------===//
608// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
609//===----------------------------------------------------------------------===//
610
611/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
612/// inheritance hierarchy of 'rProto'.
613static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
614 ObjCProtocolDecl *rProto) {
615 if (lProto == rProto)
616 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000617 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
618 E = rProto->protocol_end(); PI != E; ++PI)
619 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000620 return true;
621 return false;
622}
623
624/// ClassImplementsProtocol - Checks that 'lProto' protocol
625/// has been implemented in IDecl class, its super class or categories (if
626/// lookupCategory is true).
627static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
628 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000629 bool lookupCategory,
630 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000631
632 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000633 const ObjCList<ObjCProtocolDecl> &Protocols =
634 IDecl->getReferencedProtocols();
635
636 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
637 E = Protocols.end(); PI != E; ++PI) {
638 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000639 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000640 // This is dubious and is added to be compatible with gcc.
641 // In gcc, it is also allowed assigning a protocol-qualified 'id'
642 // type to a LHS object when protocol in qualified LHS is in list
643 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000644 // FIXME: Treat this as an extension, and flag this as an error when
645 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000646 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000647 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000648 }
649
650 // 2nd, look up the category.
651 if (lookupCategory)
652 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
653 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000654 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
655 E = CDecl->protocol_end(); PI != E; ++PI)
656 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000657 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000658 }
659
660 // 3rd, look up the super class(s)
661 if (IDecl->getSuperClass())
662 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000663 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
664 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000665
666 return false;
667}
668
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000669/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
670/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000671/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000672bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
673 bool compare) {
674 // Allow id<P..> and an 'id' or void* type in all cases.
675 if (const PointerType *PT = lhs->getAsPointerType()) {
676 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000677 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000678 return true;
679 } else if (const PointerType *PT = rhs->getAsPointerType()) {
680 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000681 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000682 return true;
683 }
684
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000685 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
686 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
687 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000688 QualType rtype;
689
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000690 if (!rhsQID) {
691 // Not comparing two ObjCQualifiedIdType's?
692 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000693
694 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000695 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000696 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000697 // If the RHS is a unqualified interface pointer "NSString*",
698 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000699 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
700 ObjCInterfaceDecl *rhsID = IT->getDecl();
701 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
702 // when comparing an id<P> on lhs with a static type on rhs,
703 // see if static class implements all of id's protocols, directly or
704 // through its super class and categories.
705 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
706 return false;
707 }
708 return true;
709 }
710 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000711 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000712
713 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000714 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000715 RHSProtoI = rhsQI->qual_begin();
716 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000717 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000718 RHSProtoI = rhsQID->qual_begin();
719 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000720 } else {
721 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000722 }
723
724 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
725 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
726 bool match = false;
727
728 // when comparing an id<P> on lhs with a static type on rhs,
729 // see if static class implements all of id's protocols, directly or
730 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000731 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
732 ObjCProtocolDecl *rhsProto = *RHSProtoI;
733 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000734 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000735 match = true;
736 break;
737 }
738 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000739 if (rhsQI) {
740 // If the RHS is a qualified interface pointer "NSString<P>*",
741 // make sure we check the class hierarchy.
742 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
743 ObjCInterfaceDecl *rhsID = IT->getDecl();
744 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
745 // when comparing an id<P> on lhs with a static type on rhs,
746 // see if static class implements all of id's protocols, directly or
747 // through its super class and categories.
748 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
749 match = true;
750 break;
751 }
752 }
753 }
754 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000755 if (!match)
756 return false;
757 }
758
759 return true;
760 }
761
762 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
763 assert(rhsQID && "One of the LHS/RHS should be id<x>");
764
765 if (!lhs->isPointerType())
766 return false;
767
768 QualType ltype = lhs->getAsPointerType()->getPointeeType();
769 if (const ObjCQualifiedInterfaceType *lhsQI =
770 ltype->getAsObjCQualifiedInterfaceType()) {
771 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
772 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
773 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
774 bool match = false;
775 ObjCProtocolDecl *lhsProto = *LHSProtoI;
776 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
777 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
778 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000779 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000780 match = true;
781 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000782 }
783 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000784 if (!match)
785 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000786 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000787 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000788 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000789
790 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
791 // for static type vs. qualified 'id' type, check that class implements
792 // all of 'id's protocols.
793 ObjCInterfaceDecl *lhsID = IT->getDecl();
794 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
795 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000796 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000797 return false;
798 }
799 return true;
800 }
801 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000802}
803