blob: 141cd80bff679e7f11055ea8edd2f28a82f45498 [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.
248 if (!Method) {
249 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
250 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000251 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000252 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000253 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000254
255 // Before we give up, check if the selector is an instance method.
256 // But only in the root. This matches gcc's behaviour and what the
257 // runtime expects.
258 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000259 Method = ClassDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000260 // Look through local category implementations associated
261 // with the root class.
262 if (!Method)
263 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
264 }
265
266 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000267 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000268 return Method;
269}
270
271ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
272 ObjCInterfaceDecl *ClassDecl) {
273 ObjCMethodDecl *Method = 0;
274 while (ClassDecl && !Method) {
275 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000276 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000277 Method = ImpDecl->getInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000278
279 // Look through local category implementations associated with the class.
280 if (!Method) {
281 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
282 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000283 Method = ObjCCategoryImpls[i]->getInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000284 }
285 }
286 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000287 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000288 return Method;
289}
290
Steve Naroff61f72cb2009-03-09 21:12:44 +0000291Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
292 IdentifierInfo &receiverName,
293 IdentifierInfo &propertyName,
294 SourceLocation &receiverNameLoc,
295 SourceLocation &propertyNameLoc) {
296
297 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
298
299 // Search for a declared property first.
300
301 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000303
304 // If this reference is in an @implementation, check for 'private' methods.
305 if (!Getter)
306 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
307 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000308 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000309 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000310
311 if (Getter) {
312 // FIXME: refactor/share with ActOnMemberReference().
313 // Check if we can reference this property.
314 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
315 return ExprError();
316 }
317
318 // Look for the matching setter, in case it is needed.
Steve Narofffdc92b72009-03-10 17:24:38 +0000319 Selector SetterSel =
320 SelectorTable::constructSetterName(PP.getIdentifierTable(),
321 PP.getSelectorTable(), &propertyName);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000322
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000323 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000324 if (!Setter) {
325 // If this reference is in an @implementation, also check for 'private'
326 // methods.
327 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
328 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000329 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000330 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000331 }
332 // Look through local category implementations associated with the class.
333 if (!Setter) {
334 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) {
335 if (ObjCCategoryImpls[i]->getClassInterface() == IFace)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000336 Setter = ObjCCategoryImpls[i]->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000337 }
338 }
339
340 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
341 return ExprError();
342
343 if (Getter || Setter) {
344 QualType PType;
345
346 if (Getter)
347 PType = Getter->getResultType();
348 else {
349 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
350 E = Setter->param_end(); PI != E; ++PI)
351 PType = (*PI)->getType();
352 }
353 return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter,
354 propertyNameLoc, IFace, receiverNameLoc));
355 }
356 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
357 << &propertyName << Context.getObjCInterfaceType(IFace));
358}
359
360
Chris Lattner85a932e2008-01-04 22:32:30 +0000361// ActOnClassMessage - used for both unary and keyword messages.
362// ArgExprs is optional - if it is present, the number of expressions
363// is obtained from Sel.getNumArgs().
364Sema::ExprResult Sema::ActOnClassMessage(
365 Scope *S,
366 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000367 SourceLocation lbrac, SourceLocation receiverLoc,
368 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000369 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000370{
371 assert(receiverName && "missing receiver class name");
372
373 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000374 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000375 bool isSuper = false;
376
Chris Lattner84692652008-11-20 05:35:30 +0000377 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000378 if (getCurMethodDecl()) {
379 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000380 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
381 if (!OID)
382 return Diag(lbrac, diag::error_no_super_class_message)
383 << getCurMethodDecl()->getDeclName();
384 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000385 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000386 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000387 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000388 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000389 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000390 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
391 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000392 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000393 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
394 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000395 }
396 // We are sending a message to 'super' within a class method. Do nothing,
397 // the receiver will pass through as 'super' (how convenient:-).
398 } else {
399 // 'super' has been used outside a method context. If a variable named
400 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000401 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000402 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
403 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000404 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
405 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000406 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000407 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
408 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000409 }
Chris Lattner08631c52008-11-23 21:45:46 +0000410 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000411 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000412 } else
413 ClassDecl = getObjCInterfaceDecl(receiverName);
414
Steve Naroff7c778f12008-07-25 19:39:00 +0000415 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000416 //
417 // typedef XCElementDisplayRect XCElementGraphicsRect;
418 //
419 // @implementation XCRASlice
420 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
421 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
422 // }
423 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000424 // If necessary, the following lookup could move to getObjCInterfaceDecl().
425 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000426 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000427 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
428 const ObjCInterfaceType *OCIT;
429 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Chris Lattner64540d72009-03-29 05:01:10 +0000430 if (!OCIT) {
431 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
432 return true;
433 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000434 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000435 }
436 }
437 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000438 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000439 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000440 if (ClassDecl->isForwardDecl()) {
441 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000442 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000443 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
444 if (Method)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000445 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
446 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000447 }
448 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000449 Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroff7c778f12008-07-25 19:39:00 +0000450
451 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000452 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000453 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000454
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000455 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
456 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000457
Chris Lattner077bf5e2008-11-24 03:33:13 +0000458 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000459 lbrac, rbrac, returnType))
460 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000461
Anders Carlsson187ba152009-05-26 15:22:25 +0000462 returnType = returnType.getNonReferenceType();
463
Mike Stump390b4cc2009-05-16 07:39:55 +0000464 // If we have the ObjCInterfaceDecl* for the class that is receiving the
465 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
466 // IdentifierInfo* for the class.
467 // FIXME: need to do a better job handling 'super' usage within a class. For
468 // now, we simply pass the "super" identifier through (which isn't consistent
469 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000470 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000471 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
472 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000473 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000474 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
475 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000476}
477
478// ActOnInstanceMessage - used for both unary and keyword messages.
479// ArgExprs is optional - if it is present, the number of expressions
480// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000481Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000482 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000483 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000484 SourceLocation rbrac,
485 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000486 assert(receiver && "missing receiver expression");
487
488 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
489 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattnerd0d45992009-04-29 05:48:32 +0000490
491 // If necessary, apply function/array conversion to the receiver.
492 // C99 6.7.5.3p[7,8].
493 DefaultFunctionArrayConversion(RExpr);
494
Chris Lattner85a932e2008-01-04 22:32:30 +0000495 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000496 QualType ReceiverCType =
497 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000498
499 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000500 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000501 ObjCMethodDecl *Method = 0;
502 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
503 // If we have an interface in scope, check 'super' methods.
504 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000505 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000506 Method = SuperDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000507
508 if (!Method)
509 // If we have implementations in scope, check "private" methods.
510 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
511 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000512 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000513
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000514 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
515 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000516
Chris Lattner077bf5e2008-11-24 03:33:13 +0000517 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000518 lbrac, rbrac, returnType))
519 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000520
521 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000522 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
523 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000524 }
525
Steve Naroff14108da2009-07-10 23:34:53 +0000526 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000527 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000528 ReceiverCType->isBlockPointerType() ||
529 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000530 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
531 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000532 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000534 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000535 lbrac, rbrac, returnType))
536 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000537 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000538 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
539 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000540 }
541
542 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000543 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000544 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000545
Chris Lattner6562fda2008-07-21 06:44:27 +0000546 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000547 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
548 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000549 Method = ClassDecl->lookupClassMethod(Sel);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000550
Steve Narofff1afaf62009-02-26 15:55:06 +0000551 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000552 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000553 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000554 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
555 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000556 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000557 if (!Method) {
558 // If not messaging 'self', look for any factory method named 'Sel'.
559 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000560 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000561 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000562 // If no class (factory) method was found, check if an _instance_
563 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000564 Method = LookupInstanceMethodInGlobalPool(
565 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000566 if (Method)
567 if (const ObjCInterfaceDecl *ID =
568 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
569 if (ID->getSuperClass())
570 Diag(lbrac, diag::warn_root_inst_method_not_found)
571 << Sel << SourceRange(lbrac, rbrac);
572 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000573 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000574 }
575 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000576 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000577 lbrac, rbrac, returnType))
578 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000579 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000580 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
581 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000582 }
583
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000584 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000585 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000586
587 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
588 // long as one of the protocols implements the selector (if not, warn).
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000589 if (const ObjCObjectPointerType *QIdTy =
590 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000591 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000592 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000593 E = QIdTy->qual_end(); I != E; ++I) {
594 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000595 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000596 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000597 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000598 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000599 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000600 }
Steve Naroff14108da2009-07-10 23:34:53 +0000601 } else if (const ObjCObjectPointerType *OCIType =
602 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000603 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000604
Steve Naroff14108da2009-07-10 23:34:53 +0000605 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000606 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000607 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000608 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000609 Method = ClassDecl->lookupInstanceMethod(Sel);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000610
611 if (!Method) {
612 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000613 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000614 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000615 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000616 break;
617 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000618 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000619 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000620 // If we have implementations in scope, check "private" methods.
621 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
622
623 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000624 // If we still haven't found a method, look in the global pool. This
625 // behavior isn't very desirable, however we need it for GCC
626 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000627 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000628 Method = LookupInstanceMethodInGlobalPool(
629 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000630 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000631 Diag(lbrac, diag::warn_maynot_respond)
Steve Naroff14108da2009-07-10 23:34:53 +0000632 << OCIType->getInterfaceDecl()->getIdentifier()->getName() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000633 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000634 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000635 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000636 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
637 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000638 } else if (!Context.getObjCIdType().isNull() &&
639 (ReceiverCType->isPointerType() ||
640 (ReceiverCType->isIntegerType() &&
641 ReceiverCType->isScalarType()))) {
642 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000643 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000644 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000645 ImpCastExprToType(RExpr, Context.getObjCIdType());
646 } else {
647 // Reject other random receiver types (e.g. structs).
648 Diag(lbrac, diag::err_bad_receiver_type)
649 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000650 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000651 }
652
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000653 if (Method)
654 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000655 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000656 lbrac, rbrac, returnType))
657 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000658 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000659 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
660 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000661}
Chris Lattnereca7be62008-04-07 05:30:13 +0000662
663//===----------------------------------------------------------------------===//
664// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
665//===----------------------------------------------------------------------===//
666
667/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
668/// inheritance hierarchy of 'rProto'.
669static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
670 ObjCProtocolDecl *rProto) {
671 if (lProto == rProto)
672 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000673 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
674 E = rProto->protocol_end(); PI != E; ++PI)
675 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000676 return true;
677 return false;
678}
679
680/// ClassImplementsProtocol - Checks that 'lProto' protocol
681/// has been implemented in IDecl class, its super class or categories (if
682/// lookupCategory is true).
683static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
684 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000685 bool lookupCategory,
686 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000687
688 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000689 const ObjCList<ObjCProtocolDecl> &Protocols =
690 IDecl->getReferencedProtocols();
691
692 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
693 E = Protocols.end(); PI != E; ++PI) {
694 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000695 return true;
Mike Stump390b4cc2009-05-16 07:39:55 +0000696 // This is dubious and is added to be compatible with gcc. In gcc, it is
697 // also allowed assigning a protocol-qualified 'id' type to a LHS object
698 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
699 // object. This IMO, should be a bug.
700 // FIXME: Treat this as an extension, and flag this as an error when GCC
701 // extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000702 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000703 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000704 }
705
706 // 2nd, look up the category.
707 if (lookupCategory)
708 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
709 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000710 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
711 E = CDecl->protocol_end(); PI != E; ++PI)
712 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000713 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000714 }
715
716 // 3rd, look up the super class(s)
717 if (IDecl->getSuperClass())
718 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000719 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
720 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000721
722 return false;
723}
724
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000725/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
726/// return true if lhs's protocols conform to rhs's protocol; false
727/// otherwise.
728bool Sema::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
729 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
730 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
731 return false;
732}
733
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000734/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
735/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000736/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000737bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
738 bool compare) {
739 // Allow id<P..> and an 'id' or void* type in all cases.
Steve Naroff14108da2009-07-10 23:34:53 +0000740 if (lhs->isVoidPointerType() ||
741 lhs->isObjCIdType() || lhs->isObjCClassType())
742 return true;
743 else if (rhs->isVoidPointerType() ||
744 rhs->isObjCIdType() || rhs->isObjCClassType())
745 return true;
746
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000747 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000748 const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
Steve Naroff289d9f22008-06-01 02:43:50 +0000749
Steve Naroff14108da2009-07-10 23:34:53 +0000750 if (!rhsOPT) return false;
751
752 if (rhsOPT->qual_empty()) {
753 // If the RHS is a unqualified interface pointer "NSString*",
754 // make sure we check the class hierarchy.
755 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
756 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
757 E = lhsQID->qual_end(); I != E; ++I) {
758 // when comparing an id<P> on lhs with a static type on rhs,
759 // see if static class implements all of id's protocols, directly or
760 // through its super class and categories.
761 if (!ClassImplementsProtocol(*I, rhsID, true))
762 return false;
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000763 }
Steve Naroff14108da2009-07-10 23:34:53 +0000764 }
765 // If there are no qualifiers and no interface, we have an 'id'.
766 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000767 }
Steve Naroff14108da2009-07-10 23:34:53 +0000768 // Both the right and left sides have qualifiers.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000769 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000770 E = lhsQID->qual_end(); I != E; ++I) {
771 ObjCProtocolDecl *lhsProto = *I;
Chris Lattnereca7be62008-04-07 05:30:13 +0000772 bool match = false;
773
774 // when comparing an id<P> on lhs with a static type on rhs,
775 // see if static class implements all of id's protocols, directly or
776 // through its super class and categories.
Steve Naroff14108da2009-07-10 23:34:53 +0000777 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
778 E = rhsOPT->qual_end(); J != E; ++J) {
779 ObjCProtocolDecl *rhsProto = *J;
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000780 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000781 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000782 match = true;
783 break;
784 }
785 }
Steve Naroff14108da2009-07-10 23:34:53 +0000786 // If the RHS is a qualified interface pointer "NSString<P>*",
787 // make sure we check the class hierarchy.
788 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
789 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
790 E = lhsQID->qual_end(); I != E; ++I) {
791 // when comparing an id<P> on lhs with a static type on rhs,
792 // see if static class implements all of id's protocols, directly or
793 // through its super class and categories.
794 if (ClassImplementsProtocol(*I, rhsID, true)) {
795 match = true;
796 break;
Steve Naroff289d9f22008-06-01 02:43:50 +0000797 }
798 }
799 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000800 if (!match)
801 return false;
802 }
803
804 return true;
805 }
806
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000807 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000808 assert(rhsQID && "One of the LHS/RHS should be id<x>");
Steve Naroff14108da2009-07-10 23:34:53 +0000809
810 if (const ObjCObjectPointerType *lhsOPT =
811 lhs->getAsObjCInterfacePointerType()) {
812 if (lhsOPT->qual_empty()) {
813 bool match = false;
814 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
815 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
816 E = rhsQID->qual_end(); I != E; ++I) {
817 // when comparing an id<P> on lhs with a static type on rhs,
818 // see if static class implements all of id's protocols, directly or
819 // through its super class and categories.
820 if (ClassImplementsProtocol(*I, lhsID, true)) {
821 match = true;
822 break;
823 }
824 }
825 if (!match)
826 return false;
827 }
828 return true;
829 }
830 // Both the right and left sides have qualifiers.
831 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
832 E = lhsOPT->qual_end(); I != E; ++I) {
833 ObjCProtocolDecl *lhsProto = *I;
834 bool match = false;
835
836 // when comparing an id<P> on lhs with a static type on rhs,
837 // see if static class implements all of id's protocols, directly or
838 // through its super class and categories.
839 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
840 E = rhsQID->qual_end(); J != E; ++J) {
841 ObjCProtocolDecl *rhsProto = *J;
842 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
843 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
844 match = true;
845 break;
846 }
847 }
848 if (!match)
849 return false;
850 }
851 return true;
852 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000853 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000854}
855