1 //===- TypeLoc.cpp - Type Source Info Wrapper -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the TypeLoc subclasses implementations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/TypeLoc.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/NestedNameSpecifier.h"
19 #include "clang/AST/TemplateBase.h"
20 #include "clang/AST/TemplateName.h"
21 #include "clang/AST/TypeLocVisitor.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "clang/Basic/Specifiers.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include <algorithm>
27 #include <cassert>
28 #include <cstdint>
29 #include <cstring>
30 
31 using namespace clang;
32 
33 static const unsigned TypeLocMaxDataAlign = alignof(void *);
34 
35 //===----------------------------------------------------------------------===//
36 // TypeLoc Implementation
37 //===----------------------------------------------------------------------===//
38 
39 namespace {
40 
41 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
42 public:
43 #define ABSTRACT_TYPELOC(CLASS, PARENT)
44 #define TYPELOC(CLASS, PARENT) \
45   SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
46     return TyLoc.getLocalSourceRange(); \
47   }
48 #include "clang/AST/TypeLocNodes.def"
49 };
50 
51 } // namespace
52 
53 SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
54   if (TL.isNull()) return SourceRange();
55   return TypeLocRanger().Visit(TL);
56 }
57 
58 namespace {
59 
60 class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
61 public:
62 #define ABSTRACT_TYPELOC(CLASS, PARENT)
63 #define TYPELOC(CLASS, PARENT) \
64   unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
65     return TyLoc.getLocalDataAlignment(); \
66   }
67 #include "clang/AST/TypeLocNodes.def"
68 };
69 
70 } // namespace
71 
72 /// Returns the alignment of the type source info data block.
73 unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
74   if (Ty.isNull()) return 1;
75   return TypeAligner().Visit(TypeLoc(Ty, nullptr));
76 }
77 
78 namespace {
79 
80 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
81 public:
82 #define ABSTRACT_TYPELOC(CLASS, PARENT)
83 #define TYPELOC(CLASS, PARENT) \
84   unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
85     return TyLoc.getLocalDataSize(); \
86   }
87 #include "clang/AST/TypeLocNodes.def"
88 };
89 
90 } // namespace
91 
92 /// Returns the size of the type source info data block.
93 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
94   unsigned Total = 0;
95   TypeLoc TyLoc(Ty, nullptr);
96   unsigned MaxAlign = 1;
97   while (!TyLoc.isNull()) {
98     unsigned Align = getLocalAlignmentForType(TyLoc.getType());
99     MaxAlign = std::max(Align, MaxAlign);
100     Total = llvm::alignTo(Total, Align);
101     Total += TypeSizer().Visit(TyLoc);
102     TyLoc = TyLoc.getNextTypeLoc();
103   }
104   Total = llvm::alignTo(Total, MaxAlign);
105   return Total;
106 }
107 
108 namespace {
109 
110 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
111 public:
112 #define ABSTRACT_TYPELOC(CLASS, PARENT)
113 #define TYPELOC(CLASS, PARENT) \
114   TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
115     return TyLoc.getNextTypeLoc(); \
116   }
117 #include "clang/AST/TypeLocNodes.def"
118 };
119 
120 } // namespace
121 
122 /// Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
123 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
124 TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
125   return NextLoc().Visit(TL);
126 }
127 
128 /// Initializes a type location, and all of its children
129 /// recursively, as if the entire tree had been written in the
130 /// given location.
131 void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
132                              SourceLocation Loc) {
133   while (true) {
134     switch (TL.getTypeLocClass()) {
135 #define ABSTRACT_TYPELOC(CLASS, PARENT)
136 #define TYPELOC(CLASS, PARENT)        \
137     case CLASS: {                     \
138       CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
139       TLCasted.initializeLocal(Context, Loc);  \
140       TL = TLCasted.getNextTypeLoc(); \
141       if (!TL) return;                \
142       continue;                       \
143     }
144 #include "clang/AST/TypeLocNodes.def"
145     }
146   }
147 }
148 
149 namespace {
150 
151 class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
152   TypeLoc Source;
153 
154 public:
155   TypeLocCopier(TypeLoc source) : Source(source) {}
156 
157 #define ABSTRACT_TYPELOC(CLASS, PARENT)
158 #define TYPELOC(CLASS, PARENT)                          \
159   void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) {   \
160     dest.copyLocal(Source.castAs<CLASS##TypeLoc>());  \
161   }
162 #include "clang/AST/TypeLocNodes.def"
163 };
164 
165 } // namespace
166 
167 void TypeLoc::copy(TypeLoc other) {
168   assert(getFullDataSize() == other.getFullDataSize());
169 
170   // If both data pointers are aligned to the maximum alignment, we
171   // can memcpy because getFullDataSize() accurately reflects the
172   // layout of the data.
173   if (reinterpret_cast<uintptr_t>(Data) ==
174           llvm::alignTo(reinterpret_cast<uintptr_t>(Data),
175                         TypeLocMaxDataAlign) &&
176       reinterpret_cast<uintptr_t>(other.Data) ==
177           llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
178                         TypeLocMaxDataAlign)) {
179     memcpy(Data, other.Data, getFullDataSize());
180     return;
181   }
182 
183   // Copy each of the pieces.
184   TypeLoc TL(getType(), Data);
185   do {
186     TypeLocCopier(other).Visit(TL);
187     other = other.getNextTypeLoc();
188   } while ((TL = TL.getNextTypeLoc()));
189 }
190 
191 SourceLocation TypeLoc::getBeginLoc() const {
192   TypeLoc Cur = *this;
193   TypeLoc LeftMost = Cur;
194   while (true) {
195     switch (Cur.getTypeLocClass()) {
196     case Elaborated:
197       LeftMost = Cur;
198       break;
199     case FunctionProto:
200       if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
201               ->hasTrailingReturn()) {
202         LeftMost = Cur;
203         break;
204       }
205       LLVM_FALLTHROUGH;
206     case FunctionNoProto:
207     case ConstantArray:
208     case DependentSizedArray:
209     case IncompleteArray:
210     case VariableArray:
211       // FIXME: Currently QualifiedTypeLoc does not have a source range
212     case Qualified:
213       Cur = Cur.getNextTypeLoc();
214       continue;
215     default:
216       if (Cur.getLocalSourceRange().getBegin().isValid())
217         LeftMost = Cur;
218       Cur = Cur.getNextTypeLoc();
219       if (Cur.isNull())
220         break;
221       continue;
222     } // switch
223     break;
224   } // while
225   return LeftMost.getLocalSourceRange().getBegin();
226 }
227 
228 SourceLocation TypeLoc::getEndLoc() const {
229   TypeLoc Cur = *this;
230   TypeLoc Last;
231   while (true) {
232     switch (Cur.getTypeLocClass()) {
233     default:
234       if (!Last)
235         Last = Cur;
236       return Last.getLocalSourceRange().getEnd();
237     case Paren:
238     case ConstantArray:
239     case DependentSizedArray:
240     case IncompleteArray:
241     case VariableArray:
242     case FunctionNoProto:
243       // The innermost type with suffix syntax always determines the end of the
244       // type.
245       Last = Cur;
246       break;
247     case FunctionProto:
248       if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
249         Last = TypeLoc();
250       else
251         Last = Cur;
252       break;
253     case ObjCObjectPointer:
254       // `id` and `id<...>` have no star location.
255       if (Cur.castAs<ObjCObjectPointerTypeLoc>().getStarLoc().isInvalid())
256         break;
257       LLVM_FALLTHROUGH;
258     case Pointer:
259     case BlockPointer:
260     case MemberPointer:
261     case LValueReference:
262     case RValueReference:
263     case PackExpansion:
264       // Types with prefix syntax only determine the end of the type if there
265       // is no suffix type.
266       if (!Last)
267         Last = Cur;
268       break;
269     case Qualified:
270     case Elaborated:
271       break;
272     }
273     Cur = Cur.getNextTypeLoc();
274   }
275 }
276 
277 namespace {
278 
279 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
280   // Overload resolution does the real work for us.
281   static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
282   static bool isTypeSpec(TypeLoc _) { return false; }
283 
284 #define ABSTRACT_TYPELOC(CLASS, PARENT)
285 #define TYPELOC(CLASS, PARENT) \
286   bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
287     return isTypeSpec(TyLoc); \
288   }
289 #include "clang/AST/TypeLocNodes.def"
290 };
291 
292 } // namespace
293 
294 /// Determines if the given type loc corresponds to a
295 /// TypeSpecTypeLoc.  Since there is not actually a TypeSpecType in
296 /// the type hierarchy, this is made somewhat complicated.
297 ///
298 /// There are a lot of types that currently use TypeSpecTypeLoc
299 /// because it's a convenient base class.  Ideally we would not accept
300 /// those here, but ideally we would have better implementations for
301 /// them.
302 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
303   if (TL.getType().hasLocalQualifiers()) return false;
304   return TSTChecker().Visit(TL);
305 }
306 
307 bool TagTypeLoc::isDefinition() const {
308   TagDecl *D = getDecl();
309   return D->isCompleteDefinition() &&
310          (D->getIdentifier() == nullptr || D->getLocation() == getNameLoc());
311 }
312 
313 // Reimplemented to account for GNU/C++ extension
314 //     typeof unary-expression
315 // where there are no parentheses.
316 SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
317   if (getRParenLoc().isValid())
318     return SourceRange(getTypeofLoc(), getRParenLoc());
319   else
320     return SourceRange(getTypeofLoc(),
321                        getUnderlyingExpr()->getSourceRange().getEnd());
322 }
323 
324 
325 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
326   if (needsExtraLocalData())
327     return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
328   switch (getTypePtr()->getKind()) {
329   case BuiltinType::Void:
330     return TST_void;
331   case BuiltinType::Bool:
332     return TST_bool;
333   case BuiltinType::Char_U:
334   case BuiltinType::Char_S:
335     return TST_char;
336   case BuiltinType::Char8:
337     return TST_char8;
338   case BuiltinType::Char16:
339     return TST_char16;
340   case BuiltinType::Char32:
341     return TST_char32;
342   case BuiltinType::WChar_S:
343   case BuiltinType::WChar_U:
344     return TST_wchar;
345   case BuiltinType::UChar:
346   case BuiltinType::UShort:
347   case BuiltinType::UInt:
348   case BuiltinType::ULong:
349   case BuiltinType::ULongLong:
350   case BuiltinType::UInt128:
351   case BuiltinType::SChar:
352   case BuiltinType::Short:
353   case BuiltinType::Int:
354   case BuiltinType::Long:
355   case BuiltinType::LongLong:
356   case BuiltinType::Int128:
357   case BuiltinType::Half:
358   case BuiltinType::Float:
359   case BuiltinType::Double:
360   case BuiltinType::LongDouble:
361   case BuiltinType::Float16:
362   case BuiltinType::Float128:
363   case BuiltinType::Ibm128:
364   case BuiltinType::ShortAccum:
365   case BuiltinType::Accum:
366   case BuiltinType::LongAccum:
367   case BuiltinType::UShortAccum:
368   case BuiltinType::UAccum:
369   case BuiltinType::ULongAccum:
370   case BuiltinType::ShortFract:
371   case BuiltinType::Fract:
372   case BuiltinType::LongFract:
373   case BuiltinType::UShortFract:
374   case BuiltinType::UFract:
375   case BuiltinType::ULongFract:
376   case BuiltinType::SatShortAccum:
377   case BuiltinType::SatAccum:
378   case BuiltinType::SatLongAccum:
379   case BuiltinType::SatUShortAccum:
380   case BuiltinType::SatUAccum:
381   case BuiltinType::SatULongAccum:
382   case BuiltinType::SatShortFract:
383   case BuiltinType::SatFract:
384   case BuiltinType::SatLongFract:
385   case BuiltinType::SatUShortFract:
386   case BuiltinType::SatUFract:
387   case BuiltinType::SatULongFract:
388   case BuiltinType::BFloat16:
389     llvm_unreachable("Builtin type needs extra local data!");
390     // Fall through, if the impossible happens.
391 
392   case BuiltinType::NullPtr:
393   case BuiltinType::Overload:
394   case BuiltinType::Dependent:
395   case BuiltinType::BoundMember:
396   case BuiltinType::UnknownAny:
397   case BuiltinType::ARCUnbridgedCast:
398   case BuiltinType::PseudoObject:
399   case BuiltinType::ObjCId:
400   case BuiltinType::ObjCClass:
401   case BuiltinType::ObjCSel:
402 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
403   case BuiltinType::Id:
404 #include "clang/Basic/OpenCLImageTypes.def"
405 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
406   case BuiltinType::Id:
407 #include "clang/Basic/OpenCLExtensionTypes.def"
408   case BuiltinType::OCLSampler:
409   case BuiltinType::OCLEvent:
410   case BuiltinType::OCLClkEvent:
411   case BuiltinType::OCLQueue:
412   case BuiltinType::OCLReserveID:
413 #define SVE_TYPE(Name, Id, SingletonId) \
414   case BuiltinType::Id:
415 #include "clang/Basic/AArch64SVEACLETypes.def"
416 #define PPC_VECTOR_TYPE(Name, Id, Size) \
417   case BuiltinType::Id:
418 #include "clang/Basic/PPCTypes.def"
419 #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
420 #include "clang/Basic/RISCVVTypes.def"
421   case BuiltinType::BuiltinFn:
422   case BuiltinType::IncompleteMatrixIdx:
423   case BuiltinType::OMPArraySection:
424   case BuiltinType::OMPArrayShaping:
425   case BuiltinType::OMPIterator:
426     return TST_unspecified;
427   }
428 
429   llvm_unreachable("Invalid BuiltinType Kind!");
430 }
431 
432 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
433   while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
434     TL = PTL.getInnerLoc();
435   return TL;
436 }
437 
438 SourceLocation TypeLoc::findNullabilityLoc() const {
439   if (auto ATL = getAs<AttributedTypeLoc>()) {
440     const Attr *A = ATL.getAttr();
441     if (A && (isa<TypeNullableAttr>(A) || isa<TypeNonNullAttr>(A) ||
442               isa<TypeNullUnspecifiedAttr>(A)))
443       return A->getLocation();
444   }
445 
446   return {};
447 }
448 
449 TypeLoc TypeLoc::findExplicitQualifierLoc() const {
450   // Qualified types.
451   if (auto qual = getAs<QualifiedTypeLoc>())
452     return qual;
453 
454   TypeLoc loc = IgnoreParens();
455 
456   // Attributed types.
457   if (auto attr = loc.getAs<AttributedTypeLoc>()) {
458     if (attr.isQualifier()) return attr;
459     return attr.getModifiedLoc().findExplicitQualifierLoc();
460   }
461 
462   // C11 _Atomic types.
463   if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
464     return atomic;
465   }
466 
467   return {};
468 }
469 
470 void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
471                                            SourceLocation Loc) {
472   setNameLoc(Loc);
473   if (!getNumProtocols()) return;
474 
475   setProtocolLAngleLoc(Loc);
476   setProtocolRAngleLoc(Loc);
477   for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
478     setProtocolLoc(i, Loc);
479 }
480 
481 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
482                                         SourceLocation Loc) {
483   setHasBaseTypeAsWritten(true);
484   setTypeArgsLAngleLoc(Loc);
485   setTypeArgsRAngleLoc(Loc);
486   for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
487     setTypeArgTInfo(i,
488                    Context.getTrivialTypeSourceInfo(
489                      getTypePtr()->getTypeArgsAsWritten()[i], Loc));
490   }
491   setProtocolLAngleLoc(Loc);
492   setProtocolRAngleLoc(Loc);
493   for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
494     setProtocolLoc(i, Loc);
495 }
496 
497 SourceRange AttributedTypeLoc::getLocalSourceRange() const {
498   // Note that this does *not* include the range of the attribute
499   // enclosure, e.g.:
500   //    __attribute__((foo(bar)))
501   //    ^~~~~~~~~~~~~~~        ~~
502   // or
503   //    [[foo(bar)]]
504   //    ^~        ~~
505   // That enclosure doesn't necessarily belong to a single attribute
506   // anyway.
507   return getAttr() ? getAttr()->getRange() : SourceRange();
508 }
509 
510 SourceRange BTFTagAttributedTypeLoc::getLocalSourceRange() const {
511   return getAttr() ? getAttr()->getRange() : SourceRange();
512 }
513 
514 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
515                                        SourceLocation Loc) {
516   TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
517       ::initializeLocal(Context, Loc);
518   this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
519       getUnderlyingType(), Loc);
520 }
521 
522 void UnaryTransformTypeLoc::initializeLocal(ASTContext &Context,
523                                        SourceLocation Loc) {
524     setKWLoc(Loc);
525     setRParenLoc(Loc);
526     setLParenLoc(Loc);
527     this->setUnderlyingTInfo(
528         Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
529 }
530 
531 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
532                                         SourceLocation Loc) {
533   setElaboratedKeywordLoc(Loc);
534   NestedNameSpecifierLocBuilder Builder;
535   Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
536   setQualifierLoc(Builder.getWithLocInContext(Context));
537 }
538 
539 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
540                                            SourceLocation Loc) {
541   setElaboratedKeywordLoc(Loc);
542   NestedNameSpecifierLocBuilder Builder;
543   Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
544   setQualifierLoc(Builder.getWithLocInContext(Context));
545   setNameLoc(Loc);
546 }
547 
548 void
549 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
550                                                         SourceLocation Loc) {
551   setElaboratedKeywordLoc(Loc);
552   if (getTypePtr()->getQualifier()) {
553     NestedNameSpecifierLocBuilder Builder;
554     Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
555     setQualifierLoc(Builder.getWithLocInContext(Context));
556   } else {
557     setQualifierLoc(NestedNameSpecifierLoc());
558   }
559   setTemplateKeywordLoc(Loc);
560   setTemplateNameLoc(Loc);
561   setLAngleLoc(Loc);
562   setRAngleLoc(Loc);
563   TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
564                                                    getTypePtr()->getArgs(),
565                                                    getArgInfos(), Loc);
566 }
567 
568 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
569                                                       unsigned NumArgs,
570                                                   const TemplateArgument *Args,
571                                               TemplateArgumentLocInfo *ArgInfos,
572                                                       SourceLocation Loc) {
573   for (unsigned i = 0, e = NumArgs; i != e; ++i) {
574     switch (Args[i].getKind()) {
575     case TemplateArgument::Null:
576       llvm_unreachable("Impossible TemplateArgument");
577 
578     case TemplateArgument::Integral:
579     case TemplateArgument::Declaration:
580     case TemplateArgument::NullPtr:
581       ArgInfos[i] = TemplateArgumentLocInfo();
582       break;
583 
584     case TemplateArgument::Expression:
585       ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
586       break;
587 
588     case TemplateArgument::Type:
589       ArgInfos[i] = TemplateArgumentLocInfo(
590                           Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
591                                                            Loc));
592       break;
593 
594     case TemplateArgument::Template:
595     case TemplateArgument::TemplateExpansion: {
596       NestedNameSpecifierLocBuilder Builder;
597       TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
598       if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
599         Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
600       else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
601         Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
602 
603       ArgInfos[i] = TemplateArgumentLocInfo(
604           Context, Builder.getWithLocInContext(Context), Loc,
605           Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
606                                                           : Loc);
607       break;
608     }
609 
610     case TemplateArgument::Pack:
611       ArgInfos[i] = TemplateArgumentLocInfo();
612       break;
613     }
614   }
615 }
616 
617 DeclarationNameInfo AutoTypeLoc::getConceptNameInfo() const {
618   return DeclarationNameInfo(getNamedConcept()->getDeclName(),
619                              getLocalData()->ConceptNameLoc);
620 }
621 
622 void AutoTypeLoc::initializeLocal(ASTContext &Context, SourceLocation Loc) {
623   setNestedNameSpecifierLoc(NestedNameSpecifierLoc());
624   setTemplateKWLoc(Loc);
625   setConceptNameLoc(Loc);
626   setFoundDecl(nullptr);
627   setRAngleLoc(Loc);
628   setLAngleLoc(Loc);
629   setRParenLoc(Loc);
630   TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
631                                                    getTypePtr()->getArgs(),
632                                                    getArgInfos(), Loc);
633   setNameLoc(Loc);
634 }
635 
636 
637 namespace {
638 
639   class GetContainedAutoTypeLocVisitor :
640     public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
641   public:
642     using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
643 
644     TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
645       return TL;
646     }
647 
648     // Only these types can contain the desired 'auto' type.
649 
650     TypeLoc VisitElaboratedTypeLoc(ElaboratedTypeLoc T) {
651       return Visit(T.getNamedTypeLoc());
652     }
653 
654     TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
655       return Visit(T.getUnqualifiedLoc());
656     }
657 
658     TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
659       return Visit(T.getPointeeLoc());
660     }
661 
662     TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
663       return Visit(T.getPointeeLoc());
664     }
665 
666     TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
667       return Visit(T.getPointeeLoc());
668     }
669 
670     TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
671       return Visit(T.getPointeeLoc());
672     }
673 
674     TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
675       return Visit(T.getElementLoc());
676     }
677 
678     TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
679       return Visit(T.getReturnLoc());
680     }
681 
682     TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
683       return Visit(T.getInnerLoc());
684     }
685 
686     TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
687       return Visit(T.getModifiedLoc());
688     }
689 
690     TypeLoc VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc T) {
691       return Visit(T.getWrappedLoc());
692     }
693 
694     TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
695       return Visit(T.getInnerLoc());
696     }
697 
698     TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
699       return Visit(T.getOriginalLoc());
700     }
701 
702     TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
703       return Visit(T.getPatternLoc());
704     }
705   };
706 
707 } // namespace
708 
709 AutoTypeLoc TypeLoc::getContainedAutoTypeLoc() const {
710   TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
711   if (Res.isNull())
712     return AutoTypeLoc();
713   return Res.getAs<AutoTypeLoc>();
714 }
715