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 "llvm/Support/raw_ostream.h"
15 #include "clang/AST/TypeLocVisitor.h"
16 #include "clang/AST/Expr.h"
17 #include "llvm/Support/ErrorHandling.h"
18 using namespace clang;
20 //===----------------------------------------------------------------------===//
21 // TypeLoc Implementation
22 //===----------------------------------------------------------------------===//
25 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
27 #define ABSTRACT_TYPELOC(CLASS, PARENT)
28 #define TYPELOC(CLASS, PARENT) \
29 SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
30 return TyLoc.getSourceRange(); \
32 #include "clang/AST/TypeLocNodes.def"
36 SourceRange TypeLoc::getSourceRangeImpl(TypeLoc TL) {
37 if (TL.isNull()) return SourceRange();
38 return TypeLocRanger().Visit(TL);
42 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
44 #define ABSTRACT_TYPELOC(CLASS, PARENT)
45 #define TYPELOC(CLASS, PARENT) \
46 unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
47 return TyLoc.getFullDataSize(); \
49 #include "clang/AST/TypeLocNodes.def"
53 /// \brief Returns the size of the type source info data block.
54 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
55 if (Ty.isNull()) return 0;
56 return TypeSizer().Visit(TypeLoc(Ty, 0));
60 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
62 #define ABSTRACT_TYPELOC(CLASS, PARENT)
63 #define TYPELOC(CLASS, PARENT) \
64 TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
65 return TyLoc.getNextTypeLoc(); \
67 #include "clang/AST/TypeLocNodes.def"
71 /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
72 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
73 TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
74 return NextLoc().Visit(TL);
78 struct TypeLocInitializer : public TypeLocVisitor<TypeLocInitializer> {
80 TypeLocInitializer(SourceLocation Loc) : Loc(Loc) {}
82 #define ABSTRACT_TYPELOC(CLASS, PARENT)
83 #define TYPELOC(CLASS, PARENT) \
84 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
85 TyLoc.initializeLocal(Loc); \
87 #include "clang/AST/TypeLocNodes.def"
91 /// \brief Initializes a type location, and all of its children
92 /// recursively, as if the entire tree had been written in the
94 void TypeLoc::initializeImpl(TypeLoc TL, SourceLocation Loc) {
96 TypeLocInitializer(Loc).Visit(TL);
97 } while ((TL = TL.getNextTypeLoc()));
101 struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
102 // Overload resolution does the real work for us.
103 static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
104 static bool isTypeSpec(TypeLoc _) { return false; }
106 #define ABSTRACT_TYPELOC(CLASS, PARENT)
107 #define TYPELOC(CLASS, PARENT) \
108 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
109 return isTypeSpec(TyLoc); \
111 #include "clang/AST/TypeLocNodes.def"
116 /// \brief Determines if the given type loc corresponds to a
117 /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
118 /// the type hierarchy, this is made somewhat complicated.
120 /// There are a lot of types that currently use TypeSpecTypeLoc
121 /// because it's a convenient base class. Ideally we would not accept
122 /// those here, but ideally we would have better implementations for
124 bool TypeSpecTypeLoc::classof(const TypeLoc *TL) {
125 if (TL->getType().hasLocalQualifiers()) return false;
126 return TSTChecker().Visit(*TL);
129 // Reimplemented to account for GNU/C++ extension
130 // typeof unary-expression
131 // where there are no parentheses.
132 SourceRange TypeOfExprTypeLoc::getSourceRange() const {
133 if (getRParenLoc().isValid())
134 return SourceRange(getTypeofLoc(), getRParenLoc());
136 return SourceRange(getTypeofLoc(),
137 getUnderlyingExpr()->getSourceRange().getEnd());
141 TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
142 if (needsExtraLocalData())
143 return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
145 switch (getTypePtr()->getKind()) {
146 case BuiltinType::Void:
148 case BuiltinType::Bool:
150 case BuiltinType::Char_U:
151 case BuiltinType::Char_S:
153 case BuiltinType::Char16:
155 case BuiltinType::Char32:
157 case BuiltinType::WChar:
159 case BuiltinType::Float:
161 case BuiltinType::Double:
162 case BuiltinType::LongDouble:
164 case BuiltinType::UndeducedAuto:
167 case BuiltinType::UChar:
168 case BuiltinType::UShort:
169 case BuiltinType::UInt:
170 case BuiltinType::ULong:
171 case BuiltinType::ULongLong:
172 case BuiltinType::UInt128:
173 case BuiltinType::SChar:
174 case BuiltinType::Short:
175 case BuiltinType::Int:
176 case BuiltinType::Long:
177 case BuiltinType::LongLong:
178 case BuiltinType::Int128:
179 llvm_unreachable("Builtin type needs extra local data!");
180 // Fall through, if the impossible happens.
182 case BuiltinType::NullPtr:
183 case BuiltinType::Overload:
184 case BuiltinType::Dependent:
185 case BuiltinType::ObjCId:
186 case BuiltinType::ObjCClass:
187 case BuiltinType::ObjCSel:
188 return TST_unspecified;
192 return TST_unspecified;