blob: 5a257eaa33f82d83b8766e49a8dc229a04b100a8 [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
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000442 if (Getter || Setter) {
443 QualType PType;
444 if (Getter)
445 PType = Getter->getSendResultType();
446 else {
447 ParmVarDecl *ArgDecl = *Setter->param_begin();
448 PType = ArgDecl->getType();
449 }
450
John McCall09431682010-11-18 19:01:18 +0000451 ExprValueKind VK = VK_LValue;
452 ExprObjectKind OK = OK_ObjCProperty;
453 if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
454 PType->isVoidType())
455 VK = VK_RValue, OK = OK_Ordinary;
456
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000457 if (Super)
John McCall12f78a62010-12-02 01:19:52 +0000458 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
459 PType, VK, OK,
460 MemberLoc,
461 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000462 else
John McCall12f78a62010-12-02 01:19:52 +0000463 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
464 PType, VK, OK,
465 MemberLoc, BaseExpr));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000466
Chris Lattner7f816522010-04-11 07:45:24 +0000467 }
468
469 // Attempt to correct for typos in property names.
470 LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000471 if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Chris Lattner7f816522010-04-11 07:45:24 +0000472 Res.getAsSingle<ObjCPropertyDecl>()) {
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000473 DeclarationName TypoResult = Res.getLookupName();
Chris Lattner7f816522010-04-11 07:45:24 +0000474 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000475 << MemberName << QualType(OPT, 0) << TypoResult
476 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000477 ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
478 Diag(Property->getLocation(), diag::note_previous_decl)
479 << Property->getDeclName();
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000480 return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
481 SuperLoc, SuperType, Super);
Chris Lattner7f816522010-04-11 07:45:24 +0000482 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000483
Chris Lattner7f816522010-04-11 07:45:24 +0000484 Diag(MemberLoc, diag::err_property_not_found)
485 << MemberName << QualType(OPT, 0);
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000486 if (Setter)
Chris Lattner7f816522010-04-11 07:45:24 +0000487 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000488 << MemberName << BaseExpr->getSourceRange();
Chris Lattner7f816522010-04-11 07:45:24 +0000489 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000490}
491
492
493
John McCall60d7b3a2010-08-24 06:29:42 +0000494ExprResult Sema::
Chris Lattnereb483eb2010-04-11 08:28:14 +0000495ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
496 IdentifierInfo &propertyName,
497 SourceLocation receiverNameLoc,
498 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000500 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000501 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
502 receiverNameLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000503 if (IFace == 0) {
504 // If the "receiver" is 'super' in a method, handle it as an expression-like
505 // property reference.
506 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
507 if (receiverNamePtr->isStr("super")) {
508 if (CurMethod->isInstanceMethod()) {
509 QualType T =
510 Context.getObjCInterfaceType(CurMethod->getClassInterface());
511 T = Context.getObjCObjectPointerType(T);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000512
513 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000514 /*BaseExpr*/0, &propertyName,
515 propertyNameLoc,
516 receiverNameLoc, T, true);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattnereb483eb2010-04-11 08:28:14 +0000519 // Otherwise, if this is a class method, try dispatching to our
520 // superclass.
521 IFace = CurMethod->getClassInterface()->getSuperClass();
522 }
523
524 if (IFace == 0) {
525 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
526 return ExprError();
527 }
528 }
529
530 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000531 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000532 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000533
534 // If this reference is in an @implementation, check for 'private' methods.
535 if (!Getter)
536 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
537 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000538 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000539 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000540
541 if (Getter) {
542 // FIXME: refactor/share with ActOnMemberReference().
543 // Check if we can reference this property.
544 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
545 return ExprError();
546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Steve Naroff61f72cb2009-03-09 21:12:44 +0000548 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000549 Selector SetterSel =
550 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000551 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000553 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000554 if (!Setter) {
555 // If this reference is in an @implementation, also check for 'private'
556 // methods.
557 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
558 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000559 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000560 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000561 }
562 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000563 if (!Setter)
564 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000565
566 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
567 return ExprError();
568
569 if (Getter || Setter) {
570 QualType PType;
571
John McCall09431682010-11-18 19:01:18 +0000572 ExprValueKind VK = VK_LValue;
573 if (Getter) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000574 PType = Getter->getSendResultType();
John McCall09431682010-11-18 19:01:18 +0000575 if (!getLangOptions().CPlusPlus &&
576 !PType.hasQualifiers() && PType->isVoidType())
577 VK = VK_RValue;
578 } else {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000579 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
580 E = Setter->param_end(); PI != E; ++PI)
581 PType = (*PI)->getType();
John McCall09431682010-11-18 19:01:18 +0000582 VK = VK_LValue;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000583 }
John McCall09431682010-11-18 19:01:18 +0000584
585 ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
586
John McCall12f78a62010-12-02 01:19:52 +0000587 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
588 PType, VK, OK,
589 propertyNameLoc,
590 receiverNameLoc, IFace));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000591 }
592 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
593 << &propertyName << Context.getObjCInterfaceType(IFace));
594}
595
Douglas Gregor47bd5432010-04-14 02:46:37 +0000596Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +0000597 IdentifierInfo *Name,
Douglas Gregor47bd5432010-04-14 02:46:37 +0000598 SourceLocation NameLoc,
599 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +0000600 bool HasTrailingDot,
John McCallb3d87482010-08-24 05:47:05 +0000601 ParsedType &ReceiverType) {
602 ReceiverType = ParsedType();
Douglas Gregor1569f952010-04-21 20:38:13 +0000603
Douglas Gregor47bd5432010-04-14 02:46:37 +0000604 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor95f42922010-10-14 22:11:03 +0000605 // messaging super. If the identifier is "super" and there is a
606 // trailing dot, it's an instance message.
607 if (IsSuper && S->isInObjcMethodScope())
608 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000609
610 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
611 LookupName(Result, S);
612
613 switch (Result.getResultKind()) {
614 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000615 // Normal name lookup didn't find anything. If we're in an
616 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor95f42922010-10-14 22:11:03 +0000617 // FIXME: This is a hack. Ivar lookup should be part of normal
618 // lookup.
Douglas Gregored464422010-04-19 20:09:36 +0000619 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
620 ObjCInterfaceDecl *ClassDeclared;
621 if (Method->getClassInterface()->lookupInstanceVariable(Name,
622 ClassDeclared))
623 return ObjCInstanceMessage;
624 }
Douglas Gregor95f42922010-10-14 22:11:03 +0000625
Douglas Gregor47bd5432010-04-14 02:46:37 +0000626 // Break out; we'll perform typo correction below.
627 break;
628
629 case LookupResult::NotFoundInCurrentInstantiation:
630 case LookupResult::FoundOverloaded:
631 case LookupResult::FoundUnresolvedValue:
632 case LookupResult::Ambiguous:
633 Result.suppressDiagnostics();
634 return ObjCInstanceMessage;
635
636 case LookupResult::Found: {
637 // We found something. If it's a type, then we have a class
638 // message. Otherwise, it's an instance message.
639 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000640 QualType T;
641 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
642 T = Context.getObjCInterfaceType(Class);
643 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
644 T = Context.getTypeDeclType(Type);
645 else
646 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000647
Douglas Gregor1569f952010-04-21 20:38:13 +0000648 // We have a class message, and T is the type we're
649 // messaging. Build source-location information for it.
650 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000651 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor1569f952010-04-21 20:38:13 +0000652 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000653 }
654 }
655
Douglas Gregoraaf87162010-04-14 20:04:41 +0000656 // Determine our typo-correction context.
657 CorrectTypoContext CTC = CTC_Expression;
658 if (ObjCMethodDecl *Method = getCurMethodDecl())
659 if (Method->getClassInterface() &&
660 Method->getClassInterface()->getSuperClass())
661 CTC = CTC_ObjCMessageReceiver;
662
663 if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
664 if (Result.isSingleResult()) {
665 // If we found a declaration, correct when it refers to an Objective-C
666 // class.
667 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000668 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Douglas Gregoraaf87162010-04-14 20:04:41 +0000669 Diag(NameLoc, diag::err_unknown_receiver_suggest)
670 << Name << Result.getLookupName()
671 << FixItHint::CreateReplacement(SourceRange(NameLoc),
672 ND->getNameAsString());
673 Diag(ND->getLocation(), diag::note_previous_decl)
674 << Corrected;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000675
Douglas Gregor1569f952010-04-21 20:38:13 +0000676 QualType T = Context.getObjCInterfaceType(Class);
677 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000678 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000679 return ObjCClassMessage;
680 }
681 } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
682 Corrected.getAsIdentifierInfo()->isStr("super")) {
683 // If we've found the keyword "super", this is a send to super.
684 Diag(NameLoc, diag::err_unknown_receiver_suggest)
685 << Name << Corrected
686 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
687 Name = Corrected.getAsIdentifierInfo();
688 return ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000689 }
690 }
691
692 // Fall back: let the parser try to parse it as an instance message.
693 return ObjCInstanceMessage;
694}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000695
John McCall60d7b3a2010-08-24 06:29:42 +0000696ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000697 SourceLocation SuperLoc,
698 Selector Sel,
699 SourceLocation LBracLoc,
700 SourceLocation SelectorLoc,
701 SourceLocation RBracLoc,
702 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000703 // Determine whether we are inside a method or not.
704 ObjCMethodDecl *Method = getCurMethodDecl();
Douglas Gregorf95861a2010-04-21 20:01:04 +0000705 if (!Method) {
706 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
707 return ExprError();
708 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000709
Douglas Gregorf95861a2010-04-21 20:01:04 +0000710 ObjCInterfaceDecl *Class = Method->getClassInterface();
711 if (!Class) {
712 Diag(SuperLoc, diag::error_no_super_class_message)
713 << Method->getDeclName();
714 return ExprError();
715 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000716
Douglas Gregorf95861a2010-04-21 20:01:04 +0000717 ObjCInterfaceDecl *Super = Class->getSuperClass();
718 if (!Super) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000719 // The current class does not have a superclass.
720 Diag(SuperLoc, diag::error_no_super_class) << Class->getIdentifier();
721 return ExprError();
Chris Lattner15faee12010-04-12 05:38:43 +0000722 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000723
Douglas Gregorf95861a2010-04-21 20:01:04 +0000724 // We are in a method whose class has a superclass, so 'super'
725 // is acting as a keyword.
726 if (Method->isInstanceMethod()) {
727 // Since we are in an instance method, this is an instance
728 // message to the superclass instance.
729 QualType SuperTy = Context.getObjCInterfaceType(Super);
730 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCall9ae2f072010-08-23 23:25:46 +0000731 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000732 Sel, /*Method=*/0,
733 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000734 }
Douglas Gregorf95861a2010-04-21 20:01:04 +0000735
736 // Since we are in a class method, this is a class message to
737 // the superclass.
738 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
739 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000740 SuperLoc, Sel, /*Method=*/0,
741 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000742}
743
744/// \brief Build an Objective-C class message expression.
745///
746/// This routine takes care of both normal class messages and
747/// class messages to the superclass.
748///
749/// \param ReceiverTypeInfo Type source information that describes the
750/// receiver of this message. This may be NULL, in which case we are
751/// sending to the superclass and \p SuperLoc must be a valid source
752/// location.
753
754/// \param ReceiverType The type of the object receiving the
755/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
756/// type as that refers to. For a superclass send, this is the type of
757/// the superclass.
758///
759/// \param SuperLoc The location of the "super" keyword in a
760/// superclass message.
761///
762/// \param Sel The selector to which the message is being sent.
763///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000764/// \param Method The method that this class message is invoking, if
765/// already known.
766///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000767/// \param LBracLoc The location of the opening square bracket ']'.
768///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000769/// \param RBrac The location of the closing square bracket ']'.
770///
771/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000772ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000773 QualType ReceiverType,
774 SourceLocation SuperLoc,
775 Selector Sel,
776 ObjCMethodDecl *Method,
777 SourceLocation LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000778 SourceLocation SelectorLoc,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000779 SourceLocation RBracLoc,
780 MultiExprArg ArgsIn) {
781 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregor9497a732010-09-16 01:51:54 +0000782 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregor0fbda682010-09-15 14:51:05 +0000783 if (LBracLoc.isInvalid()) {
784 Diag(Loc, diag::err_missing_open_square_message_send)
785 << FixItHint::CreateInsertion(Loc, "[");
786 LBracLoc = Loc;
787 }
788
Douglas Gregor92e986e2010-04-22 16:44:27 +0000789 if (ReceiverType->isDependentType()) {
790 // If the receiver type is dependent, we can't type-check anything
791 // at this point. Build a dependent expression.
792 unsigned NumArgs = ArgsIn.size();
793 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
794 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCallf89e55a2010-11-18 06:31:45 +0000795 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
796 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000797 Sel, SelectorLoc, /*Method=*/0,
Douglas Gregor92e986e2010-04-22 16:44:27 +0000798 Args, NumArgs, RBracLoc));
799 }
Chris Lattner15faee12010-04-12 05:38:43 +0000800
Douglas Gregor2725ca82010-04-21 19:57:20 +0000801 // Find the class to which we are sending this message.
802 ObjCInterfaceDecl *Class = 0;
John McCallc12c5bb2010-05-15 11:32:37 +0000803 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
804 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000805 Diag(Loc, diag::err_invalid_receiver_class_message)
806 << ReceiverType;
807 return ExprError();
Steve Naroff7c778f12008-07-25 19:39:00 +0000808 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000809 assert(Class && "We don't know which class we're messaging?");
810
811 // Find the method we are messaging.
Douglas Gregorf49bb082010-04-22 17:01:48 +0000812 if (!Method) {
813 if (Class->isForwardDecl()) {
814 // A forward class used in messaging is treated as a 'Class'
815 Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
816 Method = LookupFactoryMethodInGlobalPool(Sel,
817 SourceRange(LBracLoc, RBracLoc));
818 if (Method)
819 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
820 << Method->getDeclName();
821 }
822 if (!Method)
823 Method = Class->lookupClassMethod(Sel);
824
825 // If we have an implementation in scope, check "private" methods.
826 if (!Method)
827 Method = LookupPrivateClassMethod(Sel, Class);
828
829 if (Method && DiagnoseUseOfDecl(Method, Loc))
830 return ExprError();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregor2725ca82010-04-21 19:57:20 +0000833 // Check the argument types and determine the result type.
834 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +0000835 ExprValueKind VK = VK_RValue;
836
Douglas Gregor2725ca82010-04-21 19:57:20 +0000837 unsigned NumArgs = ArgsIn.size();
838 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
839 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
John McCallf89e55a2010-11-18 06:31:45 +0000840 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +0000841 return ExprError();
Ted Kremenek4df728e2008-06-24 15:50:53 +0000842
Douglas Gregor483dd2f2011-01-11 03:23:19 +0000843 if (Method && !Method->getResultType()->isVoidType() &&
844 RequireCompleteType(LBracLoc, Method->getResultType(),
845 diag::err_illegal_message_expr_incomplete_type))
846 return ExprError();
847
Douglas Gregor2725ca82010-04-21 19:57:20 +0000848 // Construct the appropriate ObjCMessageExpr.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000849 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +0000850 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +0000851 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000852 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000853 ReceiverType, Sel, SelectorLoc,
854 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000855 else
John McCallf89e55a2010-11-18 06:31:45 +0000856 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000857 ReceiverTypeInfo, Sel, SelectorLoc,
858 Method, Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000859 return MaybeBindToTemporary(Result);
Chris Lattner85a932e2008-01-04 22:32:30 +0000860}
861
Douglas Gregor2725ca82010-04-21 19:57:20 +0000862// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattner85a932e2008-01-04 22:32:30 +0000863// ArgExprs is optional - if it is present, the number of expressions
864// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +0000865ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor77328d12010-09-15 23:19:31 +0000866 ParsedType Receiver,
867 Selector Sel,
868 SourceLocation LBracLoc,
869 SourceLocation SelectorLoc,
870 SourceLocation RBracLoc,
871 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000872 TypeSourceInfo *ReceiverTypeInfo;
873 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
874 if (ReceiverType.isNull())
875 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Douglas Gregor2725ca82010-04-21 19:57:20 +0000878 if (!ReceiverTypeInfo)
879 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
880
881 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorf49bb082010-04-22 17:01:48 +0000882 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000883 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +0000884}
885
886/// \brief Build an Objective-C instance message expression.
887///
888/// This routine takes care of both normal instance messages and
889/// instance messages to the superclass instance.
890///
891/// \param Receiver The expression that computes the object that will
892/// receive this message. This may be empty, in which case we are
893/// sending to the superclass instance and \p SuperLoc must be a valid
894/// source location.
895///
896/// \param ReceiverType The (static) type of the object receiving the
897/// message. When a \p Receiver expression is provided, this is the
898/// same type as that expression. For a superclass instance send, this
899/// is a pointer to the type of the superclass.
900///
901/// \param SuperLoc The location of the "super" keyword in a
902/// superclass instance message.
903///
904/// \param Sel The selector to which the message is being sent.
905///
Douglas Gregorf49bb082010-04-22 17:01:48 +0000906/// \param Method The method that this instance message is invoking, if
907/// already known.
908///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000909/// \param LBracLoc The location of the opening square bracket ']'.
910///
Douglas Gregor2725ca82010-04-21 19:57:20 +0000911/// \param RBrac The location of the closing square bracket ']'.
912///
913/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +0000914ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000915 QualType ReceiverType,
916 SourceLocation SuperLoc,
917 Selector Sel,
918 ObjCMethodDecl *Method,
919 SourceLocation LBracLoc,
920 SourceLocation SelectorLoc,
921 SourceLocation RBracLoc,
922 MultiExprArg ArgsIn) {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000923 // The location of the receiver.
924 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
925
926 if (LBracLoc.isInvalid()) {
927 Diag(Loc, diag::err_missing_open_square_message_send)
928 << FixItHint::CreateInsertion(Loc, "[");
929 LBracLoc = Loc;
930 }
931
Douglas Gregor2725ca82010-04-21 19:57:20 +0000932 // If we have a receiver expression, perform appropriate promotions
933 // and determine receiver type.
Douglas Gregor2725ca82010-04-21 19:57:20 +0000934 if (Receiver) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000935 if (Receiver->isTypeDependent()) {
936 // If the receiver is type-dependent, we can't type-check anything
937 // at this point. Build a dependent expression.
938 unsigned NumArgs = ArgsIn.size();
939 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
940 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
941 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCallf89e55a2010-11-18 06:31:45 +0000942 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +0000943 SelectorLoc, /*Method=*/0,
944 Args, NumArgs, RBracLoc));
Douglas Gregor92e986e2010-04-22 16:44:27 +0000945 }
946
Douglas Gregor2725ca82010-04-21 19:57:20 +0000947 // If necessary, apply function/array conversion to the receiver.
948 // C99 6.7.5.3p[7,8].
949 DefaultFunctionArrayLvalueConversion(Receiver);
950 ReceiverType = Receiver->getType();
951 }
952
Douglas Gregorf49bb082010-04-22 17:01:48 +0000953 if (!Method) {
954 // Handle messages to id.
Fariborz Jahanianba551982010-08-10 18:10:50 +0000955 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000956 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorf49bb082010-04-22 17:01:48 +0000957 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
958 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000959 SourceRange(LBracLoc, RBracLoc),
960 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000961 if (!Method)
Douglas Gregor2725ca82010-04-21 19:57:20 +0000962 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000963 SourceRange(LBracLoc, RBracLoc),
964 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000965 } else if (ReceiverType->isObjCClassType() ||
966 ReceiverType->isObjCQualifiedClassType()) {
967 // Handle messages to Class.
968 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
969 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
970 // First check the public methods in the class interface.
971 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregorf49bb082010-04-22 17:01:48 +0000973 if (!Method)
974 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Douglas Gregor04badcf2010-04-21 00:45:42 +0000975
Douglas Gregorf49bb082010-04-22 17:01:48 +0000976 // FIXME: if we still haven't found a method, we need to look in
977 // protocols (if we have qualifiers).
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000978 }
Douglas Gregorf49bb082010-04-22 17:01:48 +0000979 if (Method && DiagnoseUseOfDecl(Method, Loc))
980 return ExprError();
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000981 }
Douglas Gregor04badcf2010-04-21 00:45:42 +0000982 if (!Method) {
Douglas Gregorf49bb082010-04-22 17:01:48 +0000983 // If not messaging 'self', look for any factory method named 'Sel'.
984 if (!Receiver || !isSelfExpr(Receiver)) {
985 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000986 SourceRange(LBracLoc, RBracLoc),
987 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000988 if (!Method) {
989 // If no class (factory) method was found, check if an _instance_
990 // method of the same name exists in the root class only.
Douglas Gregor2725ca82010-04-21 19:57:20 +0000991 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000992 SourceRange(LBracLoc, RBracLoc),
993 true);
Douglas Gregorf49bb082010-04-22 17:01:48 +0000994 if (Method)
995 if (const ObjCInterfaceDecl *ID =
996 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
997 if (ID->getSuperClass())
998 Diag(Loc, diag::warn_root_inst_method_not_found)
999 << Sel << SourceRange(LBracLoc, RBracLoc);
1000 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001001 }
1002 }
1003 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001004 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001005 ObjCInterfaceDecl* ClassDecl = 0;
1006
1007 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1008 // long as one of the protocols implements the selector (if not, warn).
1009 if (const ObjCObjectPointerType *QIdTy
1010 = ReceiverType->getAsObjCQualifiedIdType()) {
1011 // Search protocols for instance methods.
1012 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
1013 E = QIdTy->qual_end(); I != E; ++I) {
1014 ObjCProtocolDecl *PDecl = *I;
1015 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
1016 break;
1017 // Since we aren't supporting "Class<foo>", look for a class method.
1018 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
1019 break;
1020 }
1021 } else if (const ObjCObjectPointerType *OCIType
1022 = ReceiverType->getAsObjCInterfacePointerType()) {
1023 // We allow sending a message to a pointer to an interface (an object).
1024 ClassDecl = OCIType->getInterfaceDecl();
1025 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
1026 // faster than the following method (which can do *many* linear searches).
Sebastian Redldb9d2142010-08-02 23:18:59 +00001027 // The idea is to add class info to MethodPool.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001028 Method = ClassDecl->lookupInstanceMethod(Sel);
1029
1030 if (!Method) {
1031 // Search protocol qualifiers.
1032 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
1033 E = OCIType->qual_end(); QI != E; ++QI) {
1034 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
1035 break;
1036 }
1037 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001038 bool forwardClass = false;
Douglas Gregorf49bb082010-04-22 17:01:48 +00001039 if (!Method) {
1040 // If we have implementations in scope, check "private" methods.
1041 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1042
1043 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
1044 // If we still haven't found a method, look in the global pool. This
1045 // behavior isn't very desirable, however we need it for GCC
1046 // compatibility. FIXME: should we deviate??
1047 if (OCIType->qual_empty()) {
1048 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001049 SourceRange(LBracLoc, RBracLoc));
1050 forwardClass = OCIType->getInterfaceDecl()->isForwardDecl();
1051 if (Method && !forwardClass)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001052 Diag(Loc, diag::warn_maynot_respond)
1053 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1054 }
1055 }
1056 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001057 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorf49bb082010-04-22 17:01:48 +00001058 return ExprError();
1059 } else if (!Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001060 (ReceiverType->isPointerType() ||
1061 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001062 // Implicitly convert integers and pointers to 'id' but emit a warning.
1063 Diag(Loc, diag::warn_bad_receiver_type)
1064 << ReceiverType
1065 << Receiver->getSourceRange();
1066 if (ReceiverType->isPointerType())
1067 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall2de56d12010-08-25 11:45:40 +00001068 CK_BitCast);
John McCall404cd162010-11-13 01:35:44 +00001069 else {
1070 // TODO: specialized warning on null receivers?
1071 bool IsNull = Receiver->isNullPointerConstant(Context,
1072 Expr::NPC_ValueDependentIsNull);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001073 ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall404cd162010-11-13 01:35:44 +00001074 IsNull ? CK_NullToPointer : CK_IntegralToPointer);
1075 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001076 ReceiverType = Receiver->getType();
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001077 }
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001078 else if (getLangOptions().CPlusPlus &&
1079 !PerformContextuallyConvertToObjCId(Receiver)) {
1080 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
1081 Receiver = ICE->getSubExpr();
1082 ReceiverType = Receiver->getType();
1083 }
John McCall9ae2f072010-08-23 23:25:46 +00001084 return BuildInstanceMessage(Receiver,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001085 ReceiverType,
1086 SuperLoc,
1087 Sel,
1088 Method,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001089 LBracLoc,
1090 SelectorLoc,
Fariborz Jahanian79d3f042010-05-12 23:29:11 +00001091 RBracLoc,
1092 move(ArgsIn));
Douglas Gregorf49bb082010-04-22 17:01:48 +00001093 } else {
1094 // Reject other random receiver types (e.g. structs).
1095 Diag(Loc, diag::err_bad_receiver_type)
1096 << ReceiverType << Receiver->getSourceRange();
1097 return ExprError();
1098 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001099 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregor2725ca82010-04-21 19:57:20 +00001102 // Check the message arguments.
1103 unsigned NumArgs = ArgsIn.size();
1104 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1105 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001106 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001107 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1108 ReceiverType->isObjCQualifiedClassType());
1109 if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
John McCallf89e55a2010-11-18 06:31:45 +00001110 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001111 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001112
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001113 if (Method && !Method->getResultType()->isVoidType() &&
1114 RequireCompleteType(LBracLoc, Method->getResultType(),
1115 diag::err_illegal_message_expr_incomplete_type))
1116 return ExprError();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001117
Douglas Gregor2725ca82010-04-21 19:57:20 +00001118 // Construct the appropriate ObjCMessageExpr instance.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001119 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001120 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001121 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001122 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001123 ReceiverType, Sel, SelectorLoc, Method,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001124 Args, NumArgs, RBracLoc);
1125 else
John McCallf89e55a2010-11-18 06:31:45 +00001126 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001127 Receiver, Sel, SelectorLoc, Method,
1128 Args, NumArgs, RBracLoc);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001129 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001130}
1131
1132// ActOnInstanceMessage - used for both unary and keyword messages.
1133// ArgExprs is optional - if it is present, the number of expressions
1134// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001135ExprResult Sema::ActOnInstanceMessage(Scope *S,
1136 Expr *Receiver,
1137 Selector Sel,
1138 SourceLocation LBracLoc,
1139 SourceLocation SelectorLoc,
1140 SourceLocation RBracLoc,
1141 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001142 if (!Receiver)
1143 return ExprError();
1144
John McCall9ae2f072010-08-23 23:25:46 +00001145 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001146 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001147 LBracLoc, SelectorLoc, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001148}
Chris Lattnereca7be62008-04-07 05:30:13 +00001149