blob: cfa279aa3111b5dd36e6daba661d1691707aa03e [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
Steve Naroffd9fd7642009-04-07 14:18:33 +000072 // the NSString interface is seen in this translation unit. Note: We
73 // don't use NSConstantString, since the runtime team considers this
74 // interface private (even though it appears in the header files).
Chris Lattnera0af1fe2009-02-18 06:06:56 +000075 QualType Ty = Context.getObjCConstantStringInterface();
76 if (!Ty.isNull()) {
77 Ty = Context.getPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000078 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000079 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Chris Lattnera0af1fe2009-02-18 06:06:56 +000080 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
81 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
82 Context.setObjCConstantStringInterface(StrIF);
83 Ty = Context.getObjCConstantStringInterface();
84 Ty = Context.getPointerType(Ty);
85 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000086 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +000087 // strings as untyped objects and let the runtime figure it out later.
88 Ty = Context.getObjCIdType();
89 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000090 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +000091
Chris Lattnerf4b136f2009-02-18 06:13:04 +000092 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000093}
94
95Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
96 SourceLocation EncodeLoc,
97 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +000098 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +000099 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000100 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +0000101
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000102 std::string Str;
103 Context.getObjCEncodingForType(EncodedType, Str);
104
105 // The type of @encode is the same as the type of the corresponding string,
106 // which is an array type.
107 QualType StrTy = Context.CharTy;
108 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
109 if (getLangOptions().CPlusPlus)
110 StrTy.addConst();
111 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
112 ArrayType::Normal, 0);
113
114 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000115}
116
117Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
118 SourceLocation AtLoc,
119 SourceLocation SelLoc,
120 SourceLocation LParenLoc,
121 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000122 QualType Ty = Context.getObjCSelType();
123 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000124}
125
126Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
127 SourceLocation AtLoc,
128 SourceLocation ProtoLoc,
129 SourceLocation LParenLoc,
130 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000131 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000132 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000133 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000134 return true;
135 }
136
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000137 QualType Ty = Context.getObjCProtoType();
138 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000139 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000140 Ty = Context.getPointerType(Ty);
141 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000142}
143
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000144bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
145 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000146 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000147 SourceLocation lbrac, SourceLocation rbrac,
148 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000149 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000150 // Apply default argument promotion as for (C99 6.5.2.2p6).
151 for (unsigned i = 0; i != NumArgs; i++)
152 DefaultArgumentPromotion(Args[i]);
153
Chris Lattner077bf5e2008-11-24 03:33:13 +0000154 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
155 diag::warn_inst_method_not_found;
156 Diag(lbrac, DiagID)
157 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000158 ReturnType = Context.getObjCIdType();
159 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000160 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000161
162 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000163
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000164 unsigned NumNamedArgs = Sel.getNumArgs();
165 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
166
Chris Lattner85a932e2008-01-04 22:32:30 +0000167 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000168 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000169 Expr *argExpr = Args[i];
170 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
171
Chris Lattner89951a82009-02-20 18:43:26 +0000172 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000173 QualType rhsType = argExpr->getType();
174
175 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000176 if (lhsType->isArrayType())
177 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000178 else if (lhsType->isFunctionType())
179 lhsType = Context.getPointerType(lhsType);
180
Chris Lattner987798a2008-04-02 17:17:33 +0000181 AssignConvertType Result =
182 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000183 if (Args[i] != argExpr) // The expression was converted.
184 Args[i] = argExpr; // Make sure we store the converted expression.
185
186 anyIncompatibleArgs |=
187 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
188 argExpr, "sending");
189 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000190
191 // Promote additional arguments to variadic methods.
192 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000193 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
194 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000195 } else {
196 // Check for extra arguments to non-variadic methods.
197 if (NumArgs != NumNamedArgs) {
198 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000199 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000200 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000201 << SourceRange(Args[NumNamedArgs]->getLocStart(),
202 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000203 }
204 }
205
Chris Lattner85a932e2008-01-04 22:32:30 +0000206 return anyIncompatibleArgs;
207}
208
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000209bool Sema::isSelfExpr(Expr *RExpr) {
210 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
211 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
212 return true;
213 return false;
214}
215
Steve Narofff1afaf62009-02-26 15:55:06 +0000216// Helper method for ActOnClassMethod/ActOnInstanceMethod.
217// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000218// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000219// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000220ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000221 ObjCInterfaceDecl *ClassDecl) {
222 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000223 // lookup in class and all superclasses
224 while (ClassDecl && !Method) {
225 if (ObjCImplementationDecl *ImpDecl =
226 ObjCImplementations[ClassDecl->getIdentifier()])
227 Method = ImpDecl->getClassMethod(Sel);
Steve Narofff1afaf62009-02-26 15:55:06 +0000228
Steve Naroff5609ec02009-03-08 18:56:13 +0000229 // Look through local category implementations associated with the class.
230 if (!Method) {
231 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
232 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
233 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
234 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000235 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000236
237 // Before we give up, check if the selector is an instance method.
238 // But only in the root. This matches gcc's behaviour and what the
239 // runtime expects.
240 if (!Method && !ClassDecl->getSuperClass()) {
241 Method = ClassDecl->lookupInstanceMethod(Sel);
242 // Look through local category implementations associated
243 // with the root class.
244 if (!Method)
245 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
246 }
247
248 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000249 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000250 return Method;
251}
252
253ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
254 ObjCInterfaceDecl *ClassDecl) {
255 ObjCMethodDecl *Method = 0;
256 while (ClassDecl && !Method) {
257 // If we have implementations in scope, check "private" methods.
258 if (ObjCImplementationDecl *ImpDecl =
259 ObjCImplementations[ClassDecl->getIdentifier()])
260 Method = ImpDecl->getInstanceMethod(Sel);
261
262 // Look through local category implementations associated with the class.
263 if (!Method) {
264 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
265 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
266 Method = ObjCCategoryImpls[i]->getInstanceMethod(Sel);
267 }
268 }
269 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000270 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000271 return Method;
272}
273
Steve Naroff61f72cb2009-03-09 21:12:44 +0000274Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
275 IdentifierInfo &receiverName,
276 IdentifierInfo &propertyName,
277 SourceLocation &receiverNameLoc,
278 SourceLocation &propertyNameLoc) {
279
280 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
281
282 // Search for a declared property first.
283
284 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
285 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
286
287 // If this reference is in an @implementation, check for 'private' methods.
288 if (!Getter)
289 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
290 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
291 if (ObjCImplementationDecl *ImpDecl =
292 ObjCImplementations[ClassDecl->getIdentifier()])
293 Getter = ImpDecl->getClassMethod(Sel);
294
295 if (Getter) {
296 // FIXME: refactor/share with ActOnMemberReference().
297 // Check if we can reference this property.
298 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
299 return ExprError();
300 }
301
302 // Look for the matching setter, in case it is needed.
Steve Narofffdc92b72009-03-10 17:24:38 +0000303 Selector SetterSel =
304 SelectorTable::constructSetterName(PP.getIdentifierTable(),
305 PP.getSelectorTable(), &propertyName);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000306
Steve Naroff61f72cb2009-03-09 21:12:44 +0000307 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
308 if (!Setter) {
309 // If this reference is in an @implementation, also check for 'private'
310 // methods.
311 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
312 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
313 if (ObjCImplementationDecl *ImpDecl =
314 ObjCImplementations[ClassDecl->getIdentifier()])
315 Setter = ImpDecl->getClassMethod(SetterSel);
316 }
317 // Look through local category implementations associated with the class.
318 if (!Setter) {
319 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) {
320 if (ObjCCategoryImpls[i]->getClassInterface() == IFace)
321 Setter = ObjCCategoryImpls[i]->getClassMethod(SetterSel);
322 }
323 }
324
325 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
326 return ExprError();
327
328 if (Getter || Setter) {
329 QualType PType;
330
331 if (Getter)
332 PType = Getter->getResultType();
333 else {
334 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
335 E = Setter->param_end(); PI != E; ++PI)
336 PType = (*PI)->getType();
337 }
338 return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter,
339 propertyNameLoc, IFace, receiverNameLoc));
340 }
341 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
342 << &propertyName << Context.getObjCInterfaceType(IFace));
343}
344
345
Chris Lattner85a932e2008-01-04 22:32:30 +0000346// ActOnClassMessage - used for both unary and keyword messages.
347// ArgExprs is optional - if it is present, the number of expressions
348// is obtained from Sel.getNumArgs().
349Sema::ExprResult Sema::ActOnClassMessage(
350 Scope *S,
351 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000352 SourceLocation lbrac, SourceLocation receiverLoc,
353 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000354 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000355{
356 assert(receiverName && "missing receiver class name");
357
358 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000360 bool isSuper = false;
361
Chris Lattner84692652008-11-20 05:35:30 +0000362 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000363 if (getCurMethodDecl()) {
364 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000365 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
366 if (!OID)
367 return Diag(lbrac, diag::error_no_super_class_message)
368 << getCurMethodDecl()->getDeclName();
369 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000370 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000371 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000372 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000373 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
374 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000375 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
376 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000377 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000378 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
379 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000380 }
381 // We are sending a message to 'super' within a class method. Do nothing,
382 // the receiver will pass through as 'super' (how convenient:-).
383 } else {
384 // 'super' has been used outside a method context. If a variable named
385 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000386 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000387 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
388 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000389 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
390 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000391 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000392 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
393 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000394 }
Chris Lattner08631c52008-11-23 21:45:46 +0000395 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000396 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000397 } else
398 ClassDecl = getObjCInterfaceDecl(receiverName);
399
Steve Naroff7c778f12008-07-25 19:39:00 +0000400 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000401 //
402 // typedef XCElementDisplayRect XCElementGraphicsRect;
403 //
404 // @implementation XCRASlice
405 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
406 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
407 // }
408 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000409 // If necessary, the following lookup could move to getObjCInterfaceDecl().
410 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000411 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000412 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
413 const ObjCInterfaceType *OCIT;
414 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Chris Lattner64540d72009-03-29 05:01:10 +0000415 if (!OCIT) {
416 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
417 return true;
418 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000419 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000420 }
421 }
422 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000423 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000424 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000425 Method = ClassDecl->lookupClassMethod(Sel);
426
427 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000428 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000429 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000430
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000431 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
432 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000433
Chris Lattner077bf5e2008-11-24 03:33:13 +0000434 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000435 lbrac, rbrac, returnType))
436 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000437
438 // If we have the ObjCInterfaceDecl* for the class that is receiving
439 // the message, use that to construct the ObjCMessageExpr. Otherwise
440 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000441 // FIXME: need to do a better job handling 'super' usage within a class
442 // For now, we simply pass the "super" identifier through (which isn't
443 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000444 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000445 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
446 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000447 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000448 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
449 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000450}
451
452// ActOnInstanceMessage - used for both unary and keyword messages.
453// ArgExprs is optional - if it is present, the number of expressions
454// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000455Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000456 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000457 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000458 SourceLocation rbrac,
459 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000460 assert(receiver && "missing receiver expression");
461
462 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
463 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000464 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000465
Chris Lattnerb77792e2008-07-26 22:17:49 +0000466 QualType ReceiverCType =
467 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000468
469 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000470 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000471 ObjCMethodDecl *Method = 0;
472 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
473 // If we have an interface in scope, check 'super' methods.
474 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000475 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000476 Method = SuperDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000477
478 if (!Method)
479 // If we have implementations in scope, check "private" methods.
480 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
481 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000482 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000483
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000484 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
485 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000486
Chris Lattner077bf5e2008-11-24 03:33:13 +0000487 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000488 lbrac, rbrac, returnType))
489 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000490 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
491 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000492 }
493
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000494 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000495 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Chris Lattner0c73f372009-03-09 21:19:16 +0000496 ReceiverCType->isBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000497 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
498 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000499 if (!Method)
500 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000501 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000502 lbrac, rbrac, returnType))
503 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000504 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
505 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000506 }
507
508 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000509 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000510 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000511
Chris Lattner6562fda2008-07-21 06:44:27 +0000512 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000513 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
514 // First check the public methods in the class interface.
515 Method = ClassDecl->lookupClassMethod(Sel);
516
Steve Narofff1afaf62009-02-26 15:55:06 +0000517 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000518 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000519 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000520 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
521 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000522 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000523 if (!Method) {
524 // If not messaging 'self', look for any factory method named 'Sel'.
525 if (!isSelfExpr(RExpr)) {
526 Method = FactoryMethodPool[Sel].Method;
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000527 if (!Method) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000528 Method = LookupInstanceMethodInGlobalPool(
529 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000530 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000531 }
532 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000533 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000534 lbrac, rbrac, returnType))
535 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000536 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
537 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000538 }
539
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000540 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000541 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000542
543 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
544 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000545 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000546 // Search protocols for instance methods.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000547 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
548 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
549 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
550 break;
551 }
Steve Naroff279d8962009-02-23 15:40:48 +0000552 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000553 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000554 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000555
Steve Naroff279d8962009-02-23 15:40:48 +0000556 ClassDecl = OCIType->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000557 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
558 // faster than the following method (which can do *many* linear searches).
559 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000560 Method = ClassDecl->lookupInstanceMethod(Sel);
561
562 if (!Method) {
563 // Search protocol qualifiers.
Steve Naroff279d8962009-02-23 15:40:48 +0000564 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
565 E = OCIType->qual_end(); QI != E; ++QI) {
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000566 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000567 break;
568 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000569 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000570 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000571 // If we have implementations in scope, check "private" methods.
572 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
573
574 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000575 // If we still haven't found a method, look in the global pool. This
576 // behavior isn't very desirable, however we need it for GCC
577 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000578 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000579 Method = LookupInstanceMethodInGlobalPool(
580 Sel, SourceRange(lbrac,rbrac));
581 if (Method && !OCIType->getDecl()->isForwardDecl())
582 Diag(lbrac, diag::warn_maynot_respond)
583 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
584 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000585 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000586 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000587 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
588 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000589 } else if (!Context.getObjCIdType().isNull() &&
590 (ReceiverCType->isPointerType() ||
591 (ReceiverCType->isIntegerType() &&
592 ReceiverCType->isScalarType()))) {
593 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000594 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000595 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000596 ImpCastExprToType(RExpr, Context.getObjCIdType());
597 } else {
598 // Reject other random receiver types (e.g. structs).
599 Diag(lbrac, diag::err_bad_receiver_type)
600 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000601 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000602 }
603
Chris Lattner077bf5e2008-11-24 03:33:13 +0000604 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000605 lbrac, rbrac, returnType))
606 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000607 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
608 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000609}
Chris Lattnereca7be62008-04-07 05:30:13 +0000610
611//===----------------------------------------------------------------------===//
612// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
613//===----------------------------------------------------------------------===//
614
615/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
616/// inheritance hierarchy of 'rProto'.
617static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
618 ObjCProtocolDecl *rProto) {
619 if (lProto == rProto)
620 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000621 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
622 E = rProto->protocol_end(); PI != E; ++PI)
623 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000624 return true;
625 return false;
626}
627
628/// ClassImplementsProtocol - Checks that 'lProto' protocol
629/// has been implemented in IDecl class, its super class or categories (if
630/// lookupCategory is true).
631static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
632 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000633 bool lookupCategory,
634 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000635
636 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000637 const ObjCList<ObjCProtocolDecl> &Protocols =
638 IDecl->getReferencedProtocols();
639
640 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
641 E = Protocols.end(); PI != E; ++PI) {
642 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000643 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000644 // This is dubious and is added to be compatible with gcc.
645 // In gcc, it is also allowed assigning a protocol-qualified 'id'
646 // type to a LHS object when protocol in qualified LHS is in list
647 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000648 // FIXME: Treat this as an extension, and flag this as an error when
649 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000650 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000651 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000652 }
653
654 // 2nd, look up the category.
655 if (lookupCategory)
656 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
657 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000658 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
659 E = CDecl->protocol_end(); PI != E; ++PI)
660 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000661 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000662 }
663
664 // 3rd, look up the super class(s)
665 if (IDecl->getSuperClass())
666 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000667 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
668 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000669
670 return false;
671}
672
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000673/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
674/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000675/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000676bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
677 bool compare) {
678 // Allow id<P..> and an 'id' or void* type in all cases.
679 if (const PointerType *PT = lhs->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 } else if (const PointerType *PT = rhs->getAsPointerType()) {
684 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000685 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000686 return true;
687 }
688
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000689 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
690 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
691 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000692 QualType rtype;
693
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000694 if (!rhsQID) {
695 // Not comparing two ObjCQualifiedIdType's?
696 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000697
698 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000699 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000700 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000701 // If the RHS is a unqualified interface pointer "NSString*",
702 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000703 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
704 ObjCInterfaceDecl *rhsID = IT->getDecl();
705 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
706 // when comparing an id<P> on lhs with a static type on rhs,
707 // see if static class implements all of id's protocols, directly or
708 // through its super class and categories.
709 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
710 return false;
711 }
712 return true;
713 }
714 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000715 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000716
717 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000718 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000719 RHSProtoI = rhsQI->qual_begin();
720 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000721 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000722 RHSProtoI = rhsQID->qual_begin();
723 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000724 } else {
725 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000726 }
727
728 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
729 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
730 bool match = false;
731
732 // when comparing an id<P> on lhs with a static type on rhs,
733 // see if static class implements all of id's protocols, directly or
734 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000735 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
736 ObjCProtocolDecl *rhsProto = *RHSProtoI;
737 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000738 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000739 match = true;
740 break;
741 }
742 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000743 if (rhsQI) {
744 // If the RHS is a qualified interface pointer "NSString<P>*",
745 // make sure we check the class hierarchy.
746 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
747 ObjCInterfaceDecl *rhsID = IT->getDecl();
748 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
749 // when comparing an id<P> on lhs with a static type on rhs,
750 // see if static class implements all of id's protocols, directly or
751 // through its super class and categories.
752 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
753 match = true;
754 break;
755 }
756 }
757 }
758 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000759 if (!match)
760 return false;
761 }
762
763 return true;
764 }
765
766 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
767 assert(rhsQID && "One of the LHS/RHS should be id<x>");
768
769 if (!lhs->isPointerType())
770 return false;
771
772 QualType ltype = lhs->getAsPointerType()->getPointeeType();
773 if (const ObjCQualifiedInterfaceType *lhsQI =
774 ltype->getAsObjCQualifiedInterfaceType()) {
775 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
776 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
777 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
778 bool match = false;
779 ObjCProtocolDecl *lhsProto = *LHSProtoI;
780 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
781 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
782 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000783 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000784 match = true;
785 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000786 }
787 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000788 if (!match)
789 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000790 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000791 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000792 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000793
794 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
795 // for static type vs. qualified 'id' type, check that class implements
796 // all of 'id's protocols.
797 ObjCInterfaceDecl *lhsID = IT->getDecl();
798 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
799 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000800 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000801 return false;
802 }
803 return true;
804 }
805 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000806}
807