blob: 2d144100e5d0186111061562b76eee22b759a04a [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()) {
Steve Naroff14108da2009-07-10 23:34:53 +000077 Ty = Context.getObjCObjectPointerType(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();
Steve Naroff14108da2009-07-10 23:34:53 +000084 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000085 } 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;
Steve Naroff14108da2009-07-10 23:34:53 +0000159 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000160 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) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000244 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000245 Method = ImpDecl->getClassMethod(Sel);
Steve Narofff1afaf62009-02-26 15:55:06 +0000246
Steve Naroff5609ec02009-03-08 18:56:13 +0000247 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000248 if (!Method)
249 Method = ClassDecl->getCategoryClassMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000250
251 // Before we give up, check if the selector is an instance method.
252 // But only in the root. This matches gcc's behaviour and what the
253 // runtime expects.
254 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000256 // Look through local category implementations associated
257 // with the root class.
258 if (!Method)
259 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
260 }
261
262 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000263 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000264 return Method;
265}
266
267ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
268 ObjCInterfaceDecl *ClassDecl) {
269 ObjCMethodDecl *Method = 0;
270 while (ClassDecl && !Method) {
271 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000272 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000273 Method = ImpDecl->getInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000274
275 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000276 if (!Method)
277 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000278 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000279 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000280 return Method;
281}
282
Steve Naroff61f72cb2009-03-09 21:12:44 +0000283Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
284 IdentifierInfo &receiverName,
285 IdentifierInfo &propertyName,
286 SourceLocation &receiverNameLoc,
287 SourceLocation &propertyNameLoc) {
288
289 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
290
291 // Search for a declared property first.
292
293 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000294 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000295
296 // If this reference is in an @implementation, check for 'private' methods.
297 if (!Getter)
298 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
299 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000300 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000301 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000302
303 if (Getter) {
304 // FIXME: refactor/share with ActOnMemberReference().
305 // Check if we can reference this property.
306 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
307 return ExprError();
308 }
309
310 // Look for the matching setter, in case it is needed.
Steve Narofffdc92b72009-03-10 17:24:38 +0000311 Selector SetterSel =
312 SelectorTable::constructSetterName(PP.getIdentifierTable(),
313 PP.getSelectorTable(), &propertyName);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000314
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000315 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000316 if (!Setter) {
317 // If this reference is in an @implementation, also check for 'private'
318 // methods.
319 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
320 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000321 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000322 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000323 }
324 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000325 if (!Setter)
326 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000327
328 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
329 return ExprError();
330
331 if (Getter || Setter) {
332 QualType PType;
333
334 if (Getter)
335 PType = Getter->getResultType();
336 else {
337 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
338 E = Setter->param_end(); PI != E; ++PI)
339 PType = (*PI)->getType();
340 }
341 return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter,
342 propertyNameLoc, IFace, receiverNameLoc));
343 }
344 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
345 << &propertyName << Context.getObjCInterfaceType(IFace));
346}
347
348
Chris Lattner85a932e2008-01-04 22:32:30 +0000349// ActOnClassMessage - used for both unary and keyword messages.
350// ArgExprs is optional - if it is present, the number of expressions
351// is obtained from Sel.getNumArgs().
352Sema::ExprResult Sema::ActOnClassMessage(
353 Scope *S,
354 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000355 SourceLocation lbrac, SourceLocation receiverLoc,
356 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000357 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000358{
359 assert(receiverName && "missing receiver class name");
360
361 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000362 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000363 bool isSuper = false;
364
Chris Lattner84692652008-11-20 05:35:30 +0000365 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000366 if (getCurMethodDecl()) {
367 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000368 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
369 if (!OID)
370 return Diag(lbrac, diag::error_no_super_class_message)
371 << getCurMethodDecl()->getDeclName();
372 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000373 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000374 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000375 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000376 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000377 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000378 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
379 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000380 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000381 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
382 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000383 }
384 // We are sending a message to 'super' within a class method. Do nothing,
385 // the receiver will pass through as 'super' (how convenient:-).
386 } else {
387 // 'super' has been used outside a method context. If a variable named
388 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000389 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000390 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
391 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000392 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
393 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000394 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000395 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
396 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000397 }
Chris Lattner08631c52008-11-23 21:45:46 +0000398 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000399 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000400 } else
401 ClassDecl = getObjCInterfaceDecl(receiverName);
402
Steve Naroff7c778f12008-07-25 19:39:00 +0000403 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000404 //
405 // typedef XCElementDisplayRect XCElementGraphicsRect;
406 //
407 // @implementation XCRASlice
408 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
409 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
410 // }
411 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000412 // If necessary, the following lookup could move to getObjCInterfaceDecl().
413 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000414 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000415 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
416 const ObjCInterfaceType *OCIT;
417 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Chris Lattner64540d72009-03-29 05:01:10 +0000418 if (!OCIT) {
419 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
420 return true;
421 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000422 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000423 }
424 }
425 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000426 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000427 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000428 if (ClassDecl->isForwardDecl()) {
429 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000430 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000431 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
432 if (Method)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000433 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
434 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000435 }
436 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000437 Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroff7c778f12008-07-25 19:39:00 +0000438
439 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000440 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000441 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000442
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000443 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
444 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000445
Chris Lattner077bf5e2008-11-24 03:33:13 +0000446 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000447 lbrac, rbrac, returnType))
448 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000449
Anders Carlsson187ba152009-05-26 15:22:25 +0000450 returnType = returnType.getNonReferenceType();
451
Mike Stump390b4cc2009-05-16 07:39:55 +0000452 // If we have the ObjCInterfaceDecl* for the class that is receiving the
453 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
454 // IdentifierInfo* for the class.
455 // FIXME: need to do a better job handling 'super' usage within a class. For
456 // now, we simply pass the "super" identifier through (which isn't consistent
457 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000458 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000459 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
460 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000461 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000462 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
463 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000464}
465
466// ActOnInstanceMessage - used for both unary and keyword messages.
467// ArgExprs is optional - if it is present, the number of expressions
468// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000469Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000470 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000471 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000472 SourceLocation rbrac,
473 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000474 assert(receiver && "missing receiver expression");
475
476 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
477 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattnerd0d45992009-04-29 05:48:32 +0000478
479 // If necessary, apply function/array conversion to the receiver.
480 // C99 6.7.5.3p[7,8].
481 DefaultFunctionArrayConversion(RExpr);
482
Chris Lattner85a932e2008-01-04 22:32:30 +0000483 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000484 QualType ReceiverCType =
485 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000486
487 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000488 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000489 ObjCMethodDecl *Method = 0;
490 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
491 // If we have an interface in scope, check 'super' methods.
492 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000493 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000494 Method = SuperDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000495
496 if (!Method)
497 // If we have implementations in scope, check "private" methods.
498 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
499 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000500 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000501
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000502 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
503 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000504
Chris Lattner077bf5e2008-11-24 03:33:13 +0000505 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000506 lbrac, rbrac, returnType))
507 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000508
509 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000510 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
511 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000512 }
513
Steve Naroff14108da2009-07-10 23:34:53 +0000514 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000515 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000516 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000517 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
518 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000519 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000520 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000521 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000522 lbrac, rbrac, returnType))
523 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000524 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000525 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
526 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000527 }
528
529 // Handle messages to Class.
Steve Naroff470301b2009-07-22 16:07:01 +0000530 if (ReceiverCType->isObjCClassType() ||
531 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000532 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000533
Chris Lattner6562fda2008-07-21 06:44:27 +0000534 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000535 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
536 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000537 Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000538
Steve Narofff1afaf62009-02-26 15:55:06 +0000539 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000540 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff470301b2009-07-22 16:07:01 +0000541
542 // FIXME: if we still haven't found a method, we need to look in
543 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000544 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000545 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
546 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000547 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000548 if (!Method) {
549 // If not messaging 'self', look for any factory method named 'Sel'.
550 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000552 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000553 // If no class (factory) method was found, check if an _instance_
554 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000555 Method = LookupInstanceMethodInGlobalPool(
556 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000557 if (Method)
558 if (const ObjCInterfaceDecl *ID =
559 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
560 if (ID->getSuperClass())
561 Diag(lbrac, diag::warn_root_inst_method_not_found)
562 << Sel << SourceRange(lbrac, rbrac);
563 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000564 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000565 }
566 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000567 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000568 lbrac, rbrac, returnType))
569 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000570 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000571 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
572 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000573 }
574
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000575 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000576 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000577
578 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
579 // long as one of the protocols implements the selector (if not, warn).
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000580 if (const ObjCObjectPointerType *QIdTy =
581 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000582 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000583 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000584 E = QIdTy->qual_end(); I != E; ++I) {
585 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000586 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000587 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000588 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000589 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000590 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000591 }
Steve Naroff14108da2009-07-10 23:34:53 +0000592 } else if (const ObjCObjectPointerType *OCIType =
593 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000594 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000595
Steve Naroff14108da2009-07-10 23:34:53 +0000596 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000597 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000598 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000599 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000600 Method = ClassDecl->lookupInstanceMethod(Sel);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000601
602 if (!Method) {
603 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000604 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000605 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000606 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000607 break;
608 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000609 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000610 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000611 // If we have implementations in scope, check "private" methods.
612 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
613
614 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000615 // If we still haven't found a method, look in the global pool. This
616 // behavior isn't very desirable, however we need it for GCC
617 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000618 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000619 Method = LookupInstanceMethodInGlobalPool(
620 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000621 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000622 Diag(lbrac, diag::warn_maynot_respond)
Steve Naroff14108da2009-07-10 23:34:53 +0000623 << OCIType->getInterfaceDecl()->getIdentifier()->getName() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000624 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000625 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000626 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000627 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
628 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000629 } else if (!Context.getObjCIdType().isNull() &&
630 (ReceiverCType->isPointerType() ||
631 (ReceiverCType->isIntegerType() &&
632 ReceiverCType->isScalarType()))) {
633 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000634 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000635 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000636 ImpCastExprToType(RExpr, Context.getObjCIdType());
637 } else {
638 // Reject other random receiver types (e.g. structs).
639 Diag(lbrac, diag::err_bad_receiver_type)
640 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000641 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000642 }
643
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000644 if (Method)
645 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000646 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000647 lbrac, rbrac, returnType))
648 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000649 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000650 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
651 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000652}
Chris Lattnereca7be62008-04-07 05:30:13 +0000653
654//===----------------------------------------------------------------------===//
655// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
656//===----------------------------------------------------------------------===//
657
658/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
659/// inheritance hierarchy of 'rProto'.
660static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
661 ObjCProtocolDecl *rProto) {
662 if (lProto == rProto)
663 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000664 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
665 E = rProto->protocol_end(); PI != E; ++PI)
666 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000667 return true;
668 return false;
669}
670
671/// ClassImplementsProtocol - Checks that 'lProto' protocol
672/// has been implemented in IDecl class, its super class or categories (if
673/// lookupCategory is true).
674static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
675 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000676 bool lookupCategory,
677 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000678
679 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000680 const ObjCList<ObjCProtocolDecl> &Protocols =
681 IDecl->getReferencedProtocols();
682
683 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
684 E = Protocols.end(); PI != E; ++PI) {
685 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000686 return true;
Mike Stump390b4cc2009-05-16 07:39:55 +0000687 // This is dubious and is added to be compatible with gcc. In gcc, it is
688 // also allowed assigning a protocol-qualified 'id' type to a LHS object
689 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
690 // object. This IMO, should be a bug.
691 // FIXME: Treat this as an extension, and flag this as an error when GCC
692 // extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000693 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000694 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000695 }
696
697 // 2nd, look up the category.
698 if (lookupCategory)
699 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
700 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000701 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
702 E = CDecl->protocol_end(); PI != E; ++PI)
703 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000704 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000705 }
706
707 // 3rd, look up the super class(s)
708 if (IDecl->getSuperClass())
709 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000710 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
711 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000712
713 return false;
714}
715
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000716/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
717/// return true if lhs's protocols conform to rhs's protocol; false
718/// otherwise.
719bool Sema::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
720 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
721 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
722 return false;
723}
724
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000725/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
726/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000727/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000728bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
729 bool compare) {
730 // Allow id<P..> and an 'id' or void* type in all cases.
Steve Naroff14108da2009-07-10 23:34:53 +0000731 if (lhs->isVoidPointerType() ||
732 lhs->isObjCIdType() || lhs->isObjCClassType())
733 return true;
734 else if (rhs->isVoidPointerType() ||
735 rhs->isObjCIdType() || rhs->isObjCClassType())
736 return true;
737
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000738 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000739 const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
Steve Naroff289d9f22008-06-01 02:43:50 +0000740
Steve Naroff14108da2009-07-10 23:34:53 +0000741 if (!rhsOPT) return false;
742
743 if (rhsOPT->qual_empty()) {
744 // If the RHS is a unqualified interface pointer "NSString*",
745 // make sure we check the class hierarchy.
746 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
747 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
748 E = lhsQID->qual_end(); I != E; ++I) {
749 // when comparing an id<P> on lhs with a static type on rhs,
750 // see if static class implements all of id's protocols, directly or
751 // through its super class and categories.
752 if (!ClassImplementsProtocol(*I, rhsID, true))
753 return false;
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000754 }
Steve Naroff14108da2009-07-10 23:34:53 +0000755 }
756 // If there are no qualifiers and no interface, we have an 'id'.
757 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000758 }
Steve Naroff14108da2009-07-10 23:34:53 +0000759 // Both the right and left sides have qualifiers.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000760 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000761 E = lhsQID->qual_end(); I != E; ++I) {
762 ObjCProtocolDecl *lhsProto = *I;
Chris Lattnereca7be62008-04-07 05:30:13 +0000763 bool match = false;
764
765 // when comparing an id<P> on lhs with a static type on rhs,
766 // see if static class implements all of id's protocols, directly or
767 // through its super class and categories.
Steve Naroff14108da2009-07-10 23:34:53 +0000768 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
769 E = rhsOPT->qual_end(); J != E; ++J) {
770 ObjCProtocolDecl *rhsProto = *J;
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000771 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000772 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000773 match = true;
774 break;
775 }
776 }
Steve Naroff14108da2009-07-10 23:34:53 +0000777 // If the RHS is a qualified interface pointer "NSString<P>*",
778 // make sure we check the class hierarchy.
779 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
780 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
781 E = lhsQID->qual_end(); I != E; ++I) {
782 // when comparing an id<P> on lhs with a static type on rhs,
783 // see if static class implements all of id's protocols, directly or
784 // through its super class and categories.
785 if (ClassImplementsProtocol(*I, rhsID, true)) {
786 match = true;
787 break;
Steve Naroff289d9f22008-06-01 02:43:50 +0000788 }
789 }
790 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000791 if (!match)
792 return false;
793 }
794
795 return true;
796 }
797
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000798 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000799 assert(rhsQID && "One of the LHS/RHS should be id<x>");
Steve Naroff14108da2009-07-10 23:34:53 +0000800
801 if (const ObjCObjectPointerType *lhsOPT =
802 lhs->getAsObjCInterfacePointerType()) {
803 if (lhsOPT->qual_empty()) {
804 bool match = false;
805 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
806 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
807 E = rhsQID->qual_end(); I != E; ++I) {
808 // when comparing an id<P> on lhs with a static type on rhs,
809 // see if static class implements all of id's protocols, directly or
810 // through its super class and categories.
811 if (ClassImplementsProtocol(*I, lhsID, true)) {
812 match = true;
813 break;
814 }
815 }
816 if (!match)
817 return false;
818 }
819 return true;
820 }
821 // Both the right and left sides have qualifiers.
822 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
823 E = lhsOPT->qual_end(); I != E; ++I) {
824 ObjCProtocolDecl *lhsProto = *I;
825 bool match = false;
826
827 // when comparing an id<P> on lhs with a static type on rhs,
828 // see if static class implements all of id's protocols, directly or
829 // through its super class and categories.
830 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
831 E = rhsQID->qual_end(); J != E; ++J) {
832 ObjCProtocolDecl *rhsProto = *J;
833 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
834 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
835 match = true;
836 break;
837 }
838 }
839 if (!match)
840 return false;
841 }
842 return true;
843 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000844 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000845}
846