]> granicus.if.org Git - clang/blob - lib/AST/TypeLoc.cpp
Introduce TypeLoc::getSourceRange().
[clang] / lib / AST / TypeLoc.cpp
1 //===--- TypeLoc.cpp - Type Source Info Wrapper -----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the TypeLoc subclasses implementations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/TypeLoc.h"
15 using namespace clang;
16
17 //===----------------------------------------------------------------------===//
18 // TypeLoc Implementation
19 //===----------------------------------------------------------------------===//
20
21 namespace {
22
23 /// \brief Return the source range for the visited TypeSpecLoc.
24 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
25 public:
26 #define ABSTRACT_TYPELOC(CLASS)
27 #define TYPELOC(CLASS, PARENT, TYPE) \
28     SourceRange Visit##CLASS(CLASS TyLoc) { return TyLoc.getSourceRange(); }
29 #include "clang/AST/TypeLocNodes.def"
30
31   SourceRange VisitTypeLoc(TypeLoc TyLoc) {
32     assert(0 && "A typeloc wrapper was not handled!");
33     return SourceRange();
34   }
35 };
36
37 }
38
39 SourceRange TypeLoc::getSourceRange() const {
40   if (isNull())
41     return SourceRange();
42   return TypeLocRanger().Visit(*this);
43 }
44
45 /// \brief Returns the size of type source info data block for the given type.
46 unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
47   return TypeLoc(Ty, 0).getFullDataSize();
48 }
49
50 /// \brief Find the TypeSpecLoc that is part of this TypeLoc.
51 TypeSpecLoc TypeLoc::getTypeSpecLoc() const {
52   if (isNull())
53     return TypeSpecLoc();
54
55   if (const DeclaratorLoc *DL = dyn_cast<DeclaratorLoc>(this))
56     return DL->getTypeSpecLoc();
57   return cast<TypeSpecLoc>(*this);
58 }
59
60 /// \brief Find the TypeSpecLoc that is part of this TypeLoc and return its
61 /// SourceRange.
62 SourceRange TypeLoc::getTypeSpecRange() const {
63   return getTypeSpecLoc().getSourceRange();
64 }
65
66 namespace {
67
68 /// \brief Report the full source info data size for the visited TypeLoc.
69 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
70 public:
71 #define ABSTRACT_TYPELOC(CLASS)
72 #define TYPELOC(CLASS, PARENT, TYPE) \
73     unsigned Visit##CLASS(CLASS TyLoc) { return TyLoc.getFullDataSize(); }
74 #include "clang/AST/TypeLocNodes.def"
75
76   unsigned VisitTypeLoc(TypeLoc TyLoc) {
77     assert(0 && "A type loc wrapper was not handled!");
78     return 0;
79   }
80 };
81
82 }
83
84 /// \brief Returns the size of the type source info data block.
85 unsigned TypeLoc::getFullDataSize() const {
86   return TypeSizer().Visit(*this);
87 }
88
89 namespace {
90
91 /// \brief Return the "next" TypeLoc for the visited TypeLoc, e.g for "int*" the
92 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
93 class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
94 public:
95 #define TYPELOC(CLASS, PARENT, TYPE)
96 #define DECLARATOR_TYPELOC(CLASS, TYPE) \
97     TypeLoc Visit##CLASS(CLASS TyLoc);
98 #include "clang/AST/TypeLocNodes.def"
99
100   TypeLoc VisitTypeSpecLoc(TypeLoc TyLoc) { return TypeLoc(); }
101
102   TypeLoc VisitTypeLoc(TypeLoc TyLoc) {
103     assert(0 && "A declarator loc wrapper was not handled!");
104     return TypeLoc();
105   }
106 };
107
108 }
109
110 TypeLoc NextLoc::VisitPointerLoc(PointerLoc TL) {
111   return TL.getPointeeLoc();
112 }
113 TypeLoc NextLoc::VisitMemberPointerLoc(MemberPointerLoc TL) {
114   return TL.getPointeeLoc();
115 }
116 TypeLoc NextLoc::VisitBlockPointerLoc(BlockPointerLoc TL) {
117   return TL.getPointeeLoc();
118 }
119 TypeLoc NextLoc::VisitReferenceLoc(ReferenceLoc TL) {
120   return TL.getPointeeLoc();
121 }
122 TypeLoc NextLoc::VisitFunctionLoc(FunctionLoc TL) {
123   return TL.getResultLoc();
124 }
125 TypeLoc NextLoc::VisitArrayLoc(ArrayLoc TL) {
126   return TL.getElementLoc();
127 }
128
129 /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
130 /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
131 TypeLoc TypeLoc::getNextTypeLoc() const {
132   return NextLoc().Visit(*this);
133 }
134
135 //===----------------------------------------------------------------------===//
136 // TypeSpecLoc Implementation
137 //===----------------------------------------------------------------------===//
138
139 namespace {
140 class TypeSpecChecker : public TypeLocVisitor<TypeSpecChecker, bool> {
141 public:
142   bool VisitTypeSpecLoc(TypeSpecLoc TyLoc) { return true; }
143 };
144
145 }
146
147 bool TypeSpecLoc::classof(const TypeLoc *TL) {
148   return TypeSpecChecker().Visit(*TL);
149 }
150
151 //===----------------------------------------------------------------------===//
152 // DeclaratorLoc Implementation
153 //===----------------------------------------------------------------------===//
154
155 namespace {
156
157 /// \brief Return the TypeSpecLoc for the visited DeclaratorLoc.
158 class TypeSpecGetter : public TypeLocVisitor<TypeSpecGetter, TypeSpecLoc> {
159 public:
160 #define TYPELOC(CLASS, PARENT, TYPE)
161 #define DECLARATOR_TYPELOC(CLASS, TYPE) \
162     TypeSpecLoc Visit##CLASS(CLASS TyLoc) { return TyLoc.getTypeSpecLoc(); }
163 #include "clang/AST/TypeLocNodes.def"
164
165   TypeSpecLoc VisitTypeLoc(TypeLoc TyLoc) {
166     assert(0 && "A declarator loc wrapper was not handled!");
167     return TypeSpecLoc();
168   }
169 };
170
171 }
172
173 /// \brief Find the TypeSpecLoc that is part of this DeclaratorLoc.
174 TypeSpecLoc DeclaratorLoc::getTypeSpecLoc() const {
175   return TypeSpecGetter().Visit(*this);
176 }
177
178 namespace {
179
180 class DeclaratorLocChecker : public TypeLocVisitor<DeclaratorLocChecker, bool> {
181 public:
182   bool VisitDeclaratorLoc(DeclaratorLoc TyLoc) { return true; }
183 };
184
185 }
186
187 bool DeclaratorLoc::classof(const TypeLoc *TL) {
188   return DeclaratorLocChecker().Visit(*TL);
189 }
190
191 //===----------------------------------------------------------------------===//
192 // DefaultTypeSpecLoc Implementation
193 //===----------------------------------------------------------------------===//
194
195 namespace {
196
197 class DefaultTypeSpecLocChecker :
198                         public TypeLocVisitor<DefaultTypeSpecLocChecker, bool> {
199 public:
200   bool VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) { return true; }
201 };
202
203 }
204
205 bool DefaultTypeSpecLoc::classof(const TypeLoc *TL) {
206   return DefaultTypeSpecLocChecker().Visit(*TL);
207 }
208
209 //===----------------------------------------------------------------------===//
210 // TypedefLoc Implementation
211 //===----------------------------------------------------------------------===//
212
213 namespace {
214
215 class TypedefLocChecker : public TypeLocVisitor<TypedefLocChecker, bool> {
216 public:
217   bool VisitTypedefLoc(TypedefLoc TyLoc) { return true; }
218 };
219
220 }
221
222 bool TypedefLoc::classof(const TypeLoc *TL) {
223   return TypedefLocChecker().Visit(*TL);
224 }
225
226 //===----------------------------------------------------------------------===//
227 // PointerLoc Implementation
228 //===----------------------------------------------------------------------===//
229
230 namespace {
231
232 class PointerLocChecker : public TypeLocVisitor<PointerLocChecker, bool> {
233 public:
234   bool VisitPointerLoc(PointerLoc TyLoc) { return true; }
235 };
236
237 }
238
239 bool PointerLoc::classof(const TypeLoc *TL) {
240   return PointerLocChecker().Visit(*TL);
241 }
242
243 //===----------------------------------------------------------------------===//
244 // BlockPointerLoc Implementation
245 //===----------------------------------------------------------------------===//
246
247 namespace {
248
249 class BlockPointerLocChecker :
250            public TypeLocVisitor<BlockPointerLocChecker, bool> {
251 public:
252   bool VisitBlockPointerLoc(BlockPointerLoc TyLoc) { return true; }
253 };
254
255 }
256
257 bool BlockPointerLoc::classof(const TypeLoc *TL) {
258   return BlockPointerLocChecker().Visit(*TL);
259 }
260
261 //===----------------------------------------------------------------------===//
262 // MemberPointerLoc Implementation
263 //===----------------------------------------------------------------------===//
264
265 namespace {
266
267 class MemberPointerLocChecker :
268            public TypeLocVisitor<MemberPointerLocChecker, bool> {
269 public:
270   bool VisitMemberPointerLoc(MemberPointerLoc TyLoc) { return true; }
271 };
272
273 }
274
275 bool MemberPointerLoc::classof(const TypeLoc *TL) {
276   return MemberPointerLocChecker().Visit(*TL);
277 }
278
279 //===----------------------------------------------------------------------===//
280 // ReferenceLoc Implementation
281 //===----------------------------------------------------------------------===//
282
283 namespace {
284
285 class ReferenceLocChecker : public TypeLocVisitor<ReferenceLocChecker, bool> {
286 public:
287   bool VisitReferenceLoc(ReferenceLoc TyLoc) { return true; }
288 };
289
290 }
291
292 bool ReferenceLoc::classof(const TypeLoc *TL) {
293   return ReferenceLocChecker().Visit(*TL);
294 }
295
296 //===----------------------------------------------------------------------===//
297 // FunctionLoc Implementation
298 //===----------------------------------------------------------------------===//
299
300 namespace {
301
302 class FunctionLocChecker : public TypeLocVisitor<FunctionLocChecker, bool> {
303 public:
304   bool VisitFunctionLoc(FunctionLoc TyLoc) { return true; }
305 };
306
307 }
308
309 bool FunctionLoc::classof(const TypeLoc *TL) {
310   return FunctionLocChecker().Visit(*TL);
311 }
312
313 //===----------------------------------------------------------------------===//
314 // ArrayLoc Implementation
315 //===----------------------------------------------------------------------===//
316
317 namespace {
318
319 class ArrayLocChecker : public TypeLocVisitor<ArrayLocChecker, bool> {
320 public:
321   bool VisitArrayLoc(ArrayLoc TyLoc) { return true; }
322 };
323
324 }
325
326 bool ArrayLoc::classof(const TypeLoc *TL) {
327   return ArrayLocChecker().Visit(*TL);
328 }