/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaDeclObjC.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
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 declarations. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "TypeLocBuilder.h" |
14 | | #include "clang/AST/ASTConsumer.h" |
15 | | #include "clang/AST/ASTContext.h" |
16 | | #include "clang/AST/ASTMutationListener.h" |
17 | | #include "clang/AST/DeclObjC.h" |
18 | | #include "clang/AST/Expr.h" |
19 | | #include "clang/AST/ExprObjC.h" |
20 | | #include "clang/AST/RecursiveASTVisitor.h" |
21 | | #include "clang/Basic/SourceManager.h" |
22 | | #include "clang/Basic/TargetInfo.h" |
23 | | #include "clang/Sema/DeclSpec.h" |
24 | | #include "clang/Sema/Lookup.h" |
25 | | #include "clang/Sema/Scope.h" |
26 | | #include "clang/Sema/ScopeInfo.h" |
27 | | #include "clang/Sema/SemaInternal.h" |
28 | | #include "llvm/ADT/DenseMap.h" |
29 | | #include "llvm/ADT/DenseSet.h" |
30 | | |
31 | | using namespace clang; |
32 | | |
33 | | /// Check whether the given method, which must be in the 'init' |
34 | | /// family, is a valid member of that family. |
35 | | /// |
36 | | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
37 | | /// if non-null, check this as if making a call to it with the given |
38 | | /// receiver type |
39 | | /// |
40 | | /// \return true to indicate that there was an error and appropriate |
41 | | /// actions were taken |
42 | | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
43 | 2.46k | QualType receiverTypeIfCall) { |
44 | 2.46k | if (method->isInvalidDecl()) return true0 ; |
45 | | |
46 | | // This castAs is safe: methods that don't return an object |
47 | | // pointer won't be inferred as inits and will reject an explicit |
48 | | // objc_method_family(init). |
49 | | |
50 | | // We ignore protocols here. Should we? What about Class? |
51 | | |
52 | 2.46k | const ObjCObjectType *result = |
53 | 2.46k | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
54 | | |
55 | 2.46k | if (result->isObjCId()) { |
56 | 2.22k | return false; |
57 | 2.22k | } else if (243 result->isObjCClass()243 ) { |
58 | | // fall through: always an error |
59 | 243 | } else { |
60 | 243 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
61 | 243 | assert(resultClass && "unexpected object type!"); |
62 | | |
63 | | // It's okay for the result type to still be a forward declaration |
64 | | // if we're checking an interface declaration. |
65 | 243 | if (!resultClass->hasDefinition()) { |
66 | 45 | if (receiverTypeIfCall.isNull() && |
67 | 45 | !isa<ObjCImplementationDecl>(method->getDeclContext())42 ) |
68 | 24 | return false; |
69 | | |
70 | | // Otherwise, we try to compare class types. |
71 | 198 | } else { |
72 | | // If this method was declared in a protocol, we can't check |
73 | | // anything unless we have a receiver type that's an interface. |
74 | 198 | const ObjCInterfaceDecl *receiverClass = nullptr; |
75 | 198 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
76 | 0 | if (receiverTypeIfCall.isNull()) |
77 | 0 | return false; |
78 | | |
79 | 0 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
80 | 0 | ->getInterfaceDecl(); |
81 | | |
82 | | // This can be null for calls to e.g. id<Foo>. |
83 | 0 | if (!receiverClass) return false; |
84 | 198 | } else { |
85 | 198 | receiverClass = method->getClassInterface(); |
86 | 198 | assert(receiverClass && "method not associated with a class!"); |
87 | 198 | } |
88 | | |
89 | | // If either class is a subclass of the other, it's fine. |
90 | 198 | if (receiverClass->isSuperClassOf(resultClass) || |
91 | 198 | resultClass->isSuperClassOf(receiverClass)72 ) |
92 | 162 | return false; |
93 | 198 | } |
94 | 243 | } |
95 | | |
96 | 57 | SourceLocation loc = method->getLocation(); |
97 | | |
98 | | // If we're in a system header, and this is not a call, just make |
99 | | // the method unusable. |
100 | 57 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)54 ) { |
101 | 0 | method->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
102 | 0 | UnavailableAttr::IR_ARCInitReturnsUnrelated, loc)); |
103 | 0 | return true; |
104 | 0 | } |
105 | | |
106 | | // Otherwise, it's an error. |
107 | 57 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
108 | 57 | method->setInvalidDecl(); |
109 | 57 | return true; |
110 | 57 | } |
111 | | |
112 | | /// Issue a warning if the parameter of the overridden method is non-escaping |
113 | | /// but the parameter of the overriding method is not. |
114 | | static bool diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
115 | 90.7k | Sema &S) { |
116 | 90.7k | if (OldD->hasAttr<NoEscapeAttr>() && !NewD->hasAttr<NoEscapeAttr>()3.52k ) { |
117 | 6 | S.Diag(NewD->getLocation(), diag::warn_overriding_method_missing_noescape); |
118 | 6 | S.Diag(OldD->getLocation(), diag::note_overridden_marked_noescape); |
119 | 6 | return false; |
120 | 6 | } |
121 | | |
122 | 90.7k | return true; |
123 | 90.7k | } |
124 | | |
125 | | /// Produce additional diagnostics if a category conforms to a protocol that |
126 | | /// defines a method taking a non-escaping parameter. |
127 | | static void diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
128 | | const ObjCCategoryDecl *CD, |
129 | 6 | const ObjCProtocolDecl *PD, Sema &S) { |
130 | 6 | if (!diagnoseNoescape(NewD, OldD, S)) |
131 | 2 | S.Diag(CD->getLocation(), diag::note_cat_conform_to_noescape_prot) |
132 | 2 | << CD->IsClassExtension() << PD |
133 | 2 | << cast<ObjCMethodDecl>(NewD->getDeclContext()); |
134 | 6 | } |
135 | | |
136 | | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
137 | 145k | const ObjCMethodDecl *Overridden) { |
138 | 145k | if (Overridden->hasRelatedResultType() && |
139 | 145k | !NewMethod->hasRelatedResultType()28.7k ) { |
140 | | // This can only happen when the method follows a naming convention that |
141 | | // implies a related result type, and the original (overridden) method has |
142 | | // a suitable return type, but the new (overriding) method does not have |
143 | | // a suitable return type. |
144 | 42 | QualType ResultType = NewMethod->getReturnType(); |
145 | 42 | SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
146 | | |
147 | | // Figure out which class this method is part of, if any. |
148 | 42 | ObjCInterfaceDecl *CurrentClass |
149 | 42 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
150 | 42 | if (!CurrentClass) { |
151 | 37 | DeclContext *DC = NewMethod->getDeclContext(); |
152 | 37 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
153 | 0 | CurrentClass = Cat->getClassInterface(); |
154 | 37 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
155 | 34 | CurrentClass = Impl->getClassInterface(); |
156 | 3 | else if (ObjCCategoryImplDecl *CatImpl |
157 | 3 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
158 | 0 | CurrentClass = CatImpl->getClassInterface(); |
159 | 37 | } |
160 | | |
161 | 42 | if (CurrentClass) { |
162 | 39 | Diag(NewMethod->getLocation(), |
163 | 39 | diag::warn_related_result_type_compatibility_class) |
164 | 39 | << Context.getObjCInterfaceType(CurrentClass) |
165 | 39 | << ResultType |
166 | 39 | << ResultTypeRange; |
167 | 39 | } else { |
168 | 3 | Diag(NewMethod->getLocation(), |
169 | 3 | diag::warn_related_result_type_compatibility_protocol) |
170 | 3 | << ResultType |
171 | 3 | << ResultTypeRange; |
172 | 3 | } |
173 | | |
174 | 42 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
175 | 36 | Diag(Overridden->getLocation(), |
176 | 36 | diag::note_related_result_type_family) |
177 | 36 | << /*overridden method*/ 0 |
178 | 36 | << Family; |
179 | 6 | else |
180 | 6 | Diag(Overridden->getLocation(), |
181 | 6 | diag::note_related_result_type_overridden); |
182 | 42 | } |
183 | | |
184 | 145k | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
185 | 145k | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
186 | 11 | Diag(NewMethod->getLocation(), |
187 | 11 | getLangOpts().ObjCAutoRefCount |
188 | 11 | ? diag::err_nsreturns_retained_attribute_mismatch1 |
189 | 11 | : diag::warn_nsreturns_retained_attribute_mismatch10 ) |
190 | 11 | << 1; |
191 | 11 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
192 | 11 | } |
193 | 145k | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
194 | 145k | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
195 | 1 | Diag(NewMethod->getLocation(), |
196 | 1 | getLangOpts().ObjCAutoRefCount |
197 | 1 | ? diag::err_nsreturns_retained_attribute_mismatch |
198 | 1 | : diag::warn_nsreturns_retained_attribute_mismatch0 ) |
199 | 1 | << 0; |
200 | 1 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
201 | 1 | } |
202 | | |
203 | 145k | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
204 | 145k | oe = Overridden->param_end(); |
205 | 145k | for (ObjCMethodDecl::param_iterator ni = NewMethod->param_begin(), |
206 | 145k | ne = NewMethod->param_end(); |
207 | 236k | ni != ne && oi != oe90.7k ; ++ni, ++oi90.7k ) { |
208 | 90.7k | const ParmVarDecl *oldDecl = (*oi); |
209 | 90.7k | ParmVarDecl *newDecl = (*ni); |
210 | 90.7k | if (newDecl->hasAttr<NSConsumedAttr>() != |
211 | 90.7k | oldDecl->hasAttr<NSConsumedAttr>()) { |
212 | 3 | Diag(newDecl->getLocation(), |
213 | 3 | getLangOpts().ObjCAutoRefCount |
214 | 3 | ? diag::err_nsconsumed_attribute_mismatch2 |
215 | 3 | : diag::warn_nsconsumed_attribute_mismatch1 ); |
216 | 3 | Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter"; |
217 | 3 | } |
218 | | |
219 | 90.7k | diagnoseNoescape(newDecl, oldDecl, *this); |
220 | 90.7k | } |
221 | 145k | } |
222 | | |
223 | | /// Check a method declaration for compatibility with the Objective-C |
224 | | /// ARC conventions. |
225 | 34.1k | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
226 | 34.1k | ObjCMethodFamily family = method->getMethodFamily(); |
227 | 34.1k | switch (family) { |
228 | 30.2k | case OMF_None: |
229 | 30.4k | case OMF_finalize: |
230 | 30.5k | case OMF_retain: |
231 | 30.6k | case OMF_release: |
232 | 30.8k | case OMF_autorelease: |
233 | 30.9k | case OMF_retainCount: |
234 | 30.9k | case OMF_self: |
235 | 30.9k | case OMF_initialize: |
236 | 30.9k | case OMF_performSelector: |
237 | 30.9k | return false; |
238 | | |
239 | 238 | case OMF_dealloc: |
240 | 238 | if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
241 | 4 | SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
242 | 4 | if (ResultTypeRange.isInvalid()) |
243 | 2 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
244 | 2 | << method->getReturnType() |
245 | 2 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
246 | 2 | else |
247 | 2 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
248 | 2 | << method->getReturnType() |
249 | 2 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
250 | 4 | return true; |
251 | 4 | } |
252 | 234 | return false; |
253 | | |
254 | 2.26k | case OMF_init: |
255 | | // If the method doesn't obey the init rules, don't bother annotating it. |
256 | 2.26k | if (checkInitMethod(method, QualType())) |
257 | 54 | return true; |
258 | | |
259 | 2.21k | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
260 | | |
261 | | // Don't add a second copy of this attribute, but otherwise don't |
262 | | // let it be suppressed. |
263 | 2.21k | if (method->hasAttr<NSReturnsRetainedAttr>()) |
264 | 575 | return false; |
265 | 1.63k | break; |
266 | | |
267 | 1.63k | case OMF_alloc: |
268 | 308 | case OMF_copy: |
269 | 416 | case OMF_mutableCopy: |
270 | 697 | case OMF_new: |
271 | 697 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
272 | 697 | method->hasAttr<NSReturnsNotRetainedAttr>()603 || |
273 | 697 | method->hasAttr<NSReturnsAutoreleasedAttr>()596 ) |
274 | 101 | return false; |
275 | 596 | break; |
276 | 34.1k | } |
277 | | |
278 | 2.23k | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
279 | 2.23k | return false; |
280 | 34.1k | } |
281 | | |
282 | | static void DiagnoseObjCImplementedDeprecations(Sema &S, const NamedDecl *ND, |
283 | 6.31k | SourceLocation ImplLoc) { |
284 | 6.31k | if (!ND) |
285 | 0 | return; |
286 | 6.31k | bool IsCategory = false; |
287 | 6.31k | StringRef RealizedPlatform; |
288 | 6.31k | AvailabilityResult Availability = ND->getAvailability( |
289 | 6.31k | /*Message=*/nullptr, /*EnclosingVersion=*/VersionTuple(), |
290 | 6.31k | &RealizedPlatform); |
291 | 6.31k | if (Availability != AR_Deprecated) { |
292 | 6.30k | if (isa<ObjCMethodDecl>(ND)) { |
293 | 956 | if (Availability != AR_Unavailable) |
294 | 944 | return; |
295 | 12 | if (RealizedPlatform.empty()) |
296 | 7 | RealizedPlatform = S.Context.getTargetInfo().getPlatformName(); |
297 | | // Warn about implementing unavailable methods, unless the unavailable |
298 | | // is for an app extension. |
299 | 12 | if (RealizedPlatform.endswith("_app_extension")) |
300 | 2 | return; |
301 | 10 | S.Diag(ImplLoc, diag::warn_unavailable_def); |
302 | 10 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
303 | 10 | << ND->getDeclName(); |
304 | 10 | return; |
305 | 12 | } |
306 | 5.34k | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND)) { |
307 | 502 | if (!CD->getClassInterface()->isDeprecated()) |
308 | 499 | return; |
309 | 3 | ND = CD->getClassInterface(); |
310 | 3 | IsCategory = true; |
311 | 3 | } else |
312 | 4.84k | return; |
313 | 5.34k | } |
314 | 16 | S.Diag(ImplLoc, diag::warn_deprecated_def) |
315 | 16 | << (isa<ObjCMethodDecl>(ND) |
316 | 16 | ? /*Method*/ 07 |
317 | 16 | : isa<ObjCCategoryDecl>(ND)9 || IsCategory6 ? /*Category*/ 26 |
318 | 9 | : /*Class*/ 13 ); |
319 | 16 | if (isa<ObjCMethodDecl>(ND)) |
320 | 7 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
321 | 7 | << ND->getDeclName(); |
322 | 9 | else |
323 | 9 | S.Diag(ND->getLocation(), diag::note_previous_decl) |
324 | 9 | << (isa<ObjCCategoryDecl>(ND) ? "category"3 : "class"6 ); |
325 | 16 | } |
326 | | |
327 | | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
328 | | /// pool. |
329 | 7.81k | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
330 | 7.81k | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
331 | | |
332 | | // If we don't have a valid method decl, simply return. |
333 | 7.81k | if (!MDecl) |
334 | 0 | return; |
335 | 7.81k | if (MDecl->isInstanceMethod()) |
336 | 6.59k | AddInstanceMethodToGlobalPool(MDecl, true); |
337 | 1.22k | else |
338 | 1.22k | AddFactoryMethodToGlobalPool(MDecl, true); |
339 | 7.81k | } |
340 | | |
341 | | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
342 | | /// has explicit ownership attribute; false otherwise. |
343 | | static bool |
344 | 319 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
345 | 319 | QualType T = Param->getType(); |
346 | | |
347 | 319 | if (const PointerType *PT = T->getAs<PointerType>()) { |
348 | 96 | T = PT->getPointeeType(); |
349 | 223 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
350 | 4 | T = RT->getPointeeType(); |
351 | 219 | } else { |
352 | 219 | return true; |
353 | 219 | } |
354 | | |
355 | | // If we have a lifetime qualifier, but it's local, we must have |
356 | | // inferred it. So, it is implicit. |
357 | 100 | return !T.getLocalQualifiers().hasObjCLifetime(); |
358 | 319 | } |
359 | | |
360 | | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
361 | | /// and user declared, in the method definition's AST. |
362 | 7.31k | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
363 | 7.31k | ImplicitlyRetainedSelfLocs.clear(); |
364 | 7.31k | assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
365 | 0 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
366 | | |
367 | 7.31k | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
368 | | |
369 | | // If we don't have a valid method decl, simply return. |
370 | 7.31k | if (!MDecl) |
371 | 0 | return; |
372 | | |
373 | 7.31k | QualType ResultType = MDecl->getReturnType(); |
374 | 7.31k | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
375 | 7.31k | !MDecl->isInvalidDecl()3.80k && |
376 | 7.31k | RequireCompleteType(MDecl->getLocation(), ResultType, |
377 | 3.77k | diag::err_func_def_incomplete_result)) |
378 | 1 | MDecl->setInvalidDecl(); |
379 | | |
380 | | // Allow all of Sema to see that we are entering a method definition. |
381 | 7.31k | PushDeclContext(FnBodyScope, MDecl); |
382 | 7.31k | PushFunctionScope(); |
383 | | |
384 | | // Create Decl objects for each parameter, entrring them in the scope for |
385 | | // binding to their use. |
386 | | |
387 | | // Insert the invisible arguments, self and _cmd! |
388 | 7.31k | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
389 | | |
390 | 7.31k | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
391 | 7.31k | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
392 | | |
393 | | // The ObjC parser requires parameter names so there's no need to check. |
394 | 7.31k | CheckParmsForFunctionDef(MDecl->parameters(), |
395 | 7.31k | /*CheckParameterNames=*/false); |
396 | | |
397 | | // Introduce all of the other parameters into this scope. |
398 | 7.31k | for (auto *Param : MDecl->parameters()) { |
399 | 3.76k | if (!Param->isInvalidDecl() && |
400 | 3.76k | getLangOpts().ObjCAutoRefCount3.76k && |
401 | 3.76k | !HasExplicitOwnershipAttr(*this, Param)319 ) |
402 | 20 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
403 | 20 | Param->getType(); |
404 | | |
405 | 3.76k | if (Param->getIdentifier()) |
406 | 3.76k | PushOnScopeChains(Param, FnBodyScope); |
407 | 3.76k | } |
408 | | |
409 | | // In ARC, disallow definition of retain/release/autorelease/retainCount |
410 | 7.31k | if (getLangOpts().ObjCAutoRefCount) { |
411 | 928 | switch (MDecl->getMethodFamily()) { |
412 | 5 | case OMF_retain: |
413 | 10 | case OMF_retainCount: |
414 | 15 | case OMF_release: |
415 | 20 | case OMF_autorelease: |
416 | 20 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
417 | 20 | << 0 << MDecl->getSelector(); |
418 | 20 | break; |
419 | | |
420 | 578 | case OMF_None: |
421 | 684 | case OMF_dealloc: |
422 | 717 | case OMF_finalize: |
423 | 721 | case OMF_alloc: |
424 | 884 | case OMF_init: |
425 | 886 | case OMF_mutableCopy: |
426 | 892 | case OMF_copy: |
427 | 905 | case OMF_new: |
428 | 905 | case OMF_self: |
429 | 905 | case OMF_initialize: |
430 | 908 | case OMF_performSelector: |
431 | 908 | break; |
432 | 928 | } |
433 | 928 | } |
434 | | |
435 | | // Warn on deprecated methods under -Wdeprecated-implementations, |
436 | | // and prepare for warning on missing super calls. |
437 | 7.31k | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
438 | 7.30k | ObjCMethodDecl *IMD = |
439 | 7.30k | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
440 | | |
441 | 7.30k | if (IMD) { |
442 | 4.75k | ObjCImplDecl *ImplDeclOfMethodDef = |
443 | 4.75k | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
444 | 4.75k | ObjCContainerDecl *ContDeclOfMethodDecl = |
445 | 4.75k | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
446 | 4.75k | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
447 | 4.75k | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
448 | 4.06k | ImplDeclOfMethodDecl = OID->getImplementation(); |
449 | 691 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
450 | 380 | if (CD->IsClassExtension()) { |
451 | 59 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
452 | 59 | ImplDeclOfMethodDecl = OID->getImplementation(); |
453 | 59 | } else |
454 | 321 | ImplDeclOfMethodDecl = CD->getImplementation(); |
455 | 380 | } |
456 | | // No need to issue deprecated warning if deprecated mehod in class/category |
457 | | // is being implemented in its own implementation (no overriding is involved). |
458 | 4.75k | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef3.97k ) |
459 | 963 | DiagnoseObjCImplementedDeprecations(*this, IMD, MDecl->getLocation()); |
460 | 4.75k | } |
461 | | |
462 | 7.30k | if (MDecl->getMethodFamily() == OMF_init) { |
463 | 870 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
464 | 145 | getCurFunction()->ObjCIsDesignatedInit = true; |
465 | 145 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
466 | 145 | IC->getSuperClass() != nullptr; |
467 | 725 | } else if (IC->hasDesignatedInitializers()) { |
468 | 11 | getCurFunction()->ObjCIsSecondaryInit = true; |
469 | 11 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
470 | 11 | } |
471 | 870 | } |
472 | | |
473 | | // If this is "dealloc" or "finalize", set some bit here. |
474 | | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
475 | | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
476 | | // Only do this if the current class actually has a superclass. |
477 | 7.30k | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
478 | 4.14k | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
479 | 4.14k | if (Family == OMF_dealloc) { |
480 | 272 | if (!(getLangOpts().ObjCAutoRefCount || |
481 | 272 | getLangOpts().getGC() == LangOptions::GCOnly207 )) |
482 | 205 | getCurFunction()->ObjCShouldCallSuper = true; |
483 | | |
484 | 3.87k | } else if (Family == OMF_finalize) { |
485 | 12 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
486 | 4 | getCurFunction()->ObjCShouldCallSuper = true; |
487 | | |
488 | 3.86k | } else { |
489 | 3.86k | const ObjCMethodDecl *SuperMethod = |
490 | 3.86k | SuperClass->lookupMethod(MDecl->getSelector(), |
491 | 3.86k | MDecl->isInstanceMethod()); |
492 | 3.86k | getCurFunction()->ObjCShouldCallSuper = |
493 | 3.86k | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()950 ); |
494 | 3.86k | } |
495 | 4.14k | } |
496 | 7.30k | } |
497 | 7.31k | } |
498 | | |
499 | | namespace { |
500 | | |
501 | | // Callback to only accept typo corrections that are Objective-C classes. |
502 | | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
503 | | // function will reject corrections to that class. |
504 | | class ObjCInterfaceValidatorCCC final : public CorrectionCandidateCallback { |
505 | | public: |
506 | 62 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
507 | | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
508 | 4 | : CurrentIDecl(IDecl) {} |
509 | | |
510 | 26 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
511 | 26 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
512 | 26 | return ID && !declaresSameEntity(ID, CurrentIDecl)24 ; |
513 | 26 | } |
514 | | |
515 | 64 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
516 | 64 | return std::make_unique<ObjCInterfaceValidatorCCC>(*this); |
517 | 64 | } |
518 | | |
519 | | private: |
520 | | ObjCInterfaceDecl *CurrentIDecl; |
521 | | }; |
522 | | |
523 | | } // end anonymous namespace |
524 | | |
525 | | static void diagnoseUseOfProtocols(Sema &TheSema, |
526 | | ObjCContainerDecl *CD, |
527 | | ObjCProtocolDecl *const *ProtoRefs, |
528 | | unsigned NumProtoRefs, |
529 | 39.6k | const SourceLocation *ProtoLocs) { |
530 | 39.6k | assert(ProtoRefs); |
531 | | // Diagnose availability in the context of the ObjC container. |
532 | 0 | Sema::ContextRAII SavedContext(TheSema, CD); |
533 | 95.9k | for (unsigned i = 0; i < NumProtoRefs; ++i56.2k ) { |
534 | 56.2k | (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i], |
535 | 56.2k | /*UnknownObjCClass=*/nullptr, |
536 | 56.2k | /*ObjCPropertyAccess=*/false, |
537 | 56.2k | /*AvoidPartialAvailabilityChecks=*/true); |
538 | 56.2k | } |
539 | 39.6k | } |
540 | | |
541 | | void Sema:: |
542 | | ActOnSuperClassOfClassInterface(Scope *S, |
543 | | SourceLocation AtInterfaceLoc, |
544 | | ObjCInterfaceDecl *IDecl, |
545 | | IdentifierInfo *ClassName, |
546 | | SourceLocation ClassLoc, |
547 | | IdentifierInfo *SuperName, |
548 | | SourceLocation SuperLoc, |
549 | | ArrayRef<ParsedType> SuperTypeArgs, |
550 | 63.3k | SourceRange SuperTypeArgsRange) { |
551 | | // Check if a different kind of symbol declared in this scope. |
552 | 63.3k | NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
553 | 63.3k | LookupOrdinaryName); |
554 | | |
555 | 63.3k | if (!PrevDecl) { |
556 | | // Try to correct for a typo in the superclass name without correcting |
557 | | // to the class we're defining. |
558 | 4 | ObjCInterfaceValidatorCCC CCC(IDecl); |
559 | 4 | if (TypoCorrection Corrected = CorrectTypo( |
560 | 4 | DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, |
561 | 4 | TUScope, nullptr, CCC, CTK_ErrorRecovery)) { |
562 | 1 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
563 | 1 | << SuperName << ClassName); |
564 | 1 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
565 | 1 | } |
566 | 4 | } |
567 | | |
568 | 63.3k | if (declaresSameEntity(PrevDecl, IDecl)) { |
569 | 1 | Diag(SuperLoc, diag::err_recursive_superclass) |
570 | 1 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
571 | 1 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
572 | 63.3k | } else { |
573 | 63.3k | ObjCInterfaceDecl *SuperClassDecl = |
574 | 63.3k | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
575 | 63.3k | QualType SuperClassType; |
576 | | |
577 | | // Diagnose classes that inherit from deprecated classes. |
578 | 63.3k | if (SuperClassDecl) { |
579 | 63.1k | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
580 | 63.1k | SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
581 | 63.1k | } |
582 | | |
583 | 63.3k | if (PrevDecl && !SuperClassDecl63.3k ) { |
584 | | // The previous declaration was not a class decl. Check if we have a |
585 | | // typedef. If we do, get the underlying class type. |
586 | 136 | if (const TypedefNameDecl *TDecl = |
587 | 136 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
588 | 136 | QualType T = TDecl->getUnderlyingType(); |
589 | 136 | if (T->isObjCObjectType()) { |
590 | 135 | if (NamedDecl *IDecl = T->castAs<ObjCObjectType>()->getInterface()) { |
591 | 135 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
592 | 135 | SuperClassType = Context.getTypeDeclType(TDecl); |
593 | | |
594 | | // This handles the following case: |
595 | | // @interface NewI @end |
596 | | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
597 | | // @interface SI : DeprI /* warn here */ @end |
598 | 135 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
599 | 135 | } |
600 | 135 | } |
601 | 136 | } |
602 | | |
603 | | // This handles the following case: |
604 | | // |
605 | | // typedef int SuperClass; |
606 | | // @interface MyClass : SuperClass {} @end |
607 | | // |
608 | 136 | if (!SuperClassDecl) { |
609 | 1 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
610 | 1 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
611 | 1 | } |
612 | 136 | } |
613 | | |
614 | 63.3k | if (!isa_and_nonnull<TypedefNameDecl>(PrevDecl)) { |
615 | 63.1k | if (!SuperClassDecl) |
616 | 3 | Diag(SuperLoc, diag::err_undef_superclass) |
617 | 3 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
618 | 63.1k | else if (RequireCompleteType(SuperLoc, |
619 | 63.1k | SuperClassType, |
620 | 63.1k | diag::err_forward_superclass, |
621 | 63.1k | SuperClassDecl->getDeclName(), |
622 | 63.1k | ClassName, |
623 | 63.1k | SourceRange(AtInterfaceLoc, ClassLoc))) { |
624 | 5 | SuperClassDecl = nullptr; |
625 | 5 | SuperClassType = QualType(); |
626 | 5 | } |
627 | 63.1k | } |
628 | | |
629 | 63.3k | if (SuperClassType.isNull()) { |
630 | 9 | assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
631 | 0 | return; |
632 | 9 | } |
633 | | |
634 | | // Handle type arguments on the superclass. |
635 | 63.3k | TypeSourceInfo *SuperClassTInfo = nullptr; |
636 | 63.3k | if (!SuperTypeArgs.empty()) { |
637 | 1.36k | TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
638 | 1.36k | S, |
639 | 1.36k | SuperLoc, |
640 | 1.36k | CreateParsedType(SuperClassType, |
641 | 1.36k | nullptr), |
642 | 1.36k | SuperTypeArgsRange.getBegin(), |
643 | 1.36k | SuperTypeArgs, |
644 | 1.36k | SuperTypeArgsRange.getEnd(), |
645 | 1.36k | SourceLocation(), |
646 | 1.36k | { }, |
647 | 1.36k | { }, |
648 | 1.36k | SourceLocation()); |
649 | 1.36k | if (!fullSuperClassType.isUsable()) |
650 | 0 | return; |
651 | | |
652 | 1.36k | SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
653 | 1.36k | &SuperClassTInfo); |
654 | 1.36k | } |
655 | | |
656 | 63.3k | if (!SuperClassTInfo) { |
657 | 61.9k | SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
658 | 61.9k | SuperLoc); |
659 | 61.9k | } |
660 | | |
661 | 63.3k | IDecl->setSuperClass(SuperClassTInfo); |
662 | 63.3k | IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getEndLoc()); |
663 | 63.3k | } |
664 | 63.3k | } |
665 | | |
666 | | DeclResult Sema::actOnObjCTypeParam(Scope *S, |
667 | | ObjCTypeParamVariance variance, |
668 | | SourceLocation varianceLoc, |
669 | | unsigned index, |
670 | | IdentifierInfo *paramName, |
671 | | SourceLocation paramLoc, |
672 | | SourceLocation colonLoc, |
673 | 47.0k | ParsedType parsedTypeBound) { |
674 | | // If there was an explicitly-provided type bound, check it. |
675 | 47.0k | TypeSourceInfo *typeBoundInfo = nullptr; |
676 | 47.0k | if (parsedTypeBound) { |
677 | | // The type bound can be any Objective-C pointer type. |
678 | 402 | QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
679 | 402 | if (typeBound->isObjCObjectPointerType()) { |
680 | | // okay |
681 | 400 | } else if (2 typeBound->isObjCObjectType()2 ) { |
682 | | // The user forgot the * on an Objective-C pointer type, e.g., |
683 | | // "T : NSView". |
684 | 1 | SourceLocation starLoc = getLocForEndOfToken( |
685 | 1 | typeBoundInfo->getTypeLoc().getEndLoc()); |
686 | 1 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
687 | 1 | diag::err_objc_type_param_bound_missing_pointer) |
688 | 1 | << typeBound << paramName |
689 | 1 | << FixItHint::CreateInsertion(starLoc, " *"); |
690 | | |
691 | | // Create a new type location builder so we can update the type |
692 | | // location information we have. |
693 | 1 | TypeLocBuilder builder; |
694 | 1 | builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
695 | | |
696 | | // Create the Objective-C pointer type. |
697 | 1 | typeBound = Context.getObjCObjectPointerType(typeBound); |
698 | 1 | ObjCObjectPointerTypeLoc newT |
699 | 1 | = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
700 | 1 | newT.setStarLoc(starLoc); |
701 | | |
702 | | // Form the new type source information. |
703 | 1 | typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
704 | 1 | } else { |
705 | | // Not a valid type bound. |
706 | 1 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
707 | 1 | diag::err_objc_type_param_bound_nonobject) |
708 | 1 | << typeBound << paramName; |
709 | | |
710 | | // Forget the bound; we'll default to id later. |
711 | 1 | typeBoundInfo = nullptr; |
712 | 1 | } |
713 | | |
714 | | // Type bounds cannot have qualifiers (even indirectly) or explicit |
715 | | // nullability. |
716 | 402 | if (typeBoundInfo) { |
717 | 401 | QualType typeBound = typeBoundInfo->getType(); |
718 | 401 | TypeLoc qual = typeBoundInfo->getTypeLoc().findExplicitQualifierLoc(); |
719 | 401 | if (qual || typeBound.hasQualifiers()386 ) { |
720 | 19 | bool diagnosed = false; |
721 | 19 | SourceRange rangeToRemove; |
722 | 19 | if (qual) { |
723 | 15 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
724 | 11 | rangeToRemove = attr.getLocalSourceRange(); |
725 | 11 | if (attr.getTypePtr()->getImmediateNullability()) { |
726 | 1 | Diag(attr.getBeginLoc(), |
727 | 1 | diag::err_objc_type_param_bound_explicit_nullability) |
728 | 1 | << paramName << typeBound |
729 | 1 | << FixItHint::CreateRemoval(rangeToRemove); |
730 | 1 | diagnosed = true; |
731 | 1 | } |
732 | 11 | } |
733 | 15 | } |
734 | | |
735 | 19 | if (!diagnosed) { |
736 | 18 | Diag(qual ? qual.getBeginLoc()14 |
737 | 18 | : typeBoundInfo->getTypeLoc().getBeginLoc()4 , |
738 | 18 | diag::err_objc_type_param_bound_qualified) |
739 | 18 | << paramName << typeBound |
740 | 18 | << typeBound.getQualifiers().getAsString() |
741 | 18 | << FixItHint::CreateRemoval(rangeToRemove); |
742 | 18 | } |
743 | | |
744 | | // If the type bound has qualifiers other than CVR, we need to strip |
745 | | // them or we'll probably assert later when trying to apply new |
746 | | // qualifiers. |
747 | 19 | Qualifiers quals = typeBound.getQualifiers(); |
748 | 19 | quals.removeCVRQualifiers(); |
749 | 19 | if (!quals.empty()) { |
750 | 14 | typeBoundInfo = |
751 | 14 | Context.getTrivialTypeSourceInfo(typeBound.getUnqualifiedType()); |
752 | 14 | } |
753 | 19 | } |
754 | 401 | } |
755 | 402 | } |
756 | | |
757 | | // If there was no explicit type bound (or we removed it due to an error), |
758 | | // use 'id' instead. |
759 | 47.0k | if (!typeBoundInfo) { |
760 | 46.6k | colonLoc = SourceLocation(); |
761 | 46.6k | typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
762 | 46.6k | } |
763 | | |
764 | | // Create the type parameter. |
765 | 47.0k | return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
766 | 47.0k | index, paramLoc, paramName, colonLoc, |
767 | 47.0k | typeBoundInfo); |
768 | 47.0k | } |
769 | | |
770 | | ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
771 | | SourceLocation lAngleLoc, |
772 | | ArrayRef<Decl *> typeParamsIn, |
773 | 35.7k | SourceLocation rAngleLoc) { |
774 | | // We know that the array only contains Objective-C type parameters. |
775 | 35.7k | ArrayRef<ObjCTypeParamDecl *> |
776 | 35.7k | typeParams( |
777 | 35.7k | reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
778 | 35.7k | typeParamsIn.size()); |
779 | | |
780 | | // Diagnose redeclarations of type parameters. |
781 | | // We do this now because Objective-C type parameters aren't pushed into |
782 | | // scope until later (after the instance variable block), but we want the |
783 | | // diagnostics to occur right after we parse the type parameter list. |
784 | 35.7k | llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
785 | 47.0k | for (auto typeParam : typeParams) { |
786 | 47.0k | auto known = knownParams.find(typeParam->getIdentifier()); |
787 | 47.0k | if (known != knownParams.end()) { |
788 | 2 | Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
789 | 2 | << typeParam->getIdentifier() |
790 | 2 | << SourceRange(known->second->getLocation()); |
791 | | |
792 | 2 | typeParam->setInvalidDecl(); |
793 | 47.0k | } else { |
794 | 47.0k | knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
795 | | |
796 | | // Push the type parameter into scope. |
797 | 47.0k | PushOnScopeChains(typeParam, S, /*AddToContext=*/false); |
798 | 47.0k | } |
799 | 47.0k | } |
800 | | |
801 | | // Create the parameter list. |
802 | 35.7k | return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
803 | 35.7k | } |
804 | | |
805 | 35.7k | void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
806 | 47.0k | for (auto typeParam : *typeParamList) { |
807 | 47.0k | if (!typeParam->isInvalidDecl()) { |
808 | 47.0k | S->RemoveDecl(typeParam); |
809 | 47.0k | IdResolver.RemoveDecl(typeParam); |
810 | 47.0k | } |
811 | 47.0k | } |
812 | 35.7k | } |
813 | | |
814 | | namespace { |
815 | | /// The context in which an Objective-C type parameter list occurs, for use |
816 | | /// in diagnostics. |
817 | | enum class TypeParamListContext { |
818 | | ForwardDeclaration, |
819 | | Definition, |
820 | | Category, |
821 | | Extension |
822 | | }; |
823 | | } // end anonymous namespace |
824 | | |
825 | | /// Check consistency between two Objective-C type parameter lists, e.g., |
826 | | /// between a category/extension and an \@interface or between an \@class and an |
827 | | /// \@interface. |
828 | | static bool checkTypeParamListConsistency(Sema &S, |
829 | | ObjCTypeParamList *prevTypeParams, |
830 | | ObjCTypeParamList *newTypeParams, |
831 | 31.7k | TypeParamListContext newContext) { |
832 | | // If the sizes don't match, complain about that. |
833 | 31.7k | if (prevTypeParams->size() != newTypeParams->size()) { |
834 | 2 | SourceLocation diagLoc; |
835 | 2 | if (newTypeParams->size() > prevTypeParams->size()) { |
836 | 1 | diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
837 | 1 | } else { |
838 | 1 | diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getEndLoc()); |
839 | 1 | } |
840 | | |
841 | 2 | S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
842 | 2 | << static_cast<unsigned>(newContext) |
843 | 2 | << (newTypeParams->size() > prevTypeParams->size()) |
844 | 2 | << prevTypeParams->size() |
845 | 2 | << newTypeParams->size(); |
846 | | |
847 | 2 | return true; |
848 | 2 | } |
849 | | |
850 | | // Match up the type parameters. |
851 | 73.9k | for (unsigned i = 0, n = prevTypeParams->size(); 31.7k i != n; ++i42.1k ) { |
852 | 42.1k | ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
853 | 42.1k | ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
854 | | |
855 | | // Check for consistency of the variance. |
856 | 42.1k | if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
857 | 36.8k | if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
858 | 36.8k | newContext != TypeParamListContext::Definition35.9k ) { |
859 | | // When the new type parameter is invariant and is not part |
860 | | // of the definition, just propagate the variance. |
861 | 35.9k | newTypeParam->setVariance(prevTypeParam->getVariance()); |
862 | 35.9k | } else if (882 prevTypeParam->getVariance() |
863 | 882 | == ObjCTypeParamVariance::Invariant && |
864 | 882 | !(878 isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext())878 && |
865 | 878 | cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
866 | 878 | ->getDefinition() == prevTypeParam->getDeclContext())) { |
867 | | // When the old parameter is invariant and was not part of the |
868 | | // definition, just ignore the difference because it doesn't |
869 | | // matter. |
870 | 878 | } else { |
871 | 4 | { |
872 | | // Diagnose the conflict and update the second declaration. |
873 | 4 | SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
874 | 4 | if (diagLoc.isInvalid()) |
875 | 1 | diagLoc = newTypeParam->getBeginLoc(); |
876 | | |
877 | 4 | auto diag = S.Diag(diagLoc, |
878 | 4 | diag::err_objc_type_param_variance_conflict) |
879 | 4 | << static_cast<unsigned>(newTypeParam->getVariance()) |
880 | 4 | << newTypeParam->getDeclName() |
881 | 4 | << static_cast<unsigned>(prevTypeParam->getVariance()) |
882 | 4 | << prevTypeParam->getDeclName(); |
883 | 4 | switch (prevTypeParam->getVariance()) { |
884 | 0 | case ObjCTypeParamVariance::Invariant: |
885 | 0 | diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
886 | 0 | break; |
887 | | |
888 | 2 | case ObjCTypeParamVariance::Covariant: |
889 | 4 | case ObjCTypeParamVariance::Contravariant: { |
890 | 4 | StringRef newVarianceStr |
891 | 4 | = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
892 | 4 | ? "__covariant"2 |
893 | 4 | : "__contravariant"2 ; |
894 | 4 | if (newTypeParam->getVariance() |
895 | 4 | == ObjCTypeParamVariance::Invariant) { |
896 | 1 | diag << FixItHint::CreateInsertion(newTypeParam->getBeginLoc(), |
897 | 1 | (newVarianceStr + " ").str()); |
898 | 3 | } else { |
899 | 3 | diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
900 | 3 | newVarianceStr); |
901 | 3 | } |
902 | 4 | } |
903 | 4 | } |
904 | 4 | } |
905 | | |
906 | 4 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
907 | 4 | << prevTypeParam->getDeclName(); |
908 | | |
909 | | // Override the variance. |
910 | 4 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
911 | 4 | } |
912 | 36.8k | } |
913 | | |
914 | | // If the bound types match, there's nothing to do. |
915 | 42.1k | if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
916 | 42.1k | newTypeParam->getUnderlyingType())) |
917 | 42.0k | continue; |
918 | | |
919 | | // If the new type parameter's bound was explicit, complain about it being |
920 | | // different from the original. |
921 | 16 | if (newTypeParam->hasExplicitBound()) { |
922 | 5 | SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
923 | 5 | ->getTypeLoc().getSourceRange(); |
924 | 5 | S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
925 | 5 | << newTypeParam->getUnderlyingType() |
926 | 5 | << newTypeParam->getDeclName() |
927 | 5 | << prevTypeParam->hasExplicitBound() |
928 | 5 | << prevTypeParam->getUnderlyingType() |
929 | 5 | << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
930 | 5 | << prevTypeParam->getDeclName() |
931 | 5 | << FixItHint::CreateReplacement( |
932 | 5 | newBoundRange, |
933 | 5 | prevTypeParam->getUnderlyingType().getAsString( |
934 | 5 | S.Context.getPrintingPolicy())); |
935 | | |
936 | 5 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
937 | 5 | << prevTypeParam->getDeclName(); |
938 | | |
939 | | // Override the new type parameter's bound type with the previous type, |
940 | | // so that it's consistent. |
941 | 5 | S.Context.adjustObjCTypeParamBoundType(prevTypeParam, newTypeParam); |
942 | 5 | continue; |
943 | 5 | } |
944 | | |
945 | | // The new type parameter got the implicit bound of 'id'. That's okay for |
946 | | // categories and extensions (overwrite it later), but not for forward |
947 | | // declarations and @interfaces, because those must be standalone. |
948 | 11 | if (newContext == TypeParamListContext::ForwardDeclaration || |
949 | 11 | newContext == TypeParamListContext::Definition9 ) { |
950 | | // Diagnose this problem for forward declarations and definitions. |
951 | 3 | SourceLocation insertionLoc |
952 | 3 | = S.getLocForEndOfToken(newTypeParam->getLocation()); |
953 | 3 | std::string newCode |
954 | 3 | = " : " + prevTypeParam->getUnderlyingType().getAsString( |
955 | 3 | S.Context.getPrintingPolicy()); |
956 | 3 | S.Diag(newTypeParam->getLocation(), |
957 | 3 | diag::err_objc_type_param_bound_missing) |
958 | 3 | << prevTypeParam->getUnderlyingType() |
959 | 3 | << newTypeParam->getDeclName() |
960 | 3 | << (newContext == TypeParamListContext::ForwardDeclaration) |
961 | 3 | << FixItHint::CreateInsertion(insertionLoc, newCode); |
962 | | |
963 | 3 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
964 | 3 | << prevTypeParam->getDeclName(); |
965 | 3 | } |
966 | | |
967 | | // Update the new type parameter's bound to match the previous one. |
968 | 11 | S.Context.adjustObjCTypeParamBoundType(prevTypeParam, newTypeParam); |
969 | 11 | } |
970 | | |
971 | 31.7k | return false; |
972 | 31.7k | } |
973 | | |
974 | | ObjCInterfaceDecl *Sema::ActOnStartClassInterface( |
975 | | Scope *S, SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
976 | | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
977 | | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
978 | | ArrayRef<ParsedType> SuperTypeArgs, SourceRange SuperTypeArgsRange, |
979 | | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
980 | | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
981 | 71.5k | const ParsedAttributesView &AttrList) { |
982 | 71.5k | assert(ClassName && "Missing class identifier"); |
983 | | |
984 | | // Check for another declaration kind with the same name. |
985 | 0 | NamedDecl *PrevDecl = |
986 | 71.5k | LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
987 | 71.5k | forRedeclarationInCurContext()); |
988 | | |
989 | 71.5k | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)18.6k ) { |
990 | 2 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
991 | 2 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
992 | 2 | } |
993 | | |
994 | | // Create a declaration to describe this @interface. |
995 | 71.5k | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
996 | | |
997 | 71.5k | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName18.6k ) { |
998 | | // A previous decl with a different name is because of |
999 | | // @compatibility_alias, for example: |
1000 | | // \code |
1001 | | // @class NewImage; |
1002 | | // @compatibility_alias OldImage NewImage; |
1003 | | // \endcode |
1004 | | // A lookup for 'OldImage' will return the 'NewImage' decl. |
1005 | | // |
1006 | | // In such a case use the real declaration name, instead of the alias one, |
1007 | | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
1008 | | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
1009 | | // has been aliased. |
1010 | 3 | ClassName = PrevIDecl->getIdentifier(); |
1011 | 3 | } |
1012 | | |
1013 | | // If there was a forward declaration with type parameters, check |
1014 | | // for consistency. |
1015 | 71.5k | if (PrevIDecl) { |
1016 | 18.6k | if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
1017 | 885 | if (typeParamList) { |
1018 | | // Both have type parameter lists; check for consistency. |
1019 | 884 | if (checkTypeParamListConsistency(*this, prevTypeParamList, |
1020 | 884 | typeParamList, |
1021 | 884 | TypeParamListContext::Definition)) { |
1022 | 0 | typeParamList = nullptr; |
1023 | 0 | } |
1024 | 884 | } else { |
1025 | 1 | Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
1026 | 1 | << ClassName; |
1027 | 1 | Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
1028 | 1 | << ClassName; |
1029 | | |
1030 | | // Clone the type parameter list. |
1031 | 1 | SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
1032 | 1 | for (auto typeParam : *prevTypeParamList) { |
1033 | 1 | clonedTypeParams.push_back( |
1034 | 1 | ObjCTypeParamDecl::Create( |
1035 | 1 | Context, |
1036 | 1 | CurContext, |
1037 | 1 | typeParam->getVariance(), |
1038 | 1 | SourceLocation(), |
1039 | 1 | typeParam->getIndex(), |
1040 | 1 | SourceLocation(), |
1041 | 1 | typeParam->getIdentifier(), |
1042 | 1 | SourceLocation(), |
1043 | 1 | Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
1044 | 1 | } |
1045 | | |
1046 | 1 | typeParamList = ObjCTypeParamList::create(Context, |
1047 | 1 | SourceLocation(), |
1048 | 1 | clonedTypeParams, |
1049 | 1 | SourceLocation()); |
1050 | 1 | } |
1051 | 885 | } |
1052 | 18.6k | } |
1053 | | |
1054 | 71.5k | ObjCInterfaceDecl *IDecl |
1055 | 71.5k | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
1056 | 71.5k | typeParamList, PrevIDecl, ClassLoc); |
1057 | 71.5k | if (PrevIDecl) { |
1058 | | // Class already seen. Was it a definition? |
1059 | 18.6k | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
1060 | 10 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
1061 | 10 | << PrevIDecl->getDeclName(); |
1062 | 10 | Diag(Def->getLocation(), diag::note_previous_definition); |
1063 | 10 | IDecl->setInvalidDecl(); |
1064 | 10 | } |
1065 | 18.6k | } |
1066 | | |
1067 | 71.5k | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
1068 | 71.5k | AddPragmaAttributes(TUScope, IDecl); |
1069 | | |
1070 | | // Merge attributes from previous declarations. |
1071 | 71.5k | if (PrevIDecl) |
1072 | 18.6k | mergeDeclAttributes(IDecl, PrevIDecl); |
1073 | | |
1074 | 71.5k | PushOnScopeChains(IDecl, TUScope); |
1075 | | |
1076 | | // Start the definition of this class. If we're in a redefinition case, there |
1077 | | // may already be a definition, so we'll end up adding to it. |
1078 | 71.5k | if (!IDecl->hasDefinition()) |
1079 | 71.5k | IDecl->startDefinition(); |
1080 | | |
1081 | 71.5k | if (SuperName) { |
1082 | | // Diagnose availability in the context of the @interface. |
1083 | 63.3k | ContextRAII SavedContext(*this, IDecl); |
1084 | | |
1085 | 63.3k | ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
1086 | 63.3k | ClassName, ClassLoc, |
1087 | 63.3k | SuperName, SuperLoc, SuperTypeArgs, |
1088 | 63.3k | SuperTypeArgsRange); |
1089 | 63.3k | } else { // we have a root class. |
1090 | 8.24k | IDecl->setEndOfDefinitionLoc(ClassLoc); |
1091 | 8.24k | } |
1092 | | |
1093 | | // Check then save referenced protocols. |
1094 | 71.5k | if (NumProtoRefs) { |
1095 | 25.5k | diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
1096 | 25.5k | NumProtoRefs, ProtoLocs); |
1097 | 25.5k | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
1098 | 25.5k | ProtoLocs, Context); |
1099 | 25.5k | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
1100 | 25.5k | } |
1101 | | |
1102 | 71.5k | CheckObjCDeclScope(IDecl); |
1103 | 71.5k | ActOnObjCContainerStartDefinition(IDecl); |
1104 | 71.5k | return IDecl; |
1105 | 71.5k | } |
1106 | | |
1107 | | /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
1108 | | /// typedef'ed use for a qualified super class and adds them to the list |
1109 | | /// of the protocols. |
1110 | | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
1111 | | SmallVectorImpl<SourceLocation> &ProtocolLocs, |
1112 | | IdentifierInfo *SuperName, |
1113 | 71.5k | SourceLocation SuperLoc) { |
1114 | 71.5k | if (!SuperName) |
1115 | 8.24k | return; |
1116 | 63.3k | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
1117 | 63.3k | LookupOrdinaryName); |
1118 | 63.3k | if (!IDecl) |
1119 | 5 | return; |
1120 | | |
1121 | 63.3k | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
1122 | 136 | QualType T = TDecl->getUnderlyingType(); |
1123 | 136 | if (T->isObjCObjectType()) |
1124 | 135 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) { |
1125 | 135 | ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
1126 | | // FIXME: Consider whether this should be an invalid loc since the loc |
1127 | | // is not actually pointing to a protocol name reference but to the |
1128 | | // typedef reference. Note that the base class name loc is also pointing |
1129 | | // at the typedef. |
1130 | 135 | ProtocolLocs.append(OPT->getNumProtocols(), SuperLoc); |
1131 | 135 | } |
1132 | 136 | } |
1133 | 63.3k | } |
1134 | | |
1135 | | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
1136 | | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
1137 | | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
1138 | | IdentifierInfo *AliasName, |
1139 | | SourceLocation AliasLocation, |
1140 | | IdentifierInfo *ClassName, |
1141 | 247 | SourceLocation ClassLocation) { |
1142 | | // Look for previous declaration of alias name |
1143 | 247 | NamedDecl *ADecl = |
1144 | 247 | LookupSingleName(TUScope, AliasName, AliasLocation, LookupOrdinaryName, |
1145 | 247 | forRedeclarationInCurContext()); |
1146 | 247 | if (ADecl) { |
1147 | 2 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
1148 | 2 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
1149 | 2 | return nullptr; |
1150 | 2 | } |
1151 | | // Check for class declaration |
1152 | 245 | NamedDecl *CDeclU = |
1153 | 245 | LookupSingleName(TUScope, ClassName, ClassLocation, LookupOrdinaryName, |
1154 | 245 | forRedeclarationInCurContext()); |
1155 | 245 | if (const TypedefNameDecl *TDecl = |
1156 | 245 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
1157 | 30 | QualType T = TDecl->getUnderlyingType(); |
1158 | 30 | if (T->isObjCObjectType()) { |
1159 | 29 | if (NamedDecl *IDecl = T->castAs<ObjCObjectType>()->getInterface()) { |
1160 | 29 | ClassName = IDecl->getIdentifier(); |
1161 | 29 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
1162 | 29 | LookupOrdinaryName, |
1163 | 29 | forRedeclarationInCurContext()); |
1164 | 29 | } |
1165 | 29 | } |
1166 | 30 | } |
1167 | 245 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
1168 | 245 | if (!CDecl) { |
1169 | 2 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
1170 | 2 | if (CDeclU) |
1171 | 1 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
1172 | 2 | return nullptr; |
1173 | 2 | } |
1174 | | |
1175 | | // Everything checked out, instantiate a new alias declaration AST. |
1176 | 243 | ObjCCompatibleAliasDecl *AliasDecl = |
1177 | 243 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
1178 | | |
1179 | 243 | if (!CheckObjCDeclScope(AliasDecl)) |
1180 | 242 | PushOnScopeChains(AliasDecl, TUScope); |
1181 | | |
1182 | 243 | return AliasDecl; |
1183 | 245 | } |
1184 | | |
1185 | | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
1186 | | IdentifierInfo *PName, |
1187 | | SourceLocation &Ploc, SourceLocation PrevLoc, |
1188 | 9.65k | const ObjCList<ObjCProtocolDecl> &PList) { |
1189 | | |
1190 | 9.65k | bool res = false; |
1191 | 9.65k | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
1192 | 14.6k | E = PList.end(); I != E; ++I4.94k ) { |
1193 | 4.94k | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
1194 | 4.94k | Ploc)) { |
1195 | 4.94k | if (PDecl->getIdentifier() == PName) { |
1196 | 1 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
1197 | 1 | Diag(PrevLoc, diag::note_previous_definition); |
1198 | 1 | res = true; |
1199 | 1 | } |
1200 | | |
1201 | 4.94k | if (!PDecl->hasDefinition()) |
1202 | 2 | continue; |
1203 | | |
1204 | 4.94k | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
1205 | 4.94k | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
1206 | 2 | res = true; |
1207 | 4.94k | } |
1208 | 4.94k | } |
1209 | 9.65k | return res; |
1210 | 9.65k | } |
1211 | | |
1212 | | ObjCProtocolDecl *Sema::ActOnStartProtocolInterface( |
1213 | | SourceLocation AtProtoInterfaceLoc, IdentifierInfo *ProtocolName, |
1214 | | SourceLocation ProtocolLoc, Decl *const *ProtoRefs, unsigned NumProtoRefs, |
1215 | | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
1216 | 17.6k | const ParsedAttributesView &AttrList) { |
1217 | 17.6k | bool err = false; |
1218 | | // FIXME: Deal with AttrList. |
1219 | 17.6k | assert(ProtocolName && "Missing protocol identifier"); |
1220 | 0 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
1221 | 17.6k | forRedeclarationInCurContext()); |
1222 | 17.6k | ObjCProtocolDecl *PDecl = nullptr; |
1223 | 17.6k | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
1224 | | // If we already have a definition, complain. |
1225 | 13 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
1226 | 13 | Diag(Def->getLocation(), diag::note_previous_definition); |
1227 | | |
1228 | | // Create a new protocol that is completely distinct from previous |
1229 | | // declarations, and do not make this protocol available for name lookup. |
1230 | | // That way, we'll end up completely ignoring the duplicate. |
1231 | | // FIXME: Can we turn this into an error? |
1232 | 13 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
1233 | 13 | ProtocolLoc, AtProtoInterfaceLoc, |
1234 | 13 | /*PrevDecl=*/nullptr); |
1235 | | |
1236 | | // If we are using modules, add the decl to the context in order to |
1237 | | // serialize something meaningful. |
1238 | 13 | if (getLangOpts().Modules) |
1239 | 1 | PushOnScopeChains(PDecl, TUScope); |
1240 | 13 | PDecl->startDefinition(); |
1241 | 17.6k | } else { |
1242 | 17.6k | if (PrevDecl) { |
1243 | | // Check for circular dependencies among protocol declarations. This can |
1244 | | // only happen if this protocol was forward-declared. |
1245 | 4.71k | ObjCList<ObjCProtocolDecl> PList; |
1246 | 4.71k | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
1247 | 4.71k | err = CheckForwardProtocolDeclarationForCircularDependency( |
1248 | 4.71k | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
1249 | 4.71k | } |
1250 | | |
1251 | | // Create the new declaration. |
1252 | 17.6k | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
1253 | 17.6k | ProtocolLoc, AtProtoInterfaceLoc, |
1254 | 17.6k | /*PrevDecl=*/PrevDecl); |
1255 | | |
1256 | 17.6k | PushOnScopeChains(PDecl, TUScope); |
1257 | 17.6k | PDecl->startDefinition(); |
1258 | 17.6k | } |
1259 | | |
1260 | 17.6k | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
1261 | 17.6k | AddPragmaAttributes(TUScope, PDecl); |
1262 | | |
1263 | | // Merge attributes from previous declarations. |
1264 | 17.6k | if (PrevDecl) |
1265 | 4.72k | mergeDeclAttributes(PDecl, PrevDecl); |
1266 | | |
1267 | 17.6k | if (!err && NumProtoRefs17.6k ) { |
1268 | | /// Check then save referenced protocols. |
1269 | 13.0k | diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
1270 | 13.0k | NumProtoRefs, ProtoLocs); |
1271 | 13.0k | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
1272 | 13.0k | ProtoLocs, Context); |
1273 | 13.0k | } |
1274 | | |
1275 | 17.6k | CheckObjCDeclScope(PDecl); |
1276 | 17.6k | ActOnObjCContainerStartDefinition(PDecl); |
1277 | 17.6k | return PDecl; |
1278 | 17.6k | } |
1279 | | |
1280 | | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
1281 | 62.1k | ObjCProtocolDecl *&UndefinedProtocol) { |
1282 | 62.1k | if (!PDecl->hasDefinition() || |
1283 | 62.1k | !PDecl->getDefinition()->isUnconditionallyVisible()62.1k ) { |
1284 | 14 | UndefinedProtocol = PDecl; |
1285 | 14 | return true; |
1286 | 14 | } |
1287 | | |
1288 | 62.1k | for (auto *PI : PDecl->protocols()) |
1289 | 19.0k | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
1290 | 1 | UndefinedProtocol = PI; |
1291 | 1 | return true; |
1292 | 1 | } |
1293 | 62.1k | return false; |
1294 | 62.1k | } |
1295 | | |
1296 | | /// FindProtocolDeclaration - This routine looks up protocols and |
1297 | | /// issues an error if they are not declared. It returns list of |
1298 | | /// protocol declarations in its 'Protocols' argument. |
1299 | | void |
1300 | | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
1301 | | ArrayRef<IdentifierLocPair> ProtocolId, |
1302 | 15.4k | SmallVectorImpl<Decl *> &Protocols) { |
1303 | 15.9k | for (const IdentifierLocPair &Pair : ProtocolId) { |
1304 | 15.9k | ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second); |
1305 | 15.9k | if (!PDecl) { |
1306 | 2 | DeclFilterCCC<ObjCProtocolDecl> CCC{}; |
1307 | 2 | TypoCorrection Corrected = CorrectTypo( |
1308 | 2 | DeclarationNameInfo(Pair.first, Pair.second), LookupObjCProtocolName, |
1309 | 2 | TUScope, nullptr, CCC, CTK_ErrorRecovery); |
1310 | 2 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
1311 | 1 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
1312 | 1 | << Pair.first); |
1313 | 2 | } |
1314 | | |
1315 | 15.9k | if (!PDecl) { |
1316 | 1 | Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first; |
1317 | 1 | continue; |
1318 | 1 | } |
1319 | | // If this is a forward protocol declaration, get its definition. |
1320 | 15.9k | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()203 ) |
1321 | 180 | PDecl = PDecl->getDefinition(); |
1322 | | |
1323 | | // For an objc container, delay protocol reference checking until after we |
1324 | | // can set the objc decl as the availability context, otherwise check now. |
1325 | 15.9k | if (!ForObjCContainer) { |
1326 | 18 | (void)DiagnoseUseOfDecl(PDecl, Pair.second); |
1327 | 18 | } |
1328 | | |
1329 | | // If this is a forward declaration and we are supposed to warn in this |
1330 | | // case, do it. |
1331 | | // FIXME: Recover nicely in the hidden case. |
1332 | 15.9k | ObjCProtocolDecl *UndefinedProtocol; |
1333 | | |
1334 | 15.9k | if (WarnOnDeclarations && |
1335 | 15.9k | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)2.85k ) { |
1336 | 12 | Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first; |
1337 | 12 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
1338 | 12 | << UndefinedProtocol; |
1339 | 12 | } |
1340 | 15.9k | Protocols.push_back(PDecl); |
1341 | 15.9k | } |
1342 | 15.4k | } |
1343 | | |
1344 | | namespace { |
1345 | | // Callback to only accept typo corrections that are either |
1346 | | // Objective-C protocols or valid Objective-C type arguments. |
1347 | | class ObjCTypeArgOrProtocolValidatorCCC final |
1348 | | : public CorrectionCandidateCallback { |
1349 | | ASTContext &Context; |
1350 | | Sema::LookupNameKind LookupKind; |
1351 | | public: |
1352 | | ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
1353 | | Sema::LookupNameKind lookupKind) |
1354 | 8 | : Context(context), LookupKind(lookupKind) { } |
1355 | | |
1356 | 4 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
1357 | | // If we're allowed to find protocols and we have a protocol, accept it. |
1358 | 4 | if (LookupKind != Sema::LookupOrdinaryName) { |
1359 | 3 | if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
1360 | 2 | return true; |
1361 | 3 | } |
1362 | | |
1363 | | // If we're allowed to find type names and we have one, accept it. |
1364 | 2 | if (LookupKind != Sema::LookupObjCProtocolName) { |
1365 | | // If we have a type declaration, we might accept this result. |
1366 | 2 | if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
1367 | | // If we found a tag declaration outside of C++, skip it. This |
1368 | | // can happy because we look for any name when there is no |
1369 | | // bias to protocol or type names. |
1370 | 1 | if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus0 ) |
1371 | 0 | return false; |
1372 | | |
1373 | | // Make sure the type is something we would accept as a type |
1374 | | // argument. |
1375 | 1 | auto type = Context.getTypeDeclType(typeDecl); |
1376 | 1 | if (type->isObjCObjectPointerType() || |
1377 | 1 | type->isBlockPointerType()0 || |
1378 | 1 | type->isDependentType()0 || |
1379 | 1 | type->isObjCObjectType()0 ) |
1380 | 1 | return true; |
1381 | | |
1382 | 0 | return false; |
1383 | 1 | } |
1384 | | |
1385 | | // If we have an Objective-C class type, accept it; there will |
1386 | | // be another fix to add the '*'. |
1387 | 1 | if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
1388 | 1 | return true; |
1389 | | |
1390 | 0 | return false; |
1391 | 1 | } |
1392 | | |
1393 | 0 | return false; |
1394 | 2 | } |
1395 | | |
1396 | 8 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
1397 | 8 | return std::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(*this); |
1398 | 8 | } |
1399 | | }; |
1400 | | } // end anonymous namespace |
1401 | | |
1402 | | void Sema::DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId, |
1403 | | SourceLocation ProtocolLoc, |
1404 | | IdentifierInfo *TypeArgId, |
1405 | | SourceLocation TypeArgLoc, |
1406 | 4 | bool SelectProtocolFirst) { |
1407 | 4 | Diag(TypeArgLoc, diag::err_objc_type_args_and_protocols) |
1408 | 4 | << SelectProtocolFirst << TypeArgId << ProtocolId |
1409 | 4 | << SourceRange(ProtocolLoc); |
1410 | 4 | } |
1411 | | |
1412 | | void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
1413 | | Scope *S, |
1414 | | ParsedType baseType, |
1415 | | SourceLocation lAngleLoc, |
1416 | | ArrayRef<IdentifierInfo *> identifiers, |
1417 | | ArrayRef<SourceLocation> identifierLocs, |
1418 | | SourceLocation rAngleLoc, |
1419 | | SourceLocation &typeArgsLAngleLoc, |
1420 | | SmallVectorImpl<ParsedType> &typeArgs, |
1421 | | SourceLocation &typeArgsRAngleLoc, |
1422 | | SourceLocation &protocolLAngleLoc, |
1423 | | SmallVectorImpl<Decl *> &protocols, |
1424 | | SourceLocation &protocolRAngleLoc, |
1425 | 104k | bool warnOnIncompleteProtocols) { |
1426 | | // Local function that updates the declaration specifiers with |
1427 | | // protocol information. |
1428 | 104k | unsigned numProtocolsResolved = 0; |
1429 | 104k | auto resolvedAsProtocols = [&] { |
1430 | 46.7k | assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
1431 | | |
1432 | | // Determine whether the base type is a parameterized class, in |
1433 | | // which case we want to warn about typos such as |
1434 | | // "NSArray<NSObject>" (that should be NSArray<NSObject *>). |
1435 | 0 | ObjCInterfaceDecl *baseClass = nullptr; |
1436 | 46.7k | QualType base = GetTypeFromParser(baseType, nullptr); |
1437 | 46.7k | bool allAreTypeNames = false; |
1438 | 46.7k | SourceLocation firstClassNameLoc; |
1439 | 46.7k | if (!base.isNull()) { |
1440 | 22.4k | if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
1441 | 5.13k | baseClass = objcObjectType->getInterface(); |
1442 | 5.13k | if (baseClass) { |
1443 | 5.13k | if (auto typeParams = baseClass->getTypeParamList()) { |
1444 | 25 | if (typeParams->size() == numProtocolsResolved) { |
1445 | | // Note that we should be looking for type names, too. |
1446 | 12 | allAreTypeNames = true; |
1447 | 12 | } |
1448 | 25 | } |
1449 | 5.13k | } |
1450 | 5.13k | } |
1451 | 22.4k | } |
1452 | | |
1453 | 110k | for (unsigned i = 0, n = protocols.size(); i != n; ++i63.6k ) { |
1454 | 63.6k | ObjCProtocolDecl *&proto |
1455 | 63.6k | = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
1456 | | // For an objc container, delay protocol reference checking until after we |
1457 | | // can set the objc decl as the availability context, otherwise check now. |
1458 | 63.6k | if (!warnOnIncompleteProtocols) { |
1459 | 23.3k | (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
1460 | 23.3k | } |
1461 | | |
1462 | | // If this is a forward protocol declaration, get its definition. |
1463 | 63.6k | if (!proto->isThisDeclarationADefinition() && proto->getDefinition()7.14k ) |
1464 | 544 | proto = proto->getDefinition(); |
1465 | | |
1466 | | // If this is a forward declaration and we are supposed to warn in this |
1467 | | // case, do it. |
1468 | | // FIXME: Recover nicely in the hidden case. |
1469 | 63.6k | ObjCProtocolDecl *forwardDecl = nullptr; |
1470 | 63.6k | if (warnOnIncompleteProtocols && |
1471 | 63.6k | NestedProtocolHasNoDefinition(proto, forwardDecl)40.2k ) { |
1472 | 2 | Diag(identifierLocs[i], diag::warn_undef_protocolref) |
1473 | 2 | << proto->getDeclName(); |
1474 | 2 | Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
1475 | 2 | << forwardDecl; |
1476 | 2 | } |
1477 | | |
1478 | | // If everything this far has been a type name (and we care |
1479 | | // about such things), check whether this name refers to a type |
1480 | | // as well. |
1481 | 63.6k | if (allAreTypeNames) { |
1482 | 14 | if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
1483 | 14 | LookupOrdinaryName)) { |
1484 | 3 | if (isa<ObjCInterfaceDecl>(decl)) { |
1485 | 3 | if (firstClassNameLoc.isInvalid()) |
1486 | 2 | firstClassNameLoc = identifierLocs[i]; |
1487 | 3 | } else if (0 !isa<TypeDecl>(decl)0 ) { |
1488 | | // Not a type. |
1489 | 0 | allAreTypeNames = false; |
1490 | 0 | } |
1491 | 11 | } else { |
1492 | 11 | allAreTypeNames = false; |
1493 | 11 | } |
1494 | 14 | } |
1495 | 63.6k | } |
1496 | | |
1497 | | // All of the protocols listed also have type names, and at least |
1498 | | // one is an Objective-C class name. Check whether all of the |
1499 | | // protocol conformances are declared by the base class itself, in |
1500 | | // which case we warn. |
1501 | 46.7k | if (allAreTypeNames && firstClassNameLoc.isValid()1 ) { |
1502 | 1 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
1503 | 1 | Context.CollectInheritedProtocols(baseClass, knownProtocols); |
1504 | 1 | bool allProtocolsDeclared = true; |
1505 | 1 | for (auto proto : protocols) { |
1506 | 1 | if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
1507 | 0 | allProtocolsDeclared = false; |
1508 | 0 | break; |
1509 | 0 | } |
1510 | 1 | } |
1511 | | |
1512 | 1 | if (allProtocolsDeclared) { |
1513 | 1 | Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
1514 | 1 | << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
1515 | 1 | << FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc), |
1516 | 1 | " *"); |
1517 | 1 | } |
1518 | 1 | } |
1519 | | |
1520 | 46.7k | protocolLAngleLoc = lAngleLoc; |
1521 | 46.7k | protocolRAngleLoc = rAngleLoc; |
1522 | 46.7k | assert(protocols.size() == identifierLocs.size()); |
1523 | 46.7k | }; |
1524 | | |
1525 | | // Attempt to resolve all of the identifiers as protocols. |
1526 | 237k | for (unsigned i = 0, n = identifiers.size(); i != n; ++i132k ) { |
1527 | 132k | ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
1528 | 132k | protocols.push_back(proto); |
1529 | 132k | if (proto) |
1530 | 63.6k | ++numProtocolsResolved; |
1531 | 132k | } |
1532 | | |
1533 | | // If all of the names were protocols, these were protocol qualifiers. |
1534 | 104k | if (numProtocolsResolved == identifiers.size()) |
1535 | 46.7k | return resolvedAsProtocols(); |
1536 | | |
1537 | | // Attempt to resolve all of the identifiers as type names or |
1538 | | // Objective-C class names. The latter is technically ill-formed, |
1539 | | // but is probably something like \c NSArray<NSView *> missing the |
1540 | | // \c*. |
1541 | 57.5k | typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
1542 | 57.5k | SmallVector<TypeOrClassDecl, 4> typeDecls; |
1543 | 57.5k | unsigned numTypeDeclsResolved = 0; |
1544 | 126k | for (unsigned i = 0, n = identifiers.size(); i != n; ++i69.3k ) { |
1545 | 69.3k | NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
1546 | 69.3k | LookupOrdinaryName); |
1547 | 69.3k | if (!decl) { |
1548 | 16 | typeDecls.push_back(TypeOrClassDecl()); |
1549 | 16 | continue; |
1550 | 16 | } |
1551 | | |
1552 | 69.3k | if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
1553 | 69.3k | typeDecls.push_back(typeDecl); |
1554 | 69.3k | ++numTypeDeclsResolved; |
1555 | 69.3k | continue; |
1556 | 69.3k | } |
1557 | | |
1558 | 6 | if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
1559 | 6 | typeDecls.push_back(objcClass); |
1560 | 6 | ++numTypeDeclsResolved; |
1561 | 6 | continue; |
1562 | 6 | } |
1563 | | |
1564 | 0 | typeDecls.push_back(TypeOrClassDecl()); |
1565 | 0 | } |
1566 | | |
1567 | 57.5k | AttributeFactory attrFactory; |
1568 | | |
1569 | | // Local function that forms a reference to the given type or |
1570 | | // Objective-C class declaration. |
1571 | 57.5k | auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
1572 | 69.3k | -> TypeResult { |
1573 | | // Form declaration specifiers. They simply refer to the type. |
1574 | 69.3k | DeclSpec DS(attrFactory); |
1575 | 69.3k | const char* prevSpec; // unused |
1576 | 69.3k | unsigned diagID; // unused |
1577 | 69.3k | QualType type; |
1578 | 69.3k | if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
1579 | 69.3k | type = Context.getTypeDeclType(actualTypeDecl); |
1580 | 5 | else |
1581 | 5 | type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
1582 | 69.3k | TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
1583 | 69.3k | ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
1584 | 69.3k | DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
1585 | 69.3k | parsedType, Context.getPrintingPolicy()); |
1586 | | // Use the identifier location for the type source range. |
1587 | 69.3k | DS.SetRangeStart(loc); |
1588 | 69.3k | DS.SetRangeEnd(loc); |
1589 | | |
1590 | | // Form the declarator. |
1591 | 69.3k | Declarator D(DS, ParsedAttributesView::none(), DeclaratorContext::TypeName); |
1592 | | |
1593 | | // If we have a typedef of an Objective-C class type that is missing a '*', |
1594 | | // add the '*'. |
1595 | 69.3k | if (type->getAs<ObjCInterfaceType>()) { |
1596 | 5 | SourceLocation starLoc = getLocForEndOfToken(loc); |
1597 | 5 | D.AddTypeInfo(DeclaratorChunk::getPointer(/*TypeQuals=*/0, starLoc, |
1598 | 5 | SourceLocation(), |
1599 | 5 | SourceLocation(), |
1600 | 5 | SourceLocation(), |
1601 | 5 | SourceLocation(), |
1602 | 5 | SourceLocation()), |
1603 | 5 | starLoc); |
1604 | | |
1605 | | // Diagnose the missing '*'. |
1606 | 5 | Diag(loc, diag::err_objc_type_arg_missing_star) |
1607 | 5 | << type |
1608 | 5 | << FixItHint::CreateInsertion(starLoc, " *"); |
1609 | 5 | } |
1610 | | |
1611 | | // Convert this to a type. |
1612 | 69.3k | return ActOnTypeName(S, D); |
1613 | 69.3k | }; |
1614 | | |
1615 | | // Local function that updates the declaration specifiers with |
1616 | | // type argument information. |
1617 | 57.5k | auto resolvedAsTypeDecls = [&] { |
1618 | | // We did not resolve these as protocols. |
1619 | 57.5k | protocols.clear(); |
1620 | | |
1621 | 57.5k | assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
1622 | | // Map type declarations to type arguments. |
1623 | 126k | for (unsigned i = 0, n = identifiers.size(); i != n; ++i69.3k ) { |
1624 | | // Map type reference to a type. |
1625 | 69.3k | TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
1626 | 69.3k | if (!type.isUsable()) { |
1627 | 0 | typeArgs.clear(); |
1628 | 0 | return; |
1629 | 0 | } |
1630 | | |
1631 | 69.3k | typeArgs.push_back(type.get()); |
1632 | 69.3k | } |
1633 | | |
1634 | 57.5k | typeArgsLAngleLoc = lAngleLoc; |
1635 | 57.5k | typeArgsRAngleLoc = rAngleLoc; |
1636 | 57.5k | }; |
1637 | | |
1638 | | // If all of the identifiers can be resolved as type names or |
1639 | | // Objective-C class names, we have type arguments. |
1640 | 57.5k | if (numTypeDeclsResolved == identifiers.size()) |
1641 | 57.5k | return resolvedAsTypeDecls(); |
1642 | | |
1643 | | // Error recovery: some names weren't found, or we have a mix of |
1644 | | // type and protocol names. Go resolve all of the unresolved names |
1645 | | // and complain if we can't find a consistent answer. |
1646 | 9 | LookupNameKind lookupKind = LookupAnyName; |
1647 | 24 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i15 ) { |
1648 | | // If we already have a protocol or type. Check whether it is the |
1649 | | // right thing. |
1650 | 20 | if (protocols[i] || typeDecls[i]12 ) { |
1651 | | // If we haven't figured out whether we want types or protocols |
1652 | | // yet, try to figure it out from this name. |
1653 | 12 | if (lookupKind == LookupAnyName) { |
1654 | | // If this name refers to both a protocol and a type (e.g., \c |
1655 | | // NSObject), don't conclude anything yet. |
1656 | 10 | if (protocols[i] && typeDecls[i]8 ) |
1657 | 4 | continue; |
1658 | | |
1659 | | // Otherwise, let this name decide whether we'll be correcting |
1660 | | // toward types or protocols. |
1661 | 6 | lookupKind = protocols[i] ? LookupObjCProtocolName4 |
1662 | 6 | : LookupOrdinaryName2 ; |
1663 | 6 | continue; |
1664 | 10 | } |
1665 | | |
1666 | | // If we want protocols and we have a protocol, there's nothing |
1667 | | // more to do. |
1668 | 2 | if (lookupKind == LookupObjCProtocolName && protocols[i]1 ) |
1669 | 0 | continue; |
1670 | | |
1671 | | // If we want types and we have a type declaration, there's |
1672 | | // nothing more to do. |
1673 | 2 | if (lookupKind == LookupOrdinaryName && typeDecls[i]1 ) |
1674 | 1 | continue; |
1675 | | |
1676 | | // We have a conflict: some names refer to protocols and others |
1677 | | // refer to types. |
1678 | 1 | DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0], |
1679 | 1 | identifiers[i], identifierLocs[i], |
1680 | 1 | protocols[i] != nullptr); |
1681 | | |
1682 | 1 | protocols.clear(); |
1683 | 1 | typeArgs.clear(); |
1684 | 1 | return; |
1685 | 2 | } |
1686 | | |
1687 | | // Perform typo correction on the name. |
1688 | 8 | ObjCTypeArgOrProtocolValidatorCCC CCC(Context, lookupKind); |
1689 | 8 | TypoCorrection corrected = |
1690 | 8 | CorrectTypo(DeclarationNameInfo(identifiers[i], identifierLocs[i]), |
1691 | 8 | lookupKind, S, nullptr, CCC, CTK_ErrorRecovery); |
1692 | 8 | if (corrected) { |
1693 | | // Did we find a protocol? |
1694 | 4 | if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
1695 | 2 | diagnoseTypo(corrected, |
1696 | 2 | PDiag(diag::err_undeclared_protocol_suggest) |
1697 | 2 | << identifiers[i]); |
1698 | 2 | lookupKind = LookupObjCProtocolName; |
1699 | 2 | protocols[i] = proto; |
1700 | 2 | ++numProtocolsResolved; |
1701 | 2 | continue; |
1702 | 2 | } |
1703 | | |
1704 | | // Did we find a type? |
1705 | 2 | if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
1706 | 1 | diagnoseTypo(corrected, |
1707 | 1 | PDiag(diag::err_unknown_typename_suggest) |
1708 | 1 | << identifiers[i]); |
1709 | 1 | lookupKind = LookupOrdinaryName; |
1710 | 1 | typeDecls[i] = typeDecl; |
1711 | 1 | ++numTypeDeclsResolved; |
1712 | 1 | continue; |
1713 | 1 | } |
1714 | | |
1715 | | // Did we find an Objective-C class? |
1716 | 1 | if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
1717 | 1 | diagnoseTypo(corrected, |
1718 | 1 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
1719 | 1 | << identifiers[i] << true); |
1720 | 1 | lookupKind = LookupOrdinaryName; |
1721 | 1 | typeDecls[i] = objcClass; |
1722 | 1 | ++numTypeDeclsResolved; |
1723 | 1 | continue; |
1724 | 1 | } |
1725 | 1 | } |
1726 | | |
1727 | | // We couldn't find anything. |
1728 | 4 | Diag(identifierLocs[i], |
1729 | 4 | (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing1 |
1730 | 4 | : lookupKind == LookupObjCProtocolName3 ? diag::err_undeclared_protocol2 |
1731 | 3 | : diag::err_unknown_typename1 )) |
1732 | 4 | << identifiers[i]; |
1733 | 4 | protocols.clear(); |
1734 | 4 | typeArgs.clear(); |
1735 | 4 | return; |
1736 | 8 | } |
1737 | | |
1738 | | // If all of the names were (corrected to) protocols, these were |
1739 | | // protocol qualifiers. |
1740 | 4 | if (numProtocolsResolved == identifiers.size()) |
1741 | 2 | return resolvedAsProtocols(); |
1742 | | |
1743 | | // Otherwise, all of the names were (corrected to) types. |
1744 | 2 | assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
1745 | 0 | return resolvedAsTypeDecls(); |
1746 | 4 | } |
1747 | | |
1748 | | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
1749 | | /// a class method in its extension. |
1750 | | /// |
1751 | | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
1752 | 896 | ObjCInterfaceDecl *ID) { |
1753 | 896 | if (!ID) |
1754 | 8 | return; // Possibly due to previous error |
1755 | | |
1756 | 888 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
1757 | 888 | for (auto *MD : ID->methods()) |
1758 | 4.71k | MethodMap[MD->getSelector()] = MD; |
1759 | | |
1760 | 888 | if (MethodMap.empty()) |
1761 | 246 | return; |
1762 | 2.60k | for (const auto *Method : CAT->methods())642 { |
1763 | 2.60k | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
1764 | 2.60k | if (PrevMethod && |
1765 | 2.60k | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod())31 && |
1766 | 2.60k | !MatchTwoMethodDeclarations(Method, PrevMethod)29 ) { |
1767 | 3 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
1768 | 3 | << Method->getDeclName(); |
1769 | 3 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
1770 | 3 | } |
1771 | 2.60k | } |
1772 | 642 | } |
1773 | | |
1774 | | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
1775 | | Sema::DeclGroupPtrTy |
1776 | | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
1777 | | ArrayRef<IdentifierLocPair> IdentList, |
1778 | 4.68k | const ParsedAttributesView &attrList) { |
1779 | 4.68k | SmallVector<Decl *, 8> DeclsInGroup; |
1780 | 5.40k | for (const IdentifierLocPair &IdentPair : IdentList) { |
1781 | 5.40k | IdentifierInfo *Ident = IdentPair.first; |
1782 | 5.40k | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second, |
1783 | 5.40k | forRedeclarationInCurContext()); |
1784 | 5.40k | ObjCProtocolDecl *PDecl |
1785 | 5.40k | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
1786 | 5.40k | IdentPair.second, AtProtocolLoc, |
1787 | 5.40k | PrevDecl); |
1788 | | |
1789 | 5.40k | PushOnScopeChains(PDecl, TUScope); |
1790 | 5.40k | CheckObjCDeclScope(PDecl); |
1791 | | |
1792 | 5.40k | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
1793 | 5.40k | AddPragmaAttributes(TUScope, PDecl); |
1794 | | |
1795 | 5.40k | if (PrevDecl) |
1796 | 295 | mergeDeclAttributes(PDecl, PrevDecl); |
1797 | | |
1798 | 5.40k | DeclsInGroup.push_back(PDecl); |
1799 | 5.40k | } |
1800 | | |
1801 | 4.68k | return BuildDeclaratorGroup(DeclsInGroup); |
1802 | 4.68k | } |
1803 | | |
1804 | | ObjCCategoryDecl *Sema::ActOnStartCategoryInterface( |
1805 | | SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
1806 | | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
1807 | | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
1808 | | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
1809 | | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
1810 | 41.0k | const ParsedAttributesView &AttrList) { |
1811 | 41.0k | ObjCCategoryDecl *CDecl; |
1812 | 41.0k | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
1813 | | |
1814 | | /// Check that class of this category is already completely declared. |
1815 | | |
1816 | 41.0k | if (!IDecl |
1817 | 41.0k | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
1818 | 41.0k | diag::err_category_forward_interface, |
1819 | 41.0k | CategoryName == nullptr)) { |
1820 | | // Create an invalid ObjCCategoryDecl to serve as context for |
1821 | | // the enclosing method declarations. We mark the decl invalid |
1822 | | // to make it clear that this isn't a valid AST. |
1823 | 22 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
1824 | 22 | ClassLoc, CategoryLoc, CategoryName, |
1825 | 22 | IDecl, typeParamList); |
1826 | 22 | CDecl->setInvalidDecl(); |
1827 | 22 | CurContext->addDecl(CDecl); |
1828 | | |
1829 | 22 | if (!IDecl) |
1830 | 21 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
1831 | 22 | ActOnObjCContainerStartDefinition(CDecl); |
1832 | 22 | return CDecl; |
1833 | 22 | } |
1834 | | |
1835 | 41.0k | if (!CategoryName && IDecl->getImplementation()888 ) { |
1836 | 1 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
1837 | 1 | Diag(IDecl->getImplementation()->getLocation(), |
1838 | 1 | diag::note_implementation_declared); |
1839 | 1 | } |
1840 | | |
1841 | 41.0k | if (CategoryName) { |
1842 | | /// Check for duplicate interface declaration for this category |
1843 | 40.1k | if (ObjCCategoryDecl *Previous |
1844 | 40.1k | = IDecl->FindCategoryDeclaration(CategoryName)) { |
1845 | | // Class extensions can be declared multiple times, categories cannot. |
1846 | 25 | Diag(CategoryLoc, diag::warn_dup_category_def) |
1847 | 25 | << ClassName << CategoryName; |
1848 | 25 | Diag(Previous->getLocation(), diag::note_previous_definition); |
1849 | 25 | } |
1850 | 40.1k | } |
1851 | | |
1852 | | // If we have a type parameter list, check it. |
1853 | 41.0k | if (typeParamList) { |
1854 | 10.3k | if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
1855 | 10.3k | if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
1856 | 10.3k | CategoryName |
1857 | 10.3k | ? TypeParamListContext::Category10.3k |
1858 | 10.3k | : TypeParamListContext::Extension9 )) |
1859 | 2 | typeParamList = nullptr; |
1860 | 10.3k | } else { |
1861 | 2 | Diag(typeParamList->getLAngleLoc(), |
1862 | 2 | diag::err_objc_parameterized_category_nonclass) |
1863 | 2 | << (CategoryName != nullptr) |
1864 | 2 | << ClassName |
1865 | 2 | << typeParamList->getSourceRange(); |
1866 | | |
1867 | 2 | typeParamList = nullptr; |
1868 | 2 | } |
1869 | 10.3k | } |
1870 | | |
1871 | 41.0k | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
1872 | 41.0k | ClassLoc, CategoryLoc, CategoryName, IDecl, |
1873 | 41.0k | typeParamList); |
1874 | | // FIXME: PushOnScopeChains? |
1875 | 41.0k | CurContext->addDecl(CDecl); |
1876 | | |
1877 | | // Process the attributes before looking at protocols to ensure that the |
1878 | | // availability attribute is attached to the category to provide availability |
1879 | | // checking for protocol uses. |
1880 | 41.0k | ProcessDeclAttributeList(TUScope, CDecl, AttrList); |
1881 | 41.0k | AddPragmaAttributes(TUScope, CDecl); |
1882 | | |
1883 | 41.0k | if (NumProtoRefs) { |
1884 | 1.03k | diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
1885 | 1.03k | NumProtoRefs, ProtoLocs); |
1886 | 1.03k | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
1887 | 1.03k | ProtoLocs, Context); |
1888 | | // Protocols in the class extension belong to the class. |
1889 | 1.03k | if (CDecl->IsClassExtension()) |
1890 | 123 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
1891 | 123 | NumProtoRefs, Context); |
1892 | 1.03k | } |
1893 | | |
1894 | 41.0k | CheckObjCDeclScope(CDecl); |
1895 | 41.0k | ActOnObjCContainerStartDefinition(CDecl); |
1896 | 41.0k | return CDecl; |
1897 | 41.0k | } |
1898 | | |
1899 | | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
1900 | | /// category implementation declaration and build an ObjCCategoryImplDecl |
1901 | | /// object. |
1902 | | ObjCCategoryImplDecl *Sema::ActOnStartCategoryImplementation( |
1903 | | SourceLocation AtCatImplLoc, IdentifierInfo *ClassName, |
1904 | | SourceLocation ClassLoc, IdentifierInfo *CatName, SourceLocation CatLoc, |
1905 | 521 | const ParsedAttributesView &Attrs) { |
1906 | 521 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
1907 | 521 | ObjCCategoryDecl *CatIDecl = nullptr; |
1908 | 521 | if (IDecl && IDecl->hasDefinition()508 ) { |
1909 | 506 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
1910 | 506 | if (!CatIDecl) { |
1911 | | // Category @implementation with no corresponding @interface. |
1912 | | // Create and install one. |
1913 | 111 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
1914 | 111 | ClassLoc, CatLoc, |
1915 | 111 | CatName, IDecl, |
1916 | 111 | /*typeParamList=*/nullptr); |
1917 | 111 | CatIDecl->setImplicit(); |
1918 | 111 | } |
1919 | 506 | } |
1920 | | |
1921 | 521 | ObjCCategoryImplDecl *CDecl = |
1922 | 521 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
1923 | 521 | ClassLoc, AtCatImplLoc, CatLoc); |
1924 | | /// Check that class of this category is already completely declared. |
1925 | 521 | if (!IDecl) { |
1926 | 13 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
1927 | 13 | CDecl->setInvalidDecl(); |
1928 | 508 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
1929 | 508 | diag::err_undef_interface)) { |
1930 | 2 | CDecl->setInvalidDecl(); |
1931 | 2 | } |
1932 | | |
1933 | 521 | ProcessDeclAttributeList(TUScope, CDecl, Attrs); |
1934 | 521 | AddPragmaAttributes(TUScope, CDecl); |
1935 | | |
1936 | | // FIXME: PushOnScopeChains? |
1937 | 521 | CurContext->addDecl(CDecl); |
1938 | | |
1939 | | // If the interface has the objc_runtime_visible attribute, we |
1940 | | // cannot implement a category for it. |
1941 | 521 | if (IDecl && IDecl->hasAttr<ObjCRuntimeVisibleAttr>()508 ) { |
1942 | 1 | Diag(ClassLoc, diag::err_objc_runtime_visible_category) |
1943 | 1 | << IDecl->getDeclName(); |
1944 | 1 | } |
1945 | | |
1946 | | /// Check that CatName, category name, is not used in another implementation. |
1947 | 521 | if (CatIDecl) { |
1948 | 506 | if (CatIDecl->getImplementation()) { |
1949 | 1 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
1950 | 1 | << CatName; |
1951 | 1 | Diag(CatIDecl->getImplementation()->getLocation(), |
1952 | 1 | diag::note_previous_definition); |
1953 | 1 | CDecl->setInvalidDecl(); |
1954 | 505 | } else { |
1955 | 505 | CatIDecl->setImplementation(CDecl); |
1956 | | // Warn on implementating category of deprecated class under |
1957 | | // -Wdeprecated-implementations flag. |
1958 | 505 | DiagnoseObjCImplementedDeprecations(*this, CatIDecl, |
1959 | 505 | CDecl->getLocation()); |
1960 | 505 | } |
1961 | 506 | } |
1962 | | |
1963 | 521 | CheckObjCDeclScope(CDecl); |
1964 | 521 | ActOnObjCContainerStartDefinition(CDecl); |
1965 | 521 | return CDecl; |
1966 | 521 | } |
1967 | | |
1968 | | ObjCImplementationDecl *Sema::ActOnStartClassImplementation( |
1969 | | SourceLocation AtClassImplLoc, IdentifierInfo *ClassName, |
1970 | | SourceLocation ClassLoc, IdentifierInfo *SuperClassname, |
1971 | 4.85k | SourceLocation SuperClassLoc, const ParsedAttributesView &Attrs) { |
1972 | 4.85k | ObjCInterfaceDecl *IDecl = nullptr; |
1973 | | // Check for another declaration kind with the same name. |
1974 | 4.85k | NamedDecl *PrevDecl |
1975 | 4.85k | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
1976 | 4.85k | forRedeclarationInCurContext()); |
1977 | 4.85k | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)4.78k ) { |
1978 | 1 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
1979 | 1 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
1980 | 4.85k | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
1981 | | // FIXME: This will produce an error if the definition of the interface has |
1982 | | // been imported from a module but is not visible. |
1983 | 4.78k | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
1984 | 4.78k | diag::warn_undef_interface); |
1985 | 4.78k | } else { |
1986 | | // We did not find anything with the name ClassName; try to correct for |
1987 | | // typos in the class name. |
1988 | 62 | ObjCInterfaceValidatorCCC CCC{}; |
1989 | 62 | TypoCorrection Corrected = |
1990 | 62 | CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc), |
1991 | 62 | LookupOrdinaryName, TUScope, nullptr, CCC, CTK_NonError); |
1992 | 62 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
1993 | | // Suggest the (potentially) correct interface name. Don't provide a |
1994 | | // code-modification hint or use the typo name for recovery, because |
1995 | | // this is just a warning. The program may actually be correct. |
1996 | 5 | diagnoseTypo(Corrected, |
1997 | 5 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
1998 | 5 | /*ErrorRecovery*/false); |
1999 | 57 | } else { |
2000 | 57 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
2001 | 57 | } |
2002 | 62 | } |
2003 | | |
2004 | | // Check that super class name is valid class name |
2005 | 4.85k | ObjCInterfaceDecl *SDecl = nullptr; |
2006 | 4.85k | if (SuperClassname) { |
2007 | | // Check if a different kind of symbol declared in this scope. |
2008 | 51 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
2009 | 51 | LookupOrdinaryName); |
2010 | 51 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)50 ) { |
2011 | 1 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
2012 | 1 | << SuperClassname; |
2013 | 1 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
2014 | 50 | } else { |
2015 | 50 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
2016 | 50 | if (SDecl && !SDecl->hasDefinition()49 ) |
2017 | 1 | SDecl = nullptr; |
2018 | 50 | if (!SDecl) |
2019 | 2 | Diag(SuperClassLoc, diag::err_undef_superclass) |
2020 | 2 | << SuperClassname << ClassName; |
2021 | 48 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)36 ) { |
2022 | | // This implementation and its interface do not have the same |
2023 | | // super class. |
2024 | 2 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
2025 | 2 | << SDecl->getDeclName(); |
2026 | 2 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
2027 | 2 | } |
2028 | 50 | } |
2029 | 51 | } |
2030 | | |
2031 | 4.85k | if (!IDecl) { |
2032 | | // Legacy case of @implementation with no corresponding @interface. |
2033 | | // Build, chain & install the interface decl into the identifier. |
2034 | | |
2035 | | // FIXME: Do we support attributes on the @implementation? If so we should |
2036 | | // copy them over. |
2037 | 63 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
2038 | 63 | ClassName, /*typeParamList=*/nullptr, |
2039 | 63 | /*PrevDecl=*/nullptr, ClassLoc, |
2040 | 63 | true); |
2041 | 63 | AddPragmaAttributes(TUScope, IDecl); |
2042 | 63 | IDecl->startDefinition(); |
2043 | 63 | if (SDecl) { |
2044 | 12 | IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
2045 | 12 | Context.getObjCInterfaceType(SDecl), |
2046 | 12 | SuperClassLoc)); |
2047 | 12 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
2048 | 51 | } else { |
2049 | 51 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
2050 | 51 | } |
2051 | | |
2052 | 63 | PushOnScopeChains(IDecl, TUScope); |
2053 | 4.78k | } else { |
2054 | | // Mark the interface as being completed, even if it was just as |
2055 | | // @class ....; |
2056 | | // declaration; the user cannot reopen it. |
2057 | 4.78k | if (!IDecl->hasDefinition()) |
2058 | 4 | IDecl->startDefinition(); |
2059 | 4.78k | } |
2060 | | |
2061 | 4.85k | ObjCImplementationDecl* IMPDecl = |
2062 | 4.85k | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
2063 | 4.85k | ClassLoc, AtClassImplLoc, SuperClassLoc); |
2064 | | |
2065 | 4.85k | ProcessDeclAttributeList(TUScope, IMPDecl, Attrs); |
2066 | 4.85k | AddPragmaAttributes(TUScope, IMPDecl); |
2067 | | |
2068 | 4.85k | if (CheckObjCDeclScope(IMPDecl)) { |
2069 | 2 | ActOnObjCContainerStartDefinition(IMPDecl); |
2070 | 2 | return IMPDecl; |
2071 | 2 | } |
2072 | | |
2073 | | // Check that there is no duplicate implementation of this class. |
2074 | 4.84k | if (IDecl->getImplementation()) { |
2075 | | // FIXME: Don't leak everything! |
2076 | 3 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
2077 | 3 | Diag(IDecl->getImplementation()->getLocation(), |
2078 | 3 | diag::note_previous_definition); |
2079 | 3 | IMPDecl->setInvalidDecl(); |
2080 | 4.84k | } else { // add it to the list. |
2081 | 4.84k | IDecl->setImplementation(IMPDecl); |
2082 | 4.84k | PushOnScopeChains(IMPDecl, TUScope); |
2083 | | // Warn on implementating deprecated class under |
2084 | | // -Wdeprecated-implementations flag. |
2085 | 4.84k | DiagnoseObjCImplementedDeprecations(*this, IDecl, IMPDecl->getLocation()); |
2086 | 4.84k | } |
2087 | | |
2088 | | // If the superclass has the objc_runtime_visible attribute, we |
2089 | | // cannot implement a subclass of it. |
2090 | 4.84k | if (IDecl->getSuperClass() && |
2091 | 4.84k | IDecl->getSuperClass()->hasAttr<ObjCRuntimeVisibleAttr>()2.21k ) { |
2092 | 1 | Diag(ClassLoc, diag::err_objc_runtime_visible_subclass) |
2093 | 1 | << IDecl->getDeclName() |
2094 | 1 | << IDecl->getSuperClass()->getDeclName(); |
2095 | 1 | } |
2096 | | |
2097 | 4.84k | ActOnObjCContainerStartDefinition(IMPDecl); |
2098 | 4.84k | return IMPDecl; |
2099 | 4.85k | } |
2100 | | |
2101 | | Sema::DeclGroupPtrTy |
2102 | 5.37k | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
2103 | 5.37k | SmallVector<Decl *, 64> DeclsInGroup; |
2104 | 5.37k | DeclsInGroup.reserve(Decls.size() + 1); |
2105 | | |
2106 | 13.4k | for (unsigned i = 0, e = Decls.size(); i != e; ++i8.09k ) { |
2107 | 8.09k | Decl *Dcl = Decls[i]; |
2108 | 8.09k | if (!Dcl) |
2109 | 0 | continue; |
2110 | 8.09k | if (Dcl->getDeclContext()->isFileContext()) |
2111 | 191 | Dcl->setTopLevelDeclInObjCContainer(); |
2112 | 8.09k | DeclsInGroup.push_back(Dcl); |
2113 | 8.09k | } |
2114 | | |
2115 | 5.37k | DeclsInGroup.push_back(ObjCImpDecl); |
2116 | | |
2117 | 5.37k | return BuildDeclaratorGroup(DeclsInGroup); |
2118 | 5.37k | } |
2119 | | |
2120 | | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
2121 | | ObjCIvarDecl **ivars, unsigned numIvars, |
2122 | 235 | SourceLocation RBrace) { |
2123 | 235 | assert(ImpDecl && "missing implementation decl"); |
2124 | 0 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
2125 | 235 | if (!IDecl) |
2126 | 0 | return; |
2127 | | /// Check case of non-existing \@interface decl. |
2128 | | /// (legacy objective-c \@implementation decl without an \@interface decl). |
2129 | | /// Add implementations's ivar to the synthesize class's ivar list. |
2130 | 235 | if (IDecl->isImplicitInterfaceDecl()) { |
2131 | 10 | IDecl->setEndOfDefinitionLoc(RBrace); |
2132 | | // Add ivar's to class's DeclContext. |
2133 | 25 | for (unsigned i = 0, e = numIvars; i != e; ++i15 ) { |
2134 | 15 | ivars[i]->setLexicalDeclContext(ImpDecl); |
2135 | | // In a 'fragile' runtime the ivar was added to the implicit |
2136 | | // ObjCInterfaceDecl while in a 'non-fragile' runtime the ivar is |
2137 | | // only in the ObjCImplementationDecl. In the non-fragile case the ivar |
2138 | | // therefore also needs to be propagated to the ObjCInterfaceDecl. |
2139 | 15 | if (!LangOpts.ObjCRuntime.isFragile()) |
2140 | 10 | IDecl->makeDeclVisibleInContext(ivars[i]); |
2141 | 15 | ImpDecl->addDecl(ivars[i]); |
2142 | 15 | } |
2143 | | |
2144 | 10 | return; |
2145 | 10 | } |
2146 | | // If implementation has empty ivar list, just return. |
2147 | 225 | if (numIvars == 0) |
2148 | 10 | return; |
2149 | | |
2150 | 215 | assert(ivars && "missing @implementation ivars"); |
2151 | 215 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
2152 | 206 | if (ImpDecl->getSuperClass()) |
2153 | 7 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
2154 | 482 | for (unsigned i = 0; i < numIvars; i++276 ) { |
2155 | 276 | ObjCIvarDecl* ImplIvar = ivars[i]; |
2156 | 276 | if (const ObjCIvarDecl *ClsIvar = |
2157 | 276 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
2158 | 2 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
2159 | 2 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
2160 | 2 | continue; |
2161 | 2 | } |
2162 | | // Check class extensions (unnamed categories) for duplicate ivars. |
2163 | 274 | for (const auto *CDecl : IDecl->visible_extensions()) { |
2164 | 101 | if (const ObjCIvarDecl *ClsExtIvar = |
2165 | 101 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
2166 | 6 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
2167 | 6 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
2168 | 6 | continue; |
2169 | 6 | } |
2170 | 101 | } |
2171 | | // Instance ivar to Implementation's DeclContext. |
2172 | 274 | ImplIvar->setLexicalDeclContext(ImpDecl); |
2173 | 274 | IDecl->makeDeclVisibleInContext(ImplIvar); |
2174 | 274 | ImpDecl->addDecl(ImplIvar); |
2175 | 274 | } |
2176 | 206 | return; |
2177 | 206 | } |
2178 | | // Check interface's Ivar list against those in the implementation. |
2179 | | // names and types must match. |
2180 | | // |
2181 | 9 | unsigned j = 0; |
2182 | 9 | ObjCInterfaceDecl::ivar_iterator |
2183 | 9 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
2184 | 21 | for (; numIvars > 0 && IVI != IVE13 ; ++IVI12 ) { |
2185 | 12 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
2186 | 12 | ObjCIvarDecl* ClsIvar = *IVI; |
2187 | 12 | assert (ImplIvar && "missing implementation ivar"); |
2188 | 0 | assert (ClsIvar && "missing class ivar"); |
2189 | | |
2190 | | // First, make sure the types match. |
2191 | 12 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
2192 | 2 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
2193 | 2 | << ImplIvar->getIdentifier() |
2194 | 2 | << ImplIvar->getType() << ClsIvar->getType(); |
2195 | 2 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
2196 | 10 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField()3 && |
2197 | 10 | ImplIvar->getBitWidthValue(Context) != |
2198 | 3 | ClsIvar->getBitWidthValue(Context)) { |
2199 | 1 | Diag(ImplIvar->getBitWidth()->getBeginLoc(), |
2200 | 1 | diag::err_conflicting_ivar_bitwidth) |
2201 | 1 | << ImplIvar->getIdentifier(); |
2202 | 1 | Diag(ClsIvar->getBitWidth()->getBeginLoc(), |
2203 | 1 | diag::note_previous_definition); |
2204 | 1 | } |
2205 | | // Make sure the names are identical. |
2206 | 12 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
2207 | 1 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
2208 | 1 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
2209 | 1 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
2210 | 1 | } |
2211 | 12 | --numIvars; |
2212 | 12 | } |
2213 | | |
2214 | 9 | if (numIvars > 0) |
2215 | 1 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
2216 | 8 | else if (IVI != IVE) |
2217 | 1 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
2218 | 9 | } |
2219 | | |
2220 | | static void WarnUndefinedMethod(Sema &S, ObjCImplDecl *Impl, |
2221 | | ObjCMethodDecl *method, bool &IncompleteImpl, |
2222 | | unsigned DiagID, |
2223 | 1.35k | NamedDecl *NeededFor = nullptr) { |
2224 | | // No point warning no definition of method which is 'unavailable'. |
2225 | 1.35k | if (method->getAvailability() == AR_Unavailable) |
2226 | 23 | return; |
2227 | | |
2228 | | // FIXME: For now ignore 'IncompleteImpl'. |
2229 | | // Previously we grouped all unimplemented methods under a single |
2230 | | // warning, but some users strongly voiced that they would prefer |
2231 | | // separate warnings. We will give that approach a try, as that |
2232 | | // matches what we do with protocols. |
2233 | 1.32k | { |
2234 | 1.32k | const Sema::SemaDiagnosticBuilder &B = S.Diag(Impl->getLocation(), DiagID); |
2235 | 1.32k | B << method; |
2236 | 1.32k | if (NeededFor) |
2237 | 132 | B << NeededFor; |
2238 | | |
2239 | | // Add an empty definition at the end of the @implementation. |
2240 | 1.32k | std::string FixItStr; |
2241 | 1.32k | llvm::raw_string_ostream Out(FixItStr); |
2242 | 1.32k | method->print(Out, Impl->getASTContext().getPrintingPolicy()); |
2243 | 1.32k | Out << " {\n}\n\n"; |
2244 | | |
2245 | 1.32k | SourceLocation Loc = Impl->getAtEndRange().getBegin(); |
2246 | 1.32k | B << FixItHint::CreateInsertion(Loc, FixItStr); |
2247 | 1.32k | } |
2248 | | |
2249 | | // Issue a note to the original declaration. |
2250 | 1.32k | SourceLocation MethodLoc = method->getBeginLoc(); |
2251 | 1.32k | if (MethodLoc.isValid()) |
2252 | 1.32k | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
2253 | 1.32k | } |
2254 | | |
2255 | | /// Determines if type B can be substituted for type A. Returns true if we can |
2256 | | /// guarantee that anything that the user will do to an object of type A can |
2257 | | /// also be done to an object of type B. This is trivially true if the two |
2258 | | /// types are the same, or if B is a subclass of A. It becomes more complex |
2259 | | /// in cases where protocols are involved. |
2260 | | /// |
2261 | | /// Object types in Objective-C describe the minimum requirements for an |
2262 | | /// object, rather than providing a complete description of a type. For |
2263 | | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
2264 | | /// The principle of substitutability means that we may use an instance of A |
2265 | | /// anywhere that we may use an instance of B - it will implement all of the |
2266 | | /// ivars of B and all of the methods of B. |
2267 | | /// |
2268 | | /// This substitutability is important when type checking methods, because |
2269 | | /// the implementation may have stricter type definitions than the interface. |
2270 | | /// The interface specifies minimum requirements, but the implementation may |
2271 | | /// have more accurate ones. For example, a method may privately accept |
2272 | | /// instances of B, but only publish that it accepts instances of A. Any |
2273 | | /// object passed to it will be type checked against B, and so will implicitly |
2274 | | /// by a valid A*. Similarly, a method may return a subclass of the class that |
2275 | | /// it is declared as returning. |
2276 | | /// |
2277 | | /// This is most important when considering subclassing. A method in a |
2278 | | /// subclass must accept any object as an argument that its superclass's |
2279 | | /// implementation accepts. It may, however, accept a more general type |
2280 | | /// without breaking substitutability (i.e. you can still use the subclass |
2281 | | /// anywhere that you can use the superclass, but not vice versa). The |
2282 | | /// converse requirement applies to return types: the return type for a |
2283 | | /// subclass method must be a valid object of the kind that the superclass |
2284 | | /// advertises, but it may be specified more accurately. This avoids the need |
2285 | | /// for explicit down-casting by callers. |
2286 | | /// |
2287 | | /// Note: This is a stricter requirement than for assignment. |
2288 | | static bool isObjCTypeSubstitutable(ASTContext &Context, |
2289 | | const ObjCObjectPointerType *A, |
2290 | | const ObjCObjectPointerType *B, |
2291 | 6.71k | bool rejectId) { |
2292 | | // Reject a protocol-unqualified id. |
2293 | 6.71k | if (rejectId && B->isObjCIdType()1.19k ) return false891 ; |
2294 | | |
2295 | | // If B is a qualified id, then A must also be a qualified id and it must |
2296 | | // implement all of the protocols in B. It may not be a qualified class. |
2297 | | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
2298 | | // stricter definition so it is not substitutable for id<A>. |
2299 | 5.82k | if (B->isObjCQualifiedIdType()) { |
2300 | 450 | return A->isObjCQualifiedIdType() && |
2301 | 450 | Context.ObjCQualifiedIdTypesAreCompatible(A, B, false)446 ; |
2302 | 450 | } |
2303 | | |
2304 | | /* |
2305 | | // id is a special type that bypasses type checking completely. We want a |
2306 | | // warning when it is used in one place but not another. |
2307 | | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
2308 | | |
2309 | | |
2310 | | // If B is a qualified id, then A must also be a qualified id (which it isn't |
2311 | | // if we've got this far) |
2312 | | if (B->isObjCQualifiedIdType()) return false; |
2313 | | */ |
2314 | | |
2315 | | // Now we know that A and B are (potentially-qualified) class types. The |
2316 | | // normal rules for assignment apply. |
2317 | 5.37k | return Context.canAssignObjCInterfaces(A, B); |
2318 | 5.82k | } |
2319 | | |
2320 | 2.65k | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
2321 | 2.65k | return (TSI ? TSI->getTypeLoc().getSourceRange()2.64k : SourceRange()9 ); |
2322 | 2.65k | } |
2323 | | |
2324 | | /// Determine whether two set of Objective-C declaration qualifiers conflict. |
2325 | | static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
2326 | 14.1k | Decl::ObjCDeclQualifier y) { |
2327 | 14.1k | return (x & ~Decl::OBJC_TQ_CSNullability) != |
2328 | 14.1k | (y & ~Decl::OBJC_TQ_CSNullability); |
2329 | 14.1k | } |
2330 | | |
2331 | | static bool CheckMethodOverrideReturn(Sema &S, |
2332 | | ObjCMethodDecl *MethodImpl, |
2333 | | ObjCMethodDecl *MethodDecl, |
2334 | | bool IsProtocolMethodDecl, |
2335 | | bool IsOverridingMode, |
2336 | 53.1k | bool Warn) { |
2337 | 53.1k | if (IsProtocolMethodDecl && |
2338 | 53.1k | objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
2339 | 7.64k | MethodImpl->getObjCDeclQualifier())) { |
2340 | 6 | if (Warn) { |
2341 | 6 | S.Diag(MethodImpl->getLocation(), |
2342 | 6 | (IsOverridingMode |
2343 | 6 | ? diag::warn_conflicting_overriding_ret_type_modifiers4 |
2344 | 6 | : diag::warn_conflicting_ret_type_modifiers2 )) |
2345 | 6 | << MethodImpl->getDeclName() |
2346 | 6 | << MethodImpl->getReturnTypeSourceRange(); |
2347 | 6 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
2348 | 6 | << MethodDecl->getReturnTypeSourceRange(); |
2349 | 6 | } |
2350 | 0 | else |
2351 | 0 | return false; |
2352 | 6 | } |
2353 | 53.1k | if (Warn && IsOverridingMode53.1k && |
2354 | 53.1k | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext())48.1k && |
2355 | 53.1k | !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
2356 | 43.4k | MethodDecl->getReturnType(), |
2357 | 43.4k | false)) { |
2358 | 1 | auto nullabilityMethodImpl = |
2359 | 1 | *MethodImpl->getReturnType()->getNullability(S.Context); |
2360 | 1 | auto nullabilityMethodDecl = |
2361 | 1 | *MethodDecl->getReturnType()->getNullability(S.Context); |
2362 | 1 | S.Diag(MethodImpl->getLocation(), |
2363 | 1 | diag::warn_conflicting_nullability_attr_overriding_ret_types) |
2364 | 1 | << DiagNullabilityKind( |
2365 | 1 | nullabilityMethodImpl, |
2366 | 1 | ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
2367 | 1 | != 0)) |
2368 | 1 | << DiagNullabilityKind( |
2369 | 1 | nullabilityMethodDecl, |
2370 | 1 | ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
2371 | 1 | != 0)); |
2372 | 1 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
2373 | 1 | } |
2374 | | |
2375 | 53.1k | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
2376 | 53.1k | MethodDecl->getReturnType())) |
2377 | 47.4k | return true; |
2378 | 5.78k | if (!Warn) |
2379 | 2 | return false; |
2380 | | |
2381 | 5.78k | unsigned DiagID = |
2382 | 5.78k | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types5.59k |
2383 | 5.78k | : diag::warn_conflicting_ret_types192 ; |
2384 | | |
2385 | | // Mismatches between ObjC pointers go into a different warning |
2386 | | // category, and sometimes they're even completely explicitly allowed. |
2387 | 5.78k | if (const ObjCObjectPointerType *ImplPtrTy = |
2388 | 5.78k | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
2389 | 5.57k | if (const ObjCObjectPointerType *IfacePtrTy = |
2390 | 5.57k | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
2391 | | // Allow non-matching return types as long as they don't violate |
2392 | | // the principle of substitutability. Specifically, we permit |
2393 | | // return types that are subclasses of the declared return type, |
2394 | | // or that are more-qualified versions of the declared type. |
2395 | 5.52k | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
2396 | 5.23k | return false; |
2397 | | |
2398 | 296 | DiagID = |
2399 | 296 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types258 |
2400 | 296 | : diag::warn_non_covariant_ret_types38 ; |
2401 | 296 | } |
2402 | 5.57k | } |
2403 | | |
2404 | 554 | S.Diag(MethodImpl->getLocation(), DiagID) |
2405 | 554 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
2406 | 554 | << MethodImpl->getReturnType() |
2407 | 554 | << MethodImpl->getReturnTypeSourceRange(); |
2408 | 554 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
2409 | 554 | ? diag::note_previous_declaration430 |
2410 | 554 | : diag::note_previous_definition124 ) |
2411 | 554 | << MethodDecl->getReturnTypeSourceRange(); |
2412 | 554 | return false; |
2413 | 5.78k | } |
2414 | | |
2415 | | static bool CheckMethodOverrideParam(Sema &S, |
2416 | | ObjCMethodDecl *MethodImpl, |
2417 | | ObjCMethodDecl *MethodDecl, |
2418 | | ParmVarDecl *ImplVar, |
2419 | | ParmVarDecl *IfaceVar, |
2420 | | bool IsProtocolMethodDecl, |
2421 | | bool IsOverridingMode, |
2422 | 38.6k | bool Warn) { |
2423 | 38.6k | if (IsProtocolMethodDecl && |
2424 | 38.6k | objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
2425 | 6.53k | IfaceVar->getObjCDeclQualifier())) { |
2426 | 148 | if (Warn) { |
2427 | 148 | if (IsOverridingMode) |
2428 | 74 | S.Diag(ImplVar->getLocation(), |
2429 | 74 | diag::warn_conflicting_overriding_param_modifiers) |
2430 | 74 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
2431 | 74 | << MethodImpl->getDeclName(); |
2432 | 74 | else S.Diag(ImplVar->getLocation(), |
2433 | 74 | diag::warn_conflicting_param_modifiers) |
2434 | 74 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
2435 | 74 | << MethodImpl->getDeclName(); |
2436 | 148 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
2437 | 148 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
2438 | 148 | } |
2439 | 0 | else |
2440 | 0 | return false; |
2441 | 148 | } |
2442 | | |
2443 | 38.6k | QualType ImplTy = ImplVar->getType(); |
2444 | 38.6k | QualType IfaceTy = IfaceVar->getType(); |
2445 | 38.6k | if (Warn && IsOverridingMode38.6k && |
2446 | 38.6k | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext())35.4k && |
2447 | 38.6k | !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)32.4k ) { |
2448 | 1 | S.Diag(ImplVar->getLocation(), |
2449 | 1 | diag::warn_conflicting_nullability_attr_overriding_param_types) |
2450 | 1 | << DiagNullabilityKind( |
2451 | 1 | *ImplTy->getNullability(S.Context), |
2452 | 1 | ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
2453 | 1 | != 0)) |
2454 | 1 | << DiagNullabilityKind( |
2455 | 1 | *IfaceTy->getNullability(S.Context), |
2456 | 1 | ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
2457 | 1 | != 0)); |
2458 | 1 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
2459 | 1 | } |
2460 | 38.6k | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
2461 | 37.3k | return true; |
2462 | | |
2463 | 1.23k | if (!Warn) |
2464 | 0 | return false; |
2465 | 1.23k | unsigned DiagID = |
2466 | 1.23k | IsOverridingMode ? diag::warn_conflicting_overriding_param_types1.19k |
2467 | 1.23k | : diag::warn_conflicting_param_types35 ; |
2468 | | |
2469 | | // Mismatches between ObjC pointers go into a different warning |
2470 | | // category, and sometimes they're even completely explicitly allowed.. |
2471 | 1.23k | if (const ObjCObjectPointerType *ImplPtrTy = |
2472 | 1.23k | ImplTy->getAs<ObjCObjectPointerType>()) { |
2473 | 1.20k | if (const ObjCObjectPointerType *IfacePtrTy = |
2474 | 1.20k | IfaceTy->getAs<ObjCObjectPointerType>()) { |
2475 | | // Allow non-matching argument types as long as they don't |
2476 | | // violate the principle of substitutability. Specifically, the |
2477 | | // implementation must accept any objects that the superclass |
2478 | | // accepts, however it may also accept others. |
2479 | 1.19k | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
2480 | 56 | return false; |
2481 | | |
2482 | 1.13k | DiagID = |
2483 | 1.13k | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types1.11k |
2484 | 1.13k | : diag::warn_non_contravariant_param_types16 ; |
2485 | 1.13k | } |
2486 | 1.20k | } |
2487 | | |
2488 | 1.17k | S.Diag(ImplVar->getLocation(), DiagID) |
2489 | 1.17k | << getTypeRange(ImplVar->getTypeSourceInfo()) |
2490 | 1.17k | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
2491 | 1.17k | S.Diag(IfaceVar->getLocation(), |
2492 | 1.17k | (IsOverridingMode ? diag::note_previous_declaration1.14k |
2493 | 1.17k | : diag::note_previous_definition28 )) |
2494 | 1.17k | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
2495 | 1.17k | return false; |
2496 | 1.23k | } |
2497 | | |
2498 | | /// In ARC, check whether the conventional meanings of the two methods |
2499 | | /// match. If they don't, it's a hard error. |
2500 | | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
2501 | 561 | ObjCMethodDecl *decl) { |
2502 | 561 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
2503 | 561 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
2504 | 561 | if (implFamily == declFamily) return false531 ; |
2505 | | |
2506 | | // Since conventions are sorted by selector, the only possibility is |
2507 | | // that the types differ enough to cause one selector or the other |
2508 | | // to fall out of the family. |
2509 | 30 | assert(implFamily == OMF_None || declFamily == OMF_None); |
2510 | | |
2511 | | // No further diagnostics required on invalid declarations. |
2512 | 30 | if (impl->isInvalidDecl() || decl->isInvalidDecl()24 ) return true9 ; |
2513 | | |
2514 | 21 | const ObjCMethodDecl *unmatched = impl; |
2515 | 21 | ObjCMethodFamily family = declFamily; |
2516 | 21 | unsigned errorID = diag::err_arc_lost_method_convention; |
2517 | 21 | unsigned noteID = diag::note_arc_lost_method_convention; |
2518 | 21 | if (declFamily == OMF_None) { |
2519 | 9 | unmatched = decl; |
2520 | 9 | family = implFamily; |
2521 | 9 | errorID = diag::err_arc_gained_method_convention; |
2522 | 9 | noteID = diag::note_arc_gained_method_convention; |
2523 | 9 | } |
2524 | | |
2525 | | // Indexes into a %select clause in the diagnostic. |
2526 | 21 | enum FamilySelector { |
2527 | 21 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
2528 | 21 | }; |
2529 | 21 | FamilySelector familySelector = FamilySelector(); |
2530 | | |
2531 | 21 | switch (family) { |
2532 | 0 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
2533 | 0 | case OMF_retain: |
2534 | 0 | case OMF_release: |
2535 | 0 | case OMF_autorelease: |
2536 | 0 | case OMF_dealloc: |
2537 | 0 | case OMF_finalize: |
2538 | 0 | case OMF_retainCount: |
2539 | 0 | case OMF_self: |
2540 | 0 | case OMF_initialize: |
2541 | 0 | case OMF_performSelector: |
2542 | | // Mismatches for these methods don't change ownership |
2543 | | // conventions, so we don't care. |
2544 | 0 | return false; |
2545 | | |
2546 | 21 | case OMF_init: familySelector = F_init; break; |
2547 | 0 | case OMF_alloc: familySelector = F_alloc; break; |
2548 | 0 | case OMF_copy: familySelector = F_copy; break; |
2549 | 0 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
2550 | 0 | case OMF_new: familySelector = F_new; break; |
2551 | 21 | } |
2552 | | |
2553 | 21 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
2554 | 21 | ReasonSelector reasonSelector; |
2555 | | |
2556 | | // The only reason these methods don't fall within their families is |
2557 | | // due to unusual result types. |
2558 | 21 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
2559 | 0 | reasonSelector = R_UnrelatedReturn; |
2560 | 21 | } else { |
2561 | 21 | reasonSelector = R_NonObjectReturn; |
2562 | 21 | } |
2563 | | |
2564 | 21 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
2565 | 21 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
2566 | | |
2567 | 21 | return true; |
2568 | 21 | } |
2569 | | |
2570 | | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
2571 | | ObjCMethodDecl *MethodDecl, |
2572 | 5.00k | bool IsProtocolMethodDecl) { |
2573 | 5.00k | if (getLangOpts().ObjCAutoRefCount && |
2574 | 5.00k | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)561 ) |
2575 | 30 | return; |
2576 | | |
2577 | 4.97k | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
2578 | 4.97k | IsProtocolMethodDecl, false, |
2579 | 4.97k | true); |
2580 | | |
2581 | 4.97k | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
2582 | 4.97k | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
2583 | 4.97k | EF = MethodDecl->param_end(); |
2584 | 8.15k | IM != EM && IF != EF3.17k ; ++IM, ++IF3.17k ) { |
2585 | 3.17k | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
2586 | 3.17k | IsProtocolMethodDecl, false, true); |
2587 | 3.17k | } |
2588 | | |
2589 | 4.97k | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
2590 | 2 | Diag(ImpMethodDecl->getLocation(), |
2591 | 2 | diag::warn_conflicting_variadic); |
2592 | 2 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
2593 | 2 | } |
2594 | 4.97k | } |
2595 | | |
2596 | | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
2597 | | ObjCMethodDecl *Overridden, |
2598 | 48.1k | bool IsProtocolMethodDecl) { |
2599 | | |
2600 | 48.1k | CheckMethodOverrideReturn(*this, Method, Overridden, |
2601 | 48.1k | IsProtocolMethodDecl, true, |
2602 | 48.1k | true); |
2603 | | |
2604 | 48.1k | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
2605 | 48.1k | IF = Overridden->param_begin(), EM = Method->param_end(), |
2606 | 48.1k | EF = Overridden->param_end(); |
2607 | 83.6k | IM != EM && IF != EF35.4k ; ++IM, ++IF35.4k ) { |
2608 | 35.4k | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
2609 | 35.4k | IsProtocolMethodDecl, true, true); |
2610 | 35.4k | } |
2611 | | |
2612 | 48.1k | if (Method->isVariadic() != Overridden->isVariadic()) { |
2613 | 3 | Diag(Method->getLocation(), |
2614 | 3 | diag::warn_conflicting_overriding_variadic); |
2615 | 3 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
2616 | 3 | } |
2617 | 48.1k | } |
2618 | | |
2619 | | /// WarnExactTypedMethods - This routine issues a warning if method |
2620 | | /// implementation declaration matches exactly that of its declaration. |
2621 | | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
2622 | | ObjCMethodDecl *MethodDecl, |
2623 | 24 | bool IsProtocolMethodDecl) { |
2624 | | // don't issue warning when protocol method is optional because primary |
2625 | | // class is not required to implement it and it is safe for protocol |
2626 | | // to implement it. |
2627 | 24 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
2628 | 1 | return; |
2629 | | // don't issue warning when primary class's method is |
2630 | | // deprecated/unavailable. |
2631 | 23 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
2632 | 23 | MethodDecl->hasAttr<DeprecatedAttr>()) |
2633 | 0 | return; |
2634 | | |
2635 | 23 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
2636 | 23 | IsProtocolMethodDecl, false, false); |
2637 | 23 | if (match) |
2638 | 21 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
2639 | 21 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
2640 | 21 | EF = MethodDecl->param_end(); |
2641 | 24 | IM != EM && IF != EF3 ; ++IM, ++IF3 ) { |
2642 | 3 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
2643 | 3 | *IM, *IF, |
2644 | 3 | IsProtocolMethodDecl, false, false); |
2645 | 3 | if (!match) |
2646 | 0 | break; |
2647 | 3 | } |
2648 | 23 | if (match) |
2649 | 21 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
2650 | 23 | if (match) |
2651 | 21 | match = !(MethodDecl->isClassMethod() && |
2652 | 21 | MethodDecl->getSelector() == GetNullarySelector("load", Context)0 ); |
2653 | | |
2654 | 23 | if (match) { |
2655 | 21 | Diag(ImpMethodDecl->getLocation(), |
2656 | 21 | diag::warn_category_method_impl_match); |
2657 | 21 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
2658 | 21 | << MethodDecl->getDeclName(); |
2659 | 21 | } |
2660 | 23 | } |
2661 | | |
2662 | | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
2663 | | /// improve the efficiency of selector lookups and type checking by associating |
2664 | | /// with each protocol / interface / category the flattened instance tables. If |
2665 | | /// we used an immutable set to keep the table then it wouldn't add significant |
2666 | | /// memory cost and it would be handy for lookups. |
2667 | | |
2668 | | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
2669 | | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
2670 | | |
2671 | | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
2672 | 9 | ProtocolNameSet &PNS) { |
2673 | 9 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
2674 | 7 | PNS.insert(PDecl->getIdentifier()); |
2675 | 9 | for (const auto *PI : PDecl->protocols()) |
2676 | 1 | findProtocolsWithExplicitImpls(PI, PNS); |
2677 | 9 | } |
2678 | | |
2679 | | /// Recursively populates a set with all conformed protocols in a class |
2680 | | /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
2681 | | /// attribute. |
2682 | | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
2683 | 22 | ProtocolNameSet &PNS) { |
2684 | 22 | if (!Super) |
2685 | 12 | return; |
2686 | | |
2687 | 10 | for (const auto *I : Super->all_referenced_protocols()) |
2688 | 8 | findProtocolsWithExplicitImpls(I, PNS); |
2689 | | |
2690 | 10 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
2691 | 10 | } |
2692 | | |
2693 | | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
2694 | | /// Declared in protocol, and those referenced by it. |
2695 | | static void CheckProtocolMethodDefs( |
2696 | | Sema &S, ObjCImplDecl *Impl, ObjCProtocolDecl *PDecl, bool &IncompleteImpl, |
2697 | | const Sema::SelectorSet &InsMap, const Sema::SelectorSet &ClsMap, |
2698 | 760 | ObjCContainerDecl *CDecl, LazyProtocolNameSet &ProtocolsExplictImpl) { |
2699 | 760 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
2700 | 760 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()25 |
2701 | 760 | : dyn_cast<ObjCInterfaceDecl>(CDecl)735 ; |
2702 | 760 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
2703 | | |
2704 | 0 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
2705 | 760 | ObjCInterfaceDecl *NSIDecl = nullptr; |
2706 | | |
2707 | | // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
2708 | | // then we should check if any class in the super class hierarchy also |
2709 | | // conforms to this protocol, either directly or via protocol inheritance. |
2710 | | // If so, we can skip checking this protocol completely because we |
2711 | | // know that a parent class already satisfies this protocol. |
2712 | | // |
2713 | | // Note: we could generalize this logic for all protocols, and merely |
2714 | | // add the limit on looking at the super class chain for just |
2715 | | // specially marked protocols. This may be a good optimization. This |
2716 | | // change is restricted to 'objc_protocol_requires_explicit_implementation' |
2717 | | // protocols for now for controlled evaluation. |
2718 | 760 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
2719 | 16 | if (!ProtocolsExplictImpl) { |
2720 | 12 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
2721 | 12 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
2722 | 12 | } |
2723 | 16 | if (ProtocolsExplictImpl->contains(PDecl->getIdentifier())) |
2724 | 6 | return; |
2725 | | |
2726 | | // If no super class conforms to the protocol, we should not search |
2727 | | // for methods in the super class to implicitly satisfy the protocol. |
2728 | 10 | Super = nullptr; |
2729 | 10 | } |
2730 | | |
2731 | 754 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
2732 | | // check to see if class implements forwardInvocation method and objects |
2733 | | // of this class are derived from 'NSProxy' so that to forward requests |
2734 | | // from one object to another. |
2735 | | // Under such conditions, which means that every method possible is |
2736 | | // implemented in the class, we should not issue "Method definition not |
2737 | | // found" warnings. |
2738 | | // FIXME: Use a general GetUnarySelector method for this. |
2739 | 731 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
2740 | 731 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
2741 | 731 | if (InsMap.count(fISelector)) |
2742 | | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
2743 | | // need be implemented in the implementation. |
2744 | 1 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
2745 | 731 | } |
2746 | | |
2747 | | // If this is a forward protocol declaration, get its definition. |
2748 | 754 | if (!PDecl->isThisDeclarationADefinition() && |
2749 | 754 | PDecl->getDefinition()6 ) |
2750 | 1 | PDecl = PDecl->getDefinition(); |
2751 | | |
2752 | | // If a method lookup fails locally we still need to look and see if |
2753 | | // the method was implemented by a base class or an inherited |
2754 | | // protocol. This lookup is slow, but occurs rarely in correct code |
2755 | | // and otherwise would terminate in a warning. |
2756 | | |
2757 | | // check unimplemented instance methods. |
2758 | 754 | if (!NSIDecl) |
2759 | 1.04k | for (auto *method : PDecl->instance_methods())753 { |
2760 | 1.04k | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
2761 | 1.04k | !method->isPropertyAccessor()997 && |
2762 | 1.04k | !InsMap.count(method->getSelector())677 && |
2763 | 1.04k | (389 !Super389 || !Super->lookupMethod(method->getSelector(), |
2764 | 284 | true /* instance */, |
2765 | 284 | false /* shallowCategory */, |
2766 | 284 | true /* followsSuper */, |
2767 | 284 | nullptr /* category */))) { |
2768 | | // If a method is not implemented in the category implementation but |
2769 | | // has been declared in its primary class, superclass, |
2770 | | // or in one of their protocols, no need to issue the warning. |
2771 | | // This is because method will be implemented in the primary class |
2772 | | // or one of its super class implementation. |
2773 | | |
2774 | | // Ugly, but necessary. Method declared in protocol might have |
2775 | | // have been synthesized due to a property declared in the class which |
2776 | | // uses the protocol. |
2777 | 119 | if (ObjCMethodDecl *MethodInClass = |
2778 | 119 | IDecl->lookupMethod(method->getSelector(), |
2779 | 119 | true /* instance */, |
2780 | 119 | true /* shallowCategoryLookup */, |
2781 | 119 | false /* followSuper */)) |
2782 | 34 | if (C || MethodInClass->isPropertyAccessor()31 ) |
2783 | 5 | continue; |
2784 | 114 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
2785 | 114 | if (!S.Diags.isIgnored(DIAG, Impl->getLocation())) { |
2786 | 105 | WarnUndefinedMethod(S, Impl, method, IncompleteImpl, DIAG, PDecl); |
2787 | 105 | } |
2788 | 114 | } |
2789 | 1.04k | } |
2790 | | // check unimplemented class methods |
2791 | 754 | for (auto *method : PDecl->class_methods()) { |
2792 | 139 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
2793 | 139 | !ClsMap.count(method->getSelector())136 && |
2794 | 139 | (34 !Super34 || !Super->lookupMethod(method->getSelector(), |
2795 | 28 | false /* class method */, |
2796 | 28 | false /* shallowCategoryLookup */, |
2797 | 28 | true /* followSuper */, |
2798 | 34 | nullptr /* category */))) { |
2799 | | // See above comment for instance method lookups. |
2800 | 34 | if (C && IDecl->lookupMethod(method->getSelector(), |
2801 | 1 | false /* class */, |
2802 | 1 | true /* shallowCategoryLookup */, |
2803 | 1 | false /* followSuper */)) |
2804 | 1 | continue; |
2805 | | |
2806 | 33 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
2807 | 33 | if (!S.Diags.isIgnored(DIAG, Impl->getLocation())) { |
2808 | 27 | WarnUndefinedMethod(S, Impl, method, IncompleteImpl, DIAG, PDecl); |
2809 | 27 | } |
2810 | 33 | } |
2811 | 139 | } |
2812 | | // Check on this protocols's referenced protocols, recursively. |
2813 | 754 | for (auto *PI : PDecl->protocols()) |
2814 | 186 | CheckProtocolMethodDefs(S, Impl, PI, IncompleteImpl, InsMap, ClsMap, CDecl, |
2815 | 186 | ProtocolsExplictImpl); |
2816 | 754 | } |
2817 | | |
2818 | | /// MatchAllMethodDeclarations - Check methods declared in interface |
2819 | | /// or protocol against those declared in their implementations. |
2820 | | /// |
2821 | | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
2822 | | const SelectorSet &ClsMap, |
2823 | | SelectorSet &InsMapSeen, |
2824 | | SelectorSet &ClsMapSeen, |
2825 | | ObjCImplDecl* IMPDecl, |
2826 | | ObjCContainerDecl* CDecl, |
2827 | | bool &IncompleteImpl, |
2828 | | bool ImmediateClass, |
2829 | 20.5k | bool WarnCategoryMethodImpl) { |
2830 | | // Check and see if instance methods in class interface have been |
2831 | | // implemented in the implementation class. If so, their types match. |
2832 | 68.7k | for (auto *I : CDecl->instance_methods()) { |
2833 | 68.7k | if (!InsMapSeen.insert(I->getSelector()).second) |
2834 | 1.56k | continue; |
2835 | 67.2k | if (!I->isPropertyAccessor() && |
2836 | 67.2k | !InsMap.count(I->getSelector())52.5k ) { |
2837 | 48.7k | if (ImmediateClass) |
2838 | 1.04k | WarnUndefinedMethod(*this, IMPDecl, I, IncompleteImpl, |
2839 | 1.04k | diag::warn_undef_method_impl); |
2840 | 48.7k | continue; |
2841 | 48.7k | } else { |
2842 | 18.4k | ObjCMethodDecl *ImpMethodDecl = |
2843 | 18.4k | IMPDecl->getInstanceMethod(I->getSelector()); |
2844 | 18.4k | assert(CDecl->getInstanceMethod(I->getSelector(), true/*AllowHidden*/) && |
2845 | 18.4k | "Expected to find the method through lookup as well"); |
2846 | | // ImpMethodDecl may be null as in a @dynamic property. |
2847 | 18.4k | if (ImpMethodDecl) { |
2848 | | // Skip property accessor function stubs. |
2849 | 9.28k | if (ImpMethodDecl->isSynthesizedAccessorStub()) |
2850 | 5.02k | continue; |
2851 | 4.26k | if (!WarnCategoryMethodImpl) |
2852 | 4.22k | WarnConflictingTypedMethods(ImpMethodDecl, I, |
2853 | 4.22k | isa<ObjCProtocolDecl>(CDecl)); |
2854 | 33 | else if (!I->isPropertyAccessor()) |
2855 | 24 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
2856 | 4.26k | } |
2857 | 18.4k | } |
2858 | 67.2k | } |
2859 | | |
2860 | | // Check and see if class methods in class interface have been |
2861 | | // implemented in the implementation class. If so, their types match. |
2862 | 20.5k | for (auto *I : CDecl->class_methods()) { |
2863 | 15.6k | if (!ClsMapSeen.insert(I->getSelector()).second) |
2864 | 92 | continue; |
2865 | 15.5k | if (!I->isPropertyAccessor() && |
2866 | 15.5k | !ClsMap.count(I->getSelector())14.9k ) { |
2867 | 14.2k | if (ImmediateClass) |
2868 | 175 | WarnUndefinedMethod(*this, IMPDecl, I, IncompleteImpl, |
2869 | 175 | diag::warn_undef_method_impl); |
2870 | 14.2k | } else { |
2871 | 1.32k | ObjCMethodDecl *ImpMethodDecl = |
2872 | 1.32k | IMPDecl->getClassMethod(I->getSelector()); |
2873 | 1.32k | assert(CDecl->getClassMethod(I->getSelector(), true/*AllowHidden*/) && |
2874 | 1.32k | "Expected to find the method through lookup as well"); |
2875 | | // ImpMethodDecl may be null as in a @dynamic property. |
2876 | 1.32k | if (ImpMethodDecl) { |
2877 | | // Skip property accessor function stubs. |
2878 | 777 | if (ImpMethodDecl->isSynthesizedAccessorStub()) |
2879 | 0 | continue; |
2880 | 777 | if (!WarnCategoryMethodImpl) |
2881 | 777 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
2882 | 777 | isa<ObjCProtocolDecl>(CDecl)); |
2883 | 0 | else if (!I->isPropertyAccessor()) |
2884 | 0 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
2885 | 777 | } |
2886 | 1.32k | } |
2887 | 15.5k | } |
2888 | | |
2889 | 20.5k | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
2890 | | // Also, check for methods declared in protocols inherited by |
2891 | | // this protocol. |
2892 | 2.15k | for (auto *PI : PD->protocols()) |
2893 | 306 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
2894 | 306 | IMPDecl, PI, IncompleteImpl, false, |
2895 | 306 | WarnCategoryMethodImpl); |
2896 | 2.15k | } |
2897 | | |
2898 | 20.5k | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
2899 | | // when checking that methods in implementation match their declaration, |
2900 | | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
2901 | | // extension; as well as those in categories. |
2902 | 7.80k | if (!WarnCategoryMethodImpl) { |
2903 | 7.44k | for (auto *Cat : I->visible_categories()) |
2904 | 10.0k | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
2905 | 10.0k | IMPDecl, Cat, IncompleteImpl, |
2906 | 10.0k | ImmediateClass && Cat->IsClassExtension()730 , |
2907 | 10.0k | WarnCategoryMethodImpl); |
2908 | 7.44k | } else { |
2909 | | // Also methods in class extensions need be looked at next. |
2910 | 357 | for (auto *Ext : I->visible_extensions()) |
2911 | 18 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
2912 | 18 | IMPDecl, Ext, IncompleteImpl, false, |
2913 | 18 | WarnCategoryMethodImpl); |
2914 | 357 | } |
2915 | | |
2916 | | // Check for any implementation of a methods declared in protocol. |
2917 | 7.80k | for (auto *PI : I->all_referenced_protocols()) |
2918 | 1.84k | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
2919 | 1.84k | IMPDecl, PI, IncompleteImpl, false, |
2920 | 1.84k | WarnCategoryMethodImpl); |
2921 | | |
2922 | | // FIXME. For now, we are not checking for exact match of methods |
2923 | | // in category implementation and its primary class's super class. |
2924 | 7.80k | if (!WarnCategoryMethodImpl && I->getSuperClass()7.44k ) |
2925 | 2.59k | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
2926 | 2.59k | IMPDecl, |
2927 | 2.59k | I->getSuperClass(), IncompleteImpl, false); |
2928 | 7.80k | } |
2929 | 20.5k | } |
2930 | | |
2931 | | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
2932 | | /// category matches with those implemented in its primary class and |
2933 | | /// warns each time an exact match is found. |
2934 | | void Sema::CheckCategoryVsClassMethodMatches( |
2935 | 506 | ObjCCategoryImplDecl *CatIMPDecl) { |
2936 | | // Get category's primary class. |
2937 | 506 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
2938 | 506 | if (!CatDecl) |
2939 | 0 | return; |
2940 | 506 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
2941 | 506 | if (!IDecl) |
2942 | 0 | return; |
2943 | 506 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
2944 | 506 | SelectorSet InsMap, ClsMap; |
2945 | | |
2946 | 506 | for (const auto *I : CatIMPDecl->instance_methods()) { |
2947 | 440 | Selector Sel = I->getSelector(); |
2948 | | // When checking for methods implemented in the category, skip over |
2949 | | // those declared in category class's super class. This is because |
2950 | | // the super class must implement the method. |
2951 | 440 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)307 ) |
2952 | 25 | continue; |
2953 | 415 | InsMap.insert(Sel); |
2954 | 415 | } |
2955 | | |
2956 | 506 | for (const auto *I : CatIMPDecl->class_methods()) { |
2957 | 64 | Selector Sel = I->getSelector(); |
2958 | 64 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)47 ) |
2959 | 5 | continue; |
2960 | 59 | ClsMap.insert(Sel); |
2961 | 59 | } |
2962 | 506 | if (InsMap.empty() && ClsMap.empty()181 ) |
2963 | 149 | return; |
2964 | | |
2965 | 357 | SelectorSet InsMapSeen, ClsMapSeen; |
2966 | 357 | bool IncompleteImpl = false; |
2967 | 357 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
2968 | 357 | CatIMPDecl, IDecl, |
2969 | 357 | IncompleteImpl, false, |
2970 | 357 | true /*WarnCategoryMethodImpl*/); |
2971 | 357 | } |
2972 | | |
2973 | | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
2974 | | ObjCContainerDecl* CDecl, |
2975 | 5.35k | bool IncompleteImpl) { |
2976 | 5.35k | SelectorSet InsMap; |
2977 | | // Check and see if instance methods in class interface have been |
2978 | | // implemented in the implementation class. |
2979 | 5.35k | for (const auto *I : IMPDecl->instance_methods()) |
2980 | 11.6k | InsMap.insert(I->getSelector()); |
2981 | | |
2982 | | // Add the selectors for getters/setters of @dynamic properties. |
2983 | 5.35k | for (const auto *PImpl : IMPDecl->property_impls()) { |
2984 | | // We only care about @dynamic implementations. |
2985 | 3.00k | if (PImpl->getPropertyImplementation() != ObjCPropertyImplDecl::Dynamic) |
2986 | 2.78k | continue; |
2987 | | |
2988 | 219 | const auto *P = PImpl->getPropertyDecl(); |
2989 | 219 | if (!P) continue0 ; |
2990 | | |
2991 | 219 | InsMap.insert(P->getGetterName()); |
2992 | 219 | if (!P->getSetterName().isNull()) |
2993 | 219 | InsMap.insert(P->getSetterName()); |
2994 | 219 | } |
2995 | | |
2996 | | // Check and see if properties declared in the interface have either 1) |
2997 | | // an implementation or 2) there is a @synthesize/@dynamic implementation |
2998 | | // of the property in the @implementation. |
2999 | 5.35k | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
3000 | 4.85k | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
3001 | 4.85k | LangOpts.ObjCRuntime.isNonFragile()4.83k && |
3002 | 4.85k | !IDecl->isObjCRequiresPropertyDefs()4.61k ; |
3003 | 4.85k | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
3004 | 4.85k | } |
3005 | | |
3006 | | // Diagnose null-resettable synthesized setters. |
3007 | 5.35k | diagnoseNullResettableSynthesizedSetters(IMPDecl); |
3008 | | |
3009 | 5.35k | SelectorSet ClsMap; |
3010 | 5.35k | for (const auto *I : IMPDecl->class_methods()) |
3011 | 1.22k | ClsMap.insert(I->getSelector()); |
3012 | | |
3013 | | // Check for type conflict of methods declared in a class/protocol and |
3014 | | // its implementation; if any. |
3015 | 5.35k | SelectorSet InsMapSeen, ClsMapSeen; |
3016 | 5.35k | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
3017 | 5.35k | IMPDecl, CDecl, |
3018 | 5.35k | IncompleteImpl, true); |
3019 | | |
3020 | | // check all methods implemented in category against those declared |
3021 | | // in its primary class. |
3022 | 5.35k | if (ObjCCategoryImplDecl *CatDecl = |
3023 | 5.35k | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
3024 | 506 | CheckCategoryVsClassMethodMatches(CatDecl); |
3025 | | |
3026 | | // Check the protocol list for unimplemented methods in the @implementation |
3027 | | // class. |
3028 | | // Check and see if class methods in class interface have been |
3029 | | // implemented in the implementation class. |
3030 | | |
3031 | 5.35k | LazyProtocolNameSet ExplicitImplProtocols; |
3032 | | |
3033 | 5.35k | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
3034 | 4.85k | for (auto *PI : I->all_referenced_protocols()) |
3035 | 549 | CheckProtocolMethodDefs(*this, IMPDecl, PI, IncompleteImpl, InsMap, |
3036 | 549 | ClsMap, I, ExplicitImplProtocols); |
3037 | 4.85k | } else if (ObjCCategoryDecl *506 C506 = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
3038 | | // For extended class, unimplemented methods in its protocols will |
3039 | | // be reported in the primary class. |
3040 | 506 | if (!C->IsClassExtension()) { |
3041 | 506 | for (auto *P : C->protocols()) |
3042 | 25 | CheckProtocolMethodDefs(*this, IMPDecl, P, IncompleteImpl, InsMap, |
3043 | 25 | ClsMap, CDecl, ExplicitImplProtocols); |
3044 | 506 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
3045 | 506 | /*SynthesizeProperties=*/false); |
3046 | 506 | } |
3047 | 506 | } else |
3048 | 0 | llvm_unreachable("invalid ObjCContainerDecl type."); |
3049 | 5.35k | } |
3050 | | |
3051 | | Sema::DeclGroupPtrTy |
3052 | | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
3053 | | IdentifierInfo **IdentList, |
3054 | | SourceLocation *IdentLocs, |
3055 | | ArrayRef<ObjCTypeParamList *> TypeParamLists, |
3056 | 63.8k | unsigned NumElts) { |
3057 | 63.8k | SmallVector<Decl *, 8> DeclsInGroup; |
3058 | 196k | for (unsigned i = 0; i != NumElts; ++i132k ) { |
3059 | | // Check for another declaration kind with the same name. |
3060 | 132k | NamedDecl *PrevDecl |
3061 | 132k | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
3062 | 132k | LookupOrdinaryName, forRedeclarationInCurContext()); |
3063 | 132k | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)106k ) { |
3064 | | // GCC apparently allows the following idiom: |
3065 | | // |
3066 | | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
3067 | | // @class XCElementToggler; |
3068 | | // |
3069 | | // Here we have chosen to ignore the forward class declaration |
3070 | | // with a warning. Since this is the implied behavior. |
3071 | 4 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
3072 | 4 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
3073 | 1 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
3074 | 1 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
3075 | 3 | } else { |
3076 | | // a forward class declaration matching a typedef name of a class refers |
3077 | | // to the underlying class. Just ignore the forward class with a warning |
3078 | | // as this will force the intended behavior which is to lookup the |
3079 | | // typedef name. |
3080 | 3 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
3081 | 3 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) |
3082 | 3 | << IdentList[i]; |
3083 | 3 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
3084 | 3 | continue; |
3085 | 3 | } |
3086 | 3 | } |
3087 | 4 | } |
3088 | | |
3089 | | // Create a declaration to describe this forward declaration. |
3090 | 132k | ObjCInterfaceDecl *PrevIDecl |
3091 | 132k | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
3092 | | |
3093 | 132k | IdentifierInfo *ClassName = IdentList[i]; |
3094 | 132k | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName106k ) { |
3095 | | // A previous decl with a different name is because of |
3096 | | // @compatibility_alias, for example: |
3097 | | // \code |
3098 | | // @class NewImage; |
3099 | | // @compatibility_alias OldImage NewImage; |
3100 | | // \endcode |
3101 | | // A lookup for 'OldImage' will return the 'NewImage' decl. |
3102 | | // |
3103 | | // In such a case use the real declaration name, instead of the alias one, |
3104 | | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
3105 | | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
3106 | | // has been aliased. |
3107 | 6 | ClassName = PrevIDecl->getIdentifier(); |
3108 | 6 | } |
3109 | | |
3110 | | // If this forward declaration has type parameters, compare them with the |
3111 | | // type parameters of the previous declaration. |
3112 | 132k | ObjCTypeParamList *TypeParams = TypeParamLists[i]; |
3113 | 132k | if (PrevIDecl && TypeParams106k ) { |
3114 | 21.2k | if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) { |
3115 | | // Check for consistency with the previous declaration. |
3116 | 20.5k | if (checkTypeParamListConsistency( |
3117 | 20.5k | *this, PrevTypeParams, TypeParams, |
3118 | 20.5k | TypeParamListContext::ForwardDeclaration)) { |
3119 | 0 | TypeParams = nullptr; |
3120 | 0 | } |
3121 | 20.5k | } else if (ObjCInterfaceDecl *659 Def659 = PrevIDecl->getDefinition()) { |
3122 | | // The @interface does not have type parameters. Complain. |
3123 | 1 | Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class) |
3124 | 1 | << ClassName |
3125 | 1 | << TypeParams->getSourceRange(); |
3126 | 1 | Diag(Def->getLocation(), diag::note_defined_here) |
3127 | 1 | << ClassName; |
3128 | | |
3129 | 1 | TypeParams = nullptr; |
3130 | 1 | } |
3131 | 21.2k | } |
3132 | | |
3133 | 132k | ObjCInterfaceDecl *IDecl |
3134 | 132k | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
3135 | 132k | ClassName, TypeParams, PrevIDecl, |
3136 | 132k | IdentLocs[i]); |
3137 | 132k | IDecl->setAtEndRange(IdentLocs[i]); |
3138 | | |
3139 | 132k | if (PrevIDecl) |
3140 | 106k | mergeDeclAttributes(IDecl, PrevIDecl); |
3141 | | |
3142 | 132k | PushOnScopeChains(IDecl, TUScope); |
3143 | 132k | CheckObjCDeclScope(IDecl); |
3144 | 132k | DeclsInGroup.push_back(IDecl); |
3145 | 132k | } |
3146 | | |
3147 | 63.8k | return BuildDeclaratorGroup(DeclsInGroup); |
3148 | 63.8k | } |
3149 | | |
3150 | | static bool tryMatchRecordTypes(ASTContext &Context, |
3151 | | Sema::MethodMatchStrategy strategy, |
3152 | | const Type *left, const Type *right); |
3153 | | |
3154 | | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
3155 | 1.31M | QualType leftQT, QualType rightQT) { |
3156 | 1.31M | const Type *left = |
3157 | 1.31M | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
3158 | 1.31M | const Type *right = |
3159 | 1.31M | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
3160 | | |
3161 | 1.31M | if (left == right) return true1.18M ; |
3162 | | |
3163 | | // If we're doing a strict match, the types have to match exactly. |
3164 | 122k | if (strategy == Sema::MMS_strict) return false119k ; |
3165 | | |
3166 | 2.58k | if (left->isIncompleteType() || right->isIncompleteType()) return false7 ; |
3167 | | |
3168 | | // Otherwise, use this absurdly complicated algorithm to try to |
3169 | | // validate the basic, low-level compatibility of the two types. |
3170 | | |
3171 | | // As a minimum, require the sizes and alignments to match. |
3172 | 2.57k | TypeInfo LeftTI = Context.getTypeInfo(left); |
3173 | 2.57k | TypeInfo RightTI = Context.getTypeInfo(right); |
3174 | 2.57k | if (LeftTI.Width != RightTI.Width) |
3175 | 20 | return false; |
3176 | | |
3177 | 2.55k | if (LeftTI.Align != RightTI.Align) |
3178 | 0 | return false; |
3179 | | |
3180 | | // Consider all the kinds of non-dependent canonical types: |
3181 | | // - functions and arrays aren't possible as return and parameter types |
3182 | | |
3183 | | // - vector types of equal size can be arbitrarily mixed |
3184 | 2.55k | if (isa<VectorType>(left)) return isa<VectorType>(right)0 ; |
3185 | 2.55k | if (isa<VectorType>(right)) return false0 ; |
3186 | | |
3187 | | // - references should only match references of identical type |
3188 | | // - structs, unions, and Objective-C objects must match more-or-less |
3189 | | // exactly |
3190 | | // - everything else should be a scalar |
3191 | 2.55k | if (!left->isScalarType() || !right->isScalarType()2.55k ) |
3192 | 2 | return tryMatchRecordTypes(Context, strategy, left, right); |
3193 | | |
3194 | | // Make scalars agree in kind, except count bools as chars, and group |
3195 | | // all non-member pointers together. |
3196 | 2.55k | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
3197 | 2.55k | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
3198 | 2.55k | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral0 ; |
3199 | 2.55k | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral0 ; |
3200 | 2.55k | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer2.54k ) |
3201 | 10 | leftSK = Type::STK_ObjCObjectPointer; |
3202 | 2.55k | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer2.53k ) |
3203 | 15 | rightSK = Type::STK_ObjCObjectPointer; |
3204 | | |
3205 | | // Note that data member pointers and function member pointers don't |
3206 | | // intermix because of the size differences. |
3207 | | |
3208 | 2.55k | return (leftSK == rightSK); |
3209 | 2.55k | } |
3210 | | |
3211 | | static bool tryMatchRecordTypes(ASTContext &Context, |
3212 | | Sema::MethodMatchStrategy strategy, |
3213 | 2 | const Type *lt, const Type *rt) { |
3214 | 2 | assert(lt && rt && lt != rt); |
3215 | | |
3216 | 2 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false0 ; |
3217 | 2 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
3218 | 2 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
3219 | | |
3220 | | // Require union-hood to match. |
3221 | 2 | if (left->isUnion() != right->isUnion()) return false0 ; |
3222 | | |
3223 | | // Require an exact match if either is non-POD. |
3224 | 2 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()0 ) || |
3225 | 2 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()0 )) |
3226 | 0 | return false; |
3227 | | |
3228 | | // Require size and alignment to match. |
3229 | 2 | TypeInfo LeftTI = Context.getTypeInfo(lt); |
3230 | 2 | TypeInfo RightTI = Context.getTypeInfo(rt); |
3231 | 2 | if (LeftTI.Width != RightTI.Width) |
3232 | 0 | return false; |
3233 | | |
3234 | 2 | if (LeftTI.Align != RightTI.Align) |
3235 | 0 | return false; |
3236 | | |
3237 | | // Require fields to match. |
3238 | 2 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
3239 | 2 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
3240 | 4 | for (; li != le && ri != re3 ; ++li, ++ri2 ) { |
3241 | 3 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
3242 | 1 | return false; |
3243 | 3 | } |
3244 | 1 | return (li == le && ri == re); |
3245 | 2 | } |
3246 | | |
3247 | | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
3248 | | /// returns true, or false, accordingly. |
3249 | | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
3250 | | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
3251 | | const ObjCMethodDecl *right, |
3252 | 929k | MethodMatchStrategy strategy) { |
3253 | 929k | if (!matchTypes(Context, strategy, left->getReturnType(), |
3254 | 929k | right->getReturnType())) |
3255 | 72.8k | return false; |
3256 | | |
3257 | | // If either is hidden, it is not considered to match. |
3258 | 856k | if (!left->isUnconditionallyVisible() || !right->isUnconditionallyVisible()856k ) |
3259 | 7 | return false; |
3260 | | |
3261 | 856k | if (left->isDirectMethod() != right->isDirectMethod()) |
3262 | 266 | return false; |
3263 | | |
3264 | 856k | if (getLangOpts().ObjCAutoRefCount && |
3265 | 856k | (9.97k left->hasAttr<NSReturnsRetainedAttr>() |
3266 | 9.97k | != right->hasAttr<NSReturnsRetainedAttr>() || |
3267 | 9.97k | left->hasAttr<NSConsumesSelfAttr>() |
3268 | 9.97k | != right->hasAttr<NSConsumesSelfAttr>())) |
3269 | 11 | return false; |
3270 | | |
3271 | 856k | ObjCMethodDecl::param_const_iterator |
3272 | 856k | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
3273 | 856k | re = right->param_end(); |
3274 | | |
3275 | 1.19M | for (; li != le && ri != re382k ; ++li, ++ri335k ) { |
3276 | 382k | assert(ri != right->param_end() && "Param mismatch"); |
3277 | 0 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
3278 | | |
3279 | 382k | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
3280 | 46.9k | return false; |
3281 | | |
3282 | 335k | if (getLangOpts().ObjCAutoRefCount && |
3283 | 335k | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()3.57k ) |
3284 | 6 | return false; |
3285 | 335k | } |
3286 | 809k | return true; |
3287 | 856k | } |
3288 | | |
3289 | | static bool isMethodContextSameForKindofLookup(ObjCMethodDecl *Method, |
3290 | 798k | ObjCMethodDecl *MethodInList) { |
3291 | 798k | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
3292 | 798k | auto *MethodInListProtocol = |
3293 | 798k | dyn_cast<ObjCProtocolDecl>(MethodInList->getDeclContext()); |
3294 | | // If this method belongs to a protocol but the method in list does not, or |
3295 | | // vice versa, we say the context is not the same. |
3296 | 798k | if ((MethodProtocol && !MethodInListProtocol585 ) || |
3297 | 798k | (798k !MethodProtocol798k && MethodInListProtocol798k )) |
3298 | 14.3k | return false; |
3299 | | |
3300 | 784k | if (MethodProtocol && MethodInListProtocol231 ) |
3301 | 231 | return true; |
3302 | | |
3303 | 784k | ObjCInterfaceDecl *MethodInterface = Method->getClassInterface(); |
3304 | 784k | ObjCInterfaceDecl *MethodInListInterface = |
3305 | 784k | MethodInList->getClassInterface(); |
3306 | 784k | return MethodInterface == MethodInListInterface; |
3307 | 784k | } |
3308 | | |
3309 | | void Sema::addMethodToGlobalList(ObjCMethodList *List, |
3310 | 1.13M | ObjCMethodDecl *Method) { |
3311 | | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
3312 | | // inside categories. |
3313 | 1.13M | if (ObjCCategoryDecl *CD = |
3314 | 1.13M | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
3315 | 257k | if (!CD->IsClassExtension() && List->getBits() < 2254k ) |
3316 | 243k | List->setBits(List->getBits() + 1); |
3317 | | |
3318 | | // If the list is empty, make it a singleton list. |
3319 | 1.13M | if (List->getMethod() == nullptr) { |
3320 | 918k | List->setMethod(Method); |
3321 | 918k | List->setNext(nullptr); |
3322 | 918k | return; |
3323 | 918k | } |
3324 | | |
3325 | | // We've seen a method with this name, see if we have already seen this type |
3326 | | // signature. |
3327 | 213k | ObjCMethodList *Previous = List; |
3328 | 213k | ObjCMethodList *ListWithSameDeclaration = nullptr; |
3329 | 1.26M | for (; List; Previous = List, List = List->getNext()1.05M ) { |
3330 | | // If we are building a module, keep all of the methods. |
3331 | 1.05M | if (getLangOpts().isCompilingModule()) |
3332 | 140k | continue; |
3333 | | |
3334 | 918k | bool SameDeclaration = MatchTwoMethodDeclarations(Method, |
3335 | 918k | List->getMethod()); |
3336 | | // Looking for method with a type bound requires the correct context exists. |
3337 | | // We need to insert a method into the list if the context is different. |
3338 | | // If the method's declaration matches the list |
3339 | | // a> the method belongs to a different context: we need to insert it, in |
3340 | | // order to emit the availability message, we need to prioritize over |
3341 | | // availability among the methods with the same declaration. |
3342 | | // b> the method belongs to the same context: there is no need to insert a |
3343 | | // new entry. |
3344 | | // If the method's declaration does not match the list, we insert it to the |
3345 | | // end. |
3346 | 918k | if (!SameDeclaration || |
3347 | 918k | !isMethodContextSameForKindofLookup(Method, List->getMethod())798k ) { |
3348 | | // Even if two method types do not match, we would like to say |
3349 | | // there is more than one declaration so unavailability/deprecated |
3350 | | // warning is not too noisy. |
3351 | 914k | if (!Method->isDefined()) |
3352 | 902k | List->setHasMoreThanOneDecl(true); |
3353 | | |
3354 | | // For methods with the same declaration, the one that is deprecated |
3355 | | // should be put in the front for better diagnostics. |
3356 | 914k | if (Method->isDeprecated() && SameDeclaration113k && |
3357 | 914k | !ListWithSameDeclaration111k && !List->getMethod()->isDeprecated()31.0k ) |
3358 | 7.12k | ListWithSameDeclaration = List; |
3359 | | |
3360 | 914k | if (Method->isUnavailable() && SameDeclaration81.1k && |
3361 | 914k | !ListWithSameDeclaration81.1k && |
3362 | 914k | List->getMethod()->getAvailability() < AR_Deprecated37.2k ) |
3363 | 3.67k | ListWithSameDeclaration = List; |
3364 | 914k | continue; |
3365 | 914k | } |
3366 | | |
3367 | 4.95k | ObjCMethodDecl *PrevObjCMethod = List->getMethod(); |
3368 | | |
3369 | | // Propagate the 'defined' bit. |
3370 | 4.95k | if (Method->isDefined()) |
3371 | 3.82k | PrevObjCMethod->setDefined(true); |
3372 | 1.13k | else { |
3373 | | // Objective-C doesn't allow an @interface for a class after its |
3374 | | // @implementation. So if Method is not defined and there already is |
3375 | | // an entry for this type signature, Method has to be for a different |
3376 | | // class than PrevObjCMethod. |
3377 | 1.13k | List->setHasMoreThanOneDecl(true); |
3378 | 1.13k | } |
3379 | | |
3380 | | // If a method is deprecated, push it in the global pool. |
3381 | | // This is used for better diagnostics. |
3382 | 4.95k | if (Method->isDeprecated()) { |
3383 | 50 | if (!PrevObjCMethod->isDeprecated()) |
3384 | 15 | List->setMethod(Method); |
3385 | 50 | } |
3386 | | // If the new method is unavailable, push it into global pool |
3387 | | // unless previous one is deprecated. |
3388 | 4.95k | if (Method->isUnavailable()) { |
3389 | 21 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
3390 | 0 | List->setMethod(Method); |
3391 | 21 | } |
3392 | | |
3393 | 4.95k | return; |
3394 | 918k | } |
3395 | | |
3396 | | // We have a new signature for an existing method - add it. |
3397 | | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
3398 | 208k | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
3399 | | |
3400 | | // We insert it right before ListWithSameDeclaration. |
3401 | 208k | if (ListWithSameDeclaration) { |
3402 | 10.7k | auto *List = new (Mem) ObjCMethodList(*ListWithSameDeclaration); |
3403 | | // FIXME: should we clear the other bits in ListWithSameDeclaration? |
3404 | 10.7k | ListWithSameDeclaration->setMethod(Method); |
3405 | 10.7k | ListWithSameDeclaration->setNext(List); |
3406 | 10.7k | return; |
3407 | 10.7k | } |
3408 | | |
3409 | 197k | Previous->setNext(new (Mem) ObjCMethodList(Method)); |
3410 | 197k | } |
3411 | | |
3412 | | /// Read the contents of the method pool for a given selector from |
3413 | | /// external storage. |
3414 | 73.4k | void Sema::ReadMethodPool(Selector Sel) { |
3415 | 73.4k | assert(ExternalSource && "We need an external AST source"); |
3416 | 0 | ExternalSource->ReadMethodPool(Sel); |
3417 | 73.4k | } |
3418 | | |
3419 | 1.78k | void Sema::updateOutOfDateSelector(Selector Sel) { |
3420 | 1.78k | if (!ExternalSource) |
3421 | 0 | return; |
3422 | 1.78k | ExternalSource->updateOutOfDateSelector(Sel); |
3423 | 1.78k | } |
3424 | | |
3425 | | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
3426 | 1.12M | bool instance) { |
3427 | | // Ignore methods of invalid containers. |
3428 | 1.12M | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
3429 | 36 | return; |
3430 | | |
3431 | 1.12M | if (ExternalSource) |
3432 | 51.0k | ReadMethodPool(Method->getSelector()); |
3433 | | |
3434 | 1.12M | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
3435 | 1.12M | if (Pos == MethodPool.end()) |
3436 | 908k | Pos = MethodPool |
3437 | 908k | .insert(std::make_pair(Method->getSelector(), |
3438 | 908k | GlobalMethodPool::Lists())) |
3439 | 908k | .first; |
3440 | | |
3441 | 1.12M | Method->setDefined(impl); |
3442 | | |
3443 | 1.12M | ObjCMethodList &Entry = instance ? Pos->second.first928k : Pos->second.second197k ; |
3444 | 1.12M | addMethodToGlobalList(&Entry, Method); |
3445 | 1.12M | } |
3446 | | |
3447 | | /// Determines if this is an "acceptable" loose mismatch in the global |
3448 | | /// method pool. This exists mostly as a hack to get around certain |
3449 | | /// global mismatches which we can't afford to make warnings / errors. |
3450 | | /// Really, what we want is a way to take a method out of the global |
3451 | | /// method pool. |
3452 | | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
3453 | 29 | ObjCMethodDecl *other) { |
3454 | 29 | if (!chosen->isInstanceMethod()) |
3455 | 0 | return false; |
3456 | | |
3457 | 29 | if (chosen->isDirectMethod() != other->isDirectMethod()) |
3458 | 1 | return false; |
3459 | | |
3460 | 28 | Selector sel = chosen->getSelector(); |
3461 | 28 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length"12 ) |
3462 | 28 | return false; |
3463 | | |
3464 | | // Don't complain about mismatches for -length if the method we |
3465 | | // chose has an integral result type. |
3466 | 0 | return (chosen->getReturnType()->isIntegerType()); |
3467 | 28 | } |
3468 | | |
3469 | | /// Return true if the given method is wthin the type bound. |
3470 | | static bool FilterMethodsByTypeBound(ObjCMethodDecl *Method, |
3471 | 13.9k | const ObjCObjectType *TypeBound) { |
3472 | 13.9k | if (!TypeBound) |
3473 | 13.9k | return true; |
3474 | | |
3475 | 23 | if (TypeBound->isObjCId()) |
3476 | | // FIXME: should we handle the case of bounding to id<A, B> differently? |
3477 | 3 | return true; |
3478 | | |
3479 | 20 | auto *BoundInterface = TypeBound->getInterface(); |
3480 | 20 | assert(BoundInterface && "unexpected object type!"); |
3481 | | |
3482 | | // Check if the Method belongs to a protocol. We should allow any method |
3483 | | // defined in any protocol, because any subclass could adopt the protocol. |
3484 | 0 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
3485 | 20 | if (MethodProtocol) { |
3486 | 1 | return true; |
3487 | 1 | } |
3488 | | |
3489 | | // If the Method belongs to a class, check if it belongs to the class |
3490 | | // hierarchy of the class bound. |
3491 | 19 | if (ObjCInterfaceDecl *MethodInterface = Method->getClassInterface()) { |
3492 | | // We allow methods declared within classes that are part of the hierarchy |
3493 | | // of the class bound (superclass of, subclass of, or the same as the class |
3494 | | // bound). |
3495 | 19 | return MethodInterface == BoundInterface || |
3496 | 19 | MethodInterface->isSuperClassOf(BoundInterface)15 || |
3497 | 19 | BoundInterface->isSuperClassOf(MethodInterface)14 ; |
3498 | 19 | } |
3499 | 0 | llvm_unreachable("unknown method context"); |
3500 | 0 | } |
3501 | | |
3502 | | /// We first select the type of the method: Instance or Factory, then collect |
3503 | | /// all methods with that type. |
3504 | | bool Sema::CollectMultipleMethodsInGlobalPool( |
3505 | | Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, |
3506 | | bool InstanceFirst, bool CheckTheOther, |
3507 | 4.62k | const ObjCObjectType *TypeBound) { |
3508 | 4.62k | if (ExternalSource) |
3509 | 798 | ReadMethodPool(Sel); |
3510 | | |
3511 | 4.62k | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
3512 | 4.62k | if (Pos == MethodPool.end()) |
3513 | 971 | return false; |
3514 | | |
3515 | | // Gather the non-hidden methods. |
3516 | 3.64k | ObjCMethodList &MethList = InstanceFirst ? Pos->second.first3.60k : |
3517 | 3.64k | Pos->second.second43 ; |
3518 | 17.6k | for (ObjCMethodList *M = &MethList; M; M = M->getNext()13.9k ) |
3519 | 13.9k | if (M->getMethod() && M->getMethod()->isUnconditionallyVisible()13.9k ) { |
3520 | 13.9k | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
3521 | 13.9k | Methods.push_back(M->getMethod()); |
3522 | 13.9k | } |
3523 | | |
3524 | | // Return if we find any method with the desired kind. |
3525 | 3.64k | if (!Methods.empty()) |
3526 | 3.57k | return Methods.size() > 1; |
3527 | | |
3528 | 73 | if (!CheckTheOther) |
3529 | 41 | return false; |
3530 | | |
3531 | | // Gather the other kind. |
3532 | 32 | ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second29 : |
3533 | 32 | Pos->second.first3 ; |
3534 | 64 | for (ObjCMethodList *M = &MethList2; M; M = M->getNext()32 ) |
3535 | 32 | if (M->getMethod() && M->getMethod()->isUnconditionallyVisible()29 ) { |
3536 | 29 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
3537 | 29 | Methods.push_back(M->getMethod()); |
3538 | 29 | } |
3539 | | |
3540 | 32 | return Methods.size() > 1; |
3541 | 73 | } |
3542 | | |
3543 | | bool Sema::AreMultipleMethodsInGlobalPool( |
3544 | | Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R, |
3545 | 3.56k | bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
3546 | | // Diagnose finding more than one method in global pool. |
3547 | 3.56k | SmallVector<ObjCMethodDecl *, 4> FilteredMethods; |
3548 | 3.56k | FilteredMethods.push_back(BestMethod); |
3549 | | |
3550 | 3.56k | for (auto *M : Methods) |
3551 | 13.8k | if (M != BestMethod && !M->hasAttr<UnavailableAttr>()10.3k ) |
3552 | 10.3k | FilteredMethods.push_back(M); |
3553 | | |
3554 | 3.56k | if (FilteredMethods.size() > 1) |
3555 | 2.30k | DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R, |
3556 | 2.30k | receiverIdOrClass); |
3557 | | |
3558 | 3.56k | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
3559 | | // Test for no method in the pool which should not trigger any warning by |
3560 | | // caller. |
3561 | 3.56k | if (Pos == MethodPool.end()) |
3562 | 0 | return true; |
3563 | 3.56k | ObjCMethodList &MethList = |
3564 | 3.56k | BestMethod->isInstanceMethod() ? Pos->second.first3.53k : Pos->second.second26 ; |
3565 | 3.56k | return MethList.hasMoreThanOneDecl(); |
3566 | 3.56k | } |
3567 | | |
3568 | | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
3569 | | bool receiverIdOrClass, |
3570 | 691 | bool instance) { |
3571 | 691 | if (ExternalSource) |
3572 | 36 | ReadMethodPool(Sel); |
3573 | | |
3574 | 691 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
3575 | 691 | if (Pos == MethodPool.end()) |
3576 | 264 | return nullptr; |
3577 | | |
3578 | | // Gather the non-hidden methods. |
3579 | 427 | ObjCMethodList &MethList = instance ? Pos->second.first341 : Pos->second.second86 ; |
3580 | 427 | SmallVector<ObjCMethodDecl *, 4> Methods; |
3581 | 478 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()51 ) { |
3582 | 427 | if (M->getMethod() && M->getMethod()->isUnconditionallyVisible()376 ) |
3583 | 376 | return M->getMethod(); |
3584 | 427 | } |
3585 | 51 | return nullptr; |
3586 | 427 | } |
3587 | | |
3588 | | void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, |
3589 | | Selector Sel, SourceRange R, |
3590 | 2.30k | bool receiverIdOrClass) { |
3591 | | // We found multiple methods, so we may have to complain. |
3592 | 2.30k | bool issueDiagnostic = false, issueError = false; |
3593 | | |
3594 | | // We support a warning which complains about *any* difference in |
3595 | | // method signature. |
3596 | 2.30k | bool strictSelectorMatch = |
3597 | 2.30k | receiverIdOrClass && |
3598 | 2.30k | !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin()); |
3599 | 2.30k | if (strictSelectorMatch) { |
3600 | 7 | for (unsigned I = 1, N = Methods.size(); I != N; ++I0 ) { |
3601 | 7 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
3602 | 7 | issueDiagnostic = true; |
3603 | 7 | break; |
3604 | 7 | } |
3605 | 7 | } |
3606 | 7 | } |
3607 | | |
3608 | | // If we didn't see any strict differences, we won't see any loose |
3609 | | // differences. In ARC, however, we also need to check for loose |
3610 | | // mismatches, because most of them are errors. |
3611 | 2.30k | if (!strictSelectorMatch || |
3612 | 2.30k | (7 issueDiagnostic7 && getLangOpts().ObjCAutoRefCount7 )) |
3613 | 12.5k | for (unsigned I = 1, N = Methods.size(); 2.29k I != N; ++I10.2k ) { |
3614 | | // This checks if the methods differ in type mismatch. |
3615 | 10.3k | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
3616 | 10.3k | !isAcceptableMethodMismatch(Methods[0], Methods[I])29 ) { |
3617 | 29 | issueDiagnostic = true; |
3618 | 29 | if (getLangOpts().ObjCAutoRefCount) |
3619 | 10 | issueError = true; |
3620 | 29 | break; |
3621 | 29 | } |
3622 | 10.3k | } |
3623 | | |
3624 | 2.30k | if (issueDiagnostic) { |
3625 | 36 | if (issueError) |
3626 | 10 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
3627 | 26 | else if (strictSelectorMatch) |
3628 | 7 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
3629 | 19 | else |
3630 | 19 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
3631 | | |
3632 | 36 | Diag(Methods[0]->getBeginLoc(), |
3633 | 36 | issueError ? diag::note_possibility10 : diag::note_using26 ) |
3634 | 36 | << Methods[0]->getSourceRange(); |
3635 | 78 | for (unsigned I = 1, N = Methods.size(); I != N; ++I42 ) { |
3636 | 42 | Diag(Methods[I]->getBeginLoc(), diag::note_also_found) |
3637 | 42 | << Methods[I]->getSourceRange(); |
3638 | 42 | } |
3639 | 36 | } |
3640 | 2.30k | } |
3641 | | |
3642 | 106 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
3643 | 106 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
3644 | 106 | if (Pos == MethodPool.end()) |
3645 | 1 | return nullptr; |
3646 | | |
3647 | 105 | GlobalMethodPool::Lists &Methods = Pos->second; |
3648 | 175 | for (const ObjCMethodList *Method = &Methods.first; Method; |
3649 | 105 | Method = Method->getNext()70 ) |
3650 | 121 | if (Method->getMethod() && |
3651 | 121 | (99 Method->getMethod()->isDefined()99 || |
3652 | 99 | Method->getMethod()->isPropertyAccessor()54 )) |
3653 | 51 | return Method->getMethod(); |
3654 | | |
3655 | 98 | for (const ObjCMethodList *Method = &Methods.second; 54 Method; |
3656 | 54 | Method = Method->getNext()44 ) |
3657 | 54 | if (Method->getMethod() && |
3658 | 54 | (23 Method->getMethod()->isDefined()23 || |
3659 | 23 | Method->getMethod()->isPropertyAccessor()13 )) |
3660 | 10 | return Method->getMethod(); |
3661 | 44 | return nullptr; |
3662 | 54 | } |
3663 | | |
3664 | | static void |
3665 | | HelperSelectorsForTypoCorrection( |
3666 | | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
3667 | 43.8k | StringRef Typo, const ObjCMethodDecl * Method) { |
3668 | 43.8k | const unsigned MaxEditDistance = 1; |
3669 | 43.8k | unsigned BestEditDistance = MaxEditDistance + 1; |
3670 | 43.8k | std::string MethodName = Method->getSelector().getAsString(); |
3671 | | |
3672 | 43.8k | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
3673 | 43.8k | if (MinPossibleEditDistance > 0 && |
3674 | 43.8k | Typo.size() / MinPossibleEditDistance < 142.0k ) |
3675 | 2.48k | return; |
3676 | 41.3k | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
3677 | 41.3k | if (EditDistance > MaxEditDistance) |
3678 | 41.3k | return; |
3679 | 61 | if (EditDistance == BestEditDistance) |
3680 | 0 | BestMethod.push_back(Method); |
3681 | 61 | else if (EditDistance < BestEditDistance) { |
3682 | 61 | BestMethod.clear(); |
3683 | 61 | BestMethod.push_back(Method); |
3684 | 61 | } |
3685 | 61 | } |
3686 | | |
3687 | | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
3688 | 45.9k | QualType ObjectType) { |
3689 | 45.9k | if (ObjectType.isNull()) |
3690 | 41.7k | return true; |
3691 | 4.25k | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
3692 | 728 | return true; |
3693 | 3.53k | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != |
3694 | 3.53k | nullptr; |
3695 | 4.25k | } |
3696 | | |
3697 | | const ObjCMethodDecl * |
3698 | | Sema::SelectorsForTypoCorrection(Selector Sel, |
3699 | 760 | QualType ObjectType) { |
3700 | 760 | unsigned NumArgs = Sel.getNumArgs(); |
3701 | 760 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
3702 | 760 | bool ObjectIsId = true, ObjectIsClass = true; |
3703 | 760 | if (ObjectType.isNull()) |
3704 | 128 | ObjectIsId = ObjectIsClass = false; |
3705 | 632 | else if (!ObjectType->isObjCObjectPointerType()) |
3706 | 102 | return nullptr; |
3707 | 530 | else if (const ObjCObjectPointerType *ObjCPtr = |
3708 | 530 | ObjectType->getAsObjCInterfacePointerType()) { |
3709 | 343 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
3710 | 343 | ObjectIsId = ObjectIsClass = false; |
3711 | 343 | } |
3712 | 187 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()38 ) |
3713 | 179 | ObjectIsClass = false; |
3714 | 8 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()2 ) |
3715 | 8 | ObjectIsId = false; |
3716 | 0 | else |
3717 | 0 | return nullptr; |
3718 | | |
3719 | 658 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
3720 | 99.5k | e = MethodPool.end(); b != e; b++98.8k ) { |
3721 | | // instance methods |
3722 | 218k | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()119k ) |
3723 | 119k | if (M->getMethod() && |
3724 | 119k | (M->getMethod()->getSelector().getNumArgs() == NumArgs)98.6k && |
3725 | 119k | (M->getMethod()->getSelector() != Sel)36.9k ) { |
3726 | 36.8k | if (ObjectIsId) |
3727 | 824 | Methods.push_back(M->getMethod()); |
3728 | 36.0k | else if (!ObjectIsClass && |
3729 | 36.0k | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
3730 | 36.0k | ObjectType)) |
3731 | 33.9k | Methods.push_back(M->getMethod()); |
3732 | 36.8k | } |
3733 | | // class methods |
3734 | 199k | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()100k ) |
3735 | 100k | if (M->getMethod() && |
3736 | 100k | (M->getMethod()->getSelector().getNumArgs() == NumArgs)24.1k && |
3737 | 100k | (M->getMethod()->getSelector() != Sel)10.9k ) { |
3738 | 10.8k | if (ObjectIsClass) |
3739 | 15 | Methods.push_back(M->getMethod()); |
3740 | 10.8k | else if (!ObjectIsId && |
3741 | 10.8k | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
3742 | 9.96k | ObjectType)) |
3743 | 9.07k | Methods.push_back(M->getMethod()); |
3744 | 10.8k | } |
3745 | 98.8k | } |
3746 | | |
3747 | 658 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
3748 | 44.5k | for (unsigned i = 0, e = Methods.size(); i < e; i++43.8k ) { |
3749 | 43.8k | HelperSelectorsForTypoCorrection(SelectedMethods, |
3750 | 43.8k | Sel.getAsString(), Methods[i]); |
3751 | 43.8k | } |
3752 | 658 | return (SelectedMethods.size() == 1) ? SelectedMethods[0]42 : nullptr616 ; |
3753 | 760 | } |
3754 | | |
3755 | | /// DiagnoseDuplicateIvars - |
3756 | | /// Check for duplicate ivars in the entire class at the start of |
3757 | | /// \@implementation. This becomes necesssary because class extension can |
3758 | | /// add ivars to a class in random order which will not be known until |
3759 | | /// class's \@implementation is seen. |
3760 | | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
3761 | 36.2k | ObjCInterfaceDecl *SID) { |
3762 | 134k | for (auto *Ivar : ID->ivars()) { |
3763 | 134k | if (Ivar->isInvalidDecl()) |
3764 | 2 | continue; |
3765 | 134k | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
3766 | 134k | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
3767 | 134k | if (prevIvar) { |
3768 | 4 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
3769 | 4 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
3770 | 4 | Ivar->setInvalidDecl(); |
3771 | 4 | } |
3772 | 134k | } |
3773 | 134k | } |
3774 | 36.2k | } |
3775 | | |
3776 | | /// Diagnose attempts to define ARC-__weak ivars when __weak is disabled. |
3777 | 4.85k | static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) { |
3778 | 4.85k | if (S.getLangOpts().ObjCWeak) return378 ; |
3779 | | |
3780 | 4.47k | for (auto ivar = ID->getClassInterface()->all_declared_ivar_begin(); |
3781 | 8.46k | ivar; ivar = ivar->getNextIvar()3.98k ) { |
3782 | 3.98k | if (ivar->isInvalidDecl()) continue16 ; |
3783 | 3.97k | if (ivar->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
3784 | 4 | if (S.getLangOpts().ObjCWeakRuntime) { |
3785 | 1 | S.Diag(ivar->getLocation(), diag::err_arc_weak_disabled); |
3786 | 3 | } else { |
3787 | 3 | S.Diag(ivar->getLocation(), diag::err_arc_weak_no_runtime); |
3788 | 3 | } |
3789 | 4 | } |
3790 | 3.97k | } |
3791 | 4.47k | } |
3792 | | |
3793 | | /// Diagnose attempts to use flexible array member with retainable object type. |
3794 | | static void DiagnoseRetainableFlexibleArrayMember(Sema &S, |
3795 | 4.85k | ObjCInterfaceDecl *ID) { |
3796 | 4.85k | if (!S.getLangOpts().ObjCAutoRefCount) |
3797 | 4.24k | return; |
3798 | | |
3799 | 1.48k | for (auto ivar = ID->all_declared_ivar_begin(); 611 ivar; |
3800 | 869 | ivar = ivar->getNextIvar()) { |
3801 | 869 | if (ivar->isInvalidDecl()) |
3802 | 1 | continue; |
3803 | 868 | QualType IvarTy = ivar->getType(); |
3804 | 868 | if (IvarTy->isIncompleteArrayType() && |
3805 | 868 | (IvarTy.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)4 && |
3806 | 868 | IvarTy->isObjCLifetimeType()3 ) { |
3807 | 1 | S.Diag(ivar->getLocation(), diag::err_flexible_array_arc_retainable); |
3808 | 1 | ivar->setInvalidDecl(); |
3809 | 1 | } |
3810 | 868 | } |
3811 | 611 | } |
3812 | | |
3813 | 272k | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
3814 | 272k | switch (CurContext->getDeclKind()) { |
3815 | 71.6k | case Decl::ObjCInterface: |
3816 | 71.6k | return Sema::OCK_Interface; |
3817 | 17.6k | case Decl::ObjCProtocol: |
3818 | 17.6k | return Sema::OCK_Protocol; |
3819 | 41.0k | case Decl::ObjCCategory: |
3820 | 41.0k | if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
3821 | 897 | return Sema::OCK_ClassExtension; |
3822 | 40.1k | return Sema::OCK_Category; |
3823 | 4.85k | case Decl::ObjCImplementation: |
3824 | 4.85k | return Sema::OCK_Implementation; |
3825 | 521 | case Decl::ObjCCategoryImpl: |
3826 | 521 | return Sema::OCK_CategoryImplementation; |
3827 | | |
3828 | 136k | default: |
3829 | 136k | return Sema::OCK_None; |
3830 | 272k | } |
3831 | 272k | } |
3832 | | |
3833 | 32.9k | static bool IsVariableSizedType(QualType T) { |
3834 | 32.9k | if (T->isIncompleteArrayType()) |
3835 | 13 | return true; |
3836 | 32.9k | const auto *RecordTy = T->getAs<RecordType>(); |
3837 | 32.9k | return (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()307 ); |
3838 | 32.9k | } |
3839 | | |
3840 | 135k | static void DiagnoseVariableSizedIvars(Sema &S, ObjCContainerDecl *OCD) { |
3841 | 135k | ObjCInterfaceDecl *IntfDecl = nullptr; |
3842 | 135k | ObjCInterfaceDecl::ivar_range Ivars = llvm::make_range( |
3843 | 135k | ObjCInterfaceDecl::ivar_iterator(), ObjCInterfaceDecl::ivar_iterator()); |
3844 | 135k | if ((IntfDecl = dyn_cast<ObjCInterfaceDecl>(OCD))) { |
3845 | 71.5k | Ivars = IntfDecl->ivars(); |
3846 | 71.5k | } else if (auto *64.1k ImplDecl64.1k = dyn_cast<ObjCImplementationDecl>(OCD)) { |
3847 | 4.85k | IntfDecl = ImplDecl->getClassInterface(); |
3848 | 4.85k | Ivars = ImplDecl->ivars(); |
3849 | 59.2k | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(OCD)) { |
3850 | 41.0k | if (CategoryDecl->IsClassExtension()) { |
3851 | 896 | IntfDecl = CategoryDecl->getClassInterface(); |
3852 | 896 | Ivars = CategoryDecl->ivars(); |
3853 | 896 | } |
3854 | 41.0k | } |
3855 | | |
3856 | | // Check if variable sized ivar is in interface and visible to subclasses. |
3857 | 135k | if (!isa<ObjCInterfaceDecl>(OCD)) { |
3858 | 64.1k | for (auto ivar : Ivars) { |
3859 | 2.53k | if (!ivar->isInvalidDecl() && IsVariableSizedType(ivar->getType())2.52k ) { |
3860 | 9 | S.Diag(ivar->getLocation(), diag::warn_variable_sized_ivar_visibility) |
3861 | 9 | << ivar->getDeclName() << ivar->getType(); |
3862 | 9 | } |
3863 | 2.53k | } |
3864 | 64.1k | } |
3865 | | |
3866 | | // Subsequent checks require interface decl. |
3867 | 135k | if (!IntfDecl) |
3868 | 58.3k | return; |
3869 | | |
3870 | | // Check if variable sized ivar is followed by another ivar. |
3871 | 217k | for (ObjCIvarDecl *ivar = IntfDecl->all_declared_ivar_begin(); 77.3k ivar; |
3872 | 140k | ivar = ivar->getNextIvar()) { |
3873 | 140k | if (ivar->isInvalidDecl() || !ivar->getNextIvar()140k ) |
3874 | 34.9k | continue; |
3875 | 105k | QualType IvarTy = ivar->getType(); |
3876 | 105k | bool IsInvalidIvar = false; |
3877 | 105k | if (IvarTy->isIncompleteArrayType()) { |
3878 | 11 | S.Diag(ivar->getLocation(), diag::err_flexible_array_not_at_end) |
3879 | 11 | << ivar->getDeclName() << IvarTy |
3880 | 11 | << TTK_Class; // Use "class" for Obj-C. |
3881 | 11 | IsInvalidIvar = true; |
3882 | 105k | } else if (const RecordType *RecordTy = IvarTy->getAs<RecordType>()) { |
3883 | 1.71k | if (RecordTy->getDecl()->hasFlexibleArrayMember()) { |
3884 | 5 | S.Diag(ivar->getLocation(), |
3885 | 5 | diag::err_objc_variable_sized_type_not_at_end) |
3886 | 5 | << ivar->getDeclName() << IvarTy; |
3887 | 5 | IsInvalidIvar = true; |
3888 | 5 | } |
3889 | 1.71k | } |
3890 | 105k | if (IsInvalidIvar) { |
3891 | 16 | S.Diag(ivar->getNextIvar()->getLocation(), |
3892 | 16 | diag::note_next_ivar_declaration) |
3893 | 16 | << ivar->getNextIvar()->getSynthesize(); |
3894 | 16 | ivar->setInvalidDecl(); |
3895 | 16 | } |
3896 | 105k | } |
3897 | | |
3898 | | // Check if ObjC container adds ivars after variable sized ivar in superclass. |
3899 | | // Perform the check only if OCD is the first container to declare ivars to |
3900 | | // avoid multiple warnings for the same ivar. |
3901 | 77.3k | ObjCIvarDecl *FirstIvar = |
3902 | 77.3k | (Ivars.begin() == Ivars.end()) ? nullptr43.5k : *Ivars.begin()33.7k ; |
3903 | 77.3k | if (FirstIvar && (FirstIvar == IntfDecl->all_declared_ivar_begin())33.7k ) { |
3904 | 33.4k | const ObjCInterfaceDecl *SuperClass = IntfDecl->getSuperClass(); |
3905 | 41.1k | while (SuperClass && SuperClass->ivar_empty()38.1k ) |
3906 | 7.68k | SuperClass = SuperClass->getSuperClass(); |
3907 | 33.4k | if (SuperClass) { |
3908 | 30.4k | auto IvarIter = SuperClass->ivar_begin(); |
3909 | 30.4k | std::advance(IvarIter, SuperClass->ivar_size() - 1); |
3910 | 30.4k | const ObjCIvarDecl *LastIvar = *IvarIter; |
3911 | 30.4k | if (IsVariableSizedType(LastIvar->getType())) { |
3912 | 6 | S.Diag(FirstIvar->getLocation(), |
3913 | 6 | diag::warn_superclass_variable_sized_type_not_at_end) |
3914 | 6 | << FirstIvar->getDeclName() << LastIvar->getDeclName() |
3915 | 6 | << LastIvar->getType() << SuperClass->getDeclName(); |
3916 | 6 | S.Diag(LastIvar->getLocation(), diag::note_entity_declared_at) |
3917 | 6 | << LastIvar->getDeclName(); |
3918 | 6 | } |
3919 | 30.4k | } |
3920 | 33.4k | } |
3921 | 77.3k | } |
3922 | | |
3923 | | static void DiagnoseCategoryDirectMembersProtocolConformance( |
3924 | | Sema &S, ObjCProtocolDecl *PDecl, ObjCCategoryDecl *CDecl); |
3925 | | |
3926 | | static void DiagnoseCategoryDirectMembersProtocolConformance( |
3927 | | Sema &S, ObjCCategoryDecl *CDecl, |
3928 | 43.6k | const llvm::iterator_range<ObjCProtocolList::iterator> &Protocols) { |
3929 | 43.6k | for (auto *PI : Protocols) |
3930 | 2.54k | DiagnoseCategoryDirectMembersProtocolConformance(S, PI, CDecl); |
3931 | 43.6k | } |
3932 | | |
3933 | | static void DiagnoseCategoryDirectMembersProtocolConformance( |
3934 | 2.54k | Sema &S, ObjCProtocolDecl *PDecl, ObjCCategoryDecl *CDecl) { |
3935 | 2.54k | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()3 ) |
3936 | 0 | PDecl = PDecl->getDefinition(); |
3937 | | |
3938 | 2.54k | llvm::SmallVector<const Decl *, 4> DirectMembers; |
3939 | 2.54k | const auto *IDecl = CDecl->getClassInterface(); |
3940 | 25.4k | for (auto *MD : PDecl->methods()) { |
3941 | 25.4k | if (!MD->isPropertyAccessor()) { |
3942 | 19.0k | if (const auto *CMD = |
3943 | 19.0k | IDecl->getMethod(MD->getSelector(), MD->isInstanceMethod())) { |
3944 | 43 | if (CMD->isDirectMethod()) |
3945 | 3 | DirectMembers.push_back(CMD); |
3946 | 43 | } |
3947 | 19.0k | } |
3948 | 25.4k | } |
3949 | 5.90k | for (auto *PD : PDecl->properties()) { |
3950 | 5.90k | if (const auto *CPD = IDecl->FindPropertyVisibleInPrimaryClass( |
3951 | 5.90k | PD->getIdentifier(), |
3952 | 5.90k | PD->isClassProperty() |
3953 | 5.90k | ? ObjCPropertyQueryKind::OBJC_PR_query_class |
3954 | 5.90k | : ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
3955 | 693 | if (CPD->isDirectProperty()) |
3956 | 2 | DirectMembers.push_back(CPD); |
3957 | 693 | } |
3958 | 5.90k | } |
3959 | 2.54k | if (!DirectMembers.empty()) { |
3960 | 5 | S.Diag(CDecl->getLocation(), diag::err_objc_direct_protocol_conformance) |
3961 | 5 | << CDecl->IsClassExtension() << CDecl << PDecl << IDecl; |
3962 | 5 | for (const auto *MD : DirectMembers) |
3963 | 5 | S.Diag(MD->getLocation(), diag::note_direct_member_here); |
3964 | 5 | return; |
3965 | 5 | } |
3966 | | |
3967 | | // Check on this protocols's referenced protocols, recursively. |
3968 | 2.53k | DiagnoseCategoryDirectMembersProtocolConformance(S, CDecl, |
3969 | 2.53k | PDecl->protocols()); |
3970 | 2.53k | } |
3971 | | |
3972 | | // Note: For class/category implementations, allMethods is always null. |
3973 | | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
3974 | 135k | ArrayRef<DeclGroupPtrTy> allTUVars) { |
3975 | 135k | if (getObjCContainerKind() == Sema::OCK_None) |
3976 | 0 | return nullptr; |
3977 | | |
3978 | 135k | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
3979 | | |
3980 | 0 | auto *OCD = cast<ObjCContainerDecl>(CurContext); |
3981 | 135k | Decl *ClassDecl = OCD; |
3982 | | |
3983 | 135k | bool isInterfaceDeclKind = |
3984 | 135k | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)64.1k |
3985 | 135k | || isa<ObjCProtocolDecl>(ClassDecl)23.0k ; |
3986 | 135k | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
3987 | | |
3988 | | // Make synthesized accessor stub functions visible. |
3989 | | // ActOnPropertyImplDecl() creates them as not visible in case |
3990 | | // they are overridden by an explicit method that is encountered |
3991 | | // later. |
3992 | 135k | if (auto *OID = dyn_cast<ObjCImplementationDecl>(CurContext)) { |
3993 | 4.85k | for (auto PropImpl : OID->property_impls()) { |
3994 | 2.99k | if (auto *Getter = PropImpl->getGetterMethodDecl()) |
3995 | 2.78k | if (Getter->isSynthesizedAccessorStub()) |
3996 | 2.67k | OID->addDecl(Getter); |
3997 | 2.99k | if (auto *Setter = PropImpl->getSetterMethodDecl()) |
3998 | 2.42k | if (Setter->isSynthesizedAccessorStub()) |
3999 | 2.34k | OID->addDecl(Setter); |
4000 | 2.99k | } |
4001 | 4.85k | } |
4002 | | |
4003 | | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
4004 | 135k | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
4005 | 135k | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
4006 | | |
4007 | 784k | for (unsigned i = 0, e = allMethods.size(); i != e; i++649k ) { |
4008 | 649k | ObjCMethodDecl *Method = |
4009 | 649k | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
4010 | | |
4011 | 649k | if (!Method) continue0 ; // Already issued a diagnostic. |
4012 | 649k | if (Method->isInstanceMethod()) { |
4013 | | /// Check for instance method of the same name with incompatible types |
4014 | 519k | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
4015 | 519k | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)19 |
4016 | 519k | : false519k ; |
4017 | 519k | if ((isInterfaceDeclKind && PrevMethod && !match19 ) |
4018 | 519k | || (519k checkIdenticalMethods519k && match0 )) { |
4019 | 5 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
4020 | 5 | << Method->getDeclName(); |
4021 | 5 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
4022 | 5 | Method->setInvalidDecl(); |
4023 | 519k | } else { |
4024 | 519k | if (PrevMethod) { |
4025 | 14 | Method->setAsRedeclaration(PrevMethod); |
4026 | 14 | if (!Context.getSourceManager().isInSystemHeader( |
4027 | 14 | Method->getLocation())) |
4028 | 13 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
4029 | 13 | << Method->getDeclName(); |
4030 | 14 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
4031 | 14 | } |
4032 | 519k | InsMap[Method->getSelector()] = Method; |
4033 | | /// The following allows us to typecheck messages to "id". |
4034 | 519k | AddInstanceMethodToGlobalPool(Method); |
4035 | 519k | } |
4036 | 519k | } else { |
4037 | | /// Check for class method of the same name with incompatible types |
4038 | 129k | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
4039 | 129k | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)2 |
4040 | 129k | : false129k ; |
4041 | 129k | if ((isInterfaceDeclKind && PrevMethod && !match2 ) |
4042 | 129k | || (129k checkIdenticalMethods129k && match0 )) { |
4043 | 2 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
4044 | 2 | << Method->getDeclName(); |
4045 | 2 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
4046 | 2 | Method->setInvalidDecl(); |
4047 | 129k | } else { |
4048 | 129k | if (PrevMethod) { |
4049 | 0 | Method->setAsRedeclaration(PrevMethod); |
4050 | 0 | if (!Context.getSourceManager().isInSystemHeader( |
4051 | 0 | Method->getLocation())) |
4052 | 0 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
4053 | 0 | << Method->getDeclName(); |
4054 | 0 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
4055 | 0 | } |
4056 | 129k | ClsMap[Method->getSelector()] = Method; |
4057 | 129k | AddFactoryMethodToGlobalPool(Method); |
4058 | 129k | } |
4059 | 129k | } |
4060 | 649k | } |
4061 | 135k | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
4062 | | // Nothing to do here. |
4063 | 71.5k | } else if (ObjCCategoryDecl *64.1k C64.1k = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
4064 | | // Categories are used to extend the class by declaring new methods. |
4065 | | // By the same token, they are also used to add new properties. No |
4066 | | // need to compare the added property to those in the class. |
4067 | | |
4068 | 41.0k | if (C->IsClassExtension()) { |
4069 | 896 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
4070 | 896 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
4071 | 896 | } |
4072 | | |
4073 | 41.0k | DiagnoseCategoryDirectMembersProtocolConformance(*this, C, C->protocols()); |
4074 | 41.0k | } |
4075 | 135k | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
4076 | 135k | if (CDecl->getIdentifier()) |
4077 | | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
4078 | | // user-defined setter/getter. It also synthesizes setter/getter methods |
4079 | | // and adds them to the DeclContext and global method pools. |
4080 | 134k | for (auto *I : CDecl->properties()) |
4081 | 351k | ProcessPropertyDecl(I); |
4082 | 135k | CDecl->setAtEndRange(AtEnd); |
4083 | 135k | } |
4084 | 135k | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
4085 | 4.85k | IC->setAtEndRange(AtEnd); |
4086 | 4.85k | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
4087 | | // Any property declared in a class extension might have user |
4088 | | // declared setter or getter in current class extension or one |
4089 | | // of the other class extensions. Mark them as synthesized as |
4090 | | // property will be synthesized when property with same name is |
4091 | | // seen in the @implementation. |
4092 | 4.85k | for (const auto *Ext : IDecl->visible_extensions()) { |
4093 | 478 | for (const auto *Property : Ext->instance_properties()) { |
4094 | | // Skip over properties declared @dynamic |
4095 | 232 | if (const ObjCPropertyImplDecl *PIDecl |
4096 | 232 | = IC->FindPropertyImplDecl(Property->getIdentifier(), |
4097 | 232 | Property->getQueryKind())) |
4098 | 219 | if (PIDecl->getPropertyImplementation() |
4099 | 219 | == ObjCPropertyImplDecl::Dynamic) |
4100 | 12 | continue; |
4101 | | |
4102 | 300 | for (const auto *Ext : IDecl->visible_extensions())220 { |
4103 | 300 | if (ObjCMethodDecl *GetterMethod = |
4104 | 300 | Ext->getInstanceMethod(Property->getGetterName())) |
4105 | 174 | GetterMethod->setPropertyAccessor(true); |
4106 | 300 | if (!Property->isReadOnly()) |
4107 | 280 | if (ObjCMethodDecl *SetterMethod |
4108 | 280 | = Ext->getInstanceMethod(Property->getSetterName())) |
4109 | 184 | SetterMethod->setPropertyAccessor(true); |
4110 | 300 | } |
4111 | 220 | } |
4112 | 478 | } |
4113 | 4.85k | ImplMethodsVsClassMethods(S, IC, IDecl); |
4114 | 4.85k | AtomicPropertySetterGetterRules(IC, IDecl); |
4115 | 4.85k | DiagnoseOwningPropertyGetterSynthesis(IC); |
4116 | 4.85k | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
4117 | 4.85k | if (IDecl->hasDesignatedInitializers()) |
4118 | 14 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
4119 | 4.85k | DiagnoseWeakIvars(*this, IC); |
4120 | 4.85k | DiagnoseRetainableFlexibleArrayMember(*this, IDecl); |
4121 | | |
4122 | 4.85k | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
4123 | 4.85k | if (IDecl->getSuperClass() == nullptr) { |
4124 | | // This class has no superclass, so check that it has been marked with |
4125 | | // __attribute((objc_root_class)). |
4126 | 2.63k | if (!HasRootClassAttr) { |
4127 | 2.51k | SourceLocation DeclLoc(IDecl->getLocation()); |
4128 | 2.51k | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
4129 | 2.51k | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
4130 | 2.51k | << IDecl->getIdentifier(); |
4131 | | // See if NSObject is in the current scope, and if it is, suggest |
4132 | | // adding " : NSObject " to the class declaration. |
4133 | 2.51k | NamedDecl *IF = LookupSingleName(TUScope, |
4134 | 2.51k | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
4135 | 2.51k | DeclLoc, LookupOrdinaryName); |
4136 | 2.51k | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
4137 | 2.51k | if (NSObjectDecl && NSObjectDecl->getDefinition()293 ) { |
4138 | 281 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
4139 | 281 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
4140 | 2.23k | } else { |
4141 | 2.23k | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
4142 | 2.23k | } |
4143 | 2.51k | } |
4144 | 2.63k | } else if (2.21k HasRootClassAttr2.21k ) { |
4145 | | // Complain that only root classes may have this attribute. |
4146 | 1 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
4147 | 1 | } |
4148 | | |
4149 | 4.85k | if (const ObjCInterfaceDecl *Super = IDecl->getSuperClass()) { |
4150 | | // An interface can subclass another interface with a |
4151 | | // objc_subclassing_restricted attribute when it has that attribute as |
4152 | | // well (because of interfaces imported from Swift). Therefore we have |
4153 | | // to check if we can subclass in the implementation as well. |
4154 | 2.21k | if (IDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
4155 | 2.21k | Super->hasAttr<ObjCSubclassingRestrictedAttr>()4 ) { |
4156 | 2 | Diag(IC->getLocation(), diag::err_restricted_superclass_mismatch); |
4157 | 2 | Diag(Super->getLocation(), diag::note_class_declared); |
4158 | 2 | } |
4159 | 2.21k | } |
4160 | | |
4161 | 4.85k | if (IDecl->hasAttr<ObjCClassStubAttr>()) |
4162 | 2 | Diag(IC->getLocation(), diag::err_implementation_of_class_stub); |
4163 | | |
4164 | 4.85k | if (LangOpts.ObjCRuntime.isNonFragile()) { |
4165 | 7.15k | while (IDecl->getSuperClass()) { |
4166 | 2.52k | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
4167 | 2.52k | IDecl = IDecl->getSuperClass(); |
4168 | 2.52k | } |
4169 | 4.63k | } |
4170 | 4.85k | } |
4171 | 4.85k | SetIvarInitializers(IC); |
4172 | 130k | } else if (ObjCCategoryImplDecl* CatImplClass = |
4173 | 130k | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
4174 | 521 | CatImplClass->setAtEndRange(AtEnd); |
4175 | | |
4176 | | // Find category interface decl and then check that all methods declared |
4177 | | // in this interface are implemented in the category @implementation. |
4178 | 521 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
4179 | 508 | if (ObjCCategoryDecl *Cat |
4180 | 508 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
4181 | 506 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
4182 | 506 | } |
4183 | 508 | } |
4184 | 130k | } else if (const auto *IntfDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
4185 | 71.5k | if (const ObjCInterfaceDecl *Super = IntfDecl->getSuperClass()) { |
4186 | 63.3k | if (!IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
4187 | 63.3k | Super->hasAttr<ObjCSubclassingRestrictedAttr>()63.3k ) { |
4188 | 2 | Diag(IntfDecl->getLocation(), diag::err_restricted_superclass_mismatch); |
4189 | 2 | Diag(Super->getLocation(), diag::note_class_declared); |
4190 | 2 | } |
4191 | 63.3k | } |
4192 | | |
4193 | 71.5k | if (IntfDecl->hasAttr<ObjCClassStubAttr>() && |
4194 | 71.5k | !IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>()6 ) |
4195 | 2 | Diag(IntfDecl->getLocation(), diag::err_class_stub_subclassing_mismatch); |
4196 | 71.5k | } |
4197 | 135k | DiagnoseVariableSizedIvars(*this, OCD); |
4198 | 135k | if (isInterfaceDeclKind) { |
4199 | | // Reject invalid vardecls. |
4200 | 175k | for (unsigned i = 0, e = allTUVars.size(); i != e; i++44.9k ) { |
4201 | 44.9k | DeclGroupRef DG = allTUVars[i].get(); |
4202 | 92.8k | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I47.8k ) |
4203 | 47.8k | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
4204 | 31.2k | if (!VDecl->hasExternalStorage()) |
4205 | 24 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
4206 | 31.2k | } |
4207 | 44.9k | } |
4208 | 130k | } |
4209 | 135k | ActOnObjCContainerFinishDefinition(); |
4210 | | |
4211 | 180k | for (unsigned i = 0, e = allTUVars.size(); i != e; i++44.9k ) { |
4212 | 44.9k | DeclGroupRef DG = allTUVars[i].get(); |
4213 | 92.8k | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I47.8k ) |
4214 | 47.8k | (*I)->setTopLevelDeclInObjCContainer(); |
4215 | 44.9k | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
4216 | 44.9k | } |
4217 | | |
4218 | 135k | ActOnDocumentableDecl(ClassDecl); |
4219 | 135k | return ClassDecl; |
4220 | 135k | } |
4221 | | |
4222 | | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
4223 | | /// objective-c's type qualifier from the parser version of the same info. |
4224 | | static Decl::ObjCDeclQualifier |
4225 | 1.62M | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
4226 | 1.62M | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
4227 | 1.62M | } |
4228 | | |
4229 | | /// Check whether the declared result type of the given Objective-C |
4230 | | /// method declaration is compatible with the method's class. |
4231 | | /// |
4232 | | static Sema::ResultTypeCompatibilityKind |
4233 | | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
4234 | 657k | ObjCInterfaceDecl *CurrentClass) { |
4235 | 657k | QualType ResultType = Method->getReturnType(); |
4236 | | |
4237 | | // If an Objective-C method inherits its related result type, then its |
4238 | | // declared result type must be compatible with its own class type. The |
4239 | | // declared result type is compatible if: |
4240 | 657k | if (const ObjCObjectPointerType *ResultObjectType |
4241 | 657k | = ResultType->getAs<ObjCObjectPointerType>()) { |
4242 | | // - it is id or qualified id, or |
4243 | 325k | if (ResultObjectType->isObjCIdType() || |
4244 | 325k | ResultObjectType->isObjCQualifiedIdType()168k ) |
4245 | 157k | return Sema::RTC_Compatible; |
4246 | | |
4247 | 167k | if (CurrentClass) { |
4248 | 163k | if (ObjCInterfaceDecl *ResultClass |
4249 | 163k | = ResultObjectType->getInterfaceDecl()) { |
4250 | | // - it is the same as the method's class type, or |
4251 | 160k | if (declaresSameEntity(CurrentClass, ResultClass)) |
4252 | 72.1k | return Sema::RTC_Compatible; |
4253 | | |
4254 | | // - it is a superclass of the method's class type |
4255 | 88.6k | if (ResultClass->isSuperClassOf(CurrentClass)) |
4256 | 1.65k | return Sema::RTC_Compatible; |
4257 | 88.6k | } |
4258 | 163k | } else { |
4259 | | // Any Objective-C pointer type might be acceptable for a protocol |
4260 | | // method; we just don't know. |
4261 | 3.82k | return Sema::RTC_Unknown; |
4262 | 3.82k | } |
4263 | 167k | } |
4264 | | |
4265 | 421k | return Sema::RTC_Incompatible; |
4266 | 657k | } |
4267 | | |
4268 | | namespace { |
4269 | | /// A helper class for searching for methods which a particular method |
4270 | | /// overrides. |
4271 | | class OverrideSearch { |
4272 | | public: |
4273 | | const ObjCMethodDecl *Method; |
4274 | | llvm::SmallSetVector<ObjCMethodDecl*, 4> Overridden; |
4275 | | bool Recursive; |
4276 | | |
4277 | | public: |
4278 | 1.12M | OverrideSearch(Sema &S, const ObjCMethodDecl *method) : Method(method) { |
4279 | 1.12M | Selector selector = method->getSelector(); |
4280 | | |
4281 | | // Bypass this search if we've never seen an instance/class method |
4282 | | // with this selector before. |
4283 | 1.12M | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
4284 | 1.12M | if (it == S.MethodPool.end()) { |
4285 | 538k | if (!S.getExternalSource()) return516k ; |
4286 | 21.5k | S.ReadMethodPool(selector); |
4287 | | |
4288 | 21.5k | it = S.MethodPool.find(selector); |
4289 | 21.5k | if (it == S.MethodPool.end()) |
4290 | 21.1k | return; |
4291 | 21.5k | } |
4292 | 588k | const ObjCMethodList &list = |
4293 | 588k | method->isInstanceMethod() ? it->second.first506k : it->second.second82.3k ; |
4294 | 588k | if (!list.getMethod()) return4.21k ; |
4295 | | |
4296 | 584k | const ObjCContainerDecl *container |
4297 | 584k | = cast<ObjCContainerDecl>(method->getDeclContext()); |
4298 | | |
4299 | | // Prevent the search from reaching this container again. This is |
4300 | | // important with categories, which override methods from the |
4301 | | // interface and each other. |
4302 | 584k | if (const ObjCCategoryDecl *Category = |
4303 | 584k | dyn_cast<ObjCCategoryDecl>(container)) { |
4304 | 81.7k | searchFromContainer(container); |
4305 | 81.7k | if (const ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
4306 | 81.7k | searchFromContainer(Interface); |
4307 | 502k | } else { |
4308 | 502k | searchFromContainer(container); |
4309 | 502k | } |
4310 | 584k | } |
4311 | | |
4312 | | typedef decltype(Overridden)::iterator iterator; |
4313 | 1.12M | iterator begin() const { return Overridden.begin(); } |
4314 | 1.12M | iterator end() const { return Overridden.end(); } |
4315 | | |
4316 | | private: |
4317 | 10.0M | void searchFromContainer(const ObjCContainerDecl *container) { |
4318 | 10.0M | if (container->isInvalidDecl()) return11 ; |
4319 | | |
4320 | 10.0M | switch (container->getDeclKind()) { |
4321 | 0 | #define OBJCCONTAINER(type, base) \ |
4322 | 10.0M | case Decl::type: \ |
4323 | 10.0M | searchFrom(cast<type##Decl>(container)); \ |
4324 | 10.0M | break; |
4325 | 0 | #define ABSTRACT_DECL(expansion) |
4326 | 0 | #define DECL(type, base) \ |
4327 | 0 | case Decl::type: |
4328 | 0 | #include "clang/AST/DeclNodes.inc" |
4329 | 0 | llvm_unreachable("not an ObjC container!"); |
4330 | 10.0M | } |
4331 | 10.0M | } |
4332 | | |
4333 | 1.86M | void searchFrom(const ObjCProtocolDecl *protocol) { |
4334 | 1.86M | if (!protocol->hasDefinition()) |
4335 | 3 | return; |
4336 | | |
4337 | | // A method in a protocol declaration overrides declarations from |
4338 | | // referenced ("parent") protocols. |
4339 | 1.86M | search(protocol->getReferencedProtocols()); |
4340 | 1.86M | } |
4341 | | |
4342 | 6.79M | void searchFrom(const ObjCCategoryDecl *category) { |
4343 | | // A method in a category declaration overrides declarations from |
4344 | | // the main class and from protocols the category references. |
4345 | | // The main class is handled in the constructor. |
4346 | 6.79M | search(category->getReferencedProtocols()); |
4347 | 6.79M | } |
4348 | | |
4349 | 374 | void searchFrom(const ObjCCategoryImplDecl *impl) { |
4350 | | // A method in a category definition that has a category |
4351 | | // declaration overrides declarations from the category |
4352 | | // declaration. |
4353 | 374 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
4354 | 374 | search(category); |
4355 | 374 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
4356 | 374 | search(Interface); |
4357 | | |
4358 | | // Otherwise it overrides declarations from the class. |
4359 | 374 | } else if (const auto *0 Interface0 = impl->getClassInterface()) { |
4360 | 0 | search(Interface); |
4361 | 0 | } |
4362 | 374 | } |
4363 | | |
4364 | 1.34M | void searchFrom(const ObjCInterfaceDecl *iface) { |
4365 | | // A method in a class declaration overrides declarations from |
4366 | 1.34M | if (!iface->hasDefinition()) |
4367 | 0 | return; |
4368 | | |
4369 | | // - categories, |
4370 | 1.34M | for (auto *Cat : iface->known_categories()) |
4371 | 6.80M | search(Cat); |
4372 | | |
4373 | | // - the super class, and |
4374 | 1.34M | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
4375 | 808k | search(super); |
4376 | | |
4377 | | // - any referenced protocols. |
4378 | 1.34M | search(iface->getReferencedProtocols()); |
4379 | 1.34M | } |
4380 | | |
4381 | 5.01k | void searchFrom(const ObjCImplementationDecl *impl) { |
4382 | | // A method in a class implementation overrides declarations from |
4383 | | // the class interface. |
4384 | 5.01k | if (const auto *Interface = impl->getClassInterface()) |
4385 | 5.01k | search(Interface); |
4386 | 5.01k | } |
4387 | | |
4388 | 10.0M | void search(const ObjCProtocolList &protocols) { |
4389 | 10.0M | for (const auto *Proto : protocols) |
4390 | 1.87M | search(Proto); |
4391 | 10.0M | } |
4392 | | |
4393 | 9.49M | void search(const ObjCContainerDecl *container) { |
4394 | | // Check for a method in this container which matches this selector. |
4395 | 9.49M | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
4396 | 9.49M | Method->isInstanceMethod(), |
4397 | 9.49M | /*AllowHidden=*/true); |
4398 | | |
4399 | | // If we find one, record it and bail out. |
4400 | 9.49M | if (meth) { |
4401 | 146k | Overridden.insert(meth); |
4402 | 146k | return; |
4403 | 146k | } |
4404 | | |
4405 | | // Otherwise, search for methods that a hypothetical method here |
4406 | | // would have overridden. |
4407 | | |
4408 | | // Note that we're now in a recursive case. |
4409 | 9.34M | Recursive = true; |
4410 | | |
4411 | 9.34M | searchFromContainer(container); |
4412 | 9.34M | } |
4413 | | }; |
4414 | | } // end anonymous namespace |
4415 | | |
4416 | | void Sema::CheckObjCMethodDirectOverrides(ObjCMethodDecl *method, |
4417 | 60.4k | ObjCMethodDecl *overridden) { |
4418 | 60.4k | if (overridden->isDirectMethod()) { |
4419 | 23 | const auto *attr = overridden->getAttr<ObjCDirectAttr>(); |
4420 | 23 | Diag(method->getLocation(), diag::err_objc_override_direct_method); |
4421 | 23 | Diag(attr->getLocation(), diag::note_previous_declaration); |
4422 | 60.4k | } else if (method->isDirectMethod()) { |
4423 | 8 | const auto *attr = method->getAttr<ObjCDirectAttr>(); |
4424 | 8 | Diag(attr->getLocation(), diag::err_objc_direct_on_override) |
4425 | 8 | << isa<ObjCProtocolDecl>(overridden->getDeclContext()); |
4426 | 8 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
4427 | 8 | } |
4428 | 60.4k | } |
4429 | | |
4430 | | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
4431 | | ObjCInterfaceDecl *CurrentClass, |
4432 | 1.12M | ResultTypeCompatibilityKind RTC) { |
4433 | 1.12M | if (!ObjCMethod) |
4434 | 0 | return; |
4435 | | // Search for overridden methods and merge information down from them. |
4436 | 1.12M | OverrideSearch overrides(*this, ObjCMethod); |
4437 | | // Keep track if the method overrides any method in the class's base classes, |
4438 | | // its protocols, or its categories' protocols; we will keep that info |
4439 | | // in the ObjCMethodDecl. |
4440 | | // For this info, a method in an implementation is not considered as |
4441 | | // overriding the same method in the interface or its categories. |
4442 | 1.12M | bool hasOverriddenMethodsInBaseOrProtocol = false; |
4443 | 1.12M | for (ObjCMethodDecl *overridden : overrides) { |
4444 | 145k | if (!hasOverriddenMethodsInBaseOrProtocol) { |
4445 | 145k | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
4446 | 145k | CurrentClass != overridden->getClassInterface()134k || |
4447 | 145k | overridden->isOverriding()85.3k ) { |
4448 | 60.4k | CheckObjCMethodDirectOverrides(ObjCMethod, overridden); |
4449 | 60.4k | hasOverriddenMethodsInBaseOrProtocol = true; |
4450 | 84.8k | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
4451 | | // OverrideSearch will return as "overridden" the same method in the |
4452 | | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
4453 | | // check whether a category of a base class introduced a method with the |
4454 | | // same selector, after the interface method declaration. |
4455 | | // To avoid unnecessary lookups in the majority of cases, we use the |
4456 | | // extra info bits in GlobalMethodPool to check whether there were any |
4457 | | // category methods with this selector. |
4458 | 3.50k | GlobalMethodPool::iterator It = |
4459 | 3.50k | MethodPool.find(ObjCMethod->getSelector()); |
4460 | 3.50k | if (It != MethodPool.end()) { |
4461 | 3.50k | ObjCMethodList &List = |
4462 | 3.50k | ObjCMethod->isInstanceMethod()? It->second.first2.92k : It->second.second586 ; |
4463 | 3.50k | unsigned CategCount = List.getBits(); |
4464 | 3.50k | if (CategCount > 0) { |
4465 | | // If the method is in a category we'll do lookup if there were at |
4466 | | // least 2 category methods recorded, otherwise only one will do. |
4467 | 332 | if (CategCount > 1 || |
4468 | 332 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())303 ) { |
4469 | 332 | OverrideSearch overrides(*this, overridden); |
4470 | 332 | for (ObjCMethodDecl *SuperOverridden : overrides) { |
4471 | 314 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
4472 | 314 | CurrentClass != SuperOverridden->getClassInterface()313 ) { |
4473 | 3 | CheckObjCMethodDirectOverrides(ObjCMethod, SuperOverridden); |
4474 | 3 | hasOverriddenMethodsInBaseOrProtocol = true; |
4475 | 3 | overridden->setOverriding(true); |
4476 | 3 | break; |
4477 | 3 | } |
4478 | 314 | } |
4479 | 332 | } |
4480 | 332 | } |
4481 | 3.50k | } |
4482 | 3.50k | } |
4483 | 145k | } |
4484 | | |
4485 | | // Propagate down the 'related result type' bit from overridden methods. |
4486 | 145k | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()106k ) |
4487 | 28.7k | ObjCMethod->setRelatedResultType(); |
4488 | | |
4489 | | // Then merge the declarations. |
4490 | 145k | mergeObjCMethodDecls(ObjCMethod, overridden); |
4491 | | |
4492 | 145k | if (ObjCMethod->isImplicit() && overridden->isImplicit()64.0k ) |
4493 | 63.5k | continue; // Conflicting properties are detected elsewhere. |
4494 | | |
4495 | | // Check for overriding methods |
4496 | 82.0k | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
4497 | 82.0k | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())38.5k ) |
4498 | 48.1k | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
4499 | 48.1k | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
4500 | | |
4501 | 82.0k | if (CurrentClass && overridden->getDeclContext() != CurrentClass82.0k && |
4502 | 82.0k | isa<ObjCInterfaceDecl>(overridden->getDeclContext())78.3k && |
4503 | 82.0k | !overridden->isImplicit()31.5k /* not meant for properties */) { |
4504 | 27.1k | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
4505 | 27.1k | E = ObjCMethod->param_end(); |
4506 | 27.1k | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
4507 | 27.1k | PrevE = overridden->param_end(); |
4508 | 44.6k | for (; ParamI != E && PrevI != PrevE17.8k ; ++ParamI, ++PrevI17.5k ) { |
4509 | 17.8k | assert(PrevI != overridden->param_end() && "Param mismatch"); |
4510 | 0 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
4511 | 17.8k | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
4512 | | // If type of argument of method in this class does not match its |
4513 | | // respective argument type in the super class method, issue warning; |
4514 | 17.8k | if (!Context.typesAreCompatible(T1, T2)) { |
4515 | 339 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
4516 | 339 | << T1 << T2; |
4517 | 339 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
4518 | 339 | break; |
4519 | 339 | } |
4520 | 17.8k | } |
4521 | 27.1k | } |
4522 | 82.0k | } |
4523 | | |
4524 | 1.12M | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
4525 | 1.12M | } |
4526 | | |
4527 | | /// Merge type nullability from for a redeclaration of the same entity, |
4528 | | /// producing the updated type of the redeclared entity. |
4529 | | static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc, |
4530 | | QualType type, |
4531 | | bool usesCSKeyword, |
4532 | | SourceLocation prevLoc, |
4533 | | QualType prevType, |
4534 | 8.25k | bool prevUsesCSKeyword) { |
4535 | | // Determine the nullability of both types. |
4536 | 8.25k | auto nullability = type->getNullability(S.Context); |
4537 | 8.25k | auto prevNullability = prevType->getNullability(S.Context); |
4538 | | |
4539 | | // Easy case: both have nullability. |
4540 | 8.25k | if (nullability.has_value() == prevNullability.has_value()) { |
4541 | | // Neither has nullability; continue. |
4542 | 8.22k | if (!nullability) |
4543 | 8.20k | return type; |
4544 | | |
4545 | | // The nullabilities are equivalent; do nothing. |
4546 | 18 | if (*nullability == *prevNullability) |
4547 | 16 | return type; |
4548 | | |
4549 | | // Complain about mismatched nullability. |
4550 | 2 | S.Diag(loc, diag::err_nullability_conflicting) |
4551 | 2 | << DiagNullabilityKind(*nullability, usesCSKeyword) |
4552 | 2 | << DiagNullabilityKind(*prevNullability, prevUsesCSKeyword); |
4553 | 2 | return type; |
4554 | 18 | } |
4555 | | |
4556 | | // If it's the redeclaration that has nullability, don't change anything. |
4557 | 28 | if (nullability) |
4558 | 2 | return type; |
4559 | | |
4560 | | // Otherwise, provide the result with the same nullability. |
4561 | 26 | return S.Context.getAttributedType( |
4562 | 26 | AttributedType::getNullabilityAttrKind(*prevNullability), |
4563 | 26 | type, type); |
4564 | 28 | } |
4565 | | |
4566 | | /// Merge information from the declaration of a method in the \@interface |
4567 | | /// (or a category/extension) into the corresponding method in the |
4568 | | /// @implementation (for a class or category). |
4569 | | static void mergeInterfaceMethodToImpl(Sema &S, |
4570 | | ObjCMethodDecl *method, |
4571 | 5.07k | ObjCMethodDecl *prevMethod) { |
4572 | | // Merge the objc_requires_super attribute. |
4573 | 5.07k | if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() && |
4574 | 5.07k | !method->hasAttr<ObjCRequiresSuperAttr>()80 ) { |
4575 | | // merge the attribute into implementation. |
4576 | 80 | method->addAttr( |
4577 | 80 | ObjCRequiresSuperAttr::CreateImplicit(S.Context, |
4578 | 80 | method->getLocation())); |
4579 | 80 | } |
4580 | | |
4581 | | // Merge nullability of the result type. |
4582 | 5.07k | QualType newReturnType |
4583 | 5.07k | = mergeTypeNullabilityForRedecl( |
4584 | 5.07k | S, method->getReturnTypeSourceRange().getBegin(), |
4585 | 5.07k | method->getReturnType(), |
4586 | 5.07k | method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
4587 | 5.07k | prevMethod->getReturnTypeSourceRange().getBegin(), |
4588 | 5.07k | prevMethod->getReturnType(), |
4589 | 5.07k | prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
4590 | 5.07k | method->setReturnType(newReturnType); |
4591 | | |
4592 | | // Handle each of the parameters. |
4593 | 5.07k | unsigned numParams = method->param_size(); |
4594 | 5.07k | unsigned numPrevParams = prevMethod->param_size(); |
4595 | 8.25k | for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i3.17k ) { |
4596 | 3.17k | ParmVarDecl *param = method->param_begin()[i]; |
4597 | 3.17k | ParmVarDecl *prevParam = prevMethod->param_begin()[i]; |
4598 | | |
4599 | | // Merge nullability. |
4600 | 3.17k | QualType newParamType |
4601 | 3.17k | = mergeTypeNullabilityForRedecl( |
4602 | 3.17k | S, param->getLocation(), param->getType(), |
4603 | 3.17k | param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
4604 | 3.17k | prevParam->getLocation(), prevParam->getType(), |
4605 | 3.17k | prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
4606 | 3.17k | param->setType(newParamType); |
4607 | 3.17k | } |
4608 | 5.07k | } |
4609 | | |
4610 | | /// Verify that the method parameters/return value have types that are supported |
4611 | | /// by the x86 target. |
4612 | | static void checkObjCMethodX86VectorTypes(Sema &SemaRef, |
4613 | 391 | const ObjCMethodDecl *Method) { |
4614 | 391 | assert(SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == |
4615 | 391 | llvm::Triple::x86 && |
4616 | 391 | "x86-specific check invoked for a different target"); |
4617 | 0 | SourceLocation Loc; |
4618 | 391 | QualType T; |
4619 | 391 | for (const ParmVarDecl *P : Method->parameters()) { |
4620 | 150 | if (P->getType()->isVectorType()) { |
4621 | 54 | Loc = P->getBeginLoc(); |
4622 | 54 | T = P->getType(); |
4623 | 54 | break; |
4624 | 54 | } |
4625 | 150 | } |
4626 | 391 | if (Loc.isInvalid()) { |
4627 | 337 | if (Method->getReturnType()->isVectorType()) { |
4628 | 23 | Loc = Method->getReturnTypeSourceRange().getBegin(); |
4629 | 23 | T = Method->getReturnType(); |
4630 | 23 | } else |
4631 | 314 | return; |
4632 | 337 | } |
4633 | | |
4634 | | // Vector parameters/return values are not supported by objc_msgSend on x86 in |
4635 | | // iOS < 9 and macOS < 10.11. |
4636 | 77 | const auto &Triple = SemaRef.getASTContext().getTargetInfo().getTriple(); |
4637 | 77 | VersionTuple AcceptedInVersion; |
4638 | 77 | if (Triple.getOS() == llvm::Triple::IOS) |
4639 | 17 | AcceptedInVersion = VersionTuple(/*Major=*/9); |
4640 | 60 | else if (Triple.isMacOSX()) |
4641 | 42 | AcceptedInVersion = VersionTuple(/*Major=*/10, /*Minor=*/11); |
4642 | 18 | else |
4643 | 18 | return; |
4644 | 59 | if (SemaRef.getASTContext().getTargetInfo().getPlatformMinVersion() >= |
4645 | 59 | AcceptedInVersion) |
4646 | 27 | return; |
4647 | 32 | SemaRef.Diag(Loc, diag::err_objc_method_unsupported_param_ret_type) |
4648 | 32 | << T << (Method->getReturnType()->isVectorType() ? /*return value*/ 18 |
4649 | 32 | : /*parameter*/ 024 ) |
4650 | 32 | << (Triple.isMacOSX() ? "macOS 10.11"24 : "iOS 9"8 ); |
4651 | 32 | } |
4652 | | |
4653 | 595k | static void mergeObjCDirectMembers(Sema &S, Decl *CD, ObjCMethodDecl *Method) { |
4654 | 595k | if (!Method->isDirectMethod() && !Method->hasAttr<UnavailableAttr>()595k && |
4655 | 595k | CD->hasAttr<ObjCDirectMembersAttr>()593k ) { |
4656 | 11 | Method->addAttr( |
4657 | 11 | ObjCDirectAttr::CreateImplicit(S.Context, Method->getLocation())); |
4658 | 11 | } |
4659 | 595k | } |
4660 | | |
4661 | | static void checkObjCDirectMethodClashes(Sema &S, ObjCInterfaceDecl *IDecl, |
4662 | | ObjCMethodDecl *Method, |
4663 | 595k | ObjCImplDecl *ImpDecl = nullptr) { |
4664 | 595k | auto Sel = Method->getSelector(); |
4665 | 595k | bool isInstance = Method->isInstanceMethod(); |
4666 | 595k | bool diagnosed = false; |
4667 | | |
4668 | 595k | auto diagClash = [&](const ObjCMethodDecl *IMD) { |
4669 | 201 | if (diagnosed || IMD->isImplicit()) |
4670 | 33 | return; |
4671 | 168 | if (Method->isDirectMethod() || IMD->isDirectMethod()166 ) { |
4672 | 3 | S.Diag(Method->getLocation(), diag::err_objc_direct_duplicate_decl) |
4673 | 3 | << Method->isDirectMethod() << /* method */ 0 << IMD->isDirectMethod() |
4674 | 3 | << Method->getDeclName(); |
4675 | 3 | S.Diag(IMD->getLocation(), diag::note_previous_declaration); |
4676 | 3 | diagnosed = true; |
4677 | 3 | } |
4678 | 168 | }; |
4679 | | |
4680 | | // Look for any other declaration of this method anywhere we can see in this |
4681 | | // compilation unit. |
4682 | | // |
4683 | | // We do not use IDecl->lookupMethod() because we have specific needs: |
4684 | | // |
4685 | | // - we absolutely do not need to walk protocols, because |
4686 | | // diag::err_objc_direct_on_protocol has already been emitted |
4687 | | // during parsing if there's a conflict, |
4688 | | // |
4689 | | // - when we do not find a match in a given @interface container, |
4690 | | // we need to attempt looking it up in the @implementation block if the |
4691 | | // translation unit sees it to find more clashes. |
4692 | | |
4693 | 595k | if (auto *IMD = IDecl->getMethod(Sel, isInstance)) |
4694 | 176 | diagClash(IMD); |
4695 | 595k | else if (auto *Impl = IDecl->getImplementation()) |
4696 | 2.69k | if (Impl != ImpDecl) |
4697 | 100 | if (auto *IMD = IDecl->getImplementation()->getMethod(Sel, isInstance)) |
4698 | 5 | diagClash(IMD); |
4699 | | |
4700 | 595k | for (const auto *Cat : IDecl->visible_categories()) |
4701 | 665k | if (auto *IMD = Cat->getMethod(Sel, isInstance)) |
4702 | 20 | diagClash(IMD); |
4703 | 665k | else if (auto CatImpl = Cat->getImplementation()) |
4704 | 158 | if (CatImpl != ImpDecl) |
4705 | 13 | if (auto *IMD = Cat->getMethod(Sel, isInstance)) |
4706 | 0 | diagClash(IMD); |
4707 | 595k | } |
4708 | | |
4709 | | Decl *Sema::ActOnMethodDeclaration( |
4710 | | Scope *S, SourceLocation MethodLoc, SourceLocation EndLoc, |
4711 | | tok::TokenKind MethodType, ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
4712 | | ArrayRef<SourceLocation> SelectorLocs, Selector Sel, |
4713 | | // optional arguments. The number of types/arguments is obtained |
4714 | | // from the Sel.getNumArgs(). |
4715 | | ObjCArgInfo *ArgInfo, DeclaratorChunk::ParamInfo *CParamInfo, |
4716 | | unsigned CNumArgs, // c-style args |
4717 | | const ParsedAttributesView &AttrList, tok::ObjCKeywordKind MethodDeclKind, |
4718 | 657k | bool isVariadic, bool MethodDefinition) { |
4719 | | // Make sure we can establish a context for the method. |
4720 | 657k | if (!CurContext->isObjCContainer()) { |
4721 | 25 | Diag(MethodLoc, diag::err_missing_method_context); |
4722 | 25 | return nullptr; |
4723 | 25 | } |
4724 | | |
4725 | 657k | Decl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
4726 | 657k | QualType resultDeclType; |
4727 | | |
4728 | 657k | bool HasRelatedResultType = false; |
4729 | 657k | TypeSourceInfo *ReturnTInfo = nullptr; |
4730 | 657k | if (ReturnType) { |
4731 | 655k | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
4732 | | |
4733 | 655k | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
4734 | 4 | return nullptr; |
4735 | | |
4736 | 655k | QualType bareResultType = resultDeclType; |
4737 | 655k | (void)AttributedType::stripOuterNullability(bareResultType); |
4738 | 655k | HasRelatedResultType = (bareResultType == Context.getObjCInstanceType()); |
4739 | 655k | } else { // get the type for "id". |
4740 | 1.64k | resultDeclType = Context.getObjCIdType(); |
4741 | 1.64k | Diag(MethodLoc, diag::warn_missing_method_return_type) |
4742 | 1.64k | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
4743 | 1.64k | } |
4744 | | |
4745 | 657k | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
4746 | 657k | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
4747 | 657k | MethodType == tok::minus, isVariadic, |
4748 | 657k | /*isPropertyAccessor=*/false, /*isSynthesizedAccessorStub=*/false, |
4749 | 657k | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
4750 | 657k | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional38.6k |
4751 | 657k | : ObjCMethodDecl::Required618k , |
4752 | 657k | HasRelatedResultType); |
4753 | | |
4754 | 657k | SmallVector<ParmVarDecl*, 16> Params; |
4755 | | |
4756 | 1.62M | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i969k ) { |
4757 | 969k | QualType ArgType; |
4758 | 969k | TypeSourceInfo *DI; |
4759 | | |
4760 | 969k | if (!ArgInfo[i].Type) { |
4761 | 135 | ArgType = Context.getObjCIdType(); |
4762 | 135 | DI = nullptr; |
4763 | 969k | } else { |
4764 | 969k | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
4765 | 969k | } |
4766 | | |
4767 | 969k | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
4768 | 969k | LookupOrdinaryName, forRedeclarationInCurContext()); |
4769 | 969k | LookupName(R, S); |
4770 | 969k | if (R.isSingleResult()) { |
4771 | 11.2k | NamedDecl *PrevDecl = R.getFoundDecl(); |
4772 | 11.2k | if (S->isDeclScope(PrevDecl)) { |
4773 | 8 | Diag(ArgInfo[i].NameLoc, |
4774 | 8 | (MethodDefinition ? diag::warn_method_param_redefinition1 |
4775 | 8 | : diag::warn_method_param_declaration7 )) |
4776 | 8 | << ArgInfo[i].Name; |
4777 | 8 | Diag(PrevDecl->getLocation(), |
4778 | 8 | diag::note_previous_declaration); |
4779 | 8 | } |
4780 | 11.2k | } |
4781 | | |
4782 | 969k | SourceLocation StartLoc = DI |
4783 | 969k | ? DI->getTypeLoc().getBeginLoc()969k |
4784 | 969k | : ArgInfo[i].NameLoc135 ; |
4785 | | |
4786 | 969k | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
4787 | 969k | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
4788 | 969k | ArgType, DI, SC_None); |
4789 | | |
4790 | 969k | Param->setObjCMethodScopeInfo(i); |
4791 | | |
4792 | 969k | Param->setObjCDeclQualifier( |
4793 | 969k | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
4794 | | |
4795 | | // Apply the attributes to the parameter. |
4796 | 969k | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
4797 | 969k | AddPragmaAttributes(TUScope, Param); |
4798 | | |
4799 | 969k | if (Param->hasAttr<BlocksAttr>()) { |
4800 | 4 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
4801 | 4 | Param->setInvalidDecl(); |
4802 | 4 | } |
4803 | 969k | S->AddDecl(Param); |
4804 | 969k | IdResolver.AddDecl(Param); |
4805 | | |
4806 | 969k | Params.push_back(Param); |
4807 | 969k | } |
4808 | | |
4809 | 657k | for (unsigned i = 0, e = CNumArgs; i != e; ++i15 ) { |
4810 | 15 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
4811 | 15 | QualType ArgType = Param->getType(); |
4812 | 15 | if (ArgType.isNull()) |
4813 | 0 | ArgType = Context.getObjCIdType(); |
4814 | 15 | else |
4815 | | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
4816 | 15 | ArgType = Context.getAdjustedParameterType(ArgType); |
4817 | | |
4818 | 15 | Param->setDeclContext(ObjCMethod); |
4819 | 15 | Params.push_back(Param); |
4820 | 15 | } |
4821 | | |
4822 | 657k | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
4823 | 657k | ObjCMethod->setObjCDeclQualifier( |
4824 | 657k | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
4825 | | |
4826 | 657k | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
4827 | 657k | AddPragmaAttributes(TUScope, ObjCMethod); |
4828 | | |
4829 | | // Add the method now. |
4830 | 657k | const ObjCMethodDecl *PrevMethod = nullptr; |
4831 | 657k | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
4832 | 7.82k | if (MethodType == tok::minus) { |
4833 | 6.59k | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
4834 | 6.59k | ImpDecl->addInstanceMethod(ObjCMethod); |
4835 | 6.59k | } else { |
4836 | 1.22k | PrevMethod = ImpDecl->getClassMethod(Sel); |
4837 | 1.22k | ImpDecl->addClassMethod(ObjCMethod); |
4838 | 1.22k | } |
4839 | | |
4840 | | // If this method overrides a previous @synthesize declaration, |
4841 | | // register it with the property. Linear search through all |
4842 | | // properties here, because the autosynthesized stub hasn't been |
4843 | | // made visible yet, so it can be overridden by a later |
4844 | | // user-specified implementation. |
4845 | 7.82k | for (ObjCPropertyImplDecl *PropertyImpl : ImpDecl->property_impls()) { |
4846 | 3.13k | if (auto *Setter = PropertyImpl->getSetterMethodDecl()) |
4847 | 2.32k | if (Setter->getSelector() == Sel && |
4848 | 2.32k | Setter->isInstanceMethod() == ObjCMethod->isInstanceMethod()47 ) { |
4849 | 47 | assert(Setter->isSynthesizedAccessorStub() && "autosynth stub expected"); |
4850 | 0 | PropertyImpl->setSetterMethodDecl(ObjCMethod); |
4851 | 47 | } |
4852 | 3.13k | if (auto *Getter = PropertyImpl->getGetterMethodDecl()) |
4853 | 2.94k | if (Getter->getSelector() == Sel && |
4854 | 2.94k | Getter->isInstanceMethod() == ObjCMethod->isInstanceMethod()48 ) { |
4855 | 47 | assert(Getter->isSynthesizedAccessorStub() && "autosynth stub expected"); |
4856 | 0 | PropertyImpl->setGetterMethodDecl(ObjCMethod); |
4857 | 47 | break; |
4858 | 47 | } |
4859 | 3.13k | } |
4860 | | |
4861 | | // A method is either tagged direct explicitly, or inherits it from its |
4862 | | // canonical declaration. |
4863 | | // |
4864 | | // We have to do the merge upfront and not in mergeInterfaceMethodToImpl() |
4865 | | // because IDecl->lookupMethod() returns more possible matches than just |
4866 | | // the canonical declaration. |
4867 | 7.82k | if (!ObjCMethod->isDirectMethod()) { |
4868 | 7.76k | const ObjCMethodDecl *CanonicalMD = ObjCMethod->getCanonicalDecl(); |
4869 | 7.76k | if (CanonicalMD->isDirectMethod()) { |
4870 | 40 | const auto *attr = CanonicalMD->getAttr<ObjCDirectAttr>(); |
4871 | 40 | ObjCMethod->addAttr( |
4872 | 40 | ObjCDirectAttr::CreateImplicit(Context, attr->getLocation())); |
4873 | 40 | } |
4874 | 7.76k | } |
4875 | | |
4876 | | // Merge information from the @interface declaration into the |
4877 | | // @implementation. |
4878 | 7.82k | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) { |
4879 | 7.81k | if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
4880 | 7.81k | ObjCMethod->isInstanceMethod())) { |
4881 | 5.07k | mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD); |
4882 | | |
4883 | | // The Idecl->lookupMethod() above will find declarations for ObjCMethod |
4884 | | // in one of these places: |
4885 | | // |
4886 | | // (1) the canonical declaration in an @interface container paired |
4887 | | // with the ImplDecl, |
4888 | | // (2) non canonical declarations in @interface not paired with the |
4889 | | // ImplDecl for the same Class, |
4890 | | // (3) any superclass container. |
4891 | | // |
4892 | | // Direct methods only allow for canonical declarations in the matching |
4893 | | // container (case 1). |
4894 | | // |
4895 | | // Direct methods overriding a superclass declaration (case 3) is |
4896 | | // handled during overrides checks in CheckObjCMethodOverrides(). |
4897 | | // |
4898 | | // We deal with same-class container mismatches (Case 2) here. |
4899 | 5.07k | if (IDecl == IMD->getClassInterface()) { |
4900 | 4.06k | auto diagContainerMismatch = [&] { |
4901 | 16 | int decl = 0, impl = 0; |
4902 | | |
4903 | 16 | if (auto *Cat = dyn_cast<ObjCCategoryDecl>(IMD->getDeclContext())) |
4904 | 12 | decl = Cat->IsClassExtension() ? 14 : 28 ; |
4905 | | |
4906 | 16 | if (isa<ObjCCategoryImplDecl>(ImpDecl)) |
4907 | 12 | impl = 1 + (decl != 0); |
4908 | | |
4909 | 16 | Diag(ObjCMethod->getLocation(), |
4910 | 16 | diag::err_objc_direct_impl_decl_mismatch) |
4911 | 16 | << decl << impl; |
4912 | 16 | Diag(IMD->getLocation(), diag::note_previous_declaration); |
4913 | 16 | }; |
4914 | | |
4915 | 4.06k | if (ObjCMethod->isDirectMethod()) { |
4916 | 57 | const auto *attr = ObjCMethod->getAttr<ObjCDirectAttr>(); |
4917 | 57 | if (ObjCMethod->getCanonicalDecl() != IMD) { |
4918 | 8 | diagContainerMismatch(); |
4919 | 49 | } else if (!IMD->isDirectMethod()) { |
4920 | 5 | Diag(attr->getLocation(), diag::err_objc_direct_missing_on_decl); |
4921 | 5 | Diag(IMD->getLocation(), diag::note_previous_declaration); |
4922 | 5 | } |
4923 | 4.01k | } else if (IMD->isDirectMethod()) { |
4924 | 8 | const auto *attr = IMD->getAttr<ObjCDirectAttr>(); |
4925 | 8 | if (ObjCMethod->getCanonicalDecl() != IMD) { |
4926 | 8 | diagContainerMismatch(); |
4927 | 8 | } else { |
4928 | 0 | ObjCMethod->addAttr( |
4929 | 0 | ObjCDirectAttr::CreateImplicit(Context, attr->getLocation())); |
4930 | 0 | } |
4931 | 8 | } |
4932 | 4.06k | } |
4933 | | |
4934 | | // Warn about defining -dealloc in a category. |
4935 | 5.07k | if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding()359 && |
4936 | 5.07k | ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc20 ) { |
4937 | 2 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
4938 | 2 | << ObjCMethod->getDeclName(); |
4939 | 2 | } |
4940 | 5.07k | } else { |
4941 | 2.73k | mergeObjCDirectMembers(*this, ClassDecl, ObjCMethod); |
4942 | 2.73k | checkObjCDirectMethodClashes(*this, IDecl, ObjCMethod, ImpDecl); |
4943 | 2.73k | } |
4944 | | |
4945 | | // Warn if a method declared in a protocol to which a category or |
4946 | | // extension conforms is non-escaping and the implementation's method is |
4947 | | // escaping. |
4948 | 7.81k | for (auto *C : IDecl->visible_categories()) |
4949 | 1.85k | for (auto &P : C->protocols()) |
4950 | 108 | if (auto *IMD = P->lookupMethod(ObjCMethod->getSelector(), |
4951 | 108 | ObjCMethod->isInstanceMethod())) { |
4952 | 15 | assert(ObjCMethod->parameters().size() == |
4953 | 15 | IMD->parameters().size() && |
4954 | 15 | "Methods have different number of parameters"); |
4955 | 0 | auto OI = IMD->param_begin(), OE = IMD->param_end(); |
4956 | 15 | auto NI = ObjCMethod->param_begin(); |
4957 | 21 | for (; OI != OE; ++OI, ++NI6 ) |
4958 | 6 | diagnoseNoescape(*NI, *OI, C, P, *this); |
4959 | 15 | } |
4960 | 7.81k | } |
4961 | 649k | } else { |
4962 | 649k | if (!isa<ObjCProtocolDecl>(ClassDecl)) { |
4963 | 592k | mergeObjCDirectMembers(*this, ClassDecl, ObjCMethod); |
4964 | | |
4965 | 592k | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
4966 | 592k | if (!IDecl) |
4967 | 201k | IDecl = cast<ObjCCategoryDecl>(ClassDecl)->getClassInterface(); |
4968 | | // For valid code, we should always know the primary interface |
4969 | | // declaration by now, however for invalid code we'll keep parsing |
4970 | | // but we won't find the primary interface and IDecl will be nil. |
4971 | 592k | if (IDecl) |
4972 | 592k | checkObjCDirectMethodClashes(*this, IDecl, ObjCMethod); |
4973 | 592k | } |
4974 | | |
4975 | 649k | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
4976 | 649k | } |
4977 | | |
4978 | 657k | if (PrevMethod) { |
4979 | | // You can never have two method definitions with the same name. |
4980 | 3 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
4981 | 3 | << ObjCMethod->getDeclName(); |
4982 | 3 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
4983 | 3 | ObjCMethod->setInvalidDecl(); |
4984 | 3 | return ObjCMethod; |
4985 | 3 | } |
4986 | | |
4987 | | // If this Objective-C method does not have a related result type, but we |
4988 | | // are allowed to infer related result types, try to do so based on the |
4989 | | // method family. |
4990 | 657k | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
4991 | 657k | if (!CurrentClass) { |
4992 | 266k | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
4993 | 201k | CurrentClass = Cat->getClassInterface(); |
4994 | 64.2k | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
4995 | 7.81k | CurrentClass = Impl->getClassInterface(); |
4996 | 56.4k | else if (ObjCCategoryImplDecl *CatImpl |
4997 | 56.4k | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
4998 | 0 | CurrentClass = CatImpl->getClassInterface(); |
4999 | 266k | } |
5000 | | |
5001 | 657k | ResultTypeCompatibilityKind RTC |
5002 | 657k | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
5003 | | |
5004 | 657k | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
5005 | | |
5006 | 657k | bool ARCError = false; |
5007 | 657k | if (getLangOpts().ObjCAutoRefCount) |
5008 | 18.7k | ARCError = CheckARCMethodDecl(ObjCMethod); |
5009 | | |
5010 | | // Infer the related result type when possible. |
5011 | 657k | if (!ARCError && RTC == Sema::RTC_Compatible657k && |
5012 | 657k | !ObjCMethod->hasRelatedResultType()231k && |
5013 | 657k | LangOpts.ObjCInferRelatedResultType118k ) { |
5014 | 118k | bool InferRelatedResultType = false; |
5015 | 118k | switch (ObjCMethod->getMethodFamily()) { |
5016 | 102k | case OMF_None: |
5017 | 104k | case OMF_copy: |
5018 | 104k | case OMF_dealloc: |
5019 | 104k | case OMF_finalize: |
5020 | 105k | case OMF_mutableCopy: |
5021 | 105k | case OMF_release: |
5022 | 105k | case OMF_retainCount: |
5023 | 105k | case OMF_initialize: |
5024 | 106k | case OMF_performSelector: |
5025 | 106k | break; |
5026 | | |
5027 | 976 | case OMF_alloc: |
5028 | 1.61k | case OMF_new: |
5029 | 1.61k | InferRelatedResultType = ObjCMethod->isClassMethod(); |
5030 | 1.61k | break; |
5031 | | |
5032 | 10.4k | case OMF_init: |
5033 | 10.6k | case OMF_autorelease: |
5034 | 10.8k | case OMF_retain: |
5035 | 10.8k | case OMF_self: |
5036 | 10.8k | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
5037 | 10.8k | break; |
5038 | 118k | } |
5039 | | |
5040 | 118k | if (InferRelatedResultType && |
5041 | 118k | !ObjCMethod->getReturnType()->isObjCIndependentClassType()12.1k ) |
5042 | 12.1k | ObjCMethod->setRelatedResultType(); |
5043 | 118k | } |
5044 | | |
5045 | 657k | if (MethodDefinition && |
5046 | 657k | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x867.81k ) |
5047 | 391 | checkObjCMethodX86VectorTypes(*this, ObjCMethod); |
5048 | | |
5049 | | // + load method cannot have availability attributes. It get called on |
5050 | | // startup, so it has to have the availability of the deployment target. |
5051 | 657k | if (const auto *attr = ObjCMethod->getAttr<AvailabilityAttr>()) { |
5052 | 194k | if (ObjCMethod->isClassMethod() && |
5053 | 194k | ObjCMethod->getSelector().getAsString() == "load"42.5k ) { |
5054 | 2 | Diag(attr->getLocation(), diag::warn_availability_on_static_initializer) |
5055 | 2 | << 0; |
5056 | 2 | ObjCMethod->dropAttr<AvailabilityAttr>(); |
5057 | 2 | } |
5058 | 194k | } |
5059 | | |
5060 | | // Insert the invisible arguments, self and _cmd! |
5061 | 657k | ObjCMethod->createImplicitParams(Context, ObjCMethod->getClassInterface()); |
5062 | | |
5063 | 657k | ActOnDocumentableDecl(ObjCMethod); |
5064 | | |
5065 | 657k | return ObjCMethod; |
5066 | 657k | } |
5067 | | |
5068 | 273k | bool Sema::CheckObjCDeclScope(Decl *D) { |
5069 | | // Following is also an error. But it is caused by a missing @end |
5070 | | // and diagnostic is issued elsewhere. |
5071 | 273k | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
5072 | 4 | return false; |
5073 | | |
5074 | | // If we switched context to translation unit while we are still lexically in |
5075 | | // an objc container, it means the parser missed emitting an error. |
5076 | 273k | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
5077 | 273k | return false; |
|