clang 22.0.0git
TypeLoc.cpp
Go to the documentation of this file.
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"
16#include "clang/AST/Attr.h"
18#include "clang/AST/Expr.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(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.
76 if (Ty.isNull()) return 1;
77 return TypeAligner().Visit(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.
96 unsigned Total = 0;
97 TypeLoc TyLoc(Ty, nullptr);
98 unsigned MaxAlign = 1;
99 while (!TyLoc.isNull()) {
100 unsigned Align = getLocalAlignmentForType(TyLoc.getType());
101 MaxAlign = std::max(Align, MaxAlign);
102 Total = llvm::alignTo(Total, Align);
103 Total += TypeSizer().Visit(TyLoc);
104 TyLoc = TyLoc.getNextTypeLoc();
105 }
106 Total = llvm::alignTo(Total, 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(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
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(reinterpret_cast<uintptr_t>(Data),
178 reinterpret_cast<uintptr_t>(other.Data) ==
179 llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
181 memcpy(Data, other.Data, getFullDataSize());
182 return;
183 }
184
185 // Copy each of the pieces.
186 TypeLoc TL(getType(), Data);
187 do {
188 TypeLocCopier(other).Visit(TL);
189 other = other.getNextTypeLoc();
190 } while ((TL = TL.getNextTypeLoc()));
191}
192
194 TypeLoc Cur = *this;
195 TypeLoc LeftMost = Cur;
196 while (true) {
197 switch (Cur.getTypeLocClass()) {
198 case FunctionProto:
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:
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
228 TypeLoc Cur = *this;
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:
248 Last = TypeLoc();
249 else
250 Last = Cur;
251 break;
252 case ObjCObjectPointer:
253 // `id` and `id<...>` have no star location.
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(TL);
303}
304
306 return getTypePtr()->isTagOwned() &&
308}
309
310// Reimplemented to account for GNU/C++ extension
311// typeof unary-expression
312// where there are no parentheses.
314 if (getRParenLoc().isValid())
316 else
317 return SourceRange(getTypeofLoc(),
318 getUnderlyingExpr()->getSourceRange().getEnd());
319}
320
321
324 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
325 switch (getTypePtr()->getKind()) {
326 case BuiltinType::Void:
327 return TST_void;
328 case BuiltinType::Bool:
329 return TST_bool;
330 case BuiltinType::Char_U:
331 case BuiltinType::Char_S:
332 return TST_char;
333 case BuiltinType::Char8:
334 return TST_char8;
335 case BuiltinType::Char16:
336 return TST_char16;
337 case BuiltinType::Char32:
338 return TST_char32;
339 case BuiltinType::WChar_S:
340 case BuiltinType::WChar_U:
341 return TST_wchar;
342 case BuiltinType::UChar:
343 case BuiltinType::UShort:
344 case BuiltinType::UInt:
345 case BuiltinType::ULong:
346 case BuiltinType::ULongLong:
347 case BuiltinType::UInt128:
348 case BuiltinType::SChar:
349 case BuiltinType::Short:
350 case BuiltinType::Int:
351 case BuiltinType::Long:
352 case BuiltinType::LongLong:
353 case BuiltinType::Int128:
354 case BuiltinType::Half:
355 case BuiltinType::Float:
356 case BuiltinType::Double:
357 case BuiltinType::LongDouble:
358 case BuiltinType::Float16:
359 case BuiltinType::Float128:
360 case BuiltinType::Ibm128:
361 case BuiltinType::ShortAccum:
362 case BuiltinType::Accum:
363 case BuiltinType::LongAccum:
364 case BuiltinType::UShortAccum:
365 case BuiltinType::UAccum:
366 case BuiltinType::ULongAccum:
367 case BuiltinType::ShortFract:
368 case BuiltinType::Fract:
369 case BuiltinType::LongFract:
370 case BuiltinType::UShortFract:
371 case BuiltinType::UFract:
372 case BuiltinType::ULongFract:
373 case BuiltinType::SatShortAccum:
374 case BuiltinType::SatAccum:
375 case BuiltinType::SatLongAccum:
376 case BuiltinType::SatUShortAccum:
377 case BuiltinType::SatUAccum:
378 case BuiltinType::SatULongAccum:
379 case BuiltinType::SatShortFract:
380 case BuiltinType::SatFract:
381 case BuiltinType::SatLongFract:
382 case BuiltinType::SatUShortFract:
383 case BuiltinType::SatUFract:
384 case BuiltinType::SatULongFract:
385 case BuiltinType::BFloat16:
386 llvm_unreachable("Builtin type needs extra local data!");
387 // Fall through, if the impossible happens.
388
389 case BuiltinType::NullPtr:
390 case BuiltinType::Overload:
391 case BuiltinType::Dependent:
392 case BuiltinType::UnresolvedTemplate:
393 case BuiltinType::BoundMember:
394 case BuiltinType::UnknownAny:
395 case BuiltinType::ARCUnbridgedCast:
396 case BuiltinType::PseudoObject:
397 case BuiltinType::ObjCId:
398 case BuiltinType::ObjCClass:
399 case BuiltinType::ObjCSel:
400#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
401 case BuiltinType::Id:
402#include "clang/Basic/OpenCLImageTypes.def"
403#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
404 case BuiltinType::Id:
405#include "clang/Basic/OpenCLExtensionTypes.def"
406 case BuiltinType::OCLSampler:
407 case BuiltinType::OCLEvent:
408 case BuiltinType::OCLClkEvent:
409 case BuiltinType::OCLQueue:
410 case BuiltinType::OCLReserveID:
411#define SVE_TYPE(Name, Id, SingletonId) \
412 case BuiltinType::Id:
413#include "clang/Basic/AArch64ACLETypes.def"
414#define PPC_VECTOR_TYPE(Name, Id, Size) \
415 case BuiltinType::Id:
416#include "clang/Basic/PPCTypes.def"
417#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
418#include "clang/Basic/RISCVVTypes.def"
419#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
420#include "clang/Basic/WebAssemblyReferenceTypes.def"
421#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
422#include "clang/Basic/AMDGPUTypes.def"
423#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
424#include "clang/Basic/HLSLIntangibleTypes.def"
425 case BuiltinType::BuiltinFn:
426 case BuiltinType::IncompleteMatrixIdx:
427 case BuiltinType::ArraySection:
428 case BuiltinType::OMPArrayShaping:
429 case BuiltinType::OMPIterator:
430 return TST_unspecified;
431 }
432
433 llvm_unreachable("Invalid BuiltinType Kind!");
434}
435
436TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
437 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
438 TL = PTL.getInnerLoc();
439 return TL;
440}
441
443 if (auto ATL = getAs<AttributedTypeLoc>()) {
444 const Attr *A = ATL.getAttr();
445 if (A && (isa<TypeNullableAttr>(A) || isa<TypeNonNullAttr>(A) ||
447 return A->getLocation();
448 }
449
450 return {};
451}
452
454 // Qualified types.
455 if (auto qual = getAs<QualifiedTypeLoc>())
456 return qual;
457
458 TypeLoc loc = IgnoreParens();
459
460 // Attributed types.
461 if (auto attr = loc.getAs<AttributedTypeLoc>()) {
462 if (attr.isQualifier()) return attr;
463 return attr.getModifiedLoc().findExplicitQualifierLoc();
464 }
465
466 // C11 _Atomic types.
467 if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
468 return atomic;
469 }
470
471 return {};
472}
473
475 switch (getTypeLocClass()) {
476 case TypeLoc::DependentName:
477 return castAs<DependentNameTypeLoc>().getQualifierLoc();
478 case TypeLoc::TemplateSpecialization:
479 return castAs<TemplateSpecializationTypeLoc>().getQualifierLoc();
480 case TypeLoc::DeducedTemplateSpecialization:
481 return castAs<DeducedTemplateSpecializationTypeLoc>().getQualifierLoc();
482 case TypeLoc::Enum:
483 case TypeLoc::Record:
484 case TypeLoc::InjectedClassName:
485 return castAs<TagTypeLoc>().getQualifierLoc();
486 case TypeLoc::Typedef:
487 return castAs<TypedefTypeLoc>().getQualifierLoc();
488 case TypeLoc::UnresolvedUsing:
489 return castAs<UnresolvedUsingTypeLoc>().getQualifierLoc();
490 case TypeLoc::Using:
491 return castAs<UsingTypeLoc>().getQualifierLoc();
492 default:
493 return NestedNameSpecifierLoc();
494 }
495}
496
498 switch (getTypeLocClass()) {
499 case TypeLoc::TemplateSpecialization: {
502 if (!Loc.isValid())
503 Loc = TL.getTemplateNameLoc();
504 return Loc;
505 }
506 case TypeLoc::DeducedTemplateSpecialization: {
509 if (!Loc.isValid())
510 Loc = TL.getTemplateNameLoc();
511 return Loc;
512 }
513 case TypeLoc::DependentName:
514 return castAs<DependentNameTypeLoc>().getNameLoc();
515 case TypeLoc::Enum:
516 case TypeLoc::Record:
517 case TypeLoc::InjectedClassName:
518 return castAs<TagTypeLoc>().getNameLoc();
519 case TypeLoc::Typedef:
520 return castAs<TypedefTypeLoc>().getNameLoc();
521 case TypeLoc::UnresolvedUsing:
522 return castAs<UnresolvedUsingTypeLoc>().getNameLoc();
523 case TypeLoc::Using:
524 return castAs<UsingTypeLoc>().getNameLoc();
525 default:
526 return getBeginLoc();
527 }
528}
529
531 // For elaborated types (e.g. `struct a::A`) we want the portion after the
532 // `struct` but including the namespace qualifier, `a::`.
533 switch (getTypeLocClass()) {
536 .getUnqualifiedLoc()
537 .getNonElaboratedBeginLoc();
538 case TypeLoc::TemplateSpecialization: {
540 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
541 return QualifierLoc.getBeginLoc();
542 return T.getTemplateNameLoc();
543 }
544 case TypeLoc::DeducedTemplateSpecialization: {
546 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
547 return QualifierLoc.getBeginLoc();
548 return T.getTemplateNameLoc();
549 }
550 case TypeLoc::DependentName: {
552 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
553 return QualifierLoc.getBeginLoc();
554 return T.getNameLoc();
555 }
556 case TypeLoc::Enum:
557 case TypeLoc::Record:
558 case TypeLoc::InjectedClassName: {
559 auto T = castAs<TagTypeLoc>();
560 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
561 return QualifierLoc.getBeginLoc();
562 return T.getNameLoc();
563 }
564 case TypeLoc::Typedef: {
565 auto T = castAs<TypedefTypeLoc>();
566 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
567 return QualifierLoc.getBeginLoc();
568 return T.getNameLoc();
569 }
570 case TypeLoc::UnresolvedUsing: {
572 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
573 return QualifierLoc.getBeginLoc();
574 return T.getNameLoc();
575 }
576 case TypeLoc::Using: {
577 auto T = castAs<UsingTypeLoc>();
578 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
579 return QualifierLoc.getBeginLoc();
580 return T.getNameLoc();
581 }
582 default:
583 return getBeginLoc();
584 }
585}
586
588 SourceLocation Loc) {
589 setNameLoc(Loc);
590 if (!getNumProtocols()) return;
591
594 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
595 setProtocolLoc(i, Loc);
596}
597
599 SourceLocation Loc) {
603 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
605 Context.getTrivialTypeSourceInfo(
606 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
607 }
610 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
611 setProtocolLoc(i, Loc);
612}
613
615 // Note that this does *not* include the range of the attribute
616 // enclosure, e.g.:
617 // __attribute__((foo(bar)))
618 // ^~~~~~~~~~~~~~~ ~~
619 // or
620 // [[foo(bar)]]
621 // ^~ ~~
622 // That enclosure doesn't necessarily belong to a single attribute
623 // anyway.
624 return getAttr() ? getAttr()->getRange() : SourceRange();
625}
626
630
634
642
644 SourceLocation Loc) {
645 setKWLoc(Loc);
646 setRParenLoc(Loc);
647 setLParenLoc(Loc);
648 this->setUnderlyingTInfo(
649 Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
650}
651
652template <class TL>
654 T.setElaboratedKeywordLoc(T.getTypePtr()->getKeyword() !=
656 ? Loc
657 : SourceLocation());
658}
659
661 NestedNameSpecifier Qualifier,
662 SourceLocation Loc) {
663 if (!Qualifier)
664 return NestedNameSpecifierLoc();
666 Builder.MakeTrivial(Context, Qualifier, Loc);
667 return Builder.getWithLocInContext(Context);
668}
669
671 SourceLocation Loc) {
672 initializeElaboratedKeyword(*this, Loc);
674 initializeQualifier(Context, getTypePtr()->getQualifier(), Loc));
675 setNameLoc(Loc);
676}
677
679 NestedNameSpecifierLoc QualifierLoc,
680 SourceLocation TemplateKeywordLoc,
681 SourceLocation NameLoc,
682 SourceLocation LAngleLoc,
683 SourceLocation RAngleLoc) {
685
686 Data.ElaboratedKWLoc = ElaboratedKeywordLoc;
687 SourceLocation BeginLoc = ElaboratedKeywordLoc;
688
689 getLocalData()->QualifierData = QualifierLoc.getOpaqueData();
690
691 assert(QualifierLoc.getNestedNameSpecifier() ==
692 getTypePtr()->getTemplateName().getQualifier());
693 Data.QualifierData = QualifierLoc ? QualifierLoc.getOpaqueData() : nullptr;
694 if (QualifierLoc && !BeginLoc.isValid())
695 BeginLoc = QualifierLoc.getBeginLoc();
696
697 Data.TemplateKWLoc = TemplateKeywordLoc;
698 if (!BeginLoc.isValid())
699 BeginLoc = TemplateKeywordLoc;
700
701 Data.NameLoc = NameLoc;
702 if (!BeginLoc.isValid())
703 BeginLoc = NameLoc;
704
705 Data.LAngleLoc = LAngleLoc;
706 Data.SR = SourceRange(BeginLoc, RAngleLoc);
707}
708
710 NestedNameSpecifierLoc QualifierLoc,
711 SourceLocation TemplateKeywordLoc,
712 SourceLocation NameLoc,
713 const TemplateArgumentListInfo &TAL) {
714 set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
715 TAL.getLAngleLoc(), TAL.getRAngleLoc());
717 assert(TAL.size() == ArgInfos.size());
718 for (unsigned I = 0, N = TAL.size(); I != N; ++I)
719 ArgInfos[I] = TAL[I].getLocInfo();
720}
721
723 SourceLocation Loc) {
724
725 auto [Qualifier, HasTemplateKeyword] =
726 getTypePtr()->getTemplateName().getQualifierAndTemplateKeyword();
727
728 SourceLocation ElaboratedKeywordLoc =
729 getTypePtr()->getKeyword() != ElaboratedTypeKeyword::None
730 ? Loc
731 : SourceLocation();
732
733 NestedNameSpecifierLoc QualifierLoc;
734 if (Qualifier) {
736 Builder.MakeTrivial(Context, Qualifier, Loc);
737 QualifierLoc = Builder.getWithLocInContext(Context);
738 }
739
740 TemplateArgumentListInfo TAL(Loc, Loc);
741 set(ElaboratedKeywordLoc, QualifierLoc,
742 /*TemplateKeywordLoc=*/HasTemplateKeyword ? Loc : SourceLocation(),
743 /*NameLoc=*/Loc, /*LAngleLoc=*/Loc, /*RAngleLoc=*/Loc);
744 initializeArgLocs(Context, getTypePtr()->template_arguments(), getArgInfos(),
745 Loc);
746}
747
751 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
752 switch (Args[i].getKind()) {
754 llvm_unreachable("Impossible TemplateArgument");
755
760 ArgInfos[i] = TemplateArgumentLocInfo();
761 break;
762
764 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
765 break;
766
768 ArgInfos[i] = TemplateArgumentLocInfo(
769 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
770 Loc));
771 break;
772
776 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
777 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
778 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
779 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
780 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
781
782 ArgInfos[i] = TemplateArgumentLocInfo(
783 Context, Loc, Builder.getWithLocInContext(Context), Loc,
784 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
785 : Loc);
786 break;
787 }
788
790 ArgInfos[i] = TemplateArgumentLocInfo();
791 break;
792 }
793 }
794}
795
796// Builds a ConceptReference where all locations point at the same token,
797// for use in trivial TypeSourceInfo for constrained AutoType
799 SourceLocation Loc,
800 const AutoType *AT) {
802 DeclarationNameInfo(AT->getTypeConstraintConcept()->getDeclName(), Loc,
803 AT->getTypeConstraintConcept()->getDeclName());
804 unsigned size = AT->getTypeConstraintArguments().size();
807 Context, AT->getTypeConstraintArguments(), TALI.data(), Loc);
809 for (unsigned i = 0; i < size; ++i) {
810 TAListI.addArgument(
811 TemplateArgumentLoc(AT->getTypeConstraintArguments()[i],
812 TALI[i])); // TemplateArgumentLocInfo()
813 }
814
815 auto *ConceptRef = ConceptReference::Create(
816 Context, NestedNameSpecifierLoc{}, Loc, DNI, nullptr,
817 AT->getTypeConstraintConcept(),
818 ASTTemplateArgumentListInfo::Create(Context, TAListI));
819 return ConceptRef;
820}
821
823 setRParenLoc(Loc);
824 setNameLoc(Loc);
825 setConceptReference(nullptr);
826 if (getTypePtr()->isConstrained()) {
829 }
830}
831
833 SourceLocation Loc) {
834 initializeElaboratedKeyword(*this, Loc);
836 Context, getTypePtr()->getTemplateName().getQualifier(), Loc));
838}
839
840namespace {
841
842 class GetContainedAutoTypeLocVisitor :
843 public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
844 public:
845 using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
846
847 TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
848 return TL;
849 }
850
851 // Only these types can contain the desired 'auto' type.
852
853 TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
854 return Visit(T.getUnqualifiedLoc());
855 }
856
857 TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
858 return Visit(T.getPointeeLoc());
859 }
860
861 TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
862 return Visit(T.getPointeeLoc());
863 }
864
865 TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
866 return Visit(T.getPointeeLoc());
867 }
868
869 TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
870 return Visit(T.getPointeeLoc());
871 }
872
873 TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
874 return Visit(T.getElementLoc());
875 }
876
877 TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
878 return Visit(T.getReturnLoc());
879 }
880
881 TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
882 return Visit(T.getInnerLoc());
883 }
884
885 TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
886 return Visit(T.getModifiedLoc());
887 }
888
889 TypeLoc VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc T) {
890 return Visit(T.getWrappedLoc());
891 }
892
893 TypeLoc
894 VisitHLSLAttributedResourceTypeLoc(HLSLAttributedResourceTypeLoc T) {
895 return Visit(T.getWrappedLoc());
896 }
897
898 TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
899 return Visit(T.getInnerLoc());
900 }
901
902 TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
903 return Visit(T.getOriginalLoc());
904 }
905
906 TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
907 return Visit(T.getPatternLoc());
908 }
909 };
910
911} // namespace
912
914 TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
915 if (Res.isNull())
916 return AutoTypeLoc();
917 return Res.getAs<AutoTypeLoc>();
918}
919
921 if (const auto TSTL = getAsAdjusted<TemplateSpecializationTypeLoc>())
922 return TSTL.getTemplateKeywordLoc();
923 return SourceLocation();
924}
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)
Definition TypeLoc.cpp:798
static const unsigned TypeLocMaxDataAlign
Definition TypeLoc.cpp:35
static NestedNameSpecifierLoc initializeQualifier(ASTContext &Context, NestedNameSpecifier Qualifier, SourceLocation Loc)
Definition TypeLoc.cpp:660
static void initializeElaboratedKeyword(TL T, SourceLocation Loc)
Definition TypeLoc.cpp:653
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 ...
Definition ASTContext.h:188
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
Attr - This represents one attribute.
Definition Attr.h:44
SourceLocation getLocation() const
Definition Attr.h:97
Type source information for an attributed type.
Definition TypeLoc.h:1017
const Attr * getAttr() const
The type attribute.
Definition TypeLoc.h:1040
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:614
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:822
void setConceptReference(ConceptReference *CR)
Definition TypeLoc.h:2385
bool isConstrained() const
Definition TypeLoc.h:2381
void setRParenLoc(SourceLocation Loc)
Definition TypeLoc.h:2379
const BTFTypeTagAttr * getAttr() const
The btf_type_tag attribute.
Definition TypeLoc.h:1072
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:631
TypeSpecifierType getWrittenTypeSpec() const
Definition TypeLoc.cpp:322
bool needsExtraLocalData() const
Definition TypeLoc.h:611
WrittenBuiltinSpecs & getWrittenBuiltinSpecs()
Definition TypeLoc.h:604
A reference to a concept and its template args, as it appears in the code.
Definition ASTConcept.h:126
static ConceptReference * Create(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Expr * getCountExpr() const
Definition TypeLoc.h:1334
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:627
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition TypeLoc.h:2511
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:832
void setTemplateNameLoc(SourceLocation Loc)
Definition TypeLoc.h:2499
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:670
void setNameLoc(SourceLocation Loc)
Definition TypeLoc.h:2581
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition TypeLoc.h:2570
bool hasTrailingReturn() const
Whether this function prototype has a trailing return type.
Definition TypeBase.h:5684
const TypeClass * getTypePtr() const
Definition TypeLoc.h:531
Class that aids in the construction of nested-name-specifiers along with source-location information ...
void MakeTrivial(ASTContext &Context, NestedNameSpecifier Qualifier, SourceRange R)
Make a new nested-name-specifier from incomplete source-location information.
A C++ nested-name-specifier augmented with source location information.
NestedNameSpecifier getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
SourceLocation getBeginLoc() const
Retrieve the location of the beginning of this nested-name-specifier.
void * getOpaqueData() const
Retrieve the opaque pointer that refers to source-location data.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
Wraps an ObjCPointerType with source location information.
Definition TypeLoc.h:1566
SourceLocation getStarLoc() const
Definition TypeLoc.h:1568
void setTypeArgsRAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1176
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:598
unsigned getNumTypeArgs() const
Definition TypeLoc.h:1180
unsigned getNumProtocols() const
Definition TypeLoc.h:1210
void setTypeArgsLAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1168
void setTypeArgTInfo(unsigned i, TypeSourceInfo *TInfo)
Definition TypeLoc.h:1189
void setProtocolLAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1198
void setProtocolRAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1206
void setHasBaseTypeAsWritten(bool HasBaseType)
Definition TypeLoc.h:1238
void setProtocolLoc(unsigned i, SourceLocation Loc)
Definition TypeLoc.h:1219
unsigned getNumProtocols() const
Definition TypeLoc.h:941
void setProtocolLoc(unsigned i, SourceLocation Loc)
Definition TypeLoc.h:950
void setProtocolLAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:927
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:587
void setProtocolRAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:937
void setNameLoc(SourceLocation Loc)
Definition TypeLoc.h:917
A (possibly-)qualified type.
Definition TypeBase.h:937
bool hasLocalQualifiers() const
Determine whether this particular QualType instance has any qualifiers, without looking through any t...
Definition TypeBase.h:1064
Represents a template name as written in source code.
Wrapper of type source information for a type with non-trivial direct qualifiers.
Definition TypeLoc.h:305
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...
Definition Stmt.cpp:334
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3809
TagDecl * getOriginalDecl() const
Definition TypeLoc.h:801
bool isDefinition() const
True if the tag was defined in this type specifier.
Definition TypeLoc.cpp:305
A convenient class for passing around template argument information.
SourceLocation getRAngleLoc() const
void addArgument(const TemplateArgumentLoc &Loc)
SourceLocation getLAngleLoc() const
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.
static void initializeArgLocs(ASTContext &Context, ArrayRef< TemplateArgument > Args, TemplateArgumentLocInfo *ArgInfos, SourceLocation Loc)
Definition TypeLoc.cpp:748
MutableArrayRef< TemplateArgumentLocInfo > getArgLocInfos()
Definition TypeLoc.h:1893
void set(SourceLocation ElaboratedKeywordLoc, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKeywordLoc, SourceLocation NameLoc, SourceLocation LAngleLoc, SourceLocation RAngleLoc)
Definition TypeLoc.cpp:678
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:722
RetTy Visit(TypeLoc TyLoc)
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
SourceLocation findNullabilityLoc() const
Find the location of the nullability specifier (__nonnull, __nullable, or __null_unspecifier),...
Definition TypeLoc.cpp:442
TypeLoc()=default
static unsigned getLocalAlignmentForType(QualType Ty)
Returns the alignment of type source info data block for the given type.
Definition TypeLoc.cpp:75
TypeLoc findExplicitQualifierLoc() const
Find a type with the location of an explicit type qualifier.
Definition TypeLoc.cpp:453
QualType getType() const
Get the type for which this source info wrapper provides information.
Definition TypeLoc.h:133
TypeLoc getNextTypeLoc() const
Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the TypeLoc is a PointerLoc and next Typ...
Definition TypeLoc.h:171
T getAs() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition TypeLoc.h:89
NestedNameSpecifierLoc getPrefix() const
If this type represents a qualified-id, this returns it's nested name specifier.
Definition TypeLoc.cpp:474
TypeLoc IgnoreParens() const
Definition TypeLoc.h:1417
T castAs() const
Convert to the specified TypeLoc type, asserting that this TypeLoc is of the desired type.
Definition TypeLoc.h:78
void * Data
Definition TypeLoc.h:64
SourceLocation getNonElaboratedBeginLoc() const
This returns the position of the type after any elaboration, such as the 'struct' keyword.
Definition TypeLoc.cpp:530
SourceRange getSourceRange() const LLVM_READONLY
Get the full source range.
Definition TypeLoc.h:154
SourceRange getLocalSourceRange() const
Get the local source range.
Definition TypeLoc.h:160
unsigned getFullDataSize() const
Returns the size of the type source info data block.
Definition TypeLoc.h:165
AutoTypeLoc getContainedAutoTypeLoc() const
Get the typeloc of an AutoType whose type will be deduced for a variable with an initializer of this ...
Definition TypeLoc.cpp:913
const void * Ty
Definition TypeLoc.h:63
SourceLocation getTemplateKeywordLoc() const
Get the SourceLocation of the template keyword (if any).
Definition TypeLoc.cpp:920
void copy(TypeLoc other)
Copies the other type loc into this one.
Definition TypeLoc.cpp:169
TypeLocClass getTypeLocClass() const
Definition TypeLoc.h:116
static unsigned getFullDataSizeForType(QualType Ty)
Returns the size of type source info data block for the given type.
Definition TypeLoc.cpp:95
bool isNull() const
Definition TypeLoc.h:121
SourceLocation getEndLoc() const
Get the end source location.
Definition TypeLoc.cpp:227
T getAsAdjusted() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition TypeLoc.h:2715
SourceLocation getBeginLoc() const
Get the begin source location.
Definition TypeLoc.cpp:193
SourceLocation getNonPrefixBeginLoc() const
This returns the position of the type after any elaboration, such as the 'struct' keyword,...
Definition TypeLoc.cpp:497
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:313
Expr * getUnderlyingExpr() const
Definition TypeLoc.h:2224
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:635
QualType getUnmodifiedType() const
Definition TypeLoc.h:2237
A reasonable base class for TypeLocs that correspond to types that are written as a type-specifier.
Definition TypeLoc.h:545
void setNameLoc(SourceLocation Loc)
Definition TypeLoc.h:556
void setRParenLoc(SourceLocation Loc)
Definition TypeLoc.h:2330
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:643
void setKWLoc(SourceLocation Loc)
Definition TypeLoc.h:2324
void setUnderlyingTInfo(TypeSourceInfo *TInfo)
Definition TypeLoc.h:2336
void setLParenLoc(SourceLocation Loc)
Definition TypeLoc.h:2327
The JSON file list parser is used to communicate input to InstallAPI.
TypeSpecifierType
Specifies the kind of type.
Definition Specifiers.h:55
@ TST_char32
Definition Specifiers.h:62
@ TST_wchar
Definition Specifiers.h:59
@ TST_char16
Definition Specifiers.h:61
@ TST_char
Definition Specifiers.h:58
@ TST_unspecified
Definition Specifiers.h:56
@ TST_bool
Definition Specifiers.h:75
@ TST_void
Definition Specifiers.h:57
@ TST_char8
Definition Specifiers.h:60
bool isa(CodeGen::Address addr)
Definition Address.h:330
const FunctionProtoType * T
@ Template
We are parsing a template declaration.
Definition Parser.h:81
@ None
No keyword precedes the qualified type name.
Definition TypeBase.h:5884
__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.
TypeSourceInfo * UnmodifiedTInfo
Definition TypeLoc.h:2169