clang: lib/AST/TypeLoc.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/MathExtras.h"
27#include
28#include
29#include
30#include
31
32using namespace clang;
33
35
36
37
38
39
40namespace {
41
42class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
43public:
44#define ABSTRACT_TYPELOC(CLASS, PARENT)
45#define TYPELOC(CLASS, PARENT) \
46 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
47 return TyLoc.getLocalSourceRange(); \
48 }
49#include "clang/AST/TypeLocNodes.def"
50};
51
52}
53
56 return TypeLocRanger().Visit(TL);
57}
58
59namespace {
60
61class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
62public:
63#define ABSTRACT_TYPELOC(CLASS, PARENT)
64#define TYPELOC(CLASS, PARENT) \
65 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
66 return TyLoc.getLocalDataAlignment(); \
67 }
68#include "clang/AST/TypeLocNodes.def"
69};
70
71}
72
73
75 if (Ty.isNull()) return 1;
76 return TypeAligner().Visit(TypeLoc(Ty, nullptr));
77}
78
79namespace {
80
81class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
82public:
83#define ABSTRACT_TYPELOC(CLASS, PARENT)
84#define TYPELOC(CLASS, PARENT) \
85 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
86 return TyLoc.getLocalDataSize(); \
87 }
88#include "clang/AST/TypeLocNodes.def"
89};
90
91}
92
93
95 unsigned Total = 0;
97 unsigned MaxAlign = 1;
98 while (!TyLoc.isNull()) {
100 MaxAlign = std::max(Align, MaxAlign);
101 Total = llvm::alignTo(Total, Align);
102 Total += TypeSizer().Visit(TyLoc);
104 }
105 Total = llvm::alignTo(Total, MaxAlign);
106 return Total;
107}
108
109namespace {
110
111class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
112public:
113#define ABSTRACT_TYPELOC(CLASS, PARENT)
114#define TYPELOC(CLASS, PARENT) \
115 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
116 return TyLoc.getNextTypeLoc(); \
117 }
118#include "clang/AST/TypeLocNodes.def"
119};
120
121}
122
123
124
126 return NextLoc().Visit(TL);
127}
128
129
130
131
134 while (true) {
136#define ABSTRACT_TYPELOC(CLASS, PARENT)
137#define TYPELOC(CLASS, PARENT) \
138 case CLASS: { \
139 CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
140 TLCasted.initializeLocal(Context, Loc); \
141 TL = TLCasted.getNextTypeLoc(); \
142 if (!TL) return; \
143 continue; \
144 }
145#include "clang/AST/TypeLocNodes.def"
146 }
147 }
148}
149
150namespace {
151
152class TypeLocCopier : public TypeLocVisitor {
154
155public:
156 TypeLocCopier(TypeLoc source) : Source(source) {}
157
158#define ABSTRACT_TYPELOC(CLASS, PARENT)
159#define TYPELOC(CLASS, PARENT) \
160 void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
161 dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
162 }
163#include "clang/AST/TypeLocNodes.def"
164};
165
166}
167
170
171
172
173
178 llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
181 return;
182 }
183
184
186 do {
187 TypeLocCopier(other).Visit(TL);
190}
191
195 while (true) {
197 case Elaborated:
199 LeftMost = Cur;
200 break;
201 }
204 break;
205 continue;
206 case FunctionProto:
209 LeftMost = Cur;
210 break;
211 }
212 [[fallthrough]];
213 case FunctionNoProto:
214 case ConstantArray:
215 case DependentSizedArray:
216 case IncompleteArray:
217 case VariableArray:
218
221 continue;
222 default:
224 LeftMost = Cur;
227 break;
228 continue;
229 }
230 break;
231 }
233}
234
238 while (true) {
240 default:
243 return Last.getLocalSourceRange().getEnd();
245 case ConstantArray:
246 case DependentSizedArray:
247 case IncompleteArray:
248 case VariableArray:
249 case FunctionNoProto:
250
251
253 break;
254 case FunctionProto:
257 else
259 break;
260 case ObjCObjectPointer:
261
263 break;
264 [[fallthrough]];
266 case BlockPointer:
267 case MemberPointer:
268 case LValueReference:
269 case RValueReference:
270 case PackExpansion:
271
272
275 break;
277 case Elaborated:
278 break;
279 }
281 }
282}
283
284namespace {
285
286struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
287
288 static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
289 static bool isTypeSpec(TypeLoc _) { return false; }
290
291#define ABSTRACT_TYPELOC(CLASS, PARENT)
292#define TYPELOC(CLASS, PARENT) \
293 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
294 return isTypeSpec(TyLoc); \
295 }
296#include "clang/AST/TypeLocNodes.def"
297};
298
299}
300
301
302
303
304
305
306
307
308
309bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
311 return TSTChecker().Visit(TL);
312}
313
316 return D->isCompleteDefinition() &&
318}
319
320
321
322
326 else
329}
330
331
336 case BuiltinType::Void:
338 case BuiltinType::Bool:
340 case BuiltinType::Char_U:
341 case BuiltinType::Char_S:
343 case BuiltinType::Char8:
345 case BuiltinType::Char16:
347 case BuiltinType::Char32:
349 case BuiltinType::WChar_S:
350 case BuiltinType::WChar_U:
352 case BuiltinType::UChar:
353 case BuiltinType::UShort:
354 case BuiltinType::UInt:
355 case BuiltinType::ULong:
356 case BuiltinType::ULongLong:
357 case BuiltinType::UInt128:
358 case BuiltinType::SChar:
359 case BuiltinType::Short:
360 case BuiltinType::Int:
361 case BuiltinType::Long:
362 case BuiltinType::LongLong:
363 case BuiltinType::Int128:
364 case BuiltinType::Half:
365 case BuiltinType::Float:
366 case BuiltinType::Double:
367 case BuiltinType::LongDouble:
368 case BuiltinType::Float16:
369 case BuiltinType::Float128:
370 case BuiltinType::Ibm128:
371 case BuiltinType::ShortAccum:
372 case BuiltinType::Accum:
373 case BuiltinType::LongAccum:
374 case BuiltinType::UShortAccum:
375 case BuiltinType::UAccum:
376 case BuiltinType::ULongAccum:
377 case BuiltinType::ShortFract:
378 case BuiltinType::Fract:
379 case BuiltinType::LongFract:
380 case BuiltinType::UShortFract:
381 case BuiltinType::UFract:
382 case BuiltinType::ULongFract:
383 case BuiltinType::SatShortAccum:
384 case BuiltinType::SatAccum:
385 case BuiltinType::SatLongAccum:
386 case BuiltinType::SatUShortAccum:
387 case BuiltinType::SatUAccum:
388 case BuiltinType::SatULongAccum:
389 case BuiltinType::SatShortFract:
390 case BuiltinType::SatFract:
391 case BuiltinType::SatLongFract:
392 case BuiltinType::SatUShortFract:
393 case BuiltinType::SatUFract:
394 case BuiltinType::SatULongFract:
395 case BuiltinType::BFloat16:
396 llvm_unreachable("Builtin type needs extra local data!");
397
398
399 case BuiltinType::NullPtr:
400 case BuiltinType::Overload:
401 case BuiltinType::Dependent:
402 case BuiltinType::UnresolvedTemplate:
403 case BuiltinType::BoundMember:
404 case BuiltinType::UnknownAny:
405 case BuiltinType::ARCUnbridgedCast:
406 case BuiltinType::PseudoObject:
407 case BuiltinType::ObjCId:
408 case BuiltinType::ObjCClass:
409 case BuiltinType::ObjCSel:
410#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
411 case BuiltinType::Id:
412#include "clang/Basic/OpenCLImageTypes.def"
413#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
414 case BuiltinType::Id:
415#include "clang/Basic/OpenCLExtensionTypes.def"
416 case BuiltinType::OCLSampler:
417 case BuiltinType::OCLEvent:
418 case BuiltinType::OCLClkEvent:
419 case BuiltinType::OCLQueue:
420 case BuiltinType::OCLReserveID:
421#define SVE_TYPE(Name, Id, SingletonId) \
422 case BuiltinType::Id:
423#include "clang/Basic/AArch64SVEACLETypes.def"
424#define PPC_VECTOR_TYPE(Name, Id, Size) \
425 case BuiltinType::Id:
426#include "clang/Basic/PPCTypes.def"
427#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
428#include "clang/Basic/RISCVVTypes.def"
429#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
430#include "clang/Basic/WebAssemblyReferenceTypes.def"
431#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
432#include "clang/Basic/AMDGPUTypes.def"
433#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
434#include "clang/Basic/HLSLIntangibleTypes.def"
435 case BuiltinType::BuiltinFn:
436 case BuiltinType::IncompleteMatrixIdx:
437 case BuiltinType::ArraySection:
438 case BuiltinType::OMPArrayShaping:
439 case BuiltinType::OMPIterator:
441 }
442
443 llvm_unreachable("Invalid BuiltinType Kind!");
444}
445
448 TL = PTL.getInnerLoc();
449 return TL;
450}
451
453 if (auto ATL = getAs()) {
454 const Attr *A = ATL.getAttr();
455 if (A && (isa(A) || isa(A) ||
456 isa(A)))
458 }
459
460 return {};
461}
462
464
465 if (auto qual = getAs())
466 return qual;
467
469
470
472 if (attr.isQualifier()) return attr;
473 return attr.getModifiedLoc().findExplicitQualifierLoc();
474 }
475
476
478 return atomic;
479 }
480
481 return {};
482}
483
488
493}
494
500 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
504 }
509}
510
512
513
514
515
516
517
518
519
520
522}
523
526}
527
530}
531
538}
539
547}
548
552 return;
555 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
557}
558
563 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
566}
567
568void
574 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
576 } else {
578 }
584 Context, getTypePtr()->template_arguments(), getArgInfos(), Loc);
585}
586
590 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
591 switch (Args[i].getKind()) {
593 llvm_unreachable("Impossible TemplateArgument");
594
600 break;
601
604 break;
605
610 break;
611
615 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
617 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
619 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
620
622 Context, Builder.getWithLocInContext(Context), Loc,
625 break;
626 }
627
630 break;
631 }
632 }
633}
634
635
636
648 for (unsigned i = 0; i < size; ++i) {
651 TALI[i]));
652 }
653
658 delete[] TALI;
659 return ConceptRef;
660}
661
669 }
670}
671
672namespace {
673
674 class GetContainedAutoTypeLocVisitor :
675 public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
676 public:
678
680 return TL;
681 }
682
683
684
686 return Visit(T.getNamedTypeLoc());
687 }
688
690 return Visit(T.getUnqualifiedLoc());
691 }
692
694 return Visit(T.getPointeeLoc());
695 }
696
698 return Visit(T.getPointeeLoc());
699 }
700
702 return Visit(T.getPointeeLoc());
703 }
704
706 return Visit(T.getPointeeLoc());
707 }
708
710 return Visit(T.getElementLoc());
711 }
712
714 return Visit(T.getReturnLoc());
715 }
716
718 return Visit(T.getInnerLoc());
719 }
720
722 return Visit(T.getModifiedLoc());
723 }
724
726 return Visit(T.getWrappedLoc());
727 }
728
731 return Visit(T.getWrappedLoc());
732 }
733
735 return Visit(T.getInnerLoc());
736 }
737
739 return Visit(T.getOriginalLoc());
740 }
741
743 return Visit(T.getPatternLoc());
744 }
745 };
746
747}
748
750 TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
754}
755
757 if (const auto TSTL = getAsAdjusted())
758 return TSTL.getTemplateKeywordLoc();
759 if (const auto DTSTL =
760 getAsAdjusted())
761 return DTSTL.getTemplateKeywordLoc();
763}
This file provides AST data structures related to concepts.
Defines the clang::ASTContext interface.
static Decl::Kind getKind(const Decl *D)
Defines the C++ template declaration subclasses.
Defines the clang::SourceLocation class and associated facilities.
Defines various enumerations that describe declaration and type specifiers.
static ConceptReference * createTrivialConceptReference(ASTContext &Context, SourceLocation Loc, const AutoType *AT)
static const unsigned TypeLocMaxDataAlign
Defines the clang::TypeLoc interface and its subclasses.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
Wrapper for source info for arrays.
Attr - This represents one attribute.
SourceLocation getLocation() const
SourceRange getRange() const
Type source information for an attributed type.
const Attr * getAttr() const
The type attribute.
SourceRange getLocalSourceRange() const
void initializeLocal(ASTContext &Context, SourceLocation Loc)
void setConceptReference(ConceptReference *CR)
bool isConstrained() const
void setRParenLoc(SourceLocation Loc)
Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained by a type-constraint.
ArrayRef< TemplateArgument > getTypeConstraintArguments() const
ConceptDecl * getTypeConstraintConcept() const
Type source information for an btf_tag attributed type.
const BTFTypeTagAttr * getAttr() const
The btf_type_tag attribute.
SourceRange getLocalSourceRange() const
Wrapper for source info for block pointers.
TypeSpecifierType getWrittenTypeSpec() const
bool needsExtraLocalData() const
WrittenBuiltinSpecs & getWrittenBuiltinSpecs()
A reference to a concept and its template args, as it appears in the code.
static ConceptReference * Create(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
const BuiltinType * getTypePtr() const
LocalData * getLocalData() const
Expr * getCountExpr() const
SourceRange getLocalSourceRange() const
SourceLocation getLocation() const
void initializeLocal(ASTContext &Context, SourceLocation Loc)
void setNameLoc(SourceLocation Loc)
void setElaboratedKeywordLoc(SourceLocation Loc)
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Represents a dependent template name that cannot be resolved prior to template instantiation.
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
void initializeLocal(ASTContext &Context, SourceLocation Loc)
void setTemplateKeywordLoc(SourceLocation Loc)
void setElaboratedKeywordLoc(SourceLocation Loc)
void setRAngleLoc(SourceLocation Loc)
void setLAngleLoc(SourceLocation Loc)
void setTemplateNameLoc(SourceLocation Loc)
void initializeLocal(ASTContext &Context, SourceLocation Loc)
void setElaboratedKeywordLoc(SourceLocation Loc)
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
bool hasTrailingReturn() const
Whether this function prototype has a trailing return type.
Wrapper for source info for functions.
Type source information for HLSL attributed resource type.
const TypeClass * getTypePtr() const
Wrapper for source info for member pointers.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Class that aids in the construction of nested-name-specifiers along with source-location information ...
A C++ nested-name-specifier augmented with source location information.
Wraps an ObjCPointerType with source location information.
SourceLocation getStarLoc() const
void setTypeArgsRAngleLoc(SourceLocation Loc)
void initializeLocal(ASTContext &Context, SourceLocation Loc)
unsigned getNumTypeArgs() const
unsigned getNumProtocols() const
void setTypeArgsLAngleLoc(SourceLocation Loc)
void setTypeArgTInfo(unsigned i, TypeSourceInfo *TInfo)
void setProtocolLAngleLoc(SourceLocation Loc)
void setProtocolRAngleLoc(SourceLocation Loc)
void setHasBaseTypeAsWritten(bool HasBaseType)
void setProtocolLoc(unsigned i, SourceLocation Loc)
unsigned getNumProtocols() const
void setProtocolLoc(unsigned i, SourceLocation Loc)
void setProtocolLAngleLoc(SourceLocation Loc)
void initializeLocal(ASTContext &Context, SourceLocation Loc)
void setProtocolRAngleLoc(SourceLocation Loc)
void setNameLoc(SourceLocation Loc)
Wrapper for source info for pointers.
A (possibly-)qualified type.
bool hasLocalQualifiers() const
Determine whether this particular QualType instance has any qualifiers, without looking through any t...
Represents a template name as written in source code.
Wrapper of type source information for a type with non-trivial direct qualifiers.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Represents the declaration of a struct/union/class/enum.
TagDecl * getDecl() const
bool isDefinition() const
True if the tag was defined in this type specifier.
A convenient class for passing around template argument information.
void addArgument(const TemplateArgumentLoc &Loc)
Location wrapper for a TemplateArgument.
@ Declaration
The template argument is a declaration that was provided for a pointer, reference,...
@ Template
The template argument is a template name that was provided for a template template parameter.
@ StructuralValue
The template argument is a non-type template argument that can't be represented by the special-case D...
@ Pack
The template argument is actually a parameter pack.
@ TemplateExpansion
The template argument is a pack expansion of a template name that was provided for a template templat...
@ NullPtr
The template argument is a null pointer or null pointer to member that was provided for a non-type te...
@ Type
The template argument is a type.
@ Null
Represents an empty template argument, e.g., one that has not been deduced.
@ Integral
The template argument is an integral value stored in an llvm::APSInt that was provided for an integra...
@ Expression
The template argument is an expression, and we've not resolved it to one of the other forms yet,...
Represents a C++ template name within the type system.
DependentTemplateName * getAsDependentTemplateName() const
Retrieve the underlying dependent template name structure, if any.
QualifiedTemplateName * getAsQualifiedTemplateName() const
Retrieve the underlying qualified template name structure, if any.
static void initializeArgLocs(ASTContext &Context, ArrayRef< TemplateArgument > Args, TemplateArgumentLocInfo *ArgInfos, SourceLocation Loc)
RetTy Visit(TypeLoc TyLoc)
Base wrapper for a particular "section" of type source info.
SourceLocation findNullabilityLoc() const
Find the location of the nullability specifier (__nonnull, __nullable, or __null_unspecifier),...
static unsigned getLocalAlignmentForType(QualType Ty)
Returns the alignment of type source info data block for the given type.
TypeLoc findExplicitQualifierLoc() const
Find a type with the location of an explicit type qualifier.
QualType getType() const
Get the type for which this source info wrapper provides information.
TypeLoc getNextTypeLoc() const
Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the TypeLoc is a PointerLoc and next Typ...
T getAs() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
TypeLoc IgnoreParens() const
T castAs() const
Convert to the specified TypeLoc type, asserting that this TypeLoc is of the desired type.
SourceRange getLocalSourceRange() const
Get the local source range.
unsigned getFullDataSize() const
Returns the size of the type source info data block.
AutoTypeLoc getContainedAutoTypeLoc() const
Get the typeloc of an AutoType whose type will be deduced for a variable with an initializer of this ...
SourceLocation getTemplateKeywordLoc() const
Get the SourceLocation of the template keyword (if any).
void copy(TypeLoc other)
Copies the other type loc into this one.
TypeLocClass getTypeLocClass() const
static unsigned getFullDataSizeForType(QualType Ty)
Returns the size of type source info data block for the given type.
SourceLocation getEndLoc() const
Get the end source location.
SourceLocation getBeginLoc() const
Get the begin source location.
SourceRange getLocalSourceRange() const
Expr * getUnderlyingExpr() const
void initializeLocal(ASTContext &Context, SourceLocation Loc)
QualType getUnmodifiedType() const
A reasonable base class for TypeLocs that correspond to types that are written as a type-specifier.
SourceLocation getNameLoc() const
void setNameLoc(SourceLocation Loc)
SourceLocation getRParenLoc() const
SourceLocation getTypeofLoc() const
void setRParenLoc(SourceLocation Loc)
void initializeLocal(ASTContext &Context, SourceLocation Loc)
void setKWLoc(SourceLocation Loc)
void setUnderlyingTInfo(TypeSourceInfo *TInfo)
void setLParenLoc(SourceLocation Loc)
const internal::VariadicAllOfMatcher< Attr > attr
Matches attributes.
The JSON file list parser is used to communicate input to InstallAPI.
TypeSpecifierType
Specifies the kind of type.
const FunctionProtoType * T
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
static const ASTTemplateArgumentListInfo * Create(const ASTContext &C, const TemplateArgumentListInfo &List)
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
Location information for a TemplateArgument.