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       Last = Cur;
244       break;
245     case FunctionProto:
246       if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
247         Last = TypeLoc();
248       else
249         Last = Cur;
250       break;
251     case Pointer:
252     case BlockPointer:
253     case MemberPointer:
254     case LValueReference:
255     case RValueReference:
256     case PackExpansion:
257       if (!Last)
258         Last = Cur;
259       break;
260     case ObjCObjectPointer:
261     case Qualified:
262     case Elaborated:
263       break;
264     }
265     Cur = Cur.getNextTypeLoc();
266   }
267 }
268 
269 namespace {
270 
271 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
272   // Overload resolution does the real work for us.
273   static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
274   static bool isTypeSpec(TypeLoc _) { return false; }
275 
276 #define ABSTRACT_TYPELOC(CLASS, PARENT)
277 #define TYPELOC(CLASS, PARENT) \
278   bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
279     return isTypeSpec(TyLoc); \
280   }
281 #include "clang/AST/TypeLocNodes.def"
282 };
283 
284 } // namespace
285 
286 /// Determines if the given type loc corresponds to a
287 /// TypeSpecTypeLoc.  Since there is not actually a TypeSpecType in
288 /// the type hierarchy, this is made somewhat complicated.
289 ///
290 /// There are a lot of types that currently use TypeSpecTypeLoc
291 /// because it's a convenient base class.  Ideally we would not accept
292 /// those here, but ideally we would have better implementations for
293 /// them.
294 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
295   if (TL.getType().hasLocalQualifiers()) return false;
296   return TSTChecker().Visit(TL);
297 }
298 
299 bool TagTypeLoc::isDefinition() const {
300   TagDecl *D = getDecl();
301   return D->isCompleteDefinition() &&
302          (D->getIdentifier() == nullptr || D->getLocation() == getNameLoc());
303 }
304 
305 // Reimplemented to account for GNU/C++ extension
306 //     typeof unary-expression
307 // where there are no parentheses.
308 SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
309   if (getRParenLoc().isValid())
310     return SourceRange(getTypeofLoc(), getRParenLoc());
311   else
312     return SourceRange(getTypeofLoc(),
313                        getUnderlyingExpr()->getSourceRange().getEnd());
314 }
315 
316 
317 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
318   if (needsExtraLocalData())
319     return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
320   switch (getTypePtr()->getKind()) {
321   case BuiltinType::Void:
322     return TST_void;
323   case BuiltinType::Bool:
324     return TST_bool;
325   case BuiltinType::Char_U:
326   case BuiltinType::Char_S:
327     return TST_char;
328   case BuiltinType::Char8:
329     return TST_char8;
330   case BuiltinType::Char16:
331     return TST_char16;
332   case BuiltinType::Char32:
333     return TST_char32;
334   case BuiltinType::WChar_S:
335   case BuiltinType::WChar_U:
336     return TST_wchar;
337   case BuiltinType::UChar:
338   case BuiltinType::UShort:
339   case BuiltinType::UInt:
340   case BuiltinType::ULong:
341   case BuiltinType::ULongLong:
342   case BuiltinType::UInt128:
343   case BuiltinType::SChar:
344   case BuiltinType::Short:
345   case BuiltinType::Int:
346   case BuiltinType::Long:
347   case BuiltinType::LongLong:
348   case BuiltinType::Int128:
349   case BuiltinType::Half:
350   case BuiltinType::Float:
351   case BuiltinType::Double:
352   case BuiltinType::LongDouble:
353   case BuiltinType::Float16:
354   case BuiltinType::Float128:
355   case BuiltinType::Ibm128:
356   case BuiltinType::ShortAccum:
357   case BuiltinType::Accum:
358   case BuiltinType::LongAccum:
359   case BuiltinType::UShortAccum:
360   case BuiltinType::UAccum:
361   case BuiltinType::ULongAccum:
362   case BuiltinType::ShortFract:
363   case BuiltinType::Fract:
364   case BuiltinType::LongFract:
365   case BuiltinType::UShortFract:
366   case BuiltinType::UFract:
367   case BuiltinType::ULongFract:
368   case BuiltinType::SatShortAccum:
369   case BuiltinType::SatAccum:
370   case BuiltinType::SatLongAccum:
371   case BuiltinType::SatUShortAccum:
372   case BuiltinType::SatUAccum:
373   case BuiltinType::SatULongAccum:
374   case BuiltinType::SatShortFract:
375   case BuiltinType::SatFract:
376   case BuiltinType::SatLongFract:
377   case BuiltinType::SatUShortFract:
378   case BuiltinType::SatUFract:
379   case BuiltinType::SatULongFract:
380   case BuiltinType::BFloat16:
381     llvm_unreachable("Builtin type needs extra local data!");
382     // Fall through, if the impossible happens.
383 
384   case BuiltinType::NullPtr:
385   case BuiltinType::Overload:
386   case BuiltinType::Dependent:
387   case BuiltinType::BoundMember:
388   case BuiltinType::UnknownAny:
389   case BuiltinType::ARCUnbridgedCast:
390   case BuiltinType::PseudoObject:
391   case BuiltinType::ObjCId:
392   case BuiltinType::ObjCClass:
393   case BuiltinType::ObjCSel:
394 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
395   case BuiltinType::Id:
396 #include "clang/Basic/OpenCLImageTypes.def"
397 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
398   case BuiltinType::Id:
399 #include "clang/Basic/OpenCLExtensionTypes.def"
400   case BuiltinType::OCLSampler:
401   case BuiltinType::OCLEvent:
402   case BuiltinType::OCLClkEvent:
403   case BuiltinType::OCLQueue:
404   case BuiltinType::OCLReserveID:
405 #define SVE_TYPE(Name, Id, SingletonId) \
406   case BuiltinType::Id:
407 #include "clang/Basic/AArch64SVEACLETypes.def"
408 #define PPC_VECTOR_TYPE(Name, Id, Size) \
409   case BuiltinType::Id:
410 #include "clang/Basic/PPCTypes.def"
411 #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
412 #include "clang/Basic/RISCVVTypes.def"
413   case BuiltinType::BuiltinFn:
414   case BuiltinType::IncompleteMatrixIdx:
415   case BuiltinType::OMPArraySection:
416   case BuiltinType::OMPArrayShaping:
417   case BuiltinType::OMPIterator:
418     return TST_unspecified;
419   }
420 
421   llvm_unreachable("Invalid BuiltinType Kind!");
422 }
423 
424 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
425   while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
426     TL = PTL.getInnerLoc();
427   return TL;
428 }
429 
430 SourceLocation TypeLoc::findNullabilityLoc() const {
431   if (auto ATL = getAs<AttributedTypeLoc>()) {
432     const Attr *A = ATL.getAttr();
433     if (A && (isa<TypeNullableAttr>(A) || isa<TypeNonNullAttr>(A) ||
434               isa<TypeNullUnspecifiedAttr>(A)))
435       return A->getLocation();
436   }
437 
438   return {};
439 }
440 
441 TypeLoc TypeLoc::findExplicitQualifierLoc() const {
442   // Qualified types.
443   if (auto qual = getAs<QualifiedTypeLoc>())
444     return qual;
445 
446   TypeLoc loc = IgnoreParens();
447 
448   // Attributed types.
449   if (auto attr = loc.getAs<AttributedTypeLoc>()) {
450     if (attr.isQualifier()) return attr;
451     return attr.getModifiedLoc().findExplicitQualifierLoc();
452   }
453 
454   // C11 _Atomic types.
455   if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
456     return atomic;
457   }
458 
459   return {};
460 }
461 
462 void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
463                                            SourceLocation Loc) {
464   setNameLoc(Loc);
465   if (!getNumProtocols()) return;
466 
467   setProtocolLAngleLoc(Loc);
468   setProtocolRAngleLoc(Loc);
469   for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
470     setProtocolLoc(i, Loc);
471 }
472 
473 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
474                                         SourceLocation Loc) {
475   setHasBaseTypeAsWritten(true);
476   setTypeArgsLAngleLoc(Loc);
477   setTypeArgsRAngleLoc(Loc);
478   for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
479     setTypeArgTInfo(i,
480                    Context.getTrivialTypeSourceInfo(
481                      getTypePtr()->getTypeArgsAsWritten()[i], Loc));
482   }
483   setProtocolLAngleLoc(Loc);
484   setProtocolRAngleLoc(Loc);
485   for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
486     setProtocolLoc(i, Loc);
487 }
488 
489 SourceRange AttributedTypeLoc::getLocalSourceRange() const {
490   // Note that this does *not* include the range of the attribute
491   // enclosure, e.g.:
492   //    __attribute__((foo(bar)))
493   //    ^~~~~~~~~~~~~~~        ~~
494   // or
495   //    [[foo(bar)]]
496   //    ^~        ~~
497   // That enclosure doesn't necessarily belong to a single attribute
498   // anyway.
499   return getAttr() ? getAttr()->getRange() : SourceRange();
500 }
501 
502 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
503                                        SourceLocation Loc) {
504   TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
505       ::initializeLocal(Context, Loc);
506   this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
507       getUnderlyingType(), Loc);
508 }
509 
510 void UnaryTransformTypeLoc::initializeLocal(ASTContext &Context,
511                                        SourceLocation Loc) {
512     setKWLoc(Loc);
513     setRParenLoc(Loc);
514     setLParenLoc(Loc);
515     this->setUnderlyingTInfo(
516         Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
517 }
518 
519 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
520                                         SourceLocation Loc) {
521   setElaboratedKeywordLoc(Loc);
522   NestedNameSpecifierLocBuilder Builder;
523   Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
524   setQualifierLoc(Builder.getWithLocInContext(Context));
525 }
526 
527 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
528                                            SourceLocation Loc) {
529   setElaboratedKeywordLoc(Loc);
530   NestedNameSpecifierLocBuilder Builder;
531   Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
532   setQualifierLoc(Builder.getWithLocInContext(Context));
533   setNameLoc(Loc);
534 }
535 
536 void
537 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
538                                                         SourceLocation Loc) {
539   setElaboratedKeywordLoc(Loc);
540   if (getTypePtr()->getQualifier()) {
541     NestedNameSpecifierLocBuilder Builder;
542     Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
543     setQualifierLoc(Builder.getWithLocInContext(Context));
544   } else {
545     setQualifierLoc(NestedNameSpecifierLoc());
546   }
547   setTemplateKeywordLoc(Loc);
548   setTemplateNameLoc(Loc);
549   setLAngleLoc(Loc);
550   setRAngleLoc(Loc);
551   TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
552                                                    getTypePtr()->getArgs(),
553                                                    getArgInfos(), Loc);
554 }
555 
556 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
557                                                       unsigned NumArgs,
558                                                   const TemplateArgument *Args,
559                                               TemplateArgumentLocInfo *ArgInfos,
560                                                       SourceLocation Loc) {
561   for (unsigned i = 0, e = NumArgs; i != e; ++i) {
562     switch (Args[i].getKind()) {
563     case TemplateArgument::Null:
564       llvm_unreachable("Impossible TemplateArgument");
565 
566     case TemplateArgument::Integral:
567     case TemplateArgument::Declaration:
568     case TemplateArgument::NullPtr:
569       ArgInfos[i] = TemplateArgumentLocInfo();
570       break;
571 
572     case TemplateArgument::Expression:
573       ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
574       break;
575 
576     case TemplateArgument::Type:
577       ArgInfos[i] = TemplateArgumentLocInfo(
578                           Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
579                                                            Loc));
580       break;
581 
582     case TemplateArgument::Template:
583     case TemplateArgument::TemplateExpansion: {
584       NestedNameSpecifierLocBuilder Builder;
585       TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
586       if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
587         Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
588       else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
589         Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
590 
591       ArgInfos[i] = TemplateArgumentLocInfo(
592           Context, Builder.getWithLocInContext(Context), Loc,
593           Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
594                                                           : Loc);
595       break;
596     }
597 
598     case TemplateArgument::Pack:
599       ArgInfos[i] = TemplateArgumentLocInfo();
600       break;
601     }
602   }
603 }
604 
605 DeclarationNameInfo AutoTypeLoc::getConceptNameInfo() const {
606   return DeclarationNameInfo(getNamedConcept()->getDeclName(),
607                              getLocalData()->ConceptNameLoc);
608 }
609 
610 void AutoTypeLoc::initializeLocal(ASTContext &Context, SourceLocation Loc) {
611   setNestedNameSpecifierLoc(NestedNameSpecifierLoc());
612   setTemplateKWLoc(Loc);
613   setConceptNameLoc(Loc);
614   setFoundDecl(nullptr);
615   setRAngleLoc(Loc);
616   setLAngleLoc(Loc);
617   TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
618                                                    getTypePtr()->getArgs(),
619                                                    getArgInfos(), Loc);
620   setNameLoc(Loc);
621 }
622 
623 
624 namespace {
625 
626   class GetContainedAutoTypeLocVisitor :
627     public TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc> {
628   public:
629     using TypeLocVisitor<GetContainedAutoTypeLocVisitor, TypeLoc>::Visit;
630 
631     TypeLoc VisitAutoTypeLoc(AutoTypeLoc TL) {
632       return TL;
633     }
634 
635     // Only these types can contain the desired 'auto' type.
636 
637     TypeLoc VisitElaboratedTypeLoc(ElaboratedTypeLoc T) {
638       return Visit(T.getNamedTypeLoc());
639     }
640 
641     TypeLoc VisitQualifiedTypeLoc(QualifiedTypeLoc T) {
642       return Visit(T.getUnqualifiedLoc());
643     }
644 
645     TypeLoc VisitPointerTypeLoc(PointerTypeLoc T) {
646       return Visit(T.getPointeeLoc());
647     }
648 
649     TypeLoc VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
650       return Visit(T.getPointeeLoc());
651     }
652 
653     TypeLoc VisitReferenceTypeLoc(ReferenceTypeLoc T) {
654       return Visit(T.getPointeeLoc());
655     }
656 
657     TypeLoc VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
658       return Visit(T.getPointeeLoc());
659     }
660 
661     TypeLoc VisitArrayTypeLoc(ArrayTypeLoc T) {
662       return Visit(T.getElementLoc());
663     }
664 
665     TypeLoc VisitFunctionTypeLoc(FunctionTypeLoc T) {
666       return Visit(T.getReturnLoc());
667     }
668 
669     TypeLoc VisitParenTypeLoc(ParenTypeLoc T) {
670       return Visit(T.getInnerLoc());
671     }
672 
673     TypeLoc VisitAttributedTypeLoc(AttributedTypeLoc T) {
674       return Visit(T.getModifiedLoc());
675     }
676 
677     TypeLoc VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc T) {
678       return Visit(T.getInnerLoc());
679     }
680 
681     TypeLoc VisitAdjustedTypeLoc(AdjustedTypeLoc T) {
682       return Visit(T.getOriginalLoc());
683     }
684 
685     TypeLoc VisitPackExpansionTypeLoc(PackExpansionTypeLoc T) {
686       return Visit(T.getPatternLoc());
687     }
688   };
689 
690 } // namespace
691 
692 AutoTypeLoc TypeLoc::getContainedAutoTypeLoc() const {
693   TypeLoc Res = GetContainedAutoTypeLocVisitor().Visit(*this);
694   if (Res.isNull())
695     return AutoTypeLoc();
696   return Res.getAs<AutoTypeLoc>();
697 }
698