blob: 1feca6d25c1366ba9bd61e03c3d55a8e26132ee9 [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,
Douglas Gregor81d34662010-04-20 15:39:42 +000098 TypeSourceInfo *EncodedTypeInfo,
Anders Carlssonfc0f0212009-06-07 18:45:35 +000099 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +0000100 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000101 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000102 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000103 StrTy = Context.DependentTy;
104 else {
105 std::string Str;
106 Context.getObjCEncodingForType(EncodedType, Str);
107
108 // The type of @encode is the same as the type of the corresponding string,
109 // which is an array type.
110 StrTy = Context.CharTy;
111 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000112 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000113 StrTy.addConst();
114 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
115 ArrayType::Normal, 0);
116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor81d34662010-04-20 15:39:42 +0000118 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000119}
120
Chris Lattner85a932e2008-01-04 22:32:30 +0000121Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
122 SourceLocation EncodeLoc,
123 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000124 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +0000125 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000126 // FIXME: Preserve type source info ?
Douglas Gregor81d34662010-04-20 15:39:42 +0000127 TypeSourceInfo *TInfo;
128 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
129 if (!TInfo)
130 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
131 PP.getLocForEndOfToken(LParenLoc));
Chris Lattner85a932e2008-01-04 22:32:30 +0000132
Douglas Gregor81d34662010-04-20 15:39:42 +0000133 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000134}
135
136Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
137 SourceLocation AtLoc,
138 SourceLocation SelLoc,
139 SourceLocation LParenLoc,
140 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000141 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +0000142 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000143 if (!Method)
144 Method = LookupFactoryMethodInGlobalPool(Sel,
145 SourceRange(LParenLoc, RParenLoc));
146 if (!Method)
147 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
148
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000149 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000150 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000151}
152
153Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
154 SourceLocation AtLoc,
155 SourceLocation ProtoLoc,
156 SourceLocation LParenLoc,
157 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000158 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000159 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000160 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000161 return true;
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000164 QualType Ty = Context.getObjCProtoType();
165 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000166 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000167 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000168 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000169}
170
Mike Stump1eb44332009-09-09 15:08:12 +0000171bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
172 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000173 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000174 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump1eb44332009-09-09 15:08:12 +0000175 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000176 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000177 // Apply default argument promotion as for (C99 6.5.2.2p6).
178 for (unsigned i = 0; i != NumArgs; i++)
179 DefaultArgumentPromotion(Args[i]);
180
Chris Lattner077bf5e2008-11-24 03:33:13 +0000181 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
182 diag::warn_inst_method_not_found;
183 Diag(lbrac, DiagID)
184 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000185 ReturnType = Context.getObjCIdType();
186 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner077bf5e2008-11-24 03:33:13 +0000189 ReturnType = Method->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000191 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000192 // Method might have more arguments than selector indicates. This is due
193 // to addition of c-style arguments in method.
194 if (Method->param_size() > Sel.getNumArgs())
195 NumNamedArgs = Method->param_size();
196 // FIXME. This need be cleaned up.
197 if (NumArgs < NumNamedArgs) {
Eric Christopherd77b9a22010-04-16 04:48:22 +0000198 Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2
199 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000200 return false;
201 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000202
Chris Lattner312531a2009-04-12 08:11:20 +0000203 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000204 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000205 Expr *argExpr = Args[i];
206 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Chris Lattner89951a82009-02-20 18:43:26 +0000208 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000209 QualType rhsType = argExpr->getType();
210
Mike Stump1eb44332009-09-09 15:08:12 +0000211 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000212 if (lhsType->isArrayType())
213 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000214 else if (lhsType->isFunctionType())
215 lhsType = Context.getPointerType(lhsType);
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217 AssignConvertType Result =
Chris Lattner987798a2008-04-02 17:17:33 +0000218 CheckSingleAssignmentConstraints(lhsType, argExpr);
Fariborz Jahanianb00ab272010-04-20 20:28:15 +0000219 if (Result == Incompatible && !getLangOptions().CPlusPlus &&
220 CheckTransparentUnionArgumentConstraints(lhsType, argExpr)
221 == Sema::Compatible)
222 Result = Compatible;
223
Chris Lattner85a932e2008-01-04 22:32:30 +0000224 if (Args[i] != argExpr) // The expression was converted.
225 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000226
227 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000228 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor68647482009-12-16 03:45:30 +0000229 argExpr, AA_Sending);
Chris Lattner85a932e2008-01-04 22:32:30 +0000230 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000231
232 // Promote additional arguments to variadic methods.
233 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000234 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000235 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000236 } else {
237 // Check for extra arguments to non-variadic methods.
238 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000239 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000240 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000241 << 2 /*method*/ << NumNamedArgs << NumArgs
242 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000243 << SourceRange(Args[NumNamedArgs]->getLocStart(),
244 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000245 }
246 }
247
Chris Lattner312531a2009-04-12 08:11:20 +0000248 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000249}
250
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000251bool Sema::isSelfExpr(Expr *RExpr) {
252 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
253 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
254 return true;
255 return false;
256}
257
Steve Narofff1afaf62009-02-26 15:55:06 +0000258// Helper method for ActOnClassMethod/ActOnInstanceMethod.
259// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000260// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000261// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000262ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000263 ObjCInterfaceDecl *ClassDecl) {
264 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000265 // lookup in class and all superclasses
266 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000267 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000268 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Steve Naroff5609ec02009-03-08 18:56:13 +0000270 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000271 if (!Method)
272 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Steve Naroff5609ec02009-03-08 18:56:13 +0000274 // Before we give up, check if the selector is an instance method.
275 // But only in the root. This matches gcc's behaviour and what the
276 // runtime expects.
277 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000278 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000279 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000280 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000281 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000282 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Naroff5609ec02009-03-08 18:56:13 +0000285 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000286 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000287 return Method;
288}
289
290ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
291 ObjCInterfaceDecl *ClassDecl) {
292 ObjCMethodDecl *Method = 0;
293 while (ClassDecl && !Method) {
294 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000295 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000296 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Steve Naroff5609ec02009-03-08 18:56:13 +0000298 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000299 if (!Method)
300 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000301 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000302 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000303 return Method;
304}
305
Chris Lattner7f816522010-04-11 07:45:24 +0000306/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
307/// objective C interface. This is a property reference expression.
308Action::OwningExprResult Sema::
309HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000310 Expr *BaseExpr, DeclarationName MemberName,
311 SourceLocation MemberLoc) {
Chris Lattner7f816522010-04-11 07:45:24 +0000312 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
313 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
314 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
315
316 // Search for a declared property first.
317 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
318 // Check whether we can reference this property.
319 if (DiagnoseUseOfDecl(PD, MemberLoc))
320 return ExprError();
321 QualType ResTy = PD->getType();
322 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
323 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
324 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
325 ResTy = Getter->getResultType();
326 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
327 MemberLoc, BaseExpr));
328 }
329 // Check protocols on qualified interfaces.
330 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
331 E = OPT->qual_end(); I != E; ++I)
332 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
333 // Check whether we can reference this property.
334 if (DiagnoseUseOfDecl(PD, MemberLoc))
335 return ExprError();
336
337 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
338 MemberLoc, BaseExpr));
339 }
340 // If that failed, look for an "implicit" property by seeing if the nullary
341 // selector is implemented.
342
343 // FIXME: The logic for looking up nullary and unary selectors should be
344 // shared with the code in ActOnInstanceMessage.
345
346 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
347 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
348
349 // If this reference is in an @implementation, check for 'private' methods.
350 if (!Getter)
351 Getter = IFace->lookupPrivateInstanceMethod(Sel);
352
353 // Look through local category implementations associated with the class.
354 if (!Getter)
355 Getter = IFace->getCategoryInstanceMethod(Sel);
356 if (Getter) {
357 // Check if we can reference this property.
358 if (DiagnoseUseOfDecl(Getter, MemberLoc))
359 return ExprError();
360 }
361 // If we found a getter then this may be a valid dot-reference, we
362 // will look for the matching setter, in case it is needed.
363 Selector SetterSel =
364 SelectorTable::constructSetterName(PP.getIdentifierTable(),
365 PP.getSelectorTable(), Member);
366 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
367 if (!Setter) {
368 // If this reference is in an @implementation, also check for 'private'
369 // methods.
370 Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
371 }
372 // Look through local category implementations associated with the class.
373 if (!Setter)
374 Setter = IFace->getCategoryInstanceMethod(SetterSel);
375
376 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
377 return ExprError();
378
379 if (Getter) {
380 QualType PType;
381 PType = Getter->getResultType();
382 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType,
383 Setter, MemberLoc, BaseExpr));
384 }
385
386 // Attempt to correct for typos in property names.
387 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000388 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000389 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000390 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000391 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000392 << MemberName << QualType(OPT, 0) << TypoResult
393 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000394 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
395 Diag(Property->getLocation(), diag::note_previous_decl)
396 << Property->getDeclName();
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000397 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc);
Chris Lattner7f816522010-04-11 07:45:24 +0000398 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000399
Chris Lattner7f816522010-04-11 07:45:24 +0000400 Diag(MemberLoc, diag::err_property_not_found)
401 << MemberName << QualType(OPT, 0);
402 if (Setter && !Getter)
403 Diag(Setter->getLocation(), diag::note_getter_unavailable)
404 << MemberName << BaseExpr->getSourceRange();
405 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000406}
407
408
409
Chris Lattnereb483eb2010-04-11 08:28:14 +0000410Action::OwningExprResult Sema::
411ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
412 IdentifierInfo &propertyName,
413 SourceLocation receiverNameLoc,
414 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000416 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000417 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
418 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000419 if (IFace == 0) {
420 // If the "receiver" is 'super' in a method, handle it as an expression-like
421 // property reference.
422 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
423 if (receiverNamePtr->isStr("super")) {
424 if (CurMethod->isInstanceMethod()) {
425 QualType T =
426 Context.getObjCInterfaceType(CurMethod->getClassInterface());
427 T = Context.getObjCObjectPointerType(T);
428 Expr *SuperExpr = new (Context) ObjCSuperExpr(receiverNameLoc, T);
429
430 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
431 SuperExpr, &propertyName,
432 propertyNameLoc);
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Chris Lattnereb483eb2010-04-11 08:28:14 +0000435 // Otherwise, if this is a class method, try dispatching to our
436 // superclass.
437 IFace = CurMethod->getClassInterface()->getSuperClass();
438 }
439
440 if (IFace == 0) {
441 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
442 return ExprError();
443 }
444 }
445
446 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000447 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000448 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000449
450 // If this reference is in an @implementation, check for 'private' methods.
451 if (!Getter)
452 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
453 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000454 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000455 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000456
457 if (Getter) {
458 // FIXME: refactor/share with ActOnMemberReference().
459 // Check if we can reference this property.
460 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
461 return ExprError();
462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Steve Naroff61f72cb2009-03-09 21:12:44 +0000464 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000465 Selector SetterSel =
466 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000467 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000469 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000470 if (!Setter) {
471 // If this reference is in an @implementation, also check for 'private'
472 // methods.
473 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
474 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000475 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000476 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000477 }
478 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000479 if (!Setter)
480 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000481
482 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
483 return ExprError();
484
485 if (Getter || Setter) {
486 QualType PType;
487
488 if (Getter)
489 PType = Getter->getResultType();
490 else {
491 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
492 E = Setter->param_end(); PI != E; ++PI)
493 PType = (*PI)->getType();
494 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000495 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump1eb44332009-09-09 15:08:12 +0000496 Getter, PType, Setter,
Steve Naroff61f72cb2009-03-09 21:12:44 +0000497 propertyNameLoc, IFace, receiverNameLoc));
498 }
499 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
500 << &propertyName << Context.getObjCInterfaceType(IFace));
501}
502
Douglas Gregor47bd5432010-04-14 02:46:37 +0000503Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
504 IdentifierInfo *&Name,
505 SourceLocation NameLoc,
506 bool IsSuper,
507 bool HasTrailingDot) {
508 // If the identifier is "super" and there is no trailing dot, we're
509 // messaging super.
510 if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
511 return ObjCSuperMessage;
512
513 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
514 LookupName(Result, S);
515
516 switch (Result.getResultKind()) {
517 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000518 // Normal name lookup didn't find anything. If we're in an
519 // Objective-C method, look for ivars. If we find one, we're done!
520 // FIXME: This is a hack. Ivar lookup should be part of normal lookup.
521 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
522 ObjCInterfaceDecl *ClassDeclared;
523 if (Method->getClassInterface()->lookupInstanceVariable(Name,
524 ClassDeclared))
525 return ObjCInstanceMessage;
526 }
527
Douglas Gregor47bd5432010-04-14 02:46:37 +0000528 // Break out; we'll perform typo correction below.
529 break;
530
531 case LookupResult::NotFoundInCurrentInstantiation:
532 case LookupResult::FoundOverloaded:
533 case LookupResult::FoundUnresolvedValue:
534 case LookupResult::Ambiguous:
535 Result.suppressDiagnostics();
536 return ObjCInstanceMessage;
537
538 case LookupResult::Found: {
539 // We found something. If it's a type, then we have a class
540 // message. Otherwise, it's an instance message.
541 NamedDecl *ND = Result.getFoundDecl();
542 if (isa<ObjCInterfaceDecl>(ND) || isa<TypeDecl>(ND) ||
543 isa<UnresolvedUsingTypenameDecl>(ND))
544 return ObjCClassMessage;
545
546 return ObjCInstanceMessage;
547 }
548 }
549
Douglas Gregoraaf87162010-04-14 20:04:41 +0000550 // Determine our typo-correction context.
551 CorrectTypoContext CTC = CTC_Expression;
552 if (ObjCMethodDecl *Method = getCurMethodDecl())
553 if (Method->getClassInterface() &&
554 Method->getClassInterface()->getSuperClass())
555 CTC = CTC_ObjCMessageReceiver;
556
557 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
558 if (Result.isSingleResult()) {
559 // If we found a declaration, correct when it refers to an Objective-C
560 // class.
561 NamedDecl *ND = Result.getFoundDecl();
562 if (isa<ObjCInterfaceDecl>(ND)) {
563 Diag(NameLoc, diag::err_unknown_receiver_suggest)
564 << Name << Result.getLookupName()
565 << FixItHint::CreateReplacement(SourceRange(NameLoc),
566 ND->getNameAsString());
567 Diag(ND->getLocation(), diag::note_previous_decl)
568 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000569
Douglas Gregoraaf87162010-04-14 20:04:41 +0000570 Name = ND->getIdentifier();
571 return ObjCClassMessage;
572 }
573 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
574 Corrected.getAsIdentifierInfo()->isStr("super")) {
575 // If we've found the keyword "super", this is a send to super.
576 Diag(NameLoc, diag::err_unknown_receiver_suggest)
577 << Name << Corrected
578 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
579 Name = Corrected.getAsIdentifierInfo();
580 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000581 }
582 }
583
584 // Fall back: let the parser try to parse it as an instance message.
585 return ObjCInstanceMessage;
586}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000587
Chris Lattner85a932e2008-01-04 22:32:30 +0000588// ActOnClassMessage - used for both unary and keyword messages.
589// ArgExprs is optional - if it is present, the number of expressions
590// is obtained from Sel.getNumArgs().
Chris Lattnereb483eb2010-04-11 08:28:14 +0000591Sema::ExprResult Sema::
592ActOnClassMessage(Scope *S, IdentifierInfo *receiverName, Selector Sel,
593 SourceLocation lbrac, SourceLocation receiverLoc,
594 SourceLocation selectorLoc, SourceLocation rbrac,
595 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000596 assert(receiverName && "missing receiver class name");
597
598 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Chris Lattner15faee12010-04-12 05:38:43 +0000599 ObjCInterfaceDecl *ClassDecl = 0;
Chris Lattnera7a98c92010-04-12 17:25:51 +0000600 bool isSuper = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Chris Lattner15faee12010-04-12 05:38:43 +0000602 // Special case a message to super, which can be either a class message or an
603 // instance message, depending on what CurMethodDecl is.
Chris Lattner84692652008-11-20 05:35:30 +0000604 if (receiverName->isStr("super")) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000605 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000606 ObjCInterfaceDecl *OID = CurMethod->getClassInterface();
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000607 if (!OID)
Mike Stump1eb44332009-09-09 15:08:12 +0000608 return Diag(lbrac, diag::error_no_super_class_message)
Chris Lattnereb483eb2010-04-11 08:28:14 +0000609 << CurMethod->getDeclName();
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000610 ClassDecl = OID->getSuperClass();
Chris Lattner15faee12010-04-12 05:38:43 +0000611 if (ClassDecl == 0)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000612 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Chris Lattnereb483eb2010-04-11 08:28:14 +0000613 if (CurMethod->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000614 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000615 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000616 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
617 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000618 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000619 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000620 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000621 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000622
Chris Lattner15faee12010-04-12 05:38:43 +0000623 // Otherwise, if this is a class method, try dispatching to our
624 // superclass, which is in ClassDecl.
Chris Lattnera7a98c92010-04-12 17:25:51 +0000625 isSuper = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000626 }
Chris Lattner15faee12010-04-12 05:38:43 +0000627 }
628
629 if (ClassDecl == 0)
Douglas Gregorc83c6872010-04-15 22:33:43 +0000630 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Steve Naroff7c778f12008-07-25 19:39:00 +0000632 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000633 //
634 // typedef XCElementDisplayRect XCElementGraphicsRect;
635 //
636 // @implementation XCRASlice
637 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
638 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
639 // }
640 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000641 // If necessary, the following lookup could move to getObjCInterfaceDecl().
642 if (!ClassDecl) {
John McCallf36e02d2009-10-09 21:13:30 +0000643 NamedDecl *IDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +0000644 = LookupSingleName(TUScope, receiverName, receiverLoc,
645 LookupOrdinaryName);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000646 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl))
647 if (const ObjCInterfaceType *OCIT
648 = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>())
649 ClassDecl = OCIT->getDecl();
650
651 if (!ClassDecl) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000652 // Give a better error message for invalid use of super.
653 if (receiverName->isStr("super"))
654 Diag(receiverLoc, diag::err_invalid_receiver_to_message_super);
655 else
656 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
Douglas Gregorc5e77d52010-02-19 15:18:45 +0000657 return true;
Steve Naroff7c778f12008-07-25 19:39:00 +0000658 }
659 }
660 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000661 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000662 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000663 if (ClassDecl->isForwardDecl()) {
664 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000665 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000666 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
667 if (Method)
Mike Stump1eb44332009-09-09 15:08:12 +0000668 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000669 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000670 }
671 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000672 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Steve Naroff7c778f12008-07-25 19:39:00 +0000674 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000675 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000676 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000677
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000678 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
679 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000680
681 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000682 lbrac, rbrac, returnType))
683 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000684
Anders Carlsson187ba152009-05-26 15:22:25 +0000685 returnType = returnType.getNonReferenceType();
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Chris Lattnera7a98c92010-04-12 17:25:51 +0000687 // FIXME: need to do a better job handling 'super' usage within a class. For
688 // now, we simply pass the "super" identifier through (which isn't consistent
689 // with instance methods.
690 if (isSuper)
691 return new (Context) ObjCMessageExpr(Context, receiverName, receiverLoc,
692 Sel, returnType, Method, lbrac, rbrac,
693 ArgExprs, NumArgs);
694
Mike Stump390b4cc2009-05-16 07:39:55 +0000695 // If we have the ObjCInterfaceDecl* for the class that is receiving the
696 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
697 // IdentifierInfo* for the class.
Chris Lattner15faee12010-04-12 05:38:43 +0000698 return new (Context) ObjCMessageExpr(Context, ClassDecl, receiverLoc,
699 Sel, returnType, Method, lbrac, rbrac,
700 ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000701}
702
703// ActOnInstanceMessage - used for both unary and keyword messages.
704// ArgExprs is optional - if it is present, the number of expressions
705// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000706Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump1eb44332009-09-09 15:08:12 +0000707 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000708 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000709 SourceLocation rbrac,
710 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000711 assert(receiver && "missing receiver expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattner85a932e2008-01-04 22:32:30 +0000713 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
714 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnerd0d45992009-04-29 05:48:32 +0000716 // If necessary, apply function/array conversion to the receiver.
717 // C99 6.7.5.3p[7,8].
Douglas Gregora873dfc2010-02-03 00:27:59 +0000718 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Chris Lattner85a932e2008-01-04 22:32:30 +0000720 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000721 QualType ReceiverCType =
722 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000723
Mike Stump1eb44332009-09-09 15:08:12 +0000724 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000725 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000726 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000727 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
728 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000729 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000730 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump1eb44332009-09-09 15:08:12 +0000731 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000732 lbrac, rbrac, returnType))
733 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000734 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000735 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
736 Method, lbrac, rbrac,
737 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000740 // Handle messages to Class.
Mike Stump1eb44332009-09-09 15:08:12 +0000741 if (ReceiverCType->isObjCClassType() ||
Steve Naroff470301b2009-07-22 16:07:01 +0000742 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000743 ObjCMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattner6562fda2008-07-21 06:44:27 +0000745 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000746 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
747 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000748 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Steve Narofff1afaf62009-02-26 15:55:06 +0000750 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000751 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000752
753 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff470301b2009-07-22 16:07:01 +0000754 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000755 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000756 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
757 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000758 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000759 if (!Method) {
760 // If not messaging 'self', look for any factory method named 'Sel'.
761 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000762 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000763 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000764 // If no class (factory) method was found, check if an _instance_
765 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000766 Method = LookupInstanceMethodInGlobalPool(
767 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000768 if (Method)
769 if (const ObjCInterfaceDecl *ID =
770 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
771 if (ID->getSuperClass())
772 Diag(lbrac, diag::warn_root_inst_method_not_found)
773 << Sel << SourceRange(lbrac, rbrac);
774 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000775 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000776 }
777 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000778 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000779 lbrac, rbrac, returnType))
780 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000781 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000782 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType,
783 Method, lbrac, rbrac,
784 ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000787 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000788 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
790 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000791 // long as one of the protocols implements the selector (if not, warn).
Mike Stump1eb44332009-09-09 15:08:12 +0000792 if (const ObjCObjectPointerType *QIdTy =
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000793 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000794 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000795 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000796 E = QIdTy->qual_end(); I != E; ++I) {
797 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000798 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000799 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000800 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000801 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000802 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff14108da2009-07-10 23:34:53 +0000805 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000806 // We allow sending a message to a pointer to an interface (an object).
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Steve Naroff14108da2009-07-10 23:34:53 +0000808 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000809 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000810 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000811 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000812 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000814 if (!Method) {
815 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000816 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000817 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000818 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000819 break;
820 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000821 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000822 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000823 // If we have implementations in scope, check "private" methods.
824 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Steve Naroff5609ec02009-03-08 18:56:13 +0000826 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000827 // If we still haven't found a method, look in the global pool. This
828 // behavior isn't very desirable, however we need it for GCC
829 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000830 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000831 Method = LookupInstanceMethodInGlobalPool(
832 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000833 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump1eb44332009-09-09 15:08:12 +0000834 Diag(lbrac, diag::warn_maynot_respond)
Chris Lattner0784fcd2010-04-11 07:04:01 +0000835 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000836 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000837 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000838 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000839 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
840 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000841 } else if (!Context.getObjCIdType().isNull() &&
842 (ReceiverCType->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000843 (ReceiverCType->isIntegerType() &&
Chris Lattner0c73f372009-03-09 21:19:16 +0000844 ReceiverCType->isScalarType()))) {
845 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000846 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000847 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman73c39ab2009-10-20 08:27:19 +0000848 if (ReceiverCType->isPointerType())
849 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
850 else
851 ImpCastExprToType(RExpr, Context.getObjCIdType(),
852 CastExpr::CK_IntegralToPointer);
Chris Lattner0c73f372009-03-09 21:19:16 +0000853 } else {
854 // Reject other random receiver types (e.g. structs).
855 Diag(lbrac, diag::err_bad_receiver_type)
856 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000857 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000860 if (Method)
861 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000862 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000863 lbrac, rbrac, returnType))
864 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000865 returnType = returnType.getNonReferenceType();
Ted Kremenekeb3b3242010-02-11 22:41:21 +0000866 return new (Context) ObjCMessageExpr(Context, RExpr, Sel, returnType, Method,
867 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000868}
Chris Lattnereca7be62008-04-07 05:30:13 +0000869