1 //===- TypeLoc.cpp - Type Source Info Wrapper -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TypeLoc subclasses implementations.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/TypeLoc.h"
15 #include "clang/AST/ASTContext.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"
30 using namespace clang;
32 static const unsigned TypeLocMaxDataAlign = alignof(void *);
34 //===----------------------------------------------------------------------===//
35 // TypeLoc Implementation
36 //===----------------------------------------------------------------------===//
40 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
42 #define ABSTRACT_TYPELOC(CLASS, PARENT)
43 #define TYPELOC(CLASS, PARENT) \
44 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
45 return TyLoc.getLocalSourceRange(); \
47 #include "clang/AST/TypeLocNodes.def"
52 SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
53 if (TL.isNull()) return SourceRange();
54 return TypeLocRanger().Visit(TL);
59 class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
61 #define ABSTRACT_TYPELOC(CLASS, PARENT)
62 #define TYPELOC(CLASS, PARENT) \
63 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
64 return TyLoc.getLocalDataAlignment(); \
66 #include "clang/AST/TypeLocNodes.def"
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));
79 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
81 #define ABSTRACT_TYPELOC(CLASS, PARENT)
82 #define TYPELOC(CLASS, PARENT) \
83 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
84 return TyLoc.getLocalDataSize(); \
86 #include "clang/AST/TypeLocNodes.def"
91 /// Returns the size of the type source info data block.
92 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
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();
103 Total = llvm::alignTo(Total, MaxAlign);
109 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
111 #define ABSTRACT_TYPELOC(CLASS, PARENT)
112 #define TYPELOC(CLASS, PARENT) \
113 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
114 return TyLoc.getNextTypeLoc(); \
116 #include "clang/AST/TypeLocNodes.def"
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);
127 /// Initializes a type location, and all of its children
128 /// recursively, as if the entire tree had been written in the
130 void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
131 SourceLocation Loc) {
133 switch (TL.getTypeLocClass()) {
134 #define ABSTRACT_TYPELOC(CLASS, PARENT)
135 #define TYPELOC(CLASS, PARENT) \
137 CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
138 TLCasted.initializeLocal(Context, Loc); \
139 TL = TLCasted.getNextTypeLoc(); \
143 #include "clang/AST/TypeLocNodes.def"
150 class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
154 TypeLocCopier(TypeLoc source) : Source(source) {}
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>()); \
161 #include "clang/AST/TypeLocNodes.def"
166 void TypeLoc::copy(TypeLoc other) {
167 assert(getFullDataSize() == other.getFullDataSize());
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());
182 // Copy each of the pieces.
183 TypeLoc TL(getType(), Data);
185 TypeLocCopier(other).Visit(TL);
186 other = other.getNextTypeLoc();
187 } while ((TL = TL.getNextTypeLoc()));
190 SourceLocation TypeLoc::getBeginLoc() const {
192 TypeLoc LeftMost = Cur;
194 switch (Cur.getTypeLocClass()) {
199 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
200 ->hasTrailingReturn()) {
205 case FunctionNoProto:
207 case DependentSizedArray:
208 case IncompleteArray:
210 // FIXME: Currently QualifiedTypeLoc does not have a source range
212 Cur = Cur.getNextTypeLoc();
215 if (Cur.getLocalSourceRange().getBegin().isValid())
217 Cur = Cur.getNextTypeLoc();
224 return LeftMost.getLocalSourceRange().getBegin();
227 SourceLocation TypeLoc::getEndLoc() const {
231 switch (Cur.getTypeLocClass()) {
235 return Last.getLocalSourceRange().getEnd();
238 case DependentSizedArray:
239 case IncompleteArray:
241 case FunctionNoProto:
245 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
253 case LValueReference:
254 case RValueReference:
263 Cur = Cur.getNextTypeLoc();
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; }
274 #define ABSTRACT_TYPELOC(CLASS, PARENT)
275 #define TYPELOC(CLASS, PARENT) \
276 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
277 return isTypeSpec(TyLoc); \
279 #include "clang/AST/TypeLocNodes.def"
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.
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
292 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
293 if (TL.getType().hasLocalQualifiers()) return false;
294 return TSTChecker().Visit(TL);
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());
304 return SourceRange(getTypeofLoc(),
305 getUnderlyingExpr()->getSourceRange().getEnd());
309 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
310 if (needsExtraLocalData())
311 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
312 switch (getTypePtr()->getKind()) {
313 case BuiltinType::Void:
315 case BuiltinType::Bool:
317 case BuiltinType::Char_U:
318 case BuiltinType::Char_S:
320 case BuiltinType::Char8:
322 case BuiltinType::Char16:
324 case BuiltinType::Char32:
326 case BuiltinType::WChar_S:
327 case BuiltinType::WChar_U:
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.
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 case BuiltinType::OCLSampler:
388 case BuiltinType::OCLEvent:
389 case BuiltinType::OCLClkEvent:
390 case BuiltinType::OCLQueue:
391 case BuiltinType::OCLReserveID:
392 case BuiltinType::BuiltinFn:
393 case BuiltinType::OMPArraySection:
394 return TST_unspecified;
397 llvm_unreachable("Invalid BuiltinType Kind!");
400 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
401 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
402 TL = PTL.getInnerLoc();
406 SourceLocation TypeLoc::findNullabilityLoc() const {
407 if (auto attributedLoc = getAs<AttributedTypeLoc>()) {
408 if (attributedLoc.getAttrKind() == AttributedType::attr_nullable ||
409 attributedLoc.getAttrKind() == AttributedType::attr_nonnull ||
410 attributedLoc.getAttrKind() == AttributedType::attr_null_unspecified)
411 return attributedLoc.getAttrNameLoc();
417 TypeLoc TypeLoc::findExplicitQualifierLoc() const {
419 if (auto qual = getAs<QualifiedTypeLoc>())
422 TypeLoc loc = IgnoreParens();
425 if (auto attr = loc.getAs<AttributedTypeLoc>()) {
426 if (attr.isQualifier()) return attr;
427 return attr.getModifiedLoc().findExplicitQualifierLoc();
430 // C11 _Atomic types.
431 if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
438 void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
439 SourceLocation Loc) {
441 if (!getNumProtocols()) return;
443 setProtocolLAngleLoc(Loc);
444 setProtocolRAngleLoc(Loc);
445 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
446 setProtocolLoc(i, Loc);
449 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
450 SourceLocation Loc) {
451 setHasBaseTypeAsWritten(true);
452 setTypeArgsLAngleLoc(Loc);
453 setTypeArgsRAngleLoc(Loc);
454 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
456 Context.getTrivialTypeSourceInfo(
457 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
459 setProtocolLAngleLoc(Loc);
460 setProtocolRAngleLoc(Loc);
461 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
462 setProtocolLoc(i, Loc);
465 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
466 SourceLocation Loc) {
467 TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
468 ::initializeLocal(Context, Loc);
469 this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
470 getUnderlyingType(), Loc);
473 void UnaryTransformTypeLoc::initializeLocal(ASTContext &Context,
474 SourceLocation Loc) {
478 this->setUnderlyingTInfo(
479 Context.getTrivialTypeSourceInfo(getTypePtr()->getBaseType(), Loc));
482 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
483 SourceLocation Loc) {
484 setElaboratedKeywordLoc(Loc);
485 NestedNameSpecifierLocBuilder Builder;
486 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
487 setQualifierLoc(Builder.getWithLocInContext(Context));
490 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
491 SourceLocation Loc) {
492 setElaboratedKeywordLoc(Loc);
493 NestedNameSpecifierLocBuilder Builder;
494 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
495 setQualifierLoc(Builder.getWithLocInContext(Context));
500 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
501 SourceLocation Loc) {
502 setElaboratedKeywordLoc(Loc);
503 if (getTypePtr()->getQualifier()) {
504 NestedNameSpecifierLocBuilder Builder;
505 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
506 setQualifierLoc(Builder.getWithLocInContext(Context));
508 setQualifierLoc(NestedNameSpecifierLoc());
510 setTemplateKeywordLoc(Loc);
511 setTemplateNameLoc(Loc);
514 TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
515 getTypePtr()->getArgs(),
519 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
521 const TemplateArgument *Args,
522 TemplateArgumentLocInfo *ArgInfos,
523 SourceLocation Loc) {
524 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
525 switch (Args[i].getKind()) {
526 case TemplateArgument::Null:
527 llvm_unreachable("Impossible TemplateArgument");
529 case TemplateArgument::Integral:
530 case TemplateArgument::Declaration:
531 case TemplateArgument::NullPtr:
532 ArgInfos[i] = TemplateArgumentLocInfo();
535 case TemplateArgument::Expression:
536 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
539 case TemplateArgument::Type:
540 ArgInfos[i] = TemplateArgumentLocInfo(
541 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
545 case TemplateArgument::Template:
546 case TemplateArgument::TemplateExpansion: {
547 NestedNameSpecifierLocBuilder Builder;
548 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
549 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
550 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
551 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
552 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
554 ArgInfos[i] = TemplateArgumentLocInfo(
555 Builder.getWithLocInContext(Context), Loc,
556 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
561 case TemplateArgument::Pack:
562 ArgInfos[i] = TemplateArgumentLocInfo();