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