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