1 //===- TypeLoc.cpp - Type Source Info Wrapper -----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the TypeLoc subclasses implementations.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/TypeLoc.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/NestedNameSpecifier.h"
19 #include "clang/AST/TemplateBase.h"
20 #include "clang/AST/TemplateName.h"
21 #include "clang/AST/TypeLocVisitor.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "clang/Basic/Specifiers.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
31 using namespace clang;
33 static const unsigned TypeLocMaxDataAlign = alignof(void *);
35 //===----------------------------------------------------------------------===//
36 // TypeLoc Implementation
37 //===----------------------------------------------------------------------===//
41 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
43 #define ABSTRACT_TYPELOC(CLASS, PARENT)
44 #define TYPELOC(CLASS, PARENT) \
45 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
46 return TyLoc.getLocalSourceRange(); \
48 #include "clang/AST/TypeLocNodes.def"
53 SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
54 if (TL.isNull()) return SourceRange();
55 return TypeLocRanger().Visit(TL);
60 class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
62 #define ABSTRACT_TYPELOC(CLASS, PARENT)
63 #define TYPELOC(CLASS, PARENT) \
64 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
65 return TyLoc.getLocalDataAlignment(); \
67 #include "clang/AST/TypeLocNodes.def"
72 /// Returns the alignment of the type source info data block.
73 unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
74 if (Ty.isNull()) return 1;
75 return TypeAligner().Visit(TypeLoc(Ty, nullptr));
80 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
82 #define ABSTRACT_TYPELOC(CLASS, PARENT)
83 #define TYPELOC(CLASS, PARENT) \
84 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
85 return TyLoc.getLocalDataSize(); \
87 #include "clang/AST/TypeLocNodes.def"
92 /// Returns the size of the type source info data block.
93 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
95 TypeLoc TyLoc(Ty, nullptr);
96 unsigned MaxAlign = 1;
97 while (!TyLoc.isNull()) {
98 unsigned Align = getLocalAlignmentForType(TyLoc.getType());
99 MaxAlign = std::max(Align, MaxAlign);
100 Total = llvm::alignTo(Total, Align);
101 Total += TypeSizer().Visit(TyLoc);
102 TyLoc = TyLoc.getNextTypeLoc();
104 Total = llvm::alignTo(Total, MaxAlign);
110 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
112 #define ABSTRACT_TYPELOC(CLASS, PARENT)
113 #define TYPELOC(CLASS, PARENT) \
114 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
115 return TyLoc.getNextTypeLoc(); \
117 #include "clang/AST/TypeLocNodes.def"
122 /// Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
123 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
124 TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
125 return NextLoc().Visit(TL);
128 /// Initializes a type location, and all of its children
129 /// recursively, as if the entire tree had been written in the
131 void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
132 SourceLocation Loc) {
134 switch (TL.getTypeLocClass()) {
135 #define ABSTRACT_TYPELOC(CLASS, PARENT)
136 #define TYPELOC(CLASS, PARENT) \
138 CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
139 TLCasted.initializeLocal(Context, Loc); \
140 TL = TLCasted.getNextTypeLoc(); \
144 #include "clang/AST/TypeLocNodes.def"
151 class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
155 TypeLocCopier(TypeLoc source) : Source(source) {}
157 #define ABSTRACT_TYPELOC(CLASS, PARENT)
158 #define TYPELOC(CLASS, PARENT) \
159 void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
160 dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
162 #include "clang/AST/TypeLocNodes.def"
167 void TypeLoc::copy(TypeLoc other) {
168 assert(getFullDataSize() == other.getFullDataSize());
170 // If both data pointers are aligned to the maximum alignment, we
171 // can memcpy because getFullDataSize() accurately reflects the
172 // layout of the data.
173 if (reinterpret_cast<uintptr_t>(Data) ==
174 llvm::alignTo(reinterpret_cast<uintptr_t>(Data),
175 TypeLocMaxDataAlign) &&
176 reinterpret_cast<uintptr_t>(other.Data) ==
177 llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
178 TypeLocMaxDataAlign)) {
179 memcpy(Data, other.Data, getFullDataSize());
183 // Copy each of the pieces.
184 TypeLoc TL(getType(), Data);
186 TypeLocCopier(other).Visit(TL);
187 other = other.getNextTypeLoc();
188 } while ((TL = TL.getNextTypeLoc()));
191 SourceLocation TypeLoc::getBeginLoc() const {
193 TypeLoc LeftMost = Cur;
195 switch (Cur.getTypeLocClass()) {
200 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
201 ->hasTrailingReturn()) {
206 case FunctionNoProto:
208 case DependentSizedArray:
209 case IncompleteArray:
211 // FIXME: Currently QualifiedTypeLoc does not have a source range
213 Cur = Cur.getNextTypeLoc();
216 if (Cur.getLocalSourceRange().getBegin().isValid())
218 Cur = Cur.getNextTypeLoc();
225 return LeftMost.getLocalSourceRange().getBegin();
228 SourceLocation TypeLoc::getEndLoc() const {
232 switch (Cur.getTypeLocClass()) {
236 return Last.getLocalSourceRange().getEnd();
239 case DependentSizedArray:
240 case IncompleteArray:
242 case FunctionNoProto:
246 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
254 case LValueReference:
255 case RValueReference:
264 Cur = Cur.getNextTypeLoc();
270 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
271 // Overload resolution does the real work for us.
272 static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
273 static bool isTypeSpec(TypeLoc _) { return false; }
275 #define ABSTRACT_TYPELOC(CLASS, PARENT)
276 #define TYPELOC(CLASS, PARENT) \
277 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
278 return isTypeSpec(TyLoc); \
280 #include "clang/AST/TypeLocNodes.def"
285 /// Determines if the given type loc corresponds to a
286 /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
287 /// the type hierarchy, this is made somewhat complicated.
289 /// There are a lot of types that currently use TypeSpecTypeLoc
290 /// because it's a convenient base class. Ideally we would not accept
291 /// those here, but ideally we would have better implementations for
293 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
294 if (TL.getType().hasLocalQualifiers()) return false;
295 return TSTChecker().Visit(TL);
298 bool TagTypeLoc::isDefinition() const {
299 TagDecl *D = getDecl();
300 return D->isCompleteDefinition() &&
301 (D->getIdentifier() == nullptr || D->getLocation() == getNameLoc());
304 // Reimplemented to account for GNU/C++ extension
305 // typeof unary-expression
306 // where there are no parentheses.
307 SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
308 if (getRParenLoc().isValid())
309 return SourceRange(getTypeofLoc(), getRParenLoc());
311 return SourceRange(getTypeofLoc(),
312 getUnderlyingExpr()->getSourceRange().getEnd());
316 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
317 if (needsExtraLocalData())
318 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
319 switch (getTypePtr()->getKind()) {
320 case BuiltinType::Void:
322 case BuiltinType::Bool:
324 case BuiltinType::Char_U:
325 case BuiltinType::Char_S:
327 case BuiltinType::Char8:
329 case BuiltinType::Char16:
331 case BuiltinType::Char32:
333 case BuiltinType::WChar_S:
334 case BuiltinType::WChar_U:
336 case BuiltinType::UChar:
337 case BuiltinType::UShort:
338 case BuiltinType::UInt:
339 case BuiltinType::ULong:
340 case BuiltinType::ULongLong:
341 case BuiltinType::UInt128:
342 case BuiltinType::SChar:
343 case BuiltinType::Short:
344 case BuiltinType::Int:
345 case BuiltinType::Long:
346 case BuiltinType::LongLong:
347 case BuiltinType::Int128:
348 case BuiltinType::Half:
349 case BuiltinType::Float:
350 case BuiltinType::Double:
351 case BuiltinType::LongDouble:
352 case BuiltinType::Float16:
353 case BuiltinType::Float128:
354 case BuiltinType::ShortAccum:
355 case BuiltinType::Accum:
356 case BuiltinType::LongAccum:
357 case BuiltinType::UShortAccum:
358 case BuiltinType::UAccum:
359 case BuiltinType::ULongAccum:
360 case BuiltinType::ShortFract:
361 case BuiltinType::Fract:
362 case BuiltinType::LongFract:
363 case BuiltinType::UShortFract:
364 case BuiltinType::UFract:
365 case BuiltinType::ULongFract:
366 case BuiltinType::SatShortAccum:
367 case BuiltinType::SatAccum:
368 case BuiltinType::SatLongAccum:
369 case BuiltinType::SatUShortAccum:
370 case BuiltinType::SatUAccum:
371 case BuiltinType::SatULongAccum:
372 case BuiltinType::SatShortFract:
373 case BuiltinType::SatFract:
374 case BuiltinType::SatLongFract:
375 case BuiltinType::SatUShortFract:
376 case BuiltinType::SatUFract:
377 case BuiltinType::SatULongFract:
378 case BuiltinType::BFloat16:
379 llvm_unreachable("Builtin type needs extra local data!");
380 // Fall through, if the impossible happens.
382 case BuiltinType::NullPtr:
383 case BuiltinType::Overload:
384 case BuiltinType::Dependent:
385 case BuiltinType::BoundMember:
386 case BuiltinType::UnknownAny:
387 case BuiltinType::ARCUnbridgedCast:
388 case BuiltinType::PseudoObject:
389 case BuiltinType::ObjCId:
390 case BuiltinType::ObjCClass:
391 case BuiltinType::ObjCSel:
392 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
393 case BuiltinType::Id:
394 #include "clang/Basic/OpenCLImageTypes.def"
395 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
396 case BuiltinType::Id:
397 #include "clang/Basic/OpenCLExtensionTypes.def"
398 case BuiltinType::OCLSampler:
399 case BuiltinType::OCLEvent:
400 case BuiltinType::OCLClkEvent:
401 case BuiltinType::OCLQueue:
402 case BuiltinType::OCLReserveID:
403 #define SVE_TYPE(Name, Id, SingletonId) \
404 case BuiltinType::Id:
405 #include "clang/Basic/AArch64SVEACLETypes.def"
406 case BuiltinType::BuiltinFn:
407 case BuiltinType::IncompleteMatrixIdx:
408 case BuiltinType::OMPArraySection:
409 case BuiltinType::OMPArrayShaping:
410 case BuiltinType::OMPIterator:
411 return TST_unspecified;
414 llvm_unreachable("Invalid BuiltinType Kind!");
417 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
418 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
419 TL = PTL.getInnerLoc();
423 SourceLocation TypeLoc::findNullabilityLoc() const {
424 if (auto ATL = getAs<AttributedTypeLoc>()) {
425 const Attr *A = ATL.getAttr();
426 if (A && (isa<TypeNullableAttr>(A) || isa<TypeNonNullAttr>(A) ||
427 isa<TypeNullUnspecifiedAttr>(A)))
428 return A->getLocation();
434 TypeLoc TypeLoc::findExplicitQualifierLoc() const {
436 if (auto qual = getAs<QualifiedTypeLoc>())
439 TypeLoc loc = IgnoreParens();
442 if (auto attr = loc.getAs<AttributedTypeLoc>()) {
443 if (attr.isQualifier()) return attr;
444 return attr.getModifiedLoc().findExplicitQualifierLoc();
447 // C11 _Atomic types.
448 if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
455 void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
456 SourceLocation Loc) {
458 if (!getNumProtocols()) return;
460 setProtocolLAngleLoc(Loc);
461 setProtocolRAngleLoc(Loc);
462 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
463 setProtocolLoc(i, Loc);
466 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
467 SourceLocation Loc) {
468 setHasBaseTypeAsWritten(true);
469 setTypeArgsLAngleLoc(Loc);
470 setTypeArgsRAngleLoc(Loc);
471 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
473 Context.getTrivialTypeSourceInfo(
474 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
476 setProtocolLAngleLoc(Loc);
477 setProtocolRAngleLoc(Loc);
478 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
479 setProtocolLoc(i, Loc);
482 SourceRange AttributedTypeLoc::getLocalSourceRange() const {
483 // Note that this does *not* include the range of the attribute
485 // __attribute__((foo(bar)))
486 // ^~~~~~~~~~~~~~~ ~~
490 // That enclosure doesn't necessarily belong to a single attribute
492 return getAttr() ? getAttr()->getRange() : SourceRange();
495 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
496 SourceLocation Loc) {
497 TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
498 ::initializeLocal(Context, Loc);
499 this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
500 getUnderlyingType(), Loc);
503 void UnaryTransformTypeLoc::initializeLocal(ASTContext &Context,
504 SourceLocation Loc) {
508 this->setUnderlyingTInfo(
509 Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
512 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
513 SourceLocation Loc) {
514 setElaboratedKeywordLoc(Loc);
515 NestedNameSpecifierLocBuilder Builder;
516 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
517 setQualifierLoc(Builder.getWithLocInContext(Context));
520 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
521 SourceLocation Loc) {
522 setElaboratedKeywordLoc(Loc);
523 NestedNameSpecifierLocBuilder Builder;
524 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
525 setQualifierLoc(Builder.getWithLocInContext(Context));
530 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
531 SourceLocation Loc) {
532 setElaboratedKeywordLoc(Loc);
533 if (getTypePtr()->getQualifier()) {
534 NestedNameSpecifierLocBuilder Builder;
535 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
536 setQualifierLoc(Builder.getWithLocInContext(Context));
538 setQualifierLoc(NestedNameSpecifierLoc());
540 setTemplateKeywordLoc(Loc);
541 setTemplateNameLoc(Loc);
544 TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
545 getTypePtr()->getArgs(),
549 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
551 const TemplateArgument *Args,
552 TemplateArgumentLocInfo *ArgInfos,
553 SourceLocation Loc) {
554 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
555 switch (Args[i].getKind()) {
556 case TemplateArgument::Null:
557 llvm_unreachable("Impossible TemplateArgument");
559 case TemplateArgument::Integral:
560 case TemplateArgument::Declaration:
561 case TemplateArgument::NullPtr:
562 ArgInfos[i] = TemplateArgumentLocInfo();
565 case TemplateArgument::Expression:
566 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
569 case TemplateArgument::Type:
570 ArgInfos[i] = TemplateArgumentLocInfo(
571 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
575 case TemplateArgument::Template:
576 case TemplateArgument::TemplateExpansion: {
577 NestedNameSpecifierLocBuilder Builder;
578 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
579 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
580 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
581 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
582 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
584 ArgInfos[i] = TemplateArgumentLocInfo(
585 Builder.getWithLocInContext(Context), Loc,
586 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
591 case TemplateArgument::Pack:
592 ArgInfos[i] = TemplateArgumentLocInfo();
598 DeclarationNameInfo AutoTypeLoc::getConceptNameInfo() const {
599 return DeclarationNameInfo(getNamedConcept()->getDeclName(),
600 getLocalData()->ConceptNameLoc);
603 void AutoTypeLoc::initializeLocal(ASTContext &Context, SourceLocation Loc) {
604 setNestedNameSpecifierLoc(NestedNameSpecifierLoc());
605 setTemplateKWLoc(Loc);
606 setConceptNameLoc(Loc);
607 setFoundDecl(nullptr);
610 TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
611 getTypePtr()->getArgs(),
619 class GetContainedAutoTypeLocVisitor :
620 public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
622 using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
624 TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
628 // Only these types can contain the desired 'auto' type.
630 TypeLoc VisitElaboratedTypeLoc(ElaboratedTypeLoc T) {
631 return Visit(T.getNamedTypeLoc());
634 TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
635 return Visit(T.getUnqualifiedLoc());
638 TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
639 return Visit(T.getPointeeLoc());
642 TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
643 return Visit(T.getPointeeLoc());
646 TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
647 return Visit(T.getPointeeLoc());
650 TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
651 return Visit(T.getPointeeLoc());
654 TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
655 return Visit(T.getElementLoc());
658 TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
659 return Visit(T.getReturnLoc());
662 TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
663 return Visit(T.getInnerLoc());
666 TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
667 return Visit(T.getModifiedLoc());
670 TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
671 return Visit(T.getInnerLoc());
674 TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
675 return Visit(T.getOriginalLoc());
678 TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
679 return Visit(T.getPatternLoc());
685 AutoTypeLoc TypeLoc::getContainedAutoTypeLoc() const {
686 TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
688 return AutoTypeLoc();
689 return Res.getAs<AutoTypeLoc>();