clang 24.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() && getDecl()->isCompleteDefinition();
307}
308
309// Reimplemented to account for GNU/C++ extension
310// typeof unary-expression
311// where there are no parentheses.
313 if (getRParenLoc().isValid())
315 else
316 return SourceRange(getTypeofLoc(),
317 getUnderlyingExpr()->getSourceRange().getEnd());
318}
319
320
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 SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
425#include "clang/Basic/SPIRVTypes.def"
426 case BuiltinType::BuiltinFn:
427 case BuiltinType::IncompleteMatrixIdx:
428 case BuiltinType::ArraySection:
429 case BuiltinType::OMPArrayShaping:
430 case BuiltinType::OMPIterator:
431 return TST_unspecified;
432 }
433
434 llvm_unreachable("Invalid BuiltinType Kind!");
435}
436
437TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
438 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
439 TL = PTL.getInnerLoc();
440 return TL;
441}
442
444 if (auto ATL = getAs<AttributedTypeLoc>()) {
445 const Attr *A = ATL.getAttr();
446 if (A && (isa<TypeNullableAttr>(A) || isa<TypeNonNullAttr>(A) ||
448 return A->getLocation();
449 }
450
451 return {};
452}
453
455 // Qualified types.
456 if (auto qual = getAs<QualifiedTypeLoc>())
457 return qual;
458
459 TypeLoc loc = IgnoreParens();
460
461 // Attributed types.
462 if (auto attr = loc.getAs<AttributedTypeLoc>()) {
463 if (attr.isQualifier()) return attr;
464 return attr.getModifiedLoc().findExplicitQualifierLoc();
465 }
466
467 // C11 _Atomic types.
468 if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
469 return atomic;
470 }
471
472 return {};
473}
474
476 switch (getTypeLocClass()) {
477 case TypeLoc::DependentName:
478 return castAs<DependentNameTypeLoc>().getQualifierLoc();
479 case TypeLoc::TemplateSpecialization:
480 return castAs<TemplateSpecializationTypeLoc>().getQualifierLoc();
481 case TypeLoc::DeducedTemplateSpecialization:
482 return castAs<DeducedTemplateSpecializationTypeLoc>().getQualifierLoc();
483 case TypeLoc::Enum:
484 case TypeLoc::Record:
485 case TypeLoc::InjectedClassName:
486 return castAs<TagTypeLoc>().getQualifierLoc();
487 case TypeLoc::Typedef:
488 return castAs<TypedefTypeLoc>().getQualifierLoc();
489 case TypeLoc::UnresolvedUsing:
490 return castAs<UnresolvedUsingTypeLoc>().getQualifierLoc();
491 case TypeLoc::Using:
492 return castAs<UsingTypeLoc>().getQualifierLoc();
493 default:
494 return NestedNameSpecifierLoc();
495 }
496}
497
499 // For elaborated types (e.g. `struct a::A`) we want the portion after the
500 // `struct` but including the namespace qualifier, `a::`.
501 switch (getTypeLocClass()) {
504 .getUnqualifiedLoc()
505 .getNonElaboratedBeginLoc();
506 case TypeLoc::TemplateSpecialization: {
508 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
509 return QualifierLoc.getBeginLoc();
510 return T.getTemplateNameLoc();
511 }
512 case TypeLoc::DeducedTemplateSpecialization: {
514 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
515 return QualifierLoc.getBeginLoc();
516 return T.getTemplateNameLoc();
517 }
518 case TypeLoc::DependentName: {
520 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
521 return QualifierLoc.getBeginLoc();
522 return T.getNameLoc();
523 }
524 case TypeLoc::Enum:
525 case TypeLoc::Record:
526 case TypeLoc::InjectedClassName: {
527 auto T = castAs<TagTypeLoc>();
528 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
529 return QualifierLoc.getBeginLoc();
530 return T.getNameLoc();
531 }
532 case TypeLoc::Typedef: {
533 auto T = castAs<TypedefTypeLoc>();
534 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
535 return QualifierLoc.getBeginLoc();
536 return T.getNameLoc();
537 }
538 case TypeLoc::UnresolvedUsing: {
540 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
541 return QualifierLoc.getBeginLoc();
542 return T.getNameLoc();
543 }
544 case TypeLoc::Using: {
545 auto T = castAs<UsingTypeLoc>();
546 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
547 return QualifierLoc.getBeginLoc();
548 return T.getNameLoc();
549 }
550 default:
551 return getBeginLoc();
552 }
553}
554
556 SourceLocation Loc) {
557 setNameLoc(Loc);
558 if (!getNumProtocols()) return;
559
562 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
563 setProtocolLoc(i, Loc);
564}
565
567 SourceLocation Loc) {
571 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
573 Context.getTrivialTypeSourceInfo(
574 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
575 }
578 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
579 setProtocolLoc(i, Loc);
580}
581
583 // Note that this does *not* include the range of the attribute
584 // enclosure, e.g.:
585 // __attribute__((foo(bar)))
586 // ^~~~~~~~~~~~~~~ ~~
587 // or
588 // [[foo(bar)]]
589 // ^~ ~~
590 // That enclosure doesn't necessarily belong to a single attribute
591 // anyway.
592 return getAttr() ? getAttr()->getRange() : SourceRange();
593}
594
598
602
606
614
616 SourceLocation Loc) {
617 setKWLoc(Loc);
618 setRParenLoc(Loc);
619 setLParenLoc(Loc);
620 this->setUnderlyingTInfo(
621 Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
622}
623
624template <class TL>
626 T.setElaboratedKeywordLoc(T.getTypePtr()->getKeyword() !=
628 ? Loc
629 : SourceLocation());
630}
631
633 NestedNameSpecifier Qualifier,
634 SourceLocation Loc) {
635 if (!Qualifier)
636 return NestedNameSpecifierLoc();
638 Builder.MakeTrivial(Context, Qualifier, Loc);
639 return Builder.getWithLocInContext(Context);
640}
641
643 SourceLocation Loc) {
644 initializeElaboratedKeyword(*this, Loc);
646 initializeQualifier(Context, getTypePtr()->getQualifier(), Loc));
647 setNameLoc(Loc);
648}
649
651 NestedNameSpecifierLoc QualifierLoc,
652 SourceLocation TemplateKeywordLoc,
653 SourceLocation NameLoc,
654 SourceLocation LAngleLoc,
655 SourceLocation RAngleLoc) {
657
658 Data.ElaboratedKWLoc = ElaboratedKeywordLoc;
659 SourceLocation BeginLoc = ElaboratedKeywordLoc;
660
661 getLocalData()->QualifierData = QualifierLoc.getOpaqueData();
662
663 assert(QualifierLoc.getNestedNameSpecifier() ==
664 getTypePtr()->getTemplateName().getQualifier());
665 Data.QualifierData = QualifierLoc ? QualifierLoc.getOpaqueData() : nullptr;
666 if (QualifierLoc && !BeginLoc.isValid())
667 BeginLoc = QualifierLoc.getBeginLoc();
668
669 Data.TemplateKWLoc = TemplateKeywordLoc;
670 if (!BeginLoc.isValid())
671 BeginLoc = TemplateKeywordLoc;
672
673 Data.NameLoc = NameLoc;
674 if (!BeginLoc.isValid())
675 BeginLoc = NameLoc;
676
677 Data.LAngleLoc = LAngleLoc;
678 Data.SR = SourceRange(BeginLoc, RAngleLoc);
679}
680
682 NestedNameSpecifierLoc QualifierLoc,
683 SourceLocation TemplateKeywordLoc,
684 SourceLocation NameLoc,
685 const TemplateArgumentListInfo &TAL) {
686 set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
687 TAL.getLAngleLoc(), TAL.getRAngleLoc());
689 assert(TAL.size() == ArgInfos.size());
690 for (unsigned I = 0, N = TAL.size(); I != N; ++I)
691 ArgInfos[I] = TAL[I].getLocInfo();
692}
693
695 SourceLocation Loc) {
696
697 auto [Qualifier, HasTemplateKeyword] =
698 getTypePtr()->getTemplateName().getQualifierAndTemplateKeyword();
699
700 SourceLocation ElaboratedKeywordLoc =
701 getTypePtr()->getKeyword() != ElaboratedTypeKeyword::None
702 ? Loc
703 : SourceLocation();
704
705 NestedNameSpecifierLoc QualifierLoc;
706 if (Qualifier) {
708 Builder.MakeTrivial(Context, Qualifier, Loc);
709 QualifierLoc = Builder.getWithLocInContext(Context);
710 }
711
712 TemplateArgumentListInfo TAL(Loc, Loc);
713 set(ElaboratedKeywordLoc, QualifierLoc,
714 /*TemplateKeywordLoc=*/HasTemplateKeyword ? Loc : SourceLocation(),
715 /*NameLoc=*/Loc, /*LAngleLoc=*/Loc, /*RAngleLoc=*/Loc);
716 initializeArgLocs(Context, getTypePtr()->template_arguments(), getArgInfos(),
717 Loc);
718}
719
723 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
724 switch (Args[i].getKind()) {
726 llvm_unreachable("Impossible TemplateArgument");
727
733 ArgInfos[i] = TemplateArgumentLocInfo(Context, Loc);
734 break;
735
737 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
738 break;
739
741 ArgInfos[i] = TemplateArgumentLocInfo(
742 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
743 Loc));
744 break;
745
749 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
750 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
751 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
752 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
753 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
754
755 ArgInfos[i] = TemplateArgumentLocInfo(
756 Context, Loc, Builder.getWithLocInContext(Context), Loc,
757 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
758 : Loc);
759 break;
760 }
761 }
762 }
763}
764
765// Builds a ConceptReference where all locations point at the same token,
766// for use in trivial TypeSourceInfo for constrained AutoType
768 SourceLocation Loc,
769 const AutoType *AT) {
771 DeclarationNameInfo(AT->getTypeConstraintConcept()->getDeclName(), Loc,
772 AT->getTypeConstraintConcept()->getDeclName());
773 unsigned size = AT->getTypeConstraintArguments().size();
776 Context, AT->getTypeConstraintArguments(), TALI.data(), Loc);
778 for (unsigned i = 0; i < size; ++i) {
779 TAListI.addArgument(
780 TemplateArgumentLoc(AT->getTypeConstraintArguments()[i],
781 TALI[i])); // TemplateArgumentLocInfo()
782 }
783
784 auto *ConceptRef = ConceptReference::Create(
785 Context, NestedNameSpecifierLoc{}, Loc, DNI, nullptr,
786 AT->getTypeConstraintConcept(),
787 ASTTemplateArgumentListInfo::Create(Context, TAListI));
788 return ConceptRef;
789}
790
792 setRParenLoc(Loc);
793 setNameLoc(Loc);
794 setConceptReference(nullptr);
795 if (getTypePtr()->isConstrained()) {
798 }
799}
800
802 SourceLocation Loc) {
803 initializeElaboratedKeyword(*this, Loc);
805 Context, getTypePtr()->getTemplateName().getQualifier(), Loc));
807}
808
809namespace {
810
811 class GetContainedAutoTypeLocVisitor :
812 public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
813 public:
814 using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
815
816 TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
817 return TL;
818 }
819
820 // Only these types can contain the desired 'auto' type.
821
822 TypeLoc VisitAtomicTypeLoc(AtomicTypeLoc T) {
823 return Visit(T.getValueLoc());
824 }
825
826 TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
827 return Visit(T.getUnqualifiedLoc());
828 }
829
830 TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
831 return Visit(T.getPointeeLoc());
832 }
833
834 TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
835 return Visit(T.getPointeeLoc());
836 }
837
838 TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
839 return Visit(T.getPointeeLoc());
840 }
841
842 TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
843 return Visit(T.getPointeeLoc());
844 }
845
846 TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
847 return Visit(T.getElementLoc());
848 }
849
850 TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
851 return Visit(T.getReturnLoc());
852 }
853
854 TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
855 return Visit(T.getInnerLoc());
856 }
857
858 TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
859 return Visit(T.getModifiedLoc());
860 }
861
862 TypeLoc VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc T) {
863 return Visit(T.getWrappedLoc());
864 }
865
866 TypeLoc VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc T) {
867 return Visit(T.getWrappedLoc());
868 }
869
870 TypeLoc
871 VisitHLSLAttributedResourceTypeLoc(HLSLAttributedResourceTypeLoc T) {
872 return Visit(T.getWrappedLoc());
873 }
874
875 TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
876 return Visit(T.getInnerLoc());
877 }
878
879 TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
880 return Visit(T.getOriginalLoc());
881 }
882
883 TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
884 return Visit(T.getPatternLoc());
885 }
886 };
887
888} // namespace
889
891 TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
892 if (Res.isNull())
893 return AutoTypeLoc();
894 return Res.getAs<AutoTypeLoc>();
895}
896
898 if (const auto TSTL = getAsAdjusted<TemplateSpecializationTypeLoc>())
899 return TSTL.getTemplateKeywordLoc();
900 return SourceLocation();
901}
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:767
static const unsigned TypeLocMaxDataAlign
Definition TypeLoc.cpp:35
static NestedNameSpecifierLoc initializeQualifier(ASTContext &Context, NestedNameSpecifier Qualifier, SourceLocation Loc)
Definition TypeLoc.cpp:632
static void initializeElaboratedKeyword(TL T, SourceLocation Loc)
Definition TypeLoc.cpp:625
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:223
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:46
SourceLocation getLocation() const
Definition Attr.h:99
Type source information for an attributed type.
Definition TypeLoc.h:1008
const Attr * getAttr() const
The type attribute.
Definition TypeLoc.h:1031
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:582
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:791
void setConceptReference(ConceptReference *CR)
Definition TypeLoc.h:2436
bool isConstrained() const
Definition TypeLoc.h:2432
void setRParenLoc(SourceLocation Loc)
Definition TypeLoc.h:2430
const BTFTypeTagAttr * getAttr() const
The btf_type_tag attribute.
Definition TypeLoc.h:1063
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:599
TypeSpecifierType getWrittenTypeSpec() const
Definition TypeLoc.cpp:321
bool needsExtraLocalData() const
Definition TypeLoc.h:606
WrittenBuiltinSpecs & getWrittenBuiltinSpecs()
Definition TypeLoc.h:599
A reference to a concept and its template args, as it appears in the code.
Definition ASTConcept.h:130
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:1354
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:595
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition TypeLoc.h:2562
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:801
void setTemplateNameLoc(SourceLocation Loc)
Definition TypeLoc.h:2550
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:642
void setNameLoc(SourceLocation Loc)
Definition TypeLoc.h:2632
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition TypeLoc.h:2621
bool hasTrailingReturn() const
Whether this function prototype has a trailing return type.
Definition TypeBase.h:5841
const TypeClass * getTypePtr() const
Definition TypeLoc.h:526
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:1617
SourceLocation getStarLoc() const
Definition TypeLoc.h:1619
void setTypeArgsRAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1196
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:566
unsigned getNumTypeArgs() const
Definition TypeLoc.h:1200
unsigned getNumProtocols() const
Definition TypeLoc.h:1230
void setTypeArgsLAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1188
void setTypeArgTInfo(unsigned i, TypeSourceInfo *TInfo)
Definition TypeLoc.h:1209
void setProtocolLAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1218
void setProtocolRAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:1226
void setHasBaseTypeAsWritten(bool HasBaseType)
Definition TypeLoc.h:1258
void setProtocolLoc(unsigned i, SourceLocation Loc)
Definition TypeLoc.h:1239
unsigned getNumProtocols() const
Definition TypeLoc.h:932
void setProtocolLoc(unsigned i, SourceLocation Loc)
Definition TypeLoc.h:941
void setProtocolLAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:918
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:555
void setProtocolRAngleLoc(SourceLocation Loc)
Definition TypeLoc.h:928
void setNameLoc(SourceLocation Loc)
Definition TypeLoc.h:908
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:603
A (possibly-)qualified type.
Definition TypeBase.h:938
bool hasLocalQualifiers() const
Determine whether this particular QualType instance has any qualifiers, without looking through any t...
Definition TypeBase.h:1065
Represents a template name as written in source code.
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:343
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3952
TagDecl * getDecl() const
Definition TypeLoc.h:796
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:720
MutableArrayRef< TemplateArgumentLocInfo > getArgLocInfos()
Definition TypeLoc.h:1944
void set(SourceLocation ElaboratedKeywordLoc, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKeywordLoc, SourceLocation NameLoc, SourceLocation LAngleLoc, SourceLocation RAngleLoc)
Definition TypeLoc.cpp:650
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:694
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:443
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:454
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:475
TypeLoc IgnoreParens() const
Definition TypeLoc.h:1468
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:498
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:890
const void * Ty
Definition TypeLoc.h:63
SourceLocation getTemplateKeywordLoc() const
Get the SourceLocation of the template keyword (if any).
Definition TypeLoc.cpp:897
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:2766
SourceLocation getBeginLoc() const
Get the begin source location.
Definition TypeLoc.cpp:193
SourceRange getLocalSourceRange() const
Definition TypeLoc.cpp:312
Expr * getUnderlyingExpr() const
Definition TypeLoc.h:2275
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:607
QualType getUnmodifiedType() const
Definition TypeLoc.h:2288
A reasonable base class for TypeLocs that correspond to types that are written as a type-specifier.
Definition TypeLoc.h:540
void setNameLoc(SourceLocation Loc)
Definition TypeLoc.h:551
void setRParenLoc(SourceLocation Loc)
Definition TypeLoc.h:2381
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition TypeLoc.cpp:615
void setKWLoc(SourceLocation Loc)
Definition TypeLoc.h:2375
void setUnderlyingTInfo(TypeSourceInfo *TInfo)
Definition TypeLoc.h:2387
void setLParenLoc(SourceLocation Loc)
Definition TypeLoc.h:2378
Top level wrappers for InstallAPI frontend operations.
TypeSpecifierType
Specifies the kind of type.
Definition Specifiers.h:56
@ TST_char32
Definition Specifiers.h:63
@ TST_wchar
Definition Specifiers.h:60
@ TST_char16
Definition Specifiers.h:62
@ TST_char
Definition Specifiers.h:59
@ TST_unspecified
Definition Specifiers.h:57
@ TST_bool
Definition Specifiers.h:76
@ TST_void
Definition Specifiers.h:58
@ TST_char8
Definition Specifiers.h:61
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:6041
__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:2220