1f4a2713aSLionel Sambuc //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements semantic analysis for Objective-C expressions.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
18f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
19f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
20f4a2713aSLionel Sambuc #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
21f4a2713aSLionel Sambuc #include "clang/Edit/Commit.h"
22f4a2713aSLionel Sambuc #include "clang/Edit/Rewriters.h"
23f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
26f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
27f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
28f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc using namespace clang;
31f4a2713aSLionel Sambuc using namespace sema;
32f4a2713aSLionel Sambuc using llvm::makeArrayRef;
33f4a2713aSLionel Sambuc
ParseObjCStringLiteral(SourceLocation * AtLocs,Expr ** strings,unsigned NumStrings)34f4a2713aSLionel Sambuc ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
35f4a2713aSLionel Sambuc Expr **strings,
36f4a2713aSLionel Sambuc unsigned NumStrings) {
37f4a2713aSLionel Sambuc StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc // Most ObjC strings are formed out of a single piece. However, we *can*
40f4a2713aSLionel Sambuc // have strings formed out of multiple @ strings with multiple pptokens in
41f4a2713aSLionel Sambuc // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
42f4a2713aSLionel Sambuc // StringLiteral for ObjCStringLiteral to hold onto.
43f4a2713aSLionel Sambuc StringLiteral *S = Strings[0];
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc // If we have a multi-part string, merge it all together.
46f4a2713aSLionel Sambuc if (NumStrings != 1) {
47f4a2713aSLionel Sambuc // Concatenate objc strings.
48f4a2713aSLionel Sambuc SmallString<128> StrBuf;
49f4a2713aSLionel Sambuc SmallVector<SourceLocation, 8> StrLocs;
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumStrings; ++i) {
52f4a2713aSLionel Sambuc S = Strings[i];
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc // ObjC strings can't be wide or UTF.
55f4a2713aSLionel Sambuc if (!S->isAscii()) {
56f4a2713aSLionel Sambuc Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
57f4a2713aSLionel Sambuc << S->getSourceRange();
58f4a2713aSLionel Sambuc return true;
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc
61f4a2713aSLionel Sambuc // Append the string.
62f4a2713aSLionel Sambuc StrBuf += S->getString();
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc // Get the locations of the string tokens.
65f4a2713aSLionel Sambuc StrLocs.append(S->tokloc_begin(), S->tokloc_end());
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc // Create the aggregate string with the appropriate content and location
69f4a2713aSLionel Sambuc // information.
70*0a6a1f1dSLionel Sambuc const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType());
71*0a6a1f1dSLionel Sambuc assert(CAT && "String literal not of constant array type!");
72*0a6a1f1dSLionel Sambuc QualType StrTy = Context.getConstantArrayType(
73*0a6a1f1dSLionel Sambuc CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1),
74*0a6a1f1dSLionel Sambuc CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
75*0a6a1f1dSLionel Sambuc S = StringLiteral::Create(Context, StrBuf, StringLiteral::Ascii,
76*0a6a1f1dSLionel Sambuc /*Pascal=*/false, StrTy, &StrLocs[0],
77*0a6a1f1dSLionel Sambuc StrLocs.size());
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc return BuildObjCStringLiteral(AtLocs[0], S);
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc
BuildObjCStringLiteral(SourceLocation AtLoc,StringLiteral * S)83f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCStringLiteral(SourceLocation AtLoc, StringLiteral *S){
84f4a2713aSLionel Sambuc // Verify that this composite string is acceptable for ObjC strings.
85f4a2713aSLionel Sambuc if (CheckObjCString(S))
86f4a2713aSLionel Sambuc return true;
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc // Initialize the constant string interface lazily. This assumes
89f4a2713aSLionel Sambuc // the NSString interface is seen in this translation unit. Note: We
90f4a2713aSLionel Sambuc // don't use NSConstantString, since the runtime team considers this
91f4a2713aSLionel Sambuc // interface private (even though it appears in the header files).
92f4a2713aSLionel Sambuc QualType Ty = Context.getObjCConstantStringInterface();
93f4a2713aSLionel Sambuc if (!Ty.isNull()) {
94f4a2713aSLionel Sambuc Ty = Context.getObjCObjectPointerType(Ty);
95f4a2713aSLionel Sambuc } else if (getLangOpts().NoConstantCFStrings) {
96*0a6a1f1dSLionel Sambuc IdentifierInfo *NSIdent=nullptr;
97f4a2713aSLionel Sambuc std::string StringClass(getLangOpts().ObjCConstantStringClass);
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc if (StringClass.empty())
100f4a2713aSLionel Sambuc NSIdent = &Context.Idents.get("NSConstantString");
101f4a2713aSLionel Sambuc else
102f4a2713aSLionel Sambuc NSIdent = &Context.Idents.get(StringClass);
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc,
105f4a2713aSLionel Sambuc LookupOrdinaryName);
106f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
107f4a2713aSLionel Sambuc Context.setObjCConstantStringInterface(StrIF);
108f4a2713aSLionel Sambuc Ty = Context.getObjCConstantStringInterface();
109f4a2713aSLionel Sambuc Ty = Context.getObjCObjectPointerType(Ty);
110f4a2713aSLionel Sambuc } else {
111f4a2713aSLionel Sambuc // If there is no NSConstantString interface defined then treat this
112f4a2713aSLionel Sambuc // as error and recover from it.
113f4a2713aSLionel Sambuc Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent
114f4a2713aSLionel Sambuc << S->getSourceRange();
115f4a2713aSLionel Sambuc Ty = Context.getObjCIdType();
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc } else {
118f4a2713aSLionel Sambuc IdentifierInfo *NSIdent = NSAPIObj->getNSClassId(NSAPI::ClassId_NSString);
119f4a2713aSLionel Sambuc NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc,
120f4a2713aSLionel Sambuc LookupOrdinaryName);
121f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
122f4a2713aSLionel Sambuc Context.setObjCConstantStringInterface(StrIF);
123f4a2713aSLionel Sambuc Ty = Context.getObjCConstantStringInterface();
124f4a2713aSLionel Sambuc Ty = Context.getObjCObjectPointerType(Ty);
125f4a2713aSLionel Sambuc } else {
126f4a2713aSLionel Sambuc // If there is no NSString interface defined, implicitly declare
127f4a2713aSLionel Sambuc // a @class NSString; and use that instead. This is to make sure
128f4a2713aSLionel Sambuc // type of an NSString literal is represented correctly, instead of
129f4a2713aSLionel Sambuc // being an 'id' type.
130f4a2713aSLionel Sambuc Ty = Context.getObjCNSStringType();
131f4a2713aSLionel Sambuc if (Ty.isNull()) {
132f4a2713aSLionel Sambuc ObjCInterfaceDecl *NSStringIDecl =
133f4a2713aSLionel Sambuc ObjCInterfaceDecl::Create (Context,
134f4a2713aSLionel Sambuc Context.getTranslationUnitDecl(),
135f4a2713aSLionel Sambuc SourceLocation(), NSIdent,
136*0a6a1f1dSLionel Sambuc nullptr, SourceLocation());
137f4a2713aSLionel Sambuc Ty = Context.getObjCInterfaceType(NSStringIDecl);
138f4a2713aSLionel Sambuc Context.setObjCNSStringType(Ty);
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc Ty = Context.getObjCObjectPointerType(Ty);
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc
144f4a2713aSLionel Sambuc return new (Context) ObjCStringLiteral(S, Ty, AtLoc);
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc /// \brief Emits an error if the given method does not exist, or if the return
148f4a2713aSLionel Sambuc /// type is not an Objective-C object.
validateBoxingMethod(Sema & S,SourceLocation Loc,const ObjCInterfaceDecl * Class,Selector Sel,const ObjCMethodDecl * Method)149f4a2713aSLionel Sambuc static bool validateBoxingMethod(Sema &S, SourceLocation Loc,
150f4a2713aSLionel Sambuc const ObjCInterfaceDecl *Class,
151f4a2713aSLionel Sambuc Selector Sel, const ObjCMethodDecl *Method) {
152f4a2713aSLionel Sambuc if (!Method) {
153f4a2713aSLionel Sambuc // FIXME: Is there a better way to avoid quotes than using getName()?
154f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_undeclared_boxing_method) << Sel << Class->getName();
155f4a2713aSLionel Sambuc return false;
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc // Make sure the return type is reasonable.
159*0a6a1f1dSLionel Sambuc QualType ReturnType = Method->getReturnType();
160f4a2713aSLionel Sambuc if (!ReturnType->isObjCObjectPointerType()) {
161f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_objc_literal_method_sig)
162f4a2713aSLionel Sambuc << Sel;
163f4a2713aSLionel Sambuc S.Diag(Method->getLocation(), diag::note_objc_literal_method_return)
164f4a2713aSLionel Sambuc << ReturnType;
165f4a2713aSLionel Sambuc return false;
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc return true;
169f4a2713aSLionel Sambuc }
170f4a2713aSLionel Sambuc
171f4a2713aSLionel Sambuc /// \brief Retrieve the NSNumber factory method that should be used to create
172f4a2713aSLionel Sambuc /// an Objective-C literal for the given type.
getNSNumberFactoryMethod(Sema & S,SourceLocation Loc,QualType NumberType,bool isLiteral=false,SourceRange R=SourceRange ())173f4a2713aSLionel Sambuc static ObjCMethodDecl *getNSNumberFactoryMethod(Sema &S, SourceLocation Loc,
174f4a2713aSLionel Sambuc QualType NumberType,
175f4a2713aSLionel Sambuc bool isLiteral = false,
176f4a2713aSLionel Sambuc SourceRange R = SourceRange()) {
177f4a2713aSLionel Sambuc Optional<NSAPI::NSNumberLiteralMethodKind> Kind =
178f4a2713aSLionel Sambuc S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType);
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc if (!Kind) {
181f4a2713aSLionel Sambuc if (isLiteral) {
182f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_invalid_nsnumber_type)
183f4a2713aSLionel Sambuc << NumberType << R;
184f4a2713aSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc return nullptr;
186f4a2713aSLionel Sambuc }
187f4a2713aSLionel Sambuc
188f4a2713aSLionel Sambuc // If we already looked up this method, we're done.
189f4a2713aSLionel Sambuc if (S.NSNumberLiteralMethods[*Kind])
190f4a2713aSLionel Sambuc return S.NSNumberLiteralMethods[*Kind];
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind,
193f4a2713aSLionel Sambuc /*Instance=*/false);
194f4a2713aSLionel Sambuc
195f4a2713aSLionel Sambuc ASTContext &CX = S.Context;
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc // Look up the NSNumber class, if we haven't done so already. It's cached
198f4a2713aSLionel Sambuc // in the Sema instance.
199f4a2713aSLionel Sambuc if (!S.NSNumberDecl) {
200f4a2713aSLionel Sambuc IdentifierInfo *NSNumberId =
201f4a2713aSLionel Sambuc S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSNumber);
202f4a2713aSLionel Sambuc NamedDecl *IF = S.LookupSingleName(S.TUScope, NSNumberId,
203f4a2713aSLionel Sambuc Loc, Sema::LookupOrdinaryName);
204f4a2713aSLionel Sambuc S.NSNumberDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
205f4a2713aSLionel Sambuc if (!S.NSNumberDecl) {
206f4a2713aSLionel Sambuc if (S.getLangOpts().DebuggerObjCLiteral) {
207f4a2713aSLionel Sambuc // Create a stub definition of NSNumber.
208f4a2713aSLionel Sambuc S.NSNumberDecl = ObjCInterfaceDecl::Create(CX,
209f4a2713aSLionel Sambuc CX.getTranslationUnitDecl(),
210f4a2713aSLionel Sambuc SourceLocation(), NSNumberId,
211*0a6a1f1dSLionel Sambuc nullptr, SourceLocation());
212f4a2713aSLionel Sambuc } else {
213f4a2713aSLionel Sambuc // Otherwise, require a declaration of NSNumber.
214f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_undeclared_nsnumber);
215*0a6a1f1dSLionel Sambuc return nullptr;
216f4a2713aSLionel Sambuc }
217f4a2713aSLionel Sambuc } else if (!S.NSNumberDecl->hasDefinition()) {
218f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_undeclared_nsnumber);
219*0a6a1f1dSLionel Sambuc return nullptr;
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc // generate the pointer to NSNumber type.
223f4a2713aSLionel Sambuc QualType NSNumberObject = CX.getObjCInterfaceType(S.NSNumberDecl);
224f4a2713aSLionel Sambuc S.NSNumberPointer = CX.getObjCObjectPointerType(NSNumberObject);
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc // Look for the appropriate method within NSNumber.
228f4a2713aSLionel Sambuc ObjCMethodDecl *Method = S.NSNumberDecl->lookupClassMethod(Sel);
229f4a2713aSLionel Sambuc if (!Method && S.getLangOpts().DebuggerObjCLiteral) {
230f4a2713aSLionel Sambuc // create a stub definition this NSNumber factory method.
231*0a6a1f1dSLionel Sambuc TypeSourceInfo *ReturnTInfo = nullptr;
232*0a6a1f1dSLionel Sambuc Method =
233*0a6a1f1dSLionel Sambuc ObjCMethodDecl::Create(CX, SourceLocation(), SourceLocation(), Sel,
234*0a6a1f1dSLionel Sambuc S.NSNumberPointer, ReturnTInfo, S.NSNumberDecl,
235f4a2713aSLionel Sambuc /*isInstance=*/false, /*isVariadic=*/false,
236f4a2713aSLionel Sambuc /*isPropertyAccessor=*/false,
237f4a2713aSLionel Sambuc /*isImplicitlyDeclared=*/true,
238*0a6a1f1dSLionel Sambuc /*isDefined=*/false, ObjCMethodDecl::Required,
239f4a2713aSLionel Sambuc /*HasRelatedResultType=*/false);
240f4a2713aSLionel Sambuc ParmVarDecl *value = ParmVarDecl::Create(S.Context, Method,
241f4a2713aSLionel Sambuc SourceLocation(), SourceLocation(),
242f4a2713aSLionel Sambuc &CX.Idents.get("value"),
243*0a6a1f1dSLionel Sambuc NumberType, /*TInfo=*/nullptr,
244*0a6a1f1dSLionel Sambuc SC_None, nullptr);
245f4a2713aSLionel Sambuc Method->setMethodParams(S.Context, value, None);
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc
248f4a2713aSLionel Sambuc if (!validateBoxingMethod(S, Loc, S.NSNumberDecl, Sel, Method))
249*0a6a1f1dSLionel Sambuc return nullptr;
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc // Note: if the parameter type is out-of-line, we'll catch it later in the
252f4a2713aSLionel Sambuc // implicit conversion.
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc S.NSNumberLiteralMethods[*Kind] = Method;
255f4a2713aSLionel Sambuc return Method;
256f4a2713aSLionel Sambuc }
257f4a2713aSLionel Sambuc
258f4a2713aSLionel Sambuc /// BuildObjCNumericLiteral - builds an ObjCBoxedExpr AST node for the
259f4a2713aSLionel Sambuc /// numeric literal expression. Type of the expression will be "NSNumber *".
BuildObjCNumericLiteral(SourceLocation AtLoc,Expr * Number)260f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCNumericLiteral(SourceLocation AtLoc, Expr *Number) {
261f4a2713aSLionel Sambuc // Determine the type of the literal.
262f4a2713aSLionel Sambuc QualType NumberType = Number->getType();
263f4a2713aSLionel Sambuc if (CharacterLiteral *Char = dyn_cast<CharacterLiteral>(Number)) {
264f4a2713aSLionel Sambuc // In C, character literals have type 'int'. That's not the type we want
265f4a2713aSLionel Sambuc // to use to determine the Objective-c literal kind.
266f4a2713aSLionel Sambuc switch (Char->getKind()) {
267f4a2713aSLionel Sambuc case CharacterLiteral::Ascii:
268f4a2713aSLionel Sambuc NumberType = Context.CharTy;
269f4a2713aSLionel Sambuc break;
270f4a2713aSLionel Sambuc
271f4a2713aSLionel Sambuc case CharacterLiteral::Wide:
272f4a2713aSLionel Sambuc NumberType = Context.getWideCharType();
273f4a2713aSLionel Sambuc break;
274f4a2713aSLionel Sambuc
275f4a2713aSLionel Sambuc case CharacterLiteral::UTF16:
276f4a2713aSLionel Sambuc NumberType = Context.Char16Ty;
277f4a2713aSLionel Sambuc break;
278f4a2713aSLionel Sambuc
279f4a2713aSLionel Sambuc case CharacterLiteral::UTF32:
280f4a2713aSLionel Sambuc NumberType = Context.Char32Ty;
281f4a2713aSLionel Sambuc break;
282f4a2713aSLionel Sambuc }
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc
285f4a2713aSLionel Sambuc // Look for the appropriate method within NSNumber.
286f4a2713aSLionel Sambuc // Construct the literal.
287f4a2713aSLionel Sambuc SourceRange NR(Number->getSourceRange());
288f4a2713aSLionel Sambuc ObjCMethodDecl *Method = getNSNumberFactoryMethod(*this, AtLoc, NumberType,
289f4a2713aSLionel Sambuc true, NR);
290f4a2713aSLionel Sambuc if (!Method)
291f4a2713aSLionel Sambuc return ExprError();
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc // Convert the number to the type that the parameter expects.
294*0a6a1f1dSLionel Sambuc ParmVarDecl *ParamDecl = Method->parameters()[0];
295f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
296f4a2713aSLionel Sambuc ParamDecl);
297f4a2713aSLionel Sambuc ExprResult ConvertedNumber = PerformCopyInitialization(Entity,
298f4a2713aSLionel Sambuc SourceLocation(),
299*0a6a1f1dSLionel Sambuc Number);
300f4a2713aSLionel Sambuc if (ConvertedNumber.isInvalid())
301f4a2713aSLionel Sambuc return ExprError();
302f4a2713aSLionel Sambuc Number = ConvertedNumber.get();
303f4a2713aSLionel Sambuc
304f4a2713aSLionel Sambuc // Use the effective source range of the literal, including the leading '@'.
305f4a2713aSLionel Sambuc return MaybeBindToTemporary(
306f4a2713aSLionel Sambuc new (Context) ObjCBoxedExpr(Number, NSNumberPointer, Method,
307f4a2713aSLionel Sambuc SourceRange(AtLoc, NR.getEnd())));
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc
ActOnObjCBoolLiteral(SourceLocation AtLoc,SourceLocation ValueLoc,bool Value)310f4a2713aSLionel Sambuc ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation AtLoc,
311f4a2713aSLionel Sambuc SourceLocation ValueLoc,
312f4a2713aSLionel Sambuc bool Value) {
313f4a2713aSLionel Sambuc ExprResult Inner;
314f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus) {
315f4a2713aSLionel Sambuc Inner = ActOnCXXBoolLiteral(ValueLoc, Value? tok::kw_true : tok::kw_false);
316f4a2713aSLionel Sambuc } else {
317f4a2713aSLionel Sambuc // C doesn't actually have a way to represent literal values of type
318f4a2713aSLionel Sambuc // _Bool. So, we'll use 0/1 and implicit cast to _Bool.
319f4a2713aSLionel Sambuc Inner = ActOnIntegerConstant(ValueLoc, Value? 1 : 0);
320f4a2713aSLionel Sambuc Inner = ImpCastExprToType(Inner.get(), Context.BoolTy,
321f4a2713aSLionel Sambuc CK_IntegralToBoolean);
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc return BuildObjCNumericLiteral(AtLoc, Inner.get());
325f4a2713aSLionel Sambuc }
326f4a2713aSLionel Sambuc
327f4a2713aSLionel Sambuc /// \brief Check that the given expression is a valid element of an Objective-C
328f4a2713aSLionel Sambuc /// collection literal.
CheckObjCCollectionLiteralElement(Sema & S,Expr * Element,QualType T,bool ArrayLiteral=false)329f4a2713aSLionel Sambuc static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element,
330f4a2713aSLionel Sambuc QualType T,
331f4a2713aSLionel Sambuc bool ArrayLiteral = false) {
332f4a2713aSLionel Sambuc // If the expression is type-dependent, there's nothing for us to do.
333f4a2713aSLionel Sambuc if (Element->isTypeDependent())
334f4a2713aSLionel Sambuc return Element;
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc ExprResult Result = S.CheckPlaceholderExpr(Element);
337f4a2713aSLionel Sambuc if (Result.isInvalid())
338f4a2713aSLionel Sambuc return ExprError();
339f4a2713aSLionel Sambuc Element = Result.get();
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc // In C++, check for an implicit conversion to an Objective-C object pointer
342f4a2713aSLionel Sambuc // type.
343f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus && Element->getType()->isRecordType()) {
344f4a2713aSLionel Sambuc InitializedEntity Entity
345f4a2713aSLionel Sambuc = InitializedEntity::InitializeParameter(S.Context, T,
346f4a2713aSLionel Sambuc /*Consumed=*/false);
347f4a2713aSLionel Sambuc InitializationKind Kind
348f4a2713aSLionel Sambuc = InitializationKind::CreateCopy(Element->getLocStart(),
349f4a2713aSLionel Sambuc SourceLocation());
350f4a2713aSLionel Sambuc InitializationSequence Seq(S, Entity, Kind, Element);
351f4a2713aSLionel Sambuc if (!Seq.Failed())
352f4a2713aSLionel Sambuc return Seq.Perform(S, Entity, Kind, Element);
353f4a2713aSLionel Sambuc }
354f4a2713aSLionel Sambuc
355f4a2713aSLionel Sambuc Expr *OrigElement = Element;
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc // Perform lvalue-to-rvalue conversion.
358f4a2713aSLionel Sambuc Result = S.DefaultLvalueConversion(Element);
359f4a2713aSLionel Sambuc if (Result.isInvalid())
360f4a2713aSLionel Sambuc return ExprError();
361f4a2713aSLionel Sambuc Element = Result.get();
362f4a2713aSLionel Sambuc
363f4a2713aSLionel Sambuc // Make sure that we have an Objective-C pointer type or block.
364f4a2713aSLionel Sambuc if (!Element->getType()->isObjCObjectPointerType() &&
365f4a2713aSLionel Sambuc !Element->getType()->isBlockPointerType()) {
366f4a2713aSLionel Sambuc bool Recovered = false;
367f4a2713aSLionel Sambuc
368f4a2713aSLionel Sambuc // If this is potentially an Objective-C numeric literal, add the '@'.
369f4a2713aSLionel Sambuc if (isa<IntegerLiteral>(OrigElement) ||
370f4a2713aSLionel Sambuc isa<CharacterLiteral>(OrigElement) ||
371f4a2713aSLionel Sambuc isa<FloatingLiteral>(OrigElement) ||
372f4a2713aSLionel Sambuc isa<ObjCBoolLiteralExpr>(OrigElement) ||
373f4a2713aSLionel Sambuc isa<CXXBoolLiteralExpr>(OrigElement)) {
374f4a2713aSLionel Sambuc if (S.NSAPIObj->getNSNumberFactoryMethodKind(OrigElement->getType())) {
375f4a2713aSLionel Sambuc int Which = isa<CharacterLiteral>(OrigElement) ? 1
376f4a2713aSLionel Sambuc : (isa<CXXBoolLiteralExpr>(OrigElement) ||
377f4a2713aSLionel Sambuc isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2
378f4a2713aSLionel Sambuc : 3;
379f4a2713aSLionel Sambuc
380f4a2713aSLionel Sambuc S.Diag(OrigElement->getLocStart(), diag::err_box_literal_collection)
381f4a2713aSLionel Sambuc << Which << OrigElement->getSourceRange()
382f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(OrigElement->getLocStart(), "@");
383f4a2713aSLionel Sambuc
384f4a2713aSLionel Sambuc Result = S.BuildObjCNumericLiteral(OrigElement->getLocStart(),
385f4a2713aSLionel Sambuc OrigElement);
386f4a2713aSLionel Sambuc if (Result.isInvalid())
387f4a2713aSLionel Sambuc return ExprError();
388f4a2713aSLionel Sambuc
389f4a2713aSLionel Sambuc Element = Result.get();
390f4a2713aSLionel Sambuc Recovered = true;
391f4a2713aSLionel Sambuc }
392f4a2713aSLionel Sambuc }
393f4a2713aSLionel Sambuc // If this is potentially an Objective-C string literal, add the '@'.
394f4a2713aSLionel Sambuc else if (StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) {
395f4a2713aSLionel Sambuc if (String->isAscii()) {
396f4a2713aSLionel Sambuc S.Diag(OrigElement->getLocStart(), diag::err_box_literal_collection)
397f4a2713aSLionel Sambuc << 0 << OrigElement->getSourceRange()
398f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(OrigElement->getLocStart(), "@");
399f4a2713aSLionel Sambuc
400f4a2713aSLionel Sambuc Result = S.BuildObjCStringLiteral(OrigElement->getLocStart(), String);
401f4a2713aSLionel Sambuc if (Result.isInvalid())
402f4a2713aSLionel Sambuc return ExprError();
403f4a2713aSLionel Sambuc
404f4a2713aSLionel Sambuc Element = Result.get();
405f4a2713aSLionel Sambuc Recovered = true;
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc }
408f4a2713aSLionel Sambuc
409f4a2713aSLionel Sambuc if (!Recovered) {
410f4a2713aSLionel Sambuc S.Diag(Element->getLocStart(), diag::err_invalid_collection_element)
411f4a2713aSLionel Sambuc << Element->getType();
412f4a2713aSLionel Sambuc return ExprError();
413f4a2713aSLionel Sambuc }
414f4a2713aSLionel Sambuc }
415f4a2713aSLionel Sambuc if (ArrayLiteral)
416f4a2713aSLionel Sambuc if (ObjCStringLiteral *getString =
417f4a2713aSLionel Sambuc dyn_cast<ObjCStringLiteral>(OrigElement)) {
418f4a2713aSLionel Sambuc if (StringLiteral *SL = getString->getString()) {
419f4a2713aSLionel Sambuc unsigned numConcat = SL->getNumConcatenated();
420f4a2713aSLionel Sambuc if (numConcat > 1) {
421f4a2713aSLionel Sambuc // Only warn if the concatenated string doesn't come from a macro.
422f4a2713aSLionel Sambuc bool hasMacro = false;
423f4a2713aSLionel Sambuc for (unsigned i = 0; i < numConcat ; ++i)
424f4a2713aSLionel Sambuc if (SL->getStrTokenLoc(i).isMacroID()) {
425f4a2713aSLionel Sambuc hasMacro = true;
426f4a2713aSLionel Sambuc break;
427f4a2713aSLionel Sambuc }
428f4a2713aSLionel Sambuc if (!hasMacro)
429f4a2713aSLionel Sambuc S.Diag(Element->getLocStart(),
430f4a2713aSLionel Sambuc diag::warn_concatenated_nsarray_literal)
431f4a2713aSLionel Sambuc << Element->getType();
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc }
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc
436f4a2713aSLionel Sambuc // Make sure that the element has the type that the container factory
437f4a2713aSLionel Sambuc // function expects.
438f4a2713aSLionel Sambuc return S.PerformCopyInitialization(
439f4a2713aSLionel Sambuc InitializedEntity::InitializeParameter(S.Context, T,
440f4a2713aSLionel Sambuc /*Consumed=*/false),
441f4a2713aSLionel Sambuc Element->getLocStart(), Element);
442f4a2713aSLionel Sambuc }
443f4a2713aSLionel Sambuc
BuildObjCBoxedExpr(SourceRange SR,Expr * ValueExpr)444f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
445f4a2713aSLionel Sambuc if (ValueExpr->isTypeDependent()) {
446f4a2713aSLionel Sambuc ObjCBoxedExpr *BoxedExpr =
447*0a6a1f1dSLionel Sambuc new (Context) ObjCBoxedExpr(ValueExpr, Context.DependentTy, nullptr, SR);
448*0a6a1f1dSLionel Sambuc return BoxedExpr;
449f4a2713aSLionel Sambuc }
450*0a6a1f1dSLionel Sambuc ObjCMethodDecl *BoxingMethod = nullptr;
451f4a2713aSLionel Sambuc QualType BoxedType;
452f4a2713aSLionel Sambuc // Convert the expression to an RValue, so we can check for pointer types...
453f4a2713aSLionel Sambuc ExprResult RValue = DefaultFunctionArrayLvalueConversion(ValueExpr);
454f4a2713aSLionel Sambuc if (RValue.isInvalid()) {
455f4a2713aSLionel Sambuc return ExprError();
456f4a2713aSLionel Sambuc }
457f4a2713aSLionel Sambuc ValueExpr = RValue.get();
458f4a2713aSLionel Sambuc QualType ValueType(ValueExpr->getType());
459f4a2713aSLionel Sambuc if (const PointerType *PT = ValueType->getAs<PointerType>()) {
460f4a2713aSLionel Sambuc QualType PointeeType = PT->getPointeeType();
461f4a2713aSLionel Sambuc if (Context.hasSameUnqualifiedType(PointeeType, Context.CharTy)) {
462f4a2713aSLionel Sambuc
463f4a2713aSLionel Sambuc if (!NSStringDecl) {
464f4a2713aSLionel Sambuc IdentifierInfo *NSStringId =
465f4a2713aSLionel Sambuc NSAPIObj->getNSClassId(NSAPI::ClassId_NSString);
466f4a2713aSLionel Sambuc NamedDecl *Decl = LookupSingleName(TUScope, NSStringId,
467f4a2713aSLionel Sambuc SR.getBegin(), LookupOrdinaryName);
468f4a2713aSLionel Sambuc NSStringDecl = dyn_cast_or_null<ObjCInterfaceDecl>(Decl);
469f4a2713aSLionel Sambuc if (!NSStringDecl) {
470f4a2713aSLionel Sambuc if (getLangOpts().DebuggerObjCLiteral) {
471f4a2713aSLionel Sambuc // Support boxed expressions in the debugger w/o NSString declaration.
472f4a2713aSLionel Sambuc DeclContext *TU = Context.getTranslationUnitDecl();
473f4a2713aSLionel Sambuc NSStringDecl = ObjCInterfaceDecl::Create(Context, TU,
474f4a2713aSLionel Sambuc SourceLocation(),
475f4a2713aSLionel Sambuc NSStringId,
476*0a6a1f1dSLionel Sambuc nullptr, SourceLocation());
477f4a2713aSLionel Sambuc } else {
478f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_undeclared_nsstring);
479f4a2713aSLionel Sambuc return ExprError();
480f4a2713aSLionel Sambuc }
481f4a2713aSLionel Sambuc } else if (!NSStringDecl->hasDefinition()) {
482f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_undeclared_nsstring);
483f4a2713aSLionel Sambuc return ExprError();
484f4a2713aSLionel Sambuc }
485f4a2713aSLionel Sambuc assert(NSStringDecl && "NSStringDecl should not be NULL");
486f4a2713aSLionel Sambuc QualType NSStringObject = Context.getObjCInterfaceType(NSStringDecl);
487f4a2713aSLionel Sambuc NSStringPointer = Context.getObjCObjectPointerType(NSStringObject);
488f4a2713aSLionel Sambuc }
489f4a2713aSLionel Sambuc
490f4a2713aSLionel Sambuc if (!StringWithUTF8StringMethod) {
491f4a2713aSLionel Sambuc IdentifierInfo *II = &Context.Idents.get("stringWithUTF8String");
492f4a2713aSLionel Sambuc Selector stringWithUTF8String = Context.Selectors.getUnarySelector(II);
493f4a2713aSLionel Sambuc
494f4a2713aSLionel Sambuc // Look for the appropriate method within NSString.
495f4a2713aSLionel Sambuc BoxingMethod = NSStringDecl->lookupClassMethod(stringWithUTF8String);
496f4a2713aSLionel Sambuc if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) {
497f4a2713aSLionel Sambuc // Debugger needs to work even if NSString hasn't been defined.
498*0a6a1f1dSLionel Sambuc TypeSourceInfo *ReturnTInfo = nullptr;
499*0a6a1f1dSLionel Sambuc ObjCMethodDecl *M = ObjCMethodDecl::Create(
500*0a6a1f1dSLionel Sambuc Context, SourceLocation(), SourceLocation(), stringWithUTF8String,
501*0a6a1f1dSLionel Sambuc NSStringPointer, ReturnTInfo, NSStringDecl,
502f4a2713aSLionel Sambuc /*isInstance=*/false, /*isVariadic=*/false,
503f4a2713aSLionel Sambuc /*isPropertyAccessor=*/false,
504f4a2713aSLionel Sambuc /*isImplicitlyDeclared=*/true,
505*0a6a1f1dSLionel Sambuc /*isDefined=*/false, ObjCMethodDecl::Required,
506f4a2713aSLionel Sambuc /*HasRelatedResultType=*/false);
507f4a2713aSLionel Sambuc QualType ConstCharType = Context.CharTy.withConst();
508f4a2713aSLionel Sambuc ParmVarDecl *value =
509f4a2713aSLionel Sambuc ParmVarDecl::Create(Context, M,
510f4a2713aSLionel Sambuc SourceLocation(), SourceLocation(),
511f4a2713aSLionel Sambuc &Context.Idents.get("value"),
512f4a2713aSLionel Sambuc Context.getPointerType(ConstCharType),
513*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr,
514*0a6a1f1dSLionel Sambuc SC_None, nullptr);
515f4a2713aSLionel Sambuc M->setMethodParams(Context, value, None);
516f4a2713aSLionel Sambuc BoxingMethod = M;
517f4a2713aSLionel Sambuc }
518f4a2713aSLionel Sambuc
519f4a2713aSLionel Sambuc if (!validateBoxingMethod(*this, SR.getBegin(), NSStringDecl,
520f4a2713aSLionel Sambuc stringWithUTF8String, BoxingMethod))
521f4a2713aSLionel Sambuc return ExprError();
522f4a2713aSLionel Sambuc
523f4a2713aSLionel Sambuc StringWithUTF8StringMethod = BoxingMethod;
524f4a2713aSLionel Sambuc }
525f4a2713aSLionel Sambuc
526f4a2713aSLionel Sambuc BoxingMethod = StringWithUTF8StringMethod;
527f4a2713aSLionel Sambuc BoxedType = NSStringPointer;
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc } else if (ValueType->isBuiltinType()) {
530f4a2713aSLionel Sambuc // The other types we support are numeric, char and BOOL/bool. We could also
531f4a2713aSLionel Sambuc // provide limited support for structure types, such as NSRange, NSRect, and
532f4a2713aSLionel Sambuc // NSSize. See NSValue (NSValueGeometryExtensions) in <Foundation/NSGeometry.h>
533f4a2713aSLionel Sambuc // for more details.
534f4a2713aSLionel Sambuc
535f4a2713aSLionel Sambuc // Check for a top-level character literal.
536f4a2713aSLionel Sambuc if (const CharacterLiteral *Char =
537f4a2713aSLionel Sambuc dyn_cast<CharacterLiteral>(ValueExpr->IgnoreParens())) {
538f4a2713aSLionel Sambuc // In C, character literals have type 'int'. That's not the type we want
539f4a2713aSLionel Sambuc // to use to determine the Objective-c literal kind.
540f4a2713aSLionel Sambuc switch (Char->getKind()) {
541f4a2713aSLionel Sambuc case CharacterLiteral::Ascii:
542f4a2713aSLionel Sambuc ValueType = Context.CharTy;
543f4a2713aSLionel Sambuc break;
544f4a2713aSLionel Sambuc
545f4a2713aSLionel Sambuc case CharacterLiteral::Wide:
546f4a2713aSLionel Sambuc ValueType = Context.getWideCharType();
547f4a2713aSLionel Sambuc break;
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc case CharacterLiteral::UTF16:
550f4a2713aSLionel Sambuc ValueType = Context.Char16Ty;
551f4a2713aSLionel Sambuc break;
552f4a2713aSLionel Sambuc
553f4a2713aSLionel Sambuc case CharacterLiteral::UTF32:
554f4a2713aSLionel Sambuc ValueType = Context.Char32Ty;
555f4a2713aSLionel Sambuc break;
556f4a2713aSLionel Sambuc }
557f4a2713aSLionel Sambuc }
558*0a6a1f1dSLionel Sambuc CheckForIntOverflow(ValueExpr);
559f4a2713aSLionel Sambuc // FIXME: Do I need to do anything special with BoolTy expressions?
560f4a2713aSLionel Sambuc
561f4a2713aSLionel Sambuc // Look for the appropriate method within NSNumber.
562f4a2713aSLionel Sambuc BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(), ValueType);
563f4a2713aSLionel Sambuc BoxedType = NSNumberPointer;
564f4a2713aSLionel Sambuc
565f4a2713aSLionel Sambuc } else if (const EnumType *ET = ValueType->getAs<EnumType>()) {
566f4a2713aSLionel Sambuc if (!ET->getDecl()->isComplete()) {
567f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_incomplete_boxed_expression_type)
568f4a2713aSLionel Sambuc << ValueType << ValueExpr->getSourceRange();
569f4a2713aSLionel Sambuc return ExprError();
570f4a2713aSLionel Sambuc }
571f4a2713aSLionel Sambuc
572f4a2713aSLionel Sambuc BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(),
573f4a2713aSLionel Sambuc ET->getDecl()->getIntegerType());
574f4a2713aSLionel Sambuc BoxedType = NSNumberPointer;
575f4a2713aSLionel Sambuc }
576f4a2713aSLionel Sambuc
577f4a2713aSLionel Sambuc if (!BoxingMethod) {
578f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_illegal_boxed_expression_type)
579f4a2713aSLionel Sambuc << ValueType << ValueExpr->getSourceRange();
580f4a2713aSLionel Sambuc return ExprError();
581f4a2713aSLionel Sambuc }
582f4a2713aSLionel Sambuc
583f4a2713aSLionel Sambuc // Convert the expression to the type that the parameter requires.
584*0a6a1f1dSLionel Sambuc ParmVarDecl *ParamDecl = BoxingMethod->parameters()[0];
585f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
586f4a2713aSLionel Sambuc ParamDecl);
587f4a2713aSLionel Sambuc ExprResult ConvertedValueExpr = PerformCopyInitialization(Entity,
588f4a2713aSLionel Sambuc SourceLocation(),
589*0a6a1f1dSLionel Sambuc ValueExpr);
590f4a2713aSLionel Sambuc if (ConvertedValueExpr.isInvalid())
591f4a2713aSLionel Sambuc return ExprError();
592f4a2713aSLionel Sambuc ValueExpr = ConvertedValueExpr.get();
593f4a2713aSLionel Sambuc
594f4a2713aSLionel Sambuc ObjCBoxedExpr *BoxedExpr =
595f4a2713aSLionel Sambuc new (Context) ObjCBoxedExpr(ValueExpr, BoxedType,
596f4a2713aSLionel Sambuc BoxingMethod, SR);
597f4a2713aSLionel Sambuc return MaybeBindToTemporary(BoxedExpr);
598f4a2713aSLionel Sambuc }
599f4a2713aSLionel Sambuc
600f4a2713aSLionel Sambuc /// Build an ObjC subscript pseudo-object expression, given that
601f4a2713aSLionel Sambuc /// that's supported by the runtime.
BuildObjCSubscriptExpression(SourceLocation RB,Expr * BaseExpr,Expr * IndexExpr,ObjCMethodDecl * getterMethod,ObjCMethodDecl * setterMethod)602f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCSubscriptExpression(SourceLocation RB, Expr *BaseExpr,
603f4a2713aSLionel Sambuc Expr *IndexExpr,
604f4a2713aSLionel Sambuc ObjCMethodDecl *getterMethod,
605f4a2713aSLionel Sambuc ObjCMethodDecl *setterMethod) {
606f4a2713aSLionel Sambuc assert(!LangOpts.isSubscriptPointerArithmetic());
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc // We can't get dependent types here; our callers should have
609f4a2713aSLionel Sambuc // filtered them out.
610f4a2713aSLionel Sambuc assert((!BaseExpr->isTypeDependent() && !IndexExpr->isTypeDependent()) &&
611f4a2713aSLionel Sambuc "base or index cannot have dependent type here");
612f4a2713aSLionel Sambuc
613f4a2713aSLionel Sambuc // Filter out placeholders in the index. In theory, overloads could
614f4a2713aSLionel Sambuc // be preserved here, although that might not actually work correctly.
615f4a2713aSLionel Sambuc ExprResult Result = CheckPlaceholderExpr(IndexExpr);
616f4a2713aSLionel Sambuc if (Result.isInvalid())
617f4a2713aSLionel Sambuc return ExprError();
618f4a2713aSLionel Sambuc IndexExpr = Result.get();
619f4a2713aSLionel Sambuc
620f4a2713aSLionel Sambuc // Perform lvalue-to-rvalue conversion on the base.
621f4a2713aSLionel Sambuc Result = DefaultLvalueConversion(BaseExpr);
622f4a2713aSLionel Sambuc if (Result.isInvalid())
623f4a2713aSLionel Sambuc return ExprError();
624f4a2713aSLionel Sambuc BaseExpr = Result.get();
625f4a2713aSLionel Sambuc
626f4a2713aSLionel Sambuc // Build the pseudo-object expression.
627*0a6a1f1dSLionel Sambuc return ObjCSubscriptRefExpr::Create(Context, BaseExpr, IndexExpr,
628*0a6a1f1dSLionel Sambuc Context.PseudoObjectTy, getterMethod,
629*0a6a1f1dSLionel Sambuc setterMethod, RB);
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc
BuildObjCArrayLiteral(SourceRange SR,MultiExprArg Elements)632f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) {
633f4a2713aSLionel Sambuc // Look up the NSArray class, if we haven't done so already.
634f4a2713aSLionel Sambuc if (!NSArrayDecl) {
635f4a2713aSLionel Sambuc NamedDecl *IF = LookupSingleName(TUScope,
636f4a2713aSLionel Sambuc NSAPIObj->getNSClassId(NSAPI::ClassId_NSArray),
637f4a2713aSLionel Sambuc SR.getBegin(),
638f4a2713aSLionel Sambuc LookupOrdinaryName);
639f4a2713aSLionel Sambuc NSArrayDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
640f4a2713aSLionel Sambuc if (!NSArrayDecl && getLangOpts().DebuggerObjCLiteral)
641f4a2713aSLionel Sambuc NSArrayDecl = ObjCInterfaceDecl::Create (Context,
642f4a2713aSLionel Sambuc Context.getTranslationUnitDecl(),
643f4a2713aSLionel Sambuc SourceLocation(),
644f4a2713aSLionel Sambuc NSAPIObj->getNSClassId(NSAPI::ClassId_NSArray),
645*0a6a1f1dSLionel Sambuc nullptr, SourceLocation());
646f4a2713aSLionel Sambuc
647f4a2713aSLionel Sambuc if (!NSArrayDecl) {
648f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_undeclared_nsarray);
649f4a2713aSLionel Sambuc return ExprError();
650f4a2713aSLionel Sambuc }
651f4a2713aSLionel Sambuc }
652f4a2713aSLionel Sambuc
653f4a2713aSLionel Sambuc // Find the arrayWithObjects:count: method, if we haven't done so already.
654f4a2713aSLionel Sambuc QualType IdT = Context.getObjCIdType();
655f4a2713aSLionel Sambuc if (!ArrayWithObjectsMethod) {
656f4a2713aSLionel Sambuc Selector
657f4a2713aSLionel Sambuc Sel = NSAPIObj->getNSArraySelector(NSAPI::NSArr_arrayWithObjectsCount);
658f4a2713aSLionel Sambuc ObjCMethodDecl *Method = NSArrayDecl->lookupClassMethod(Sel);
659f4a2713aSLionel Sambuc if (!Method && getLangOpts().DebuggerObjCLiteral) {
660*0a6a1f1dSLionel Sambuc TypeSourceInfo *ReturnTInfo = nullptr;
661*0a6a1f1dSLionel Sambuc Method = ObjCMethodDecl::Create(
662*0a6a1f1dSLionel Sambuc Context, SourceLocation(), SourceLocation(), Sel, IdT, ReturnTInfo,
663*0a6a1f1dSLionel Sambuc Context.getTranslationUnitDecl(), false /*Instance*/,
664*0a6a1f1dSLionel Sambuc false /*isVariadic*/,
665f4a2713aSLionel Sambuc /*isPropertyAccessor=*/false,
666f4a2713aSLionel Sambuc /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
667*0a6a1f1dSLionel Sambuc ObjCMethodDecl::Required, false);
668f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 2> Params;
669f4a2713aSLionel Sambuc ParmVarDecl *objects = ParmVarDecl::Create(Context, Method,
670f4a2713aSLionel Sambuc SourceLocation(),
671f4a2713aSLionel Sambuc SourceLocation(),
672f4a2713aSLionel Sambuc &Context.Idents.get("objects"),
673f4a2713aSLionel Sambuc Context.getPointerType(IdT),
674*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr,
675*0a6a1f1dSLionel Sambuc SC_None, nullptr);
676f4a2713aSLionel Sambuc Params.push_back(objects);
677f4a2713aSLionel Sambuc ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method,
678f4a2713aSLionel Sambuc SourceLocation(),
679f4a2713aSLionel Sambuc SourceLocation(),
680f4a2713aSLionel Sambuc &Context.Idents.get("cnt"),
681f4a2713aSLionel Sambuc Context.UnsignedLongTy,
682*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr, SC_None,
683*0a6a1f1dSLionel Sambuc nullptr);
684f4a2713aSLionel Sambuc Params.push_back(cnt);
685f4a2713aSLionel Sambuc Method->setMethodParams(Context, Params, None);
686f4a2713aSLionel Sambuc }
687f4a2713aSLionel Sambuc
688f4a2713aSLionel Sambuc if (!validateBoxingMethod(*this, SR.getBegin(), NSArrayDecl, Sel, Method))
689f4a2713aSLionel Sambuc return ExprError();
690f4a2713aSLionel Sambuc
691f4a2713aSLionel Sambuc // Dig out the type that all elements should be converted to.
692*0a6a1f1dSLionel Sambuc QualType T = Method->parameters()[0]->getType();
693f4a2713aSLionel Sambuc const PointerType *PtrT = T->getAs<PointerType>();
694f4a2713aSLionel Sambuc if (!PtrT ||
695f4a2713aSLionel Sambuc !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) {
696f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
697f4a2713aSLionel Sambuc << Sel;
698*0a6a1f1dSLionel Sambuc Diag(Method->parameters()[0]->getLocation(),
699f4a2713aSLionel Sambuc diag::note_objc_literal_method_param)
700f4a2713aSLionel Sambuc << 0 << T
701f4a2713aSLionel Sambuc << Context.getPointerType(IdT.withConst());
702f4a2713aSLionel Sambuc return ExprError();
703f4a2713aSLionel Sambuc }
704f4a2713aSLionel Sambuc
705f4a2713aSLionel Sambuc // Check that the 'count' parameter is integral.
706*0a6a1f1dSLionel Sambuc if (!Method->parameters()[1]->getType()->isIntegerType()) {
707f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
708f4a2713aSLionel Sambuc << Sel;
709*0a6a1f1dSLionel Sambuc Diag(Method->parameters()[1]->getLocation(),
710f4a2713aSLionel Sambuc diag::note_objc_literal_method_param)
711f4a2713aSLionel Sambuc << 1
712*0a6a1f1dSLionel Sambuc << Method->parameters()[1]->getType()
713f4a2713aSLionel Sambuc << "integral";
714f4a2713aSLionel Sambuc return ExprError();
715f4a2713aSLionel Sambuc }
716f4a2713aSLionel Sambuc
717f4a2713aSLionel Sambuc // We've found a good +arrayWithObjects:count: method. Save it!
718f4a2713aSLionel Sambuc ArrayWithObjectsMethod = Method;
719f4a2713aSLionel Sambuc }
720f4a2713aSLionel Sambuc
721*0a6a1f1dSLionel Sambuc QualType ObjectsType = ArrayWithObjectsMethod->parameters()[0]->getType();
722f4a2713aSLionel Sambuc QualType RequiredType = ObjectsType->castAs<PointerType>()->getPointeeType();
723f4a2713aSLionel Sambuc
724f4a2713aSLionel Sambuc // Check that each of the elements provided is valid in a collection literal,
725f4a2713aSLionel Sambuc // performing conversions as necessary.
726f4a2713aSLionel Sambuc Expr **ElementsBuffer = Elements.data();
727f4a2713aSLionel Sambuc for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
728f4a2713aSLionel Sambuc ExprResult Converted = CheckObjCCollectionLiteralElement(*this,
729f4a2713aSLionel Sambuc ElementsBuffer[I],
730f4a2713aSLionel Sambuc RequiredType, true);
731f4a2713aSLionel Sambuc if (Converted.isInvalid())
732f4a2713aSLionel Sambuc return ExprError();
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc ElementsBuffer[I] = Converted.get();
735f4a2713aSLionel Sambuc }
736f4a2713aSLionel Sambuc
737f4a2713aSLionel Sambuc QualType Ty
738f4a2713aSLionel Sambuc = Context.getObjCObjectPointerType(
739f4a2713aSLionel Sambuc Context.getObjCInterfaceType(NSArrayDecl));
740f4a2713aSLionel Sambuc
741f4a2713aSLionel Sambuc return MaybeBindToTemporary(
742f4a2713aSLionel Sambuc ObjCArrayLiteral::Create(Context, Elements, Ty,
743f4a2713aSLionel Sambuc ArrayWithObjectsMethod, SR));
744f4a2713aSLionel Sambuc }
745f4a2713aSLionel Sambuc
BuildObjCDictionaryLiteral(SourceRange SR,ObjCDictionaryElement * Elements,unsigned NumElements)746f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR,
747f4a2713aSLionel Sambuc ObjCDictionaryElement *Elements,
748f4a2713aSLionel Sambuc unsigned NumElements) {
749f4a2713aSLionel Sambuc // Look up the NSDictionary class, if we haven't done so already.
750f4a2713aSLionel Sambuc if (!NSDictionaryDecl) {
751f4a2713aSLionel Sambuc NamedDecl *IF = LookupSingleName(TUScope,
752f4a2713aSLionel Sambuc NSAPIObj->getNSClassId(NSAPI::ClassId_NSDictionary),
753f4a2713aSLionel Sambuc SR.getBegin(), LookupOrdinaryName);
754f4a2713aSLionel Sambuc NSDictionaryDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
755f4a2713aSLionel Sambuc if (!NSDictionaryDecl && getLangOpts().DebuggerObjCLiteral)
756f4a2713aSLionel Sambuc NSDictionaryDecl = ObjCInterfaceDecl::Create (Context,
757f4a2713aSLionel Sambuc Context.getTranslationUnitDecl(),
758f4a2713aSLionel Sambuc SourceLocation(),
759f4a2713aSLionel Sambuc NSAPIObj->getNSClassId(NSAPI::ClassId_NSDictionary),
760*0a6a1f1dSLionel Sambuc nullptr, SourceLocation());
761f4a2713aSLionel Sambuc
762f4a2713aSLionel Sambuc if (!NSDictionaryDecl) {
763f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_undeclared_nsdictionary);
764f4a2713aSLionel Sambuc return ExprError();
765f4a2713aSLionel Sambuc }
766f4a2713aSLionel Sambuc }
767f4a2713aSLionel Sambuc
768f4a2713aSLionel Sambuc // Find the dictionaryWithObjects:forKeys:count: method, if we haven't done
769f4a2713aSLionel Sambuc // so already.
770f4a2713aSLionel Sambuc QualType IdT = Context.getObjCIdType();
771f4a2713aSLionel Sambuc if (!DictionaryWithObjectsMethod) {
772f4a2713aSLionel Sambuc Selector Sel = NSAPIObj->getNSDictionarySelector(
773f4a2713aSLionel Sambuc NSAPI::NSDict_dictionaryWithObjectsForKeysCount);
774f4a2713aSLionel Sambuc ObjCMethodDecl *Method = NSDictionaryDecl->lookupClassMethod(Sel);
775f4a2713aSLionel Sambuc if (!Method && getLangOpts().DebuggerObjCLiteral) {
776f4a2713aSLionel Sambuc Method = ObjCMethodDecl::Create(Context,
777f4a2713aSLionel Sambuc SourceLocation(), SourceLocation(), Sel,
778f4a2713aSLionel Sambuc IdT,
779*0a6a1f1dSLionel Sambuc nullptr /*TypeSourceInfo */,
780f4a2713aSLionel Sambuc Context.getTranslationUnitDecl(),
781f4a2713aSLionel Sambuc false /*Instance*/, false/*isVariadic*/,
782f4a2713aSLionel Sambuc /*isPropertyAccessor=*/false,
783f4a2713aSLionel Sambuc /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
784f4a2713aSLionel Sambuc ObjCMethodDecl::Required,
785f4a2713aSLionel Sambuc false);
786f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 3> Params;
787f4a2713aSLionel Sambuc ParmVarDecl *objects = ParmVarDecl::Create(Context, Method,
788f4a2713aSLionel Sambuc SourceLocation(),
789f4a2713aSLionel Sambuc SourceLocation(),
790f4a2713aSLionel Sambuc &Context.Idents.get("objects"),
791f4a2713aSLionel Sambuc Context.getPointerType(IdT),
792*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr, SC_None,
793*0a6a1f1dSLionel Sambuc nullptr);
794f4a2713aSLionel Sambuc Params.push_back(objects);
795f4a2713aSLionel Sambuc ParmVarDecl *keys = ParmVarDecl::Create(Context, Method,
796f4a2713aSLionel Sambuc SourceLocation(),
797f4a2713aSLionel Sambuc SourceLocation(),
798f4a2713aSLionel Sambuc &Context.Idents.get("keys"),
799f4a2713aSLionel Sambuc Context.getPointerType(IdT),
800*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr, SC_None,
801*0a6a1f1dSLionel Sambuc nullptr);
802f4a2713aSLionel Sambuc Params.push_back(keys);
803f4a2713aSLionel Sambuc ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method,
804f4a2713aSLionel Sambuc SourceLocation(),
805f4a2713aSLionel Sambuc SourceLocation(),
806f4a2713aSLionel Sambuc &Context.Idents.get("cnt"),
807f4a2713aSLionel Sambuc Context.UnsignedLongTy,
808*0a6a1f1dSLionel Sambuc /*TInfo=*/nullptr, SC_None,
809*0a6a1f1dSLionel Sambuc nullptr);
810f4a2713aSLionel Sambuc Params.push_back(cnt);
811f4a2713aSLionel Sambuc Method->setMethodParams(Context, Params, None);
812f4a2713aSLionel Sambuc }
813f4a2713aSLionel Sambuc
814f4a2713aSLionel Sambuc if (!validateBoxingMethod(*this, SR.getBegin(), NSDictionaryDecl, Sel,
815f4a2713aSLionel Sambuc Method))
816f4a2713aSLionel Sambuc return ExprError();
817f4a2713aSLionel Sambuc
818f4a2713aSLionel Sambuc // Dig out the type that all values should be converted to.
819*0a6a1f1dSLionel Sambuc QualType ValueT = Method->parameters()[0]->getType();
820f4a2713aSLionel Sambuc const PointerType *PtrValue = ValueT->getAs<PointerType>();
821f4a2713aSLionel Sambuc if (!PtrValue ||
822f4a2713aSLionel Sambuc !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) {
823f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
824f4a2713aSLionel Sambuc << Sel;
825*0a6a1f1dSLionel Sambuc Diag(Method->parameters()[0]->getLocation(),
826f4a2713aSLionel Sambuc diag::note_objc_literal_method_param)
827f4a2713aSLionel Sambuc << 0 << ValueT
828f4a2713aSLionel Sambuc << Context.getPointerType(IdT.withConst());
829f4a2713aSLionel Sambuc return ExprError();
830f4a2713aSLionel Sambuc }
831f4a2713aSLionel Sambuc
832f4a2713aSLionel Sambuc // Dig out the type that all keys should be converted to.
833*0a6a1f1dSLionel Sambuc QualType KeyT = Method->parameters()[1]->getType();
834f4a2713aSLionel Sambuc const PointerType *PtrKey = KeyT->getAs<PointerType>();
835f4a2713aSLionel Sambuc if (!PtrKey ||
836f4a2713aSLionel Sambuc !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(),
837f4a2713aSLionel Sambuc IdT)) {
838f4a2713aSLionel Sambuc bool err = true;
839f4a2713aSLionel Sambuc if (PtrKey) {
840f4a2713aSLionel Sambuc if (QIDNSCopying.isNull()) {
841f4a2713aSLionel Sambuc // key argument of selector is id<NSCopying>?
842f4a2713aSLionel Sambuc if (ObjCProtocolDecl *NSCopyingPDecl =
843f4a2713aSLionel Sambuc LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) {
844f4a2713aSLionel Sambuc ObjCProtocolDecl *PQ[] = {NSCopyingPDecl};
845f4a2713aSLionel Sambuc QIDNSCopying =
846f4a2713aSLionel Sambuc Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
847f4a2713aSLionel Sambuc (ObjCProtocolDecl**) PQ,1);
848f4a2713aSLionel Sambuc QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying);
849f4a2713aSLionel Sambuc }
850f4a2713aSLionel Sambuc }
851f4a2713aSLionel Sambuc if (!QIDNSCopying.isNull())
852f4a2713aSLionel Sambuc err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(),
853f4a2713aSLionel Sambuc QIDNSCopying);
854f4a2713aSLionel Sambuc }
855f4a2713aSLionel Sambuc
856f4a2713aSLionel Sambuc if (err) {
857f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
858f4a2713aSLionel Sambuc << Sel;
859*0a6a1f1dSLionel Sambuc Diag(Method->parameters()[1]->getLocation(),
860f4a2713aSLionel Sambuc diag::note_objc_literal_method_param)
861f4a2713aSLionel Sambuc << 1 << KeyT
862f4a2713aSLionel Sambuc << Context.getPointerType(IdT.withConst());
863f4a2713aSLionel Sambuc return ExprError();
864f4a2713aSLionel Sambuc }
865f4a2713aSLionel Sambuc }
866f4a2713aSLionel Sambuc
867f4a2713aSLionel Sambuc // Check that the 'count' parameter is integral.
868*0a6a1f1dSLionel Sambuc QualType CountType = Method->parameters()[2]->getType();
869f4a2713aSLionel Sambuc if (!CountType->isIntegerType()) {
870f4a2713aSLionel Sambuc Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
871f4a2713aSLionel Sambuc << Sel;
872*0a6a1f1dSLionel Sambuc Diag(Method->parameters()[2]->getLocation(),
873f4a2713aSLionel Sambuc diag::note_objc_literal_method_param)
874f4a2713aSLionel Sambuc << 2 << CountType
875f4a2713aSLionel Sambuc << "integral";
876f4a2713aSLionel Sambuc return ExprError();
877f4a2713aSLionel Sambuc }
878f4a2713aSLionel Sambuc
879f4a2713aSLionel Sambuc // We've found a good +dictionaryWithObjects:keys:count: method; save it!
880f4a2713aSLionel Sambuc DictionaryWithObjectsMethod = Method;
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc
883*0a6a1f1dSLionel Sambuc QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType();
884f4a2713aSLionel Sambuc QualType ValueT = ValuesT->castAs<PointerType>()->getPointeeType();
885*0a6a1f1dSLionel Sambuc QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType();
886f4a2713aSLionel Sambuc QualType KeyT = KeysT->castAs<PointerType>()->getPointeeType();
887f4a2713aSLionel Sambuc
888f4a2713aSLionel Sambuc // Check that each of the keys and values provided is valid in a collection
889f4a2713aSLionel Sambuc // literal, performing conversions as necessary.
890f4a2713aSLionel Sambuc bool HasPackExpansions = false;
891f4a2713aSLionel Sambuc for (unsigned I = 0, N = NumElements; I != N; ++I) {
892f4a2713aSLionel Sambuc // Check the key.
893f4a2713aSLionel Sambuc ExprResult Key = CheckObjCCollectionLiteralElement(*this, Elements[I].Key,
894f4a2713aSLionel Sambuc KeyT);
895f4a2713aSLionel Sambuc if (Key.isInvalid())
896f4a2713aSLionel Sambuc return ExprError();
897f4a2713aSLionel Sambuc
898f4a2713aSLionel Sambuc // Check the value.
899f4a2713aSLionel Sambuc ExprResult Value
900f4a2713aSLionel Sambuc = CheckObjCCollectionLiteralElement(*this, Elements[I].Value, ValueT);
901f4a2713aSLionel Sambuc if (Value.isInvalid())
902f4a2713aSLionel Sambuc return ExprError();
903f4a2713aSLionel Sambuc
904f4a2713aSLionel Sambuc Elements[I].Key = Key.get();
905f4a2713aSLionel Sambuc Elements[I].Value = Value.get();
906f4a2713aSLionel Sambuc
907f4a2713aSLionel Sambuc if (Elements[I].EllipsisLoc.isInvalid())
908f4a2713aSLionel Sambuc continue;
909f4a2713aSLionel Sambuc
910f4a2713aSLionel Sambuc if (!Elements[I].Key->containsUnexpandedParameterPack() &&
911f4a2713aSLionel Sambuc !Elements[I].Value->containsUnexpandedParameterPack()) {
912f4a2713aSLionel Sambuc Diag(Elements[I].EllipsisLoc,
913f4a2713aSLionel Sambuc diag::err_pack_expansion_without_parameter_packs)
914f4a2713aSLionel Sambuc << SourceRange(Elements[I].Key->getLocStart(),
915f4a2713aSLionel Sambuc Elements[I].Value->getLocEnd());
916f4a2713aSLionel Sambuc return ExprError();
917f4a2713aSLionel Sambuc }
918f4a2713aSLionel Sambuc
919f4a2713aSLionel Sambuc HasPackExpansions = true;
920f4a2713aSLionel Sambuc }
921f4a2713aSLionel Sambuc
922f4a2713aSLionel Sambuc
923f4a2713aSLionel Sambuc QualType Ty
924f4a2713aSLionel Sambuc = Context.getObjCObjectPointerType(
925f4a2713aSLionel Sambuc Context.getObjCInterfaceType(NSDictionaryDecl));
926f4a2713aSLionel Sambuc return MaybeBindToTemporary(ObjCDictionaryLiteral::Create(
927f4a2713aSLionel Sambuc Context, makeArrayRef(Elements, NumElements), HasPackExpansions, Ty,
928f4a2713aSLionel Sambuc DictionaryWithObjectsMethod, SR));
929f4a2713aSLionel Sambuc }
930f4a2713aSLionel Sambuc
BuildObjCEncodeExpression(SourceLocation AtLoc,TypeSourceInfo * EncodedTypeInfo,SourceLocation RParenLoc)931f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
932f4a2713aSLionel Sambuc TypeSourceInfo *EncodedTypeInfo,
933f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
934f4a2713aSLionel Sambuc QualType EncodedType = EncodedTypeInfo->getType();
935f4a2713aSLionel Sambuc QualType StrTy;
936f4a2713aSLionel Sambuc if (EncodedType->isDependentType())
937f4a2713aSLionel Sambuc StrTy = Context.DependentTy;
938f4a2713aSLionel Sambuc else {
939f4a2713aSLionel Sambuc if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled.
940f4a2713aSLionel Sambuc !EncodedType->isVoidType()) // void is handled too.
941f4a2713aSLionel Sambuc if (RequireCompleteType(AtLoc, EncodedType,
942f4a2713aSLionel Sambuc diag::err_incomplete_type_objc_at_encode,
943f4a2713aSLionel Sambuc EncodedTypeInfo->getTypeLoc()))
944f4a2713aSLionel Sambuc return ExprError();
945f4a2713aSLionel Sambuc
946f4a2713aSLionel Sambuc std::string Str;
947*0a6a1f1dSLionel Sambuc QualType NotEncodedT;
948*0a6a1f1dSLionel Sambuc Context.getObjCEncodingForType(EncodedType, Str, nullptr, &NotEncodedT);
949*0a6a1f1dSLionel Sambuc if (!NotEncodedT.isNull())
950*0a6a1f1dSLionel Sambuc Diag(AtLoc, diag::warn_incomplete_encoded_type)
951*0a6a1f1dSLionel Sambuc << EncodedType << NotEncodedT;
952f4a2713aSLionel Sambuc
953f4a2713aSLionel Sambuc // The type of @encode is the same as the type of the corresponding string,
954f4a2713aSLionel Sambuc // which is an array type.
955f4a2713aSLionel Sambuc StrTy = Context.CharTy;
956f4a2713aSLionel Sambuc // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
957f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings)
958f4a2713aSLionel Sambuc StrTy.addConst();
959f4a2713aSLionel Sambuc StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
960f4a2713aSLionel Sambuc ArrayType::Normal, 0);
961f4a2713aSLionel Sambuc }
962f4a2713aSLionel Sambuc
963f4a2713aSLionel Sambuc return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
964f4a2713aSLionel Sambuc }
965f4a2713aSLionel Sambuc
ParseObjCEncodeExpression(SourceLocation AtLoc,SourceLocation EncodeLoc,SourceLocation LParenLoc,ParsedType ty,SourceLocation RParenLoc)966f4a2713aSLionel Sambuc ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
967f4a2713aSLionel Sambuc SourceLocation EncodeLoc,
968f4a2713aSLionel Sambuc SourceLocation LParenLoc,
969f4a2713aSLionel Sambuc ParsedType ty,
970f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
971f4a2713aSLionel Sambuc // FIXME: Preserve type source info ?
972f4a2713aSLionel Sambuc TypeSourceInfo *TInfo;
973f4a2713aSLionel Sambuc QualType EncodedType = GetTypeFromParser(ty, &TInfo);
974f4a2713aSLionel Sambuc if (!TInfo)
975f4a2713aSLionel Sambuc TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
976f4a2713aSLionel Sambuc PP.getLocForEndOfToken(LParenLoc));
977f4a2713aSLionel Sambuc
978f4a2713aSLionel Sambuc return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
979f4a2713aSLionel Sambuc }
980f4a2713aSLionel Sambuc
HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema & S,SourceLocation AtLoc,SourceLocation LParenLoc,SourceLocation RParenLoc,ObjCMethodDecl * Method,ObjCMethodList & MethList)981*0a6a1f1dSLionel Sambuc static bool HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
982*0a6a1f1dSLionel Sambuc SourceLocation AtLoc,
983*0a6a1f1dSLionel Sambuc SourceLocation LParenLoc,
984*0a6a1f1dSLionel Sambuc SourceLocation RParenLoc,
985*0a6a1f1dSLionel Sambuc ObjCMethodDecl *Method,
986*0a6a1f1dSLionel Sambuc ObjCMethodList &MethList) {
987*0a6a1f1dSLionel Sambuc ObjCMethodList *M = &MethList;
988*0a6a1f1dSLionel Sambuc bool Warned = false;
989*0a6a1f1dSLionel Sambuc for (M = M->getNext(); M; M=M->getNext()) {
990*0a6a1f1dSLionel Sambuc ObjCMethodDecl *MatchingMethodDecl = M->getMethod();
991*0a6a1f1dSLionel Sambuc if (MatchingMethodDecl == Method ||
992*0a6a1f1dSLionel Sambuc isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()) ||
993*0a6a1f1dSLionel Sambuc MatchingMethodDecl->getSelector() != Method->getSelector())
994*0a6a1f1dSLionel Sambuc continue;
995*0a6a1f1dSLionel Sambuc if (!S.MatchTwoMethodDeclarations(Method,
996*0a6a1f1dSLionel Sambuc MatchingMethodDecl, Sema::MMS_loose)) {
997*0a6a1f1dSLionel Sambuc if (!Warned) {
998*0a6a1f1dSLionel Sambuc Warned = true;
999*0a6a1f1dSLionel Sambuc S.Diag(AtLoc, diag::warning_multiple_selectors)
1000*0a6a1f1dSLionel Sambuc << Method->getSelector() << FixItHint::CreateInsertion(LParenLoc, "(")
1001*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(RParenLoc, ")");
1002*0a6a1f1dSLionel Sambuc S.Diag(Method->getLocation(), diag::note_method_declared_at)
1003*0a6a1f1dSLionel Sambuc << Method->getDeclName();
1004*0a6a1f1dSLionel Sambuc }
1005*0a6a1f1dSLionel Sambuc S.Diag(MatchingMethodDecl->getLocation(), diag::note_method_declared_at)
1006*0a6a1f1dSLionel Sambuc << MatchingMethodDecl->getDeclName();
1007*0a6a1f1dSLionel Sambuc }
1008*0a6a1f1dSLionel Sambuc }
1009*0a6a1f1dSLionel Sambuc return Warned;
1010*0a6a1f1dSLionel Sambuc }
1011*0a6a1f1dSLionel Sambuc
DiagnoseMismatchedSelectors(Sema & S,SourceLocation AtLoc,ObjCMethodDecl * Method,SourceLocation LParenLoc,SourceLocation RParenLoc,bool WarnMultipleSelectors)1012*0a6a1f1dSLionel Sambuc static void DiagnoseMismatchedSelectors(Sema &S, SourceLocation AtLoc,
1013*0a6a1f1dSLionel Sambuc ObjCMethodDecl *Method,
1014*0a6a1f1dSLionel Sambuc SourceLocation LParenLoc,
1015*0a6a1f1dSLionel Sambuc SourceLocation RParenLoc,
1016*0a6a1f1dSLionel Sambuc bool WarnMultipleSelectors) {
1017*0a6a1f1dSLionel Sambuc if (!WarnMultipleSelectors ||
1018*0a6a1f1dSLionel Sambuc S.Diags.isIgnored(diag::warning_multiple_selectors, SourceLocation()))
1019*0a6a1f1dSLionel Sambuc return;
1020*0a6a1f1dSLionel Sambuc bool Warned = false;
1021*0a6a1f1dSLionel Sambuc for (Sema::GlobalMethodPool::iterator b = S.MethodPool.begin(),
1022*0a6a1f1dSLionel Sambuc e = S.MethodPool.end(); b != e; b++) {
1023*0a6a1f1dSLionel Sambuc // first, instance methods
1024*0a6a1f1dSLionel Sambuc ObjCMethodList &InstMethList = b->second.first;
1025*0a6a1f1dSLionel Sambuc if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc,
1026*0a6a1f1dSLionel Sambuc Method, InstMethList))
1027*0a6a1f1dSLionel Sambuc Warned = true;
1028*0a6a1f1dSLionel Sambuc
1029*0a6a1f1dSLionel Sambuc // second, class methods
1030*0a6a1f1dSLionel Sambuc ObjCMethodList &ClsMethList = b->second.second;
1031*0a6a1f1dSLionel Sambuc if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc,
1032*0a6a1f1dSLionel Sambuc Method, ClsMethList) || Warned)
1033*0a6a1f1dSLionel Sambuc return;
1034*0a6a1f1dSLionel Sambuc }
1035*0a6a1f1dSLionel Sambuc }
1036*0a6a1f1dSLionel Sambuc
ParseObjCSelectorExpression(Selector Sel,SourceLocation AtLoc,SourceLocation SelLoc,SourceLocation LParenLoc,SourceLocation RParenLoc,bool WarnMultipleSelectors)1037f4a2713aSLionel Sambuc ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
1038f4a2713aSLionel Sambuc SourceLocation AtLoc,
1039f4a2713aSLionel Sambuc SourceLocation SelLoc,
1040f4a2713aSLionel Sambuc SourceLocation LParenLoc,
1041*0a6a1f1dSLionel Sambuc SourceLocation RParenLoc,
1042*0a6a1f1dSLionel Sambuc bool WarnMultipleSelectors) {
1043f4a2713aSLionel Sambuc ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
1044f4a2713aSLionel Sambuc SourceRange(LParenLoc, RParenLoc), false, false);
1045f4a2713aSLionel Sambuc if (!Method)
1046f4a2713aSLionel Sambuc Method = LookupFactoryMethodInGlobalPool(Sel,
1047f4a2713aSLionel Sambuc SourceRange(LParenLoc, RParenLoc));
1048f4a2713aSLionel Sambuc if (!Method) {
1049f4a2713aSLionel Sambuc if (const ObjCMethodDecl *OM = SelectorsForTypoCorrection(Sel)) {
1050f4a2713aSLionel Sambuc Selector MatchedSel = OM->getSelector();
1051f4a2713aSLionel Sambuc SourceRange SelectorRange(LParenLoc.getLocWithOffset(1),
1052f4a2713aSLionel Sambuc RParenLoc.getLocWithOffset(-1));
1053f4a2713aSLionel Sambuc Diag(SelLoc, diag::warn_undeclared_selector_with_typo)
1054f4a2713aSLionel Sambuc << Sel << MatchedSel
1055f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString());
1056f4a2713aSLionel Sambuc
1057f4a2713aSLionel Sambuc } else
1058f4a2713aSLionel Sambuc Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
1059*0a6a1f1dSLionel Sambuc } else
1060*0a6a1f1dSLionel Sambuc DiagnoseMismatchedSelectors(*this, AtLoc, Method, LParenLoc, RParenLoc,
1061*0a6a1f1dSLionel Sambuc WarnMultipleSelectors);
1062f4a2713aSLionel Sambuc
1063*0a6a1f1dSLionel Sambuc if (Method &&
1064*0a6a1f1dSLionel Sambuc Method->getImplementationControl() != ObjCMethodDecl::Optional &&
1065*0a6a1f1dSLionel Sambuc !getSourceManager().isInSystemHeader(Method->getLocation())) {
1066f4a2713aSLionel Sambuc llvm::DenseMap<Selector, SourceLocation>::iterator Pos
1067f4a2713aSLionel Sambuc = ReferencedSelectors.find(Sel);
1068f4a2713aSLionel Sambuc if (Pos == ReferencedSelectors.end())
1069f4a2713aSLionel Sambuc ReferencedSelectors.insert(std::make_pair(Sel, AtLoc));
1070f4a2713aSLionel Sambuc }
1071f4a2713aSLionel Sambuc
1072f4a2713aSLionel Sambuc // In ARC, forbid the user from using @selector for
1073f4a2713aSLionel Sambuc // retain/release/autorelease/dealloc/retainCount.
1074f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount) {
1075f4a2713aSLionel Sambuc switch (Sel.getMethodFamily()) {
1076f4a2713aSLionel Sambuc case OMF_retain:
1077f4a2713aSLionel Sambuc case OMF_release:
1078f4a2713aSLionel Sambuc case OMF_autorelease:
1079f4a2713aSLionel Sambuc case OMF_retainCount:
1080f4a2713aSLionel Sambuc case OMF_dealloc:
1081f4a2713aSLionel Sambuc Diag(AtLoc, diag::err_arc_illegal_selector) <<
1082f4a2713aSLionel Sambuc Sel << SourceRange(LParenLoc, RParenLoc);
1083f4a2713aSLionel Sambuc break;
1084f4a2713aSLionel Sambuc
1085f4a2713aSLionel Sambuc case OMF_None:
1086f4a2713aSLionel Sambuc case OMF_alloc:
1087f4a2713aSLionel Sambuc case OMF_copy:
1088f4a2713aSLionel Sambuc case OMF_finalize:
1089f4a2713aSLionel Sambuc case OMF_init:
1090f4a2713aSLionel Sambuc case OMF_mutableCopy:
1091f4a2713aSLionel Sambuc case OMF_new:
1092f4a2713aSLionel Sambuc case OMF_self:
1093*0a6a1f1dSLionel Sambuc case OMF_initialize:
1094f4a2713aSLionel Sambuc case OMF_performSelector:
1095f4a2713aSLionel Sambuc break;
1096f4a2713aSLionel Sambuc }
1097f4a2713aSLionel Sambuc }
1098f4a2713aSLionel Sambuc QualType Ty = Context.getObjCSelType();
1099f4a2713aSLionel Sambuc return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
1100f4a2713aSLionel Sambuc }
1101f4a2713aSLionel Sambuc
ParseObjCProtocolExpression(IdentifierInfo * ProtocolId,SourceLocation AtLoc,SourceLocation ProtoLoc,SourceLocation LParenLoc,SourceLocation ProtoIdLoc,SourceLocation RParenLoc)1102f4a2713aSLionel Sambuc ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
1103f4a2713aSLionel Sambuc SourceLocation AtLoc,
1104f4a2713aSLionel Sambuc SourceLocation ProtoLoc,
1105f4a2713aSLionel Sambuc SourceLocation LParenLoc,
1106f4a2713aSLionel Sambuc SourceLocation ProtoIdLoc,
1107f4a2713aSLionel Sambuc SourceLocation RParenLoc) {
1108f4a2713aSLionel Sambuc ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoIdLoc);
1109f4a2713aSLionel Sambuc if (!PDecl) {
1110f4a2713aSLionel Sambuc Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
1111f4a2713aSLionel Sambuc return true;
1112f4a2713aSLionel Sambuc }
1113*0a6a1f1dSLionel Sambuc if (PDecl->hasDefinition())
1114*0a6a1f1dSLionel Sambuc PDecl = PDecl->getDefinition();
1115f4a2713aSLionel Sambuc
1116f4a2713aSLionel Sambuc QualType Ty = Context.getObjCProtoType();
1117f4a2713aSLionel Sambuc if (Ty.isNull())
1118f4a2713aSLionel Sambuc return true;
1119f4a2713aSLionel Sambuc Ty = Context.getObjCObjectPointerType(Ty);
1120f4a2713aSLionel Sambuc return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, ProtoIdLoc, RParenLoc);
1121f4a2713aSLionel Sambuc }
1122f4a2713aSLionel Sambuc
1123f4a2713aSLionel Sambuc /// Try to capture an implicit reference to 'self'.
tryCaptureObjCSelf(SourceLocation Loc)1124f4a2713aSLionel Sambuc ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) {
1125f4a2713aSLionel Sambuc DeclContext *DC = getFunctionLevelDeclContext();
1126f4a2713aSLionel Sambuc
1127f4a2713aSLionel Sambuc // If we're not in an ObjC method, error out. Note that, unlike the
1128f4a2713aSLionel Sambuc // C++ case, we don't require an instance method --- class methods
1129f4a2713aSLionel Sambuc // still have a 'self', and we really do still need to capture it!
1130f4a2713aSLionel Sambuc ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
1131f4a2713aSLionel Sambuc if (!method)
1132*0a6a1f1dSLionel Sambuc return nullptr;
1133f4a2713aSLionel Sambuc
1134f4a2713aSLionel Sambuc tryCaptureVariable(method->getSelfDecl(), Loc);
1135f4a2713aSLionel Sambuc
1136f4a2713aSLionel Sambuc return method;
1137f4a2713aSLionel Sambuc }
1138f4a2713aSLionel Sambuc
stripObjCInstanceType(ASTContext & Context,QualType T)1139f4a2713aSLionel Sambuc static QualType stripObjCInstanceType(ASTContext &Context, QualType T) {
1140f4a2713aSLionel Sambuc if (T == Context.getObjCInstanceType())
1141f4a2713aSLionel Sambuc return Context.getObjCIdType();
1142f4a2713aSLionel Sambuc
1143f4a2713aSLionel Sambuc return T;
1144f4a2713aSLionel Sambuc }
1145f4a2713aSLionel Sambuc
getMessageSendResultType(QualType ReceiverType,ObjCMethodDecl * Method,bool isClassMessage,bool isSuperMessage)1146f4a2713aSLionel Sambuc QualType Sema::getMessageSendResultType(QualType ReceiverType,
1147f4a2713aSLionel Sambuc ObjCMethodDecl *Method,
1148f4a2713aSLionel Sambuc bool isClassMessage, bool isSuperMessage) {
1149f4a2713aSLionel Sambuc assert(Method && "Must have a method");
1150f4a2713aSLionel Sambuc if (!Method->hasRelatedResultType())
1151f4a2713aSLionel Sambuc return Method->getSendResultType();
1152f4a2713aSLionel Sambuc
1153f4a2713aSLionel Sambuc // If a method has a related return type:
1154f4a2713aSLionel Sambuc // - if the method found is an instance method, but the message send
1155f4a2713aSLionel Sambuc // was a class message send, T is the declared return type of the method
1156f4a2713aSLionel Sambuc // found
1157f4a2713aSLionel Sambuc if (Method->isInstanceMethod() && isClassMessage)
1158f4a2713aSLionel Sambuc return stripObjCInstanceType(Context, Method->getSendResultType());
1159f4a2713aSLionel Sambuc
1160f4a2713aSLionel Sambuc // - if the receiver is super, T is a pointer to the class of the
1161f4a2713aSLionel Sambuc // enclosing method definition
1162f4a2713aSLionel Sambuc if (isSuperMessage) {
1163f4a2713aSLionel Sambuc if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
1164f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface())
1165f4a2713aSLionel Sambuc return Context.getObjCObjectPointerType(
1166f4a2713aSLionel Sambuc Context.getObjCInterfaceType(Class));
1167f4a2713aSLionel Sambuc }
1168f4a2713aSLionel Sambuc
1169f4a2713aSLionel Sambuc // - if the receiver is the name of a class U, T is a pointer to U
1170f4a2713aSLionel Sambuc if (ReceiverType->getAs<ObjCInterfaceType>() ||
1171f4a2713aSLionel Sambuc ReceiverType->isObjCQualifiedInterfaceType())
1172f4a2713aSLionel Sambuc return Context.getObjCObjectPointerType(ReceiverType);
1173f4a2713aSLionel Sambuc // - if the receiver is of type Class or qualified Class type,
1174f4a2713aSLionel Sambuc // T is the declared return type of the method.
1175f4a2713aSLionel Sambuc if (ReceiverType->isObjCClassType() ||
1176f4a2713aSLionel Sambuc ReceiverType->isObjCQualifiedClassType())
1177f4a2713aSLionel Sambuc return stripObjCInstanceType(Context, Method->getSendResultType());
1178f4a2713aSLionel Sambuc
1179f4a2713aSLionel Sambuc // - if the receiver is id, qualified id, Class, or qualified Class, T
1180f4a2713aSLionel Sambuc // is the receiver type, otherwise
1181f4a2713aSLionel Sambuc // - T is the type of the receiver expression.
1182f4a2713aSLionel Sambuc return ReceiverType;
1183f4a2713aSLionel Sambuc }
1184f4a2713aSLionel Sambuc
1185f4a2713aSLionel Sambuc /// Look for an ObjC method whose result type exactly matches the given type.
1186f4a2713aSLionel Sambuc static const ObjCMethodDecl *
findExplicitInstancetypeDeclarer(const ObjCMethodDecl * MD,QualType instancetype)1187f4a2713aSLionel Sambuc findExplicitInstancetypeDeclarer(const ObjCMethodDecl *MD,
1188f4a2713aSLionel Sambuc QualType instancetype) {
1189*0a6a1f1dSLionel Sambuc if (MD->getReturnType() == instancetype)
1190*0a6a1f1dSLionel Sambuc return MD;
1191f4a2713aSLionel Sambuc
1192f4a2713aSLionel Sambuc // For these purposes, a method in an @implementation overrides a
1193f4a2713aSLionel Sambuc // declaration in the @interface.
1194f4a2713aSLionel Sambuc if (const ObjCImplDecl *impl =
1195f4a2713aSLionel Sambuc dyn_cast<ObjCImplDecl>(MD->getDeclContext())) {
1196f4a2713aSLionel Sambuc const ObjCContainerDecl *iface;
1197f4a2713aSLionel Sambuc if (const ObjCCategoryImplDecl *catImpl =
1198f4a2713aSLionel Sambuc dyn_cast<ObjCCategoryImplDecl>(impl)) {
1199f4a2713aSLionel Sambuc iface = catImpl->getCategoryDecl();
1200f4a2713aSLionel Sambuc } else {
1201f4a2713aSLionel Sambuc iface = impl->getClassInterface();
1202f4a2713aSLionel Sambuc }
1203f4a2713aSLionel Sambuc
1204f4a2713aSLionel Sambuc const ObjCMethodDecl *ifaceMD =
1205f4a2713aSLionel Sambuc iface->getMethod(MD->getSelector(), MD->isInstanceMethod());
1206f4a2713aSLionel Sambuc if (ifaceMD) return findExplicitInstancetypeDeclarer(ifaceMD, instancetype);
1207f4a2713aSLionel Sambuc }
1208f4a2713aSLionel Sambuc
1209f4a2713aSLionel Sambuc SmallVector<const ObjCMethodDecl *, 4> overrides;
1210f4a2713aSLionel Sambuc MD->getOverriddenMethods(overrides);
1211f4a2713aSLionel Sambuc for (unsigned i = 0, e = overrides.size(); i != e; ++i) {
1212f4a2713aSLionel Sambuc if (const ObjCMethodDecl *result =
1213f4a2713aSLionel Sambuc findExplicitInstancetypeDeclarer(overrides[i], instancetype))
1214f4a2713aSLionel Sambuc return result;
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc
1217*0a6a1f1dSLionel Sambuc return nullptr;
1218f4a2713aSLionel Sambuc }
1219f4a2713aSLionel Sambuc
EmitRelatedResultTypeNoteForReturn(QualType destType)1220f4a2713aSLionel Sambuc void Sema::EmitRelatedResultTypeNoteForReturn(QualType destType) {
1221f4a2713aSLionel Sambuc // Only complain if we're in an ObjC method and the required return
1222f4a2713aSLionel Sambuc // type doesn't match the method's declared return type.
1223f4a2713aSLionel Sambuc ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurContext);
1224f4a2713aSLionel Sambuc if (!MD || !MD->hasRelatedResultType() ||
1225*0a6a1f1dSLionel Sambuc Context.hasSameUnqualifiedType(destType, MD->getReturnType()))
1226f4a2713aSLionel Sambuc return;
1227f4a2713aSLionel Sambuc
1228f4a2713aSLionel Sambuc // Look for a method overridden by this method which explicitly uses
1229f4a2713aSLionel Sambuc // 'instancetype'.
1230f4a2713aSLionel Sambuc if (const ObjCMethodDecl *overridden =
1231f4a2713aSLionel Sambuc findExplicitInstancetypeDeclarer(MD, Context.getObjCInstanceType())) {
1232*0a6a1f1dSLionel Sambuc SourceRange range = overridden->getReturnTypeSourceRange();
1233*0a6a1f1dSLionel Sambuc SourceLocation loc = range.getBegin();
1234f4a2713aSLionel Sambuc if (loc.isInvalid())
1235f4a2713aSLionel Sambuc loc = overridden->getLocation();
1236f4a2713aSLionel Sambuc Diag(loc, diag::note_related_result_type_explicit)
1237f4a2713aSLionel Sambuc << /*current method*/ 1 << range;
1238f4a2713aSLionel Sambuc return;
1239f4a2713aSLionel Sambuc }
1240f4a2713aSLionel Sambuc
1241f4a2713aSLionel Sambuc // Otherwise, if we have an interesting method family, note that.
1242f4a2713aSLionel Sambuc // This should always trigger if the above didn't.
1243f4a2713aSLionel Sambuc if (ObjCMethodFamily family = MD->getMethodFamily())
1244f4a2713aSLionel Sambuc Diag(MD->getLocation(), diag::note_related_result_type_family)
1245f4a2713aSLionel Sambuc << /*current method*/ 1
1246f4a2713aSLionel Sambuc << family;
1247f4a2713aSLionel Sambuc }
1248f4a2713aSLionel Sambuc
EmitRelatedResultTypeNote(const Expr * E)1249f4a2713aSLionel Sambuc void Sema::EmitRelatedResultTypeNote(const Expr *E) {
1250f4a2713aSLionel Sambuc E = E->IgnoreParenImpCasts();
1251f4a2713aSLionel Sambuc const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E);
1252f4a2713aSLionel Sambuc if (!MsgSend)
1253f4a2713aSLionel Sambuc return;
1254f4a2713aSLionel Sambuc
1255f4a2713aSLionel Sambuc const ObjCMethodDecl *Method = MsgSend->getMethodDecl();
1256f4a2713aSLionel Sambuc if (!Method)
1257f4a2713aSLionel Sambuc return;
1258f4a2713aSLionel Sambuc
1259f4a2713aSLionel Sambuc if (!Method->hasRelatedResultType())
1260f4a2713aSLionel Sambuc return;
1261f4a2713aSLionel Sambuc
1262*0a6a1f1dSLionel Sambuc if (Context.hasSameUnqualifiedType(
1263*0a6a1f1dSLionel Sambuc Method->getReturnType().getNonReferenceType(), MsgSend->getType()))
1264f4a2713aSLionel Sambuc return;
1265f4a2713aSLionel Sambuc
1266*0a6a1f1dSLionel Sambuc if (!Context.hasSameUnqualifiedType(Method->getReturnType(),
1267f4a2713aSLionel Sambuc Context.getObjCInstanceType()))
1268f4a2713aSLionel Sambuc return;
1269f4a2713aSLionel Sambuc
1270f4a2713aSLionel Sambuc Diag(Method->getLocation(), diag::note_related_result_type_inferred)
1271f4a2713aSLionel Sambuc << Method->isInstanceMethod() << Method->getSelector()
1272f4a2713aSLionel Sambuc << MsgSend->getType();
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc
CheckMessageArgumentTypes(QualType ReceiverType,MultiExprArg Args,Selector Sel,ArrayRef<SourceLocation> SelectorLocs,ObjCMethodDecl * Method,bool isClassMessage,bool isSuperMessage,SourceLocation lbrac,SourceLocation rbrac,SourceRange RecRange,QualType & ReturnType,ExprValueKind & VK)1275f4a2713aSLionel Sambuc bool Sema::CheckMessageArgumentTypes(QualType ReceiverType,
1276f4a2713aSLionel Sambuc MultiExprArg Args,
1277f4a2713aSLionel Sambuc Selector Sel,
1278f4a2713aSLionel Sambuc ArrayRef<SourceLocation> SelectorLocs,
1279f4a2713aSLionel Sambuc ObjCMethodDecl *Method,
1280f4a2713aSLionel Sambuc bool isClassMessage, bool isSuperMessage,
1281f4a2713aSLionel Sambuc SourceLocation lbrac, SourceLocation rbrac,
1282*0a6a1f1dSLionel Sambuc SourceRange RecRange,
1283f4a2713aSLionel Sambuc QualType &ReturnType, ExprValueKind &VK) {
1284f4a2713aSLionel Sambuc SourceLocation SelLoc;
1285f4a2713aSLionel Sambuc if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
1286f4a2713aSLionel Sambuc SelLoc = SelectorLocs.front();
1287f4a2713aSLionel Sambuc else
1288f4a2713aSLionel Sambuc SelLoc = lbrac;
1289f4a2713aSLionel Sambuc
1290f4a2713aSLionel Sambuc if (!Method) {
1291f4a2713aSLionel Sambuc // Apply default argument promotion as for (C99 6.5.2.2p6).
1292f4a2713aSLionel Sambuc for (unsigned i = 0, e = Args.size(); i != e; i++) {
1293f4a2713aSLionel Sambuc if (Args[i]->isTypeDependent())
1294f4a2713aSLionel Sambuc continue;
1295f4a2713aSLionel Sambuc
1296f4a2713aSLionel Sambuc ExprResult result;
1297f4a2713aSLionel Sambuc if (getLangOpts().DebuggerSupport) {
1298f4a2713aSLionel Sambuc QualType paramTy; // ignored
1299f4a2713aSLionel Sambuc result = checkUnknownAnyArg(SelLoc, Args[i], paramTy);
1300f4a2713aSLionel Sambuc } else {
1301f4a2713aSLionel Sambuc result = DefaultArgumentPromotion(Args[i]);
1302f4a2713aSLionel Sambuc }
1303f4a2713aSLionel Sambuc if (result.isInvalid())
1304f4a2713aSLionel Sambuc return true;
1305*0a6a1f1dSLionel Sambuc Args[i] = result.get();
1306f4a2713aSLionel Sambuc }
1307f4a2713aSLionel Sambuc
1308f4a2713aSLionel Sambuc unsigned DiagID;
1309f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount)
1310f4a2713aSLionel Sambuc DiagID = diag::err_arc_method_not_found;
1311f4a2713aSLionel Sambuc else
1312f4a2713aSLionel Sambuc DiagID = isClassMessage ? diag::warn_class_method_not_found
1313f4a2713aSLionel Sambuc : diag::warn_inst_method_not_found;
1314f4a2713aSLionel Sambuc if (!getLangOpts().DebuggerSupport) {
1315f4a2713aSLionel Sambuc const ObjCMethodDecl *OMD = SelectorsForTypoCorrection(Sel, ReceiverType);
1316f4a2713aSLionel Sambuc if (OMD && !OMD->isInvalidDecl()) {
1317f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount)
1318f4a2713aSLionel Sambuc DiagID = diag::error_method_not_found_with_typo;
1319f4a2713aSLionel Sambuc else
1320f4a2713aSLionel Sambuc DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo
1321f4a2713aSLionel Sambuc : diag::warn_instance_method_not_found_with_typo;
1322f4a2713aSLionel Sambuc Selector MatchedSel = OMD->getSelector();
1323f4a2713aSLionel Sambuc SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back());
1324*0a6a1f1dSLionel Sambuc if (MatchedSel.isUnarySelector())
1325f4a2713aSLionel Sambuc Diag(SelLoc, DiagID)
1326f4a2713aSLionel Sambuc << Sel<< isClassMessage << MatchedSel
1327f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString());
1328*0a6a1f1dSLionel Sambuc else
1329*0a6a1f1dSLionel Sambuc Diag(SelLoc, DiagID) << Sel<< isClassMessage << MatchedSel;
1330f4a2713aSLionel Sambuc }
1331f4a2713aSLionel Sambuc else
1332f4a2713aSLionel Sambuc Diag(SelLoc, DiagID)
1333f4a2713aSLionel Sambuc << Sel << isClassMessage << SourceRange(SelectorLocs.front(),
1334f4a2713aSLionel Sambuc SelectorLocs.back());
1335f4a2713aSLionel Sambuc // Find the class to which we are sending this message.
1336f4a2713aSLionel Sambuc if (ReceiverType->isObjCObjectPointerType()) {
1337*0a6a1f1dSLionel Sambuc if (ObjCInterfaceDecl *ThisClass =
1338*0a6a1f1dSLionel Sambuc ReceiverType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()) {
1339*0a6a1f1dSLionel Sambuc Diag(ThisClass->getLocation(), diag::note_receiver_class_declared);
1340*0a6a1f1dSLionel Sambuc if (!RecRange.isInvalid())
1341*0a6a1f1dSLionel Sambuc if (ThisClass->lookupClassMethod(Sel))
1342*0a6a1f1dSLionel Sambuc Diag(RecRange.getBegin(),diag::note_receiver_expr_here)
1343*0a6a1f1dSLionel Sambuc << FixItHint::CreateReplacement(RecRange,
1344*0a6a1f1dSLionel Sambuc ThisClass->getNameAsString());
1345*0a6a1f1dSLionel Sambuc }
1346f4a2713aSLionel Sambuc }
1347f4a2713aSLionel Sambuc }
1348f4a2713aSLionel Sambuc
1349f4a2713aSLionel Sambuc // In debuggers, we want to use __unknown_anytype for these
1350f4a2713aSLionel Sambuc // results so that clients can cast them.
1351f4a2713aSLionel Sambuc if (getLangOpts().DebuggerSupport) {
1352f4a2713aSLionel Sambuc ReturnType = Context.UnknownAnyTy;
1353f4a2713aSLionel Sambuc } else {
1354f4a2713aSLionel Sambuc ReturnType = Context.getObjCIdType();
1355f4a2713aSLionel Sambuc }
1356f4a2713aSLionel Sambuc VK = VK_RValue;
1357f4a2713aSLionel Sambuc return false;
1358f4a2713aSLionel Sambuc }
1359f4a2713aSLionel Sambuc
1360f4a2713aSLionel Sambuc ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage,
1361f4a2713aSLionel Sambuc isSuperMessage);
1362*0a6a1f1dSLionel Sambuc VK = Expr::getValueKindForType(Method->getReturnType());
1363f4a2713aSLionel Sambuc
1364f4a2713aSLionel Sambuc unsigned NumNamedArgs = Sel.getNumArgs();
1365f4a2713aSLionel Sambuc // Method might have more arguments than selector indicates. This is due
1366f4a2713aSLionel Sambuc // to addition of c-style arguments in method.
1367f4a2713aSLionel Sambuc if (Method->param_size() > Sel.getNumArgs())
1368f4a2713aSLionel Sambuc NumNamedArgs = Method->param_size();
1369f4a2713aSLionel Sambuc // FIXME. This need be cleaned up.
1370f4a2713aSLionel Sambuc if (Args.size() < NumNamedArgs) {
1371f4a2713aSLionel Sambuc Diag(SelLoc, diag::err_typecheck_call_too_few_args)
1372f4a2713aSLionel Sambuc << 2 << NumNamedArgs << static_cast<unsigned>(Args.size());
1373f4a2713aSLionel Sambuc return false;
1374f4a2713aSLionel Sambuc }
1375f4a2713aSLionel Sambuc
1376f4a2713aSLionel Sambuc bool IsError = false;
1377f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumNamedArgs; i++) {
1378f4a2713aSLionel Sambuc // We can't do any type-checking on a type-dependent argument.
1379f4a2713aSLionel Sambuc if (Args[i]->isTypeDependent())
1380f4a2713aSLionel Sambuc continue;
1381f4a2713aSLionel Sambuc
1382f4a2713aSLionel Sambuc Expr *argExpr = Args[i];
1383f4a2713aSLionel Sambuc
1384*0a6a1f1dSLionel Sambuc ParmVarDecl *param = Method->parameters()[i];
1385f4a2713aSLionel Sambuc assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
1386f4a2713aSLionel Sambuc
1387f4a2713aSLionel Sambuc // Strip the unbridged-cast placeholder expression off unless it's
1388f4a2713aSLionel Sambuc // a consumed argument.
1389f4a2713aSLionel Sambuc if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) &&
1390f4a2713aSLionel Sambuc !param->hasAttr<CFConsumedAttr>())
1391f4a2713aSLionel Sambuc argExpr = stripARCUnbridgedCast(argExpr);
1392f4a2713aSLionel Sambuc
1393f4a2713aSLionel Sambuc // If the parameter is __unknown_anytype, infer its type
1394f4a2713aSLionel Sambuc // from the argument.
1395f4a2713aSLionel Sambuc if (param->getType() == Context.UnknownAnyTy) {
1396f4a2713aSLionel Sambuc QualType paramType;
1397f4a2713aSLionel Sambuc ExprResult argE = checkUnknownAnyArg(SelLoc, argExpr, paramType);
1398f4a2713aSLionel Sambuc if (argE.isInvalid()) {
1399f4a2713aSLionel Sambuc IsError = true;
1400f4a2713aSLionel Sambuc } else {
1401*0a6a1f1dSLionel Sambuc Args[i] = argE.get();
1402f4a2713aSLionel Sambuc
1403f4a2713aSLionel Sambuc // Update the parameter type in-place.
1404f4a2713aSLionel Sambuc param->setType(paramType);
1405f4a2713aSLionel Sambuc }
1406f4a2713aSLionel Sambuc continue;
1407f4a2713aSLionel Sambuc }
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
1410f4a2713aSLionel Sambuc param->getType(),
1411f4a2713aSLionel Sambuc diag::err_call_incomplete_argument, argExpr))
1412f4a2713aSLionel Sambuc return true;
1413f4a2713aSLionel Sambuc
1414f4a2713aSLionel Sambuc InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
1415f4a2713aSLionel Sambuc param);
1416*0a6a1f1dSLionel Sambuc ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), argExpr);
1417f4a2713aSLionel Sambuc if (ArgE.isInvalid())
1418f4a2713aSLionel Sambuc IsError = true;
1419f4a2713aSLionel Sambuc else
1420*0a6a1f1dSLionel Sambuc Args[i] = ArgE.getAs<Expr>();
1421f4a2713aSLionel Sambuc }
1422f4a2713aSLionel Sambuc
1423f4a2713aSLionel Sambuc // Promote additional arguments to variadic methods.
1424f4a2713aSLionel Sambuc if (Method->isVariadic()) {
1425f4a2713aSLionel Sambuc for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) {
1426f4a2713aSLionel Sambuc if (Args[i]->isTypeDependent())
1427f4a2713aSLionel Sambuc continue;
1428f4a2713aSLionel Sambuc
1429f4a2713aSLionel Sambuc ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod,
1430*0a6a1f1dSLionel Sambuc nullptr);
1431f4a2713aSLionel Sambuc IsError |= Arg.isInvalid();
1432*0a6a1f1dSLionel Sambuc Args[i] = Arg.get();
1433f4a2713aSLionel Sambuc }
1434f4a2713aSLionel Sambuc } else {
1435f4a2713aSLionel Sambuc // Check for extra arguments to non-variadic methods.
1436f4a2713aSLionel Sambuc if (Args.size() != NumNamedArgs) {
1437f4a2713aSLionel Sambuc Diag(Args[NumNamedArgs]->getLocStart(),
1438f4a2713aSLionel Sambuc diag::err_typecheck_call_too_many_args)
1439f4a2713aSLionel Sambuc << 2 /*method*/ << NumNamedArgs << static_cast<unsigned>(Args.size())
1440f4a2713aSLionel Sambuc << Method->getSourceRange()
1441f4a2713aSLionel Sambuc << SourceRange(Args[NumNamedArgs]->getLocStart(),
1442f4a2713aSLionel Sambuc Args.back()->getLocEnd());
1443f4a2713aSLionel Sambuc }
1444f4a2713aSLionel Sambuc }
1445f4a2713aSLionel Sambuc
1446f4a2713aSLionel Sambuc DiagnoseSentinelCalls(Method, SelLoc, Args);
1447f4a2713aSLionel Sambuc
1448f4a2713aSLionel Sambuc // Do additional checkings on method.
1449f4a2713aSLionel Sambuc IsError |= CheckObjCMethodCall(
1450*0a6a1f1dSLionel Sambuc Method, SelLoc, makeArrayRef(Args.data(), Args.size()));
1451f4a2713aSLionel Sambuc
1452f4a2713aSLionel Sambuc return IsError;
1453f4a2713aSLionel Sambuc }
1454f4a2713aSLionel Sambuc
isSelfExpr(Expr * RExpr)1455*0a6a1f1dSLionel Sambuc bool Sema::isSelfExpr(Expr *RExpr) {
1456f4a2713aSLionel Sambuc // 'self' is objc 'self' in an objc method only.
1457*0a6a1f1dSLionel Sambuc ObjCMethodDecl *Method =
1458f4a2713aSLionel Sambuc dyn_cast_or_null<ObjCMethodDecl>(CurContext->getNonClosureAncestor());
1459*0a6a1f1dSLionel Sambuc return isSelfExpr(RExpr, Method);
1460*0a6a1f1dSLionel Sambuc }
1461*0a6a1f1dSLionel Sambuc
isSelfExpr(Expr * receiver,const ObjCMethodDecl * method)1462*0a6a1f1dSLionel Sambuc bool Sema::isSelfExpr(Expr *receiver, const ObjCMethodDecl *method) {
1463f4a2713aSLionel Sambuc if (!method) return false;
1464f4a2713aSLionel Sambuc
1465f4a2713aSLionel Sambuc receiver = receiver->IgnoreParenLValueCasts();
1466f4a2713aSLionel Sambuc if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
1467f4a2713aSLionel Sambuc if (DRE->getDecl() == method->getSelfDecl())
1468f4a2713aSLionel Sambuc return true;
1469f4a2713aSLionel Sambuc return false;
1470f4a2713aSLionel Sambuc }
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc /// LookupMethodInType - Look up a method in an ObjCObjectType.
LookupMethodInObjectType(Selector sel,QualType type,bool isInstance)1473f4a2713aSLionel Sambuc ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type,
1474f4a2713aSLionel Sambuc bool isInstance) {
1475f4a2713aSLionel Sambuc const ObjCObjectType *objType = type->castAs<ObjCObjectType>();
1476f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *iface = objType->getInterface()) {
1477f4a2713aSLionel Sambuc // Look it up in the main interface (and categories, etc.)
1478f4a2713aSLionel Sambuc if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance))
1479f4a2713aSLionel Sambuc return method;
1480f4a2713aSLionel Sambuc
1481f4a2713aSLionel Sambuc // Okay, look for "private" methods declared in any
1482f4a2713aSLionel Sambuc // @implementations we've seen.
1483f4a2713aSLionel Sambuc if (ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance))
1484f4a2713aSLionel Sambuc return method;
1485f4a2713aSLionel Sambuc }
1486f4a2713aSLionel Sambuc
1487f4a2713aSLionel Sambuc // Check qualifiers.
1488*0a6a1f1dSLionel Sambuc for (const auto *I : objType->quals())
1489*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *method = I->lookupMethod(sel, isInstance))
1490f4a2713aSLionel Sambuc return method;
1491f4a2713aSLionel Sambuc
1492*0a6a1f1dSLionel Sambuc return nullptr;
1493f4a2713aSLionel Sambuc }
1494f4a2713aSLionel Sambuc
1495f4a2713aSLionel Sambuc /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
1496f4a2713aSLionel Sambuc /// list of a qualified objective pointer type.
LookupMethodInQualifiedType(Selector Sel,const ObjCObjectPointerType * OPT,bool Instance)1497f4a2713aSLionel Sambuc ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
1498f4a2713aSLionel Sambuc const ObjCObjectPointerType *OPT,
1499f4a2713aSLionel Sambuc bool Instance)
1500f4a2713aSLionel Sambuc {
1501*0a6a1f1dSLionel Sambuc ObjCMethodDecl *MD = nullptr;
1502*0a6a1f1dSLionel Sambuc for (const auto *PROTO : OPT->quals()) {
1503f4a2713aSLionel Sambuc if ((MD = PROTO->lookupMethod(Sel, Instance))) {
1504f4a2713aSLionel Sambuc return MD;
1505f4a2713aSLionel Sambuc }
1506f4a2713aSLionel Sambuc }
1507*0a6a1f1dSLionel Sambuc return nullptr;
1508f4a2713aSLionel Sambuc }
1509f4a2713aSLionel Sambuc
DiagnoseARCUseOfWeakReceiver(Sema & S,Expr * Receiver)1510f4a2713aSLionel Sambuc static void DiagnoseARCUseOfWeakReceiver(Sema &S, Expr *Receiver) {
1511f4a2713aSLionel Sambuc if (!Receiver)
1512f4a2713aSLionel Sambuc return;
1513f4a2713aSLionel Sambuc
1514f4a2713aSLionel Sambuc if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Receiver))
1515f4a2713aSLionel Sambuc Receiver = OVE->getSourceExpr();
1516f4a2713aSLionel Sambuc
1517f4a2713aSLionel Sambuc Expr *RExpr = Receiver->IgnoreParenImpCasts();
1518f4a2713aSLionel Sambuc SourceLocation Loc = RExpr->getLocStart();
1519f4a2713aSLionel Sambuc QualType T = RExpr->getType();
1520*0a6a1f1dSLionel Sambuc const ObjCPropertyDecl *PDecl = nullptr;
1521*0a6a1f1dSLionel Sambuc const ObjCMethodDecl *GDecl = nullptr;
1522f4a2713aSLionel Sambuc if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(RExpr)) {
1523f4a2713aSLionel Sambuc RExpr = POE->getSyntacticForm();
1524f4a2713aSLionel Sambuc if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(RExpr)) {
1525f4a2713aSLionel Sambuc if (PRE->isImplicitProperty()) {
1526f4a2713aSLionel Sambuc GDecl = PRE->getImplicitPropertyGetter();
1527f4a2713aSLionel Sambuc if (GDecl) {
1528*0a6a1f1dSLionel Sambuc T = GDecl->getReturnType();
1529f4a2713aSLionel Sambuc }
1530f4a2713aSLionel Sambuc }
1531f4a2713aSLionel Sambuc else {
1532f4a2713aSLionel Sambuc PDecl = PRE->getExplicitProperty();
1533f4a2713aSLionel Sambuc if (PDecl) {
1534f4a2713aSLionel Sambuc T = PDecl->getType();
1535f4a2713aSLionel Sambuc }
1536f4a2713aSLionel Sambuc }
1537f4a2713aSLionel Sambuc }
1538f4a2713aSLionel Sambuc }
1539f4a2713aSLionel Sambuc else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RExpr)) {
1540f4a2713aSLionel Sambuc // See if receiver is a method which envokes a synthesized getter
1541f4a2713aSLionel Sambuc // backing a 'weak' property.
1542f4a2713aSLionel Sambuc ObjCMethodDecl *Method = ME->getMethodDecl();
1543f4a2713aSLionel Sambuc if (Method && Method->getSelector().getNumArgs() == 0) {
1544f4a2713aSLionel Sambuc PDecl = Method->findPropertyDecl();
1545f4a2713aSLionel Sambuc if (PDecl)
1546f4a2713aSLionel Sambuc T = PDecl->getType();
1547f4a2713aSLionel Sambuc }
1548f4a2713aSLionel Sambuc }
1549f4a2713aSLionel Sambuc
1550f4a2713aSLionel Sambuc if (T.getObjCLifetime() != Qualifiers::OCL_Weak) {
1551f4a2713aSLionel Sambuc if (!PDecl)
1552f4a2713aSLionel Sambuc return;
1553f4a2713aSLionel Sambuc if (!(PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak))
1554f4a2713aSLionel Sambuc return;
1555f4a2713aSLionel Sambuc }
1556f4a2713aSLionel Sambuc
1557f4a2713aSLionel Sambuc S.Diag(Loc, diag::warn_receiver_is_weak)
1558f4a2713aSLionel Sambuc << ((!PDecl && !GDecl) ? 0 : (PDecl ? 1 : 2));
1559f4a2713aSLionel Sambuc
1560f4a2713aSLionel Sambuc if (PDecl)
1561f4a2713aSLionel Sambuc S.Diag(PDecl->getLocation(), diag::note_property_declare);
1562f4a2713aSLionel Sambuc else if (GDecl)
1563f4a2713aSLionel Sambuc S.Diag(GDecl->getLocation(), diag::note_method_declared_at) << GDecl;
1564f4a2713aSLionel Sambuc
1565f4a2713aSLionel Sambuc S.Diag(Loc, diag::note_arc_assign_to_strong);
1566f4a2713aSLionel Sambuc }
1567f4a2713aSLionel Sambuc
1568f4a2713aSLionel Sambuc /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
1569f4a2713aSLionel Sambuc /// objective C interface. This is a property reference expression.
1570f4a2713aSLionel Sambuc ExprResult Sema::
HandleExprPropertyRefExpr(const ObjCObjectPointerType * OPT,Expr * BaseExpr,SourceLocation OpLoc,DeclarationName MemberName,SourceLocation MemberLoc,SourceLocation SuperLoc,QualType SuperType,bool Super)1571f4a2713aSLionel Sambuc HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
1572f4a2713aSLionel Sambuc Expr *BaseExpr, SourceLocation OpLoc,
1573f4a2713aSLionel Sambuc DeclarationName MemberName,
1574f4a2713aSLionel Sambuc SourceLocation MemberLoc,
1575f4a2713aSLionel Sambuc SourceLocation SuperLoc, QualType SuperType,
1576f4a2713aSLionel Sambuc bool Super) {
1577f4a2713aSLionel Sambuc const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
1578f4a2713aSLionel Sambuc ObjCInterfaceDecl *IFace = IFaceT->getDecl();
1579f4a2713aSLionel Sambuc
1580f4a2713aSLionel Sambuc if (!MemberName.isIdentifier()) {
1581f4a2713aSLionel Sambuc Diag(MemberLoc, diag::err_invalid_property_name)
1582f4a2713aSLionel Sambuc << MemberName << QualType(OPT, 0);
1583f4a2713aSLionel Sambuc return ExprError();
1584f4a2713aSLionel Sambuc }
1585f4a2713aSLionel Sambuc
1586f4a2713aSLionel Sambuc IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1587f4a2713aSLionel Sambuc
1588f4a2713aSLionel Sambuc SourceRange BaseRange = Super? SourceRange(SuperLoc)
1589f4a2713aSLionel Sambuc : BaseExpr->getSourceRange();
1590f4a2713aSLionel Sambuc if (RequireCompleteType(MemberLoc, OPT->getPointeeType(),
1591f4a2713aSLionel Sambuc diag::err_property_not_found_forward_class,
1592f4a2713aSLionel Sambuc MemberName, BaseRange))
1593f4a2713aSLionel Sambuc return ExprError();
1594f4a2713aSLionel Sambuc
1595f4a2713aSLionel Sambuc // Search for a declared property first.
1596f4a2713aSLionel Sambuc if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
1597f4a2713aSLionel Sambuc // Check whether we can reference this property.
1598f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(PD, MemberLoc))
1599f4a2713aSLionel Sambuc return ExprError();
1600f4a2713aSLionel Sambuc if (Super)
1601*0a6a1f1dSLionel Sambuc return new (Context)
1602*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue,
1603*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, SuperLoc, SuperType);
1604f4a2713aSLionel Sambuc else
1605*0a6a1f1dSLionel Sambuc return new (Context)
1606*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue,
1607*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, BaseExpr);
1608f4a2713aSLionel Sambuc }
1609f4a2713aSLionel Sambuc // Check protocols on qualified interfaces.
1610*0a6a1f1dSLionel Sambuc for (const auto *I : OPT->quals())
1611*0a6a1f1dSLionel Sambuc if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration(Member)) {
1612f4a2713aSLionel Sambuc // Check whether we can reference this property.
1613f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(PD, MemberLoc))
1614f4a2713aSLionel Sambuc return ExprError();
1615f4a2713aSLionel Sambuc
1616f4a2713aSLionel Sambuc if (Super)
1617*0a6a1f1dSLionel Sambuc return new (Context) ObjCPropertyRefExpr(
1618*0a6a1f1dSLionel Sambuc PD, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty, MemberLoc,
1619*0a6a1f1dSLionel Sambuc SuperLoc, SuperType);
1620f4a2713aSLionel Sambuc else
1621*0a6a1f1dSLionel Sambuc return new (Context)
1622*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue,
1623*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, BaseExpr);
1624f4a2713aSLionel Sambuc }
1625f4a2713aSLionel Sambuc // If that failed, look for an "implicit" property by seeing if the nullary
1626f4a2713aSLionel Sambuc // selector is implemented.
1627f4a2713aSLionel Sambuc
1628f4a2713aSLionel Sambuc // FIXME: The logic for looking up nullary and unary selectors should be
1629f4a2713aSLionel Sambuc // shared with the code in ActOnInstanceMessage.
1630f4a2713aSLionel Sambuc
1631f4a2713aSLionel Sambuc Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
1632f4a2713aSLionel Sambuc ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
1633f4a2713aSLionel Sambuc
1634f4a2713aSLionel Sambuc // May be founf in property's qualified list.
1635f4a2713aSLionel Sambuc if (!Getter)
1636f4a2713aSLionel Sambuc Getter = LookupMethodInQualifiedType(Sel, OPT, true);
1637f4a2713aSLionel Sambuc
1638f4a2713aSLionel Sambuc // If this reference is in an @implementation, check for 'private' methods.
1639f4a2713aSLionel Sambuc if (!Getter)
1640f4a2713aSLionel Sambuc Getter = IFace->lookupPrivateMethod(Sel);
1641f4a2713aSLionel Sambuc
1642f4a2713aSLionel Sambuc if (Getter) {
1643f4a2713aSLionel Sambuc // Check if we can reference this property.
1644f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Getter, MemberLoc))
1645f4a2713aSLionel Sambuc return ExprError();
1646f4a2713aSLionel Sambuc }
1647f4a2713aSLionel Sambuc // If we found a getter then this may be a valid dot-reference, we
1648f4a2713aSLionel Sambuc // will look for the matching setter, in case it is needed.
1649f4a2713aSLionel Sambuc Selector SetterSel =
1650f4a2713aSLionel Sambuc SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1651f4a2713aSLionel Sambuc PP.getSelectorTable(), Member);
1652f4a2713aSLionel Sambuc ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
1653f4a2713aSLionel Sambuc
1654f4a2713aSLionel Sambuc // May be founf in property's qualified list.
1655f4a2713aSLionel Sambuc if (!Setter)
1656f4a2713aSLionel Sambuc Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
1657f4a2713aSLionel Sambuc
1658f4a2713aSLionel Sambuc if (!Setter) {
1659f4a2713aSLionel Sambuc // If this reference is in an @implementation, also check for 'private'
1660f4a2713aSLionel Sambuc // methods.
1661f4a2713aSLionel Sambuc Setter = IFace->lookupPrivateMethod(SetterSel);
1662f4a2713aSLionel Sambuc }
1663f4a2713aSLionel Sambuc
1664f4a2713aSLionel Sambuc if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
1665f4a2713aSLionel Sambuc return ExprError();
1666f4a2713aSLionel Sambuc
1667*0a6a1f1dSLionel Sambuc // Special warning if member name used in a property-dot for a setter accessor
1668*0a6a1f1dSLionel Sambuc // does not use a property with same name; e.g. obj.X = ... for a property with
1669*0a6a1f1dSLionel Sambuc // name 'x'.
1670*0a6a1f1dSLionel Sambuc if (Setter && Setter->isImplicit() && Setter->isPropertyAccessor()
1671*0a6a1f1dSLionel Sambuc && !IFace->FindPropertyDeclaration(Member)) {
1672*0a6a1f1dSLionel Sambuc if (const ObjCPropertyDecl *PDecl = Setter->findPropertyDecl()) {
1673*0a6a1f1dSLionel Sambuc // Do not warn if user is using property-dot syntax to make call to
1674*0a6a1f1dSLionel Sambuc // user named setter.
1675*0a6a1f1dSLionel Sambuc if (!(PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter))
1676*0a6a1f1dSLionel Sambuc Diag(MemberLoc,
1677*0a6a1f1dSLionel Sambuc diag::warn_property_access_suggest)
1678*0a6a1f1dSLionel Sambuc << MemberName << QualType(OPT, 0) << PDecl->getName()
1679*0a6a1f1dSLionel Sambuc << FixItHint::CreateReplacement(MemberLoc, PDecl->getName());
1680*0a6a1f1dSLionel Sambuc }
1681*0a6a1f1dSLionel Sambuc }
1682*0a6a1f1dSLionel Sambuc
1683f4a2713aSLionel Sambuc if (Getter || Setter) {
1684f4a2713aSLionel Sambuc if (Super)
1685*0a6a1f1dSLionel Sambuc return new (Context)
1686*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue,
1687*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, SuperLoc, SuperType);
1688f4a2713aSLionel Sambuc else
1689*0a6a1f1dSLionel Sambuc return new (Context)
1690*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue,
1691*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, BaseExpr);
1692f4a2713aSLionel Sambuc
1693f4a2713aSLionel Sambuc }
1694f4a2713aSLionel Sambuc
1695f4a2713aSLionel Sambuc // Attempt to correct for typos in property names.
1696*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected =
1697*0a6a1f1dSLionel Sambuc CorrectTypo(DeclarationNameInfo(MemberName, MemberLoc),
1698*0a6a1f1dSLionel Sambuc LookupOrdinaryName, nullptr, nullptr,
1699*0a6a1f1dSLionel Sambuc llvm::make_unique<DeclFilterCCC<ObjCPropertyDecl>>(),
1700*0a6a1f1dSLionel Sambuc CTK_ErrorRecovery, IFace, false, OPT)) {
1701f4a2713aSLionel Sambuc diagnoseTypo(Corrected, PDiag(diag::err_property_not_found_suggest)
1702f4a2713aSLionel Sambuc << MemberName << QualType(OPT, 0));
1703f4a2713aSLionel Sambuc DeclarationName TypoResult = Corrected.getCorrection();
1704f4a2713aSLionel Sambuc return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc,
1705f4a2713aSLionel Sambuc TypoResult, MemberLoc,
1706f4a2713aSLionel Sambuc SuperLoc, SuperType, Super);
1707f4a2713aSLionel Sambuc }
1708f4a2713aSLionel Sambuc ObjCInterfaceDecl *ClassDeclared;
1709f4a2713aSLionel Sambuc if (ObjCIvarDecl *Ivar =
1710f4a2713aSLionel Sambuc IFace->lookupInstanceVariable(Member, ClassDeclared)) {
1711f4a2713aSLionel Sambuc QualType T = Ivar->getType();
1712f4a2713aSLionel Sambuc if (const ObjCObjectPointerType * OBJPT =
1713f4a2713aSLionel Sambuc T->getAsObjCInterfacePointerType()) {
1714f4a2713aSLionel Sambuc if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(),
1715f4a2713aSLionel Sambuc diag::err_property_not_as_forward_class,
1716f4a2713aSLionel Sambuc MemberName, BaseExpr))
1717f4a2713aSLionel Sambuc return ExprError();
1718f4a2713aSLionel Sambuc }
1719f4a2713aSLionel Sambuc Diag(MemberLoc,
1720f4a2713aSLionel Sambuc diag::err_ivar_access_using_property_syntax_suggest)
1721f4a2713aSLionel Sambuc << MemberName << QualType(OPT, 0) << Ivar->getDeclName()
1722f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(OpLoc, "->");
1723f4a2713aSLionel Sambuc return ExprError();
1724f4a2713aSLionel Sambuc }
1725f4a2713aSLionel Sambuc
1726f4a2713aSLionel Sambuc Diag(MemberLoc, diag::err_property_not_found)
1727f4a2713aSLionel Sambuc << MemberName << QualType(OPT, 0);
1728f4a2713aSLionel Sambuc if (Setter)
1729f4a2713aSLionel Sambuc Diag(Setter->getLocation(), diag::note_getter_unavailable)
1730f4a2713aSLionel Sambuc << MemberName << BaseExpr->getSourceRange();
1731f4a2713aSLionel Sambuc return ExprError();
1732f4a2713aSLionel Sambuc }
1733f4a2713aSLionel Sambuc
1734f4a2713aSLionel Sambuc
1735f4a2713aSLionel Sambuc
1736f4a2713aSLionel Sambuc ExprResult Sema::
ActOnClassPropertyRefExpr(IdentifierInfo & receiverName,IdentifierInfo & propertyName,SourceLocation receiverNameLoc,SourceLocation propertyNameLoc)1737f4a2713aSLionel Sambuc ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
1738f4a2713aSLionel Sambuc IdentifierInfo &propertyName,
1739f4a2713aSLionel Sambuc SourceLocation receiverNameLoc,
1740f4a2713aSLionel Sambuc SourceLocation propertyNameLoc) {
1741f4a2713aSLionel Sambuc
1742f4a2713aSLionel Sambuc IdentifierInfo *receiverNamePtr = &receiverName;
1743f4a2713aSLionel Sambuc ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
1744f4a2713aSLionel Sambuc receiverNameLoc);
1745f4a2713aSLionel Sambuc
1746f4a2713aSLionel Sambuc bool IsSuper = false;
1747*0a6a1f1dSLionel Sambuc if (!IFace) {
1748f4a2713aSLionel Sambuc // If the "receiver" is 'super' in a method, handle it as an expression-like
1749f4a2713aSLionel Sambuc // property reference.
1750f4a2713aSLionel Sambuc if (receiverNamePtr->isStr("super")) {
1751f4a2713aSLionel Sambuc IsSuper = true;
1752f4a2713aSLionel Sambuc
1753f4a2713aSLionel Sambuc if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) {
1754f4a2713aSLionel Sambuc if (CurMethod->isInstanceMethod()) {
1755f4a2713aSLionel Sambuc ObjCInterfaceDecl *Super =
1756f4a2713aSLionel Sambuc CurMethod->getClassInterface()->getSuperClass();
1757f4a2713aSLionel Sambuc if (!Super) {
1758f4a2713aSLionel Sambuc // The current class does not have a superclass.
1759f4a2713aSLionel Sambuc Diag(receiverNameLoc, diag::error_root_class_cannot_use_super)
1760f4a2713aSLionel Sambuc << CurMethod->getClassInterface()->getIdentifier();
1761f4a2713aSLionel Sambuc return ExprError();
1762f4a2713aSLionel Sambuc }
1763f4a2713aSLionel Sambuc QualType T = Context.getObjCInterfaceType(Super);
1764f4a2713aSLionel Sambuc T = Context.getObjCObjectPointerType(T);
1765f4a2713aSLionel Sambuc
1766f4a2713aSLionel Sambuc return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
1767*0a6a1f1dSLionel Sambuc /*BaseExpr*/nullptr,
1768f4a2713aSLionel Sambuc SourceLocation()/*OpLoc*/,
1769f4a2713aSLionel Sambuc &propertyName,
1770f4a2713aSLionel Sambuc propertyNameLoc,
1771f4a2713aSLionel Sambuc receiverNameLoc, T, true);
1772f4a2713aSLionel Sambuc }
1773f4a2713aSLionel Sambuc
1774f4a2713aSLionel Sambuc // Otherwise, if this is a class method, try dispatching to our
1775f4a2713aSLionel Sambuc // superclass.
1776f4a2713aSLionel Sambuc IFace = CurMethod->getClassInterface()->getSuperClass();
1777f4a2713aSLionel Sambuc }
1778f4a2713aSLionel Sambuc }
1779f4a2713aSLionel Sambuc
1780*0a6a1f1dSLionel Sambuc if (!IFace) {
1781*0a6a1f1dSLionel Sambuc Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier
1782*0a6a1f1dSLionel Sambuc << tok::l_paren;
1783f4a2713aSLionel Sambuc return ExprError();
1784f4a2713aSLionel Sambuc }
1785f4a2713aSLionel Sambuc }
1786f4a2713aSLionel Sambuc
1787f4a2713aSLionel Sambuc // Search for a declared property first.
1788f4a2713aSLionel Sambuc Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
1789f4a2713aSLionel Sambuc ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
1790f4a2713aSLionel Sambuc
1791f4a2713aSLionel Sambuc // If this reference is in an @implementation, check for 'private' methods.
1792f4a2713aSLionel Sambuc if (!Getter)
1793*0a6a1f1dSLionel Sambuc Getter = IFace->lookupPrivateClassMethod(Sel);
1794f4a2713aSLionel Sambuc
1795f4a2713aSLionel Sambuc if (Getter) {
1796f4a2713aSLionel Sambuc // FIXME: refactor/share with ActOnMemberReference().
1797f4a2713aSLionel Sambuc // Check if we can reference this property.
1798f4a2713aSLionel Sambuc if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
1799f4a2713aSLionel Sambuc return ExprError();
1800f4a2713aSLionel Sambuc }
1801f4a2713aSLionel Sambuc
1802f4a2713aSLionel Sambuc // Look for the matching setter, in case it is needed.
1803f4a2713aSLionel Sambuc Selector SetterSel =
1804f4a2713aSLionel Sambuc SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1805f4a2713aSLionel Sambuc PP.getSelectorTable(),
1806f4a2713aSLionel Sambuc &propertyName);
1807f4a2713aSLionel Sambuc
1808f4a2713aSLionel Sambuc ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
1809f4a2713aSLionel Sambuc if (!Setter) {
1810f4a2713aSLionel Sambuc // If this reference is in an @implementation, also check for 'private'
1811f4a2713aSLionel Sambuc // methods.
1812*0a6a1f1dSLionel Sambuc Setter = IFace->lookupPrivateClassMethod(SetterSel);
1813f4a2713aSLionel Sambuc }
1814f4a2713aSLionel Sambuc // Look through local category implementations associated with the class.
1815f4a2713aSLionel Sambuc if (!Setter)
1816f4a2713aSLionel Sambuc Setter = IFace->getCategoryClassMethod(SetterSel);
1817f4a2713aSLionel Sambuc
1818f4a2713aSLionel Sambuc if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
1819f4a2713aSLionel Sambuc return ExprError();
1820f4a2713aSLionel Sambuc
1821f4a2713aSLionel Sambuc if (Getter || Setter) {
1822f4a2713aSLionel Sambuc if (IsSuper)
1823*0a6a1f1dSLionel Sambuc return new (Context)
1824*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue,
1825*0a6a1f1dSLionel Sambuc OK_ObjCProperty, propertyNameLoc, receiverNameLoc,
1826*0a6a1f1dSLionel Sambuc Context.getObjCInterfaceType(IFace));
1827f4a2713aSLionel Sambuc
1828*0a6a1f1dSLionel Sambuc return new (Context) ObjCPropertyRefExpr(
1829*0a6a1f1dSLionel Sambuc Getter, Setter, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty,
1830*0a6a1f1dSLionel Sambuc propertyNameLoc, receiverNameLoc, IFace);
1831f4a2713aSLionel Sambuc }
1832f4a2713aSLionel Sambuc return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
1833f4a2713aSLionel Sambuc << &propertyName << Context.getObjCInterfaceType(IFace));
1834f4a2713aSLionel Sambuc }
1835f4a2713aSLionel Sambuc
1836f4a2713aSLionel Sambuc namespace {
1837f4a2713aSLionel Sambuc
1838f4a2713aSLionel Sambuc class ObjCInterfaceOrSuperCCC : public CorrectionCandidateCallback {
1839f4a2713aSLionel Sambuc public:
ObjCInterfaceOrSuperCCC(ObjCMethodDecl * Method)1840f4a2713aSLionel Sambuc ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) {
1841f4a2713aSLionel Sambuc // Determine whether "super" is acceptable in the current context.
1842f4a2713aSLionel Sambuc if (Method && Method->getClassInterface())
1843f4a2713aSLionel Sambuc WantObjCSuper = Method->getClassInterface()->getSuperClass();
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc
ValidateCandidate(const TypoCorrection & candidate)1846*0a6a1f1dSLionel Sambuc bool ValidateCandidate(const TypoCorrection &candidate) override {
1847f4a2713aSLionel Sambuc return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() ||
1848f4a2713aSLionel Sambuc candidate.isKeyword("super");
1849f4a2713aSLionel Sambuc }
1850f4a2713aSLionel Sambuc };
1851f4a2713aSLionel Sambuc
1852f4a2713aSLionel Sambuc }
1853f4a2713aSLionel Sambuc
getObjCMessageKind(Scope * S,IdentifierInfo * Name,SourceLocation NameLoc,bool IsSuper,bool HasTrailingDot,ParsedType & ReceiverType)1854f4a2713aSLionel Sambuc Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
1855f4a2713aSLionel Sambuc IdentifierInfo *Name,
1856f4a2713aSLionel Sambuc SourceLocation NameLoc,
1857f4a2713aSLionel Sambuc bool IsSuper,
1858f4a2713aSLionel Sambuc bool HasTrailingDot,
1859f4a2713aSLionel Sambuc ParsedType &ReceiverType) {
1860f4a2713aSLionel Sambuc ReceiverType = ParsedType();
1861f4a2713aSLionel Sambuc
1862f4a2713aSLionel Sambuc // If the identifier is "super" and there is no trailing dot, we're
1863f4a2713aSLionel Sambuc // messaging super. If the identifier is "super" and there is a
1864f4a2713aSLionel Sambuc // trailing dot, it's an instance message.
1865f4a2713aSLionel Sambuc if (IsSuper && S->isInObjcMethodScope())
1866f4a2713aSLionel Sambuc return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
1867f4a2713aSLionel Sambuc
1868f4a2713aSLionel Sambuc LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
1869f4a2713aSLionel Sambuc LookupName(Result, S);
1870f4a2713aSLionel Sambuc
1871f4a2713aSLionel Sambuc switch (Result.getResultKind()) {
1872f4a2713aSLionel Sambuc case LookupResult::NotFound:
1873f4a2713aSLionel Sambuc // Normal name lookup didn't find anything. If we're in an
1874f4a2713aSLionel Sambuc // Objective-C method, look for ivars. If we find one, we're done!
1875f4a2713aSLionel Sambuc // FIXME: This is a hack. Ivar lookup should be part of normal
1876f4a2713aSLionel Sambuc // lookup.
1877f4a2713aSLionel Sambuc if (ObjCMethodDecl *Method = getCurMethodDecl()) {
1878f4a2713aSLionel Sambuc if (!Method->getClassInterface()) {
1879f4a2713aSLionel Sambuc // Fall back: let the parser try to parse it as an instance message.
1880f4a2713aSLionel Sambuc return ObjCInstanceMessage;
1881f4a2713aSLionel Sambuc }
1882f4a2713aSLionel Sambuc
1883f4a2713aSLionel Sambuc ObjCInterfaceDecl *ClassDeclared;
1884f4a2713aSLionel Sambuc if (Method->getClassInterface()->lookupInstanceVariable(Name,
1885f4a2713aSLionel Sambuc ClassDeclared))
1886f4a2713aSLionel Sambuc return ObjCInstanceMessage;
1887f4a2713aSLionel Sambuc }
1888f4a2713aSLionel Sambuc
1889f4a2713aSLionel Sambuc // Break out; we'll perform typo correction below.
1890f4a2713aSLionel Sambuc break;
1891f4a2713aSLionel Sambuc
1892f4a2713aSLionel Sambuc case LookupResult::NotFoundInCurrentInstantiation:
1893f4a2713aSLionel Sambuc case LookupResult::FoundOverloaded:
1894f4a2713aSLionel Sambuc case LookupResult::FoundUnresolvedValue:
1895f4a2713aSLionel Sambuc case LookupResult::Ambiguous:
1896f4a2713aSLionel Sambuc Result.suppressDiagnostics();
1897f4a2713aSLionel Sambuc return ObjCInstanceMessage;
1898f4a2713aSLionel Sambuc
1899f4a2713aSLionel Sambuc case LookupResult::Found: {
1900f4a2713aSLionel Sambuc // If the identifier is a class or not, and there is a trailing dot,
1901f4a2713aSLionel Sambuc // it's an instance message.
1902f4a2713aSLionel Sambuc if (HasTrailingDot)
1903f4a2713aSLionel Sambuc return ObjCInstanceMessage;
1904f4a2713aSLionel Sambuc // We found something. If it's a type, then we have a class
1905f4a2713aSLionel Sambuc // message. Otherwise, it's an instance message.
1906f4a2713aSLionel Sambuc NamedDecl *ND = Result.getFoundDecl();
1907f4a2713aSLionel Sambuc QualType T;
1908f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
1909f4a2713aSLionel Sambuc T = Context.getObjCInterfaceType(Class);
1910f4a2713aSLionel Sambuc else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) {
1911f4a2713aSLionel Sambuc T = Context.getTypeDeclType(Type);
1912f4a2713aSLionel Sambuc DiagnoseUseOfDecl(Type, NameLoc);
1913f4a2713aSLionel Sambuc }
1914f4a2713aSLionel Sambuc else
1915f4a2713aSLionel Sambuc return ObjCInstanceMessage;
1916f4a2713aSLionel Sambuc
1917f4a2713aSLionel Sambuc // We have a class message, and T is the type we're
1918f4a2713aSLionel Sambuc // messaging. Build source-location information for it.
1919f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
1920f4a2713aSLionel Sambuc ReceiverType = CreateParsedType(T, TSInfo);
1921f4a2713aSLionel Sambuc return ObjCClassMessage;
1922f4a2713aSLionel Sambuc }
1923f4a2713aSLionel Sambuc }
1924f4a2713aSLionel Sambuc
1925*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected = CorrectTypo(
1926*0a6a1f1dSLionel Sambuc Result.getLookupNameInfo(), Result.getLookupKind(), S, nullptr,
1927*0a6a1f1dSLionel Sambuc llvm::make_unique<ObjCInterfaceOrSuperCCC>(getCurMethodDecl()),
1928*0a6a1f1dSLionel Sambuc CTK_ErrorRecovery, nullptr, false, nullptr, false)) {
1929f4a2713aSLionel Sambuc if (Corrected.isKeyword()) {
1930f4a2713aSLionel Sambuc // If we've found the keyword "super" (the only keyword that would be
1931f4a2713aSLionel Sambuc // returned by CorrectTypo), this is a send to super.
1932f4a2713aSLionel Sambuc diagnoseTypo(Corrected,
1933f4a2713aSLionel Sambuc PDiag(diag::err_unknown_receiver_suggest) << Name);
1934f4a2713aSLionel Sambuc return ObjCSuperMessage;
1935f4a2713aSLionel Sambuc } else if (ObjCInterfaceDecl *Class =
1936f4a2713aSLionel Sambuc Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1937f4a2713aSLionel Sambuc // If we found a declaration, correct when it refers to an Objective-C
1938f4a2713aSLionel Sambuc // class.
1939f4a2713aSLionel Sambuc diagnoseTypo(Corrected,
1940f4a2713aSLionel Sambuc PDiag(diag::err_unknown_receiver_suggest) << Name);
1941f4a2713aSLionel Sambuc QualType T = Context.getObjCInterfaceType(Class);
1942f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
1943f4a2713aSLionel Sambuc ReceiverType = CreateParsedType(T, TSInfo);
1944f4a2713aSLionel Sambuc return ObjCClassMessage;
1945f4a2713aSLionel Sambuc }
1946f4a2713aSLionel Sambuc }
1947f4a2713aSLionel Sambuc
1948f4a2713aSLionel Sambuc // Fall back: let the parser try to parse it as an instance message.
1949f4a2713aSLionel Sambuc return ObjCInstanceMessage;
1950f4a2713aSLionel Sambuc }
1951f4a2713aSLionel Sambuc
ActOnSuperMessage(Scope * S,SourceLocation SuperLoc,Selector Sel,SourceLocation LBracLoc,ArrayRef<SourceLocation> SelectorLocs,SourceLocation RBracLoc,MultiExprArg Args)1952f4a2713aSLionel Sambuc ExprResult Sema::ActOnSuperMessage(Scope *S,
1953f4a2713aSLionel Sambuc SourceLocation SuperLoc,
1954f4a2713aSLionel Sambuc Selector Sel,
1955f4a2713aSLionel Sambuc SourceLocation LBracLoc,
1956f4a2713aSLionel Sambuc ArrayRef<SourceLocation> SelectorLocs,
1957f4a2713aSLionel Sambuc SourceLocation RBracLoc,
1958f4a2713aSLionel Sambuc MultiExprArg Args) {
1959f4a2713aSLionel Sambuc // Determine whether we are inside a method or not.
1960f4a2713aSLionel Sambuc ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc);
1961f4a2713aSLionel Sambuc if (!Method) {
1962f4a2713aSLionel Sambuc Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
1963f4a2713aSLionel Sambuc return ExprError();
1964f4a2713aSLionel Sambuc }
1965f4a2713aSLionel Sambuc
1966f4a2713aSLionel Sambuc ObjCInterfaceDecl *Class = Method->getClassInterface();
1967f4a2713aSLionel Sambuc if (!Class) {
1968f4a2713aSLionel Sambuc Diag(SuperLoc, diag::error_no_super_class_message)
1969f4a2713aSLionel Sambuc << Method->getDeclName();
1970f4a2713aSLionel Sambuc return ExprError();
1971f4a2713aSLionel Sambuc }
1972f4a2713aSLionel Sambuc
1973f4a2713aSLionel Sambuc ObjCInterfaceDecl *Super = Class->getSuperClass();
1974f4a2713aSLionel Sambuc if (!Super) {
1975f4a2713aSLionel Sambuc // The current class does not have a superclass.
1976f4a2713aSLionel Sambuc Diag(SuperLoc, diag::error_root_class_cannot_use_super)
1977f4a2713aSLionel Sambuc << Class->getIdentifier();
1978f4a2713aSLionel Sambuc return ExprError();
1979f4a2713aSLionel Sambuc }
1980f4a2713aSLionel Sambuc
1981f4a2713aSLionel Sambuc // We are in a method whose class has a superclass, so 'super'
1982f4a2713aSLionel Sambuc // is acting as a keyword.
1983f4a2713aSLionel Sambuc if (Method->getSelector() == Sel)
1984f4a2713aSLionel Sambuc getCurFunction()->ObjCShouldCallSuper = false;
1985f4a2713aSLionel Sambuc
1986f4a2713aSLionel Sambuc if (Method->isInstanceMethod()) {
1987f4a2713aSLionel Sambuc // Since we are in an instance method, this is an instance
1988f4a2713aSLionel Sambuc // message to the superclass instance.
1989f4a2713aSLionel Sambuc QualType SuperTy = Context.getObjCInterfaceType(Super);
1990f4a2713aSLionel Sambuc SuperTy = Context.getObjCObjectPointerType(SuperTy);
1991*0a6a1f1dSLionel Sambuc return BuildInstanceMessage(nullptr, SuperTy, SuperLoc,
1992*0a6a1f1dSLionel Sambuc Sel, /*Method=*/nullptr,
1993f4a2713aSLionel Sambuc LBracLoc, SelectorLocs, RBracLoc, Args);
1994f4a2713aSLionel Sambuc }
1995f4a2713aSLionel Sambuc
1996f4a2713aSLionel Sambuc // Since we are in a class method, this is a class message to
1997f4a2713aSLionel Sambuc // the superclass.
1998*0a6a1f1dSLionel Sambuc return BuildClassMessage(/*ReceiverTypeInfo=*/nullptr,
1999f4a2713aSLionel Sambuc Context.getObjCInterfaceType(Super),
2000*0a6a1f1dSLionel Sambuc SuperLoc, Sel, /*Method=*/nullptr,
2001f4a2713aSLionel Sambuc LBracLoc, SelectorLocs, RBracLoc, Args);
2002f4a2713aSLionel Sambuc }
2003f4a2713aSLionel Sambuc
2004f4a2713aSLionel Sambuc
BuildClassMessageImplicit(QualType ReceiverType,bool isSuperReceiver,SourceLocation Loc,Selector Sel,ObjCMethodDecl * Method,MultiExprArg Args)2005f4a2713aSLionel Sambuc ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType,
2006f4a2713aSLionel Sambuc bool isSuperReceiver,
2007f4a2713aSLionel Sambuc SourceLocation Loc,
2008f4a2713aSLionel Sambuc Selector Sel,
2009f4a2713aSLionel Sambuc ObjCMethodDecl *Method,
2010f4a2713aSLionel Sambuc MultiExprArg Args) {
2011*0a6a1f1dSLionel Sambuc TypeSourceInfo *receiverTypeInfo = nullptr;
2012f4a2713aSLionel Sambuc if (!ReceiverType.isNull())
2013f4a2713aSLionel Sambuc receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType);
2014f4a2713aSLionel Sambuc
2015f4a2713aSLionel Sambuc return BuildClassMessage(receiverTypeInfo, ReceiverType,
2016f4a2713aSLionel Sambuc /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(),
2017f4a2713aSLionel Sambuc Sel, Method, Loc, Loc, Loc, Args,
2018f4a2713aSLionel Sambuc /*isImplicit=*/true);
2019f4a2713aSLionel Sambuc
2020f4a2713aSLionel Sambuc }
2021f4a2713aSLionel Sambuc
applyCocoaAPICheck(Sema & S,const ObjCMessageExpr * Msg,unsigned DiagID,bool (* refactor)(const ObjCMessageExpr *,const NSAPI &,edit::Commit &))2022f4a2713aSLionel Sambuc static void applyCocoaAPICheck(Sema &S, const ObjCMessageExpr *Msg,
2023f4a2713aSLionel Sambuc unsigned DiagID,
2024f4a2713aSLionel Sambuc bool (*refactor)(const ObjCMessageExpr *,
2025f4a2713aSLionel Sambuc const NSAPI &, edit::Commit &)) {
2026f4a2713aSLionel Sambuc SourceLocation MsgLoc = Msg->getExprLoc();
2027*0a6a1f1dSLionel Sambuc if (S.Diags.isIgnored(DiagID, MsgLoc))
2028f4a2713aSLionel Sambuc return;
2029f4a2713aSLionel Sambuc
2030f4a2713aSLionel Sambuc SourceManager &SM = S.SourceMgr;
2031f4a2713aSLionel Sambuc edit::Commit ECommit(SM, S.LangOpts);
2032f4a2713aSLionel Sambuc if (refactor(Msg,*S.NSAPIObj, ECommit)) {
2033f4a2713aSLionel Sambuc DiagnosticBuilder Builder = S.Diag(MsgLoc, DiagID)
2034f4a2713aSLionel Sambuc << Msg->getSelector() << Msg->getSourceRange();
2035f4a2713aSLionel Sambuc // FIXME: Don't emit diagnostic at all if fixits are non-commitable.
2036f4a2713aSLionel Sambuc if (!ECommit.isCommitable())
2037f4a2713aSLionel Sambuc return;
2038f4a2713aSLionel Sambuc for (edit::Commit::edit_iterator
2039f4a2713aSLionel Sambuc I = ECommit.edit_begin(), E = ECommit.edit_end(); I != E; ++I) {
2040f4a2713aSLionel Sambuc const edit::Commit::Edit &Edit = *I;
2041f4a2713aSLionel Sambuc switch (Edit.Kind) {
2042f4a2713aSLionel Sambuc case edit::Commit::Act_Insert:
2043f4a2713aSLionel Sambuc Builder.AddFixItHint(FixItHint::CreateInsertion(Edit.OrigLoc,
2044f4a2713aSLionel Sambuc Edit.Text,
2045f4a2713aSLionel Sambuc Edit.BeforePrev));
2046f4a2713aSLionel Sambuc break;
2047f4a2713aSLionel Sambuc case edit::Commit::Act_InsertFromRange:
2048f4a2713aSLionel Sambuc Builder.AddFixItHint(
2049f4a2713aSLionel Sambuc FixItHint::CreateInsertionFromRange(Edit.OrigLoc,
2050f4a2713aSLionel Sambuc Edit.getInsertFromRange(SM),
2051f4a2713aSLionel Sambuc Edit.BeforePrev));
2052f4a2713aSLionel Sambuc break;
2053f4a2713aSLionel Sambuc case edit::Commit::Act_Remove:
2054f4a2713aSLionel Sambuc Builder.AddFixItHint(FixItHint::CreateRemoval(Edit.getFileRange(SM)));
2055f4a2713aSLionel Sambuc break;
2056f4a2713aSLionel Sambuc }
2057f4a2713aSLionel Sambuc }
2058f4a2713aSLionel Sambuc }
2059f4a2713aSLionel Sambuc }
2060f4a2713aSLionel Sambuc
checkCocoaAPI(Sema & S,const ObjCMessageExpr * Msg)2061f4a2713aSLionel Sambuc static void checkCocoaAPI(Sema &S, const ObjCMessageExpr *Msg) {
2062f4a2713aSLionel Sambuc applyCocoaAPICheck(S, Msg, diag::warn_objc_redundant_literal_use,
2063f4a2713aSLionel Sambuc edit::rewriteObjCRedundantCallWithLiteral);
2064f4a2713aSLionel Sambuc }
2065f4a2713aSLionel Sambuc
2066*0a6a1f1dSLionel Sambuc /// \brief Diagnose use of %s directive in an NSString which is being passed
2067*0a6a1f1dSLionel Sambuc /// as formatting string to formatting method.
2068*0a6a1f1dSLionel Sambuc static void
DiagnoseCStringFormatDirectiveInObjCAPI(Sema & S,ObjCMethodDecl * Method,Selector Sel,Expr ** Args,unsigned NumArgs)2069*0a6a1f1dSLionel Sambuc DiagnoseCStringFormatDirectiveInObjCAPI(Sema &S,
2070*0a6a1f1dSLionel Sambuc ObjCMethodDecl *Method,
2071*0a6a1f1dSLionel Sambuc Selector Sel,
2072*0a6a1f1dSLionel Sambuc Expr **Args, unsigned NumArgs) {
2073*0a6a1f1dSLionel Sambuc unsigned Idx = 0;
2074*0a6a1f1dSLionel Sambuc bool Format = false;
2075*0a6a1f1dSLionel Sambuc ObjCStringFormatFamily SFFamily = Sel.getStringFormatFamily();
2076*0a6a1f1dSLionel Sambuc if (SFFamily == ObjCStringFormatFamily::SFF_NSString) {
2077*0a6a1f1dSLionel Sambuc Idx = 0;
2078*0a6a1f1dSLionel Sambuc Format = true;
2079*0a6a1f1dSLionel Sambuc }
2080*0a6a1f1dSLionel Sambuc else if (Method) {
2081*0a6a1f1dSLionel Sambuc for (const auto *I : Method->specific_attrs<FormatAttr>()) {
2082*0a6a1f1dSLionel Sambuc if (S.GetFormatNSStringIdx(I, Idx)) {
2083*0a6a1f1dSLionel Sambuc Format = true;
2084*0a6a1f1dSLionel Sambuc break;
2085*0a6a1f1dSLionel Sambuc }
2086*0a6a1f1dSLionel Sambuc }
2087*0a6a1f1dSLionel Sambuc }
2088*0a6a1f1dSLionel Sambuc if (!Format || NumArgs <= Idx)
2089*0a6a1f1dSLionel Sambuc return;
2090*0a6a1f1dSLionel Sambuc
2091*0a6a1f1dSLionel Sambuc Expr *FormatExpr = Args[Idx];
2092*0a6a1f1dSLionel Sambuc if (ObjCStringLiteral *OSL =
2093*0a6a1f1dSLionel Sambuc dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) {
2094*0a6a1f1dSLionel Sambuc StringLiteral *FormatString = OSL->getString();
2095*0a6a1f1dSLionel Sambuc if (S.FormatStringHasSArg(FormatString)) {
2096*0a6a1f1dSLionel Sambuc S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
2097*0a6a1f1dSLionel Sambuc << "%s" << 0 << 0;
2098*0a6a1f1dSLionel Sambuc if (Method)
2099*0a6a1f1dSLionel Sambuc S.Diag(Method->getLocation(), diag::note_method_declared_at)
2100*0a6a1f1dSLionel Sambuc << Method->getDeclName();
2101*0a6a1f1dSLionel Sambuc }
2102*0a6a1f1dSLionel Sambuc }
2103*0a6a1f1dSLionel Sambuc }
2104*0a6a1f1dSLionel Sambuc
2105f4a2713aSLionel Sambuc /// \brief Build an Objective-C class message expression.
2106f4a2713aSLionel Sambuc ///
2107f4a2713aSLionel Sambuc /// This routine takes care of both normal class messages and
2108f4a2713aSLionel Sambuc /// class messages to the superclass.
2109f4a2713aSLionel Sambuc ///
2110f4a2713aSLionel Sambuc /// \param ReceiverTypeInfo Type source information that describes the
2111f4a2713aSLionel Sambuc /// receiver of this message. This may be NULL, in which case we are
2112f4a2713aSLionel Sambuc /// sending to the superclass and \p SuperLoc must be a valid source
2113f4a2713aSLionel Sambuc /// location.
2114f4a2713aSLionel Sambuc
2115f4a2713aSLionel Sambuc /// \param ReceiverType The type of the object receiving the
2116f4a2713aSLionel Sambuc /// message. When \p ReceiverTypeInfo is non-NULL, this is the same
2117f4a2713aSLionel Sambuc /// type as that refers to. For a superclass send, this is the type of
2118f4a2713aSLionel Sambuc /// the superclass.
2119f4a2713aSLionel Sambuc ///
2120f4a2713aSLionel Sambuc /// \param SuperLoc The location of the "super" keyword in a
2121f4a2713aSLionel Sambuc /// superclass message.
2122f4a2713aSLionel Sambuc ///
2123f4a2713aSLionel Sambuc /// \param Sel The selector to which the message is being sent.
2124f4a2713aSLionel Sambuc ///
2125f4a2713aSLionel Sambuc /// \param Method The method that this class message is invoking, if
2126f4a2713aSLionel Sambuc /// already known.
2127f4a2713aSLionel Sambuc ///
2128f4a2713aSLionel Sambuc /// \param LBracLoc The location of the opening square bracket ']'.
2129f4a2713aSLionel Sambuc ///
2130f4a2713aSLionel Sambuc /// \param RBracLoc The location of the closing square bracket ']'.
2131f4a2713aSLionel Sambuc ///
2132f4a2713aSLionel Sambuc /// \param ArgsIn The message arguments.
BuildClassMessage(TypeSourceInfo * ReceiverTypeInfo,QualType ReceiverType,SourceLocation SuperLoc,Selector Sel,ObjCMethodDecl * Method,SourceLocation LBracLoc,ArrayRef<SourceLocation> SelectorLocs,SourceLocation RBracLoc,MultiExprArg ArgsIn,bool isImplicit)2133f4a2713aSLionel Sambuc ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
2134f4a2713aSLionel Sambuc QualType ReceiverType,
2135f4a2713aSLionel Sambuc SourceLocation SuperLoc,
2136f4a2713aSLionel Sambuc Selector Sel,
2137f4a2713aSLionel Sambuc ObjCMethodDecl *Method,
2138f4a2713aSLionel Sambuc SourceLocation LBracLoc,
2139f4a2713aSLionel Sambuc ArrayRef<SourceLocation> SelectorLocs,
2140f4a2713aSLionel Sambuc SourceLocation RBracLoc,
2141f4a2713aSLionel Sambuc MultiExprArg ArgsIn,
2142f4a2713aSLionel Sambuc bool isImplicit) {
2143f4a2713aSLionel Sambuc SourceLocation Loc = SuperLoc.isValid()? SuperLoc
2144f4a2713aSLionel Sambuc : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
2145f4a2713aSLionel Sambuc if (LBracLoc.isInvalid()) {
2146f4a2713aSLionel Sambuc Diag(Loc, diag::err_missing_open_square_message_send)
2147f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(Loc, "[");
2148f4a2713aSLionel Sambuc LBracLoc = Loc;
2149f4a2713aSLionel Sambuc }
2150f4a2713aSLionel Sambuc SourceLocation SelLoc;
2151f4a2713aSLionel Sambuc if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2152f4a2713aSLionel Sambuc SelLoc = SelectorLocs.front();
2153f4a2713aSLionel Sambuc else
2154f4a2713aSLionel Sambuc SelLoc = Loc;
2155f4a2713aSLionel Sambuc
2156f4a2713aSLionel Sambuc if (ReceiverType->isDependentType()) {
2157f4a2713aSLionel Sambuc // If the receiver type is dependent, we can't type-check anything
2158f4a2713aSLionel Sambuc // at this point. Build a dependent expression.
2159f4a2713aSLionel Sambuc unsigned NumArgs = ArgsIn.size();
2160f4a2713aSLionel Sambuc Expr **Args = ArgsIn.data();
2161f4a2713aSLionel Sambuc assert(SuperLoc.isInvalid() && "Message to super with dependent type");
2162*0a6a1f1dSLionel Sambuc return ObjCMessageExpr::Create(
2163*0a6a1f1dSLionel Sambuc Context, ReceiverType, VK_RValue, LBracLoc, ReceiverTypeInfo, Sel,
2164*0a6a1f1dSLionel Sambuc SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs), RBracLoc,
2165*0a6a1f1dSLionel Sambuc isImplicit);
2166f4a2713aSLionel Sambuc }
2167f4a2713aSLionel Sambuc
2168f4a2713aSLionel Sambuc // Find the class to which we are sending this message.
2169*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *Class = nullptr;
2170f4a2713aSLionel Sambuc const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
2171f4a2713aSLionel Sambuc if (!ClassType || !(Class = ClassType->getInterface())) {
2172f4a2713aSLionel Sambuc Diag(Loc, diag::err_invalid_receiver_class_message)
2173f4a2713aSLionel Sambuc << ReceiverType;
2174f4a2713aSLionel Sambuc return ExprError();
2175f4a2713aSLionel Sambuc }
2176f4a2713aSLionel Sambuc assert(Class && "We don't know which class we're messaging?");
2177f4a2713aSLionel Sambuc // objc++ diagnoses during typename annotation.
2178f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus)
2179f4a2713aSLionel Sambuc (void)DiagnoseUseOfDecl(Class, SelLoc);
2180f4a2713aSLionel Sambuc // Find the method we are messaging.
2181f4a2713aSLionel Sambuc if (!Method) {
2182f4a2713aSLionel Sambuc SourceRange TypeRange
2183f4a2713aSLionel Sambuc = SuperLoc.isValid()? SourceRange(SuperLoc)
2184f4a2713aSLionel Sambuc : ReceiverTypeInfo->getTypeLoc().getSourceRange();
2185f4a2713aSLionel Sambuc if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class),
2186f4a2713aSLionel Sambuc (getLangOpts().ObjCAutoRefCount
2187f4a2713aSLionel Sambuc ? diag::err_arc_receiver_forward_class
2188f4a2713aSLionel Sambuc : diag::warn_receiver_forward_class),
2189f4a2713aSLionel Sambuc TypeRange)) {
2190f4a2713aSLionel Sambuc // A forward class used in messaging is treated as a 'Class'
2191f4a2713aSLionel Sambuc Method = LookupFactoryMethodInGlobalPool(Sel,
2192f4a2713aSLionel Sambuc SourceRange(LBracLoc, RBracLoc));
2193f4a2713aSLionel Sambuc if (Method && !getLangOpts().ObjCAutoRefCount)
2194f4a2713aSLionel Sambuc Diag(Method->getLocation(), diag::note_method_sent_forward_class)
2195f4a2713aSLionel Sambuc << Method->getDeclName();
2196f4a2713aSLionel Sambuc }
2197f4a2713aSLionel Sambuc if (!Method)
2198f4a2713aSLionel Sambuc Method = Class->lookupClassMethod(Sel);
2199f4a2713aSLionel Sambuc
2200f4a2713aSLionel Sambuc // If we have an implementation in scope, check "private" methods.
2201f4a2713aSLionel Sambuc if (!Method)
2202f4a2713aSLionel Sambuc Method = Class->lookupPrivateClassMethod(Sel);
2203f4a2713aSLionel Sambuc
2204f4a2713aSLionel Sambuc if (Method && DiagnoseUseOfDecl(Method, SelLoc))
2205f4a2713aSLionel Sambuc return ExprError();
2206f4a2713aSLionel Sambuc }
2207f4a2713aSLionel Sambuc
2208f4a2713aSLionel Sambuc // Check the argument types and determine the result type.
2209f4a2713aSLionel Sambuc QualType ReturnType;
2210f4a2713aSLionel Sambuc ExprValueKind VK = VK_RValue;
2211f4a2713aSLionel Sambuc
2212f4a2713aSLionel Sambuc unsigned NumArgs = ArgsIn.size();
2213f4a2713aSLionel Sambuc Expr **Args = ArgsIn.data();
2214f4a2713aSLionel Sambuc if (CheckMessageArgumentTypes(ReceiverType, MultiExprArg(Args, NumArgs),
2215f4a2713aSLionel Sambuc Sel, SelectorLocs,
2216f4a2713aSLionel Sambuc Method, true,
2217f4a2713aSLionel Sambuc SuperLoc.isValid(), LBracLoc, RBracLoc,
2218*0a6a1f1dSLionel Sambuc SourceRange(),
2219f4a2713aSLionel Sambuc ReturnType, VK))
2220f4a2713aSLionel Sambuc return ExprError();
2221f4a2713aSLionel Sambuc
2222*0a6a1f1dSLionel Sambuc if (Method && !Method->getReturnType()->isVoidType() &&
2223*0a6a1f1dSLionel Sambuc RequireCompleteType(LBracLoc, Method->getReturnType(),
2224f4a2713aSLionel Sambuc diag::err_illegal_message_expr_incomplete_type))
2225f4a2713aSLionel Sambuc return ExprError();
2226f4a2713aSLionel Sambuc
2227*0a6a1f1dSLionel Sambuc // Warn about explicit call of +initialize on its own class. But not on 'super'.
2228*0a6a1f1dSLionel Sambuc if (Method && Method->getMethodFamily() == OMF_initialize) {
2229*0a6a1f1dSLionel Sambuc if (!SuperLoc.isValid()) {
2230*0a6a1f1dSLionel Sambuc const ObjCInterfaceDecl *ID =
2231*0a6a1f1dSLionel Sambuc dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext());
2232*0a6a1f1dSLionel Sambuc if (ID == Class) {
2233*0a6a1f1dSLionel Sambuc Diag(Loc, diag::warn_direct_initialize_call);
2234*0a6a1f1dSLionel Sambuc Diag(Method->getLocation(), diag::note_method_declared_at)
2235*0a6a1f1dSLionel Sambuc << Method->getDeclName();
2236*0a6a1f1dSLionel Sambuc }
2237*0a6a1f1dSLionel Sambuc }
2238*0a6a1f1dSLionel Sambuc else if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
2239*0a6a1f1dSLionel Sambuc // [super initialize] is allowed only within an +initialize implementation
2240*0a6a1f1dSLionel Sambuc if (CurMeth->getMethodFamily() != OMF_initialize) {
2241*0a6a1f1dSLionel Sambuc Diag(Loc, diag::warn_direct_super_initialize_call);
2242*0a6a1f1dSLionel Sambuc Diag(Method->getLocation(), diag::note_method_declared_at)
2243*0a6a1f1dSLionel Sambuc << Method->getDeclName();
2244*0a6a1f1dSLionel Sambuc Diag(CurMeth->getLocation(), diag::note_method_declared_at)
2245*0a6a1f1dSLionel Sambuc << CurMeth->getDeclName();
2246*0a6a1f1dSLionel Sambuc }
2247*0a6a1f1dSLionel Sambuc }
2248*0a6a1f1dSLionel Sambuc }
2249*0a6a1f1dSLionel Sambuc
2250*0a6a1f1dSLionel Sambuc DiagnoseCStringFormatDirectiveInObjCAPI(*this, Method, Sel, Args, NumArgs);
2251*0a6a1f1dSLionel Sambuc
2252f4a2713aSLionel Sambuc // Construct the appropriate ObjCMessageExpr.
2253f4a2713aSLionel Sambuc ObjCMessageExpr *Result;
2254f4a2713aSLionel Sambuc if (SuperLoc.isValid())
2255f4a2713aSLionel Sambuc Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
2256f4a2713aSLionel Sambuc SuperLoc, /*IsInstanceSuper=*/false,
2257f4a2713aSLionel Sambuc ReceiverType, Sel, SelectorLocs,
2258f4a2713aSLionel Sambuc Method, makeArrayRef(Args, NumArgs),
2259f4a2713aSLionel Sambuc RBracLoc, isImplicit);
2260f4a2713aSLionel Sambuc else {
2261f4a2713aSLionel Sambuc Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
2262f4a2713aSLionel Sambuc ReceiverTypeInfo, Sel, SelectorLocs,
2263f4a2713aSLionel Sambuc Method, makeArrayRef(Args, NumArgs),
2264f4a2713aSLionel Sambuc RBracLoc, isImplicit);
2265f4a2713aSLionel Sambuc if (!isImplicit)
2266f4a2713aSLionel Sambuc checkCocoaAPI(*this, Result);
2267f4a2713aSLionel Sambuc }
2268f4a2713aSLionel Sambuc return MaybeBindToTemporary(Result);
2269f4a2713aSLionel Sambuc }
2270f4a2713aSLionel Sambuc
2271f4a2713aSLionel Sambuc // ActOnClassMessage - used for both unary and keyword messages.
2272f4a2713aSLionel Sambuc // ArgExprs is optional - if it is present, the number of expressions
2273f4a2713aSLionel Sambuc // is obtained from Sel.getNumArgs().
ActOnClassMessage(Scope * S,ParsedType Receiver,Selector Sel,SourceLocation LBracLoc,ArrayRef<SourceLocation> SelectorLocs,SourceLocation RBracLoc,MultiExprArg Args)2274f4a2713aSLionel Sambuc ExprResult Sema::ActOnClassMessage(Scope *S,
2275f4a2713aSLionel Sambuc ParsedType Receiver,
2276f4a2713aSLionel Sambuc Selector Sel,
2277f4a2713aSLionel Sambuc SourceLocation LBracLoc,
2278f4a2713aSLionel Sambuc ArrayRef<SourceLocation> SelectorLocs,
2279f4a2713aSLionel Sambuc SourceLocation RBracLoc,
2280f4a2713aSLionel Sambuc MultiExprArg Args) {
2281f4a2713aSLionel Sambuc TypeSourceInfo *ReceiverTypeInfo;
2282f4a2713aSLionel Sambuc QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
2283f4a2713aSLionel Sambuc if (ReceiverType.isNull())
2284f4a2713aSLionel Sambuc return ExprError();
2285f4a2713aSLionel Sambuc
2286f4a2713aSLionel Sambuc
2287f4a2713aSLionel Sambuc if (!ReceiverTypeInfo)
2288f4a2713aSLionel Sambuc ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
2289f4a2713aSLionel Sambuc
2290f4a2713aSLionel Sambuc return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
2291*0a6a1f1dSLionel Sambuc /*SuperLoc=*/SourceLocation(), Sel,
2292*0a6a1f1dSLionel Sambuc /*Method=*/nullptr, LBracLoc, SelectorLocs, RBracLoc,
2293*0a6a1f1dSLionel Sambuc Args);
2294f4a2713aSLionel Sambuc }
2295f4a2713aSLionel Sambuc
BuildInstanceMessageImplicit(Expr * Receiver,QualType ReceiverType,SourceLocation Loc,Selector Sel,ObjCMethodDecl * Method,MultiExprArg Args)2296f4a2713aSLionel Sambuc ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver,
2297f4a2713aSLionel Sambuc QualType ReceiverType,
2298f4a2713aSLionel Sambuc SourceLocation Loc,
2299f4a2713aSLionel Sambuc Selector Sel,
2300f4a2713aSLionel Sambuc ObjCMethodDecl *Method,
2301f4a2713aSLionel Sambuc MultiExprArg Args) {
2302f4a2713aSLionel Sambuc return BuildInstanceMessage(Receiver, ReceiverType,
2303f4a2713aSLionel Sambuc /*SuperLoc=*/!Receiver ? Loc : SourceLocation(),
2304f4a2713aSLionel Sambuc Sel, Method, Loc, Loc, Loc, Args,
2305f4a2713aSLionel Sambuc /*isImplicit=*/true);
2306f4a2713aSLionel Sambuc }
2307f4a2713aSLionel Sambuc
2308f4a2713aSLionel Sambuc /// \brief Build an Objective-C instance message expression.
2309f4a2713aSLionel Sambuc ///
2310f4a2713aSLionel Sambuc /// This routine takes care of both normal instance messages and
2311f4a2713aSLionel Sambuc /// instance messages to the superclass instance.
2312f4a2713aSLionel Sambuc ///
2313f4a2713aSLionel Sambuc /// \param Receiver The expression that computes the object that will
2314f4a2713aSLionel Sambuc /// receive this message. This may be empty, in which case we are
2315f4a2713aSLionel Sambuc /// sending to the superclass instance and \p SuperLoc must be a valid
2316f4a2713aSLionel Sambuc /// source location.
2317f4a2713aSLionel Sambuc ///
2318f4a2713aSLionel Sambuc /// \param ReceiverType The (static) type of the object receiving the
2319f4a2713aSLionel Sambuc /// message. When a \p Receiver expression is provided, this is the
2320f4a2713aSLionel Sambuc /// same type as that expression. For a superclass instance send, this
2321f4a2713aSLionel Sambuc /// is a pointer to the type of the superclass.
2322f4a2713aSLionel Sambuc ///
2323f4a2713aSLionel Sambuc /// \param SuperLoc The location of the "super" keyword in a
2324f4a2713aSLionel Sambuc /// superclass instance message.
2325f4a2713aSLionel Sambuc ///
2326f4a2713aSLionel Sambuc /// \param Sel The selector to which the message is being sent.
2327f4a2713aSLionel Sambuc ///
2328f4a2713aSLionel Sambuc /// \param Method The method that this instance message is invoking, if
2329f4a2713aSLionel Sambuc /// already known.
2330f4a2713aSLionel Sambuc ///
2331f4a2713aSLionel Sambuc /// \param LBracLoc The location of the opening square bracket ']'.
2332f4a2713aSLionel Sambuc ///
2333f4a2713aSLionel Sambuc /// \param RBracLoc The location of the closing square bracket ']'.
2334f4a2713aSLionel Sambuc ///
2335f4a2713aSLionel Sambuc /// \param ArgsIn The message arguments.
BuildInstanceMessage(Expr * Receiver,QualType ReceiverType,SourceLocation SuperLoc,Selector Sel,ObjCMethodDecl * Method,SourceLocation LBracLoc,ArrayRef<SourceLocation> SelectorLocs,SourceLocation RBracLoc,MultiExprArg ArgsIn,bool isImplicit)2336f4a2713aSLionel Sambuc ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
2337f4a2713aSLionel Sambuc QualType ReceiverType,
2338f4a2713aSLionel Sambuc SourceLocation SuperLoc,
2339f4a2713aSLionel Sambuc Selector Sel,
2340f4a2713aSLionel Sambuc ObjCMethodDecl *Method,
2341f4a2713aSLionel Sambuc SourceLocation LBracLoc,
2342f4a2713aSLionel Sambuc ArrayRef<SourceLocation> SelectorLocs,
2343f4a2713aSLionel Sambuc SourceLocation RBracLoc,
2344f4a2713aSLionel Sambuc MultiExprArg ArgsIn,
2345f4a2713aSLionel Sambuc bool isImplicit) {
2346f4a2713aSLionel Sambuc // The location of the receiver.
2347f4a2713aSLionel Sambuc SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
2348f4a2713aSLionel Sambuc SourceRange RecRange =
2349f4a2713aSLionel Sambuc SuperLoc.isValid()? SuperLoc : Receiver->getSourceRange();
2350f4a2713aSLionel Sambuc SourceLocation SelLoc;
2351f4a2713aSLionel Sambuc if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2352f4a2713aSLionel Sambuc SelLoc = SelectorLocs.front();
2353f4a2713aSLionel Sambuc else
2354f4a2713aSLionel Sambuc SelLoc = Loc;
2355f4a2713aSLionel Sambuc
2356f4a2713aSLionel Sambuc if (LBracLoc.isInvalid()) {
2357f4a2713aSLionel Sambuc Diag(Loc, diag::err_missing_open_square_message_send)
2358f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(Loc, "[");
2359f4a2713aSLionel Sambuc LBracLoc = Loc;
2360f4a2713aSLionel Sambuc }
2361f4a2713aSLionel Sambuc
2362f4a2713aSLionel Sambuc // If we have a receiver expression, perform appropriate promotions
2363f4a2713aSLionel Sambuc // and determine receiver type.
2364f4a2713aSLionel Sambuc if (Receiver) {
2365f4a2713aSLionel Sambuc if (Receiver->hasPlaceholderType()) {
2366f4a2713aSLionel Sambuc ExprResult Result;
2367f4a2713aSLionel Sambuc if (Receiver->getType() == Context.UnknownAnyTy)
2368f4a2713aSLionel Sambuc Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType());
2369f4a2713aSLionel Sambuc else
2370f4a2713aSLionel Sambuc Result = CheckPlaceholderExpr(Receiver);
2371f4a2713aSLionel Sambuc if (Result.isInvalid()) return ExprError();
2372*0a6a1f1dSLionel Sambuc Receiver = Result.get();
2373f4a2713aSLionel Sambuc }
2374f4a2713aSLionel Sambuc
2375f4a2713aSLionel Sambuc if (Receiver->isTypeDependent()) {
2376f4a2713aSLionel Sambuc // If the receiver is type-dependent, we can't type-check anything
2377f4a2713aSLionel Sambuc // at this point. Build a dependent expression.
2378f4a2713aSLionel Sambuc unsigned NumArgs = ArgsIn.size();
2379f4a2713aSLionel Sambuc Expr **Args = ArgsIn.data();
2380f4a2713aSLionel Sambuc assert(SuperLoc.isInvalid() && "Message to super with dependent type");
2381*0a6a1f1dSLionel Sambuc return ObjCMessageExpr::Create(
2382*0a6a1f1dSLionel Sambuc Context, Context.DependentTy, VK_RValue, LBracLoc, Receiver, Sel,
2383*0a6a1f1dSLionel Sambuc SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs),
2384*0a6a1f1dSLionel Sambuc RBracLoc, isImplicit);
2385f4a2713aSLionel Sambuc }
2386f4a2713aSLionel Sambuc
2387f4a2713aSLionel Sambuc // If necessary, apply function/array conversion to the receiver.
2388f4a2713aSLionel Sambuc // C99 6.7.5.3p[7,8].
2389f4a2713aSLionel Sambuc ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
2390f4a2713aSLionel Sambuc if (Result.isInvalid())
2391f4a2713aSLionel Sambuc return ExprError();
2392*0a6a1f1dSLionel Sambuc Receiver = Result.get();
2393f4a2713aSLionel Sambuc ReceiverType = Receiver->getType();
2394f4a2713aSLionel Sambuc
2395f4a2713aSLionel Sambuc // If the receiver is an ObjC pointer, a block pointer, or an
2396f4a2713aSLionel Sambuc // __attribute__((NSObject)) pointer, we don't need to do any
2397f4a2713aSLionel Sambuc // special conversion in order to look up a receiver.
2398f4a2713aSLionel Sambuc if (ReceiverType->isObjCRetainableType()) {
2399f4a2713aSLionel Sambuc // do nothing
2400f4a2713aSLionel Sambuc } else if (!getLangOpts().ObjCAutoRefCount &&
2401f4a2713aSLionel Sambuc !Context.getObjCIdType().isNull() &&
2402f4a2713aSLionel Sambuc (ReceiverType->isPointerType() ||
2403f4a2713aSLionel Sambuc ReceiverType->isIntegerType())) {
2404f4a2713aSLionel Sambuc // Implicitly convert integers and pointers to 'id' but emit a warning.
2405f4a2713aSLionel Sambuc // But not in ARC.
2406f4a2713aSLionel Sambuc Diag(Loc, diag::warn_bad_receiver_type)
2407f4a2713aSLionel Sambuc << ReceiverType
2408f4a2713aSLionel Sambuc << Receiver->getSourceRange();
2409f4a2713aSLionel Sambuc if (ReceiverType->isPointerType()) {
2410f4a2713aSLionel Sambuc Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
2411*0a6a1f1dSLionel Sambuc CK_CPointerToObjCPointerCast).get();
2412f4a2713aSLionel Sambuc } else {
2413f4a2713aSLionel Sambuc // TODO: specialized warning on null receivers?
2414f4a2713aSLionel Sambuc bool IsNull = Receiver->isNullPointerConstant(Context,
2415f4a2713aSLionel Sambuc Expr::NPC_ValueDependentIsNull);
2416f4a2713aSLionel Sambuc CastKind Kind = IsNull ? CK_NullToPointer : CK_IntegralToPointer;
2417f4a2713aSLionel Sambuc Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
2418*0a6a1f1dSLionel Sambuc Kind).get();
2419f4a2713aSLionel Sambuc }
2420f4a2713aSLionel Sambuc ReceiverType = Receiver->getType();
2421f4a2713aSLionel Sambuc } else if (getLangOpts().CPlusPlus) {
2422f4a2713aSLionel Sambuc // The receiver must be a complete type.
2423f4a2713aSLionel Sambuc if (RequireCompleteType(Loc, Receiver->getType(),
2424f4a2713aSLionel Sambuc diag::err_incomplete_receiver_type))
2425f4a2713aSLionel Sambuc return ExprError();
2426f4a2713aSLionel Sambuc
2427f4a2713aSLionel Sambuc ExprResult result = PerformContextuallyConvertToObjCPointer(Receiver);
2428f4a2713aSLionel Sambuc if (result.isUsable()) {
2429*0a6a1f1dSLionel Sambuc Receiver = result.get();
2430f4a2713aSLionel Sambuc ReceiverType = Receiver->getType();
2431f4a2713aSLionel Sambuc }
2432f4a2713aSLionel Sambuc }
2433f4a2713aSLionel Sambuc }
2434f4a2713aSLionel Sambuc
2435f4a2713aSLionel Sambuc // There's a somewhat weird interaction here where we assume that we
2436f4a2713aSLionel Sambuc // won't actually have a method unless we also don't need to do some
2437f4a2713aSLionel Sambuc // of the more detailed type-checking on the receiver.
2438f4a2713aSLionel Sambuc
2439f4a2713aSLionel Sambuc if (!Method) {
2440f4a2713aSLionel Sambuc // Handle messages to id.
2441f4a2713aSLionel Sambuc bool receiverIsId = ReceiverType->isObjCIdType();
2442f4a2713aSLionel Sambuc if (receiverIsId || ReceiverType->isBlockPointerType() ||
2443f4a2713aSLionel Sambuc (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
2444f4a2713aSLionel Sambuc Method = LookupInstanceMethodInGlobalPool(Sel,
2445f4a2713aSLionel Sambuc SourceRange(LBracLoc, RBracLoc),
2446f4a2713aSLionel Sambuc receiverIsId);
2447f4a2713aSLionel Sambuc if (!Method)
2448f4a2713aSLionel Sambuc Method = LookupFactoryMethodInGlobalPool(Sel,
2449f4a2713aSLionel Sambuc SourceRange(LBracLoc,RBracLoc),
2450f4a2713aSLionel Sambuc receiverIsId);
2451*0a6a1f1dSLionel Sambuc if (Method) {
2452*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *BestMethod =
2453*0a6a1f1dSLionel Sambuc SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod()))
2454*0a6a1f1dSLionel Sambuc Method = BestMethod;
2455*0a6a1f1dSLionel Sambuc if (!AreMultipleMethodsInGlobalPool(Sel, Method->isInstanceMethod()))
2456*0a6a1f1dSLionel Sambuc DiagnoseUseOfDecl(Method, SelLoc);
2457*0a6a1f1dSLionel Sambuc }
2458f4a2713aSLionel Sambuc } else if (ReceiverType->isObjCClassType() ||
2459f4a2713aSLionel Sambuc ReceiverType->isObjCQualifiedClassType()) {
2460f4a2713aSLionel Sambuc // Handle messages to Class.
2461f4a2713aSLionel Sambuc // We allow sending a message to a qualified Class ("Class<foo>"), which
2462*0a6a1f1dSLionel Sambuc // is ok as long as one of the protocols implements the selector (if not,
2463*0a6a1f1dSLionel Sambuc // warn).
2464f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *QClassTy
2465f4a2713aSLionel Sambuc = ReceiverType->getAsObjCQualifiedClassType()) {
2466f4a2713aSLionel Sambuc // Search protocols for class methods.
2467f4a2713aSLionel Sambuc Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
2468f4a2713aSLionel Sambuc if (!Method) {
2469f4a2713aSLionel Sambuc Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
2470f4a2713aSLionel Sambuc // warn if instance method found for a Class message.
2471f4a2713aSLionel Sambuc if (Method) {
2472f4a2713aSLionel Sambuc Diag(SelLoc, diag::warn_instance_method_on_class_found)
2473f4a2713aSLionel Sambuc << Method->getSelector() << Sel;
2474f4a2713aSLionel Sambuc Diag(Method->getLocation(), diag::note_method_declared_at)
2475f4a2713aSLionel Sambuc << Method->getDeclName();
2476f4a2713aSLionel Sambuc }
2477f4a2713aSLionel Sambuc }
2478f4a2713aSLionel Sambuc } else {
2479f4a2713aSLionel Sambuc if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
2480f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
2481f4a2713aSLionel Sambuc // First check the public methods in the class interface.
2482f4a2713aSLionel Sambuc Method = ClassDecl->lookupClassMethod(Sel);
2483f4a2713aSLionel Sambuc
2484f4a2713aSLionel Sambuc if (!Method)
2485f4a2713aSLionel Sambuc Method = ClassDecl->lookupPrivateClassMethod(Sel);
2486f4a2713aSLionel Sambuc }
2487f4a2713aSLionel Sambuc if (Method && DiagnoseUseOfDecl(Method, SelLoc))
2488f4a2713aSLionel Sambuc return ExprError();
2489f4a2713aSLionel Sambuc }
2490f4a2713aSLionel Sambuc if (!Method) {
2491f4a2713aSLionel Sambuc // If not messaging 'self', look for any factory method named 'Sel'.
2492f4a2713aSLionel Sambuc if (!Receiver || !isSelfExpr(Receiver)) {
2493f4a2713aSLionel Sambuc Method = LookupFactoryMethodInGlobalPool(Sel,
2494f4a2713aSLionel Sambuc SourceRange(LBracLoc, RBracLoc),
2495f4a2713aSLionel Sambuc true);
2496f4a2713aSLionel Sambuc if (!Method) {
2497f4a2713aSLionel Sambuc // If no class (factory) method was found, check if an _instance_
2498f4a2713aSLionel Sambuc // method of the same name exists in the root class only.
2499f4a2713aSLionel Sambuc Method = LookupInstanceMethodInGlobalPool(Sel,
2500f4a2713aSLionel Sambuc SourceRange(LBracLoc, RBracLoc),
2501f4a2713aSLionel Sambuc true);
2502f4a2713aSLionel Sambuc if (Method)
2503f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *ID =
2504f4a2713aSLionel Sambuc dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
2505f4a2713aSLionel Sambuc if (ID->getSuperClass())
2506f4a2713aSLionel Sambuc Diag(SelLoc, diag::warn_root_inst_method_not_found)
2507f4a2713aSLionel Sambuc << Sel << SourceRange(LBracLoc, RBracLoc);
2508f4a2713aSLionel Sambuc }
2509f4a2713aSLionel Sambuc }
2510*0a6a1f1dSLionel Sambuc if (Method)
2511*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *BestMethod =
2512*0a6a1f1dSLionel Sambuc SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod()))
2513*0a6a1f1dSLionel Sambuc Method = BestMethod;
2514f4a2713aSLionel Sambuc }
2515f4a2713aSLionel Sambuc }
2516f4a2713aSLionel Sambuc }
2517f4a2713aSLionel Sambuc } else {
2518*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *ClassDecl = nullptr;
2519f4a2713aSLionel Sambuc
2520f4a2713aSLionel Sambuc // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
2521f4a2713aSLionel Sambuc // long as one of the protocols implements the selector (if not, warn).
2522f4a2713aSLionel Sambuc // And as long as message is not deprecated/unavailable (warn if it is).
2523f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *QIdTy
2524f4a2713aSLionel Sambuc = ReceiverType->getAsObjCQualifiedIdType()) {
2525f4a2713aSLionel Sambuc // Search protocols for instance methods.
2526f4a2713aSLionel Sambuc Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
2527f4a2713aSLionel Sambuc if (!Method)
2528f4a2713aSLionel Sambuc Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
2529f4a2713aSLionel Sambuc if (Method && DiagnoseUseOfDecl(Method, SelLoc))
2530f4a2713aSLionel Sambuc return ExprError();
2531f4a2713aSLionel Sambuc } else if (const ObjCObjectPointerType *OCIType
2532f4a2713aSLionel Sambuc = ReceiverType->getAsObjCInterfacePointerType()) {
2533f4a2713aSLionel Sambuc // We allow sending a message to a pointer to an interface (an object).
2534f4a2713aSLionel Sambuc ClassDecl = OCIType->getInterfaceDecl();
2535f4a2713aSLionel Sambuc
2536f4a2713aSLionel Sambuc // Try to complete the type. Under ARC, this is a hard error from which
2537f4a2713aSLionel Sambuc // we don't try to recover.
2538*0a6a1f1dSLionel Sambuc const ObjCInterfaceDecl *forwardClass = nullptr;
2539f4a2713aSLionel Sambuc if (RequireCompleteType(Loc, OCIType->getPointeeType(),
2540f4a2713aSLionel Sambuc getLangOpts().ObjCAutoRefCount
2541f4a2713aSLionel Sambuc ? diag::err_arc_receiver_forward_instance
2542f4a2713aSLionel Sambuc : diag::warn_receiver_forward_instance,
2543f4a2713aSLionel Sambuc Receiver? Receiver->getSourceRange()
2544f4a2713aSLionel Sambuc : SourceRange(SuperLoc))) {
2545f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount)
2546f4a2713aSLionel Sambuc return ExprError();
2547f4a2713aSLionel Sambuc
2548f4a2713aSLionel Sambuc forwardClass = OCIType->getInterfaceDecl();
2549f4a2713aSLionel Sambuc Diag(Receiver ? Receiver->getLocStart()
2550f4a2713aSLionel Sambuc : SuperLoc, diag::note_receiver_is_id);
2551*0a6a1f1dSLionel Sambuc Method = nullptr;
2552f4a2713aSLionel Sambuc } else {
2553f4a2713aSLionel Sambuc Method = ClassDecl->lookupInstanceMethod(Sel);
2554f4a2713aSLionel Sambuc }
2555f4a2713aSLionel Sambuc
2556f4a2713aSLionel Sambuc if (!Method)
2557f4a2713aSLionel Sambuc // Search protocol qualifiers.
2558f4a2713aSLionel Sambuc Method = LookupMethodInQualifiedType(Sel, OCIType, true);
2559f4a2713aSLionel Sambuc
2560f4a2713aSLionel Sambuc if (!Method) {
2561f4a2713aSLionel Sambuc // If we have implementations in scope, check "private" methods.
2562f4a2713aSLionel Sambuc Method = ClassDecl->lookupPrivateMethod(Sel);
2563f4a2713aSLionel Sambuc
2564f4a2713aSLionel Sambuc if (!Method && getLangOpts().ObjCAutoRefCount) {
2565f4a2713aSLionel Sambuc Diag(SelLoc, diag::err_arc_may_not_respond)
2566f4a2713aSLionel Sambuc << OCIType->getPointeeType() << Sel << RecRange
2567f4a2713aSLionel Sambuc << SourceRange(SelectorLocs.front(), SelectorLocs.back());
2568f4a2713aSLionel Sambuc return ExprError();
2569f4a2713aSLionel Sambuc }
2570f4a2713aSLionel Sambuc
2571f4a2713aSLionel Sambuc if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
2572f4a2713aSLionel Sambuc // If we still haven't found a method, look in the global pool. This
2573f4a2713aSLionel Sambuc // behavior isn't very desirable, however we need it for GCC
2574f4a2713aSLionel Sambuc // compatibility. FIXME: should we deviate??
2575f4a2713aSLionel Sambuc if (OCIType->qual_empty()) {
2576f4a2713aSLionel Sambuc Method = LookupInstanceMethodInGlobalPool(Sel,
2577f4a2713aSLionel Sambuc SourceRange(LBracLoc, RBracLoc));
2578f4a2713aSLionel Sambuc if (Method && !forwardClass)
2579f4a2713aSLionel Sambuc Diag(SelLoc, diag::warn_maynot_respond)
2580f4a2713aSLionel Sambuc << OCIType->getInterfaceDecl()->getIdentifier()
2581f4a2713aSLionel Sambuc << Sel << RecRange;
2582f4a2713aSLionel Sambuc }
2583f4a2713aSLionel Sambuc }
2584f4a2713aSLionel Sambuc }
2585f4a2713aSLionel Sambuc if (Method && DiagnoseUseOfDecl(Method, SelLoc, forwardClass))
2586f4a2713aSLionel Sambuc return ExprError();
2587f4a2713aSLionel Sambuc } else {
2588f4a2713aSLionel Sambuc // Reject other random receiver types (e.g. structs).
2589f4a2713aSLionel Sambuc Diag(Loc, diag::err_bad_receiver_type)
2590f4a2713aSLionel Sambuc << ReceiverType << Receiver->getSourceRange();
2591f4a2713aSLionel Sambuc return ExprError();
2592f4a2713aSLionel Sambuc }
2593f4a2713aSLionel Sambuc }
2594f4a2713aSLionel Sambuc }
2595f4a2713aSLionel Sambuc
2596*0a6a1f1dSLionel Sambuc FunctionScopeInfo *DIFunctionScopeInfo =
2597*0a6a1f1dSLionel Sambuc (Method && Method->getMethodFamily() == OMF_init)
2598*0a6a1f1dSLionel Sambuc ? getEnclosingFunction() : nullptr;
2599*0a6a1f1dSLionel Sambuc
2600*0a6a1f1dSLionel Sambuc if (DIFunctionScopeInfo &&
2601*0a6a1f1dSLionel Sambuc DIFunctionScopeInfo->ObjCIsDesignatedInit &&
2602*0a6a1f1dSLionel Sambuc (SuperLoc.isValid() || isSelfExpr(Receiver))) {
2603*0a6a1f1dSLionel Sambuc bool isDesignatedInitChain = false;
2604*0a6a1f1dSLionel Sambuc if (SuperLoc.isValid()) {
2605*0a6a1f1dSLionel Sambuc if (const ObjCObjectPointerType *
2606*0a6a1f1dSLionel Sambuc OCIType = ReceiverType->getAsObjCInterfacePointerType()) {
2607*0a6a1f1dSLionel Sambuc if (const ObjCInterfaceDecl *ID = OCIType->getInterfaceDecl()) {
2608*0a6a1f1dSLionel Sambuc // Either we know this is a designated initializer or we
2609*0a6a1f1dSLionel Sambuc // conservatively assume it because we don't know for sure.
2610*0a6a1f1dSLionel Sambuc if (!ID->declaresOrInheritsDesignatedInitializers() ||
2611*0a6a1f1dSLionel Sambuc ID->isDesignatedInitializer(Sel)) {
2612*0a6a1f1dSLionel Sambuc isDesignatedInitChain = true;
2613*0a6a1f1dSLionel Sambuc DIFunctionScopeInfo->ObjCWarnForNoDesignatedInitChain = false;
2614*0a6a1f1dSLionel Sambuc }
2615*0a6a1f1dSLionel Sambuc }
2616*0a6a1f1dSLionel Sambuc }
2617*0a6a1f1dSLionel Sambuc }
2618*0a6a1f1dSLionel Sambuc if (!isDesignatedInitChain) {
2619*0a6a1f1dSLionel Sambuc const ObjCMethodDecl *InitMethod = nullptr;
2620*0a6a1f1dSLionel Sambuc bool isDesignated =
2621*0a6a1f1dSLionel Sambuc getCurMethodDecl()->isDesignatedInitializerForTheInterface(&InitMethod);
2622*0a6a1f1dSLionel Sambuc assert(isDesignated && InitMethod);
2623*0a6a1f1dSLionel Sambuc (void)isDesignated;
2624*0a6a1f1dSLionel Sambuc Diag(SelLoc, SuperLoc.isValid() ?
2625*0a6a1f1dSLionel Sambuc diag::warn_objc_designated_init_non_designated_init_call :
2626*0a6a1f1dSLionel Sambuc diag::warn_objc_designated_init_non_super_designated_init_call);
2627*0a6a1f1dSLionel Sambuc Diag(InitMethod->getLocation(),
2628*0a6a1f1dSLionel Sambuc diag::note_objc_designated_init_marked_here);
2629*0a6a1f1dSLionel Sambuc }
2630*0a6a1f1dSLionel Sambuc }
2631*0a6a1f1dSLionel Sambuc
2632*0a6a1f1dSLionel Sambuc if (DIFunctionScopeInfo &&
2633*0a6a1f1dSLionel Sambuc DIFunctionScopeInfo->ObjCIsSecondaryInit &&
2634*0a6a1f1dSLionel Sambuc (SuperLoc.isValid() || isSelfExpr(Receiver))) {
2635*0a6a1f1dSLionel Sambuc if (SuperLoc.isValid()) {
2636*0a6a1f1dSLionel Sambuc Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call);
2637*0a6a1f1dSLionel Sambuc } else {
2638*0a6a1f1dSLionel Sambuc DIFunctionScopeInfo->ObjCWarnForNoInitDelegation = false;
2639*0a6a1f1dSLionel Sambuc }
2640*0a6a1f1dSLionel Sambuc }
2641*0a6a1f1dSLionel Sambuc
2642f4a2713aSLionel Sambuc // Check the message arguments.
2643f4a2713aSLionel Sambuc unsigned NumArgs = ArgsIn.size();
2644f4a2713aSLionel Sambuc Expr **Args = ArgsIn.data();
2645f4a2713aSLionel Sambuc QualType ReturnType;
2646f4a2713aSLionel Sambuc ExprValueKind VK = VK_RValue;
2647f4a2713aSLionel Sambuc bool ClassMessage = (ReceiverType->isObjCClassType() ||
2648f4a2713aSLionel Sambuc ReceiverType->isObjCQualifiedClassType());
2649f4a2713aSLionel Sambuc if (CheckMessageArgumentTypes(ReceiverType, MultiExprArg(Args, NumArgs),
2650f4a2713aSLionel Sambuc Sel, SelectorLocs, Method,
2651f4a2713aSLionel Sambuc ClassMessage, SuperLoc.isValid(),
2652*0a6a1f1dSLionel Sambuc LBracLoc, RBracLoc, RecRange, ReturnType, VK))
2653f4a2713aSLionel Sambuc return ExprError();
2654f4a2713aSLionel Sambuc
2655*0a6a1f1dSLionel Sambuc if (Method && !Method->getReturnType()->isVoidType() &&
2656*0a6a1f1dSLionel Sambuc RequireCompleteType(LBracLoc, Method->getReturnType(),
2657f4a2713aSLionel Sambuc diag::err_illegal_message_expr_incomplete_type))
2658f4a2713aSLionel Sambuc return ExprError();
2659f4a2713aSLionel Sambuc
2660f4a2713aSLionel Sambuc // In ARC, forbid the user from sending messages to
2661f4a2713aSLionel Sambuc // retain/release/autorelease/dealloc/retainCount explicitly.
2662f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount) {
2663f4a2713aSLionel Sambuc ObjCMethodFamily family =
2664f4a2713aSLionel Sambuc (Method ? Method->getMethodFamily() : Sel.getMethodFamily());
2665f4a2713aSLionel Sambuc switch (family) {
2666f4a2713aSLionel Sambuc case OMF_init:
2667f4a2713aSLionel Sambuc if (Method)
2668f4a2713aSLionel Sambuc checkInitMethod(Method, ReceiverType);
2669f4a2713aSLionel Sambuc
2670f4a2713aSLionel Sambuc case OMF_None:
2671f4a2713aSLionel Sambuc case OMF_alloc:
2672f4a2713aSLionel Sambuc case OMF_copy:
2673f4a2713aSLionel Sambuc case OMF_finalize:
2674f4a2713aSLionel Sambuc case OMF_mutableCopy:
2675f4a2713aSLionel Sambuc case OMF_new:
2676f4a2713aSLionel Sambuc case OMF_self:
2677*0a6a1f1dSLionel Sambuc case OMF_initialize:
2678f4a2713aSLionel Sambuc break;
2679f4a2713aSLionel Sambuc
2680f4a2713aSLionel Sambuc case OMF_dealloc:
2681f4a2713aSLionel Sambuc case OMF_retain:
2682f4a2713aSLionel Sambuc case OMF_release:
2683f4a2713aSLionel Sambuc case OMF_autorelease:
2684f4a2713aSLionel Sambuc case OMF_retainCount:
2685f4a2713aSLionel Sambuc Diag(SelLoc, diag::err_arc_illegal_explicit_message)
2686f4a2713aSLionel Sambuc << Sel << RecRange;
2687f4a2713aSLionel Sambuc break;
2688f4a2713aSLionel Sambuc
2689f4a2713aSLionel Sambuc case OMF_performSelector:
2690f4a2713aSLionel Sambuc if (Method && NumArgs >= 1) {
2691f4a2713aSLionel Sambuc if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) {
2692f4a2713aSLionel Sambuc Selector ArgSel = SelExp->getSelector();
2693f4a2713aSLionel Sambuc ObjCMethodDecl *SelMethod =
2694f4a2713aSLionel Sambuc LookupInstanceMethodInGlobalPool(ArgSel,
2695f4a2713aSLionel Sambuc SelExp->getSourceRange());
2696f4a2713aSLionel Sambuc if (!SelMethod)
2697f4a2713aSLionel Sambuc SelMethod =
2698f4a2713aSLionel Sambuc LookupFactoryMethodInGlobalPool(ArgSel,
2699f4a2713aSLionel Sambuc SelExp->getSourceRange());
2700f4a2713aSLionel Sambuc if (SelMethod) {
2701f4a2713aSLionel Sambuc ObjCMethodFamily SelFamily = SelMethod->getMethodFamily();
2702f4a2713aSLionel Sambuc switch (SelFamily) {
2703f4a2713aSLionel Sambuc case OMF_alloc:
2704f4a2713aSLionel Sambuc case OMF_copy:
2705f4a2713aSLionel Sambuc case OMF_mutableCopy:
2706f4a2713aSLionel Sambuc case OMF_new:
2707f4a2713aSLionel Sambuc case OMF_self:
2708f4a2713aSLionel Sambuc case OMF_init:
2709f4a2713aSLionel Sambuc // Issue error, unless ns_returns_not_retained.
2710f4a2713aSLionel Sambuc if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
2711f4a2713aSLionel Sambuc // selector names a +1 method
2712f4a2713aSLionel Sambuc Diag(SelLoc,
2713f4a2713aSLionel Sambuc diag::err_arc_perform_selector_retains);
2714f4a2713aSLionel Sambuc Diag(SelMethod->getLocation(), diag::note_method_declared_at)
2715f4a2713aSLionel Sambuc << SelMethod->getDeclName();
2716f4a2713aSLionel Sambuc }
2717f4a2713aSLionel Sambuc break;
2718f4a2713aSLionel Sambuc default:
2719f4a2713aSLionel Sambuc // +0 call. OK. unless ns_returns_retained.
2720f4a2713aSLionel Sambuc if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) {
2721f4a2713aSLionel Sambuc // selector names a +1 method
2722f4a2713aSLionel Sambuc Diag(SelLoc,
2723f4a2713aSLionel Sambuc diag::err_arc_perform_selector_retains);
2724f4a2713aSLionel Sambuc Diag(SelMethod->getLocation(), diag::note_method_declared_at)
2725f4a2713aSLionel Sambuc << SelMethod->getDeclName();
2726f4a2713aSLionel Sambuc }
2727f4a2713aSLionel Sambuc break;
2728f4a2713aSLionel Sambuc }
2729f4a2713aSLionel Sambuc }
2730f4a2713aSLionel Sambuc } else {
2731f4a2713aSLionel Sambuc // error (may leak).
2732f4a2713aSLionel Sambuc Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
2733f4a2713aSLionel Sambuc Diag(Args[0]->getExprLoc(), diag::note_used_here);
2734f4a2713aSLionel Sambuc }
2735f4a2713aSLionel Sambuc }
2736f4a2713aSLionel Sambuc break;
2737f4a2713aSLionel Sambuc }
2738f4a2713aSLionel Sambuc }
2739f4a2713aSLionel Sambuc
2740*0a6a1f1dSLionel Sambuc DiagnoseCStringFormatDirectiveInObjCAPI(*this, Method, Sel, Args, NumArgs);
2741*0a6a1f1dSLionel Sambuc
2742f4a2713aSLionel Sambuc // Construct the appropriate ObjCMessageExpr instance.
2743f4a2713aSLionel Sambuc ObjCMessageExpr *Result;
2744f4a2713aSLionel Sambuc if (SuperLoc.isValid())
2745f4a2713aSLionel Sambuc Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
2746f4a2713aSLionel Sambuc SuperLoc, /*IsInstanceSuper=*/true,
2747f4a2713aSLionel Sambuc ReceiverType, Sel, SelectorLocs, Method,
2748f4a2713aSLionel Sambuc makeArrayRef(Args, NumArgs), RBracLoc,
2749f4a2713aSLionel Sambuc isImplicit);
2750f4a2713aSLionel Sambuc else {
2751f4a2713aSLionel Sambuc Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
2752f4a2713aSLionel Sambuc Receiver, Sel, SelectorLocs, Method,
2753f4a2713aSLionel Sambuc makeArrayRef(Args, NumArgs), RBracLoc,
2754f4a2713aSLionel Sambuc isImplicit);
2755f4a2713aSLionel Sambuc if (!isImplicit)
2756f4a2713aSLionel Sambuc checkCocoaAPI(*this, Result);
2757f4a2713aSLionel Sambuc }
2758f4a2713aSLionel Sambuc
2759f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount) {
2760*0a6a1f1dSLionel Sambuc // Do not warn about IBOutlet weak property receivers being set to null
2761*0a6a1f1dSLionel Sambuc // as this cannot asynchronously happen.
2762*0a6a1f1dSLionel Sambuc bool WarnWeakReceiver = true;
2763*0a6a1f1dSLionel Sambuc if (isImplicit && Method)
2764*0a6a1f1dSLionel Sambuc if (const ObjCPropertyDecl *PropertyDecl = Method->findPropertyDecl())
2765*0a6a1f1dSLionel Sambuc WarnWeakReceiver = !PropertyDecl->hasAttr<IBOutletAttr>();
2766*0a6a1f1dSLionel Sambuc if (WarnWeakReceiver)
2767f4a2713aSLionel Sambuc DiagnoseARCUseOfWeakReceiver(*this, Receiver);
2768f4a2713aSLionel Sambuc
2769f4a2713aSLionel Sambuc // In ARC, annotate delegate init calls.
2770f4a2713aSLionel Sambuc if (Result->getMethodFamily() == OMF_init &&
2771f4a2713aSLionel Sambuc (SuperLoc.isValid() || isSelfExpr(Receiver))) {
2772f4a2713aSLionel Sambuc // Only consider init calls *directly* in init implementations,
2773f4a2713aSLionel Sambuc // not within blocks.
2774f4a2713aSLionel Sambuc ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext);
2775f4a2713aSLionel Sambuc if (method && method->getMethodFamily() == OMF_init) {
2776f4a2713aSLionel Sambuc // The implicit assignment to self means we also don't want to
2777f4a2713aSLionel Sambuc // consume the result.
2778f4a2713aSLionel Sambuc Result->setDelegateInitCall(true);
2779*0a6a1f1dSLionel Sambuc return Result;
2780f4a2713aSLionel Sambuc }
2781f4a2713aSLionel Sambuc }
2782f4a2713aSLionel Sambuc
2783f4a2713aSLionel Sambuc // In ARC, check for message sends which are likely to introduce
2784f4a2713aSLionel Sambuc // retain cycles.
2785f4a2713aSLionel Sambuc checkRetainCycles(Result);
2786f4a2713aSLionel Sambuc
2787f4a2713aSLionel Sambuc if (!isImplicit && Method) {
2788f4a2713aSLionel Sambuc if (const ObjCPropertyDecl *Prop = Method->findPropertyDecl()) {
2789f4a2713aSLionel Sambuc bool IsWeak =
2790f4a2713aSLionel Sambuc Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak;
2791f4a2713aSLionel Sambuc if (!IsWeak && Sel.isUnarySelector())
2792f4a2713aSLionel Sambuc IsWeak = ReturnType.getObjCLifetime() & Qualifiers::OCL_Weak;
2793*0a6a1f1dSLionel Sambuc if (IsWeak &&
2794*0a6a1f1dSLionel Sambuc !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, LBracLoc))
2795f4a2713aSLionel Sambuc getCurFunction()->recordUseOfWeak(Result, Prop);
2796f4a2713aSLionel Sambuc }
2797f4a2713aSLionel Sambuc }
2798f4a2713aSLionel Sambuc }
2799f4a2713aSLionel Sambuc
2800f4a2713aSLionel Sambuc return MaybeBindToTemporary(Result);
2801f4a2713aSLionel Sambuc }
2802f4a2713aSLionel Sambuc
RemoveSelectorFromWarningCache(Sema & S,Expr * Arg)2803f4a2713aSLionel Sambuc static void RemoveSelectorFromWarningCache(Sema &S, Expr* Arg) {
2804f4a2713aSLionel Sambuc if (ObjCSelectorExpr *OSE =
2805f4a2713aSLionel Sambuc dyn_cast<ObjCSelectorExpr>(Arg->IgnoreParenCasts())) {
2806f4a2713aSLionel Sambuc Selector Sel = OSE->getSelector();
2807f4a2713aSLionel Sambuc SourceLocation Loc = OSE->getAtLoc();
2808f4a2713aSLionel Sambuc llvm::DenseMap<Selector, SourceLocation>::iterator Pos
2809f4a2713aSLionel Sambuc = S.ReferencedSelectors.find(Sel);
2810f4a2713aSLionel Sambuc if (Pos != S.ReferencedSelectors.end() && Pos->second == Loc)
2811f4a2713aSLionel Sambuc S.ReferencedSelectors.erase(Pos);
2812f4a2713aSLionel Sambuc }
2813f4a2713aSLionel Sambuc }
2814f4a2713aSLionel Sambuc
2815f4a2713aSLionel Sambuc // ActOnInstanceMessage - used for both unary and keyword messages.
2816f4a2713aSLionel Sambuc // ArgExprs is optional - if it is present, the number of expressions
2817f4a2713aSLionel Sambuc // is obtained from Sel.getNumArgs().
ActOnInstanceMessage(Scope * S,Expr * Receiver,Selector Sel,SourceLocation LBracLoc,ArrayRef<SourceLocation> SelectorLocs,SourceLocation RBracLoc,MultiExprArg Args)2818f4a2713aSLionel Sambuc ExprResult Sema::ActOnInstanceMessage(Scope *S,
2819f4a2713aSLionel Sambuc Expr *Receiver,
2820f4a2713aSLionel Sambuc Selector Sel,
2821f4a2713aSLionel Sambuc SourceLocation LBracLoc,
2822f4a2713aSLionel Sambuc ArrayRef<SourceLocation> SelectorLocs,
2823f4a2713aSLionel Sambuc SourceLocation RBracLoc,
2824f4a2713aSLionel Sambuc MultiExprArg Args) {
2825f4a2713aSLionel Sambuc if (!Receiver)
2826f4a2713aSLionel Sambuc return ExprError();
2827f4a2713aSLionel Sambuc
2828f4a2713aSLionel Sambuc // A ParenListExpr can show up while doing error recovery with invalid code.
2829f4a2713aSLionel Sambuc if (isa<ParenListExpr>(Receiver)) {
2830f4a2713aSLionel Sambuc ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Receiver);
2831f4a2713aSLionel Sambuc if (Result.isInvalid()) return ExprError();
2832*0a6a1f1dSLionel Sambuc Receiver = Result.get();
2833f4a2713aSLionel Sambuc }
2834f4a2713aSLionel Sambuc
2835f4a2713aSLionel Sambuc if (RespondsToSelectorSel.isNull()) {
2836f4a2713aSLionel Sambuc IdentifierInfo *SelectorId = &Context.Idents.get("respondsToSelector");
2837f4a2713aSLionel Sambuc RespondsToSelectorSel = Context.Selectors.getUnarySelector(SelectorId);
2838f4a2713aSLionel Sambuc }
2839f4a2713aSLionel Sambuc if (Sel == RespondsToSelectorSel)
2840f4a2713aSLionel Sambuc RemoveSelectorFromWarningCache(*this, Args[0]);
2841f4a2713aSLionel Sambuc
2842f4a2713aSLionel Sambuc return BuildInstanceMessage(Receiver, Receiver->getType(),
2843*0a6a1f1dSLionel Sambuc /*SuperLoc=*/SourceLocation(), Sel,
2844*0a6a1f1dSLionel Sambuc /*Method=*/nullptr, LBracLoc, SelectorLocs,
2845*0a6a1f1dSLionel Sambuc RBracLoc, Args);
2846f4a2713aSLionel Sambuc }
2847f4a2713aSLionel Sambuc
2848f4a2713aSLionel Sambuc enum ARCConversionTypeClass {
2849f4a2713aSLionel Sambuc /// int, void, struct A
2850f4a2713aSLionel Sambuc ACTC_none,
2851f4a2713aSLionel Sambuc
2852f4a2713aSLionel Sambuc /// id, void (^)()
2853f4a2713aSLionel Sambuc ACTC_retainable,
2854f4a2713aSLionel Sambuc
2855f4a2713aSLionel Sambuc /// id*, id***, void (^*)(),
2856f4a2713aSLionel Sambuc ACTC_indirectRetainable,
2857f4a2713aSLionel Sambuc
2858f4a2713aSLionel Sambuc /// void* might be a normal C type, or it might a CF type.
2859f4a2713aSLionel Sambuc ACTC_voidPtr,
2860f4a2713aSLionel Sambuc
2861f4a2713aSLionel Sambuc /// struct A*
2862f4a2713aSLionel Sambuc ACTC_coreFoundation
2863f4a2713aSLionel Sambuc };
isAnyRetainable(ARCConversionTypeClass ACTC)2864f4a2713aSLionel Sambuc static bool isAnyRetainable(ARCConversionTypeClass ACTC) {
2865f4a2713aSLionel Sambuc return (ACTC == ACTC_retainable ||
2866f4a2713aSLionel Sambuc ACTC == ACTC_coreFoundation ||
2867f4a2713aSLionel Sambuc ACTC == ACTC_voidPtr);
2868f4a2713aSLionel Sambuc }
isAnyCLike(ARCConversionTypeClass ACTC)2869f4a2713aSLionel Sambuc static bool isAnyCLike(ARCConversionTypeClass ACTC) {
2870f4a2713aSLionel Sambuc return ACTC == ACTC_none ||
2871f4a2713aSLionel Sambuc ACTC == ACTC_voidPtr ||
2872f4a2713aSLionel Sambuc ACTC == ACTC_coreFoundation;
2873f4a2713aSLionel Sambuc }
2874f4a2713aSLionel Sambuc
classifyTypeForARCConversion(QualType type)2875f4a2713aSLionel Sambuc static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) {
2876f4a2713aSLionel Sambuc bool isIndirect = false;
2877f4a2713aSLionel Sambuc
2878f4a2713aSLionel Sambuc // Ignore an outermost reference type.
2879f4a2713aSLionel Sambuc if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
2880f4a2713aSLionel Sambuc type = ref->getPointeeType();
2881f4a2713aSLionel Sambuc isIndirect = true;
2882f4a2713aSLionel Sambuc }
2883f4a2713aSLionel Sambuc
2884f4a2713aSLionel Sambuc // Drill through pointers and arrays recursively.
2885f4a2713aSLionel Sambuc while (true) {
2886f4a2713aSLionel Sambuc if (const PointerType *ptr = type->getAs<PointerType>()) {
2887f4a2713aSLionel Sambuc type = ptr->getPointeeType();
2888f4a2713aSLionel Sambuc
2889f4a2713aSLionel Sambuc // The first level of pointer may be the innermost pointer on a CF type.
2890f4a2713aSLionel Sambuc if (!isIndirect) {
2891f4a2713aSLionel Sambuc if (type->isVoidType()) return ACTC_voidPtr;
2892f4a2713aSLionel Sambuc if (type->isRecordType()) return ACTC_coreFoundation;
2893f4a2713aSLionel Sambuc }
2894f4a2713aSLionel Sambuc } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) {
2895f4a2713aSLionel Sambuc type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
2896f4a2713aSLionel Sambuc } else {
2897f4a2713aSLionel Sambuc break;
2898f4a2713aSLionel Sambuc }
2899f4a2713aSLionel Sambuc isIndirect = true;
2900f4a2713aSLionel Sambuc }
2901f4a2713aSLionel Sambuc
2902f4a2713aSLionel Sambuc if (isIndirect) {
2903f4a2713aSLionel Sambuc if (type->isObjCARCBridgableType())
2904f4a2713aSLionel Sambuc return ACTC_indirectRetainable;
2905f4a2713aSLionel Sambuc return ACTC_none;
2906f4a2713aSLionel Sambuc }
2907f4a2713aSLionel Sambuc
2908f4a2713aSLionel Sambuc if (type->isObjCARCBridgableType())
2909f4a2713aSLionel Sambuc return ACTC_retainable;
2910f4a2713aSLionel Sambuc
2911f4a2713aSLionel Sambuc return ACTC_none;
2912f4a2713aSLionel Sambuc }
2913f4a2713aSLionel Sambuc
2914f4a2713aSLionel Sambuc namespace {
2915f4a2713aSLionel Sambuc /// A result from the cast checker.
2916f4a2713aSLionel Sambuc enum ACCResult {
2917f4a2713aSLionel Sambuc /// Cannot be casted.
2918f4a2713aSLionel Sambuc ACC_invalid,
2919f4a2713aSLionel Sambuc
2920f4a2713aSLionel Sambuc /// Can be safely retained or not retained.
2921f4a2713aSLionel Sambuc ACC_bottom,
2922f4a2713aSLionel Sambuc
2923f4a2713aSLionel Sambuc /// Can be casted at +0.
2924f4a2713aSLionel Sambuc ACC_plusZero,
2925f4a2713aSLionel Sambuc
2926f4a2713aSLionel Sambuc /// Can be casted at +1.
2927f4a2713aSLionel Sambuc ACC_plusOne
2928f4a2713aSLionel Sambuc };
merge(ACCResult left,ACCResult right)2929f4a2713aSLionel Sambuc ACCResult merge(ACCResult left, ACCResult right) {
2930f4a2713aSLionel Sambuc if (left == right) return left;
2931f4a2713aSLionel Sambuc if (left == ACC_bottom) return right;
2932f4a2713aSLionel Sambuc if (right == ACC_bottom) return left;
2933f4a2713aSLionel Sambuc return ACC_invalid;
2934f4a2713aSLionel Sambuc }
2935f4a2713aSLionel Sambuc
2936f4a2713aSLionel Sambuc /// A checker which white-lists certain expressions whose conversion
2937f4a2713aSLionel Sambuc /// to or from retainable type would otherwise be forbidden in ARC.
2938f4a2713aSLionel Sambuc class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> {
2939f4a2713aSLionel Sambuc typedef StmtVisitor<ARCCastChecker, ACCResult> super;
2940f4a2713aSLionel Sambuc
2941f4a2713aSLionel Sambuc ASTContext &Context;
2942f4a2713aSLionel Sambuc ARCConversionTypeClass SourceClass;
2943f4a2713aSLionel Sambuc ARCConversionTypeClass TargetClass;
2944f4a2713aSLionel Sambuc bool Diagnose;
2945f4a2713aSLionel Sambuc
isCFType(QualType type)2946f4a2713aSLionel Sambuc static bool isCFType(QualType type) {
2947f4a2713aSLionel Sambuc // Someday this can use ns_bridged. For now, it has to do this.
2948f4a2713aSLionel Sambuc return type->isCARCBridgableType();
2949f4a2713aSLionel Sambuc }
2950f4a2713aSLionel Sambuc
2951f4a2713aSLionel Sambuc public:
ARCCastChecker(ASTContext & Context,ARCConversionTypeClass source,ARCConversionTypeClass target,bool diagnose)2952f4a2713aSLionel Sambuc ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source,
2953f4a2713aSLionel Sambuc ARCConversionTypeClass target, bool diagnose)
2954f4a2713aSLionel Sambuc : Context(Context), SourceClass(source), TargetClass(target),
2955f4a2713aSLionel Sambuc Diagnose(diagnose) {}
2956f4a2713aSLionel Sambuc
2957f4a2713aSLionel Sambuc using super::Visit;
Visit(Expr * e)2958f4a2713aSLionel Sambuc ACCResult Visit(Expr *e) {
2959f4a2713aSLionel Sambuc return super::Visit(e->IgnoreParens());
2960f4a2713aSLionel Sambuc }
2961f4a2713aSLionel Sambuc
VisitStmt(Stmt * s)2962f4a2713aSLionel Sambuc ACCResult VisitStmt(Stmt *s) {
2963f4a2713aSLionel Sambuc return ACC_invalid;
2964f4a2713aSLionel Sambuc }
2965f4a2713aSLionel Sambuc
2966f4a2713aSLionel Sambuc /// Null pointer constants can be casted however you please.
VisitExpr(Expr * e)2967f4a2713aSLionel Sambuc ACCResult VisitExpr(Expr *e) {
2968f4a2713aSLionel Sambuc if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
2969f4a2713aSLionel Sambuc return ACC_bottom;
2970f4a2713aSLionel Sambuc return ACC_invalid;
2971f4a2713aSLionel Sambuc }
2972f4a2713aSLionel Sambuc
2973f4a2713aSLionel Sambuc /// Objective-C string literals can be safely casted.
VisitObjCStringLiteral(ObjCStringLiteral * e)2974f4a2713aSLionel Sambuc ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) {
2975f4a2713aSLionel Sambuc // If we're casting to any retainable type, go ahead. Global
2976f4a2713aSLionel Sambuc // strings are immune to retains, so this is bottom.
2977f4a2713aSLionel Sambuc if (isAnyRetainable(TargetClass)) return ACC_bottom;
2978f4a2713aSLionel Sambuc
2979f4a2713aSLionel Sambuc return ACC_invalid;
2980f4a2713aSLionel Sambuc }
2981f4a2713aSLionel Sambuc
2982f4a2713aSLionel Sambuc /// Look through certain implicit and explicit casts.
VisitCastExpr(CastExpr * e)2983f4a2713aSLionel Sambuc ACCResult VisitCastExpr(CastExpr *e) {
2984f4a2713aSLionel Sambuc switch (e->getCastKind()) {
2985f4a2713aSLionel Sambuc case CK_NullToPointer:
2986f4a2713aSLionel Sambuc return ACC_bottom;
2987f4a2713aSLionel Sambuc
2988f4a2713aSLionel Sambuc case CK_NoOp:
2989f4a2713aSLionel Sambuc case CK_LValueToRValue:
2990f4a2713aSLionel Sambuc case CK_BitCast:
2991f4a2713aSLionel Sambuc case CK_CPointerToObjCPointerCast:
2992f4a2713aSLionel Sambuc case CK_BlockPointerToObjCPointerCast:
2993f4a2713aSLionel Sambuc case CK_AnyPointerToBlockPointerCast:
2994f4a2713aSLionel Sambuc return Visit(e->getSubExpr());
2995f4a2713aSLionel Sambuc
2996f4a2713aSLionel Sambuc default:
2997f4a2713aSLionel Sambuc return ACC_invalid;
2998f4a2713aSLionel Sambuc }
2999f4a2713aSLionel Sambuc }
3000f4a2713aSLionel Sambuc
3001f4a2713aSLionel Sambuc /// Look through unary extension.
VisitUnaryExtension(UnaryOperator * e)3002f4a2713aSLionel Sambuc ACCResult VisitUnaryExtension(UnaryOperator *e) {
3003f4a2713aSLionel Sambuc return Visit(e->getSubExpr());
3004f4a2713aSLionel Sambuc }
3005f4a2713aSLionel Sambuc
3006f4a2713aSLionel Sambuc /// Ignore the LHS of a comma operator.
VisitBinComma(BinaryOperator * e)3007f4a2713aSLionel Sambuc ACCResult VisitBinComma(BinaryOperator *e) {
3008f4a2713aSLionel Sambuc return Visit(e->getRHS());
3009f4a2713aSLionel Sambuc }
3010f4a2713aSLionel Sambuc
3011f4a2713aSLionel Sambuc /// Conditional operators are okay if both sides are okay.
VisitConditionalOperator(ConditionalOperator * e)3012f4a2713aSLionel Sambuc ACCResult VisitConditionalOperator(ConditionalOperator *e) {
3013f4a2713aSLionel Sambuc ACCResult left = Visit(e->getTrueExpr());
3014f4a2713aSLionel Sambuc if (left == ACC_invalid) return ACC_invalid;
3015f4a2713aSLionel Sambuc return merge(left, Visit(e->getFalseExpr()));
3016f4a2713aSLionel Sambuc }
3017f4a2713aSLionel Sambuc
3018f4a2713aSLionel Sambuc /// Look through pseudo-objects.
VisitPseudoObjectExpr(PseudoObjectExpr * e)3019f4a2713aSLionel Sambuc ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) {
3020f4a2713aSLionel Sambuc // If we're getting here, we should always have a result.
3021f4a2713aSLionel Sambuc return Visit(e->getResultExpr());
3022f4a2713aSLionel Sambuc }
3023f4a2713aSLionel Sambuc
3024f4a2713aSLionel Sambuc /// Statement expressions are okay if their result expression is okay.
VisitStmtExpr(StmtExpr * e)3025f4a2713aSLionel Sambuc ACCResult VisitStmtExpr(StmtExpr *e) {
3026f4a2713aSLionel Sambuc return Visit(e->getSubStmt()->body_back());
3027f4a2713aSLionel Sambuc }
3028f4a2713aSLionel Sambuc
3029f4a2713aSLionel Sambuc /// Some declaration references are okay.
VisitDeclRefExpr(DeclRefExpr * e)3030f4a2713aSLionel Sambuc ACCResult VisitDeclRefExpr(DeclRefExpr *e) {
3031f4a2713aSLionel Sambuc // References to global constants from system headers are okay.
3032f4a2713aSLionel Sambuc // These are things like 'kCFStringTransformToLatin'. They are
3033f4a2713aSLionel Sambuc // can also be assumed to be immune to retains.
3034f4a2713aSLionel Sambuc VarDecl *var = dyn_cast<VarDecl>(e->getDecl());
3035f4a2713aSLionel Sambuc if (isAnyRetainable(TargetClass) &&
3036f4a2713aSLionel Sambuc isAnyRetainable(SourceClass) &&
3037f4a2713aSLionel Sambuc var &&
3038f4a2713aSLionel Sambuc var->getStorageClass() == SC_Extern &&
3039f4a2713aSLionel Sambuc var->getType().isConstQualified() &&
3040f4a2713aSLionel Sambuc Context.getSourceManager().isInSystemHeader(var->getLocation())) {
3041f4a2713aSLionel Sambuc return ACC_bottom;
3042f4a2713aSLionel Sambuc }
3043f4a2713aSLionel Sambuc
3044f4a2713aSLionel Sambuc // Nothing else.
3045f4a2713aSLionel Sambuc return ACC_invalid;
3046f4a2713aSLionel Sambuc }
3047f4a2713aSLionel Sambuc
3048f4a2713aSLionel Sambuc /// Some calls are okay.
VisitCallExpr(CallExpr * e)3049f4a2713aSLionel Sambuc ACCResult VisitCallExpr(CallExpr *e) {
3050f4a2713aSLionel Sambuc if (FunctionDecl *fn = e->getDirectCallee())
3051f4a2713aSLionel Sambuc if (ACCResult result = checkCallToFunction(fn))
3052f4a2713aSLionel Sambuc return result;
3053f4a2713aSLionel Sambuc
3054f4a2713aSLionel Sambuc return super::VisitCallExpr(e);
3055f4a2713aSLionel Sambuc }
3056f4a2713aSLionel Sambuc
checkCallToFunction(FunctionDecl * fn)3057f4a2713aSLionel Sambuc ACCResult checkCallToFunction(FunctionDecl *fn) {
3058f4a2713aSLionel Sambuc // Require a CF*Ref return type.
3059*0a6a1f1dSLionel Sambuc if (!isCFType(fn->getReturnType()))
3060f4a2713aSLionel Sambuc return ACC_invalid;
3061f4a2713aSLionel Sambuc
3062f4a2713aSLionel Sambuc if (!isAnyRetainable(TargetClass))
3063f4a2713aSLionel Sambuc return ACC_invalid;
3064f4a2713aSLionel Sambuc
3065f4a2713aSLionel Sambuc // Honor an explicit 'not retained' attribute.
3066f4a2713aSLionel Sambuc if (fn->hasAttr<CFReturnsNotRetainedAttr>())
3067f4a2713aSLionel Sambuc return ACC_plusZero;
3068f4a2713aSLionel Sambuc
3069f4a2713aSLionel Sambuc // Honor an explicit 'retained' attribute, except that for
3070f4a2713aSLionel Sambuc // now we're not going to permit implicit handling of +1 results,
3071f4a2713aSLionel Sambuc // because it's a bit frightening.
3072f4a2713aSLionel Sambuc if (fn->hasAttr<CFReturnsRetainedAttr>())
3073f4a2713aSLionel Sambuc return Diagnose ? ACC_plusOne
3074f4a2713aSLionel Sambuc : ACC_invalid; // ACC_plusOne if we start accepting this
3075f4a2713aSLionel Sambuc
3076f4a2713aSLionel Sambuc // Recognize this specific builtin function, which is used by CFSTR.
3077f4a2713aSLionel Sambuc unsigned builtinID = fn->getBuiltinID();
3078f4a2713aSLionel Sambuc if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
3079f4a2713aSLionel Sambuc return ACC_bottom;
3080f4a2713aSLionel Sambuc
3081f4a2713aSLionel Sambuc // Otherwise, don't do anything implicit with an unaudited function.
3082f4a2713aSLionel Sambuc if (!fn->hasAttr<CFAuditedTransferAttr>())
3083f4a2713aSLionel Sambuc return ACC_invalid;
3084f4a2713aSLionel Sambuc
3085f4a2713aSLionel Sambuc // Otherwise, it's +0 unless it follows the create convention.
3086f4a2713aSLionel Sambuc if (ento::coreFoundation::followsCreateRule(fn))
3087f4a2713aSLionel Sambuc return Diagnose ? ACC_plusOne
3088f4a2713aSLionel Sambuc : ACC_invalid; // ACC_plusOne if we start accepting this
3089f4a2713aSLionel Sambuc
3090f4a2713aSLionel Sambuc return ACC_plusZero;
3091f4a2713aSLionel Sambuc }
3092f4a2713aSLionel Sambuc
VisitObjCMessageExpr(ObjCMessageExpr * e)3093f4a2713aSLionel Sambuc ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) {
3094f4a2713aSLionel Sambuc return checkCallToMethod(e->getMethodDecl());
3095f4a2713aSLionel Sambuc }
3096f4a2713aSLionel Sambuc
VisitObjCPropertyRefExpr(ObjCPropertyRefExpr * e)3097f4a2713aSLionel Sambuc ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) {
3098f4a2713aSLionel Sambuc ObjCMethodDecl *method;
3099f4a2713aSLionel Sambuc if (e->isExplicitProperty())
3100f4a2713aSLionel Sambuc method = e->getExplicitProperty()->getGetterMethodDecl();
3101f4a2713aSLionel Sambuc else
3102f4a2713aSLionel Sambuc method = e->getImplicitPropertyGetter();
3103f4a2713aSLionel Sambuc return checkCallToMethod(method);
3104f4a2713aSLionel Sambuc }
3105f4a2713aSLionel Sambuc
checkCallToMethod(ObjCMethodDecl * method)3106f4a2713aSLionel Sambuc ACCResult checkCallToMethod(ObjCMethodDecl *method) {
3107f4a2713aSLionel Sambuc if (!method) return ACC_invalid;
3108f4a2713aSLionel Sambuc
3109f4a2713aSLionel Sambuc // Check for message sends to functions returning CF types. We
3110f4a2713aSLionel Sambuc // just obey the Cocoa conventions with these, even though the
3111f4a2713aSLionel Sambuc // return type is CF.
3112*0a6a1f1dSLionel Sambuc if (!isAnyRetainable(TargetClass) || !isCFType(method->getReturnType()))
3113f4a2713aSLionel Sambuc return ACC_invalid;
3114f4a2713aSLionel Sambuc
3115f4a2713aSLionel Sambuc // If the method is explicitly marked not-retained, it's +0.
3116f4a2713aSLionel Sambuc if (method->hasAttr<CFReturnsNotRetainedAttr>())
3117f4a2713aSLionel Sambuc return ACC_plusZero;
3118f4a2713aSLionel Sambuc
3119f4a2713aSLionel Sambuc // If the method is explicitly marked as returning retained, or its
3120f4a2713aSLionel Sambuc // selector follows a +1 Cocoa convention, treat it as +1.
3121f4a2713aSLionel Sambuc if (method->hasAttr<CFReturnsRetainedAttr>())
3122f4a2713aSLionel Sambuc return ACC_plusOne;
3123f4a2713aSLionel Sambuc
3124f4a2713aSLionel Sambuc switch (method->getSelector().getMethodFamily()) {
3125f4a2713aSLionel Sambuc case OMF_alloc:
3126f4a2713aSLionel Sambuc case OMF_copy:
3127f4a2713aSLionel Sambuc case OMF_mutableCopy:
3128f4a2713aSLionel Sambuc case OMF_new:
3129f4a2713aSLionel Sambuc return ACC_plusOne;
3130f4a2713aSLionel Sambuc
3131f4a2713aSLionel Sambuc default:
3132f4a2713aSLionel Sambuc // Otherwise, treat it as +0.
3133f4a2713aSLionel Sambuc return ACC_plusZero;
3134f4a2713aSLionel Sambuc }
3135f4a2713aSLionel Sambuc }
3136f4a2713aSLionel Sambuc };
3137f4a2713aSLionel Sambuc }
3138f4a2713aSLionel Sambuc
isKnownName(StringRef name)3139f4a2713aSLionel Sambuc bool Sema::isKnownName(StringRef name) {
3140f4a2713aSLionel Sambuc if (name.empty())
3141f4a2713aSLionel Sambuc return false;
3142f4a2713aSLionel Sambuc LookupResult R(*this, &Context.Idents.get(name), SourceLocation(),
3143f4a2713aSLionel Sambuc Sema::LookupOrdinaryName);
3144f4a2713aSLionel Sambuc return LookupName(R, TUScope, false);
3145f4a2713aSLionel Sambuc }
3146f4a2713aSLionel Sambuc
addFixitForObjCARCConversion(Sema & S,DiagnosticBuilder & DiagB,Sema::CheckedConversionKind CCK,SourceLocation afterLParen,QualType castType,Expr * castExpr,Expr * realCast,const char * bridgeKeyword,const char * CFBridgeName)3147f4a2713aSLionel Sambuc static void addFixitForObjCARCConversion(Sema &S,
3148f4a2713aSLionel Sambuc DiagnosticBuilder &DiagB,
3149f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK,
3150f4a2713aSLionel Sambuc SourceLocation afterLParen,
3151f4a2713aSLionel Sambuc QualType castType,
3152f4a2713aSLionel Sambuc Expr *castExpr,
3153f4a2713aSLionel Sambuc Expr *realCast,
3154f4a2713aSLionel Sambuc const char *bridgeKeyword,
3155f4a2713aSLionel Sambuc const char *CFBridgeName) {
3156f4a2713aSLionel Sambuc // We handle C-style and implicit casts here.
3157f4a2713aSLionel Sambuc switch (CCK) {
3158f4a2713aSLionel Sambuc case Sema::CCK_ImplicitConversion:
3159f4a2713aSLionel Sambuc case Sema::CCK_CStyleCast:
3160f4a2713aSLionel Sambuc case Sema::CCK_OtherCast:
3161f4a2713aSLionel Sambuc break;
3162f4a2713aSLionel Sambuc case Sema::CCK_FunctionalCast:
3163f4a2713aSLionel Sambuc return;
3164f4a2713aSLionel Sambuc }
3165f4a2713aSLionel Sambuc
3166f4a2713aSLionel Sambuc if (CFBridgeName) {
3167f4a2713aSLionel Sambuc if (CCK == Sema::CCK_OtherCast) {
3168f4a2713aSLionel Sambuc if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) {
3169f4a2713aSLionel Sambuc SourceRange range(NCE->getOperatorLoc(),
3170f4a2713aSLionel Sambuc NCE->getAngleBrackets().getEnd());
3171f4a2713aSLionel Sambuc SmallString<32> BridgeCall;
3172f4a2713aSLionel Sambuc
3173f4a2713aSLionel Sambuc SourceManager &SM = S.getSourceManager();
3174f4a2713aSLionel Sambuc char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
3175f4a2713aSLionel Sambuc if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts()))
3176f4a2713aSLionel Sambuc BridgeCall += ' ';
3177f4a2713aSLionel Sambuc
3178f4a2713aSLionel Sambuc BridgeCall += CFBridgeName;
3179f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateReplacement(range, BridgeCall));
3180f4a2713aSLionel Sambuc }
3181f4a2713aSLionel Sambuc return;
3182f4a2713aSLionel Sambuc }
3183f4a2713aSLionel Sambuc Expr *castedE = castExpr;
3184f4a2713aSLionel Sambuc if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE))
3185f4a2713aSLionel Sambuc castedE = CCE->getSubExpr();
3186f4a2713aSLionel Sambuc castedE = castedE->IgnoreImpCasts();
3187f4a2713aSLionel Sambuc SourceRange range = castedE->getSourceRange();
3188f4a2713aSLionel Sambuc
3189f4a2713aSLionel Sambuc SmallString<32> BridgeCall;
3190f4a2713aSLionel Sambuc
3191f4a2713aSLionel Sambuc SourceManager &SM = S.getSourceManager();
3192f4a2713aSLionel Sambuc char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
3193f4a2713aSLionel Sambuc if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts()))
3194f4a2713aSLionel Sambuc BridgeCall += ' ';
3195f4a2713aSLionel Sambuc
3196f4a2713aSLionel Sambuc BridgeCall += CFBridgeName;
3197f4a2713aSLionel Sambuc
3198f4a2713aSLionel Sambuc if (isa<ParenExpr>(castedE)) {
3199f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
3200f4a2713aSLionel Sambuc BridgeCall));
3201f4a2713aSLionel Sambuc } else {
3202f4a2713aSLionel Sambuc BridgeCall += '(';
3203f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
3204f4a2713aSLionel Sambuc BridgeCall));
3205f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(
3206f4a2713aSLionel Sambuc S.PP.getLocForEndOfToken(range.getEnd()),
3207f4a2713aSLionel Sambuc ")"));
3208f4a2713aSLionel Sambuc }
3209f4a2713aSLionel Sambuc return;
3210f4a2713aSLionel Sambuc }
3211f4a2713aSLionel Sambuc
3212f4a2713aSLionel Sambuc if (CCK == Sema::CCK_CStyleCast) {
3213f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword));
3214f4a2713aSLionel Sambuc } else if (CCK == Sema::CCK_OtherCast) {
3215f4a2713aSLionel Sambuc if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) {
3216f4a2713aSLionel Sambuc std::string castCode = "(";
3217f4a2713aSLionel Sambuc castCode += bridgeKeyword;
3218f4a2713aSLionel Sambuc castCode += castType.getAsString();
3219f4a2713aSLionel Sambuc castCode += ")";
3220f4a2713aSLionel Sambuc SourceRange Range(NCE->getOperatorLoc(),
3221f4a2713aSLionel Sambuc NCE->getAngleBrackets().getEnd());
3222f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateReplacement(Range, castCode));
3223f4a2713aSLionel Sambuc }
3224f4a2713aSLionel Sambuc } else {
3225f4a2713aSLionel Sambuc std::string castCode = "(";
3226f4a2713aSLionel Sambuc castCode += bridgeKeyword;
3227f4a2713aSLionel Sambuc castCode += castType.getAsString();
3228f4a2713aSLionel Sambuc castCode += ")";
3229f4a2713aSLionel Sambuc Expr *castedE = castExpr->IgnoreImpCasts();
3230f4a2713aSLionel Sambuc SourceRange range = castedE->getSourceRange();
3231f4a2713aSLionel Sambuc if (isa<ParenExpr>(castedE)) {
3232f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
3233f4a2713aSLionel Sambuc castCode));
3234f4a2713aSLionel Sambuc } else {
3235f4a2713aSLionel Sambuc castCode += "(";
3236f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
3237f4a2713aSLionel Sambuc castCode));
3238f4a2713aSLionel Sambuc DiagB.AddFixItHint(FixItHint::CreateInsertion(
3239f4a2713aSLionel Sambuc S.PP.getLocForEndOfToken(range.getEnd()),
3240f4a2713aSLionel Sambuc ")"));
3241f4a2713aSLionel Sambuc }
3242f4a2713aSLionel Sambuc }
3243f4a2713aSLionel Sambuc }
3244f4a2713aSLionel Sambuc
3245*0a6a1f1dSLionel Sambuc template <typename T>
getObjCBridgeAttr(const TypedefType * TD)3246*0a6a1f1dSLionel Sambuc static inline T *getObjCBridgeAttr(const TypedefType *TD) {
3247*0a6a1f1dSLionel Sambuc TypedefNameDecl *TDNDecl = TD->getDecl();
3248*0a6a1f1dSLionel Sambuc QualType QT = TDNDecl->getUnderlyingType();
3249*0a6a1f1dSLionel Sambuc if (QT->isPointerType()) {
3250*0a6a1f1dSLionel Sambuc QT = QT->getPointeeType();
3251*0a6a1f1dSLionel Sambuc if (const RecordType *RT = QT->getAs<RecordType>())
3252*0a6a1f1dSLionel Sambuc if (RecordDecl *RD = RT->getDecl()->getMostRecentDecl())
3253*0a6a1f1dSLionel Sambuc return RD->getAttr<T>();
3254*0a6a1f1dSLionel Sambuc }
3255*0a6a1f1dSLionel Sambuc return nullptr;
3256*0a6a1f1dSLionel Sambuc }
3257*0a6a1f1dSLionel Sambuc
ObjCBridgeRelatedAttrFromType(QualType T,TypedefNameDecl * & TDNDecl)3258*0a6a1f1dSLionel Sambuc static ObjCBridgeRelatedAttr *ObjCBridgeRelatedAttrFromType(QualType T,
3259*0a6a1f1dSLionel Sambuc TypedefNameDecl *&TDNDecl) {
3260*0a6a1f1dSLionel Sambuc while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
3261*0a6a1f1dSLionel Sambuc TDNDecl = TD->getDecl();
3262*0a6a1f1dSLionel Sambuc if (ObjCBridgeRelatedAttr *ObjCBAttr =
3263*0a6a1f1dSLionel Sambuc getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD))
3264*0a6a1f1dSLionel Sambuc return ObjCBAttr;
3265*0a6a1f1dSLionel Sambuc T = TDNDecl->getUnderlyingType();
3266*0a6a1f1dSLionel Sambuc }
3267*0a6a1f1dSLionel Sambuc return nullptr;
3268*0a6a1f1dSLionel Sambuc }
3269*0a6a1f1dSLionel Sambuc
3270f4a2713aSLionel Sambuc static void
diagnoseObjCARCConversion(Sema & S,SourceRange castRange,QualType castType,ARCConversionTypeClass castACTC,Expr * castExpr,Expr * realCast,ARCConversionTypeClass exprACTC,Sema::CheckedConversionKind CCK)3271f4a2713aSLionel Sambuc diagnoseObjCARCConversion(Sema &S, SourceRange castRange,
3272f4a2713aSLionel Sambuc QualType castType, ARCConversionTypeClass castACTC,
3273f4a2713aSLionel Sambuc Expr *castExpr, Expr *realCast,
3274f4a2713aSLionel Sambuc ARCConversionTypeClass exprACTC,
3275f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK) {
3276f4a2713aSLionel Sambuc SourceLocation loc =
3277f4a2713aSLionel Sambuc (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc());
3278f4a2713aSLionel Sambuc
3279f4a2713aSLionel Sambuc if (S.makeUnavailableInSystemHeader(loc,
3280f4a2713aSLionel Sambuc "converts between Objective-C and C pointers in -fobjc-arc"))
3281f4a2713aSLionel Sambuc return;
3282f4a2713aSLionel Sambuc
3283f4a2713aSLionel Sambuc QualType castExprType = castExpr->getType();
3284*0a6a1f1dSLionel Sambuc TypedefNameDecl *TDNDecl = nullptr;
3285*0a6a1f1dSLionel Sambuc if ((castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable &&
3286*0a6a1f1dSLionel Sambuc ObjCBridgeRelatedAttrFromType(castType, TDNDecl)) ||
3287*0a6a1f1dSLionel Sambuc (exprACTC == ACTC_coreFoundation && castACTC == ACTC_retainable &&
3288*0a6a1f1dSLionel Sambuc ObjCBridgeRelatedAttrFromType(castExprType, TDNDecl)))
3289*0a6a1f1dSLionel Sambuc return;
3290f4a2713aSLionel Sambuc
3291f4a2713aSLionel Sambuc unsigned srcKind = 0;
3292f4a2713aSLionel Sambuc switch (exprACTC) {
3293f4a2713aSLionel Sambuc case ACTC_none:
3294f4a2713aSLionel Sambuc case ACTC_coreFoundation:
3295f4a2713aSLionel Sambuc case ACTC_voidPtr:
3296f4a2713aSLionel Sambuc srcKind = (castExprType->isPointerType() ? 1 : 0);
3297f4a2713aSLionel Sambuc break;
3298f4a2713aSLionel Sambuc case ACTC_retainable:
3299f4a2713aSLionel Sambuc srcKind = (castExprType->isBlockPointerType() ? 2 : 3);
3300f4a2713aSLionel Sambuc break;
3301f4a2713aSLionel Sambuc case ACTC_indirectRetainable:
3302f4a2713aSLionel Sambuc srcKind = 4;
3303f4a2713aSLionel Sambuc break;
3304f4a2713aSLionel Sambuc }
3305f4a2713aSLionel Sambuc
3306f4a2713aSLionel Sambuc // Check whether this could be fixed with a bridge cast.
3307f4a2713aSLionel Sambuc SourceLocation afterLParen = S.PP.getLocForEndOfToken(castRange.getBegin());
3308f4a2713aSLionel Sambuc SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc;
3309f4a2713aSLionel Sambuc
3310f4a2713aSLionel Sambuc // Bridge from an ARC type to a CF type.
3311f4a2713aSLionel Sambuc if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) {
3312f4a2713aSLionel Sambuc
3313f4a2713aSLionel Sambuc S.Diag(loc, diag::err_arc_cast_requires_bridge)
3314f4a2713aSLionel Sambuc << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit
3315f4a2713aSLionel Sambuc << 2 // of C pointer type
3316f4a2713aSLionel Sambuc << castExprType
3317f4a2713aSLionel Sambuc << unsigned(castType->isBlockPointerType()) // to ObjC|block type
3318f4a2713aSLionel Sambuc << castType
3319f4a2713aSLionel Sambuc << castRange
3320f4a2713aSLionel Sambuc << castExpr->getSourceRange();
3321f4a2713aSLionel Sambuc bool br = S.isKnownName("CFBridgingRelease");
3322f4a2713aSLionel Sambuc ACCResult CreateRule =
3323f4a2713aSLionel Sambuc ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr);
3324f4a2713aSLionel Sambuc assert(CreateRule != ACC_bottom && "This cast should already be accepted.");
3325f4a2713aSLionel Sambuc if (CreateRule != ACC_plusOne)
3326f4a2713aSLionel Sambuc {
3327f4a2713aSLionel Sambuc DiagnosticBuilder DiagB =
3328f4a2713aSLionel Sambuc (CCK != Sema::CCK_OtherCast) ? S.Diag(noteLoc, diag::note_arc_bridge)
3329f4a2713aSLionel Sambuc : S.Diag(noteLoc, diag::note_arc_cstyle_bridge);
3330f4a2713aSLionel Sambuc
3331f4a2713aSLionel Sambuc addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
3332*0a6a1f1dSLionel Sambuc castType, castExpr, realCast, "__bridge ",
3333*0a6a1f1dSLionel Sambuc nullptr);
3334f4a2713aSLionel Sambuc }
3335f4a2713aSLionel Sambuc if (CreateRule != ACC_plusZero)
3336f4a2713aSLionel Sambuc {
3337f4a2713aSLionel Sambuc DiagnosticBuilder DiagB =
3338f4a2713aSLionel Sambuc (CCK == Sema::CCK_OtherCast && !br) ?
3339f4a2713aSLionel Sambuc S.Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer) << castExprType :
3340f4a2713aSLionel Sambuc S.Diag(br ? castExpr->getExprLoc() : noteLoc,
3341f4a2713aSLionel Sambuc diag::note_arc_bridge_transfer)
3342f4a2713aSLionel Sambuc << castExprType << br;
3343f4a2713aSLionel Sambuc
3344f4a2713aSLionel Sambuc addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
3345f4a2713aSLionel Sambuc castType, castExpr, realCast, "__bridge_transfer ",
3346*0a6a1f1dSLionel Sambuc br ? "CFBridgingRelease" : nullptr);
3347f4a2713aSLionel Sambuc }
3348f4a2713aSLionel Sambuc
3349f4a2713aSLionel Sambuc return;
3350f4a2713aSLionel Sambuc }
3351f4a2713aSLionel Sambuc
3352f4a2713aSLionel Sambuc // Bridge from a CF type to an ARC type.
3353f4a2713aSLionel Sambuc if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) {
3354f4a2713aSLionel Sambuc bool br = S.isKnownName("CFBridgingRetain");
3355f4a2713aSLionel Sambuc S.Diag(loc, diag::err_arc_cast_requires_bridge)
3356f4a2713aSLionel Sambuc << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit
3357f4a2713aSLionel Sambuc << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type
3358f4a2713aSLionel Sambuc << castExprType
3359f4a2713aSLionel Sambuc << 2 // to C pointer type
3360f4a2713aSLionel Sambuc << castType
3361f4a2713aSLionel Sambuc << castRange
3362f4a2713aSLionel Sambuc << castExpr->getSourceRange();
3363f4a2713aSLionel Sambuc ACCResult CreateRule =
3364f4a2713aSLionel Sambuc ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr);
3365f4a2713aSLionel Sambuc assert(CreateRule != ACC_bottom && "This cast should already be accepted.");
3366f4a2713aSLionel Sambuc if (CreateRule != ACC_plusOne)
3367f4a2713aSLionel Sambuc {
3368f4a2713aSLionel Sambuc DiagnosticBuilder DiagB =
3369f4a2713aSLionel Sambuc (CCK != Sema::CCK_OtherCast) ? S.Diag(noteLoc, diag::note_arc_bridge)
3370f4a2713aSLionel Sambuc : S.Diag(noteLoc, diag::note_arc_cstyle_bridge);
3371f4a2713aSLionel Sambuc addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
3372*0a6a1f1dSLionel Sambuc castType, castExpr, realCast, "__bridge ",
3373*0a6a1f1dSLionel Sambuc nullptr);
3374f4a2713aSLionel Sambuc }
3375f4a2713aSLionel Sambuc if (CreateRule != ACC_plusZero)
3376f4a2713aSLionel Sambuc {
3377f4a2713aSLionel Sambuc DiagnosticBuilder DiagB =
3378f4a2713aSLionel Sambuc (CCK == Sema::CCK_OtherCast && !br) ?
3379f4a2713aSLionel Sambuc S.Diag(noteLoc, diag::note_arc_cstyle_bridge_retained) << castType :
3380f4a2713aSLionel Sambuc S.Diag(br ? castExpr->getExprLoc() : noteLoc,
3381f4a2713aSLionel Sambuc diag::note_arc_bridge_retained)
3382f4a2713aSLionel Sambuc << castType << br;
3383f4a2713aSLionel Sambuc
3384f4a2713aSLionel Sambuc addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
3385f4a2713aSLionel Sambuc castType, castExpr, realCast, "__bridge_retained ",
3386*0a6a1f1dSLionel Sambuc br ? "CFBridgingRetain" : nullptr);
3387f4a2713aSLionel Sambuc }
3388f4a2713aSLionel Sambuc
3389f4a2713aSLionel Sambuc return;
3390f4a2713aSLionel Sambuc }
3391f4a2713aSLionel Sambuc
3392f4a2713aSLionel Sambuc S.Diag(loc, diag::err_arc_mismatched_cast)
3393f4a2713aSLionel Sambuc << (CCK != Sema::CCK_ImplicitConversion)
3394f4a2713aSLionel Sambuc << srcKind << castExprType << castType
3395f4a2713aSLionel Sambuc << castRange << castExpr->getSourceRange();
3396f4a2713aSLionel Sambuc }
3397f4a2713aSLionel Sambuc
3398*0a6a1f1dSLionel Sambuc template <typename TB>
CheckObjCBridgeNSCast(Sema & S,QualType castType,Expr * castExpr,bool & HadTheAttribute,bool warn)3399*0a6a1f1dSLionel Sambuc static bool CheckObjCBridgeNSCast(Sema &S, QualType castType, Expr *castExpr,
3400*0a6a1f1dSLionel Sambuc bool &HadTheAttribute, bool warn) {
3401f4a2713aSLionel Sambuc QualType T = castExpr->getType();
3402*0a6a1f1dSLionel Sambuc HadTheAttribute = false;
3403f4a2713aSLionel Sambuc while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
3404f4a2713aSLionel Sambuc TypedefNameDecl *TDNDecl = TD->getDecl();
3405*0a6a1f1dSLionel Sambuc if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
3406f4a2713aSLionel Sambuc if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) {
3407*0a6a1f1dSLionel Sambuc HadTheAttribute = true;
3408*0a6a1f1dSLionel Sambuc if (Parm->isStr("id"))
3409*0a6a1f1dSLionel Sambuc return true;
3410*0a6a1f1dSLionel Sambuc
3411*0a6a1f1dSLionel Sambuc NamedDecl *Target = nullptr;
3412f4a2713aSLionel Sambuc // Check for an existing type with this name.
3413f4a2713aSLionel Sambuc LookupResult R(S, DeclarationName(Parm), SourceLocation(),
3414f4a2713aSLionel Sambuc Sema::LookupOrdinaryName);
3415f4a2713aSLionel Sambuc if (S.LookupName(R, S.TUScope)) {
3416f4a2713aSLionel Sambuc Target = R.getFoundDecl();
3417f4a2713aSLionel Sambuc if (Target && isa<ObjCInterfaceDecl>(Target)) {
3418f4a2713aSLionel Sambuc ObjCInterfaceDecl *ExprClass = cast<ObjCInterfaceDecl>(Target);
3419f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *InterfacePointerType =
3420f4a2713aSLionel Sambuc castType->getAsObjCInterfacePointerType()) {
3421f4a2713aSLionel Sambuc ObjCInterfaceDecl *CastClass
3422f4a2713aSLionel Sambuc = InterfacePointerType->getObjectType()->getInterface();
3423*0a6a1f1dSLionel Sambuc if ((CastClass == ExprClass) ||
3424*0a6a1f1dSLionel Sambuc (CastClass && ExprClass->isSuperClassOf(CastClass)))
3425f4a2713aSLionel Sambuc return true;
3426*0a6a1f1dSLionel Sambuc if (warn)
3427f4a2713aSLionel Sambuc S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge)
3428f4a2713aSLionel Sambuc << T << Target->getName() << castType->getPointeeType();
3429*0a6a1f1dSLionel Sambuc return false;
3430*0a6a1f1dSLionel Sambuc } else if (castType->isObjCIdType() ||
3431*0a6a1f1dSLionel Sambuc (S.Context.ObjCObjectAdoptsQTypeProtocols(
3432*0a6a1f1dSLionel Sambuc castType, ExprClass)))
3433*0a6a1f1dSLionel Sambuc // ok to cast to 'id'.
3434*0a6a1f1dSLionel Sambuc // casting to id<p-list> is ok if bridge type adopts all of
3435*0a6a1f1dSLionel Sambuc // p-list protocols.
3436f4a2713aSLionel Sambuc return true;
3437*0a6a1f1dSLionel Sambuc else {
3438*0a6a1f1dSLionel Sambuc if (warn) {
3439f4a2713aSLionel Sambuc S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge)
3440f4a2713aSLionel Sambuc << T << Target->getName() << castType;
3441*0a6a1f1dSLionel Sambuc S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3442*0a6a1f1dSLionel Sambuc S.Diag(Target->getLocStart(), diag::note_declared_at);
3443*0a6a1f1dSLionel Sambuc }
3444*0a6a1f1dSLionel Sambuc return false;
3445f4a2713aSLionel Sambuc }
3446f4a2713aSLionel Sambuc }
3447f4a2713aSLionel Sambuc }
3448f4a2713aSLionel Sambuc S.Diag(castExpr->getLocStart(), diag::err_objc_cf_bridged_not_interface)
3449*0a6a1f1dSLionel Sambuc << castExpr->getType() << Parm;
3450f4a2713aSLionel Sambuc S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3451f4a2713aSLionel Sambuc if (Target)
3452f4a2713aSLionel Sambuc S.Diag(Target->getLocStart(), diag::note_declared_at);
3453f4a2713aSLionel Sambuc return true;
3454f4a2713aSLionel Sambuc }
3455f4a2713aSLionel Sambuc return false;
3456f4a2713aSLionel Sambuc }
3457*0a6a1f1dSLionel Sambuc T = TDNDecl->getUnderlyingType();
3458*0a6a1f1dSLionel Sambuc }
3459*0a6a1f1dSLionel Sambuc return true;
3460*0a6a1f1dSLionel Sambuc }
3461f4a2713aSLionel Sambuc
3462*0a6a1f1dSLionel Sambuc template <typename TB>
CheckObjCBridgeCFCast(Sema & S,QualType castType,Expr * castExpr,bool & HadTheAttribute,bool warn)3463*0a6a1f1dSLionel Sambuc static bool CheckObjCBridgeCFCast(Sema &S, QualType castType, Expr *castExpr,
3464*0a6a1f1dSLionel Sambuc bool &HadTheAttribute, bool warn) {
3465f4a2713aSLionel Sambuc QualType T = castType;
3466*0a6a1f1dSLionel Sambuc HadTheAttribute = false;
3467f4a2713aSLionel Sambuc while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
3468f4a2713aSLionel Sambuc TypedefNameDecl *TDNDecl = TD->getDecl();
3469*0a6a1f1dSLionel Sambuc if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
3470f4a2713aSLionel Sambuc if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) {
3471*0a6a1f1dSLionel Sambuc HadTheAttribute = true;
3472*0a6a1f1dSLionel Sambuc NamedDecl *Target = nullptr;
3473f4a2713aSLionel Sambuc // Check for an existing type with this name.
3474f4a2713aSLionel Sambuc LookupResult R(S, DeclarationName(Parm), SourceLocation(),
3475f4a2713aSLionel Sambuc Sema::LookupOrdinaryName);
3476f4a2713aSLionel Sambuc if (S.LookupName(R, S.TUScope)) {
3477f4a2713aSLionel Sambuc Target = R.getFoundDecl();
3478f4a2713aSLionel Sambuc if (Target && isa<ObjCInterfaceDecl>(Target)) {
3479f4a2713aSLionel Sambuc ObjCInterfaceDecl *CastClass = cast<ObjCInterfaceDecl>(Target);
3480f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *InterfacePointerType =
3481f4a2713aSLionel Sambuc castExpr->getType()->getAsObjCInterfacePointerType()) {
3482f4a2713aSLionel Sambuc ObjCInterfaceDecl *ExprClass
3483f4a2713aSLionel Sambuc = InterfacePointerType->getObjectType()->getInterface();
3484*0a6a1f1dSLionel Sambuc if ((CastClass == ExprClass) ||
3485*0a6a1f1dSLionel Sambuc (ExprClass && CastClass->isSuperClassOf(ExprClass)))
3486f4a2713aSLionel Sambuc return true;
3487*0a6a1f1dSLionel Sambuc if (warn) {
3488f4a2713aSLionel Sambuc S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge_to_cf)
3489f4a2713aSLionel Sambuc << castExpr->getType()->getPointeeType() << T;
3490f4a2713aSLionel Sambuc S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3491*0a6a1f1dSLionel Sambuc }
3492*0a6a1f1dSLionel Sambuc return false;
3493*0a6a1f1dSLionel Sambuc } else if (castExpr->getType()->isObjCIdType() ||
3494*0a6a1f1dSLionel Sambuc (S.Context.QIdProtocolsAdoptObjCObjectProtocols(
3495*0a6a1f1dSLionel Sambuc castExpr->getType(), CastClass)))
3496*0a6a1f1dSLionel Sambuc // ok to cast an 'id' expression to a CFtype.
3497*0a6a1f1dSLionel Sambuc // ok to cast an 'id<plist>' expression to CFtype provided plist
3498*0a6a1f1dSLionel Sambuc // adopts all of CFtype's ObjetiveC's class plist.
3499f4a2713aSLionel Sambuc return true;
3500*0a6a1f1dSLionel Sambuc else {
3501*0a6a1f1dSLionel Sambuc if (warn) {
3502f4a2713aSLionel Sambuc S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge_to_cf)
3503f4a2713aSLionel Sambuc << castExpr->getType() << castType;
3504f4a2713aSLionel Sambuc S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3505*0a6a1f1dSLionel Sambuc S.Diag(Target->getLocStart(), diag::note_declared_at);
3506*0a6a1f1dSLionel Sambuc }
3507*0a6a1f1dSLionel Sambuc return false;
3508f4a2713aSLionel Sambuc }
3509f4a2713aSLionel Sambuc }
3510f4a2713aSLionel Sambuc }
3511f4a2713aSLionel Sambuc S.Diag(castExpr->getLocStart(), diag::err_objc_ns_bridged_invalid_cfobject)
3512f4a2713aSLionel Sambuc << castExpr->getType() << castType;
3513f4a2713aSLionel Sambuc S.Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3514f4a2713aSLionel Sambuc if (Target)
3515f4a2713aSLionel Sambuc S.Diag(Target->getLocStart(), diag::note_declared_at);
3516*0a6a1f1dSLionel Sambuc return true;
3517*0a6a1f1dSLionel Sambuc }
3518*0a6a1f1dSLionel Sambuc return false;
3519*0a6a1f1dSLionel Sambuc }
3520*0a6a1f1dSLionel Sambuc T = TDNDecl->getUnderlyingType();
3521f4a2713aSLionel Sambuc }
3522f4a2713aSLionel Sambuc return true;
3523f4a2713aSLionel Sambuc }
3524*0a6a1f1dSLionel Sambuc
CheckTollFreeBridgeCast(QualType castType,Expr * castExpr)3525*0a6a1f1dSLionel Sambuc void Sema::CheckTollFreeBridgeCast(QualType castType, Expr *castExpr) {
3526*0a6a1f1dSLionel Sambuc if (!getLangOpts().ObjC1)
3527*0a6a1f1dSLionel Sambuc return;
3528*0a6a1f1dSLionel Sambuc // warn in presence of __bridge casting to or from a toll free bridge cast.
3529*0a6a1f1dSLionel Sambuc ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExpr->getType());
3530*0a6a1f1dSLionel Sambuc ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType);
3531*0a6a1f1dSLionel Sambuc if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) {
3532*0a6a1f1dSLionel Sambuc bool HasObjCBridgeAttr;
3533*0a6a1f1dSLionel Sambuc bool ObjCBridgeAttrWillNotWarn =
3534*0a6a1f1dSLionel Sambuc CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
3535*0a6a1f1dSLionel Sambuc false);
3536*0a6a1f1dSLionel Sambuc if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
3537*0a6a1f1dSLionel Sambuc return;
3538*0a6a1f1dSLionel Sambuc bool HasObjCBridgeMutableAttr;
3539*0a6a1f1dSLionel Sambuc bool ObjCBridgeMutableAttrWillNotWarn =
3540*0a6a1f1dSLionel Sambuc CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
3541*0a6a1f1dSLionel Sambuc HasObjCBridgeMutableAttr, false);
3542*0a6a1f1dSLionel Sambuc if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
3543*0a6a1f1dSLionel Sambuc return;
3544*0a6a1f1dSLionel Sambuc
3545*0a6a1f1dSLionel Sambuc if (HasObjCBridgeAttr)
3546*0a6a1f1dSLionel Sambuc CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
3547*0a6a1f1dSLionel Sambuc true);
3548*0a6a1f1dSLionel Sambuc else if (HasObjCBridgeMutableAttr)
3549*0a6a1f1dSLionel Sambuc CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
3550*0a6a1f1dSLionel Sambuc HasObjCBridgeMutableAttr, true);
3551*0a6a1f1dSLionel Sambuc }
3552*0a6a1f1dSLionel Sambuc else if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable) {
3553*0a6a1f1dSLionel Sambuc bool HasObjCBridgeAttr;
3554*0a6a1f1dSLionel Sambuc bool ObjCBridgeAttrWillNotWarn =
3555*0a6a1f1dSLionel Sambuc CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
3556*0a6a1f1dSLionel Sambuc false);
3557*0a6a1f1dSLionel Sambuc if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
3558*0a6a1f1dSLionel Sambuc return;
3559*0a6a1f1dSLionel Sambuc bool HasObjCBridgeMutableAttr;
3560*0a6a1f1dSLionel Sambuc bool ObjCBridgeMutableAttrWillNotWarn =
3561*0a6a1f1dSLionel Sambuc CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
3562*0a6a1f1dSLionel Sambuc HasObjCBridgeMutableAttr, false);
3563*0a6a1f1dSLionel Sambuc if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
3564*0a6a1f1dSLionel Sambuc return;
3565*0a6a1f1dSLionel Sambuc
3566*0a6a1f1dSLionel Sambuc if (HasObjCBridgeAttr)
3567*0a6a1f1dSLionel Sambuc CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
3568*0a6a1f1dSLionel Sambuc true);
3569*0a6a1f1dSLionel Sambuc else if (HasObjCBridgeMutableAttr)
3570*0a6a1f1dSLionel Sambuc CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
3571*0a6a1f1dSLionel Sambuc HasObjCBridgeMutableAttr, true);
3572*0a6a1f1dSLionel Sambuc }
3573*0a6a1f1dSLionel Sambuc }
3574*0a6a1f1dSLionel Sambuc
CheckObjCBridgeRelatedCast(QualType castType,Expr * castExpr)3575*0a6a1f1dSLionel Sambuc void Sema::CheckObjCBridgeRelatedCast(QualType castType, Expr *castExpr) {
3576*0a6a1f1dSLionel Sambuc QualType SrcType = castExpr->getType();
3577*0a6a1f1dSLionel Sambuc if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(castExpr)) {
3578*0a6a1f1dSLionel Sambuc if (PRE->isExplicitProperty()) {
3579*0a6a1f1dSLionel Sambuc if (ObjCPropertyDecl *PDecl = PRE->getExplicitProperty())
3580*0a6a1f1dSLionel Sambuc SrcType = PDecl->getType();
3581*0a6a1f1dSLionel Sambuc }
3582*0a6a1f1dSLionel Sambuc else if (PRE->isImplicitProperty()) {
3583*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *Getter = PRE->getImplicitPropertyGetter())
3584*0a6a1f1dSLionel Sambuc SrcType = Getter->getReturnType();
3585*0a6a1f1dSLionel Sambuc
3586*0a6a1f1dSLionel Sambuc }
3587*0a6a1f1dSLionel Sambuc }
3588*0a6a1f1dSLionel Sambuc
3589*0a6a1f1dSLionel Sambuc ARCConversionTypeClass srcExprACTC = classifyTypeForARCConversion(SrcType);
3590*0a6a1f1dSLionel Sambuc ARCConversionTypeClass castExprACTC = classifyTypeForARCConversion(castType);
3591*0a6a1f1dSLionel Sambuc if (srcExprACTC != ACTC_retainable || castExprACTC != ACTC_coreFoundation)
3592*0a6a1f1dSLionel Sambuc return;
3593*0a6a1f1dSLionel Sambuc CheckObjCBridgeRelatedConversions(castExpr->getLocStart(),
3594*0a6a1f1dSLionel Sambuc castType, SrcType, castExpr);
3595*0a6a1f1dSLionel Sambuc return;
3596*0a6a1f1dSLionel Sambuc }
3597*0a6a1f1dSLionel Sambuc
CheckTollFreeBridgeStaticCast(QualType castType,Expr * castExpr,CastKind & Kind)3598*0a6a1f1dSLionel Sambuc bool Sema::CheckTollFreeBridgeStaticCast(QualType castType, Expr *castExpr,
3599*0a6a1f1dSLionel Sambuc CastKind &Kind) {
3600*0a6a1f1dSLionel Sambuc if (!getLangOpts().ObjC1)
3601*0a6a1f1dSLionel Sambuc return false;
3602*0a6a1f1dSLionel Sambuc ARCConversionTypeClass exprACTC =
3603*0a6a1f1dSLionel Sambuc classifyTypeForARCConversion(castExpr->getType());
3604*0a6a1f1dSLionel Sambuc ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType);
3605*0a6a1f1dSLionel Sambuc if ((castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) ||
3606*0a6a1f1dSLionel Sambuc (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable)) {
3607*0a6a1f1dSLionel Sambuc CheckTollFreeBridgeCast(castType, castExpr);
3608*0a6a1f1dSLionel Sambuc Kind = (castACTC == ACTC_coreFoundation) ? CK_BitCast
3609*0a6a1f1dSLionel Sambuc : CK_CPointerToObjCPointerCast;
3610*0a6a1f1dSLionel Sambuc return true;
3611*0a6a1f1dSLionel Sambuc }
3612*0a6a1f1dSLionel Sambuc return false;
3613*0a6a1f1dSLionel Sambuc }
3614*0a6a1f1dSLionel Sambuc
checkObjCBridgeRelatedComponents(SourceLocation Loc,QualType DestType,QualType SrcType,ObjCInterfaceDecl * & RelatedClass,ObjCMethodDecl * & ClassMethod,ObjCMethodDecl * & InstanceMethod,TypedefNameDecl * & TDNDecl,bool CfToNs)3615*0a6a1f1dSLionel Sambuc bool Sema::checkObjCBridgeRelatedComponents(SourceLocation Loc,
3616*0a6a1f1dSLionel Sambuc QualType DestType, QualType SrcType,
3617*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *&RelatedClass,
3618*0a6a1f1dSLionel Sambuc ObjCMethodDecl *&ClassMethod,
3619*0a6a1f1dSLionel Sambuc ObjCMethodDecl *&InstanceMethod,
3620*0a6a1f1dSLionel Sambuc TypedefNameDecl *&TDNDecl,
3621*0a6a1f1dSLionel Sambuc bool CfToNs) {
3622*0a6a1f1dSLionel Sambuc QualType T = CfToNs ? SrcType : DestType;
3623*0a6a1f1dSLionel Sambuc ObjCBridgeRelatedAttr *ObjCBAttr = ObjCBridgeRelatedAttrFromType(T, TDNDecl);
3624*0a6a1f1dSLionel Sambuc if (!ObjCBAttr)
3625*0a6a1f1dSLionel Sambuc return false;
3626*0a6a1f1dSLionel Sambuc
3627*0a6a1f1dSLionel Sambuc IdentifierInfo *RCId = ObjCBAttr->getRelatedClass();
3628*0a6a1f1dSLionel Sambuc IdentifierInfo *CMId = ObjCBAttr->getClassMethod();
3629*0a6a1f1dSLionel Sambuc IdentifierInfo *IMId = ObjCBAttr->getInstanceMethod();
3630*0a6a1f1dSLionel Sambuc if (!RCId)
3631*0a6a1f1dSLionel Sambuc return false;
3632*0a6a1f1dSLionel Sambuc NamedDecl *Target = nullptr;
3633*0a6a1f1dSLionel Sambuc // Check for an existing type with this name.
3634*0a6a1f1dSLionel Sambuc LookupResult R(*this, DeclarationName(RCId), SourceLocation(),
3635*0a6a1f1dSLionel Sambuc Sema::LookupOrdinaryName);
3636*0a6a1f1dSLionel Sambuc if (!LookupName(R, TUScope)) {
3637*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_invalid_class) << RCId
3638*0a6a1f1dSLionel Sambuc << SrcType << DestType;
3639*0a6a1f1dSLionel Sambuc Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3640*0a6a1f1dSLionel Sambuc return false;
3641*0a6a1f1dSLionel Sambuc }
3642*0a6a1f1dSLionel Sambuc Target = R.getFoundDecl();
3643*0a6a1f1dSLionel Sambuc if (Target && isa<ObjCInterfaceDecl>(Target))
3644*0a6a1f1dSLionel Sambuc RelatedClass = cast<ObjCInterfaceDecl>(Target);
3645*0a6a1f1dSLionel Sambuc else {
3646*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId
3647*0a6a1f1dSLionel Sambuc << SrcType << DestType;
3648*0a6a1f1dSLionel Sambuc Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3649*0a6a1f1dSLionel Sambuc if (Target)
3650*0a6a1f1dSLionel Sambuc Diag(Target->getLocStart(), diag::note_declared_at);
3651*0a6a1f1dSLionel Sambuc return false;
3652*0a6a1f1dSLionel Sambuc }
3653*0a6a1f1dSLionel Sambuc
3654*0a6a1f1dSLionel Sambuc // Check for an existing class method with the given selector name.
3655*0a6a1f1dSLionel Sambuc if (CfToNs && CMId) {
3656*0a6a1f1dSLionel Sambuc Selector Sel = Context.Selectors.getUnarySelector(CMId);
3657*0a6a1f1dSLionel Sambuc ClassMethod = RelatedClass->lookupMethod(Sel, false);
3658*0a6a1f1dSLionel Sambuc if (!ClassMethod) {
3659*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_known_method)
3660*0a6a1f1dSLionel Sambuc << SrcType << DestType << Sel << false;
3661*0a6a1f1dSLionel Sambuc Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3662*0a6a1f1dSLionel Sambuc return false;
3663*0a6a1f1dSLionel Sambuc }
3664*0a6a1f1dSLionel Sambuc }
3665*0a6a1f1dSLionel Sambuc
3666*0a6a1f1dSLionel Sambuc // Check for an existing instance method with the given selector name.
3667*0a6a1f1dSLionel Sambuc if (!CfToNs && IMId) {
3668*0a6a1f1dSLionel Sambuc Selector Sel = Context.Selectors.getNullarySelector(IMId);
3669*0a6a1f1dSLionel Sambuc InstanceMethod = RelatedClass->lookupMethod(Sel, true);
3670*0a6a1f1dSLionel Sambuc if (!InstanceMethod) {
3671*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_known_method)
3672*0a6a1f1dSLionel Sambuc << SrcType << DestType << Sel << true;
3673*0a6a1f1dSLionel Sambuc Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3674*0a6a1f1dSLionel Sambuc return false;
3675*0a6a1f1dSLionel Sambuc }
3676*0a6a1f1dSLionel Sambuc }
3677*0a6a1f1dSLionel Sambuc return true;
3678*0a6a1f1dSLionel Sambuc }
3679*0a6a1f1dSLionel Sambuc
3680*0a6a1f1dSLionel Sambuc bool
CheckObjCBridgeRelatedConversions(SourceLocation Loc,QualType DestType,QualType SrcType,Expr * & SrcExpr)3681*0a6a1f1dSLionel Sambuc Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc,
3682*0a6a1f1dSLionel Sambuc QualType DestType, QualType SrcType,
3683*0a6a1f1dSLionel Sambuc Expr *&SrcExpr) {
3684*0a6a1f1dSLionel Sambuc ARCConversionTypeClass rhsExprACTC = classifyTypeForARCConversion(SrcType);
3685*0a6a1f1dSLionel Sambuc ARCConversionTypeClass lhsExprACTC = classifyTypeForARCConversion(DestType);
3686*0a6a1f1dSLionel Sambuc bool CfToNs = (rhsExprACTC == ACTC_coreFoundation && lhsExprACTC == ACTC_retainable);
3687*0a6a1f1dSLionel Sambuc bool NsToCf = (rhsExprACTC == ACTC_retainable && lhsExprACTC == ACTC_coreFoundation);
3688*0a6a1f1dSLionel Sambuc if (!CfToNs && !NsToCf)
3689*0a6a1f1dSLionel Sambuc return false;
3690*0a6a1f1dSLionel Sambuc
3691*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *RelatedClass;
3692*0a6a1f1dSLionel Sambuc ObjCMethodDecl *ClassMethod = nullptr;
3693*0a6a1f1dSLionel Sambuc ObjCMethodDecl *InstanceMethod = nullptr;
3694*0a6a1f1dSLionel Sambuc TypedefNameDecl *TDNDecl = nullptr;
3695*0a6a1f1dSLionel Sambuc if (!checkObjCBridgeRelatedComponents(Loc, DestType, SrcType, RelatedClass,
3696*0a6a1f1dSLionel Sambuc ClassMethod, InstanceMethod, TDNDecl, CfToNs))
3697*0a6a1f1dSLionel Sambuc return false;
3698*0a6a1f1dSLionel Sambuc
3699*0a6a1f1dSLionel Sambuc if (CfToNs) {
3700*0a6a1f1dSLionel Sambuc // Implicit conversion from CF to ObjC object is needed.
3701*0a6a1f1dSLionel Sambuc if (ClassMethod) {
3702*0a6a1f1dSLionel Sambuc std::string ExpressionString = "[";
3703*0a6a1f1dSLionel Sambuc ExpressionString += RelatedClass->getNameAsString();
3704*0a6a1f1dSLionel Sambuc ExpressionString += " ";
3705*0a6a1f1dSLionel Sambuc ExpressionString += ClassMethod->getSelector().getAsString();
3706*0a6a1f1dSLionel Sambuc SourceLocation SrcExprEndLoc = PP.getLocForEndOfToken(SrcExpr->getLocEnd());
3707*0a6a1f1dSLionel Sambuc // Provide a fixit: [RelatedClass ClassMethod SrcExpr]
3708*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_known_method)
3709*0a6a1f1dSLionel Sambuc << SrcType << DestType << ClassMethod->getSelector() << false
3710*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(SrcExpr->getLocStart(), ExpressionString)
3711*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(SrcExprEndLoc, "]");
3712*0a6a1f1dSLionel Sambuc Diag(RelatedClass->getLocStart(), diag::note_declared_at);
3713*0a6a1f1dSLionel Sambuc Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3714*0a6a1f1dSLionel Sambuc
3715*0a6a1f1dSLionel Sambuc QualType receiverType =
3716*0a6a1f1dSLionel Sambuc Context.getObjCInterfaceType(RelatedClass);
3717*0a6a1f1dSLionel Sambuc // Argument.
3718*0a6a1f1dSLionel Sambuc Expr *args[] = { SrcExpr };
3719*0a6a1f1dSLionel Sambuc ExprResult msg = BuildClassMessageImplicit(receiverType, false,
3720*0a6a1f1dSLionel Sambuc ClassMethod->getLocation(),
3721*0a6a1f1dSLionel Sambuc ClassMethod->getSelector(), ClassMethod,
3722*0a6a1f1dSLionel Sambuc MultiExprArg(args, 1));
3723*0a6a1f1dSLionel Sambuc SrcExpr = msg.get();
3724*0a6a1f1dSLionel Sambuc return true;
3725*0a6a1f1dSLionel Sambuc }
3726*0a6a1f1dSLionel Sambuc }
3727*0a6a1f1dSLionel Sambuc else {
3728*0a6a1f1dSLionel Sambuc // Implicit conversion from ObjC type to CF object is needed.
3729*0a6a1f1dSLionel Sambuc if (InstanceMethod) {
3730*0a6a1f1dSLionel Sambuc std::string ExpressionString;
3731*0a6a1f1dSLionel Sambuc SourceLocation SrcExprEndLoc = PP.getLocForEndOfToken(SrcExpr->getLocEnd());
3732*0a6a1f1dSLionel Sambuc if (InstanceMethod->isPropertyAccessor())
3733*0a6a1f1dSLionel Sambuc if (const ObjCPropertyDecl *PDecl = InstanceMethod->findPropertyDecl()) {
3734*0a6a1f1dSLionel Sambuc // fixit: ObjectExpr.propertyname when it is aproperty accessor.
3735*0a6a1f1dSLionel Sambuc ExpressionString = ".";
3736*0a6a1f1dSLionel Sambuc ExpressionString += PDecl->getNameAsString();
3737*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_known_method)
3738*0a6a1f1dSLionel Sambuc << SrcType << DestType << InstanceMethod->getSelector() << true
3739*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString);
3740*0a6a1f1dSLionel Sambuc }
3741*0a6a1f1dSLionel Sambuc if (ExpressionString.empty()) {
3742*0a6a1f1dSLionel Sambuc // Provide a fixit: [ObjectExpr InstanceMethod]
3743*0a6a1f1dSLionel Sambuc ExpressionString = " ";
3744*0a6a1f1dSLionel Sambuc ExpressionString += InstanceMethod->getSelector().getAsString();
3745*0a6a1f1dSLionel Sambuc ExpressionString += "]";
3746*0a6a1f1dSLionel Sambuc
3747*0a6a1f1dSLionel Sambuc Diag(Loc, diag::err_objc_bridged_related_known_method)
3748*0a6a1f1dSLionel Sambuc << SrcType << DestType << InstanceMethod->getSelector() << true
3749*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(SrcExpr->getLocStart(), "[")
3750*0a6a1f1dSLionel Sambuc << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString);
3751*0a6a1f1dSLionel Sambuc }
3752*0a6a1f1dSLionel Sambuc Diag(RelatedClass->getLocStart(), diag::note_declared_at);
3753*0a6a1f1dSLionel Sambuc Diag(TDNDecl->getLocStart(), diag::note_declared_at);
3754*0a6a1f1dSLionel Sambuc
3755*0a6a1f1dSLionel Sambuc ExprResult msg =
3756*0a6a1f1dSLionel Sambuc BuildInstanceMessageImplicit(SrcExpr, SrcType,
3757*0a6a1f1dSLionel Sambuc InstanceMethod->getLocation(),
3758*0a6a1f1dSLionel Sambuc InstanceMethod->getSelector(),
3759*0a6a1f1dSLionel Sambuc InstanceMethod, None);
3760*0a6a1f1dSLionel Sambuc SrcExpr = msg.get();
3761*0a6a1f1dSLionel Sambuc return true;
3762*0a6a1f1dSLionel Sambuc }
3763f4a2713aSLionel Sambuc }
3764f4a2713aSLionel Sambuc return false;
3765f4a2713aSLionel Sambuc }
3766f4a2713aSLionel Sambuc
3767f4a2713aSLionel Sambuc Sema::ARCConversionResult
CheckObjCARCConversion(SourceRange castRange,QualType castType,Expr * & castExpr,CheckedConversionKind CCK,bool DiagnoseCFAudited,BinaryOperatorKind Opc)3768f4a2713aSLionel Sambuc Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
3769f4a2713aSLionel Sambuc Expr *&castExpr, CheckedConversionKind CCK,
3770*0a6a1f1dSLionel Sambuc bool DiagnoseCFAudited,
3771*0a6a1f1dSLionel Sambuc BinaryOperatorKind Opc) {
3772f4a2713aSLionel Sambuc QualType castExprType = castExpr->getType();
3773f4a2713aSLionel Sambuc
3774f4a2713aSLionel Sambuc // For the purposes of the classification, we assume reference types
3775f4a2713aSLionel Sambuc // will bind to temporaries.
3776f4a2713aSLionel Sambuc QualType effCastType = castType;
3777f4a2713aSLionel Sambuc if (const ReferenceType *ref = castType->getAs<ReferenceType>())
3778f4a2713aSLionel Sambuc effCastType = ref->getPointeeType();
3779f4a2713aSLionel Sambuc
3780f4a2713aSLionel Sambuc ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType);
3781f4a2713aSLionel Sambuc ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType);
3782f4a2713aSLionel Sambuc if (exprACTC == castACTC) {
3783f4a2713aSLionel Sambuc // check for viablity and report error if casting an rvalue to a
3784f4a2713aSLionel Sambuc // life-time qualifier.
3785f4a2713aSLionel Sambuc if ((castACTC == ACTC_retainable) &&
3786f4a2713aSLionel Sambuc (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) &&
3787f4a2713aSLionel Sambuc (castType != castExprType)) {
3788f4a2713aSLionel Sambuc const Type *DT = castType.getTypePtr();
3789f4a2713aSLionel Sambuc QualType QDT = castType;
3790f4a2713aSLionel Sambuc // We desugar some types but not others. We ignore those
3791f4a2713aSLionel Sambuc // that cannot happen in a cast; i.e. auto, and those which
3792f4a2713aSLionel Sambuc // should not be de-sugared; i.e typedef.
3793f4a2713aSLionel Sambuc if (const ParenType *PT = dyn_cast<ParenType>(DT))
3794f4a2713aSLionel Sambuc QDT = PT->desugar();
3795f4a2713aSLionel Sambuc else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
3796f4a2713aSLionel Sambuc QDT = TP->desugar();
3797f4a2713aSLionel Sambuc else if (const AttributedType *AT = dyn_cast<AttributedType>(DT))
3798f4a2713aSLionel Sambuc QDT = AT->desugar();
3799f4a2713aSLionel Sambuc if (QDT != castType &&
3800f4a2713aSLionel Sambuc QDT.getObjCLifetime() != Qualifiers::OCL_None) {
3801f4a2713aSLionel Sambuc SourceLocation loc =
3802f4a2713aSLionel Sambuc (castRange.isValid() ? castRange.getBegin()
3803f4a2713aSLionel Sambuc : castExpr->getExprLoc());
3804f4a2713aSLionel Sambuc Diag(loc, diag::err_arc_nolifetime_behavior);
3805f4a2713aSLionel Sambuc }
3806f4a2713aSLionel Sambuc }
3807f4a2713aSLionel Sambuc return ACR_okay;
3808f4a2713aSLionel Sambuc }
3809f4a2713aSLionel Sambuc
3810f4a2713aSLionel Sambuc if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay;
3811f4a2713aSLionel Sambuc
3812f4a2713aSLionel Sambuc // Allow all of these types to be cast to integer types (but not
3813f4a2713aSLionel Sambuc // vice-versa).
3814f4a2713aSLionel Sambuc if (castACTC == ACTC_none && castType->isIntegralType(Context))
3815f4a2713aSLionel Sambuc return ACR_okay;
3816f4a2713aSLionel Sambuc
3817f4a2713aSLionel Sambuc // Allow casts between pointers to lifetime types (e.g., __strong id*)
3818f4a2713aSLionel Sambuc // and pointers to void (e.g., cv void *). Casting from void* to lifetime*
3819f4a2713aSLionel Sambuc // must be explicit.
3820f4a2713aSLionel Sambuc if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr)
3821f4a2713aSLionel Sambuc return ACR_okay;
3822f4a2713aSLionel Sambuc if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr &&
3823f4a2713aSLionel Sambuc CCK != CCK_ImplicitConversion)
3824f4a2713aSLionel Sambuc return ACR_okay;
3825f4a2713aSLionel Sambuc
3826f4a2713aSLionel Sambuc switch (ARCCastChecker(Context, exprACTC, castACTC, false).Visit(castExpr)) {
3827f4a2713aSLionel Sambuc // For invalid casts, fall through.
3828f4a2713aSLionel Sambuc case ACC_invalid:
3829f4a2713aSLionel Sambuc break;
3830f4a2713aSLionel Sambuc
3831f4a2713aSLionel Sambuc // Do nothing for both bottom and +0.
3832f4a2713aSLionel Sambuc case ACC_bottom:
3833f4a2713aSLionel Sambuc case ACC_plusZero:
3834f4a2713aSLionel Sambuc return ACR_okay;
3835f4a2713aSLionel Sambuc
3836f4a2713aSLionel Sambuc // If the result is +1, consume it here.
3837f4a2713aSLionel Sambuc case ACC_plusOne:
3838f4a2713aSLionel Sambuc castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(),
3839f4a2713aSLionel Sambuc CK_ARCConsumeObject, castExpr,
3840*0a6a1f1dSLionel Sambuc nullptr, VK_RValue);
3841f4a2713aSLionel Sambuc ExprNeedsCleanups = true;
3842f4a2713aSLionel Sambuc return ACR_okay;
3843f4a2713aSLionel Sambuc }
3844f4a2713aSLionel Sambuc
3845f4a2713aSLionel Sambuc // If this is a non-implicit cast from id or block type to a
3846f4a2713aSLionel Sambuc // CoreFoundation type, delay complaining in case the cast is used
3847f4a2713aSLionel Sambuc // in an acceptable context.
3848f4a2713aSLionel Sambuc if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) &&
3849f4a2713aSLionel Sambuc CCK != CCK_ImplicitConversion)
3850f4a2713aSLionel Sambuc return ACR_unbridged;
3851f4a2713aSLionel Sambuc
3852*0a6a1f1dSLionel Sambuc // Do not issue bridge cast" diagnostic when implicit casting a cstring
3853*0a6a1f1dSLionel Sambuc // to 'NSString *'. Let caller issue a normal mismatched diagnostic with
3854*0a6a1f1dSLionel Sambuc // suitable fix-it.
3855*0a6a1f1dSLionel Sambuc if (castACTC == ACTC_retainable && exprACTC == ACTC_none &&
3856*0a6a1f1dSLionel Sambuc ConversionToObjCStringLiteralCheck(castType, castExpr))
3857*0a6a1f1dSLionel Sambuc return ACR_okay;
3858*0a6a1f1dSLionel Sambuc
3859f4a2713aSLionel Sambuc // Do not issue "bridge cast" diagnostic when implicit casting
3860f4a2713aSLionel Sambuc // a retainable object to a CF type parameter belonging to an audited
3861f4a2713aSLionel Sambuc // CF API function. Let caller issue a normal type mismatched diagnostic
3862f4a2713aSLionel Sambuc // instead.
3863f4a2713aSLionel Sambuc if (!DiagnoseCFAudited || exprACTC != ACTC_retainable ||
3864f4a2713aSLionel Sambuc castACTC != ACTC_coreFoundation)
3865*0a6a1f1dSLionel Sambuc if (!(exprACTC == ACTC_voidPtr && castACTC == ACTC_retainable &&
3866*0a6a1f1dSLionel Sambuc (Opc == BO_NE || Opc == BO_EQ)))
3867f4a2713aSLionel Sambuc diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
3868f4a2713aSLionel Sambuc castExpr, castExpr, exprACTC, CCK);
3869f4a2713aSLionel Sambuc return ACR_okay;
3870f4a2713aSLionel Sambuc }
3871f4a2713aSLionel Sambuc
3872f4a2713aSLionel Sambuc /// Given that we saw an expression with the ARCUnbridgedCastTy
3873f4a2713aSLionel Sambuc /// placeholder type, complain bitterly.
diagnoseARCUnbridgedCast(Expr * e)3874f4a2713aSLionel Sambuc void Sema::diagnoseARCUnbridgedCast(Expr *e) {
3875f4a2713aSLionel Sambuc // We expect the spurious ImplicitCastExpr to already have been stripped.
3876f4a2713aSLionel Sambuc assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
3877f4a2713aSLionel Sambuc CastExpr *realCast = cast<CastExpr>(e->IgnoreParens());
3878f4a2713aSLionel Sambuc
3879f4a2713aSLionel Sambuc SourceRange castRange;
3880f4a2713aSLionel Sambuc QualType castType;
3881f4a2713aSLionel Sambuc CheckedConversionKind CCK;
3882f4a2713aSLionel Sambuc
3883f4a2713aSLionel Sambuc if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) {
3884f4a2713aSLionel Sambuc castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc());
3885f4a2713aSLionel Sambuc castType = cast->getTypeAsWritten();
3886f4a2713aSLionel Sambuc CCK = CCK_CStyleCast;
3887f4a2713aSLionel Sambuc } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) {
3888f4a2713aSLionel Sambuc castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
3889f4a2713aSLionel Sambuc castType = cast->getTypeAsWritten();
3890f4a2713aSLionel Sambuc CCK = CCK_OtherCast;
3891f4a2713aSLionel Sambuc } else {
3892f4a2713aSLionel Sambuc castType = cast->getType();
3893f4a2713aSLionel Sambuc CCK = CCK_ImplicitConversion;
3894f4a2713aSLionel Sambuc }
3895f4a2713aSLionel Sambuc
3896f4a2713aSLionel Sambuc ARCConversionTypeClass castACTC =
3897f4a2713aSLionel Sambuc classifyTypeForARCConversion(castType.getNonReferenceType());
3898f4a2713aSLionel Sambuc
3899f4a2713aSLionel Sambuc Expr *castExpr = realCast->getSubExpr();
3900f4a2713aSLionel Sambuc assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable);
3901f4a2713aSLionel Sambuc
3902f4a2713aSLionel Sambuc diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
3903f4a2713aSLionel Sambuc castExpr, realCast, ACTC_retainable, CCK);
3904f4a2713aSLionel Sambuc }
3905f4a2713aSLionel Sambuc
3906f4a2713aSLionel Sambuc /// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast
3907f4a2713aSLionel Sambuc /// type, remove the placeholder cast.
stripARCUnbridgedCast(Expr * e)3908f4a2713aSLionel Sambuc Expr *Sema::stripARCUnbridgedCast(Expr *e) {
3909f4a2713aSLionel Sambuc assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
3910f4a2713aSLionel Sambuc
3911f4a2713aSLionel Sambuc if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
3912f4a2713aSLionel Sambuc Expr *sub = stripARCUnbridgedCast(pe->getSubExpr());
3913f4a2713aSLionel Sambuc return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub);
3914f4a2713aSLionel Sambuc } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
3915f4a2713aSLionel Sambuc assert(uo->getOpcode() == UO_Extension);
3916f4a2713aSLionel Sambuc Expr *sub = stripARCUnbridgedCast(uo->getSubExpr());
3917f4a2713aSLionel Sambuc return new (Context) UnaryOperator(sub, UO_Extension, sub->getType(),
3918f4a2713aSLionel Sambuc sub->getValueKind(), sub->getObjectKind(),
3919f4a2713aSLionel Sambuc uo->getOperatorLoc());
3920f4a2713aSLionel Sambuc } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
3921f4a2713aSLionel Sambuc assert(!gse->isResultDependent());
3922f4a2713aSLionel Sambuc
3923f4a2713aSLionel Sambuc unsigned n = gse->getNumAssocs();
3924f4a2713aSLionel Sambuc SmallVector<Expr*, 4> subExprs(n);
3925f4a2713aSLionel Sambuc SmallVector<TypeSourceInfo*, 4> subTypes(n);
3926f4a2713aSLionel Sambuc for (unsigned i = 0; i != n; ++i) {
3927f4a2713aSLionel Sambuc subTypes[i] = gse->getAssocTypeSourceInfo(i);
3928f4a2713aSLionel Sambuc Expr *sub = gse->getAssocExpr(i);
3929f4a2713aSLionel Sambuc if (i == gse->getResultIndex())
3930f4a2713aSLionel Sambuc sub = stripARCUnbridgedCast(sub);
3931f4a2713aSLionel Sambuc subExprs[i] = sub;
3932f4a2713aSLionel Sambuc }
3933f4a2713aSLionel Sambuc
3934f4a2713aSLionel Sambuc return new (Context) GenericSelectionExpr(Context, gse->getGenericLoc(),
3935f4a2713aSLionel Sambuc gse->getControllingExpr(),
3936f4a2713aSLionel Sambuc subTypes, subExprs,
3937f4a2713aSLionel Sambuc gse->getDefaultLoc(),
3938f4a2713aSLionel Sambuc gse->getRParenLoc(),
3939f4a2713aSLionel Sambuc gse->containsUnexpandedParameterPack(),
3940f4a2713aSLionel Sambuc gse->getResultIndex());
3941f4a2713aSLionel Sambuc } else {
3942f4a2713aSLionel Sambuc assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!");
3943f4a2713aSLionel Sambuc return cast<ImplicitCastExpr>(e)->getSubExpr();
3944f4a2713aSLionel Sambuc }
3945f4a2713aSLionel Sambuc }
3946f4a2713aSLionel Sambuc
CheckObjCARCUnavailableWeakConversion(QualType castType,QualType exprType)3947f4a2713aSLionel Sambuc bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType,
3948f4a2713aSLionel Sambuc QualType exprType) {
3949f4a2713aSLionel Sambuc QualType canCastType =
3950f4a2713aSLionel Sambuc Context.getCanonicalType(castType).getUnqualifiedType();
3951f4a2713aSLionel Sambuc QualType canExprType =
3952f4a2713aSLionel Sambuc Context.getCanonicalType(exprType).getUnqualifiedType();
3953f4a2713aSLionel Sambuc if (isa<ObjCObjectPointerType>(canCastType) &&
3954f4a2713aSLionel Sambuc castType.getObjCLifetime() == Qualifiers::OCL_Weak &&
3955f4a2713aSLionel Sambuc canExprType->isObjCObjectPointerType()) {
3956f4a2713aSLionel Sambuc if (const ObjCObjectPointerType *ObjT =
3957f4a2713aSLionel Sambuc canExprType->getAs<ObjCObjectPointerType>())
3958f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl())
3959f4a2713aSLionel Sambuc return !ObjI->isArcWeakrefUnavailable();
3960f4a2713aSLionel Sambuc }
3961f4a2713aSLionel Sambuc return true;
3962f4a2713aSLionel Sambuc }
3963f4a2713aSLionel Sambuc
3964f4a2713aSLionel Sambuc /// Look for an ObjCReclaimReturnedObject cast and destroy it.
maybeUndoReclaimObject(Expr * e)3965f4a2713aSLionel Sambuc static Expr *maybeUndoReclaimObject(Expr *e) {
3966f4a2713aSLionel Sambuc // For now, we just undo operands that are *immediately* reclaim
3967f4a2713aSLionel Sambuc // expressions, which prevents the vast majority of potential
3968f4a2713aSLionel Sambuc // problems here. To catch them all, we'd need to rebuild arbitrary
3969f4a2713aSLionel Sambuc // value-propagating subexpressions --- we can't reliably rebuild
3970f4a2713aSLionel Sambuc // in-place because of expression sharing.
3971f4a2713aSLionel Sambuc if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3972f4a2713aSLionel Sambuc if (ice->getCastKind() == CK_ARCReclaimReturnedObject)
3973f4a2713aSLionel Sambuc return ice->getSubExpr();
3974f4a2713aSLionel Sambuc
3975f4a2713aSLionel Sambuc return e;
3976f4a2713aSLionel Sambuc }
3977f4a2713aSLionel Sambuc
BuildObjCBridgedCast(SourceLocation LParenLoc,ObjCBridgeCastKind Kind,SourceLocation BridgeKeywordLoc,TypeSourceInfo * TSInfo,Expr * SubExpr)3978f4a2713aSLionel Sambuc ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc,
3979f4a2713aSLionel Sambuc ObjCBridgeCastKind Kind,
3980f4a2713aSLionel Sambuc SourceLocation BridgeKeywordLoc,
3981f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo,
3982f4a2713aSLionel Sambuc Expr *SubExpr) {
3983f4a2713aSLionel Sambuc ExprResult SubResult = UsualUnaryConversions(SubExpr);
3984f4a2713aSLionel Sambuc if (SubResult.isInvalid()) return ExprError();
3985*0a6a1f1dSLionel Sambuc SubExpr = SubResult.get();
3986f4a2713aSLionel Sambuc
3987f4a2713aSLionel Sambuc QualType T = TSInfo->getType();
3988f4a2713aSLionel Sambuc QualType FromType = SubExpr->getType();
3989f4a2713aSLionel Sambuc
3990f4a2713aSLionel Sambuc CastKind CK;
3991f4a2713aSLionel Sambuc
3992f4a2713aSLionel Sambuc bool MustConsume = false;
3993f4a2713aSLionel Sambuc if (T->isDependentType() || SubExpr->isTypeDependent()) {
3994f4a2713aSLionel Sambuc // Okay: we'll build a dependent expression type.
3995f4a2713aSLionel Sambuc CK = CK_Dependent;
3996f4a2713aSLionel Sambuc } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) {
3997f4a2713aSLionel Sambuc // Casting CF -> id
3998f4a2713aSLionel Sambuc CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast
3999f4a2713aSLionel Sambuc : CK_CPointerToObjCPointerCast);
4000f4a2713aSLionel Sambuc switch (Kind) {
4001f4a2713aSLionel Sambuc case OBC_Bridge:
4002f4a2713aSLionel Sambuc break;
4003f4a2713aSLionel Sambuc
4004f4a2713aSLionel Sambuc case OBC_BridgeRetained: {
4005f4a2713aSLionel Sambuc bool br = isKnownName("CFBridgingRelease");
4006f4a2713aSLionel Sambuc Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4007f4a2713aSLionel Sambuc << 2
4008f4a2713aSLionel Sambuc << FromType
4009f4a2713aSLionel Sambuc << (T->isBlockPointerType()? 1 : 0)
4010f4a2713aSLionel Sambuc << T
4011f4a2713aSLionel Sambuc << SubExpr->getSourceRange()
4012f4a2713aSLionel Sambuc << Kind;
4013f4a2713aSLionel Sambuc Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4014f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge");
4015f4a2713aSLionel Sambuc Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
4016f4a2713aSLionel Sambuc << FromType << br
4017f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(BridgeKeywordLoc,
4018f4a2713aSLionel Sambuc br ? "CFBridgingRelease "
4019f4a2713aSLionel Sambuc : "__bridge_transfer ");
4020f4a2713aSLionel Sambuc
4021f4a2713aSLionel Sambuc Kind = OBC_Bridge;
4022f4a2713aSLionel Sambuc break;
4023f4a2713aSLionel Sambuc }
4024f4a2713aSLionel Sambuc
4025f4a2713aSLionel Sambuc case OBC_BridgeTransfer:
4026f4a2713aSLionel Sambuc // We must consume the Objective-C object produced by the cast.
4027f4a2713aSLionel Sambuc MustConsume = true;
4028f4a2713aSLionel Sambuc break;
4029f4a2713aSLionel Sambuc }
4030f4a2713aSLionel Sambuc } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) {
4031f4a2713aSLionel Sambuc // Okay: id -> CF
4032f4a2713aSLionel Sambuc CK = CK_BitCast;
4033f4a2713aSLionel Sambuc switch (Kind) {
4034f4a2713aSLionel Sambuc case OBC_Bridge:
4035f4a2713aSLionel Sambuc // Reclaiming a value that's going to be __bridge-casted to CF
4036f4a2713aSLionel Sambuc // is very dangerous, so we don't do it.
4037f4a2713aSLionel Sambuc SubExpr = maybeUndoReclaimObject(SubExpr);
4038f4a2713aSLionel Sambuc break;
4039f4a2713aSLionel Sambuc
4040f4a2713aSLionel Sambuc case OBC_BridgeRetained:
4041f4a2713aSLionel Sambuc // Produce the object before casting it.
4042f4a2713aSLionel Sambuc SubExpr = ImplicitCastExpr::Create(Context, FromType,
4043f4a2713aSLionel Sambuc CK_ARCProduceObject,
4044*0a6a1f1dSLionel Sambuc SubExpr, nullptr, VK_RValue);
4045f4a2713aSLionel Sambuc break;
4046f4a2713aSLionel Sambuc
4047f4a2713aSLionel Sambuc case OBC_BridgeTransfer: {
4048f4a2713aSLionel Sambuc bool br = isKnownName("CFBridgingRetain");
4049f4a2713aSLionel Sambuc Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4050f4a2713aSLionel Sambuc << (FromType->isBlockPointerType()? 1 : 0)
4051f4a2713aSLionel Sambuc << FromType
4052f4a2713aSLionel Sambuc << 2
4053f4a2713aSLionel Sambuc << T
4054f4a2713aSLionel Sambuc << SubExpr->getSourceRange()
4055f4a2713aSLionel Sambuc << Kind;
4056f4a2713aSLionel Sambuc
4057f4a2713aSLionel Sambuc Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4058f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge ");
4059f4a2713aSLionel Sambuc Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
4060f4a2713aSLionel Sambuc << T << br
4061f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(BridgeKeywordLoc,
4062f4a2713aSLionel Sambuc br ? "CFBridgingRetain " : "__bridge_retained");
4063f4a2713aSLionel Sambuc
4064f4a2713aSLionel Sambuc Kind = OBC_Bridge;
4065f4a2713aSLionel Sambuc break;
4066f4a2713aSLionel Sambuc }
4067f4a2713aSLionel Sambuc }
4068f4a2713aSLionel Sambuc } else {
4069f4a2713aSLionel Sambuc Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
4070f4a2713aSLionel Sambuc << FromType << T << Kind
4071f4a2713aSLionel Sambuc << SubExpr->getSourceRange()
4072f4a2713aSLionel Sambuc << TSInfo->getTypeLoc().getSourceRange();
4073f4a2713aSLionel Sambuc return ExprError();
4074f4a2713aSLionel Sambuc }
4075f4a2713aSLionel Sambuc
4076f4a2713aSLionel Sambuc Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK,
4077f4a2713aSLionel Sambuc BridgeKeywordLoc,
4078f4a2713aSLionel Sambuc TSInfo, SubExpr);
4079f4a2713aSLionel Sambuc
4080f4a2713aSLionel Sambuc if (MustConsume) {
4081f4a2713aSLionel Sambuc ExprNeedsCleanups = true;
4082f4a2713aSLionel Sambuc Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result,
4083*0a6a1f1dSLionel Sambuc nullptr, VK_RValue);
4084f4a2713aSLionel Sambuc }
4085f4a2713aSLionel Sambuc
4086f4a2713aSLionel Sambuc return Result;
4087f4a2713aSLionel Sambuc }
4088f4a2713aSLionel Sambuc
ActOnObjCBridgedCast(Scope * S,SourceLocation LParenLoc,ObjCBridgeCastKind Kind,SourceLocation BridgeKeywordLoc,ParsedType Type,SourceLocation RParenLoc,Expr * SubExpr)4089f4a2713aSLionel Sambuc ExprResult Sema::ActOnObjCBridgedCast(Scope *S,
4090f4a2713aSLionel Sambuc SourceLocation LParenLoc,
4091f4a2713aSLionel Sambuc ObjCBridgeCastKind Kind,
4092f4a2713aSLionel Sambuc SourceLocation BridgeKeywordLoc,
4093f4a2713aSLionel Sambuc ParsedType Type,
4094f4a2713aSLionel Sambuc SourceLocation RParenLoc,
4095f4a2713aSLionel Sambuc Expr *SubExpr) {
4096*0a6a1f1dSLionel Sambuc TypeSourceInfo *TSInfo = nullptr;
4097f4a2713aSLionel Sambuc QualType T = GetTypeFromParser(Type, &TSInfo);
4098*0a6a1f1dSLionel Sambuc if (Kind == OBC_Bridge)
4099*0a6a1f1dSLionel Sambuc CheckTollFreeBridgeCast(T, SubExpr);
4100f4a2713aSLionel Sambuc if (!TSInfo)
4101f4a2713aSLionel Sambuc TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc);
4102f4a2713aSLionel Sambuc return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo,
4103f4a2713aSLionel Sambuc SubExpr);
4104f4a2713aSLionel Sambuc }
4105