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/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/NestedNameSpecifier.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "clang/AST/TemplateName.h"
20 #include "clang/AST/TypeLocVisitor.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/Specifiers.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/MathExtras.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <cstdint>
28 #include <cstring>
29 
30 using namespace clang;
31 
32 static const unsigned TypeLocMaxDataAlign = alignof(void *);
33 
34 //===----------------------------------------------------------------------===//
35 // TypeLoc Implementation
36 //===----------------------------------------------------------------------===//
37 
38 namespace {
39 
40 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
41 public:
42 #define ABSTRACT_TYPELOC(CLASS, PARENT)
43 #define TYPELOC(CLASS, PARENT) \
44   SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
45     return TyLoc.getLocalSourceRange(); \
46   }
47 #include "clang/AST/TypeLocNodes.def"
48 };
49 
50 } // namespace
51 
52 SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
53   if (TL.isNull()) return SourceRange();
54   return TypeLocRanger().Visit(TL);
55 }
56 
57 namespace {
58 
59 class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
60 public:
61 #define ABSTRACT_TYPELOC(CLASS, PARENT)
62 #define TYPELOC(CLASS, PARENT) \
63   unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
64     return TyLoc.getLocalDataAlignment(); \
65   }
66 #include "clang/AST/TypeLocNodes.def"
67 };
68 
69 } // namespace
70 
71 /// Returns the alignment of the type source info data block.
72 unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
73   if (Ty.isNull()) return 1;
74   return TypeAligner().Visit(TypeLoc(Ty, nullptr));
75 }
76 
77 namespace {
78 
79 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
80 public:
81 #define ABSTRACT_TYPELOC(CLASS, PARENT)
82 #define TYPELOC(CLASS, PARENT) \
83   unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
84     return TyLoc.getLocalDataSize(); \
85   }
86 #include "clang/AST/TypeLocNodes.def"
87 };
88 
89 } // namespace
90 
91 /// Returns the size of the type source info data block.
92 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
93   unsigned Total = 0;
94   TypeLoc TyLoc(Ty, nullptr);
95   unsigned MaxAlign = 1;
96   while (!TyLoc.isNull()) {
97     unsigned Align = getLocalAlignmentForType(TyLoc.getType());
98     MaxAlign = std::max(Align, MaxAlign);
99     Total = llvm::alignTo(Total, Align);
100     Total += TypeSizer().Visit(TyLoc);
101     TyLoc = TyLoc.getNextTypeLoc();
102   }
103   Total = llvm::alignTo(Total, MaxAlign);
104   return Total;
105 }
106 
107 namespace {
108 
109 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
110 public:
111 #define ABSTRACT_TYPELOC(CLASS, PARENT)
112 #define TYPELOC(CLASS, PARENT) \
113   TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
114     return TyLoc.getNextTypeLoc(); \
115   }
116 #include "clang/AST/TypeLocNodes.def"
117 };
118 
119 } // namespace
120 
121 /// Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
122 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
123 TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
124   return NextLoc().Visit(TL);
125 }
126 
127 /// Initializes a type location, and all of its children
128 /// recursively, as if the entire tree had been written in the
129 /// given location.
130 void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
131                              SourceLocation Loc) {
132   while (true) {
133     switch (TL.getTypeLocClass()) {
134 #define ABSTRACT_TYPELOC(CLASS, PARENT)
135 #define TYPELOC(CLASS, PARENT)        \
136     case CLASS: {                     \
137       CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
138       TLCasted.initializeLocal(Context, Loc);  \
139       TL = TLCasted.getNextTypeLoc(); \
140       if (!TL) return;                \
141       continue;                       \
142     }
143 #include "clang/AST/TypeLocNodes.def"
144     }
145   }
146 }
147 
148 namespace {
149 
150 class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
151   TypeLoc Source;
152 
153 public:
154   TypeLocCopier(TypeLoc source) : Source(source) {}
155 
156 #define ABSTRACT_TYPELOC(CLASS, PARENT)
157 #define TYPELOC(CLASS, PARENT)                          \
158   void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) {   \
159     dest.copyLocal(Source.castAs<CLASS##TypeLoc>());  \
160   }
161 #include "clang/AST/TypeLocNodes.def"
162 };
163 
164 } // namespace
165 
166 void TypeLoc::copy(TypeLoc other) {
167   assert(getFullDataSize() == other.getFullDataSize());
168 
169   // If both data pointers are aligned to the maximum alignment, we
170   // can memcpy because getFullDataSize() accurately reflects the
171   // layout of the data.
172   if (reinterpret_cast<uintptr_t>(Data) ==
173           llvm::alignTo(reinterpret_cast<uintptr_t>(Data),
174                         TypeLocMaxDataAlign) &&
175       reinterpret_cast<uintptr_t>(other.Data) ==
176           llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
177                         TypeLocMaxDataAlign)) {
178     memcpy(Data, other.Data, getFullDataSize());
179     return;
180   }
181 
182   // Copy each of the pieces.
183   TypeLoc TL(getType(), Data);
184   do {
185     TypeLocCopier(other).Visit(TL);
186     other = other.getNextTypeLoc();
187   } while ((TL = TL.getNextTypeLoc()));
188 }
189 
190 SourceLocation TypeLoc::getBeginLoc() const {
191   TypeLoc Cur = *this;
192   TypeLoc LeftMost = Cur;
193   while (true) {
194     switch (Cur.getTypeLocClass()) {
195     case Elaborated:
196       LeftMost = Cur;
197       break;
198     case FunctionProto:
199       if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
200               ->hasTrailingReturn()) {
201         LeftMost = Cur;
202         break;
203       }
204       LLVM_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:
215       if (Cur.getLocalSourceRange().getBegin().isValid())
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 
227 SourceLocation TypeLoc::getEndLoc() const {
228   TypeLoc Cur = *this;
229   TypeLoc Last;
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       Last = Cur;
243       break;
244     case FunctionProto:
245       if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
246         Last = TypeLoc();
247       else
248         Last = Cur;
249       break;
250     case Pointer:
251     case BlockPointer:
252     case MemberPointer:
253     case LValueReference:
254     case RValueReference:
255     case PackExpansion:
256       if (!Last)
257         Last = Cur;
258       break;
259     case Qualified:
260     case Elaborated:
261       break;
262     }
263     Cur = Cur.getNextTypeLoc();
264   }
265 }
266 
267 namespace {
268 
269 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
270   // Overload resolution does the real work for us.
271   static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
272   static bool isTypeSpec(TypeLoc _) { return false; }
273 
274 #define ABSTRACT_TYPELOC(CLASS, PARENT)
275 #define TYPELOC(CLASS, PARENT) \
276   bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
277     return isTypeSpec(TyLoc); \
278   }
279 #include "clang/AST/TypeLocNodes.def"
280 };
281 
282 } // namespace
283 
284 /// Determines if the given type loc corresponds to a
285 /// TypeSpecTypeLoc.  Since there is not actually a TypeSpecType in
286 /// the type hierarchy, this is made somewhat complicated.
287 ///
288 /// There are a lot of types that currently use TypeSpecTypeLoc
289 /// because it's a convenient base class.  Ideally we would not accept
290 /// those here, but ideally we would have better implementations for
291 /// them.
292 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
293   if (TL.getType().hasLocalQualifiers()) return false;
294   return TSTChecker().Visit(TL);
295 }
296 
297 // Reimplemented to account for GNU/C++ extension
298 //     typeof unary-expression
299 // where there are no parentheses.
300 SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
301   if (getRParenLoc().isValid())
302     return SourceRange(getTypeofLoc(), getRParenLoc());
303   else
304     return SourceRange(getTypeofLoc(),
305                        getUnderlyingExpr()->getSourceRange().getEnd());
306 }
307 
308 
309 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
310   if (needsExtraLocalData())
311     return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
312   switch (getTypePtr()->getKind()) {
313   case BuiltinType::Void:
314     return TST_void;
315   case BuiltinType::Bool:
316     return TST_bool;
317   case BuiltinType::Char_U:
318   case BuiltinType::Char_S:
319     return TST_char;
320   case BuiltinType::Char8:
321     return TST_char8;
322   case BuiltinType::Char16:
323     return TST_char16;
324   case BuiltinType::Char32:
325     return TST_char32;
326   case BuiltinType::WChar_S:
327   case BuiltinType::WChar_U:
328     return TST_wchar;
329   case BuiltinType::UChar:
330   case BuiltinType::UShort:
331   case BuiltinType::UInt:
332   case BuiltinType::ULong:
333   case BuiltinType::ULongLong:
334   case BuiltinType::UInt128:
335   case BuiltinType::SChar:
336   case BuiltinType::Short:
337   case BuiltinType::Int:
338   case BuiltinType::Long:
339   case BuiltinType::LongLong:
340   case BuiltinType::Int128:
341   case BuiltinType::Half:
342   case BuiltinType::Float:
343   case BuiltinType::Double:
344   case BuiltinType::LongDouble:
345   case BuiltinType::Float16:
346   case BuiltinType::Float128:
347   case BuiltinType::ShortAccum:
348   case BuiltinType::Accum:
349   case BuiltinType::LongAccum:
350   case BuiltinType::UShortAccum:
351   case BuiltinType::UAccum:
352   case BuiltinType::ULongAccum:
353   case BuiltinType::ShortFract:
354   case BuiltinType::Fract:
355   case BuiltinType::LongFract:
356   case BuiltinType::UShortFract:
357   case BuiltinType::UFract:
358   case BuiltinType::ULongFract:
359   case BuiltinType::SatShortAccum:
360   case BuiltinType::SatAccum:
361   case BuiltinType::SatLongAccum:
362   case BuiltinType::SatUShortAccum:
363   case BuiltinType::SatUAccum:
364   case BuiltinType::SatULongAccum:
365   case BuiltinType::SatShortFract:
366   case BuiltinType::SatFract:
367   case BuiltinType::SatLongFract:
368   case BuiltinType::SatUShortFract:
369   case BuiltinType::SatUFract:
370   case BuiltinType::SatULongFract:
371     llvm_unreachable("Builtin type needs extra local data!");
372     // Fall through, if the impossible happens.
373 
374   case BuiltinType::NullPtr:
375   case BuiltinType::Overload:
376   case BuiltinType::Dependent:
377   case BuiltinType::BoundMember:
378   case BuiltinType::UnknownAny:
379   case BuiltinType::ARCUnbridgedCast:
380   case BuiltinType::PseudoObject:
381   case BuiltinType::ObjCId:
382   case BuiltinType::ObjCClass:
383   case BuiltinType::ObjCSel:
384 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
385   case BuiltinType::Id:
386 #include "clang/Basic/OpenCLImageTypes.def"
387 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
388   case BuiltinType::Id:
389 #include "clang/Basic/OpenCLExtensionTypes.def"
390   case BuiltinType::OCLSampler:
391   case BuiltinType::OCLEvent:
392   case BuiltinType::OCLClkEvent:
393   case BuiltinType::OCLQueue:
394   case BuiltinType::OCLReserveID:
395 #define SVE_TYPE(Name, Id, SingletonId) \
396   case BuiltinType::Id:
397 #include "clang/Basic/AArch64SVEACLETypes.def"
398   case BuiltinType::BuiltinFn:
399   case BuiltinType::OMPArraySection:
400     return TST_unspecified;
401   }
402 
403   llvm_unreachable("Invalid BuiltinType Kind!");
404 }
405 
406 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
407   while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
408     TL = PTL.getInnerLoc();
409   return TL;
410 }
411 
412 SourceLocation TypeLoc::findNullabilityLoc() const {
413   if (auto ATL = getAs<AttributedTypeLoc>()) {
414     const Attr *A = ATL.getAttr();
415     if (A && (isa<TypeNullableAttr>(A) || isa<TypeNonNullAttr>(A) ||
416               isa<TypeNullUnspecifiedAttr>(A)))
417       return A->getLocation();
418   }
419 
420   return {};
421 }
422 
423 TypeLoc TypeLoc::findExplicitQualifierLoc() const {
424   // Qualified types.
425   if (auto qual = getAs<QualifiedTypeLoc>())
426     return qual;
427 
428   TypeLoc loc = IgnoreParens();
429 
430   // Attributed types.
431   if (auto attr = loc.getAs<AttributedTypeLoc>()) {
432     if (attr.isQualifier()) return attr;
433     return attr.getModifiedLoc().findExplicitQualifierLoc();
434   }
435 
436   // C11 _Atomic types.
437   if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
438     return atomic;
439   }
440 
441   return {};
442 }
443 
444 void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
445                                            SourceLocation Loc) {
446   setNameLoc(Loc);
447   if (!getNumProtocols()) return;
448 
449   setProtocolLAngleLoc(Loc);
450   setProtocolRAngleLoc(Loc);
451   for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
452     setProtocolLoc(i, Loc);
453 }
454 
455 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
456                                         SourceLocation Loc) {
457   setHasBaseTypeAsWritten(true);
458   setTypeArgsLAngleLoc(Loc);
459   setTypeArgsRAngleLoc(Loc);
460   for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
461     setTypeArgTInfo(i,
462                    Context.getTrivialTypeSourceInfo(
463                      getTypePtr()->getTypeArgsAsWritten()[i], Loc));
464   }
465   setProtocolLAngleLoc(Loc);
466   setProtocolRAngleLoc(Loc);
467   for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
468     setProtocolLoc(i, Loc);
469 }
470 
471 SourceRange AttributedTypeLoc::getLocalSourceRange() const {
472   // Note that this does *not* include the range of the attribute
473   // enclosure, e.g.:
474   //    __attribute__((foo(bar)))
475   //    ^~~~~~~~~~~~~~~        ~~
476   // or
477   //    [[foo(bar)]]
478   //    ^~        ~~
479   // That enclosure doesn't necessarily belong to a single attribute
480   // anyway.
481   return getAttr() ? getAttr()->getRange() : SourceRange();
482 }
483 
484 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
485                                        SourceLocation Loc) {
486   TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
487       ::initializeLocal(Context, Loc);
488   this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
489       getUnderlyingType(), Loc);
490 }
491 
492 void UnaryTransformTypeLoc::initializeLocal(ASTContext &Context,
493                                        SourceLocation Loc) {
494     setKWLoc(Loc);
495     setRParenLoc(Loc);
496     setLParenLoc(Loc);
497     this->setUnderlyingTInfo(
498         Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
499 }
500 
501 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
502                                         SourceLocation Loc) {
503   setElaboratedKeywordLoc(Loc);
504   NestedNameSpecifierLocBuilder Builder;
505   Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
506   setQualifierLoc(Builder.getWithLocInContext(Context));
507 }
508 
509 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
510                                            SourceLocation Loc) {
511   setElaboratedKeywordLoc(Loc);
512   NestedNameSpecifierLocBuilder Builder;
513   Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
514   setQualifierLoc(Builder.getWithLocInContext(Context));
515   setNameLoc(Loc);
516 }
517 
518 void
519 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
520                                                         SourceLocation Loc) {
521   setElaboratedKeywordLoc(Loc);
522   if (getTypePtr()->getQualifier()) {
523     NestedNameSpecifierLocBuilder Builder;
524     Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
525     setQualifierLoc(Builder.getWithLocInContext(Context));
526   } else {
527     setQualifierLoc(NestedNameSpecifierLoc());
528   }
529   setTemplateKeywordLoc(Loc);
530   setTemplateNameLoc(Loc);
531   setLAngleLoc(Loc);
532   setRAngleLoc(Loc);
533   TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
534                                                    getTypePtr()->getArgs(),
535                                                    getArgInfos(), Loc);
536 }
537 
538 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
539                                                       unsigned NumArgs,
540                                                   const TemplateArgument *Args,
541                                               TemplateArgumentLocInfo *ArgInfos,
542                                                       SourceLocation Loc) {
543   for (unsigned i = 0, e = NumArgs; i != e; ++i) {
544     switch (Args[i].getKind()) {
545     case TemplateArgument::Null:
546       llvm_unreachable("Impossible TemplateArgument");
547 
548     case TemplateArgument::Integral:
549     case TemplateArgument::Declaration:
550     case TemplateArgument::NullPtr:
551       ArgInfos[i] = TemplateArgumentLocInfo();
552       break;
553 
554     case TemplateArgument::Expression:
555       ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
556       break;
557 
558     case TemplateArgument::Type:
559       ArgInfos[i] = TemplateArgumentLocInfo(
560                           Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
561                                                            Loc));
562       break;
563 
564     case TemplateArgument::Template:
565     case TemplateArgument::TemplateExpansion: {
566       NestedNameSpecifierLocBuilder Builder;
567       TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
568       if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
569         Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
570       else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
571         Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
572 
573       ArgInfos[i] = TemplateArgumentLocInfo(
574           Builder.getWithLocInContext(Context), Loc,
575           Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
576                                                           : Loc);
577       break;
578     }
579 
580     case TemplateArgument::Pack:
581       ArgInfos[i] = TemplateArgumentLocInfo();
582       break;
583     }
584   }
585 }
586