blob: 22e432d8dc5cd7352ac93ee971e8aaf2ef5e9680 [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"
John McCall26743b22011-02-03 09:00:02 +000017#include "clang/Sema/ScopeInfo.h"
Douglas Gregore737f502010-08-12 20:07:10 +000018#include "clang/Sema/Initialization.h"
John McCall2cf031d2011-10-01 01:01:08 +000019#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclObjC.h"
Steve Narofff494b572008-05-29 21:12:08 +000022#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000023#include "clang/AST/StmtVisitor.h"
Douglas Gregor2725ca82010-04-21 19:57:20 +000024#include "clang/AST/TypeLoc.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000025#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000026#include "clang/Lex/Preprocessor.h"
27
Chris Lattner85a932e2008-01-04 22:32:30 +000028using namespace clang;
John McCall26743b22011-02-03 09:00:02 +000029using namespace sema;
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +000030using llvm::makeArrayRef;
Chris Lattner85a932e2008-01-04 22:32:30 +000031
John McCallf312b1e2010-08-26 23:41:50 +000032ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
33 Expr **strings,
34 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000035 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
36
Chris Lattnerf4b136f2009-02-18 06:13:04 +000037 // Most ObjC strings are formed out of a single piece. However, we *can*
38 // have strings formed out of multiple @ strings with multiple pptokens in
39 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
40 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000041 StringLiteral *S = Strings[0];
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattnerf4b136f2009-02-18 06:13:04 +000043 // If we have a multi-part string, merge it all together.
44 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000045 // Concatenate objc strings.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000046 SmallString<128> StrBuf;
Chris Lattner5f9e2722011-07-23 10:55:15 +000047 SmallVector<SourceLocation, 8> StrLocs;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner726e1682009-02-18 05:49:11 +000049 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000050 S = Strings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000051
Douglas Gregor5cee1192011-07-27 05:40:30 +000052 // ObjC strings can't be wide or UTF.
53 if (!S->isAscii()) {
Chris Lattnerf4b136f2009-02-18 06:13:04 +000054 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
55 << S->getSourceRange();
56 return true;
57 }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +000059 // Append the string.
60 StrBuf += S->getString();
Mike Stump1eb44332009-09-09 15:08:12 +000061
Chris Lattner39c28bb2009-02-18 06:48:40 +000062 // Get the locations of the string tokens.
63 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Chris Lattner85a932e2008-01-04 22:32:30 +000064 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner39c28bb2009-02-18 06:48:40 +000066 // Create the aggregate string with the appropriate content and location
67 // information.
Jay Foad65aa6882011-06-21 15:13:30 +000068 S = StringLiteral::Create(Context, StrBuf,
Douglas Gregor5cee1192011-07-27 05:40:30 +000069 StringLiteral::Ascii, /*Pascal=*/false,
Chris Lattner2085fd62009-02-18 06:40:38 +000070 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000071 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000072 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner69039812009-02-18 06:01:06 +000074 // Verify that this composite string is acceptable for ObjC strings.
75 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000076 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000077
78 // Initialize the constant string interface lazily. This assumes
Steve Naroffd9fd7642009-04-07 14:18:33 +000079 // the NSString interface is seen in this translation unit. Note: We
80 // don't use NSConstantString, since the runtime team considers this
81 // interface private (even though it appears in the header files).
Chris Lattnera0af1fe2009-02-18 06:06:56 +000082 QualType Ty = Context.getObjCConstantStringInterface();
83 if (!Ty.isNull()) {
Steve Naroff14108da2009-07-10 23:34:53 +000084 Ty = Context.getObjCObjectPointerType(Ty);
Fariborz Jahanian8a437762010-04-23 23:19:04 +000085 } else if (getLangOptions().NoConstantCFStrings) {
Fariborz Jahanian4c733072010-10-19 17:19:29 +000086 IdentifierInfo *NSIdent=0;
87 std::string StringClass(getLangOptions().ObjCConstantStringClass);
88
89 if (StringClass.empty())
90 NSIdent = &Context.Idents.get("NSConstantString");
91 else
92 NSIdent = &Context.Idents.get(StringClass);
93
Fariborz Jahanian8a437762010-04-23 23:19:04 +000094 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
95 LookupOrdinaryName);
96 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
97 Context.setObjCConstantStringInterface(StrIF);
98 Ty = Context.getObjCConstantStringInterface();
99 Ty = Context.getObjCObjectPointerType(Ty);
100 } else {
101 // If there is no NSConstantString interface defined then treat this
102 // as error and recover from it.
103 Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent
104 << S->getSourceRange();
105 Ty = Context.getObjCIdType();
106 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000107 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +0000108 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000109 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
110 LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000111 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
112 Context.setObjCConstantStringInterface(StrIF);
113 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +0000114 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000115 } else {
Fariborz Jahanianf64bc202012-02-23 22:51:36 +0000116 // If there is no NSString interface defined, implicitly declare
117 // a @class NSString; and use that instead. This is to make sure
118 // type of an NSString literal is represented correctly, instead of
119 // being an 'id' type.
120 Ty = Context.getObjCNSStringType();
121 if (Ty.isNull()) {
122 ObjCInterfaceDecl *NSStringIDecl =
123 ObjCInterfaceDecl::Create (Context,
124 Context.getTranslationUnitDecl(),
125 SourceLocation(), NSIdent,
126 0, SourceLocation());
127 Ty = Context.getObjCInterfaceType(NSStringIDecl);
128 Context.setObjCNSStringType(Ty);
129 }
130 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000131 }
Chris Lattner13fd7e52008-06-21 21:44:18 +0000132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattnerf4b136f2009-02-18 06:13:04 +0000134 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +0000135}
136
Argyrios Kyrtzidis3b5904b2011-05-14 20:32:39 +0000137ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Douglas Gregor81d34662010-04-20 15:39:42 +0000138 TypeSourceInfo *EncodedTypeInfo,
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000139 SourceLocation RParenLoc) {
Douglas Gregor81d34662010-04-20 15:39:42 +0000140 QualType EncodedType = EncodedTypeInfo->getType();
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000141 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000142 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000143 StrTy = Context.DependentTy;
144 else {
Fariborz Jahanian6c916152011-06-16 22:34:44 +0000145 if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled.
146 !EncodedType->isVoidType()) // void is handled too.
Argyrios Kyrtzidis3b5904b2011-05-14 20:32:39 +0000147 if (RequireCompleteType(AtLoc, EncodedType,
148 PDiag(diag::err_incomplete_type_objc_at_encode)
149 << EncodedTypeInfo->getTypeLoc().getSourceRange()))
150 return ExprError();
151
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000152 std::string Str;
153 Context.getObjCEncodingForType(EncodedType, Str);
154
155 // The type of @encode is the same as the type of the corresponding string,
156 // which is an array type.
157 StrTy = Context.CharTy;
158 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
John McCall4b7a8342010-03-15 10:54:44 +0000159 if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000160 StrTy.addConst();
161 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
162 ArrayType::Normal, 0);
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Douglas Gregor81d34662010-04-20 15:39:42 +0000165 return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000166}
167
John McCallf312b1e2010-08-26 23:41:50 +0000168ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
169 SourceLocation EncodeLoc,
170 SourceLocation LParenLoc,
171 ParsedType ty,
172 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000173 // FIXME: Preserve type source info ?
Douglas Gregor81d34662010-04-20 15:39:42 +0000174 TypeSourceInfo *TInfo;
175 QualType EncodedType = GetTypeFromParser(ty, &TInfo);
176 if (!TInfo)
177 TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
178 PP.getLocForEndOfToken(LParenLoc));
Chris Lattner85a932e2008-01-04 22:32:30 +0000179
Douglas Gregor81d34662010-04-20 15:39:42 +0000180 return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000181}
182
John McCallf312b1e2010-08-26 23:41:50 +0000183ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
184 SourceLocation AtLoc,
185 SourceLocation SelLoc,
186 SourceLocation LParenLoc,
187 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000188 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +0000189 SourceRange(LParenLoc, RParenLoc), false, false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000190 if (!Method)
191 Method = LookupFactoryMethodInGlobalPool(Sel,
192 SourceRange(LParenLoc, RParenLoc));
193 if (!Method)
194 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
Fariborz Jahanian4c91d892011-07-13 19:05:43 +0000195
196 if (!Method ||
197 Method->getImplementationControl() != ObjCMethodDecl::Optional) {
198 llvm::DenseMap<Selector, SourceLocation>::iterator Pos
199 = ReferencedSelectors.find(Sel);
200 if (Pos == ReferencedSelectors.end())
201 ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
202 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000203
John McCallf85e1932011-06-15 23:02:42 +0000204 // In ARC, forbid the user from using @selector for
205 // retain/release/autorelease/dealloc/retainCount.
206 if (getLangOptions().ObjCAutoRefCount) {
207 switch (Sel.getMethodFamily()) {
208 case OMF_retain:
209 case OMF_release:
210 case OMF_autorelease:
211 case OMF_retainCount:
212 case OMF_dealloc:
213 Diag(AtLoc, diag::err_arc_illegal_selector) <<
214 Sel << SourceRange(LParenLoc, RParenLoc);
215 break;
216
217 case OMF_None:
218 case OMF_alloc:
219 case OMF_copy:
Nico Weber80cb6e62011-08-28 22:35:17 +0000220 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000221 case OMF_init:
222 case OMF_mutableCopy:
223 case OMF_new:
224 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000225 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000226 break;
227 }
228 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000229 QualType Ty = Context.getObjCSelType();
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000230 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000231}
232
John McCallf312b1e2010-08-26 23:41:50 +0000233ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
234 SourceLocation AtLoc,
235 SourceLocation ProtoLoc,
236 SourceLocation LParenLoc,
237 SourceLocation RParenLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000238 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000239 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000240 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000241 return true;
242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000244 QualType Ty = Context.getObjCProtoType();
245 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000246 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000247 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000248 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000249}
250
John McCall26743b22011-02-03 09:00:02 +0000251/// Try to capture an implicit reference to 'self'.
Eli Friedmanb942cb22012-02-03 22:47:37 +0000252ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) {
253 DeclContext *DC = getFunctionLevelDeclContext();
John McCall26743b22011-02-03 09:00:02 +0000254
255 // If we're not in an ObjC method, error out. Note that, unlike the
256 // C++ case, we don't require an instance method --- class methods
257 // still have a 'self', and we really do still need to capture it!
258 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
259 if (!method)
260 return 0;
261
Douglas Gregor999713e2012-02-18 09:37:24 +0000262 tryCaptureVariable(method->getSelfDecl(), Loc);
John McCall26743b22011-02-03 09:00:02 +0000263
264 return method;
265}
266
Douglas Gregor5c16d632011-09-09 20:05:21 +0000267static QualType stripObjCInstanceType(ASTContext &Context, QualType T) {
268 if (T == Context.getObjCInstanceType())
269 return Context.getObjCIdType();
270
271 return T;
272}
273
Douglas Gregor926df6c2011-06-11 01:09:30 +0000274QualType Sema::getMessageSendResultType(QualType ReceiverType,
275 ObjCMethodDecl *Method,
276 bool isClassMessage, bool isSuperMessage) {
277 assert(Method && "Must have a method");
278 if (!Method->hasRelatedResultType())
279 return Method->getSendResultType();
280
281 // If a method has a related return type:
282 // - if the method found is an instance method, but the message send
283 // was a class message send, T is the declared return type of the method
284 // found
285 if (Method->isInstanceMethod() && isClassMessage)
Douglas Gregor5c16d632011-09-09 20:05:21 +0000286 return stripObjCInstanceType(Context, Method->getSendResultType());
Douglas Gregor926df6c2011-06-11 01:09:30 +0000287
288 // - if the receiver is super, T is a pointer to the class of the
289 // enclosing method definition
290 if (isSuperMessage) {
291 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
292 if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface())
293 return Context.getObjCObjectPointerType(
294 Context.getObjCInterfaceType(Class));
295 }
296
297 // - if the receiver is the name of a class U, T is a pointer to U
298 if (ReceiverType->getAs<ObjCInterfaceType>() ||
299 ReceiverType->isObjCQualifiedInterfaceType())
300 return Context.getObjCObjectPointerType(ReceiverType);
301 // - if the receiver is of type Class or qualified Class type,
302 // T is the declared return type of the method.
303 if (ReceiverType->isObjCClassType() ||
304 ReceiverType->isObjCQualifiedClassType())
Douglas Gregor5c16d632011-09-09 20:05:21 +0000305 return stripObjCInstanceType(Context, Method->getSendResultType());
Douglas Gregor926df6c2011-06-11 01:09:30 +0000306
307 // - if the receiver is id, qualified id, Class, or qualified Class, T
308 // is the receiver type, otherwise
309 // - T is the type of the receiver expression.
310 return ReceiverType;
311}
John McCall26743b22011-02-03 09:00:02 +0000312
Douglas Gregor926df6c2011-06-11 01:09:30 +0000313void Sema::EmitRelatedResultTypeNote(const Expr *E) {
314 E = E->IgnoreParenImpCasts();
315 const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E);
316 if (!MsgSend)
317 return;
318
319 const ObjCMethodDecl *Method = MsgSend->getMethodDecl();
320 if (!Method)
321 return;
322
323 if (!Method->hasRelatedResultType())
324 return;
325
326 if (Context.hasSameUnqualifiedType(Method->getResultType()
327 .getNonReferenceType(),
328 MsgSend->getType()))
329 return;
330
Douglas Gregore97179c2011-09-08 01:46:34 +0000331 if (!Context.hasSameUnqualifiedType(Method->getResultType(),
332 Context.getObjCInstanceType()))
333 return;
334
Douglas Gregor926df6c2011-06-11 01:09:30 +0000335 Diag(Method->getLocation(), diag::note_related_result_type_inferred)
336 << Method->isInstanceMethod() << Method->getSelector()
337 << MsgSend->getType();
338}
339
340bool Sema::CheckMessageArgumentTypes(QualType ReceiverType,
341 Expr **Args, unsigned NumArgs,
Mike Stump1eb44332009-09-09 15:08:12 +0000342 Selector Sel, ObjCMethodDecl *Method,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000343 bool isClassMessage, bool isSuperMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000344 SourceLocation lbrac, SourceLocation rbrac,
John McCallf89e55a2010-11-18 06:31:45 +0000345 QualType &ReturnType, ExprValueKind &VK) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000346 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000347 // Apply default argument promotion as for (C99 6.5.2.2p6).
Douglas Gregor92e986e2010-04-22 16:44:27 +0000348 for (unsigned i = 0; i != NumArgs; i++) {
349 if (Args[i]->isTypeDependent())
350 continue;
351
John Wiegley429bb272011-04-08 18:41:53 +0000352 ExprResult Result = DefaultArgumentPromotion(Args[i]);
353 if (Result.isInvalid())
354 return true;
355 Args[i] = Result.take();
Douglas Gregor92e986e2010-04-22 16:44:27 +0000356 }
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000357
John McCallf85e1932011-06-15 23:02:42 +0000358 unsigned DiagID;
359 if (getLangOptions().ObjCAutoRefCount)
360 DiagID = diag::err_arc_method_not_found;
361 else
362 DiagID = isClassMessage ? diag::warn_class_method_not_found
363 : diag::warn_inst_method_not_found;
John McCall819e7452011-08-31 20:57:36 +0000364 if (!getLangOptions().DebuggerSupport)
365 Diag(lbrac, DiagID)
366 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
John McCall48218c62011-07-13 17:56:40 +0000367
368 // In debuggers, we want to use __unknown_anytype for these
369 // results so that clients can cast them.
370 if (getLangOptions().DebuggerSupport) {
371 ReturnType = Context.UnknownAnyTy;
372 } else {
373 ReturnType = Context.getObjCIdType();
374 }
John McCallf89e55a2010-11-18 06:31:45 +0000375 VK = VK_RValue;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000376 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregor926df6c2011-06-11 01:09:30 +0000379 ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage,
380 isSuperMessage);
John McCallf89e55a2010-11-18 06:31:45 +0000381 VK = Expr::getValueKindForType(Method->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000383 unsigned NumNamedArgs = Sel.getNumArgs();
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000384 // Method might have more arguments than selector indicates. This is due
385 // to addition of c-style arguments in method.
386 if (Method->param_size() > Sel.getNumArgs())
387 NumNamedArgs = Method->param_size();
388 // FIXME. This need be cleaned up.
389 if (NumArgs < NumNamedArgs) {
John McCallf89e55a2010-11-18 06:31:45 +0000390 Diag(lbrac, diag::err_typecheck_call_too_few_args)
391 << 2 << NumNamedArgs << NumArgs;
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000392 return false;
393 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000394
Chris Lattner312531a2009-04-12 08:11:20 +0000395 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000396 for (unsigned i = 0; i < NumNamedArgs; i++) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000397 // We can't do any type-checking on a type-dependent argument.
398 if (Args[i]->isTypeDependent())
399 continue;
400
Chris Lattner85a932e2008-01-04 22:32:30 +0000401 Expr *argExpr = Args[i];
Douglas Gregor92e986e2010-04-22 16:44:27 +0000402
John McCall5acb0c92011-10-17 18:40:02 +0000403 ParmVarDecl *param = Method->param_begin()[i];
Chris Lattner85a932e2008-01-04 22:32:30 +0000404 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000405
John McCall5acb0c92011-10-17 18:40:02 +0000406 // Strip the unbridged-cast placeholder expression off unless it's
407 // a consumed argument.
408 if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) &&
409 !param->hasAttr<CFConsumedAttr>())
410 argExpr = stripARCUnbridgedCast(argExpr);
411
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000412 if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
John McCall5acb0c92011-10-17 18:40:02 +0000413 param->getType(),
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000414 PDiag(diag::err_call_incomplete_argument)
415 << argExpr->getSourceRange()))
416 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000417
Fariborz Jahanian745da3a2010-09-24 17:30:16 +0000418 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
John McCall5acb0c92011-10-17 18:40:02 +0000419 param);
John McCall3fa5cae2010-10-26 07:05:15 +0000420 ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
Douglas Gregor688fc9b2010-04-21 23:24:10 +0000421 if (ArgE.isInvalid())
422 IsError = true;
423 else
424 Args[i] = ArgE.takeAs<Expr>();
Chris Lattner85a932e2008-01-04 22:32:30 +0000425 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000426
427 // Promote additional arguments to variadic methods.
428 if (Method->isVariadic()) {
Douglas Gregor92e986e2010-04-22 16:44:27 +0000429 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
430 if (Args[i]->isTypeDependent())
431 continue;
432
John Wiegley429bb272011-04-08 18:41:53 +0000433 ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
434 IsError |= Arg.isInvalid();
435 Args[i] = Arg.take();
Douglas Gregor92e986e2010-04-22 16:44:27 +0000436 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000437 } else {
438 // Check for extra arguments to non-variadic methods.
439 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000440 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000441 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000442 << 2 /*method*/ << NumNamedArgs << NumArgs
443 << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000444 << SourceRange(Args[NumNamedArgs]->getLocStart(),
445 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000446 }
447 }
448
Douglas Gregor2725ca82010-04-21 19:57:20 +0000449 DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +0000450
451 // Do additional checkings on method.
452 IsError |= CheckObjCMethodCall(Method, lbrac, Args, NumArgs);
453
Chris Lattner312531a2009-04-12 08:11:20 +0000454 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000455}
456
Douglas Gregorc737acb2011-09-27 16:10:05 +0000457bool Sema::isSelfExpr(Expr *receiver) {
Fariborz Jahanianf2d74cc2011-03-27 19:53:47 +0000458 // 'self' is objc 'self' in an objc method only.
John McCall4b9c2d22011-11-06 09:01:30 +0000459 ObjCMethodDecl *method =
460 dyn_cast<ObjCMethodDecl>(CurContext->getNonClosureAncestor());
461 if (!method) return false;
462
John McCallf85e1932011-06-15 23:02:42 +0000463 receiver = receiver->IgnoreParenLValueCasts();
464 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
John McCall4b9c2d22011-11-06 09:01:30 +0000465 if (DRE->getDecl() == method->getSelfDecl())
Douglas Gregorc737acb2011-09-27 16:10:05 +0000466 return true;
467 return false;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000468}
469
Steve Narofff1afaf62009-02-26 15:55:06 +0000470// Helper method for ActOnClassMethod/ActOnInstanceMethod.
471// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000472// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000473// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000474ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000475 ObjCInterfaceDecl *ClassDecl) {
476 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000477 // lookup in class and all superclasses
478 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000479 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000480 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Steve Naroff5609ec02009-03-08 18:56:13 +0000482 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000483 if (!Method)
484 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Steve Naroff5609ec02009-03-08 18:56:13 +0000486 // Before we give up, check if the selector is an instance method.
487 // But only in the root. This matches gcc's behaviour and what the
488 // runtime expects.
489 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000490 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000491 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000492 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000493 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000494 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Steve Naroff5609ec02009-03-08 18:56:13 +0000497 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000498 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000499 return Method;
500}
501
502ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
503 ObjCInterfaceDecl *ClassDecl) {
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000504 if (!ClassDecl->hasDefinition())
505 return 0;
506
Steve Naroff5609ec02009-03-08 18:56:13 +0000507 ObjCMethodDecl *Method = 0;
508 while (ClassDecl && !Method) {
509 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000510 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000511 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Steve Naroff5609ec02009-03-08 18:56:13 +0000513 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000514 if (!Method)
515 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000516 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000517 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000518 return Method;
519}
520
John McCall3c3b7f92011-10-25 17:37:35 +0000521/// LookupMethodInType - Look up a method in an ObjCObjectType.
522ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type,
523 bool isInstance) {
524 const ObjCObjectType *objType = type->castAs<ObjCObjectType>();
525 if (ObjCInterfaceDecl *iface = objType->getInterface()) {
526 // Look it up in the main interface (and categories, etc.)
527 if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance))
528 return method;
529
530 // Okay, look for "private" methods declared in any
531 // @implementations we've seen.
532 if (isInstance) {
533 if (ObjCMethodDecl *method = LookupPrivateInstanceMethod(sel, iface))
534 return method;
535 } else {
536 if (ObjCMethodDecl *method = LookupPrivateClassMethod(sel, iface))
537 return method;
538 }
539 }
540
541 // Check qualifiers.
542 for (ObjCObjectType::qual_iterator
543 i = objType->qual_begin(), e = objType->qual_end(); i != e; ++i)
544 if (ObjCMethodDecl *method = (*i)->lookupMethod(sel, isInstance))
545 return method;
546
547 return 0;
548}
549
Fariborz Jahanian61478062011-03-09 20:18:06 +0000550/// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
551/// list of a qualified objective pointer type.
552ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
553 const ObjCObjectPointerType *OPT,
554 bool Instance)
555{
556 ObjCMethodDecl *MD = 0;
557 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
558 E = OPT->qual_end(); I != E; ++I) {
559 ObjCProtocolDecl *PROTO = (*I);
560 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
561 return MD;
562 }
563 }
564 return 0;
565}
566
Chris Lattner7f816522010-04-11 07:45:24 +0000567/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
568/// objective C interface. This is a property reference expression.
John McCall60d7b3a2010-08-24 06:29:42 +0000569ExprResult Sema::
Chris Lattner7f816522010-04-11 07:45:24 +0000570HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Fariborz Jahanian6326e052011-06-28 00:00:52 +0000571 Expr *BaseExpr, SourceLocation OpLoc,
572 DeclarationName MemberName,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000573 SourceLocation MemberLoc,
574 SourceLocation SuperLoc, QualType SuperType,
575 bool Super) {
Chris Lattner7f816522010-04-11 07:45:24 +0000576 const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
577 ObjCInterfaceDecl *IFace = IFaceT->getDecl();
Douglas Gregor109ec1b2011-04-20 18:19:55 +0000578
579 if (MemberName.getNameKind() != DeclarationName::Identifier) {
580 Diag(MemberLoc, diag::err_invalid_property_name)
581 << MemberName << QualType(OPT, 0);
582 return ExprError();
583 }
584
Chris Lattner7f816522010-04-11 07:45:24 +0000585 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
Douglas Gregorb3029962011-11-14 22:10:01 +0000586 SourceRange BaseRange = Super? SourceRange(SuperLoc)
587 : BaseExpr->getSourceRange();
588 if (RequireCompleteType(MemberLoc, OPT->getPointeeType(),
589 PDiag(diag::err_property_not_found_forward_class)
590 << MemberName << BaseRange))
Fariborz Jahanian8b1aba42010-12-16 00:56:28 +0000591 return ExprError();
Douglas Gregorb3029962011-11-14 22:10:01 +0000592
Chris Lattner7f816522010-04-11 07:45:24 +0000593 // Search for a declared property first.
594 if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
595 // Check whether we can reference this property.
596 if (DiagnoseUseOfDecl(PD, MemberLoc))
597 return ExprError();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000598
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000599 if (Super)
John McCall3c3b7f92011-10-25 17:37:35 +0000600 return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy,
John McCallf89e55a2010-11-18 06:31:45 +0000601 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000602 MemberLoc,
603 SuperLoc, SuperType));
604 else
John McCall3c3b7f92011-10-25 17:37:35 +0000605 return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy,
John McCallf89e55a2010-11-18 06:31:45 +0000606 VK_LValue, OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000607 MemberLoc, BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000608 }
609 // Check protocols on qualified interfaces.
610 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
611 E = OPT->qual_end(); I != E; ++I)
612 if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
613 // Check whether we can reference this property.
614 if (DiagnoseUseOfDecl(PD, MemberLoc))
615 return ExprError();
Douglas Gregor926df6c2011-06-11 01:09:30 +0000616
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000617 if (Super)
John McCall3c3b7f92011-10-25 17:37:35 +0000618 return Owned(new (Context) ObjCPropertyRefExpr(PD,
619 Context.PseudoObjectTy,
John McCallf89e55a2010-11-18 06:31:45 +0000620 VK_LValue,
621 OK_ObjCProperty,
622 MemberLoc,
623 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000624 else
John McCall3c3b7f92011-10-25 17:37:35 +0000625 return Owned(new (Context) ObjCPropertyRefExpr(PD,
626 Context.PseudoObjectTy,
John McCallf89e55a2010-11-18 06:31:45 +0000627 VK_LValue,
628 OK_ObjCProperty,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000629 MemberLoc,
630 BaseExpr));
Chris Lattner7f816522010-04-11 07:45:24 +0000631 }
632 // If that failed, look for an "implicit" property by seeing if the nullary
633 // selector is implemented.
634
635 // FIXME: The logic for looking up nullary and unary selectors should be
636 // shared with the code in ActOnInstanceMessage.
637
638 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
639 ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
Fariborz Jahanian27569b02011-03-09 22:17:12 +0000640
641 // May be founf in property's qualified list.
642 if (!Getter)
643 Getter = LookupMethodInQualifiedType(Sel, OPT, true);
Chris Lattner7f816522010-04-11 07:45:24 +0000644
645 // If this reference is in an @implementation, check for 'private' methods.
646 if (!Getter)
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000647 Getter = IFace->lookupPrivateMethod(Sel);
Chris Lattner7f816522010-04-11 07:45:24 +0000648
649 // Look through local category implementations associated with the class.
650 if (!Getter)
651 Getter = IFace->getCategoryInstanceMethod(Sel);
652 if (Getter) {
653 // Check if we can reference this property.
654 if (DiagnoseUseOfDecl(Getter, MemberLoc))
655 return ExprError();
656 }
657 // If we found a getter then this may be a valid dot-reference, we
658 // will look for the matching setter, in case it is needed.
659 Selector SetterSel =
660 SelectorTable::constructSetterName(PP.getIdentifierTable(),
661 PP.getSelectorTable(), Member);
662 ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
Fariborz Jahanian27569b02011-03-09 22:17:12 +0000663
664 // May be founf in property's qualified list.
665 if (!Setter)
666 Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
667
Chris Lattner7f816522010-04-11 07:45:24 +0000668 if (!Setter) {
669 // If this reference is in an @implementation, also check for 'private'
670 // methods.
Fariborz Jahanian74b27562010-12-03 23:37:08 +0000671 Setter = IFace->lookupPrivateMethod(SetterSel);
Chris Lattner7f816522010-04-11 07:45:24 +0000672 }
673 // Look through local category implementations associated with the class.
674 if (!Setter)
675 Setter = IFace->getCategoryInstanceMethod(SetterSel);
Fariborz Jahanian27569b02011-03-09 22:17:12 +0000676
Chris Lattner7f816522010-04-11 07:45:24 +0000677 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
678 return ExprError();
679
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000680 if (Getter || Setter) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000681 if (Super)
John McCall12f78a62010-12-02 01:19:52 +0000682 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
John McCall3c3b7f92011-10-25 17:37:35 +0000683 Context.PseudoObjectTy,
684 VK_LValue, OK_ObjCProperty,
John McCall12f78a62010-12-02 01:19:52 +0000685 MemberLoc,
686 SuperLoc, SuperType));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000687 else
John McCall12f78a62010-12-02 01:19:52 +0000688 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
John McCall3c3b7f92011-10-25 17:37:35 +0000689 Context.PseudoObjectTy,
690 VK_LValue, OK_ObjCProperty,
John McCall12f78a62010-12-02 01:19:52 +0000691 MemberLoc, BaseExpr));
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000692
Chris Lattner7f816522010-04-11 07:45:24 +0000693 }
694
695 // Attempt to correct for typos in property names.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000696 DeclFilterCCC<ObjCPropertyDecl> Validator;
697 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000698 DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, NULL,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000699 NULL, Validator, IFace, false, OPT)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000700 ObjCPropertyDecl *Property =
701 Corrected.getCorrectionDeclAs<ObjCPropertyDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000702 DeclarationName TypoResult = Corrected.getCorrection();
Chris Lattner7f816522010-04-11 07:45:24 +0000703 Diag(MemberLoc, diag::err_property_not_found_suggest)
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000704 << MemberName << QualType(OPT, 0) << TypoResult
705 << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
Chris Lattner7f816522010-04-11 07:45:24 +0000706 Diag(Property->getLocation(), diag::note_previous_decl)
707 << Property->getDeclName();
Fariborz Jahanian6326e052011-06-28 00:00:52 +0000708 return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc,
709 TypoResult, MemberLoc,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000710 SuperLoc, SuperType, Super);
Chris Lattner7f816522010-04-11 07:45:24 +0000711 }
Fariborz Jahanian41aadbc2011-02-17 01:26:14 +0000712 ObjCInterfaceDecl *ClassDeclared;
713 if (ObjCIvarDecl *Ivar =
714 IFace->lookupInstanceVariable(Member, ClassDeclared)) {
715 QualType T = Ivar->getType();
716 if (const ObjCObjectPointerType * OBJPT =
717 T->getAsObjCInterfacePointerType()) {
Douglas Gregorb3029962011-11-14 22:10:01 +0000718 if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(),
719 PDiag(diag::err_property_not_as_forward_class)
720 << MemberName << BaseExpr->getSourceRange()))
721 return ExprError();
Fariborz Jahanian41aadbc2011-02-17 01:26:14 +0000722 }
Fariborz Jahanian6326e052011-06-28 00:00:52 +0000723 Diag(MemberLoc,
724 diag::err_ivar_access_using_property_syntax_suggest)
725 << MemberName << QualType(OPT, 0) << Ivar->getDeclName()
726 << FixItHint::CreateReplacement(OpLoc, "->");
727 return ExprError();
Fariborz Jahanian41aadbc2011-02-17 01:26:14 +0000728 }
Chris Lattnerb9d4fc12010-04-11 07:51:10 +0000729
Chris Lattner7f816522010-04-11 07:45:24 +0000730 Diag(MemberLoc, diag::err_property_not_found)
731 << MemberName << QualType(OPT, 0);
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000732 if (Setter)
Chris Lattner7f816522010-04-11 07:45:24 +0000733 Diag(Setter->getLocation(), diag::note_getter_unavailable)
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000734 << MemberName << BaseExpr->getSourceRange();
Chris Lattner7f816522010-04-11 07:45:24 +0000735 return ExprError();
Chris Lattner7f816522010-04-11 07:45:24 +0000736}
737
738
739
John McCall60d7b3a2010-08-24 06:29:42 +0000740ExprResult Sema::
Chris Lattnereb483eb2010-04-11 08:28:14 +0000741ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
742 IdentifierInfo &propertyName,
743 SourceLocation receiverNameLoc,
744 SourceLocation propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000746 IdentifierInfo *receiverNamePtr = &receiverName;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000747 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
748 receiverNameLoc);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000749
750 bool IsSuper = false;
Chris Lattnereb483eb2010-04-11 08:28:14 +0000751 if (IFace == 0) {
752 // If the "receiver" is 'super' in a method, handle it as an expression-like
753 // property reference.
John McCall26743b22011-02-03 09:00:02 +0000754 if (receiverNamePtr->isStr("super")) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000755 IsSuper = true;
756
Eli Friedmanb942cb22012-02-03 22:47:37 +0000757 if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +0000758 if (CurMethod->isInstanceMethod()) {
759 QualType T =
760 Context.getObjCInterfaceType(CurMethod->getClassInterface());
761 T = Context.getObjCObjectPointerType(T);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000762
763 return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
Fariborz Jahanian6326e052011-06-28 00:00:52 +0000764 /*BaseExpr*/0,
765 SourceLocation()/*OpLoc*/,
766 &propertyName,
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000767 propertyNameLoc,
768 receiverNameLoc, T, true);
Chris Lattnereb483eb2010-04-11 08:28:14 +0000769 }
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattnereb483eb2010-04-11 08:28:14 +0000771 // Otherwise, if this is a class method, try dispatching to our
772 // superclass.
773 IFace = CurMethod->getClassInterface()->getSuperClass();
774 }
John McCall26743b22011-02-03 09:00:02 +0000775 }
Chris Lattnereb483eb2010-04-11 08:28:14 +0000776
777 if (IFace == 0) {
778 Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
779 return ExprError();
780 }
781 }
782
783 // Search for a declared property first.
Steve Naroff61f72cb2009-03-09 21:12:44 +0000784 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000785 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000786
787 // If this reference is in an @implementation, check for 'private' methods.
788 if (!Getter)
789 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
790 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000791 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000792 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000793
794 if (Getter) {
795 // FIXME: refactor/share with ActOnMemberReference().
796 // Check if we can reference this property.
797 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
798 return ExprError();
799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Steve Naroff61f72cb2009-03-09 21:12:44 +0000801 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000802 Selector SetterSel =
803 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000804 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000806 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000807 if (!Setter) {
808 // If this reference is in an @implementation, also check for 'private'
809 // methods.
810 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
811 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000812 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000813 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000814 }
815 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000816 if (!Setter)
817 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000818
819 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
820 return ExprError();
821
822 if (Getter || Setter) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000823 if (IsSuper)
824 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
John McCall3c3b7f92011-10-25 17:37:35 +0000825 Context.PseudoObjectTy,
826 VK_LValue, OK_ObjCProperty,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000827 propertyNameLoc,
828 receiverNameLoc,
829 Context.getObjCInterfaceType(IFace)));
830
John McCall12f78a62010-12-02 01:19:52 +0000831 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
John McCall3c3b7f92011-10-25 17:37:35 +0000832 Context.PseudoObjectTy,
833 VK_LValue, OK_ObjCProperty,
John McCall12f78a62010-12-02 01:19:52 +0000834 propertyNameLoc,
835 receiverNameLoc, IFace));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000836 }
837 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
838 << &propertyName << Context.getObjCInterfaceType(IFace));
839}
840
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000841namespace {
842
843class ObjCInterfaceOrSuperCCC : public CorrectionCandidateCallback {
844 public:
845 ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) {
846 // Determine whether "super" is acceptable in the current context.
847 if (Method && Method->getClassInterface())
848 WantObjCSuper = Method->getClassInterface()->getSuperClass();
849 }
850
851 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
852 return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() ||
853 candidate.isKeyword("super");
854 }
855};
856
857}
858
Douglas Gregor47bd5432010-04-14 02:46:37 +0000859Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Douglas Gregor1569f952010-04-21 20:38:13 +0000860 IdentifierInfo *Name,
Douglas Gregor47bd5432010-04-14 02:46:37 +0000861 SourceLocation NameLoc,
862 bool IsSuper,
Douglas Gregor1569f952010-04-21 20:38:13 +0000863 bool HasTrailingDot,
John McCallb3d87482010-08-24 05:47:05 +0000864 ParsedType &ReceiverType) {
865 ReceiverType = ParsedType();
Douglas Gregor1569f952010-04-21 20:38:13 +0000866
Douglas Gregor47bd5432010-04-14 02:46:37 +0000867 // If the identifier is "super" and there is no trailing dot, we're
Douglas Gregor95f42922010-10-14 22:11:03 +0000868 // messaging super. If the identifier is "super" and there is a
869 // trailing dot, it's an instance message.
870 if (IsSuper && S->isInObjcMethodScope())
871 return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000872
873 LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
874 LookupName(Result, S);
875
876 switch (Result.getResultKind()) {
877 case LookupResult::NotFound:
Douglas Gregored464422010-04-19 20:09:36 +0000878 // Normal name lookup didn't find anything. If we're in an
879 // Objective-C method, look for ivars. If we find one, we're done!
Douglas Gregor95f42922010-10-14 22:11:03 +0000880 // FIXME: This is a hack. Ivar lookup should be part of normal
881 // lookup.
Douglas Gregored464422010-04-19 20:09:36 +0000882 if (ObjCMethodDecl *Method = getCurMethodDecl()) {
Argyrios Kyrtzidisccc9e762011-11-09 00:22:48 +0000883 if (!Method->getClassInterface()) {
884 // Fall back: let the parser try to parse it as an instance message.
885 return ObjCInstanceMessage;
886 }
887
Douglas Gregored464422010-04-19 20:09:36 +0000888 ObjCInterfaceDecl *ClassDeclared;
889 if (Method->getClassInterface()->lookupInstanceVariable(Name,
890 ClassDeclared))
891 return ObjCInstanceMessage;
892 }
Douglas Gregor95f42922010-10-14 22:11:03 +0000893
Douglas Gregor47bd5432010-04-14 02:46:37 +0000894 // Break out; we'll perform typo correction below.
895 break;
896
897 case LookupResult::NotFoundInCurrentInstantiation:
898 case LookupResult::FoundOverloaded:
899 case LookupResult::FoundUnresolvedValue:
900 case LookupResult::Ambiguous:
901 Result.suppressDiagnostics();
902 return ObjCInstanceMessage;
903
904 case LookupResult::Found: {
Fariborz Jahanian8348de32011-02-08 00:23:07 +0000905 // If the identifier is a class or not, and there is a trailing dot,
906 // it's an instance message.
907 if (HasTrailingDot)
908 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000909 // We found something. If it's a type, then we have a class
910 // message. Otherwise, it's an instance message.
911 NamedDecl *ND = Result.getFoundDecl();
Douglas Gregor1569f952010-04-21 20:38:13 +0000912 QualType T;
913 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
914 T = Context.getObjCInterfaceType(Class);
915 else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
916 T = Context.getTypeDeclType(Type);
917 else
918 return ObjCInstanceMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000919
Douglas Gregor1569f952010-04-21 20:38:13 +0000920 // We have a class message, and T is the type we're
921 // messaging. Build source-location information for it.
922 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
John McCallb3d87482010-08-24 05:47:05 +0000923 ReceiverType = CreateParsedType(T, TSInfo);
Douglas Gregor1569f952010-04-21 20:38:13 +0000924 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000925 }
926 }
927
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000928 ObjCInterfaceOrSuperCCC Validator(getCurMethodDecl());
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000929 if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(),
930 Result.getLookupKind(), S, NULL,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000931 Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000932 if (Corrected.isKeyword()) {
933 // If we've found the keyword "super" (the only keyword that would be
934 // returned by CorrectTypo), this is a send to super.
Douglas Gregoraaf87162010-04-14 20:04:41 +0000935 Diag(NameLoc, diag::err_unknown_receiver_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000936 << Name << Corrected.getCorrection()
Douglas Gregoraaf87162010-04-14 20:04:41 +0000937 << FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
Douglas Gregoraaf87162010-04-14 20:04:41 +0000938 return ObjCSuperMessage;
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000939 } else if (ObjCInterfaceDecl *Class =
940 Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
941 // If we found a declaration, correct when it refers to an Objective-C
942 // class.
943 Diag(NameLoc, diag::err_unknown_receiver_suggest)
944 << Name << Corrected.getCorrection()
945 << FixItHint::CreateReplacement(SourceRange(NameLoc),
946 Class->getNameAsString());
947 Diag(Class->getLocation(), diag::note_previous_decl)
948 << Corrected.getCorrection();
949
950 QualType T = Context.getObjCInterfaceType(Class);
951 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
952 ReceiverType = CreateParsedType(T, TSInfo);
953 return ObjCClassMessage;
Douglas Gregor47bd5432010-04-14 02:46:37 +0000954 }
955 }
956
957 // Fall back: let the parser try to parse it as an instance message.
958 return ObjCInstanceMessage;
959}
Steve Naroff61f72cb2009-03-09 21:12:44 +0000960
John McCall60d7b3a2010-08-24 06:29:42 +0000961ExprResult Sema::ActOnSuperMessage(Scope *S,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000962 SourceLocation SuperLoc,
963 Selector Sel,
964 SourceLocation LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +0000965 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor0fbda682010-09-15 14:51:05 +0000966 SourceLocation RBracLoc,
967 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000968 // Determine whether we are inside a method or not.
Eli Friedmanb942cb22012-02-03 22:47:37 +0000969 ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc);
Douglas Gregorf95861a2010-04-21 20:01:04 +0000970 if (!Method) {
971 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
972 return ExprError();
973 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000974
Douglas Gregorf95861a2010-04-21 20:01:04 +0000975 ObjCInterfaceDecl *Class = Method->getClassInterface();
976 if (!Class) {
977 Diag(SuperLoc, diag::error_no_super_class_message)
978 << Method->getDeclName();
979 return ExprError();
980 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000981
Douglas Gregorf95861a2010-04-21 20:01:04 +0000982 ObjCInterfaceDecl *Super = Class->getSuperClass();
983 if (!Super) {
Douglas Gregor2725ca82010-04-21 19:57:20 +0000984 // The current class does not have a superclass.
Ted Kremeneke00909a2011-01-23 17:21:34 +0000985 Diag(SuperLoc, diag::error_root_class_cannot_use_super)
986 << Class->getIdentifier();
Douglas Gregor2725ca82010-04-21 19:57:20 +0000987 return ExprError();
Chris Lattner15faee12010-04-12 05:38:43 +0000988 }
Douglas Gregor2725ca82010-04-21 19:57:20 +0000989
Douglas Gregorf95861a2010-04-21 20:01:04 +0000990 // We are in a method whose class has a superclass, so 'super'
991 // is acting as a keyword.
992 if (Method->isInstanceMethod()) {
Nico Weber9a1ecf02011-08-22 17:25:57 +0000993 if (Sel.getMethodFamily() == OMF_dealloc)
994 ObjCShouldCallSuperDealloc = false;
Nico Weber80cb6e62011-08-28 22:35:17 +0000995 if (Sel.getMethodFamily() == OMF_finalize)
996 ObjCShouldCallSuperFinalize = false;
Nico Weber9a1ecf02011-08-22 17:25:57 +0000997
Douglas Gregorf95861a2010-04-21 20:01:04 +0000998 // Since we are in an instance method, this is an instance
999 // message to the superclass instance.
1000 QualType SuperTy = Context.getObjCInterfaceType(Super);
1001 SuperTy = Context.getObjCObjectPointerType(SuperTy);
John McCall9ae2f072010-08-23 23:25:46 +00001002 return BuildInstanceMessage(0, SuperTy, SuperLoc,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001003 Sel, /*Method=*/0,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001004 LBracLoc, SelectorLocs, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +00001005 }
Douglas Gregorf95861a2010-04-21 20:01:04 +00001006
1007 // Since we are in a class method, this is a class message to
1008 // the superclass.
1009 return BuildClassMessage(/*ReceiverTypeInfo=*/0,
1010 Context.getObjCInterfaceType(Super),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001011 SuperLoc, Sel, /*Method=*/0,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001012 LBracLoc, SelectorLocs, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +00001013}
1014
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001015
1016ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType,
1017 bool isSuperReceiver,
1018 SourceLocation Loc,
1019 Selector Sel,
1020 ObjCMethodDecl *Method,
1021 MultiExprArg Args) {
1022 TypeSourceInfo *receiverTypeInfo = 0;
1023 if (!ReceiverType.isNull())
1024 receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType);
1025
1026 return BuildClassMessage(receiverTypeInfo, ReceiverType,
1027 /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(),
1028 Sel, Method, Loc, Loc, Loc, Args,
1029 /*isImplicit=*/true);
1030
1031}
1032
Douglas Gregor2725ca82010-04-21 19:57:20 +00001033/// \brief Build an Objective-C class message expression.
1034///
1035/// This routine takes care of both normal class messages and
1036/// class messages to the superclass.
1037///
1038/// \param ReceiverTypeInfo Type source information that describes the
1039/// receiver of this message. This may be NULL, in which case we are
1040/// sending to the superclass and \p SuperLoc must be a valid source
1041/// location.
1042
1043/// \param ReceiverType The type of the object receiving the
1044/// message. When \p ReceiverTypeInfo is non-NULL, this is the same
1045/// type as that refers to. For a superclass send, this is the type of
1046/// the superclass.
1047///
1048/// \param SuperLoc The location of the "super" keyword in a
1049/// superclass message.
1050///
1051/// \param Sel The selector to which the message is being sent.
1052///
Douglas Gregorf49bb082010-04-22 17:01:48 +00001053/// \param Method The method that this class message is invoking, if
1054/// already known.
1055///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001056/// \param LBracLoc The location of the opening square bracket ']'.
1057///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001058/// \param RBrac The location of the closing square bracket ']'.
1059///
1060/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +00001061ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
Douglas Gregor0fbda682010-09-15 14:51:05 +00001062 QualType ReceiverType,
1063 SourceLocation SuperLoc,
1064 Selector Sel,
1065 ObjCMethodDecl *Method,
1066 SourceLocation LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001067 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor0fbda682010-09-15 14:51:05 +00001068 SourceLocation RBracLoc,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001069 MultiExprArg ArgsIn,
1070 bool isImplicit) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001071 SourceLocation Loc = SuperLoc.isValid()? SuperLoc
Douglas Gregor9497a732010-09-16 01:51:54 +00001072 : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001073 if (LBracLoc.isInvalid()) {
1074 Diag(Loc, diag::err_missing_open_square_message_send)
1075 << FixItHint::CreateInsertion(Loc, "[");
1076 LBracLoc = Loc;
1077 }
1078
Douglas Gregor92e986e2010-04-22 16:44:27 +00001079 if (ReceiverType->isDependentType()) {
1080 // If the receiver type is dependent, we can't type-check anything
1081 // at this point. Build a dependent expression.
1082 unsigned NumArgs = ArgsIn.size();
1083 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1084 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
John McCallf89e55a2010-11-18 06:31:45 +00001085 return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
1086 VK_RValue, LBracLoc, ReceiverTypeInfo,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001087 Sel, SelectorLocs, /*Method=*/0,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001088 makeArrayRef(Args, NumArgs),RBracLoc,
1089 isImplicit));
Douglas Gregor92e986e2010-04-22 16:44:27 +00001090 }
Chris Lattner15faee12010-04-12 05:38:43 +00001091
Douglas Gregor2725ca82010-04-21 19:57:20 +00001092 // Find the class to which we are sending this message.
1093 ObjCInterfaceDecl *Class = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00001094 const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
1095 if (!ClassType || !(Class = ClassType->getInterface())) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001096 Diag(Loc, diag::err_invalid_receiver_class_message)
1097 << ReceiverType;
1098 return ExprError();
Steve Naroff7c778f12008-07-25 19:39:00 +00001099 }
Douglas Gregor2725ca82010-04-21 19:57:20 +00001100 assert(Class && "We don't know which class we're messaging?");
Fariborz Jahanian43bcdb22011-10-15 19:18:36 +00001101 // objc++ diagnoses during typename annotation.
1102 if (!getLangOptions().CPlusPlus)
1103 (void)DiagnoseUseOfDecl(Class, Loc);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001104 // Find the method we are messaging.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001105 if (!Method) {
Douglas Gregorb3029962011-11-14 22:10:01 +00001106 SourceRange TypeRange
1107 = SuperLoc.isValid()? SourceRange(SuperLoc)
1108 : ReceiverTypeInfo->getTypeLoc().getSourceRange();
1109 if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class),
1110 (getLangOptions().ObjCAutoRefCount
1111 ? PDiag(diag::err_arc_receiver_forward_class)
1112 : PDiag(diag::warn_receiver_forward_class))
1113 << TypeRange)) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001114 // A forward class used in messaging is treated as a 'Class'
Douglas Gregorf49bb082010-04-22 17:01:48 +00001115 Method = LookupFactoryMethodInGlobalPool(Sel,
1116 SourceRange(LBracLoc, RBracLoc));
John McCallf85e1932011-06-15 23:02:42 +00001117 if (Method && !getLangOptions().ObjCAutoRefCount)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001118 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
1119 << Method->getDeclName();
1120 }
1121 if (!Method)
1122 Method = Class->lookupClassMethod(Sel);
1123
1124 // If we have an implementation in scope, check "private" methods.
1125 if (!Method)
1126 Method = LookupPrivateClassMethod(Sel, Class);
1127
1128 if (Method && DiagnoseUseOfDecl(Method, Loc))
1129 return ExprError();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +00001130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregor2725ca82010-04-21 19:57:20 +00001132 // Check the argument types and determine the result type.
1133 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001134 ExprValueKind VK = VK_RValue;
1135
Douglas Gregor2725ca82010-04-21 19:57:20 +00001136 unsigned NumArgs = ArgsIn.size();
1137 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
Douglas Gregor926df6c2011-06-11 01:09:30 +00001138 if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, true,
1139 SuperLoc.isValid(), LBracLoc, RBracLoc,
1140 ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001141 return ExprError();
Ted Kremenek4df728e2008-06-24 15:50:53 +00001142
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001143 if (Method && !Method->getResultType()->isVoidType() &&
1144 RequireCompleteType(LBracLoc, Method->getResultType(),
1145 diag::err_illegal_message_expr_incomplete_type))
1146 return ExprError();
1147
Douglas Gregor2725ca82010-04-21 19:57:20 +00001148 // Construct the appropriate ObjCMessageExpr.
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001149 Expr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001150 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001151 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001152 SuperLoc, /*IsInstanceSuper=*/false,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001153 ReceiverType, Sel, SelectorLocs,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00001154 Method, makeArrayRef(Args, NumArgs),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001155 RBracLoc, isImplicit);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001156 else
John McCallf89e55a2010-11-18 06:31:45 +00001157 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001158 ReceiverTypeInfo, Sel, SelectorLocs,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00001159 Method, makeArrayRef(Args, NumArgs),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001160 RBracLoc, isImplicit);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001161 return MaybeBindToTemporary(Result);
Chris Lattner85a932e2008-01-04 22:32:30 +00001162}
1163
Douglas Gregor2725ca82010-04-21 19:57:20 +00001164// ActOnClassMessage - used for both unary and keyword messages.
Chris Lattner85a932e2008-01-04 22:32:30 +00001165// ArgExprs is optional - if it is present, the number of expressions
1166// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001167ExprResult Sema::ActOnClassMessage(Scope *S,
Douglas Gregor77328d12010-09-15 23:19:31 +00001168 ParsedType Receiver,
1169 Selector Sel,
1170 SourceLocation LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001171 ArrayRef<SourceLocation> SelectorLocs,
Douglas Gregor77328d12010-09-15 23:19:31 +00001172 SourceLocation RBracLoc,
1173 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001174 TypeSourceInfo *ReceiverTypeInfo;
1175 QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
1176 if (ReceiverType.isNull())
1177 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Douglas Gregor2725ca82010-04-21 19:57:20 +00001180 if (!ReceiverTypeInfo)
1181 ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
1182
1183 return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
Douglas Gregorf49bb082010-04-22 17:01:48 +00001184 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001185 LBracLoc, SelectorLocs, RBracLoc, move(Args));
Douglas Gregor2725ca82010-04-21 19:57:20 +00001186}
1187
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001188ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver,
1189 QualType ReceiverType,
1190 SourceLocation Loc,
1191 Selector Sel,
1192 ObjCMethodDecl *Method,
1193 MultiExprArg Args) {
1194 return BuildInstanceMessage(Receiver, ReceiverType,
1195 /*SuperLoc=*/!Receiver ? Loc : SourceLocation(),
1196 Sel, Method, Loc, Loc, Loc, Args,
1197 /*isImplicit=*/true);
1198}
1199
Douglas Gregor2725ca82010-04-21 19:57:20 +00001200/// \brief Build an Objective-C instance message expression.
1201///
1202/// This routine takes care of both normal instance messages and
1203/// instance messages to the superclass instance.
1204///
1205/// \param Receiver The expression that computes the object that will
1206/// receive this message. This may be empty, in which case we are
1207/// sending to the superclass instance and \p SuperLoc must be a valid
1208/// source location.
1209///
1210/// \param ReceiverType The (static) type of the object receiving the
1211/// message. When a \p Receiver expression is provided, this is the
1212/// same type as that expression. For a superclass instance send, this
1213/// is a pointer to the type of the superclass.
1214///
1215/// \param SuperLoc The location of the "super" keyword in a
1216/// superclass instance message.
1217///
1218/// \param Sel The selector to which the message is being sent.
1219///
Douglas Gregorf49bb082010-04-22 17:01:48 +00001220/// \param Method The method that this instance message is invoking, if
1221/// already known.
1222///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001223/// \param LBracLoc The location of the opening square bracket ']'.
1224///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001225/// \param RBrac The location of the closing square bracket ']'.
1226///
1227/// \param Args The message arguments.
John McCall60d7b3a2010-08-24 06:29:42 +00001228ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001229 QualType ReceiverType,
1230 SourceLocation SuperLoc,
1231 Selector Sel,
1232 ObjCMethodDecl *Method,
1233 SourceLocation LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001234 ArrayRef<SourceLocation> SelectorLocs,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00001235 SourceLocation RBracLoc,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001236 MultiExprArg ArgsIn,
1237 bool isImplicit) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001238 // The location of the receiver.
1239 SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
1240
1241 if (LBracLoc.isInvalid()) {
1242 Diag(Loc, diag::err_missing_open_square_message_send)
1243 << FixItHint::CreateInsertion(Loc, "[");
1244 LBracLoc = Loc;
1245 }
1246
Douglas Gregor2725ca82010-04-21 19:57:20 +00001247 // If we have a receiver expression, perform appropriate promotions
1248 // and determine receiver type.
Douglas Gregor2725ca82010-04-21 19:57:20 +00001249 if (Receiver) {
John McCall5acb0c92011-10-17 18:40:02 +00001250 if (Receiver->hasPlaceholderType()) {
Douglas Gregorf1d1ca52011-12-01 01:37:36 +00001251 ExprResult Result;
1252 if (Receiver->getType() == Context.UnknownAnyTy)
1253 Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType());
1254 else
1255 Result = CheckPlaceholderExpr(Receiver);
1256 if (Result.isInvalid()) return ExprError();
1257 Receiver = Result.take();
John McCall5acb0c92011-10-17 18:40:02 +00001258 }
1259
Douglas Gregor92e986e2010-04-22 16:44:27 +00001260 if (Receiver->isTypeDependent()) {
1261 // If the receiver is type-dependent, we can't type-check anything
1262 // at this point. Build a dependent expression.
1263 unsigned NumArgs = ArgsIn.size();
1264 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1265 assert(SuperLoc.isInvalid() && "Message to super with dependent type");
1266 return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
John McCallf89e55a2010-11-18 06:31:45 +00001267 VK_RValue, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001268 SelectorLocs, /*Method=*/0,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00001269 makeArrayRef(Args, NumArgs),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001270 RBracLoc, isImplicit));
Douglas Gregor92e986e2010-04-22 16:44:27 +00001271 }
1272
Douglas Gregor2725ca82010-04-21 19:57:20 +00001273 // If necessary, apply function/array conversion to the receiver.
1274 // C99 6.7.5.3p[7,8].
John Wiegley429bb272011-04-08 18:41:53 +00001275 ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
1276 if (Result.isInvalid())
1277 return ExprError();
1278 Receiver = Result.take();
Douglas Gregor2725ca82010-04-21 19:57:20 +00001279 ReceiverType = Receiver->getType();
1280 }
1281
Douglas Gregorf49bb082010-04-22 17:01:48 +00001282 if (!Method) {
1283 // Handle messages to id.
Fariborz Jahanianba551982010-08-10 18:10:50 +00001284 bool receiverIsId = ReceiverType->isObjCIdType();
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001285 if (receiverIsId || ReceiverType->isBlockPointerType() ||
Douglas Gregorf49bb082010-04-22 17:01:48 +00001286 (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
1287 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001288 SourceRange(LBracLoc, RBracLoc),
1289 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001290 if (!Method)
Douglas Gregor2725ca82010-04-21 19:57:20 +00001291 Method = LookupFactoryMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001292 SourceRange(LBracLoc, RBracLoc),
1293 receiverIsId);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001294 } else if (ReceiverType->isObjCClassType() ||
1295 ReceiverType->isObjCQualifiedClassType()) {
1296 // Handle messages to Class.
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001297 // We allow sending a message to a qualified Class ("Class<foo>"), which
1298 // is ok as long as one of the protocols implements the selector (if not, warn).
1299 if (const ObjCObjectPointerType *QClassTy
1300 = ReceiverType->getAsObjCQualifiedClassType()) {
1301 // Search protocols for class methods.
1302 Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
1303 if (!Method) {
1304 Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
1305 // warn if instance method found for a Class message.
1306 if (Method) {
1307 Diag(Loc, diag::warn_instance_method_on_class_found)
1308 << Method->getSelector() << Sel;
Ted Kremenek3306ec12012-02-27 22:55:11 +00001309 Diag(Method->getLocation(), diag::note_method_declared_at)
1310 << Method->getDeclName();
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001311 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +00001312 }
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001313 } else {
1314 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1315 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1316 // First check the public methods in the class interface.
1317 Method = ClassDecl->lookupClassMethod(Sel);
1318
1319 if (!Method)
1320 Method = LookupPrivateClassMethod(Sel, ClassDecl);
1321 }
1322 if (Method && DiagnoseUseOfDecl(Method, Loc))
1323 return ExprError();
1324 }
1325 if (!Method) {
1326 // If not messaging 'self', look for any factory method named 'Sel'.
Douglas Gregorc737acb2011-09-27 16:10:05 +00001327 if (!Receiver || !isSelfExpr(Receiver)) {
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001328 Method = LookupFactoryMethodInGlobalPool(Sel,
1329 SourceRange(LBracLoc, RBracLoc),
1330 true);
1331 if (!Method) {
1332 // If no class (factory) method was found, check if an _instance_
1333 // method of the same name exists in the root class only.
1334 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001335 SourceRange(LBracLoc, RBracLoc),
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001336 true);
1337 if (Method)
1338 if (const ObjCInterfaceDecl *ID =
1339 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1340 if (ID->getSuperClass())
1341 Diag(Loc, diag::warn_root_inst_method_not_found)
1342 << Sel << SourceRange(LBracLoc, RBracLoc);
1343 }
1344 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001345 }
1346 }
1347 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001348 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001349 ObjCInterfaceDecl* ClassDecl = 0;
1350
1351 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1352 // long as one of the protocols implements the selector (if not, warn).
1353 if (const ObjCObjectPointerType *QIdTy
1354 = ReceiverType->getAsObjCQualifiedIdType()) {
1355 // Search protocols for instance methods.
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001356 Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
1357 if (!Method)
1358 Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001359 } else if (const ObjCObjectPointerType *OCIType
1360 = ReceiverType->getAsObjCInterfacePointerType()) {
1361 // We allow sending a message to a pointer to an interface (an object).
1362 ClassDecl = OCIType->getInterfaceDecl();
John McCallf85e1932011-06-15 23:02:42 +00001363
Douglas Gregorb3029962011-11-14 22:10:01 +00001364 // Try to complete the type. Under ARC, this is a hard error from which
1365 // we don't try to recover.
1366 const ObjCInterfaceDecl *forwardClass = 0;
1367 if (RequireCompleteType(Loc, OCIType->getPointeeType(),
1368 getLangOptions().ObjCAutoRefCount
1369 ? PDiag(diag::err_arc_receiver_forward_instance)
1370 << (Receiver ? Receiver->getSourceRange()
1371 : SourceRange(SuperLoc))
Fariborz Jahanian4cc9b102012-02-03 01:02:44 +00001372 : PDiag(diag::warn_receiver_forward_instance)
1373 << (Receiver ? Receiver->getSourceRange()
1374 : SourceRange(SuperLoc)))) {
Douglas Gregorb3029962011-11-14 22:10:01 +00001375 if (getLangOptions().ObjCAutoRefCount)
1376 return ExprError();
1377
1378 forwardClass = OCIType->getInterfaceDecl();
Fariborz Jahanian4cc9b102012-02-03 01:02:44 +00001379 Diag(Receiver ? Receiver->getLocStart()
1380 : SuperLoc, diag::note_receiver_is_id);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001381 Method = 0;
1382 } else {
1383 Method = ClassDecl->lookupInstanceMethod(Sel);
John McCallf85e1932011-06-15 23:02:42 +00001384 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001385
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001386 if (!Method)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001387 // Search protocol qualifiers.
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001388 Method = LookupMethodInQualifiedType(Sel, OCIType, true);
1389
Douglas Gregorf49bb082010-04-22 17:01:48 +00001390 if (!Method) {
1391 // If we have implementations in scope, check "private" methods.
1392 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1393
John McCallf85e1932011-06-15 23:02:42 +00001394 if (!Method && getLangOptions().ObjCAutoRefCount) {
1395 Diag(Loc, diag::err_arc_may_not_respond)
1396 << OCIType->getPointeeType() << Sel;
1397 return ExprError();
1398 }
1399
Douglas Gregorc737acb2011-09-27 16:10:05 +00001400 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001401 // If we still haven't found a method, look in the global pool. This
1402 // behavior isn't very desirable, however we need it for GCC
1403 // compatibility. FIXME: should we deviate??
1404 if (OCIType->qual_empty()) {
1405 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001406 SourceRange(LBracLoc, RBracLoc));
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001407 if (Method && !forwardClass)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001408 Diag(Loc, diag::warn_maynot_respond)
1409 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1410 }
1411 }
1412 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001413 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorf49bb082010-04-22 17:01:48 +00001414 return ExprError();
John McCallf85e1932011-06-15 23:02:42 +00001415 } else if (!getLangOptions().ObjCAutoRefCount &&
1416 !Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001417 (ReceiverType->isPointerType() ||
1418 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001419 // Implicitly convert integers and pointers to 'id' but emit a warning.
John McCallf85e1932011-06-15 23:02:42 +00001420 // But not in ARC.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001421 Diag(Loc, diag::warn_bad_receiver_type)
1422 << ReceiverType
1423 << Receiver->getSourceRange();
1424 if (ReceiverType->isPointerType())
John Wiegley429bb272011-04-08 18:41:53 +00001425 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall1d9b3b22011-09-09 05:25:32 +00001426 CK_CPointerToObjCPointerCast).take();
John McCall404cd162010-11-13 01:35:44 +00001427 else {
1428 // TODO: specialized warning on null receivers?
1429 bool IsNull = Receiver->isNullPointerConstant(Context,
1430 Expr::NPC_ValueDependentIsNull);
John Wiegley429bb272011-04-08 18:41:53 +00001431 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1432 IsNull ? CK_NullToPointer : CK_IntegralToPointer).take();
John McCall404cd162010-11-13 01:35:44 +00001433 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001434 ReceiverType = Receiver->getType();
John McCall0bcc9bc2011-09-09 06:11:02 +00001435 } else {
John Wiegley429bb272011-04-08 18:41:53 +00001436 ExprResult ReceiverRes;
1437 if (getLangOptions().CPlusPlus)
John McCall0bcc9bc2011-09-09 06:11:02 +00001438 ReceiverRes = PerformContextuallyConvertToObjCPointer(Receiver);
John Wiegley429bb272011-04-08 18:41:53 +00001439 if (ReceiverRes.isUsable()) {
1440 Receiver = ReceiverRes.take();
John Wiegley429bb272011-04-08 18:41:53 +00001441 return BuildInstanceMessage(Receiver,
1442 ReceiverType,
1443 SuperLoc,
1444 Sel,
1445 Method,
1446 LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001447 SelectorLocs,
John Wiegley429bb272011-04-08 18:41:53 +00001448 RBracLoc,
1449 move(ArgsIn));
1450 } else {
1451 // Reject other random receiver types (e.g. structs).
1452 Diag(Loc, diag::err_bad_receiver_type)
1453 << ReceiverType << Receiver->getSourceRange();
1454 return ExprError();
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001455 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001456 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001457 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001458 }
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Douglas Gregor2725ca82010-04-21 19:57:20 +00001460 // Check the message arguments.
1461 unsigned NumArgs = ArgsIn.size();
1462 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1463 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001464 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001465 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1466 ReceiverType->isObjCQualifiedClassType());
Douglas Gregor926df6c2011-06-11 01:09:30 +00001467 if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method,
1468 ClassMessage, SuperLoc.isValid(),
John McCallf89e55a2010-11-18 06:31:45 +00001469 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001470 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001471
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001472 if (Method && !Method->getResultType()->isVoidType() &&
1473 RequireCompleteType(LBracLoc, Method->getResultType(),
1474 diag::err_illegal_message_expr_incomplete_type))
1475 return ExprError();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001476
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001477 SourceLocation SelLoc = SelectorLocs.front();
1478
John McCallf85e1932011-06-15 23:02:42 +00001479 // In ARC, forbid the user from sending messages to
1480 // retain/release/autorelease/dealloc/retainCount explicitly.
1481 if (getLangOptions().ObjCAutoRefCount) {
1482 ObjCMethodFamily family =
1483 (Method ? Method->getMethodFamily() : Sel.getMethodFamily());
1484 switch (family) {
1485 case OMF_init:
1486 if (Method)
1487 checkInitMethod(Method, ReceiverType);
1488
1489 case OMF_None:
1490 case OMF_alloc:
1491 case OMF_copy:
Nico Weber80cb6e62011-08-28 22:35:17 +00001492 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001493 case OMF_mutableCopy:
1494 case OMF_new:
1495 case OMF_self:
1496 break;
1497
1498 case OMF_dealloc:
1499 case OMF_retain:
1500 case OMF_release:
1501 case OMF_autorelease:
1502 case OMF_retainCount:
1503 Diag(Loc, diag::err_arc_illegal_explicit_message)
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001504 << Sel << SelLoc;
John McCallf85e1932011-06-15 23:02:42 +00001505 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001506
1507 case OMF_performSelector:
1508 if (Method && NumArgs >= 1) {
1509 if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) {
1510 Selector ArgSel = SelExp->getSelector();
1511 ObjCMethodDecl *SelMethod =
1512 LookupInstanceMethodInGlobalPool(ArgSel,
1513 SelExp->getSourceRange());
1514 if (!SelMethod)
1515 SelMethod =
1516 LookupFactoryMethodInGlobalPool(ArgSel,
1517 SelExp->getSourceRange());
1518 if (SelMethod) {
1519 ObjCMethodFamily SelFamily = SelMethod->getMethodFamily();
1520 switch (SelFamily) {
1521 case OMF_alloc:
1522 case OMF_copy:
1523 case OMF_mutableCopy:
1524 case OMF_new:
1525 case OMF_self:
1526 case OMF_init:
1527 // Issue error, unless ns_returns_not_retained.
1528 if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1529 // selector names a +1 method
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001530 Diag(SelLoc,
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001531 diag::err_arc_perform_selector_retains);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001532 Diag(SelMethod->getLocation(), diag::note_method_declared_at)
1533 << SelMethod->getDeclName();
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001534 }
1535 break;
1536 default:
1537 // +0 call. OK. unless ns_returns_retained.
1538 if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) {
1539 // selector names a +1 method
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001540 Diag(SelLoc,
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001541 diag::err_arc_perform_selector_retains);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001542 Diag(SelMethod->getLocation(), diag::note_method_declared_at)
1543 << SelMethod->getDeclName();
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001544 }
1545 break;
1546 }
1547 }
1548 } else {
1549 // error (may leak).
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001550 Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001551 Diag(Args[0]->getExprLoc(), diag::note_used_here);
1552 }
1553 }
1554 break;
John McCallf85e1932011-06-15 23:02:42 +00001555 }
1556 }
1557
Douglas Gregor2725ca82010-04-21 19:57:20 +00001558 // Construct the appropriate ObjCMessageExpr instance.
John McCallf85e1932011-06-15 23:02:42 +00001559 ObjCMessageExpr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001560 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001561 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001562 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001563 ReceiverType, Sel, SelectorLocs, Method,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001564 makeArrayRef(Args, NumArgs), RBracLoc,
1565 isImplicit);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001566 else
John McCallf89e55a2010-11-18 06:31:45 +00001567 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001568 Receiver, Sel, SelectorLocs, Method,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001569 makeArrayRef(Args, NumArgs), RBracLoc,
1570 isImplicit);
John McCallf85e1932011-06-15 23:02:42 +00001571
1572 if (getLangOptions().ObjCAutoRefCount) {
1573 // In ARC, annotate delegate init calls.
1574 if (Result->getMethodFamily() == OMF_init &&
Douglas Gregorc737acb2011-09-27 16:10:05 +00001575 (SuperLoc.isValid() || isSelfExpr(Receiver))) {
John McCallf85e1932011-06-15 23:02:42 +00001576 // Only consider init calls *directly* in init implementations,
1577 // not within blocks.
1578 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext);
1579 if (method && method->getMethodFamily() == OMF_init) {
1580 // The implicit assignment to self means we also don't want to
1581 // consume the result.
1582 Result->setDelegateInitCall(true);
1583 return Owned(Result);
1584 }
1585 }
1586
1587 // In ARC, check for message sends which are likely to introduce
1588 // retain cycles.
1589 checkRetainCycles(Result);
1590 }
1591
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001592 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001593}
1594
1595// ActOnInstanceMessage - used for both unary and keyword messages.
1596// ArgExprs is optional - if it is present, the number of expressions
1597// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001598ExprResult Sema::ActOnInstanceMessage(Scope *S,
1599 Expr *Receiver,
1600 Selector Sel,
1601 SourceLocation LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001602 ArrayRef<SourceLocation> SelectorLocs,
John McCall60d7b3a2010-08-24 06:29:42 +00001603 SourceLocation RBracLoc,
1604 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001605 if (!Receiver)
1606 return ExprError();
1607
John McCall9ae2f072010-08-23 23:25:46 +00001608 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001609 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001610 LBracLoc, SelectorLocs, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001611}
Chris Lattnereca7be62008-04-07 05:30:13 +00001612
John McCallf85e1932011-06-15 23:02:42 +00001613enum ARCConversionTypeClass {
John McCall2cf031d2011-10-01 01:01:08 +00001614 /// int, void, struct A
John McCallf85e1932011-06-15 23:02:42 +00001615 ACTC_none,
John McCall2cf031d2011-10-01 01:01:08 +00001616
1617 /// id, void (^)()
John McCallf85e1932011-06-15 23:02:42 +00001618 ACTC_retainable,
John McCall2cf031d2011-10-01 01:01:08 +00001619
1620 /// id*, id***, void (^*)(),
1621 ACTC_indirectRetainable,
1622
1623 /// void* might be a normal C type, or it might a CF type.
1624 ACTC_voidPtr,
1625
1626 /// struct A*
1627 ACTC_coreFoundation
John McCallf85e1932011-06-15 23:02:42 +00001628};
John McCall2cf031d2011-10-01 01:01:08 +00001629static bool isAnyRetainable(ARCConversionTypeClass ACTC) {
1630 return (ACTC == ACTC_retainable ||
1631 ACTC == ACTC_coreFoundation ||
1632 ACTC == ACTC_voidPtr);
1633}
1634static bool isAnyCLike(ARCConversionTypeClass ACTC) {
1635 return ACTC == ACTC_none ||
1636 ACTC == ACTC_voidPtr ||
1637 ACTC == ACTC_coreFoundation;
1638}
1639
John McCallf85e1932011-06-15 23:02:42 +00001640static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) {
John McCall2cf031d2011-10-01 01:01:08 +00001641 bool isIndirect = false;
John McCallf85e1932011-06-15 23:02:42 +00001642
1643 // Ignore an outermost reference type.
John McCall2cf031d2011-10-01 01:01:08 +00001644 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
John McCallf85e1932011-06-15 23:02:42 +00001645 type = ref->getPointeeType();
John McCall2cf031d2011-10-01 01:01:08 +00001646 isIndirect = true;
1647 }
John McCallf85e1932011-06-15 23:02:42 +00001648
1649 // Drill through pointers and arrays recursively.
1650 while (true) {
1651 if (const PointerType *ptr = type->getAs<PointerType>()) {
1652 type = ptr->getPointeeType();
John McCall2cf031d2011-10-01 01:01:08 +00001653
1654 // The first level of pointer may be the innermost pointer on a CF type.
1655 if (!isIndirect) {
1656 if (type->isVoidType()) return ACTC_voidPtr;
1657 if (type->isRecordType()) return ACTC_coreFoundation;
1658 }
John McCallf85e1932011-06-15 23:02:42 +00001659 } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) {
1660 type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
1661 } else {
1662 break;
1663 }
John McCall2cf031d2011-10-01 01:01:08 +00001664 isIndirect = true;
John McCallf85e1932011-06-15 23:02:42 +00001665 }
1666
John McCall2cf031d2011-10-01 01:01:08 +00001667 if (isIndirect) {
1668 if (type->isObjCARCBridgableType())
1669 return ACTC_indirectRetainable;
1670 return ACTC_none;
1671 }
1672
1673 if (type->isObjCARCBridgableType())
1674 return ACTC_retainable;
1675
1676 return ACTC_none;
John McCallf85e1932011-06-15 23:02:42 +00001677}
1678
1679namespace {
John McCall2cf031d2011-10-01 01:01:08 +00001680 /// A result from the cast checker.
1681 enum ACCResult {
1682 /// Cannot be casted.
1683 ACC_invalid,
1684
1685 /// Can be safely retained or not retained.
1686 ACC_bottom,
1687
1688 /// Can be casted at +0.
1689 ACC_plusZero,
1690
1691 /// Can be casted at +1.
1692 ACC_plusOne
1693 };
1694 ACCResult merge(ACCResult left, ACCResult right) {
1695 if (left == right) return left;
1696 if (left == ACC_bottom) return right;
1697 if (right == ACC_bottom) return left;
1698 return ACC_invalid;
1699 }
1700
1701 /// A checker which white-lists certain expressions whose conversion
1702 /// to or from retainable type would otherwise be forbidden in ARC.
1703 class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> {
1704 typedef StmtVisitor<ARCCastChecker, ACCResult> super;
1705
John McCallf85e1932011-06-15 23:02:42 +00001706 ASTContext &Context;
John McCall2cf031d2011-10-01 01:01:08 +00001707 ARCConversionTypeClass SourceClass;
1708 ARCConversionTypeClass TargetClass;
1709
1710 static bool isCFType(QualType type) {
1711 // Someday this can use ns_bridged. For now, it has to do this.
1712 return type->isCARCBridgableType();
John McCallf85e1932011-06-15 23:02:42 +00001713 }
John McCall2cf031d2011-10-01 01:01:08 +00001714
1715 public:
1716 ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source,
1717 ARCConversionTypeClass target)
1718 : Context(Context), SourceClass(source), TargetClass(target) {}
1719
1720 using super::Visit;
1721 ACCResult Visit(Expr *e) {
1722 return super::Visit(e->IgnoreParens());
1723 }
1724
1725 ACCResult VisitStmt(Stmt *s) {
1726 return ACC_invalid;
1727 }
1728
1729 /// Null pointer constants can be casted however you please.
1730 ACCResult VisitExpr(Expr *e) {
1731 if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
1732 return ACC_bottom;
1733 return ACC_invalid;
1734 }
1735
1736 /// Objective-C string literals can be safely casted.
1737 ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) {
1738 // If we're casting to any retainable type, go ahead. Global
1739 // strings are immune to retains, so this is bottom.
1740 if (isAnyRetainable(TargetClass)) return ACC_bottom;
1741
1742 return ACC_invalid;
John McCallf85e1932011-06-15 23:02:42 +00001743 }
1744
John McCall2cf031d2011-10-01 01:01:08 +00001745 /// Look through certain implicit and explicit casts.
1746 ACCResult VisitCastExpr(CastExpr *e) {
John McCallf85e1932011-06-15 23:02:42 +00001747 switch (e->getCastKind()) {
1748 case CK_NullToPointer:
John McCall2cf031d2011-10-01 01:01:08 +00001749 return ACC_bottom;
1750
John McCallf85e1932011-06-15 23:02:42 +00001751 case CK_NoOp:
1752 case CK_LValueToRValue:
1753 case CK_BitCast:
John McCall1d9b3b22011-09-09 05:25:32 +00001754 case CK_CPointerToObjCPointerCast:
1755 case CK_BlockPointerToObjCPointerCast:
John McCallf85e1932011-06-15 23:02:42 +00001756 case CK_AnyPointerToBlockPointerCast:
1757 return Visit(e->getSubExpr());
John McCall2cf031d2011-10-01 01:01:08 +00001758
John McCallf85e1932011-06-15 23:02:42 +00001759 default:
John McCall2cf031d2011-10-01 01:01:08 +00001760 return ACC_invalid;
John McCallf85e1932011-06-15 23:02:42 +00001761 }
1762 }
John McCall2cf031d2011-10-01 01:01:08 +00001763
1764 /// Look through unary extension.
1765 ACCResult VisitUnaryExtension(UnaryOperator *e) {
John McCallf85e1932011-06-15 23:02:42 +00001766 return Visit(e->getSubExpr());
1767 }
John McCall2cf031d2011-10-01 01:01:08 +00001768
1769 /// Ignore the LHS of a comma operator.
1770 ACCResult VisitBinComma(BinaryOperator *e) {
John McCallf85e1932011-06-15 23:02:42 +00001771 return Visit(e->getRHS());
1772 }
John McCall2cf031d2011-10-01 01:01:08 +00001773
1774 /// Conditional operators are okay if both sides are okay.
1775 ACCResult VisitConditionalOperator(ConditionalOperator *e) {
1776 ACCResult left = Visit(e->getTrueExpr());
1777 if (left == ACC_invalid) return ACC_invalid;
1778 return merge(left, Visit(e->getFalseExpr()));
John McCallf85e1932011-06-15 23:02:42 +00001779 }
John McCall2cf031d2011-10-01 01:01:08 +00001780
John McCall4b9c2d22011-11-06 09:01:30 +00001781 /// Look through pseudo-objects.
1782 ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) {
1783 // If we're getting here, we should always have a result.
1784 return Visit(e->getResultExpr());
1785 }
1786
John McCall2cf031d2011-10-01 01:01:08 +00001787 /// Statement expressions are okay if their result expression is okay.
1788 ACCResult VisitStmtExpr(StmtExpr *e) {
John McCallf85e1932011-06-15 23:02:42 +00001789 return Visit(e->getSubStmt()->body_back());
1790 }
John McCallf85e1932011-06-15 23:02:42 +00001791
John McCall2cf031d2011-10-01 01:01:08 +00001792 /// Some declaration references are okay.
1793 ACCResult VisitDeclRefExpr(DeclRefExpr *e) {
1794 // References to global constants from system headers are okay.
1795 // These are things like 'kCFStringTransformToLatin'. They are
1796 // can also be assumed to be immune to retains.
1797 VarDecl *var = dyn_cast<VarDecl>(e->getDecl());
1798 if (isAnyRetainable(TargetClass) &&
1799 isAnyRetainable(SourceClass) &&
1800 var &&
1801 var->getStorageClass() == SC_Extern &&
1802 var->getType().isConstQualified() &&
1803 Context.getSourceManager().isInSystemHeader(var->getLocation())) {
1804 return ACC_bottom;
1805 }
1806
1807 // Nothing else.
1808 return ACC_invalid;
Fariborz Jahanianaf975172011-06-21 17:38:29 +00001809 }
John McCall2cf031d2011-10-01 01:01:08 +00001810
1811 /// Some calls are okay.
1812 ACCResult VisitCallExpr(CallExpr *e) {
1813 if (FunctionDecl *fn = e->getDirectCallee())
1814 if (ACCResult result = checkCallToFunction(fn))
1815 return result;
1816
1817 return super::VisitCallExpr(e);
1818 }
1819
1820 ACCResult checkCallToFunction(FunctionDecl *fn) {
1821 // Require a CF*Ref return type.
1822 if (!isCFType(fn->getResultType()))
1823 return ACC_invalid;
1824
1825 if (!isAnyRetainable(TargetClass))
1826 return ACC_invalid;
1827
1828 // Honor an explicit 'not retained' attribute.
1829 if (fn->hasAttr<CFReturnsNotRetainedAttr>())
1830 return ACC_plusZero;
1831
1832 // Honor an explicit 'retained' attribute, except that for
1833 // now we're not going to permit implicit handling of +1 results,
1834 // because it's a bit frightening.
1835 if (fn->hasAttr<CFReturnsRetainedAttr>())
1836 return ACC_invalid; // ACC_plusOne if we start accepting this
1837
1838 // Recognize this specific builtin function, which is used by CFSTR.
1839 unsigned builtinID = fn->getBuiltinID();
1840 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
1841 return ACC_bottom;
1842
1843 // Otherwise, don't do anything implicit with an unaudited function.
1844 if (!fn->hasAttr<CFAuditedTransferAttr>())
1845 return ACC_invalid;
1846
1847 // Otherwise, it's +0 unless it follows the create convention.
1848 if (ento::coreFoundation::followsCreateRule(fn))
1849 return ACC_invalid; // ACC_plusOne if we start accepting this
1850
1851 return ACC_plusZero;
1852 }
1853
1854 ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) {
1855 return checkCallToMethod(e->getMethodDecl());
1856 }
1857
1858 ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) {
1859 ObjCMethodDecl *method;
1860 if (e->isExplicitProperty())
1861 method = e->getExplicitProperty()->getGetterMethodDecl();
1862 else
1863 method = e->getImplicitPropertyGetter();
1864 return checkCallToMethod(method);
1865 }
1866
1867 ACCResult checkCallToMethod(ObjCMethodDecl *method) {
1868 if (!method) return ACC_invalid;
1869
1870 // Check for message sends to functions returning CF types. We
1871 // just obey the Cocoa conventions with these, even though the
1872 // return type is CF.
1873 if (!isAnyRetainable(TargetClass) || !isCFType(method->getResultType()))
1874 return ACC_invalid;
1875
1876 // If the method is explicitly marked not-retained, it's +0.
1877 if (method->hasAttr<CFReturnsNotRetainedAttr>())
1878 return ACC_plusZero;
1879
1880 // If the method is explicitly marked as returning retained, or its
1881 // selector follows a +1 Cocoa convention, treat it as +1.
1882 if (method->hasAttr<CFReturnsRetainedAttr>())
1883 return ACC_plusOne;
1884
1885 switch (method->getSelector().getMethodFamily()) {
1886 case OMF_alloc:
1887 case OMF_copy:
1888 case OMF_mutableCopy:
1889 case OMF_new:
1890 return ACC_plusOne;
1891
1892 default:
1893 // Otherwise, treat it as +0.
1894 return ACC_plusZero;
Fariborz Jahanianc8505ad2011-06-21 19:42:38 +00001895 }
1896 }
John McCall2cf031d2011-10-01 01:01:08 +00001897 };
Fariborz Jahanian1522a7c2011-06-20 20:54:42 +00001898}
1899
Fariborz Jahanian52b62362012-02-01 22:56:20 +00001900static bool
1901KnownName(Sema &S, const char *name) {
1902 LookupResult R(S, &S.Context.Idents.get(name), SourceLocation(),
1903 Sema::LookupOrdinaryName);
1904 return S.LookupName(R, S.TUScope, false);
1905}
1906
Argyrios Kyrtzidisae1b4af2012-02-16 17:31:07 +00001907static void addFixitForObjCARCConversion(Sema &S,
1908 DiagnosticBuilder &DiagB,
1909 Sema::CheckedConversionKind CCK,
1910 SourceLocation afterLParen,
1911 QualType castType,
1912 Expr *castExpr,
1913 const char *bridgeKeyword,
1914 const char *CFBridgeName) {
1915 // We handle C-style and implicit casts here.
1916 switch (CCK) {
1917 case Sema::CCK_ImplicitConversion:
1918 case Sema::CCK_CStyleCast:
1919 break;
1920 case Sema::CCK_FunctionalCast:
1921 case Sema::CCK_OtherCast:
1922 return;
1923 }
1924
1925 if (CFBridgeName) {
1926 Expr *castedE = castExpr;
1927 if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE))
1928 castedE = CCE->getSubExpr();
1929 castedE = castedE->IgnoreImpCasts();
1930 SourceRange range = castedE->getSourceRange();
1931 if (isa<ParenExpr>(castedE)) {
1932 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1933 CFBridgeName));
1934 } else {
1935 std::string namePlusParen = CFBridgeName;
1936 namePlusParen += "(";
1937 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1938 namePlusParen));
1939 DiagB.AddFixItHint(FixItHint::CreateInsertion(
1940 S.PP.getLocForEndOfToken(range.getEnd()),
1941 ")"));
1942 }
1943 return;
1944 }
1945
1946 if (CCK == Sema::CCK_CStyleCast) {
1947 DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword));
1948 } else {
1949 std::string castCode = "(";
1950 castCode += bridgeKeyword;
1951 castCode += castType.getAsString();
1952 castCode += ")";
1953 Expr *castedE = castExpr->IgnoreImpCasts();
1954 SourceRange range = castedE->getSourceRange();
1955 if (isa<ParenExpr>(castedE)) {
1956 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1957 castCode));
1958 } else {
1959 castCode += "(";
1960 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1961 castCode));
1962 DiagB.AddFixItHint(FixItHint::CreateInsertion(
1963 S.PP.getLocForEndOfToken(range.getEnd()),
1964 ")"));
1965 }
1966 }
1967}
1968
John McCall5acb0c92011-10-17 18:40:02 +00001969static void
1970diagnoseObjCARCConversion(Sema &S, SourceRange castRange,
1971 QualType castType, ARCConversionTypeClass castACTC,
1972 Expr *castExpr, ARCConversionTypeClass exprACTC,
1973 Sema::CheckedConversionKind CCK) {
John McCallf85e1932011-06-15 23:02:42 +00001974 SourceLocation loc =
John McCall2cf031d2011-10-01 01:01:08 +00001975 (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc());
John McCallf85e1932011-06-15 23:02:42 +00001976
John McCall5acb0c92011-10-17 18:40:02 +00001977 if (S.makeUnavailableInSystemHeader(loc,
John McCall2cf031d2011-10-01 01:01:08 +00001978 "converts between Objective-C and C pointers in -fobjc-arc"))
John McCallf85e1932011-06-15 23:02:42 +00001979 return;
John McCall5acb0c92011-10-17 18:40:02 +00001980
1981 QualType castExprType = castExpr->getType();
John McCallf85e1932011-06-15 23:02:42 +00001982
John McCall71c482c2011-06-17 06:50:50 +00001983 unsigned srcKind = 0;
John McCallf85e1932011-06-15 23:02:42 +00001984 switch (exprACTC) {
John McCall2cf031d2011-10-01 01:01:08 +00001985 case ACTC_none:
1986 case ACTC_coreFoundation:
1987 case ACTC_voidPtr:
1988 srcKind = (castExprType->isPointerType() ? 1 : 0);
1989 break;
1990 case ACTC_retainable:
1991 srcKind = (castExprType->isBlockPointerType() ? 2 : 3);
1992 break;
1993 case ACTC_indirectRetainable:
1994 srcKind = 4;
1995 break;
John McCallf85e1932011-06-15 23:02:42 +00001996 }
1997
John McCall5acb0c92011-10-17 18:40:02 +00001998 // Check whether this could be fixed with a bridge cast.
1999 SourceLocation afterLParen = S.PP.getLocForEndOfToken(castRange.getBegin());
2000 SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc;
John McCallf85e1932011-06-15 23:02:42 +00002001
John McCall5acb0c92011-10-17 18:40:02 +00002002 // Bridge from an ARC type to a CF type.
2003 if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) {
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002004
John McCall5acb0c92011-10-17 18:40:02 +00002005 S.Diag(loc, diag::err_arc_cast_requires_bridge)
2006 << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit
2007 << 2 // of C pointer type
2008 << castExprType
2009 << unsigned(castType->isBlockPointerType()) // to ObjC|block type
2010 << castType
2011 << castRange
2012 << castExpr->getSourceRange();
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002013 bool br = KnownName(S, "CFBridgingRelease");
Argyrios Kyrtzidisae1b4af2012-02-16 17:31:07 +00002014 {
2015 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge);
2016 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2017 castType, castExpr, "__bridge ", 0);
2018 }
2019 {
2020 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_transfer)
2021 << castExprType << br;
2022 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2023 castType, castExpr, "__bridge_transfer ",
2024 br ? "CFBridgingRelease" : 0);
2025 }
John McCall5acb0c92011-10-17 18:40:02 +00002026
2027 return;
2028 }
2029
2030 // Bridge from a CF type to an ARC type.
2031 if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) {
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002032 bool br = KnownName(S, "CFBridgingRetain");
John McCall5acb0c92011-10-17 18:40:02 +00002033 S.Diag(loc, diag::err_arc_cast_requires_bridge)
2034 << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit
2035 << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type
2036 << castExprType
2037 << 2 // to C pointer type
2038 << castType
2039 << castRange
2040 << castExpr->getSourceRange();
2041
Argyrios Kyrtzidisae1b4af2012-02-16 17:31:07 +00002042 {
2043 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge);
2044 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2045 castType, castExpr, "__bridge ", 0);
2046 }
2047 {
2048 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_retained)
2049 << castType << br;
2050 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2051 castType, castExpr, "__bridge_retained ",
2052 br ? "CFBridgingRetain" : 0);
2053 }
John McCall5acb0c92011-10-17 18:40:02 +00002054
2055 return;
John McCallf85e1932011-06-15 23:02:42 +00002056 }
2057
John McCall5acb0c92011-10-17 18:40:02 +00002058 S.Diag(loc, diag::err_arc_mismatched_cast)
2059 << (CCK != Sema::CCK_ImplicitConversion)
2060 << srcKind << castExprType << castType
John McCallf85e1932011-06-15 23:02:42 +00002061 << castRange << castExpr->getSourceRange();
2062}
2063
John McCall5acb0c92011-10-17 18:40:02 +00002064Sema::ARCConversionResult
2065Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
2066 Expr *&castExpr, CheckedConversionKind CCK) {
2067 QualType castExprType = castExpr->getType();
2068
2069 // For the purposes of the classification, we assume reference types
2070 // will bind to temporaries.
2071 QualType effCastType = castType;
2072 if (const ReferenceType *ref = castType->getAs<ReferenceType>())
2073 effCastType = ref->getPointeeType();
2074
2075 ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType);
2076 ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType);
Fariborz Jahanian6d09f012011-10-28 20:06:07 +00002077 if (exprACTC == castACTC) {
2078 // check for viablity and report error if casting an rvalue to a
2079 // life-time qualifier.
Fariborz Jahanianfc2eff52011-10-29 00:06:10 +00002080 if ((castACTC == ACTC_retainable) &&
Fariborz Jahanian6d09f012011-10-28 20:06:07 +00002081 (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) &&
Fariborz Jahanianfc2eff52011-10-29 00:06:10 +00002082 (castType != castExprType)) {
2083 const Type *DT = castType.getTypePtr();
2084 QualType QDT = castType;
2085 // We desugar some types but not others. We ignore those
2086 // that cannot happen in a cast; i.e. auto, and those which
2087 // should not be de-sugared; i.e typedef.
2088 if (const ParenType *PT = dyn_cast<ParenType>(DT))
2089 QDT = PT->desugar();
2090 else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
2091 QDT = TP->desugar();
2092 else if (const AttributedType *AT = dyn_cast<AttributedType>(DT))
2093 QDT = AT->desugar();
2094 if (QDT != castType &&
2095 QDT.getObjCLifetime() != Qualifiers::OCL_None) {
2096 SourceLocation loc =
2097 (castRange.isValid() ? castRange.getBegin()
2098 : castExpr->getExprLoc());
2099 Diag(loc, diag::err_arc_nolifetime_behavior);
2100 }
Fariborz Jahanian6d09f012011-10-28 20:06:07 +00002101 }
2102 return ACR_okay;
2103 }
2104
John McCall5acb0c92011-10-17 18:40:02 +00002105 if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay;
2106
2107 // Allow all of these types to be cast to integer types (but not
2108 // vice-versa).
2109 if (castACTC == ACTC_none && castType->isIntegralType(Context))
2110 return ACR_okay;
2111
2112 // Allow casts between pointers to lifetime types (e.g., __strong id*)
2113 // and pointers to void (e.g., cv void *). Casting from void* to lifetime*
2114 // must be explicit.
2115 if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr)
2116 return ACR_okay;
2117 if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr &&
2118 CCK != CCK_ImplicitConversion)
2119 return ACR_okay;
2120
2121 switch (ARCCastChecker(Context, exprACTC, castACTC).Visit(castExpr)) {
2122 // For invalid casts, fall through.
2123 case ACC_invalid:
2124 break;
2125
2126 // Do nothing for both bottom and +0.
2127 case ACC_bottom:
2128 case ACC_plusZero:
2129 return ACR_okay;
2130
2131 // If the result is +1, consume it here.
2132 case ACC_plusOne:
2133 castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(),
2134 CK_ARCConsumeObject, castExpr,
2135 0, VK_RValue);
2136 ExprNeedsCleanups = true;
2137 return ACR_okay;
2138 }
2139
2140 // If this is a non-implicit cast from id or block type to a
2141 // CoreFoundation type, delay complaining in case the cast is used
2142 // in an acceptable context.
2143 if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) &&
2144 CCK != CCK_ImplicitConversion)
2145 return ACR_unbridged;
2146
2147 diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
2148 castExpr, exprACTC, CCK);
2149 return ACR_okay;
2150}
2151
2152/// Given that we saw an expression with the ARCUnbridgedCastTy
2153/// placeholder type, complain bitterly.
2154void Sema::diagnoseARCUnbridgedCast(Expr *e) {
2155 // We expect the spurious ImplicitCastExpr to already have been stripped.
2156 assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
2157 CastExpr *realCast = cast<CastExpr>(e->IgnoreParens());
2158
2159 SourceRange castRange;
2160 QualType castType;
2161 CheckedConversionKind CCK;
2162
2163 if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) {
2164 castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc());
2165 castType = cast->getTypeAsWritten();
2166 CCK = CCK_CStyleCast;
2167 } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) {
2168 castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
2169 castType = cast->getTypeAsWritten();
2170 CCK = CCK_OtherCast;
2171 } else {
2172 castType = cast->getType();
2173 CCK = CCK_ImplicitConversion;
2174 }
2175
2176 ARCConversionTypeClass castACTC =
2177 classifyTypeForARCConversion(castType.getNonReferenceType());
2178
2179 Expr *castExpr = realCast->getSubExpr();
2180 assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable);
2181
2182 diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
2183 castExpr, ACTC_retainable, CCK);
2184}
2185
2186/// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast
2187/// type, remove the placeholder cast.
2188Expr *Sema::stripARCUnbridgedCast(Expr *e) {
2189 assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
2190
2191 if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
2192 Expr *sub = stripARCUnbridgedCast(pe->getSubExpr());
2193 return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub);
2194 } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
2195 assert(uo->getOpcode() == UO_Extension);
2196 Expr *sub = stripARCUnbridgedCast(uo->getSubExpr());
2197 return new (Context) UnaryOperator(sub, UO_Extension, sub->getType(),
2198 sub->getValueKind(), sub->getObjectKind(),
2199 uo->getOperatorLoc());
2200 } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
2201 assert(!gse->isResultDependent());
2202
2203 unsigned n = gse->getNumAssocs();
2204 SmallVector<Expr*, 4> subExprs(n);
2205 SmallVector<TypeSourceInfo*, 4> subTypes(n);
2206 for (unsigned i = 0; i != n; ++i) {
2207 subTypes[i] = gse->getAssocTypeSourceInfo(i);
2208 Expr *sub = gse->getAssocExpr(i);
2209 if (i == gse->getResultIndex())
2210 sub = stripARCUnbridgedCast(sub);
2211 subExprs[i] = sub;
2212 }
2213
2214 return new (Context) GenericSelectionExpr(Context, gse->getGenericLoc(),
2215 gse->getControllingExpr(),
2216 subTypes.data(), subExprs.data(),
2217 n, gse->getDefaultLoc(),
2218 gse->getRParenLoc(),
2219 gse->containsUnexpandedParameterPack(),
2220 gse->getResultIndex());
2221 } else {
2222 assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!");
2223 return cast<ImplicitCastExpr>(e)->getSubExpr();
2224 }
2225}
2226
Fariborz Jahanian7a084ec2011-07-07 23:04:17 +00002227bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType,
2228 QualType exprType) {
2229 QualType canCastType =
2230 Context.getCanonicalType(castType).getUnqualifiedType();
2231 QualType canExprType =
2232 Context.getCanonicalType(exprType).getUnqualifiedType();
2233 if (isa<ObjCObjectPointerType>(canCastType) &&
2234 castType.getObjCLifetime() == Qualifiers::OCL_Weak &&
2235 canExprType->isObjCObjectPointerType()) {
2236 if (const ObjCObjectPointerType *ObjT =
2237 canExprType->getAs<ObjCObjectPointerType>())
2238 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable())
2239 return false;
2240 }
2241 return true;
2242}
2243
John McCall7e5e5f42011-07-07 06:58:02 +00002244/// Look for an ObjCReclaimReturnedObject cast and destroy it.
2245static Expr *maybeUndoReclaimObject(Expr *e) {
2246 // For now, we just undo operands that are *immediately* reclaim
2247 // expressions, which prevents the vast majority of potential
2248 // problems here. To catch them all, we'd need to rebuild arbitrary
2249 // value-propagating subexpressions --- we can't reliably rebuild
2250 // in-place because of expression sharing.
2251 if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
John McCall33e56f32011-09-10 06:18:15 +00002252 if (ice->getCastKind() == CK_ARCReclaimReturnedObject)
John McCall7e5e5f42011-07-07 06:58:02 +00002253 return ice->getSubExpr();
2254
2255 return e;
2256}
2257
John McCallf85e1932011-06-15 23:02:42 +00002258ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc,
2259 ObjCBridgeCastKind Kind,
2260 SourceLocation BridgeKeywordLoc,
2261 TypeSourceInfo *TSInfo,
2262 Expr *SubExpr) {
John McCall4906cf92011-08-26 00:48:42 +00002263 ExprResult SubResult = UsualUnaryConversions(SubExpr);
2264 if (SubResult.isInvalid()) return ExprError();
2265 SubExpr = SubResult.take();
2266
John McCallf85e1932011-06-15 23:02:42 +00002267 QualType T = TSInfo->getType();
2268 QualType FromType = SubExpr->getType();
2269
John McCall1d9b3b22011-09-09 05:25:32 +00002270 CastKind CK;
2271
John McCallf85e1932011-06-15 23:02:42 +00002272 bool MustConsume = false;
2273 if (T->isDependentType() || SubExpr->isTypeDependent()) {
2274 // Okay: we'll build a dependent expression type.
John McCall1d9b3b22011-09-09 05:25:32 +00002275 CK = CK_Dependent;
John McCallf85e1932011-06-15 23:02:42 +00002276 } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) {
2277 // Casting CF -> id
John McCall1d9b3b22011-09-09 05:25:32 +00002278 CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast
2279 : CK_CPointerToObjCPointerCast);
John McCallf85e1932011-06-15 23:02:42 +00002280 switch (Kind) {
2281 case OBC_Bridge:
2282 break;
2283
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002284 case OBC_BridgeRetained: {
2285 bool br = KnownName(*this, "CFBridgingRelease");
John McCallf85e1932011-06-15 23:02:42 +00002286 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
2287 << 2
2288 << FromType
2289 << (T->isBlockPointerType()? 1 : 0)
2290 << T
2291 << SubExpr->getSourceRange()
2292 << Kind;
2293 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
2294 << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge");
2295 Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002296 << FromType << br
John McCallf85e1932011-06-15 23:02:42 +00002297 << FixItHint::CreateReplacement(BridgeKeywordLoc,
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002298 br ? "CFBridgingRelease "
2299 : "__bridge_transfer ");
John McCallf85e1932011-06-15 23:02:42 +00002300
2301 Kind = OBC_Bridge;
2302 break;
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002303 }
John McCallf85e1932011-06-15 23:02:42 +00002304
2305 case OBC_BridgeTransfer:
2306 // We must consume the Objective-C object produced by the cast.
2307 MustConsume = true;
2308 break;
2309 }
2310 } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) {
2311 // Okay: id -> CF
John McCall1d9b3b22011-09-09 05:25:32 +00002312 CK = CK_BitCast;
John McCallf85e1932011-06-15 23:02:42 +00002313 switch (Kind) {
2314 case OBC_Bridge:
John McCall7e5e5f42011-07-07 06:58:02 +00002315 // Reclaiming a value that's going to be __bridge-casted to CF
2316 // is very dangerous, so we don't do it.
2317 SubExpr = maybeUndoReclaimObject(SubExpr);
John McCallf85e1932011-06-15 23:02:42 +00002318 break;
2319
2320 case OBC_BridgeRetained:
2321 // Produce the object before casting it.
2322 SubExpr = ImplicitCastExpr::Create(Context, FromType,
John McCall33e56f32011-09-10 06:18:15 +00002323 CK_ARCProduceObject,
John McCallf85e1932011-06-15 23:02:42 +00002324 SubExpr, 0, VK_RValue);
2325 break;
2326
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002327 case OBC_BridgeTransfer: {
2328 bool br = KnownName(*this, "CFBridgingRetain");
John McCallf85e1932011-06-15 23:02:42 +00002329 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
2330 << (FromType->isBlockPointerType()? 1 : 0)
2331 << FromType
2332 << 2
2333 << T
2334 << SubExpr->getSourceRange()
2335 << Kind;
2336
2337 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
2338 << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge ");
2339 Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002340 << T << br
2341 << FixItHint::CreateReplacement(BridgeKeywordLoc,
2342 br ? "CFBridgingRetain " : "__bridge_retained");
John McCallf85e1932011-06-15 23:02:42 +00002343
2344 Kind = OBC_Bridge;
2345 break;
2346 }
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002347 }
John McCallf85e1932011-06-15 23:02:42 +00002348 } else {
2349 Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
2350 << FromType << T << Kind
2351 << SubExpr->getSourceRange()
2352 << TSInfo->getTypeLoc().getSourceRange();
2353 return ExprError();
2354 }
2355
John McCall1d9b3b22011-09-09 05:25:32 +00002356 Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK,
John McCallf85e1932011-06-15 23:02:42 +00002357 BridgeKeywordLoc,
2358 TSInfo, SubExpr);
2359
2360 if (MustConsume) {
2361 ExprNeedsCleanups = true;
John McCall33e56f32011-09-10 06:18:15 +00002362 Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result,
John McCallf85e1932011-06-15 23:02:42 +00002363 0, VK_RValue);
2364 }
2365
2366 return Result;
2367}
2368
2369ExprResult Sema::ActOnObjCBridgedCast(Scope *S,
2370 SourceLocation LParenLoc,
2371 ObjCBridgeCastKind Kind,
2372 SourceLocation BridgeKeywordLoc,
2373 ParsedType Type,
2374 SourceLocation RParenLoc,
2375 Expr *SubExpr) {
2376 TypeSourceInfo *TSInfo = 0;
2377 QualType T = GetTypeFromParser(Type, &TSInfo);
2378 if (!TSInfo)
2379 TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc);
2380 return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo,
2381 SubExpr);
2382}