blob: 86d69388e228abeed112c8ef7f22ac39d2f90d58 [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"
Chris Lattner7f816522010-04-11 07:45:24 +000015#include "Lookup.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclObjC.h"
Steve Narofff494b572008-05-29 21:12:08 +000018#include "clang/AST/ExprObjC.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000019#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000020#include "clang/Lex/Preprocessor.h"
21
Chris Lattner85a932e2008-01-04 22:32:30 +000022using namespace clang;
23
Mike Stump1eb44332009-09-09 15:08:12 +000024Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner39c28bb2009-02-18 06:48:40 +000025 ExprTy **strings,
Chris Lattner85a932e2008-01-04 22:32:30 +000026 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000027 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
28
Chris Lattnerf4b136f2009-02-18 06:13:04 +000029 // Most ObjC strings are formed out of a single piece. However, we *can*
30 // have strings formed out of multiple @ strings with multiple pptokens in
31 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
32 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000033 StringLiteral *S = Strings[0];
Mike Stump1eb44332009-09-09 15:08:12 +000034
Chris Lattnerf4b136f2009-02-18 06:13:04 +000035 // If we have a multi-part string, merge it all together.
36 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000037 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000038 llvm::SmallString<128> StrBuf;
39 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump1eb44332009-09-09 15:08:12 +000040
Chris Lattner726e1682009-02-18 05:49:11 +000041 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000042 S = Strings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner39c28bb2009-02-18 06:48:40 +000044 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000045 if (S->isWide()) {
46 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
47 << S->getSourceRange();
48 return true;
49 }
Mike Stump1eb44332009-09-09 15:08:12 +000050
Chris Lattner39c28bb2009-02-18 06:48:40 +000051 // Get the string data.
52 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
Mike Stump1eb44332009-09-09 15:08:12 +000053
Chris Lattner39c28bb2009-02-18 06:48:40 +000054 // Get the locations of the string tokens.
55 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner39c28bb2009-02-18 06:48:40 +000057 // Free the temporary string.
Ted Kremenek8189cde2009-02-07 01:47:29 +000058 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000059 }
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner39c28bb2009-02-18 06:48:40 +000061 // Create the aggregate string with the appropriate content and location
62 // information.
63 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000064 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000065 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000066 }
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner69039812009-02-18 06:01:06 +000068 // Verify that this composite string is acceptable for ObjC strings.
69 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000070 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000071
72 // Initialize the constant string interface lazily. This assumes
Steve Naroffd9fd7642009-04-07 14:18:33 +000073 // the NSString interface is seen in this translation unit. Note: We
74 // don't use NSConstantString, since the runtime team considers this
75 // interface private (even though it appears in the header files).
Chris Lattnera0af1fe2009-02-18 06:06:56 +000076 QualType Ty = Context.getObjCConstantStringInterface();
77 if (!Ty.isNull()) {
Steve Naroff14108da2009-07-10 23:34:53 +000078 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000079 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000080 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
John McCallf36e02d2009-10-09 21:13:30 +000081 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000082 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
83 Context.setObjCConstantStringInterface(StrIF);
84 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +000085 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000086 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000087 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +000088 // strings as untyped objects and let the runtime figure it out later.
89 Ty = Context.getObjCIdType();
90 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000091 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattnerf4b136f2009-02-18 06:13:04 +000093 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000094}
95
Mike Stump1eb44332009-09-09 15:08:12 +000096Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Anders Carlssonfc0f0212009-06-07 18:45:35 +000097 QualType EncodedType,
98 SourceLocation RParenLoc) {
99 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000100 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000101 StrTy = Context.DependentTy;
102 else {
103 std::string Str;
104 Context.getObjCEncodingForType(EncodedType, Str);
105
106 // The type of @encode is the same as the type of the corresponding string,
107 // which is an array type.
108 StrTy = Context.CharTy;
109 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000110 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000111 StrTy.addConst();
112 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
113 ArrayType::Normal, 0);
114 }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000116 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
117}
118
Chris Lattner85a932e2008-01-04 22:32:30 +0000119Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
120 SourceLocation EncodeLoc,
121 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000122 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +0000123 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000124 // FIXME: Preserve type source info ?
125 QualType EncodedType = GetTypeFromParser(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +0000126
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000127 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000128}
129
130Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
131 SourceLocation AtLoc,
132 SourceLocation SelLoc,
133 SourceLocation LParenLoc,
134 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000135 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +0000136 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000137 if (!Method)
138 Method = LookupFactoryMethodInGlobalPool(Sel,
139 SourceRange(LParenLoc, RParenLoc));
140 if (!Method)
141 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
142
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000143 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000144 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000145}
146
147Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
148 SourceLocation AtLoc,
149 SourceLocation ProtoLoc,
150 SourceLocation LParenLoc,
151 SourceLocation RParenLoc) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000152 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattner85a932e2008-01-04 22:32:30 +0000153 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000154 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000155 return true;
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000158 QualType Ty = Context.getObjCProtoType();
159 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000160 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000161 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000162 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000163}
164
Mike Stump1eb44332009-09-09 15:08:12 +0000165bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
166 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000167 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000168 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump1eb44332009-09-09 15:08:12 +0000169 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000170 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000171 // Apply default argument promotion as for (C99 6.5.2.2p6).
172 for (unsigned i = 0; i != NumArgs; i++)
173 DefaultArgumentPromotion(Args[i]);
174
Chris Lattner077bf5e2008-11-24 03:33:13 +0000175 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
176 diag::warn_inst_method_not_found;
177 Diag(lbrac, DiagID)
178 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000179 ReturnType = Context.getObjCIdType();
180 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner077bf5e2008-11-24 03:33:13 +0000183 ReturnType = Method->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000185 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000186 // Method might have more arguments than selector indicates. This is due
187 // to addition of c-style arguments in method.
188 if (Method->param_size() > Sel.getNumArgs())
189 NumNamedArgs = Method->param_size();
190 // FIXME. This need be cleaned up.
191 if (NumArgs < NumNamedArgs) {
192 Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2;
193 return false;
194 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000195
Chris Lattner312531a2009-04-12 08:11:20 +0000196 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000197 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000198 Expr *argExpr = Args[i];
199 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattner89951a82009-02-20 18:43:26 +0000201 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000202 QualType rhsType = argExpr->getType();
203
Mike Stump1eb44332009-09-09 15:08:12 +0000204 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000205 if (lhsType->isArrayType())
206 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000207 else if (lhsType->isFunctionType())
208 lhsType = Context.getPointerType(lhsType);
209
Mike Stump1eb44332009-09-09 15:08:12 +0000210 AssignConvertType Result =
Chris Lattner987798a2008-04-02 17:17:33 +0000211 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000212 if (Args[i] != argExpr) // The expression was converted.
213 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000214
215 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000216 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor68647482009-12-16 03:45:30 +0000217 argExpr, AA_Sending);
Chris Lattner85a932e2008-01-04 22:32:30 +0000218 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000219
220 // Promote additional arguments to variadic methods.
221 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000222 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000223 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000224 } else {
225 // Check for extra arguments to non-variadic methods.
226 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000227 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000228 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000229 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000230 << SourceRange(Args[NumNamedArgs]->getLocStart(),
231 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000232 }
233 }
234
Chris Lattner312531a2009-04-12 08:11:20 +0000235 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000236}
237
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000238bool Sema::isSelfExpr(Expr *RExpr) {
239 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
240 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
241 return true;
242 return false;
243}
244
Steve Narofff1afaf62009-02-26 15:55:06 +0000245// Helper method for ActOnClassMethod/ActOnInstanceMethod.
246// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000247// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000248// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000249ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000250 ObjCInterfaceDecl *ClassDecl) {
251 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000252 // lookup in class and all superclasses
253 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000254 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Steve Naroff5609ec02009-03-08 18:56:13 +0000257 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000258 if (!Method)
259 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Steve Naroff5609ec02009-03-08 18:56:13 +0000261 // Before we give up, check if the selector is an instance method.
262 // But only in the root. This matches gcc's behaviour and what the
263 // runtime expects.
264 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000265 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000266 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000267 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000268 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000269 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Steve Naroff5609ec02009-03-08 18:56:13 +0000272 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000273 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000274 return Method;
275}
276
277ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
278 ObjCInterfaceDecl *ClassDecl) {
279 ObjCMethodDecl *Method = 0;
280 while (ClassDecl && !Method) {
281 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000282 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000283 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Naroff5609ec02009-03-08 18:56:13 +0000285 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000286 if (!Method)
287 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000288 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000289 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000290 return Method;
291}
292
Chris Lattner7f816522010-04-11 07:45:24 +0000293/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
294/// objective C interface. This is a property reference expression.
295Action::OwningExprResult Sema::
296HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
297 Expr *&BaseExpr, bool &IsArrow,
298 DeclarationName MemberName,
299 SourceLocation MemberLoc, SourceLocation OpLoc,
300 CXXScopeSpec &SS, DeclPtrTy ObjCImpDecl) {
301 assert(!IsArrow && "Should only be called with '.' expressions");
302 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
303 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
304 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
305
306 // Search for a declared property first.
307 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
308 // Check whether we can reference this property.
309 if (DiagnoseUseOfDecl(PD, MemberLoc))
310 return ExprError();
311 QualType ResTy = PD->getType();
312 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
313 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
314 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
315 ResTy = Getter->getResultType();
316 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
317 MemberLoc, BaseExpr));
318 }
319 // Check protocols on qualified interfaces.
320 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
321 E = OPT->qual_end(); I != E; ++I)
322 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
323 // Check whether we can reference this property.
324 if (DiagnoseUseOfDecl(PD, MemberLoc))
325 return ExprError();
326
327 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
328 MemberLoc, BaseExpr));
329 }
330 // If that failed, look for an "implicit" property by seeing if the nullary
331 // selector is implemented.
332
333 // FIXME: The logic for looking up nullary and unary selectors should be
334 // shared with the code in ActOnInstanceMessage.
335
336 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
337 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
338
339 // If this reference is in an @implementation, check for 'private' methods.
340 if (!Getter)
341 Getter = IFace->lookupPrivateInstanceMethod(Sel);
342
343 // Look through local category implementations associated with the class.
344 if (!Getter)
345 Getter = IFace->getCategoryInstanceMethod(Sel);
346 if (Getter) {
347 // Check if we can reference this property.
348 if (DiagnoseUseOfDecl(Getter, MemberLoc))
349 return ExprError();
350 }
351 // If we found a getter then this may be a valid dot-reference, we
352 // will look for the matching setter, in case it is needed.
353 Selector SetterSel =
354 SelectorTable::constructSetterName(PP.getIdentifierTable(),
355 PP.getSelectorTable(), Member);
356 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
357 if (!Setter) {
358 // If this reference is in an @implementation, also check for 'private'
359 // methods.
360 Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
361 }
362 // Look through local category implementations associated with the class.
363 if (!Setter)
364 Setter = IFace->getCategoryInstanceMethod(SetterSel);
365
366 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
367 return ExprError();
368
369 if (Getter) {
370 QualType PType;
371 PType = Getter->getResultType();
372 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType,
373 Setter, MemberLoc, BaseExpr));
374 }
375
376 // Attempt to correct for typos in property names.
377 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
378 if (CorrectTypo(Res, 0, 0, IFace, false, OPT) &&
379 Res.getAsSingle<ObjCPropertyDecl>()) {
380 Diag(MemberLoc, diag::err_property_not_found_suggest)
381 << MemberName << QualType(OPT, 0) << Res.getLookupName()
382 << FixItHint::CreateReplacement(MemberLoc,
383 Res.getLookupName().getAsString());
384 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
385 Diag(Property->getLocation(), diag::note_previous_decl)
386 << Property->getDeclName();
387
388 return LookupMemberExpr(Res, BaseExpr, IsArrow, OpLoc, SS, ObjCImpDecl);
389 }
390 Diag(MemberLoc, diag::err_property_not_found)
391 << MemberName << QualType(OPT, 0);
392 if (Setter && !Getter)
393 Diag(Setter->getLocation(), diag::note_getter_unavailable)
394 << MemberName << BaseExpr->getSourceRange();
395 return ExprError();
396
397}
398
399
400
Steve Naroff61f72cb2009-03-09 21:12:44 +0000401Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
402 IdentifierInfo &receiverName,
403 IdentifierInfo &propertyName,
404 SourceLocation &receiverNameLoc,
405 SourceLocation &propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000407 IdentifierInfo *receiverNamePtr = &receiverName;
408 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr);
Fariborz Jahanian8149a572010-03-22 21:02:34 +0000409 if (!IFace) {
410 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
411 return ExprError();
412 }
Steve Naroff61f72cb2009-03-09 21:12:44 +0000413 // Search for a declared property first.
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Steve Naroff61f72cb2009-03-09 21:12:44 +0000415 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000416 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000417
418 // If this reference is in an @implementation, check for 'private' methods.
419 if (!Getter)
420 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
421 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000422 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000423 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000424
425 if (Getter) {
426 // FIXME: refactor/share with ActOnMemberReference().
427 // Check if we can reference this property.
428 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
429 return ExprError();
430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Steve Naroff61f72cb2009-03-09 21:12:44 +0000432 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Selector SetterSel =
434 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000435 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000437 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000438 if (!Setter) {
439 // If this reference is in an @implementation, also check for 'private'
440 // methods.
441 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
442 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000443 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000444 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000445 }
446 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000447 if (!Setter)
448 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000449
450 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
451 return ExprError();
452
453 if (Getter || Setter) {
454 QualType PType;
455
456 if (Getter)
457 PType = Getter->getResultType();
458 else {
459 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
460 E = Setter->param_end(); PI != E; ++PI)
461 PType = (*PI)->getType();
462 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000463 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump1eb44332009-09-09 15:08:12 +0000464 Getter, PType, Setter,
Steve Naroff61f72cb2009-03-09 21:12:44 +0000465 propertyNameLoc, IFace, receiverNameLoc));
466 }
467 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
468 << &propertyName << Context.getObjCInterfaceType(IFace));
469}
470
471
Chris Lattner85a932e2008-01-04 22:32:30 +0000472// ActOnClassMessage - used for both unary and keyword messages.
473// ArgExprs is optional - if it is present, the number of expressions
474// is obtained from Sel.getNumArgs().
475Sema::ExprResult Sema::ActOnClassMessage(
476 Scope *S,
477 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000478 SourceLocation lbrac, SourceLocation receiverLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000479 SourceLocation selectorLoc, SourceLocation rbrac,
480 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000481 assert(receiverName && "missing receiver class name");
482
483 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000484 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000485 bool isSuper = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Chris Lattner84692652008-11-20 05:35:30 +0000487 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000488 if (getCurMethodDecl()) {
489 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000490 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
491 if (!OID)
Mike Stump1eb44332009-09-09 15:08:12 +0000492 return Diag(lbrac, diag::error_no_super_class_message)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000493 << getCurMethodDecl()->getDeclName();
494 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000495 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000496 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000497 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000498 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000499 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000500 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
501 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000502 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000503 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000504 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000505 }
506 // We are sending a message to 'super' within a class method. Do nothing,
507 // the receiver will pass through as 'super' (how convenient:-).
508 } else {
509 // 'super' has been used outside a method context. If a variable named
510 // 'super' has been declared, redirect. If not, produce a diagnostic.
John McCallf36e02d2009-10-09 21:13:30 +0000511 NamedDecl *SuperDecl
512 = LookupSingleName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000513 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
514 if (VD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000515 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
Ted Kremenek8189cde2009-02-07 01:47:29 +0000516 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000517 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000518 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000519 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000520 }
Fariborz Jahaniane4fb8282010-01-22 23:04:44 +0000521 else if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(SuperDecl)) {
522 const ObjCInterfaceType *OCIT;
523 OCIT = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>();
524 if (!OCIT) {
525 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
526 return true;
527 }
528 ClassDecl = OCIT->getDecl();
529 }
530 else
531 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Mike Stump1eb44332009-09-09 15:08:12 +0000532 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000533 } else
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000534 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Steve Naroff7c778f12008-07-25 19:39:00 +0000536 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000537 //
538 // typedef XCElementDisplayRect XCElementGraphicsRect;
539 //
540 // @implementation XCRASlice
541 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
542 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
543 // }
544 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000545 // If necessary, the following lookup could move to getObjCInterfaceDecl().
546 if (!ClassDecl) {
John McCallf36e02d2009-10-09 21:13:30 +0000547 NamedDecl *IDecl
548 = LookupSingleName(TUScope, receiverName, LookupOrdinaryName);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000549 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl))
550 if (const ObjCInterfaceType *OCIT
551 = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>())
552 ClassDecl = OCIT->getDecl();
553
554 if (!ClassDecl) {
555 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
556 return true;
Steve Naroff7c778f12008-07-25 19:39:00 +0000557 }
558 }
559 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000560 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000561 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000562 if (ClassDecl->isForwardDecl()) {
563 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000564 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000565 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
566 if (Method)
Mike Stump1eb44332009-09-09 15:08:12 +0000567 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000568 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000569 }
570 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000571 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Steve Naroff7c778f12008-07-25 19:39:00 +0000573 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000574 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000575 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000576
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000577 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
578 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
580 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000581 lbrac, rbrac, returnType))
582 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000583
Anders Carlsson187ba152009-05-26 15:22:25 +0000584 returnType = returnType.getNonReferenceType();
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Mike Stump390b4cc2009-05-16 07:39:55 +0000586 // If we have the ObjCInterfaceDecl* for the class that is receiving the
587 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
588 // IdentifierInfo* for the class.
589 // FIXME: need to do a better job handling 'super' usage within a class. For
590 // now, we simply pass the "super" identifier through (which isn't consistent
591 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000592 if (isSuper)
Douglas Gregorc2350e52010-03-08 16:40:19 +0000593 return new (Context) ObjCMessageExpr(Context, receiverName, receiverLoc,
594 Sel, returnType, Method, lbrac, rbrac,
595 ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000596 else
Douglas Gregorc2350e52010-03-08 16:40:19 +0000597 return new (Context) ObjCMessageExpr(Context, ClassDecl, receiverLoc,
598 Sel, returnType, Method, lbrac, rbrac,
599 ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000600}
601
602// ActOnInstanceMessage - used for both unary and keyword messages.
603// ArgExprs is optional - if it is present, the number of expressions
604// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000605Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump1eb44332009-09-09 15:08:12 +0000606 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000607 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000608 SourceLocation rbrac,
609 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000610 assert(receiver && "missing receiver expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattner85a932e2008-01-04 22:32:30 +0000612 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
613 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattnerd0d45992009-04-29 05:48:32 +0000615 // If necessary, apply function/array conversion to the receiver.
616 // C99 6.7.5.3p[7,8].
Douglas Gregora873dfc2010-02-03 00:27:59 +0000617 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Chris Lattner85a932e2008-01-04 22:32:30 +0000619 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000620 QualType ReceiverCType =
621 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000622
623 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000624 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000625 ObjCMethodDecl *Method = 0;
626 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
627 // If we have an interface in scope, check 'super' methods.
628 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000629 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000630 Method = SuperDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
632 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000633 // If we have implementations in scope, check "private" methods.
634 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
635 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000636 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000637
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000638 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
639 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000640
Chris Lattner077bf5e2008-11-24 03:33:13 +0000641 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000642 lbrac, rbrac, returnType))
643 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Anders Carlsson187ba152009-05-26 15:22:25 +0000645 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000646 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
647 Method, lbrac, rbrac,
648 ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000649 }
650
Mike Stump1eb44332009-09-09 15:08:12 +0000651 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000652 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000653 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000654 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
655 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000656 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000657 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump1eb44332009-09-09 15:08:12 +0000658 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000659 lbrac, rbrac, returnType))
660 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000661 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000662 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
663 Method, lbrac, rbrac,
664 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000665 }
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000667 // Handle messages to Class.
Mike Stump1eb44332009-09-09 15:08:12 +0000668 if (ReceiverCType->isObjCClassType() ||
Steve Naroff470301b2009-07-22 16:07:01 +0000669 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000670 ObjCMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattner6562fda2008-07-21 06:44:27 +0000672 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000673 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
674 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000675 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Steve Narofff1afaf62009-02-26 15:55:06 +0000677 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000678 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
680 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff470301b2009-07-22 16:07:01 +0000681 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000682 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000683 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
684 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000685 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000686 if (!Method) {
687 // If not messaging 'self', look for any factory method named 'Sel'.
688 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000689 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000690 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000691 // If no class (factory) method was found, check if an _instance_
692 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000693 Method = LookupInstanceMethodInGlobalPool(
694 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000695 if (Method)
696 if (const ObjCInterfaceDecl *ID =
697 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
698 if (ID->getSuperClass())
699 Diag(lbrac, diag::warn_root_inst_method_not_found)
700 << Sel << SourceRange(lbrac, rbrac);
701 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000702 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000703 }
704 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000705 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000706 lbrac, rbrac, returnType))
707 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000708 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000709 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
710 Method, lbrac, rbrac,
711 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000714 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000715 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000716
717 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000718 // long as one of the protocols implements the selector (if not, warn).
Mike Stump1eb44332009-09-09 15:08:12 +0000719 if (const ObjCObjectPointerType *QIdTy =
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000720 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000721 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000722 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000723 E = QIdTy->qual_end(); I != E; ++I) {
724 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000725 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000726 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000727 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000728 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000729 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff14108da2009-07-10 23:34:53 +0000732 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000733 // We allow sending a message to a pointer to an interface (an object).
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Steve Naroff14108da2009-07-10 23:34:53 +0000735 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000736 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000737 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000738 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000739 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000741 if (!Method) {
742 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000743 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000744 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000745 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000746 break;
747 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000748 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000749 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000750 // If we have implementations in scope, check "private" methods.
751 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Steve Naroff5609ec02009-03-08 18:56:13 +0000753 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000754 // If we still haven't found a method, look in the global pool. This
755 // behavior isn't very desirable, however we need it for GCC
756 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000757 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000758 Method = LookupInstanceMethodInGlobalPool(
759 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000760 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump1eb44332009-09-09 15:08:12 +0000761 Diag(lbrac, diag::warn_maynot_respond)
Chris Lattner0784fcd2010-04-11 07:04:01 +0000762 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000763 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000764 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000765 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000766 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
767 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000768 } else if (!Context.getObjCIdType().isNull() &&
769 (ReceiverCType->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000770 (ReceiverCType->isIntegerType() &&
Chris Lattner0c73f372009-03-09 21:19:16 +0000771 ReceiverCType->isScalarType()))) {
772 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000773 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000774 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman73c39ab2009-10-20 08:27:19 +0000775 if (ReceiverCType->isPointerType())
776 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
777 else
778 ImpCastExprToType(RExpr, Context.getObjCIdType(),
779 CastExpr::CK_IntegralToPointer);
Chris Lattner0c73f372009-03-09 21:19:16 +0000780 } else {
781 // Reject other random receiver types (e.g. structs).
782 Diag(lbrac, diag::err_bad_receiver_type)
783 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000784 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000787 if (Method)
788 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000789 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000790 lbrac, rbrac, returnType))
791 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000792 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000793 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType, Method,
794 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000795}
Chris Lattnereca7be62008-04-07 05:30:13 +0000796