1 | //===- DeclCXX.h - Classes for representing C++ declarations --*- C++ -*-=====// |
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 | /// \file |
10 | /// Defines the C++ Decl subclasses, other than those for templates |
11 | /// (found in DeclTemplate.h) and friends (in DeclFriend.h). |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_CLANG_AST_DECLCXX_H |
16 | #define LLVM_CLANG_AST_DECLCXX_H |
17 | |
18 | #include "clang/AST/ASTUnresolvedSet.h" |
19 | #include "clang/AST/Decl.h" |
20 | #include "clang/AST/DeclBase.h" |
21 | #include "clang/AST/DeclarationName.h" |
22 | #include "clang/AST/Expr.h" |
23 | #include "clang/AST/ExternalASTSource.h" |
24 | #include "clang/AST/LambdaCapture.h" |
25 | #include "clang/AST/NestedNameSpecifier.h" |
26 | #include "clang/AST/Redeclarable.h" |
27 | #include "clang/AST/Stmt.h" |
28 | #include "clang/AST/Type.h" |
29 | #include "clang/AST/TypeLoc.h" |
30 | #include "clang/AST/UnresolvedSet.h" |
31 | #include "clang/Basic/LLVM.h" |
32 | #include "clang/Basic/Lambda.h" |
33 | #include "clang/Basic/LangOptions.h" |
34 | #include "clang/Basic/OperatorKinds.h" |
35 | #include "clang/Basic/SourceLocation.h" |
36 | #include "clang/Basic/Specifiers.h" |
37 | #include "llvm/ADT/ArrayRef.h" |
38 | #include "llvm/ADT/DenseMap.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/STLExtras.h" |
42 | #include "llvm/ADT/TinyPtrVector.h" |
43 | #include "llvm/ADT/iterator_range.h" |
44 | #include "llvm/Support/Casting.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/PointerLikeTypeTraits.h" |
47 | #include "llvm/Support/TrailingObjects.h" |
48 | #include <cassert> |
49 | #include <cstddef> |
50 | #include <iterator> |
51 | #include <memory> |
52 | #include <vector> |
53 | |
54 | namespace clang { |
55 | |
56 | class ASTContext; |
57 | class ClassTemplateDecl; |
58 | class ConstructorUsingShadowDecl; |
59 | class CXXBasePath; |
60 | class CXXBasePaths; |
61 | class CXXConstructorDecl; |
62 | class CXXDestructorDecl; |
63 | class CXXFinalOverriderMap; |
64 | class CXXIndirectPrimaryBaseSet; |
65 | class CXXMethodDecl; |
66 | class DecompositionDecl; |
67 | class FriendDecl; |
68 | class FunctionTemplateDecl; |
69 | class IdentifierInfo; |
70 | class MemberSpecializationInfo; |
71 | class BaseUsingDecl; |
72 | class TemplateDecl; |
73 | class TemplateParameterList; |
74 | class UsingDecl; |
75 | |
76 | /// Represents an access specifier followed by colon ':'. |
77 | /// |
78 | /// An objects of this class represents sugar for the syntactic occurrence |
79 | /// of an access specifier followed by a colon in the list of member |
80 | /// specifiers of a C++ class definition. |
81 | /// |
82 | /// Note that they do not represent other uses of access specifiers, |
83 | /// such as those occurring in a list of base specifiers. |
84 | /// Also note that this class has nothing to do with so-called |
85 | /// "access declarations" (C++98 11.3 [class.access.dcl]). |
86 | class AccessSpecDecl : public Decl { |
87 | /// The location of the ':'. |
88 | SourceLocation ColonLoc; |
89 | |
90 | AccessSpecDecl(AccessSpecifier AS, DeclContext *DC, |
91 | SourceLocation ASLoc, SourceLocation ColonLoc) |
92 | : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) { |
93 | setAccess(AS); |
94 | } |
95 | |
96 | AccessSpecDecl(EmptyShell Empty) : Decl(AccessSpec, Empty) {} |
97 | |
98 | virtual void anchor(); |
99 | |
100 | public: |
101 | /// The location of the access specifier. |
102 | SourceLocation getAccessSpecifierLoc() const { return getLocation(); } |
103 | |
104 | /// Sets the location of the access specifier. |
105 | void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); } |
106 | |
107 | /// The location of the colon following the access specifier. |
108 | SourceLocation getColonLoc() const { return ColonLoc; } |
109 | |
110 | /// Sets the location of the colon. |
111 | void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; } |
112 | |
113 | SourceRange getSourceRange() const override LLVM_READONLY { |
114 | return SourceRange(getAccessSpecifierLoc(), getColonLoc()); |
115 | } |
116 | |
117 | static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS, |
118 | DeclContext *DC, SourceLocation ASLoc, |
119 | SourceLocation ColonLoc) { |
120 | return new (C, DC) AccessSpecDecl(AS, DC, ASLoc, ColonLoc); |
121 | } |
122 | |
123 | static AccessSpecDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
124 | |
125 | // Implement isa/cast/dyncast/etc. |
126 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
127 | static bool classofKind(Kind K) { return K == AccessSpec; } |
128 | }; |
129 | |
130 | /// Represents a base class of a C++ class. |
131 | /// |
132 | /// Each CXXBaseSpecifier represents a single, direct base class (or |
133 | /// struct) of a C++ class (or struct). It specifies the type of that |
134 | /// base class, whether it is a virtual or non-virtual base, and what |
135 | /// level of access (public, protected, private) is used for the |
136 | /// derivation. For example: |
137 | /// |
138 | /// \code |
139 | /// class A { }; |
140 | /// class B { }; |
141 | /// class C : public virtual A, protected B { }; |
142 | /// \endcode |
143 | /// |
144 | /// In this code, C will have two CXXBaseSpecifiers, one for "public |
145 | /// virtual A" and the other for "protected B". |
146 | class CXXBaseSpecifier { |
147 | /// The source code range that covers the full base |
148 | /// specifier, including the "virtual" (if present) and access |
149 | /// specifier (if present). |
150 | SourceRange Range; |
151 | |
152 | /// The source location of the ellipsis, if this is a pack |
153 | /// expansion. |
154 | SourceLocation EllipsisLoc; |
155 | |
156 | /// Whether this is a virtual base class or not. |
157 | LLVM_PREFERRED_TYPE(bool) |
158 | unsigned Virtual : 1; |
159 | |
160 | /// Whether this is the base of a class (true) or of a struct (false). |
161 | /// |
162 | /// This determines the mapping from the access specifier as written in the |
163 | /// source code to the access specifier used for semantic analysis. |
164 | LLVM_PREFERRED_TYPE(bool) |
165 | unsigned BaseOfClass : 1; |
166 | |
167 | /// Access specifier as written in the source code (may be AS_none). |
168 | /// |
169 | /// The actual type of data stored here is an AccessSpecifier, but we use |
170 | /// "unsigned" here to work around Microsoft ABI. |
171 | LLVM_PREFERRED_TYPE(AccessSpecifier) |
172 | unsigned Access : 2; |
173 | |
174 | /// Whether the class contains a using declaration |
175 | /// to inherit the named class's constructors. |
176 | LLVM_PREFERRED_TYPE(bool) |
177 | unsigned InheritConstructors : 1; |
178 | |
179 | /// The type of the base class. |
180 | /// |
181 | /// This will be a class or struct (or a typedef of such). The source code |
182 | /// range does not include the \c virtual or the access specifier. |
183 | TypeSourceInfo *BaseTypeInfo; |
184 | |
185 | public: |
186 | CXXBaseSpecifier() = default; |
187 | CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, |
188 | TypeSourceInfo *TInfo, SourceLocation EllipsisLoc) |
189 | : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC), |
190 | Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) {} |
191 | |
192 | /// Retrieves the source range that contains the entire base specifier. |
193 | SourceRange getSourceRange() const LLVM_READONLY { return Range; } |
194 | SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } |
195 | SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } |
196 | |
197 | /// Get the location at which the base class type was written. |
198 | SourceLocation getBaseTypeLoc() const LLVM_READONLY { |
199 | return BaseTypeInfo->getTypeLoc().getBeginLoc(); |
200 | } |
201 | |
202 | /// Determines whether the base class is a virtual base class (or not). |
203 | bool isVirtual() const { return Virtual; } |
204 | |
205 | /// Determine whether this base class is a base of a class declared |
206 | /// with the 'class' keyword (vs. one declared with the 'struct' keyword). |
207 | bool isBaseOfClass() const { return BaseOfClass; } |
208 | |
209 | /// Determine whether this base specifier is a pack expansion. |
210 | bool isPackExpansion() const { return EllipsisLoc.isValid(); } |
211 | |
212 | /// Determine whether this base class's constructors get inherited. |
213 | bool getInheritConstructors() const { return InheritConstructors; } |
214 | |
215 | /// Set that this base class's constructors should be inherited. |
216 | void setInheritConstructors(bool Inherit = true) { |
217 | InheritConstructors = Inherit; |
218 | } |
219 | |
220 | /// For a pack expansion, determine the location of the ellipsis. |
221 | SourceLocation getEllipsisLoc() const { |
222 | return EllipsisLoc; |
223 | } |
224 | |
225 | /// Returns the access specifier for this base specifier. |
226 | /// |
227 | /// This is the actual base specifier as used for semantic analysis, so |
228 | /// the result can never be AS_none. To retrieve the access specifier as |
229 | /// written in the source code, use getAccessSpecifierAsWritten(). |
230 | AccessSpecifier getAccessSpecifier() const { |
231 | if ((AccessSpecifier)Access == AS_none) |
232 | return BaseOfClass? AS_private : AS_public; |
233 | else |
234 | return (AccessSpecifier)Access; |
235 | } |
236 | |
237 | /// Retrieves the access specifier as written in the source code |
238 | /// (which may mean that no access specifier was explicitly written). |
239 | /// |
240 | /// Use getAccessSpecifier() to retrieve the access specifier for use in |
241 | /// semantic analysis. |
242 | AccessSpecifier getAccessSpecifierAsWritten() const { |
243 | return (AccessSpecifier)Access; |
244 | } |
245 | |
246 | /// Retrieves the type of the base class. |
247 | /// |
248 | /// This type will always be an unqualified class type. |
249 | QualType getType() const { |
250 | return BaseTypeInfo->getType().getUnqualifiedType(); |
251 | } |
252 | |
253 | /// Retrieves the type and source location of the base class. |
254 | TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; } |
255 | }; |
256 | |
257 | /// Represents a C++ struct/union/class. |
258 | class CXXRecordDecl : public RecordDecl { |
259 | friend class ASTDeclReader; |
260 | friend class ASTDeclWriter; |
261 | friend class ASTNodeImporter; |
262 | friend class ASTReader; |
263 | friend class ASTRecordWriter; |
264 | friend class ASTWriter; |
265 | friend class DeclContext; |
266 | friend class LambdaExpr; |
267 | friend class ODRDiagsEmitter; |
268 | |
269 | friend void FunctionDecl::setIsPureVirtual(bool); |
270 | friend void TagDecl::startDefinition(); |
271 | |
272 | /// Values used in DefinitionData fields to represent special members. |
273 | enum SpecialMemberFlags { |
274 | SMF_DefaultConstructor = 0x1, |
275 | SMF_CopyConstructor = 0x2, |
276 | SMF_MoveConstructor = 0x4, |
277 | SMF_CopyAssignment = 0x8, |
278 | SMF_MoveAssignment = 0x10, |
279 | SMF_Destructor = 0x20, |
280 | SMF_All = 0x3f |
281 | }; |
282 | |
283 | public: |
284 | enum LambdaDependencyKind { |
285 | LDK_Unknown = 0, |
286 | LDK_AlwaysDependent, |
287 | LDK_NeverDependent, |
288 | }; |
289 | |
290 | private: |
291 | struct DefinitionData { |
292 | #define FIELD(Name, Width, Merge) \ |
293 | unsigned Name : Width; |
294 | #include "CXXRecordDeclDefinitionBits.def" |
295 | |
296 | /// Whether this class describes a C++ lambda. |
297 | LLVM_PREFERRED_TYPE(bool) |
298 | unsigned IsLambda : 1; |
299 | |
300 | /// Whether we are currently parsing base specifiers. |
301 | LLVM_PREFERRED_TYPE(bool) |
302 | unsigned IsParsingBaseSpecifiers : 1; |
303 | |
304 | /// True when visible conversion functions are already computed |
305 | /// and are available. |
306 | LLVM_PREFERRED_TYPE(bool) |
307 | unsigned ComputedVisibleConversions : 1; |
308 | |
309 | LLVM_PREFERRED_TYPE(bool) |
310 | unsigned HasODRHash : 1; |
311 | |
312 | /// A hash of parts of the class to help in ODR checking. |
313 | unsigned ODRHash = 0; |
314 | |
315 | /// The number of base class specifiers in Bases. |
316 | unsigned NumBases = 0; |
317 | |
318 | /// The number of virtual base class specifiers in VBases. |
319 | unsigned NumVBases = 0; |
320 | |
321 | /// Base classes of this class. |
322 | /// |
323 | /// FIXME: This is wasted space for a union. |
324 | LazyCXXBaseSpecifiersPtr Bases; |
325 | |
326 | /// direct and indirect virtual base classes of this class. |
327 | LazyCXXBaseSpecifiersPtr VBases; |
328 | |
329 | /// The conversion functions of this C++ class (but not its |
330 | /// inherited conversion functions). |
331 | /// |
332 | /// Each of the entries in this overload set is a CXXConversionDecl. |
333 | LazyASTUnresolvedSet Conversions; |
334 | |
335 | /// The conversion functions of this C++ class and all those |
336 | /// inherited conversion functions that are visible in this class. |
337 | /// |
338 | /// Each of the entries in this overload set is a CXXConversionDecl or a |
339 | /// FunctionTemplateDecl. |
340 | LazyASTUnresolvedSet VisibleConversions; |
341 | |
342 | /// The declaration which defines this record. |
343 | CXXRecordDecl *Definition; |
344 | |
345 | /// The first friend declaration in this class, or null if there |
346 | /// aren't any. |
347 | /// |
348 | /// This is actually currently stored in reverse order. |
349 | LazyDeclPtr FirstFriend; |
350 | |
351 | DefinitionData(CXXRecordDecl *D); |
352 | |
353 | /// Retrieve the set of direct base classes. |
354 | CXXBaseSpecifier *getBases() const { |
355 | if (!Bases.isOffset()) |
356 | return Bases.get(Source: nullptr); |
357 | return getBasesSlowCase(); |
358 | } |
359 | |
360 | /// Retrieve the set of virtual base classes. |
361 | CXXBaseSpecifier *getVBases() const { |
362 | if (!VBases.isOffset()) |
363 | return VBases.get(Source: nullptr); |
364 | return getVBasesSlowCase(); |
365 | } |
366 | |
367 | ArrayRef<CXXBaseSpecifier> bases() const { |
368 | return llvm::ArrayRef(getBases(), NumBases); |
369 | } |
370 | |
371 | ArrayRef<CXXBaseSpecifier> vbases() const { |
372 | return llvm::ArrayRef(getVBases(), NumVBases); |
373 | } |
374 | |
375 | private: |
376 | CXXBaseSpecifier *getBasesSlowCase() const; |
377 | CXXBaseSpecifier *getVBasesSlowCase() const; |
378 | }; |
379 | |
380 | struct DefinitionData *DefinitionData; |
381 | |
382 | /// Describes a C++ closure type (generated by a lambda expression). |
383 | struct LambdaDefinitionData : public DefinitionData { |
384 | using Capture = LambdaCapture; |
385 | |
386 | /// Whether this lambda is known to be dependent, even if its |
387 | /// context isn't dependent. |
388 | /// |
389 | /// A lambda with a non-dependent context can be dependent if it occurs |
390 | /// within the default argument of a function template, because the |
391 | /// lambda will have been created with the enclosing context as its |
392 | /// declaration context, rather than function. This is an unfortunate |
393 | /// artifact of having to parse the default arguments before. |
394 | LLVM_PREFERRED_TYPE(LambdaDependencyKind) |
395 | unsigned DependencyKind : 2; |
396 | |
397 | /// Whether this lambda is a generic lambda. |
398 | LLVM_PREFERRED_TYPE(bool) |
399 | unsigned IsGenericLambda : 1; |
400 | |
401 | /// The Default Capture. |
402 | LLVM_PREFERRED_TYPE(LambdaCaptureDefault) |
403 | unsigned CaptureDefault : 2; |
404 | |
405 | /// The number of captures in this lambda is limited 2^NumCaptures. |
406 | unsigned NumCaptures : 15; |
407 | |
408 | /// The number of explicit captures in this lambda. |
409 | unsigned NumExplicitCaptures : 12; |
410 | |
411 | /// Has known `internal` linkage. |
412 | LLVM_PREFERRED_TYPE(bool) |
413 | unsigned HasKnownInternalLinkage : 1; |
414 | |
415 | /// The number used to indicate this lambda expression for name |
416 | /// mangling in the Itanium C++ ABI. |
417 | unsigned ManglingNumber : 31; |
418 | |
419 | /// The index of this lambda within its context declaration. This is not in |
420 | /// general the same as the mangling number. |
421 | unsigned IndexInContext; |
422 | |
423 | /// The declaration that provides context for this lambda, if the |
424 | /// actual DeclContext does not suffice. This is used for lambdas that |
425 | /// occur within default arguments of function parameters within the class |
426 | /// or within a data member initializer. |
427 | LazyDeclPtr ContextDecl; |
428 | |
429 | /// The lists of captures, both explicit and implicit, for this |
430 | /// lambda. One list is provided for each merged copy of the lambda. |
431 | /// The first list corresponds to the canonical definition. |
432 | /// The destructor is registered by AddCaptureList when necessary. |
433 | llvm::TinyPtrVector<Capture*> Captures; |
434 | |
435 | /// The type of the call method. |
436 | TypeSourceInfo *MethodTyInfo; |
437 | |
438 | LambdaDefinitionData(CXXRecordDecl *D, TypeSourceInfo *Info, unsigned DK, |
439 | bool IsGeneric, LambdaCaptureDefault CaptureDefault) |
440 | : DefinitionData(D), DependencyKind(DK), IsGenericLambda(IsGeneric), |
441 | CaptureDefault(CaptureDefault), NumCaptures(0), |
442 | NumExplicitCaptures(0), HasKnownInternalLinkage(0), ManglingNumber(0), |
443 | IndexInContext(0), MethodTyInfo(Info) { |
444 | IsLambda = true; |
445 | |
446 | // C++1z [expr.prim.lambda]p4: |
447 | // This class type is not an aggregate type. |
448 | Aggregate = false; |
449 | PlainOldData = false; |
450 | } |
451 | |
452 | // Add a list of captures. |
453 | void AddCaptureList(ASTContext &Ctx, Capture *CaptureList); |
454 | }; |
455 | |
456 | struct DefinitionData *dataPtr() const { |
457 | // Complete the redecl chain (if necessary). |
458 | getMostRecentDecl(); |
459 | return DefinitionData; |
460 | } |
461 | |
462 | struct DefinitionData &data() const { |
463 | auto *DD = dataPtr(); |
464 | assert(DD && "queried property of class with no definition" ); |
465 | return *DD; |
466 | } |
467 | |
468 | struct LambdaDefinitionData &getLambdaData() const { |
469 | // No update required: a merged definition cannot change any lambda |
470 | // properties. |
471 | auto *DD = DefinitionData; |
472 | assert(DD && DD->IsLambda && "queried lambda property of non-lambda class" ); |
473 | return static_cast<LambdaDefinitionData&>(*DD); |
474 | } |
475 | |
476 | /// The template or declaration that this declaration |
477 | /// describes or was instantiated from, respectively. |
478 | /// |
479 | /// For non-templates, this value will be null. For record |
480 | /// declarations that describe a class template, this will be a |
481 | /// pointer to a ClassTemplateDecl. For member |
482 | /// classes of class template specializations, this will be the |
483 | /// MemberSpecializationInfo referring to the member class that was |
484 | /// instantiated or specialized. |
485 | llvm::PointerUnion<ClassTemplateDecl *, MemberSpecializationInfo *> |
486 | TemplateOrInstantiation; |
487 | |
488 | /// Called from setBases and addedMember to notify the class that a |
489 | /// direct or virtual base class or a member of class type has been added. |
490 | void addedClassSubobject(CXXRecordDecl *Base); |
491 | |
492 | /// Notify the class that member has been added. |
493 | /// |
494 | /// This routine helps maintain information about the class based on which |
495 | /// members have been added. It will be invoked by DeclContext::addDecl() |
496 | /// whenever a member is added to this record. |
497 | void addedMember(Decl *D); |
498 | |
499 | void markedVirtualFunctionPure(); |
500 | |
501 | /// Get the head of our list of friend declarations, possibly |
502 | /// deserializing the friends from an external AST source. |
503 | FriendDecl *getFirstFriend() const; |
504 | |
505 | /// Determine whether this class has an empty base class subobject of type X |
506 | /// or of one of the types that might be at offset 0 within X (per the C++ |
507 | /// "standard layout" rules). |
508 | bool hasSubobjectAtOffsetZeroOfEmptyBaseType(ASTContext &Ctx, |
509 | const CXXRecordDecl *X); |
510 | |
511 | protected: |
512 | CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C, DeclContext *DC, |
513 | SourceLocation StartLoc, SourceLocation IdLoc, |
514 | IdentifierInfo *Id, CXXRecordDecl *PrevDecl); |
515 | |
516 | public: |
517 | /// Iterator that traverses the base classes of a class. |
518 | using base_class_iterator = CXXBaseSpecifier *; |
519 | |
520 | /// Iterator that traverses the base classes of a class. |
521 | using base_class_const_iterator = const CXXBaseSpecifier *; |
522 | |
523 | CXXRecordDecl *getCanonicalDecl() override { |
524 | return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl()); |
525 | } |
526 | |
527 | const CXXRecordDecl *getCanonicalDecl() const { |
528 | return const_cast<CXXRecordDecl*>(this)->getCanonicalDecl(); |
529 | } |
530 | |
531 | CXXRecordDecl *getPreviousDecl() { |
532 | return cast_or_null<CXXRecordDecl>( |
533 | static_cast<RecordDecl *>(this)->getPreviousDecl()); |
534 | } |
535 | |
536 | const CXXRecordDecl *getPreviousDecl() const { |
537 | return const_cast<CXXRecordDecl*>(this)->getPreviousDecl(); |
538 | } |
539 | |
540 | CXXRecordDecl *getMostRecentDecl() { |
541 | return cast<CXXRecordDecl>( |
542 | static_cast<RecordDecl *>(this)->getMostRecentDecl()); |
543 | } |
544 | |
545 | const CXXRecordDecl *getMostRecentDecl() const { |
546 | return const_cast<CXXRecordDecl*>(this)->getMostRecentDecl(); |
547 | } |
548 | |
549 | CXXRecordDecl *getMostRecentNonInjectedDecl() { |
550 | CXXRecordDecl *Recent = |
551 | static_cast<CXXRecordDecl *>(this)->getMostRecentDecl(); |
552 | while (Recent->isInjectedClassName()) { |
553 | // FIXME: Does injected class name need to be in the redeclarations chain? |
554 | assert(Recent->getPreviousDecl()); |
555 | Recent = Recent->getPreviousDecl(); |
556 | } |
557 | return Recent; |
558 | } |
559 | |
560 | const CXXRecordDecl *getMostRecentNonInjectedDecl() const { |
561 | return const_cast<CXXRecordDecl*>(this)->getMostRecentNonInjectedDecl(); |
562 | } |
563 | |
564 | CXXRecordDecl *getDefinition() const { |
565 | // We only need an update if we don't already know which |
566 | // declaration is the definition. |
567 | auto *DD = DefinitionData ? DefinitionData : dataPtr(); |
568 | return DD ? DD->Definition : nullptr; |
569 | } |
570 | |
571 | bool hasDefinition() const { return DefinitionData || dataPtr(); } |
572 | |
573 | static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
574 | SourceLocation StartLoc, SourceLocation IdLoc, |
575 | IdentifierInfo *Id, |
576 | CXXRecordDecl *PrevDecl = nullptr, |
577 | bool DelayTypeCreation = false); |
578 | static CXXRecordDecl *CreateLambda(const ASTContext &C, DeclContext *DC, |
579 | TypeSourceInfo *Info, SourceLocation Loc, |
580 | unsigned DependencyKind, bool IsGeneric, |
581 | LambdaCaptureDefault CaptureDefault); |
582 | static CXXRecordDecl *CreateDeserialized(const ASTContext &C, DeclID ID); |
583 | |
584 | bool isDynamicClass() const { |
585 | return data().Polymorphic || data().NumVBases != 0; |
586 | } |
587 | |
588 | /// @returns true if class is dynamic or might be dynamic because the |
589 | /// definition is incomplete of dependent. |
590 | bool mayBeDynamicClass() const { |
591 | return !hasDefinition() || isDynamicClass() || hasAnyDependentBases(); |
592 | } |
593 | |
594 | /// @returns true if class is non dynamic or might be non dynamic because the |
595 | /// definition is incomplete of dependent. |
596 | bool mayBeNonDynamicClass() const { |
597 | return !hasDefinition() || !isDynamicClass() || hasAnyDependentBases(); |
598 | } |
599 | |
600 | void setIsParsingBaseSpecifiers() { data().IsParsingBaseSpecifiers = true; } |
601 | |
602 | bool isParsingBaseSpecifiers() const { |
603 | return data().IsParsingBaseSpecifiers; |
604 | } |
605 | |
606 | unsigned getODRHash() const; |
607 | |
608 | /// Sets the base classes of this struct or class. |
609 | void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases); |
610 | |
611 | /// Retrieves the number of base classes of this class. |
612 | unsigned getNumBases() const { return data().NumBases; } |
613 | |
614 | using base_class_range = llvm::iterator_range<base_class_iterator>; |
615 | using base_class_const_range = |
616 | llvm::iterator_range<base_class_const_iterator>; |
617 | |
618 | base_class_range bases() { |
619 | return base_class_range(bases_begin(), bases_end()); |
620 | } |
621 | base_class_const_range bases() const { |
622 | return base_class_const_range(bases_begin(), bases_end()); |
623 | } |
624 | |
625 | base_class_iterator bases_begin() { return data().getBases(); } |
626 | base_class_const_iterator bases_begin() const { return data().getBases(); } |
627 | base_class_iterator bases_end() { return bases_begin() + data().NumBases; } |
628 | base_class_const_iterator bases_end() const { |
629 | return bases_begin() + data().NumBases; |
630 | } |
631 | |
632 | /// Retrieves the number of virtual base classes of this class. |
633 | unsigned getNumVBases() const { return data().NumVBases; } |
634 | |
635 | base_class_range vbases() { |
636 | return base_class_range(vbases_begin(), vbases_end()); |
637 | } |
638 | base_class_const_range vbases() const { |
639 | return base_class_const_range(vbases_begin(), vbases_end()); |
640 | } |
641 | |
642 | base_class_iterator vbases_begin() { return data().getVBases(); } |
643 | base_class_const_iterator vbases_begin() const { return data().getVBases(); } |
644 | base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; } |
645 | base_class_const_iterator vbases_end() const { |
646 | return vbases_begin() + data().NumVBases; |
647 | } |
648 | |
649 | /// Determine whether this class has any dependent base classes which |
650 | /// are not the current instantiation. |
651 | bool hasAnyDependentBases() const; |
652 | |
653 | /// Iterator access to method members. The method iterator visits |
654 | /// all method members of the class, including non-instance methods, |
655 | /// special methods, etc. |
656 | using method_iterator = specific_decl_iterator<CXXMethodDecl>; |
657 | using method_range = |
658 | llvm::iterator_range<specific_decl_iterator<CXXMethodDecl>>; |
659 | |
660 | method_range methods() const { |
661 | return method_range(method_begin(), method_end()); |
662 | } |
663 | |
664 | /// Method begin iterator. Iterates in the order the methods |
665 | /// were declared. |
666 | method_iterator method_begin() const { |
667 | return method_iterator(decls_begin()); |
668 | } |
669 | |
670 | /// Method past-the-end iterator. |
671 | method_iterator method_end() const { |
672 | return method_iterator(decls_end()); |
673 | } |
674 | |
675 | /// Iterator access to constructor members. |
676 | using ctor_iterator = specific_decl_iterator<CXXConstructorDecl>; |
677 | using ctor_range = |
678 | llvm::iterator_range<specific_decl_iterator<CXXConstructorDecl>>; |
679 | |
680 | ctor_range ctors() const { return ctor_range(ctor_begin(), ctor_end()); } |
681 | |
682 | ctor_iterator ctor_begin() const { |
683 | return ctor_iterator(decls_begin()); |
684 | } |
685 | |
686 | ctor_iterator ctor_end() const { |
687 | return ctor_iterator(decls_end()); |
688 | } |
689 | |
690 | /// An iterator over friend declarations. All of these are defined |
691 | /// in DeclFriend.h. |
692 | class friend_iterator; |
693 | using friend_range = llvm::iterator_range<friend_iterator>; |
694 | |
695 | friend_range friends() const; |
696 | friend_iterator friend_begin() const; |
697 | friend_iterator friend_end() const; |
698 | void pushFriendDecl(FriendDecl *FD); |
699 | |
700 | /// Determines whether this record has any friends. |
701 | bool hasFriends() const { |
702 | return data().FirstFriend.isValid(); |
703 | } |
704 | |
705 | /// \c true if a defaulted copy constructor for this class would be |
706 | /// deleted. |
707 | bool defaultedCopyConstructorIsDeleted() const { |
708 | assert((!needsOverloadResolutionForCopyConstructor() || |
709 | (data().DeclaredSpecialMembers & SMF_CopyConstructor)) && |
710 | "this property has not yet been computed by Sema" ); |
711 | return data().DefaultedCopyConstructorIsDeleted; |
712 | } |
713 | |
714 | /// \c true if a defaulted move constructor for this class would be |
715 | /// deleted. |
716 | bool defaultedMoveConstructorIsDeleted() const { |
717 | assert((!needsOverloadResolutionForMoveConstructor() || |
718 | (data().DeclaredSpecialMembers & SMF_MoveConstructor)) && |
719 | "this property has not yet been computed by Sema" ); |
720 | return data().DefaultedMoveConstructorIsDeleted; |
721 | } |
722 | |
723 | /// \c true if a defaulted destructor for this class would be deleted. |
724 | bool defaultedDestructorIsDeleted() const { |
725 | assert((!needsOverloadResolutionForDestructor() || |
726 | (data().DeclaredSpecialMembers & SMF_Destructor)) && |
727 | "this property has not yet been computed by Sema" ); |
728 | return data().DefaultedDestructorIsDeleted; |
729 | } |
730 | |
731 | /// \c true if we know for sure that this class has a single, |
732 | /// accessible, unambiguous copy constructor that is not deleted. |
733 | bool hasSimpleCopyConstructor() const { |
734 | return !hasUserDeclaredCopyConstructor() && |
735 | !data().DefaultedCopyConstructorIsDeleted; |
736 | } |
737 | |
738 | /// \c true if we know for sure that this class has a single, |
739 | /// accessible, unambiguous move constructor that is not deleted. |
740 | bool hasSimpleMoveConstructor() const { |
741 | return !hasUserDeclaredMoveConstructor() && hasMoveConstructor() && |
742 | !data().DefaultedMoveConstructorIsDeleted; |
743 | } |
744 | |
745 | /// \c true if we know for sure that this class has a single, |
746 | /// accessible, unambiguous copy assignment operator that is not deleted. |
747 | bool hasSimpleCopyAssignment() const { |
748 | return !hasUserDeclaredCopyAssignment() && |
749 | !data().DefaultedCopyAssignmentIsDeleted; |
750 | } |
751 | |
752 | /// \c true if we know for sure that this class has a single, |
753 | /// accessible, unambiguous move assignment operator that is not deleted. |
754 | bool hasSimpleMoveAssignment() const { |
755 | return !hasUserDeclaredMoveAssignment() && hasMoveAssignment() && |
756 | !data().DefaultedMoveAssignmentIsDeleted; |
757 | } |
758 | |
759 | /// \c true if we know for sure that this class has an accessible |
760 | /// destructor that is not deleted. |
761 | bool hasSimpleDestructor() const { |
762 | return !hasUserDeclaredDestructor() && |
763 | !data().DefaultedDestructorIsDeleted; |
764 | } |
765 | |
766 | /// Determine whether this class has any default constructors. |
767 | bool hasDefaultConstructor() const { |
768 | return (data().DeclaredSpecialMembers & SMF_DefaultConstructor) || |
769 | needsImplicitDefaultConstructor(); |
770 | } |
771 | |
772 | /// Determine if we need to declare a default constructor for |
773 | /// this class. |
774 | /// |
775 | /// This value is used for lazy creation of default constructors. |
776 | bool needsImplicitDefaultConstructor() const { |
777 | return (!data().UserDeclaredConstructor && |
778 | !(data().DeclaredSpecialMembers & SMF_DefaultConstructor) && |
779 | (!isLambda() || lambdaIsDefaultConstructibleAndAssignable())) || |
780 | // FIXME: Proposed fix to core wording issue: if a class inherits |
781 | // a default constructor and doesn't explicitly declare one, one |
782 | // is declared implicitly. |
783 | (data().HasInheritedDefaultConstructor && |
784 | !(data().DeclaredSpecialMembers & SMF_DefaultConstructor)); |
785 | } |
786 | |
787 | /// Determine whether this class has any user-declared constructors. |
788 | /// |
789 | /// When true, a default constructor will not be implicitly declared. |
790 | bool hasUserDeclaredConstructor() const { |
791 | return data().UserDeclaredConstructor; |
792 | } |
793 | |
794 | /// Whether this class has a user-provided default constructor |
795 | /// per C++11. |
796 | bool hasUserProvidedDefaultConstructor() const { |
797 | return data().UserProvidedDefaultConstructor; |
798 | } |
799 | |
800 | /// Determine whether this class has a user-declared copy constructor. |
801 | /// |
802 | /// When false, a copy constructor will be implicitly declared. |
803 | bool hasUserDeclaredCopyConstructor() const { |
804 | return data().UserDeclaredSpecialMembers & SMF_CopyConstructor; |
805 | } |
806 | |
807 | /// Determine whether this class needs an implicit copy |
808 | /// constructor to be lazily declared. |
809 | bool needsImplicitCopyConstructor() const { |
810 | return !(data().DeclaredSpecialMembers & SMF_CopyConstructor); |
811 | } |
812 | |
813 | /// Determine whether we need to eagerly declare a defaulted copy |
814 | /// constructor for this class. |
815 | bool needsOverloadResolutionForCopyConstructor() const { |
816 | // C++17 [class.copy.ctor]p6: |
817 | // If the class definition declares a move constructor or move assignment |
818 | // operator, the implicitly declared copy constructor is defined as |
819 | // deleted. |
820 | // In MSVC mode, sometimes a declared move assignment does not delete an |
821 | // implicit copy constructor, so defer this choice to Sema. |
822 | if (data().UserDeclaredSpecialMembers & |
823 | (SMF_MoveConstructor | SMF_MoveAssignment)) |
824 | return true; |
825 | return data().NeedOverloadResolutionForCopyConstructor; |
826 | } |
827 | |
828 | /// Determine whether an implicit copy constructor for this type |
829 | /// would have a parameter with a const-qualified reference type. |
830 | bool implicitCopyConstructorHasConstParam() const { |
831 | return data().ImplicitCopyConstructorCanHaveConstParamForNonVBase && |
832 | (isAbstract() || |
833 | data().ImplicitCopyConstructorCanHaveConstParamForVBase); |
834 | } |
835 | |
836 | /// Determine whether this class has a copy constructor with |
837 | /// a parameter type which is a reference to a const-qualified type. |
838 | bool hasCopyConstructorWithConstParam() const { |
839 | return data().HasDeclaredCopyConstructorWithConstParam || |
840 | (needsImplicitCopyConstructor() && |
841 | implicitCopyConstructorHasConstParam()); |
842 | } |
843 | |
844 | /// Whether this class has a user-declared move constructor or |
845 | /// assignment operator. |
846 | /// |
847 | /// When false, a move constructor and assignment operator may be |
848 | /// implicitly declared. |
849 | bool hasUserDeclaredMoveOperation() const { |
850 | return data().UserDeclaredSpecialMembers & |
851 | (SMF_MoveConstructor | SMF_MoveAssignment); |
852 | } |
853 | |
854 | /// Determine whether this class has had a move constructor |
855 | /// declared by the user. |
856 | bool hasUserDeclaredMoveConstructor() const { |
857 | return data().UserDeclaredSpecialMembers & SMF_MoveConstructor; |
858 | } |
859 | |
860 | /// Determine whether this class has a move constructor. |
861 | bool hasMoveConstructor() const { |
862 | return (data().DeclaredSpecialMembers & SMF_MoveConstructor) || |
863 | needsImplicitMoveConstructor(); |
864 | } |
865 | |
866 | /// Set that we attempted to declare an implicit copy |
867 | /// constructor, but overload resolution failed so we deleted it. |
868 | void setImplicitCopyConstructorIsDeleted() { |
869 | assert((data().DefaultedCopyConstructorIsDeleted || |
870 | needsOverloadResolutionForCopyConstructor()) && |
871 | "Copy constructor should not be deleted" ); |
872 | data().DefaultedCopyConstructorIsDeleted = true; |
873 | } |
874 | |
875 | /// Set that we attempted to declare an implicit move |
876 | /// constructor, but overload resolution failed so we deleted it. |
877 | void setImplicitMoveConstructorIsDeleted() { |
878 | assert((data().DefaultedMoveConstructorIsDeleted || |
879 | needsOverloadResolutionForMoveConstructor()) && |
880 | "move constructor should not be deleted" ); |
881 | data().DefaultedMoveConstructorIsDeleted = true; |
882 | } |
883 | |
884 | /// Set that we attempted to declare an implicit destructor, |
885 | /// but overload resolution failed so we deleted it. |
886 | void setImplicitDestructorIsDeleted() { |
887 | assert((data().DefaultedDestructorIsDeleted || |
888 | needsOverloadResolutionForDestructor()) && |
889 | "destructor should not be deleted" ); |
890 | data().DefaultedDestructorIsDeleted = true; |
891 | } |
892 | |
893 | /// Determine whether this class should get an implicit move |
894 | /// constructor or if any existing special member function inhibits this. |
895 | bool needsImplicitMoveConstructor() const { |
896 | return !(data().DeclaredSpecialMembers & SMF_MoveConstructor) && |
897 | !hasUserDeclaredCopyConstructor() && |
898 | !hasUserDeclaredCopyAssignment() && |
899 | !hasUserDeclaredMoveAssignment() && |
900 | !hasUserDeclaredDestructor(); |
901 | } |
902 | |
903 | /// Determine whether we need to eagerly declare a defaulted move |
904 | /// constructor for this class. |
905 | bool needsOverloadResolutionForMoveConstructor() const { |
906 | return data().NeedOverloadResolutionForMoveConstructor; |
907 | } |
908 | |
909 | /// Determine whether this class has a user-declared copy assignment |
910 | /// operator. |
911 | /// |
912 | /// When false, a copy assignment operator will be implicitly declared. |
913 | bool hasUserDeclaredCopyAssignment() const { |
914 | return data().UserDeclaredSpecialMembers & SMF_CopyAssignment; |
915 | } |
916 | |
917 | /// Set that we attempted to declare an implicit copy assignment |
918 | /// operator, but overload resolution failed so we deleted it. |
919 | void setImplicitCopyAssignmentIsDeleted() { |
920 | assert((data().DefaultedCopyAssignmentIsDeleted || |
921 | needsOverloadResolutionForCopyAssignment()) && |
922 | "copy assignment should not be deleted" ); |
923 | data().DefaultedCopyAssignmentIsDeleted = true; |
924 | } |
925 | |
926 | /// Determine whether this class needs an implicit copy |
927 | /// assignment operator to be lazily declared. |
928 | bool needsImplicitCopyAssignment() const { |
929 | return !(data().DeclaredSpecialMembers & SMF_CopyAssignment); |
930 | } |
931 | |
932 | /// Determine whether we need to eagerly declare a defaulted copy |
933 | /// assignment operator for this class. |
934 | bool needsOverloadResolutionForCopyAssignment() const { |
935 | // C++20 [class.copy.assign]p2: |
936 | // If the class definition declares a move constructor or move assignment |
937 | // operator, the implicitly declared copy assignment operator is defined |
938 | // as deleted. |
939 | // In MSVC mode, sometimes a declared move constructor does not delete an |
940 | // implicit copy assignment, so defer this choice to Sema. |
941 | if (data().UserDeclaredSpecialMembers & |
942 | (SMF_MoveConstructor | SMF_MoveAssignment)) |
943 | return true; |
944 | return data().NeedOverloadResolutionForCopyAssignment; |
945 | } |
946 | |
947 | /// Determine whether an implicit copy assignment operator for this |
948 | /// type would have a parameter with a const-qualified reference type. |
949 | bool implicitCopyAssignmentHasConstParam() const { |
950 | return data().ImplicitCopyAssignmentHasConstParam; |
951 | } |
952 | |
953 | /// Determine whether this class has a copy assignment operator with |
954 | /// a parameter type which is a reference to a const-qualified type or is not |
955 | /// a reference. |
956 | bool hasCopyAssignmentWithConstParam() const { |
957 | return data().HasDeclaredCopyAssignmentWithConstParam || |
958 | (needsImplicitCopyAssignment() && |
959 | implicitCopyAssignmentHasConstParam()); |
960 | } |
961 | |
962 | /// Determine whether this class has had a move assignment |
963 | /// declared by the user. |
964 | bool hasUserDeclaredMoveAssignment() const { |
965 | return data().UserDeclaredSpecialMembers & SMF_MoveAssignment; |
966 | } |
967 | |
968 | /// Determine whether this class has a move assignment operator. |
969 | bool hasMoveAssignment() const { |
970 | return (data().DeclaredSpecialMembers & SMF_MoveAssignment) || |
971 | needsImplicitMoveAssignment(); |
972 | } |
973 | |
974 | /// Set that we attempted to declare an implicit move assignment |
975 | /// operator, but overload resolution failed so we deleted it. |
976 | void setImplicitMoveAssignmentIsDeleted() { |
977 | assert((data().DefaultedMoveAssignmentIsDeleted || |
978 | needsOverloadResolutionForMoveAssignment()) && |
979 | "move assignment should not be deleted" ); |
980 | data().DefaultedMoveAssignmentIsDeleted = true; |
981 | } |
982 | |
983 | /// Determine whether this class should get an implicit move |
984 | /// assignment operator or if any existing special member function inhibits |
985 | /// this. |
986 | bool needsImplicitMoveAssignment() const { |
987 | return !(data().DeclaredSpecialMembers & SMF_MoveAssignment) && |
988 | !hasUserDeclaredCopyConstructor() && |
989 | !hasUserDeclaredCopyAssignment() && |
990 | !hasUserDeclaredMoveConstructor() && |
991 | !hasUserDeclaredDestructor() && |
992 | (!isLambda() || lambdaIsDefaultConstructibleAndAssignable()); |
993 | } |
994 | |
995 | /// Determine whether we need to eagerly declare a move assignment |
996 | /// operator for this class. |
997 | bool needsOverloadResolutionForMoveAssignment() const { |
998 | return data().NeedOverloadResolutionForMoveAssignment; |
999 | } |
1000 | |
1001 | /// Determine whether this class has a user-declared destructor. |
1002 | /// |
1003 | /// When false, a destructor will be implicitly declared. |
1004 | bool hasUserDeclaredDestructor() const { |
1005 | return data().UserDeclaredSpecialMembers & SMF_Destructor; |
1006 | } |
1007 | |
1008 | /// Determine whether this class needs an implicit destructor to |
1009 | /// be lazily declared. |
1010 | bool needsImplicitDestructor() const { |
1011 | return !(data().DeclaredSpecialMembers & SMF_Destructor); |
1012 | } |
1013 | |
1014 | /// Determine whether we need to eagerly declare a destructor for this |
1015 | /// class. |
1016 | bool needsOverloadResolutionForDestructor() const { |
1017 | return data().NeedOverloadResolutionForDestructor; |
1018 | } |
1019 | |
1020 | /// Determine whether this class describes a lambda function object. |
1021 | bool isLambda() const { |
1022 | // An update record can't turn a non-lambda into a lambda. |
1023 | auto *DD = DefinitionData; |
1024 | return DD && DD->IsLambda; |
1025 | } |
1026 | |
1027 | /// Determine whether this class describes a generic |
1028 | /// lambda function object (i.e. function call operator is |
1029 | /// a template). |
1030 | bool isGenericLambda() const; |
1031 | |
1032 | /// Determine whether this lambda should have an implicit default constructor |
1033 | /// and copy and move assignment operators. |
1034 | bool lambdaIsDefaultConstructibleAndAssignable() const; |
1035 | |
1036 | /// Retrieve the lambda call operator of the closure type |
1037 | /// if this is a closure type. |
1038 | CXXMethodDecl *getLambdaCallOperator() const; |
1039 | |
1040 | /// Retrieve the dependent lambda call operator of the closure type |
1041 | /// if this is a templated closure type. |
1042 | FunctionTemplateDecl *getDependentLambdaCallOperator() const; |
1043 | |
1044 | /// Retrieve the lambda static invoker, the address of which |
1045 | /// is returned by the conversion operator, and the body of which |
1046 | /// is forwarded to the lambda call operator. The version that does not |
1047 | /// take a calling convention uses the 'default' calling convention for free |
1048 | /// functions if the Lambda's calling convention was not modified via |
1049 | /// attribute. Otherwise, it will return the calling convention specified for |
1050 | /// the lambda. |
1051 | CXXMethodDecl *getLambdaStaticInvoker() const; |
1052 | CXXMethodDecl *getLambdaStaticInvoker(CallingConv CC) const; |
1053 | |
1054 | /// Retrieve the generic lambda's template parameter list. |
1055 | /// Returns null if the class does not represent a lambda or a generic |
1056 | /// lambda. |
1057 | TemplateParameterList *getGenericLambdaTemplateParameterList() const; |
1058 | |
1059 | /// Retrieve the lambda template parameters that were specified explicitly. |
1060 | ArrayRef<NamedDecl *> getLambdaExplicitTemplateParameters() const; |
1061 | |
1062 | LambdaCaptureDefault getLambdaCaptureDefault() const { |
1063 | assert(isLambda()); |
1064 | return static_cast<LambdaCaptureDefault>(getLambdaData().CaptureDefault); |
1065 | } |
1066 | |
1067 | bool isCapturelessLambda() const { |
1068 | if (!isLambda()) |
1069 | return false; |
1070 | return getLambdaCaptureDefault() == LCD_None && capture_size() == 0; |
1071 | } |
1072 | |
1073 | /// Set the captures for this lambda closure type. |
1074 | void setCaptures(ASTContext &Context, ArrayRef<LambdaCapture> Captures); |
1075 | |
1076 | /// For a closure type, retrieve the mapping from captured |
1077 | /// variables and \c this to the non-static data members that store the |
1078 | /// values or references of the captures. |
1079 | /// |
1080 | /// \param Captures Will be populated with the mapping from captured |
1081 | /// variables to the corresponding fields. |
1082 | /// |
1083 | /// \param ThisCapture Will be set to the field declaration for the |
1084 | /// \c this capture. |
1085 | /// |
1086 | /// \note No entries will be added for init-captures, as they do not capture |
1087 | /// variables. |
1088 | /// |
1089 | /// \note If multiple versions of the lambda are merged together, they may |
1090 | /// have different variable declarations corresponding to the same capture. |
1091 | /// In that case, all of those variable declarations will be added to the |
1092 | /// Captures list, so it may have more than one variable listed per field. |
1093 | void |
1094 | getCaptureFields(llvm::DenseMap<const ValueDecl *, FieldDecl *> &Captures, |
1095 | FieldDecl *&ThisCapture) const; |
1096 | |
1097 | using capture_const_iterator = const LambdaCapture *; |
1098 | using capture_const_range = llvm::iterator_range<capture_const_iterator>; |
1099 | |
1100 | capture_const_range captures() const { |
1101 | return capture_const_range(captures_begin(), captures_end()); |
1102 | } |
1103 | |
1104 | capture_const_iterator captures_begin() const { |
1105 | if (!isLambda()) return nullptr; |
1106 | LambdaDefinitionData &LambdaData = getLambdaData(); |
1107 | return LambdaData.Captures.empty() ? nullptr : LambdaData.Captures.front(); |
1108 | } |
1109 | |
1110 | capture_const_iterator captures_end() const { |
1111 | return isLambda() ? captures_begin() + getLambdaData().NumCaptures |
1112 | : nullptr; |
1113 | } |
1114 | |
1115 | unsigned capture_size() const { return getLambdaData().NumCaptures; } |
1116 | |
1117 | const LambdaCapture *getCapture(unsigned I) const { |
1118 | assert(isLambda() && I < capture_size() && "invalid index for capture" ); |
1119 | return captures_begin() + I; |
1120 | } |
1121 | |
1122 | using conversion_iterator = UnresolvedSetIterator; |
1123 | |
1124 | conversion_iterator conversion_begin() const { |
1125 | return data().Conversions.get(C&: getASTContext()).begin(); |
1126 | } |
1127 | |
1128 | conversion_iterator conversion_end() const { |
1129 | return data().Conversions.get(C&: getASTContext()).end(); |
1130 | } |
1131 | |
1132 | /// Removes a conversion function from this class. The conversion |
1133 | /// function must currently be a member of this class. Furthermore, |
1134 | /// this class must currently be in the process of being defined. |
1135 | void removeConversion(const NamedDecl *Old); |
1136 | |
1137 | /// Get all conversion functions visible in current class, |
1138 | /// including conversion function templates. |
1139 | llvm::iterator_range<conversion_iterator> |
1140 | getVisibleConversionFunctions() const; |
1141 | |
1142 | /// Determine whether this class is an aggregate (C++ [dcl.init.aggr]), |
1143 | /// which is a class with no user-declared constructors, no private |
1144 | /// or protected non-static data members, no base classes, and no virtual |
1145 | /// functions (C++ [dcl.init.aggr]p1). |
1146 | bool isAggregate() const { return data().Aggregate; } |
1147 | |
1148 | /// Whether this class has any in-class initializers |
1149 | /// for non-static data members (including those in anonymous unions or |
1150 | /// structs). |
1151 | bool hasInClassInitializer() const { return data().HasInClassInitializer; } |
1152 | |
1153 | /// Whether this class or any of its subobjects has any members of |
1154 | /// reference type which would make value-initialization ill-formed. |
1155 | /// |
1156 | /// Per C++03 [dcl.init]p5: |
1157 | /// - if T is a non-union class type without a user-declared constructor, |
1158 | /// then every non-static data member and base-class component of T is |
1159 | /// value-initialized [...] A program that calls for [...] |
1160 | /// value-initialization of an entity of reference type is ill-formed. |
1161 | bool hasUninitializedReferenceMember() const { |
1162 | return !isUnion() && !hasUserDeclaredConstructor() && |
1163 | data().HasUninitializedReferenceMember; |
1164 | } |
1165 | |
1166 | /// Whether this class is a POD-type (C++ [class]p4) |
1167 | /// |
1168 | /// For purposes of this function a class is POD if it is an aggregate |
1169 | /// that has no non-static non-POD data members, no reference data |
1170 | /// members, no user-defined copy assignment operator and no |
1171 | /// user-defined destructor. |
1172 | /// |
1173 | /// Note that this is the C++ TR1 definition of POD. |
1174 | bool isPOD() const { return data().PlainOldData; } |
1175 | |
1176 | /// True if this class is C-like, without C++-specific features, e.g. |
1177 | /// it contains only public fields, no bases, tag kind is not 'class', etc. |
1178 | bool isCLike() const; |
1179 | |
1180 | /// Determine whether this is an empty class in the sense of |
1181 | /// (C++11 [meta.unary.prop]). |
1182 | /// |
1183 | /// The CXXRecordDecl is a class type, but not a union type, |
1184 | /// with no non-static data members other than bit-fields of length 0, |
1185 | /// no virtual member functions, no virtual base classes, |
1186 | /// and no base class B for which is_empty<B>::value is false. |
1187 | /// |
1188 | /// \note This does NOT include a check for union-ness. |
1189 | bool isEmpty() const { return data().Empty; } |
1190 | /// Marks this record as empty. This is used by DWARFASTParserClang |
1191 | /// when parsing records with empty fields having [[no_unique_address]] |
1192 | /// attribute |
1193 | void markEmpty() { data().Empty = true; } |
1194 | |
1195 | void setInitMethod(bool Val) { data().HasInitMethod = Val; } |
1196 | bool hasInitMethod() const { return data().HasInitMethod; } |
1197 | |
1198 | bool hasPrivateFields() const { |
1199 | return data().HasPrivateFields; |
1200 | } |
1201 | |
1202 | bool hasProtectedFields() const { |
1203 | return data().HasProtectedFields; |
1204 | } |
1205 | |
1206 | /// Determine whether this class has direct non-static data members. |
1207 | bool hasDirectFields() const { |
1208 | auto &D = data(); |
1209 | return D.HasPublicFields || D.HasProtectedFields || D.HasPrivateFields; |
1210 | } |
1211 | |
1212 | /// Whether this class is polymorphic (C++ [class.virtual]), |
1213 | /// which means that the class contains or inherits a virtual function. |
1214 | bool isPolymorphic() const { return data().Polymorphic; } |
1215 | |
1216 | /// Determine whether this class has a pure virtual function. |
1217 | /// |
1218 | /// The class is abstract per (C++ [class.abstract]p2) if it declares |
1219 | /// a pure virtual function or inherits a pure virtual function that is |
1220 | /// not overridden. |
1221 | bool isAbstract() const { return data().Abstract; } |
1222 | |
1223 | /// Determine whether this class is standard-layout per |
1224 | /// C++ [class]p7. |
1225 | bool isStandardLayout() const { return data().IsStandardLayout; } |
1226 | |
1227 | /// Determine whether this class was standard-layout per |
1228 | /// C++11 [class]p7, specifically using the C++11 rules without any DRs. |
1229 | bool isCXX11StandardLayout() const { return data().IsCXX11StandardLayout; } |
1230 | |
1231 | /// Determine whether this class, or any of its class subobjects, |
1232 | /// contains a mutable field. |
1233 | bool hasMutableFields() const { return data().HasMutableFields; } |
1234 | |
1235 | /// Determine whether this class has any variant members. |
1236 | bool hasVariantMembers() const { return data().HasVariantMembers; } |
1237 | |
1238 | /// Determine whether this class has a trivial default constructor |
1239 | /// (C++11 [class.ctor]p5). |
1240 | bool hasTrivialDefaultConstructor() const { |
1241 | return hasDefaultConstructor() && |
1242 | (data().HasTrivialSpecialMembers & SMF_DefaultConstructor); |
1243 | } |
1244 | |
1245 | /// Determine whether this class has a non-trivial default constructor |
1246 | /// (C++11 [class.ctor]p5). |
1247 | bool hasNonTrivialDefaultConstructor() const { |
1248 | return (data().DeclaredNonTrivialSpecialMembers & SMF_DefaultConstructor) || |
1249 | (needsImplicitDefaultConstructor() && |
1250 | !(data().HasTrivialSpecialMembers & SMF_DefaultConstructor)); |
1251 | } |
1252 | |
1253 | /// Determine whether this class has at least one constexpr constructor |
1254 | /// other than the copy or move constructors. |
1255 | bool hasConstexprNonCopyMoveConstructor() const { |
1256 | return data().HasConstexprNonCopyMoveConstructor || |
1257 | (needsImplicitDefaultConstructor() && |
1258 | defaultedDefaultConstructorIsConstexpr()); |
1259 | } |
1260 | |
1261 | /// Determine whether a defaulted default constructor for this class |
1262 | /// would be constexpr. |
1263 | bool defaultedDefaultConstructorIsConstexpr() const { |
1264 | return data().DefaultedDefaultConstructorIsConstexpr && |
1265 | (!isUnion() || hasInClassInitializer() || !hasVariantMembers() || |
1266 | getLangOpts().CPlusPlus20); |
1267 | } |
1268 | |
1269 | /// Determine whether this class has a constexpr default constructor. |
1270 | bool hasConstexprDefaultConstructor() const { |
1271 | return data().HasConstexprDefaultConstructor || |
1272 | (needsImplicitDefaultConstructor() && |
1273 | defaultedDefaultConstructorIsConstexpr()); |
1274 | } |
1275 | |
1276 | /// Determine whether this class has a trivial copy constructor |
1277 | /// (C++ [class.copy]p6, C++11 [class.copy]p12) |
1278 | bool hasTrivialCopyConstructor() const { |
1279 | return data().HasTrivialSpecialMembers & SMF_CopyConstructor; |
1280 | } |
1281 | |
1282 | bool hasTrivialCopyConstructorForCall() const { |
1283 | return data().HasTrivialSpecialMembersForCall & SMF_CopyConstructor; |
1284 | } |
1285 | |
1286 | /// Determine whether this class has a non-trivial copy constructor |
1287 | /// (C++ [class.copy]p6, C++11 [class.copy]p12) |
1288 | bool hasNonTrivialCopyConstructor() const { |
1289 | return data().DeclaredNonTrivialSpecialMembers & SMF_CopyConstructor || |
1290 | !hasTrivialCopyConstructor(); |
1291 | } |
1292 | |
1293 | bool hasNonTrivialCopyConstructorForCall() const { |
1294 | return (data().DeclaredNonTrivialSpecialMembersForCall & |
1295 | SMF_CopyConstructor) || |
1296 | !hasTrivialCopyConstructorForCall(); |
1297 | } |
1298 | |
1299 | /// Determine whether this class has a trivial move constructor |
1300 | /// (C++11 [class.copy]p12) |
1301 | bool hasTrivialMoveConstructor() const { |
1302 | return hasMoveConstructor() && |
1303 | (data().HasTrivialSpecialMembers & SMF_MoveConstructor); |
1304 | } |
1305 | |
1306 | bool hasTrivialMoveConstructorForCall() const { |
1307 | return hasMoveConstructor() && |
1308 | (data().HasTrivialSpecialMembersForCall & SMF_MoveConstructor); |
1309 | } |
1310 | |
1311 | /// Determine whether this class has a non-trivial move constructor |
1312 | /// (C++11 [class.copy]p12) |
1313 | bool hasNonTrivialMoveConstructor() const { |
1314 | return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveConstructor) || |
1315 | (needsImplicitMoveConstructor() && |
1316 | !(data().HasTrivialSpecialMembers & SMF_MoveConstructor)); |
1317 | } |
1318 | |
1319 | bool hasNonTrivialMoveConstructorForCall() const { |
1320 | return (data().DeclaredNonTrivialSpecialMembersForCall & |
1321 | SMF_MoveConstructor) || |
1322 | (needsImplicitMoveConstructor() && |
1323 | !(data().HasTrivialSpecialMembersForCall & SMF_MoveConstructor)); |
1324 | } |
1325 | |
1326 | /// Determine whether this class has a trivial copy assignment operator |
1327 | /// (C++ [class.copy]p11, C++11 [class.copy]p25) |
1328 | bool hasTrivialCopyAssignment() const { |
1329 | return data().HasTrivialSpecialMembers & SMF_CopyAssignment; |
1330 | } |
1331 | |
1332 | /// Determine whether this class has a non-trivial copy assignment |
1333 | /// operator (C++ [class.copy]p11, C++11 [class.copy]p25) |
1334 | bool hasNonTrivialCopyAssignment() const { |
1335 | return data().DeclaredNonTrivialSpecialMembers & SMF_CopyAssignment || |
1336 | !hasTrivialCopyAssignment(); |
1337 | } |
1338 | |
1339 | /// Determine whether this class has a trivial move assignment operator |
1340 | /// (C++11 [class.copy]p25) |
1341 | bool hasTrivialMoveAssignment() const { |
1342 | return hasMoveAssignment() && |
1343 | (data().HasTrivialSpecialMembers & SMF_MoveAssignment); |
1344 | } |
1345 | |
1346 | /// Determine whether this class has a non-trivial move assignment |
1347 | /// operator (C++11 [class.copy]p25) |
1348 | bool hasNonTrivialMoveAssignment() const { |
1349 | return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveAssignment) || |
1350 | (needsImplicitMoveAssignment() && |
1351 | !(data().HasTrivialSpecialMembers & SMF_MoveAssignment)); |
1352 | } |
1353 | |
1354 | /// Determine whether a defaulted default constructor for this class |
1355 | /// would be constexpr. |
1356 | bool defaultedDestructorIsConstexpr() const { |
1357 | return data().DefaultedDestructorIsConstexpr && |
1358 | getLangOpts().CPlusPlus20; |
1359 | } |
1360 | |
1361 | /// Determine whether this class has a constexpr destructor. |
1362 | bool hasConstexprDestructor() const; |
1363 | |
1364 | /// Determine whether this class has a trivial destructor |
1365 | /// (C++ [class.dtor]p3) |
1366 | bool hasTrivialDestructor() const { |
1367 | return data().HasTrivialSpecialMembers & SMF_Destructor; |
1368 | } |
1369 | |
1370 | bool hasTrivialDestructorForCall() const { |
1371 | return data().HasTrivialSpecialMembersForCall & SMF_Destructor; |
1372 | } |
1373 | |
1374 | /// Determine whether this class has a non-trivial destructor |
1375 | /// (C++ [class.dtor]p3) |
1376 | bool hasNonTrivialDestructor() const { |
1377 | return !(data().HasTrivialSpecialMembers & SMF_Destructor); |
1378 | } |
1379 | |
1380 | bool hasNonTrivialDestructorForCall() const { |
1381 | return !(data().HasTrivialSpecialMembersForCall & SMF_Destructor); |
1382 | } |
1383 | |
1384 | void setHasTrivialSpecialMemberForCall() { |
1385 | data().HasTrivialSpecialMembersForCall = |
1386 | (SMF_CopyConstructor | SMF_MoveConstructor | SMF_Destructor); |
1387 | } |
1388 | |
1389 | /// Determine whether declaring a const variable with this type is ok |
1390 | /// per core issue 253. |
1391 | bool allowConstDefaultInit() const { |
1392 | return !data().HasUninitializedFields || |
1393 | !(data().HasDefaultedDefaultConstructor || |
1394 | needsImplicitDefaultConstructor()); |
1395 | } |
1396 | |
1397 | /// Determine whether this class has a destructor which has no |
1398 | /// semantic effect. |
1399 | /// |
1400 | /// Any such destructor will be trivial, public, defaulted and not deleted, |
1401 | /// and will call only irrelevant destructors. |
1402 | bool hasIrrelevantDestructor() const { |
1403 | return data().HasIrrelevantDestructor; |
1404 | } |
1405 | |
1406 | /// Determine whether this class has a non-literal or/ volatile type |
1407 | /// non-static data member or base class. |
1408 | bool hasNonLiteralTypeFieldsOrBases() const { |
1409 | return data().HasNonLiteralTypeFieldsOrBases; |
1410 | } |
1411 | |
1412 | /// Determine whether this class has a using-declaration that names |
1413 | /// a user-declared base class constructor. |
1414 | bool hasInheritedConstructor() const { |
1415 | return data().HasInheritedConstructor; |
1416 | } |
1417 | |
1418 | /// Determine whether this class has a using-declaration that names |
1419 | /// a base class assignment operator. |
1420 | bool hasInheritedAssignment() const { |
1421 | return data().HasInheritedAssignment; |
1422 | } |
1423 | |
1424 | /// Determine whether this class is considered trivially copyable per |
1425 | /// (C++11 [class]p6). |
1426 | bool isTriviallyCopyable() const; |
1427 | |
1428 | /// Determine whether this class is considered trivially copyable per |
1429 | bool isTriviallyCopyConstructible() const; |
1430 | |
1431 | /// Determine whether this class is considered trivial. |
1432 | /// |
1433 | /// C++11 [class]p6: |
1434 | /// "A trivial class is a class that has a trivial default constructor and |
1435 | /// is trivially copyable." |
1436 | bool isTrivial() const { |
1437 | return isTriviallyCopyable() && hasTrivialDefaultConstructor(); |
1438 | } |
1439 | |
1440 | /// Determine whether this class is a literal type. |
1441 | /// |
1442 | /// C++20 [basic.types]p10: |
1443 | /// A class type that has all the following properties: |
1444 | /// - it has a constexpr destructor |
1445 | /// - all of its non-static non-variant data members and base classes |
1446 | /// are of non-volatile literal types, and it: |
1447 | /// - is a closure type |
1448 | /// - is an aggregate union type that has either no variant members |
1449 | /// or at least one variant member of non-volatile literal type |
1450 | /// - is a non-union aggregate type for which each of its anonymous |
1451 | /// union members satisfies the above requirements for an aggregate |
1452 | /// union type, or |
1453 | /// - has at least one constexpr constructor or constructor template |
1454 | /// that is not a copy or move constructor. |
1455 | bool isLiteral() const; |
1456 | |
1457 | /// Determine whether this is a structural type. |
1458 | bool isStructural() const { |
1459 | return isLiteral() && data().StructuralIfLiteral; |
1460 | } |
1461 | |
1462 | /// Notify the class that this destructor is now selected. |
1463 | /// |
1464 | /// Important properties of the class depend on destructor properties. Since |
1465 | /// C++20, it is possible to have multiple destructor declarations in a class |
1466 | /// out of which one will be selected at the end. |
1467 | /// This is called separately from addedMember because it has to be deferred |
1468 | /// to the completion of the class. |
1469 | void addedSelectedDestructor(CXXDestructorDecl *DD); |
1470 | |
1471 | /// Notify the class that an eligible SMF has been added. |
1472 | /// This updates triviality and destructor based properties of the class accordingly. |
1473 | void addedEligibleSpecialMemberFunction(const CXXMethodDecl *MD, unsigned SMKind); |
1474 | |
1475 | /// If this record is an instantiation of a member class, |
1476 | /// retrieves the member class from which it was instantiated. |
1477 | /// |
1478 | /// This routine will return non-null for (non-templated) member |
1479 | /// classes of class templates. For example, given: |
1480 | /// |
1481 | /// \code |
1482 | /// template<typename T> |
1483 | /// struct X { |
1484 | /// struct A { }; |
1485 | /// }; |
1486 | /// \endcode |
1487 | /// |
1488 | /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl |
1489 | /// whose parent is the class template specialization X<int>. For |
1490 | /// this declaration, getInstantiatedFromMemberClass() will return |
1491 | /// the CXXRecordDecl X<T>::A. When a complete definition of |
1492 | /// X<int>::A is required, it will be instantiated from the |
1493 | /// declaration returned by getInstantiatedFromMemberClass(). |
1494 | CXXRecordDecl *getInstantiatedFromMemberClass() const; |
1495 | |
1496 | /// If this class is an instantiation of a member class of a |
1497 | /// class template specialization, retrieves the member specialization |
1498 | /// information. |
1499 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
1500 | |
1501 | /// Specify that this record is an instantiation of the |
1502 | /// member class \p RD. |
1503 | void setInstantiationOfMemberClass(CXXRecordDecl *RD, |
1504 | TemplateSpecializationKind TSK); |
1505 | |
1506 | /// Retrieves the class template that is described by this |
1507 | /// class declaration. |
1508 | /// |
1509 | /// Every class template is represented as a ClassTemplateDecl and a |
1510 | /// CXXRecordDecl. The former contains template properties (such as |
1511 | /// the template parameter lists) while the latter contains the |
1512 | /// actual description of the template's |
1513 | /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the |
1514 | /// CXXRecordDecl that from a ClassTemplateDecl, while |
1515 | /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from |
1516 | /// a CXXRecordDecl. |
1517 | ClassTemplateDecl *getDescribedClassTemplate() const; |
1518 | |
1519 | void setDescribedClassTemplate(ClassTemplateDecl *Template); |
1520 | |
1521 | /// Determine whether this particular class is a specialization or |
1522 | /// instantiation of a class template or member class of a class template, |
1523 | /// and how it was instantiated or specialized. |
1524 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
1525 | |
1526 | /// Set the kind of specialization or template instantiation this is. |
1527 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK); |
1528 | |
1529 | /// Retrieve the record declaration from which this record could be |
1530 | /// instantiated. Returns null if this class is not a template instantiation. |
1531 | const CXXRecordDecl *getTemplateInstantiationPattern() const; |
1532 | |
1533 | CXXRecordDecl *getTemplateInstantiationPattern() { |
1534 | return const_cast<CXXRecordDecl *>(const_cast<const CXXRecordDecl *>(this) |
1535 | ->getTemplateInstantiationPattern()); |
1536 | } |
1537 | |
1538 | /// Returns the destructor decl for this class. |
1539 | CXXDestructorDecl *getDestructor() const; |
1540 | |
1541 | /// Returns true if the class destructor, or any implicitly invoked |
1542 | /// destructors are marked noreturn. |
1543 | bool isAnyDestructorNoReturn() const { return data().IsAnyDestructorNoReturn; } |
1544 | |
1545 | /// If the class is a local class [class.local], returns |
1546 | /// the enclosing function declaration. |
1547 | const FunctionDecl *isLocalClass() const { |
1548 | if (const auto *RD = dyn_cast<CXXRecordDecl>(getDeclContext())) |
1549 | return RD->isLocalClass(); |
1550 | |
1551 | return dyn_cast<FunctionDecl>(getDeclContext()); |
1552 | } |
1553 | |
1554 | FunctionDecl *isLocalClass() { |
1555 | return const_cast<FunctionDecl*>( |
1556 | const_cast<const CXXRecordDecl*>(this)->isLocalClass()); |
1557 | } |
1558 | |
1559 | /// Determine whether this dependent class is a current instantiation, |
1560 | /// when viewed from within the given context. |
1561 | bool isCurrentInstantiation(const DeclContext *CurContext) const; |
1562 | |
1563 | /// Determine whether this class is derived from the class \p Base. |
1564 | /// |
1565 | /// This routine only determines whether this class is derived from \p Base, |
1566 | /// but does not account for factors that may make a Derived -> Base class |
1567 | /// ill-formed, such as private/protected inheritance or multiple, ambiguous |
1568 | /// base class subobjects. |
1569 | /// |
1570 | /// \param Base the base class we are searching for. |
1571 | /// |
1572 | /// \returns true if this class is derived from Base, false otherwise. |
1573 | bool isDerivedFrom(const CXXRecordDecl *Base) const; |
1574 | |
1575 | /// Determine whether this class is derived from the type \p Base. |
1576 | /// |
1577 | /// This routine only determines whether this class is derived from \p Base, |
1578 | /// but does not account for factors that may make a Derived -> Base class |
1579 | /// ill-formed, such as private/protected inheritance or multiple, ambiguous |
1580 | /// base class subobjects. |
1581 | /// |
1582 | /// \param Base the base class we are searching for. |
1583 | /// |
1584 | /// \param Paths will contain the paths taken from the current class to the |
1585 | /// given \p Base class. |
1586 | /// |
1587 | /// \returns true if this class is derived from \p Base, false otherwise. |
1588 | /// |
1589 | /// \todo add a separate parameter to configure IsDerivedFrom, rather than |
1590 | /// tangling input and output in \p Paths |
1591 | bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const; |
1592 | |
1593 | /// Determine whether this class is virtually derived from |
1594 | /// the class \p Base. |
1595 | /// |
1596 | /// This routine only determines whether this class is virtually |
1597 | /// derived from \p Base, but does not account for factors that may |
1598 | /// make a Derived -> Base class ill-formed, such as |
1599 | /// private/protected inheritance or multiple, ambiguous base class |
1600 | /// subobjects. |
1601 | /// |
1602 | /// \param Base the base class we are searching for. |
1603 | /// |
1604 | /// \returns true if this class is virtually derived from Base, |
1605 | /// false otherwise. |
1606 | bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const; |
1607 | |
1608 | /// Determine whether this class is provably not derived from |
1609 | /// the type \p Base. |
1610 | bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const; |
1611 | |
1612 | /// Function type used by forallBases() as a callback. |
1613 | /// |
1614 | /// \param BaseDefinition the definition of the base class |
1615 | /// |
1616 | /// \returns true if this base matched the search criteria |
1617 | using ForallBasesCallback = |
1618 | llvm::function_ref<bool(const CXXRecordDecl *BaseDefinition)>; |
1619 | |
1620 | /// Determines if the given callback holds for all the direct |
1621 | /// or indirect base classes of this type. |
1622 | /// |
1623 | /// The class itself does not count as a base class. This routine |
1624 | /// returns false if the class has non-computable base classes. |
1625 | /// |
1626 | /// \param BaseMatches Callback invoked for each (direct or indirect) base |
1627 | /// class of this type until a call returns false. |
1628 | bool forallBases(ForallBasesCallback BaseMatches) const; |
1629 | |
1630 | /// Function type used by lookupInBases() to determine whether a |
1631 | /// specific base class subobject matches the lookup criteria. |
1632 | /// |
1633 | /// \param Specifier the base-class specifier that describes the inheritance |
1634 | /// from the base class we are trying to match. |
1635 | /// |
1636 | /// \param Path the current path, from the most-derived class down to the |
1637 | /// base named by the \p Specifier. |
1638 | /// |
1639 | /// \returns true if this base matched the search criteria, false otherwise. |
1640 | using BaseMatchesCallback = |
1641 | llvm::function_ref<bool(const CXXBaseSpecifier *Specifier, |
1642 | CXXBasePath &Path)>; |
1643 | |
1644 | /// Look for entities within the base classes of this C++ class, |
1645 | /// transitively searching all base class subobjects. |
1646 | /// |
1647 | /// This routine uses the callback function \p BaseMatches to find base |
1648 | /// classes meeting some search criteria, walking all base class subobjects |
1649 | /// and populating the given \p Paths structure with the paths through the |
1650 | /// inheritance hierarchy that resulted in a match. On a successful search, |
1651 | /// the \p Paths structure can be queried to retrieve the matching paths and |
1652 | /// to determine if there were any ambiguities. |
1653 | /// |
1654 | /// \param BaseMatches callback function used to determine whether a given |
1655 | /// base matches the user-defined search criteria. |
1656 | /// |
1657 | /// \param Paths used to record the paths from this class to its base class |
1658 | /// subobjects that match the search criteria. |
1659 | /// |
1660 | /// \param LookupInDependent can be set to true to extend the search to |
1661 | /// dependent base classes. |
1662 | /// |
1663 | /// \returns true if there exists any path from this class to a base class |
1664 | /// subobject that matches the search criteria. |
1665 | bool lookupInBases(BaseMatchesCallback BaseMatches, CXXBasePaths &Paths, |
1666 | bool LookupInDependent = false) const; |
1667 | |
1668 | /// Base-class lookup callback that determines whether the given |
1669 | /// base class specifier refers to a specific class declaration. |
1670 | /// |
1671 | /// This callback can be used with \c lookupInBases() to determine whether |
1672 | /// a given derived class has is a base class subobject of a particular type. |
1673 | /// The base record pointer should refer to the canonical CXXRecordDecl of the |
1674 | /// base class that we are searching for. |
1675 | static bool FindBaseClass(const CXXBaseSpecifier *Specifier, |
1676 | CXXBasePath &Path, const CXXRecordDecl *BaseRecord); |
1677 | |
1678 | /// Base-class lookup callback that determines whether the |
1679 | /// given base class specifier refers to a specific class |
1680 | /// declaration and describes virtual derivation. |
1681 | /// |
1682 | /// This callback can be used with \c lookupInBases() to determine |
1683 | /// whether a given derived class has is a virtual base class |
1684 | /// subobject of a particular type. The base record pointer should |
1685 | /// refer to the canonical CXXRecordDecl of the base class that we |
1686 | /// are searching for. |
1687 | static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, |
1688 | CXXBasePath &Path, |
1689 | const CXXRecordDecl *BaseRecord); |
1690 | |
1691 | /// Retrieve the final overriders for each virtual member |
1692 | /// function in the class hierarchy where this class is the |
1693 | /// most-derived class in the class hierarchy. |
1694 | void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const; |
1695 | |
1696 | /// Get the indirect primary bases for this class. |
1697 | void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const; |
1698 | |
1699 | /// Determine whether this class has a member with the given name, possibly |
1700 | /// in a non-dependent base class. |
1701 | /// |
1702 | /// No check for ambiguity is performed, so this should never be used when |
1703 | /// implementing language semantics, but it may be appropriate for warnings, |
1704 | /// static analysis, or similar. |
1705 | bool hasMemberName(DeclarationName N) const; |
1706 | |
1707 | /// Performs an imprecise lookup of a dependent name in this class. |
1708 | /// |
1709 | /// This function does not follow strict semantic rules and should be used |
1710 | /// only when lookup rules can be relaxed, e.g. indexing. |
1711 | std::vector<const NamedDecl *> |
1712 | lookupDependentName(DeclarationName Name, |
1713 | llvm::function_ref<bool(const NamedDecl *ND)> Filter); |
1714 | |
1715 | /// Renders and displays an inheritance diagram |
1716 | /// for this C++ class and all of its base classes (transitively) using |
1717 | /// GraphViz. |
1718 | void viewInheritance(ASTContext& Context) const; |
1719 | |
1720 | /// Calculates the access of a decl that is reached |
1721 | /// along a path. |
1722 | static AccessSpecifier MergeAccess(AccessSpecifier PathAccess, |
1723 | AccessSpecifier DeclAccess) { |
1724 | assert(DeclAccess != AS_none); |
1725 | if (DeclAccess == AS_private) return AS_none; |
1726 | return (PathAccess > DeclAccess ? PathAccess : DeclAccess); |
1727 | } |
1728 | |
1729 | /// Indicates that the declaration of a defaulted or deleted special |
1730 | /// member function is now complete. |
1731 | void finishedDefaultedOrDeletedMember(CXXMethodDecl *MD); |
1732 | |
1733 | void setTrivialForCallFlags(CXXMethodDecl *MD); |
1734 | |
1735 | /// Indicates that the definition of this class is now complete. |
1736 | void completeDefinition() override; |
1737 | |
1738 | /// Indicates that the definition of this class is now complete, |
1739 | /// and provides a final overrider map to help determine |
1740 | /// |
1741 | /// \param FinalOverriders The final overrider map for this class, which can |
1742 | /// be provided as an optimization for abstract-class checking. If NULL, |
1743 | /// final overriders will be computed if they are needed to complete the |
1744 | /// definition. |
1745 | void completeDefinition(CXXFinalOverriderMap *FinalOverriders); |
1746 | |
1747 | /// Determine whether this class may end up being abstract, even though |
1748 | /// it is not yet known to be abstract. |
1749 | /// |
1750 | /// \returns true if this class is not known to be abstract but has any |
1751 | /// base classes that are abstract. In this case, \c completeDefinition() |
1752 | /// will need to compute final overriders to determine whether the class is |
1753 | /// actually abstract. |
1754 | bool mayBeAbstract() const; |
1755 | |
1756 | /// Determine whether it's impossible for a class to be derived from this |
1757 | /// class. This is best-effort, and may conservatively return false. |
1758 | bool isEffectivelyFinal() const; |
1759 | |
1760 | /// If this is the closure type of a lambda expression, retrieve the |
1761 | /// number to be used for name mangling in the Itanium C++ ABI. |
1762 | /// |
1763 | /// Zero indicates that this closure type has internal linkage, so the |
1764 | /// mangling number does not matter, while a non-zero value indicates which |
1765 | /// lambda expression this is in this particular context. |
1766 | unsigned getLambdaManglingNumber() const { |
1767 | assert(isLambda() && "Not a lambda closure type!" ); |
1768 | return getLambdaData().ManglingNumber; |
1769 | } |
1770 | |
1771 | /// The lambda is known to has internal linkage no matter whether it has name |
1772 | /// mangling number. |
1773 | bool hasKnownLambdaInternalLinkage() const { |
1774 | assert(isLambda() && "Not a lambda closure type!" ); |
1775 | return getLambdaData().HasKnownInternalLinkage; |
1776 | } |
1777 | |
1778 | /// Retrieve the declaration that provides additional context for a |
1779 | /// lambda, when the normal declaration context is not specific enough. |
1780 | /// |
1781 | /// Certain contexts (default arguments of in-class function parameters and |
1782 | /// the initializers of data members) have separate name mangling rules for |
1783 | /// lambdas within the Itanium C++ ABI. For these cases, this routine provides |
1784 | /// the declaration in which the lambda occurs, e.g., the function parameter |
1785 | /// or the non-static data member. Otherwise, it returns NULL to imply that |
1786 | /// the declaration context suffices. |
1787 | Decl *getLambdaContextDecl() const; |
1788 | |
1789 | /// Retrieve the index of this lambda within the context declaration returned |
1790 | /// by getLambdaContextDecl(). |
1791 | unsigned getLambdaIndexInContext() const { |
1792 | assert(isLambda() && "Not a lambda closure type!" ); |
1793 | return getLambdaData().IndexInContext; |
1794 | } |
1795 | |
1796 | /// Information about how a lambda is numbered within its context. |
1797 | struct LambdaNumbering { |
1798 | Decl *ContextDecl = nullptr; |
1799 | unsigned IndexInContext = 0; |
1800 | unsigned ManglingNumber = 0; |
1801 | unsigned DeviceManglingNumber = 0; |
1802 | bool HasKnownInternalLinkage = false; |
1803 | }; |
1804 | |
1805 | /// Set the mangling numbers and context declaration for a lambda class. |
1806 | void setLambdaNumbering(LambdaNumbering Numbering); |
1807 | |
1808 | // Get the mangling numbers and context declaration for a lambda class. |
1809 | LambdaNumbering getLambdaNumbering() const { |
1810 | return {.ContextDecl: getLambdaContextDecl(), .IndexInContext: getLambdaIndexInContext(), |
1811 | .ManglingNumber: getLambdaManglingNumber(), .DeviceManglingNumber: getDeviceLambdaManglingNumber(), |
1812 | .HasKnownInternalLinkage: hasKnownLambdaInternalLinkage()}; |
1813 | } |
1814 | |
1815 | /// Retrieve the device side mangling number. |
1816 | unsigned getDeviceLambdaManglingNumber() const; |
1817 | |
1818 | /// Returns the inheritance model used for this record. |
1819 | MSInheritanceModel getMSInheritanceModel() const; |
1820 | |
1821 | /// Calculate what the inheritance model would be for this class. |
1822 | MSInheritanceModel calculateInheritanceModel() const; |
1823 | |
1824 | /// In the Microsoft C++ ABI, use zero for the field offset of a null data |
1825 | /// member pointer if we can guarantee that zero is not a valid field offset, |
1826 | /// or if the member pointer has multiple fields. Polymorphic classes have a |
1827 | /// vfptr at offset zero, so we can use zero for null. If there are multiple |
1828 | /// fields, we can use zero even if it is a valid field offset because |
1829 | /// null-ness testing will check the other fields. |
1830 | bool nullFieldOffsetIsZero() const; |
1831 | |
1832 | /// Controls when vtordisps will be emitted if this record is used as a |
1833 | /// virtual base. |
1834 | MSVtorDispMode getMSVtorDispMode() const; |
1835 | |
1836 | /// Determine whether this lambda expression was known to be dependent |
1837 | /// at the time it was created, even if its context does not appear to be |
1838 | /// dependent. |
1839 | /// |
1840 | /// This flag is a workaround for an issue with parsing, where default |
1841 | /// arguments are parsed before their enclosing function declarations have |
1842 | /// been created. This means that any lambda expressions within those |
1843 | /// default arguments will have as their DeclContext the context enclosing |
1844 | /// the function declaration, which may be non-dependent even when the |
1845 | /// function declaration itself is dependent. This flag indicates when we |
1846 | /// know that the lambda is dependent despite that. |
1847 | bool isDependentLambda() const { |
1848 | return isLambda() && getLambdaData().DependencyKind == LDK_AlwaysDependent; |
1849 | } |
1850 | |
1851 | bool isNeverDependentLambda() const { |
1852 | return isLambda() && getLambdaData().DependencyKind == LDK_NeverDependent; |
1853 | } |
1854 | |
1855 | unsigned getLambdaDependencyKind() const { |
1856 | if (!isLambda()) |
1857 | return LDK_Unknown; |
1858 | return getLambdaData().DependencyKind; |
1859 | } |
1860 | |
1861 | TypeSourceInfo *getLambdaTypeInfo() const { |
1862 | return getLambdaData().MethodTyInfo; |
1863 | } |
1864 | |
1865 | void setLambdaTypeInfo(TypeSourceInfo *TS) { |
1866 | assert(DefinitionData && DefinitionData->IsLambda && |
1867 | "setting lambda property of non-lambda class" ); |
1868 | auto &DL = static_cast<LambdaDefinitionData &>(*DefinitionData); |
1869 | DL.MethodTyInfo = TS; |
1870 | } |
1871 | |
1872 | void setLambdaDependencyKind(unsigned Kind) { |
1873 | getLambdaData().DependencyKind = Kind; |
1874 | } |
1875 | |
1876 | void setLambdaIsGeneric(bool IsGeneric) { |
1877 | assert(DefinitionData && DefinitionData->IsLambda && |
1878 | "setting lambda property of non-lambda class" ); |
1879 | auto &DL = static_cast<LambdaDefinitionData &>(*DefinitionData); |
1880 | DL.IsGenericLambda = IsGeneric; |
1881 | } |
1882 | |
1883 | // Determine whether this type is an Interface Like type for |
1884 | // __interface inheritance purposes. |
1885 | bool isInterfaceLike() const; |
1886 | |
1887 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1888 | static bool classofKind(Kind K) { |
1889 | return K >= firstCXXRecord && K <= lastCXXRecord; |
1890 | } |
1891 | void markAbstract() { data().Abstract = true; } |
1892 | }; |
1893 | |
1894 | /// Store information needed for an explicit specifier. |
1895 | /// Used by CXXDeductionGuideDecl, CXXConstructorDecl and CXXConversionDecl. |
1896 | class ExplicitSpecifier { |
1897 | llvm::PointerIntPair<Expr *, 2, ExplicitSpecKind> ExplicitSpec{ |
1898 | nullptr, ExplicitSpecKind::ResolvedFalse}; |
1899 | |
1900 | public: |
1901 | ExplicitSpecifier() = default; |
1902 | ExplicitSpecifier(Expr *Expression, ExplicitSpecKind Kind) |
1903 | : ExplicitSpec(Expression, Kind) {} |
1904 | ExplicitSpecKind getKind() const { return ExplicitSpec.getInt(); } |
1905 | const Expr *getExpr() const { return ExplicitSpec.getPointer(); } |
1906 | Expr *getExpr() { return ExplicitSpec.getPointer(); } |
1907 | |
1908 | /// Determine if the declaration had an explicit specifier of any kind. |
1909 | bool isSpecified() const { |
1910 | return ExplicitSpec.getInt() != ExplicitSpecKind::ResolvedFalse || |
1911 | ExplicitSpec.getPointer(); |
1912 | } |
1913 | |
1914 | /// Check for equivalence of explicit specifiers. |
1915 | /// \return true if the explicit specifier are equivalent, false otherwise. |
1916 | bool isEquivalent(const ExplicitSpecifier Other) const; |
1917 | /// Determine whether this specifier is known to correspond to an explicit |
1918 | /// declaration. Returns false if the specifier is absent or has an |
1919 | /// expression that is value-dependent or evaluates to false. |
1920 | bool isExplicit() const { |
1921 | return ExplicitSpec.getInt() == ExplicitSpecKind::ResolvedTrue; |
1922 | } |
1923 | /// Determine if the explicit specifier is invalid. |
1924 | /// This state occurs after a substitution failures. |
1925 | bool isInvalid() const { |
1926 | return ExplicitSpec.getInt() == ExplicitSpecKind::Unresolved && |
1927 | !ExplicitSpec.getPointer(); |
1928 | } |
1929 | void setKind(ExplicitSpecKind Kind) { ExplicitSpec.setInt(Kind); } |
1930 | void setExpr(Expr *E) { ExplicitSpec.setPointer(E); } |
1931 | // Retrieve the explicit specifier in the given declaration, if any. |
1932 | static ExplicitSpecifier getFromDecl(FunctionDecl *Function); |
1933 | static const ExplicitSpecifier getFromDecl(const FunctionDecl *Function) { |
1934 | return getFromDecl(Function: const_cast<FunctionDecl *>(Function)); |
1935 | } |
1936 | static ExplicitSpecifier Invalid() { |
1937 | return ExplicitSpecifier(nullptr, ExplicitSpecKind::Unresolved); |
1938 | } |
1939 | }; |
1940 | |
1941 | /// Represents a C++ deduction guide declaration. |
1942 | /// |
1943 | /// \code |
1944 | /// template<typename T> struct A { A(); A(T); }; |
1945 | /// A() -> A<int>; |
1946 | /// \endcode |
1947 | /// |
1948 | /// In this example, there will be an explicit deduction guide from the |
1949 | /// second line, and implicit deduction guide templates synthesized from |
1950 | /// the constructors of \c A. |
1951 | class CXXDeductionGuideDecl : public FunctionDecl { |
1952 | void anchor() override; |
1953 | |
1954 | private: |
1955 | CXXDeductionGuideDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1956 | ExplicitSpecifier ES, |
1957 | const DeclarationNameInfo &NameInfo, QualType T, |
1958 | TypeSourceInfo *TInfo, SourceLocation EndLocation, |
1959 | CXXConstructorDecl *Ctor, DeductionCandidate Kind) |
1960 | : FunctionDecl(CXXDeductionGuide, C, DC, StartLoc, NameInfo, T, TInfo, |
1961 | SC_None, false, false, ConstexprSpecKind::Unspecified), |
1962 | Ctor(Ctor), ExplicitSpec(ES) { |
1963 | if (EndLocation.isValid()) |
1964 | setRangeEnd(EndLocation); |
1965 | setDeductionCandidateKind(Kind); |
1966 | } |
1967 | |
1968 | CXXConstructorDecl *Ctor; |
1969 | ExplicitSpecifier ExplicitSpec; |
1970 | void setExplicitSpecifier(ExplicitSpecifier ES) { ExplicitSpec = ES; } |
1971 | |
1972 | public: |
1973 | friend class ASTDeclReader; |
1974 | friend class ASTDeclWriter; |
1975 | |
1976 | static CXXDeductionGuideDecl * |
1977 | Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1978 | ExplicitSpecifier ES, const DeclarationNameInfo &NameInfo, QualType T, |
1979 | TypeSourceInfo *TInfo, SourceLocation EndLocation, |
1980 | CXXConstructorDecl *Ctor = nullptr, |
1981 | DeductionCandidate Kind = DeductionCandidate::Normal); |
1982 | |
1983 | static CXXDeductionGuideDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
1984 | |
1985 | ExplicitSpecifier getExplicitSpecifier() { return ExplicitSpec; } |
1986 | const ExplicitSpecifier getExplicitSpecifier() const { return ExplicitSpec; } |
1987 | |
1988 | /// Return true if the declaration is already resolved to be explicit. |
1989 | bool isExplicit() const { return ExplicitSpec.isExplicit(); } |
1990 | |
1991 | /// Get the template for which this guide performs deduction. |
1992 | TemplateDecl *getDeducedTemplate() const { |
1993 | return getDeclName().getCXXDeductionGuideTemplate(); |
1994 | } |
1995 | |
1996 | /// Get the constructor from which this deduction guide was generated, if |
1997 | /// this is an implicit deduction guide. |
1998 | CXXConstructorDecl *getCorrespondingConstructor() const { return Ctor; } |
1999 | |
2000 | void setDeductionCandidateKind(DeductionCandidate K) { |
2001 | FunctionDeclBits.DeductionCandidateKind = static_cast<unsigned char>(K); |
2002 | } |
2003 | |
2004 | DeductionCandidate getDeductionCandidateKind() const { |
2005 | return static_cast<DeductionCandidate>( |
2006 | FunctionDeclBits.DeductionCandidateKind); |
2007 | } |
2008 | |
2009 | // Implement isa/cast/dyncast/etc. |
2010 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2011 | static bool classofKind(Kind K) { return K == CXXDeductionGuide; } |
2012 | }; |
2013 | |
2014 | /// \brief Represents the body of a requires-expression. |
2015 | /// |
2016 | /// This decl exists merely to serve as the DeclContext for the local |
2017 | /// parameters of the requires expression as well as other declarations inside |
2018 | /// it. |
2019 | /// |
2020 | /// \code |
2021 | /// template<typename T> requires requires (T t) { {t++} -> regular; } |
2022 | /// \endcode |
2023 | /// |
2024 | /// In this example, a RequiresExpr object will be generated for the expression, |
2025 | /// and a RequiresExprBodyDecl will be created to hold the parameter t and the |
2026 | /// template argument list imposed by the compound requirement. |
2027 | class RequiresExprBodyDecl : public Decl, public DeclContext { |
2028 | RequiresExprBodyDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc) |
2029 | : Decl(RequiresExprBody, DC, StartLoc), DeclContext(RequiresExprBody) {} |
2030 | |
2031 | public: |
2032 | friend class ASTDeclReader; |
2033 | friend class ASTDeclWriter; |
2034 | |
2035 | static RequiresExprBodyDecl *Create(ASTContext &C, DeclContext *DC, |
2036 | SourceLocation StartLoc); |
2037 | |
2038 | static RequiresExprBodyDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2039 | |
2040 | // Implement isa/cast/dyncast/etc. |
2041 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2042 | static bool classofKind(Kind K) { return K == RequiresExprBody; } |
2043 | |
2044 | static DeclContext *castToDeclContext(const RequiresExprBodyDecl *D) { |
2045 | return static_cast<DeclContext *>(const_cast<RequiresExprBodyDecl *>(D)); |
2046 | } |
2047 | |
2048 | static RequiresExprBodyDecl *castFromDeclContext(const DeclContext *DC) { |
2049 | return static_cast<RequiresExprBodyDecl *>(const_cast<DeclContext *>(DC)); |
2050 | } |
2051 | }; |
2052 | |
2053 | /// Represents a static or instance method of a struct/union/class. |
2054 | /// |
2055 | /// In the terminology of the C++ Standard, these are the (static and |
2056 | /// non-static) member functions, whether virtual or not. |
2057 | class CXXMethodDecl : public FunctionDecl { |
2058 | void anchor() override; |
2059 | |
2060 | protected: |
2061 | CXXMethodDecl(Kind DK, ASTContext &C, CXXRecordDecl *RD, |
2062 | SourceLocation StartLoc, const DeclarationNameInfo &NameInfo, |
2063 | QualType T, TypeSourceInfo *TInfo, StorageClass SC, |
2064 | bool UsesFPIntrin, bool isInline, |
2065 | ConstexprSpecKind ConstexprKind, SourceLocation EndLocation, |
2066 | Expr *TrailingRequiresClause = nullptr) |
2067 | : FunctionDecl(DK, C, RD, StartLoc, NameInfo, T, TInfo, SC, UsesFPIntrin, |
2068 | isInline, ConstexprKind, TrailingRequiresClause) { |
2069 | if (EndLocation.isValid()) |
2070 | setRangeEnd(EndLocation); |
2071 | } |
2072 | |
2073 | public: |
2074 | static CXXMethodDecl * |
2075 | Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2076 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
2077 | StorageClass SC, bool UsesFPIntrin, bool isInline, |
2078 | ConstexprSpecKind ConstexprKind, SourceLocation EndLocation, |
2079 | Expr *TrailingRequiresClause = nullptr); |
2080 | |
2081 | static CXXMethodDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2082 | |
2083 | bool isStatic() const; |
2084 | bool isInstance() const { return !isStatic(); } |
2085 | |
2086 | /// [C++2b][dcl.fct]/p7 |
2087 | /// An explicit object member function is a non-static |
2088 | /// member function with an explicit object parameter. e.g., |
2089 | /// void func(this SomeType); |
2090 | bool isExplicitObjectMemberFunction() const; |
2091 | |
2092 | /// [C++2b][dcl.fct]/p7 |
2093 | /// An implicit object member function is a non-static |
2094 | /// member function without an explicit object parameter. |
2095 | bool isImplicitObjectMemberFunction() const; |
2096 | |
2097 | /// Returns true if the given operator is implicitly static in a record |
2098 | /// context. |
2099 | static bool isStaticOverloadedOperator(OverloadedOperatorKind OOK) { |
2100 | // [class.free]p1: |
2101 | // Any allocation function for a class T is a static member |
2102 | // (even if not explicitly declared static). |
2103 | // [class.free]p6 Any deallocation function for a class X is a static member |
2104 | // (even if not explicitly declared static). |
2105 | return OOK == OO_New || OOK == OO_Array_New || OOK == OO_Delete || |
2106 | OOK == OO_Array_Delete; |
2107 | } |
2108 | |
2109 | bool isConst() const { return getType()->castAs<FunctionType>()->isConst(); } |
2110 | bool isVolatile() const { return getType()->castAs<FunctionType>()->isVolatile(); } |
2111 | |
2112 | bool isVirtual() const { |
2113 | CXXMethodDecl *CD = const_cast<CXXMethodDecl*>(this)->getCanonicalDecl(); |
2114 | |
2115 | // Member function is virtual if it is marked explicitly so, or if it is |
2116 | // declared in __interface -- then it is automatically pure virtual. |
2117 | if (CD->isVirtualAsWritten() || CD->isPureVirtual()) |
2118 | return true; |
2119 | |
2120 | return CD->size_overridden_methods() != 0; |
2121 | } |
2122 | |
2123 | /// If it's possible to devirtualize a call to this method, return the called |
2124 | /// function. Otherwise, return null. |
2125 | |
2126 | /// \param Base The object on which this virtual function is called. |
2127 | /// \param IsAppleKext True if we are compiling for Apple kext. |
2128 | CXXMethodDecl *getDevirtualizedMethod(const Expr *Base, bool IsAppleKext); |
2129 | |
2130 | const CXXMethodDecl *getDevirtualizedMethod(const Expr *Base, |
2131 | bool IsAppleKext) const { |
2132 | return const_cast<CXXMethodDecl *>(this)->getDevirtualizedMethod( |
2133 | Base, IsAppleKext); |
2134 | } |
2135 | |
2136 | /// Determine whether this is a usual deallocation function (C++ |
2137 | /// [basic.stc.dynamic.deallocation]p2), which is an overloaded delete or |
2138 | /// delete[] operator with a particular signature. Populates \p PreventedBy |
2139 | /// with the declarations of the functions of the same kind if they were the |
2140 | /// reason for this function returning false. This is used by |
2141 | /// Sema::isUsualDeallocationFunction to reconsider the answer based on the |
2142 | /// context. |
2143 | bool isUsualDeallocationFunction( |
2144 | SmallVectorImpl<const FunctionDecl *> &PreventedBy) const; |
2145 | |
2146 | /// Determine whether this is a copy-assignment operator, regardless |
2147 | /// of whether it was declared implicitly or explicitly. |
2148 | bool isCopyAssignmentOperator() const; |
2149 | |
2150 | /// Determine whether this is a move assignment operator. |
2151 | bool isMoveAssignmentOperator() const; |
2152 | |
2153 | CXXMethodDecl *getCanonicalDecl() override { |
2154 | return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); |
2155 | } |
2156 | const CXXMethodDecl *getCanonicalDecl() const { |
2157 | return const_cast<CXXMethodDecl*>(this)->getCanonicalDecl(); |
2158 | } |
2159 | |
2160 | CXXMethodDecl *getMostRecentDecl() { |
2161 | return cast<CXXMethodDecl>( |
2162 | static_cast<FunctionDecl *>(this)->getMostRecentDecl()); |
2163 | } |
2164 | const CXXMethodDecl *getMostRecentDecl() const { |
2165 | return const_cast<CXXMethodDecl*>(this)->getMostRecentDecl(); |
2166 | } |
2167 | |
2168 | void addOverriddenMethod(const CXXMethodDecl *MD); |
2169 | |
2170 | using method_iterator = const CXXMethodDecl *const *; |
2171 | |
2172 | method_iterator begin_overridden_methods() const; |
2173 | method_iterator end_overridden_methods() const; |
2174 | unsigned size_overridden_methods() const; |
2175 | |
2176 | using overridden_method_range = llvm::iterator_range< |
2177 | llvm::TinyPtrVector<const CXXMethodDecl *>::const_iterator>; |
2178 | |
2179 | overridden_method_range overridden_methods() const; |
2180 | |
2181 | /// Return the parent of this method declaration, which |
2182 | /// is the class in which this method is defined. |
2183 | const CXXRecordDecl *getParent() const { |
2184 | return cast<CXXRecordDecl>(FunctionDecl::getParent()); |
2185 | } |
2186 | |
2187 | /// Return the parent of this method declaration, which |
2188 | /// is the class in which this method is defined. |
2189 | CXXRecordDecl *getParent() { |
2190 | return const_cast<CXXRecordDecl *>( |
2191 | cast<CXXRecordDecl>(FunctionDecl::getParent())); |
2192 | } |
2193 | |
2194 | /// Return the type of the \c this pointer. |
2195 | /// |
2196 | /// Should only be called for instance (i.e., non-static) methods. Note |
2197 | /// that for the call operator of a lambda closure type, this returns the |
2198 | /// desugared 'this' type (a pointer to the closure type), not the captured |
2199 | /// 'this' type. |
2200 | QualType getThisType() const; |
2201 | |
2202 | /// Return the type of the object pointed by \c this. |
2203 | /// |
2204 | /// See getThisType() for usage restriction. |
2205 | |
2206 | QualType getFunctionObjectParameterReferenceType() const; |
2207 | QualType getFunctionObjectParameterType() const { |
2208 | return getFunctionObjectParameterReferenceType().getNonReferenceType(); |
2209 | } |
2210 | |
2211 | unsigned getNumExplicitParams() const { |
2212 | return getNumParams() - (isExplicitObjectMemberFunction() ? 1 : 0); |
2213 | } |
2214 | |
2215 | static QualType getThisType(const FunctionProtoType *FPT, |
2216 | const CXXRecordDecl *Decl); |
2217 | |
2218 | Qualifiers getMethodQualifiers() const { |
2219 | return getType()->castAs<FunctionProtoType>()->getMethodQuals(); |
2220 | } |
2221 | |
2222 | /// Retrieve the ref-qualifier associated with this method. |
2223 | /// |
2224 | /// In the following example, \c f() has an lvalue ref-qualifier, \c g() |
2225 | /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier. |
2226 | /// @code |
2227 | /// struct X { |
2228 | /// void f() &; |
2229 | /// void g() &&; |
2230 | /// void h(); |
2231 | /// }; |
2232 | /// @endcode |
2233 | RefQualifierKind getRefQualifier() const { |
2234 | return getType()->castAs<FunctionProtoType>()->getRefQualifier(); |
2235 | } |
2236 | |
2237 | bool hasInlineBody() const; |
2238 | |
2239 | /// Determine whether this is a lambda closure type's static member |
2240 | /// function that is used for the result of the lambda's conversion to |
2241 | /// function pointer (for a lambda with no captures). |
2242 | /// |
2243 | /// The function itself, if used, will have a placeholder body that will be |
2244 | /// supplied by IR generation to either forward to the function call operator |
2245 | /// or clone the function call operator. |
2246 | bool isLambdaStaticInvoker() const; |
2247 | |
2248 | /// Find the method in \p RD that corresponds to this one. |
2249 | /// |
2250 | /// Find if \p RD or one of the classes it inherits from override this method. |
2251 | /// If so, return it. \p RD is assumed to be a subclass of the class defining |
2252 | /// this method (or be the class itself), unless \p MayBeBase is set to true. |
2253 | CXXMethodDecl * |
2254 | getCorrespondingMethodInClass(const CXXRecordDecl *RD, |
2255 | bool MayBeBase = false); |
2256 | |
2257 | const CXXMethodDecl * |
2258 | getCorrespondingMethodInClass(const CXXRecordDecl *RD, |
2259 | bool MayBeBase = false) const { |
2260 | return const_cast<CXXMethodDecl *>(this) |
2261 | ->getCorrespondingMethodInClass(RD, MayBeBase); |
2262 | } |
2263 | |
2264 | /// Find if \p RD declares a function that overrides this function, and if so, |
2265 | /// return it. Does not search base classes. |
2266 | CXXMethodDecl *getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD, |
2267 | bool MayBeBase = false); |
2268 | const CXXMethodDecl * |
2269 | getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD, |
2270 | bool MayBeBase = false) const { |
2271 | return const_cast<CXXMethodDecl *>(this) |
2272 | ->getCorrespondingMethodDeclaredInClass(RD, MayBeBase); |
2273 | } |
2274 | |
2275 | // Implement isa/cast/dyncast/etc. |
2276 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2277 | static bool classofKind(Kind K) { |
2278 | return K >= firstCXXMethod && K <= lastCXXMethod; |
2279 | } |
2280 | }; |
2281 | |
2282 | /// Represents a C++ base or member initializer. |
2283 | /// |
2284 | /// This is part of a constructor initializer that |
2285 | /// initializes one non-static member variable or one base class. For |
2286 | /// example, in the following, both 'A(a)' and 'f(3.14159)' are member |
2287 | /// initializers: |
2288 | /// |
2289 | /// \code |
2290 | /// class A { }; |
2291 | /// class B : public A { |
2292 | /// float f; |
2293 | /// public: |
2294 | /// B(A& a) : A(a), f(3.14159) { } |
2295 | /// }; |
2296 | /// \endcode |
2297 | class CXXCtorInitializer final { |
2298 | /// Either the base class name/delegating constructor type (stored as |
2299 | /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field |
2300 | /// (IndirectFieldDecl*) being initialized. |
2301 | llvm::PointerUnion<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *> |
2302 | Initializee; |
2303 | |
2304 | /// The argument used to initialize the base or member, which may |
2305 | /// end up constructing an object (when multiple arguments are involved). |
2306 | Stmt *Init; |
2307 | |
2308 | /// The source location for the field name or, for a base initializer |
2309 | /// pack expansion, the location of the ellipsis. |
2310 | /// |
2311 | /// In the case of a delegating |
2312 | /// constructor, it will still include the type's source location as the |
2313 | /// Initializee points to the CXXConstructorDecl (to allow loop detection). |
2314 | SourceLocation MemberOrEllipsisLocation; |
2315 | |
2316 | /// Location of the left paren of the ctor-initializer. |
2317 | SourceLocation LParenLoc; |
2318 | |
2319 | /// Location of the right paren of the ctor-initializer. |
2320 | SourceLocation RParenLoc; |
2321 | |
2322 | /// If the initializee is a type, whether that type makes this |
2323 | /// a delegating initialization. |
2324 | LLVM_PREFERRED_TYPE(bool) |
2325 | unsigned IsDelegating : 1; |
2326 | |
2327 | /// If the initializer is a base initializer, this keeps track |
2328 | /// of whether the base is virtual or not. |
2329 | LLVM_PREFERRED_TYPE(bool) |
2330 | unsigned IsVirtual : 1; |
2331 | |
2332 | /// Whether or not the initializer is explicitly written |
2333 | /// in the sources. |
2334 | LLVM_PREFERRED_TYPE(bool) |
2335 | unsigned IsWritten : 1; |
2336 | |
2337 | /// If IsWritten is true, then this number keeps track of the textual order |
2338 | /// of this initializer in the original sources, counting from 0. |
2339 | unsigned SourceOrder : 13; |
2340 | |
2341 | public: |
2342 | /// Creates a new base-class initializer. |
2343 | explicit |
2344 | CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual, |
2345 | SourceLocation L, Expr *Init, SourceLocation R, |
2346 | SourceLocation EllipsisLoc); |
2347 | |
2348 | /// Creates a new member initializer. |
2349 | explicit |
2350 | CXXCtorInitializer(ASTContext &Context, FieldDecl *Member, |
2351 | SourceLocation MemberLoc, SourceLocation L, Expr *Init, |
2352 | SourceLocation R); |
2353 | |
2354 | /// Creates a new anonymous field initializer. |
2355 | explicit |
2356 | CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member, |
2357 | SourceLocation MemberLoc, SourceLocation L, Expr *Init, |
2358 | SourceLocation R); |
2359 | |
2360 | /// Creates a new delegating initializer. |
2361 | explicit |
2362 | CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, |
2363 | SourceLocation L, Expr *Init, SourceLocation R); |
2364 | |
2365 | /// \return Unique reproducible object identifier. |
2366 | int64_t getID(const ASTContext &Context) const; |
2367 | |
2368 | /// Determine whether this initializer is initializing a base class. |
2369 | bool isBaseInitializer() const { |
2370 | return Initializee.is<TypeSourceInfo*>() && !IsDelegating; |
2371 | } |
2372 | |
2373 | /// Determine whether this initializer is initializing a non-static |
2374 | /// data member. |
2375 | bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); } |
2376 | |
2377 | bool isAnyMemberInitializer() const { |
2378 | return isMemberInitializer() || isIndirectMemberInitializer(); |
2379 | } |
2380 | |
2381 | bool isIndirectMemberInitializer() const { |
2382 | return Initializee.is<IndirectFieldDecl*>(); |
2383 | } |
2384 | |
2385 | /// Determine whether this initializer is an implicit initializer |
2386 | /// generated for a field with an initializer defined on the member |
2387 | /// declaration. |
2388 | /// |
2389 | /// In-class member initializers (also known as "non-static data member |
2390 | /// initializations", NSDMIs) were introduced in C++11. |
2391 | bool isInClassMemberInitializer() const { |
2392 | return Init->getStmtClass() == Stmt::CXXDefaultInitExprClass; |
2393 | } |
2394 | |
2395 | /// Determine whether this initializer is creating a delegating |
2396 | /// constructor. |
2397 | bool isDelegatingInitializer() const { |
2398 | return Initializee.is<TypeSourceInfo*>() && IsDelegating; |
2399 | } |
2400 | |
2401 | /// Determine whether this initializer is a pack expansion. |
2402 | bool isPackExpansion() const { |
2403 | return isBaseInitializer() && MemberOrEllipsisLocation.isValid(); |
2404 | } |
2405 | |
2406 | // For a pack expansion, returns the location of the ellipsis. |
2407 | SourceLocation getEllipsisLoc() const { |
2408 | if (!isPackExpansion()) |
2409 | return {}; |
2410 | return MemberOrEllipsisLocation; |
2411 | } |
2412 | |
2413 | /// If this is a base class initializer, returns the type of the |
2414 | /// base class with location information. Otherwise, returns an NULL |
2415 | /// type location. |
2416 | TypeLoc getBaseClassLoc() const; |
2417 | |
2418 | /// If this is a base class initializer, returns the type of the base class. |
2419 | /// Otherwise, returns null. |
2420 | const Type *getBaseClass() const; |
2421 | |
2422 | /// Returns whether the base is virtual or not. |
2423 | bool isBaseVirtual() const { |
2424 | assert(isBaseInitializer() && "Must call this on base initializer!" ); |
2425 | |
2426 | return IsVirtual; |
2427 | } |
2428 | |
2429 | /// Returns the declarator information for a base class or delegating |
2430 | /// initializer. |
2431 | TypeSourceInfo *getTypeSourceInfo() const { |
2432 | return Initializee.dyn_cast<TypeSourceInfo *>(); |
2433 | } |
2434 | |
2435 | /// If this is a member initializer, returns the declaration of the |
2436 | /// non-static data member being initialized. Otherwise, returns null. |
2437 | FieldDecl *getMember() const { |
2438 | if (isMemberInitializer()) |
2439 | return Initializee.get<FieldDecl*>(); |
2440 | return nullptr; |
2441 | } |
2442 | |
2443 | FieldDecl *getAnyMember() const { |
2444 | if (isMemberInitializer()) |
2445 | return Initializee.get<FieldDecl*>(); |
2446 | if (isIndirectMemberInitializer()) |
2447 | return Initializee.get<IndirectFieldDecl*>()->getAnonField(); |
2448 | return nullptr; |
2449 | } |
2450 | |
2451 | IndirectFieldDecl *getIndirectMember() const { |
2452 | if (isIndirectMemberInitializer()) |
2453 | return Initializee.get<IndirectFieldDecl*>(); |
2454 | return nullptr; |
2455 | } |
2456 | |
2457 | SourceLocation getMemberLocation() const { |
2458 | return MemberOrEllipsisLocation; |
2459 | } |
2460 | |
2461 | /// Determine the source location of the initializer. |
2462 | SourceLocation getSourceLocation() const; |
2463 | |
2464 | /// Determine the source range covering the entire initializer. |
2465 | SourceRange getSourceRange() const LLVM_READONLY; |
2466 | |
2467 | /// Determine whether this initializer is explicitly written |
2468 | /// in the source code. |
2469 | bool isWritten() const { return IsWritten; } |
2470 | |
2471 | /// Return the source position of the initializer, counting from 0. |
2472 | /// If the initializer was implicit, -1 is returned. |
2473 | int getSourceOrder() const { |
2474 | return IsWritten ? static_cast<int>(SourceOrder) : -1; |
2475 | } |
2476 | |
2477 | /// Set the source order of this initializer. |
2478 | /// |
2479 | /// This can only be called once for each initializer; it cannot be called |
2480 | /// on an initializer having a positive number of (implicit) array indices. |
2481 | /// |
2482 | /// This assumes that the initializer was written in the source code, and |
2483 | /// ensures that isWritten() returns true. |
2484 | void setSourceOrder(int Pos) { |
2485 | assert(!IsWritten && |
2486 | "setSourceOrder() used on implicit initializer" ); |
2487 | assert(SourceOrder == 0 && |
2488 | "calling twice setSourceOrder() on the same initializer" ); |
2489 | assert(Pos >= 0 && |
2490 | "setSourceOrder() used to make an initializer implicit" ); |
2491 | IsWritten = true; |
2492 | SourceOrder = static_cast<unsigned>(Pos); |
2493 | } |
2494 | |
2495 | SourceLocation getLParenLoc() const { return LParenLoc; } |
2496 | SourceLocation getRParenLoc() const { return RParenLoc; } |
2497 | |
2498 | /// Get the initializer. |
2499 | Expr *getInit() const { return static_cast<Expr *>(Init); } |
2500 | }; |
2501 | |
2502 | /// Description of a constructor that was inherited from a base class. |
2503 | class InheritedConstructor { |
2504 | ConstructorUsingShadowDecl *Shadow = nullptr; |
2505 | CXXConstructorDecl *BaseCtor = nullptr; |
2506 | |
2507 | public: |
2508 | InheritedConstructor() = default; |
2509 | InheritedConstructor(ConstructorUsingShadowDecl *Shadow, |
2510 | CXXConstructorDecl *BaseCtor) |
2511 | : Shadow(Shadow), BaseCtor(BaseCtor) {} |
2512 | |
2513 | explicit operator bool() const { return Shadow; } |
2514 | |
2515 | ConstructorUsingShadowDecl *getShadowDecl() const { return Shadow; } |
2516 | CXXConstructorDecl *getConstructor() const { return BaseCtor; } |
2517 | }; |
2518 | |
2519 | /// Represents a C++ constructor within a class. |
2520 | /// |
2521 | /// For example: |
2522 | /// |
2523 | /// \code |
2524 | /// class X { |
2525 | /// public: |
2526 | /// explicit X(int); // represented by a CXXConstructorDecl. |
2527 | /// }; |
2528 | /// \endcode |
2529 | class CXXConstructorDecl final |
2530 | : public CXXMethodDecl, |
2531 | private llvm::TrailingObjects<CXXConstructorDecl, InheritedConstructor, |
2532 | ExplicitSpecifier> { |
2533 | // This class stores some data in DeclContext::CXXConstructorDeclBits |
2534 | // to save some space. Use the provided accessors to access it. |
2535 | |
2536 | /// \name Support for base and member initializers. |
2537 | /// \{ |
2538 | /// The arguments used to initialize the base or member. |
2539 | LazyCXXCtorInitializersPtr CtorInitializers; |
2540 | |
2541 | CXXConstructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2542 | const DeclarationNameInfo &NameInfo, QualType T, |
2543 | TypeSourceInfo *TInfo, ExplicitSpecifier ES, |
2544 | bool UsesFPIntrin, bool isInline, |
2545 | bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind, |
2546 | InheritedConstructor Inherited, |
2547 | Expr *TrailingRequiresClause); |
2548 | |
2549 | void anchor() override; |
2550 | |
2551 | size_t numTrailingObjects(OverloadToken<InheritedConstructor>) const { |
2552 | return CXXConstructorDeclBits.IsInheritingConstructor; |
2553 | } |
2554 | size_t numTrailingObjects(OverloadToken<ExplicitSpecifier>) const { |
2555 | return CXXConstructorDeclBits.HasTrailingExplicitSpecifier; |
2556 | } |
2557 | |
2558 | ExplicitSpecifier getExplicitSpecifierInternal() const { |
2559 | if (CXXConstructorDeclBits.HasTrailingExplicitSpecifier) |
2560 | return *getTrailingObjects<ExplicitSpecifier>(); |
2561 | return ExplicitSpecifier( |
2562 | nullptr, CXXConstructorDeclBits.IsSimpleExplicit |
2563 | ? ExplicitSpecKind::ResolvedTrue |
2564 | : ExplicitSpecKind::ResolvedFalse); |
2565 | } |
2566 | |
2567 | enum TrailingAllocKind { |
2568 | TAKInheritsConstructor = 1, |
2569 | TAKHasTailExplicit = 1 << 1, |
2570 | }; |
2571 | |
2572 | uint64_t getTrailingAllocKind() const { |
2573 | return numTrailingObjects(OverloadToken<InheritedConstructor>()) | |
2574 | (numTrailingObjects(OverloadToken<ExplicitSpecifier>()) << 1); |
2575 | } |
2576 | |
2577 | public: |
2578 | friend class ASTDeclReader; |
2579 | friend class ASTDeclWriter; |
2580 | friend TrailingObjects; |
2581 | |
2582 | static CXXConstructorDecl *CreateDeserialized(ASTContext &C, DeclID ID, |
2583 | uint64_t AllocKind); |
2584 | static CXXConstructorDecl * |
2585 | Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2586 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
2587 | ExplicitSpecifier ES, bool UsesFPIntrin, bool isInline, |
2588 | bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind, |
2589 | InheritedConstructor Inherited = InheritedConstructor(), |
2590 | Expr *TrailingRequiresClause = nullptr); |
2591 | |
2592 | void setExplicitSpecifier(ExplicitSpecifier ES) { |
2593 | assert((!ES.getExpr() || |
2594 | CXXConstructorDeclBits.HasTrailingExplicitSpecifier) && |
2595 | "cannot set this explicit specifier. no trail-allocated space for " |
2596 | "explicit" ); |
2597 | if (ES.getExpr()) |
2598 | *getCanonicalDecl()->getTrailingObjects<ExplicitSpecifier>() = ES; |
2599 | else |
2600 | CXXConstructorDeclBits.IsSimpleExplicit = ES.isExplicit(); |
2601 | } |
2602 | |
2603 | ExplicitSpecifier getExplicitSpecifier() { |
2604 | return getCanonicalDecl()->getExplicitSpecifierInternal(); |
2605 | } |
2606 | const ExplicitSpecifier getExplicitSpecifier() const { |
2607 | return getCanonicalDecl()->getExplicitSpecifierInternal(); |
2608 | } |
2609 | |
2610 | /// Return true if the declaration is already resolved to be explicit. |
2611 | bool isExplicit() const { return getExplicitSpecifier().isExplicit(); } |
2612 | |
2613 | /// Iterates through the member/base initializer list. |
2614 | using init_iterator = CXXCtorInitializer **; |
2615 | |
2616 | /// Iterates through the member/base initializer list. |
2617 | using init_const_iterator = CXXCtorInitializer *const *; |
2618 | |
2619 | using init_range = llvm::iterator_range<init_iterator>; |
2620 | using init_const_range = llvm::iterator_range<init_const_iterator>; |
2621 | |
2622 | init_range inits() { return init_range(init_begin(), init_end()); } |
2623 | init_const_range inits() const { |
2624 | return init_const_range(init_begin(), init_end()); |
2625 | } |
2626 | |
2627 | /// Retrieve an iterator to the first initializer. |
2628 | init_iterator init_begin() { |
2629 | const auto *ConstThis = this; |
2630 | return const_cast<init_iterator>(ConstThis->init_begin()); |
2631 | } |
2632 | |
2633 | /// Retrieve an iterator to the first initializer. |
2634 | init_const_iterator init_begin() const; |
2635 | |
2636 | /// Retrieve an iterator past the last initializer. |
2637 | init_iterator init_end() { |
2638 | return init_begin() + getNumCtorInitializers(); |
2639 | } |
2640 | |
2641 | /// Retrieve an iterator past the last initializer. |
2642 | init_const_iterator init_end() const { |
2643 | return init_begin() + getNumCtorInitializers(); |
2644 | } |
2645 | |
2646 | using init_reverse_iterator = std::reverse_iterator<init_iterator>; |
2647 | using init_const_reverse_iterator = |
2648 | std::reverse_iterator<init_const_iterator>; |
2649 | |
2650 | init_reverse_iterator init_rbegin() { |
2651 | return init_reverse_iterator(init_end()); |
2652 | } |
2653 | init_const_reverse_iterator init_rbegin() const { |
2654 | return init_const_reverse_iterator(init_end()); |
2655 | } |
2656 | |
2657 | init_reverse_iterator init_rend() { |
2658 | return init_reverse_iterator(init_begin()); |
2659 | } |
2660 | init_const_reverse_iterator init_rend() const { |
2661 | return init_const_reverse_iterator(init_begin()); |
2662 | } |
2663 | |
2664 | /// Determine the number of arguments used to initialize the member |
2665 | /// or base. |
2666 | unsigned getNumCtorInitializers() const { |
2667 | return CXXConstructorDeclBits.NumCtorInitializers; |
2668 | } |
2669 | |
2670 | void setNumCtorInitializers(unsigned numCtorInitializers) { |
2671 | CXXConstructorDeclBits.NumCtorInitializers = numCtorInitializers; |
2672 | // This assert added because NumCtorInitializers is stored |
2673 | // in CXXConstructorDeclBits as a bitfield and its width has |
2674 | // been shrunk from 32 bits to fit into CXXConstructorDeclBitfields. |
2675 | assert(CXXConstructorDeclBits.NumCtorInitializers == |
2676 | numCtorInitializers && "NumCtorInitializers overflow!" ); |
2677 | } |
2678 | |
2679 | void setCtorInitializers(CXXCtorInitializer **Initializers) { |
2680 | CtorInitializers = Initializers; |
2681 | } |
2682 | |
2683 | /// Determine whether this constructor is a delegating constructor. |
2684 | bool isDelegatingConstructor() const { |
2685 | return (getNumCtorInitializers() == 1) && |
2686 | init_begin()[0]->isDelegatingInitializer(); |
2687 | } |
2688 | |
2689 | /// When this constructor delegates to another, retrieve the target. |
2690 | CXXConstructorDecl *getTargetConstructor() const; |
2691 | |
2692 | /// Whether this constructor is a default |
2693 | /// constructor (C++ [class.ctor]p5), which can be used to |
2694 | /// default-initialize a class of this type. |
2695 | bool isDefaultConstructor() const; |
2696 | |
2697 | /// Whether this constructor is a copy constructor (C++ [class.copy]p2, |
2698 | /// which can be used to copy the class. |
2699 | /// |
2700 | /// \p TypeQuals will be set to the qualifiers on the |
2701 | /// argument type. For example, \p TypeQuals would be set to \c |
2702 | /// Qualifiers::Const for the following copy constructor: |
2703 | /// |
2704 | /// \code |
2705 | /// class X { |
2706 | /// public: |
2707 | /// X(const X&); |
2708 | /// }; |
2709 | /// \endcode |
2710 | bool isCopyConstructor(unsigned &TypeQuals) const; |
2711 | |
2712 | /// Whether this constructor is a copy |
2713 | /// constructor (C++ [class.copy]p2, which can be used to copy the |
2714 | /// class. |
2715 | bool isCopyConstructor() const { |
2716 | unsigned TypeQuals = 0; |
2717 | return isCopyConstructor(TypeQuals); |
2718 | } |
2719 | |
2720 | /// Determine whether this constructor is a move constructor |
2721 | /// (C++11 [class.copy]p3), which can be used to move values of the class. |
2722 | /// |
2723 | /// \param TypeQuals If this constructor is a move constructor, will be set |
2724 | /// to the type qualifiers on the referent of the first parameter's type. |
2725 | bool isMoveConstructor(unsigned &TypeQuals) const; |
2726 | |
2727 | /// Determine whether this constructor is a move constructor |
2728 | /// (C++11 [class.copy]p3), which can be used to move values of the class. |
2729 | bool isMoveConstructor() const { |
2730 | unsigned TypeQuals = 0; |
2731 | return isMoveConstructor(TypeQuals); |
2732 | } |
2733 | |
2734 | /// Determine whether this is a copy or move constructor. |
2735 | /// |
2736 | /// \param TypeQuals Will be set to the type qualifiers on the reference |
2737 | /// parameter, if in fact this is a copy or move constructor. |
2738 | bool isCopyOrMoveConstructor(unsigned &TypeQuals) const; |
2739 | |
2740 | /// Determine whether this a copy or move constructor. |
2741 | bool isCopyOrMoveConstructor() const { |
2742 | unsigned Quals; |
2743 | return isCopyOrMoveConstructor(TypeQuals&: Quals); |
2744 | } |
2745 | |
2746 | /// Whether this constructor is a |
2747 | /// converting constructor (C++ [class.conv.ctor]), which can be |
2748 | /// used for user-defined conversions. |
2749 | bool isConvertingConstructor(bool AllowExplicit) const; |
2750 | |
2751 | /// Determine whether this is a member template specialization that |
2752 | /// would copy the object to itself. Such constructors are never used to copy |
2753 | /// an object. |
2754 | bool isSpecializationCopyingObject() const; |
2755 | |
2756 | /// Determine whether this is an implicit constructor synthesized to |
2757 | /// model a call to a constructor inherited from a base class. |
2758 | bool isInheritingConstructor() const { |
2759 | return CXXConstructorDeclBits.IsInheritingConstructor; |
2760 | } |
2761 | |
2762 | /// State that this is an implicit constructor synthesized to |
2763 | /// model a call to a constructor inherited from a base class. |
2764 | void setInheritingConstructor(bool isIC = true) { |
2765 | CXXConstructorDeclBits.IsInheritingConstructor = isIC; |
2766 | } |
2767 | |
2768 | /// Get the constructor that this inheriting constructor is based on. |
2769 | InheritedConstructor getInheritedConstructor() const { |
2770 | return isInheritingConstructor() ? |
2771 | *getTrailingObjects<InheritedConstructor>() : InheritedConstructor(); |
2772 | } |
2773 | |
2774 | CXXConstructorDecl *getCanonicalDecl() override { |
2775 | return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl()); |
2776 | } |
2777 | const CXXConstructorDecl *getCanonicalDecl() const { |
2778 | return const_cast<CXXConstructorDecl*>(this)->getCanonicalDecl(); |
2779 | } |
2780 | |
2781 | // Implement isa/cast/dyncast/etc. |
2782 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2783 | static bool classofKind(Kind K) { return K == CXXConstructor; } |
2784 | }; |
2785 | |
2786 | /// Represents a C++ destructor within a class. |
2787 | /// |
2788 | /// For example: |
2789 | /// |
2790 | /// \code |
2791 | /// class X { |
2792 | /// public: |
2793 | /// ~X(); // represented by a CXXDestructorDecl. |
2794 | /// }; |
2795 | /// \endcode |
2796 | class CXXDestructorDecl : public CXXMethodDecl { |
2797 | friend class ASTDeclReader; |
2798 | friend class ASTDeclWriter; |
2799 | |
2800 | // FIXME: Don't allocate storage for these except in the first declaration |
2801 | // of a virtual destructor. |
2802 | FunctionDecl *OperatorDelete = nullptr; |
2803 | Expr *OperatorDeleteThisArg = nullptr; |
2804 | |
2805 | CXXDestructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2806 | const DeclarationNameInfo &NameInfo, QualType T, |
2807 | TypeSourceInfo *TInfo, bool UsesFPIntrin, bool isInline, |
2808 | bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind, |
2809 | Expr *TrailingRequiresClause = nullptr) |
2810 | : CXXMethodDecl(CXXDestructor, C, RD, StartLoc, NameInfo, T, TInfo, |
2811 | SC_None, UsesFPIntrin, isInline, ConstexprKind, |
2812 | SourceLocation(), TrailingRequiresClause) { |
2813 | setImplicit(isImplicitlyDeclared); |
2814 | } |
2815 | |
2816 | void anchor() override; |
2817 | |
2818 | public: |
2819 | static CXXDestructorDecl * |
2820 | Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2821 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
2822 | bool UsesFPIntrin, bool isInline, bool isImplicitlyDeclared, |
2823 | ConstexprSpecKind ConstexprKind, |
2824 | Expr *TrailingRequiresClause = nullptr); |
2825 | static CXXDestructorDecl *CreateDeserialized(ASTContext & C, DeclID ID); |
2826 | |
2827 | void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg); |
2828 | |
2829 | const FunctionDecl *getOperatorDelete() const { |
2830 | return getCanonicalDecl()->OperatorDelete; |
2831 | } |
2832 | |
2833 | Expr *getOperatorDeleteThisArg() const { |
2834 | return getCanonicalDecl()->OperatorDeleteThisArg; |
2835 | } |
2836 | |
2837 | CXXDestructorDecl *getCanonicalDecl() override { |
2838 | return cast<CXXDestructorDecl>(FunctionDecl::getCanonicalDecl()); |
2839 | } |
2840 | const CXXDestructorDecl *getCanonicalDecl() const { |
2841 | return const_cast<CXXDestructorDecl*>(this)->getCanonicalDecl(); |
2842 | } |
2843 | |
2844 | // Implement isa/cast/dyncast/etc. |
2845 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2846 | static bool classofKind(Kind K) { return K == CXXDestructor; } |
2847 | }; |
2848 | |
2849 | /// Represents a C++ conversion function within a class. |
2850 | /// |
2851 | /// For example: |
2852 | /// |
2853 | /// \code |
2854 | /// class X { |
2855 | /// public: |
2856 | /// operator bool(); |
2857 | /// }; |
2858 | /// \endcode |
2859 | class CXXConversionDecl : public CXXMethodDecl { |
2860 | CXXConversionDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2861 | const DeclarationNameInfo &NameInfo, QualType T, |
2862 | TypeSourceInfo *TInfo, bool UsesFPIntrin, bool isInline, |
2863 | ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind, |
2864 | SourceLocation EndLocation, |
2865 | Expr *TrailingRequiresClause = nullptr) |
2866 | : CXXMethodDecl(CXXConversion, C, RD, StartLoc, NameInfo, T, TInfo, |
2867 | SC_None, UsesFPIntrin, isInline, ConstexprKind, |
2868 | EndLocation, TrailingRequiresClause), |
2869 | ExplicitSpec(ES) {} |
2870 | void anchor() override; |
2871 | |
2872 | ExplicitSpecifier ExplicitSpec; |
2873 | |
2874 | public: |
2875 | friend class ASTDeclReader; |
2876 | friend class ASTDeclWriter; |
2877 | |
2878 | static CXXConversionDecl * |
2879 | Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
2880 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
2881 | bool UsesFPIntrin, bool isInline, ExplicitSpecifier ES, |
2882 | ConstexprSpecKind ConstexprKind, SourceLocation EndLocation, |
2883 | Expr *TrailingRequiresClause = nullptr); |
2884 | static CXXConversionDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2885 | |
2886 | ExplicitSpecifier getExplicitSpecifier() { |
2887 | return getCanonicalDecl()->ExplicitSpec; |
2888 | } |
2889 | |
2890 | const ExplicitSpecifier getExplicitSpecifier() const { |
2891 | return getCanonicalDecl()->ExplicitSpec; |
2892 | } |
2893 | |
2894 | /// Return true if the declaration is already resolved to be explicit. |
2895 | bool isExplicit() const { return getExplicitSpecifier().isExplicit(); } |
2896 | void setExplicitSpecifier(ExplicitSpecifier ES) { ExplicitSpec = ES; } |
2897 | |
2898 | /// Returns the type that this conversion function is converting to. |
2899 | QualType getConversionType() const { |
2900 | return getType()->castAs<FunctionType>()->getReturnType(); |
2901 | } |
2902 | |
2903 | /// Determine whether this conversion function is a conversion from |
2904 | /// a lambda closure type to a block pointer. |
2905 | bool isLambdaToBlockPointerConversion() const; |
2906 | |
2907 | CXXConversionDecl *getCanonicalDecl() override { |
2908 | return cast<CXXConversionDecl>(FunctionDecl::getCanonicalDecl()); |
2909 | } |
2910 | const CXXConversionDecl *getCanonicalDecl() const { |
2911 | return const_cast<CXXConversionDecl*>(this)->getCanonicalDecl(); |
2912 | } |
2913 | |
2914 | // Implement isa/cast/dyncast/etc. |
2915 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2916 | static bool classofKind(Kind K) { return K == CXXConversion; } |
2917 | }; |
2918 | |
2919 | /// Represents the language in a linkage specification. |
2920 | /// |
2921 | /// The values are part of the serialization ABI for |
2922 | /// ASTs and cannot be changed without altering that ABI. |
2923 | enum class LinkageSpecLanguageIDs { C = 1, CXX = 2 }; |
2924 | |
2925 | /// Represents a linkage specification. |
2926 | /// |
2927 | /// For example: |
2928 | /// \code |
2929 | /// extern "C" void foo(); |
2930 | /// \endcode |
2931 | class LinkageSpecDecl : public Decl, public DeclContext { |
2932 | virtual void anchor(); |
2933 | // This class stores some data in DeclContext::LinkageSpecDeclBits to save |
2934 | // some space. Use the provided accessors to access it. |
2935 | |
2936 | /// The source location for the extern keyword. |
2937 | SourceLocation ExternLoc; |
2938 | |
2939 | /// The source location for the right brace (if valid). |
2940 | SourceLocation RBraceLoc; |
2941 | |
2942 | LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc, |
2943 | SourceLocation LangLoc, LinkageSpecLanguageIDs lang, |
2944 | bool HasBraces); |
2945 | |
2946 | public: |
2947 | static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, |
2948 | SourceLocation ExternLoc, |
2949 | SourceLocation LangLoc, |
2950 | LinkageSpecLanguageIDs Lang, bool HasBraces); |
2951 | static LinkageSpecDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2952 | |
2953 | /// Return the language specified by this linkage specification. |
2954 | LinkageSpecLanguageIDs getLanguage() const { |
2955 | return static_cast<LinkageSpecLanguageIDs>(LinkageSpecDeclBits.Language); |
2956 | } |
2957 | |
2958 | /// Set the language specified by this linkage specification. |
2959 | void setLanguage(LinkageSpecLanguageIDs L) { |
2960 | LinkageSpecDeclBits.Language = llvm::to_underlying(E: L); |
2961 | } |
2962 | |
2963 | /// Determines whether this linkage specification had braces in |
2964 | /// its syntactic form. |
2965 | bool hasBraces() const { |
2966 | assert(!RBraceLoc.isValid() || LinkageSpecDeclBits.HasBraces); |
2967 | return LinkageSpecDeclBits.HasBraces; |
2968 | } |
2969 | |
2970 | SourceLocation getExternLoc() const { return ExternLoc; } |
2971 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
2972 | void setExternLoc(SourceLocation L) { ExternLoc = L; } |
2973 | void setRBraceLoc(SourceLocation L) { |
2974 | RBraceLoc = L; |
2975 | LinkageSpecDeclBits.HasBraces = RBraceLoc.isValid(); |
2976 | } |
2977 | |
2978 | SourceLocation getEndLoc() const LLVM_READONLY { |
2979 | if (hasBraces()) |
2980 | return getRBraceLoc(); |
2981 | // No braces: get the end location of the (only) declaration in context |
2982 | // (if present). |
2983 | return decls_empty() ? getLocation() : decls_begin()->getEndLoc(); |
2984 | } |
2985 | |
2986 | SourceRange getSourceRange() const override LLVM_READONLY { |
2987 | return SourceRange(ExternLoc, getEndLoc()); |
2988 | } |
2989 | |
2990 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2991 | static bool classofKind(Kind K) { return K == LinkageSpec; } |
2992 | |
2993 | static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { |
2994 | return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); |
2995 | } |
2996 | |
2997 | static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { |
2998 | return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); |
2999 | } |
3000 | }; |
3001 | |
3002 | /// Represents C++ using-directive. |
3003 | /// |
3004 | /// For example: |
3005 | /// \code |
3006 | /// using namespace std; |
3007 | /// \endcode |
3008 | /// |
3009 | /// \note UsingDirectiveDecl should be Decl not NamedDecl, but we provide |
3010 | /// artificial names for all using-directives in order to store |
3011 | /// them in DeclContext effectively. |
3012 | class UsingDirectiveDecl : public NamedDecl { |
3013 | /// The location of the \c using keyword. |
3014 | SourceLocation UsingLoc; |
3015 | |
3016 | /// The location of the \c namespace keyword. |
3017 | SourceLocation NamespaceLoc; |
3018 | |
3019 | /// The nested-name-specifier that precedes the namespace. |
3020 | NestedNameSpecifierLoc QualifierLoc; |
3021 | |
3022 | /// The namespace nominated by this using-directive. |
3023 | NamedDecl *NominatedNamespace; |
3024 | |
3025 | /// Enclosing context containing both using-directive and nominated |
3026 | /// namespace. |
3027 | DeclContext *CommonAncestor; |
3028 | |
3029 | UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc, |
3030 | SourceLocation NamespcLoc, |
3031 | NestedNameSpecifierLoc QualifierLoc, |
3032 | SourceLocation IdentLoc, |
3033 | NamedDecl *Nominated, |
3034 | DeclContext *CommonAncestor) |
3035 | : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc), |
3036 | NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc), |
3037 | NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) {} |
3038 | |
3039 | /// Returns special DeclarationName used by using-directives. |
3040 | /// |
3041 | /// This is only used by DeclContext for storing UsingDirectiveDecls in |
3042 | /// its lookup structure. |
3043 | static DeclarationName getName() { |
3044 | return DeclarationName::getUsingDirectiveName(); |
3045 | } |
3046 | |
3047 | void anchor() override; |
3048 | |
3049 | public: |
3050 | friend class ASTDeclReader; |
3051 | |
3052 | // Friend for getUsingDirectiveName. |
3053 | friend class DeclContext; |
3054 | |
3055 | /// Retrieve the nested-name-specifier that qualifies the |
3056 | /// name of the namespace, with source-location information. |
3057 | NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } |
3058 | |
3059 | /// Retrieve the nested-name-specifier that qualifies the |
3060 | /// name of the namespace. |
3061 | NestedNameSpecifier *getQualifier() const { |
3062 | return QualifierLoc.getNestedNameSpecifier(); |
3063 | } |
3064 | |
3065 | NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; } |
3066 | const NamedDecl *getNominatedNamespaceAsWritten() const { |
3067 | return NominatedNamespace; |
3068 | } |
3069 | |
3070 | /// Returns the namespace nominated by this using-directive. |
3071 | NamespaceDecl *getNominatedNamespace(); |
3072 | |
3073 | const NamespaceDecl *getNominatedNamespace() const { |
3074 | return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); |
3075 | } |
3076 | |
3077 | /// Returns the common ancestor context of this using-directive and |
3078 | /// its nominated namespace. |
3079 | DeclContext *getCommonAncestor() { return CommonAncestor; } |
3080 | const DeclContext *getCommonAncestor() const { return CommonAncestor; } |
3081 | |
3082 | /// Return the location of the \c using keyword. |
3083 | SourceLocation getUsingLoc() const { return UsingLoc; } |
3084 | |
3085 | // FIXME: Could omit 'Key' in name. |
3086 | /// Returns the location of the \c namespace keyword. |
3087 | SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } |
3088 | |
3089 | /// Returns the location of this using declaration's identifier. |
3090 | SourceLocation getIdentLocation() const { return getLocation(); } |
3091 | |
3092 | static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, |
3093 | SourceLocation UsingLoc, |
3094 | SourceLocation NamespaceLoc, |
3095 | NestedNameSpecifierLoc QualifierLoc, |
3096 | SourceLocation IdentLoc, |
3097 | NamedDecl *Nominated, |
3098 | DeclContext *CommonAncestor); |
3099 | static UsingDirectiveDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3100 | |
3101 | SourceRange getSourceRange() const override LLVM_READONLY { |
3102 | return SourceRange(UsingLoc, getLocation()); |
3103 | } |
3104 | |
3105 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3106 | static bool classofKind(Kind K) { return K == UsingDirective; } |
3107 | }; |
3108 | |
3109 | /// Represents a C++ namespace alias. |
3110 | /// |
3111 | /// For example: |
3112 | /// |
3113 | /// \code |
3114 | /// namespace Foo = Bar; |
3115 | /// \endcode |
3116 | class NamespaceAliasDecl : public NamedDecl, |
3117 | public Redeclarable<NamespaceAliasDecl> { |
3118 | friend class ASTDeclReader; |
3119 | |
3120 | /// The location of the \c namespace keyword. |
3121 | SourceLocation NamespaceLoc; |
3122 | |
3123 | /// The location of the namespace's identifier. |
3124 | /// |
3125 | /// This is accessed by TargetNameLoc. |
3126 | SourceLocation IdentLoc; |
3127 | |
3128 | /// The nested-name-specifier that precedes the namespace. |
3129 | NestedNameSpecifierLoc QualifierLoc; |
3130 | |
3131 | /// The Decl that this alias points to, either a NamespaceDecl or |
3132 | /// a NamespaceAliasDecl. |
3133 | NamedDecl *Namespace; |
3134 | |
3135 | NamespaceAliasDecl(ASTContext &C, DeclContext *DC, |
3136 | SourceLocation NamespaceLoc, SourceLocation AliasLoc, |
3137 | IdentifierInfo *Alias, NestedNameSpecifierLoc QualifierLoc, |
3138 | SourceLocation IdentLoc, NamedDecl *Namespace) |
3139 | : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), redeclarable_base(C), |
3140 | NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc), |
3141 | QualifierLoc(QualifierLoc), Namespace(Namespace) {} |
3142 | |
3143 | void anchor() override; |
3144 | |
3145 | using redeclarable_base = Redeclarable<NamespaceAliasDecl>; |
3146 | |
3147 | NamespaceAliasDecl *getNextRedeclarationImpl() override; |
3148 | NamespaceAliasDecl *getPreviousDeclImpl() override; |
3149 | NamespaceAliasDecl *getMostRecentDeclImpl() override; |
3150 | |
3151 | public: |
3152 | static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, |
3153 | SourceLocation NamespaceLoc, |
3154 | SourceLocation AliasLoc, |
3155 | IdentifierInfo *Alias, |
3156 | NestedNameSpecifierLoc QualifierLoc, |
3157 | SourceLocation IdentLoc, |
3158 | NamedDecl *Namespace); |
3159 | |
3160 | static NamespaceAliasDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3161 | |
3162 | using redecl_range = redeclarable_base::redecl_range; |
3163 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3164 | |
3165 | using redeclarable_base::redecls_begin; |
3166 | using redeclarable_base::redecls_end; |
3167 | using redeclarable_base::redecls; |
3168 | using redeclarable_base::getPreviousDecl; |
3169 | using redeclarable_base::getMostRecentDecl; |
3170 | |
3171 | NamespaceAliasDecl *getCanonicalDecl() override { |
3172 | return getFirstDecl(); |
3173 | } |
3174 | const NamespaceAliasDecl *getCanonicalDecl() const { |
3175 | return getFirstDecl(); |
3176 | } |
3177 | |
3178 | /// Retrieve the nested-name-specifier that qualifies the |
3179 | /// name of the namespace, with source-location information. |
3180 | NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } |
3181 | |
3182 | /// Retrieve the nested-name-specifier that qualifies the |
3183 | /// name of the namespace. |
3184 | NestedNameSpecifier *getQualifier() const { |
3185 | return QualifierLoc.getNestedNameSpecifier(); |
3186 | } |
3187 | |
3188 | /// Retrieve the namespace declaration aliased by this directive. |
3189 | NamespaceDecl *getNamespace() { |
3190 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(Val: Namespace)) |
3191 | return AD->getNamespace(); |
3192 | |
3193 | return cast<NamespaceDecl>(Val: Namespace); |
3194 | } |
3195 | |
3196 | const NamespaceDecl *getNamespace() const { |
3197 | return const_cast<NamespaceAliasDecl *>(this)->getNamespace(); |
3198 | } |
3199 | |
3200 | /// Returns the location of the alias name, i.e. 'foo' in |
3201 | /// "namespace foo = ns::bar;". |
3202 | SourceLocation getAliasLoc() const { return getLocation(); } |
3203 | |
3204 | /// Returns the location of the \c namespace keyword. |
3205 | SourceLocation getNamespaceLoc() const { return NamespaceLoc; } |
3206 | |
3207 | /// Returns the location of the identifier in the named namespace. |
3208 | SourceLocation getTargetNameLoc() const { return IdentLoc; } |
3209 | |
3210 | /// Retrieve the namespace that this alias refers to, which |
3211 | /// may either be a NamespaceDecl or a NamespaceAliasDecl. |
3212 | NamedDecl *getAliasedNamespace() const { return Namespace; } |
3213 | |
3214 | SourceRange getSourceRange() const override LLVM_READONLY { |
3215 | return SourceRange(NamespaceLoc, IdentLoc); |
3216 | } |
3217 | |
3218 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3219 | static bool classofKind(Kind K) { return K == NamespaceAlias; } |
3220 | }; |
3221 | |
3222 | /// Implicit declaration of a temporary that was materialized by |
3223 | /// a MaterializeTemporaryExpr and lifetime-extended by a declaration |
3224 | class LifetimeExtendedTemporaryDecl final |
3225 | : public Decl, |
3226 | public Mergeable<LifetimeExtendedTemporaryDecl> { |
3227 | friend class MaterializeTemporaryExpr; |
3228 | friend class ASTDeclReader; |
3229 | |
3230 | Stmt *ExprWithTemporary = nullptr; |
3231 | |
3232 | /// The declaration which lifetime-extended this reference, if any. |
3233 | /// Either a VarDecl, or (for a ctor-initializer) a FieldDecl. |
3234 | ValueDecl *ExtendingDecl = nullptr; |
3235 | unsigned ManglingNumber; |
3236 | |
3237 | mutable APValue *Value = nullptr; |
3238 | |
3239 | virtual void anchor(); |
3240 | |
3241 | LifetimeExtendedTemporaryDecl(Expr *Temp, ValueDecl *EDecl, unsigned Mangling) |
3242 | : Decl(Decl::LifetimeExtendedTemporary, EDecl->getDeclContext(), |
3243 | EDecl->getLocation()), |
3244 | ExprWithTemporary(Temp), ExtendingDecl(EDecl), |
3245 | ManglingNumber(Mangling) {} |
3246 | |
3247 | LifetimeExtendedTemporaryDecl(EmptyShell) |
3248 | : Decl(Decl::LifetimeExtendedTemporary, EmptyShell{}) {} |
3249 | |
3250 | public: |
3251 | static LifetimeExtendedTemporaryDecl *Create(Expr *Temp, ValueDecl *EDec, |
3252 | unsigned Mangling) { |
3253 | return new (EDec->getASTContext(), EDec->getDeclContext()) |
3254 | LifetimeExtendedTemporaryDecl(Temp, EDec, Mangling); |
3255 | } |
3256 | static LifetimeExtendedTemporaryDecl *CreateDeserialized(ASTContext &C, |
3257 | DeclID ID) { |
3258 | return new (C, ID) LifetimeExtendedTemporaryDecl(EmptyShell{}); |
3259 | } |
3260 | |
3261 | ValueDecl *getExtendingDecl() { return ExtendingDecl; } |
3262 | const ValueDecl *getExtendingDecl() const { return ExtendingDecl; } |
3263 | |
3264 | /// Retrieve the storage duration for the materialized temporary. |
3265 | StorageDuration getStorageDuration() const; |
3266 | |
3267 | /// Retrieve the expression to which the temporary materialization conversion |
3268 | /// was applied. This isn't necessarily the initializer of the temporary due |
3269 | /// to the C++98 delayed materialization rules, but |
3270 | /// skipRValueSubobjectAdjustments can be used to find said initializer within |
3271 | /// the subexpression. |
3272 | Expr *getTemporaryExpr() { return cast<Expr>(Val: ExprWithTemporary); } |
3273 | const Expr *getTemporaryExpr() const { return cast<Expr>(Val: ExprWithTemporary); } |
3274 | |
3275 | unsigned getManglingNumber() const { return ManglingNumber; } |
3276 | |
3277 | /// Get the storage for the constant value of a materialized temporary |
3278 | /// of static storage duration. |
3279 | APValue *getOrCreateValue(bool MayCreate) const; |
3280 | |
3281 | APValue *getValue() const { return Value; } |
3282 | |
3283 | // Iterators |
3284 | Stmt::child_range childrenExpr() { |
3285 | return Stmt::child_range(&ExprWithTemporary, &ExprWithTemporary + 1); |
3286 | } |
3287 | |
3288 | Stmt::const_child_range childrenExpr() const { |
3289 | return Stmt::const_child_range(&ExprWithTemporary, &ExprWithTemporary + 1); |
3290 | } |
3291 | |
3292 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3293 | static bool classofKind(Kind K) { |
3294 | return K == Decl::LifetimeExtendedTemporary; |
3295 | } |
3296 | }; |
3297 | |
3298 | /// Represents a shadow declaration implicitly introduced into a scope by a |
3299 | /// (resolved) using-declaration or using-enum-declaration to achieve |
3300 | /// the desired lookup semantics. |
3301 | /// |
3302 | /// For example: |
3303 | /// \code |
3304 | /// namespace A { |
3305 | /// void foo(); |
3306 | /// void foo(int); |
3307 | /// struct foo {}; |
3308 | /// enum bar { bar1, bar2 }; |
3309 | /// } |
3310 | /// namespace B { |
3311 | /// // add a UsingDecl and three UsingShadowDecls (named foo) to B. |
3312 | /// using A::foo; |
3313 | /// // adds UsingEnumDecl and two UsingShadowDecls (named bar1 and bar2) to B. |
3314 | /// using enum A::bar; |
3315 | /// } |
3316 | /// \endcode |
3317 | class UsingShadowDecl : public NamedDecl, public Redeclarable<UsingShadowDecl> { |
3318 | friend class BaseUsingDecl; |
3319 | |
3320 | /// The referenced declaration. |
3321 | NamedDecl *Underlying = nullptr; |
3322 | |
3323 | /// The using declaration which introduced this decl or the next using |
3324 | /// shadow declaration contained in the aforementioned using declaration. |
3325 | NamedDecl *UsingOrNextShadow = nullptr; |
3326 | |
3327 | void anchor() override; |
3328 | |
3329 | using redeclarable_base = Redeclarable<UsingShadowDecl>; |
3330 | |
3331 | UsingShadowDecl *getNextRedeclarationImpl() override { |
3332 | return getNextRedeclaration(); |
3333 | } |
3334 | |
3335 | UsingShadowDecl *getPreviousDeclImpl() override { |
3336 | return getPreviousDecl(); |
3337 | } |
3338 | |
3339 | UsingShadowDecl *getMostRecentDeclImpl() override { |
3340 | return getMostRecentDecl(); |
3341 | } |
3342 | |
3343 | protected: |
3344 | UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC, SourceLocation Loc, |
3345 | DeclarationName Name, BaseUsingDecl *Introducer, |
3346 | NamedDecl *Target); |
3347 | UsingShadowDecl(Kind K, ASTContext &C, EmptyShell); |
3348 | |
3349 | public: |
3350 | friend class ASTDeclReader; |
3351 | friend class ASTDeclWriter; |
3352 | |
3353 | static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC, |
3354 | SourceLocation Loc, DeclarationName Name, |
3355 | BaseUsingDecl *Introducer, NamedDecl *Target) { |
3356 | return new (C, DC) |
3357 | UsingShadowDecl(UsingShadow, C, DC, Loc, Name, Introducer, Target); |
3358 | } |
3359 | |
3360 | static UsingShadowDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3361 | |
3362 | using redecl_range = redeclarable_base::redecl_range; |
3363 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3364 | |
3365 | using redeclarable_base::redecls_begin; |
3366 | using redeclarable_base::redecls_end; |
3367 | using redeclarable_base::redecls; |
3368 | using redeclarable_base::getPreviousDecl; |
3369 | using redeclarable_base::getMostRecentDecl; |
3370 | using redeclarable_base::isFirstDecl; |
3371 | |
3372 | UsingShadowDecl *getCanonicalDecl() override { |
3373 | return getFirstDecl(); |
3374 | } |
3375 | const UsingShadowDecl *getCanonicalDecl() const { |
3376 | return getFirstDecl(); |
3377 | } |
3378 | |
3379 | /// Gets the underlying declaration which has been brought into the |
3380 | /// local scope. |
3381 | NamedDecl *getTargetDecl() const { return Underlying; } |
3382 | |
3383 | /// Sets the underlying declaration which has been brought into the |
3384 | /// local scope. |
3385 | void setTargetDecl(NamedDecl *ND) { |
3386 | assert(ND && "Target decl is null!" ); |
3387 | Underlying = ND; |
3388 | // A UsingShadowDecl is never a friend or local extern declaration, even |
3389 | // if it is a shadow declaration for one. |
3390 | IdentifierNamespace = |
3391 | ND->getIdentifierNamespace() & |
3392 | ~(IDNS_OrdinaryFriend | IDNS_TagFriend | IDNS_LocalExtern); |
3393 | } |
3394 | |
3395 | /// Gets the (written or instantiated) using declaration that introduced this |
3396 | /// declaration. |
3397 | BaseUsingDecl *getIntroducer() const; |
3398 | |
3399 | /// The next using shadow declaration contained in the shadow decl |
3400 | /// chain of the using declaration which introduced this decl. |
3401 | UsingShadowDecl *getNextUsingShadowDecl() const { |
3402 | return dyn_cast_or_null<UsingShadowDecl>(Val: UsingOrNextShadow); |
3403 | } |
3404 | |
3405 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3406 | static bool classofKind(Kind K) { |
3407 | return K == Decl::UsingShadow || K == Decl::ConstructorUsingShadow; |
3408 | } |
3409 | }; |
3410 | |
3411 | /// Represents a C++ declaration that introduces decls from somewhere else. It |
3412 | /// provides a set of the shadow decls so introduced. |
3413 | |
3414 | class BaseUsingDecl : public NamedDecl { |
3415 | /// The first shadow declaration of the shadow decl chain associated |
3416 | /// with this using declaration. |
3417 | /// |
3418 | /// The bool member of the pair is a bool flag a derived type may use |
3419 | /// (UsingDecl makes use of it). |
3420 | llvm::PointerIntPair<UsingShadowDecl *, 1, bool> FirstUsingShadow; |
3421 | |
3422 | protected: |
3423 | BaseUsingDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) |
3424 | : NamedDecl(DK, DC, L, N), FirstUsingShadow(nullptr, false) {} |
3425 | |
3426 | private: |
3427 | void anchor() override; |
3428 | |
3429 | protected: |
3430 | /// A bool flag for use by a derived type |
3431 | bool getShadowFlag() const { return FirstUsingShadow.getInt(); } |
3432 | |
3433 | /// A bool flag a derived type may set |
3434 | void setShadowFlag(bool V) { FirstUsingShadow.setInt(V); } |
3435 | |
3436 | public: |
3437 | friend class ASTDeclReader; |
3438 | friend class ASTDeclWriter; |
3439 | |
3440 | /// Iterates through the using shadow declarations associated with |
3441 | /// this using declaration. |
3442 | class shadow_iterator { |
3443 | /// The current using shadow declaration. |
3444 | UsingShadowDecl *Current = nullptr; |
3445 | |
3446 | public: |
3447 | using value_type = UsingShadowDecl *; |
3448 | using reference = UsingShadowDecl *; |
3449 | using pointer = UsingShadowDecl *; |
3450 | using iterator_category = std::forward_iterator_tag; |
3451 | using difference_type = std::ptrdiff_t; |
3452 | |
3453 | shadow_iterator() = default; |
3454 | explicit shadow_iterator(UsingShadowDecl *C) : Current(C) {} |
3455 | |
3456 | reference operator*() const { return Current; } |
3457 | pointer operator->() const { return Current; } |
3458 | |
3459 | shadow_iterator &operator++() { |
3460 | Current = Current->getNextUsingShadowDecl(); |
3461 | return *this; |
3462 | } |
3463 | |
3464 | shadow_iterator operator++(int) { |
3465 | shadow_iterator tmp(*this); |
3466 | ++(*this); |
3467 | return tmp; |
3468 | } |
3469 | |
3470 | friend bool operator==(shadow_iterator x, shadow_iterator y) { |
3471 | return x.Current == y.Current; |
3472 | } |
3473 | friend bool operator!=(shadow_iterator x, shadow_iterator y) { |
3474 | return x.Current != y.Current; |
3475 | } |
3476 | }; |
3477 | |
3478 | using shadow_range = llvm::iterator_range<shadow_iterator>; |
3479 | |
3480 | shadow_range shadows() const { |
3481 | return shadow_range(shadow_begin(), shadow_end()); |
3482 | } |
3483 | |
3484 | shadow_iterator shadow_begin() const { |
3485 | return shadow_iterator(FirstUsingShadow.getPointer()); |
3486 | } |
3487 | |
3488 | shadow_iterator shadow_end() const { return shadow_iterator(); } |
3489 | |
3490 | /// Return the number of shadowed declarations associated with this |
3491 | /// using declaration. |
3492 | unsigned shadow_size() const { |
3493 | return std::distance(first: shadow_begin(), last: shadow_end()); |
3494 | } |
3495 | |
3496 | void addShadowDecl(UsingShadowDecl *S); |
3497 | void removeShadowDecl(UsingShadowDecl *S); |
3498 | |
3499 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3500 | static bool classofKind(Kind K) { return K == Using || K == UsingEnum; } |
3501 | }; |
3502 | |
3503 | /// Represents a C++ using-declaration. |
3504 | /// |
3505 | /// For example: |
3506 | /// \code |
3507 | /// using someNameSpace::someIdentifier; |
3508 | /// \endcode |
3509 | class UsingDecl : public BaseUsingDecl, public Mergeable<UsingDecl> { |
3510 | /// The source location of the 'using' keyword itself. |
3511 | SourceLocation UsingLocation; |
3512 | |
3513 | /// The nested-name-specifier that precedes the name. |
3514 | NestedNameSpecifierLoc QualifierLoc; |
3515 | |
3516 | /// Provides source/type location info for the declaration name |
3517 | /// embedded in the ValueDecl base class. |
3518 | DeclarationNameLoc DNLoc; |
3519 | |
3520 | UsingDecl(DeclContext *DC, SourceLocation UL, |
3521 | NestedNameSpecifierLoc QualifierLoc, |
3522 | const DeclarationNameInfo &NameInfo, bool HasTypenameKeyword) |
3523 | : BaseUsingDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()), |
3524 | UsingLocation(UL), QualifierLoc(QualifierLoc), |
3525 | DNLoc(NameInfo.getInfo()) { |
3526 | setShadowFlag(HasTypenameKeyword); |
3527 | } |
3528 | |
3529 | void anchor() override; |
3530 | |
3531 | public: |
3532 | friend class ASTDeclReader; |
3533 | friend class ASTDeclWriter; |
3534 | |
3535 | /// Return the source location of the 'using' keyword. |
3536 | SourceLocation getUsingLoc() const { return UsingLocation; } |
3537 | |
3538 | /// Set the source location of the 'using' keyword. |
3539 | void setUsingLoc(SourceLocation L) { UsingLocation = L; } |
3540 | |
3541 | /// Retrieve the nested-name-specifier that qualifies the name, |
3542 | /// with source-location information. |
3543 | NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } |
3544 | |
3545 | /// Retrieve the nested-name-specifier that qualifies the name. |
3546 | NestedNameSpecifier *getQualifier() const { |
3547 | return QualifierLoc.getNestedNameSpecifier(); |
3548 | } |
3549 | |
3550 | DeclarationNameInfo getNameInfo() const { |
3551 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
3552 | } |
3553 | |
3554 | /// Return true if it is a C++03 access declaration (no 'using'). |
3555 | bool isAccessDeclaration() const { return UsingLocation.isInvalid(); } |
3556 | |
3557 | /// Return true if the using declaration has 'typename'. |
3558 | bool hasTypename() const { return getShadowFlag(); } |
3559 | |
3560 | /// Sets whether the using declaration has 'typename'. |
3561 | void setTypename(bool TN) { setShadowFlag(TN); } |
3562 | |
3563 | static UsingDecl *Create(ASTContext &C, DeclContext *DC, |
3564 | SourceLocation UsingL, |
3565 | NestedNameSpecifierLoc QualifierLoc, |
3566 | const DeclarationNameInfo &NameInfo, |
3567 | bool HasTypenameKeyword); |
3568 | |
3569 | static UsingDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3570 | |
3571 | SourceRange getSourceRange() const override LLVM_READONLY; |
3572 | |
3573 | /// Retrieves the canonical declaration of this declaration. |
3574 | UsingDecl *getCanonicalDecl() override { |
3575 | return cast<UsingDecl>(getFirstDecl()); |
3576 | } |
3577 | const UsingDecl *getCanonicalDecl() const { |
3578 | return cast<UsingDecl>(getFirstDecl()); |
3579 | } |
3580 | |
3581 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3582 | static bool classofKind(Kind K) { return K == Using; } |
3583 | }; |
3584 | |
3585 | /// Represents a shadow constructor declaration introduced into a |
3586 | /// class by a C++11 using-declaration that names a constructor. |
3587 | /// |
3588 | /// For example: |
3589 | /// \code |
3590 | /// struct Base { Base(int); }; |
3591 | /// struct Derived { |
3592 | /// using Base::Base; // creates a UsingDecl and a ConstructorUsingShadowDecl |
3593 | /// }; |
3594 | /// \endcode |
3595 | class ConstructorUsingShadowDecl final : public UsingShadowDecl { |
3596 | /// If this constructor using declaration inherted the constructor |
3597 | /// from an indirect base class, this is the ConstructorUsingShadowDecl |
3598 | /// in the named direct base class from which the declaration was inherited. |
3599 | ConstructorUsingShadowDecl *NominatedBaseClassShadowDecl = nullptr; |
3600 | |
3601 | /// If this constructor using declaration inherted the constructor |
3602 | /// from an indirect base class, this is the ConstructorUsingShadowDecl |
3603 | /// that will be used to construct the unique direct or virtual base class |
3604 | /// that receives the constructor arguments. |
3605 | ConstructorUsingShadowDecl *ConstructedBaseClassShadowDecl = nullptr; |
3606 | |
3607 | /// \c true if the constructor ultimately named by this using shadow |
3608 | /// declaration is within a virtual base class subobject of the class that |
3609 | /// contains this declaration. |
3610 | LLVM_PREFERRED_TYPE(bool) |
3611 | unsigned IsVirtual : 1; |
3612 | |
3613 | ConstructorUsingShadowDecl(ASTContext &C, DeclContext *DC, SourceLocation Loc, |
3614 | UsingDecl *Using, NamedDecl *Target, |
3615 | bool TargetInVirtualBase) |
3616 | : UsingShadowDecl(ConstructorUsingShadow, C, DC, Loc, |
3617 | Using->getDeclName(), Using, |
3618 | Target->getUnderlyingDecl()), |
3619 | NominatedBaseClassShadowDecl( |
3620 | dyn_cast<ConstructorUsingShadowDecl>(Val: Target)), |
3621 | ConstructedBaseClassShadowDecl(NominatedBaseClassShadowDecl), |
3622 | IsVirtual(TargetInVirtualBase) { |
3623 | // If we found a constructor that chains to a constructor for a virtual |
3624 | // base, we should directly call that virtual base constructor instead. |
3625 | // FIXME: This logic belongs in Sema. |
3626 | if (NominatedBaseClassShadowDecl && |
3627 | NominatedBaseClassShadowDecl->constructsVirtualBase()) { |
3628 | ConstructedBaseClassShadowDecl = |
3629 | NominatedBaseClassShadowDecl->ConstructedBaseClassShadowDecl; |
3630 | IsVirtual = true; |
3631 | } |
3632 | } |
3633 | |
3634 | ConstructorUsingShadowDecl(ASTContext &C, EmptyShell Empty) |
3635 | : UsingShadowDecl(ConstructorUsingShadow, C, Empty), IsVirtual(false) {} |
3636 | |
3637 | void anchor() override; |
3638 | |
3639 | public: |
3640 | friend class ASTDeclReader; |
3641 | friend class ASTDeclWriter; |
3642 | |
3643 | static ConstructorUsingShadowDecl *Create(ASTContext &C, DeclContext *DC, |
3644 | SourceLocation Loc, |
3645 | UsingDecl *Using, NamedDecl *Target, |
3646 | bool IsVirtual); |
3647 | static ConstructorUsingShadowDecl *CreateDeserialized(ASTContext &C, |
3648 | DeclID ID); |
3649 | |
3650 | /// Override the UsingShadowDecl's getIntroducer, returning the UsingDecl that |
3651 | /// introduced this. |
3652 | UsingDecl *getIntroducer() const { |
3653 | return cast<UsingDecl>(UsingShadowDecl::getIntroducer()); |
3654 | } |
3655 | |
3656 | /// Returns the parent of this using shadow declaration, which |
3657 | /// is the class in which this is declared. |
3658 | //@{ |
3659 | const CXXRecordDecl *getParent() const { |
3660 | return cast<CXXRecordDecl>(getDeclContext()); |
3661 | } |
3662 | CXXRecordDecl *getParent() { |
3663 | return cast<CXXRecordDecl>(getDeclContext()); |
3664 | } |
3665 | //@} |
3666 | |
3667 | /// Get the inheriting constructor declaration for the direct base |
3668 | /// class from which this using shadow declaration was inherited, if there is |
3669 | /// one. This can be different for each redeclaration of the same shadow decl. |
3670 | ConstructorUsingShadowDecl *getNominatedBaseClassShadowDecl() const { |
3671 | return NominatedBaseClassShadowDecl; |
3672 | } |
3673 | |
3674 | /// Get the inheriting constructor declaration for the base class |
3675 | /// for which we don't have an explicit initializer, if there is one. |
3676 | ConstructorUsingShadowDecl *getConstructedBaseClassShadowDecl() const { |
3677 | return ConstructedBaseClassShadowDecl; |
3678 | } |
3679 | |
3680 | /// Get the base class that was named in the using declaration. This |
3681 | /// can be different for each redeclaration of this same shadow decl. |
3682 | CXXRecordDecl *getNominatedBaseClass() const; |
3683 | |
3684 | /// Get the base class whose constructor or constructor shadow |
3685 | /// declaration is passed the constructor arguments. |
3686 | CXXRecordDecl *getConstructedBaseClass() const { |
3687 | return cast<CXXRecordDecl>((ConstructedBaseClassShadowDecl |
3688 | ? ConstructedBaseClassShadowDecl |
3689 | : getTargetDecl()) |
3690 | ->getDeclContext()); |
3691 | } |
3692 | |
3693 | /// Returns \c true if the constructed base class is a virtual base |
3694 | /// class subobject of this declaration's class. |
3695 | bool constructsVirtualBase() const { |
3696 | return IsVirtual; |
3697 | } |
3698 | |
3699 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3700 | static bool classofKind(Kind K) { return K == ConstructorUsingShadow; } |
3701 | }; |
3702 | |
3703 | /// Represents a C++ using-enum-declaration. |
3704 | /// |
3705 | /// For example: |
3706 | /// \code |
3707 | /// using enum SomeEnumTag ; |
3708 | /// \endcode |
3709 | |
3710 | class UsingEnumDecl : public BaseUsingDecl, public Mergeable<UsingEnumDecl> { |
3711 | /// The source location of the 'using' keyword itself. |
3712 | SourceLocation UsingLocation; |
3713 | /// The source location of the 'enum' keyword. |
3714 | SourceLocation EnumLocation; |
3715 | /// 'qual::SomeEnum' as an EnumType, possibly with Elaborated/Typedef sugar. |
3716 | TypeSourceInfo *EnumType; |
3717 | |
3718 | UsingEnumDecl(DeclContext *DC, DeclarationName DN, SourceLocation UL, |
3719 | SourceLocation EL, SourceLocation NL, TypeSourceInfo *EnumType) |
3720 | : BaseUsingDecl(UsingEnum, DC, NL, DN), UsingLocation(UL), EnumLocation(EL), |
3721 | EnumType(EnumType){} |
3722 | |
3723 | void anchor() override; |
3724 | |
3725 | public: |
3726 | friend class ASTDeclReader; |
3727 | friend class ASTDeclWriter; |
3728 | |
3729 | /// The source location of the 'using' keyword. |
3730 | SourceLocation getUsingLoc() const { return UsingLocation; } |
3731 | void setUsingLoc(SourceLocation L) { UsingLocation = L; } |
3732 | |
3733 | /// The source location of the 'enum' keyword. |
3734 | SourceLocation getEnumLoc() const { return EnumLocation; } |
3735 | void setEnumLoc(SourceLocation L) { EnumLocation = L; } |
3736 | NestedNameSpecifier *getQualifier() const { |
3737 | return getQualifierLoc().getNestedNameSpecifier(); |
3738 | } |
3739 | NestedNameSpecifierLoc getQualifierLoc() const { |
3740 | if (auto ETL = EnumType->getTypeLoc().getAs<ElaboratedTypeLoc>()) |
3741 | return ETL.getQualifierLoc(); |
3742 | return NestedNameSpecifierLoc(); |
3743 | } |
3744 | // Returns the "qualifier::Name" part as a TypeLoc. |
3745 | TypeLoc getEnumTypeLoc() const { |
3746 | return EnumType->getTypeLoc(); |
3747 | } |
3748 | TypeSourceInfo *getEnumType() const { |
3749 | return EnumType; |
3750 | } |
3751 | void setEnumType(TypeSourceInfo *TSI) { EnumType = TSI; } |
3752 | |
3753 | public: |
3754 | EnumDecl *getEnumDecl() const { return cast<EnumDecl>(Val: EnumType->getType()->getAsTagDecl()); } |
3755 | |
3756 | static UsingEnumDecl *Create(ASTContext &C, DeclContext *DC, |
3757 | SourceLocation UsingL, SourceLocation EnumL, |
3758 | SourceLocation NameL, TypeSourceInfo *EnumType); |
3759 | |
3760 | static UsingEnumDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3761 | |
3762 | SourceRange getSourceRange() const override LLVM_READONLY; |
3763 | |
3764 | /// Retrieves the canonical declaration of this declaration. |
3765 | UsingEnumDecl *getCanonicalDecl() override { |
3766 | return cast<UsingEnumDecl>(getFirstDecl()); |
3767 | } |
3768 | const UsingEnumDecl *getCanonicalDecl() const { |
3769 | return cast<UsingEnumDecl>(getFirstDecl()); |
3770 | } |
3771 | |
3772 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3773 | static bool classofKind(Kind K) { return K == UsingEnum; } |
3774 | }; |
3775 | |
3776 | /// Represents a pack of using declarations that a single |
3777 | /// using-declarator pack-expanded into. |
3778 | /// |
3779 | /// \code |
3780 | /// template<typename ...T> struct X : T... { |
3781 | /// using T::operator()...; |
3782 | /// using T::operator T...; |
3783 | /// }; |
3784 | /// \endcode |
3785 | /// |
3786 | /// In the second case above, the UsingPackDecl will have the name |
3787 | /// 'operator T' (which contains an unexpanded pack), but the individual |
3788 | /// UsingDecls and UsingShadowDecls will have more reasonable names. |
3789 | class UsingPackDecl final |
3790 | : public NamedDecl, public Mergeable<UsingPackDecl>, |
3791 | private llvm::TrailingObjects<UsingPackDecl, NamedDecl *> { |
3792 | /// The UnresolvedUsingValueDecl or UnresolvedUsingTypenameDecl from |
3793 | /// which this waas instantiated. |
3794 | NamedDecl *InstantiatedFrom; |
3795 | |
3796 | /// The number of using-declarations created by this pack expansion. |
3797 | unsigned NumExpansions; |
3798 | |
3799 | UsingPackDecl(DeclContext *DC, NamedDecl *InstantiatedFrom, |
3800 | ArrayRef<NamedDecl *> UsingDecls) |
3801 | : NamedDecl(UsingPack, DC, |
3802 | InstantiatedFrom ? InstantiatedFrom->getLocation() |
3803 | : SourceLocation(), |
3804 | InstantiatedFrom ? InstantiatedFrom->getDeclName() |
3805 | : DeclarationName()), |
3806 | InstantiatedFrom(InstantiatedFrom), NumExpansions(UsingDecls.size()) { |
3807 | std::uninitialized_copy(UsingDecls.begin(), UsingDecls.end(), |
3808 | getTrailingObjects<NamedDecl *>()); |
3809 | } |
3810 | |
3811 | void anchor() override; |
3812 | |
3813 | public: |
3814 | friend class ASTDeclReader; |
3815 | friend class ASTDeclWriter; |
3816 | friend TrailingObjects; |
3817 | |
3818 | /// Get the using declaration from which this was instantiated. This will |
3819 | /// always be an UnresolvedUsingValueDecl or an UnresolvedUsingTypenameDecl |
3820 | /// that is a pack expansion. |
3821 | NamedDecl *getInstantiatedFromUsingDecl() const { return InstantiatedFrom; } |
3822 | |
3823 | /// Get the set of using declarations that this pack expanded into. Note that |
3824 | /// some of these may still be unresolved. |
3825 | ArrayRef<NamedDecl *> expansions() const { |
3826 | return llvm::ArrayRef(getTrailingObjects<NamedDecl *>(), NumExpansions); |
3827 | } |
3828 | |
3829 | static UsingPackDecl *Create(ASTContext &C, DeclContext *DC, |
3830 | NamedDecl *InstantiatedFrom, |
3831 | ArrayRef<NamedDecl *> UsingDecls); |
3832 | |
3833 | static UsingPackDecl *CreateDeserialized(ASTContext &C, DeclID ID, |
3834 | unsigned NumExpansions); |
3835 | |
3836 | SourceRange getSourceRange() const override LLVM_READONLY { |
3837 | return InstantiatedFrom->getSourceRange(); |
3838 | } |
3839 | |
3840 | UsingPackDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3841 | const UsingPackDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3842 | |
3843 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3844 | static bool classofKind(Kind K) { return K == UsingPack; } |
3845 | }; |
3846 | |
3847 | /// Represents a dependent using declaration which was not marked with |
3848 | /// \c typename. |
3849 | /// |
3850 | /// Unlike non-dependent using declarations, these *only* bring through |
3851 | /// non-types; otherwise they would break two-phase lookup. |
3852 | /// |
3853 | /// \code |
3854 | /// template \<class T> class A : public Base<T> { |
3855 | /// using Base<T>::foo; |
3856 | /// }; |
3857 | /// \endcode |
3858 | class UnresolvedUsingValueDecl : public ValueDecl, |
3859 | public Mergeable<UnresolvedUsingValueDecl> { |
3860 | /// The source location of the 'using' keyword |
3861 | SourceLocation UsingLocation; |
3862 | |
3863 | /// If this is a pack expansion, the location of the '...'. |
3864 | SourceLocation EllipsisLoc; |
3865 | |
3866 | /// The nested-name-specifier that precedes the name. |
3867 | NestedNameSpecifierLoc QualifierLoc; |
3868 | |
3869 | /// Provides source/type location info for the declaration name |
3870 | /// embedded in the ValueDecl base class. |
3871 | DeclarationNameLoc DNLoc; |
3872 | |
3873 | UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty, |
3874 | SourceLocation UsingLoc, |
3875 | NestedNameSpecifierLoc QualifierLoc, |
3876 | const DeclarationNameInfo &NameInfo, |
3877 | SourceLocation EllipsisLoc) |
3878 | : ValueDecl(UnresolvedUsingValue, DC, |
3879 | NameInfo.getLoc(), NameInfo.getName(), Ty), |
3880 | UsingLocation(UsingLoc), EllipsisLoc(EllipsisLoc), |
3881 | QualifierLoc(QualifierLoc), DNLoc(NameInfo.getInfo()) {} |
3882 | |
3883 | void anchor() override; |
3884 | |
3885 | public: |
3886 | friend class ASTDeclReader; |
3887 | friend class ASTDeclWriter; |
3888 | |
3889 | /// Returns the source location of the 'using' keyword. |
3890 | SourceLocation getUsingLoc() const { return UsingLocation; } |
3891 | |
3892 | /// Set the source location of the 'using' keyword. |
3893 | void setUsingLoc(SourceLocation L) { UsingLocation = L; } |
3894 | |
3895 | /// Return true if it is a C++03 access declaration (no 'using'). |
3896 | bool isAccessDeclaration() const { return UsingLocation.isInvalid(); } |
3897 | |
3898 | /// Retrieve the nested-name-specifier that qualifies the name, |
3899 | /// with source-location information. |
3900 | NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } |
3901 | |
3902 | /// Retrieve the nested-name-specifier that qualifies the name. |
3903 | NestedNameSpecifier *getQualifier() const { |
3904 | return QualifierLoc.getNestedNameSpecifier(); |
3905 | } |
3906 | |
3907 | DeclarationNameInfo getNameInfo() const { |
3908 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
3909 | } |
3910 | |
3911 | /// Determine whether this is a pack expansion. |
3912 | bool isPackExpansion() const { |
3913 | return EllipsisLoc.isValid(); |
3914 | } |
3915 | |
3916 | /// Get the location of the ellipsis if this is a pack expansion. |
3917 | SourceLocation getEllipsisLoc() const { |
3918 | return EllipsisLoc; |
3919 | } |
3920 | |
3921 | static UnresolvedUsingValueDecl * |
3922 | Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, |
3923 | NestedNameSpecifierLoc QualifierLoc, |
3924 | const DeclarationNameInfo &NameInfo, SourceLocation EllipsisLoc); |
3925 | |
3926 | static UnresolvedUsingValueDecl * |
3927 | CreateDeserialized(ASTContext &C, DeclID ID); |
3928 | |
3929 | SourceRange getSourceRange() const override LLVM_READONLY; |
3930 | |
3931 | /// Retrieves the canonical declaration of this declaration. |
3932 | UnresolvedUsingValueDecl *getCanonicalDecl() override { |
3933 | return getFirstDecl(); |
3934 | } |
3935 | const UnresolvedUsingValueDecl *getCanonicalDecl() const { |
3936 | return getFirstDecl(); |
3937 | } |
3938 | |
3939 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3940 | static bool classofKind(Kind K) { return K == UnresolvedUsingValue; } |
3941 | }; |
3942 | |
3943 | /// Represents a dependent using declaration which was marked with |
3944 | /// \c typename. |
3945 | /// |
3946 | /// \code |
3947 | /// template \<class T> class A : public Base<T> { |
3948 | /// using typename Base<T>::foo; |
3949 | /// }; |
3950 | /// \endcode |
3951 | /// |
3952 | /// The type associated with an unresolved using typename decl is |
3953 | /// currently always a typename type. |
3954 | class UnresolvedUsingTypenameDecl |
3955 | : public TypeDecl, |
3956 | public Mergeable<UnresolvedUsingTypenameDecl> { |
3957 | friend class ASTDeclReader; |
3958 | |
3959 | /// The source location of the 'typename' keyword |
3960 | SourceLocation TypenameLocation; |
3961 | |
3962 | /// If this is a pack expansion, the location of the '...'. |
3963 | SourceLocation EllipsisLoc; |
3964 | |
3965 | /// The nested-name-specifier that precedes the name. |
3966 | NestedNameSpecifierLoc QualifierLoc; |
3967 | |
3968 | UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc, |
3969 | SourceLocation TypenameLoc, |
3970 | NestedNameSpecifierLoc QualifierLoc, |
3971 | SourceLocation TargetNameLoc, |
3972 | IdentifierInfo *TargetName, |
3973 | SourceLocation EllipsisLoc) |
3974 | : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName, |
3975 | UsingLoc), |
3976 | TypenameLocation(TypenameLoc), EllipsisLoc(EllipsisLoc), |
3977 | QualifierLoc(QualifierLoc) {} |
3978 | |
3979 | void anchor() override; |
3980 | |
3981 | public: |
3982 | /// Returns the source location of the 'using' keyword. |
3983 | SourceLocation getUsingLoc() const { return getBeginLoc(); } |
3984 | |
3985 | /// Returns the source location of the 'typename' keyword. |
3986 | SourceLocation getTypenameLoc() const { return TypenameLocation; } |
3987 | |
3988 | /// Retrieve the nested-name-specifier that qualifies the name, |
3989 | /// with source-location information. |
3990 | NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } |
3991 | |
3992 | /// Retrieve the nested-name-specifier that qualifies the name. |
3993 | NestedNameSpecifier *getQualifier() const { |
3994 | return QualifierLoc.getNestedNameSpecifier(); |
3995 | } |
3996 | |
3997 | DeclarationNameInfo getNameInfo() const { |
3998 | return DeclarationNameInfo(getDeclName(), getLocation()); |
3999 | } |
4000 | |
4001 | /// Determine whether this is a pack expansion. |
4002 | bool isPackExpansion() const { |
4003 | return EllipsisLoc.isValid(); |
4004 | } |
4005 | |
4006 | /// Get the location of the ellipsis if this is a pack expansion. |
4007 | SourceLocation getEllipsisLoc() const { |
4008 | return EllipsisLoc; |
4009 | } |
4010 | |
4011 | static UnresolvedUsingTypenameDecl * |
4012 | Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, |
4013 | SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc, |
4014 | SourceLocation TargetNameLoc, DeclarationName TargetName, |
4015 | SourceLocation EllipsisLoc); |
4016 | |
4017 | static UnresolvedUsingTypenameDecl * |
4018 | CreateDeserialized(ASTContext &C, DeclID ID); |
4019 | |
4020 | /// Retrieves the canonical declaration of this declaration. |
4021 | UnresolvedUsingTypenameDecl *getCanonicalDecl() override { |
4022 | return getFirstDecl(); |
4023 | } |
4024 | const UnresolvedUsingTypenameDecl *getCanonicalDecl() const { |
4025 | return getFirstDecl(); |
4026 | } |
4027 | |
4028 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
4029 | static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; } |
4030 | }; |
4031 | |
4032 | /// This node is generated when a using-declaration that was annotated with |
4033 | /// __attribute__((using_if_exists)) failed to resolve to a known declaration. |
4034 | /// In that case, Sema builds a UsingShadowDecl whose target is an instance of |
4035 | /// this declaration, adding it to the current scope. Referring to this |
4036 | /// declaration in any way is an error. |
4037 | class UnresolvedUsingIfExistsDecl final : public NamedDecl { |
4038 | UnresolvedUsingIfExistsDecl(DeclContext *DC, SourceLocation Loc, |
4039 | DeclarationName Name); |
4040 | |
4041 | void anchor() override; |
4042 | |
4043 | public: |
4044 | static UnresolvedUsingIfExistsDecl *Create(ASTContext &Ctx, DeclContext *DC, |
4045 | SourceLocation Loc, |
4046 | DeclarationName Name); |
4047 | static UnresolvedUsingIfExistsDecl *CreateDeserialized(ASTContext &Ctx, |
4048 | DeclID ID); |
4049 | |
4050 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
4051 | static bool classofKind(Kind K) { return K == Decl::UnresolvedUsingIfExists; } |
4052 | }; |
4053 | |
4054 | /// Represents a C++11 static_assert declaration. |
4055 | class StaticAssertDecl : public Decl { |
4056 | llvm::PointerIntPair<Expr *, 1, bool> AssertExprAndFailed; |
4057 | Expr *Message; |
4058 | SourceLocation RParenLoc; |
4059 | |
4060 | StaticAssertDecl(DeclContext *DC, SourceLocation StaticAssertLoc, |
4061 | Expr *AssertExpr, Expr *Message, SourceLocation RParenLoc, |
4062 | bool Failed) |
4063 | : Decl(StaticAssert, DC, StaticAssertLoc), |
4064 | |
---|