blob: ec5329c4d4cb6488cadd186f76caab22677c7d41 [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;
1309 Diag(Method->getLocation(), diag::note_method_declared_at);
1310 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +00001311 }
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001312 } else {
1313 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
1314 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
1315 // First check the public methods in the class interface.
1316 Method = ClassDecl->lookupClassMethod(Sel);
1317
1318 if (!Method)
1319 Method = LookupPrivateClassMethod(Sel, ClassDecl);
1320 }
1321 if (Method && DiagnoseUseOfDecl(Method, Loc))
1322 return ExprError();
1323 }
1324 if (!Method) {
1325 // If not messaging 'self', look for any factory method named 'Sel'.
Douglas Gregorc737acb2011-09-27 16:10:05 +00001326 if (!Receiver || !isSelfExpr(Receiver)) {
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001327 Method = LookupFactoryMethodInGlobalPool(Sel,
1328 SourceRange(LBracLoc, RBracLoc),
1329 true);
1330 if (!Method) {
1331 // If no class (factory) method was found, check if an _instance_
1332 // method of the same name exists in the root class only.
1333 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001334 SourceRange(LBracLoc, RBracLoc),
Fariborz Jahanian759abb42011-04-06 18:40:08 +00001335 true);
1336 if (Method)
1337 if (const ObjCInterfaceDecl *ID =
1338 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
1339 if (ID->getSuperClass())
1340 Diag(Loc, diag::warn_root_inst_method_not_found)
1341 << Sel << SourceRange(LBracLoc, RBracLoc);
1342 }
1343 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001344 }
1345 }
1346 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001347 } else {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001348 ObjCInterfaceDecl* ClassDecl = 0;
1349
1350 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
1351 // long as one of the protocols implements the selector (if not, warn).
1352 if (const ObjCObjectPointerType *QIdTy
1353 = ReceiverType->getAsObjCQualifiedIdType()) {
1354 // Search protocols for instance methods.
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001355 Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
1356 if (!Method)
1357 Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
Douglas Gregorf49bb082010-04-22 17:01:48 +00001358 } else if (const ObjCObjectPointerType *OCIType
1359 = ReceiverType->getAsObjCInterfacePointerType()) {
1360 // We allow sending a message to a pointer to an interface (an object).
1361 ClassDecl = OCIType->getInterfaceDecl();
John McCallf85e1932011-06-15 23:02:42 +00001362
Douglas Gregorb3029962011-11-14 22:10:01 +00001363 // Try to complete the type. Under ARC, this is a hard error from which
1364 // we don't try to recover.
1365 const ObjCInterfaceDecl *forwardClass = 0;
1366 if (RequireCompleteType(Loc, OCIType->getPointeeType(),
1367 getLangOptions().ObjCAutoRefCount
1368 ? PDiag(diag::err_arc_receiver_forward_instance)
1369 << (Receiver ? Receiver->getSourceRange()
1370 : SourceRange(SuperLoc))
Fariborz Jahanian4cc9b102012-02-03 01:02:44 +00001371 : PDiag(diag::warn_receiver_forward_instance)
1372 << (Receiver ? Receiver->getSourceRange()
1373 : SourceRange(SuperLoc)))) {
Douglas Gregorb3029962011-11-14 22:10:01 +00001374 if (getLangOptions().ObjCAutoRefCount)
1375 return ExprError();
1376
1377 forwardClass = OCIType->getInterfaceDecl();
Fariborz Jahanian4cc9b102012-02-03 01:02:44 +00001378 Diag(Receiver ? Receiver->getLocStart()
1379 : SuperLoc, diag::note_receiver_is_id);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001380 Method = 0;
1381 } else {
1382 Method = ClassDecl->lookupInstanceMethod(Sel);
John McCallf85e1932011-06-15 23:02:42 +00001383 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001384
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001385 if (!Method)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001386 // Search protocol qualifiers.
Fariborz Jahanian27569b02011-03-09 22:17:12 +00001387 Method = LookupMethodInQualifiedType(Sel, OCIType, true);
1388
Douglas Gregorf49bb082010-04-22 17:01:48 +00001389 if (!Method) {
1390 // If we have implementations in scope, check "private" methods.
1391 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
1392
John McCallf85e1932011-06-15 23:02:42 +00001393 if (!Method && getLangOptions().ObjCAutoRefCount) {
1394 Diag(Loc, diag::err_arc_may_not_respond)
1395 << OCIType->getPointeeType() << Sel;
1396 return ExprError();
1397 }
1398
Douglas Gregorc737acb2011-09-27 16:10:05 +00001399 if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001400 // If we still haven't found a method, look in the global pool. This
1401 // behavior isn't very desirable, however we need it for GCC
1402 // compatibility. FIXME: should we deviate??
1403 if (OCIType->qual_empty()) {
1404 Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001405 SourceRange(LBracLoc, RBracLoc));
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001406 if (Method && !forwardClass)
Douglas Gregorf49bb082010-04-22 17:01:48 +00001407 Diag(Loc, diag::warn_maynot_respond)
1408 << OCIType->getInterfaceDecl()->getIdentifier() << Sel;
1409 }
1410 }
1411 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001412 if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
Douglas Gregorf49bb082010-04-22 17:01:48 +00001413 return ExprError();
John McCallf85e1932011-06-15 23:02:42 +00001414 } else if (!getLangOptions().ObjCAutoRefCount &&
1415 !Context.getObjCIdType().isNull() &&
Douglas Gregorf6094622010-07-23 15:58:24 +00001416 (ReceiverType->isPointerType() ||
1417 ReceiverType->isIntegerType())) {
Douglas Gregorf49bb082010-04-22 17:01:48 +00001418 // Implicitly convert integers and pointers to 'id' but emit a warning.
John McCallf85e1932011-06-15 23:02:42 +00001419 // But not in ARC.
Douglas Gregorf49bb082010-04-22 17:01:48 +00001420 Diag(Loc, diag::warn_bad_receiver_type)
1421 << ReceiverType
1422 << Receiver->getSourceRange();
1423 if (ReceiverType->isPointerType())
John Wiegley429bb272011-04-08 18:41:53 +00001424 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
John McCall1d9b3b22011-09-09 05:25:32 +00001425 CK_CPointerToObjCPointerCast).take();
John McCall404cd162010-11-13 01:35:44 +00001426 else {
1427 // TODO: specialized warning on null receivers?
1428 bool IsNull = Receiver->isNullPointerConstant(Context,
1429 Expr::NPC_ValueDependentIsNull);
John Wiegley429bb272011-04-08 18:41:53 +00001430 Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
1431 IsNull ? CK_NullToPointer : CK_IntegralToPointer).take();
John McCall404cd162010-11-13 01:35:44 +00001432 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001433 ReceiverType = Receiver->getType();
John McCall0bcc9bc2011-09-09 06:11:02 +00001434 } else {
John Wiegley429bb272011-04-08 18:41:53 +00001435 ExprResult ReceiverRes;
1436 if (getLangOptions().CPlusPlus)
John McCall0bcc9bc2011-09-09 06:11:02 +00001437 ReceiverRes = PerformContextuallyConvertToObjCPointer(Receiver);
John Wiegley429bb272011-04-08 18:41:53 +00001438 if (ReceiverRes.isUsable()) {
1439 Receiver = ReceiverRes.take();
John Wiegley429bb272011-04-08 18:41:53 +00001440 return BuildInstanceMessage(Receiver,
1441 ReceiverType,
1442 SuperLoc,
1443 Sel,
1444 Method,
1445 LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001446 SelectorLocs,
John Wiegley429bb272011-04-08 18:41:53 +00001447 RBracLoc,
1448 move(ArgsIn));
1449 } else {
1450 // Reject other random receiver types (e.g. structs).
1451 Diag(Loc, diag::err_bad_receiver_type)
1452 << ReceiverType << Receiver->getSourceRange();
1453 return ExprError();
Fariborz Jahanian3ba60612010-05-13 17:19:25 +00001454 }
Douglas Gregorf49bb082010-04-22 17:01:48 +00001455 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001456 }
Chris Lattnerfe1a5532008-07-21 05:57:44 +00001457 }
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregor2725ca82010-04-21 19:57:20 +00001459 // Check the message arguments.
1460 unsigned NumArgs = ArgsIn.size();
1461 Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
1462 QualType ReturnType;
John McCallf89e55a2010-11-18 06:31:45 +00001463 ExprValueKind VK = VK_RValue;
Fariborz Jahanian26005032010-12-01 01:07:24 +00001464 bool ClassMessage = (ReceiverType->isObjCClassType() ||
1465 ReceiverType->isObjCQualifiedClassType());
Douglas Gregor926df6c2011-06-11 01:09:30 +00001466 if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method,
1467 ClassMessage, SuperLoc.isValid(),
John McCallf89e55a2010-11-18 06:31:45 +00001468 LBracLoc, RBracLoc, ReturnType, VK))
Douglas Gregor2725ca82010-04-21 19:57:20 +00001469 return ExprError();
Fariborz Jahanianda59e092010-06-16 19:56:08 +00001470
Douglas Gregor483dd2f2011-01-11 03:23:19 +00001471 if (Method && !Method->getResultType()->isVoidType() &&
1472 RequireCompleteType(LBracLoc, Method->getResultType(),
1473 diag::err_illegal_message_expr_incomplete_type))
1474 return ExprError();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001475
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001476 SourceLocation SelLoc = SelectorLocs.front();
1477
John McCallf85e1932011-06-15 23:02:42 +00001478 // In ARC, forbid the user from sending messages to
1479 // retain/release/autorelease/dealloc/retainCount explicitly.
1480 if (getLangOptions().ObjCAutoRefCount) {
1481 ObjCMethodFamily family =
1482 (Method ? Method->getMethodFamily() : Sel.getMethodFamily());
1483 switch (family) {
1484 case OMF_init:
1485 if (Method)
1486 checkInitMethod(Method, ReceiverType);
1487
1488 case OMF_None:
1489 case OMF_alloc:
1490 case OMF_copy:
Nico Weber80cb6e62011-08-28 22:35:17 +00001491 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001492 case OMF_mutableCopy:
1493 case OMF_new:
1494 case OMF_self:
1495 break;
1496
1497 case OMF_dealloc:
1498 case OMF_retain:
1499 case OMF_release:
1500 case OMF_autorelease:
1501 case OMF_retainCount:
1502 Diag(Loc, diag::err_arc_illegal_explicit_message)
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001503 << Sel << SelLoc;
John McCallf85e1932011-06-15 23:02:42 +00001504 break;
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001505
1506 case OMF_performSelector:
1507 if (Method && NumArgs >= 1) {
1508 if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) {
1509 Selector ArgSel = SelExp->getSelector();
1510 ObjCMethodDecl *SelMethod =
1511 LookupInstanceMethodInGlobalPool(ArgSel,
1512 SelExp->getSourceRange());
1513 if (!SelMethod)
1514 SelMethod =
1515 LookupFactoryMethodInGlobalPool(ArgSel,
1516 SelExp->getSourceRange());
1517 if (SelMethod) {
1518 ObjCMethodFamily SelFamily = SelMethod->getMethodFamily();
1519 switch (SelFamily) {
1520 case OMF_alloc:
1521 case OMF_copy:
1522 case OMF_mutableCopy:
1523 case OMF_new:
1524 case OMF_self:
1525 case OMF_init:
1526 // Issue error, unless ns_returns_not_retained.
1527 if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1528 // selector names a +1 method
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001529 Diag(SelLoc,
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001530 diag::err_arc_perform_selector_retains);
1531 Diag(SelMethod->getLocation(), diag::note_method_declared_at);
1532 }
1533 break;
1534 default:
1535 // +0 call. OK. unless ns_returns_retained.
1536 if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) {
1537 // selector names a +1 method
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001538 Diag(SelLoc,
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001539 diag::err_arc_perform_selector_retains);
1540 Diag(SelMethod->getLocation(), diag::note_method_declared_at);
1541 }
1542 break;
1543 }
1544 }
1545 } else {
1546 // error (may leak).
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001547 Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001548 Diag(Args[0]->getExprLoc(), diag::note_used_here);
1549 }
1550 }
1551 break;
John McCallf85e1932011-06-15 23:02:42 +00001552 }
1553 }
1554
Douglas Gregor2725ca82010-04-21 19:57:20 +00001555 // Construct the appropriate ObjCMessageExpr instance.
John McCallf85e1932011-06-15 23:02:42 +00001556 ObjCMessageExpr *Result;
Douglas Gregor2725ca82010-04-21 19:57:20 +00001557 if (SuperLoc.isValid())
John McCallf89e55a2010-11-18 06:31:45 +00001558 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001559 SuperLoc, /*IsInstanceSuper=*/true,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001560 ReceiverType, Sel, SelectorLocs, Method,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001561 makeArrayRef(Args, NumArgs), RBracLoc,
1562 isImplicit);
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001563 else
John McCallf89e55a2010-11-18 06:31:45 +00001564 Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001565 Receiver, Sel, SelectorLocs, Method,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00001566 makeArrayRef(Args, NumArgs), RBracLoc,
1567 isImplicit);
John McCallf85e1932011-06-15 23:02:42 +00001568
1569 if (getLangOptions().ObjCAutoRefCount) {
1570 // In ARC, annotate delegate init calls.
1571 if (Result->getMethodFamily() == OMF_init &&
Douglas Gregorc737acb2011-09-27 16:10:05 +00001572 (SuperLoc.isValid() || isSelfExpr(Receiver))) {
John McCallf85e1932011-06-15 23:02:42 +00001573 // Only consider init calls *directly* in init implementations,
1574 // not within blocks.
1575 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext);
1576 if (method && method->getMethodFamily() == OMF_init) {
1577 // The implicit assignment to self means we also don't want to
1578 // consume the result.
1579 Result->setDelegateInitCall(true);
1580 return Owned(Result);
1581 }
1582 }
1583
1584 // In ARC, check for message sends which are likely to introduce
1585 // retain cycles.
1586 checkRetainCycles(Result);
1587 }
1588
Douglas Gregor2d6b0e92010-05-22 05:17:18 +00001589 return MaybeBindToTemporary(Result);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001590}
1591
1592// ActOnInstanceMessage - used for both unary and keyword messages.
1593// ArgExprs is optional - if it is present, the number of expressions
1594// is obtained from Sel.getNumArgs().
John McCall60d7b3a2010-08-24 06:29:42 +00001595ExprResult Sema::ActOnInstanceMessage(Scope *S,
1596 Expr *Receiver,
1597 Selector Sel,
1598 SourceLocation LBracLoc,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001599 ArrayRef<SourceLocation> SelectorLocs,
John McCall60d7b3a2010-08-24 06:29:42 +00001600 SourceLocation RBracLoc,
1601 MultiExprArg Args) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001602 if (!Receiver)
1603 return ExprError();
1604
John McCall9ae2f072010-08-23 23:25:46 +00001605 return BuildInstanceMessage(Receiver, Receiver->getType(),
Douglas Gregorf49bb082010-04-22 17:01:48 +00001606 /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00001607 LBracLoc, SelectorLocs, RBracLoc, move(Args));
Chris Lattner85a932e2008-01-04 22:32:30 +00001608}
Chris Lattnereca7be62008-04-07 05:30:13 +00001609
John McCallf85e1932011-06-15 23:02:42 +00001610enum ARCConversionTypeClass {
John McCall2cf031d2011-10-01 01:01:08 +00001611 /// int, void, struct A
John McCallf85e1932011-06-15 23:02:42 +00001612 ACTC_none,
John McCall2cf031d2011-10-01 01:01:08 +00001613
1614 /// id, void (^)()
John McCallf85e1932011-06-15 23:02:42 +00001615 ACTC_retainable,
John McCall2cf031d2011-10-01 01:01:08 +00001616
1617 /// id*, id***, void (^*)(),
1618 ACTC_indirectRetainable,
1619
1620 /// void* might be a normal C type, or it might a CF type.
1621 ACTC_voidPtr,
1622
1623 /// struct A*
1624 ACTC_coreFoundation
John McCallf85e1932011-06-15 23:02:42 +00001625};
John McCall2cf031d2011-10-01 01:01:08 +00001626static bool isAnyRetainable(ARCConversionTypeClass ACTC) {
1627 return (ACTC == ACTC_retainable ||
1628 ACTC == ACTC_coreFoundation ||
1629 ACTC == ACTC_voidPtr);
1630}
1631static bool isAnyCLike(ARCConversionTypeClass ACTC) {
1632 return ACTC == ACTC_none ||
1633 ACTC == ACTC_voidPtr ||
1634 ACTC == ACTC_coreFoundation;
1635}
1636
John McCallf85e1932011-06-15 23:02:42 +00001637static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) {
John McCall2cf031d2011-10-01 01:01:08 +00001638 bool isIndirect = false;
John McCallf85e1932011-06-15 23:02:42 +00001639
1640 // Ignore an outermost reference type.
John McCall2cf031d2011-10-01 01:01:08 +00001641 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
John McCallf85e1932011-06-15 23:02:42 +00001642 type = ref->getPointeeType();
John McCall2cf031d2011-10-01 01:01:08 +00001643 isIndirect = true;
1644 }
John McCallf85e1932011-06-15 23:02:42 +00001645
1646 // Drill through pointers and arrays recursively.
1647 while (true) {
1648 if (const PointerType *ptr = type->getAs<PointerType>()) {
1649 type = ptr->getPointeeType();
John McCall2cf031d2011-10-01 01:01:08 +00001650
1651 // The first level of pointer may be the innermost pointer on a CF type.
1652 if (!isIndirect) {
1653 if (type->isVoidType()) return ACTC_voidPtr;
1654 if (type->isRecordType()) return ACTC_coreFoundation;
1655 }
John McCallf85e1932011-06-15 23:02:42 +00001656 } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) {
1657 type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
1658 } else {
1659 break;
1660 }
John McCall2cf031d2011-10-01 01:01:08 +00001661 isIndirect = true;
John McCallf85e1932011-06-15 23:02:42 +00001662 }
1663
John McCall2cf031d2011-10-01 01:01:08 +00001664 if (isIndirect) {
1665 if (type->isObjCARCBridgableType())
1666 return ACTC_indirectRetainable;
1667 return ACTC_none;
1668 }
1669
1670 if (type->isObjCARCBridgableType())
1671 return ACTC_retainable;
1672
1673 return ACTC_none;
John McCallf85e1932011-06-15 23:02:42 +00001674}
1675
1676namespace {
John McCall2cf031d2011-10-01 01:01:08 +00001677 /// A result from the cast checker.
1678 enum ACCResult {
1679 /// Cannot be casted.
1680 ACC_invalid,
1681
1682 /// Can be safely retained or not retained.
1683 ACC_bottom,
1684
1685 /// Can be casted at +0.
1686 ACC_plusZero,
1687
1688 /// Can be casted at +1.
1689 ACC_plusOne
1690 };
1691 ACCResult merge(ACCResult left, ACCResult right) {
1692 if (left == right) return left;
1693 if (left == ACC_bottom) return right;
1694 if (right == ACC_bottom) return left;
1695 return ACC_invalid;
1696 }
1697
1698 /// A checker which white-lists certain expressions whose conversion
1699 /// to or from retainable type would otherwise be forbidden in ARC.
1700 class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> {
1701 typedef StmtVisitor<ARCCastChecker, ACCResult> super;
1702
John McCallf85e1932011-06-15 23:02:42 +00001703 ASTContext &Context;
John McCall2cf031d2011-10-01 01:01:08 +00001704 ARCConversionTypeClass SourceClass;
1705 ARCConversionTypeClass TargetClass;
1706
1707 static bool isCFType(QualType type) {
1708 // Someday this can use ns_bridged. For now, it has to do this.
1709 return type->isCARCBridgableType();
John McCallf85e1932011-06-15 23:02:42 +00001710 }
John McCall2cf031d2011-10-01 01:01:08 +00001711
1712 public:
1713 ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source,
1714 ARCConversionTypeClass target)
1715 : Context(Context), SourceClass(source), TargetClass(target) {}
1716
1717 using super::Visit;
1718 ACCResult Visit(Expr *e) {
1719 return super::Visit(e->IgnoreParens());
1720 }
1721
1722 ACCResult VisitStmt(Stmt *s) {
1723 return ACC_invalid;
1724 }
1725
1726 /// Null pointer constants can be casted however you please.
1727 ACCResult VisitExpr(Expr *e) {
1728 if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
1729 return ACC_bottom;
1730 return ACC_invalid;
1731 }
1732
1733 /// Objective-C string literals can be safely casted.
1734 ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) {
1735 // If we're casting to any retainable type, go ahead. Global
1736 // strings are immune to retains, so this is bottom.
1737 if (isAnyRetainable(TargetClass)) return ACC_bottom;
1738
1739 return ACC_invalid;
John McCallf85e1932011-06-15 23:02:42 +00001740 }
1741
John McCall2cf031d2011-10-01 01:01:08 +00001742 /// Look through certain implicit and explicit casts.
1743 ACCResult VisitCastExpr(CastExpr *e) {
John McCallf85e1932011-06-15 23:02:42 +00001744 switch (e->getCastKind()) {
1745 case CK_NullToPointer:
John McCall2cf031d2011-10-01 01:01:08 +00001746 return ACC_bottom;
1747
John McCallf85e1932011-06-15 23:02:42 +00001748 case CK_NoOp:
1749 case CK_LValueToRValue:
1750 case CK_BitCast:
John McCall1d9b3b22011-09-09 05:25:32 +00001751 case CK_CPointerToObjCPointerCast:
1752 case CK_BlockPointerToObjCPointerCast:
John McCallf85e1932011-06-15 23:02:42 +00001753 case CK_AnyPointerToBlockPointerCast:
1754 return Visit(e->getSubExpr());
John McCall2cf031d2011-10-01 01:01:08 +00001755
John McCallf85e1932011-06-15 23:02:42 +00001756 default:
John McCall2cf031d2011-10-01 01:01:08 +00001757 return ACC_invalid;
John McCallf85e1932011-06-15 23:02:42 +00001758 }
1759 }
John McCall2cf031d2011-10-01 01:01:08 +00001760
1761 /// Look through unary extension.
1762 ACCResult VisitUnaryExtension(UnaryOperator *e) {
John McCallf85e1932011-06-15 23:02:42 +00001763 return Visit(e->getSubExpr());
1764 }
John McCall2cf031d2011-10-01 01:01:08 +00001765
1766 /// Ignore the LHS of a comma operator.
1767 ACCResult VisitBinComma(BinaryOperator *e) {
John McCallf85e1932011-06-15 23:02:42 +00001768 return Visit(e->getRHS());
1769 }
John McCall2cf031d2011-10-01 01:01:08 +00001770
1771 /// Conditional operators are okay if both sides are okay.
1772 ACCResult VisitConditionalOperator(ConditionalOperator *e) {
1773 ACCResult left = Visit(e->getTrueExpr());
1774 if (left == ACC_invalid) return ACC_invalid;
1775 return merge(left, Visit(e->getFalseExpr()));
John McCallf85e1932011-06-15 23:02:42 +00001776 }
John McCall2cf031d2011-10-01 01:01:08 +00001777
John McCall4b9c2d22011-11-06 09:01:30 +00001778 /// Look through pseudo-objects.
1779 ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) {
1780 // If we're getting here, we should always have a result.
1781 return Visit(e->getResultExpr());
1782 }
1783
John McCall2cf031d2011-10-01 01:01:08 +00001784 /// Statement expressions are okay if their result expression is okay.
1785 ACCResult VisitStmtExpr(StmtExpr *e) {
John McCallf85e1932011-06-15 23:02:42 +00001786 return Visit(e->getSubStmt()->body_back());
1787 }
John McCallf85e1932011-06-15 23:02:42 +00001788
John McCall2cf031d2011-10-01 01:01:08 +00001789 /// Some declaration references are okay.
1790 ACCResult VisitDeclRefExpr(DeclRefExpr *e) {
1791 // References to global constants from system headers are okay.
1792 // These are things like 'kCFStringTransformToLatin'. They are
1793 // can also be assumed to be immune to retains.
1794 VarDecl *var = dyn_cast<VarDecl>(e->getDecl());
1795 if (isAnyRetainable(TargetClass) &&
1796 isAnyRetainable(SourceClass) &&
1797 var &&
1798 var->getStorageClass() == SC_Extern &&
1799 var->getType().isConstQualified() &&
1800 Context.getSourceManager().isInSystemHeader(var->getLocation())) {
1801 return ACC_bottom;
1802 }
1803
1804 // Nothing else.
1805 return ACC_invalid;
Fariborz Jahanianaf975172011-06-21 17:38:29 +00001806 }
John McCall2cf031d2011-10-01 01:01:08 +00001807
1808 /// Some calls are okay.
1809 ACCResult VisitCallExpr(CallExpr *e) {
1810 if (FunctionDecl *fn = e->getDirectCallee())
1811 if (ACCResult result = checkCallToFunction(fn))
1812 return result;
1813
1814 return super::VisitCallExpr(e);
1815 }
1816
1817 ACCResult checkCallToFunction(FunctionDecl *fn) {
1818 // Require a CF*Ref return type.
1819 if (!isCFType(fn->getResultType()))
1820 return ACC_invalid;
1821
1822 if (!isAnyRetainable(TargetClass))
1823 return ACC_invalid;
1824
1825 // Honor an explicit 'not retained' attribute.
1826 if (fn->hasAttr<CFReturnsNotRetainedAttr>())
1827 return ACC_plusZero;
1828
1829 // Honor an explicit 'retained' attribute, except that for
1830 // now we're not going to permit implicit handling of +1 results,
1831 // because it's a bit frightening.
1832 if (fn->hasAttr<CFReturnsRetainedAttr>())
1833 return ACC_invalid; // ACC_plusOne if we start accepting this
1834
1835 // Recognize this specific builtin function, which is used by CFSTR.
1836 unsigned builtinID = fn->getBuiltinID();
1837 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
1838 return ACC_bottom;
1839
1840 // Otherwise, don't do anything implicit with an unaudited function.
1841 if (!fn->hasAttr<CFAuditedTransferAttr>())
1842 return ACC_invalid;
1843
1844 // Otherwise, it's +0 unless it follows the create convention.
1845 if (ento::coreFoundation::followsCreateRule(fn))
1846 return ACC_invalid; // ACC_plusOne if we start accepting this
1847
1848 return ACC_plusZero;
1849 }
1850
1851 ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) {
1852 return checkCallToMethod(e->getMethodDecl());
1853 }
1854
1855 ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) {
1856 ObjCMethodDecl *method;
1857 if (e->isExplicitProperty())
1858 method = e->getExplicitProperty()->getGetterMethodDecl();
1859 else
1860 method = e->getImplicitPropertyGetter();
1861 return checkCallToMethod(method);
1862 }
1863
1864 ACCResult checkCallToMethod(ObjCMethodDecl *method) {
1865 if (!method) return ACC_invalid;
1866
1867 // Check for message sends to functions returning CF types. We
1868 // just obey the Cocoa conventions with these, even though the
1869 // return type is CF.
1870 if (!isAnyRetainable(TargetClass) || !isCFType(method->getResultType()))
1871 return ACC_invalid;
1872
1873 // If the method is explicitly marked not-retained, it's +0.
1874 if (method->hasAttr<CFReturnsNotRetainedAttr>())
1875 return ACC_plusZero;
1876
1877 // If the method is explicitly marked as returning retained, or its
1878 // selector follows a +1 Cocoa convention, treat it as +1.
1879 if (method->hasAttr<CFReturnsRetainedAttr>())
1880 return ACC_plusOne;
1881
1882 switch (method->getSelector().getMethodFamily()) {
1883 case OMF_alloc:
1884 case OMF_copy:
1885 case OMF_mutableCopy:
1886 case OMF_new:
1887 return ACC_plusOne;
1888
1889 default:
1890 // Otherwise, treat it as +0.
1891 return ACC_plusZero;
Fariborz Jahanianc8505ad2011-06-21 19:42:38 +00001892 }
1893 }
John McCall2cf031d2011-10-01 01:01:08 +00001894 };
Fariborz Jahanian1522a7c2011-06-20 20:54:42 +00001895}
1896
Fariborz Jahanian52b62362012-02-01 22:56:20 +00001897static bool
1898KnownName(Sema &S, const char *name) {
1899 LookupResult R(S, &S.Context.Idents.get(name), SourceLocation(),
1900 Sema::LookupOrdinaryName);
1901 return S.LookupName(R, S.TUScope, false);
1902}
1903
Argyrios Kyrtzidisae1b4af2012-02-16 17:31:07 +00001904static void addFixitForObjCARCConversion(Sema &S,
1905 DiagnosticBuilder &DiagB,
1906 Sema::CheckedConversionKind CCK,
1907 SourceLocation afterLParen,
1908 QualType castType,
1909 Expr *castExpr,
1910 const char *bridgeKeyword,
1911 const char *CFBridgeName) {
1912 // We handle C-style and implicit casts here.
1913 switch (CCK) {
1914 case Sema::CCK_ImplicitConversion:
1915 case Sema::CCK_CStyleCast:
1916 break;
1917 case Sema::CCK_FunctionalCast:
1918 case Sema::CCK_OtherCast:
1919 return;
1920 }
1921
1922 if (CFBridgeName) {
1923 Expr *castedE = castExpr;
1924 if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE))
1925 castedE = CCE->getSubExpr();
1926 castedE = castedE->IgnoreImpCasts();
1927 SourceRange range = castedE->getSourceRange();
1928 if (isa<ParenExpr>(castedE)) {
1929 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1930 CFBridgeName));
1931 } else {
1932 std::string namePlusParen = CFBridgeName;
1933 namePlusParen += "(";
1934 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1935 namePlusParen));
1936 DiagB.AddFixItHint(FixItHint::CreateInsertion(
1937 S.PP.getLocForEndOfToken(range.getEnd()),
1938 ")"));
1939 }
1940 return;
1941 }
1942
1943 if (CCK == Sema::CCK_CStyleCast) {
1944 DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword));
1945 } else {
1946 std::string castCode = "(";
1947 castCode += bridgeKeyword;
1948 castCode += castType.getAsString();
1949 castCode += ")";
1950 Expr *castedE = castExpr->IgnoreImpCasts();
1951 SourceRange range = castedE->getSourceRange();
1952 if (isa<ParenExpr>(castedE)) {
1953 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1954 castCode));
1955 } else {
1956 castCode += "(";
1957 DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
1958 castCode));
1959 DiagB.AddFixItHint(FixItHint::CreateInsertion(
1960 S.PP.getLocForEndOfToken(range.getEnd()),
1961 ")"));
1962 }
1963 }
1964}
1965
John McCall5acb0c92011-10-17 18:40:02 +00001966static void
1967diagnoseObjCARCConversion(Sema &S, SourceRange castRange,
1968 QualType castType, ARCConversionTypeClass castACTC,
1969 Expr *castExpr, ARCConversionTypeClass exprACTC,
1970 Sema::CheckedConversionKind CCK) {
John McCallf85e1932011-06-15 23:02:42 +00001971 SourceLocation loc =
John McCall2cf031d2011-10-01 01:01:08 +00001972 (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc());
John McCallf85e1932011-06-15 23:02:42 +00001973
John McCall5acb0c92011-10-17 18:40:02 +00001974 if (S.makeUnavailableInSystemHeader(loc,
John McCall2cf031d2011-10-01 01:01:08 +00001975 "converts between Objective-C and C pointers in -fobjc-arc"))
John McCallf85e1932011-06-15 23:02:42 +00001976 return;
John McCall5acb0c92011-10-17 18:40:02 +00001977
1978 QualType castExprType = castExpr->getType();
John McCallf85e1932011-06-15 23:02:42 +00001979
John McCall71c482c2011-06-17 06:50:50 +00001980 unsigned srcKind = 0;
John McCallf85e1932011-06-15 23:02:42 +00001981 switch (exprACTC) {
John McCall2cf031d2011-10-01 01:01:08 +00001982 case ACTC_none:
1983 case ACTC_coreFoundation:
1984 case ACTC_voidPtr:
1985 srcKind = (castExprType->isPointerType() ? 1 : 0);
1986 break;
1987 case ACTC_retainable:
1988 srcKind = (castExprType->isBlockPointerType() ? 2 : 3);
1989 break;
1990 case ACTC_indirectRetainable:
1991 srcKind = 4;
1992 break;
John McCallf85e1932011-06-15 23:02:42 +00001993 }
1994
John McCall5acb0c92011-10-17 18:40:02 +00001995 // Check whether this could be fixed with a bridge cast.
1996 SourceLocation afterLParen = S.PP.getLocForEndOfToken(castRange.getBegin());
1997 SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc;
John McCallf85e1932011-06-15 23:02:42 +00001998
John McCall5acb0c92011-10-17 18:40:02 +00001999 // Bridge from an ARC type to a CF type.
2000 if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) {
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002001
John McCall5acb0c92011-10-17 18:40:02 +00002002 S.Diag(loc, diag::err_arc_cast_requires_bridge)
2003 << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit
2004 << 2 // of C pointer type
2005 << castExprType
2006 << unsigned(castType->isBlockPointerType()) // to ObjC|block type
2007 << castType
2008 << castRange
2009 << castExpr->getSourceRange();
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002010 bool br = KnownName(S, "CFBridgingRelease");
Argyrios Kyrtzidisae1b4af2012-02-16 17:31:07 +00002011 {
2012 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge);
2013 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2014 castType, castExpr, "__bridge ", 0);
2015 }
2016 {
2017 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_transfer)
2018 << castExprType << br;
2019 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2020 castType, castExpr, "__bridge_transfer ",
2021 br ? "CFBridgingRelease" : 0);
2022 }
John McCall5acb0c92011-10-17 18:40:02 +00002023
2024 return;
2025 }
2026
2027 // Bridge from a CF type to an ARC type.
2028 if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) {
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002029 bool br = KnownName(S, "CFBridgingRetain");
John McCall5acb0c92011-10-17 18:40:02 +00002030 S.Diag(loc, diag::err_arc_cast_requires_bridge)
2031 << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit
2032 << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type
2033 << castExprType
2034 << 2 // to C pointer type
2035 << castType
2036 << castRange
2037 << castExpr->getSourceRange();
2038
Argyrios Kyrtzidisae1b4af2012-02-16 17:31:07 +00002039 {
2040 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge);
2041 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2042 castType, castExpr, "__bridge ", 0);
2043 }
2044 {
2045 DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_retained)
2046 << castType << br;
2047 addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
2048 castType, castExpr, "__bridge_retained ",
2049 br ? "CFBridgingRetain" : 0);
2050 }
John McCall5acb0c92011-10-17 18:40:02 +00002051
2052 return;
John McCallf85e1932011-06-15 23:02:42 +00002053 }
2054
John McCall5acb0c92011-10-17 18:40:02 +00002055 S.Diag(loc, diag::err_arc_mismatched_cast)
2056 << (CCK != Sema::CCK_ImplicitConversion)
2057 << srcKind << castExprType << castType
John McCallf85e1932011-06-15 23:02:42 +00002058 << castRange << castExpr->getSourceRange();
2059}
2060
John McCall5acb0c92011-10-17 18:40:02 +00002061Sema::ARCConversionResult
2062Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
2063 Expr *&castExpr, CheckedConversionKind CCK) {
2064 QualType castExprType = castExpr->getType();
2065
2066 // For the purposes of the classification, we assume reference types
2067 // will bind to temporaries.
2068 QualType effCastType = castType;
2069 if (const ReferenceType *ref = castType->getAs<ReferenceType>())
2070 effCastType = ref->getPointeeType();
2071
2072 ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType);
2073 ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType);
Fariborz Jahanian6d09f012011-10-28 20:06:07 +00002074 if (exprACTC == castACTC) {
2075 // check for viablity and report error if casting an rvalue to a
2076 // life-time qualifier.
Fariborz Jahanianfc2eff52011-10-29 00:06:10 +00002077 if ((castACTC == ACTC_retainable) &&
Fariborz Jahanian6d09f012011-10-28 20:06:07 +00002078 (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) &&
Fariborz Jahanianfc2eff52011-10-29 00:06:10 +00002079 (castType != castExprType)) {
2080 const Type *DT = castType.getTypePtr();
2081 QualType QDT = castType;
2082 // We desugar some types but not others. We ignore those
2083 // that cannot happen in a cast; i.e. auto, and those which
2084 // should not be de-sugared; i.e typedef.
2085 if (const ParenType *PT = dyn_cast<ParenType>(DT))
2086 QDT = PT->desugar();
2087 else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
2088 QDT = TP->desugar();
2089 else if (const AttributedType *AT = dyn_cast<AttributedType>(DT))
2090 QDT = AT->desugar();
2091 if (QDT != castType &&
2092 QDT.getObjCLifetime() != Qualifiers::OCL_None) {
2093 SourceLocation loc =
2094 (castRange.isValid() ? castRange.getBegin()
2095 : castExpr->getExprLoc());
2096 Diag(loc, diag::err_arc_nolifetime_behavior);
2097 }
Fariborz Jahanian6d09f012011-10-28 20:06:07 +00002098 }
2099 return ACR_okay;
2100 }
2101
John McCall5acb0c92011-10-17 18:40:02 +00002102 if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay;
2103
2104 // Allow all of these types to be cast to integer types (but not
2105 // vice-versa).
2106 if (castACTC == ACTC_none && castType->isIntegralType(Context))
2107 return ACR_okay;
2108
2109 // Allow casts between pointers to lifetime types (e.g., __strong id*)
2110 // and pointers to void (e.g., cv void *). Casting from void* to lifetime*
2111 // must be explicit.
2112 if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr)
2113 return ACR_okay;
2114 if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr &&
2115 CCK != CCK_ImplicitConversion)
2116 return ACR_okay;
2117
2118 switch (ARCCastChecker(Context, exprACTC, castACTC).Visit(castExpr)) {
2119 // For invalid casts, fall through.
2120 case ACC_invalid:
2121 break;
2122
2123 // Do nothing for both bottom and +0.
2124 case ACC_bottom:
2125 case ACC_plusZero:
2126 return ACR_okay;
2127
2128 // If the result is +1, consume it here.
2129 case ACC_plusOne:
2130 castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(),
2131 CK_ARCConsumeObject, castExpr,
2132 0, VK_RValue);
2133 ExprNeedsCleanups = true;
2134 return ACR_okay;
2135 }
2136
2137 // If this is a non-implicit cast from id or block type to a
2138 // CoreFoundation type, delay complaining in case the cast is used
2139 // in an acceptable context.
2140 if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) &&
2141 CCK != CCK_ImplicitConversion)
2142 return ACR_unbridged;
2143
2144 diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
2145 castExpr, exprACTC, CCK);
2146 return ACR_okay;
2147}
2148
2149/// Given that we saw an expression with the ARCUnbridgedCastTy
2150/// placeholder type, complain bitterly.
2151void Sema::diagnoseARCUnbridgedCast(Expr *e) {
2152 // We expect the spurious ImplicitCastExpr to already have been stripped.
2153 assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
2154 CastExpr *realCast = cast<CastExpr>(e->IgnoreParens());
2155
2156 SourceRange castRange;
2157 QualType castType;
2158 CheckedConversionKind CCK;
2159
2160 if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) {
2161 castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc());
2162 castType = cast->getTypeAsWritten();
2163 CCK = CCK_CStyleCast;
2164 } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) {
2165 castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
2166 castType = cast->getTypeAsWritten();
2167 CCK = CCK_OtherCast;
2168 } else {
2169 castType = cast->getType();
2170 CCK = CCK_ImplicitConversion;
2171 }
2172
2173 ARCConversionTypeClass castACTC =
2174 classifyTypeForARCConversion(castType.getNonReferenceType());
2175
2176 Expr *castExpr = realCast->getSubExpr();
2177 assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable);
2178
2179 diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
2180 castExpr, ACTC_retainable, CCK);
2181}
2182
2183/// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast
2184/// type, remove the placeholder cast.
2185Expr *Sema::stripARCUnbridgedCast(Expr *e) {
2186 assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
2187
2188 if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
2189 Expr *sub = stripARCUnbridgedCast(pe->getSubExpr());
2190 return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub);
2191 } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
2192 assert(uo->getOpcode() == UO_Extension);
2193 Expr *sub = stripARCUnbridgedCast(uo->getSubExpr());
2194 return new (Context) UnaryOperator(sub, UO_Extension, sub->getType(),
2195 sub->getValueKind(), sub->getObjectKind(),
2196 uo->getOperatorLoc());
2197 } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
2198 assert(!gse->isResultDependent());
2199
2200 unsigned n = gse->getNumAssocs();
2201 SmallVector<Expr*, 4> subExprs(n);
2202 SmallVector<TypeSourceInfo*, 4> subTypes(n);
2203 for (unsigned i = 0; i != n; ++i) {
2204 subTypes[i] = gse->getAssocTypeSourceInfo(i);
2205 Expr *sub = gse->getAssocExpr(i);
2206 if (i == gse->getResultIndex())
2207 sub = stripARCUnbridgedCast(sub);
2208 subExprs[i] = sub;
2209 }
2210
2211 return new (Context) GenericSelectionExpr(Context, gse->getGenericLoc(),
2212 gse->getControllingExpr(),
2213 subTypes.data(), subExprs.data(),
2214 n, gse->getDefaultLoc(),
2215 gse->getRParenLoc(),
2216 gse->containsUnexpandedParameterPack(),
2217 gse->getResultIndex());
2218 } else {
2219 assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!");
2220 return cast<ImplicitCastExpr>(e)->getSubExpr();
2221 }
2222}
2223
Fariborz Jahanian7a084ec2011-07-07 23:04:17 +00002224bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType,
2225 QualType exprType) {
2226 QualType canCastType =
2227 Context.getCanonicalType(castType).getUnqualifiedType();
2228 QualType canExprType =
2229 Context.getCanonicalType(exprType).getUnqualifiedType();
2230 if (isa<ObjCObjectPointerType>(canCastType) &&
2231 castType.getObjCLifetime() == Qualifiers::OCL_Weak &&
2232 canExprType->isObjCObjectPointerType()) {
2233 if (const ObjCObjectPointerType *ObjT =
2234 canExprType->getAs<ObjCObjectPointerType>())
2235 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable())
2236 return false;
2237 }
2238 return true;
2239}
2240
John McCall7e5e5f42011-07-07 06:58:02 +00002241/// Look for an ObjCReclaimReturnedObject cast and destroy it.
2242static Expr *maybeUndoReclaimObject(Expr *e) {
2243 // For now, we just undo operands that are *immediately* reclaim
2244 // expressions, which prevents the vast majority of potential
2245 // problems here. To catch them all, we'd need to rebuild arbitrary
2246 // value-propagating subexpressions --- we can't reliably rebuild
2247 // in-place because of expression sharing.
2248 if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
John McCall33e56f32011-09-10 06:18:15 +00002249 if (ice->getCastKind() == CK_ARCReclaimReturnedObject)
John McCall7e5e5f42011-07-07 06:58:02 +00002250 return ice->getSubExpr();
2251
2252 return e;
2253}
2254
John McCallf85e1932011-06-15 23:02:42 +00002255ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc,
2256 ObjCBridgeCastKind Kind,
2257 SourceLocation BridgeKeywordLoc,
2258 TypeSourceInfo *TSInfo,
2259 Expr *SubExpr) {
John McCall4906cf92011-08-26 00:48:42 +00002260 ExprResult SubResult = UsualUnaryConversions(SubExpr);
2261 if (SubResult.isInvalid()) return ExprError();
2262 SubExpr = SubResult.take();
2263
John McCallf85e1932011-06-15 23:02:42 +00002264 QualType T = TSInfo->getType();
2265 QualType FromType = SubExpr->getType();
2266
John McCall1d9b3b22011-09-09 05:25:32 +00002267 CastKind CK;
2268
John McCallf85e1932011-06-15 23:02:42 +00002269 bool MustConsume = false;
2270 if (T->isDependentType() || SubExpr->isTypeDependent()) {
2271 // Okay: we'll build a dependent expression type.
John McCall1d9b3b22011-09-09 05:25:32 +00002272 CK = CK_Dependent;
John McCallf85e1932011-06-15 23:02:42 +00002273 } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) {
2274 // Casting CF -> id
John McCall1d9b3b22011-09-09 05:25:32 +00002275 CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast
2276 : CK_CPointerToObjCPointerCast);
John McCallf85e1932011-06-15 23:02:42 +00002277 switch (Kind) {
2278 case OBC_Bridge:
2279 break;
2280
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002281 case OBC_BridgeRetained: {
2282 bool br = KnownName(*this, "CFBridgingRelease");
John McCallf85e1932011-06-15 23:02:42 +00002283 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
2284 << 2
2285 << FromType
2286 << (T->isBlockPointerType()? 1 : 0)
2287 << T
2288 << SubExpr->getSourceRange()
2289 << Kind;
2290 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
2291 << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge");
2292 Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002293 << FromType << br
John McCallf85e1932011-06-15 23:02:42 +00002294 << FixItHint::CreateReplacement(BridgeKeywordLoc,
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002295 br ? "CFBridgingRelease "
2296 : "__bridge_transfer ");
John McCallf85e1932011-06-15 23:02:42 +00002297
2298 Kind = OBC_Bridge;
2299 break;
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002300 }
John McCallf85e1932011-06-15 23:02:42 +00002301
2302 case OBC_BridgeTransfer:
2303 // We must consume the Objective-C object produced by the cast.
2304 MustConsume = true;
2305 break;
2306 }
2307 } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) {
2308 // Okay: id -> CF
John McCall1d9b3b22011-09-09 05:25:32 +00002309 CK = CK_BitCast;
John McCallf85e1932011-06-15 23:02:42 +00002310 switch (Kind) {
2311 case OBC_Bridge:
John McCall7e5e5f42011-07-07 06:58:02 +00002312 // Reclaiming a value that's going to be __bridge-casted to CF
2313 // is very dangerous, so we don't do it.
2314 SubExpr = maybeUndoReclaimObject(SubExpr);
John McCallf85e1932011-06-15 23:02:42 +00002315 break;
2316
2317 case OBC_BridgeRetained:
2318 // Produce the object before casting it.
2319 SubExpr = ImplicitCastExpr::Create(Context, FromType,
John McCall33e56f32011-09-10 06:18:15 +00002320 CK_ARCProduceObject,
John McCallf85e1932011-06-15 23:02:42 +00002321 SubExpr, 0, VK_RValue);
2322 break;
2323
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002324 case OBC_BridgeTransfer: {
2325 bool br = KnownName(*this, "CFBridgingRetain");
John McCallf85e1932011-06-15 23:02:42 +00002326 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
2327 << (FromType->isBlockPointerType()? 1 : 0)
2328 << FromType
2329 << 2
2330 << T
2331 << SubExpr->getSourceRange()
2332 << Kind;
2333
2334 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
2335 << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge ");
2336 Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002337 << T << br
2338 << FixItHint::CreateReplacement(BridgeKeywordLoc,
2339 br ? "CFBridgingRetain " : "__bridge_retained");
John McCallf85e1932011-06-15 23:02:42 +00002340
2341 Kind = OBC_Bridge;
2342 break;
2343 }
Fariborz Jahanian52b62362012-02-01 22:56:20 +00002344 }
John McCallf85e1932011-06-15 23:02:42 +00002345 } else {
2346 Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
2347 << FromType << T << Kind
2348 << SubExpr->getSourceRange()
2349 << TSInfo->getTypeLoc().getSourceRange();
2350 return ExprError();
2351 }
2352
John McCall1d9b3b22011-09-09 05:25:32 +00002353 Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK,
John McCallf85e1932011-06-15 23:02:42 +00002354 BridgeKeywordLoc,
2355 TSInfo, SubExpr);
2356
2357 if (MustConsume) {
2358 ExprNeedsCleanups = true;
John McCall33e56f32011-09-10 06:18:15 +00002359 Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result,
John McCallf85e1932011-06-15 23:02:42 +00002360 0, VK_RValue);
2361 }
2362
2363 return Result;
2364}
2365
2366ExprResult Sema::ActOnObjCBridgedCast(Scope *S,
2367 SourceLocation LParenLoc,
2368 ObjCBridgeCastKind Kind,
2369 SourceLocation BridgeKeywordLoc,
2370 ParsedType Type,
2371 SourceLocation RParenLoc,
2372 Expr *SubExpr) {
2373 TypeSourceInfo *TSInfo = 0;
2374 QualType T = GetTypeFromParser(Type, &TSInfo);
2375 if (!TSInfo)
2376 TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc);
2377 return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo,
2378 SubExpr);
2379}