blob: f1869f9037192ea730d727d019514196861b7ce9 [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) {
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000133 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
134 SourceRange(LParenLoc, RParenLoc));
135 if (!Method)
136 Method = LookupFactoryMethodInGlobalPool(Sel,
137 SourceRange(LParenLoc, RParenLoc));
138 if (!Method)
139 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
140
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000141 QualType Ty = Context.getObjCSelType();
142 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000143}
144
145Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
146 SourceLocation AtLoc,
147 SourceLocation ProtoLoc,
148 SourceLocation LParenLoc,
149 SourceLocation RParenLoc) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000150 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattner85a932e2008-01-04 22:32:30 +0000151 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000152 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000153 return true;
154 }
155
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000156 QualType Ty = Context.getObjCProtoType();
157 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000158 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000159 Ty = Context.getPointerType(Ty);
160 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000161}
162
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000163bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
164 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000165 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000166 SourceLocation lbrac, SourceLocation rbrac,
167 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000168 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000169 // Apply default argument promotion as for (C99 6.5.2.2p6).
170 for (unsigned i = 0; i != NumArgs; i++)
171 DefaultArgumentPromotion(Args[i]);
172
Chris Lattner077bf5e2008-11-24 03:33:13 +0000173 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
174 diag::warn_inst_method_not_found;
175 Diag(lbrac, DiagID)
176 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000177 ReturnType = Context.getObjCIdType();
178 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000179 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000180
181 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000182
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000183 unsigned NumNamedArgs = Sel.getNumArgs();
184 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
185
Chris Lattner312531a2009-04-12 08:11:20 +0000186 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000187 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000188 Expr *argExpr = Args[i];
189 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
190
Chris Lattner89951a82009-02-20 18:43:26 +0000191 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000192 QualType rhsType = argExpr->getType();
193
194 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000195 if (lhsType->isArrayType())
196 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000197 else if (lhsType->isFunctionType())
198 lhsType = Context.getPointerType(lhsType);
199
Chris Lattner987798a2008-04-02 17:17:33 +0000200 AssignConvertType Result =
201 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000202 if (Args[i] != argExpr) // The expression was converted.
203 Args[i] = argExpr; // Make sure we store the converted expression.
204
Chris Lattner312531a2009-04-12 08:11:20 +0000205 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000206 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
207 argExpr, "sending");
208 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000209
210 // Promote additional arguments to variadic methods.
211 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000212 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000213 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000214 } else {
215 // Check for extra arguments to non-variadic methods.
216 if (NumArgs != NumNamedArgs) {
217 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000218 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000219 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 << SourceRange(Args[NumNamedArgs]->getLocStart(),
221 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000222 }
223 }
224
Chris Lattner312531a2009-04-12 08:11:20 +0000225 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000226}
227
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000228bool Sema::isSelfExpr(Expr *RExpr) {
229 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
230 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
231 return true;
232 return false;
233}
234
Steve Narofff1afaf62009-02-26 15:55:06 +0000235// Helper method for ActOnClassMethod/ActOnInstanceMethod.
236// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000237// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000238// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000239ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000240 ObjCInterfaceDecl *ClassDecl) {
241 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000242 // lookup in class and all superclasses
243 while (ClassDecl && !Method) {
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000244 if (ObjCImplementationDecl *ImpDecl
245 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000246 Method = ImpDecl->getClassMethod(Context, Sel);
Steve Narofff1afaf62009-02-26 15:55:06 +0000247
Steve Naroff5609ec02009-03-08 18:56:13 +0000248 // Look through local category implementations associated with the class.
249 if (!Method) {
250 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
251 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
Douglas Gregor653f1b12009-04-23 01:02:12 +0000252 Method = ObjCCategoryImpls[i]->getClassMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000253 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000254 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000255
256 // Before we give up, check if the selector is an instance method.
257 // But only in the root. This matches gcc's behaviour and what the
258 // runtime expects.
259 if (!Method && !ClassDecl->getSuperClass()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000260 Method = ClassDecl->lookupInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000261 // Look through local category implementations associated
262 // with the root class.
263 if (!Method)
264 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
265 }
266
267 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000268 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000269 return Method;
270}
271
272ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
273 ObjCInterfaceDecl *ClassDecl) {
274 ObjCMethodDecl *Method = 0;
275 while (ClassDecl && !Method) {
276 // If we have implementations in scope, check "private" methods.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000277 if (ObjCImplementationDecl *ImpDecl
278 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000279 Method = ImpDecl->getInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000280
281 // Look through local category implementations associated with the class.
282 if (!Method) {
283 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
284 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
Douglas Gregor653f1b12009-04-23 01:02:12 +0000285 Method = ObjCCategoryImpls[i]->getInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000286 }
287 }
288 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000289 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000290 return Method;
291}
292
Steve Naroff61f72cb2009-03-09 21:12:44 +0000293Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
294 IdentifierInfo &receiverName,
295 IdentifierInfo &propertyName,
296 SourceLocation &receiverNameLoc,
297 SourceLocation &propertyNameLoc) {
298
299 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
300
301 // Search for a declared property first.
302
303 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000304 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Context, Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000305
306 // If this reference is in an @implementation, check for 'private' methods.
307 if (!Getter)
308 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
309 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000310 if (ObjCImplementationDecl *ImpDecl
311 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000312 Getter = ImpDecl->getClassMethod(Context, Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000313
314 if (Getter) {
315 // FIXME: refactor/share with ActOnMemberReference().
316 // Check if we can reference this property.
317 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
318 return ExprError();
319 }
320
321 // Look for the matching setter, in case it is needed.
Steve Narofffdc92b72009-03-10 17:24:38 +0000322 Selector SetterSel =
323 SelectorTable::constructSetterName(PP.getIdentifierTable(),
324 PP.getSelectorTable(), &propertyName);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000325
Douglas Gregor6ab35242009-04-09 21:40:53 +0000326 ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000327 if (!Setter) {
328 // If this reference is in an @implementation, also check for 'private'
329 // methods.
330 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
331 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000332 if (ObjCImplementationDecl *ImpDecl
333 = LookupObjCImplementation(ClassDecl->getIdentifier()))
Douglas Gregor653f1b12009-04-23 01:02:12 +0000334 Setter = ImpDecl->getClassMethod(Context, SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000335 }
336 // Look through local category implementations associated with the class.
337 if (!Setter) {
338 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) {
339 if (ObjCCategoryImpls[i]->getClassInterface() == IFace)
Douglas Gregor653f1b12009-04-23 01:02:12 +0000340 Setter = ObjCCategoryImpls[i]->getClassMethod(Context, SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000341 }
342 }
343
344 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
345 return ExprError();
346
347 if (Getter || Setter) {
348 QualType PType;
349
350 if (Getter)
351 PType = Getter->getResultType();
352 else {
353 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
354 E = Setter->param_end(); PI != E; ++PI)
355 PType = (*PI)->getType();
356 }
357 return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter,
358 propertyNameLoc, IFace, receiverNameLoc));
359 }
360 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
361 << &propertyName << Context.getObjCInterfaceType(IFace));
362}
363
364
Chris Lattner85a932e2008-01-04 22:32:30 +0000365// ActOnClassMessage - used for both unary and keyword messages.
366// ArgExprs is optional - if it is present, the number of expressions
367// is obtained from Sel.getNumArgs().
368Sema::ExprResult Sema::ActOnClassMessage(
369 Scope *S,
370 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000371 SourceLocation lbrac, SourceLocation receiverLoc,
372 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000373 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000374{
375 assert(receiverName && "missing receiver class name");
376
377 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000379 bool isSuper = false;
380
Chris Lattner84692652008-11-20 05:35:30 +0000381 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000382 if (getCurMethodDecl()) {
383 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000384 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
385 if (!OID)
386 return Diag(lbrac, diag::error_no_super_class_message)
387 << getCurMethodDecl()->getDeclName();
388 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000389 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000390 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000391 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000392 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
393 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000394 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
395 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000396 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000397 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
398 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000399 }
400 // We are sending a message to 'super' within a class method. Do nothing,
401 // the receiver will pass through as 'super' (how convenient:-).
402 } else {
403 // 'super' has been used outside a method context. If a variable named
404 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000405 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000406 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
407 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000408 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
409 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000410 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000411 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
412 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000413 }
Chris Lattner08631c52008-11-23 21:45:46 +0000414 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000415 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000416 } else
417 ClassDecl = getObjCInterfaceDecl(receiverName);
418
Steve Naroff7c778f12008-07-25 19:39:00 +0000419 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000420 //
421 // typedef XCElementDisplayRect XCElementGraphicsRect;
422 //
423 // @implementation XCRASlice
424 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
425 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
426 // }
427 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000428 // If necessary, the following lookup could move to getObjCInterfaceDecl().
429 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000430 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000431 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
432 const ObjCInterfaceType *OCIT;
433 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Chris Lattner64540d72009-03-29 05:01:10 +0000434 if (!OCIT) {
435 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
436 return true;
437 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000438 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000439 }
440 }
441 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000442 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000443 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000444 if (ClassDecl->isForwardDecl()) {
445 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000446 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000447 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
448 if (Method)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000449 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
450 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000451 }
452 if (!Method)
453 Method = ClassDecl->lookupClassMethod(Context, Sel);
Steve Naroff7c778f12008-07-25 19:39:00 +0000454
455 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000456 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000457 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000458
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000459 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
460 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000461
Chris Lattner077bf5e2008-11-24 03:33:13 +0000462 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000463 lbrac, rbrac, returnType))
464 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000465
Anders Carlsson187ba152009-05-26 15:22:25 +0000466 returnType = returnType.getNonReferenceType();
467
Mike Stump390b4cc2009-05-16 07:39:55 +0000468 // If we have the ObjCInterfaceDecl* for the class that is receiving the
469 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
470 // IdentifierInfo* for the class.
471 // FIXME: need to do a better job handling 'super' usage within a class. For
472 // now, we simply pass the "super" identifier through (which isn't consistent
473 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000474 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000475 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
476 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000477 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000478 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
479 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000480}
481
482// ActOnInstanceMessage - used for both unary and keyword messages.
483// ArgExprs is optional - if it is present, the number of expressions
484// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000485Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000486 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000487 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000488 SourceLocation rbrac,
489 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000490 assert(receiver && "missing receiver expression");
491
492 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
493 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattnerd0d45992009-04-29 05:48:32 +0000494
495 // If necessary, apply function/array conversion to the receiver.
496 // C99 6.7.5.3p[7,8].
497 DefaultFunctionArrayConversion(RExpr);
498
Chris Lattner85a932e2008-01-04 22:32:30 +0000499 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000500 QualType ReceiverCType =
501 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000502
503 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000504 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000505 ObjCMethodDecl *Method = 0;
506 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
507 // If we have an interface in scope, check 'super' methods.
508 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000509 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000510 Method = SuperDecl->lookupInstanceMethod(Context, Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000511
512 if (!Method)
513 // If we have implementations in scope, check "private" methods.
514 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
515 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000516 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000517
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000518 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
519 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000520
Chris Lattner077bf5e2008-11-24 03:33:13 +0000521 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000522 lbrac, rbrac, returnType))
523 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000524
525 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000526 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
527 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000528 }
529
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000530 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000531 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000532 ReceiverCType->isBlockPointerType() ||
533 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000534 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
535 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000536 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000537 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000538 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000539 lbrac, rbrac, returnType))
540 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000541 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000542 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
543 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000544 }
545
546 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000547 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000548 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000549
Chris Lattner6562fda2008-07-21 06:44:27 +0000550 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000551 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
552 // First check the public methods in the class interface.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000553 Method = ClassDecl->lookupClassMethod(Context, Sel);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000554
Steve Narofff1afaf62009-02-26 15:55:06 +0000555 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000556 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000557 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000558 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
559 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000560 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000561 if (!Method) {
562 // If not messaging 'self', look for any factory method named 'Sel'.
563 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000564 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000565 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000566 // If no class (factory) method was found, check if an _instance_
567 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000568 Method = LookupInstanceMethodInGlobalPool(
569 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000570 if (Method)
571 if (const ObjCInterfaceDecl *ID =
572 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
573 if (ID->getSuperClass())
574 Diag(lbrac, diag::warn_root_inst_method_not_found)
575 << Sel << SourceRange(lbrac, rbrac);
576 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000577 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000578 }
579 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000580 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000581 lbrac, rbrac, returnType))
582 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000583 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000584 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
585 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000586 }
587
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000588 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000589 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000590
591 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
592 // long as one of the protocols implements the selector (if not, warn).
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000593 if (const ObjCObjectPointerType *QIdTy =
594 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000595 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000596 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000597 E = QIdTy->qual_end(); I != E; ++I) {
598 ObjCProtocolDecl *PDecl = *I;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000599 if (PDecl && (Method = PDecl->lookupInstanceMethod(Context, Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000600 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000601 // Since we aren't supporting "Class<foo>", look for a class method.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000602 if (PDecl && (Method = PDecl->lookupClassMethod(Context, Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000603 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000604 }
Steve Naroff279d8962009-02-23 15:40:48 +0000605 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000606 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000607 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000608
Steve Naroff279d8962009-02-23 15:40:48 +0000609 ClassDecl = OCIType->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000610 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000611 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000612 // The idea is to add class info to InstanceMethodPool.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000613 Method = ClassDecl->lookupInstanceMethod(Context, Sel);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000614
615 if (!Method) {
616 // Search protocol qualifiers.
Steve Naroff279d8962009-02-23 15:40:48 +0000617 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
618 E = OCIType->qual_end(); QI != E; ++QI) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000619 if ((Method = (*QI)->lookupInstanceMethod(Context, Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000620 break;
621 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000622 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000623 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000624 // If we have implementations in scope, check "private" methods.
625 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
626
627 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000628 // If we still haven't found a method, look in the global pool. This
629 // behavior isn't very desirable, however we need it for GCC
630 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000631 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000632 Method = LookupInstanceMethodInGlobalPool(
633 Sel, SourceRange(lbrac,rbrac));
634 if (Method && !OCIType->getDecl()->isForwardDecl())
635 Diag(lbrac, diag::warn_maynot_respond)
636 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
637 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000638 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000639 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000640 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
641 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000642 } else if (!Context.getObjCIdType().isNull() &&
643 (ReceiverCType->isPointerType() ||
644 (ReceiverCType->isIntegerType() &&
645 ReceiverCType->isScalarType()))) {
646 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000647 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000648 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000649 ImpCastExprToType(RExpr, Context.getObjCIdType());
650 } else {
651 // Reject other random receiver types (e.g. structs).
652 Diag(lbrac, diag::err_bad_receiver_type)
653 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000654 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000655 }
656
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000657 if (Method)
658 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000659 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000660 lbrac, rbrac, returnType))
661 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000662 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000663 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
664 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000665}
Chris Lattnereca7be62008-04-07 05:30:13 +0000666
667//===----------------------------------------------------------------------===//
668// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
669//===----------------------------------------------------------------------===//
670
671/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
672/// inheritance hierarchy of 'rProto'.
673static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
674 ObjCProtocolDecl *rProto) {
675 if (lProto == rProto)
676 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000677 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
678 E = rProto->protocol_end(); PI != E; ++PI)
679 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000680 return true;
681 return false;
682}
683
684/// ClassImplementsProtocol - Checks that 'lProto' protocol
685/// has been implemented in IDecl class, its super class or categories (if
686/// lookupCategory is true).
687static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
688 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000689 bool lookupCategory,
690 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000691
692 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000693 const ObjCList<ObjCProtocolDecl> &Protocols =
694 IDecl->getReferencedProtocols();
695
696 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
697 E = Protocols.end(); PI != E; ++PI) {
698 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000699 return true;
Mike Stump390b4cc2009-05-16 07:39:55 +0000700 // This is dubious and is added to be compatible with gcc. In gcc, it is
701 // also allowed assigning a protocol-qualified 'id' type to a LHS object
702 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
703 // object. This IMO, should be a bug.
704 // FIXME: Treat this as an extension, and flag this as an error when GCC
705 // extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000706 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000707 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000708 }
709
710 // 2nd, look up the category.
711 if (lookupCategory)
712 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
713 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000714 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
715 E = CDecl->protocol_end(); PI != E; ++PI)
716 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000717 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000718 }
719
720 // 3rd, look up the super class(s)
721 if (IDecl->getSuperClass())
722 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000723 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
724 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000725
726 return false;
727}
728
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000729/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
730/// return true if lhs's protocols conform to rhs's protocol; false
731/// otherwise.
732bool Sema::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
733 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
734 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
735 return false;
736}
737
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000738/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
739/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000740/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000741bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
742 bool compare) {
743 // Allow id<P..> and an 'id' or void* type in all cases.
744 if (const PointerType *PT = lhs->getAsPointerType()) {
745 QualType PointeeTy = PT->getPointeeType();
Chris Lattner2c4463f2009-04-12 09:02:39 +0000746 if (PointeeTy->isVoidType() ||
747 Context.isObjCIdStructType(PointeeTy) ||
748 Context.isObjCClassStructType(PointeeTy))
Chris Lattnereca7be62008-04-07 05:30:13 +0000749 return true;
750 } else if (const PointerType *PT = rhs->getAsPointerType()) {
751 QualType PointeeTy = PT->getPointeeType();
Chris Lattner2c4463f2009-04-12 09:02:39 +0000752 if (PointeeTy->isVoidType() ||
753 Context.isObjCIdStructType(PointeeTy) ||
754 Context.isObjCClassStructType(PointeeTy))
Chris Lattnereca7be62008-04-07 05:30:13 +0000755 return true;
756 }
757
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000758 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
759 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000760 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000761 QualType rtype;
762
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000763 if (!rhsQID) {
764 // Not comparing two ObjCQualifiedIdType's?
765 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000766
767 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000768 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000769 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000770 // If the RHS is a unqualified interface pointer "NSString*",
771 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000772 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
773 ObjCInterfaceDecl *rhsID = IT->getDecl();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000774 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000775 E = lhsQID->qual_end(); I != E; ++I) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000776 // when comparing an id<P> on lhs with a static type on rhs,
777 // see if static class implements all of id's protocols, directly or
778 // through its super class and categories.
Steve Naroff446ee4e2009-05-27 16:21:00 +0000779 if (!ClassImplementsProtocol(*I, rhsID, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000780 return false;
781 }
782 return true;
783 }
784 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000785 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000786
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000787 ObjCObjectPointerType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000788 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000789 RHSProtoI = rhsQI->qual_begin();
790 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000791 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000792 RHSProtoI = rhsQID->qual_begin();
793 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000794 } else {
795 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000796 }
797
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000798 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000799 E = lhsQID->qual_end(); I != E; ++I) {
800 ObjCProtocolDecl *lhsProto = *I;
Chris Lattnereca7be62008-04-07 05:30:13 +0000801 bool match = false;
802
803 // when comparing an id<P> on lhs with a static type on rhs,
804 // see if static class implements all of id's protocols, directly or
805 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000806 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
807 ObjCProtocolDecl *rhsProto = *RHSProtoI;
808 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000809 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000810 match = true;
811 break;
812 }
813 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000814 if (rhsQI) {
815 // If the RHS is a qualified interface pointer "NSString<P>*",
816 // make sure we check the class hierarchy.
817 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
818 ObjCInterfaceDecl *rhsID = IT->getDecl();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000819 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000820 E = lhsQID->qual_end(); I != E; ++I) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000821 // when comparing an id<P> on lhs with a static type on rhs,
822 // see if static class implements all of id's protocols, directly or
823 // through its super class and categories.
Steve Naroff446ee4e2009-05-27 16:21:00 +0000824 if (ClassImplementsProtocol(*I, rhsID, true)) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000825 match = true;
826 break;
827 }
828 }
829 }
830 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000831 if (!match)
832 return false;
833 }
834
835 return true;
836 }
837
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000838 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000839 assert(rhsQID && "One of the LHS/RHS should be id<x>");
840
841 if (!lhs->isPointerType())
842 return false;
843
844 QualType ltype = lhs->getAsPointerType()->getPointeeType();
845 if (const ObjCQualifiedInterfaceType *lhsQI =
846 ltype->getAsObjCQualifiedInterfaceType()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000847 ObjCObjectPointerType::qual_iterator LHSProtoI = lhsQI->qual_begin();
848 ObjCObjectPointerType::qual_iterator LHSProtoE = lhsQI->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000849 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
850 bool match = false;
851 ObjCProtocolDecl *lhsProto = *LHSProtoI;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000852 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000853 E = rhsQID->qual_end(); I != E; ++I) {
854 ObjCProtocolDecl *rhsProto = *I;
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000855 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000856 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000857 match = true;
858 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000859 }
860 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000861 if (!match)
862 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000863 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000864 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000865 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000866
867 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
868 // for static type vs. qualified 'id' type, check that class implements
869 // all of 'id's protocols.
870 ObjCInterfaceDecl *lhsID = IT->getDecl();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000871 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000872 E = rhsQID->qual_end(); I != E; ++I) {
873 if (!ClassImplementsProtocol(*I, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000874 return false;
875 }
876 return true;
877 }
878 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000879}
880