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