blob: baa34f9ce8db60cac8ebac89cc32d39ddacc4b9a [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
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000016#include "clang/Sema/Scope.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Initialization.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclObjC.h"
Steve Narofff494b572008-05-29 21:12:08 +000020#include "clang/AST/ExprObjC.h"
Douglas Gregor2725ca82010-04-21 19:57:20 +000021#include "clang/AST/TypeLoc.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000022#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000023#include "clang/Lex/Preprocessor.h"
24
Chris Lattner85a932e2008-01-04 22:32:30 +000025using namespace clang;
26
John McCallf312b1e2010-08-26 23:41:50 +000027ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
28 Expr **strings,
29 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000030 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
31
Chris Lattnerf4b136f2009-02-18 06:13:04 +000032 // Most ObjC strings are formed out of a single piece. However, we *can*
33 // have strings formed out of multiple @ strings with multiple pptokens in
34 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
35 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000036 StringLiteral *S = Strings[0];
Mike Stump1eb44332009-09-09 15:08:12 +000037
Chris Lattnerf4b136f2009-02-18 06:13:04 +000038 // If we have a multi-part string, merge it all together.
39 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000040 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000041 llvm::SmallString<128> StrBuf;
42 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner726e1682009-02-18 05:49:11 +000044 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000045 S = Strings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000046
Chris Lattner39c28bb2009-02-18 06:48:40 +000047 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000048 if (S->isWide()) {
49 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
50 << S->getSourceRange();
51 return true;
52 }
Mike Stump1eb44332009-09-09 15:08:12 +000053
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +000054 // Append the string.
55 StrBuf += S->getString();
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner39c28bb2009-02-18 06:48:40 +000057 // Get the locations of the string tokens.
58 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
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);
Fariborz Jahanian8a437762010-04-23 23:19:04 +000079 } else if (getLangOptions().NoConstantCFStrings) {
Fariborz Jahanian4c733072010-10-19 17:19:29 +000080 IdentifierInfo *NSIdent=0;
81 std::string StringClass(getLangOptions().ObjCConstantStringClass);
82
83 if (StringClass.empty())
84 NSIdent = &Context.Idents.get("NSConstantString");
85 else
86 NSIdent = &Context.Idents.get(StringClass);
87
Fariborz Jahanian8a437762010-04-23 23:19:04 +000088 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
89 LookupOrdinaryName);
90 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
91 Context.setObjCConstantStringInterface(StrIF);
92 Ty = Context.getObjCConstantStringInterface();
93 Ty = Context.getObjCObjectPointerType(Ty);
94 } else {
95 // If there is no NSConstantString interface defined then treat this
96 // as error and recover from it.
97 Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent
98 << S->getSourceRange();
99 Ty = Context.getObjCIdType();
100 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000101 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000102 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000103 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
104 LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000105 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
106 Context.setObjCConstantStringInterface(StrIF);
107 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +0000108 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000109 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000110 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000111 // strings as untyped objects and let the runtime figure it out later.
112 Ty = Context.getObjCIdType();
113 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000114 }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattnerf4b136f2009-02-18 06:13:04 +0000116 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +0000117}
118
Mike Stump1eb44332009-09-09 15:08:12 +0000119Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +0000120 TypeSourceInfo *EncodedTypeInfo,
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000121 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +0000122 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000123 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000124 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000125 StrTy = Context.DependentTy;
126 else {
127 std::string Str;
128 Context.getObjCEncodingForType(EncodedType, Str);
129
130 // The type of @encode is the same as the type of the corresponding string,
131 // which is an array type.
132 StrTy = Context.CharTy;
133 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000134 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000135 StrTy.addConst();
136 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
137 ArrayType::Normal, 0);
138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Douglas Gregor81d34662010-04-20 15:39:42 +0000140 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000141}
142
John McCallf312b1e2010-08-26 23:41:50 +0000143ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
144 SourceLocation EncodeLoc,
145 SourceLocation LParenLoc,
146 ParsedType ty,
147 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000148 // FIXME: Preserve type source info ?
Douglas Gregor81d34662010-04-20 15:39:42 +0000149 TypeSourceInfo *TInfo;
150 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
151 if (!TInfo)
152 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
153 PP.getLocForEndOfToken(LParenLoc));
Chris Lattner85a932e2008-01-04 22:32:30 +0000154
Douglas Gregor81d34662010-04-20 15:39:42 +0000155 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000156}
157
John McCallf312b1e2010-08-26 23:41:50 +0000158ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
159 SourceLocation AtLoc,
160 SourceLocation SelLoc,
161 SourceLocation LParenLoc,
162 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000163 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000164 SourceRange(LParenLoc, RParenLoc), false, false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000165 if (!Method)
166 Method = LookupFactoryMethodInGlobalPool(Sel,
167 SourceRange(LParenLoc, RParenLoc));
168 if (!Method)
169 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
170
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000171 llvm::DenseMap<Selector, SourceLocation>::iterator Pos
172 = ReferencedSelectors.find(Sel);
173 if (Pos == ReferencedSelectors.end())
174 ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
175
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000176 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000177 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000178}
179
John McCallf312b1e2010-08-26 23:41:50 +0000180ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
181 SourceLocation AtLoc,
182 SourceLocation ProtoLoc,
183 SourceLocation LParenLoc,
184 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000185 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000186 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000187 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000188 return true;
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000191 QualType Ty = Context.getObjCProtoType();
192 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000193 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000194 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000195 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000196}
197
Mike Stump1eb44332009-09-09 15:08:12 +0000198bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
199 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000200 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000201 SourceLocation lbrac, SourceLocation rbrac,
John McCallf89e55a2010-11-18 06:31:45 +0000202 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000203 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000204 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregor92e986e2010-04-22 16:44:27 +0000205 for (unsigned i = 0; i != NumArgs; i++) {
206 if (Args[i]->isTypeDependent())
207 continue;
208
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000209 DefaultArgumentPromotion(Args[i]);
Douglas Gregor92e986e2010-04-22 16:44:27 +0000210 }
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000211
Chris Lattner077bf5e2008-11-24 03:33:13 +0000212 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
213 diag::warn_inst_method_not_found;
214 Diag(lbrac, DiagID)
215 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000216 ReturnType = Context.getObjCIdType();
John McCallf89e55a2010-11-18 06:31:45 +0000217 VK = VK_RValue;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000218 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000221 ReturnType = Method->getSendResultType();
John McCallf89e55a2010-11-18 06:31:45 +0000222 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000224 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000225 // Method might have more arguments than selector indicates. This is due
226 // to addition of c-style arguments in method.
227 if (Method->param_size() > Sel.getNumArgs())
228 NumNamedArgs = Method->param_size();
229 // FIXME. This need be cleaned up.
230 if (NumArgs < NumNamedArgs) {
John McCallf89e55a2010-11-18 06:31:45 +0000231 Diag(lbrac, diag::err_typecheck_call_too_few_args)
232 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000233 return false;
234 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000235
Chris Lattner312531a2009-04-12 08:11:20 +0000236 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000237 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000238 // We can't do any type-checking on a type-dependent argument.
239 if (Args[i]->isTypeDependent())
240 continue;
241
Chris Lattner85a932e2008-01-04 22:32:30 +0000242 Expr *argExpr = Args[i];
Douglas Gregor92e986e2010-04-22 16:44:27 +0000243
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000244 ParmVarDecl *Param = Method->param_begin()[i];
Chris Lattner85a932e2008-01-04 22:32:30 +0000245 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000247 if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
248 Param->getType(),
249 PDiag(diag::err_call_incomplete_argument)
250 << argExpr->getSourceRange()))
251 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000252
Fariborz Jahanian745da3a2010-09-24 17:30:16 +0000253 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
254 Param);
John McCall3fa5cae2010-10-26 07:05:15 +0000255 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000256 if (ArgE.isInvalid())
257 IsError = true;
258 else
259 Args[i] = ArgE.takeAs<Expr>();
Chris Lattner85a932e2008-01-04 22:32:30 +0000260 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000261
262 // Promote additional arguments to variadic methods.
263 if (Method->isVariadic()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000264 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
265 if (Args[i]->isTypeDependent())
266 continue;
267
Chris Lattner40378332010-05-16 04:01:30 +0000268 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
Douglas Gregor92e986e2010-04-22 16:44:27 +0000269 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000270 } else {
271 // Check for extra arguments to non-variadic methods.
272 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000274 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000275 << 2 /*method*/ << NumNamedArgs << NumArgs
276 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000277 << SourceRange(Args[NumNamedArgs]->getLocStart(),
278 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000279 }
280 }
281
Douglas Gregor2725ca82010-04-21 19:57:20 +0000282 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Chris Lattner312531a2009-04-12 08:11:20 +0000283 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000284}
285
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000286bool Sema::isSelfExpr(Expr *RExpr) {
John McCallf6a16482010-12-04 03:47:34 +0000287 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
288 if (ICE->getCastKind() == CK_LValueToRValue)
289 RExpr = ICE->getSubExpr();
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000290 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
291 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
292 return true;
293 return false;
294}
295
Steve Narofff1afaf62009-02-26 15:55:06 +0000296// Helper method for ActOnClassMethod/ActOnInstanceMethod.
297// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000298// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000299// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000300ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000301 ObjCInterfaceDecl *ClassDecl) {
302 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000303 // lookup in class and all superclasses
304 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000305 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000306 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Steve Naroff5609ec02009-03-08 18:56:13 +0000308 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000309 if (!Method)
310 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Steve Naroff5609ec02009-03-08 18:56:13 +0000312 // Before we give up, check if the selector is an instance method.
313 // But only in the root. This matches gcc's behaviour and what the
314 // runtime expects.
315 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000316 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000317 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000318 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000319 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000320 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Steve Naroff5609ec02009-03-08 18:56:13 +0000323 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000324 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000325 return Method;
326}
327
328ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
329 ObjCInterfaceDecl *ClassDecl) {
330 ObjCMethodDecl *Method = 0;
331 while (ClassDecl && !Method) {
332 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000333 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000334 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Steve Naroff5609ec02009-03-08 18:56:13 +0000336 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000337 if (!Method)
338 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000339 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000340 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000341 return Method;
342}
343
Chris Lattner7f816522010-04-11 07:45:24 +0000344/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
345/// objective C interface. This is a property reference expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000346ExprResult Sema::
Chris Lattner7f816522010-04-11 07:45:24 +0000347HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000348 Expr *BaseExpr, DeclarationName MemberName,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000349 SourceLocation MemberLoc,
350 SourceLocation SuperLoc, QualType SuperType,
351 bool Super) {
Chris Lattner7f816522010-04-11 07:45:24 +0000352 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
353 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
354 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
355
Fariborz Jahanian8b1aba42010-12-16 00:56:28 +0000356 if (IFace->isForwardDecl()) {
357 Diag(MemberLoc, diag::err_property_not_found_forward_class)
358 << MemberName << QualType(OPT, 0);
359 Diag(IFace->getLocation(), diag::note_forward_class);
360 return ExprError();
361 }
Chris Lattner7f816522010-04-11 07:45:24 +0000362 // Search for a declared property first.
363 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
364 // Check whether we can reference this property.
365 if (DiagnoseUseOfDecl(PD, MemberLoc))
366 return ExprError();
367 QualType ResTy = PD->getType();
368 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
369 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
370 if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
John McCallf89e55a2010-11-18 06:31:45 +0000371 ResTy = Getter->getResultType();
372
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000373 if (Super)
374 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000375 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000376 MemberLoc,
377 SuperLoc, SuperType));
378 else
379 return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
John McCallf89e55a2010-11-18 06:31:45 +0000380 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000381 MemberLoc, BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000382 }
383 // Check protocols on qualified interfaces.
384 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
385 E = OPT->qual_end(); I != E; ++I)
386 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
387 // Check whether we can reference this property.
388 if (DiagnoseUseOfDecl(PD, MemberLoc))
389 return ExprError();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000390 if (Super)
John McCallf89e55a2010-11-18 06:31:45 +0000391 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
392 VK_LValue,
393 OK_ObjCProperty,
394 MemberLoc,
395 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000396 else
397 return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000398 VK_LValue,
399 OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000400 MemberLoc,
401 BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000402 }
403 // If that failed, look for an "implicit" property by seeing if the nullary
404 // selector is implemented.
405
406 // FIXME: The logic for looking up nullary and unary selectors should be
407 // shared with the code in ActOnInstanceMessage.
408
409 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
410 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
411
412 // If this reference is in an @implementation, check for 'private' methods.
413 if (!Getter)
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000414 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner7f816522010-04-11 07:45:24 +0000415
416 // Look through local category implementations associated with the class.
417 if (!Getter)
418 Getter = IFace->getCategoryInstanceMethod(Sel);
419 if (Getter) {
420 // Check if we can reference this property.
421 if (DiagnoseUseOfDecl(Getter, MemberLoc))
422 return ExprError();
423 }
424 // If we found a getter then this may be a valid dot-reference, we
425 // will look for the matching setter, in case it is needed.
426 Selector SetterSel =
427 SelectorTable::constructSetterName(PP.getIdentifierTable(),
428 PP.getSelectorTable(), Member);
429 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
430 if (!Setter) {
431 // If this reference is in an @implementation, also check for 'private'
432 // methods.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000433 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner7f816522010-04-11 07:45:24 +0000434 }
435 // Look through local category implementations associated with the class.
436 if (!Setter)
437 Setter = IFace->getCategoryInstanceMethod(SetterSel);
438
439 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
440 return ExprError();
441
442 if (Getter) {
John McCall09431682010-11-18 19:01:18 +0000443 QualType PType = Getter->getSendResultType();
444 ExprValueKind VK = VK_LValue;
445 ExprObjectKind OK = OK_ObjCProperty;
446 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
447 PType->isVoidType())
448 VK = VK_RValue, OK = OK_Ordinary;
449
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000450 if (Super)
John McCall12f78a62010-12-02 01:19:52 +0000451 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
452 PType, VK, OK,
453 MemberLoc,
454 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000455 else
John McCall12f78a62010-12-02 01:19:52 +0000456 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
457 PType, VK, OK,
458 MemberLoc, BaseExpr));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000459
Chris Lattner7f816522010-04-11 07:45:24 +0000460 }
461
462 // Attempt to correct for typos in property names.
463 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000464 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000465 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000466 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000467 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000468 << MemberName << QualType(OPT, 0) << TypoResult
469 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000470 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
471 Diag(Property->getLocation(), diag::note_previous_decl)
472 << Property->getDeclName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000473 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
474 SuperLoc, SuperType, Super);
Chris Lattner7f816522010-04-11 07:45:24 +0000475 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000476
Chris Lattner7f816522010-04-11 07:45:24 +0000477 Diag(MemberLoc, diag::err_property_not_found)
478 << MemberName << QualType(OPT, 0);
479 if (Setter && !Getter)
480 Diag(Setter->getLocation(), diag::note_getter_unavailable)
481 << MemberName << BaseExpr->getSourceRange();
482 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000483}
484
485
486
John McCall60d7b3a2010-08-24 06:29:42 +0000487ExprResult Sema::
Chris Lattnereb483eb2010-04-11 08:28:14 +0000488ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
489 IdentifierInfo &propertyName,
490 SourceLocation receiverNameLoc,
491 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000493 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000494 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
495 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000496 if (IFace == 0) {
497 // If the "receiver" is 'super' in a method, handle it as an expression-like
498 // property reference.
499 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
500 if (receiverNamePtr->isStr("super")) {
501 if (CurMethod->isInstanceMethod()) {
502 QualType T =
503 Context.getObjCInterfaceType(CurMethod->getClassInterface());
504 T = Context.getObjCObjectPointerType(T);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000505
506 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000507 /*BaseExpr*/0, &propertyName,
508 propertyNameLoc,
509 receiverNameLoc, T, true);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattnereb483eb2010-04-11 08:28:14 +0000512 // Otherwise, if this is a class method, try dispatching to our
513 // superclass.
514 IFace = CurMethod->getClassInterface()->getSuperClass();
515 }
516
517 if (IFace == 0) {
518 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
519 return ExprError();
520 }
521 }
522
523 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000524 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000525 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000526
527 // If this reference is in an @implementation, check for 'private' methods.
528 if (!Getter)
529 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
530 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000531 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000532 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000533
534 if (Getter) {
535 // FIXME: refactor/share with ActOnMemberReference().
536 // Check if we can reference this property.
537 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
538 return ExprError();
539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Steve Naroff61f72cb2009-03-09 21:12:44 +0000541 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000542 Selector SetterSel =
543 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000544 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000546 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000547 if (!Setter) {
548 // If this reference is in an @implementation, also check for 'private'
549 // methods.
550 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
551 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000552 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000553 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000554 }
555 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000556 if (!Setter)
557 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000558
559 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
560 return ExprError();
561
562 if (Getter || Setter) {
563 QualType PType;
564
John McCall09431682010-11-18 19:01:18 +0000565 ExprValueKind VK = VK_LValue;
566 if (Getter) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000567 PType = Getter->getSendResultType();
John McCall09431682010-11-18 19:01:18 +0000568 if (!getLangOptions().CPlusPlus &&
569 !PType.hasQualifiers() && PType->isVoidType())
570 VK = VK_RValue;
571 } else {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000572 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
573 E = Setter->param_end(); PI != E; ++PI)
574 PType = (*PI)->getType();
John McCall09431682010-11-18 19:01:18 +0000575 VK = VK_LValue;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000576 }
John McCall09431682010-11-18 19:01:18 +0000577
578 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
579
John McCall12f78a62010-12-02 01:19:52 +0000580 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
581 PType, VK, OK,
582 propertyNameLoc,
583 receiverNameLoc, IFace));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000584 }
585 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
586 << &propertyName << Context.getObjCInterfaceType(IFace));
587}
588
Douglas Gregor47bd5432010-04-14 02:46:37 +0000589Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +0000590 IdentifierInfo *Name,
Douglas Gregor47bd5432010-04-14 02:46:37 +0000591 SourceLocation NameLoc,
592 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +0000593 bool HasTrailingDot,
John McCallb3d87482010-08-24 05:47:05 +0000594 ParsedType &ReceiverType) {
595 ReceiverType = ParsedType();
Douglas Gregor1569f952010-04-21 20:38:13 +0000596
Douglas Gregor47bd5432010-04-14 02:46:37 +0000597 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor95f42922010-10-14 22:11:03 +0000598 // messaging super. If the identifier is "super" and there is a
599 // trailing dot, it's an instance message.
600 if (IsSuper && S->isInObjcMethodScope())
601 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000602
603 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
604 LookupName(Result, S);
605
606 switch (Result.getResultKind()) {
607 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000608 // Normal name lookup didn't find anything. If we're in an
609 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor95f42922010-10-14 22:11:03 +0000610 // FIXME: This is a hack. Ivar lookup should be part of normal
611 // lookup.
Douglas Gregored464422010-04-19 20:09:36 +0000612 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
613 ObjCInterfaceDecl *ClassDeclared;
614 if (Method->getClassInterface()->lookupInstanceVariable(Name,
615 ClassDeclared))
616 return ObjCInstanceMessage;
617 }
Douglas Gregor95f42922010-10-14 22:11:03 +0000618
Douglas Gregor47bd5432010-04-14 02:46:37 +0000619 // Break out; we'll perform typo correction below.
620 break;
621
622 case LookupResult::NotFoundInCurrentInstantiation:
623 case LookupResult::FoundOverloaded:
624 case LookupResult::FoundUnresolvedValue:
625 case LookupResult::Ambiguous:
626 Result.suppressDiagnostics();
627 return ObjCInstanceMessage;
628
629 case LookupResult::Found: {
630 // We found something. If it's a type, then we have a class
631 // message. Otherwise, it's an instance message.
632 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000633 QualType T;
634 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
635 T = Context.getObjCInterfaceType(Class);
636 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
637 T = Context.getTypeDeclType(Type);
638 else
639 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000640
Douglas Gregor1569f952010-04-21 20:38:13 +0000641 // We have a class message, and T is the type we're
642 // messaging. Build source-location information for it.
643 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000644 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor1569f952010-04-21 20:38:13 +0000645 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000646 }
647 }
648
Douglas Gregoraaf87162010-04-14 20:04:41 +0000649 // Determine our typo-correction context.
650 CorrectTypoContext CTC = CTC_Expression;
651 if (ObjCMethodDecl *Method = getCurMethodDecl())
652 if (Method->getClassInterface() &&
653 Method->getClassInterface()->getSuperClass())
654 CTC = CTC_ObjCMessageReceiver;
655
656 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
657 if (Result.isSingleResult()) {
658 // If we found a declaration, correct when it refers to an Objective-C
659 // class.
660 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000661 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregoraaf87162010-04-14 20:04:41 +0000662 Diag(NameLoc, diag::err_unknown_receiver_suggest)
663 << Name << Result.getLookupName()
664 << FixItHint::CreateReplacement(SourceRange(NameLoc),
665 ND->getNameAsString());
666 Diag(ND->getLocation(), diag::note_previous_decl)
667 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000668
Douglas Gregor1569f952010-04-21 20:38:13 +0000669 QualType T = Context.getObjCInterfaceType(Class);
670 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000671 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000672 return ObjCClassMessage;
673 }
674 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
675 Corrected.getAsIdentifierInfo()->isStr("super")) {
676 // If we've found the keyword "super", this is a send to super.
677 Diag(NameLoc, diag::err_unknown_receiver_suggest)
678 << Name << Corrected
679 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
680 Name = Corrected.getAsIdentifierInfo();
681 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000682 }
683 }
684
685 // Fall back: let the parser try to parse it as an instance message.
686 return ObjCInstanceMessage;
687}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000688
John McCall60d7b3a2010-08-24 06:29:42 +0000689ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000690 SourceLocation SuperLoc,
691 Selector Sel,
692 SourceLocation LBracLoc,
693 SourceLocation SelectorLoc,
694 SourceLocation RBracLoc,
695 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000696 // Determine whether we are inside a method or not.
697 ObjCMethodDecl *Method = getCurMethodDecl();
Douglas Gregorf95861a2010-04-21 20:01:04 +0000698 if (!Method) {
699 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
700 return ExprError();
701 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000702
Douglas Gregorf95861a2010-04-21 20:01:04 +0000703 ObjCInterfaceDecl *Class = Method->getClassInterface();
704 if (!Class) {
705 Diag(SuperLoc, diag::error_no_super_class_message)
706 << Method->getDeclName();
707 return ExprError();
708 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000709
Douglas Gregorf95861a2010-04-21 20:01:04 +0000710 ObjCInterfaceDecl *Super = Class->getSuperClass();
711 if (!Super) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000712 // The current class does not have a superclass.
713 Diag(SuperLoc, diag::error_no_super_class) << Class->getIdentifier();
714 return ExprError();
Chris Lattner15faee12010-04-12 05:38:43 +0000715 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000716
Douglas Gregorf95861a2010-04-21 20:01:04 +0000717 // We are in a method whose class has a superclass, so 'super'
718 // is acting as a keyword.
719 if (Method->isInstanceMethod()) {
720 // Since we are in an instance method, this is an instance
721 // message to the superclass instance.
722 QualType SuperTy = Context.getObjCInterfaceType(Super);
723 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCall9ae2f072010-08-23 23:25:46 +0000724 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000725 Sel, /*Method=*/0,
726 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000727 }
Douglas Gregorf95861a2010-04-21 20:01:04 +0000728
729 // Since we are in a class method, this is a class message to
730 // the superclass.
731 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
732 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000733 SuperLoc, Sel, /*Method=*/0,
734 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000735}
736
737/// \brief Build an Objective-C class message expression.
738///
739/// This routine takes care of both normal class messages and
740/// class messages to the superclass.
741///
742/// \param ReceiverTypeInfo Type source information that describes the
743/// receiver of this message. This may be NULL, in which case we are
744/// sending to the superclass and \p SuperLoc must be a valid source
745/// location.
746
747/// \param ReceiverType The type of the object receiving the
748/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
749/// type as that refers to. For a superclass send, this is the type of
750/// the superclass.
751///
752/// \param SuperLoc The location of the "super" keyword in a
753/// superclass message.
754///
755/// \param Sel The selector to which the message is being sent.
756///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000757/// \param Method The method that this class message is invoking, if
758/// already known.
759///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000760/// \param LBracLoc The location of the opening square bracket ']'.
761///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000762/// \param RBrac The location of the closing square bracket ']'.
763///
764/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000765ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000766 QualType ReceiverType,
767 SourceLocation SuperLoc,
768 Selector Sel,
769 ObjCMethodDecl *Method,
770 SourceLocation LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000771 SourceLocation SelectorLoc,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000772 SourceLocation RBracLoc,
773 MultiExprArg ArgsIn) {
774 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregor9497a732010-09-16 01:51:54 +0000775 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregor0fbda682010-09-15 14:51:05 +0000776 if (LBracLoc.isInvalid()) {
777 Diag(Loc, diag::err_missing_open_square_message_send)
778 << FixItHint::CreateInsertion(Loc, "[");
779 LBracLoc = Loc;
780 }
781
Douglas Gregor92e986e2010-04-22 16:44:27 +0000782 if (ReceiverType->isDependentType()) {
783 // If the receiver type is dependent, we can't type-check anything
784 // at this point. Build a dependent expression.
785 unsigned NumArgs = ArgsIn.size();
786 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
787 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCallf89e55a2010-11-18 06:31:45 +0000788 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
789 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000790 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregor92e986e2010-04-22 16:44:27 +0000791 Args, NumArgs, RBracLoc));
792 }
Chris Lattner15faee12010-04-12 05:38:43 +0000793
Douglas Gregor2725ca82010-04-21 19:57:20 +0000794 // Find the class to which we are sending this message.
795 ObjCInterfaceDecl *Class = 0;
John McCallc12c5bb2010-05-15 11:32:37 +0000796 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
797 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000798 Diag(Loc, diag::err_invalid_receiver_class_message)
799 << ReceiverType;
800 return ExprError();
Steve Naroff7c778f12008-07-25 19:39:00 +0000801 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000802 assert(Class && "We don't know which class we're messaging?");
803
804 // Find the method we are messaging.
Douglas Gregorf49bb082010-04-22 17:01:48 +0000805 if (!Method) {
806 if (Class->isForwardDecl()) {
807 // A forward class used in messaging is treated as a 'Class'
808 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
809 Method = LookupFactoryMethodInGlobalPool(Sel,
810 SourceRange(LBracLoc, RBracLoc));
811 if (Method)
812 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
813 << Method->getDeclName();
814 }
815 if (!Method)
816 Method = Class->lookupClassMethod(Sel);
817
818 // If we have an implementation in scope, check "private" methods.
819 if (!Method)
820 Method = LookupPrivateClassMethod(Sel, Class);
821
822 if (Method && DiagnoseUseOfDecl(Method, Loc))
823 return ExprError();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Douglas Gregor2725ca82010-04-21 19:57:20 +0000826 // Check the argument types and determine the result type.
827 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +0000828 ExprValueKind VK = VK_RValue;
829
Douglas Gregor2725ca82010-04-21 19:57:20 +0000830 unsigned NumArgs = ArgsIn.size();
831 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
832 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCallf89e55a2010-11-18 06:31:45 +0000833 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +0000834 return ExprError();
Ted Kremenek4df728e2008-06-24 15:50:53 +0000835
Douglas Gregor2725ca82010-04-21 19:57:20 +0000836 // Construct the appropriate ObjCMessageExpr.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000837 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +0000838 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +0000839 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000840 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000841 ReceiverType, Sel, SelectorLoc,
842 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000843 else
John McCallf89e55a2010-11-18 06:31:45 +0000844 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000845 ReceiverTypeInfo, Sel, SelectorLoc,
846 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000847 return MaybeBindToTemporary(Result);
Chris Lattner85a932e2008-01-04 22:32:30 +0000848}
849
Douglas Gregor2725ca82010-04-21 19:57:20 +0000850// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattner85a932e2008-01-04 22:32:30 +0000851// ArgExprs is optional - if it is present, the number of expressions
852// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +0000853ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor77328d12010-09-15 23:19:31 +0000854 ParsedType Receiver,
855 Selector Sel,
856 SourceLocation LBracLoc,
857 SourceLocation SelectorLoc,
858 SourceLocation RBracLoc,
859 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000860 TypeSourceInfo *ReceiverTypeInfo;
861 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
862 if (ReceiverType.isNull())
863 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor2725ca82010-04-21 19:57:20 +0000866 if (!ReceiverTypeInfo)
867 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
868
869 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorf49bb082010-04-22 17:01:48 +0000870 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000871 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000872}
873
874/// \brief Build an Objective-C instance message expression.
875///
876/// This routine takes care of both normal instance messages and
877/// instance messages to the superclass instance.
878///
879/// \param Receiver The expression that computes the object that will
880/// receive this message. This may be empty, in which case we are
881/// sending to the superclass instance and \p SuperLoc must be a valid
882/// source location.
883///
884/// \param ReceiverType The (static) type of the object receiving the
885/// message. When a \p Receiver expression is provided, this is the
886/// same type as that expression. For a superclass instance send, this
887/// is a pointer to the type of the superclass.
888///
889/// \param SuperLoc The location of the "super" keyword in a
890/// superclass instance message.
891///
892/// \param Sel The selector to which the message is being sent.
893///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000894/// \param Method The method that this instance message is invoking, if
895/// already known.
896///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000897/// \param LBracLoc The location of the opening square bracket ']'.
898///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000899/// \param RBrac The location of the closing square bracket ']'.
900///
901/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000902ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000903 QualType ReceiverType,
904 SourceLocation SuperLoc,
905 Selector Sel,
906 ObjCMethodDecl *Method,
907 SourceLocation LBracLoc,
908 SourceLocation SelectorLoc,
909 SourceLocation RBracLoc,
910 MultiExprArg ArgsIn) {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000911 // The location of the receiver.
912 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
913
914 if (LBracLoc.isInvalid()) {
915 Diag(Loc, diag::err_missing_open_square_message_send)
916 << FixItHint::CreateInsertion(Loc, "[");
917 LBracLoc = Loc;
918 }
919
Douglas Gregor2725ca82010-04-21 19:57:20 +0000920 // If we have a receiver expression, perform appropriate promotions
921 // and determine receiver type.
Douglas Gregor2725ca82010-04-21 19:57:20 +0000922 if (Receiver) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000923 if (Receiver->isTypeDependent()) {
924 // If the receiver is type-dependent, we can't type-check anything
925 // at this point. Build a dependent expression.
926 unsigned NumArgs = ArgsIn.size();
927 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
928 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
929 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCallf89e55a2010-11-18 06:31:45 +0000930 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000931 SelectorLoc, /*Method=*/0,
932 Args, NumArgs, RBracLoc));
Douglas Gregor92e986e2010-04-22 16:44:27 +0000933 }
934
Douglas Gregor2725ca82010-04-21 19:57:20 +0000935 // If necessary, apply function/array conversion to the receiver.
936 // C99 6.7.5.3p[7,8].
937 DefaultFunctionArrayLvalueConversion(Receiver);
938 ReceiverType = Receiver->getType();
939 }
940
Douglas Gregorf49bb082010-04-22 17:01:48 +0000941 if (!Method) {
942 // Handle messages to id.
Fariborz Jahanianba551982010-08-10 18:10:50 +0000943 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000944 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorf49bb082010-04-22 17:01:48 +0000945 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
946 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000947 SourceRange(LBracLoc, RBracLoc),
948 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000949 if (!Method)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000950 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000951 SourceRange(LBracLoc, RBracLoc),
952 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000953 } else if (ReceiverType->isObjCClassType() ||
954 ReceiverType->isObjCQualifiedClassType()) {
955 // Handle messages to Class.
956 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
957 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
958 // First check the public methods in the class interface.
959 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregorf49bb082010-04-22 17:01:48 +0000961 if (!Method)
962 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Douglas Gregor04badcf2010-04-21 00:45:42 +0000963
Douglas Gregorf49bb082010-04-22 17:01:48 +0000964 // FIXME: if we still haven't found a method, we need to look in
965 // protocols (if we have qualifiers).
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000966 }
Douglas Gregorf49bb082010-04-22 17:01:48 +0000967 if (Method && DiagnoseUseOfDecl(Method, Loc))
968 return ExprError();
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000969 }
Douglas Gregor04badcf2010-04-21 00:45:42 +0000970 if (!Method) {
Douglas Gregorf49bb082010-04-22 17:01:48 +0000971 // If not messaging 'self', look for any factory method named 'Sel'.
972 if (!Receiver || !isSelfExpr(Receiver)) {
973 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000974 SourceRange(LBracLoc, RBracLoc),
975 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000976 if (!Method) {
977 // If no class (factory) method was found, check if an _instance_
978 // method of the same name exists in the root class only.
Douglas Gregor2725ca82010-04-21 19:57:20 +0000979 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000980 SourceRange(LBracLoc, RBracLoc),
981 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000982 if (Method)
983 if (const ObjCInterfaceDecl *ID =
984 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
985 if (ID->getSuperClass())
986 Diag(Loc, diag::warn_root_inst_method_not_found)
987 << Sel << SourceRange(LBracLoc, RBracLoc);
988 }
Douglas Gregor04badcf2010-04-21 00:45:42 +0000989 }
990 }
991 }
Douglas Gregor04badcf2010-04-21 00:45:42 +0000992 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +0000993 ObjCInterfaceDecl* ClassDecl = 0;
994
995 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
996 // long as one of the protocols implements the selector (if not, warn).
997 if (const ObjCObjectPointerType *QIdTy
998 = ReceiverType->getAsObjCQualifiedIdType()) {
999 // Search protocols for instance methods.
1000 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
1001 E = QIdTy->qual_end(); I != E; ++I) {
1002 ObjCProtocolDecl *PDecl = *I;
1003 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
1004 break;
1005 // Since we aren't supporting "Class<foo>", look for a class method.
1006 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
1007 break;
1008 }
1009 } else if (const ObjCObjectPointerType *OCIType
1010 = ReceiverType->getAsObjCInterfacePointerType()) {
1011 // We allow sending a message to a pointer to an interface (an object).
1012 ClassDecl = OCIType->getInterfaceDecl();
1013 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1014 // faster than the following method (which can do *many* linear searches).
Sebastian Redldb9d2142010-08-02 23:18:59 +00001015 // The idea is to add class info to MethodPool.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001016 Method = ClassDecl->lookupInstanceMethod(Sel);
1017
1018 if (!Method) {
1019 // Search protocol qualifiers.
1020 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
1021 E = OCIType->qual_end(); QI != E; ++QI) {
1022 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
1023 break;
1024 }
1025 }
1026 if (!Method) {
1027 // If we have implementations in scope, check "private" methods.
1028 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1029
1030 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1031 // If we still haven't found a method, look in the global pool. This
1032 // behavior isn't very desirable, however we need it for GCC
1033 // compatibility. FIXME: should we deviate??
1034 if (OCIType->qual_empty()) {
1035 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001036 SourceRange(LBracLoc, RBracLoc));
Douglas Gregorf49bb082010-04-22 17:01:48 +00001037 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
1038 Diag(Loc, diag::warn_maynot_respond)
1039 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1040 }
1041 }
1042 }
1043 if (Method && DiagnoseUseOfDecl(Method, Loc))
1044 return ExprError();
1045 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001046 (ReceiverType->isPointerType() ||
1047 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001048 // Implicitly convert integers and pointers to 'id' but emit a warning.
1049 Diag(Loc, diag::warn_bad_receiver_type)
1050 << ReceiverType
1051 << Receiver->getSourceRange();
1052 if (ReceiverType->isPointerType())
1053 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall2de56d12010-08-25 11:45:40 +00001054 CK_BitCast);
John McCall404cd162010-11-13 01:35:44 +00001055 else {
1056 // TODO: specialized warning on null receivers?
1057 bool IsNull = Receiver->isNullPointerConstant(Context,
1058 Expr::NPC_ValueDependentIsNull);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001059 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall404cd162010-11-13 01:35:44 +00001060 IsNull ? CK_NullToPointer : CK_IntegralToPointer);
1061 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001062 ReceiverType = Receiver->getType();
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001063 }
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001064 else if (getLangOptions().CPlusPlus &&
1065 !PerformContextuallyConvertToObjCId(Receiver)) {
1066 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1067 Receiver = ICE->getSubExpr();
1068 ReceiverType = Receiver->getType();
1069 }
John McCall9ae2f072010-08-23 23:25:46 +00001070 return BuildInstanceMessage(Receiver,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001071 ReceiverType,
1072 SuperLoc,
1073 Sel,
1074 Method,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001075 LBracLoc,
1076 SelectorLoc,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001077 RBracLoc,
1078 move(ArgsIn));
Douglas Gregorf49bb082010-04-22 17:01:48 +00001079 } else {
1080 // Reject other random receiver types (e.g. structs).
1081 Diag(Loc, diag::err_bad_receiver_type)
1082 << ReceiverType << Receiver->getSourceRange();
1083 return ExprError();
1084 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001085 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Douglas Gregor2725ca82010-04-21 19:57:20 +00001088 // Check the message arguments.
1089 unsigned NumArgs = ArgsIn.size();
1090 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1091 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001092 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001093 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1094 ReceiverType->isObjCQualifiedClassType());
1095 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCallf89e55a2010-11-18 06:31:45 +00001096 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001097 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001098
1099 if (!ReturnType->isVoidType()) {
1100 if (RequireCompleteType(LBracLoc, ReturnType,
1101 diag::err_illegal_message_expr_incomplete_type))
1102 return ExprError();
1103 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001104
Douglas Gregor2725ca82010-04-21 19:57:20 +00001105 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001106 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001107 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001108 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001109 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001110 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001111 Args, NumArgs, RBracLoc);
1112 else
John McCallf89e55a2010-11-18 06:31:45 +00001113 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001114 Receiver, Sel, SelectorLoc, Method,
1115 Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001116 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001117}
1118
1119// ActOnInstanceMessage - used for both unary and keyword messages.
1120// ArgExprs is optional - if it is present, the number of expressions
1121// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001122ExprResult Sema::ActOnInstanceMessage(Scope *S,
1123 Expr *Receiver,
1124 Selector Sel,
1125 SourceLocation LBracLoc,
1126 SourceLocation SelectorLoc,
1127 SourceLocation RBracLoc,
1128 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001129 if (!Receiver)
1130 return ExprError();
1131
John McCall9ae2f072010-08-23 23:25:46 +00001132 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001133 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001134 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001135}
Chris Lattnereca7be62008-04-07 05:30:13 +00001136