blob: 530a40f49c1aa3ac2bb8a040f080c20af3b5f771 [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");
Douglas Gregorc83c6872010-04-15 22:33:43 +000081 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
82 LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000083 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
84 Context.setObjCConstantStringInterface(StrIF);
85 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +000086 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000087 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000088 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +000089 // strings as untyped objects and let the runtime figure it out later.
90 Ty = Context.getObjCIdType();
91 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattnerf4b136f2009-02-18 06:13:04 +000094 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000095}
96
Mike Stump1eb44332009-09-09 15:08:12 +000097Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Anders Carlssonfc0f0212009-06-07 18:45:35 +000098 QualType EncodedType,
99 SourceLocation RParenLoc) {
100 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000101 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000102 StrTy = Context.DependentTy;
103 else {
104 std::string Str;
105 Context.getObjCEncodingForType(EncodedType, Str);
106
107 // The type of @encode is the same as the type of the corresponding string,
108 // which is an array type.
109 StrTy = Context.CharTy;
110 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000111 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000112 StrTy.addConst();
113 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
114 ArrayType::Normal, 0);
115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000117 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
118}
119
Chris Lattner85a932e2008-01-04 22:32:30 +0000120Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
121 SourceLocation EncodeLoc,
122 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000123 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +0000124 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000125 // FIXME: Preserve type source info ?
126 QualType EncodedType = GetTypeFromParser(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +0000127
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000128 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000129}
130
131Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
132 SourceLocation AtLoc,
133 SourceLocation SelLoc,
134 SourceLocation LParenLoc,
135 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000136 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +0000137 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000138 if (!Method)
139 Method = LookupFactoryMethodInGlobalPool(Sel,
140 SourceRange(LParenLoc, RParenLoc));
141 if (!Method)
142 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
143
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000144 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000145 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000146}
147
148Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
149 SourceLocation AtLoc,
150 SourceLocation ProtoLoc,
151 SourceLocation LParenLoc,
152 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000153 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000154 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000155 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000156 return true;
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000159 QualType Ty = Context.getObjCProtoType();
160 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000161 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000162 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000163 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000164}
165
Mike Stump1eb44332009-09-09 15:08:12 +0000166bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
167 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000168 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000169 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump1eb44332009-09-09 15:08:12 +0000170 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000171 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000172 // Apply default argument promotion as for (C99 6.5.2.2p6).
173 for (unsigned i = 0; i != NumArgs; i++)
174 DefaultArgumentPromotion(Args[i]);
175
Chris Lattner077bf5e2008-11-24 03:33:13 +0000176 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
177 diag::warn_inst_method_not_found;
178 Diag(lbrac, DiagID)
179 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000180 ReturnType = Context.getObjCIdType();
181 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Chris Lattner077bf5e2008-11-24 03:33:13 +0000184 ReturnType = Method->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000186 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000187 // Method might have more arguments than selector indicates. This is due
188 // to addition of c-style arguments in method.
189 if (Method->param_size() > Sel.getNumArgs())
190 NumNamedArgs = Method->param_size();
191 // FIXME. This need be cleaned up.
192 if (NumArgs < NumNamedArgs) {
193 Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2;
194 return false;
195 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000196
Chris Lattner312531a2009-04-12 08:11:20 +0000197 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000198 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000199 Expr *argExpr = Args[i];
200 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattner89951a82009-02-20 18:43:26 +0000202 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000203 QualType rhsType = argExpr->getType();
204
Mike Stump1eb44332009-09-09 15:08:12 +0000205 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000206 if (lhsType->isArrayType())
207 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000208 else if (lhsType->isFunctionType())
209 lhsType = Context.getPointerType(lhsType);
210
Mike Stump1eb44332009-09-09 15:08:12 +0000211 AssignConvertType Result =
Chris Lattner987798a2008-04-02 17:17:33 +0000212 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000213 if (Args[i] != argExpr) // The expression was converted.
214 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000215
216 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000217 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor68647482009-12-16 03:45:30 +0000218 argExpr, AA_Sending);
Chris Lattner85a932e2008-01-04 22:32:30 +0000219 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000220
221 // Promote additional arguments to variadic methods.
222 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000223 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000224 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000225 } else {
226 // Check for extra arguments to non-variadic methods.
227 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000228 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000229 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000230 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000231 << SourceRange(Args[NumNamedArgs]->getLocStart(),
232 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000233 }
234 }
235
Chris Lattner312531a2009-04-12 08:11:20 +0000236 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000237}
238
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000239bool Sema::isSelfExpr(Expr *RExpr) {
240 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
241 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
242 return true;
243 return false;
244}
245
Steve Narofff1afaf62009-02-26 15:55:06 +0000246// Helper method for ActOnClassMethod/ActOnInstanceMethod.
247// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000248// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000249// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000250ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000251 ObjCInterfaceDecl *ClassDecl) {
252 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000253 // lookup in class and all superclasses
254 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000255 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000256 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Steve Naroff5609ec02009-03-08 18:56:13 +0000258 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000259 if (!Method)
260 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Steve Naroff5609ec02009-03-08 18:56:13 +0000262 // Before we give up, check if the selector is an instance method.
263 // But only in the root. This matches gcc's behaviour and what the
264 // runtime expects.
265 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000266 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000267 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000268 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000269 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000270 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
271 }
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Steve Naroff5609ec02009-03-08 18:56:13 +0000273 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000274 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000275 return Method;
276}
277
278ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
279 ObjCInterfaceDecl *ClassDecl) {
280 ObjCMethodDecl *Method = 0;
281 while (ClassDecl && !Method) {
282 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000283 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000284 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Steve Naroff5609ec02009-03-08 18:56:13 +0000286 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000287 if (!Method)
288 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000289 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000290 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000291 return Method;
292}
293
Chris Lattner7f816522010-04-11 07:45:24 +0000294/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
295/// objective C interface. This is a property reference expression.
296Action::OwningExprResult Sema::
297HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000298 Expr *BaseExpr, DeclarationName MemberName,
299 SourceLocation MemberLoc) {
Chris Lattner7f816522010-04-11 07:45:24 +0000300 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
301 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
302 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
303
304 // Search for a declared property first.
305 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
306 // Check whether we can reference this property.
307 if (DiagnoseUseOfDecl(PD, MemberLoc))
308 return ExprError();
309 QualType ResTy = PD->getType();
310 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
311 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
312 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
313 ResTy = Getter->getResultType();
314 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
315 MemberLoc, BaseExpr));
316 }
317 // Check protocols on qualified interfaces.
318 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
319 E = OPT->qual_end(); I != E; ++I)
320 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
321 // Check whether we can reference this property.
322 if (DiagnoseUseOfDecl(PD, MemberLoc))
323 return ExprError();
324
325 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
326 MemberLoc, BaseExpr));
327 }
328 // If that failed, look for an "implicit" property by seeing if the nullary
329 // selector is implemented.
330
331 // FIXME: The logic for looking up nullary and unary selectors should be
332 // shared with the code in ActOnInstanceMessage.
333
334 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
335 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
336
337 // If this reference is in an @implementation, check for 'private' methods.
338 if (!Getter)
339 Getter = IFace->lookupPrivateInstanceMethod(Sel);
340
341 // Look through local category implementations associated with the class.
342 if (!Getter)
343 Getter = IFace->getCategoryInstanceMethod(Sel);
344 if (Getter) {
345 // Check if we can reference this property.
346 if (DiagnoseUseOfDecl(Getter, MemberLoc))
347 return ExprError();
348 }
349 // If we found a getter then this may be a valid dot-reference, we
350 // will look for the matching setter, in case it is needed.
351 Selector SetterSel =
352 SelectorTable::constructSetterName(PP.getIdentifierTable(),
353 PP.getSelectorTable(), Member);
354 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
355 if (!Setter) {
356 // If this reference is in an @implementation, also check for 'private'
357 // methods.
358 Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
359 }
360 // Look through local category implementations associated with the class.
361 if (!Setter)
362 Setter = IFace->getCategoryInstanceMethod(SetterSel);
363
364 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
365 return ExprError();
366
367 if (Getter) {
368 QualType PType;
369 PType = Getter->getResultType();
370 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType,
371 Setter, MemberLoc, BaseExpr));
372 }
373
374 // Attempt to correct for typos in property names.
375 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000376 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000377 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000378 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000379 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000380 << MemberName << QualType(OPT, 0) << TypoResult
381 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000382 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
383 Diag(Property->getLocation(), diag::note_previous_decl)
384 << Property->getDeclName();
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000385 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc);
Chris Lattner7f816522010-04-11 07:45:24 +0000386 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000387
Chris Lattner7f816522010-04-11 07:45:24 +0000388 Diag(MemberLoc, diag::err_property_not_found)
389 << MemberName << QualType(OPT, 0);
390 if (Setter && !Getter)
391 Diag(Setter->getLocation(), diag::note_getter_unavailable)
392 << MemberName << BaseExpr->getSourceRange();
393 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000394}
395
396
397
Chris Lattnereb483eb2010-04-11 08:28:14 +0000398Action::OwningExprResult Sema::
399ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
400 IdentifierInfo &propertyName,
401 SourceLocation receiverNameLoc,
402 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000404 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000405 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
406 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000407 if (IFace == 0) {
408 // If the "receiver" is 'super' in a method, handle it as an expression-like
409 // property reference.
410 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
411 if (receiverNamePtr->isStr("super")) {
412 if (CurMethod->isInstanceMethod()) {
413 QualType T =
414 Context.getObjCInterfaceType(CurMethod->getClassInterface());
415 T = Context.getObjCObjectPointerType(T);
416 Expr *SuperExpr = new (Context) ObjCSuperExpr(receiverNameLoc, T);
417
418 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
419 SuperExpr, &propertyName,
420 propertyNameLoc);
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnereb483eb2010-04-11 08:28:14 +0000423 // Otherwise, if this is a class method, try dispatching to our
424 // superclass.
425 IFace = CurMethod->getClassInterface()->getSuperClass();
426 }
427
428 if (IFace == 0) {
429 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
430 return ExprError();
431 }
432 }
433
434 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000435 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000436 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000437
438 // If this reference is in an @implementation, check for 'private' methods.
439 if (!Getter)
440 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
441 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000442 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000443 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000444
445 if (Getter) {
446 // FIXME: refactor/share with ActOnMemberReference().
447 // Check if we can reference this property.
448 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
449 return ExprError();
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Steve Naroff61f72cb2009-03-09 21:12:44 +0000452 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000453 Selector SetterSel =
454 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000455 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000457 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000458 if (!Setter) {
459 // If this reference is in an @implementation, also check for 'private'
460 // methods.
461 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
462 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000463 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000464 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000465 }
466 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000467 if (!Setter)
468 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000469
470 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
471 return ExprError();
472
473 if (Getter || Setter) {
474 QualType PType;
475
476 if (Getter)
477 PType = Getter->getResultType();
478 else {
479 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
480 E = Setter->param_end(); PI != E; ++PI)
481 PType = (*PI)->getType();
482 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000483 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump1eb44332009-09-09 15:08:12 +0000484 Getter, PType, Setter,
Steve Naroff61f72cb2009-03-09 21:12:44 +0000485 propertyNameLoc, IFace, receiverNameLoc));
486 }
487 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
488 << &propertyName << Context.getObjCInterfaceType(IFace));
489}
490
Douglas Gregor47bd5432010-04-14 02:46:37 +0000491Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
492 IdentifierInfo *&Name,
493 SourceLocation NameLoc,
494 bool IsSuper,
495 bool HasTrailingDot) {
496 // If the identifier is "super" and there is no trailing dot, we're
497 // messaging super.
498 if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
499 return ObjCSuperMessage;
500
501 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
502 LookupName(Result, S);
503
504 switch (Result.getResultKind()) {
505 case LookupResult::NotFound:
506 // Break out; we'll perform typo correction below.
507 break;
508
509 case LookupResult::NotFoundInCurrentInstantiation:
510 case LookupResult::FoundOverloaded:
511 case LookupResult::FoundUnresolvedValue:
512 case LookupResult::Ambiguous:
513 Result.suppressDiagnostics();
514 return ObjCInstanceMessage;
515
516 case LookupResult::Found: {
517 // We found something. If it's a type, then we have a class
518 // message. Otherwise, it's an instance message.
519 NamedDecl *ND = Result.getFoundDecl();
520 if (isa<ObjCInterfaceDecl>(ND) || isa<TypeDecl>(ND) ||
521 isa<UnresolvedUsingTypenameDecl>(ND))
522 return ObjCClassMessage;
523
524 return ObjCInstanceMessage;
525 }
526 }
527
Douglas Gregoraaf87162010-04-14 20:04:41 +0000528 // Determine our typo-correction context.
529 CorrectTypoContext CTC = CTC_Expression;
530 if (ObjCMethodDecl *Method = getCurMethodDecl())
531 if (Method->getClassInterface() &&
532 Method->getClassInterface()->getSuperClass())
533 CTC = CTC_ObjCMessageReceiver;
534
535 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
536 if (Result.isSingleResult()) {
537 // If we found a declaration, correct when it refers to an Objective-C
538 // class.
539 NamedDecl *ND = Result.getFoundDecl();
540 if (isa<ObjCInterfaceDecl>(ND)) {
541 Diag(NameLoc, diag::err_unknown_receiver_suggest)
542 << Name << Result.getLookupName()
543 << FixItHint::CreateReplacement(SourceRange(NameLoc),
544 ND->getNameAsString());
545 Diag(ND->getLocation(), diag::note_previous_decl)
546 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000547
Douglas Gregoraaf87162010-04-14 20:04:41 +0000548 Name = ND->getIdentifier();
549 return ObjCClassMessage;
550 }
551 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
552 Corrected.getAsIdentifierInfo()->isStr("super")) {
553 // If we've found the keyword "super", this is a send to super.
554 Diag(NameLoc, diag::err_unknown_receiver_suggest)
555 << Name << Corrected
556 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
557 Name = Corrected.getAsIdentifierInfo();
558 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000559 }
560 }
561
562 // Fall back: let the parser try to parse it as an instance message.
563 return ObjCInstanceMessage;
564}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000565
Chris Lattner85a932e2008-01-04 22:32:30 +0000566// ActOnClassMessage - used for both unary and keyword messages.
567// ArgExprs is optional - if it is present, the number of expressions
568// is obtained from Sel.getNumArgs().
Chris Lattnereb483eb2010-04-11 08:28:14 +0000569Sema::ExprResult Sema::
570ActOnClassMessage(Scope *S, IdentifierInfo *receiverName, Selector Sel,
571 SourceLocation lbrac, SourceLocation receiverLoc,
572 SourceLocation selectorLoc, SourceLocation rbrac,
573 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000574 assert(receiverName && "missing receiver class name");
575
576 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Chris Lattner15faee12010-04-12 05:38:43 +0000577 ObjCInterfaceDecl *ClassDecl = 0;
Chris Lattnera7a98c92010-04-12 17:25:51 +0000578 bool isSuper = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattner15faee12010-04-12 05:38:43 +0000580 // Special case a message to super, which can be either a class message or an
581 // instance message, depending on what CurMethodDecl is.
Chris Lattner84692652008-11-20 05:35:30 +0000582 if (receiverName->isStr("super")) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000583 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000584 ObjCInterfaceDecl *OID = CurMethod->getClassInterface();
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000585 if (!OID)
Mike Stump1eb44332009-09-09 15:08:12 +0000586 return Diag(lbrac, diag::error_no_super_class_message)
Chris Lattnereb483eb2010-04-11 08:28:14 +0000587 << CurMethod->getDeclName();
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000588 ClassDecl = OID->getSuperClass();
Chris Lattner15faee12010-04-12 05:38:43 +0000589 if (ClassDecl == 0)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000590 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Chris Lattnereb483eb2010-04-11 08:28:14 +0000591 if (CurMethod->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000592 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000593 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000594 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
595 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000596 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000597 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000598 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000599 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000600
Chris Lattner15faee12010-04-12 05:38:43 +0000601 // Otherwise, if this is a class method, try dispatching to our
602 // superclass, which is in ClassDecl.
Chris Lattnera7a98c92010-04-12 17:25:51 +0000603 isSuper = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000604 }
Chris Lattner15faee12010-04-12 05:38:43 +0000605 }
606
607 if (ClassDecl == 0)
Douglas Gregorc83c6872010-04-15 22:33:43 +0000608 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Steve Naroff7c778f12008-07-25 19:39:00 +0000610 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000611 //
612 // typedef XCElementDisplayRect XCElementGraphicsRect;
613 //
614 // @implementation XCRASlice
615 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
616 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
617 // }
618 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000619 // If necessary, the following lookup could move to getObjCInterfaceDecl().
620 if (!ClassDecl) {
John McCallf36e02d2009-10-09 21:13:30 +0000621 NamedDecl *IDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +0000622 = LookupSingleName(TUScope, receiverName, receiverLoc,
623 LookupOrdinaryName);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000624 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl))
625 if (const ObjCInterfaceType *OCIT
626 = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>())
627 ClassDecl = OCIT->getDecl();
628
629 if (!ClassDecl) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000630 // Give a better error message for invalid use of super.
631 if (receiverName->isStr("super"))
632 Diag(receiverLoc, diag::err_invalid_receiver_to_message_super);
633 else
634 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000635 return true;
Steve Naroff7c778f12008-07-25 19:39:00 +0000636 }
637 }
638 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000639 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000640 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000641 if (ClassDecl->isForwardDecl()) {
642 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000643 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000644 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
645 if (Method)
Mike Stump1eb44332009-09-09 15:08:12 +0000646 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000647 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000648 }
649 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000650 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Steve Naroff7c778f12008-07-25 19:39:00 +0000652 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000653 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000654 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000655
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000656 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
657 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
659 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000660 lbrac, rbrac, returnType))
661 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000662
Anders Carlsson187ba152009-05-26 15:22:25 +0000663 returnType = returnType.getNonReferenceType();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Chris Lattnera7a98c92010-04-12 17:25:51 +0000665 // FIXME: need to do a better job handling 'super' usage within a class. For
666 // now, we simply pass the "super" identifier through (which isn't consistent
667 // with instance methods.
668 if (isSuper)
669 return new (Context) ObjCMessageExpr(Context, receiverName, receiverLoc,
670 Sel, returnType, Method, lbrac, rbrac,
671 ArgExprs, NumArgs);
672
Mike Stump390b4cc2009-05-16 07:39:55 +0000673 // If we have the ObjCInterfaceDecl* for the class that is receiving the
674 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
675 // IdentifierInfo* for the class.
Chris Lattner15faee12010-04-12 05:38:43 +0000676 return new (Context) ObjCMessageExpr(Context, ClassDecl, receiverLoc,
677 Sel, returnType, Method, lbrac, rbrac,
678 ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000679}
680
681// ActOnInstanceMessage - used for both unary and keyword messages.
682// ArgExprs is optional - if it is present, the number of expressions
683// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000684Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump1eb44332009-09-09 15:08:12 +0000685 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000686 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000687 SourceLocation rbrac,
688 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000689 assert(receiver && "missing receiver expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattner85a932e2008-01-04 22:32:30 +0000691 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
692 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattnerd0d45992009-04-29 05:48:32 +0000694 // If necessary, apply function/array conversion to the receiver.
695 // C99 6.7.5.3p[7,8].
Douglas Gregora873dfc2010-02-03 00:27:59 +0000696 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Chris Lattner85a932e2008-01-04 22:32:30 +0000698 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000699 QualType ReceiverCType =
700 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000701
Mike Stump1eb44332009-09-09 15:08:12 +0000702 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000703 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000704 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000705 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
706 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000707 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000708 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump1eb44332009-09-09 15:08:12 +0000709 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000710 lbrac, rbrac, returnType))
711 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000712 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000713 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
714 Method, lbrac, rbrac,
715 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000718 // Handle messages to Class.
Mike Stump1eb44332009-09-09 15:08:12 +0000719 if (ReceiverCType->isObjCClassType() ||
Steve Naroff470301b2009-07-22 16:07:01 +0000720 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000721 ObjCMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattner6562fda2008-07-21 06:44:27 +0000723 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000724 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
725 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000726 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Steve Narofff1afaf62009-02-26 15:55:06 +0000728 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000729 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
731 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff470301b2009-07-22 16:07:01 +0000732 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000733 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000734 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
735 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000736 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000737 if (!Method) {
738 // If not messaging 'self', look for any factory method named 'Sel'.
739 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000740 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000741 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000742 // If no class (factory) method was found, check if an _instance_
743 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000744 Method = LookupInstanceMethodInGlobalPool(
745 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000746 if (Method)
747 if (const ObjCInterfaceDecl *ID =
748 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
749 if (ID->getSuperClass())
750 Diag(lbrac, diag::warn_root_inst_method_not_found)
751 << Sel << SourceRange(lbrac, rbrac);
752 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000753 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000754 }
755 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000756 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000757 lbrac, rbrac, returnType))
758 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000759 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000760 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
761 Method, lbrac, rbrac,
762 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000765 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000766 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
768 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000769 // long as one of the protocols implements the selector (if not, warn).
Mike Stump1eb44332009-09-09 15:08:12 +0000770 if (const ObjCObjectPointerType *QIdTy =
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000771 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000772 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000773 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000774 E = QIdTy->qual_end(); I != E; ++I) {
775 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000776 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000777 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000778 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000779 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000780 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff14108da2009-07-10 23:34:53 +0000783 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000784 // We allow sending a message to a pointer to an interface (an object).
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Steve Naroff14108da2009-07-10 23:34:53 +0000786 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000787 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000788 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000789 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000790 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000792 if (!Method) {
793 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000794 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000795 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000796 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000797 break;
798 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000799 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000800 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000801 // If we have implementations in scope, check "private" methods.
802 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Steve Naroff5609ec02009-03-08 18:56:13 +0000804 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000805 // If we still haven't found a method, look in the global pool. This
806 // behavior isn't very desirable, however we need it for GCC
807 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000808 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000809 Method = LookupInstanceMethodInGlobalPool(
810 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000811 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump1eb44332009-09-09 15:08:12 +0000812 Diag(lbrac, diag::warn_maynot_respond)
Chris Lattner0784fcd2010-04-11 07:04:01 +0000813 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000814 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000815 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000816 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000817 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
818 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000819 } else if (!Context.getObjCIdType().isNull() &&
820 (ReceiverCType->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000821 (ReceiverCType->isIntegerType() &&
Chris Lattner0c73f372009-03-09 21:19:16 +0000822 ReceiverCType->isScalarType()))) {
823 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000824 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000825 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman73c39ab2009-10-20 08:27:19 +0000826 if (ReceiverCType->isPointerType())
827 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
828 else
829 ImpCastExprToType(RExpr, Context.getObjCIdType(),
830 CastExpr::CK_IntegralToPointer);
Chris Lattner0c73f372009-03-09 21:19:16 +0000831 } else {
832 // Reject other random receiver types (e.g. structs).
833 Diag(lbrac, diag::err_bad_receiver_type)
834 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000835 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000838 if (Method)
839 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000840 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000841 lbrac, rbrac, returnType))
842 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000843 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000844 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType, Method,
845 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000846}
Chris Lattnereca7be62008-04-07 05:30:13 +0000847