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