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 using namespace clang;
21 static const unsigned TypeLocMaxDataAlign = alignof(void *);
23 //===----------------------------------------------------------------------===//
24 // TypeLoc Implementation
25 //===----------------------------------------------------------------------===//
28 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
30 #define ABSTRACT_TYPELOC(CLASS, PARENT)
31 #define TYPELOC(CLASS, PARENT) \
32 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
33 return TyLoc.getLocalSourceRange(); \
35 #include "clang/AST/TypeLocNodes.def"
39 SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
40 if (TL.isNull()) return SourceRange();
41 return TypeLocRanger().Visit(TL);
45 class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
47 #define ABSTRACT_TYPELOC(CLASS, PARENT)
48 #define TYPELOC(CLASS, PARENT) \
49 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
50 return TyLoc.getLocalDataAlignment(); \
52 #include "clang/AST/TypeLocNodes.def"
56 /// \brief Returns the alignment of the type source info data block.
57 unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
58 if (Ty.isNull()) return 1;
59 return TypeAligner().Visit(TypeLoc(Ty, nullptr));
63 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
65 #define ABSTRACT_TYPELOC(CLASS, PARENT)
66 #define TYPELOC(CLASS, PARENT) \
67 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
68 return TyLoc.getLocalDataSize(); \
70 #include "clang/AST/TypeLocNodes.def"
74 /// \brief Returns the size of the type source info data block.
75 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
77 TypeLoc TyLoc(Ty, nullptr);
78 unsigned MaxAlign = 1;
79 while (!TyLoc.isNull()) {
80 unsigned Align = getLocalAlignmentForType(TyLoc.getType());
81 MaxAlign = std::max(Align, MaxAlign);
82 Total = llvm::alignTo(Total, Align);
83 Total += TypeSizer().Visit(TyLoc);
84 TyLoc = TyLoc.getNextTypeLoc();
86 Total = llvm::alignTo(Total, MaxAlign);
91 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
93 #define ABSTRACT_TYPELOC(CLASS, PARENT)
94 #define TYPELOC(CLASS, PARENT) \
95 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
96 return TyLoc.getNextTypeLoc(); \
98 #include "clang/AST/TypeLocNodes.def"
102 /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
103 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
104 TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
105 return NextLoc().Visit(TL);
108 /// \brief Initializes a type location, and all of its children
109 /// recursively, as if the entire tree had been written in the
111 void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
112 SourceLocation Loc) {
114 switch (TL.getTypeLocClass()) {
115 #define ABSTRACT_TYPELOC(CLASS, PARENT)
116 #define TYPELOC(CLASS, PARENT) \
118 CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
119 TLCasted.initializeLocal(Context, Loc); \
120 TL = TLCasted.getNextTypeLoc(); \
124 #include "clang/AST/TypeLocNodes.def"
130 class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
133 TypeLocCopier(TypeLoc source) : Source(source) { }
135 #define ABSTRACT_TYPELOC(CLASS, PARENT)
136 #define TYPELOC(CLASS, PARENT) \
137 void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
138 dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
140 #include "clang/AST/TypeLocNodes.def"
145 void TypeLoc::copy(TypeLoc other) {
146 assert(getFullDataSize() == other.getFullDataSize());
148 // If both data pointers are aligned to the maximum alignment, we
149 // can memcpy because getFullDataSize() accurately reflects the
150 // layout of the data.
151 if (reinterpret_cast<uintptr_t>(Data) ==
152 llvm::alignTo(reinterpret_cast<uintptr_t>(Data),
153 TypeLocMaxDataAlign) &&
154 reinterpret_cast<uintptr_t>(other.Data) ==
155 llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
156 TypeLocMaxDataAlign)) {
157 memcpy(Data, other.Data, getFullDataSize());
161 // Copy each of the pieces.
162 TypeLoc TL(getType(), Data);
164 TypeLocCopier(other).Visit(TL);
165 other = other.getNextTypeLoc();
166 } while ((TL = TL.getNextTypeLoc()));
169 SourceLocation TypeLoc::getBeginLoc() const {
171 TypeLoc LeftMost = Cur;
173 switch (Cur.getTypeLocClass()) {
178 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
179 ->hasTrailingReturn()) {
184 case FunctionNoProto:
186 case DependentSizedArray:
187 case IncompleteArray:
189 // FIXME: Currently QualifiedTypeLoc does not have a source range
191 Cur = Cur.getNextTypeLoc();
194 if (Cur.getLocalSourceRange().getBegin().isValid())
196 Cur = Cur.getNextTypeLoc();
203 return LeftMost.getLocalSourceRange().getBegin();
206 SourceLocation TypeLoc::getEndLoc() const {
210 switch (Cur.getTypeLocClass()) {
214 return Last.getLocalSourceRange().getEnd();
217 case DependentSizedArray:
218 case IncompleteArray:
220 case FunctionNoProto:
224 if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
232 case LValueReference:
233 case RValueReference:
242 Cur = Cur.getNextTypeLoc();
248 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
249 // Overload resolution does the real work for us.
250 static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
251 static bool isTypeSpec(TypeLoc _) { return false; }
253 #define ABSTRACT_TYPELOC(CLASS, PARENT)
254 #define TYPELOC(CLASS, PARENT) \
255 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
256 return isTypeSpec(TyLoc); \
258 #include "clang/AST/TypeLocNodes.def"
263 /// \brief Determines if the given type loc corresponds to a
264 /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
265 /// the type hierarchy, this is made somewhat complicated.
267 /// There are a lot of types that currently use TypeSpecTypeLoc
268 /// because it's a convenient base class. Ideally we would not accept
269 /// those here, but ideally we would have better implementations for
271 bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
272 if (TL.getType().hasLocalQualifiers()) return false;
273 return TSTChecker().Visit(TL);
276 // Reimplemented to account for GNU/C++ extension
277 // typeof unary-expression
278 // where there are no parentheses.
279 SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
280 if (getRParenLoc().isValid())
281 return SourceRange(getTypeofLoc(), getRParenLoc());
283 return SourceRange(getTypeofLoc(),
284 getUnderlyingExpr()->getSourceRange().getEnd());
288 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
289 if (needsExtraLocalData())
290 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
291 switch (getTypePtr()->getKind()) {
292 case BuiltinType::Void:
294 case BuiltinType::Bool:
296 case BuiltinType::Char_U:
297 case BuiltinType::Char_S:
299 case BuiltinType::Char16:
301 case BuiltinType::Char32:
303 case BuiltinType::WChar_S:
304 case BuiltinType::WChar_U:
306 case BuiltinType::UChar:
307 case BuiltinType::UShort:
308 case BuiltinType::UInt:
309 case BuiltinType::ULong:
310 case BuiltinType::ULongLong:
311 case BuiltinType::UInt128:
312 case BuiltinType::SChar:
313 case BuiltinType::Short:
314 case BuiltinType::Int:
315 case BuiltinType::Long:
316 case BuiltinType::LongLong:
317 case BuiltinType::Int128:
318 case BuiltinType::Half:
319 case BuiltinType::Float:
320 case BuiltinType::Double:
321 case BuiltinType::LongDouble:
322 case BuiltinType::Float128:
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 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
337 case BuiltinType::Id:
338 #include "clang/Basic/OpenCLImageTypes.def"
339 case BuiltinType::OCLSampler:
340 case BuiltinType::OCLEvent:
341 case BuiltinType::OCLClkEvent:
342 case BuiltinType::OCLQueue:
343 case BuiltinType::OCLNDRange:
344 case BuiltinType::OCLReserveID:
345 case BuiltinType::BuiltinFn:
346 case BuiltinType::OMPArraySection:
347 return TST_unspecified;
350 llvm_unreachable("Invalid BuiltinType Kind!");
353 TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
354 while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
355 TL = PTL.getInnerLoc();
359 SourceLocation TypeLoc::findNullabilityLoc() const {
360 if (auto attributedLoc = getAs<AttributedTypeLoc>()) {
361 if (attributedLoc.getAttrKind() == AttributedType::attr_nullable ||
362 attributedLoc.getAttrKind() == AttributedType::attr_nonnull ||
363 attributedLoc.getAttrKind() == AttributedType::attr_null_unspecified)
364 return attributedLoc.getAttrNameLoc();
367 return SourceLocation();
370 TypeLoc TypeLoc::findExplicitQualifierLoc() const {
372 if (auto qual = getAs<QualifiedTypeLoc>())
375 TypeLoc loc = IgnoreParens();
378 if (auto attr = loc.getAs<AttributedTypeLoc>()) {
379 if (attr.isQualifier()) return attr;
380 return attr.getModifiedLoc().findExplicitQualifierLoc();
383 // C11 _Atomic types.
384 if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
391 void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
392 SourceLocation Loc) {
394 if (!getNumProtocols()) return;
396 setProtocolLAngleLoc(Loc);
397 setProtocolRAngleLoc(Loc);
398 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
399 setProtocolLoc(i, Loc);
402 void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
403 SourceLocation Loc) {
404 setHasBaseTypeAsWritten(true);
405 setTypeArgsLAngleLoc(Loc);
406 setTypeArgsRAngleLoc(Loc);
407 for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
409 Context.getTrivialTypeSourceInfo(
410 getTypePtr()->getTypeArgsAsWritten()[i], Loc));
412 setProtocolLAngleLoc(Loc);
413 setProtocolRAngleLoc(Loc);
414 for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
415 setProtocolLoc(i, Loc);
418 void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
419 SourceLocation Loc) {
420 TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
421 ::initializeLocal(Context, Loc);
422 this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
423 getUnderlyingType(), Loc);
426 void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
427 SourceLocation Loc) {
428 setElaboratedKeywordLoc(Loc);
429 NestedNameSpecifierLocBuilder Builder;
430 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
431 setQualifierLoc(Builder.getWithLocInContext(Context));
434 void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
435 SourceLocation Loc) {
436 setElaboratedKeywordLoc(Loc);
437 NestedNameSpecifierLocBuilder Builder;
438 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
439 setQualifierLoc(Builder.getWithLocInContext(Context));
444 DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
445 SourceLocation Loc) {
446 setElaboratedKeywordLoc(Loc);
447 if (getTypePtr()->getQualifier()) {
448 NestedNameSpecifierLocBuilder Builder;
449 Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
450 setQualifierLoc(Builder.getWithLocInContext(Context));
452 setQualifierLoc(NestedNameSpecifierLoc());
454 setTemplateKeywordLoc(Loc);
455 setTemplateNameLoc(Loc);
458 TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
459 getTypePtr()->getArgs(),
463 void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
465 const TemplateArgument *Args,
466 TemplateArgumentLocInfo *ArgInfos,
467 SourceLocation Loc) {
468 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
469 switch (Args[i].getKind()) {
470 case TemplateArgument::Null:
471 llvm_unreachable("Impossible TemplateArgument");
473 case TemplateArgument::Integral:
474 case TemplateArgument::Declaration:
475 case TemplateArgument::NullPtr:
476 ArgInfos[i] = TemplateArgumentLocInfo();
479 case TemplateArgument::Expression:
480 ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
483 case TemplateArgument::Type:
484 ArgInfos[i] = TemplateArgumentLocInfo(
485 Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
489 case TemplateArgument::Template:
490 case TemplateArgument::TemplateExpansion: {
491 NestedNameSpecifierLocBuilder Builder;
492 TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
493 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
494 Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
495 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
496 Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
498 ArgInfos[i] = TemplateArgumentLocInfo(
499 Builder.getWithLocInContext(Context), Loc,
500 Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
505 case TemplateArgument::Pack:
506 ArgInfos[i] = TemplateArgumentLocInfo();