blob: 3bee461cf6fb2f0a8f7c1e44889b771d48bc294d [file] [log] [blame]
Chris Lattner09493052008-01-04 22:32:30 +00001//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective-C expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Chris Lattner3c24ef12009-02-18 06:48:40 +000018#include "llvm/ADT/SmallString.h"
Steve Naroff73ec9322009-03-09 21:12:44 +000019#include "clang/Lex/Preprocessor.h"
20
Chris Lattner09493052008-01-04 22:32:30 +000021using namespace clang;
22
23Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner3c24ef12009-02-18 06:48:40 +000024 ExprTy **strings,
Chris Lattner09493052008-01-04 22:32:30 +000025 unsigned NumStrings) {
Chris Lattner3c24ef12009-02-18 06:48:40 +000026 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
27
Chris Lattner4ae4a912009-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 Lattner3c24ef12009-02-18 06:48:40 +000032 StringLiteral *S = Strings[0];
Chris Lattner4ae4a912009-02-18 06:13:04 +000033
34 // If we have a multi-part string, merge it all together.
35 if (NumStrings != 1) {
Chris Lattner09493052008-01-04 22:32:30 +000036 // Concatenate objc strings.
Chris Lattner3c24ef12009-02-18 06:48:40 +000037 llvm::SmallString<128> StrBuf;
38 llvm::SmallVector<SourceLocation, 8> StrLocs;
Chris Lattnerc3144742009-02-18 05:49:11 +000039
Chris Lattnerc3144742009-02-18 05:49:11 +000040 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner3c24ef12009-02-18 06:48:40 +000041 S = Strings[i];
42
43 // ObjC strings can't be wide.
Chris Lattner4ae4a912009-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 Lattner3c24ef12009-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 Kremenek0c97e042009-02-07 01:47:29 +000057 S->Destroy(Context);
Chris Lattner09493052008-01-04 22:32:30 +000058 }
Chris Lattner3c24ef12009-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 Lattneraa491192009-02-18 06:40:38 +000063 Context.getPointerType(Context.CharTy),
Chris Lattner3c24ef12009-02-18 06:48:40 +000064 &StrLocs[0], StrLocs.size());
Chris Lattner09493052008-01-04 22:32:30 +000065 }
66
Chris Lattner81f5be22009-02-18 06:01:06 +000067 // Verify that this composite string is acceptable for ObjC strings.
68 if (CheckObjCString(S))
Chris Lattner09493052008-01-04 22:32:30 +000069 return true;
Chris Lattnerecc8da32009-02-18 06:06:56 +000070
71 // Initialize the constant string interface lazily. This assumes
Steve Naroffe2caf222009-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 Lattnerecc8da32009-02-18 06:06:56 +000075 QualType Ty = Context.getObjCConstantStringInterface();
76 if (!Ty.isNull()) {
77 Ty = Context.getPointerType(Ty);
Chris Lattnerbac12452008-06-21 21:44:18 +000078 } else {
Steve Naroffe2caf222009-04-07 14:18:33 +000079 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Chris Lattnerecc8da32009-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 Naroffe2caf222009-04-07 14:18:33 +000086 // If there is no NSString interface defined then treat constant
Chris Lattnerecc8da32009-02-18 06:06:56 +000087 // strings as untyped objects and let the runtime figure it out later.
88 Ty = Context.getObjCIdType();
89 }
Chris Lattnerbac12452008-06-21 21:44:18 +000090 }
Chris Lattnerecc8da32009-02-18 06:06:56 +000091
Chris Lattner4ae4a912009-02-18 06:13:04 +000092 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner09493052008-01-04 22:32:30 +000093}
94
95Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
96 SourceLocation EncodeLoc,
97 SourceLocation LParenLoc,
Chris Lattnerecc8da32009-02-18 06:06:56 +000098 TypeTy *ty,
Chris Lattner09493052008-01-04 22:32:30 +000099 SourceLocation RParenLoc) {
Chris Lattnerecc8da32009-02-18 06:06:56 +0000100 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner09493052008-01-04 22:32:30 +0000101
Chris Lattnerc5d32632009-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 Lattner09493052008-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 Lattnerecc8da32009-02-18 06:06:56 +0000122 QualType Ty = Context.getObjCSelType();
123 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner09493052008-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) {
Douglas Gregore57c8cc2009-04-23 23:18:26 +0000131 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattner09493052008-01-04 22:32:30 +0000132 if (!PDecl) {
Chris Lattner65cae292008-11-19 08:23:25 +0000133 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner09493052008-01-04 22:32:30 +0000134 return true;
135 }
136
Chris Lattnerecc8da32009-02-18 06:06:56 +0000137 QualType Ty = Context.getObjCProtoType();
138 if (Ty.isNull())
Chris Lattner09493052008-01-04 22:32:30 +0000139 return true;
Chris Lattnerecc8da32009-02-18 06:06:56 +0000140 Ty = Context.getPointerType(Ty);
141 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner09493052008-01-04 22:32:30 +0000142}
143
Daniel Dunbar7776e102008-09-11 00:50:25 +0000144bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
145 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000146 bool isClassMessage,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000147 SourceLocation lbrac, SourceLocation rbrac,
148 QualType &ReturnType) {
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000149 if (!Method) {
Daniel Dunbar753d1102008-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 Lattner3a8f2942008-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 Dunbar842b5e22008-09-11 00:01:56 +0000158 ReturnType = Context.getObjCIdType();
159 return false;
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000160 }
Chris Lattner3a8f2942008-11-24 03:33:13 +0000161
162 ReturnType = Method->getResultType();
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000163
Daniel Dunbar7776e102008-09-11 00:50:25 +0000164 unsigned NumNamedArgs = Sel.getNumArgs();
165 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
166
Chris Lattner81f00ed2009-04-12 08:11:20 +0000167 bool IsError = false;
Daniel Dunbar7776e102008-09-11 00:50:25 +0000168 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner09493052008-01-04 22:32:30 +0000169 Expr *argExpr = Args[i];
170 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
171
Chris Lattner5c6b2c62009-02-20 18:43:26 +0000172 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner09493052008-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 Lattnera2ef0822008-04-02 17:17:33 +0000176 if (lhsType->isArrayType())
177 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner09493052008-01-04 22:32:30 +0000178 else if (lhsType->isFunctionType())
179 lhsType = Context.getPointerType(lhsType);
180
Chris Lattnera2ef0822008-04-02 17:17:33 +0000181 AssignConvertType Result =
182 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner09493052008-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
Chris Lattner81f00ed2009-04-12 08:11:20 +0000186 IsError |=
Chris Lattner09493052008-01-04 22:32:30 +0000187 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
188 argExpr, "sending");
189 }
Daniel Dunbar7776e102008-09-11 00:50:25 +0000190
191 // Promote additional arguments to variadic methods.
192 if (Method->isVariadic()) {
Anders Carlsson4b8e38c2009-01-16 16:48:51 +0000193 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner81f00ed2009-04-12 08:11:20 +0000194 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar7776e102008-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 Lattner8ba580c2008-11-19 05:08:23 +0000199 diag::err_typecheck_call_too_many_args)
Chris Lattner66beaba2008-11-21 18:44:24 +0000200 << 2 /*method*/ << Method->getSourceRange()
Chris Lattner8ba580c2008-11-19 05:08:23 +0000201 << SourceRange(Args[NumNamedArgs]->getLocStart(),
202 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar7776e102008-09-11 00:50:25 +0000203 }
204 }
205
Chris Lattner81f00ed2009-04-12 08:11:20 +0000206 return IsError;
Chris Lattner09493052008-01-04 22:32:30 +0000207}
208
Steve Naroffff6c8022009-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 Naroff4b129152009-02-26 15:55:06 +0000216// Helper method for ActOnClassMethod/ActOnInstanceMethod.
217// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian271f9062009-03-04 18:15:57 +0000218// If failed, then we search in class's root for an instance method.
Steve Naroff4b129152009-02-26 15:55:06 +0000219// Returns 0 if no method is found.
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000220ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Naroff4b129152009-02-26 15:55:06 +0000221 ObjCInterfaceDecl *ClassDecl) {
222 ObjCMethodDecl *Method = 0;
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000223 // lookup in class and all superclasses
224 while (ClassDecl && !Method) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000225 if (ObjCImplementationDecl *ImpDecl
226 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000227 Method = ImpDecl->getClassMethod(Context, Sel);
Steve Naroff4b129152009-02-26 15:55:06 +0000228
Steve Naroff9d8dc2b2009-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)
Douglas Gregorcd19b572009-04-23 01:02:12 +0000233 Method = ObjCCategoryImpls[i]->getClassMethod(Context, Sel);
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000234 }
Steve Naroff4b129152009-02-26 15:55:06 +0000235 }
Steve Naroff9d8dc2b2009-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()) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000241 Method = ClassDecl->lookupInstanceMethod(Context, Sel);
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000242 // 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 Naroff4b129152009-02-26 15:55:06 +0000249 }
Steve Naroff9d8dc2b2009-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.
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000258 if (ObjCImplementationDecl *ImpDecl
259 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000260 Method = ImpDecl->getInstanceMethod(Context, Sel);
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000261
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)
Douglas Gregorcd19b572009-04-23 01:02:12 +0000266 Method = ObjCCategoryImpls[i]->getInstanceMethod(Context, Sel);
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000267 }
268 }
269 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian271f9062009-03-04 18:15:57 +0000270 }
Steve Naroff4b129152009-02-26 15:55:06 +0000271 return Method;
272}
273
Steve Naroff73ec9322009-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);
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000285 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Context, Sel);
Steve Naroff73ec9322009-03-09 21:12:44 +0000286
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())
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000291 if (ObjCImplementationDecl *ImpDecl
292 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000293 Getter = ImpDecl->getClassMethod(Context, Sel);
Steve Naroff73ec9322009-03-09 21:12:44 +0000294
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 Naroffa60a4002009-03-10 17:24:38 +0000303 Selector SetterSel =
304 SelectorTable::constructSetterName(PP.getIdentifierTable(),
305 PP.getSelectorTable(), &propertyName);
Steve Naroff73ec9322009-03-09 21:12:44 +0000306
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000307 ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel);
Steve Naroff73ec9322009-03-09 21:12:44 +0000308 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())
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000313 if (ObjCImplementationDecl *ImpDecl
314 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregorcd19b572009-04-23 01:02:12 +0000315 Setter = ImpDecl->getClassMethod(Context, SetterSel);
Steve Naroff73ec9322009-03-09 21:12:44 +0000316 }
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)
Douglas Gregorcd19b572009-04-23 01:02:12 +0000321 Setter = ObjCCategoryImpls[i]->getClassMethod(Context, SetterSel);
Steve Naroff73ec9322009-03-09 21:12:44 +0000322 }
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 Lattner09493052008-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 Carlssonb42900d2009-02-14 18:21:46 +0000352 SourceLocation lbrac, SourceLocation receiverLoc,
353 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000354 ExprTy **Args, unsigned NumArgs)
Chris Lattner09493052008-01-04 22:32:30 +0000355{
356 assert(receiverName && "missing receiver class name");
357
358 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremenek42730c52008-01-07 19:49:32 +0000359 ObjCInterfaceDecl* ClassDecl = 0;
Steve Naroff91a69202008-07-24 19:44:33 +0000360 bool isSuper = false;
361
Chris Lattner87fada82008-11-20 05:35:30 +0000362 if (receiverName->isStr("super")) {
Steve Naroffc64a53d2008-11-19 15:54:23 +0000363 if (getCurMethodDecl()) {
364 isSuper = true;
Fariborz Jahaniana04fcb62009-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 Naroffc64a53d2008-11-19 15:54:23 +0000370 if (!ClassDecl)
Fariborz Jahaniana04fcb62009-01-07 21:01:41 +0000371 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregor5d764842009-01-09 17:18:27 +0000372 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroffc64a53d2008-11-19 15:54:23 +0000373 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
374 superTy = Context.getPointerType(superTy);
Ted Kremenek0c97e042009-02-07 01:47:29 +0000375 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
376 superTy);
Steve Naroffc64a53d2008-11-19 15:54:23 +0000377 // We are really in an instance method, redirect.
Anders Carlssonb42900d2009-02-14 18:21:46 +0000378 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
379 selectorLoc, rbrac, Args, NumArgs);
Steve Naroffc64a53d2008-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 Gregor09be81b2009-02-04 17:27:36 +0000386 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroffc64a53d2008-11-19 15:54:23 +0000387 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
388 if (VD) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000389 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
390 receiverLoc);
Steve Naroffc64a53d2008-11-19 15:54:23 +0000391 // We are really in an instance method, redirect.
Anders Carlssonb42900d2009-02-14 18:21:46 +0000392 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
393 selectorLoc, rbrac, Args, NumArgs);
Steve Naroffc64a53d2008-11-19 15:54:23 +0000394 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000395 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroffc64a53d2008-11-19 15:54:23 +0000396 }
Chris Lattner09493052008-01-04 22:32:30 +0000397 } else
398 ClassDecl = getObjCInterfaceDecl(receiverName);
399
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000400 // The following code allows for the following GCC-ism:
Steve Naroff51c6cff2008-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 Naroff2c7e0e52008-07-25 19:39:00 +0000409 // If necessary, the following lookup could move to getObjCInterfaceDecl().
410 if (!ClassDecl) {
Douglas Gregor09be81b2009-02-04 17:27:36 +0000411 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff2c7e0e52008-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 Lattner330a05b2009-03-29 05:01:10 +0000415 if (!OCIT) {
416 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
417 return true;
418 }
Fariborz Jahanian44da7a12009-01-16 20:35:09 +0000419 ClassDecl = OCIT->getDecl();
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000420 }
421 }
422 assert(ClassDecl && "missing interface declaration");
Steve Naroff51c6cff2008-06-04 23:08:38 +0000423 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000424 QualType returnType;
Fariborz Jahanian243f6042009-05-08 23:02:36 +0000425 if (ClassDecl->isForwardDecl()) {
426 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanianf366d512009-05-08 23:45:49 +0000427 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian243f6042009-05-08 23:02:36 +0000428 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
429 if (Method)
Fariborz Jahanianf366d512009-05-08 23:45:49 +0000430 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
431 << Method->getDeclName();
Fariborz Jahanian243f6042009-05-08 23:02:36 +0000432 }
433 if (!Method)
434 Method = ClassDecl->lookupClassMethod(Context, Sel);
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000435
436 // If we have an implementation in scope, check "private" methods.
Steve Naroff4b129152009-02-26 15:55:06 +0000437 if (!Method)
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000438 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000439
Douglas Gregoraa57e862009-02-18 21:56:37 +0000440 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
441 return true;
Anders Carlsson02ee8a12009-02-14 19:08:58 +0000442
Chris Lattner3a8f2942008-11-24 03:33:13 +0000443 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000444 lbrac, rbrac, returnType))
445 return true;
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000446
Anders Carlsson89f54922009-05-26 15:22:25 +0000447 returnType = returnType.getNonReferenceType();
448
Mike Stumpe127ae32009-05-16 07:39:55 +0000449 // If we have the ObjCInterfaceDecl* for the class that is receiving the
450 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
451 // IdentifierInfo* for the class.
452 // FIXME: need to do a better job handling 'super' usage within a class. For
453 // now, we simply pass the "super" identifier through (which isn't consistent
454 // with instance methods.
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000455 if (isSuper)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000456 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
457 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000458 else
Ted Kremenek0c97e042009-02-07 01:47:29 +0000459 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
460 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000461}
462
463// ActOnInstanceMessage - used for both unary and keyword messages.
464// ArgExprs is optional - if it is present, the number of expressions
465// is obtained from Sel.getNumArgs().
Chris Lattner4d018542008-07-21 06:31:05 +0000466Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000467 SourceLocation lbrac,
Anders Carlssonb42900d2009-02-14 18:21:46 +0000468 SourceLocation receiverLoc,
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000469 SourceLocation rbrac,
470 ExprTy **Args, unsigned NumArgs) {
Chris Lattner09493052008-01-04 22:32:30 +0000471 assert(receiver && "missing receiver expression");
472
473 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
474 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner5c712052009-04-29 05:48:32 +0000475
476 // If necessary, apply function/array conversion to the receiver.
477 // C99 6.7.5.3p[7,8].
478 DefaultFunctionArrayConversion(RExpr);
479
Chris Lattner09493052008-01-04 22:32:30 +0000480 QualType returnType;
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000481 QualType ReceiverCType =
482 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff7926f102008-11-17 22:29:32 +0000483
484 // Handle messages to 'super'.
Steve Naroff670901a2009-02-23 15:40:48 +0000485 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff7926f102008-11-17 22:29:32 +0000486 ObjCMethodDecl *Method = 0;
487 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
488 // If we have an interface in scope, check 'super' methods.
489 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000490 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000491 Method = SuperDecl->lookupInstanceMethod(Context, Sel);
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000492
493 if (!Method)
494 // If we have implementations in scope, check "private" methods.
495 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
496 }
Steve Naroff7926f102008-11-17 22:29:32 +0000497 }
Anders Carlssonb42900d2009-02-14 18:21:46 +0000498
Douglas Gregoraa57e862009-02-18 21:56:37 +0000499 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
500 return true;
Anders Carlsson02ee8a12009-02-14 19:08:58 +0000501
Chris Lattner3a8f2942008-11-24 03:33:13 +0000502 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff7926f102008-11-17 22:29:32 +0000503 lbrac, rbrac, returnType))
504 return true;
Anders Carlsson89f54922009-05-26 15:22:25 +0000505
506 returnType = returnType.getNonReferenceType();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000507 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
508 rbrac, ArgExprs, NumArgs);
Steve Naroff7926f102008-11-17 22:29:32 +0000509 }
510
Chris Lattner4cff4b72008-07-21 05:57:44 +0000511 // Handle messages to id.
Steve Naroffef5b9fc2008-09-29 16:51:41 +0000512 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Fariborz Jahaniand6002732009-05-21 21:04:28 +0000513 ReceiverCType->isBlockPointerType() ||
514 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff68354f32008-09-30 14:38:43 +0000515 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
516 Sel, SourceRange(lbrac,rbrac));
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000517 if (!Method)
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000518 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Chris Lattner3a8f2942008-11-24 03:33:13 +0000519 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000520 lbrac, rbrac, returnType))
521 return true;
Anders Carlsson89f54922009-05-26 15:22:25 +0000522 returnType = returnType.getNonReferenceType();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000523 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
524 rbrac, ArgExprs, NumArgs);
Chris Lattner4cff4b72008-07-21 05:57:44 +0000525 }
526
527 // Handle messages to Class.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000528 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000529 ObjCMethodDecl *Method = 0;
Steve Naroffff6c8022009-03-04 15:11:40 +0000530
Chris Lattner55a24332008-07-21 06:44:27 +0000531 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffa3d60422009-02-23 02:25:40 +0000532 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
533 // First check the public methods in the class interface.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000534 Method = ClassDecl->lookupClassMethod(Context, Sel);
Steve Naroffa3d60422009-02-23 02:25:40 +0000535
Steve Naroff4b129152009-02-26 15:55:06 +0000536 if (!Method)
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000537 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffa3d60422009-02-23 02:25:40 +0000538 }
Douglas Gregoraa57e862009-02-18 21:56:37 +0000539 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
540 return true;
Steve Naroffa3d60422009-02-23 02:25:40 +0000541 }
Steve Naroffff6c8022009-03-04 15:11:40 +0000542 if (!Method) {
543 // If not messaging 'self', look for any factory method named 'Sel'.
544 if (!isSelfExpr(RExpr)) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000545 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahaniancb3a77b2009-03-04 17:50:39 +0000546 if (!Method) {
Fariborz Jahanian0c535272009-05-05 18:34:37 +0000547 // If no class (factory) method was found, check if an _instance_
548 // method of the same name exists in the root class only.
Steve Naroffff6c8022009-03-04 15:11:40 +0000549 Method = LookupInstanceMethodInGlobalPool(
550 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian0c535272009-05-05 18:34:37 +0000551 if (Method)
552 if (const ObjCInterfaceDecl *ID =
553 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
554 if (ID->getSuperClass())
555 Diag(lbrac, diag::warn_root_inst_method_not_found)
556 << Sel << SourceRange(lbrac, rbrac);
557 }
Fariborz Jahaniancb3a77b2009-03-04 17:50:39 +0000558 }
Steve Naroffff6c8022009-03-04 15:11:40 +0000559 }
560 }
Chris Lattner3a8f2942008-11-24 03:33:13 +0000561 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000562 lbrac, rbrac, returnType))
563 return true;
Anders Carlsson89f54922009-05-26 15:22:25 +0000564 returnType = returnType.getNonReferenceType();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000565 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
566 rbrac, ArgExprs, NumArgs);
Chris Lattner4cff4b72008-07-21 05:57:44 +0000567 }
568
Chris Lattnerf9147652008-07-21 06:12:56 +0000569 ObjCMethodDecl *Method = 0;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000570 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattnerf9147652008-07-21 06:12:56 +0000571
572 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
573 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000574 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Naroffd305a862009-02-21 21:17:01 +0000575 // Search protocols for instance methods.
Chris Lattner4cff4b72008-07-21 05:57:44 +0000576 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
577 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000578 if (PDecl && (Method = PDecl->lookupInstanceMethod(Context, Sel)))
Chris Lattner4cff4b72008-07-21 05:57:44 +0000579 break;
Steve Naroff56d0e892009-04-07 15:07:57 +0000580 // Since we aren't supporting "Class<foo>", look for a class method.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000581 if (PDecl && (Method = PDecl->lookupClassMethod(Context, Sel)))
Steve Naroff56d0e892009-04-07 15:07:57 +0000582 break;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000583 }
Steve Naroff670901a2009-02-23 15:40:48 +0000584 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000585 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000586 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnere1b957d2008-02-01 06:43:02 +0000587
Steve Naroff670901a2009-02-23 15:40:48 +0000588 ClassDecl = OCIType->getDecl();
Steve Naroff68354f32008-09-30 14:38:43 +0000589 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stumpe127ae32009-05-16 07:39:55 +0000590 // faster than the following method (which can do *many* linear searches).
Steve Naroff68354f32008-09-30 14:38:43 +0000591 // The idea is to add class info to InstanceMethodPool.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000592 Method = ClassDecl->lookupInstanceMethod(Context, Sel);
Chris Lattner4cff4b72008-07-21 05:57:44 +0000593
594 if (!Method) {
595 // Search protocol qualifiers.
Steve Naroff670901a2009-02-23 15:40:48 +0000596 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
597 E = OCIType->qual_end(); QI != E; ++QI) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000598 if ((Method = (*QI)->lookupInstanceMethod(Context, Sel)))
Chris Lattner09493052008-01-04 22:32:30 +0000599 break;
600 }
Chris Lattner09493052008-01-04 22:32:30 +0000601 }
Steve Naroffd85ba922009-02-22 19:35:57 +0000602 if (!Method) {
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000603 // If we have implementations in scope, check "private" methods.
604 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
605
606 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroffff6c8022009-03-04 15:11:40 +0000607 // If we still haven't found a method, look in the global pool. This
608 // behavior isn't very desirable, however we need it for GCC
609 // compatibility. FIXME: should we deviate??
Steve Naroff9d8dc2b2009-03-08 18:56:13 +0000610 if (OCIType->qual_empty()) {
Steve Naroffff6c8022009-03-04 15:11:40 +0000611 Method = LookupInstanceMethodInGlobalPool(
612 Sel, SourceRange(lbrac,rbrac));
613 if (Method && !OCIType->getDecl()->isForwardDecl())
614 Diag(lbrac, diag::warn_maynot_respond)
615 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
616 }
Fariborz Jahanianba0041b2009-03-03 22:19:15 +0000617 }
Steve Naroffd85ba922009-02-22 19:35:57 +0000618 }
Douglas Gregoraa57e862009-02-18 21:56:37 +0000619 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
620 return true;
Chris Lattner8ccad8a2009-03-09 21:19:16 +0000621 } else if (!Context.getObjCIdType().isNull() &&
622 (ReceiverCType->isPointerType() ||
623 (ReceiverCType->isIntegerType() &&
624 ReceiverCType->isScalarType()))) {
625 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroffaa3ee072009-03-01 17:14:31 +0000626 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000627 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner8ccad8a2009-03-09 21:19:16 +0000628 ImpCastExprToType(RExpr, Context.getObjCIdType());
629 } else {
630 // Reject other random receiver types (e.g. structs).
631 Diag(lbrac, diag::err_bad_receiver_type)
632 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattnerf9147652008-07-21 06:12:56 +0000633 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000634 }
635
Fariborz Jahanian180f3412009-05-13 18:09:35 +0000636 if (Method)
637 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner3a8f2942008-11-24 03:33:13 +0000638 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000639 lbrac, rbrac, returnType))
640 return true;
Anders Carlsson89f54922009-05-26 15:22:25 +0000641 returnType = returnType.getNonReferenceType();
Ted Kremenek0c97e042009-02-07 01:47:29 +0000642 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
643 rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000644}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000645
646//===----------------------------------------------------------------------===//
647// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
648//===----------------------------------------------------------------------===//
649
650/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
651/// inheritance hierarchy of 'rProto'.
652static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
653 ObjCProtocolDecl *rProto) {
654 if (lProto == rProto)
655 return true;
Chris Lattner0be08822008-07-21 21:32:27 +0000656 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
657 E = rProto->protocol_end(); PI != E; ++PI)
658 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000659 return true;
660 return false;
661}
662
663/// ClassImplementsProtocol - Checks that 'lProto' protocol
664/// has been implemented in IDecl class, its super class or categories (if
665/// lookupCategory is true).
666static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
667 ObjCInterfaceDecl *IDecl,
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000668 bool lookupCategory,
669 bool RHSIsQualifiedID = false) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000670
671 // 1st, look up the class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000672 const ObjCList<ObjCProtocolDecl> &Protocols =
673 IDecl->getReferencedProtocols();
674
675 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
676 E = Protocols.end(); PI != E; ++PI) {
677 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000678 return true;
Mike Stumpe127ae32009-05-16 07:39:55 +0000679 // This is dubious and is added to be compatible with gcc. In gcc, it is
680 // also allowed assigning a protocol-qualified 'id' type to a LHS object
681 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
682 // object. This IMO, should be a bug.
683 // FIXME: Treat this as an extension, and flag this as an error when GCC
684 // extensions are not enabled.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000685 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000686 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000687 }
688
689 // 2nd, look up the category.
690 if (lookupCategory)
691 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
692 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner0be08822008-07-21 21:32:27 +0000693 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
694 E = CDecl->protocol_end(); PI != E; ++PI)
695 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000696 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000697 }
698
699 // 3rd, look up the super class(s)
700 if (IDecl->getSuperClass())
701 return
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000702 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
703 RHSIsQualifiedID);
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000704
705 return false;
706}
707
Fariborz Jahanian6e83c922009-05-14 23:52:54 +0000708/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
709/// return true if lhs's protocols conform to rhs's protocol; false
710/// otherwise.
711bool Sema::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
712 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
713 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
714 return false;
715}
716
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000717/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
718/// ObjCQualifiedIDType.
Steve Naroff2567c9e2009-03-03 15:43:24 +0000719/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000720bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
721 bool compare) {
722 // Allow id<P..> and an 'id' or void* type in all cases.
723 if (const PointerType *PT = lhs->getAsPointerType()) {
724 QualType PointeeTy = PT->getPointeeType();
Chris Lattner79e9a422009-04-12 09:02:39 +0000725 if (PointeeTy->isVoidType() ||
726 Context.isObjCIdStructType(PointeeTy) ||
727 Context.isObjCClassStructType(PointeeTy))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000728 return true;
729 } else if (const PointerType *PT = rhs->getAsPointerType()) {
730 QualType PointeeTy = PT->getPointeeType();
Chris Lattner79e9a422009-04-12 09:02:39 +0000731 if (PointeeTy->isVoidType() ||
732 Context.isObjCIdStructType(PointeeTy) ||
733 Context.isObjCClassStructType(PointeeTy))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000734 return true;
735 }
736
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000737 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
738 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
739 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000740 QualType rtype;
741
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000742 if (!rhsQID) {
743 // Not comparing two ObjCQualifiedIdType's?
744 if (!rhs->isPointerType()) return false;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000745
746 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000747 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000748 if (rhsQI == 0) {
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000749 // If the RHS is a unqualified interface pointer "NSString*",
750 // make sure we check the class hierarchy.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000751 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
752 ObjCInterfaceDecl *rhsID = IT->getDecl();
753 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
754 // when comparing an id<P> on lhs with a static type on rhs,
755 // see if static class implements all of id's protocols, directly or
756 // through its super class and categories.
757 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
758 return false;
759 }
760 return true;
761 }
762 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000763 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000764
765 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000766 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000767 RHSProtoI = rhsQI->qual_begin();
768 RHSProtoE = rhsQI->qual_end();
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000769 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000770 RHSProtoI = rhsQID->qual_begin();
771 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000772 } else {
773 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000774 }
775
776 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
777 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
778 bool match = false;
779
780 // when comparing an id<P> on lhs with a static type on rhs,
781 // see if static class implements all of id's protocols, directly or
782 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000783 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
784 ObjCProtocolDecl *rhsProto = *RHSProtoI;
785 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman70c167d2008-12-16 20:15:50 +0000786 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000787 match = true;
788 break;
789 }
790 }
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000791 if (rhsQI) {
792 // If the RHS is a qualified interface pointer "NSString<P>*",
793 // make sure we check the class hierarchy.
794 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
795 ObjCInterfaceDecl *rhsID = IT->getDecl();
796 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
797 // when comparing an id<P> on lhs with a static type on rhs,
798 // see if static class implements all of id's protocols, directly or
799 // through its super class and categories.
800 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
801 match = true;
802 break;
803 }
804 }
805 }
806 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000807 if (!match)
808 return false;
809 }
810
811 return true;
812 }
813
814 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
815 assert(rhsQID && "One of the LHS/RHS should be id<x>");
816
817 if (!lhs->isPointerType())
818 return false;
819
820 QualType ltype = lhs->getAsPointerType()->getPointeeType();
821 if (const ObjCQualifiedInterfaceType *lhsQI =
822 ltype->getAsObjCQualifiedInterfaceType()) {
823 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
824 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
825 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
826 bool match = false;
827 ObjCProtocolDecl *lhsProto = *LHSProtoI;
828 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
829 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
830 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman70c167d2008-12-16 20:15:50 +0000831 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000832 match = true;
833 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000834 }
835 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000836 if (!match)
837 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000838 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000839 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000840 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000841
842 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
843 // for static type vs. qualified 'id' type, check that class implements
844 // all of 'id's protocols.
845 ObjCInterfaceDecl *lhsID = IT->getDecl();
846 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
847 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000848 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000849 return false;
850 }
851 return true;
852 }
853 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000854}
855