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,
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) ||
446 isa<TypeNullUnspecifiedAttr>(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::DependentTemplateSpecialization:
481 return castAs<DependentTemplateSpecializationTypeLoc>().getQualifierLoc();
482 case TypeLoc::DeducedTemplateSpecialization:
483 return castAs<DeducedTemplateSpecializationTypeLoc>().getQualifierLoc();
484 case TypeLoc::Enum:
485 case TypeLoc::Record:
486 case TypeLoc::InjectedClassName:
487 return castAs<TagTypeLoc>().getQualifierLoc();
488 case TypeLoc::Typedef:
489 return castAs<TypedefTypeLoc>().getQualifierLoc();
490 case TypeLoc::UnresolvedUsing:
491 return castAs<UnresolvedUsingTypeLoc>().getQualifierLoc();
492 case TypeLoc::Using:
493 return castAs<UsingTypeLoc>().getQualifierLoc();
494 default:
495 return NestedNameSpecifierLoc();
496 }
497}
498
500 switch (getTypeLocClass()) {
501 case TypeLoc::TemplateSpecialization: {
502 auto TL = castAs<TemplateSpecializationTypeLoc>();
504 if (!Loc.isValid())
505 Loc = TL.getTemplateNameLoc();
506 return Loc;
507 }
508 case TypeLoc::DependentTemplateSpecialization: {
509 auto TL = castAs<DependentTemplateSpecializationTypeLoc>();
511 if (!Loc.isValid())
512 Loc = TL.getTemplateNameLoc();
513 return Loc;
514 }
515 case TypeLoc::DeducedTemplateSpecialization: {
516 auto TL = castAs<DeducedTemplateSpecializationTypeLoc>();
518 if (!Loc.isValid())
519 Loc = TL.getTemplateNameLoc();
520 return Loc;
521 }
522 case TypeLoc::DependentName:
523 return castAs<DependentNameTypeLoc>().getNameLoc();
524 case TypeLoc::Enum:
525 case TypeLoc::Record:
526 case TypeLoc::InjectedClassName:
527 return castAs<TagTypeLoc>().getNameLoc();
528 case TypeLoc::Typedef:
529 return castAs<TypedefTypeLoc>().getNameLoc();
530 case TypeLoc::UnresolvedUsing:
531 return castAs<UnresolvedUsingTypeLoc>().getNameLoc();
532 case TypeLoc::Using:
533 return castAs<UsingTypeLoc>().getNameLoc();
534 default:
535 return getBeginLoc();
536 }
537}
538
540 // For elaborated types (e.g. `struct a::A`) we want the portion after the
541 // `struct` but including the namespace qualifier, `a::`.
542 switch (getTypeLocClass()) {
544 return castAs<QualifiedTypeLoc>()
545 .getUnqualifiedLoc()
546 .getNonElaboratedBeginLoc();
547 case TypeLoc::TemplateSpecialization: {
548 auto T = castAs<TemplateSpecializationTypeLoc>();
549 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
550 return QualifierLoc.getBeginLoc();
551 return T.getTemplateNameLoc();
552 }
553 case TypeLoc::DependentTemplateSpecialization: {
554 auto T = castAs<DependentTemplateSpecializationTypeLoc>();
555 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
556 return QualifierLoc.getBeginLoc();
557 return T.getTemplateNameLoc();
558 }
559 case TypeLoc::DeducedTemplateSpecialization: {
560 auto T = castAs<DeducedTemplateSpecializationTypeLoc>();
561 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
562 return QualifierLoc.getBeginLoc();
563 return T.getTemplateNameLoc();
564 }
565 case TypeLoc::DependentName: {
566 auto T = castAs<DependentNameTypeLoc>();
567 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
568 return QualifierLoc.getBeginLoc();
569 return T.getNameLoc();
570 }
571 case TypeLoc::Enum:
572 case TypeLoc::Record:
573 case TypeLoc::InjectedClassName: {
574 auto T = castAs<TagTypeLoc>();
575 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
576 return QualifierLoc.getBeginLoc();
577 return T.getNameLoc();
578 }
579 case TypeLoc::Typedef: {
580 auto T = castAs<TypedefTypeLoc>();
581 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
582 return QualifierLoc.getBeginLoc();
583 return T.getNameLoc();
584 }
585 case TypeLoc::UnresolvedUsing: {
586 auto T = castAs<UnresolvedUsingTypeLoc>();
587 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
588 return QualifierLoc.getBeginLoc();
589 return T.getNameLoc();
590 }
591 case TypeLoc::Using: {
592 auto T = castAs<UsingTypeLoc>();
593 if (NestedNameSpecifierLoc QualifierLoc = T.getQualifierLoc())
594 return QualifierLoc.getBeginLoc();
595 return T.getNameLoc();
596 }
597 default:
598 return getBeginLoc();
599 }
600}
601
605 if (!getNumProtocols()) return;
606
609 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
611}
612
618 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
621 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
622 }
625 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
627}
628
630 // Note that this does *not* include the range of the attribute
631 // enclosure, e.g.:
632 // __attribute__((foo(bar)))
633 // ^~~~~~~~~~~~~~~ ~~
634 // or
635 // [[foo(bar)]]
636 // ^~ ~~
637 // That enclosure doesn't necessarily belong to a single attribute
638 // anyway.
639 return getAttr() ? getAttr()->getRange() : SourceRange();
640}
641
644}
645
647 return getAttr() ? getAttr()->getRange() : SourceRange();
648}
649
653 ::initializeLocal(Context, Loc);
654 this->getLocalData()->UnmodifiedTInfo =
656}
657
660 setKWLoc(Loc);
663 this->setUnderlyingTInfo(
664 Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
665}
666
667template <class TL>
669 T.setElaboratedKeywordLoc(T.getTypePtr()->getKeyword() !=
671 ? Loc
672 : SourceLocation());
673}
674
676 NestedNameSpecifier Qualifier,
678 if (!Qualifier)
679 return NestedNameSpecifierLoc();
681 Builder.MakeTrivial(Context, Qualifier, Loc);
682 return Builder.getWithLocInContext(Context);
683}
684
689 initializeQualifier(Context, getTypePtr()->getQualifier(), Loc));
691}
692
693void
698 Context, getTypePtr()->getDependentTemplateName().getQualifier(), Loc));
704 Context, getTypePtr()->template_arguments(), getArgInfos(), Loc);
705}
706
708 NestedNameSpecifierLoc QualifierLoc,
709 SourceLocation TemplateKeywordLoc,
710 SourceLocation NameLoc,
711 SourceLocation LAngleLoc,
712 SourceLocation RAngleLoc) {
714
715 Data.ElaboratedKWLoc = ElaboratedKeywordLoc;
716 SourceLocation BeginLoc = ElaboratedKeywordLoc;
717
718 getLocalData()->QualifierData = QualifierLoc.getOpaqueData();
719
720 assert(QualifierLoc.getNestedNameSpecifier() ==
721 getTypePtr()->getTemplateName().getQualifier());
722 Data.QualifierData = QualifierLoc ? QualifierLoc.getOpaqueData() : nullptr;
723 if (QualifierLoc && !BeginLoc.isValid())
724 BeginLoc = QualifierLoc.getBeginLoc();
725
726 Data.TemplateKWLoc = TemplateKeywordLoc;
727 if (!BeginLoc.isValid())
728 BeginLoc = TemplateKeywordLoc;
729
730 Data.NameLoc = NameLoc;
731 if (!BeginLoc.isValid())
732 BeginLoc = NameLoc;
733
734 Data.LAngleLoc = LAngleLoc;
735 Data.SR = SourceRange(BeginLoc, RAngleLoc);
736}
737
739 NestedNameSpecifierLoc QualifierLoc,
740 SourceLocation TemplateKeywordLoc,
741 SourceLocation NameLoc,
742 const TemplateArgumentListInfo &TAL) {
743 set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
744 TAL.getLAngleLoc(), TAL.getRAngleLoc());
746 assert(TAL.size() == ArgInfos.size());
747 for (unsigned I = 0, N = TAL.size(); I != N; ++I)
748 ArgInfos[I] = TAL[I].getLocInfo();
749}
750
753
754 auto [Qualifier, HasTemplateKeyword] =
756
757 SourceLocation ElaboratedKeywordLoc =
759 ? Loc
760 : SourceLocation();
761
762 NestedNameSpecifierLoc QualifierLoc;
763 if (Qualifier) {
765 Builder.MakeTrivial(Context, Qualifier, Loc);
766 QualifierLoc = Builder.getWithLocInContext(Context);
767 }
768
770 set(ElaboratedKeywordLoc, QualifierLoc,
771 /*TemplateKeywordLoc=*/HasTemplateKeyword ? Loc : SourceLocation(),
772 /*NameLoc=*/Loc, /*LAngleLoc=*/Loc, /*RAngleLoc=*/Loc);
773 initializeArgLocs(Context, getTypePtr()->template_arguments(), getArgInfos(),
774 Loc);
775}
776
780 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
781 switch (Args[i].getKind()) {
783 llvm_unreachable("Impossible TemplateArgument");
784
789 ArgInfos[i] = TemplateArgumentLocInfo();
790 break;
791
793 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
794 break;
795
797 ArgInfos[i] = TemplateArgumentLocInfo(
798 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
799 Loc));
800 break;
801
805 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
806 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
807 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
808 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
809 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
810
811 ArgInfos[i] = TemplateArgumentLocInfo(
812 Context, Loc, Builder.getWithLocInContext(Context), Loc,
813 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
814 : Loc);
815 break;
816 }
817
819 ArgInfos[i] = TemplateArgumentLocInfo();
820 break;
821 }
822 }
823}
824
825// Builds a ConceptReference where all locations point at the same token,
826// for use in trivial TypeSourceInfo for constrained AutoType
829 const AutoType *AT) {
833 unsigned size = AT->getTypeConstraintArguments().size();
836 Context, AT->getTypeConstraintArguments(), TALI.data(), Loc);
838 for (unsigned i = 0; i < size; ++i) {
839 TAListI.addArgument(
841 TALI[i])); // TemplateArgumentLocInfo()
842 }
843
844 auto *ConceptRef = ConceptReference::Create(
845 Context, NestedNameSpecifierLoc{}, Loc, DNI, nullptr,
847 ASTTemplateArgumentListInfo::Create(Context, TAListI));
848 return ConceptRef;
849}
850
854 setConceptReference(nullptr);
855 if (getTypePtr()->isConstrained()) {
858 }
859}
860
865 Context, getTypePtr()->getTemplateName().getQualifier(), Loc));
867}
868
869namespace {
870
871 class GetContainedAutoTypeLocVisitor :
872 public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
873 public:
874 using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
875
876 TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
877 return TL;
878 }
879
880 // Only these types can contain the desired 'auto' type.
881
882 TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
883 return Visit(T.getUnqualifiedLoc());
884 }
885
886 TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
887 return Visit(T.getPointeeLoc());
888 }
889
890 TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
891 return Visit(T.getPointeeLoc());
892 }
893
894 TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
895 return Visit(T.getPointeeLoc());
896 }
897
898 TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
899 return Visit(T.getPointeeLoc());
900 }
901
902 TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
903 return Visit(T.getElementLoc());
904 }
905
906 TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
907 return Visit(T.getReturnLoc());
908 }
909
910 TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
911 return Visit(T.getInnerLoc());
912 }
913
914 TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
915 return Visit(T.getModifiedLoc());
916 }
917
918 TypeLoc VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc T) {
919 return Visit(T.getWrappedLoc());
920 }
921
922 TypeLoc
923 VisitHLSLAttributedResourceTypeLoc(HLSLAttributedResourceTypeLoc T) {
924 return Visit(T.getWrappedLoc());
925 }
926
927 TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
928 return Visit(T.getInnerLoc());
929 }
930
931 TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
932 return Visit(T.getOriginalLoc());
933 }
934
935 TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
936 return Visit(T.getPatternLoc());
937 }
938 };
939
940} // namespace
941
943 TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
944 if (Res.isNull())
945 return AutoTypeLoc();
946 return Res.getAs<AutoTypeLoc>();
947}
948
950 if (const auto TSTL = getAsAdjusted<TemplateSpecializationTypeLoc>())
951 return TSTL.getTemplateKeywordLoc();
952 if (const auto DTSTL =
953 getAsAdjusted<DependentTemplateSpecializationTypeLoc>())
954 return DTSTL.getTemplateKeywordLoc();
955 return SourceLocation();
956}
This file provides AST data structures related to concepts.
Defines the clang::ASTContext interface.
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:1192
Defines the C++ template declaration subclasses.
SourceLocation Loc
Definition: SemaObjC.cpp:754
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:827
static const unsigned TypeLocMaxDataAlign
Definition: TypeLoc.cpp:35
static NestedNameSpecifierLoc initializeQualifier(ASTContext &Context, NestedNameSpecifier Qualifier, SourceLocation Loc)
Definition: TypeLoc.cpp:675
static void initializeElaboratedKeyword(TL T, SourceLocation Loc)
Definition: TypeLoc.cpp:668
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,...
Wrapper for source info for arrays.
Definition: TypeLoc.h:1757
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:629
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition: TypeLoc.cpp:851
void setConceptReference(ConceptReference *CR)
Definition: TypeLoc.h:2385
bool isConstrained() const
Definition: TypeLoc.h:2381
void setRParenLoc(SourceLocation Loc)
Definition: TypeLoc.h:2379
Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained by a type-constraint.
Definition: TypeBase.h:7180
ArrayRef< TemplateArgument > getTypeConstraintArguments() const
Definition: TypeBase.h:7190
TemplateDecl * getTypeConstraintConcept() const
Definition: TypeBase.h:7195
Type source information for an btf_tag attributed type.
Definition: TypeLoc.h:1067
const BTFTypeTagAttr * getAttr() const
The btf_type_tag attribute.
Definition: TypeLoc.h:1072
SourceRange getLocalSourceRange() const
Definition: TypeLoc.cpp:646
Wrapper for source info for block pointers.
Definition: TypeLoc.h:1506
TypeSpecifierType getWrittenTypeSpec() const
Definition: TypeLoc.cpp:322
bool needsExtraLocalData() const
Definition: TypeLoc.h:611
WrittenBuiltinSpecs & getWrittenBuiltinSpecs()
Definition: TypeLoc.h:604
Kind getKind() const
Definition: TypeBase.h:3230
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)
Definition: ASTConcept.cpp:87
LocalData * getLocalData() const
Definition: TypeLoc.h:451
Expr * getCountExpr() const
Definition: TypeLoc.h:1334
SourceRange getLocalSourceRange() const
Definition: TypeLoc.cpp:642
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition: TypeLoc.h:2511
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition: TypeLoc.cpp:861
void setTemplateNameLoc(SourceLocation Loc)
Definition: TypeLoc.h:2499
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition: TypeLoc.cpp:685
void setNameLoc(SourceLocation Loc)
Definition: TypeLoc.h:2581
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition: TypeLoc.h:2570
void setQualifierLoc(NestedNameSpecifierLoc QualifierLoc)
Definition: TypeLoc.h:2631
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition: TypeLoc.cpp:694
void setTemplateKeywordLoc(SourceLocation Loc)
Definition: TypeLoc.h:2651
void setRAngleLoc(SourceLocation Loc)
Definition: TypeLoc.h:2675
void setLAngleLoc(SourceLocation Loc)
Definition: TypeLoc.h:2667
void setTemplateNameLoc(SourceLocation Loc)
Definition: TypeLoc.h:2659
bool hasTrailingReturn() const
Whether this function prototype has a trailing return type.
Definition: TypeBase.h:5702
Wrapper for source info for functions.
Definition: TypeLoc.h:1624
Type source information for HLSL attributed resource type.
Definition: TypeLoc.h:1094
const TypeClass * getTypePtr() const
Definition: TypeLoc.h:531
ElaboratedTypeKeyword getKeyword() const
Definition: TypeBase.h:5959
Wrapper for source info for member pointers.
Definition: TypeLoc.h:1524
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:339
Class that aids in the construction of nested-name-specifiers along with source-location information ...
A C++ nested-name-specifier augmented with source location information.
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:613
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:602
void setProtocolRAngleLoc(SourceLocation Loc)
Definition: TypeLoc.h:937
void setNameLoc(SourceLocation Loc)
Definition: TypeLoc.h:917
Wrapper for source info for pointers.
Definition: TypeLoc.h:1493
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.
Definition: TemplateName.h:504
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:3805
TagDecl * getOriginalDecl() const
Definition: TypeLoc.h:801
bool isDefinition() const
True if the tag was defined in this type specifier.
Definition: TypeLoc.cpp:305
bool isTagOwned() const
Does the TagType own this declaration of the Tag?
Definition: TypeBase.h:6446
A convenient class for passing around template argument information.
Definition: TemplateBase.h:634
SourceLocation getRAngleLoc() const
Definition: TemplateBase.h:650
void addArgument(const TemplateArgumentLoc &Loc)
Definition: TemplateBase.h:669
SourceLocation getLAngleLoc() const
Definition: TemplateBase.h:649
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:528
@ Declaration
The template argument is a declaration that was provided for a pointer, reference,...
Definition: TemplateBase.h:74
@ Template
The template argument is a template name that was provided for a template template parameter.
Definition: TemplateBase.h:93
@ StructuralValue
The template argument is a non-type template argument that can't be represented by the special-case D...
Definition: TemplateBase.h:89
@ Pack
The template argument is actually a parameter pack.
Definition: TemplateBase.h:107
@ TemplateExpansion
The template argument is a pack expansion of a template name that was provided for a template templat...
Definition: TemplateBase.h:97
@ NullPtr
The template argument is a null pointer or null pointer to member that was provided for a non-type te...
Definition: TemplateBase.h:78
@ Type
The template argument is a type.
Definition: TemplateBase.h:70
@ Null
Represents an empty template argument, e.g., one that has not been deduced.
Definition: TemplateBase.h:67
@ Integral
The template argument is an integral value stored in an llvm::APSInt that was provided for an integra...
Definition: TemplateBase.h:82
@ Expression
The template argument is an expression, and we've not resolved it to one of the other forms yet,...
Definition: TemplateBase.h:103
Represents a C++ template name within the type system.
Definition: TemplateName.h:222
std::tuple< NestedNameSpecifier, bool > getQualifierAndTemplateKeyword() const
static void initializeArgLocs(ASTContext &Context, ArrayRef< TemplateArgument > Args, TemplateArgumentLocInfo *ArgInfos, SourceLocation Loc)
Definition: TypeLoc.cpp:777
MutableArrayRef< TemplateArgumentLocInfo > getArgLocInfos()
Definition: TypeLoc.h:1893
void set(SourceLocation ElaboratedKeywordLoc, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKeywordLoc, SourceLocation NameLoc, SourceLocation LAngleLoc, SourceLocation RAngleLoc)
Definition: TypeLoc.cpp:707
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition: TypeLoc.cpp:751
TemplateName getTemplateName() const
Retrieve the name of the template that we are specializing.
Definition: TypeBase.h:7355
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:539
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:942
const void * Ty
Definition: TypeLoc.h:63
SourceLocation getTemplateKeywordLoc() const
Get the SourceLocation of the template keyword (if any).
Definition: TypeLoc.cpp:949
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
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:499
SourceRange getLocalSourceRange() const
Definition: TypeLoc.cpp:313
Expr * getUnderlyingExpr() const
Definition: TypeLoc.h:2224
void initializeLocal(ASTContext &Context, SourceLocation Loc)
Definition: TypeLoc.cpp:650
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:658
void setKWLoc(SourceLocation Loc)
Definition: TypeLoc.h:2324
void setUnderlyingTInfo(TypeSourceInfo *TInfo)
Definition: TypeLoc.h:2336
void setLParenLoc(SourceLocation Loc)
Definition: TypeLoc.h:2327
const internal::VariadicAllOfMatcher< Attr > attr
Matches attributes.
The JSON file list parser is used to communicate input to InstallAPI.
TypeSpecifierType
Specifies the kind of type.
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
@ Template
We are parsing a template declaration.
const FunctionProtoType * T
@ None
No keyword precedes the qualified type name.
__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.
Definition: TemplateBase.h:480