/src/llvm-project/clang/lib/AST/Decl.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Decl.cpp - Declaration AST Node Implementation ---------------------===// |
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 the Decl subclasses. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/Decl.h" |
14 | | #include "Linkage.h" |
15 | | #include "clang/AST/ASTContext.h" |
16 | | #include "clang/AST/ASTDiagnostic.h" |
17 | | #include "clang/AST/ASTLambda.h" |
18 | | #include "clang/AST/ASTMutationListener.h" |
19 | | #include "clang/AST/Attr.h" |
20 | | #include "clang/AST/CanonicalType.h" |
21 | | #include "clang/AST/DeclBase.h" |
22 | | #include "clang/AST/DeclCXX.h" |
23 | | #include "clang/AST/DeclObjC.h" |
24 | | #include "clang/AST/DeclOpenMP.h" |
25 | | #include "clang/AST/DeclTemplate.h" |
26 | | #include "clang/AST/DeclarationName.h" |
27 | | #include "clang/AST/Expr.h" |
28 | | #include "clang/AST/ExprCXX.h" |
29 | | #include "clang/AST/ExternalASTSource.h" |
30 | | #include "clang/AST/ODRHash.h" |
31 | | #include "clang/AST/PrettyDeclStackTrace.h" |
32 | | #include "clang/AST/PrettyPrinter.h" |
33 | | #include "clang/AST/Randstruct.h" |
34 | | #include "clang/AST/RecordLayout.h" |
35 | | #include "clang/AST/Redeclarable.h" |
36 | | #include "clang/AST/Stmt.h" |
37 | | #include "clang/AST/TemplateBase.h" |
38 | | #include "clang/AST/Type.h" |
39 | | #include "clang/AST/TypeLoc.h" |
40 | | #include "clang/Basic/Builtins.h" |
41 | | #include "clang/Basic/IdentifierTable.h" |
42 | | #include "clang/Basic/LLVM.h" |
43 | | #include "clang/Basic/LangOptions.h" |
44 | | #include "clang/Basic/Linkage.h" |
45 | | #include "clang/Basic/Module.h" |
46 | | #include "clang/Basic/NoSanitizeList.h" |
47 | | #include "clang/Basic/PartialDiagnostic.h" |
48 | | #include "clang/Basic/Sanitizers.h" |
49 | | #include "clang/Basic/SourceLocation.h" |
50 | | #include "clang/Basic/SourceManager.h" |
51 | | #include "clang/Basic/Specifiers.h" |
52 | | #include "clang/Basic/TargetCXXABI.h" |
53 | | #include "clang/Basic/TargetInfo.h" |
54 | | #include "clang/Basic/Visibility.h" |
55 | | #include "llvm/ADT/APSInt.h" |
56 | | #include "llvm/ADT/ArrayRef.h" |
57 | | #include "llvm/ADT/STLExtras.h" |
58 | | #include "llvm/ADT/SmallVector.h" |
59 | | #include "llvm/ADT/StringRef.h" |
60 | | #include "llvm/ADT/StringSwitch.h" |
61 | | #include "llvm/Support/Casting.h" |
62 | | #include "llvm/Support/ErrorHandling.h" |
63 | | #include "llvm/Support/raw_ostream.h" |
64 | | #include "llvm/TargetParser/Triple.h" |
65 | | #include <algorithm> |
66 | | #include <cassert> |
67 | | #include <cstddef> |
68 | | #include <cstring> |
69 | | #include <memory> |
70 | | #include <optional> |
71 | | #include <string> |
72 | | #include <tuple> |
73 | | #include <type_traits> |
74 | | |
75 | | using namespace clang; |
76 | | |
77 | 0 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
78 | 0 | return D->getASTContext().getPrimaryMergedDecl(D); |
79 | 0 | } |
80 | | |
81 | 0 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
82 | 0 | SourceLocation Loc = this->Loc; |
83 | 0 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
84 | 0 | if (Loc.isValid()) { |
85 | 0 | Loc.print(OS, Context.getSourceManager()); |
86 | 0 | OS << ": "; |
87 | 0 | } |
88 | 0 | OS << Message; |
89 | |
|
90 | 0 | if (auto *ND = dyn_cast_if_present<NamedDecl>(TheDecl)) { |
91 | 0 | OS << " '"; |
92 | 0 | ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); |
93 | 0 | OS << "'"; |
94 | 0 | } |
95 | |
|
96 | 0 | OS << '\n'; |
97 | 0 | } |
98 | | |
99 | | // Defined here so that it can be inlined into its direct callers. |
100 | 2.69k | bool Decl::isOutOfLine() const { |
101 | 2.69k | return !getLexicalDeclContext()->Equals(getDeclContext()); |
102 | 2.69k | } |
103 | | |
104 | | TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) |
105 | | : Decl(TranslationUnit, nullptr, SourceLocation()), |
106 | 46 | DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {} |
107 | | |
108 | | //===----------------------------------------------------------------------===// |
109 | | // NamedDecl Implementation |
110 | | //===----------------------------------------------------------------------===// |
111 | | |
112 | | // Visibility rules aren't rigorously externally specified, but here |
113 | | // are the basic principles behind what we implement: |
114 | | // |
115 | | // 1. An explicit visibility attribute is generally a direct expression |
116 | | // of the user's intent and should be honored. Only the innermost |
117 | | // visibility attribute applies. If no visibility attribute applies, |
118 | | // global visibility settings are considered. |
119 | | // |
120 | | // 2. There is one caveat to the above: on or in a template pattern, |
121 | | // an explicit visibility attribute is just a default rule, and |
122 | | // visibility can be decreased by the visibility of template |
123 | | // arguments. But this, too, has an exception: an attribute on an |
124 | | // explicit specialization or instantiation causes all the visibility |
125 | | // restrictions of the template arguments to be ignored. |
126 | | // |
127 | | // 3. A variable that does not otherwise have explicit visibility can |
128 | | // be restricted by the visibility of its type. |
129 | | // |
130 | | // 4. A visibility restriction is explicit if it comes from an |
131 | | // attribute (or something like it), not a global visibility setting. |
132 | | // When emitting a reference to an external symbol, visibility |
133 | | // restrictions are ignored unless they are explicit. |
134 | | // |
135 | | // 5. When computing the visibility of a non-type, including a |
136 | | // non-type member of a class, only non-type visibility restrictions |
137 | | // are considered: the 'visibility' attribute, global value-visibility |
138 | | // settings, and a few special cases like __private_extern. |
139 | | // |
140 | | // 6. When computing the visibility of a type, including a type member |
141 | | // of a class, only type visibility restrictions are considered: |
142 | | // the 'type_visibility' attribute and global type-visibility settings. |
143 | | // However, a 'visibility' attribute counts as a 'type_visibility' |
144 | | // attribute on any declaration that only has the former. |
145 | | // |
146 | | // The visibility of a "secondary" entity, like a template argument, |
147 | | // is computed using the kind of that entity, not the kind of the |
148 | | // primary entity for which we are computing visibility. For example, |
149 | | // the visibility of a specialization of either of these templates: |
150 | | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
151 | | // template <class T, bool (&compare)(T, X)> class matcher; |
152 | | // is restricted according to the type visibility of the argument 'T', |
153 | | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
154 | | // the argument function 'compare'. That 'has_match' is a value |
155 | | // and 'matcher' is a type only matters when looking for attributes |
156 | | // and settings from the immediate context. |
157 | | |
158 | | /// Does this computation kind permit us to consider additional |
159 | | /// visibility settings from attributes and the like? |
160 | 5.08k | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
161 | 5.08k | return computation.IgnoreExplicitVisibility; |
162 | 5.08k | } |
163 | | |
164 | | /// Given an LVComputationKind, return one of the same type/value sort |
165 | | /// that records that it already has explicit visibility. |
166 | | static LVComputationKind |
167 | 0 | withExplicitVisibilityAlready(LVComputationKind Kind) { |
168 | 0 | Kind.IgnoreExplicitVisibility = true; |
169 | 0 | return Kind; |
170 | 0 | } |
171 | | |
172 | | static std::optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
173 | 0 | LVComputationKind kind) { |
174 | 0 | assert(!kind.IgnoreExplicitVisibility && |
175 | 0 | "asking for explicit visibility when we shouldn't be"); |
176 | 0 | return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); |
177 | 0 | } |
178 | | |
179 | | /// Is the given declaration a "type" or a "value" for the purposes of |
180 | | /// visibility computation? |
181 | 0 | static bool usesTypeVisibility(const NamedDecl *D) { |
182 | 0 | return isa<TypeDecl>(D) || |
183 | 0 | isa<ClassTemplateDecl>(D) || |
184 | 0 | isa<ObjCInterfaceDecl>(D); |
185 | 0 | } |
186 | | |
187 | | /// Does the given declaration have member specialization information, |
188 | | /// and if so, is it an explicit specialization? |
189 | | template <class T> |
190 | | static std::enable_if_t<!std::is_base_of_v<RedeclarableTemplateDecl, T>, bool> |
191 | 0 | isExplicitMemberSpecialization(const T *D) { |
192 | 0 | if (const MemberSpecializationInfo *member = |
193 | 0 | D->getMemberSpecializationInfo()) { |
194 | 0 | return member->isExplicitSpecialization(); |
195 | 0 | } |
196 | 0 | return false; |
197 | 0 | } Unexecuted instantiation: Decl.cpp:_ZL30isExplicitMemberSpecializationIN5clang13CXXMethodDeclEENSt3__19enable_ifIXntsr3stdE12is_base_of_vINS0_24RedeclarableTemplateDeclET_EEbE4typeEPKS5_ Unexecuted instantiation: Decl.cpp:_ZL30isExplicitMemberSpecializationIN5clang13CXXRecordDeclEENSt3__19enable_ifIXntsr3stdE12is_base_of_vINS0_24RedeclarableTemplateDeclET_EEbE4typeEPKS5_ Unexecuted instantiation: Decl.cpp:_ZL30isExplicitMemberSpecializationIN5clang7VarDeclEENSt3__19enable_ifIXntsr3stdE12is_base_of_vINS0_24RedeclarableTemplateDeclET_EEbE4typeEPKS5_ |
198 | | |
199 | | /// For templates, this question is easier: a member template can't be |
200 | | /// explicitly instantiated, so there's a single bit indicating whether |
201 | | /// or not this is an explicit member specialization. |
202 | 0 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
203 | 0 | return D->isMemberSpecialization(); |
204 | 0 | } |
205 | | |
206 | | /// Given a visibility attribute, return the explicit visibility |
207 | | /// associated with it. |
208 | | template <class T> |
209 | 0 | static Visibility getVisibilityFromAttr(const T *attr) { |
210 | 0 | switch (attr->getVisibility()) { |
211 | 0 | case T::Default: |
212 | 0 | return DefaultVisibility; |
213 | 0 | case T::Hidden: |
214 | 0 | return HiddenVisibility; |
215 | 0 | case T::Protected: |
216 | 0 | return ProtectedVisibility; |
217 | 0 | } |
218 | 0 | llvm_unreachable("bad visibility kind"); |
219 | 0 | } Unexecuted instantiation: Decl.cpp:clang::Visibility getVisibilityFromAttr<clang::TypeVisibilityAttr>(clang::TypeVisibilityAttr const*) Unexecuted instantiation: Decl.cpp:clang::Visibility getVisibilityFromAttr<clang::VisibilityAttr>(clang::VisibilityAttr const*) |
220 | | |
221 | | /// Return the explicit visibility of the given declaration. |
222 | | static std::optional<Visibility> |
223 | 0 | getVisibilityOf(const NamedDecl *D, NamedDecl::ExplicitVisibilityKind kind) { |
224 | | // If we're ultimately computing the visibility of a type, look for |
225 | | // a 'type_visibility' attribute before looking for 'visibility'. |
226 | 0 | if (kind == NamedDecl::VisibilityForType) { |
227 | 0 | if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { |
228 | 0 | return getVisibilityFromAttr(A); |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | | // If this declaration has an explicit visibility attribute, use it. |
233 | 0 | if (const auto *A = D->getAttr<VisibilityAttr>()) { |
234 | 0 | return getVisibilityFromAttr(A); |
235 | 0 | } |
236 | | |
237 | 0 | return std::nullopt; |
238 | 0 | } |
239 | | |
240 | | LinkageInfo LinkageComputer::getLVForType(const Type &T, |
241 | 2.58k | LVComputationKind computation) { |
242 | 2.58k | if (computation.IgnoreAllVisibility) |
243 | 2.58k | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
244 | 0 | return getTypeLinkageAndVisibility(&T); |
245 | 2.58k | } |
246 | | |
247 | | /// Get the most restrictive linkage for the types in the given |
248 | | /// template parameter list. For visibility purposes, template |
249 | | /// parameters are part of the signature of a template. |
250 | | LinkageInfo LinkageComputer::getLVForTemplateParameterList( |
251 | 0 | const TemplateParameterList *Params, LVComputationKind computation) { |
252 | 0 | LinkageInfo LV; |
253 | 0 | for (const NamedDecl *P : *Params) { |
254 | | // Template type parameters are the most common and never |
255 | | // contribute to visibility, pack or not. |
256 | 0 | if (isa<TemplateTypeParmDecl>(P)) |
257 | 0 | continue; |
258 | | |
259 | | // Non-type template parameters can be restricted by the value type, e.g. |
260 | | // template <enum X> class A { ... }; |
261 | | // We have to be careful here, though, because we can be dealing with |
262 | | // dependent types. |
263 | 0 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
264 | | // Handle the non-pack case first. |
265 | 0 | if (!NTTP->isExpandedParameterPack()) { |
266 | 0 | if (!NTTP->getType()->isDependentType()) { |
267 | 0 | LV.merge(getLVForType(*NTTP->getType(), computation)); |
268 | 0 | } |
269 | 0 | continue; |
270 | 0 | } |
271 | | |
272 | | // Look at all the types in an expanded pack. |
273 | 0 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
274 | 0 | QualType type = NTTP->getExpansionType(i); |
275 | 0 | if (!type->isDependentType()) |
276 | 0 | LV.merge(getTypeLinkageAndVisibility(type)); |
277 | 0 | } |
278 | 0 | continue; |
279 | 0 | } |
280 | | |
281 | | // Template template parameters can be restricted by their |
282 | | // template parameters, recursively. |
283 | 0 | const auto *TTP = cast<TemplateTemplateParmDecl>(P); |
284 | | |
285 | | // Handle the non-pack case first. |
286 | 0 | if (!TTP->isExpandedParameterPack()) { |
287 | 0 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), |
288 | 0 | computation)); |
289 | 0 | continue; |
290 | 0 | } |
291 | | |
292 | | // Look at all expansions in an expanded pack. |
293 | 0 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
294 | 0 | i != n; ++i) { |
295 | 0 | LV.merge(getLVForTemplateParameterList( |
296 | 0 | TTP->getExpansionTemplateParameters(i), computation)); |
297 | 0 | } |
298 | 0 | } |
299 | |
|
300 | 0 | return LV; |
301 | 0 | } |
302 | | |
303 | 0 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
304 | 0 | const Decl *Ret = nullptr; |
305 | 0 | const DeclContext *DC = D->getDeclContext(); |
306 | 0 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
307 | 0 | if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) |
308 | 0 | Ret = cast<Decl>(DC); |
309 | 0 | DC = DC->getParent(); |
310 | 0 | } |
311 | 0 | return Ret; |
312 | 0 | } |
313 | | |
314 | | /// Get the most restrictive linkage for the types and |
315 | | /// declarations in the given template argument list. |
316 | | /// |
317 | | /// Note that we don't take an LVComputationKind because we always |
318 | | /// want to honor the visibility of template arguments in the same way. |
319 | | LinkageInfo |
320 | | LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
321 | 0 | LVComputationKind computation) { |
322 | 0 | LinkageInfo LV; |
323 | |
|
324 | 0 | for (const TemplateArgument &Arg : Args) { |
325 | 0 | switch (Arg.getKind()) { |
326 | 0 | case TemplateArgument::Null: |
327 | 0 | case TemplateArgument::Integral: |
328 | 0 | case TemplateArgument::Expression: |
329 | 0 | continue; |
330 | | |
331 | 0 | case TemplateArgument::Type: |
332 | 0 | LV.merge(getLVForType(*Arg.getAsType(), computation)); |
333 | 0 | continue; |
334 | | |
335 | 0 | case TemplateArgument::Declaration: { |
336 | 0 | const NamedDecl *ND = Arg.getAsDecl(); |
337 | 0 | assert(!usesTypeVisibility(ND)); |
338 | 0 | LV.merge(getLVForDecl(ND, computation)); |
339 | 0 | continue; |
340 | 0 | } |
341 | | |
342 | 0 | case TemplateArgument::NullPtr: |
343 | 0 | LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); |
344 | 0 | continue; |
345 | | |
346 | 0 | case TemplateArgument::Template: |
347 | 0 | case TemplateArgument::TemplateExpansion: |
348 | 0 | if (TemplateDecl *Template = |
349 | 0 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
350 | 0 | LV.merge(getLVForDecl(Template, computation)); |
351 | 0 | continue; |
352 | | |
353 | 0 | case TemplateArgument::Pack: |
354 | 0 | LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); |
355 | 0 | continue; |
356 | 0 | } |
357 | 0 | llvm_unreachable("bad template argument kind"); |
358 | 0 | } |
359 | | |
360 | 0 | return LV; |
361 | 0 | } |
362 | | |
363 | | LinkageInfo |
364 | | LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
365 | 0 | LVComputationKind computation) { |
366 | 0 | return getLVForTemplateArgumentList(TArgs.asArray(), computation); |
367 | 0 | } |
368 | | |
369 | | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
370 | 0 | const FunctionTemplateSpecializationInfo *specInfo) { |
371 | | // Include visibility from the template parameters and arguments |
372 | | // only if this is not an explicit instantiation or specialization |
373 | | // with direct explicit visibility. (Implicit instantiations won't |
374 | | // have a direct attribute.) |
375 | 0 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
376 | 0 | return true; |
377 | | |
378 | 0 | return !fn->hasAttr<VisibilityAttr>(); |
379 | 0 | } |
380 | | |
381 | | /// Merge in template-related linkage and visibility for the given |
382 | | /// function template specialization. |
383 | | /// |
384 | | /// We don't need a computation kind here because we can assume |
385 | | /// LVForValue. |
386 | | /// |
387 | | /// \param[out] LV the computation to use for the parent |
388 | | void LinkageComputer::mergeTemplateLV( |
389 | | LinkageInfo &LV, const FunctionDecl *fn, |
390 | | const FunctionTemplateSpecializationInfo *specInfo, |
391 | 0 | LVComputationKind computation) { |
392 | 0 | bool considerVisibility = |
393 | 0 | shouldConsiderTemplateVisibility(fn, specInfo); |
394 | |
|
395 | 0 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
396 | | // Merge information from the template declaration. |
397 | 0 | LinkageInfo tempLV = getLVForDecl(temp, computation); |
398 | | // The linkage of the specialization should be consistent with the |
399 | | // template declaration. |
400 | 0 | LV.setLinkage(tempLV.getLinkage()); |
401 | | |
402 | | // Merge information from the template parameters. |
403 | 0 | LinkageInfo paramsLV = |
404 | 0 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
405 | 0 | LV.mergeMaybeWithVisibility(paramsLV, considerVisibility); |
406 | | |
407 | | // Merge information from the template arguments. |
408 | 0 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
409 | 0 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
410 | 0 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
411 | 0 | } |
412 | | |
413 | | /// Does the given declaration have a direct visibility attribute |
414 | | /// that would match the given rules? |
415 | | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
416 | 0 | LVComputationKind computation) { |
417 | 0 | if (computation.IgnoreAllVisibility) |
418 | 0 | return false; |
419 | | |
420 | 0 | return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || |
421 | 0 | D->hasAttr<VisibilityAttr>(); |
422 | 0 | } |
423 | | |
424 | | /// Should we consider visibility associated with the template |
425 | | /// arguments and parameters of the given class template specialization? |
426 | | static bool shouldConsiderTemplateVisibility( |
427 | | const ClassTemplateSpecializationDecl *spec, |
428 | 0 | LVComputationKind computation) { |
429 | | // Include visibility from the template parameters and arguments |
430 | | // only if this is not an explicit instantiation or specialization |
431 | | // with direct explicit visibility (and note that implicit |
432 | | // instantiations won't have a direct attribute). |
433 | | // |
434 | | // Furthermore, we want to ignore template parameters and arguments |
435 | | // for an explicit specialization when computing the visibility of a |
436 | | // member thereof with explicit visibility. |
437 | | // |
438 | | // This is a bit complex; let's unpack it. |
439 | | // |
440 | | // An explicit class specialization is an independent, top-level |
441 | | // declaration. As such, if it or any of its members has an |
442 | | // explicit visibility attribute, that must directly express the |
443 | | // user's intent, and we should honor it. The same logic applies to |
444 | | // an explicit instantiation of a member of such a thing. |
445 | | |
446 | | // Fast path: if this is not an explicit instantiation or |
447 | | // specialization, we always want to consider template-related |
448 | | // visibility restrictions. |
449 | 0 | if (!spec->isExplicitInstantiationOrSpecialization()) |
450 | 0 | return true; |
451 | | |
452 | | // This is the 'member thereof' check. |
453 | 0 | if (spec->isExplicitSpecialization() && |
454 | 0 | hasExplicitVisibilityAlready(computation)) |
455 | 0 | return false; |
456 | | |
457 | 0 | return !hasDirectVisibilityAttribute(spec, computation); |
458 | 0 | } |
459 | | |
460 | | /// Merge in template-related linkage and visibility for the given |
461 | | /// class template specialization. |
462 | | void LinkageComputer::mergeTemplateLV( |
463 | | LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, |
464 | 0 | LVComputationKind computation) { |
465 | 0 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
466 | | |
467 | | // Merge information from the template parameters, but ignore |
468 | | // visibility if we're only considering template arguments. |
469 | 0 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
470 | | // Merge information from the template declaration. |
471 | 0 | LinkageInfo tempLV = getLVForDecl(temp, computation); |
472 | | // The linkage of the specialization should be consistent with the |
473 | | // template declaration. |
474 | 0 | LV.setLinkage(tempLV.getLinkage()); |
475 | |
|
476 | 0 | LinkageInfo paramsLV = |
477 | 0 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
478 | 0 | LV.mergeMaybeWithVisibility(paramsLV, |
479 | 0 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
480 | | |
481 | | // Merge information from the template arguments. We ignore |
482 | | // template-argument visibility if we've got an explicit |
483 | | // instantiation with a visibility attribute. |
484 | 0 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
485 | 0 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
486 | 0 | if (considerVisibility) |
487 | 0 | LV.mergeVisibility(argsLV); |
488 | 0 | LV.mergeExternalVisibility(argsLV); |
489 | 0 | } |
490 | | |
491 | | /// Should we consider visibility associated with the template |
492 | | /// arguments and parameters of the given variable template |
493 | | /// specialization? As usual, follow class template specialization |
494 | | /// logic up to initialization. |
495 | | static bool shouldConsiderTemplateVisibility( |
496 | | const VarTemplateSpecializationDecl *spec, |
497 | 0 | LVComputationKind computation) { |
498 | | // Include visibility from the template parameters and arguments |
499 | | // only if this is not an explicit instantiation or specialization |
500 | | // with direct explicit visibility (and note that implicit |
501 | | // instantiations won't have a direct attribute). |
502 | 0 | if (!spec->isExplicitInstantiationOrSpecialization()) |
503 | 0 | return true; |
504 | | |
505 | | // An explicit variable specialization is an independent, top-level |
506 | | // declaration. As such, if it has an explicit visibility attribute, |
507 | | // that must directly express the user's intent, and we should honor |
508 | | // it. |
509 | 0 | if (spec->isExplicitSpecialization() && |
510 | 0 | hasExplicitVisibilityAlready(computation)) |
511 | 0 | return false; |
512 | | |
513 | 0 | return !hasDirectVisibilityAttribute(spec, computation); |
514 | 0 | } |
515 | | |
516 | | /// Merge in template-related linkage and visibility for the given |
517 | | /// variable template specialization. As usual, follow class template |
518 | | /// specialization logic up to initialization. |
519 | | void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, |
520 | | const VarTemplateSpecializationDecl *spec, |
521 | 0 | LVComputationKind computation) { |
522 | 0 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
523 | | |
524 | | // Merge information from the template parameters, but ignore |
525 | | // visibility if we're only considering template arguments. |
526 | 0 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
527 | 0 | LinkageInfo tempLV = |
528 | 0 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
529 | 0 | LV.mergeMaybeWithVisibility(tempLV, |
530 | 0 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
531 | | |
532 | | // Merge information from the template arguments. We ignore |
533 | | // template-argument visibility if we've got an explicit |
534 | | // instantiation with a visibility attribute. |
535 | 0 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
536 | 0 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
537 | 0 | if (considerVisibility) |
538 | 0 | LV.mergeVisibility(argsLV); |
539 | 0 | LV.mergeExternalVisibility(argsLV); |
540 | 0 | } |
541 | | |
542 | 0 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
543 | | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
544 | 0 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
545 | 0 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
546 | 0 | return false; |
547 | | |
548 | 0 | const auto *FD = dyn_cast<FunctionDecl>(D); |
549 | 0 | if (!FD) |
550 | 0 | return false; |
551 | | |
552 | 0 | TemplateSpecializationKind TSK = TSK_Undeclared; |
553 | 0 | if (FunctionTemplateSpecializationInfo *spec |
554 | 0 | = FD->getTemplateSpecializationInfo()) { |
555 | 0 | TSK = spec->getTemplateSpecializationKind(); |
556 | 0 | } else if (MemberSpecializationInfo *MSI = |
557 | 0 | FD->getMemberSpecializationInfo()) { |
558 | 0 | TSK = MSI->getTemplateSpecializationKind(); |
559 | 0 | } |
560 | |
|
561 | 0 | const FunctionDecl *Def = nullptr; |
562 | | // InlineVisibilityHidden only applies to definitions, and |
563 | | // isInlined() only gives meaningful answers on definitions |
564 | | // anyway. |
565 | 0 | return TSK != TSK_ExplicitInstantiationDeclaration && |
566 | 0 | TSK != TSK_ExplicitInstantiationDefinition && |
567 | 0 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
568 | 0 | } |
569 | | |
570 | 2.58k | template <typename T> static bool isFirstInExternCContext(T *D) { |
571 | 2.58k | const T *First = D->getFirstDecl(); |
572 | 2.58k | return First->isInExternCContext(); |
573 | 2.58k | } Decl.cpp:bool isFirstInExternCContext<clang::VarDecl const>(clang::VarDecl const*) Line | Count | Source | 570 | 2.58k | template <typename T> static bool isFirstInExternCContext(T *D) { | 571 | 2.58k | const T *First = D->getFirstDecl(); | 572 | 2.58k | return First->isInExternCContext(); | 573 | 2.58k | } |
Decl.cpp:bool isFirstInExternCContext<clang::FunctionDecl const>(clang::FunctionDecl const*) Line | Count | Source | 570 | 1 | template <typename T> static bool isFirstInExternCContext(T *D) { | 571 | 1 | const T *First = D->getFirstDecl(); | 572 | 1 | return First->isInExternCContext(); | 573 | 1 | } |
|
574 | | |
575 | 12.7k | static bool isSingleLineLanguageLinkage(const Decl &D) { |
576 | 12.7k | if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) |
577 | 0 | if (!SD->hasBraces()) |
578 | 0 | return true; |
579 | 12.7k | return false; |
580 | 12.7k | } |
581 | | |
582 | 0 | static bool isDeclaredInModuleInterfaceOrPartition(const NamedDecl *D) { |
583 | 0 | if (auto *M = D->getOwningModule()) |
584 | 0 | return M->isInterfaceOrPartition(); |
585 | 0 | return false; |
586 | 0 | } |
587 | | |
588 | 5.08k | static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { |
589 | 5.08k | return LinkageInfo::external(); |
590 | 5.08k | } |
591 | | |
592 | 5.08k | static StorageClass getStorageClass(const Decl *D) { |
593 | 5.08k | if (auto *TD = dyn_cast<TemplateDecl>(D)) |
594 | 0 | D = TD->getTemplatedDecl(); |
595 | 5.08k | if (D) { |
596 | 5.08k | if (auto *VD = dyn_cast<VarDecl>(D)) |
597 | 5.07k | return VD->getStorageClass(); |
598 | 19 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
599 | 19 | return FD->getStorageClass(); |
600 | 19 | } |
601 | 0 | return SC_None; |
602 | 5.08k | } |
603 | | |
604 | | LinkageInfo |
605 | | LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, |
606 | | LVComputationKind computation, |
607 | 5.08k | bool IgnoreVarTypeLinkage) { |
608 | 5.08k | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
609 | 5.08k | "Not a name having namespace scope"); |
610 | 0 | ASTContext &Context = D->getASTContext(); |
611 | | |
612 | | // C++ [basic.link]p3: |
613 | | // A name having namespace scope (3.3.6) has internal linkage if it |
614 | | // is the name of |
615 | | |
616 | 5.08k | if (getStorageClass(D->getCanonicalDecl()) == SC_Static) { |
617 | | // - a variable, variable template, function, or function template |
618 | | // that is explicitly declared static; or |
619 | | // (This bullet corresponds to C99 6.2.2p3.) |
620 | 0 | return LinkageInfo::internal(); |
621 | 0 | } |
622 | | |
623 | 5.08k | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
624 | | // - a non-template variable of non-volatile const-qualified type, unless |
625 | | // - it is explicitly declared extern, or |
626 | | // - it is declared in the purview of a module interface unit |
627 | | // (outside the private-module-fragment, if any) or module partition, or |
628 | | // - it is inline, or |
629 | | // - it was previously declared and the prior declaration did not have |
630 | | // internal linkage |
631 | | // (There is no equivalent in C99.) |
632 | 5.07k | if (Context.getLangOpts().CPlusPlus && Var->getType().isConstQualified() && |
633 | 5.07k | !Var->getType().isVolatileQualified() && !Var->isInline() && |
634 | 5.07k | !isDeclaredInModuleInterfaceOrPartition(Var) && |
635 | 5.07k | !isa<VarTemplateSpecializationDecl>(Var) && |
636 | 5.07k | !Var->getDescribedVarTemplate()) { |
637 | 0 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
638 | 0 | if (PrevVar) |
639 | 0 | return getLVForDecl(PrevVar, computation); |
640 | | |
641 | 0 | if (Var->getStorageClass() != SC_Extern && |
642 | 0 | Var->getStorageClass() != SC_PrivateExtern && |
643 | 0 | !isSingleLineLanguageLinkage(*Var)) |
644 | 0 | return LinkageInfo::internal(); |
645 | 0 | } |
646 | | |
647 | 5.28k | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
648 | 5.07k | PrevVar = PrevVar->getPreviousDecl()) { |
649 | 212 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
650 | 212 | Var->getStorageClass() == SC_None) |
651 | 0 | return getDeclLinkageAndVisibility(PrevVar); |
652 | | // Explicitly declared static. |
653 | 212 | if (PrevVar->getStorageClass() == SC_Static) |
654 | 0 | return LinkageInfo::internal(); |
655 | 212 | } |
656 | 5.07k | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { |
657 | | // - a data member of an anonymous union. |
658 | 0 | const VarDecl *VD = IFD->getVarDecl(); |
659 | 0 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); |
660 | 0 | return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); |
661 | 0 | } |
662 | 5.08k | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); |
663 | | |
664 | | // FIXME: This gives internal linkage to names that should have no linkage |
665 | | // (those not covered by [basic.link]p6). |
666 | 5.08k | if (D->isInAnonymousNamespace()) { |
667 | 0 | const auto *Var = dyn_cast<VarDecl>(D); |
668 | 0 | const auto *Func = dyn_cast<FunctionDecl>(D); |
669 | | // FIXME: The check for extern "C" here is not justified by the standard |
670 | | // wording, but we retain it from the pre-DR1113 model to avoid breaking |
671 | | // code. |
672 | | // |
673 | | // C++11 [basic.link]p4: |
674 | | // An unnamed namespace or a namespace declared directly or indirectly |
675 | | // within an unnamed namespace has internal linkage. |
676 | 0 | if ((!Var || !isFirstInExternCContext(Var)) && |
677 | 0 | (!Func || !isFirstInExternCContext(Func))) |
678 | 0 | return LinkageInfo::internal(); |
679 | 0 | } |
680 | | |
681 | | // Set up the defaults. |
682 | | |
683 | | // C99 6.2.2p5: |
684 | | // If the declaration of an identifier for an object has file |
685 | | // scope and no storage-class specifier, its linkage is |
686 | | // external. |
687 | 5.08k | LinkageInfo LV = getExternalLinkageFor(D); |
688 | | |
689 | 5.08k | if (!hasExplicitVisibilityAlready(computation)) { |
690 | 0 | if (std::optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
691 | 0 | LV.mergeVisibility(*Vis, true); |
692 | 0 | } else { |
693 | | // If we're declared in a namespace with a visibility attribute, |
694 | | // use that namespace's visibility, and it still counts as explicit. |
695 | 0 | for (const DeclContext *DC = D->getDeclContext(); |
696 | 0 | !isa<TranslationUnitDecl>(DC); |
697 | 0 | DC = DC->getParent()) { |
698 | 0 | const auto *ND = dyn_cast<NamespaceDecl>(DC); |
699 | 0 | if (!ND) continue; |
700 | 0 | if (std::optional<Visibility> Vis = |
701 | 0 | getExplicitVisibility(ND, computation)) { |
702 | 0 | LV.mergeVisibility(*Vis, true); |
703 | 0 | break; |
704 | 0 | } |
705 | 0 | } |
706 | 0 | } |
707 | | |
708 | | // Add in global settings if the above didn't give us direct visibility. |
709 | 0 | if (!LV.isVisibilityExplicit()) { |
710 | | // Use global type/value visibility as appropriate. |
711 | 0 | Visibility globalVisibility = |
712 | 0 | computation.isValueVisibility() |
713 | 0 | ? Context.getLangOpts().getValueVisibilityMode() |
714 | 0 | : Context.getLangOpts().getTypeVisibilityMode(); |
715 | 0 | LV.mergeVisibility(globalVisibility, /*explicit*/ false); |
716 | | |
717 | | // If we're paying attention to global visibility, apply |
718 | | // -finline-visibility-hidden if this is an inline method. |
719 | 0 | if (useInlineVisibilityHidden(D)) |
720 | 0 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
721 | 0 | } |
722 | 0 | } |
723 | | |
724 | | // C++ [basic.link]p4: |
725 | | |
726 | | // A name having namespace scope that has not been given internal linkage |
727 | | // above and that is the name of |
728 | | // [...bullets...] |
729 | | // has its linkage determined as follows: |
730 | | // - if the enclosing namespace has internal linkage, the name has |
731 | | // internal linkage; [handled above] |
732 | | // - otherwise, if the declaration of the name is attached to a named |
733 | | // module and is not exported, the name has module linkage; |
734 | | // - otherwise, the name has external linkage. |
735 | | // LV is currently set up to handle the last two bullets. |
736 | | // |
737 | | // The bullets are: |
738 | | |
739 | | // - a variable; or |
740 | 5.08k | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
741 | | // GCC applies the following optimization to variables and static |
742 | | // data members, but not to functions: |
743 | | // |
744 | | // Modify the variable's LV by the LV of its type unless this is |
745 | | // C or extern "C". This follows from [basic.link]p9: |
746 | | // A type without linkage shall not be used as the type of a |
747 | | // variable or function with external linkage unless |
748 | | // - the entity has C language linkage, or |
749 | | // - the entity is declared within an unnamed namespace, or |
750 | | // - the entity is not used or is defined in the same |
751 | | // translation unit. |
752 | | // and [basic.link]p10: |
753 | | // ...the types specified by all declarations referring to a |
754 | | // given variable or function shall be identical... |
755 | | // C does not have an equivalent rule. |
756 | | // |
757 | | // Ignore this if we've got an explicit attribute; the user |
758 | | // probably knows what they're doing. |
759 | | // |
760 | | // Note that we don't want to make the variable non-external |
761 | | // because of this, but unique-external linkage suits us. |
762 | | |
763 | 5.07k | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && |
764 | 5.07k | !IgnoreVarTypeLinkage) { |
765 | 2.58k | LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); |
766 | 2.58k | if (!isExternallyVisible(TypeLV.getLinkage())) |
767 | 0 | return LinkageInfo::uniqueExternal(); |
768 | 2.58k | if (!LV.isVisibilityExplicit()) |
769 | 2.58k | LV.mergeVisibility(TypeLV); |
770 | 2.58k | } |
771 | | |
772 | 5.07k | if (Var->getStorageClass() == SC_PrivateExtern) |
773 | 0 | LV.mergeVisibility(HiddenVisibility, true); |
774 | | |
775 | | // Note that Sema::MergeVarDecl already takes care of implementing |
776 | | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
777 | | // to do it here. |
778 | | |
779 | | // As per function and class template specializations (below), |
780 | | // consider LV for the template and template arguments. We're at file |
781 | | // scope, so we do not need to worry about nested specializations. |
782 | 5.07k | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
783 | 0 | mergeTemplateLV(LV, spec, computation); |
784 | 0 | } |
785 | | |
786 | | // - a function; or |
787 | 5.07k | } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
788 | | // In theory, we can modify the function's LV by the LV of its |
789 | | // type unless it has C linkage (see comment above about variables |
790 | | // for justification). In practice, GCC doesn't do this, so it's |
791 | | // just too painful to make work. |
792 | | |
793 | 19 | if (Function->getStorageClass() == SC_PrivateExtern) |
794 | 0 | LV.mergeVisibility(HiddenVisibility, true); |
795 | | |
796 | | // OpenMP target declare device functions are not callable from the host so |
797 | | // they should not be exported from the device image. This applies to all |
798 | | // functions as the host-callable kernel functions are emitted at codegen. |
799 | 19 | if (Context.getLangOpts().OpenMP && |
800 | 19 | Context.getLangOpts().OpenMPIsTargetDevice && |
801 | 19 | ((Context.getTargetInfo().getTriple().isAMDGPU() || |
802 | 0 | Context.getTargetInfo().getTriple().isNVPTX()) || |
803 | 0 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Function))) |
804 | 0 | LV.mergeVisibility(HiddenVisibility, /*newExplicit=*/false); |
805 | | |
806 | | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
807 | | // merging storage classes and visibility attributes, so we don't have to |
808 | | // look at previous decls in here. |
809 | | |
810 | | // In C++, then if the type of the function uses a type with |
811 | | // unique-external linkage, it's not legally usable from outside |
812 | | // this translation unit. However, we should use the C linkage |
813 | | // rules instead for extern "C" declarations. |
814 | 19 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { |
815 | | // Only look at the type-as-written. Otherwise, deducing the return type |
816 | | // of a function could change its linkage. |
817 | 1 | QualType TypeAsWritten = Function->getType(); |
818 | 1 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
819 | 1 | TypeAsWritten = TSI->getType(); |
820 | 1 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
821 | 0 | return LinkageInfo::uniqueExternal(); |
822 | 1 | } |
823 | | |
824 | | // Consider LV from the template and the template arguments. |
825 | | // We're at file scope, so we do not need to worry about nested |
826 | | // specializations. |
827 | 19 | if (FunctionTemplateSpecializationInfo *specInfo |
828 | 19 | = Function->getTemplateSpecializationInfo()) { |
829 | 0 | mergeTemplateLV(LV, Function, specInfo, computation); |
830 | 0 | } |
831 | | |
832 | | // - a named class (Clause 9), or an unnamed class defined in a |
833 | | // typedef declaration in which the class has the typedef name |
834 | | // for linkage purposes (7.1.3); or |
835 | | // - a named enumeration (7.2), or an unnamed enumeration |
836 | | // defined in a typedef declaration in which the enumeration |
837 | | // has the typedef name for linkage purposes (7.1.3); or |
838 | 19 | } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { |
839 | | // Unnamed tags have no linkage. |
840 | 0 | if (!Tag->hasNameForLinkage()) |
841 | 0 | return LinkageInfo::none(); |
842 | | |
843 | | // If this is a class template specialization, consider the |
844 | | // linkage of the template and template arguments. We're at file |
845 | | // scope, so we do not need to worry about nested specializations. |
846 | 0 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
847 | 0 | mergeTemplateLV(LV, spec, computation); |
848 | 0 | } |
849 | | |
850 | | // FIXME: This is not part of the C++ standard any more. |
851 | | // - an enumerator belonging to an enumeration with external linkage; or |
852 | 0 | } else if (isa<EnumConstantDecl>(D)) { |
853 | 0 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
854 | 0 | computation); |
855 | 0 | if (!isExternalFormalLinkage(EnumLV.getLinkage())) |
856 | 0 | return LinkageInfo::none(); |
857 | 0 | LV.merge(EnumLV); |
858 | | |
859 | | // - a template |
860 | 0 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
861 | 0 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
862 | 0 | LinkageInfo tempLV = |
863 | 0 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
864 | 0 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
865 | | |
866 | | // An unnamed namespace or a namespace declared directly or indirectly |
867 | | // within an unnamed namespace has internal linkage. All other namespaces |
868 | | // have external linkage. |
869 | | // |
870 | | // We handled names in anonymous namespaces above. |
871 | 0 | } else if (isa<NamespaceDecl>(D)) { |
872 | 0 | return LV; |
873 | | |
874 | | // By extension, we assign external linkage to Objective-C |
875 | | // interfaces. |
876 | 0 | } else if (isa<ObjCInterfaceDecl>(D)) { |
877 | | // fallout |
878 | |
|
879 | 0 | } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
880 | | // A typedef declaration has linkage if it gives a type a name for |
881 | | // linkage purposes. |
882 | 0 | if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
883 | 0 | return LinkageInfo::none(); |
884 | |
|
885 | 0 | } else if (isa<MSGuidDecl>(D)) { |
886 | | // A GUID behaves like an inline variable with external linkage. Fall |
887 | | // through. |
888 | | |
889 | | // Everything not covered here has no linkage. |
890 | 0 | } else { |
891 | 0 | return LinkageInfo::none(); |
892 | 0 | } |
893 | | |
894 | | // If we ended up with non-externally-visible linkage, visibility should |
895 | | // always be default. |
896 | 5.08k | if (!isExternallyVisible(LV.getLinkage())) |
897 | 0 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
898 | | |
899 | 5.08k | return LV; |
900 | 5.08k | } |
901 | | |
902 | | LinkageInfo |
903 | | LinkageComputer::getLVForClassMember(const NamedDecl *D, |
904 | | LVComputationKind computation, |
905 | 0 | bool IgnoreVarTypeLinkage) { |
906 | | // Only certain class members have linkage. Note that fields don't |
907 | | // really have linkage, but it's convenient to say they do for the |
908 | | // purposes of calculating linkage of pointer-to-data-member |
909 | | // template arguments. |
910 | | // |
911 | | // Templates also don't officially have linkage, but since we ignore |
912 | | // the C++ standard and look at template arguments when determining |
913 | | // linkage and visibility of a template specialization, we might hit |
914 | | // a template template argument that way. If we do, we need to |
915 | | // consider its linkage. |
916 | 0 | if (!(isa<CXXMethodDecl>(D) || |
917 | 0 | isa<VarDecl>(D) || |
918 | 0 | isa<FieldDecl>(D) || |
919 | 0 | isa<IndirectFieldDecl>(D) || |
920 | 0 | isa<TagDecl>(D) || |
921 | 0 | isa<TemplateDecl>(D))) |
922 | 0 | return LinkageInfo::none(); |
923 | | |
924 | 0 | LinkageInfo LV; |
925 | | |
926 | | // If we have an explicit visibility attribute, merge that in. |
927 | 0 | if (!hasExplicitVisibilityAlready(computation)) { |
928 | 0 | if (std::optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
929 | 0 | LV.mergeVisibility(*Vis, true); |
930 | | // If we're paying attention to global visibility, apply |
931 | | // -finline-visibility-hidden if this is an inline method. |
932 | | // |
933 | | // Note that we do this before merging information about |
934 | | // the class visibility. |
935 | 0 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
936 | 0 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
937 | 0 | } |
938 | | |
939 | | // If this class member has an explicit visibility attribute, the only |
940 | | // thing that can change its visibility is the template arguments, so |
941 | | // only look for them when processing the class. |
942 | 0 | LVComputationKind classComputation = computation; |
943 | 0 | if (LV.isVisibilityExplicit()) |
944 | 0 | classComputation = withExplicitVisibilityAlready(computation); |
945 | |
|
946 | 0 | LinkageInfo classLV = |
947 | 0 | getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); |
948 | | // The member has the same linkage as the class. If that's not externally |
949 | | // visible, we don't need to compute anything about the linkage. |
950 | | // FIXME: If we're only computing linkage, can we bail out here? |
951 | 0 | if (!isExternallyVisible(classLV.getLinkage())) |
952 | 0 | return classLV; |
953 | | |
954 | | |
955 | | // Otherwise, don't merge in classLV yet, because in certain cases |
956 | | // we need to completely ignore the visibility from it. |
957 | | |
958 | | // Specifically, if this decl exists and has an explicit attribute. |
959 | 0 | const NamedDecl *explicitSpecSuppressor = nullptr; |
960 | |
|
961 | 0 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
962 | | // Only look at the type-as-written. Otherwise, deducing the return type |
963 | | // of a function could change its linkage. |
964 | 0 | QualType TypeAsWritten = MD->getType(); |
965 | 0 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
966 | 0 | TypeAsWritten = TSI->getType(); |
967 | 0 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
968 | 0 | return LinkageInfo::uniqueExternal(); |
969 | | |
970 | | // If this is a method template specialization, use the linkage for |
971 | | // the template parameters and arguments. |
972 | 0 | if (FunctionTemplateSpecializationInfo *spec |
973 | 0 | = MD->getTemplateSpecializationInfo()) { |
974 | 0 | mergeTemplateLV(LV, MD, spec, computation); |
975 | 0 | if (spec->isExplicitSpecialization()) { |
976 | 0 | explicitSpecSuppressor = MD; |
977 | 0 | } else if (isExplicitMemberSpecialization(spec->getTemplate())) { |
978 | 0 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
979 | 0 | } |
980 | 0 | } else if (isExplicitMemberSpecialization(MD)) { |
981 | 0 | explicitSpecSuppressor = MD; |
982 | 0 | } |
983 | | |
984 | | // OpenMP target declare device functions are not callable from the host so |
985 | | // they should not be exported from the device image. This applies to all |
986 | | // functions as the host-callable kernel functions are emitted at codegen. |
987 | 0 | ASTContext &Context = D->getASTContext(); |
988 | 0 | if (Context.getLangOpts().OpenMP && |
989 | 0 | Context.getLangOpts().OpenMPIsTargetDevice && |
990 | 0 | ((Context.getTargetInfo().getTriple().isAMDGPU() || |
991 | 0 | Context.getTargetInfo().getTriple().isNVPTX()) || |
992 | 0 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(MD))) |
993 | 0 | LV.mergeVisibility(HiddenVisibility, /*newExplicit=*/false); |
994 | |
|
995 | 0 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
996 | 0 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
997 | 0 | mergeTemplateLV(LV, spec, computation); |
998 | 0 | if (spec->isExplicitSpecialization()) { |
999 | 0 | explicitSpecSuppressor = spec; |
1000 | 0 | } else { |
1001 | 0 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
1002 | 0 | if (isExplicitMemberSpecialization(temp)) { |
1003 | 0 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
1004 | 0 | } |
1005 | 0 | } |
1006 | 0 | } else if (isExplicitMemberSpecialization(RD)) { |
1007 | 0 | explicitSpecSuppressor = RD; |
1008 | 0 | } |
1009 | | |
1010 | | // Static data members. |
1011 | 0 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
1012 | 0 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) |
1013 | 0 | mergeTemplateLV(LV, spec, computation); |
1014 | | |
1015 | | // Modify the variable's linkage by its type, but ignore the |
1016 | | // type's visibility unless it's a definition. |
1017 | 0 | if (!IgnoreVarTypeLinkage) { |
1018 | 0 | LinkageInfo typeLV = getLVForType(*VD->getType(), computation); |
1019 | | // FIXME: If the type's linkage is not externally visible, we can |
1020 | | // give this static data member UniqueExternalLinkage. |
1021 | 0 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
1022 | 0 | LV.mergeVisibility(typeLV); |
1023 | 0 | LV.mergeExternalVisibility(typeLV); |
1024 | 0 | } |
1025 | |
|
1026 | 0 | if (isExplicitMemberSpecialization(VD)) { |
1027 | 0 | explicitSpecSuppressor = VD; |
1028 | 0 | } |
1029 | | |
1030 | | // Template members. |
1031 | 0 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
1032 | 0 | bool considerVisibility = |
1033 | 0 | (!LV.isVisibilityExplicit() && |
1034 | 0 | !classLV.isVisibilityExplicit() && |
1035 | 0 | !hasExplicitVisibilityAlready(computation)); |
1036 | 0 | LinkageInfo tempLV = |
1037 | 0 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
1038 | 0 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
1039 | |
|
1040 | 0 | if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { |
1041 | 0 | if (isExplicitMemberSpecialization(redeclTemp)) { |
1042 | 0 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
1043 | 0 | } |
1044 | 0 | } |
1045 | 0 | } |
1046 | | |
1047 | | // We should never be looking for an attribute directly on a template. |
1048 | 0 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
1049 | | |
1050 | | // If this member is an explicit member specialization, and it has |
1051 | | // an explicit attribute, ignore visibility from the parent. |
1052 | 0 | bool considerClassVisibility = true; |
1053 | 0 | if (explicitSpecSuppressor && |
1054 | | // optimization: hasDVA() is true only with explicit visibility. |
1055 | 0 | LV.isVisibilityExplicit() && |
1056 | 0 | classLV.getVisibility() != DefaultVisibility && |
1057 | 0 | hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { |
1058 | 0 | considerClassVisibility = false; |
1059 | 0 | } |
1060 | | |
1061 | | // Finally, merge in information from the class. |
1062 | 0 | LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); |
1063 | 0 | return LV; |
1064 | 0 | } |
1065 | | |
1066 | 0 | void NamedDecl::anchor() {} |
1067 | | |
1068 | 344 | bool NamedDecl::isLinkageValid() const { |
1069 | 344 | if (!hasCachedLinkage()) |
1070 | 344 | return true; |
1071 | | |
1072 | 0 | Linkage L = LinkageComputer{} |
1073 | 0 | .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) |
1074 | 0 | .getLinkage(); |
1075 | 0 | return L == getCachedLinkage(); |
1076 | 344 | } |
1077 | | |
1078 | 7.91k | bool NamedDecl::isPlaceholderVar(const LangOptions &LangOpts) const { |
1079 | | // [C++2c] [basic.scope.scope]/p5 |
1080 | | // A declaration is name-independent if its name is _ and it declares |
1081 | | // - a variable with automatic storage duration, |
1082 | | // - a structured binding not inhabiting a namespace scope, |
1083 | | // - the variable introduced by an init-capture |
1084 | | // - or a non-static data member. |
1085 | | |
1086 | 7.91k | if (!LangOpts.CPlusPlus || !getIdentifier() || |
1087 | 7.91k | !getIdentifier()->isPlaceholder()) |
1088 | 7.74k | return false; |
1089 | 165 | if (isa<FieldDecl>(this)) |
1090 | 0 | return true; |
1091 | 165 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(this)) { |
1092 | 0 | if (!getDeclContext()->isFunctionOrMethod() && |
1093 | 0 | !getDeclContext()->isRecord()) |
1094 | 0 | return false; |
1095 | 0 | const VarDecl *VD = IFD->getVarDecl(); |
1096 | 0 | return !VD || VD->getStorageDuration() == SD_Automatic; |
1097 | 0 | } |
1098 | | // and it declares a variable with automatic storage duration |
1099 | 165 | if (const auto *VD = dyn_cast<VarDecl>(this)) { |
1100 | 165 | if (isa<ParmVarDecl>(VD)) |
1101 | 0 | return false; |
1102 | 165 | if (VD->isInitCapture()) |
1103 | 0 | return true; |
1104 | 165 | return VD->getStorageDuration() == StorageDuration::SD_Automatic; |
1105 | 165 | } |
1106 | 0 | if (const auto *BD = dyn_cast<BindingDecl>(this); |
1107 | 0 | BD && getDeclContext()->isFunctionOrMethod()) { |
1108 | 0 | const VarDecl *VD = BD->getHoldingVar(); |
1109 | 0 | return !VD || VD->getStorageDuration() == StorageDuration::SD_Automatic; |
1110 | 0 | } |
1111 | 0 | return false; |
1112 | 0 | } |
1113 | | |
1114 | | ReservedIdentifierStatus |
1115 | 4.90k | NamedDecl::isReserved(const LangOptions &LangOpts) const { |
1116 | 4.90k | const IdentifierInfo *II = getIdentifier(); |
1117 | | |
1118 | | // This triggers at least for CXXLiteralIdentifiers, which we already checked |
1119 | | // at lexing time. |
1120 | 4.90k | if (!II) |
1121 | 0 | return ReservedIdentifierStatus::NotReserved; |
1122 | | |
1123 | 4.90k | ReservedIdentifierStatus Status = II->isReserved(LangOpts); |
1124 | 4.90k | if (isReservedAtGlobalScope(Status) && !isReservedInAllContexts(Status)) { |
1125 | | // This name is only reserved at global scope. Check if this declaration |
1126 | | // conflicts with a global scope declaration. |
1127 | 8 | if (isa<ParmVarDecl>(this) || isTemplateParameter()) |
1128 | 0 | return ReservedIdentifierStatus::NotReserved; |
1129 | | |
1130 | | // C++ [dcl.link]/7: |
1131 | | // Two declarations [conflict] if [...] one declares a function or |
1132 | | // variable with C language linkage, and the other declares [...] a |
1133 | | // variable that belongs to the global scope. |
1134 | | // |
1135 | | // Therefore names that are reserved at global scope are also reserved as |
1136 | | // names of variables and functions with C language linkage. |
1137 | 8 | const DeclContext *DC = getDeclContext()->getRedeclContext(); |
1138 | 8 | if (DC->isTranslationUnit()) |
1139 | 8 | return Status; |
1140 | 0 | if (auto *VD = dyn_cast<VarDecl>(this)) |
1141 | 0 | if (VD->isExternC()) |
1142 | 0 | return ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC; |
1143 | 0 | if (auto *FD = dyn_cast<FunctionDecl>(this)) |
1144 | 0 | if (FD->isExternC()) |
1145 | 0 | return ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC; |
1146 | 0 | return ReservedIdentifierStatus::NotReserved; |
1147 | 0 | } |
1148 | | |
1149 | 4.89k | return Status; |
1150 | 4.90k | } |
1151 | | |
1152 | 0 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
1153 | 0 | StringRef name = getName(); |
1154 | 0 | if (name.empty()) return SFF_None; |
1155 | | |
1156 | 0 | if (name.front() == 'C') |
1157 | 0 | if (name == "CFStringCreateWithFormat" || |
1158 | 0 | name == "CFStringCreateWithFormatAndArguments" || |
1159 | 0 | name == "CFStringAppendFormat" || |
1160 | 0 | name == "CFStringAppendFormatAndArguments") |
1161 | 0 | return SFF_CFString; |
1162 | 0 | return SFF_None; |
1163 | 0 | } |
1164 | | |
1165 | 7.41k | Linkage NamedDecl::getLinkageInternal() const { |
1166 | | // We don't care about visibility here, so ask for the cheapest |
1167 | | // possible visibility analysis. |
1168 | 7.41k | return LinkageComputer{} |
1169 | 7.41k | .getLVForDecl(this, LVComputationKind::forLinkageOnly()) |
1170 | 7.41k | .getLinkage(); |
1171 | 7.41k | } |
1172 | | |
1173 | | /// Determine whether D is attached to a named module. |
1174 | 572 | static bool isInNamedModule(const NamedDecl *D) { |
1175 | 572 | if (auto *M = D->getOwningModule()) |
1176 | 0 | return M->isNamedModule(); |
1177 | 572 | return false; |
1178 | 572 | } |
1179 | | |
1180 | 0 | static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { |
1181 | | // FIXME: Handle isModulePrivate. |
1182 | 0 | switch (D->getModuleOwnershipKind()) { |
1183 | 0 | case Decl::ModuleOwnershipKind::Unowned: |
1184 | 0 | case Decl::ModuleOwnershipKind::ReachableWhenImported: |
1185 | 0 | case Decl::ModuleOwnershipKind::ModulePrivate: |
1186 | 0 | return false; |
1187 | 0 | case Decl::ModuleOwnershipKind::Visible: |
1188 | 0 | case Decl::ModuleOwnershipKind::VisibleWhenImported: |
1189 | 0 | return isInNamedModule(D); |
1190 | 0 | } |
1191 | 0 | llvm_unreachable("unexpected module ownership kind"); |
1192 | 0 | } |
1193 | | |
1194 | | /// Get the linkage from a semantic point of view. Entities in |
1195 | | /// anonymous namespaces are external (in c++98). |
1196 | 572 | Linkage NamedDecl::getFormalLinkage() const { |
1197 | 572 | Linkage InternalLinkage = getLinkageInternal(); |
1198 | | |
1199 | | // C++ [basic.link]p4.8: |
1200 | | // - if the declaration of the name is attached to a named module and is not |
1201 | | // exported |
1202 | | // the name has module linkage; |
1203 | | // |
1204 | | // [basic.namespace.general]/p2 |
1205 | | // A namespace is never attached to a named module and never has a name with |
1206 | | // module linkage. |
1207 | 572 | if (isInNamedModule(this) && InternalLinkage == Linkage::External && |
1208 | 572 | !isExportedFromModuleInterfaceUnit( |
1209 | 0 | cast<NamedDecl>(this->getCanonicalDecl())) && |
1210 | 572 | !isa<NamespaceDecl>(this)) |
1211 | 0 | InternalLinkage = Linkage::Module; |
1212 | | |
1213 | 572 | return clang::getFormalLinkage(InternalLinkage); |
1214 | 572 | } |
1215 | | |
1216 | 0 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
1217 | 0 | return LinkageComputer{}.getDeclLinkageAndVisibility(this); |
1218 | 0 | } |
1219 | | |
1220 | | static std::optional<Visibility> |
1221 | | getExplicitVisibilityAux(const NamedDecl *ND, |
1222 | | NamedDecl::ExplicitVisibilityKind kind, |
1223 | 0 | bool IsMostRecent) { |
1224 | 0 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
1225 | | |
1226 | | // Check the declaration itself first. |
1227 | 0 | if (std::optional<Visibility> V = getVisibilityOf(ND, kind)) |
1228 | 0 | return V; |
1229 | | |
1230 | | // If this is a member class of a specialization of a class template |
1231 | | // and the corresponding decl has explicit visibility, use that. |
1232 | 0 | if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { |
1233 | 0 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
1234 | 0 | if (InstantiatedFrom) |
1235 | 0 | return getVisibilityOf(InstantiatedFrom, kind); |
1236 | 0 | } |
1237 | | |
1238 | | // If there wasn't explicit visibility there, and this is a |
1239 | | // specialization of a class template, check for visibility |
1240 | | // on the pattern. |
1241 | 0 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
1242 | | // Walk all the template decl till this point to see if there are |
1243 | | // explicit visibility attributes. |
1244 | 0 | const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); |
1245 | 0 | while (TD != nullptr) { |
1246 | 0 | auto Vis = getVisibilityOf(TD, kind); |
1247 | 0 | if (Vis != std::nullopt) |
1248 | 0 | return Vis; |
1249 | 0 | TD = TD->getPreviousDecl(); |
1250 | 0 | } |
1251 | 0 | return std::nullopt; |
1252 | 0 | } |
1253 | | |
1254 | | // Use the most recent declaration. |
1255 | 0 | if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { |
1256 | 0 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
1257 | 0 | if (MostRecent != ND) |
1258 | 0 | return getExplicitVisibilityAux(MostRecent, kind, true); |
1259 | 0 | } |
1260 | | |
1261 | 0 | if (const auto *Var = dyn_cast<VarDecl>(ND)) { |
1262 | 0 | if (Var->isStaticDataMember()) { |
1263 | 0 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
1264 | 0 | if (InstantiatedFrom) |
1265 | 0 | return getVisibilityOf(InstantiatedFrom, kind); |
1266 | 0 | } |
1267 | | |
1268 | 0 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) |
1269 | 0 | return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
1270 | 0 | kind); |
1271 | | |
1272 | 0 | return std::nullopt; |
1273 | 0 | } |
1274 | | // Also handle function template specializations. |
1275 | 0 | if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { |
1276 | | // If the function is a specialization of a template with an |
1277 | | // explicit visibility attribute, use that. |
1278 | 0 | if (FunctionTemplateSpecializationInfo *templateInfo |
1279 | 0 | = fn->getTemplateSpecializationInfo()) |
1280 | 0 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
1281 | 0 | kind); |
1282 | | |
1283 | | // If the function is a member of a specialization of a class template |
1284 | | // and the corresponding decl has explicit visibility, use that. |
1285 | 0 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
1286 | 0 | if (InstantiatedFrom) |
1287 | 0 | return getVisibilityOf(InstantiatedFrom, kind); |
1288 | | |
1289 | 0 | return std::nullopt; |
1290 | 0 | } |
1291 | | |
1292 | | // The visibility of a template is stored in the templated decl. |
1293 | 0 | if (const auto *TD = dyn_cast<TemplateDecl>(ND)) |
1294 | 0 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
1295 | | |
1296 | 0 | return std::nullopt; |
1297 | 0 | } |
1298 | | |
1299 | | std::optional<Visibility> |
1300 | 0 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
1301 | 0 | return getExplicitVisibilityAux(this, kind, false); |
1302 | 0 | } |
1303 | | |
1304 | | LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, |
1305 | | Decl *ContextDecl, |
1306 | 0 | LVComputationKind computation) { |
1307 | | // This lambda has its linkage/visibility determined by its owner. |
1308 | 0 | const NamedDecl *Owner; |
1309 | 0 | if (!ContextDecl) |
1310 | 0 | Owner = dyn_cast<NamedDecl>(DC); |
1311 | 0 | else if (isa<ParmVarDecl>(ContextDecl)) |
1312 | 0 | Owner = |
1313 | 0 | dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); |
1314 | 0 | else if (isa<ImplicitConceptSpecializationDecl>(ContextDecl)) { |
1315 | | // Replace with the concept's owning decl, which is either a namespace or a |
1316 | | // TU, so this needs a dyn_cast. |
1317 | 0 | Owner = dyn_cast<NamedDecl>(ContextDecl->getDeclContext()); |
1318 | 0 | } else { |
1319 | 0 | Owner = cast<NamedDecl>(ContextDecl); |
1320 | 0 | } |
1321 | |
|
1322 | 0 | if (!Owner) |
1323 | 0 | return LinkageInfo::none(); |
1324 | | |
1325 | | // If the owner has a deduced type, we need to skip querying the linkage and |
1326 | | // visibility of that type, because it might involve this closure type. The |
1327 | | // only effect of this is that we might give a lambda VisibleNoLinkage rather |
1328 | | // than NoLinkage when we don't strictly need to, which is benign. |
1329 | 0 | auto *VD = dyn_cast<VarDecl>(Owner); |
1330 | 0 | LinkageInfo OwnerLV = |
1331 | 0 | VD && VD->getType()->getContainedDeducedType() |
1332 | 0 | ? computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true) |
1333 | 0 | : getLVForDecl(Owner, computation); |
1334 | | |
1335 | | // A lambda never formally has linkage. But if the owner is externally |
1336 | | // visible, then the lambda is too. We apply the same rules to blocks. |
1337 | 0 | if (!isExternallyVisible(OwnerLV.getLinkage())) |
1338 | 0 | return LinkageInfo::none(); |
1339 | 0 | return LinkageInfo(Linkage::VisibleNone, OwnerLV.getVisibility(), |
1340 | 0 | OwnerLV.isVisibilityExplicit()); |
1341 | 0 | } |
1342 | | |
1343 | | LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, |
1344 | 0 | LVComputationKind computation) { |
1345 | 0 | if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
1346 | 0 | if (Function->isInAnonymousNamespace() && |
1347 | 0 | !isFirstInExternCContext(Function)) |
1348 | 0 | return LinkageInfo::internal(); |
1349 | | |
1350 | | // This is a "void f();" which got merged with a file static. |
1351 | 0 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
1352 | 0 | return LinkageInfo::internal(); |
1353 | | |
1354 | 0 | LinkageInfo LV; |
1355 | 0 | if (!hasExplicitVisibilityAlready(computation)) { |
1356 | 0 | if (std::optional<Visibility> Vis = |
1357 | 0 | getExplicitVisibility(Function, computation)) |
1358 | 0 | LV.mergeVisibility(*Vis, true); |
1359 | 0 | } |
1360 | | |
1361 | | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
1362 | | // merging storage classes and visibility attributes, so we don't have to |
1363 | | // look at previous decls in here. |
1364 | |
|
1365 | 0 | return LV; |
1366 | 0 | } |
1367 | | |
1368 | 0 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
1369 | 0 | if (Var->hasExternalStorage()) { |
1370 | 0 | if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) |
1371 | 0 | return LinkageInfo::internal(); |
1372 | | |
1373 | 0 | LinkageInfo LV; |
1374 | 0 | if (Var->getStorageClass() == SC_PrivateExtern) |
1375 | 0 | LV.mergeVisibility(HiddenVisibility, true); |
1376 | 0 | else if (!hasExplicitVisibilityAlready(computation)) { |
1377 | 0 | if (std::optional<Visibility> Vis = |
1378 | 0 | getExplicitVisibility(Var, computation)) |
1379 | 0 | LV.mergeVisibility(*Vis, true); |
1380 | 0 | } |
1381 | |
|
1382 | 0 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
1383 | 0 | LinkageInfo PrevLV = getLVForDecl(Prev, computation); |
1384 | 0 | if (PrevLV.getLinkage() != Linkage::Invalid) |
1385 | 0 | LV.setLinkage(PrevLV.getLinkage()); |
1386 | 0 | LV.mergeVisibility(PrevLV); |
1387 | 0 | } |
1388 | |
|
1389 | 0 | return LV; |
1390 | 0 | } |
1391 | | |
1392 | 0 | if (!Var->isStaticLocal()) |
1393 | 0 | return LinkageInfo::none(); |
1394 | 0 | } |
1395 | | |
1396 | 0 | ASTContext &Context = D->getASTContext(); |
1397 | 0 | if (!Context.getLangOpts().CPlusPlus) |
1398 | 0 | return LinkageInfo::none(); |
1399 | | |
1400 | 0 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
1401 | 0 | if (!OuterD || OuterD->isInvalidDecl()) |
1402 | 0 | return LinkageInfo::none(); |
1403 | | |
1404 | 0 | LinkageInfo LV; |
1405 | 0 | if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { |
1406 | 0 | if (!BD->getBlockManglingNumber()) |
1407 | 0 | return LinkageInfo::none(); |
1408 | | |
1409 | 0 | LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), |
1410 | 0 | BD->getBlockManglingContextDecl(), computation); |
1411 | 0 | } else { |
1412 | 0 | const auto *FD = cast<FunctionDecl>(OuterD); |
1413 | 0 | if (!FD->isInlined() && |
1414 | 0 | !isTemplateInstantiation(FD->getTemplateSpecializationKind())) |
1415 | 0 | return LinkageInfo::none(); |
1416 | | |
1417 | | // If a function is hidden by -fvisibility-inlines-hidden option and |
1418 | | // is not explicitly attributed as a hidden function, |
1419 | | // we should not make static local variables in the function hidden. |
1420 | 0 | LV = getLVForDecl(FD, computation); |
1421 | 0 | if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && |
1422 | 0 | !LV.isVisibilityExplicit() && |
1423 | 0 | !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) { |
1424 | 0 | assert(cast<VarDecl>(D)->isStaticLocal()); |
1425 | | // If this was an implicitly hidden inline method, check again for |
1426 | | // explicit visibility on the parent class, and use that for static locals |
1427 | | // if present. |
1428 | 0 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
1429 | 0 | LV = getLVForDecl(MD->getParent(), computation); |
1430 | 0 | if (!LV.isVisibilityExplicit()) { |
1431 | 0 | Visibility globalVisibility = |
1432 | 0 | computation.isValueVisibility() |
1433 | 0 | ? Context.getLangOpts().getValueVisibilityMode() |
1434 | 0 | : Context.getLangOpts().getTypeVisibilityMode(); |
1435 | 0 | return LinkageInfo(Linkage::VisibleNone, globalVisibility, |
1436 | 0 | /*visibilityExplicit=*/false); |
1437 | 0 | } |
1438 | 0 | } |
1439 | 0 | } |
1440 | 0 | if (!isExternallyVisible(LV.getLinkage())) |
1441 | 0 | return LinkageInfo::none(); |
1442 | 0 | return LinkageInfo(Linkage::VisibleNone, LV.getVisibility(), |
1443 | 0 | LV.isVisibilityExplicit()); |
1444 | 0 | } |
1445 | | |
1446 | | LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, |
1447 | | LVComputationKind computation, |
1448 | 5.08k | bool IgnoreVarTypeLinkage) { |
1449 | | // Internal_linkage attribute overrides other considerations. |
1450 | 5.08k | if (D->hasAttr<InternalLinkageAttr>()) |
1451 | 0 | return LinkageInfo::internal(); |
1452 | | |
1453 | | // Objective-C: treat all Objective-C declarations as having external |
1454 | | // linkage. |
1455 | 5.08k | switch (D->getKind()) { |
1456 | 5.08k | default: |
1457 | 5.08k | break; |
1458 | | |
1459 | | // Per C++ [basic.link]p2, only the names of objects, references, |
1460 | | // functions, types, templates, namespaces, and values ever have linkage. |
1461 | | // |
1462 | | // Note that the name of a typedef, namespace alias, using declaration, |
1463 | | // and so on are not the name of the corresponding type, namespace, or |
1464 | | // declaration, so they do *not* have linkage. |
1465 | 5.08k | case Decl::ImplicitParam: |
1466 | 0 | case Decl::Label: |
1467 | 0 | case Decl::NamespaceAlias: |
1468 | 0 | case Decl::ParmVar: |
1469 | 0 | case Decl::Using: |
1470 | 0 | case Decl::UsingEnum: |
1471 | 0 | case Decl::UsingShadow: |
1472 | 0 | case Decl::UsingDirective: |
1473 | 0 | return LinkageInfo::none(); |
1474 | | |
1475 | 0 | case Decl::EnumConstant: |
1476 | | // C++ [basic.link]p4: an enumerator has the linkage of its enumeration. |
1477 | 0 | if (D->getASTContext().getLangOpts().CPlusPlus) |
1478 | 0 | return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); |
1479 | 0 | return LinkageInfo::visible_none(); |
1480 | | |
1481 | 0 | case Decl::Typedef: |
1482 | 0 | case Decl::TypeAlias: |
1483 | | // A typedef declaration has linkage if it gives a type a name for |
1484 | | // linkage purposes. |
1485 | 0 | if (!cast<TypedefNameDecl>(D) |
1486 | 0 | ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
1487 | 0 | return LinkageInfo::none(); |
1488 | 0 | break; |
1489 | | |
1490 | 0 | case Decl::TemplateTemplateParm: // count these as external |
1491 | 0 | case Decl::NonTypeTemplateParm: |
1492 | 0 | case Decl::ObjCAtDefsField: |
1493 | 0 | case Decl::ObjCCategory: |
1494 | 0 | case Decl::ObjCCategoryImpl: |
1495 | 0 | case Decl::ObjCCompatibleAlias: |
1496 | 0 | case Decl::ObjCImplementation: |
1497 | 0 | case Decl::ObjCMethod: |
1498 | 0 | case Decl::ObjCProperty: |
1499 | 0 | case Decl::ObjCPropertyImpl: |
1500 | 0 | case Decl::ObjCProtocol: |
1501 | 0 | return getExternalLinkageFor(D); |
1502 | | |
1503 | 0 | case Decl::CXXRecord: { |
1504 | 0 | const auto *Record = cast<CXXRecordDecl>(D); |
1505 | 0 | if (Record->isLambda()) { |
1506 | 0 | if (Record->hasKnownLambdaInternalLinkage() || |
1507 | 0 | !Record->getLambdaManglingNumber()) { |
1508 | | // This lambda has no mangling number, so it's internal. |
1509 | 0 | return LinkageInfo::internal(); |
1510 | 0 | } |
1511 | | |
1512 | 0 | return getLVForClosure( |
1513 | 0 | Record->getDeclContext()->getRedeclContext(), |
1514 | 0 | Record->getLambdaContextDecl(), computation); |
1515 | 0 | } |
1516 | | |
1517 | 0 | break; |
1518 | 0 | } |
1519 | | |
1520 | 0 | case Decl::TemplateParamObject: { |
1521 | | // The template parameter object can be referenced from anywhere its type |
1522 | | // and value can be referenced. |
1523 | 0 | auto *TPO = cast<TemplateParamObjectDecl>(D); |
1524 | 0 | LinkageInfo LV = getLVForType(*TPO->getType(), computation); |
1525 | 0 | LV.merge(getLVForValue(TPO->getValue(), computation)); |
1526 | 0 | return LV; |
1527 | 0 | } |
1528 | 5.08k | } |
1529 | | |
1530 | | // Handle linkage for namespace-scope names. |
1531 | 5.08k | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
1532 | 5.08k | return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); |
1533 | | |
1534 | | // C++ [basic.link]p5: |
1535 | | // In addition, a member function, static data member, a named |
1536 | | // class or enumeration of class scope, or an unnamed class or |
1537 | | // enumeration defined in a class-scope typedef declaration such |
1538 | | // that the class or enumeration has the typedef name for linkage |
1539 | | // purposes (7.1.3), has external linkage if the name of the class |
1540 | | // has external linkage. |
1541 | 0 | if (D->getDeclContext()->isRecord()) |
1542 | 0 | return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); |
1543 | | |
1544 | | // C++ [basic.link]p6: |
1545 | | // The name of a function declared in block scope and the name of |
1546 | | // an object declared by a block scope extern declaration have |
1547 | | // linkage. If there is a visible declaration of an entity with |
1548 | | // linkage having the same name and type, ignoring entities |
1549 | | // declared outside the innermost enclosing namespace scope, the |
1550 | | // block scope declaration declares that same entity and receives |
1551 | | // the linkage of the previous declaration. If there is more than |
1552 | | // one such matching entity, the program is ill-formed. Otherwise, |
1553 | | // if no matching entity is found, the block scope entity receives |
1554 | | // external linkage. |
1555 | 0 | if (D->getDeclContext()->isFunctionOrMethod()) |
1556 | 0 | return getLVForLocalDecl(D, computation); |
1557 | | |
1558 | | // C++ [basic.link]p6: |
1559 | | // Names not covered by these rules have no linkage. |
1560 | 0 | return LinkageInfo::none(); |
1561 | 0 | } |
1562 | | |
1563 | | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
1564 | | LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, |
1565 | 7.41k | LVComputationKind computation) { |
1566 | | // Internal_linkage attribute overrides other considerations. |
1567 | 7.41k | if (D->hasAttr<InternalLinkageAttr>()) |
1568 | 0 | return LinkageInfo::internal(); |
1569 | | |
1570 | 7.41k | if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) |
1571 | 2.32k | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
1572 | | |
1573 | 5.08k | if (std::optional<LinkageInfo> LI = lookup(D, computation)) |
1574 | 0 | return *LI; |
1575 | | |
1576 | 5.08k | LinkageInfo LV = computeLVForDecl(D, computation); |
1577 | 5.08k | if (D->hasCachedLinkage()) |
1578 | 0 | assert(D->getCachedLinkage() == LV.getLinkage()); |
1579 | | |
1580 | 0 | D->setCachedLinkage(LV.getLinkage()); |
1581 | 5.08k | cache(D, computation, LV); |
1582 | | |
1583 | 5.08k | #ifndef NDEBUG |
1584 | | // In C (because of gnu inline) and in c++ with microsoft extensions an |
1585 | | // static can follow an extern, so we can have two decls with different |
1586 | | // linkages. |
1587 | 5.08k | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
1588 | 5.08k | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
1589 | 2.50k | return LV; |
1590 | | |
1591 | | // We have just computed the linkage for this decl. By induction we know |
1592 | | // that all other computed linkages match, check that the one we just |
1593 | | // computed also does. |
1594 | 2.58k | NamedDecl *Old = nullptr; |
1595 | 2.58k | for (auto *I : D->redecls()) { |
1596 | 2.58k | auto *T = cast<NamedDecl>(I); |
1597 | 2.58k | if (T == D) |
1598 | 2.58k | continue; |
1599 | 0 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
1600 | 0 | Old = T; |
1601 | 0 | break; |
1602 | 0 | } |
1603 | 0 | } |
1604 | 2.58k | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
1605 | 0 | #endif |
1606 | | |
1607 | 0 | return LV; |
1608 | 5.08k | } |
1609 | | |
1610 | 0 | LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { |
1611 | 0 | NamedDecl::ExplicitVisibilityKind EK = usesTypeVisibility(D) |
1612 | 0 | ? NamedDecl::VisibilityForType |
1613 | 0 | : NamedDecl::VisibilityForValue; |
1614 | 0 | LVComputationKind CK(EK); |
1615 | 0 | return getLVForDecl(D, D->getASTContext().getLangOpts().IgnoreXCOFFVisibility |
1616 | 0 | ? CK.forLinkageOnly() |
1617 | 0 | : CK); |
1618 | 0 | } |
1619 | | |
1620 | 5.08k | Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { |
1621 | 5.08k | if (isa<NamespaceDecl>(this)) |
1622 | | // Namespaces never have module linkage. It is the entities within them |
1623 | | // that [may] do. |
1624 | 0 | return nullptr; |
1625 | | |
1626 | 5.08k | Module *M = getOwningModule(); |
1627 | 5.08k | if (!M) |
1628 | 5.08k | return nullptr; |
1629 | | |
1630 | 0 | switch (M->Kind) { |
1631 | 0 | case Module::ModuleMapModule: |
1632 | | // Module map modules have no special linkage semantics. |
1633 | 0 | return nullptr; |
1634 | | |
1635 | 0 | case Module::ModuleInterfaceUnit: |
1636 | 0 | case Module::ModuleImplementationUnit: |
1637 | 0 | case Module::ModulePartitionInterface: |
1638 | 0 | case Module::ModulePartitionImplementation: |
1639 | 0 | return M; |
1640 | | |
1641 | 0 | case Module::ModuleHeaderUnit: |
1642 | 0 | case Module::ExplicitGlobalModuleFragment: |
1643 | 0 | case Module::ImplicitGlobalModuleFragment: { |
1644 | | // External linkage declarations in the global module have no owning module |
1645 | | // for linkage purposes. But internal linkage declarations in the global |
1646 | | // module fragment of a particular module are owned by that module for |
1647 | | // linkage purposes. |
1648 | | // FIXME: p1815 removes the need for this distinction -- there are no |
1649 | | // internal linkage declarations that need to be referred to from outside |
1650 | | // this TU. |
1651 | 0 | if (IgnoreLinkage) |
1652 | 0 | return nullptr; |
1653 | 0 | bool InternalLinkage; |
1654 | 0 | if (auto *ND = dyn_cast<NamedDecl>(this)) |
1655 | 0 | InternalLinkage = !ND->hasExternalFormalLinkage(); |
1656 | 0 | else |
1657 | 0 | InternalLinkage = isInAnonymousNamespace(); |
1658 | 0 | return InternalLinkage ? M->Kind == Module::ModuleHeaderUnit ? M : M->Parent |
1659 | 0 | : nullptr; |
1660 | 0 | } |
1661 | | |
1662 | 0 | case Module::PrivateModuleFragment: |
1663 | | // The private module fragment is part of its containing module for linkage |
1664 | | // purposes. |
1665 | 0 | return M->Parent; |
1666 | 0 | } |
1667 | | |
1668 | 0 | llvm_unreachable("unknown module kind"); |
1669 | 0 | } |
1670 | | |
1671 | 0 | void NamedDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { |
1672 | 0 | Name.print(OS, Policy); |
1673 | 0 | } |
1674 | | |
1675 | 46 | void NamedDecl::printName(raw_ostream &OS) const { |
1676 | 46 | printName(OS, getASTContext().getPrintingPolicy()); |
1677 | 46 | } |
1678 | | |
1679 | 0 | std::string NamedDecl::getQualifiedNameAsString() const { |
1680 | 0 | std::string QualName; |
1681 | 0 | llvm::raw_string_ostream OS(QualName); |
1682 | 0 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
1683 | 0 | return QualName; |
1684 | 0 | } |
1685 | | |
1686 | 0 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
1687 | 0 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
1688 | 0 | } |
1689 | | |
1690 | | void NamedDecl::printQualifiedName(raw_ostream &OS, |
1691 | 46 | const PrintingPolicy &P) const { |
1692 | 46 | if (getDeclContext()->isFunctionOrMethod()) { |
1693 | | // We do not print '(anonymous)' for function parameters without name. |
1694 | 0 | printName(OS, P); |
1695 | 0 | return; |
1696 | 0 | } |
1697 | 46 | printNestedNameSpecifier(OS, P); |
1698 | 46 | if (getDeclName()) |
1699 | 46 | OS << *this; |
1700 | 0 | else { |
1701 | | // Give the printName override a chance to pick a different name before we |
1702 | | // fall back to "(anonymous)". |
1703 | 0 | SmallString<64> NameBuffer; |
1704 | 0 | llvm::raw_svector_ostream NameOS(NameBuffer); |
1705 | 0 | printName(NameOS, P); |
1706 | 0 | if (NameBuffer.empty()) |
1707 | 0 | OS << "(anonymous)"; |
1708 | 0 | else |
1709 | 0 | OS << NameBuffer; |
1710 | 0 | } |
1711 | 46 | } |
1712 | | |
1713 | 0 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS) const { |
1714 | 0 | printNestedNameSpecifier(OS, getASTContext().getPrintingPolicy()); |
1715 | 0 | } |
1716 | | |
1717 | | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS, |
1718 | 46 | const PrintingPolicy &P) const { |
1719 | 46 | const DeclContext *Ctx = getDeclContext(); |
1720 | | |
1721 | | // For ObjC methods and properties, look through categories and use the |
1722 | | // interface as context. |
1723 | 46 | if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) { |
1724 | 0 | if (auto *ID = MD->getClassInterface()) |
1725 | 0 | Ctx = ID; |
1726 | 46 | } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { |
1727 | 0 | if (auto *MD = PD->getGetterMethodDecl()) |
1728 | 0 | if (auto *ID = MD->getClassInterface()) |
1729 | 0 | Ctx = ID; |
1730 | 46 | } else if (auto *ID = dyn_cast<ObjCIvarDecl>(this)) { |
1731 | 0 | if (auto *CI = ID->getContainingInterface()) |
1732 | 0 | Ctx = CI; |
1733 | 0 | } |
1734 | | |
1735 | 46 | if (Ctx->isFunctionOrMethod()) |
1736 | 0 | return; |
1737 | | |
1738 | 46 | using ContextsTy = SmallVector<const DeclContext *, 8>; |
1739 | 46 | ContextsTy Contexts; |
1740 | | |
1741 | | // Collect named contexts. |
1742 | 46 | DeclarationName NameInScope = getDeclName(); |
1743 | 92 | for (; Ctx; Ctx = Ctx->getParent()) { |
1744 | | // Suppress anonymous namespace if requested. |
1745 | 46 | if (P.SuppressUnwrittenScope && isa<NamespaceDecl>(Ctx) && |
1746 | 46 | cast<NamespaceDecl>(Ctx)->isAnonymousNamespace()) |
1747 | 0 | continue; |
1748 | | |
1749 | | // Suppress inline namespace if it doesn't make the result ambiguous. |
1750 | 46 | if (P.SuppressInlineNamespace && Ctx->isInlineNamespace() && NameInScope && |
1751 | 46 | cast<NamespaceDecl>(Ctx)->isRedundantInlineQualifierFor(NameInScope)) |
1752 | 0 | continue; |
1753 | | |
1754 | | // Skip non-named contexts such as linkage specifications and ExportDecls. |
1755 | 46 | const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx); |
1756 | 46 | if (!ND) |
1757 | 46 | continue; |
1758 | | |
1759 | 0 | Contexts.push_back(Ctx); |
1760 | 0 | NameInScope = ND->getDeclName(); |
1761 | 0 | } |
1762 | | |
1763 | 46 | for (const DeclContext *DC : llvm::reverse(Contexts)) { |
1764 | 0 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
1765 | 0 | OS << Spec->getName(); |
1766 | 0 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
1767 | 0 | printTemplateArgumentList( |
1768 | 0 | OS, TemplateArgs.asArray(), P, |
1769 | 0 | Spec->getSpecializedTemplate()->getTemplateParameters()); |
1770 | 0 | } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
1771 | 0 | if (ND->isAnonymousNamespace()) { |
1772 | 0 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
1773 | 0 | : "(anonymous namespace)"); |
1774 | 0 | } |
1775 | 0 | else |
1776 | 0 | OS << *ND; |
1777 | 0 | } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { |
1778 | 0 | if (!RD->getIdentifier()) |
1779 | 0 | OS << "(anonymous " << RD->getKindName() << ')'; |
1780 | 0 | else |
1781 | 0 | OS << *RD; |
1782 | 0 | } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { |
1783 | 0 | const FunctionProtoType *FT = nullptr; |
1784 | 0 | if (FD->hasWrittenPrototype()) |
1785 | 0 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
1786 | |
|
1787 | 0 | OS << *FD << '('; |
1788 | 0 | if (FT) { |
1789 | 0 | unsigned NumParams = FD->getNumParams(); |
1790 | 0 | for (unsigned i = 0; i < NumParams; ++i) { |
1791 | 0 | if (i) |
1792 | 0 | OS << ", "; |
1793 | 0 | OS << FD->getParamDecl(i)->getType().stream(P); |
1794 | 0 | } |
1795 | |
|
1796 | 0 | if (FT->isVariadic()) { |
1797 | 0 | if (NumParams > 0) |
1798 | 0 | OS << ", "; |
1799 | 0 | OS << "..."; |
1800 | 0 | } |
1801 | 0 | } |
1802 | 0 | OS << ')'; |
1803 | 0 | } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { |
1804 | | // C++ [dcl.enum]p10: Each enum-name and each unscoped |
1805 | | // enumerator is declared in the scope that immediately contains |
1806 | | // the enum-specifier. Each scoped enumerator is declared in the |
1807 | | // scope of the enumeration. |
1808 | | // For the case of unscoped enumerator, do not include in the qualified |
1809 | | // name any information about its enum enclosing scope, as its visibility |
1810 | | // is global. |
1811 | 0 | if (ED->isScoped()) |
1812 | 0 | OS << *ED; |
1813 | 0 | else |
1814 | 0 | continue; |
1815 | 0 | } else { |
1816 | 0 | OS << *cast<NamedDecl>(DC); |
1817 | 0 | } |
1818 | 0 | OS << "::"; |
1819 | 0 | } |
1820 | 46 | } |
1821 | | |
1822 | | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
1823 | | const PrintingPolicy &Policy, |
1824 | 0 | bool Qualified) const { |
1825 | 0 | if (Qualified) |
1826 | 0 | printQualifiedName(OS, Policy); |
1827 | 0 | else |
1828 | 0 | printName(OS, Policy); |
1829 | 0 | } |
1830 | | |
1831 | 22.5k | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
1832 | 22.5k | return true; |
1833 | 22.5k | } Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::TranslationUnitDecl>(clang::Redeclarable<clang::TranslationUnitDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::ObjCProtocolDecl>(clang::Redeclarable<clang::ObjCProtocolDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::ObjCInterfaceDecl>(clang::Redeclarable<clang::ObjCInterfaceDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::NamespaceDecl>(clang::Redeclarable<clang::NamespaceDecl>*) Decl.cpp:bool isRedeclarableImpl<clang::FunctionDecl>(clang::Redeclarable<clang::FunctionDecl>*) Line | Count | Source | 1831 | 1 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { | 1832 | 1 | return true; | 1833 | 1 | } |
Decl.cpp:bool isRedeclarableImpl<clang::VarDecl>(clang::Redeclarable<clang::VarDecl>*) Line | Count | Source | 1831 | 22.5k | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { | 1832 | 22.5k | return true; | 1833 | 22.5k | } |
Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::UsingShadowDecl>(clang::Redeclarable<clang::UsingShadowDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::TagDecl>(clang::Redeclarable<clang::TagDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::TypedefNameDecl>(clang::Redeclarable<clang::TypedefNameDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::RedeclarableTemplateDecl>(clang::Redeclarable<clang::RedeclarableTemplateDecl>*) Unexecuted instantiation: Decl.cpp:bool isRedeclarableImpl<clang::NamespaceAliasDecl>(clang::Redeclarable<clang::NamespaceAliasDecl>*) |
1834 | 0 | static bool isRedeclarableImpl(...) { return false; } |
1835 | 22.5k | static bool isRedeclarable(Decl::Kind K) { |
1836 | 22.5k | switch (K) { |
1837 | 0 | #define DECL(Type, Base) \ |
1838 | 22.5k | case Decl::Type: \ |
1839 | 22.5k | return isRedeclarableImpl((Type##Decl *)nullptr); |
1840 | 0 | #define ABSTRACT_DECL(DECL) |
1841 | 22.5k | #include "clang/AST/DeclNodes.inc" |
1842 | 22.5k | } |
1843 | 0 | llvm_unreachable("unknown decl kind"); |
1844 | 0 | } |
1845 | | |
1846 | | bool NamedDecl::declarationReplaces(const NamedDecl *OldD, |
1847 | 22.5k | bool IsKnownNewer) const { |
1848 | 22.5k | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
1849 | | |
1850 | | // Never replace one imported declaration with another; we need both results |
1851 | | // when re-exporting. |
1852 | 22.5k | if (OldD->isFromASTFile() && isFromASTFile()) |
1853 | 0 | return false; |
1854 | | |
1855 | | // A kind mismatch implies that the declaration is not replaced. |
1856 | 22.5k | if (OldD->getKind() != getKind()) |
1857 | 16 | return false; |
1858 | | |
1859 | | // For method declarations, we never replace. (Why?) |
1860 | 22.5k | if (isa<ObjCMethodDecl>(this)) |
1861 | 0 | return false; |
1862 | | |
1863 | | // For parameters, pick the newer one. This is either an error or (in |
1864 | | // Objective-C) permitted as an extension. |
1865 | 22.5k | if (isa<ParmVarDecl>(this)) |
1866 | 0 | return true; |
1867 | | |
1868 | | // Inline namespaces can give us two declarations with the same |
1869 | | // name and kind in the same scope but different contexts; we should |
1870 | | // keep both declarations in this case. |
1871 | 22.5k | if (!this->getDeclContext()->getRedeclContext()->Equals( |
1872 | 22.5k | OldD->getDeclContext()->getRedeclContext())) |
1873 | 0 | return false; |
1874 | | |
1875 | | // Using declarations can be replaced if they import the same name from the |
1876 | | // same context. |
1877 | 22.5k | if (const auto *UD = dyn_cast<UsingDecl>(this)) { |
1878 | 0 | ASTContext &Context = getASTContext(); |
1879 | 0 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
1880 | 0 | Context.getCanonicalNestedNameSpecifier( |
1881 | 0 | cast<UsingDecl>(OldD)->getQualifier()); |
1882 | 0 | } |
1883 | 22.5k | if (const auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
1884 | 0 | ASTContext &Context = getASTContext(); |
1885 | 0 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
1886 | 0 | Context.getCanonicalNestedNameSpecifier( |
1887 | 0 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
1888 | 0 | } |
1889 | | |
1890 | 22.5k | if (isRedeclarable(getKind())) { |
1891 | 22.5k | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
1892 | 22.3k | return false; |
1893 | | |
1894 | 183 | if (IsKnownNewer) |
1895 | 183 | return true; |
1896 | | |
1897 | | // Check whether this is actually newer than OldD. We want to keep the |
1898 | | // newer declaration. This loop will usually only iterate once, because |
1899 | | // OldD is usually the previous declaration. |
1900 | 0 | for (const auto *D : redecls()) { |
1901 | 0 | if (D == OldD) |
1902 | 0 | break; |
1903 | | |
1904 | | // If we reach the canonical declaration, then OldD is not actually older |
1905 | | // than this one. |
1906 | | // |
1907 | | // FIXME: In this case, we should not add this decl to the lookup table. |
1908 | 0 | if (D->isCanonicalDecl()) |
1909 | 0 | return false; |
1910 | 0 | } |
1911 | | |
1912 | | // It's a newer declaration of the same kind of declaration in the same |
1913 | | // scope: we want this decl instead of the existing one. |
1914 | 0 | return true; |
1915 | 0 | } |
1916 | | |
1917 | | // In all other cases, we need to keep both declarations in case they have |
1918 | | // different visibility. Any attempt to use the name will result in an |
1919 | | // ambiguity if more than one is visible. |
1920 | 0 | return false; |
1921 | 22.5k | } |
1922 | | |
1923 | 572 | bool NamedDecl::hasLinkage() const { |
1924 | 572 | switch (getFormalLinkage()) { |
1925 | 0 | case Linkage::Invalid: |
1926 | 0 | llvm_unreachable("Linkage hasn't been computed!"); |
1927 | 0 | case Linkage::None: |
1928 | 0 | return false; |
1929 | 0 | case Linkage::Internal: |
1930 | 0 | return true; |
1931 | 0 | case Linkage::UniqueExternal: |
1932 | 0 | case Linkage::VisibleNone: |
1933 | 0 | llvm_unreachable("Non-formal linkage is not allowed here!"); |
1934 | 0 | case Linkage::Module: |
1935 | 572 | case Linkage::External: |
1936 | 572 | return true; |
1937 | 572 | } |
1938 | 0 | llvm_unreachable("Unhandled Linkage enum"); |
1939 | 0 | } |
1940 | | |
1941 | 0 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
1942 | 0 | NamedDecl *ND = this; |
1943 | 0 | if (auto *UD = dyn_cast<UsingShadowDecl>(ND)) |
1944 | 0 | ND = UD->getTargetDecl(); |
1945 | |
|
1946 | 0 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
1947 | 0 | return AD->getClassInterface(); |
1948 | | |
1949 | 0 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) |
1950 | 0 | return AD->getNamespace(); |
1951 | | |
1952 | 0 | return ND; |
1953 | 0 | } |
1954 | | |
1955 | 0 | bool NamedDecl::isCXXInstanceMember() const { |
1956 | 0 | if (!isCXXClassMember()) |
1957 | 0 | return false; |
1958 | | |
1959 | 0 | const NamedDecl *D = this; |
1960 | 0 | if (isa<UsingShadowDecl>(D)) |
1961 | 0 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
1962 | |
|
1963 | 0 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
1964 | 0 | return true; |
1965 | 0 | if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(D->getAsFunction())) |
1966 | 0 | return MD->isInstance(); |
1967 | 0 | return false; |
1968 | 0 | } |
1969 | | |
1970 | | //===----------------------------------------------------------------------===// |
1971 | | // DeclaratorDecl Implementation |
1972 | | //===----------------------------------------------------------------------===// |
1973 | | |
1974 | | template <typename DeclT> |
1975 | 0 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
1976 | 0 | if (decl->getNumTemplateParameterLists() > 0) |
1977 | 0 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
1978 | 0 | return decl->getInnerLocStart(); |
1979 | 0 | } Unexecuted instantiation: Decl.cpp:clang::SourceLocation getTemplateOrInnerLocStart<clang::DeclaratorDecl>(clang::DeclaratorDecl const*) Unexecuted instantiation: Decl.cpp:clang::SourceLocation getTemplateOrInnerLocStart<clang::TagDecl>(clang::TagDecl const*) |
1980 | | |
1981 | 0 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
1982 | 0 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
1983 | 0 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
1984 | 0 | return SourceLocation(); |
1985 | 0 | } |
1986 | | |
1987 | 0 | SourceLocation DeclaratorDecl::getTypeSpecEndLoc() const { |
1988 | 0 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
1989 | 0 | if (TSI) return TSI->getTypeLoc().getEndLoc(); |
1990 | 0 | return SourceLocation(); |
1991 | 0 | } |
1992 | | |
1993 | 0 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
1994 | 0 | if (QualifierLoc) { |
1995 | | // Make sure the extended decl info is allocated. |
1996 | 0 | if (!hasExtInfo()) { |
1997 | | // Save (non-extended) type source info pointer. |
1998 | 0 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
1999 | | // Allocate external info struct. |
2000 | 0 | DeclInfo = new (getASTContext()) ExtInfo; |
2001 | | // Restore savedTInfo into (extended) decl info. |
2002 | 0 | getExtInfo()->TInfo = savedTInfo; |
2003 | 0 | } |
2004 | | // Set qualifier info. |
2005 | 0 | getExtInfo()->QualifierLoc = QualifierLoc; |
2006 | 0 | } else if (hasExtInfo()) { |
2007 | | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
2008 | 0 | getExtInfo()->QualifierLoc = QualifierLoc; |
2009 | 0 | } |
2010 | 0 | } |
2011 | | |
2012 | 0 | void DeclaratorDecl::setTrailingRequiresClause(Expr *TrailingRequiresClause) { |
2013 | 0 | assert(TrailingRequiresClause); |
2014 | | // Make sure the extended decl info is allocated. |
2015 | 0 | if (!hasExtInfo()) { |
2016 | | // Save (non-extended) type source info pointer. |
2017 | 0 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
2018 | | // Allocate external info struct. |
2019 | 0 | DeclInfo = new (getASTContext()) ExtInfo; |
2020 | | // Restore savedTInfo into (extended) decl info. |
2021 | 0 | getExtInfo()->TInfo = savedTInfo; |
2022 | 0 | } |
2023 | | // Set requires clause info. |
2024 | 0 | getExtInfo()->TrailingRequiresClause = TrailingRequiresClause; |
2025 | 0 | } |
2026 | | |
2027 | | void DeclaratorDecl::setTemplateParameterListsInfo( |
2028 | 0 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
2029 | 0 | assert(!TPLists.empty()); |
2030 | | // Make sure the extended decl info is allocated. |
2031 | 0 | if (!hasExtInfo()) { |
2032 | | // Save (non-extended) type source info pointer. |
2033 | 0 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
2034 | | // Allocate external info struct. |
2035 | 0 | DeclInfo = new (getASTContext()) ExtInfo; |
2036 | | // Restore savedTInfo into (extended) decl info. |
2037 | 0 | getExtInfo()->TInfo = savedTInfo; |
2038 | 0 | } |
2039 | | // Set the template parameter lists info. |
2040 | 0 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
2041 | 0 | } |
2042 | | |
2043 | 0 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
2044 | 0 | return getTemplateOrInnerLocStart(this); |
2045 | 0 | } |
2046 | | |
2047 | | // Helper function: returns true if QT is or contains a type |
2048 | | // having a postfix component. |
2049 | 0 | static bool typeIsPostfix(QualType QT) { |
2050 | 0 | while (true) { |
2051 | 0 | const Type* T = QT.getTypePtr(); |
2052 | 0 | switch (T->getTypeClass()) { |
2053 | 0 | default: |
2054 | 0 | return false; |
2055 | 0 | case Type::Pointer: |
2056 | 0 | QT = cast<PointerType>(T)->getPointeeType(); |
2057 | 0 | break; |
2058 | 0 | case Type::BlockPointer: |
2059 | 0 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
2060 | 0 | break; |
2061 | 0 | case Type::MemberPointer: |
2062 | 0 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
2063 | 0 | break; |
2064 | 0 | case Type::LValueReference: |
2065 | 0 | case Type::RValueReference: |
2066 | 0 | QT = cast<ReferenceType>(T)->getPointeeType(); |
2067 | 0 | break; |
2068 | 0 | case Type::PackExpansion: |
2069 | 0 | QT = cast<PackExpansionType>(T)->getPattern(); |
2070 | 0 | break; |
2071 | 0 | case Type::Paren: |
2072 | 0 | case Type::ConstantArray: |
2073 | 0 | case Type::DependentSizedArray: |
2074 | 0 | case Type::IncompleteArray: |
2075 | 0 | case Type::VariableArray: |
2076 | 0 | case Type::FunctionProto: |
2077 | 0 | case Type::FunctionNoProto: |
2078 | 0 | return true; |
2079 | 0 | } |
2080 | 0 | } |
2081 | 0 | } |
2082 | | |
2083 | 0 | SourceRange DeclaratorDecl::getSourceRange() const { |
2084 | 0 | SourceLocation RangeEnd = getLocation(); |
2085 | 0 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
2086 | | // If the declaration has no name or the type extends past the name take the |
2087 | | // end location of the type. |
2088 | 0 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
2089 | 0 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
2090 | 0 | } |
2091 | 0 | return SourceRange(getOuterLocStart(), RangeEnd); |
2092 | 0 | } |
2093 | | |
2094 | | void QualifierInfo::setTemplateParameterListsInfo( |
2095 | 0 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
2096 | | // Free previous template parameters (if any). |
2097 | 0 | if (NumTemplParamLists > 0) { |
2098 | 0 | Context.Deallocate(TemplParamLists); |
2099 | 0 | TemplParamLists = nullptr; |
2100 | 0 | NumTemplParamLists = 0; |
2101 | 0 | } |
2102 | | // Set info on matched template parameter lists (if any). |
2103 | 0 | if (!TPLists.empty()) { |
2104 | 0 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
2105 | 0 | NumTemplParamLists = TPLists.size(); |
2106 | 0 | std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); |
2107 | 0 | } |
2108 | 0 | } |
2109 | | |
2110 | | //===----------------------------------------------------------------------===// |
2111 | | // VarDecl Implementation |
2112 | | //===----------------------------------------------------------------------===// |
2113 | | |
2114 | 0 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
2115 | 0 | switch (SC) { |
2116 | 0 | case SC_None: break; |
2117 | 0 | case SC_Auto: return "auto"; |
2118 | 0 | case SC_Extern: return "extern"; |
2119 | 0 | case SC_PrivateExtern: return "__private_extern__"; |
2120 | 0 | case SC_Register: return "register"; |
2121 | 0 | case SC_Static: return "static"; |
2122 | 0 | } |
2123 | | |
2124 | 0 | llvm_unreachable("Invalid storage class"); |
2125 | 0 | } |
2126 | | |
2127 | | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
2128 | | SourceLocation StartLoc, SourceLocation IdLoc, |
2129 | | const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
2130 | | StorageClass SC) |
2131 | | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
2132 | 5.11k | redeclarable_base(C) { |
2133 | 5.11k | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
2134 | 5.11k | "VarDeclBitfields too large!"); |
2135 | 5.11k | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
2136 | 5.11k | "ParmVarDeclBitfields too large!"); |
2137 | 5.11k | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
2138 | 5.11k | "NonParmVarDeclBitfields too large!"); |
2139 | 5.11k | AllBits = 0; |
2140 | 5.11k | VarDeclBits.SClass = SC; |
2141 | | // Everything else is implicitly initialized to false. |
2142 | 5.11k | } |
2143 | | |
2144 | | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation StartL, |
2145 | | SourceLocation IdL, const IdentifierInfo *Id, |
2146 | 5.07k | QualType T, TypeSourceInfo *TInfo, StorageClass S) { |
2147 | 5.07k | return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S); |
2148 | 5.07k | } |
2149 | | |
2150 | 0 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
2151 | 0 | return new (C, ID) |
2152 | 0 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
2153 | 0 | QualType(), nullptr, SC_None); |
2154 | 0 | } |
2155 | | |
2156 | 0 | void VarDecl::setStorageClass(StorageClass SC) { |
2157 | 0 | assert(isLegalForVariable(SC)); |
2158 | 0 | VarDeclBits.SClass = SC; |
2159 | 0 | } |
2160 | | |
2161 | 5.82k | VarDecl::TLSKind VarDecl::getTLSKind() const { |
2162 | 5.82k | switch (VarDeclBits.TSCSpec) { |
2163 | 5.82k | case TSCS_unspecified: |
2164 | 5.82k | if (!hasAttr<ThreadAttr>() && |
2165 | 5.82k | !(getASTContext().getLangOpts().OpenMPUseTLS && |
2166 | 5.82k | getASTContext().getTargetInfo().isTLSSupported() && |
2167 | 5.82k | hasAttr<OMPThreadPrivateDeclAttr>())) |
2168 | 5.82k | return TLS_None; |
2169 | 0 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
2170 | 0 | LangOptions::MSVC2015)) || |
2171 | 0 | hasAttr<OMPThreadPrivateDeclAttr>()) |
2172 | 0 | ? TLS_Dynamic |
2173 | 0 | : TLS_Static; |
2174 | 0 | case TSCS___thread: // Fall through. |
2175 | 0 | case TSCS__Thread_local: |
2176 | 0 | return TLS_Static; |
2177 | 0 | case TSCS_thread_local: |
2178 | 0 | return TLS_Dynamic; |
2179 | 5.82k | } |
2180 | 0 | llvm_unreachable("Unknown thread storage class specifier!"); |
2181 | 0 | } |
2182 | | |
2183 | 0 | SourceRange VarDecl::getSourceRange() const { |
2184 | 0 | if (const Expr *Init = getInit()) { |
2185 | 0 | SourceLocation InitEnd = Init->getEndLoc(); |
2186 | | // If Init is implicit, ignore its source range and fallback on |
2187 | | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
2188 | 0 | if (InitEnd.isValid() && InitEnd != getLocation()) |
2189 | 0 | return SourceRange(getOuterLocStart(), InitEnd); |
2190 | 0 | } |
2191 | 0 | return DeclaratorDecl::getSourceRange(); |
2192 | 0 | } |
2193 | | |
2194 | | template<typename T> |
2195 | 474 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
2196 | | // C++ [dcl.link]p1: All function types, function names with external linkage, |
2197 | | // and variable names with external linkage have a language linkage. |
2198 | 474 | if (!D.hasExternalFormalLinkage()) |
2199 | 0 | return NoLanguageLinkage; |
2200 | | |
2201 | | // Language linkage is a C++ concept, but saying that everything else in C has |
2202 | | // C language linkage fits the implementation nicely. |
2203 | 474 | if (!D.getASTContext().getLangOpts().CPlusPlus) |
2204 | 474 | return CLanguageLinkage; |
2205 | | |
2206 | | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
2207 | | // language linkage of the names of class members and the function type of |
2208 | | // class member functions. |
2209 | 0 | const DeclContext *DC = D.getDeclContext(); |
2210 | 0 | if (DC->isRecord()) |
2211 | 0 | return CXXLanguageLinkage; |
2212 | | |
2213 | | // If the first decl is in an extern "C" context, any other redeclaration |
2214 | | // will have C language linkage. If the first one is not in an extern "C" |
2215 | | // context, we would have reported an error for any other decl being in one. |
2216 | 0 | if (isFirstInExternCContext(&D)) |
2217 | 0 | return CLanguageLinkage; |
2218 | 0 | return CXXLanguageLinkage; |
2219 | 0 | } Decl.cpp:clang::LanguageLinkage getDeclLanguageLinkage<clang::VarDecl>(clang::VarDecl const&) Line | Count | Source | 2195 | 468 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { | 2196 | | // C++ [dcl.link]p1: All function types, function names with external linkage, | 2197 | | // and variable names with external linkage have a language linkage. | 2198 | 468 | if (!D.hasExternalFormalLinkage()) | 2199 | 0 | return NoLanguageLinkage; | 2200 | | | 2201 | | // Language linkage is a C++ concept, but saying that everything else in C has | 2202 | | // C language linkage fits the implementation nicely. | 2203 | 468 | if (!D.getASTContext().getLangOpts().CPlusPlus) | 2204 | 468 | return CLanguageLinkage; | 2205 | | | 2206 | | // C++ [dcl.link]p4: A C language linkage is ignored in determining the | 2207 | | // language linkage of the names of class members and the function type of | 2208 | | // class member functions. | 2209 | 0 | const DeclContext *DC = D.getDeclContext(); | 2210 | 0 | if (DC->isRecord()) | 2211 | 0 | return CXXLanguageLinkage; | 2212 | | | 2213 | | // If the first decl is in an extern "C" context, any other redeclaration | 2214 | | // will have C language linkage. If the first one is not in an extern "C" | 2215 | | // context, we would have reported an error for any other decl being in one. | 2216 | 0 | if (isFirstInExternCContext(&D)) | 2217 | 0 | return CLanguageLinkage; | 2218 | 0 | return CXXLanguageLinkage; | 2219 | 0 | } |
Decl.cpp:clang::LanguageLinkage getDeclLanguageLinkage<clang::FunctionDecl>(clang::FunctionDecl const&) Line | Count | Source | 2195 | 6 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { | 2196 | | // C++ [dcl.link]p1: All function types, function names with external linkage, | 2197 | | // and variable names with external linkage have a language linkage. | 2198 | 6 | if (!D.hasExternalFormalLinkage()) | 2199 | 0 | return NoLanguageLinkage; | 2200 | | | 2201 | | // Language linkage is a C++ concept, but saying that everything else in C has | 2202 | | // C language linkage fits the implementation nicely. | 2203 | 6 | if (!D.getASTContext().getLangOpts().CPlusPlus) | 2204 | 6 | return CLanguageLinkage; | 2205 | | | 2206 | | // C++ [dcl.link]p4: A C language linkage is ignored in determining the | 2207 | | // language linkage of the names of class members and the function type of | 2208 | | // class member functions. | 2209 | 0 | const DeclContext *DC = D.getDeclContext(); | 2210 | 0 | if (DC->isRecord()) | 2211 | 0 | return CXXLanguageLinkage; | 2212 | | | 2213 | | // If the first decl is in an extern "C" context, any other redeclaration | 2214 | | // will have C language linkage. If the first one is not in an extern "C" | 2215 | | // context, we would have reported an error for any other decl being in one. | 2216 | 0 | if (isFirstInExternCContext(&D)) | 2217 | 0 | return CLanguageLinkage; | 2218 | 0 | return CXXLanguageLinkage; | 2219 | 0 | } |
|
2220 | | |
2221 | | template<typename T> |
2222 | 291 | static bool isDeclExternC(const T &D) { |
2223 | | // Since the context is ignored for class members, they can only have C++ |
2224 | | // language linkage or no language linkage. |
2225 | 291 | const DeclContext *DC = D.getDeclContext(); |
2226 | 291 | if (DC->isRecord()) { |
2227 | 0 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
2228 | 0 | return false; |
2229 | 0 | } |
2230 | | |
2231 | 291 | return D.getLanguageLinkage() == CLanguageLinkage; |
2232 | 291 | } Decl.cpp:bool isDeclExternC<clang::VarDecl>(clang::VarDecl const&) Line | Count | Source | 2222 | 285 | static bool isDeclExternC(const T &D) { | 2223 | | // Since the context is ignored for class members, they can only have C++ | 2224 | | // language linkage or no language linkage. | 2225 | 285 | const DeclContext *DC = D.getDeclContext(); | 2226 | 285 | if (DC->isRecord()) { | 2227 | 0 | assert(D.getASTContext().getLangOpts().CPlusPlus); | 2228 | 0 | return false; | 2229 | 0 | } | 2230 | | | 2231 | 285 | return D.getLanguageLinkage() == CLanguageLinkage; | 2232 | 285 | } |
Decl.cpp:bool isDeclExternC<clang::FunctionDecl>(clang::FunctionDecl const&) Line | Count | Source | 2222 | 6 | static bool isDeclExternC(const T &D) { | 2223 | | // Since the context is ignored for class members, they can only have C++ | 2224 | | // language linkage or no language linkage. | 2225 | 6 | const DeclContext *DC = D.getDeclContext(); | 2226 | 6 | if (DC->isRecord()) { | 2227 | 0 | assert(D.getASTContext().getLangOpts().CPlusPlus); | 2228 | 0 | return false; | 2229 | 0 | } | 2230 | | | 2231 | 6 | return D.getLanguageLinkage() == CLanguageLinkage; | 2232 | 6 | } |
|
2233 | | |
2234 | 468 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
2235 | 468 | return getDeclLanguageLinkage(*this); |
2236 | 468 | } |
2237 | | |
2238 | 285 | bool VarDecl::isExternC() const { |
2239 | 285 | return isDeclExternC(*this); |
2240 | 285 | } |
2241 | | |
2242 | 2.58k | bool VarDecl::isInExternCContext() const { |
2243 | 2.58k | return getLexicalDeclContext()->isExternCContext(); |
2244 | 2.58k | } |
2245 | | |
2246 | 183 | bool VarDecl::isInExternCXXContext() const { |
2247 | 183 | return getLexicalDeclContext()->isExternCXXContext(); |
2248 | 183 | } |
2249 | | |
2250 | 104k | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
2251 | | |
2252 | | VarDecl::DefinitionKind |
2253 | 12.8k | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
2254 | 12.8k | if (isThisDeclarationADemotedDefinition()) |
2255 | 0 | return DeclarationOnly; |
2256 | | |
2257 | | // C++ [basic.def]p2: |
2258 | | // A declaration is a definition unless [...] it contains the 'extern' |
2259 | | // specifier or a linkage-specification and neither an initializer [...], |
2260 | | // it declares a non-inline static data member in a class declaration [...], |
2261 | | // it declares a static data member outside a class definition and the variable |
2262 | | // was defined within the class with the constexpr specifier [...], |
2263 | | // C++1y [temp.expl.spec]p15: |
2264 | | // An explicit specialization of a static data member or an explicit |
2265 | | // specialization of a static data member template is a definition if the |
2266 | | // declaration includes an initializer; otherwise, it is a declaration. |
2267 | | // |
2268 | | // FIXME: How do you declare (but not define) a partial specialization of |
2269 | | // a static data member template outside the containing class? |
2270 | 12.8k | if (isStaticDataMember()) { |
2271 | 0 | if (isOutOfLine() && |
2272 | 0 | !(getCanonicalDecl()->isInline() && |
2273 | 0 | getCanonicalDecl()->isConstexpr()) && |
2274 | 0 | (hasInit() || |
2275 | | // If the first declaration is out-of-line, this may be an |
2276 | | // instantiation of an out-of-line partial specialization of a variable |
2277 | | // template for which we have not yet instantiated the initializer. |
2278 | 0 | (getFirstDecl()->isOutOfLine() |
2279 | 0 | ? getTemplateSpecializationKind() == TSK_Undeclared |
2280 | 0 | : getTemplateSpecializationKind() != |
2281 | 0 | TSK_ExplicitSpecialization) || |
2282 | 0 | isa<VarTemplatePartialSpecializationDecl>(this))) |
2283 | 0 | return Definition; |
2284 | 0 | if (!isOutOfLine() && isInline()) |
2285 | 0 | return Definition; |
2286 | 0 | return DeclarationOnly; |
2287 | 0 | } |
2288 | | // C99 6.7p5: |
2289 | | // A definition of an identifier is a declaration for that identifier that |
2290 | | // [...] causes storage to be reserved for that object. |
2291 | | // Note: that applies for all non-file-scope objects. |
2292 | | // C99 6.9.2p1: |
2293 | | // If the declaration of an identifier for an object has file scope and an |
2294 | | // initializer, the declaration is an external definition for the identifier |
2295 | 12.8k | if (hasInit()) |
2296 | 110 | return Definition; |
2297 | | |
2298 | 12.7k | if (hasDefiningAttr()) |
2299 | 0 | return Definition; |
2300 | | |
2301 | 12.7k | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
2302 | 0 | if (!SAA->isInherited()) |
2303 | 0 | return Definition; |
2304 | | |
2305 | | // A variable template specialization (other than a static data member |
2306 | | // template or an explicit specialization) is a declaration until we |
2307 | | // instantiate its initializer. |
2308 | 12.7k | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { |
2309 | 0 | if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
2310 | 0 | !isa<VarTemplatePartialSpecializationDecl>(VTSD) && |
2311 | 0 | !VTSD->IsCompleteDefinition) |
2312 | 0 | return DeclarationOnly; |
2313 | 0 | } |
2314 | | |
2315 | 12.7k | if (hasExternalStorage()) |
2316 | 0 | return DeclarationOnly; |
2317 | | |
2318 | | // [dcl.link] p7: |
2319 | | // A declaration directly contained in a linkage-specification is treated |
2320 | | // as if it contains the extern specifier for the purpose of determining |
2321 | | // the linkage of the declared name and whether it is a definition. |
2322 | 12.7k | if (isSingleLineLanguageLinkage(*this)) |
2323 | 0 | return DeclarationOnly; |
2324 | | |
2325 | | // C99 6.9.2p2: |
2326 | | // A declaration of an object that has file scope without an initializer, |
2327 | | // and without a storage class specifier or the scs 'static', constitutes |
2328 | | // a tentative definition. |
2329 | | // No such thing in C++. |
2330 | 12.7k | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
2331 | 7.86k | return TentativeDefinition; |
2332 | | |
2333 | | // What's left is (in C, block-scope) declarations without initializers or |
2334 | | // external storage. These are definitions. |
2335 | 4.86k | return Definition; |
2336 | 12.7k | } |
2337 | | |
2338 | 416 | VarDecl *VarDecl::getActingDefinition() { |
2339 | 416 | DefinitionKind Kind = isThisDeclarationADefinition(); |
2340 | 416 | if (Kind != TentativeDefinition) |
2341 | 0 | return nullptr; |
2342 | | |
2343 | 416 | VarDecl *LastTentative = nullptr; |
2344 | | |
2345 | | // Loop through the declaration chain, starting with the most recent. |
2346 | 1.06k | for (VarDecl *Decl = getMostRecentDecl(); Decl; |
2347 | 658 | Decl = Decl->getPreviousDecl()) { |
2348 | 658 | Kind = Decl->isThisDeclarationADefinition(); |
2349 | 658 | if (Kind == Definition) |
2350 | 6 | return nullptr; |
2351 | | // Record the first (most recent) TentativeDefinition that is encountered. |
2352 | 652 | if (Kind == TentativeDefinition && !LastTentative) |
2353 | 413 | LastTentative = Decl; |
2354 | 652 | } |
2355 | | |
2356 | 410 | return LastTentative; |
2357 | 416 | } |
2358 | | |
2359 | 29 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
2360 | 29 | VarDecl *First = getFirstDecl(); |
2361 | 46 | for (auto *I : First->redecls()) { |
2362 | 46 | if (I->isThisDeclarationADefinition(C) == Definition) |
2363 | 1 | return I; |
2364 | 46 | } |
2365 | 28 | return nullptr; |
2366 | 29 | } |
2367 | | |
2368 | 17 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
2369 | 17 | DefinitionKind Kind = DeclarationOnly; |
2370 | | |
2371 | 17 | const VarDecl *First = getFirstDecl(); |
2372 | 29 | for (auto *I : First->redecls()) { |
2373 | 29 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
2374 | 29 | if (Kind == Definition) |
2375 | 0 | break; |
2376 | 29 | } |
2377 | | |
2378 | 17 | return Kind; |
2379 | 17 | } |
2380 | | |
2381 | 131 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
2382 | 168 | for (auto *I : redecls()) { |
2383 | 168 | if (auto Expr = I->getInit()) { |
2384 | 0 | D = I; |
2385 | 0 | return Expr; |
2386 | 0 | } |
2387 | 168 | } |
2388 | 131 | return nullptr; |
2389 | 131 | } |
2390 | | |
2391 | 19.2k | bool VarDecl::hasInit() const { |
2392 | 19.2k | if (auto *P = dyn_cast<ParmVarDecl>(this)) |
2393 | 0 | if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) |
2394 | 0 | return false; |
2395 | | |
2396 | 19.2k | return !Init.isNull(); |
2397 | 19.2k | } |
2398 | | |
2399 | 575 | Expr *VarDecl::getInit() { |
2400 | 575 | if (!hasInit()) |
2401 | 535 | return nullptr; |
2402 | | |
2403 | 40 | if (auto *S = Init.dyn_cast<Stmt *>()) |
2404 | 40 | return cast<Expr>(S); |
2405 | | |
2406 | 0 | auto *Eval = getEvaluatedStmt(); |
2407 | 0 | return cast<Expr>(Eval->Value.isOffset() |
2408 | 0 | ? Eval->Value.get(getASTContext().getExternalSource()) |
2409 | 0 | : Eval->Value.get(nullptr)); |
2410 | 40 | } |
2411 | | |
2412 | 0 | Stmt **VarDecl::getInitAddress() { |
2413 | 0 | if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) |
2414 | 0 | return ES->Value.getAddressOfPointer(getASTContext().getExternalSource()); |
2415 | | |
2416 | 0 | return Init.getAddrOfPtr1(); |
2417 | 0 | } |
2418 | | |
2419 | 0 | VarDecl *VarDecl::getInitializingDeclaration() { |
2420 | 0 | VarDecl *Def = nullptr; |
2421 | 0 | for (auto *I : redecls()) { |
2422 | 0 | if (I->hasInit()) |
2423 | 0 | return I; |
2424 | | |
2425 | 0 | if (I->isThisDeclarationADefinition()) { |
2426 | 0 | if (isStaticDataMember()) |
2427 | 0 | return I; |
2428 | 0 | Def = I; |
2429 | 0 | } |
2430 | 0 | } |
2431 | 0 | return Def; |
2432 | 0 | } |
2433 | | |
2434 | 2.58k | bool VarDecl::isOutOfLine() const { |
2435 | 2.58k | if (Decl::isOutOfLine()) |
2436 | 0 | return true; |
2437 | | |
2438 | 2.58k | if (!isStaticDataMember()) |
2439 | 2.58k | return false; |
2440 | | |
2441 | | // If this static data member was instantiated from a static data member of |
2442 | | // a class template, check whether that static data member was defined |
2443 | | // out-of-line. |
2444 | 0 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
2445 | 0 | return VD->isOutOfLine(); |
2446 | | |
2447 | 0 | return false; |
2448 | 0 | } |
2449 | | |
2450 | 20 | void VarDecl::setInit(Expr *I) { |
2451 | 20 | if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
2452 | 0 | Eval->~EvaluatedStmt(); |
2453 | 0 | getASTContext().Deallocate(Eval); |
2454 | 0 | } |
2455 | | |
2456 | 20 | Init = I; |
2457 | 20 | } |
2458 | | |
2459 | 17 | bool VarDecl::mightBeUsableInConstantExpressions(const ASTContext &C) const { |
2460 | 17 | const LangOptions &Lang = C.getLangOpts(); |
2461 | | |
2462 | | // OpenCL permits const integral variables to be used in constant |
2463 | | // expressions, like in C++98. |
2464 | 17 | if (!Lang.CPlusPlus && !Lang.OpenCL) |
2465 | 17 | return false; |
2466 | | |
2467 | | // Function parameters are never usable in constant expressions. |
2468 | 0 | if (isa<ParmVarDecl>(this)) |
2469 | 0 | return false; |
2470 | | |
2471 | | // The values of weak variables are never usable in constant expressions. |
2472 | 0 | if (isWeak()) |
2473 | 0 | return false; |
2474 | | |
2475 | | // In C++11, any variable of reference type can be used in a constant |
2476 | | // expression if it is initialized by a constant expression. |
2477 | 0 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
2478 | 0 | return true; |
2479 | | |
2480 | | // Only const objects can be used in constant expressions in C++. C++98 does |
2481 | | // not require the variable to be non-volatile, but we consider this to be a |
2482 | | // defect. |
2483 | 0 | if (!getType().isConstant(C) || getType().isVolatileQualified()) |
2484 | 0 | return false; |
2485 | | |
2486 | | // In C++, const, non-volatile variables of integral or enumeration types |
2487 | | // can be used in constant expressions. |
2488 | 0 | if (getType()->isIntegralOrEnumerationType()) |
2489 | 0 | return true; |
2490 | | |
2491 | | // Additionally, in C++11, non-volatile constexpr variables can be used in |
2492 | | // constant expressions. |
2493 | 0 | return Lang.CPlusPlus11 && isConstexpr(); |
2494 | 0 | } |
2495 | | |
2496 | 29 | bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const { |
2497 | | // C++2a [expr.const]p3: |
2498 | | // A variable is usable in constant expressions after its initializing |
2499 | | // declaration is encountered... |
2500 | 29 | const VarDecl *DefVD = nullptr; |
2501 | 29 | const Expr *Init = getAnyInitializer(DefVD); |
2502 | 29 | if (!Init || Init->isValueDependent() || getType()->isDependentType()) |
2503 | 29 | return false; |
2504 | | // ... if it is a constexpr variable, or it is of reference type or of |
2505 | | // const-qualified integral or enumeration type, ... |
2506 | 0 | if (!DefVD->mightBeUsableInConstantExpressions(Context)) |
2507 | 0 | return false; |
2508 | | // ... and its initializer is a constant initializer. |
2509 | 0 | if (Context.getLangOpts().CPlusPlus && !DefVD->hasConstantInitialization()) |
2510 | 0 | return false; |
2511 | | // C++98 [expr.const]p1: |
2512 | | // An integral constant-expression can involve only [...] const variables |
2513 | | // or static data members of integral or enumeration types initialized with |
2514 | | // [integer] constant expressions (dcl.init) |
2515 | 0 | if ((Context.getLangOpts().CPlusPlus || Context.getLangOpts().OpenCL) && |
2516 | 0 | !Context.getLangOpts().CPlusPlus11 && !DefVD->hasICEInitializer(Context)) |
2517 | 0 | return false; |
2518 | 0 | return true; |
2519 | 0 | } |
2520 | | |
2521 | | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
2522 | | /// form, which contains extra information on the evaluated value of the |
2523 | | /// initializer. |
2524 | 0 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
2525 | 0 | auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
2526 | 0 | if (!Eval) { |
2527 | | // Note: EvaluatedStmt contains an APValue, which usually holds |
2528 | | // resources not allocated from the ASTContext. We need to do some |
2529 | | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
2530 | | // where we can detect whether there's anything to clean up or not. |
2531 | 0 | Eval = new (getASTContext()) EvaluatedStmt; |
2532 | 0 | Eval->Value = Init.get<Stmt *>(); |
2533 | 0 | Init = Eval; |
2534 | 0 | } |
2535 | 0 | return Eval; |
2536 | 0 | } |
2537 | | |
2538 | 0 | EvaluatedStmt *VarDecl::getEvaluatedStmt() const { |
2539 | 0 | return Init.dyn_cast<EvaluatedStmt *>(); |
2540 | 0 | } |
2541 | | |
2542 | 0 | APValue *VarDecl::evaluateValue() const { |
2543 | 0 | SmallVector<PartialDiagnosticAt, 8> Notes; |
2544 | 0 | return evaluateValueImpl(Notes, hasConstantInitialization()); |
2545 | 0 | } |
2546 | | |
2547 | | APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
2548 | 0 | bool IsConstantInitialization) const { |
2549 | 0 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
2550 | |
|
2551 | 0 | const auto *Init = getInit(); |
2552 | 0 | assert(!Init->isValueDependent()); |
2553 | | |
2554 | | // We only produce notes indicating why an initializer is non-constant the |
2555 | | // first time it is evaluated. FIXME: The notes won't always be emitted the |
2556 | | // first time we try evaluation, so might not be produced at all. |
2557 | 0 | if (Eval->WasEvaluated) |
2558 | 0 | return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; |
2559 | | |
2560 | 0 | if (Eval->IsEvaluating) { |
2561 | | // FIXME: Produce a diagnostic for self-initialization. |
2562 | 0 | return nullptr; |
2563 | 0 | } |
2564 | | |
2565 | 0 | Eval->IsEvaluating = true; |
2566 | |
|
2567 | 0 | ASTContext &Ctx = getASTContext(); |
2568 | 0 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes, |
2569 | 0 | IsConstantInitialization); |
2570 | | |
2571 | | // In C++, this isn't a constant initializer if we produced notes. In that |
2572 | | // case, we can't keep the result, because it may only be correct under the |
2573 | | // assumption that the initializer is a constant context. |
2574 | 0 | if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus && |
2575 | 0 | !Notes.empty()) |
2576 | 0 | Result = false; |
2577 | | |
2578 | | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
2579 | | // or that it's empty (so that there's nothing to clean up) if evaluation |
2580 | | // failed. |
2581 | 0 | if (!Result) |
2582 | 0 | Eval->Evaluated = APValue(); |
2583 | 0 | else if (Eval->Evaluated.needsCleanup()) |
2584 | 0 | Ctx.addDestruction(&Eval->Evaluated); |
2585 | |
|
2586 | 0 | Eval->IsEvaluating = false; |
2587 | 0 | Eval->WasEvaluated = true; |
2588 | |
|
2589 | 0 | return Result ? &Eval->Evaluated : nullptr; |
2590 | 0 | } |
2591 | | |
2592 | 0 | APValue *VarDecl::getEvaluatedValue() const { |
2593 | 0 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
2594 | 0 | if (Eval->WasEvaluated) |
2595 | 0 | return &Eval->Evaluated; |
2596 | | |
2597 | 0 | return nullptr; |
2598 | 0 | } |
2599 | | |
2600 | 0 | bool VarDecl::hasICEInitializer(const ASTContext &Context) const { |
2601 | 0 | const Expr *Init = getInit(); |
2602 | 0 | assert(Init && "no initializer"); |
2603 | | |
2604 | 0 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
2605 | 0 | if (!Eval->CheckedForICEInit) { |
2606 | 0 | Eval->CheckedForICEInit = true; |
2607 | 0 | Eval->HasICEInit = Init->isIntegerConstantExpr(Context); |
2608 | 0 | } |
2609 | 0 | return Eval->HasICEInit; |
2610 | 0 | } |
2611 | | |
2612 | 0 | bool VarDecl::hasConstantInitialization() const { |
2613 | | // In C, all globals (and only globals) have constant initialization. |
2614 | 0 | if (hasGlobalStorage() && !getASTContext().getLangOpts().CPlusPlus) |
2615 | 0 | return true; |
2616 | | |
2617 | | // In C++, it depends on whether the evaluation at the point of definition |
2618 | | // was evaluatable as a constant initializer. |
2619 | 0 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
2620 | 0 | return Eval->HasConstantInitialization; |
2621 | | |
2622 | 0 | return false; |
2623 | 0 | } |
2624 | | |
2625 | | bool VarDecl::checkForConstantInitialization( |
2626 | 0 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
2627 | 0 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
2628 | | // If we ask for the value before we know whether we have a constant |
2629 | | // initializer, we can compute the wrong value (for example, due to |
2630 | | // std::is_constant_evaluated()). |
2631 | 0 | assert(!Eval->WasEvaluated && |
2632 | 0 | "already evaluated var value before checking for constant init"); |
2633 | 0 | assert(getASTContext().getLangOpts().CPlusPlus && "only meaningful in C++"); |
2634 | | |
2635 | 0 | assert(!getInit()->isValueDependent()); |
2636 | | |
2637 | | // Evaluate the initializer to check whether it's a constant expression. |
2638 | 0 | Eval->HasConstantInitialization = |
2639 | 0 | evaluateValueImpl(Notes, true) && Notes.empty(); |
2640 | | |
2641 | | // If evaluation as a constant initializer failed, allow re-evaluation as a |
2642 | | // non-constant initializer if we later find we want the value. |
2643 | 0 | if (!Eval->HasConstantInitialization) |
2644 | 0 | Eval->WasEvaluated = false; |
2645 | |
|
2646 | 0 | return Eval->HasConstantInitialization; |
2647 | 0 | } |
2648 | | |
2649 | 142 | bool VarDecl::isParameterPack() const { |
2650 | 142 | return isa<PackExpansionType>(getType()); |
2651 | 142 | } |
2652 | | |
2653 | | template<typename DeclT> |
2654 | 0 | static DeclT *getDefinitionOrSelf(DeclT *D) { |
2655 | 0 | assert(D); |
2656 | 0 | if (auto *Def = D->getDefinition()) |
2657 | 0 | return Def; |
2658 | 0 | return D; |
2659 | 0 | } Unexecuted instantiation: Decl.cpp:clang::VarDecl* getDefinitionOrSelf<clang::VarDecl>(clang::VarDecl*) Unexecuted instantiation: Decl.cpp:clang::FunctionDecl* getDefinitionOrSelf<clang::FunctionDecl>(clang::FunctionDecl*) Unexecuted instantiation: Decl.cpp:clang::EnumDecl* getDefinitionOrSelf<clang::EnumDecl>(clang::EnumDecl*) |
2660 | | |
2661 | 0 | bool VarDecl::isEscapingByref() const { |
2662 | 0 | return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; |
2663 | 0 | } |
2664 | | |
2665 | 0 | bool VarDecl::isNonEscapingByref() const { |
2666 | 0 | return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; |
2667 | 0 | } |
2668 | | |
2669 | 0 | bool VarDecl::hasDependentAlignment() const { |
2670 | 0 | QualType T = getType(); |
2671 | 0 | return T->isDependentType() || T->isUndeducedType() || |
2672 | 0 | llvm::any_of(specific_attrs<AlignedAttr>(), [](const AlignedAttr *AA) { |
2673 | 0 | return AA->isAlignmentDependent(); |
2674 | 0 | }); |
2675 | 0 | } |
2676 | | |
2677 | 0 | VarDecl *VarDecl::getTemplateInstantiationPattern() const { |
2678 | 0 | const VarDecl *VD = this; |
2679 | | |
2680 | | // If this is an instantiated member, walk back to the template from which |
2681 | | // it was instantiated. |
2682 | 0 | if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { |
2683 | 0 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
2684 | 0 | VD = VD->getInstantiatedFromStaticDataMember(); |
2685 | 0 | while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) |
2686 | 0 | VD = NewVD; |
2687 | 0 | } |
2688 | 0 | } |
2689 | | |
2690 | | // If it's an instantiated variable template specialization, find the |
2691 | | // template or partial specialization from which it was instantiated. |
2692 | 0 | if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
2693 | 0 | if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) { |
2694 | 0 | auto From = VDTemplSpec->getInstantiatedFrom(); |
2695 | 0 | if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { |
2696 | 0 | while (!VTD->isMemberSpecialization()) { |
2697 | 0 | auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); |
2698 | 0 | if (!NewVTD) |
2699 | 0 | break; |
2700 | 0 | VTD = NewVTD; |
2701 | 0 | } |
2702 | 0 | return getDefinitionOrSelf(VTD->getTemplatedDecl()); |
2703 | 0 | } |
2704 | 0 | if (auto *VTPSD = |
2705 | 0 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
2706 | 0 | while (!VTPSD->isMemberSpecialization()) { |
2707 | 0 | auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); |
2708 | 0 | if (!NewVTPSD) |
2709 | 0 | break; |
2710 | 0 | VTPSD = NewVTPSD; |
2711 | 0 | } |
2712 | 0 | return getDefinitionOrSelf<VarDecl>(VTPSD); |
2713 | 0 | } |
2714 | 0 | } |
2715 | 0 | } |
2716 | | |
2717 | | // If this is the pattern of a variable template, find where it was |
2718 | | // instantiated from. FIXME: Is this necessary? |
2719 | 0 | if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { |
2720 | 0 | while (!VarTemplate->isMemberSpecialization()) { |
2721 | 0 | auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); |
2722 | 0 | if (!NewVT) |
2723 | 0 | break; |
2724 | 0 | VarTemplate = NewVT; |
2725 | 0 | } |
2726 | |
|
2727 | 0 | return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); |
2728 | 0 | } |
2729 | | |
2730 | 0 | if (VD == this) |
2731 | 0 | return nullptr; |
2732 | 0 | return getDefinitionOrSelf(const_cast<VarDecl*>(VD)); |
2733 | 0 | } |
2734 | | |
2735 | 0 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
2736 | 0 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2737 | 0 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
2738 | | |
2739 | 0 | return nullptr; |
2740 | 0 | } |
2741 | | |
2742 | 1.29k | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
2743 | 1.29k | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
2744 | 0 | return Spec->getSpecializationKind(); |
2745 | | |
2746 | 1.29k | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2747 | 0 | return MSI->getTemplateSpecializationKind(); |
2748 | | |
2749 | 1.29k | return TSK_Undeclared; |
2750 | 1.29k | } |
2751 | | |
2752 | | TemplateSpecializationKind |
2753 | 0 | VarDecl::getTemplateSpecializationKindForInstantiation() const { |
2754 | 0 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2755 | 0 | return MSI->getTemplateSpecializationKind(); |
2756 | | |
2757 | 0 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
2758 | 0 | return Spec->getSpecializationKind(); |
2759 | | |
2760 | 0 | return TSK_Undeclared; |
2761 | 0 | } |
2762 | | |
2763 | 0 | SourceLocation VarDecl::getPointOfInstantiation() const { |
2764 | 0 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
2765 | 0 | return Spec->getPointOfInstantiation(); |
2766 | | |
2767 | 0 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
2768 | 0 | return MSI->getPointOfInstantiation(); |
2769 | | |
2770 | 0 | return SourceLocation(); |
2771 | 0 | } |
2772 | | |
2773 | 1.47k | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
2774 | 1.47k | return getASTContext().getTemplateOrSpecializationInfo(this) |
2775 | 1.47k | .dyn_cast<VarTemplateDecl *>(); |
2776 | 1.47k | } |
2777 | | |
2778 | 0 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
2779 | 0 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
2780 | 0 | } |
2781 | | |
2782 | 0 | bool VarDecl::isKnownToBeDefined() const { |
2783 | 0 | const auto &LangOpts = getASTContext().getLangOpts(); |
2784 | | // In CUDA mode without relocatable device code, variables of form 'extern |
2785 | | // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared |
2786 | | // memory pool. These are never undefined variables, even if they appear |
2787 | | // inside of an anon namespace or static function. |
2788 | | // |
2789 | | // With CUDA relocatable device code enabled, these variables don't get |
2790 | | // special handling; they're treated like regular extern variables. |
2791 | 0 | if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && |
2792 | 0 | hasExternalStorage() && hasAttr<CUDASharedAttr>() && |
2793 | 0 | isa<IncompleteArrayType>(getType())) |
2794 | 0 | return true; |
2795 | | |
2796 | 0 | return hasDefinition(); |
2797 | 0 | } |
2798 | | |
2799 | 0 | bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { |
2800 | 0 | return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || |
2801 | 0 | (!Ctx.getLangOpts().RegisterStaticDestructors && |
2802 | 0 | !hasAttr<AlwaysDestroyAttr>())); |
2803 | 0 | } |
2804 | | |
2805 | | QualType::DestructionKind |
2806 | 0 | VarDecl::needsDestruction(const ASTContext &Ctx) const { |
2807 | 0 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
2808 | 0 | if (Eval->HasConstantDestruction) |
2809 | 0 | return QualType::DK_none; |
2810 | | |
2811 | 0 | if (isNoDestroy(Ctx)) |
2812 | 0 | return QualType::DK_none; |
2813 | | |
2814 | 0 | return getType().isDestructedType(); |
2815 | 0 | } |
2816 | | |
2817 | 0 | bool VarDecl::hasFlexibleArrayInit(const ASTContext &Ctx) const { |
2818 | 0 | assert(hasInit() && "Expect initializer to check for flexible array init"); |
2819 | 0 | auto *Ty = getType()->getAs<RecordType>(); |
2820 | 0 | if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember()) |
2821 | 0 | return false; |
2822 | 0 | auto *List = dyn_cast<InitListExpr>(getInit()->IgnoreParens()); |
2823 | 0 | if (!List) |
2824 | 0 | return false; |
2825 | 0 | const Expr *FlexibleInit = List->getInit(List->getNumInits() - 1); |
2826 | 0 | auto InitTy = Ctx.getAsConstantArrayType(FlexibleInit->getType()); |
2827 | 0 | if (!InitTy) |
2828 | 0 | return false; |
2829 | 0 | return InitTy->getSize() != 0; |
2830 | 0 | } |
2831 | | |
2832 | 0 | CharUnits VarDecl::getFlexibleArrayInitChars(const ASTContext &Ctx) const { |
2833 | 0 | assert(hasInit() && "Expect initializer to check for flexible array init"); |
2834 | 0 | auto *Ty = getType()->getAs<RecordType>(); |
2835 | 0 | if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember()) |
2836 | 0 | return CharUnits::Zero(); |
2837 | 0 | auto *List = dyn_cast<InitListExpr>(getInit()->IgnoreParens()); |
2838 | 0 | if (!List || List->getNumInits() == 0) |
2839 | 0 | return CharUnits::Zero(); |
2840 | 0 | const Expr *FlexibleInit = List->getInit(List->getNumInits() - 1); |
2841 | 0 | auto InitTy = Ctx.getAsConstantArrayType(FlexibleInit->getType()); |
2842 | 0 | if (!InitTy) |
2843 | 0 | return CharUnits::Zero(); |
2844 | 0 | CharUnits FlexibleArraySize = Ctx.getTypeSizeInChars(InitTy); |
2845 | 0 | const ASTRecordLayout &RL = Ctx.getASTRecordLayout(Ty->getDecl()); |
2846 | 0 | CharUnits FlexibleArrayOffset = |
2847 | 0 | Ctx.toCharUnitsFromBits(RL.getFieldOffset(RL.getFieldCount() - 1)); |
2848 | 0 | if (FlexibleArrayOffset + FlexibleArraySize < RL.getSize()) |
2849 | 0 | return CharUnits::Zero(); |
2850 | 0 | return FlexibleArrayOffset + FlexibleArraySize - RL.getSize(); |
2851 | 0 | } |
2852 | | |
2853 | 1.31k | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
2854 | 1.31k | if (isStaticDataMember()) |
2855 | | // FIXME: Remove ? |
2856 | | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
2857 | 0 | return getASTContext().getTemplateOrSpecializationInfo(this) |
2858 | 0 | .dyn_cast<MemberSpecializationInfo *>(); |
2859 | 1.31k | return nullptr; |
2860 | 1.31k | } |
2861 | | |
2862 | | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
2863 | 0 | SourceLocation PointOfInstantiation) { |
2864 | 0 | assert((isa<VarTemplateSpecializationDecl>(this) || |
2865 | 0 | getMemberSpecializationInfo()) && |
2866 | 0 | "not a variable or static data member template specialization"); |
2867 | | |
2868 | 0 | if (VarTemplateSpecializationDecl *Spec = |
2869 | 0 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
2870 | 0 | Spec->setSpecializationKind(TSK); |
2871 | 0 | if (TSK != TSK_ExplicitSpecialization && |
2872 | 0 | PointOfInstantiation.isValid() && |
2873 | 0 | Spec->getPointOfInstantiation().isInvalid()) { |
2874 | 0 | Spec->setPointOfInstantiation(PointOfInstantiation); |
2875 | 0 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
2876 | 0 | L->InstantiationRequested(this); |
2877 | 0 | } |
2878 | 0 | } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
2879 | 0 | MSI->setTemplateSpecializationKind(TSK); |
2880 | 0 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
2881 | 0 | MSI->getPointOfInstantiation().isInvalid()) { |
2882 | 0 | MSI->setPointOfInstantiation(PointOfInstantiation); |
2883 | 0 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
2884 | 0 | L->InstantiationRequested(this); |
2885 | 0 | } |
2886 | 0 | } |
2887 | 0 | } |
2888 | | |
2889 | | void |
2890 | | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
2891 | 0 | TemplateSpecializationKind TSK) { |
2892 | 0 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
2893 | 0 | "Previous template or instantiation?"); |
2894 | 0 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
2895 | 0 | } |
2896 | | |
2897 | | //===----------------------------------------------------------------------===// |
2898 | | // ParmVarDecl Implementation |
2899 | | //===----------------------------------------------------------------------===// |
2900 | | |
2901 | | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
2902 | | SourceLocation StartLoc, |
2903 | | SourceLocation IdLoc, IdentifierInfo *Id, |
2904 | | QualType T, TypeSourceInfo *TInfo, |
2905 | 40 | StorageClass S, Expr *DefArg) { |
2906 | 40 | return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo, |
2907 | 40 | S, DefArg); |
2908 | 40 | } |
2909 | | |
2910 | 0 | QualType ParmVarDecl::getOriginalType() const { |
2911 | 0 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
2912 | 0 | QualType T = TSI ? TSI->getType() : getType(); |
2913 | 0 | if (const auto *DT = dyn_cast<DecayedType>(T)) |
2914 | 0 | return DT->getOriginalType(); |
2915 | 0 | return T; |
2916 | 0 | } |
2917 | | |
2918 | 0 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
2919 | 0 | return new (C, ID) |
2920 | 0 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
2921 | 0 | nullptr, QualType(), nullptr, SC_None, nullptr); |
2922 | 0 | } |
2923 | | |
2924 | 0 | SourceRange ParmVarDecl::getSourceRange() const { |
2925 | 0 | if (!hasInheritedDefaultArg()) { |
2926 | 0 | SourceRange ArgRange = getDefaultArgRange(); |
2927 | 0 | if (ArgRange.isValid()) |
2928 | 0 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
2929 | 0 | } |
2930 | | |
2931 | | // DeclaratorDecl considers the range of postfix types as overlapping with the |
2932 | | // declaration name, but this is not the case with parameters in ObjC methods. |
2933 | 0 | if (isa<ObjCMethodDecl>(getDeclContext())) |
2934 | 0 | return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); |
2935 | | |
2936 | 0 | return DeclaratorDecl::getSourceRange(); |
2937 | 0 | } |
2938 | | |
2939 | 0 | bool ParmVarDecl::isDestroyedInCallee() const { |
2940 | | // ns_consumed only affects code generation in ARC |
2941 | 0 | if (hasAttr<NSConsumedAttr>()) |
2942 | 0 | return getASTContext().getLangOpts().ObjCAutoRefCount; |
2943 | | |
2944 | | // FIXME: isParamDestroyedInCallee() should probably imply |
2945 | | // isDestructedType() |
2946 | 0 | const auto *RT = getType()->getAs<RecordType>(); |
2947 | 0 | if (RT && RT->getDecl()->isParamDestroyedInCallee() && |
2948 | 0 | getType().isDestructedType()) |
2949 | 0 | return true; |
2950 | | |
2951 | 0 | return false; |
2952 | 0 | } |
2953 | | |
2954 | 0 | Expr *ParmVarDecl::getDefaultArg() { |
2955 | 0 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
2956 | 0 | assert(!hasUninstantiatedDefaultArg() && |
2957 | 0 | "Default argument is not yet instantiated!"); |
2958 | | |
2959 | 0 | Expr *Arg = getInit(); |
2960 | 0 | if (auto *E = dyn_cast_if_present<FullExpr>(Arg)) |
2961 | 0 | return E->getSubExpr(); |
2962 | | |
2963 | 0 | return Arg; |
2964 | 0 | } |
2965 | | |
2966 | 41 | void ParmVarDecl::setDefaultArg(Expr *defarg) { |
2967 | 41 | ParmVarDeclBits.DefaultArgKind = DAK_Normal; |
2968 | 41 | Init = defarg; |
2969 | 41 | } |
2970 | | |
2971 | 0 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
2972 | 0 | switch (ParmVarDeclBits.DefaultArgKind) { |
2973 | 0 | case DAK_None: |
2974 | 0 | case DAK_Unparsed: |
2975 | | // Nothing we can do here. |
2976 | 0 | return SourceRange(); |
2977 | | |
2978 | 0 | case DAK_Uninstantiated: |
2979 | 0 | return getUninstantiatedDefaultArg()->getSourceRange(); |
2980 | | |
2981 | 0 | case DAK_Normal: |
2982 | 0 | if (const Expr *E = getInit()) |
2983 | 0 | return E->getSourceRange(); |
2984 | | |
2985 | | // Missing an actual expression, may be invalid. |
2986 | 0 | return SourceRange(); |
2987 | 0 | } |
2988 | 0 | llvm_unreachable("Invalid default argument kind."); |
2989 | 0 | } |
2990 | | |
2991 | 0 | void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { |
2992 | 0 | ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; |
2993 | 0 | Init = arg; |
2994 | 0 | } |
2995 | | |
2996 | 0 | Expr *ParmVarDecl::getUninstantiatedDefaultArg() { |
2997 | 0 | assert(hasUninstantiatedDefaultArg() && |
2998 | 0 | "Wrong kind of initialization expression!"); |
2999 | 0 | return cast_if_present<Expr>(Init.get<Stmt *>()); |
3000 | 0 | } |
3001 | | |
3002 | 0 | bool ParmVarDecl::hasDefaultArg() const { |
3003 | | // FIXME: We should just return false for DAK_None here once callers are |
3004 | | // prepared for the case that we encountered an invalid default argument and |
3005 | | // were unable to even build an invalid expression. |
3006 | 0 | return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || |
3007 | 0 | !Init.isNull(); |
3008 | 0 | } |
3009 | | |
3010 | 0 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
3011 | 0 | getASTContext().setParameterIndex(this, parameterIndex); |
3012 | 0 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
3013 | 0 | } |
3014 | | |
3015 | 0 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
3016 | 0 | return getASTContext().getParameterIndex(this); |
3017 | 0 | } |
3018 | | |
3019 | | //===----------------------------------------------------------------------===// |
3020 | | // FunctionDecl Implementation |
3021 | | //===----------------------------------------------------------------------===// |
3022 | | |
3023 | | FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, |
3024 | | SourceLocation StartLoc, |
3025 | | const DeclarationNameInfo &NameInfo, QualType T, |
3026 | | TypeSourceInfo *TInfo, StorageClass S, |
3027 | | bool UsesFPIntrin, bool isInlineSpecified, |
3028 | | ConstexprSpecKind ConstexprKind, |
3029 | | Expr *TrailingRequiresClause) |
3030 | | : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, |
3031 | | StartLoc), |
3032 | | DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0), |
3033 | 19 | EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { |
3034 | 19 | assert(T.isNull() || T->isFunctionType()); |
3035 | 0 | FunctionDeclBits.SClass = S; |
3036 | 19 | FunctionDeclBits.IsInline = isInlineSpecified; |
3037 | 19 | FunctionDeclBits.IsInlineSpecified = isInlineSpecified; |
3038 | 19 | FunctionDeclBits.IsVirtualAsWritten = false; |
3039 | 19 | FunctionDeclBits.IsPure = false; |
3040 | 19 | FunctionDeclBits.HasInheritedPrototype = false; |
3041 | 19 | FunctionDeclBits.HasWrittenPrototype = true; |
3042 | 19 | FunctionDeclBits.IsDeleted = false; |
3043 | 19 | FunctionDeclBits.IsTrivial = false; |
3044 | 19 | FunctionDeclBits.IsTrivialForCall = false; |
3045 | 19 | FunctionDeclBits.IsDefaulted = false; |
3046 | 19 | FunctionDeclBits.IsExplicitlyDefaulted = false; |
3047 | 19 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
3048 | 19 | FunctionDeclBits.IsIneligibleOrNotSelected = false; |
3049 | 19 | FunctionDeclBits.HasImplicitReturnZero = false; |
3050 | 19 | FunctionDeclBits.IsLateTemplateParsed = false; |
3051 | 19 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind); |
3052 | 19 | FunctionDeclBits.BodyContainsImmediateEscalatingExpression = false; |
3053 | 19 | FunctionDeclBits.InstantiationIsPending = false; |
3054 | 19 | FunctionDeclBits.UsesSEHTry = false; |
3055 | 19 | FunctionDeclBits.UsesFPIntrin = UsesFPIntrin; |
3056 | 19 | FunctionDeclBits.HasSkippedBody = false; |
3057 | 19 | FunctionDeclBits.WillHaveBody = false; |
3058 | 19 | FunctionDeclBits.IsMultiVersion = false; |
3059 | 19 | FunctionDeclBits.DeductionCandidateKind = |
3060 | 19 | static_cast<unsigned char>(DeductionCandidate::Normal); |
3061 | 19 | FunctionDeclBits.HasODRHash = false; |
3062 | 19 | FunctionDeclBits.FriendConstraintRefersToEnclosingTemplate = false; |
3063 | 19 | if (TrailingRequiresClause) |
3064 | 0 | setTrailingRequiresClause(TrailingRequiresClause); |
3065 | 19 | } |
3066 | | |
3067 | | void FunctionDecl::getNameForDiagnostic( |
3068 | 0 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
3069 | 0 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
3070 | 0 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
3071 | 0 | if (TemplateArgs) |
3072 | 0 | printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); |
3073 | 0 | } |
3074 | | |
3075 | 0 | bool FunctionDecl::isVariadic() const { |
3076 | 0 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
3077 | 0 | return FT->isVariadic(); |
3078 | 0 | return false; |
3079 | 0 | } |
3080 | | |
3081 | | FunctionDecl::DefaultedFunctionInfo * |
3082 | | FunctionDecl::DefaultedFunctionInfo::Create(ASTContext &Context, |
3083 | 0 | ArrayRef<DeclAccessPair> Lookups) { |
3084 | 0 | DefaultedFunctionInfo *Info = new (Context.Allocate( |
3085 | 0 | totalSizeToAlloc<DeclAccessPair>(Lookups.size()), |
3086 | 0 | std::max(alignof(DefaultedFunctionInfo), alignof(DeclAccessPair)))) |
3087 | 0 | DefaultedFunctionInfo; |
3088 | 0 | Info->NumLookups = Lookups.size(); |
3089 | 0 | std::uninitialized_copy(Lookups.begin(), Lookups.end(), |
3090 | 0 | Info->getTrailingObjects<DeclAccessPair>()); |
3091 | 0 | return Info; |
3092 | 0 | } |
3093 | | |
3094 | 0 | void FunctionDecl::setDefaultedFunctionInfo(DefaultedFunctionInfo *Info) { |
3095 | 0 | assert(!FunctionDeclBits.HasDefaultedFunctionInfo && "already have this"); |
3096 | 0 | assert(!Body && "can't replace function body with defaulted function info"); |
3097 | | |
3098 | 0 | FunctionDeclBits.HasDefaultedFunctionInfo = true; |
3099 | 0 | DefaultedInfo = Info; |
3100 | 0 | } |
3101 | | |
3102 | | FunctionDecl::DefaultedFunctionInfo * |
3103 | 0 | FunctionDecl::getDefaultedFunctionInfo() const { |
3104 | 0 | return FunctionDeclBits.HasDefaultedFunctionInfo ? DefaultedInfo : nullptr; |
3105 | 0 | } |
3106 | | |
3107 | 0 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
3108 | 0 | for (const auto *I : redecls()) { |
3109 | 0 | if (I->doesThisDeclarationHaveABody()) { |
3110 | 0 | Definition = I; |
3111 | 0 | return true; |
3112 | 0 | } |
3113 | 0 | } |
3114 | | |
3115 | 0 | return false; |
3116 | 0 | } |
3117 | | |
3118 | 0 | bool FunctionDecl::hasTrivialBody() const { |
3119 | 0 | const Stmt *S = getBody(); |
3120 | 0 | if (!S) { |
3121 | | // Since we don't have a body for this function, we don't know if it's |
3122 | | // trivial or not. |
3123 | 0 | return false; |
3124 | 0 | } |
3125 | | |
3126 | 0 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
3127 | 0 | return true; |
3128 | 0 | return false; |
3129 | 0 | } |
3130 | | |
3131 | 0 | bool FunctionDecl::isThisDeclarationInstantiatedFromAFriendDefinition() const { |
3132 | 0 | if (!getFriendObjectKind()) |
3133 | 0 | return false; |
3134 | | |
3135 | | // Check for a friend function instantiated from a friend function |
3136 | | // definition in a templated class. |
3137 | 0 | if (const FunctionDecl *InstantiatedFrom = |
3138 | 0 | getInstantiatedFromMemberFunction()) |
3139 | 0 | return InstantiatedFrom->getFriendObjectKind() && |
3140 | 0 | InstantiatedFrom->isThisDeclarationADefinition(); |
3141 | | |
3142 | | // Check for a friend function template instantiated from a friend |
3143 | | // function template definition in a templated class. |
3144 | 0 | if (const FunctionTemplateDecl *Template = getDescribedFunctionTemplate()) { |
3145 | 0 | if (const FunctionTemplateDecl *InstantiatedFrom = |
3146 | 0 | Template->getInstantiatedFromMemberTemplate()) |
3147 | 0 | return InstantiatedFrom->getFriendObjectKind() && |
3148 | 0 | InstantiatedFrom->isThisDeclarationADefinition(); |
3149 | 0 | } |
3150 | | |
3151 | 0 | return false; |
3152 | 0 | } |
3153 | | |
3154 | | bool FunctionDecl::isDefined(const FunctionDecl *&Definition, |
3155 | 0 | bool CheckForPendingFriendDefinition) const { |
3156 | 0 | for (const FunctionDecl *FD : redecls()) { |
3157 | 0 | if (FD->isThisDeclarationADefinition()) { |
3158 | 0 | Definition = FD; |
3159 | 0 | return true; |
3160 | 0 | } |
3161 | | |
3162 | | // If this is a friend function defined in a class template, it does not |
3163 | | // have a body until it is used, nevertheless it is a definition, see |
3164 | | // [temp.inst]p2: |
3165 | | // |
3166 | | // ... for the purpose of determining whether an instantiated redeclaration |
3167 | | // is valid according to [basic.def.odr] and [class.mem], a declaration that |
3168 | | // corresponds to a definition in the template is considered to be a |
3169 | | // definition. |
3170 | | // |
3171 | | // The following code must produce redefinition error: |
3172 | | // |
3173 | | // template<typename T> struct C20 { friend void func_20() {} }; |
3174 | | // C20<int> c20i; |
3175 | | // void func_20() {} |
3176 | | // |
3177 | 0 | if (CheckForPendingFriendDefinition && |
3178 | 0 | FD->isThisDeclarationInstantiatedFromAFriendDefinition()) { |
3179 | 0 | Definition = FD; |
3180 | 0 | return true; |
3181 | 0 | } |
3182 | 0 | } |
3183 | | |
3184 | 0 | return false; |
3185 | 0 | } |
3186 | | |
3187 | 0 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
3188 | 0 | if (!hasBody(Definition)) |
3189 | 0 | return nullptr; |
3190 | | |
3191 | 0 | assert(!Definition->FunctionDeclBits.HasDefaultedFunctionInfo && |
3192 | 0 | "definition should not have a body"); |
3193 | 0 | if (Definition->Body) |
3194 | 0 | return Definition->Body.get(getASTContext().getExternalSource()); |
3195 | | |
3196 | 0 | return nullptr; |
3197 | 0 | } |
3198 | | |
3199 | 0 | void FunctionDecl::setBody(Stmt *B) { |
3200 | 0 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
3201 | 0 | Body = LazyDeclStmtPtr(B); |
3202 | 0 | if (B) |
3203 | 0 | EndRangeLoc = B->getEndLoc(); |
3204 | 0 | } |
3205 | | |
3206 | 0 | void FunctionDecl::setPure(bool P) { |
3207 | 0 | FunctionDeclBits.IsPure = P; |
3208 | 0 | if (P) |
3209 | 0 | if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
3210 | 0 | Parent->markedVirtualFunctionPure(); |
3211 | 0 | } |
3212 | | |
3213 | | template<std::size_t Len> |
3214 | 18 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
3215 | 18 | const IdentifierInfo *II = ND->getIdentifier(); |
3216 | 18 | return II && II->isStr(Str); |
3217 | 18 | } |
3218 | | |
3219 | 0 | bool FunctionDecl::isImmediateEscalating() const { |
3220 | | // C++23 [expr.const]/p17 |
3221 | | // An immediate-escalating function is |
3222 | | // - the call operator of a lambda that is not declared with the consteval |
3223 | | // specifier, |
3224 | 0 | if (isLambdaCallOperator(this) && !isConsteval()) |
3225 | 0 | return true; |
3226 | | // - a defaulted special member function that is not declared with the |
3227 | | // consteval specifier, |
3228 | 0 | if (isDefaulted() && !isConsteval()) |
3229 | 0 | return true; |
3230 | | // - a function that results from the instantiation of a templated entity |
3231 | | // defined with the constexpr specifier. |
3232 | 0 | TemplatedKind TK = getTemplatedKind(); |
3233 | 0 | if (TK != TK_NonTemplate && TK != TK_DependentNonTemplate && |
3234 | 0 | isConstexprSpecified()) |
3235 | 0 | return true; |
3236 | 0 | return false; |
3237 | 0 | } |
3238 | | |
3239 | 0 | bool FunctionDecl::isImmediateFunction() const { |
3240 | | // C++23 [expr.const]/p18 |
3241 | | // An immediate function is a function or constructor that is |
3242 | | // - declared with the consteval specifier |
3243 | 0 | if (isConsteval()) |
3244 | 0 | return true; |
3245 | | // - an immediate-escalating function F whose function body contains an |
3246 | | // immediate-escalating expression |
3247 | 0 | if (isImmediateEscalating() && BodyContainsImmediateEscalatingExpressions()) |
3248 | 0 | return true; |
3249 | | |
3250 | 0 | if (const auto *MD = dyn_cast<CXXMethodDecl>(this); |
3251 | 0 | MD && MD->isLambdaStaticInvoker()) |
3252 | 0 | return MD->getParent()->getLambdaCallOperator()->isImmediateFunction(); |
3253 | | |
3254 | 0 | return false; |
3255 | 0 | } |
3256 | | |
3257 | 18 | bool FunctionDecl::isMain() const { |
3258 | 18 | const TranslationUnitDecl *tunit = |
3259 | 18 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
3260 | 18 | return tunit && |
3261 | 18 | !tunit->getASTContext().getLangOpts().Freestanding && |
3262 | 18 | isNamed(this, "main"); |
3263 | 18 | } |
3264 | | |
3265 | 9 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
3266 | 9 | const TranslationUnitDecl *TUnit = |
3267 | 9 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
3268 | 9 | if (!TUnit) |
3269 | 0 | return false; |
3270 | | |
3271 | | // Even though we aren't really targeting MSVCRT if we are freestanding, |
3272 | | // semantic analysis for these functions remains the same. |
3273 | | |
3274 | | // MSVCRT entry points only exist on MSVCRT targets. |
3275 | 9 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
3276 | 9 | return false; |
3277 | | |
3278 | | // Nameless functions like constructors cannot be entry points. |
3279 | 0 | if (!getIdentifier()) |
3280 | 0 | return false; |
3281 | | |
3282 | 0 | return llvm::StringSwitch<bool>(getName()) |
3283 | 0 | .Cases("main", // an ANSI console app |
3284 | 0 | "wmain", // a Unicode console App |
3285 | 0 | "WinMain", // an ANSI GUI app |
3286 | 0 | "wWinMain", // a Unicode GUI app |
3287 | 0 | "DllMain", // a DLL |
3288 | 0 | true) |
3289 | 0 | .Default(false); |
3290 | 0 | } |
3291 | | |
3292 | 0 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
3293 | 0 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
3294 | 0 | return false; |
3295 | 0 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
3296 | 0 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
3297 | 0 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
3298 | 0 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
3299 | 0 | return false; |
3300 | | |
3301 | 0 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
3302 | 0 | return false; |
3303 | | |
3304 | 0 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
3305 | 0 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
3306 | 0 | return false; |
3307 | | |
3308 | 0 | const ASTContext &Context = |
3309 | 0 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
3310 | 0 | ->getASTContext(); |
3311 | | |
3312 | | // The result type and first argument type are constant across all |
3313 | | // these operators. The second argument must be exactly void*. |
3314 | 0 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
3315 | 0 | } |
3316 | | |
3317 | | bool FunctionDecl::isReplaceableGlobalAllocationFunction( |
3318 | 0 | std::optional<unsigned> *AlignmentParam, bool *IsNothrow) const { |
3319 | 0 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
3320 | 0 | return false; |
3321 | 0 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
3322 | 0 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
3323 | 0 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
3324 | 0 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
3325 | 0 | return false; |
3326 | | |
3327 | 0 | if (isa<CXXRecordDecl>(getDeclContext())) |
3328 | 0 | return false; |
3329 | | |
3330 | | // This can only fail for an invalid 'operator new' declaration. |
3331 | 0 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
3332 | 0 | return false; |
3333 | | |
3334 | 0 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
3335 | 0 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 4 || FPT->isVariadic()) |
3336 | 0 | return false; |
3337 | | |
3338 | | // If this is a single-parameter function, it must be a replaceable global |
3339 | | // allocation or deallocation function. |
3340 | 0 | if (FPT->getNumParams() == 1) |
3341 | 0 | return true; |
3342 | | |
3343 | 0 | unsigned Params = 1; |
3344 | 0 | QualType Ty = FPT->getParamType(Params); |
3345 | 0 | const ASTContext &Ctx = getASTContext(); |
3346 | |
|
3347 | 0 | auto Consume = [&] { |
3348 | 0 | ++Params; |
3349 | 0 | Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); |
3350 | 0 | }; |
3351 | | |
3352 | | // In C++14, the next parameter can be a 'std::size_t' for sized delete. |
3353 | 0 | bool IsSizedDelete = false; |
3354 | 0 | if (Ctx.getLangOpts().SizedDeallocation && |
3355 | 0 | (getDeclName().getCXXOverloadedOperator() == OO_Delete || |
3356 | 0 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && |
3357 | 0 | Ctx.hasSameType(Ty, Ctx.getSizeType())) { |
3358 | 0 | IsSizedDelete = true; |
3359 | 0 | Consume(); |
3360 | 0 | } |
3361 | | |
3362 | | // In C++17, the next parameter can be a 'std::align_val_t' for aligned |
3363 | | // new/delete. |
3364 | 0 | if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { |
3365 | 0 | Consume(); |
3366 | 0 | if (AlignmentParam) |
3367 | 0 | *AlignmentParam = Params; |
3368 | 0 | } |
3369 | | |
3370 | | // If this is not a sized delete, the next parameter can be a |
3371 | | // 'const std::nothrow_t&'. |
3372 | 0 | if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { |
3373 | 0 | Ty = Ty->getPointeeType(); |
3374 | 0 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
3375 | 0 | return false; |
3376 | 0 | if (Ty->isNothrowT()) { |
3377 | 0 | if (IsNothrow) |
3378 | 0 | *IsNothrow = true; |
3379 | 0 | Consume(); |
3380 | 0 | } |
3381 | 0 | } |
3382 | | |
3383 | | // Finally, recognize the not yet standard versions of new that take a |
3384 | | // hot/cold allocation hint (__hot_cold_t). These are currently supported by |
3385 | | // tcmalloc (see |
3386 | | // https://github.com/google/tcmalloc/blob/220043886d4e2efff7a5702d5172cb8065253664/tcmalloc/malloc_extension.h#L53). |
3387 | 0 | if (!IsSizedDelete && !Ty.isNull() && Ty->isEnumeralType()) { |
3388 | 0 | QualType T = Ty; |
3389 | 0 | while (const auto *TD = T->getAs<TypedefType>()) |
3390 | 0 | T = TD->getDecl()->getUnderlyingType(); |
3391 | 0 | const IdentifierInfo *II = |
3392 | 0 | T->castAs<EnumType>()->getDecl()->getIdentifier(); |
3393 | 0 | if (II && II->isStr("__hot_cold_t")) |
3394 | 0 | Consume(); |
3395 | 0 | } |
3396 | |
|
3397 | 0 | return Params == FPT->getNumParams(); |
3398 | 0 | } |
3399 | | |
3400 | 0 | bool FunctionDecl::isInlineBuiltinDeclaration() const { |
3401 | 0 | if (!getBuiltinID()) |
3402 | 0 | return false; |
3403 | | |
3404 | 0 | const FunctionDecl *Definition; |
3405 | 0 | if (!hasBody(Definition)) |
3406 | 0 | return false; |
3407 | | |
3408 | 0 | if (!Definition->isInlineSpecified() || |
3409 | 0 | !Definition->hasAttr<AlwaysInlineAttr>()) |
3410 | 0 | return false; |
3411 | | |
3412 | 0 | ASTContext &Context = getASTContext(); |
3413 | 0 | switch (Context.GetGVALinkageForFunction(Definition)) { |
3414 | 0 | case GVA_Internal: |
3415 | 0 | case GVA_DiscardableODR: |
3416 | 0 | case GVA_StrongODR: |
3417 | 0 | return false; |
3418 | 0 | case GVA_AvailableExternally: |
3419 | 0 | case GVA_StrongExternal: |
3420 | 0 | return true; |
3421 | 0 | } |
3422 | 0 | llvm_unreachable("Unknown GVALinkage"); |
3423 | 0 | } |
3424 | | |
3425 | 0 | bool FunctionDecl::isDestroyingOperatorDelete() const { |
3426 | | // C++ P0722: |
3427 | | // Within a class C, a single object deallocation function with signature |
3428 | | // (T, std::destroying_delete_t, <more params>) |
3429 | | // is a destroying operator delete. |
3430 | 0 | if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || |
3431 | 0 | getNumParams() < 2) |
3432 | 0 | return false; |
3433 | | |
3434 | 0 | auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); |
3435 | 0 | return RD && RD->isInStdNamespace() && RD->getIdentifier() && |
3436 | 0 | RD->getIdentifier()->isStr("destroying_delete_t"); |
3437 | 0 | } |
3438 | | |
3439 | 6 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
3440 | 6 | return getDeclLanguageLinkage(*this); |
3441 | 6 | } |
3442 | | |
3443 | 6 | bool FunctionDecl::isExternC() const { |
3444 | 6 | return isDeclExternC(*this); |
3445 | 6 | } |
3446 | | |
3447 | 1 | bool FunctionDecl::isInExternCContext() const { |
3448 | 1 | if (hasAttr<OpenCLKernelAttr>()) |
3449 | 0 | return true; |
3450 | 1 | return getLexicalDeclContext()->isExternCContext(); |
3451 | 1 | } |
3452 | | |
3453 | 0 | bool FunctionDecl::isInExternCXXContext() const { |
3454 | 0 | return getLexicalDeclContext()->isExternCXXContext(); |
3455 | 0 | } |
3456 | | |
3457 | 0 | bool FunctionDecl::isGlobal() const { |
3458 | 0 | if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) |
3459 | 0 | return Method->isStatic(); |
3460 | | |
3461 | 0 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
3462 | 0 | return false; |
3463 | | |
3464 | 0 | for (const DeclContext *DC = getDeclContext(); |
3465 | 0 | DC->isNamespace(); |
3466 | 0 | DC = DC->getParent()) { |
3467 | 0 | if (const auto *Namespace = cast<NamespaceDecl>(DC)) { |
3468 | 0 | if (!Namespace->getDeclName()) |
3469 | 0 | return false; |
3470 | 0 | } |
3471 | 0 | } |
3472 | | |
3473 | 0 | return true; |
3474 | 0 | } |
3475 | | |
3476 | 0 | bool FunctionDecl::isNoReturn() const { |
3477 | 0 | if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
3478 | 0 | hasAttr<C11NoReturnAttr>()) |
3479 | 0 | return true; |
3480 | | |
3481 | 0 | if (auto *FnTy = getType()->getAs<FunctionType>()) |
3482 | 0 | return FnTy->getNoReturnAttr(); |
3483 | | |
3484 | 0 | return false; |
3485 | 0 | } |
3486 | | |
3487 | 0 | bool FunctionDecl::isMemberLikeConstrainedFriend() const { |
3488 | | // C++20 [temp.friend]p9: |
3489 | | // A non-template friend declaration with a requires-clause [or] |
3490 | | // a friend function template with a constraint that depends on a template |
3491 | | // parameter from an enclosing template [...] does not declare the same |
3492 | | // function or function template as a declaration in any other scope. |
3493 | | |
3494 | | // If this isn't a friend then it's not a member-like constrained friend. |
3495 | 0 | if (!getFriendObjectKind()) { |
3496 | 0 | return false; |
3497 | 0 | } |
3498 | | |
3499 | 0 | if (!getDescribedFunctionTemplate()) { |
3500 | | // If these friends don't have constraints, they aren't constrained, and |
3501 | | // thus don't fall under temp.friend p9. Else the simple presence of a |
3502 | | // constraint makes them unique. |
3503 | 0 | return getTrailingRequiresClause(); |
3504 | 0 | } |
3505 | | |
3506 | 0 | return FriendConstraintRefersToEnclosingTemplate(); |
3507 | 0 | } |
3508 | | |
3509 | 9 | MultiVersionKind FunctionDecl::getMultiVersionKind() const { |
3510 | 9 | if (hasAttr<TargetAttr>()) |
3511 | 0 | return MultiVersionKind::Target; |
3512 | 9 | if (hasAttr<TargetVersionAttr>()) |
3513 | 0 | return MultiVersionKind::TargetVersion; |
3514 | 9 | if (hasAttr<CPUDispatchAttr>()) |
3515 | 0 | return MultiVersionKind::CPUDispatch; |
3516 | 9 | if (hasAttr<CPUSpecificAttr>()) |
3517 | 0 | return MultiVersionKind::CPUSpecific; |
3518 | 9 | if (hasAttr<TargetClonesAttr>()) |
3519 | 0 | return MultiVersionKind::TargetClones; |
3520 | 9 | return MultiVersionKind::None; |
3521 | 9 | } |
3522 | | |
3523 | 2 | bool FunctionDecl::isCPUDispatchMultiVersion() const { |
3524 | 2 | return isMultiVersion() && hasAttr<CPUDispatchAttr>(); |
3525 | 2 | } |
3526 | | |
3527 | 2 | bool FunctionDecl::isCPUSpecificMultiVersion() const { |
3528 | 2 | return isMultiVersion() && hasAttr<CPUSpecificAttr>(); |
3529 | 2 | } |
3530 | | |
3531 | 0 | bool FunctionDecl::isTargetMultiVersion() const { |
3532 | 0 | return isMultiVersion() && |
3533 | 0 | (hasAttr<TargetAttr>() || hasAttr<TargetVersionAttr>()); |
3534 | 0 | } |
3535 | | |
3536 | 0 | bool FunctionDecl::isTargetClonesMultiVersion() const { |
3537 | 0 | return isMultiVersion() && hasAttr<TargetClonesAttr>(); |
3538 | 0 | } |
3539 | | |
3540 | | void |
3541 | 0 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
3542 | 0 | redeclarable_base::setPreviousDecl(PrevDecl); |
3543 | |
|
3544 | 0 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
3545 | 0 | FunctionTemplateDecl *PrevFunTmpl |
3546 | 0 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
3547 | 0 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
3548 | 0 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
3549 | 0 | } |
3550 | | |
3551 | 0 | if (PrevDecl && PrevDecl->isInlined()) |
3552 | 0 | setImplicitlyInline(true); |
3553 | 0 | } |
3554 | | |
3555 | 58 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
3556 | | |
3557 | | /// Returns a value indicating whether this function corresponds to a builtin |
3558 | | /// function. |
3559 | | /// |
3560 | | /// The function corresponds to a built-in function if it is declared at |
3561 | | /// translation scope or within an extern "C" block and its name matches with |
3562 | | /// the name of a builtin. The returned value will be 0 for functions that do |
3563 | | /// not correspond to a builtin, a value of type \c Builtin::ID if in the |
3564 | | /// target-independent range \c [1,Builtin::First), or a target-specific builtin |
3565 | | /// value. |
3566 | | /// |
3567 | | /// \param ConsiderWrapperFunctions If true, we should consider wrapper |
3568 | | /// functions as their wrapped builtins. This shouldn't be done in general, but |
3569 | | /// it's useful in Sema to diagnose calls to wrappers based on their semantics. |
3570 | 6 | unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { |
3571 | 6 | unsigned BuiltinID = 0; |
3572 | | |
3573 | 6 | if (const auto *ABAA = getAttr<ArmBuiltinAliasAttr>()) { |
3574 | 0 | BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); |
3575 | 6 | } else if (const auto *BAA = getAttr<BuiltinAliasAttr>()) { |
3576 | 0 | BuiltinID = BAA->getBuiltinName()->getBuiltinID(); |
3577 | 6 | } else if (const auto *A = getAttr<BuiltinAttr>()) { |
3578 | 0 | BuiltinID = A->getID(); |
3579 | 0 | } |
3580 | | |
3581 | 6 | if (!BuiltinID) |
3582 | 6 | return 0; |
3583 | | |
3584 | | // If the function is marked "overloadable", it has a different mangled name |
3585 | | // and is not the C library function. |
3586 | 0 | if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>() && |
3587 | 0 | (!hasAttr<ArmBuiltinAliasAttr>() && !hasAttr<BuiltinAliasAttr>())) |
3588 | 0 | return 0; |
3589 | | |
3590 | 0 | const ASTContext &Context = getASTContext(); |
3591 | 0 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
3592 | 0 | return BuiltinID; |
3593 | | |
3594 | | // This function has the name of a known C library |
3595 | | // function. Determine whether it actually refers to the C library |
3596 | | // function or whether it just has the same name. |
3597 | | |
3598 | | // If this is a static function, it's not a builtin. |
3599 | 0 | if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) |
3600 | 0 | return 0; |
3601 | | |
3602 | | // OpenCL v1.2 s6.9.f - The library functions defined in |
3603 | | // the C99 standard headers are not available. |
3604 | 0 | if (Context.getLangOpts().OpenCL && |
3605 | 0 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
3606 | 0 | return 0; |
3607 | | |
3608 | | // CUDA does not have device-side standard library. printf and malloc are the |
3609 | | // only special cases that are supported by device-side runtime. |
3610 | 0 | if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && |
3611 | 0 | !hasAttr<CUDAHostAttr>() && |
3612 | 0 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
3613 | 0 | return 0; |
3614 | | |
3615 | | // As AMDGCN implementation of OpenMP does not have a device-side standard |
3616 | | // library, none of the predefined library functions except printf and malloc |
3617 | | // should be treated as a builtin i.e. 0 should be returned for them. |
3618 | 0 | if (Context.getTargetInfo().getTriple().isAMDGCN() && |
3619 | 0 | Context.getLangOpts().OpenMPIsTargetDevice && |
3620 | 0 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
3621 | 0 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
3622 | 0 | return 0; |
3623 | | |
3624 | 0 | return BuiltinID; |
3625 | 0 | } |
3626 | | |
3627 | | /// getNumParams - Return the number of parameters this function must have |
3628 | | /// based on its FunctionType. This is the length of the ParamInfo array |
3629 | | /// after it has been created. |
3630 | 38 | unsigned FunctionDecl::getNumParams() const { |
3631 | 38 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
3632 | 38 | return FPT ? FPT->getNumParams() : 0; |
3633 | 38 | } |
3634 | | |
3635 | | void FunctionDecl::setParams(ASTContext &C, |
3636 | 19 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
3637 | 19 | assert(!ParamInfo && "Already has param info!"); |
3638 | 0 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
3639 | | |
3640 | | // Zero params -> null pointer. |
3641 | 19 | if (!NewParamInfo.empty()) { |
3642 | 9 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
3643 | 9 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
3644 | 9 | } |
3645 | 19 | } |
3646 | | |
3647 | | /// getMinRequiredArguments - Returns the minimum number of arguments |
3648 | | /// needed to call this function. This may be fewer than the number of |
3649 | | /// function parameters, if some of the parameters have default |
3650 | | /// arguments (in C++) or are parameter packs (C++11). |
3651 | 0 | unsigned FunctionDecl::getMinRequiredArguments() const { |
3652 | 0 | if (!getASTContext().getLangOpts().CPlusPlus) |
3653 | 0 | return getNumParams(); |
3654 | | |
3655 | | // Note that it is possible for a parameter with no default argument to |
3656 | | // follow a parameter with a default argument. |
3657 | 0 | unsigned NumRequiredArgs = 0; |
3658 | 0 | unsigned MinParamsSoFar = 0; |
3659 | 0 | for (auto *Param : parameters()) { |
3660 | 0 | if (!Param->isParameterPack()) { |
3661 | 0 | ++MinParamsSoFar; |
3662 | 0 | if (!Param->hasDefaultArg()) |
3663 | 0 | NumRequiredArgs = MinParamsSoFar; |
3664 | 0 | } |
3665 | 0 | } |
3666 | 0 | return NumRequiredArgs; |
3667 | 0 | } |
3668 | | |
3669 | 0 | bool FunctionDecl::hasCXXExplicitFunctionObjectParameter() const { |
3670 | 0 | return getNumParams() != 0 && getParamDecl(0)->isExplicitObjectParameter(); |
3671 | 0 | } |
3672 | | |
3673 | 0 | unsigned FunctionDecl::getNumNonObjectParams() const { |
3674 | 0 | return getNumParams() - |
3675 | 0 | static_cast<unsigned>(hasCXXExplicitFunctionObjectParameter()); |
3676 | 0 | } |
3677 | | |
3678 | 0 | unsigned FunctionDecl::getMinRequiredExplicitArguments() const { |
3679 | 0 | return getMinRequiredArguments() - |
3680 | 0 | static_cast<unsigned>(hasCXXExplicitFunctionObjectParameter()); |
3681 | 0 | } |
3682 | | |
3683 | 0 | bool FunctionDecl::hasOneParamOrDefaultArgs() const { |
3684 | 0 | return getNumParams() == 1 || |
3685 | 0 | (getNumParams() > 1 && |
3686 | 0 | llvm::all_of(llvm::drop_begin(parameters()), |
3687 | 0 | [](ParmVarDecl *P) { return P->hasDefaultArg(); })); |
3688 | 0 | } |
3689 | | |
3690 | | /// The combination of the extern and inline keywords under MSVC forces |
3691 | | /// the function to be required. |
3692 | | /// |
3693 | | /// Note: This function assumes that we will only get called when isInlined() |
3694 | | /// would return true for this FunctionDecl. |
3695 | 0 | bool FunctionDecl::isMSExternInline() const { |
3696 | 0 | assert(isInlined() && "expected to get called on an inlined function!"); |
3697 | | |
3698 | 0 | const ASTContext &Context = getASTContext(); |
3699 | 0 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
3700 | 0 | !hasAttr<DLLExportAttr>()) |
3701 | 0 | return false; |
3702 | | |
3703 | 0 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
3704 | 0 | FD = FD->getPreviousDecl()) |
3705 | 0 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
3706 | 0 | return true; |
3707 | | |
3708 | 0 | return false; |
3709 | 0 | } |
3710 | | |
3711 | 0 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
3712 | 0 | if (Redecl->getStorageClass() != SC_Extern) |
3713 | 0 | return false; |
3714 | | |
3715 | 0 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
3716 | 0 | FD = FD->getPreviousDecl()) |
3717 | 0 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
3718 | 0 | return false; |
3719 | | |
3720 | 0 | return true; |
3721 | 0 | } |
3722 | | |
3723 | 0 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
3724 | | // Only consider file-scope declarations in this test. |
3725 | 0 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
3726 | 0 | return false; |
3727 | | |
3728 | | // Only consider explicit declarations; the presence of a builtin for a |
3729 | | // libcall shouldn't affect whether a definition is externally visible. |
3730 | 0 | if (Redecl->isImplicit()) |
3731 | 0 | return false; |
3732 | | |
3733 | 0 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
3734 | 0 | return true; // Not an inline definition |
3735 | | |
3736 | 0 | return false; |
3737 | 0 | } |
3738 | | |
3739 | | /// For a function declaration in C or C++, determine whether this |
3740 | | /// declaration causes the definition to be externally visible. |
3741 | | /// |
3742 | | /// For instance, this determines if adding the current declaration to the set |
3743 | | /// of redeclarations of the given functions causes |
3744 | | /// isInlineDefinitionExternallyVisible to change from false to true. |
3745 | 0 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
3746 | 0 | assert(!doesThisDeclarationHaveABody() && |
3747 | 0 | "Must have a declaration without a body."); |
3748 | | |
3749 | 0 | const ASTContext &Context = getASTContext(); |
3750 | |
|
3751 | 0 | if (Context.getLangOpts().MSVCCompat) { |
3752 | 0 | const FunctionDecl *Definition; |
3753 | 0 | if (hasBody(Definition) && Definition->isInlined() && |
3754 | 0 | redeclForcesDefMSVC(this)) |
3755 | 0 | return true; |
3756 | 0 | } |
3757 | | |
3758 | 0 | if (Context.getLangOpts().CPlusPlus) |
3759 | 0 | return false; |
3760 | | |
3761 | 0 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
3762 | | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
3763 | | // an externally visible definition. |
3764 | | // |
3765 | | // FIXME: What happens if gnu_inline gets added on after the first |
3766 | | // declaration? |
3767 | 0 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
3768 | 0 | return false; |
3769 | | |
3770 | 0 | const FunctionDecl *Prev = this; |
3771 | 0 | bool FoundBody = false; |
3772 | 0 | while ((Prev = Prev->getPreviousDecl())) { |
3773 | 0 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
3774 | |
|
3775 | 0 | if (Prev->doesThisDeclarationHaveABody()) { |
3776 | | // If it's not the case that both 'inline' and 'extern' are |
3777 | | // specified on the definition, then it is always externally visible. |
3778 | 0 | if (!Prev->isInlineSpecified() || |
3779 | 0 | Prev->getStorageClass() != SC_Extern) |
3780 | 0 | return false; |
3781 | 0 | } else if (Prev->isInlineSpecified() && |
3782 | 0 | Prev->getStorageClass() != SC_Extern) { |
3783 | 0 | return false; |
3784 | 0 | } |
3785 | 0 | } |
3786 | 0 | return FoundBody; |
3787 | 0 | } |
3788 | | |
3789 | | // C99 6.7.4p6: |
3790 | | // [...] If all of the file scope declarations for a function in a |
3791 | | // translation unit include the inline function specifier without extern, |
3792 | | // then the definition in that translation unit is an inline definition. |
3793 | 0 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
3794 | 0 | return false; |
3795 | 0 | const FunctionDecl *Prev = this; |
3796 | 0 | bool FoundBody = false; |
3797 | 0 | while ((Prev = Prev->getPreviousDecl())) { |
3798 | 0 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
3799 | 0 | if (RedeclForcesDefC99(Prev)) |
3800 | 0 | return false; |
3801 | 0 | } |
3802 | 0 | return FoundBody; |
3803 | 0 | } |
3804 | | |
3805 | 0 | FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const { |
3806 | 0 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
3807 | 0 | return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>() |
3808 | 0 | : FunctionTypeLoc(); |
3809 | 0 | } |
3810 | | |
3811 | 0 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
3812 | 0 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
3813 | 0 | if (!FTL) |
3814 | 0 | return SourceRange(); |
3815 | | |
3816 | | // Skip self-referential return types. |
3817 | 0 | const SourceManager &SM = getASTContext().getSourceManager(); |
3818 | 0 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
3819 | 0 | SourceLocation Boundary = getNameInfo().getBeginLoc(); |
3820 | 0 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
3821 | 0 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
3822 | 0 | return SourceRange(); |
3823 | | |
3824 | 0 | return RTRange; |
3825 | 0 | } |
3826 | | |
3827 | 0 | SourceRange FunctionDecl::getParametersSourceRange() const { |
3828 | 0 | unsigned NP = getNumParams(); |
3829 | 0 | SourceLocation EllipsisLoc = getEllipsisLoc(); |
3830 | |
|
3831 | 0 | if (NP == 0 && EllipsisLoc.isInvalid()) |
3832 | 0 | return SourceRange(); |
3833 | | |
3834 | 0 | SourceLocation Begin = |
3835 | 0 | NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() : EllipsisLoc; |
3836 | 0 | SourceLocation End = EllipsisLoc.isValid() |
3837 | 0 | ? EllipsisLoc |
3838 | 0 | : ParamInfo[NP - 1]->getSourceRange().getEnd(); |
3839 | |
|
3840 | 0 | return SourceRange(Begin, End); |
3841 | 0 | } |
3842 | | |
3843 | 0 | SourceRange FunctionDecl::getExceptionSpecSourceRange() const { |
3844 | 0 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
3845 | 0 | return FTL ? FTL.getExceptionSpecRange() : SourceRange(); |
3846 | 0 | } |
3847 | | |
3848 | | /// For an inline function definition in C, or for a gnu_inline function |
3849 | | /// in C++, determine whether the definition will be externally visible. |
3850 | | /// |
3851 | | /// Inline function definitions are always available for inlining optimizations. |
3852 | | /// However, depending on the language dialect, declaration specifiers, and |
3853 | | /// attributes, the definition of an inline function may or may not be |
3854 | | /// "externally" visible to other translation units in the program. |
3855 | | /// |
3856 | | /// In C99, inline definitions are not externally visible by default. However, |
3857 | | /// if even one of the global-scope declarations is marked "extern inline", the |
3858 | | /// inline definition becomes externally visible (C99 6.7.4p6). |
3859 | | /// |
3860 | | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
3861 | | /// definition, we use the GNU semantics for inline, which are nearly the |
3862 | | /// opposite of C99 semantics. In particular, "inline" by itself will create |
3863 | | /// an externally visible symbol, but "extern inline" will not create an |
3864 | | /// externally visible symbol. |
3865 | 0 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
3866 | 0 | assert((doesThisDeclarationHaveABody() || willHaveBody() || |
3867 | 0 | hasAttr<AliasAttr>()) && |
3868 | 0 | "Must be a function definition"); |
3869 | 0 | assert(isInlined() && "Function must be inline"); |
3870 | 0 | ASTContext &Context = getASTContext(); |
3871 | |
|
3872 | 0 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
3873 | | // Note: If you change the logic here, please change |
3874 | | // doesDeclarationForceExternallyVisibleDefinition as well. |
3875 | | // |
3876 | | // If it's not the case that both 'inline' and 'extern' are |
3877 | | // specified on the definition, then this inline definition is |
3878 | | // externally visible. |
3879 | 0 | if (Context.getLangOpts().CPlusPlus) |
3880 | 0 | return false; |
3881 | 0 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
3882 | 0 | return true; |
3883 | | |
3884 | | // If any declaration is 'inline' but not 'extern', then this definition |
3885 | | // is externally visible. |
3886 | 0 | for (auto *Redecl : redecls()) { |
3887 | 0 | if (Redecl->isInlineSpecified() && |
3888 | 0 | Redecl->getStorageClass() != SC_Extern) |
3889 | 0 | return true; |
3890 | 0 | } |
3891 | | |
3892 | 0 | return false; |
3893 | 0 | } |
3894 | | |
3895 | | // The rest of this function is C-only. |
3896 | 0 | assert(!Context.getLangOpts().CPlusPlus && |
3897 | 0 | "should not use C inline rules in C++"); |
3898 | | |
3899 | | // C99 6.7.4p6: |
3900 | | // [...] If all of the file scope declarations for a function in a |
3901 | | // translation unit include the inline function specifier without extern, |
3902 | | // then the definition in that translation unit is an inline definition. |
3903 | 0 | for (auto *Redecl : redecls()) { |
3904 | 0 | if (RedeclForcesDefC99(Redecl)) |
3905 | 0 | return true; |
3906 | 0 | } |
3907 | | |
3908 | | // C99 6.7.4p6: |
3909 | | // An inline definition does not provide an external definition for the |
3910 | | // function, and does not forbid an external definition in another |
3911 | | // translation unit. |
3912 | 0 | return false; |
3913 | 0 | } |
3914 | | |
3915 | | /// getOverloadedOperator - Which C++ overloaded operator this |
3916 | | /// function represents, if any. |
3917 | 1 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
3918 | 1 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
3919 | 0 | return getDeclName().getCXXOverloadedOperator(); |
3920 | 1 | return OO_None; |
3921 | 1 | } |
3922 | | |
3923 | | /// getLiteralIdentifier - The literal suffix identifier this function |
3924 | | /// represents, if any. |
3925 | 0 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
3926 | 0 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
3927 | 0 | return getDeclName().getCXXLiteralIdentifier(); |
3928 | 0 | return nullptr; |
3929 | 0 | } |
3930 | | |
3931 | 0 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
3932 | 0 | if (TemplateOrSpecialization.isNull()) |
3933 | 0 | return TK_NonTemplate; |
3934 | 0 | if (const auto *ND = TemplateOrSpecialization.dyn_cast<NamedDecl *>()) { |
3935 | 0 | if (isa<FunctionDecl>(ND)) |
3936 | 0 | return TK_DependentNonTemplate; |
3937 | 0 | assert(isa<FunctionTemplateDecl>(ND) && |
3938 | 0 | "No other valid types in NamedDecl"); |
3939 | 0 | return TK_FunctionTemplate; |
3940 | 0 | } |
3941 | 0 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
3942 | 0 | return TK_MemberSpecialization; |
3943 | 0 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
3944 | 0 | return TK_FunctionTemplateSpecialization; |
3945 | 0 | if (TemplateOrSpecialization.is |
3946 | 0 | <DependentFunctionTemplateSpecializationInfo*>()) |
3947 | 0 | return TK_DependentFunctionTemplateSpecialization; |
3948 | | |
3949 | 0 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
3950 | 0 | } |
3951 | | |
3952 | 1 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
3953 | 1 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
3954 | 0 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
3955 | | |
3956 | 1 | return nullptr; |
3957 | 1 | } |
3958 | | |
3959 | 1 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
3960 | 1 | if (auto *MSI = |
3961 | 1 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
3962 | 0 | return MSI; |
3963 | 1 | if (auto *FTSI = TemplateOrSpecialization |
3964 | 1 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
3965 | 0 | return FTSI->getMemberSpecializationInfo(); |
3966 | 1 | return nullptr; |
3967 | 1 | } |
3968 | | |
3969 | | void |
3970 | | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
3971 | | FunctionDecl *FD, |
3972 | 0 | TemplateSpecializationKind TSK) { |
3973 | 0 | assert(TemplateOrSpecialization.isNull() && |
3974 | 0 | "Member function is already a specialization"); |
3975 | 0 | MemberSpecializationInfo *Info |
3976 | 0 | = new (C) MemberSpecializationInfo(FD, TSK); |
3977 | 0 | TemplateOrSpecialization = Info; |
3978 | 0 | } |
3979 | | |
3980 | 1 | FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { |
3981 | 1 | return dyn_cast_if_present<FunctionTemplateDecl>( |
3982 | 1 | TemplateOrSpecialization.dyn_cast<NamedDecl *>()); |
3983 | 1 | } |
3984 | | |
3985 | | void FunctionDecl::setDescribedFunctionTemplate( |
3986 | 0 | FunctionTemplateDecl *Template) { |
3987 | 0 | assert(TemplateOrSpecialization.isNull() && |
3988 | 0 | "Member function is already a specialization"); |
3989 | 0 | TemplateOrSpecialization = Template; |
3990 | 0 | } |
3991 | | |
3992 | 38 | bool FunctionDecl::isFunctionTemplateSpecialization() const { |
3993 | 38 | return TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>() || |
3994 | 38 | TemplateOrSpecialization |
3995 | 38 | .is<DependentFunctionTemplateSpecializationInfo *>(); |
3996 | 38 | } |
3997 | | |
3998 | 0 | void FunctionDecl::setInstantiatedFromDecl(FunctionDecl *FD) { |
3999 | 0 | assert(TemplateOrSpecialization.isNull() && |
4000 | 0 | "Function is already a specialization"); |
4001 | 0 | TemplateOrSpecialization = FD; |
4002 | 0 | } |
4003 | | |
4004 | 0 | FunctionDecl *FunctionDecl::getInstantiatedFromDecl() const { |
4005 | 0 | return dyn_cast_if_present<FunctionDecl>( |
4006 | 0 | TemplateOrSpecialization.dyn_cast<NamedDecl *>()); |
4007 | 0 | } |
4008 | | |
4009 | 0 | bool FunctionDecl::isImplicitlyInstantiable() const { |
4010 | | // If the function is invalid, it can't be implicitly instantiated. |
4011 | 0 | if (isInvalidDecl()) |
4012 | 0 | return false; |
4013 | | |
4014 | 0 | switch (getTemplateSpecializationKindForInstantiation()) { |
4015 | 0 | case TSK_Undeclared: |
4016 | 0 | case TSK_ExplicitInstantiationDefinition: |
4017 | 0 | case TSK_ExplicitSpecialization: |
4018 | 0 | return false; |
4019 | | |
4020 | 0 | case TSK_ImplicitInstantiation: |
4021 | 0 | return true; |
4022 | | |
4023 | 0 | case TSK_ExplicitInstantiationDeclaration: |
4024 | | // Handled below. |
4025 | 0 | break; |
4026 | 0 | } |
4027 | | |
4028 | | // Find the actual template from which we will instantiate. |
4029 | 0 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
4030 | 0 | bool HasPattern = false; |
4031 | 0 | if (PatternDecl) |
4032 | 0 | HasPattern = PatternDecl->hasBody(PatternDecl); |
4033 | | |
4034 | | // C++0x [temp.explicit]p9: |
4035 | | // Except for inline functions, other explicit instantiation declarations |
4036 | | // have the effect of suppressing the implicit instantiation of the entity |
4037 | | // to which they refer. |
4038 | 0 | if (!HasPattern || !PatternDecl) |
4039 | 0 | return true; |
4040 | | |
4041 | 0 | return PatternDecl->isInlined(); |
4042 | 0 | } |
4043 | | |
4044 | 0 | bool FunctionDecl::isTemplateInstantiation() const { |
4045 | | // FIXME: Remove this, it's not clear what it means. (Which template |
4046 | | // specialization kind?) |
4047 | 0 | return clang::isTemplateInstantiation(getTemplateSpecializationKind()); |
4048 | 0 | } |
4049 | | |
4050 | | FunctionDecl * |
4051 | 0 | FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const { |
4052 | | // If this is a generic lambda call operator specialization, its |
4053 | | // instantiation pattern is always its primary template's pattern |
4054 | | // even if its primary template was instantiated from another |
4055 | | // member template (which happens with nested generic lambdas). |
4056 | | // Since a lambda's call operator's body is transformed eagerly, |
4057 | | // we don't have to go hunting for a prototype definition template |
4058 | | // (i.e. instantiated-from-member-template) to use as an instantiation |
4059 | | // pattern. |
4060 | |
|
4061 | 0 | if (isGenericLambdaCallOperatorSpecialization( |
4062 | 0 | dyn_cast<CXXMethodDecl>(this))) { |
4063 | 0 | assert(getPrimaryTemplate() && "not a generic lambda call operator?"); |
4064 | 0 | return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); |
4065 | 0 | } |
4066 | | |
4067 | | // Check for a declaration of this function that was instantiated from a |
4068 | | // friend definition. |
4069 | 0 | const FunctionDecl *FD = nullptr; |
4070 | 0 | if (!isDefined(FD, /*CheckForPendingFriendDefinition=*/true)) |
4071 | 0 | FD = this; |
4072 | |
|
4073 | 0 | if (MemberSpecializationInfo *Info = FD->getMemberSpecializationInfo()) { |
4074 | 0 | if (ForDefinition && |
4075 | 0 | !clang::isTemplateInstantiation(Info->getTemplateSpecializationKind())) |
4076 | 0 | return nullptr; |
4077 | 0 | return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom())); |
4078 | 0 | } |
4079 | | |
4080 | 0 | if (ForDefinition && |
4081 | 0 | !clang::isTemplateInstantiation(getTemplateSpecializationKind())) |
4082 | 0 | return nullptr; |
4083 | | |
4084 | 0 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
4085 | | // If we hit a point where the user provided a specialization of this |
4086 | | // template, we're done looking. |
4087 | 0 | while (!ForDefinition || !Primary->isMemberSpecialization()) { |
4088 | 0 | auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); |
4089 | 0 | if (!NewPrimary) |
4090 | 0 | break; |
4091 | 0 | Primary = NewPrimary; |
4092 | 0 | } |
4093 | |
|
4094 | 0 | return getDefinitionOrSelf(Primary->getTemplatedDecl()); |
4095 | 0 | } |
4096 | | |
4097 | 0 | return nullptr; |
4098 | 0 | } |
4099 | | |
4100 | 1 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
4101 | 1 | if (FunctionTemplateSpecializationInfo *Info |
4102 | 1 | = TemplateOrSpecialization |
4103 | 1 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
4104 | 0 | return Info->getTemplate(); |
4105 | 0 | } |
4106 | 1 | return nullptr; |
4107 | 1 | } |
4108 | | |
4109 | | FunctionTemplateSpecializationInfo * |
4110 | 19 | FunctionDecl::getTemplateSpecializationInfo() const { |
4111 | 19 | return TemplateOrSpecialization |
4112 | 19 | .dyn_cast<FunctionTemplateSpecializationInfo *>(); |
4113 | 19 | } |
4114 | | |
4115 | | const TemplateArgumentList * |
4116 | 0 | FunctionDecl::getTemplateSpecializationArgs() const { |
4117 | 0 | if (FunctionTemplateSpecializationInfo *Info |
4118 | 0 | = TemplateOrSpecialization |
4119 | 0 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
4120 | 0 | return Info->TemplateArguments; |
4121 | 0 | } |
4122 | 0 | return nullptr; |
4123 | 0 | } |
4124 | | |
4125 | | const ASTTemplateArgumentListInfo * |
4126 | 0 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
4127 | 0 | if (FunctionTemplateSpecializationInfo *Info |
4128 | 0 | = TemplateOrSpecialization |
4129 | 0 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
4130 | 0 | return Info->TemplateArgumentsAsWritten; |
4131 | 0 | } |
4132 | 0 | if (DependentFunctionTemplateSpecializationInfo *Info = |
4133 | 0 | TemplateOrSpecialization |
4134 | 0 | .dyn_cast<DependentFunctionTemplateSpecializationInfo *>()) { |
4135 | 0 | return Info->TemplateArgumentsAsWritten; |
4136 | 0 | } |
4137 | 0 | return nullptr; |
4138 | 0 | } |
4139 | | |
4140 | | void |
4141 | | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
4142 | | FunctionTemplateDecl *Template, |
4143 | | const TemplateArgumentList *TemplateArgs, |
4144 | | void *InsertPos, |
4145 | | TemplateSpecializationKind TSK, |
4146 | | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
4147 | 0 | SourceLocation PointOfInstantiation) { |
4148 | 0 | assert((TemplateOrSpecialization.isNull() || |
4149 | 0 | TemplateOrSpecialization.is<MemberSpecializationInfo *>()) && |
4150 | 0 | "Member function is already a specialization"); |
4151 | 0 | assert(TSK != TSK_Undeclared && |
4152 | 0 | "Must specify the type of function template specialization"); |
4153 | 0 | assert((TemplateOrSpecialization.isNull() || |
4154 | 0 | getFriendObjectKind() != FOK_None || |
4155 | 0 | TSK == TSK_ExplicitSpecialization) && |
4156 | 0 | "Member specialization must be an explicit specialization"); |
4157 | 0 | FunctionTemplateSpecializationInfo *Info = |
4158 | 0 | FunctionTemplateSpecializationInfo::Create( |
4159 | 0 | C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, |
4160 | 0 | PointOfInstantiation, |
4161 | 0 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()); |
4162 | 0 | TemplateOrSpecialization = Info; |
4163 | 0 | Template->addSpecialization(Info, InsertPos); |
4164 | 0 | } |
4165 | | |
4166 | | void FunctionDecl::setDependentTemplateSpecialization( |
4167 | | ASTContext &Context, const UnresolvedSetImpl &Templates, |
4168 | 0 | const TemplateArgumentListInfo *TemplateArgs) { |
4169 | 0 | assert(TemplateOrSpecialization.isNull()); |
4170 | 0 | DependentFunctionTemplateSpecializationInfo *Info = |
4171 | 0 | DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, |
4172 | 0 | TemplateArgs); |
4173 | 0 | TemplateOrSpecialization = Info; |
4174 | 0 | } |
4175 | | |
4176 | | DependentFunctionTemplateSpecializationInfo * |
4177 | 0 | FunctionDecl::getDependentSpecializationInfo() const { |
4178 | 0 | return TemplateOrSpecialization |
4179 | 0 | .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); |
4180 | 0 | } |
4181 | | |
4182 | | DependentFunctionTemplateSpecializationInfo * |
4183 | | DependentFunctionTemplateSpecializationInfo::Create( |
4184 | | ASTContext &Context, const UnresolvedSetImpl &Candidates, |
4185 | 0 | const TemplateArgumentListInfo *TArgs) { |
4186 | 0 | const auto *TArgsWritten = |
4187 | 0 | TArgs ? ASTTemplateArgumentListInfo::Create(Context, *TArgs) : nullptr; |
4188 | 0 | return new (Context.Allocate( |
4189 | 0 | totalSizeToAlloc<FunctionTemplateDecl *>(Candidates.size()))) |
4190 | 0 | DependentFunctionTemplateSpecializationInfo(Candidates, TArgsWritten); |
4191 | 0 | } |
4192 | | |
4193 | | DependentFunctionTemplateSpecializationInfo:: |
4194 | | DependentFunctionTemplateSpecializationInfo( |
4195 | | const UnresolvedSetImpl &Candidates, |
4196 | | const ASTTemplateArgumentListInfo *TemplateArgsWritten) |
4197 | | : NumCandidates(Candidates.size()), |
4198 | 0 | TemplateArgumentsAsWritten(TemplateArgsWritten) { |
4199 | 0 | std::transform(Candidates.begin(), Candidates.end(), |
4200 | 0 | getTrailingObjects<FunctionTemplateDecl *>(), |
4201 | 0 | [](NamedDecl *ND) { |
4202 | 0 | return cast<FunctionTemplateDecl>(ND->getUnderlyingDecl()); |
4203 | 0 | }); |
4204 | 0 | } |
4205 | | |
4206 | 12 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
4207 | | // For a function template specialization, query the specialization |
4208 | | // information object. |
4209 | 12 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
4210 | 12 | TemplateOrSpecialization |
4211 | 12 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
4212 | 0 | return FTSInfo->getTemplateSpecializationKind(); |
4213 | | |
4214 | 12 | if (MemberSpecializationInfo *MSInfo = |
4215 | 12 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
4216 | 0 | return MSInfo->getTemplateSpecializationKind(); |
4217 | | |
4218 | | // A dependent function template specialization is an explicit specialization, |
4219 | | // except when it's a friend declaration. |
4220 | 12 | if (TemplateOrSpecialization |
4221 | 12 | .is<DependentFunctionTemplateSpecializationInfo *>() && |
4222 | 12 | getFriendObjectKind() == FOK_None) |
4223 | 0 | return TSK_ExplicitSpecialization; |
4224 | | |
4225 | 12 | return TSK_Undeclared; |
4226 | 12 | } |
4227 | | |
4228 | | TemplateSpecializationKind |
4229 | 0 | FunctionDecl::getTemplateSpecializationKindForInstantiation() const { |
4230 | | // This is the same as getTemplateSpecializationKind(), except that for a |
4231 | | // function that is both a function template specialization and a member |
4232 | | // specialization, we prefer the member specialization information. Eg: |
4233 | | // |
4234 | | // template<typename T> struct A { |
4235 | | // template<typename U> void f() {} |
4236 | | // template<> void f<int>() {} |
4237 | | // }; |
4238 | | // |
4239 | | // Within the templated CXXRecordDecl, A<T>::f<int> is a dependent function |
4240 | | // template specialization; both getTemplateSpecializationKind() and |
4241 | | // getTemplateSpecializationKindForInstantiation() will return |
4242 | | // TSK_ExplicitSpecialization. |
4243 | | // |
4244 | | // For A<int>::f<int>(): |
4245 | | // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization |
4246 | | // * getTemplateSpecializationKindForInstantiation() will return |
4247 | | // TSK_ImplicitInstantiation |
4248 | | // |
4249 | | // This reflects the facts that A<int>::f<int> is an explicit specialization |
4250 | | // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated |
4251 | | // from A::f<int> if a definition is needed. |
4252 | 0 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
4253 | 0 | TemplateOrSpecialization |
4254 | 0 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) { |
4255 | 0 | if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) |
4256 | 0 | return MSInfo->getTemplateSpecializationKind(); |
4257 | 0 | return FTSInfo->getTemplateSpecializationKind(); |
4258 | 0 | } |
4259 | | |
4260 | 0 | if (MemberSpecializationInfo *MSInfo = |
4261 | 0 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
4262 | 0 | return MSInfo->getTemplateSpecializationKind(); |
4263 | | |
4264 | 0 | if (TemplateOrSpecialization |
4265 | 0 | .is<DependentFunctionTemplateSpecializationInfo *>() && |
4266 | 0 | getFriendObjectKind() == FOK_None) |
4267 | 0 | return TSK_ExplicitSpecialization; |
4268 | | |
4269 | 0 | return TSK_Undeclared; |
4270 | 0 | } |
4271 | | |
4272 | | void |
4273 | | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
4274 | 0 | SourceLocation PointOfInstantiation) { |
4275 | 0 | if (FunctionTemplateSpecializationInfo *FTSInfo |
4276 | 0 | = TemplateOrSpecialization.dyn_cast< |
4277 | 0 | FunctionTemplateSpecializationInfo*>()) { |
4278 | 0 | FTSInfo->setTemplateSpecializationKind(TSK); |
4279 | 0 | if (TSK != TSK_ExplicitSpecialization && |
4280 | 0 | PointOfInstantiation.isValid() && |
4281 | 0 | FTSInfo->getPointOfInstantiation().isInvalid()) { |
4282 | 0 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
4283 | 0 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
4284 | 0 | L->InstantiationRequested(this); |
4285 | 0 | } |
4286 | 0 | } else if (MemberSpecializationInfo *MSInfo |
4287 | 0 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
4288 | 0 | MSInfo->setTemplateSpecializationKind(TSK); |
4289 | 0 | if (TSK != TSK_ExplicitSpecialization && |
4290 | 0 | PointOfInstantiation.isValid() && |
4291 | 0 | MSInfo->getPointOfInstantiation().isInvalid()) { |
4292 | 0 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
4293 | 0 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
4294 | 0 | L->InstantiationRequested(this); |
4295 | 0 | } |
4296 | 0 | } else |
4297 | 0 | llvm_unreachable("Function cannot have a template specialization kind"); |
4298 | 0 | } |
4299 | | |
4300 | 0 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
4301 | 0 | if (FunctionTemplateSpecializationInfo *FTSInfo |
4302 | 0 | = TemplateOrSpecialization.dyn_cast< |
4303 | 0 | FunctionTemplateSpecializationInfo*>()) |
4304 | 0 | return FTSInfo->getPointOfInstantiation(); |
4305 | 0 | if (MemberSpecializationInfo *MSInfo = |
4306 | 0 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
4307 | 0 | return MSInfo->getPointOfInstantiation(); |
4308 | | |
4309 | 0 | return SourceLocation(); |
4310 | 0 | } |
4311 | | |
4312 | 1 | bool FunctionDecl::isOutOfLine() const { |
4313 | 1 | if (Decl::isOutOfLine()) |
4314 | 0 | return true; |
4315 | | |
4316 | | // If this function was instantiated from a member function of a |
4317 | | // class template, check whether that member function was defined out-of-line. |
4318 | 1 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
4319 | 0 | const FunctionDecl *Definition; |
4320 | 0 | if (FD->hasBody(Definition)) |
4321 | 0 | return Definition->isOutOfLine(); |
4322 | 0 | } |
4323 | | |
4324 | | // If this function was instantiated from a function template, |
4325 | | // check whether that function template was defined out-of-line. |
4326 | 1 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
4327 | 0 | const FunctionDecl *Definition; |
4328 | 0 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
4329 | 0 | return Definition->isOutOfLine(); |
4330 | 0 | } |
4331 | | |
4332 | 1 | return false; |
4333 | 1 | } |
4334 | | |
4335 | 0 | SourceRange FunctionDecl::getSourceRange() const { |
4336 | 0 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
4337 | 0 | } |
4338 | | |
4339 | 0 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
4340 | 0 | IdentifierInfo *FnInfo = getIdentifier(); |
4341 | |
|
4342 | 0 | if (!FnInfo) |
4343 | 0 | return 0; |
4344 | | |
4345 | | // Builtin handling. |
4346 | 0 | switch (getBuiltinID()) { |
4347 | 0 | case Builtin::BI__builtin_memset: |
4348 | 0 | case Builtin::BI__builtin___memset_chk: |
4349 | 0 | case Builtin::BImemset: |
4350 | 0 | return Builtin::BImemset; |
4351 | | |
4352 | 0 | case Builtin::BI__builtin_memcpy: |
4353 | 0 | case Builtin::BI__builtin___memcpy_chk: |
4354 | 0 | case Builtin::BImemcpy: |
4355 | 0 | return Builtin::BImemcpy; |
4356 | | |
4357 | 0 | case Builtin::BI__builtin_mempcpy: |
4358 | 0 | case Builtin::BI__builtin___mempcpy_chk: |
4359 | 0 | case Builtin::BImempcpy: |
4360 | 0 | return Builtin::BImempcpy; |
4361 | | |
4362 | 0 | case Builtin::BI__builtin_memmove: |
4363 | 0 | case Builtin::BI__builtin___memmove_chk: |
4364 | 0 | case Builtin::BImemmove: |
4365 | 0 | return Builtin::BImemmove; |
4366 | | |
4367 | 0 | case Builtin::BIstrlcpy: |
4368 | 0 | case Builtin::BI__builtin___strlcpy_chk: |
4369 | 0 | return Builtin::BIstrlcpy; |
4370 | | |
4371 | 0 | case Builtin::BIstrlcat: |
4372 | 0 | case Builtin::BI__builtin___strlcat_chk: |
4373 | 0 | return Builtin::BIstrlcat; |
4374 | | |
4375 | 0 | case Builtin::BI__builtin_memcmp: |
4376 | 0 | case Builtin::BImemcmp: |
4377 | 0 | return Builtin::BImemcmp; |
4378 | | |
4379 | 0 | case Builtin::BI__builtin_bcmp: |
4380 | 0 | case Builtin::BIbcmp: |
4381 | 0 | return Builtin::BIbcmp; |
4382 | | |
4383 | 0 | case Builtin::BI__builtin_strncpy: |
4384 | 0 | case Builtin::BI__builtin___strncpy_chk: |
4385 | 0 | case Builtin::BIstrncpy: |
4386 | 0 | return Builtin::BIstrncpy; |
4387 | | |
4388 | 0 | case Builtin::BI__builtin_strncmp: |
4389 | 0 | case Builtin::BIstrncmp: |
4390 | 0 | return Builtin::BIstrncmp; |
4391 | | |
4392 | 0 | case Builtin::BI__builtin_strncasecmp: |
4393 | 0 | case Builtin::BIstrncasecmp: |
4394 | 0 | return Builtin::BIstrncasecmp; |
4395 | | |
4396 | 0 | case Builtin::BI__builtin_strncat: |
4397 | 0 | case Builtin::BI__builtin___strncat_chk: |
4398 | 0 | case Builtin::BIstrncat: |
4399 | 0 | return Builtin::BIstrncat; |
4400 | | |
4401 | 0 | case Builtin::BI__builtin_strndup: |
4402 | 0 | case Builtin::BIstrndup: |
4403 | 0 | return Builtin::BIstrndup; |
4404 | | |
4405 | 0 | case Builtin::BI__builtin_strlen: |
4406 | 0 | case Builtin::BIstrlen: |
4407 | 0 | return Builtin::BIstrlen; |
4408 | | |
4409 | 0 | case Builtin::BI__builtin_bzero: |
4410 | 0 | case Builtin::BIbzero: |
4411 | 0 | return Builtin::BIbzero; |
4412 | | |
4413 | 0 | case Builtin::BI__builtin_bcopy: |
4414 | 0 | case Builtin::BIbcopy: |
4415 | 0 | return Builtin::BIbcopy; |
4416 | | |
4417 | 0 | case Builtin::BIfree: |
4418 | 0 | return Builtin::BIfree; |
4419 | | |
4420 | 0 | default: |
4421 | 0 | if (isExternC()) { |
4422 | 0 | if (FnInfo->isStr("memset")) |
4423 | 0 | return Builtin::BImemset; |
4424 | 0 | if (FnInfo->isStr("memcpy")) |
4425 | 0 | return Builtin::BImemcpy; |
4426 | 0 | if (FnInfo->isStr("mempcpy")) |
4427 | 0 | return Builtin::BImempcpy; |
4428 | 0 | if (FnInfo->isStr("memmove")) |
4429 | 0 | return Builtin::BImemmove; |
4430 | 0 | if (FnInfo->isStr("memcmp")) |
4431 | 0 | return Builtin::BImemcmp; |
4432 | 0 | if (FnInfo->isStr("bcmp")) |
4433 | 0 | return Builtin::BIbcmp; |
4434 | 0 | if (FnInfo->isStr("strncpy")) |
4435 | 0 | return Builtin::BIstrncpy; |
4436 | 0 | if (FnInfo->isStr("strncmp")) |
4437 | 0 | return Builtin::BIstrncmp; |
4438 | 0 | if (FnInfo->isStr("strncasecmp")) |
4439 | 0 | return Builtin::BIstrncasecmp; |
4440 | 0 | if (FnInfo->isStr("strncat")) |
4441 | 0 | return Builtin::BIstrncat; |
4442 | 0 | if (FnInfo->isStr("strndup")) |
4443 | 0 | return Builtin::BIstrndup; |
4444 | 0 | if (FnInfo->isStr("strlen")) |
4445 | 0 | return Builtin::BIstrlen; |
4446 | 0 | if (FnInfo->isStr("bzero")) |
4447 | 0 | return Builtin::BIbzero; |
4448 | 0 | if (FnInfo->isStr("bcopy")) |
4449 | 0 | return Builtin::BIbcopy; |
4450 | 0 | } else if (isInStdNamespace()) { |
4451 | 0 | if (FnInfo->isStr("free")) |
4452 | 0 | return Builtin::BIfree; |
4453 | 0 | } |
4454 | 0 | break; |
4455 | 0 | } |
4456 | 0 | return 0; |
4457 | 0 | } |
4458 | | |
4459 | 0 | unsigned FunctionDecl::getODRHash() const { |
4460 | 0 | assert(hasODRHash()); |
4461 | 0 | return ODRHash; |
4462 | 0 | } |
4463 | | |
4464 | 0 | unsigned FunctionDecl::getODRHash() { |
4465 | 0 | if (hasODRHash()) |
4466 | 0 | return ODRHash; |
4467 | | |
4468 | 0 | if (auto *FT = getInstantiatedFromMemberFunction()) { |
4469 | 0 | setHasODRHash(true); |
4470 | 0 | ODRHash = FT->getODRHash(); |
4471 | 0 | return ODRHash; |
4472 | 0 | } |
4473 | | |
4474 | 0 | class ODRHash Hash; |
4475 | 0 | Hash.AddFunctionDecl(this); |
4476 | 0 | setHasODRHash(true); |
4477 | 0 | ODRHash = Hash.CalculateHash(); |
4478 | 0 | return ODRHash; |
4479 | 0 | } |
4480 | | |
4481 | | //===----------------------------------------------------------------------===// |
4482 | | // FieldDecl Implementation |
4483 | | //===----------------------------------------------------------------------===// |
4484 | | |
4485 | | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
4486 | | SourceLocation StartLoc, SourceLocation IdLoc, |
4487 | | IdentifierInfo *Id, QualType T, |
4488 | | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
4489 | 460 | InClassInitStyle InitStyle) { |
4490 | 460 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
4491 | 460 | BW, Mutable, InitStyle); |
4492 | 460 | } |
4493 | | |
4494 | 0 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4495 | 0 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
4496 | 0 | SourceLocation(), nullptr, QualType(), nullptr, |
4497 | 0 | nullptr, false, ICIS_NoInit); |
4498 | 0 | } |
4499 | | |
4500 | 184 | bool FieldDecl::isAnonymousStructOrUnion() const { |
4501 | 184 | if (!isImplicit() || getDeclName()) |
4502 | 184 | return false; |
4503 | | |
4504 | 0 | if (const auto *Record = getType()->getAs<RecordType>()) |
4505 | 0 | return Record->getDecl()->isAnonymousStructOrUnion(); |
4506 | | |
4507 | 0 | return false; |
4508 | 0 | } |
4509 | | |
4510 | 0 | Expr *FieldDecl::getInClassInitializer() const { |
4511 | 0 | if (!hasInClassInitializer()) |
4512 | 0 | return nullptr; |
4513 | | |
4514 | 0 | LazyDeclStmtPtr InitPtr = BitField ? InitAndBitWidth->Init : Init; |
4515 | 0 | return cast_if_present<Expr>( |
4516 | 0 | InitPtr.isOffset() ? InitPtr.get(getASTContext().getExternalSource()) |
4517 | 0 | : InitPtr.get(nullptr)); |
4518 | 0 | } |
4519 | | |
4520 | 0 | void FieldDecl::setInClassInitializer(Expr *NewInit) { |
4521 | 0 | setLazyInClassInitializer(LazyDeclStmtPtr(NewInit)); |
4522 | 0 | } |
4523 | | |
4524 | 0 | void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) { |
4525 | 0 | assert(hasInClassInitializer() && !getInClassInitializer()); |
4526 | 0 | if (BitField) |
4527 | 0 | InitAndBitWidth->Init = NewInit; |
4528 | 0 | else |
4529 | 0 | Init = NewInit; |
4530 | 0 | } |
4531 | | |
4532 | 0 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
4533 | 0 | assert(isBitField() && "not a bitfield"); |
4534 | 0 | return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); |
4535 | 0 | } |
4536 | | |
4537 | 368 | bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { |
4538 | 368 | return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && |
4539 | 368 | getBitWidthValue(Ctx) == 0; |
4540 | 368 | } |
4541 | | |
4542 | 368 | bool FieldDecl::isZeroSize(const ASTContext &Ctx) const { |
4543 | 368 | if (isZeroLengthBitField(Ctx)) |
4544 | 0 | return true; |
4545 | | |
4546 | | // C++2a [intro.object]p7: |
4547 | | // An object has nonzero size if it |
4548 | | // -- is not a potentially-overlapping subobject, or |
4549 | 368 | if (!hasAttr<NoUniqueAddressAttr>()) |
4550 | 368 | return false; |
4551 | | |
4552 | | // -- is not of class type, or |
4553 | 0 | const auto *RT = getType()->getAs<RecordType>(); |
4554 | 0 | if (!RT) |
4555 | 0 | return false; |
4556 | 0 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
4557 | 0 | if (!RD) { |
4558 | 0 | assert(isInvalidDecl() && "valid field has incomplete type"); |
4559 | 0 | return false; |
4560 | 0 | } |
4561 | | |
4562 | | // -- [has] virtual member functions or virtual base classes, or |
4563 | | // -- has subobjects of nonzero size or bit-fields of nonzero length |
4564 | 0 | const auto *CXXRD = cast<CXXRecordDecl>(RD); |
4565 | 0 | if (!CXXRD->isEmpty()) |
4566 | 0 | return false; |
4567 | | |
4568 | | // Otherwise, [...] the circumstances under which the object has zero size |
4569 | | // are implementation-defined. |
4570 | 0 | if (!Ctx.getTargetInfo().getCXXABI().isMicrosoft()) |
4571 | 0 | return true; |
4572 | | |
4573 | | // MS ABI: has nonzero size if it is a class type with class type fields, |
4574 | | // whether or not they have nonzero size |
4575 | 0 | return !llvm::any_of(CXXRD->fields(), [](const FieldDecl *Field) { |
4576 | 0 | return Field->getType()->getAs<RecordType>(); |
4577 | 0 | }); |
4578 | 0 | } |
4579 | | |
4580 | 276 | bool FieldDecl::isPotentiallyOverlapping() const { |
4581 | 276 | return hasAttr<NoUniqueAddressAttr>() && getType()->getAsCXXRecordDecl(); |
4582 | 276 | } |
4583 | | |
4584 | 92 | unsigned FieldDecl::getFieldIndex() const { |
4585 | 92 | const FieldDecl *Canonical = getCanonicalDecl(); |
4586 | 92 | if (Canonical != this) |
4587 | 0 | return Canonical->getFieldIndex(); |
4588 | | |
4589 | 92 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
4590 | | |
4591 | 46 | unsigned Index = 0; |
4592 | 46 | const RecordDecl *RD = getParent()->getDefinition(); |
4593 | 46 | assert(RD && "requested index for field of struct with no definition"); |
4594 | | |
4595 | 92 | for (auto *Field : RD->fields()) { |
4596 | 92 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
4597 | 92 | assert(Field->getCanonicalDecl()->CachedFieldIndex == Index + 1 && |
4598 | 92 | "overflow in field numbering"); |
4599 | 0 | ++Index; |
4600 | 92 | } |
4601 | | |
4602 | 46 | assert(CachedFieldIndex && "failed to find field in parent"); |
4603 | 0 | return CachedFieldIndex - 1; |
4604 | 92 | } |
4605 | | |
4606 | 0 | SourceRange FieldDecl::getSourceRange() const { |
4607 | 0 | const Expr *FinalExpr = getInClassInitializer(); |
4608 | 0 | if (!FinalExpr) |
4609 | 0 | FinalExpr = getBitWidth(); |
4610 | 0 | if (FinalExpr) |
4611 | 0 | return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); |
4612 | 0 | return DeclaratorDecl::getSourceRange(); |
4613 | 0 | } |
4614 | | |
4615 | 0 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
4616 | 0 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
4617 | 0 | "capturing type in non-lambda or captured record."); |
4618 | 0 | assert(StorageKind == ISK_NoInit && !BitField && |
4619 | 0 | "bit-field or field with default member initializer cannot capture " |
4620 | 0 | "VLA type"); |
4621 | 0 | StorageKind = ISK_CapturedVLAType; |
4622 | 0 | CapturedVLAType = VLAType; |
4623 | 0 | } |
4624 | | |
4625 | 0 | void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { |
4626 | | // Print unnamed members using name of their type. |
4627 | 0 | if (isAnonymousStructOrUnion()) { |
4628 | 0 | this->getType().print(OS, Policy); |
4629 | 0 | return; |
4630 | 0 | } |
4631 | | // Otherwise, do the normal printing. |
4632 | 0 | DeclaratorDecl::printName(OS, Policy); |
4633 | 0 | } |
4634 | | |
4635 | | //===----------------------------------------------------------------------===// |
4636 | | // TagDecl Implementation |
4637 | | //===----------------------------------------------------------------------===// |
4638 | | |
4639 | | TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
4640 | | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
4641 | | SourceLocation StartL) |
4642 | | : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), |
4643 | 138 | TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { |
4644 | 138 | assert((DK != Enum || TK == TagTypeKind::Enum) && |
4645 | 138 | "EnumDecl not matched with TagTypeKind::Enum"); |
4646 | 0 | setPreviousDecl(PrevDecl); |
4647 | 138 | setTagKind(TK); |
4648 | 138 | setCompleteDefinition(false); |
4649 | 138 | setBeingDefined(false); |
4650 | 138 | setEmbeddedInDeclarator(false); |
4651 | 138 | setFreeStanding(false); |
4652 | 138 | setCompleteDefinitionRequired(false); |
4653 | 138 | TagDeclBits.IsThisDeclarationADemotedDefinition = false; |
4654 | 138 | } |
4655 | | |
4656 | 0 | SourceLocation TagDecl::getOuterLocStart() const { |
4657 | 0 | return getTemplateOrInnerLocStart(this); |
4658 | 0 | } |
4659 | | |
4660 | 0 | SourceRange TagDecl::getSourceRange() const { |
4661 | 0 | SourceLocation RBraceLoc = BraceRange.getEnd(); |
4662 | 0 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
4663 | 0 | return SourceRange(getOuterLocStart(), E); |
4664 | 0 | } |
4665 | | |
4666 | 1.53k | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
4667 | | |
4668 | 0 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
4669 | 0 | TypedefNameDeclOrQualifier = TDD; |
4670 | 0 | if (const Type *T = getTypeForDecl()) { |
4671 | 0 | (void)T; |
4672 | 0 | assert(T->isLinkageValid()); |
4673 | 0 | } |
4674 | 0 | assert(isLinkageValid()); |
4675 | 0 | } |
4676 | | |
4677 | 92 | void TagDecl::startDefinition() { |
4678 | 92 | setBeingDefined(true); |
4679 | | |
4680 | 92 | if (auto *D = dyn_cast<CXXRecordDecl>(this)) { |
4681 | 46 | struct CXXRecordDecl::DefinitionData *Data = |
4682 | 46 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
4683 | 46 | for (auto *I : redecls()) |
4684 | 46 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
4685 | 46 | } |
4686 | 92 | } |
4687 | | |
4688 | 138 | void TagDecl::completeDefinition() { |
4689 | 138 | assert((!isa<CXXRecordDecl>(this) || |
4690 | 138 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
4691 | 138 | "definition completed but not started"); |
4692 | | |
4693 | 0 | setCompleteDefinition(true); |
4694 | 138 | setBeingDefined(false); |
4695 | | |
4696 | 138 | if (ASTMutationListener *L = getASTMutationListener()) |
4697 | 0 | L->CompletedTagDefinition(this); |
4698 | 138 | } |
4699 | | |
4700 | 6.00k | TagDecl *TagDecl::getDefinition() const { |
4701 | 6.00k | if (isCompleteDefinition()) |
4702 | 5.08k | return const_cast<TagDecl *>(this); |
4703 | | |
4704 | | // If it's possible for us to have an out-of-date definition, check now. |
4705 | 920 | if (mayHaveOutOfDateDef()) { |
4706 | 0 | if (IdentifierInfo *II = getIdentifier()) { |
4707 | 0 | if (II->isOutOfDate()) { |
4708 | 0 | updateOutOfDate(*II); |
4709 | 0 | } |
4710 | 0 | } |
4711 | 0 | } |
4712 | | |
4713 | 920 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
4714 | 368 | return CXXRD->getDefinition(); |
4715 | | |
4716 | 552 | for (auto *R : redecls()) |
4717 | 552 | if (R->isCompleteDefinition()) |
4718 | 0 | return R; |
4719 | | |
4720 | 552 | return nullptr; |
4721 | 552 | } |
4722 | | |
4723 | 0 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
4724 | 0 | if (QualifierLoc) { |
4725 | | // Make sure the extended qualifier info is allocated. |
4726 | 0 | if (!hasExtInfo()) |
4727 | 0 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
4728 | | // Set qualifier info. |
4729 | 0 | getExtInfo()->QualifierLoc = QualifierLoc; |
4730 | 0 | } else { |
4731 | | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
4732 | 0 | if (hasExtInfo()) { |
4733 | 0 | if (getExtInfo()->NumTemplParamLists == 0) { |
4734 | 0 | getASTContext().Deallocate(getExtInfo()); |
4735 | 0 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
4736 | 0 | } |
4737 | 0 | else |
4738 | 0 | getExtInfo()->QualifierLoc = QualifierLoc; |
4739 | 0 | } |
4740 | 0 | } |
4741 | 0 | } |
4742 | | |
4743 | 46 | void TagDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { |
4744 | 46 | DeclarationName Name = getDeclName(); |
4745 | | // If the name is supposed to have an identifier but does not have one, then |
4746 | | // the tag is anonymous and we should print it differently. |
4747 | 46 | if (Name.isIdentifier() && !Name.getAsIdentifierInfo()) { |
4748 | | // If the caller wanted to print a qualified name, they've already printed |
4749 | | // the scope. And if the caller doesn't want that, the scope information |
4750 | | // is already printed as part of the type. |
4751 | 0 | PrintingPolicy Copy(Policy); |
4752 | 0 | Copy.SuppressScope = true; |
4753 | 0 | getASTContext().getTagDeclType(this).print(OS, Copy); |
4754 | 0 | return; |
4755 | 0 | } |
4756 | | // Otherwise, do the normal printing. |
4757 | 46 | Name.print(OS, Policy); |
4758 | 46 | } |
4759 | | |
4760 | | void TagDecl::setTemplateParameterListsInfo( |
4761 | 0 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
4762 | 0 | assert(!TPLists.empty()); |
4763 | | // Make sure the extended decl info is allocated. |
4764 | 0 | if (!hasExtInfo()) |
4765 | | // Allocate external info struct. |
4766 | 0 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
4767 | | // Set the template parameter lists info. |
4768 | 0 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
4769 | 0 | } |
4770 | | |
4771 | | //===----------------------------------------------------------------------===// |
4772 | | // EnumDecl Implementation |
4773 | | //===----------------------------------------------------------------------===// |
4774 | | |
4775 | | EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
4776 | | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
4777 | | bool Scoped, bool ScopedUsingClassTag, bool Fixed) |
4778 | 0 | : TagDecl(Enum, TagTypeKind::Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
4779 | 0 | assert(Scoped || !ScopedUsingClassTag); |
4780 | 0 | IntegerType = nullptr; |
4781 | 0 | setNumPositiveBits(0); |
4782 | 0 | setNumNegativeBits(0); |
4783 | 0 | setScoped(Scoped); |
4784 | 0 | setScopedUsingClassTag(ScopedUsingClassTag); |
4785 | 0 | setFixed(Fixed); |
4786 | 0 | setHasODRHash(false); |
4787 | 0 | ODRHash = 0; |
4788 | 0 | } |
4789 | | |
4790 | 0 | void EnumDecl::anchor() {} |
4791 | | |
4792 | | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
4793 | | SourceLocation StartLoc, SourceLocation IdLoc, |
4794 | | IdentifierInfo *Id, |
4795 | | EnumDecl *PrevDecl, bool IsScoped, |
4796 | 0 | bool IsScopedUsingClassTag, bool IsFixed) { |
4797 | 0 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
4798 | 0 | IsScoped, IsScopedUsingClassTag, IsFixed); |
4799 | 0 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4800 | 0 | C.getTypeDeclType(Enum, PrevDecl); |
4801 | 0 | return Enum; |
4802 | 0 | } |
4803 | | |
4804 | 0 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
4805 | 0 | EnumDecl *Enum = |
4806 | 0 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
4807 | 0 | nullptr, nullptr, false, false, false); |
4808 | 0 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4809 | 0 | return Enum; |
4810 | 0 | } |
4811 | | |
4812 | 0 | SourceRange EnumDecl::getIntegerTypeRange() const { |
4813 | 0 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
4814 | 0 | return TI->getTypeLoc().getSourceRange(); |
4815 | 0 | return SourceRange(); |
4816 | 0 | } |
4817 | | |
4818 | | void EnumDecl::completeDefinition(QualType NewType, |
4819 | | QualType NewPromotionType, |
4820 | | unsigned NumPositiveBits, |
4821 | 0 | unsigned NumNegativeBits) { |
4822 | 0 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
4823 | 0 | if (!IntegerType) |
4824 | 0 | IntegerType = NewType.getTypePtr(); |
4825 | 0 | PromotionType = NewPromotionType; |
4826 | 0 | setNumPositiveBits(NumPositiveBits); |
4827 | 0 | setNumNegativeBits(NumNegativeBits); |
4828 | 0 | TagDecl::completeDefinition(); |
4829 | 0 | } |
4830 | | |
4831 | 0 | bool EnumDecl::isClosed() const { |
4832 | 0 | if (const auto *A = getAttr<EnumExtensibilityAttr>()) |
4833 | 0 | return A->getExtensibility() == EnumExtensibilityAttr::Closed; |
4834 | 0 | return true; |
4835 | 0 | } |
4836 | | |
4837 | 0 | bool EnumDecl::isClosedFlag() const { |
4838 | 0 | return isClosed() && hasAttr<FlagEnumAttr>(); |
4839 | 0 | } |
4840 | | |
4841 | 0 | bool EnumDecl::isClosedNonFlag() const { |
4842 | 0 | return isClosed() && !hasAttr<FlagEnumAttr>(); |
4843 | 0 | } |
4844 | | |
4845 | 0 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
4846 | 0 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
4847 | 0 | return MSI->getTemplateSpecializationKind(); |
4848 | | |
4849 | 0 | return TSK_Undeclared; |
4850 | 0 | } |
4851 | | |
4852 | | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
4853 | 0 | SourceLocation PointOfInstantiation) { |
4854 | 0 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
4855 | 0 | assert(MSI && "Not an instantiated member enumeration?"); |
4856 | 0 | MSI->setTemplateSpecializationKind(TSK); |
4857 | 0 | if (TSK != TSK_ExplicitSpecialization && |
4858 | 0 | PointOfInstantiation.isValid() && |
4859 | 0 | MSI->getPointOfInstantiation().isInvalid()) |
4860 | 0 | MSI->setPointOfInstantiation(PointOfInstantiation); |
4861 | 0 | } |
4862 | | |
4863 | 0 | EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { |
4864 | 0 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
4865 | 0 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
4866 | 0 | EnumDecl *ED = getInstantiatedFromMemberEnum(); |
4867 | 0 | while (auto *NewED = ED->getInstantiatedFromMemberEnum()) |
4868 | 0 | ED = NewED; |
4869 | 0 | return getDefinitionOrSelf(ED); |
4870 | 0 | } |
4871 | 0 | } |
4872 | | |
4873 | 0 | assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && |
4874 | 0 | "couldn't find pattern for enum instantiation"); |
4875 | 0 | return nullptr; |
4876 | 0 | } |
4877 | | |
4878 | 0 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
4879 | 0 | if (SpecializationInfo) |
4880 | 0 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
4881 | | |
4882 | 0 | return nullptr; |
4883 | 0 | } |
4884 | | |
4885 | | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
4886 | 0 | TemplateSpecializationKind TSK) { |
4887 | 0 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
4888 | 0 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
4889 | 0 | } |
4890 | | |
4891 | 0 | unsigned EnumDecl::getODRHash() { |
4892 | 0 | if (hasODRHash()) |
4893 | 0 | return ODRHash; |
4894 | | |
4895 | 0 | class ODRHash Hash; |
4896 | 0 | Hash.AddEnumDecl(this); |
4897 | 0 | setHasODRHash(true); |
4898 | 0 | ODRHash = Hash.CalculateHash(); |
4899 | 0 | return ODRHash; |
4900 | 0 | } |
4901 | | |
4902 | 0 | SourceRange EnumDecl::getSourceRange() const { |
4903 | 0 | auto Res = TagDecl::getSourceRange(); |
4904 | | // Set end-point to enum-base, e.g. enum foo : ^bar |
4905 | 0 | if (auto *TSI = getIntegerTypeSourceInfo()) { |
4906 | | // TagDecl doesn't know about the enum base. |
4907 | 0 | if (!getBraceRange().getEnd().isValid()) |
4908 | 0 | Res.setEnd(TSI->getTypeLoc().getEndLoc()); |
4909 | 0 | } |
4910 | 0 | return Res; |
4911 | 0 | } |
4912 | | |
4913 | 0 | void EnumDecl::getValueRange(llvm::APInt &Max, llvm::APInt &Min) const { |
4914 | 0 | unsigned Bitwidth = getASTContext().getIntWidth(getIntegerType()); |
4915 | 0 | unsigned NumNegativeBits = getNumNegativeBits(); |
4916 | 0 | unsigned NumPositiveBits = getNumPositiveBits(); |
4917 | |
|
4918 | 0 | if (NumNegativeBits) { |
4919 | 0 | unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); |
4920 | 0 | Max = llvm::APInt(Bitwidth, 1) << (NumBits - 1); |
4921 | 0 | Min = -Max; |
4922 | 0 | } else { |
4923 | 0 | Max = llvm::APInt(Bitwidth, 1) << NumPositiveBits; |
4924 | 0 | Min = llvm::APInt::getZero(Bitwidth); |
4925 | 0 | } |
4926 | 0 | } |
4927 | | |
4928 | | //===----------------------------------------------------------------------===// |
4929 | | // RecordDecl Implementation |
4930 | | //===----------------------------------------------------------------------===// |
4931 | | |
4932 | | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
4933 | | DeclContext *DC, SourceLocation StartLoc, |
4934 | | SourceLocation IdLoc, IdentifierInfo *Id, |
4935 | | RecordDecl *PrevDecl) |
4936 | 138 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
4937 | 138 | assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); |
4938 | 0 | setHasFlexibleArrayMember(false); |
4939 | 138 | setAnonymousStructOrUnion(false); |
4940 | 138 | setHasObjectMember(false); |
4941 | 138 | setHasVolatileMember(false); |
4942 | 138 | setHasLoadedFieldsFromExternalStorage(false); |
4943 | 138 | setNonTrivialToPrimitiveDefaultInitialize(false); |
4944 | 138 | setNonTrivialToPrimitiveCopy(false); |
4945 | 138 | setNonTrivialToPrimitiveDestroy(false); |
4946 | 138 | setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); |
4947 | 138 | setHasNonTrivialToPrimitiveDestructCUnion(false); |
4948 | 138 | setHasNonTrivialToPrimitiveCopyCUnion(false); |
4949 | 138 | setParamDestroyedInCallee(false); |
4950 | 138 | setArgPassingRestrictions(RecordArgPassingKind::CanPassInRegs); |
4951 | 138 | setIsRandomized(false); |
4952 | 138 | setODRHash(0); |
4953 | 138 | } |
4954 | | |
4955 | | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
4956 | | SourceLocation StartLoc, SourceLocation IdLoc, |
4957 | 92 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
4958 | 92 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
4959 | 92 | StartLoc, IdLoc, Id, PrevDecl); |
4960 | 92 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4961 | | |
4962 | 92 | C.getTypeDeclType(R, PrevDecl); |
4963 | 92 | return R; |
4964 | 92 | } |
4965 | | |
4966 | 0 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
4967 | 0 | RecordDecl *R = new (C, ID) |
4968 | 0 | RecordDecl(Record, TagTypeKind::Struct, C, nullptr, SourceLocation(), |
4969 | 0 | SourceLocation(), nullptr, nullptr); |
4970 | 0 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
4971 | 0 | return R; |
4972 | 0 | } |
4973 | | |
4974 | 0 | bool RecordDecl::isInjectedClassName() const { |
4975 | 0 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
4976 | 0 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
4977 | 0 | } |
4978 | | |
4979 | 0 | bool RecordDecl::isLambda() const { |
4980 | 0 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
4981 | 0 | return RD->isLambda(); |
4982 | 0 | return false; |
4983 | 0 | } |
4984 | | |
4985 | 0 | bool RecordDecl::isCapturedRecord() const { |
4986 | 0 | return hasAttr<CapturedRecordAttr>(); |
4987 | 0 | } |
4988 | | |
4989 | 0 | void RecordDecl::setCapturedRecord() { |
4990 | 0 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
4991 | 0 | } |
4992 | | |
4993 | 0 | bool RecordDecl::isOrContainsUnion() const { |
4994 | 0 | if (isUnion()) |
4995 | 0 | return true; |
4996 | | |
4997 | 0 | if (const RecordDecl *Def = getDefinition()) { |
4998 | 0 | for (const FieldDecl *FD : Def->fields()) { |
4999 | 0 | const RecordType *RT = FD->getType()->getAs<RecordType>(); |
5000 | 0 | if (RT && RT->getDecl()->isOrContainsUnion()) |
5001 | 0 | return true; |
5002 | 0 | } |
5003 | 0 | } |
5004 | | |
5005 | 0 | return false; |
5006 | 0 | } |
5007 | | |
5008 | 184 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
5009 | 184 | if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) |
5010 | 0 | LoadFieldsFromExternalStorage(); |
5011 | | // This is necessary for correctness for C++ with modules. |
5012 | | // FIXME: Come up with a test case that breaks without definition. |
5013 | 184 | if (RecordDecl *D = getDefinition(); D && D != this) |
5014 | 0 | return D->field_begin(); |
5015 | 184 | return field_iterator(decl_iterator(FirstDecl)); |
5016 | 184 | } |
5017 | | |
5018 | | /// completeDefinition - Notes that the definition of this type is now |
5019 | | /// complete. |
5020 | 138 | void RecordDecl::completeDefinition() { |
5021 | 138 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
5022 | 0 | TagDecl::completeDefinition(); |
5023 | | |
5024 | 138 | ASTContext &Ctx = getASTContext(); |
5025 | | |
5026 | | // Layouts are dumped when computed, so if we are dumping for all complete |
5027 | | // types, we need to force usage to get types that wouldn't be used elsewhere. |
5028 | 138 | if (Ctx.getLangOpts().DumpRecordLayoutsComplete) |
5029 | 0 | (void)Ctx.getASTRecordLayout(this); |
5030 | 138 | } |
5031 | | |
5032 | | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
5033 | | /// This which can be turned on with an attribute, pragma, or the |
5034 | | /// -mms-bitfields command-line option. |
5035 | 46 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
5036 | 46 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
5037 | 46 | } |
5038 | | |
5039 | 0 | void RecordDecl::reorderDecls(const SmallVectorImpl<Decl *> &Decls) { |
5040 | 0 | std::tie(FirstDecl, LastDecl) = DeclContext::BuildDeclChain(Decls, false); |
5041 | 0 | LastDecl->NextInContextAndBits.setPointer(nullptr); |
5042 | 0 | setIsRandomized(true); |
5043 | 0 | } |
5044 | | |
5045 | 0 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
5046 | 0 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
5047 | 0 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
5048 | | |
5049 | | // Notify that we have a RecordDecl doing some initialization. |
5050 | 0 | ExternalASTSource::Deserializing TheFields(Source); |
5051 | |
|
5052 | 0 | SmallVector<Decl*, 64> Decls; |
5053 | 0 | setHasLoadedFieldsFromExternalStorage(true); |
5054 | 0 | Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { |
5055 | 0 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
5056 | 0 | }, Decls); |
5057 | |
|
5058 | 0 | #ifndef NDEBUG |
5059 | | // Check that all decls we got were FieldDecls. |
5060 | 0 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
5061 | 0 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
5062 | 0 | #endif |
5063 | |
|
5064 | 0 | if (Decls.empty()) |
5065 | 0 | return; |
5066 | | |
5067 | 0 | auto [ExternalFirst, ExternalLast] = |
5068 | 0 | BuildDeclChain(Decls, |
5069 | 0 | /*FieldsAlreadyLoaded=*/false); |
5070 | 0 | ExternalLast->NextInContextAndBits.setPointer(FirstDecl); |
5071 | 0 | FirstDecl = ExternalFirst; |
5072 | 0 | if (!LastDecl) |
5073 | 0 | LastDecl = ExternalLast; |
5074 | 0 | } |
5075 | | |
5076 | 46 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
5077 | 46 | ASTContext &Context = getASTContext(); |
5078 | 46 | const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & |
5079 | 46 | (SanitizerKind::Address | SanitizerKind::KernelAddress); |
5080 | 46 | if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) |
5081 | 46 | return false; |
5082 | 0 | const auto &NoSanitizeList = Context.getNoSanitizeList(); |
5083 | 0 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); |
5084 | | // We may be able to relax some of these requirements. |
5085 | 0 | int ReasonToReject = -1; |
5086 | 0 | if (!CXXRD || CXXRD->isExternCContext()) |
5087 | 0 | ReasonToReject = 0; // is not C++. |
5088 | 0 | else if (CXXRD->hasAttr<PackedAttr>()) |
5089 | 0 | ReasonToReject = 1; // is packed. |
5090 | 0 | else if (CXXRD->isUnion()) |
5091 | 0 | ReasonToReject = 2; // is a union. |
5092 | 0 | else if (CXXRD->isTriviallyCopyable()) |
5093 | 0 | ReasonToReject = 3; // is trivially copyable. |
5094 | 0 | else if (CXXRD->hasTrivialDestructor()) |
5095 | 0 | ReasonToReject = 4; // has trivial destructor. |
5096 | 0 | else if (CXXRD->isStandardLayout()) |
5097 | 0 | ReasonToReject = 5; // is standard layout. |
5098 | 0 | else if (NoSanitizeList.containsLocation(EnabledAsanMask, getLocation(), |
5099 | 0 | "field-padding")) |
5100 | 0 | ReasonToReject = 6; // is in an excluded file. |
5101 | 0 | else if (NoSanitizeList.containsType( |
5102 | 0 | EnabledAsanMask, getQualifiedNameAsString(), "field-padding")) |
5103 | 0 | ReasonToReject = 7; // The type is excluded. |
5104 | |
|
5105 | 0 | if (EmitRemark) { |
5106 | 0 | if (ReasonToReject >= 0) |
5107 | 0 | Context.getDiagnostics().Report( |
5108 | 0 | getLocation(), |
5109 | 0 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
5110 | 0 | << getQualifiedNameAsString() << ReasonToReject; |
5111 | 0 | else |
5112 | 0 | Context.getDiagnostics().Report( |
5113 | 0 | getLocation(), |
5114 | 0 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
5115 | 0 | << getQualifiedNameAsString(); |
5116 | 0 | } |
5117 | 0 | return ReasonToReject < 0; |
5118 | 46 | } |
5119 | | |
5120 | 0 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
5121 | 0 | for (const auto *I : fields()) { |
5122 | 0 | if (I->getIdentifier()) |
5123 | 0 | return I; |
5124 | | |
5125 | 0 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
5126 | 0 | if (const FieldDecl *NamedDataMember = |
5127 | 0 | RT->getDecl()->findFirstNamedDataMember()) |
5128 | 0 | return NamedDataMember; |
5129 | 0 | } |
5130 | | |
5131 | | // We didn't find a named data member. |
5132 | 0 | return nullptr; |
5133 | 0 | } |
5134 | | |
5135 | 0 | unsigned RecordDecl::getODRHash() { |
5136 | 0 | if (hasODRHash()) |
5137 | 0 | return RecordDeclBits.ODRHash; |
5138 | | |
5139 | | // Only calculate hash on first call of getODRHash per record. |
5140 | 0 | ODRHash Hash; |
5141 | 0 | Hash.AddRecordDecl(this); |
5142 | | // For RecordDecl the ODRHash is stored in the remaining 26 |
5143 | | // bit of RecordDeclBits, adjust the hash to accomodate. |
5144 | 0 | setODRHash(Hash.CalculateHash() >> 6); |
5145 | 0 | return RecordDeclBits.ODRHash; |
5146 | 0 | } |
5147 | | |
5148 | | //===----------------------------------------------------------------------===// |
5149 | | // BlockDecl Implementation |
5150 | | //===----------------------------------------------------------------------===// |
5151 | | |
5152 | | BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) |
5153 | 5 | : Decl(Block, DC, CaretLoc), DeclContext(Block) { |
5154 | 5 | setIsVariadic(false); |
5155 | 5 | setCapturesCXXThis(false); |
5156 | 5 | setBlockMissingReturnType(true); |
5157 | 5 | setIsConversionFromLambda(false); |
5158 | 5 | setDoesNotEscape(false); |
5159 | 5 | setCanAvoidCopyToHeap(false); |
5160 | 5 | } |
5161 | | |
5162 | 0 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
5163 | 0 | assert(!ParamInfo && "Already has param info!"); |
5164 | | |
5165 | | // Zero params -> null pointer. |
5166 | 0 | if (!NewParamInfo.empty()) { |
5167 | 0 | NumParams = NewParamInfo.size(); |
5168 | 0 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
5169 | 0 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
5170 | 0 | } |
5171 | 0 | } |
5172 | | |
5173 | | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
5174 | 1 | bool CapturesCXXThis) { |
5175 | 1 | this->setCapturesCXXThis(CapturesCXXThis); |
5176 | 1 | this->NumCaptures = Captures.size(); |
5177 | | |
5178 | 1 | if (Captures.empty()) { |
5179 | 1 | this->Captures = nullptr; |
5180 | 1 | return; |
5181 | 1 | } |
5182 | | |
5183 | 0 | this->Captures = Captures.copy(Context).data(); |
5184 | 0 | } |
5185 | | |
5186 | 0 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
5187 | 0 | for (const auto &I : captures()) |
5188 | | // Only auto vars can be captured, so no redeclaration worries. |
5189 | 0 | if (I.getVariable() == variable) |
5190 | 0 | return true; |
5191 | | |
5192 | 0 | return false; |
5193 | 0 | } |
5194 | | |
5195 | 0 | SourceRange BlockDecl::getSourceRange() const { |
5196 | 0 | return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); |
5197 | 0 | } |
5198 | | |
5199 | | //===----------------------------------------------------------------------===// |
5200 | | // Other Decl Allocation/Deallocation Method Implementations |
5201 | | //===----------------------------------------------------------------------===// |
5202 | | |
5203 | 0 | void TranslationUnitDecl::anchor() {} |
5204 | | |
5205 | 46 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
5206 | 46 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
5207 | 46 | } |
5208 | | |
5209 | 0 | void PragmaCommentDecl::anchor() {} |
5210 | | |
5211 | | PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, |
5212 | | TranslationUnitDecl *DC, |
5213 | | SourceLocation CommentLoc, |
5214 | | PragmaMSCommentKind CommentKind, |
5215 | 0 | StringRef Arg) { |
5216 | 0 | PragmaCommentDecl *PCD = |
5217 | 0 | new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) |
5218 | 0 | PragmaCommentDecl(DC, CommentLoc, CommentKind); |
5219 | 0 | memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); |
5220 | 0 | PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; |
5221 | 0 | return PCD; |
5222 | 0 | } |
5223 | | |
5224 | | PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, |
5225 | | unsigned ID, |
5226 | 0 | unsigned ArgSize) { |
5227 | 0 | return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) |
5228 | 0 | PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); |
5229 | 0 | } |
5230 | | |
5231 | 0 | void PragmaDetectMismatchDecl::anchor() {} |
5232 | | |
5233 | | PragmaDetectMismatchDecl * |
5234 | | PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, |
5235 | | SourceLocation Loc, StringRef Name, |
5236 | 0 | StringRef Value) { |
5237 | 0 | size_t ValueStart = Name.size() + 1; |
5238 | 0 | PragmaDetectMismatchDecl *PDMD = |
5239 | 0 | new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) |
5240 | 0 | PragmaDetectMismatchDecl(DC, Loc, ValueStart); |
5241 | 0 | memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); |
5242 | 0 | PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; |
5243 | 0 | memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), |
5244 | 0 | Value.size()); |
5245 | 0 | PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; |
5246 | 0 | return PDMD; |
5247 | 0 | } |
5248 | | |
5249 | | PragmaDetectMismatchDecl * |
5250 | | PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
5251 | 0 | unsigned NameValueSize) { |
5252 | 0 | return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) |
5253 | 0 | PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); |
5254 | 0 | } |
5255 | | |
5256 | 0 | void ExternCContextDecl::anchor() {} |
5257 | | |
5258 | | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
5259 | 18 | TranslationUnitDecl *DC) { |
5260 | 18 | return new (C, DC) ExternCContextDecl(DC); |
5261 | 18 | } |
5262 | | |
5263 | 0 | void LabelDecl::anchor() {} |
5264 | | |
5265 | | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
5266 | 0 | SourceLocation IdentL, IdentifierInfo *II) { |
5267 | 0 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
5268 | 0 | } |
5269 | | |
5270 | | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
5271 | | SourceLocation IdentL, IdentifierInfo *II, |
5272 | 0 | SourceLocation GnuLabelL) { |
5273 | 0 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
5274 | 0 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
5275 | 0 | } |
5276 | | |
5277 | 0 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5278 | 0 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
5279 | 0 | SourceLocation()); |
5280 | 0 | } |
5281 | | |
5282 | 0 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
5283 | 0 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
5284 | 0 | memcpy(Buffer, Name.data(), Name.size()); |
5285 | 0 | Buffer[Name.size()] = '\0'; |
5286 | 0 | MSAsmName = Buffer; |
5287 | 0 | } |
5288 | | |
5289 | 0 | void ValueDecl::anchor() {} |
5290 | | |
5291 | 0 | bool ValueDecl::isWeak() const { |
5292 | 0 | auto *MostRecent = getMostRecentDecl(); |
5293 | 0 | return MostRecent->hasAttr<WeakAttr>() || |
5294 | 0 | MostRecent->hasAttr<WeakRefAttr>() || isWeakImported(); |
5295 | 0 | } |
5296 | | |
5297 | 0 | bool ValueDecl::isInitCapture() const { |
5298 | 0 | if (auto *Var = llvm::dyn_cast<VarDecl>(this)) |
5299 | 0 | return Var->isInitCapture(); |
5300 | 0 | return false; |
5301 | 0 | } |
5302 | | |
5303 | 0 | void ImplicitParamDecl::anchor() {} |
5304 | | |
5305 | | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
5306 | | SourceLocation IdLoc, |
5307 | | IdentifierInfo *Id, QualType Type, |
5308 | 0 | ImplicitParamKind ParamKind) { |
5309 | 0 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); |
5310 | 0 | } |
5311 | | |
5312 | | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, |
5313 | 0 | ImplicitParamKind ParamKind) { |
5314 | 0 | return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); |
5315 | 0 | } |
5316 | | |
5317 | | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
5318 | 0 | unsigned ID) { |
5319 | 0 | return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); |
5320 | 0 | } |
5321 | | |
5322 | | FunctionDecl * |
5323 | | FunctionDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
5324 | | const DeclarationNameInfo &NameInfo, QualType T, |
5325 | | TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin, |
5326 | | bool isInlineSpecified, bool hasWrittenPrototype, |
5327 | | ConstexprSpecKind ConstexprKind, |
5328 | 19 | Expr *TrailingRequiresClause) { |
5329 | 19 | FunctionDecl *New = new (C, DC) FunctionDecl( |
5330 | 19 | Function, C, DC, StartLoc, NameInfo, T, TInfo, SC, UsesFPIntrin, |
5331 | 19 | isInlineSpecified, ConstexprKind, TrailingRequiresClause); |
5332 | 19 | New->setHasWrittenPrototype(hasWrittenPrototype); |
5333 | 19 | return New; |
5334 | 19 | } |
5335 | | |
5336 | 0 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5337 | 0 | return new (C, ID) FunctionDecl( |
5338 | 0 | Function, C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), |
5339 | 0 | nullptr, SC_None, false, false, ConstexprSpecKind::Unspecified, nullptr); |
5340 | 0 | } |
5341 | | |
5342 | 5 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
5343 | 5 | return new (C, DC) BlockDecl(DC, L); |
5344 | 5 | } |
5345 | | |
5346 | 0 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5347 | 0 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
5348 | 0 | } |
5349 | | |
5350 | | CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) |
5351 | | : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), |
5352 | 0 | NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} |
5353 | | |
5354 | | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
5355 | 0 | unsigned NumParams) { |
5356 | 0 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
5357 | 0 | CapturedDecl(DC, NumParams); |
5358 | 0 | } |
5359 | | |
5360 | | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
5361 | 0 | unsigned NumParams) { |
5362 | 0 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
5363 | 0 | CapturedDecl(nullptr, NumParams); |
5364 | 0 | } |
5365 | | |
5366 | 0 | Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } |
5367 | 0 | void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
5368 | | |
5369 | 0 | bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
5370 | 0 | void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } |
5371 | | |
5372 | | EnumConstantDecl::EnumConstantDecl(const ASTContext &C, DeclContext *DC, |
5373 | | SourceLocation L, IdentifierInfo *Id, |
5374 | | QualType T, Expr *E, const llvm::APSInt &V) |
5375 | 0 | : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt *)E) { |
5376 | 0 | setInitVal(C, V); |
5377 | 0 | } |
5378 | | |
5379 | | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
5380 | | SourceLocation L, |
5381 | | IdentifierInfo *Id, QualType T, |
5382 | 0 | Expr *E, const llvm::APSInt &V) { |
5383 | 0 | return new (C, CD) EnumConstantDecl(C, CD, L, Id, T, E, V); |
5384 | 0 | } |
5385 | | |
5386 | | EnumConstantDecl * |
5387 | 0 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5388 | 0 | return new (C, ID) EnumConstantDecl(C, nullptr, SourceLocation(), nullptr, |
5389 | 0 | QualType(), nullptr, llvm::APSInt()); |
5390 | 0 | } |
5391 | | |
5392 | 0 | void IndirectFieldDecl::anchor() {} |
5393 | | |
5394 | | IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, |
5395 | | SourceLocation L, DeclarationName N, |
5396 | | QualType T, |
5397 | | MutableArrayRef<NamedDecl *> CH) |
5398 | | : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), |
5399 | 0 | ChainingSize(CH.size()) { |
5400 | | // In C++, indirect field declarations conflict with tag declarations in the |
5401 | | // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. |
5402 | 0 | if (C.getLangOpts().CPlusPlus) |
5403 | 0 | IdentifierNamespace |= IDNS_Tag; |
5404 | 0 | } |
5405 | | |
5406 | | IndirectFieldDecl * |
5407 | | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
5408 | | IdentifierInfo *Id, QualType T, |
5409 | 0 | llvm::MutableArrayRef<NamedDecl *> CH) { |
5410 | 0 | return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); |
5411 | 0 | } |
5412 | | |
5413 | | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
5414 | 0 | unsigned ID) { |
5415 | 0 | return new (C, ID) |
5416 | 0 | IndirectFieldDecl(C, nullptr, SourceLocation(), DeclarationName(), |
5417 | 0 | QualType(), std::nullopt); |
5418 | 0 | } |
5419 | | |
5420 | 0 | SourceRange EnumConstantDecl::getSourceRange() const { |
5421 | 0 | SourceLocation End = getLocation(); |
5422 | 0 | if (Init) |
5423 | 0 | End = Init->getEndLoc(); |
5424 | 0 | return SourceRange(getLocation(), End); |
5425 | 0 | } |
5426 | | |
5427 | 0 | void TypeDecl::anchor() {} |
5428 | | |
5429 | | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
5430 | | SourceLocation StartLoc, SourceLocation IdLoc, |
5431 | 299 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
5432 | 299 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
5433 | 299 | } |
5434 | | |
5435 | 0 | void TypedefNameDecl::anchor() {} |
5436 | | |
5437 | 0 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
5438 | 0 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
5439 | 0 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
5440 | 0 | auto *ThisTypedef = this; |
5441 | 0 | if (AnyRedecl && OwningTypedef) { |
5442 | 0 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
5443 | 0 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
5444 | 0 | } |
5445 | 0 | if (OwningTypedef == ThisTypedef) |
5446 | 0 | return TT->getDecl(); |
5447 | 0 | } |
5448 | | |
5449 | 0 | return nullptr; |
5450 | 0 | } |
5451 | | |
5452 | 0 | bool TypedefNameDecl::isTransparentTagSlow() const { |
5453 | 0 | auto determineIsTransparent = [&]() { |
5454 | 0 | if (auto *TT = getUnderlyingType()->getAs<TagType>()) { |
5455 | 0 | if (auto *TD = TT->getDecl()) { |
5456 | 0 | if (TD->getName() != getName()) |
5457 | 0 | return false; |
5458 | 0 | SourceLocation TTLoc = getLocation(); |
5459 | 0 | SourceLocation TDLoc = TD->getLocation(); |
5460 | 0 | if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) |
5461 | 0 | return false; |
5462 | 0 | SourceManager &SM = getASTContext().getSourceManager(); |
5463 | 0 | return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); |
5464 | 0 | } |
5465 | 0 | } |
5466 | 0 | return false; |
5467 | 0 | }; |
5468 | |
|
5469 | 0 | bool isTransparent = determineIsTransparent(); |
5470 | 0 | MaybeModedTInfo.setInt((isTransparent << 1) | 1); |
5471 | 0 | return isTransparent; |
5472 | 0 | } |
5473 | | |
5474 | 0 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5475 | 0 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
5476 | 0 | nullptr, nullptr); |
5477 | 0 | } |
5478 | | |
5479 | | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
5480 | | SourceLocation StartLoc, |
5481 | | SourceLocation IdLoc, IdentifierInfo *Id, |
5482 | 0 | TypeSourceInfo *TInfo) { |
5483 | 0 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
5484 | 0 | } |
5485 | | |
5486 | 0 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5487 | 0 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
5488 | 0 | SourceLocation(), nullptr, nullptr); |
5489 | 0 | } |
5490 | | |
5491 | 0 | SourceRange TypedefDecl::getSourceRange() const { |
5492 | 0 | SourceLocation RangeEnd = getLocation(); |
5493 | 0 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
5494 | 0 | if (typeIsPostfix(TInfo->getType())) |
5495 | 0 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
5496 | 0 | } |
5497 | 0 | return SourceRange(getBeginLoc(), RangeEnd); |
5498 | 0 | } |
5499 | | |
5500 | 0 | SourceRange TypeAliasDecl::getSourceRange() const { |
5501 | 0 | SourceLocation RangeEnd = getBeginLoc(); |
5502 | 0 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
5503 | 0 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
5504 | 0 | return SourceRange(getBeginLoc(), RangeEnd); |
5505 | 0 | } |
5506 | | |
5507 | 0 | void FileScopeAsmDecl::anchor() {} |
5508 | | |
5509 | | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
5510 | | StringLiteral *Str, |
5511 | | SourceLocation AsmLoc, |
5512 | 0 | SourceLocation RParenLoc) { |
5513 | 0 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
5514 | 0 | } |
5515 | | |
5516 | | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
5517 | 0 | unsigned ID) { |
5518 | 0 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
5519 | 0 | SourceLocation()); |
5520 | 0 | } |
5521 | | |
5522 | 0 | void TopLevelStmtDecl::anchor() {} |
5523 | | |
5524 | 0 | TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) { |
5525 | 0 | assert(Statement); |
5526 | 0 | assert(C.getLangOpts().IncrementalExtensions && |
5527 | 0 | "Must be used only in incremental mode"); |
5528 | | |
5529 | 0 | SourceLocation BeginLoc = Statement->getBeginLoc(); |
5530 | 0 | DeclContext *DC = C.getTranslationUnitDecl(); |
5531 | |
|
5532 | 0 | return new (C, DC) TopLevelStmtDecl(DC, BeginLoc, Statement); |
5533 | 0 | } |
5534 | | |
5535 | | TopLevelStmtDecl *TopLevelStmtDecl::CreateDeserialized(ASTContext &C, |
5536 | 0 | unsigned ID) { |
5537 | 0 | return new (C, ID) |
5538 | 0 | TopLevelStmtDecl(/*DC=*/nullptr, SourceLocation(), /*S=*/nullptr); |
5539 | 0 | } |
5540 | | |
5541 | 0 | SourceRange TopLevelStmtDecl::getSourceRange() const { |
5542 | 0 | return SourceRange(getLocation(), Statement->getEndLoc()); |
5543 | 0 | } |
5544 | | |
5545 | 0 | void EmptyDecl::anchor() {} |
5546 | | |
5547 | 439 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
5548 | 439 | return new (C, DC) EmptyDecl(DC, L); |
5549 | 439 | } |
5550 | | |
5551 | 0 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5552 | 0 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
5553 | 0 | } |
5554 | | |
5555 | | HLSLBufferDecl::HLSLBufferDecl(DeclContext *DC, bool CBuffer, |
5556 | | SourceLocation KwLoc, IdentifierInfo *ID, |
5557 | | SourceLocation IDLoc, SourceLocation LBrace) |
5558 | | : NamedDecl(Decl::Kind::HLSLBuffer, DC, IDLoc, DeclarationName(ID)), |
5559 | | DeclContext(Decl::Kind::HLSLBuffer), LBraceLoc(LBrace), KwLoc(KwLoc), |
5560 | 0 | IsCBuffer(CBuffer) {} |
5561 | | |
5562 | | HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C, |
5563 | | DeclContext *LexicalParent, bool CBuffer, |
5564 | | SourceLocation KwLoc, IdentifierInfo *ID, |
5565 | | SourceLocation IDLoc, |
5566 | 0 | SourceLocation LBrace) { |
5567 | | // For hlsl like this |
5568 | | // cbuffer A { |
5569 | | // cbuffer B { |
5570 | | // } |
5571 | | // } |
5572 | | // compiler should treat it as |
5573 | | // cbuffer A { |
5574 | | // } |
5575 | | // cbuffer B { |
5576 | | // } |
5577 | | // FIXME: support nested buffers if required for back-compat. |
5578 | 0 | DeclContext *DC = LexicalParent; |
5579 | 0 | HLSLBufferDecl *Result = |
5580 | 0 | new (C, DC) HLSLBufferDecl(DC, CBuffer, KwLoc, ID, IDLoc, LBrace); |
5581 | 0 | return Result; |
5582 | 0 | } |
5583 | | |
5584 | 0 | HLSLBufferDecl *HLSLBufferDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5585 | 0 | return new (C, ID) HLSLBufferDecl(nullptr, false, SourceLocation(), nullptr, |
5586 | 0 | SourceLocation(), SourceLocation()); |
5587 | 0 | } |
5588 | | |
5589 | | //===----------------------------------------------------------------------===// |
5590 | | // ImportDecl Implementation |
5591 | | //===----------------------------------------------------------------------===// |
5592 | | |
5593 | | /// Retrieve the number of module identifiers needed to name the given |
5594 | | /// module. |
5595 | 0 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
5596 | 0 | unsigned Result = 1; |
5597 | 0 | while (Mod->Parent) { |
5598 | 0 | Mod = Mod->Parent; |
5599 | 0 | ++Result; |
5600 | 0 | } |
5601 | 0 | return Result; |
5602 | 0 | } |
5603 | | |
5604 | | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
5605 | | Module *Imported, |
5606 | | ArrayRef<SourceLocation> IdentifierLocs) |
5607 | | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
5608 | 0 | NextLocalImportAndComplete(nullptr, true) { |
5609 | 0 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
5610 | 0 | auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
5611 | 0 | std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), |
5612 | 0 | StoredLocs); |
5613 | 0 | } |
5614 | | |
5615 | | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
5616 | | Module *Imported, SourceLocation EndLoc) |
5617 | | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
5618 | 0 | NextLocalImportAndComplete(nullptr, false) { |
5619 | 0 | *getTrailingObjects<SourceLocation>() = EndLoc; |
5620 | 0 | } |
5621 | | |
5622 | | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
5623 | | SourceLocation StartLoc, Module *Imported, |
5624 | 0 | ArrayRef<SourceLocation> IdentifierLocs) { |
5625 | 0 | return new (C, DC, |
5626 | 0 | additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) |
5627 | 0 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
5628 | 0 | } |
5629 | | |
5630 | | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
5631 | | SourceLocation StartLoc, |
5632 | | Module *Imported, |
5633 | 0 | SourceLocation EndLoc) { |
5634 | 0 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) |
5635 | 0 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
5636 | 0 | Import->setImplicit(); |
5637 | 0 | return Import; |
5638 | 0 | } |
5639 | | |
5640 | | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
5641 | 0 | unsigned NumLocations) { |
5642 | 0 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) |
5643 | 0 | ImportDecl(EmptyShell()); |
5644 | 0 | } |
5645 | | |
5646 | 0 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
5647 | 0 | if (!isImportComplete()) |
5648 | 0 | return std::nullopt; |
5649 | | |
5650 | 0 | const auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
5651 | 0 | return llvm::ArrayRef(StoredLocs, |
5652 | 0 | getNumModuleIdentifiers(getImportedModule())); |
5653 | 0 | } |
5654 | | |
5655 | 0 | SourceRange ImportDecl::getSourceRange() const { |
5656 | 0 | if (!isImportComplete()) |
5657 | 0 | return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); |
5658 | | |
5659 | 0 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
5660 | 0 | } |
5661 | | |
5662 | | //===----------------------------------------------------------------------===// |
5663 | | // ExportDecl Implementation |
5664 | | //===----------------------------------------------------------------------===// |
5665 | | |
5666 | 0 | void ExportDecl::anchor() {} |
5667 | | |
5668 | | ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, |
5669 | 0 | SourceLocation ExportLoc) { |
5670 | 0 | return new (C, DC) ExportDecl(DC, ExportLoc); |
5671 | 0 | } |
5672 | | |
5673 | 0 | ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
5674 | 0 | return new (C, ID) ExportDecl(nullptr, SourceLocation()); |
5675 | 0 | } |