blob: bd94f007bad4d8706be69f8aa43c8dd0b235f64f [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();
Chris Lattner64540d72009-03-29 05:01:10 +0000413 if (!OCIT) {
414 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
415 return true;
416 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000417 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000418 }
419 }
420 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000421 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000422 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000423 Method = ClassDecl->lookupClassMethod(Sel);
424
425 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000426 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000427 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000428
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000429 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
430 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000431
Chris Lattner077bf5e2008-11-24 03:33:13 +0000432 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000433 lbrac, rbrac, returnType))
434 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000435
436 // If we have the ObjCInterfaceDecl* for the class that is receiving
437 // the message, use that to construct the ObjCMessageExpr. Otherwise
438 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000439 // FIXME: need to do a better job handling 'super' usage within a class
440 // For now, we simply pass the "super" identifier through (which isn't
441 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000442 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000443 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
444 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000445 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000446 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
447 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000448}
449
450// ActOnInstanceMessage - used for both unary and keyword messages.
451// ArgExprs is optional - if it is present, the number of expressions
452// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000453Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000454 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000455 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000456 SourceLocation rbrac,
457 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000458 assert(receiver && "missing receiver expression");
459
460 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
461 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000462 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000463
Chris Lattnerb77792e2008-07-26 22:17:49 +0000464 QualType ReceiverCType =
465 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000466
467 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000468 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000469 ObjCMethodDecl *Method = 0;
470 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
471 // If we have an interface in scope, check 'super' methods.
472 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000473 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000474 Method = SuperDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000475
476 if (!Method)
477 // If we have implementations in scope, check "private" methods.
478 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
479 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000480 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000481
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000482 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
483 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000484
Chris Lattner077bf5e2008-11-24 03:33:13 +0000485 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000486 lbrac, rbrac, returnType))
487 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000488 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
489 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000490 }
491
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000492 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000493 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Chris Lattner0c73f372009-03-09 21:19:16 +0000494 ReceiverCType->isBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000495 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
496 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000497 if (!Method)
498 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000499 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000500 lbrac, rbrac, returnType))
501 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000502 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
503 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000504 }
505
506 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000507 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000508 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000509
Chris Lattner6562fda2008-07-21 06:44:27 +0000510 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000511 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
512 // First check the public methods in the class interface.
513 Method = ClassDecl->lookupClassMethod(Sel);
514
Steve Narofff1afaf62009-02-26 15:55:06 +0000515 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000516 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000517 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000518 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
519 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000520 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000521 if (!Method) {
522 // If not messaging 'self', look for any factory method named 'Sel'.
523 if (!isSelfExpr(RExpr)) {
524 Method = FactoryMethodPool[Sel].Method;
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000525 if (!Method) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000526 Method = LookupInstanceMethodInGlobalPool(
527 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000528 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000529 }
530 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000531 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000532 lbrac, rbrac, returnType))
533 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000534 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
535 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000536 }
537
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000538 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000539 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000540
541 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
542 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000543 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000544 // Search protocols for instance methods.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000545 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
546 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
547 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
548 break;
549 }
Steve Naroff279d8962009-02-23 15:40:48 +0000550 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000551 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000552 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000553
Steve Naroff279d8962009-02-23 15:40:48 +0000554 ClassDecl = OCIType->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000555 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
556 // faster than the following method (which can do *many* linear searches).
557 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000558 Method = ClassDecl->lookupInstanceMethod(Sel);
559
560 if (!Method) {
561 // Search protocol qualifiers.
Steve Naroff279d8962009-02-23 15:40:48 +0000562 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
563 E = OCIType->qual_end(); QI != E; ++QI) {
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000564 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000565 break;
566 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000567 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000568 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000569 // If we have implementations in scope, check "private" methods.
570 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
571
572 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000573 // If we still haven't found a method, look in the global pool. This
574 // behavior isn't very desirable, however we need it for GCC
575 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000576 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000577 Method = LookupInstanceMethodInGlobalPool(
578 Sel, SourceRange(lbrac,rbrac));
579 if (Method && !OCIType->getDecl()->isForwardDecl())
580 Diag(lbrac, diag::warn_maynot_respond)
581 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
582 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000583 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000584 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000585 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
586 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000587 } else if (!Context.getObjCIdType().isNull() &&
588 (ReceiverCType->isPointerType() ||
589 (ReceiverCType->isIntegerType() &&
590 ReceiverCType->isScalarType()))) {
591 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000592 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000593 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000594 ImpCastExprToType(RExpr, Context.getObjCIdType());
595 } else {
596 // Reject other random receiver types (e.g. structs).
597 Diag(lbrac, diag::err_bad_receiver_type)
598 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000599 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000600 }
601
Chris Lattner077bf5e2008-11-24 03:33:13 +0000602 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000603 lbrac, rbrac, returnType))
604 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000605 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
606 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000607}
Chris Lattnereca7be62008-04-07 05:30:13 +0000608
609//===----------------------------------------------------------------------===//
610// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
611//===----------------------------------------------------------------------===//
612
613/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
614/// inheritance hierarchy of 'rProto'.
615static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
616 ObjCProtocolDecl *rProto) {
617 if (lProto == rProto)
618 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000619 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
620 E = rProto->protocol_end(); PI != E; ++PI)
621 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000622 return true;
623 return false;
624}
625
626/// ClassImplementsProtocol - Checks that 'lProto' protocol
627/// has been implemented in IDecl class, its super class or categories (if
628/// lookupCategory is true).
629static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
630 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000631 bool lookupCategory,
632 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000633
634 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000635 const ObjCList<ObjCProtocolDecl> &Protocols =
636 IDecl->getReferencedProtocols();
637
638 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
639 E = Protocols.end(); PI != E; ++PI) {
640 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000641 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000642 // This is dubious and is added to be compatible with gcc.
643 // In gcc, it is also allowed assigning a protocol-qualified 'id'
644 // type to a LHS object when protocol in qualified LHS is in list
645 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000646 // FIXME: Treat this as an extension, and flag this as an error when
647 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000648 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000649 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000650 }
651
652 // 2nd, look up the category.
653 if (lookupCategory)
654 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
655 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000656 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
657 E = CDecl->protocol_end(); PI != E; ++PI)
658 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000659 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000660 }
661
662 // 3rd, look up the super class(s)
663 if (IDecl->getSuperClass())
664 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000665 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
666 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000667
668 return false;
669}
670
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000671/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
672/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000673/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000674bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
675 bool compare) {
676 // Allow id<P..> and an 'id' or void* type in all cases.
677 if (const PointerType *PT = lhs->getAsPointerType()) {
678 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000679 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000680 return true;
681 } else if (const PointerType *PT = rhs->getAsPointerType()) {
682 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000683 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000684 return true;
685 }
686
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000687 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
688 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
689 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000690 QualType rtype;
691
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000692 if (!rhsQID) {
693 // Not comparing two ObjCQualifiedIdType's?
694 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000695
696 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000697 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000698 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000699 // If the RHS is a unqualified interface pointer "NSString*",
700 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000701 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
702 ObjCInterfaceDecl *rhsID = IT->getDecl();
703 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
704 // when comparing an id<P> on lhs with a static type on rhs,
705 // see if static class implements all of id's protocols, directly or
706 // through its super class and categories.
707 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
708 return false;
709 }
710 return true;
711 }
712 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000713 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000714
715 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000716 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000717 RHSProtoI = rhsQI->qual_begin();
718 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000719 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000720 RHSProtoI = rhsQID->qual_begin();
721 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000722 } else {
723 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000724 }
725
726 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
727 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
728 bool match = false;
729
730 // when comparing an id<P> on lhs with a static type on rhs,
731 // see if static class implements all of id's protocols, directly or
732 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000733 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
734 ObjCProtocolDecl *rhsProto = *RHSProtoI;
735 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000736 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000737 match = true;
738 break;
739 }
740 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000741 if (rhsQI) {
742 // If the RHS is a qualified interface pointer "NSString<P>*",
743 // make sure we check the class hierarchy.
744 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
745 ObjCInterfaceDecl *rhsID = IT->getDecl();
746 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
747 // when comparing an id<P> on lhs with a static type on rhs,
748 // see if static class implements all of id's protocols, directly or
749 // through its super class and categories.
750 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
751 match = true;
752 break;
753 }
754 }
755 }
756 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000757 if (!match)
758 return false;
759 }
760
761 return true;
762 }
763
764 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
765 assert(rhsQID && "One of the LHS/RHS should be id<x>");
766
767 if (!lhs->isPointerType())
768 return false;
769
770 QualType ltype = lhs->getAsPointerType()->getPointeeType();
771 if (const ObjCQualifiedInterfaceType *lhsQI =
772 ltype->getAsObjCQualifiedInterfaceType()) {
773 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
774 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
775 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
776 bool match = false;
777 ObjCProtocolDecl *lhsProto = *LHSProtoI;
778 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
779 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
780 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000781 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000782 match = true;
783 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000784 }
785 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000786 if (!match)
787 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000788 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000789 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000790 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000791
792 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
793 // for static type vs. qualified 'id' type, check that class implements
794 // all of 'id's protocols.
795 ObjCInterfaceDecl *lhsID = IT->getDecl();
796 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
797 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000798 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000799 return false;
800 }
801 return true;
802 }
803 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000804}
805