blob: b6cf9d8e738c2661c29166a9624dd5e7e7802100 [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
Anders Carlssonfc0f0212009-06-07 18:45:35 +000095Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
96 QualType EncodedType,
97 SourceLocation RParenLoc) {
98 QualType StrTy;
99 if (EncodedType->isDependentType())
100 StrTy = Context.DependentTy;
101 else {
102 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 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
115 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
116}
117
Chris Lattner85a932e2008-01-04 22:32:30 +0000118Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
119 SourceLocation EncodeLoc,
120 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000121 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +0000122 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000123 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +0000124
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000125 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000126}
127
128Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
129 SourceLocation AtLoc,
130 SourceLocation SelLoc,
131 SourceLocation LParenLoc,
132 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000133 QualType Ty = Context.getObjCSelType();
134 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000135}
136
137Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
138 SourceLocation AtLoc,
139 SourceLocation ProtoLoc,
140 SourceLocation LParenLoc,
141 SourceLocation RParenLoc) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000142 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattner85a932e2008-01-04 22:32:30 +0000143 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000144 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000145 return true;
146 }
147
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000148 QualType Ty = Context.getObjCProtoType();
149 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000150 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000151 Ty = Context.getPointerType(Ty);
152 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000153}
154
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000155bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
156 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000157 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000158 SourceLocation lbrac, SourceLocation rbrac,
159 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000160 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000161 // Apply default argument promotion as for (C99 6.5.2.2p6).
162 for (unsigned i = 0; i != NumArgs; i++)
163 DefaultArgumentPromotion(Args[i]);
164
Chris Lattner077bf5e2008-11-24 03:33:13 +0000165 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
166 diag::warn_inst_method_not_found;
167 Diag(lbrac, DiagID)
168 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000169 ReturnType = Context.getObjCIdType();
170 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000171 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000172
173 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000174
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000175 unsigned NumNamedArgs = Sel.getNumArgs();
176 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
177
Chris Lattner312531a2009-04-12 08:11:20 +0000178 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000179 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000180 Expr *argExpr = Args[i];
181 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
182
Chris Lattner89951a82009-02-20 18:43:26 +0000183 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000184 QualType rhsType = argExpr->getType();
185
186 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000187 if (lhsType->isArrayType())
188 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000189 else if (lhsType->isFunctionType())
190 lhsType = Context.getPointerType(lhsType);
191
Chris Lattner987798a2008-04-02 17:17:33 +0000192 AssignConvertType Result =
193 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000194 if (Args[i] != argExpr) // The expression was converted.
195 Args[i] = argExpr; // Make sure we store the converted expression.
196
Chris Lattner312531a2009-04-12 08:11:20 +0000197 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000198 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
199 argExpr, "sending");
200 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000201
202 // Promote additional arguments to variadic methods.
203 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000204 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000205 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000206 } else {
207 // Check for extra arguments to non-variadic methods.
208 if (NumArgs != NumNamedArgs) {
209 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000210 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000211 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000212 << SourceRange(Args[NumNamedArgs]->getLocStart(),
213 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000214 }
215 }
216
Chris Lattner312531a2009-04-12 08:11:20 +0000217 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000218}
219
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000220bool Sema::isSelfExpr(Expr *RExpr) {
221 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
222 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
223 return true;
224 return false;
225}
226
Steve Narofff1afaf62009-02-26 15:55:06 +0000227// Helper method for ActOnClassMethod/ActOnInstanceMethod.
228// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000229// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000230// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000231ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000232 ObjCInterfaceDecl *ClassDecl) {
233 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000234 // lookup in class and all superclasses
235 while (ClassDecl && !Method) {
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000236 if (ObjCImplementationDecl *ImpDecl
237 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000238 Method = ImpDecl->getClassMethod(Context, Sel);
Steve Narofff1afaf62009-02-26 15:55:06 +0000239
Steve Naroff5609ec02009-03-08 18:56:13 +0000240 // Look through local category implementations associated with the class.
241 if (!Method) {
242 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
243 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
Douglas Gregor653f1b12009-04-23 01:02:12 +0000244 Method = ObjCCategoryImpls[i]->getClassMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000245 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000246 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000247
248 // Before we give up, check if the selector is an instance method.
249 // But only in the root. This matches gcc's behaviour and what the
250 // runtime expects.
251 if (!Method && !ClassDecl->getSuperClass()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000252 Method = ClassDecl->lookupInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000253 // Look through local category implementations associated
254 // with the root class.
255 if (!Method)
256 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
257 }
258
259 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000260 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000261 return Method;
262}
263
264ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
265 ObjCInterfaceDecl *ClassDecl) {
266 ObjCMethodDecl *Method = 0;
267 while (ClassDecl && !Method) {
268 // If we have implementations in scope, check "private" methods.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000269 if (ObjCImplementationDecl *ImpDecl
270 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000271 Method = ImpDecl->getInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000272
273 // Look through local category implementations associated with the class.
274 if (!Method) {
275 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
276 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
Douglas Gregor653f1b12009-04-23 01:02:12 +0000277 Method = ObjCCategoryImpls[i]->getInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000278 }
279 }
280 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000281 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000282 return Method;
283}
284
Steve Naroff61f72cb2009-03-09 21:12:44 +0000285Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
286 IdentifierInfo &receiverName,
287 IdentifierInfo &propertyName,
288 SourceLocation &receiverNameLoc,
289 SourceLocation &propertyNameLoc) {
290
291 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
292
293 // Search for a declared property first.
294
295 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000296 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Context, Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000297
298 // If this reference is in an @implementation, check for 'private' methods.
299 if (!Getter)
300 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
301 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000302 if (ObjCImplementationDecl *ImpDecl
303 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000304 Getter = ImpDecl->getClassMethod(Context, Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000305
306 if (Getter) {
307 // FIXME: refactor/share with ActOnMemberReference().
308 // Check if we can reference this property.
309 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
310 return ExprError();
311 }
312
313 // Look for the matching setter, in case it is needed.
Steve Narofffdc92b72009-03-10 17:24:38 +0000314 Selector SetterSel =
315 SelectorTable::constructSetterName(PP.getIdentifierTable(),
316 PP.getSelectorTable(), &propertyName);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000317
Douglas Gregor6ab35242009-04-09 21:40:53 +0000318 ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000319 if (!Setter) {
320 // If this reference is in an @implementation, also check for 'private'
321 // methods.
322 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
323 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000324 if (ObjCImplementationDecl *ImpDecl
325 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000326 Setter = ImpDecl->getClassMethod(Context, SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000327 }
328 // Look through local category implementations associated with the class.
329 if (!Setter) {
330 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) {
331 if (ObjCCategoryImpls[i]->getClassInterface() == IFace)
Douglas Gregor653f1b12009-04-23 01:02:12 +0000332 Setter = ObjCCategoryImpls[i]->getClassMethod(Context, SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000333 }
334 }
335
336 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
337 return ExprError();
338
339 if (Getter || Setter) {
340 QualType PType;
341
342 if (Getter)
343 PType = Getter->getResultType();
344 else {
345 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
346 E = Setter->param_end(); PI != E; ++PI)
347 PType = (*PI)->getType();
348 }
349 return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter,
350 propertyNameLoc, IFace, receiverNameLoc));
351 }
352 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
353 << &propertyName << Context.getObjCInterfaceType(IFace));
354}
355
356
Chris Lattner85a932e2008-01-04 22:32:30 +0000357// ActOnClassMessage - used for both unary and keyword messages.
358// ArgExprs is optional - if it is present, the number of expressions
359// is obtained from Sel.getNumArgs().
360Sema::ExprResult Sema::ActOnClassMessage(
361 Scope *S,
362 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000363 SourceLocation lbrac, SourceLocation receiverLoc,
364 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000365 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000366{
367 assert(receiverName && "missing receiver class name");
368
369 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000370 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000371 bool isSuper = false;
372
Chris Lattner84692652008-11-20 05:35:30 +0000373 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000374 if (getCurMethodDecl()) {
375 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000376 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
377 if (!OID)
378 return Diag(lbrac, diag::error_no_super_class_message)
379 << getCurMethodDecl()->getDeclName();
380 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000381 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000382 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000383 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000384 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
385 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000386 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
387 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000388 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000389 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
390 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000391 }
392 // We are sending a message to 'super' within a class method. Do nothing,
393 // the receiver will pass through as 'super' (how convenient:-).
394 } else {
395 // 'super' has been used outside a method context. If a variable named
396 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000397 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000398 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
399 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000400 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
401 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000402 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000403 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
404 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000405 }
Chris Lattner08631c52008-11-23 21:45:46 +0000406 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000407 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000408 } else
409 ClassDecl = getObjCInterfaceDecl(receiverName);
410
Steve Naroff7c778f12008-07-25 19:39:00 +0000411 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000412 //
413 // typedef XCElementDisplayRect XCElementGraphicsRect;
414 //
415 // @implementation XCRASlice
416 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
417 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
418 // }
419 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000420 // If necessary, the following lookup could move to getObjCInterfaceDecl().
421 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000422 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000423 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
424 const ObjCInterfaceType *OCIT;
425 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Chris Lattner64540d72009-03-29 05:01:10 +0000426 if (!OCIT) {
427 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
428 return true;
429 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000430 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000431 }
432 }
433 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000434 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000435 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000436 if (ClassDecl->isForwardDecl()) {
437 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000438 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000439 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
440 if (Method)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000441 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
442 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000443 }
444 if (!Method)
445 Method = ClassDecl->lookupClassMethod(Context, Sel);
Steve Naroff7c778f12008-07-25 19:39:00 +0000446
447 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000448 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000449 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000450
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000451 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
452 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000453
Chris Lattner077bf5e2008-11-24 03:33:13 +0000454 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000455 lbrac, rbrac, returnType))
456 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000457
Anders Carlsson187ba152009-05-26 15:22:25 +0000458 returnType = returnType.getNonReferenceType();
459
Mike Stump390b4cc2009-05-16 07:39:55 +0000460 // If we have the ObjCInterfaceDecl* for the class that is receiving the
461 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
462 // IdentifierInfo* for the class.
463 // FIXME: need to do a better job handling 'super' usage within a class. For
464 // now, we simply pass the "super" identifier through (which isn't consistent
465 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000466 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000467 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
468 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000469 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000470 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
471 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000472}
473
474// ActOnInstanceMessage - used for both unary and keyword messages.
475// ArgExprs is optional - if it is present, the number of expressions
476// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000477Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000478 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000479 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000480 SourceLocation rbrac,
481 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000482 assert(receiver && "missing receiver expression");
483
484 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
485 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattnerd0d45992009-04-29 05:48:32 +0000486
487 // If necessary, apply function/array conversion to the receiver.
488 // C99 6.7.5.3p[7,8].
489 DefaultFunctionArrayConversion(RExpr);
490
Chris Lattner85a932e2008-01-04 22:32:30 +0000491 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000492 QualType ReceiverCType =
493 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000494
495 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000496 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000497 ObjCMethodDecl *Method = 0;
498 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
499 // If we have an interface in scope, check 'super' methods.
500 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000501 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000502 Method = SuperDecl->lookupInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000503
504 if (!Method)
505 // If we have implementations in scope, check "private" methods.
506 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
507 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000508 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000509
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000510 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
511 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000512
Chris Lattner077bf5e2008-11-24 03:33:13 +0000513 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000514 lbrac, rbrac, returnType))
515 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000516
517 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000518 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
519 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000520 }
521
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000522 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000523 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000524 ReceiverCType->isBlockPointerType() ||
525 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000526 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
527 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000528 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000530 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000531 lbrac, rbrac, returnType))
532 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000533 returnType = returnType.getNonReferenceType();
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
538 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000539 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000540 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000541
Chris Lattner6562fda2008-07-21 06:44:27 +0000542 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000543 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
544 // First check the public methods in the class interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000545 Method = ClassDecl->lookupClassMethod(Context, Sel);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000546
Steve Narofff1afaf62009-02-26 15:55:06 +0000547 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000548 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000549 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000550 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
551 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000552 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000553 if (!Method) {
554 // If not messaging 'self', look for any factory method named 'Sel'.
555 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000556 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000557 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000558 // If no class (factory) method was found, check if an _instance_
559 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000560 Method = LookupInstanceMethodInGlobalPool(
561 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000562 if (Method)
563 if (const ObjCInterfaceDecl *ID =
564 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
565 if (ID->getSuperClass())
566 Diag(lbrac, diag::warn_root_inst_method_not_found)
567 << Sel << SourceRange(lbrac, rbrac);
568 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000569 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000570 }
571 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000572 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000573 lbrac, rbrac, returnType))
574 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000575 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000576 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
577 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000578 }
579
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000580 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000581 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000582
583 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
584 // long as one of the protocols implements the selector (if not, warn).
Steve Naroff446ee4e2009-05-27 16:21:00 +0000585 if (ObjCQualifiedIdType *QIdTy = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000586 // Search protocols for instance methods.
Steve Naroff446ee4e2009-05-27 16:21:00 +0000587 for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(),
588 E = QIdTy->qual_end(); I != E; ++I) {
589 ObjCProtocolDecl *PDecl = *I;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000590 if (PDecl && (Method = PDecl->lookupInstanceMethod(Context, Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000591 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000592 // Since we aren't supporting "Class<foo>", look for a class method.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000593 if (PDecl && (Method = PDecl->lookupClassMethod(Context, Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000594 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000595 }
Steve Naroff279d8962009-02-23 15:40:48 +0000596 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000597 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000598 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000599
Steve Naroff279d8962009-02-23 15:40:48 +0000600 ClassDecl = OCIType->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000601 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000602 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000603 // The idea is to add class info to InstanceMethodPool.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000604 Method = ClassDecl->lookupInstanceMethod(Context, Sel);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000605
606 if (!Method) {
607 // Search protocol qualifiers.
Steve Naroff279d8962009-02-23 15:40:48 +0000608 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
609 E = OCIType->qual_end(); QI != E; ++QI) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000610 if ((Method = (*QI)->lookupInstanceMethod(Context, Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000611 break;
612 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000613 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000614 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000615 // If we have implementations in scope, check "private" methods.
616 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
617
618 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000619 // If we still haven't found a method, look in the global pool. This
620 // behavior isn't very desirable, however we need it for GCC
621 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000622 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000623 Method = LookupInstanceMethodInGlobalPool(
624 Sel, SourceRange(lbrac,rbrac));
625 if (Method && !OCIType->getDecl()->isForwardDecl())
626 Diag(lbrac, diag::warn_maynot_respond)
627 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
628 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000629 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000630 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000631 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
632 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000633 } else if (!Context.getObjCIdType().isNull() &&
634 (ReceiverCType->isPointerType() ||
635 (ReceiverCType->isIntegerType() &&
636 ReceiverCType->isScalarType()))) {
637 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000638 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000639 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000640 ImpCastExprToType(RExpr, Context.getObjCIdType());
641 } else {
642 // Reject other random receiver types (e.g. structs).
643 Diag(lbrac, diag::err_bad_receiver_type)
644 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000645 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000646 }
647
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000648 if (Method)
649 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000650 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000651 lbrac, rbrac, returnType))
652 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000653 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000654 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
655 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000656}
Chris Lattnereca7be62008-04-07 05:30:13 +0000657
658//===----------------------------------------------------------------------===//
659// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
660//===----------------------------------------------------------------------===//
661
662/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
663/// inheritance hierarchy of 'rProto'.
664static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
665 ObjCProtocolDecl *rProto) {
666 if (lProto == rProto)
667 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000668 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
669 E = rProto->protocol_end(); PI != E; ++PI)
670 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000671 return true;
672 return false;
673}
674
675/// ClassImplementsProtocol - Checks that 'lProto' protocol
676/// has been implemented in IDecl class, its super class or categories (if
677/// lookupCategory is true).
678static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
679 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000680 bool lookupCategory,
681 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000682
683 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000684 const ObjCList<ObjCProtocolDecl> &Protocols =
685 IDecl->getReferencedProtocols();
686
687 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
688 E = Protocols.end(); PI != E; ++PI) {
689 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000690 return true;
Mike Stump390b4cc2009-05-16 07:39:55 +0000691 // This is dubious and is added to be compatible with gcc. In gcc, it is
692 // also allowed assigning a protocol-qualified 'id' type to a LHS object
693 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
694 // object. This IMO, should be a bug.
695 // FIXME: Treat this as an extension, and flag this as an error when GCC
696 // extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000697 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000698 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000699 }
700
701 // 2nd, look up the category.
702 if (lookupCategory)
703 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
704 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000705 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
706 E = CDecl->protocol_end(); PI != E; ++PI)
707 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000708 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000709 }
710
711 // 3rd, look up the super class(s)
712 if (IDecl->getSuperClass())
713 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000714 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
715 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000716
717 return false;
718}
719
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000720/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
721/// return true if lhs's protocols conform to rhs's protocol; false
722/// otherwise.
723bool Sema::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
724 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
725 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
726 return false;
727}
728
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000729/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
730/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000731/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000732bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
733 bool compare) {
734 // Allow id<P..> and an 'id' or void* type in all cases.
735 if (const PointerType *PT = lhs->getAsPointerType()) {
736 QualType PointeeTy = PT->getPointeeType();
Chris Lattner2c4463f2009-04-12 09:02:39 +0000737 if (PointeeTy->isVoidType() ||
738 Context.isObjCIdStructType(PointeeTy) ||
739 Context.isObjCClassStructType(PointeeTy))
Chris Lattnereca7be62008-04-07 05:30:13 +0000740 return true;
741 } else if (const PointerType *PT = rhs->getAsPointerType()) {
742 QualType PointeeTy = PT->getPointeeType();
Chris Lattner2c4463f2009-04-12 09:02:39 +0000743 if (PointeeTy->isVoidType() ||
744 Context.isObjCIdStructType(PointeeTy) ||
745 Context.isObjCClassStructType(PointeeTy))
Chris Lattnereca7be62008-04-07 05:30:13 +0000746 return true;
747 }
748
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000749 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
750 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
751 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000752 QualType rtype;
753
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000754 if (!rhsQID) {
755 // Not comparing two ObjCQualifiedIdType's?
756 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000757
758 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000759 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000760 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000761 // If the RHS is a unqualified interface pointer "NSString*",
762 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000763 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
764 ObjCInterfaceDecl *rhsID = IT->getDecl();
Steve Naroff446ee4e2009-05-27 16:21:00 +0000765 for (ObjCQualifiedIdType::qual_iterator I = lhsQID->qual_begin(),
766 E = lhsQID->qual_end(); I != E; ++I) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000767 // when comparing an id<P> on lhs with a static type on rhs,
768 // see if static class implements all of id's protocols, directly or
769 // through its super class and categories.
Steve Naroff446ee4e2009-05-27 16:21:00 +0000770 if (!ClassImplementsProtocol(*I, rhsID, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000771 return false;
772 }
773 return true;
774 }
775 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000776 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000777
778 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000779 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000780 RHSProtoI = rhsQI->qual_begin();
781 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000782 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000783 RHSProtoI = rhsQID->qual_begin();
784 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000785 } else {
786 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000787 }
788
Steve Naroff446ee4e2009-05-27 16:21:00 +0000789 for (ObjCQualifiedIdType::qual_iterator I = lhsQID->qual_begin(),
790 E = lhsQID->qual_end(); I != E; ++I) {
791 ObjCProtocolDecl *lhsProto = *I;
Chris Lattnereca7be62008-04-07 05:30:13 +0000792 bool match = false;
793
794 // when comparing an id<P> on lhs with a static type on rhs,
795 // see if static class implements all of id's protocols, directly or
796 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000797 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
798 ObjCProtocolDecl *rhsProto = *RHSProtoI;
799 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000800 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000801 match = true;
802 break;
803 }
804 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000805 if (rhsQI) {
806 // If the RHS is a qualified interface pointer "NSString<P>*",
807 // make sure we check the class hierarchy.
808 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
809 ObjCInterfaceDecl *rhsID = IT->getDecl();
Steve Naroff446ee4e2009-05-27 16:21:00 +0000810 for (ObjCQualifiedIdType::qual_iterator I = lhsQID->qual_begin(),
811 E = lhsQID->qual_end(); I != E; ++I) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000812 // when comparing an id<P> on lhs with a static type on rhs,
813 // see if static class implements all of id's protocols, directly or
814 // through its super class and categories.
Steve Naroff446ee4e2009-05-27 16:21:00 +0000815 if (ClassImplementsProtocol(*I, rhsID, true)) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000816 match = true;
817 break;
818 }
819 }
820 }
821 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000822 if (!match)
823 return false;
824 }
825
826 return true;
827 }
828
829 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
830 assert(rhsQID && "One of the LHS/RHS should be id<x>");
831
832 if (!lhs->isPointerType())
833 return false;
834
835 QualType ltype = lhs->getAsPointerType()->getPointeeType();
836 if (const ObjCQualifiedInterfaceType *lhsQI =
837 ltype->getAsObjCQualifiedInterfaceType()) {
838 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
839 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
840 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
841 bool match = false;
842 ObjCProtocolDecl *lhsProto = *LHSProtoI;
Steve Naroff446ee4e2009-05-27 16:21:00 +0000843 for (ObjCQualifiedIdType::qual_iterator I = rhsQID->qual_begin(),
844 E = rhsQID->qual_end(); I != E; ++I) {
845 ObjCProtocolDecl *rhsProto = *I;
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000846 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000847 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000848 match = true;
849 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000850 }
851 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000852 if (!match)
853 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000854 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000855 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000856 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000857
858 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
859 // for static type vs. qualified 'id' type, check that class implements
860 // all of 'id's protocols.
861 ObjCInterfaceDecl *lhsID = IT->getDecl();
Steve Naroff446ee4e2009-05-27 16:21:00 +0000862 for (ObjCQualifiedIdType::qual_iterator I = rhsQID->qual_begin(),
863 E = rhsQID->qual_end(); I != E; ++I) {
864 if (!ClassImplementsProtocol(*I, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000865 return false;
866 }
867 return true;
868 }
869 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000870}
871