1//===- TypeLoc.cpp - Type Source Info Wrapper -----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the TypeLoc subclasses implementations.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/TypeLoc.h"
14#include "clang/AST/ASTConcept.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/NestedNameSpecifier.h"
20#include "clang/AST/TemplateBase.h"
21#include "clang/AST/TemplateName.h"
22#include "clang/AST/TypeLocVisitor.h"
23#include "clang/Basic/SourceLocation.h"
24#include "clang/Basic/Specifiers.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/MathExtras.h"
28#include <algorithm>
29#include <cassert>
30#include <cstdint>
31#include <cstring>
32
33using namespace clang;
34
35static const unsigned TypeLocMaxDataAlign = alignof(void *);
36
37//===----------------------------------------------------------------------===//
38// TypeLoc Implementation
39//===----------------------------------------------------------------------===//
40
41namespace {
42
43class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
44public:
45#define ABSTRACT_TYPELOC(CLASS, PARENT)
46#define TYPELOC(CLASS, PARENT) \
47 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
48 return TyLoc.getLocalSourceRange(); \
49 }
50#include "clang/AST/TypeLocNodes.def"
51};
52
53} // namespace
54
55SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
56 if (TL.isNull()) return SourceRange();
57 return TypeLocRanger().Visit(TyLoc: TL);
58}
59
60namespace {
61
62class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
63public:
64#define ABSTRACT_TYPELOC(CLASS, PARENT)
65#define TYPELOC(CLASS, PARENT) \
66 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
67 return TyLoc.getLocalDataAlignment(); \
68 }
69#include "clang/AST/TypeLocNodes.def"
70};
71
72} // namespace
73
74/// Returns the alignment of the type source info data block.
75unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
76 if (Ty.isNull()) return 1;
77 return TypeAligner().Visit(TyLoc: TypeLoc(Ty, nullptr));
78}
79
80namespace {
81
82class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
83public:
84#define ABSTRACT_TYPELOC(CLASS, PARENT)
85#define TYPELOC(CLASS, PARENT) \
86 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
87 return TyLoc.getLocalDataSize(); \
88 }
89#include "clang/AST/TypeLocNodes.def"
90};
91
92} // namespace
93
94/// Returns the size of the type source info data block.
95unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
96 unsigned Total = 0;
97 TypeLoc TyLoc(Ty, nullptr);
98 unsigned MaxAlign = 1;
99 while (!TyLoc.isNull()) {
100 unsigned Align = getLocalAlignmentForType(Ty: TyLoc.getType());
101 MaxAlign = std::max(a: Align, b: MaxAlign);
102 Total = llvm::alignTo(Value: Total, Align);
103 Total += TypeSizer().Visit(TyLoc);
104 TyLoc = TyLoc.getNextTypeLoc();
105 }
106 Total = llvm::alignTo(Value: Total, Align: MaxAlign);
107 return Total;
108}
109
110namespace {
111
112class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
113public:
114#define ABSTRACT_TYPELOC(CLASS, PARENT)
115#define TYPELOC(CLASS, PARENT) \
116 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
117 return TyLoc.getNextTypeLoc(); \
118 }
119#include "clang/AST/TypeLocNodes.def"
120};
121
122} // namespace
123
124/// Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
125/// TypeLoc is a PointerLoc and next TypeLoc is for "int".
126TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
127 return NextLoc().Visit(TyLoc: TL);
128}
129
130/// Initializes a type location, and all of its children
131/// recursively, as if the entire tree had been written in the
132/// given location.
133void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
134 SourceLocation Loc) {
135 while (true) {
136 switch (TL.getTypeLocClass()) {
137#define ABSTRACT_TYPELOC(CLASS, PARENT)
138#define TYPELOC(CLASS, PARENT) \
139 case CLASS: { \
140 CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
141 TLCasted.initializeLocal(Context, Loc); \
142 TL = TLCasted.getNextTypeLoc(); \
143 if (!TL) return; \
144 continue; \
145 }
146#include "clang/AST/TypeLocNodes.def"
147 }
148 }
149}
150
151namespace {
152
153class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
154 TypeLoc Source;
155
156public:
157 TypeLocCopier(TypeLoc source) : Source(source) {}
158
159#define ABSTRACT_TYPELOC(CLASS, PARENT)
160#define TYPELOC(CLASS, PARENT) \
161 void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
162 dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
163 }
164#include "clang/AST/TypeLocNodes.def"
165};
166
167} // namespace
168
169void TypeLoc::copy(TypeLoc other) {
170 assert(getFullDataSize() == other.getFullDataSize());
171
172 // If both data pointers are aligned to the maximum alignment, we
173 // can memcpy because getFullDataSize() accurately reflects the
174 // layout of the data.
175 if (reinterpret_cast<uintptr_t>(Data) ==
176 llvm::alignTo(Value: reinterpret_cast<uintptr_t>(Data),
177 Align: TypeLocMaxDataAlign) &&
178 reinterpret_cast<uintptr_t>(other.Data) ==
179 llvm::alignTo(Value: reinterpret_cast<uintptr_t>(other.Data),
180 Align: TypeLocMaxDataAlign)) {
181 memcpy(dest: Data, src: other.Data, n: getFullDataSize());
182 return;
183 }
184
185 // Copy each of the pieces.
186 TypeLoc TL(getType(), Data);
187 do {
188 TypeLocCopier(other).Visit(TyLoc: TL);
189 other = other.getNextTypeLoc();
190 } while ((TL = TL.getNextTypeLoc()));
191}
192
193SourceLocation TypeLoc::getBeginLoc() const {
194 TypeLoc Cur = *this;
195 TypeLoc LeftMost = Cur;
196 while (true) {
197 switch (Cur.getTypeLocClass()) {
198 case FunctionProto:
199 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
200 ->hasTrailingReturn()) {
201 LeftMost = Cur;
202 break;
203 }
204 [[fallthrough]];
205 case FunctionNoProto:
206 case ConstantArray:
207 case DependentSizedArray:
208 case IncompleteArray:
209 case VariableArray:
210 // FIXME: Currently QualifiedTypeLoc does not have a source range
211 case Qualified:
212 Cur = Cur.getNextTypeLoc();
213 continue;
214 default:
215 if (Cur.getLocalSourceRange().getBegin().isValid())
216 LeftMost = Cur;
217 Cur = Cur.getNextTypeLoc();
218 if (Cur.isNull())
219 break;
220 continue;
221 } // switch
222 break;
223 } // while
224 return LeftMost.getLocalSourceRange().getBegin();
225}
226
227SourceLocation TypeLoc::getEndLoc() const {
228 TypeLoc Cur = *this;
229 TypeLoc Last;
230 while (true) {
231 switch (Cur.getTypeLocClass()) {
232 default:
233 if (!Last)
234 Last = Cur;
235 return Last.getLocalSourceRange().getEnd();
236 case Paren:
237 case ConstantArray:
238 case DependentSizedArray:
239 case IncompleteArray:
240 case VariableArray:
241 case FunctionNoProto:
242 // The innermost type with suffix syntax always determines the end of the
243 // type.
244 Last = Cur;
245 break;
246 case FunctionProto:
247 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
248 Last = TypeLoc();
249 else
250 Last = Cur;
251 break;
252 case ObjCObjectPointer:
253 // `id` and `id<...>` have no star location.
254 if (Cur.castAs<ObjCObjectPointerTypeLoc>().getStarLoc().isInvalid())
255 break;
256 [[fallthrough]];
257 case Pointer:
258 case BlockPointer:
259 case MemberPointer:
260 case LValueReference:
261 case RValueReference:
262 case PackExpansion:
263 // Types with prefix syntax only determine the end of the type if there
264 // is no suffix type.
265 if (!Last)
266 Last = Cur;
267 break;
268 case Qualified:
269 break;
270 }
271 Cur = Cur.getNextTypeLoc();
272 }
273}
274
275namespace {
276
277struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
278 // Overload resolution does the real work for us.
279 static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
280 static bool isTypeSpec(TypeLoc _) { return false; }
281
282#define ABSTRACT_TYPELOC(CLASS, PARENT)
283#define TYPELOC(CLASS, PARENT) \
284 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
285 return isTypeSpec(TyLoc); \
286 }
287#include "clang/AST/TypeLocNodes.def"
288};
289
290} // namespace
291
292/// Determines if the given type loc corresponds to a
293/// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
294/// the type hierarchy, this is made somewhat complicated.
295///
296/// There are a lot of types that currently use TypeSpecTypeLoc
297/// because it's a convenient base class. Ideally we would not accept
298/// those here, but ideally we would have better implementations for
299/// them.
300bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
301 if (TL.getType().hasLocalQualifiers()) return false;
302 return TSTChecker().Visit(TyLoc: TL);
303}
304
305bool TagTypeLoc::isDefinition() const {
306 return getTypePtr()->isTagOwned() && getDecl()->isCompleteDefinition();
307}
308
309// Reimplemented to account for GNU/C++ extension
310// typeof unary-expression
311// where there are no parentheses.
312SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
313 if (getRParenLoc().isValid())
314 return SourceRange(getTypeofLoc(), getRParenLoc());
315 else
316 return SourceRange(getTypeofLoc(),
317 getUnderlyingExpr()->getSourceRange().getEnd());
318}
319
320
321TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
322 if (needsExtraLocalData())
323 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
324 switch (getTypePtr()->getKind()) {
325 case BuiltinType::Void:
326 return TST_void;
327 case BuiltinType::Bool:
328 return TST_bool;
329 case BuiltinType::Char_U:
330 case BuiltinType::Char_S:
331 return TST_char;
332 case BuiltinType::Char8:
333 return TST_char8;
334 case BuiltinType::Char16:
335 return TST_char16;
336 case BuiltinType::Char32:
337 return TST_char32;
338 case BuiltinType::WChar_S:
339 case BuiltinType::WChar_U:
340 return TST_wchar;
341 case BuiltinType::UChar:
342 case BuiltinType::UShort:
343 case BuiltinType::UInt:
344 case BuiltinType::ULong:
345 case BuiltinType::ULongLong:
346 case BuiltinType::UInt128:
347 case BuiltinType::SChar:
348 case BuiltinType::Short:
349 case BuiltinType::Int:
350 case BuiltinType::Long:
351 case BuiltinType::LongLong:
352 case BuiltinType::Int128:
353 case BuiltinType::Half:
354 case BuiltinType::Float:
355 case BuiltinType::Double:
356 case BuiltinType::LongDouble:
357 case BuiltinType::Float16:
358 case BuiltinType::Float128:
359 case BuiltinType::Ibm128:
360 case BuiltinType::ShortAccum:
361 case BuiltinType::Accum:
362 case BuiltinType::LongAccum:
363 case BuiltinType::UShortAccum:
364 case BuiltinType::UAccum:
365 case BuiltinType::ULongAccum:
366 case BuiltinType::ShortFract:
367 case BuiltinType::Fract:
368 case BuiltinType::LongFract:
369 case BuiltinType::UShortFract:
370 case BuiltinType::UFract:
371 case BuiltinType::ULongFract:
372 case BuiltinType::SatShortAccum:
373 case BuiltinType::SatAccum:
374 case BuiltinType::SatLongAccum:
375 case BuiltinType::SatUShortAccum:
376 case BuiltinType::SatUAccum:
377 case BuiltinType::SatULongAccum:
378 case BuiltinType::SatShortFract:
379 case BuiltinType::SatFract:
380 case BuiltinType::SatLongFract:
381 case BuiltinType::SatUShortFract:
382 case BuiltinType::SatUFract:
383 case BuiltinType::SatULongFract:
384 case BuiltinType::BFloat16:
385 llvm_unreachable("Builtin type needs extra local data!");
386 // Fall through, if the impossible happens.
387
388 case BuiltinType::NullPtr:
389 case BuiltinType::Overload:
390 case BuiltinType::Dependent:
391 case BuiltinType::UnresolvedTemplate:
392 case BuiltinType::BoundMember:
393 case BuiltinType::UnknownAny:
394 case BuiltinType::ARCUnbridgedCast:
395 case BuiltinType::PseudoObject:
396 case BuiltinType::ObjCId:
397 case BuiltinType::ObjCClass:
398 case BuiltinType::ObjCSel:
399#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
400 case BuiltinType::Id:
401#include "clang/Basic/OpenCLImageTypes.def"
402#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
403 case BuiltinType::Id:
404#include "clang/Basic/OpenCLExtensionTypes.def"
405 case BuiltinType::OCLSampler:
406 case BuiltinType::OCLEvent:
407 case BuiltinType::OCLClkEvent:
408 case BuiltinType::OCLQueue:
409 case BuiltinType::OCLReserveID:
410#define SVE_TYPE(Name, Id, SingletonId) \
411 case BuiltinType::Id:
412#include "clang/Basic/AArch64ACLETypes.def"
413#define PPC_VECTOR_TYPE(Name, Id, Size) \
414 case BuiltinType::Id:
415#include "clang/Basic/PPCTypes.def"
416#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
417#include "clang/Basic/RISCVVTypes.def"
418#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
419#include "clang/Basic/WebAssemblyReferenceTypes.def"
420#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
421#include "clang/Basic/AMDGPUTypes.def"
422#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
423#include "clang/Basic/HLSLIntangibleTypes.def"
424#define HLSL_PACKED_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
425#include "clang/Basic/HLSLPackedTypes.def"
426#define SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
427#include "clang/Basic/SPIRVTypes.def"
428 case BuiltinType::BuiltinFn:
429 case BuiltinType::IncompleteMatrixIdx:
430 case BuiltinType::ArraySection:
431 case BuiltinType::OMPArrayShaping:
432 case BuiltinType::OMPIterator:
433 case BuiltinType::MetaInfo:
434 return TST_unspecified;
435 }
436
437 llvm_unreachable("Invalid BuiltinType Kind!");
438}
439
440TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
441 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
442 TL = PTL.getInnerLoc();
443 return TL;
444}
445
446SourceLocation TypeLoc::findNullabilityLoc() const {
447 if (auto ATL = getAs<AttributedTypeLoc>()) {
448 const Attr *A = ATL.getAttr();
449 if (A && (isa<TypeNullableAttr>(Val: A) || isa<TypeNonNullAttr>(Val: A) ||
450 isa<TypeNullUnspecifiedAttr>(Val: A)))
451 return A->getLocation();
452 }
453
454 return {};
455}
456
457TypeLoc TypeLoc::findExplicitQualifierLoc() const {
458 // Qualified types.
459 if (auto qual = getAs<QualifiedTypeLoc>())
460 return qual;
461
462 TypeLoc loc = IgnoreParens();
463
464 // Attributed types.
465 if (auto attr = loc.getAs<AttributedTypeLoc>()) {
466 if (attr.isQualifier()) return attr;
467 return attr.getModifiedLoc().findExplicitQualifierLoc();
468 }
469
470 // C11 _Atomic types.
471 if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
472 return atomic;
473 }
474
475 return {};
476}
477
478NestedNameSpecifierLoc TypeLoc::getPrefix() const {
479 switch (getTypeLocClass()) {
480 case TypeLoc::DependentName:
481 return castAs<DependentNameTypeLoc>().getQualifierLoc();
482 case TypeLoc::TemplateSpecialization:
483 return castAs<TemplateSpecializationTypeLoc>().getQualifierLoc();
484 case TypeLoc::DeducedTemplateSpecialization:
485 return castAs<DeducedTemplateSpecializationTypeLoc>().getQualifierLoc();
486 case TypeLoc::Enum:
487 case TypeLoc::Record:
488 case TypeLoc::InjectedClassName:
489 return castAs<TagTypeLoc>().getQualifierLoc();
490 case TypeLoc::Typedef:
491 return castAs<TypedefTypeLoc>().getQualifierLoc();
492 case TypeLoc::UnresolvedUsing:
493 return castAs<UnresolvedUsingTypeLoc>().getQualifierLoc();
494 case TypeLoc::Using:
495 return castAs<UsingTypeLoc>().getQualifierLoc();
496 default:
497 return NestedNameSpecifierLoc();
498 }
499}
500
501SourceLocation TypeLoc::getNonElaboratedBeginLoc() const {
502 // For elaborated types (e.g. `struct a::A`) we want the portion after the
503 // `struct` but including the namespace qualifier, `a::`.
504 switch (getTypeLocClass()) {
505 case TypeLoc::Qualified:
506 return castAs<QualifiedTypeLoc>()
507 .getUnqualifiedLoc()
508 .getNonElaboratedBeginLoc();
509 case TypeLoc::TemplateSpecialization: {
510 auto T = castAs<TemplateSpecializationTypeLoc>();
511 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
512 return QualifierLoc.getBeginLoc();
513 return T.getTemplateNameLoc();
514 }
515 case TypeLoc::DeducedTemplateSpecialization: {
516 auto T = castAs<DeducedTemplateSpecializationTypeLoc>();
517 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
518 return QualifierLoc.getBeginLoc();
519 return T.getTemplateNameLoc();
520 }
521 case TypeLoc::DependentName: {
522 auto T = castAs<DependentNameTypeLoc>();
523 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
524 return QualifierLoc.getBeginLoc();
525 return T.getNameLoc();
526 }
527 case TypeLoc::Enum:
528 case TypeLoc::Record:
529 case TypeLoc::InjectedClassName: {
530 auto T = castAs<TagTypeLoc>();
531 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
532 return QualifierLoc.getBeginLoc();
533 return T.getNameLoc();
534 }
535 case TypeLoc::Typedef: {
536 auto T = castAs<TypedefTypeLoc>();
537 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
538 return QualifierLoc.getBeginLoc();
539 return T.getNameLoc();
540 }
541 case TypeLoc::UnresolvedUsing: {
542 auto T = castAs<UnresolvedUsingTypeLoc>();
543 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
544 return QualifierLoc.getBeginLoc();
545 return T.getNameLoc();
546 }
547 case TypeLoc::Using: {
548 auto T = castAs<UsingTypeLoc>();
549 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
550 return QualifierLoc.getBeginLoc();
551 return T.getNameLoc();
552 }
553 default:
554 return getBeginLoc();
555 }
556}
557
558void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
559 SourceLocation Loc) {
560 setNameLoc(Loc);
561 if (!getNumProtocols()) return;
562
563 setProtocolLAngleLoc(Loc);
564 setProtocolRAngleLoc(Loc);
565 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
566 setProtocolLoc(i, Loc);
567}
568
569void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
570 SourceLocation Loc) {
571 setHasBaseTypeAsWritten(true);
572 setTypeArgsLAngleLoc(Loc);
573 setTypeArgsRAngleLoc(Loc);
574 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
575 setTypeArgTInfo(i,
576 TInfo: Context.getTrivialTypeSourceInfo(
577 T: getTypePtr()->getTypeArgsAsWritten()[i], Loc));
578 }
579 setProtocolLAngleLoc(Loc);
580 setProtocolRAngleLoc(Loc);
581 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
582 setProtocolLoc(i, Loc);
583}
584
585SourceRange AttributedTypeLoc::getLocalSourceRange() const {
586 // Note that this does *not* include the range of the attribute
587 // enclosure, e.g.:
588 // __attribute__((foo(bar)))
589 // ^~~~~~~~~~~~~~~ ~~
590 // or
591 // [[foo(bar)]]
592 // ^~ ~~
593 // That enclosure doesn't necessarily belong to a single attribute
594 // anyway.
595 return getAttr() ? getAttr()->getRange() : SourceRange();
596}
597
598SourceRange CountAttributedTypeLoc::getLocalSourceRange() const {
599 return getCountExpr() ? getCountExpr()->getSourceRange() : SourceRange();
600}
601
602SourceRange BTFTagAttributedTypeLoc::getLocalSourceRange() const {
603 return getAttr() ? getAttr()->getRange() : SourceRange();
604}
605
606SourceRange OverflowBehaviorTypeLoc::getLocalSourceRange() const {
607 return SourceRange();
608}
609
610void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
611 SourceLocation Loc) {
612 TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
613 ::initializeLocal(Context, Loc);
614 this->getLocalData()->UnmodifiedTInfo =
615 Context.getTrivialTypeSourceInfo(T: getUnmodifiedType(), Loc);
616}
617
618void UnaryTransformTypeLoc::initializeLocal(ASTContext &Context,
619 SourceLocation Loc) {
620 setKWLoc(Loc);
621 setRParenLoc(Loc);
622 setLParenLoc(Loc);
623 this->setUnderlyingTInfo(
624 Context.getTrivialTypeSourceInfo(T: getTypePtr()->getBaseType(), Loc));
625}
626
627template <class TL>
628static void initializeElaboratedKeyword(TL T, SourceLocation Loc) {
629 T.setElaboratedKeywordLoc(T.getTypePtr()->getKeyword() !=
630 ElaboratedTypeKeyword::None
631 ? Loc
632 : SourceLocation());
633}
634
635static NestedNameSpecifierLoc initializeQualifier(ASTContext &Context,
636 NestedNameSpecifier Qualifier,
637 SourceLocation Loc) {
638 if (!Qualifier)
639 return NestedNameSpecifierLoc();
640 NestedNameSpecifierLocBuilder Builder;
641 Builder.MakeTrivial(Context, Qualifier, R: Loc);
642 return Builder.getWithLocInContext(Context);
643}
644
645void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
646 SourceLocation Loc) {
647 initializeElaboratedKeyword(T: *this, Loc);
648 setQualifierLoc(
649 initializeQualifier(Context, Qualifier: getTypePtr()->getQualifier(), Loc));
650 setNameLoc(Loc);
651}
652
653void TemplateSpecializationTypeLoc::set(SourceLocation ElaboratedKeywordLoc,
654 NestedNameSpecifierLoc QualifierLoc,
655 SourceLocation TemplateKeywordLoc,
656 SourceLocation NameLoc,
657 SourceLocation LAngleLoc,
658 SourceLocation RAngleLoc) {
659 TemplateSpecializationLocInfo &Data = *getLocalData();
660
661 Data.ElaboratedKWLoc = ElaboratedKeywordLoc;
662 SourceLocation BeginLoc = ElaboratedKeywordLoc;
663
664 getLocalData()->QualifierData = QualifierLoc.getOpaqueData();
665
666 assert(QualifierLoc.getNestedNameSpecifier() ==
667 getTypePtr()->getTemplateName().getQualifier());
668 Data.QualifierData = QualifierLoc ? QualifierLoc.getOpaqueData() : nullptr;
669 if (QualifierLoc && !BeginLoc.isValid())
670 BeginLoc = QualifierLoc.getBeginLoc();
671
672 Data.TemplateKWLoc = TemplateKeywordLoc;
673 if (!BeginLoc.isValid())
674 BeginLoc = TemplateKeywordLoc;
675
676 Data.NameLoc = NameLoc;
677 if (!BeginLoc.isValid())
678 BeginLoc = NameLoc;
679
680 Data.LAngleLoc = LAngleLoc;
681 Data.SR = SourceRange(BeginLoc, RAngleLoc);
682}
683
684void TemplateSpecializationTypeLoc::set(SourceLocation ElaboratedKeywordLoc,
685 NestedNameSpecifierLoc QualifierLoc,
686 SourceLocation TemplateKeywordLoc,
687 SourceLocation NameLoc,
688 const TemplateArgumentListInfo &TAL) {
689 set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
690 LAngleLoc: TAL.getLAngleLoc(), RAngleLoc: TAL.getRAngleLoc());
691 MutableArrayRef<TemplateArgumentLocInfo> ArgInfos = getArgLocInfos();
692 assert(TAL.size() == ArgInfos.size());
693 for (unsigned I = 0, N = TAL.size(); I != N; ++I)
694 ArgInfos[I] = TAL[I].getLocInfo();
695}
696
697void TemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
698 SourceLocation Loc) {
699
700 auto [Qualifier, HasTemplateKeyword] =
701 getTypePtr()->getTemplateName().getQualifierAndTemplateKeyword();
702
703 SourceLocation ElaboratedKeywordLoc =
704 getTypePtr()->getKeyword() != ElaboratedTypeKeyword::None
705 ? Loc
706 : SourceLocation();
707
708 NestedNameSpecifierLoc QualifierLoc;
709 if (Qualifier) {
710 NestedNameSpecifierLocBuilder Builder;
711 Builder.MakeTrivial(Context, Qualifier, R: Loc);
712 QualifierLoc = Builder.getWithLocInContext(Context);
713 }
714
715 TemplateArgumentListInfo TAL(Loc, Loc);
716 set(ElaboratedKeywordLoc, QualifierLoc,
717 /*TemplateKeywordLoc=*/HasTemplateKeyword ? Loc : SourceLocation(),
718 /*NameLoc=*/Loc, /*LAngleLoc=*/Loc, /*RAngleLoc=*/Loc);
719 initializeArgLocs(Context, Args: getTypePtr()->template_arguments(), ArgInfos: getArgInfos(),
720 Loc);
721}
722
723void TemplateSpecializationTypeLoc::initializeArgLocs(
724 ASTContext &Context, ArrayRef<TemplateArgument> Args,
725 TemplateArgumentLocInfo *ArgInfos, SourceLocation Loc) {
726 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
727 switch (Args[i].getKind()) {
728 case TemplateArgument::Null:
729 llvm_unreachable("Impossible TemplateArgument");
730
731 case TemplateArgument::Pack:
732 case TemplateArgument::Integral:
733 case TemplateArgument::Declaration:
734 case TemplateArgument::NullPtr:
735 case TemplateArgument::StructuralValue:
736 ArgInfos[i] = TemplateArgumentLocInfo(Context, Loc);
737 break;
738
739 case TemplateArgument::Expression:
740 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
741 break;
742
743 case TemplateArgument::Type:
744 ArgInfos[i] = TemplateArgumentLocInfo(
745 Context.getTrivialTypeSourceInfo(T: Args[i].getAsType(),
746 Loc));
747 break;
748
749 case TemplateArgument::Template:
750 case TemplateArgument::TemplateExpansion: {
751 NestedNameSpecifierLocBuilder Builder;
752 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
753 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
754 Builder.MakeTrivial(Context, Qualifier: DTN->getQualifier(), R: Loc);
755 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
756 Builder.MakeTrivial(Context, Qualifier: QTN->getQualifier(), R: Loc);
757
758 ArgInfos[i] = TemplateArgumentLocInfo(
759 Context, Loc, Builder.getWithLocInContext(Context), Loc,
760 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
761 : Loc);
762 break;
763 }
764 }
765 }
766}
767
768// Builds a ConceptReference where all locations point at the same token,
769// for use in trivial TypeSourceInfo for constrained AutoType
770static ConceptReference *createTrivialConceptReference(ASTContext &Context,
771 SourceLocation Loc,
772 const AutoType *AT) {
773 DeclarationName ConceptName =
774 Context.getNameForTemplate(Name: AT->getTypeConstraintConcept(), NameLoc: Loc).getName();
775 DeclarationNameInfo DNI = DeclarationNameInfo(ConceptName, Loc, ConceptName);
776 unsigned size = AT->getTypeConstraintArguments().size();
777 llvm::SmallVector<TemplateArgumentLocInfo, 8> TALI(size);
778 TemplateSpecializationTypeLoc::initializeArgLocs(
779 Context, Args: AT->getTypeConstraintArguments(), ArgInfos: TALI.data(), Loc);
780 TemplateArgumentListInfo TAListI;
781 for (unsigned i = 0; i < size; ++i) {
782 TAListI.addArgument(
783 Loc: TemplateArgumentLoc(AT->getTypeConstraintArguments()[i],
784 TALI[i])); // TemplateArgumentLocInfo()
785 }
786
787 auto *ConceptRef = ConceptReference::Create(
788 C: Context, NNS: NestedNameSpecifierLoc{}, TemplateKWLoc: Loc, ConceptNameInfo: DNI, FoundDecl: nullptr,
789 NamedConcept: AT->getTypeConstraintConcept(),
790 ArgsAsWritten: ASTTemplateArgumentListInfo::Create(C: Context, List: TAListI));
791 return ConceptRef;
792}
793
794void AutoTypeLoc::initializeLocal(ASTContext &Context, SourceLocation Loc) {
795 setRParenLoc(Loc);
796 setNameLoc(Loc);
797 setConceptReference(nullptr);
798 if (getTypePtr()->isConstrained()) {
799 setConceptReference(
800 createTrivialConceptReference(Context, Loc, AT: getTypePtr()));
801 }
802}
803
804void DeducedTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
805 SourceLocation Loc) {
806 initializeElaboratedKeyword(T: *this, Loc);
807 setQualifierLoc(initializeQualifier(
808 Context, Qualifier: getTypePtr()->getTemplateName().getQualifier(), Loc));
809 setTemplateNameLoc(Loc);
810}
811
812namespace {
813
814 class GetContainedAutoTypeLocVisitor :
815 public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
816 public:
817 using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
818
819 TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
820 return TL;
821 }
822
823 // Only these types can contain the desired 'auto' type.
824
825 TypeLoc VisitAtomicTypeLoc(AtomicTypeLoc T) {
826 return Visit(TyLoc: T.getValueLoc());
827 }
828
829 TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
830 return Visit(TyLoc: T.getUnqualifiedLoc());
831 }
832
833 TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
834 return Visit(TyLoc: T.getPointeeLoc());
835 }
836
837 TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
838 return Visit(TyLoc: T.getPointeeLoc());
839 }
840
841 TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
842 return Visit(TyLoc: T.getPointeeLoc());
843 }
844
845 TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
846 return Visit(TyLoc: T.getPointeeLoc());
847 }
848
849 TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
850 return Visit(TyLoc: T.getElementLoc());
851 }
852
853 TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
854 return Visit(TyLoc: T.getReturnLoc());
855 }
856
857 TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
858 return Visit(TyLoc: T.getInnerLoc());
859 }
860
861 TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
862 return Visit(TyLoc: T.getModifiedLoc());
863 }
864
865 TypeLoc VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc T) {
866 return Visit(TyLoc: T.getWrappedLoc());
867 }
868
869 TypeLoc VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc T) {
870 return Visit(TyLoc: T.getWrappedLoc());
871 }
872
873 TypeLoc
874 VisitHLSLAttributedResourceTypeLoc(HLSLAttributedResourceTypeLoc T) {
875 return Visit(TyLoc: T.getWrappedLoc());
876 }
877
878 TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
879 return Visit(TyLoc: T.getInnerLoc());
880 }
881
882 TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
883 return Visit(TyLoc: T.getOriginalLoc());
884 }
885
886 TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
887 return Visit(TyLoc: T.getPatternLoc());
888 }
889 };
890
891} // namespace
892
893AutoTypeLoc TypeLoc::getContainedAutoTypeLoc() const {
894 TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(TyLoc: *this);
895 if (Res.isNull())
896 return AutoTypeLoc();
897 return Res.getAs<AutoTypeLoc>();
898}
899
900SourceLocation TypeLoc::getTemplateKeywordLoc() const {
901 if (const auto TSTL = getAsAdjusted<TemplateSpecializationTypeLoc>())
902 return TSTL.getTemplateKeywordLoc();
903 return SourceLocation();
904}
905