1 //===--- TypeLoc.cpp - Type Source Info Wrapper -----------------*- C++ -*-===//
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/TypeLocVisitor.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace clang;
22 static const unsigned TypeLocMaxDataAlign = llvm::alignOf<void *>();
24 //===----------------------------------------------------------------------===//
25 // TypeLoc Implementation
26 //===----------------------------------------------------------------------===//
29 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
31 #define ABSTRACT_TYPELOC(CLASS, PARENT)
32 #define TYPELOC(CLASS, PARENT) \
33 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
34 return TyLoc.getLocalSourceRange(); \
36 #include "clang/AST/TypeLocNodes.def"
40 SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
41 if (TL.isNull()) return SourceRange();
42 return TypeLocRanger().Visit(TL);
46 class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
48 #define ABSTRACT_TYPELOC(CLASS, PARENT)
49 #define TYPELOC(CLASS, PARENT) \
50 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
51 return TyLoc.getLocalDataAlignment(); \
53 #include "clang/AST/TypeLocNodes.def"
57 /// \brief Returns the alignment of the type source info data block.
58 unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
59 if (Ty.isNull()) return 1;
60 return TypeAligner().Visit(TypeLoc(Ty, nullptr));
64 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
66 #define ABSTRACT_TYPELOC(CLASS, PARENT)
67 #define TYPELOC(CLASS, PARENT) \
68 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
69 return TyLoc.getLocalDataSize(); \
71 #include "clang/AST/TypeLocNodes.def"
75 /// \brief Returns the size of the type source info data block.
76 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
78 TypeLoc TyLoc(Ty, nullptr);
79 unsigned MaxAlign = 1;
80 while (!TyLoc.isNull()) {
81 unsigned Align = getLocalAlignmentForType(TyLoc.getType());
82 MaxAlign = std::max(Align, MaxAlign);
83 Total = llvm::RoundUpToAlignment(Total, Align);
84 Total += TypeSizer().Visit(TyLoc);
85 TyLoc = TyLoc.getNextTypeLoc();
87 Total = llvm::RoundUpToAlignment(Total, MaxAlign);
92 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
94 #define ABSTRACT_TYPELOC(CLASS, PARENT)
95 #define TYPELOC(CLASS, PARENT) \
96 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
97 return TyLoc.getNextTypeLoc(); \
99 #include "clang/AST/TypeLocNodes.def"
103 /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
104 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
105 TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
106 return NextLoc().Visit(TL);
109 /// \brief Initializes a type location, and all of its children
110 /// recursively, as if the entire tree had been written in the
112 void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
113 SourceLocation Loc) {
115 switch (TL.getTypeLocClass()) {
116 #define ABSTRACT_TYPELOC(CLASS, PARENT)
117 #define TYPELOC(CLASS, PARENT) \
119 CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
120 TLCasted.initializeLocal(Context, Loc); \
121 TL = TLCasted.getNextTypeLoc(); \
125 #include "clang/AST/TypeLocNodes.def"
131 class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
134 TypeLocCopier(TypeLoc source) : Source(source) { }
136 #define ABSTRACT_TYPELOC(CLASS, PARENT)
137 #define TYPELOC(CLASS, PARENT) \
138 void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
139 dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
141 #include "clang/AST/TypeLocNodes.def"
146 void TypeLoc::copy(TypeLoc other) {
147 assert(getFullDataSize() == other.getFullDataSize());
149 // If both data pointers are aligned to the maximum alignment, we
150 // can memcpy because getFullDataSize() accurately reflects the
151 // layout of the data.
152 if (reinterpret_cast<uintptr_t>(Data)
153 == llvm::RoundUpToAlignment(reinterpret_cast<uintptr_t>(Data),
154 TypeLocMaxDataAlign) &&
155 reinterpret_cast<uintptr_t>(other.Data)
156 == llvm::RoundUpToAlignment(reinterpret_cast<uintptr_t>(other.Data),
157 TypeLocMaxDataAlign)) {
158 memcpy(Data, other.Data, getFullDataSize());
162 // Copy each of the pieces.
163 TypeLoc TL(getType(), Data);
165 TypeLocCopier(other).Visit(TL);
166 other = other.getNextTypeLoc();
167 } while ((TL = TL.getNextTypeLoc()));
170 SourceLocation TypeLoc::getBeginLoc() const {
172 TypeLoc LeftMost = Cur;
174 switch (Cur.getTypeLocClass()) {
179 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
180 ->hasTrailingReturn()) {
185 case FunctionNoProto:
187 case DependentSizedArray:
188 case IncompleteArray:
190 // FIXME: Currently QualifiedTypeLoc does not have a source range
192 Cur = Cur.getNextTypeLoc();
195 if (!Cur.getLocalSourceRange().getBegin().isInvalid())
197 Cur = Cur.getNextTypeLoc();
204 return LeftMost.getLocalSourceRange().getBegin();
207 SourceLocation TypeLoc::getEndLoc() const {
211 switch (Cur.getTypeLocClass()) {
215 return Last.getLocalSourceRange().getEnd();
218 case DependentSizedArray:
219 case IncompleteArray:
221 case FunctionNoProto:
225 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
233 case LValueReference:
234 case RValueReference:
243 Cur = Cur.getNextTypeLoc();
249 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
250 // Overload resolution does the real work for us.
251 static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
252 static bool isTypeSpec(TypeLoc _) { return false; }
254 #define ABSTRACT_TYPELOC(CLASS, PARENT)
255 #define TYPELOC(CLASS, PARENT) \
256 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
257 return isTypeSpec(TyLoc); \
259 #include "clang/AST/TypeLocNodes.def"
264 /// \brief Determines if the given type loc corresponds to a
265 /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
266 /// the type hierarchy, this is made somewhat complicated.
268 /// There are a lot of types that currently use TypeSpecTypeLoc
269 /// because it's a convenient base class. Ideally we would not accept
270 /// those here, but ideally we would have better implementations for
272 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
273 if (TL.getType().hasLocalQualifiers()) return false;
274 return TSTChecker().Visit(TL);
277 // Reimplemented to account for GNU/C++ extension
278 // typeof unary-expression
279 // where there are no parentheses.
280 SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
281 if (getRParenLoc().isValid())
282 return SourceRange(getTypeofLoc(), getRParenLoc());
284 return SourceRange(getTypeofLoc(),
285 getUnderlyingExpr()->getSourceRange().getEnd());
289 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
290 if (needsExtraLocalData())
291 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
292 switch (getTypePtr()->getKind()) {
293 case BuiltinType::Void:
295 case BuiltinType::Bool:
297 case BuiltinType::Char_U:
298 case BuiltinType::Char_S:
300 case BuiltinType::Char16:
302 case BuiltinType::Char32:
304 case BuiltinType::WChar_S:
305 case BuiltinType::WChar_U:
307 case BuiltinType::UChar:
308 case BuiltinType::UShort:
309 case BuiltinType::UInt:
310 case BuiltinType::ULong:
311 case BuiltinType::ULongLong:
312 case BuiltinType::UInt128:
313 case BuiltinType::SChar:
314 case BuiltinType::Short:
315 case BuiltinType::Int:
316 case BuiltinType::Long:
317 case BuiltinType::LongLong:
318 case BuiltinType::Int128:
319 case BuiltinType::Half:
320 case BuiltinType::Float:
321 case BuiltinType::Double:
322 case BuiltinType::LongDouble:
323 llvm_unreachable("Builtin type needs extra local data!");
324 // Fall through, if the impossible happens.
326 case BuiltinType::NullPtr:
327 case BuiltinType::Overload:
328 case BuiltinType::Dependent:
329 case BuiltinType::BoundMember:
330 case BuiltinType::UnknownAny:
331 case BuiltinType::ARCUnbridgedCast:
332 case BuiltinType::PseudoObject:
333 case BuiltinType::ObjCId:
334 case BuiltinType::ObjCClass:
335 case BuiltinType::ObjCSel:
336 case BuiltinType::OCLImage1d:
337 case BuiltinType::OCLImage1dArray:
338 case BuiltinType::OCLImage1dBuffer:
339 case BuiltinType::OCLImage2d:
340 case BuiltinType::OCLImage2dArray:
341 case BuiltinType::OCLImage3d:
342 case BuiltinType::OCLSampler:
343 case BuiltinType::OCLEvent:
344 case BuiltinType::BuiltinFn:
345 return TST_unspecified;
348 llvm_unreachable("Invalid BuiltinType Kind!");
351 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
352 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
353 TL = PTL.getInnerLoc();
357 SourceLocation TypeLoc::findNullabilityLoc() const {
358 if (auto attributedLoc = getAs<AttributedTypeLoc>()) {
359 if (attributedLoc.getAttrKind() == AttributedType::attr_nullable ||
360 attributedLoc.getAttrKind() == AttributedType::attr_nonnull ||
361 attributedLoc.getAttrKind() == AttributedType::attr_null_unspecified)
362 return attributedLoc.getAttrNameLoc();
365 return SourceLocation();
368 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
369 SourceLocation Loc) {
370 setHasBaseTypeAsWritten(true);
371 setTypeArgsLAngleLoc(Loc);
372 setTypeArgsRAngleLoc(Loc);
373 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
375 Context.getTrivialTypeSourceInfo(
376 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
378 setProtocolLAngleLoc(Loc);
379 setProtocolRAngleLoc(Loc);
380 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
381 setProtocolLoc(i, Loc);
384 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
385 SourceLocation Loc) {
386 TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
387 ::initializeLocal(Context, Loc);
388 this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
389 getUnderlyingType(), Loc);
392 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
393 SourceLocation Loc) {
394 setElaboratedKeywordLoc(Loc);
395 NestedNameSpecifierLocBuilder Builder;
396 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
397 setQualifierLoc(Builder.getWithLocInContext(Context));
400 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
401 SourceLocation Loc) {
402 setElaboratedKeywordLoc(Loc);
403 NestedNameSpecifierLocBuilder Builder;
404 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
405 setQualifierLoc(Builder.getWithLocInContext(Context));
410 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
411 SourceLocation Loc) {
412 setElaboratedKeywordLoc(Loc);
413 if (getTypePtr()->getQualifier()) {
414 NestedNameSpecifierLocBuilder Builder;
415 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
416 setQualifierLoc(Builder.getWithLocInContext(Context));
418 setQualifierLoc(NestedNameSpecifierLoc());
420 setTemplateKeywordLoc(Loc);
421 setTemplateNameLoc(Loc);
424 TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
425 getTypePtr()->getArgs(),
429 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
431 const TemplateArgument *Args,
432 TemplateArgumentLocInfo *ArgInfos,
433 SourceLocation Loc) {
434 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
435 switch (Args[i].getKind()) {
436 case TemplateArgument::Null:
437 llvm_unreachable("Impossible TemplateArgument");
439 case TemplateArgument::Integral:
440 case TemplateArgument::Declaration:
441 case TemplateArgument::NullPtr:
442 ArgInfos[i] = TemplateArgumentLocInfo();
445 case TemplateArgument::Expression:
446 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
449 case TemplateArgument::Type:
450 ArgInfos[i] = TemplateArgumentLocInfo(
451 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
455 case TemplateArgument::Template:
456 case TemplateArgument::TemplateExpansion: {
457 NestedNameSpecifierLocBuilder Builder;
458 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
459 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
460 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
461 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
462 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
464 ArgInfos[i] = TemplateArgumentLocInfo(
465 Builder.getWithLocInContext(Context), Loc,
466 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
471 case TemplateArgument::Pack:
472 ArgInfos[i] = TemplateArgumentLocInfo();