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 using namespace clang;
17 //===----------------------------------------------------------------------===//
18 // TypeLoc Implementation
19 //===----------------------------------------------------------------------===//
23 /// \brief Return the source range for the visited TypeSpecLoc.
24 class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
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"
31 SourceRange VisitTypeLoc(TypeLoc TyLoc) {
32 assert(0 && "A typeloc wrapper was not handled!");
39 SourceRange TypeLoc::getSourceRange() const {
42 return TypeLocRanger().Visit(*this);
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();
50 /// \brief Find the TypeSpecLoc that is part of this TypeLoc.
51 TypeSpecLoc TypeLoc::getTypeSpecLoc() const {
55 if (const DeclaratorLoc *DL = dyn_cast<DeclaratorLoc>(this))
56 return DL->getTypeSpecLoc();
57 return cast<TypeSpecLoc>(*this);
60 /// \brief Find the TypeSpecLoc that is part of this TypeLoc and return its
62 SourceRange TypeLoc::getTypeSpecRange() const {
63 return getTypeSpecLoc().getSourceRange();
68 /// \brief Report the full source info data size for the visited TypeLoc.
69 class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
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"
76 unsigned VisitTypeLoc(TypeLoc TyLoc) {
77 assert(0 && "A type loc wrapper was not handled!");
84 /// \brief Returns the size of the type source info data block.
85 unsigned TypeLoc::getFullDataSize() const {
86 return TypeSizer().Visit(*this);
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> {
95 #define TYPELOC(CLASS, PARENT, TYPE)
96 #define DECLARATOR_TYPELOC(CLASS, TYPE) \
97 TypeLoc Visit##CLASS(CLASS TyLoc);
98 #include "clang/AST/TypeLocNodes.def"
100 TypeLoc VisitTypeSpecLoc(TypeLoc TyLoc) { return TypeLoc(); }
102 TypeLoc VisitTypeLoc(TypeLoc TyLoc) {
103 assert(0 && "A declarator loc wrapper was not handled!");
110 TypeLoc NextLoc::VisitPointerLoc(PointerLoc TL) {
111 return TL.getPointeeLoc();
113 TypeLoc NextLoc::VisitMemberPointerLoc(MemberPointerLoc TL) {
114 return TL.getPointeeLoc();
116 TypeLoc NextLoc::VisitBlockPointerLoc(BlockPointerLoc TL) {
117 return TL.getPointeeLoc();
119 TypeLoc NextLoc::VisitReferenceLoc(ReferenceLoc TL) {
120 return TL.getPointeeLoc();
122 TypeLoc NextLoc::VisitFunctionLoc(FunctionLoc TL) {
123 return TL.getResultLoc();
125 TypeLoc NextLoc::VisitArrayLoc(ArrayLoc TL) {
126 return TL.getElementLoc();
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);
135 //===----------------------------------------------------------------------===//
136 // TypeSpecLoc Implementation
137 //===----------------------------------------------------------------------===//
140 class TypeSpecChecker : public TypeLocVisitor<TypeSpecChecker, bool> {
142 bool VisitTypeSpecLoc(TypeSpecLoc TyLoc) { return true; }
147 bool TypeSpecLoc::classof(const TypeLoc *TL) {
148 return TypeSpecChecker().Visit(*TL);
151 //===----------------------------------------------------------------------===//
152 // DeclaratorLoc Implementation
153 //===----------------------------------------------------------------------===//
157 /// \brief Return the TypeSpecLoc for the visited DeclaratorLoc.
158 class TypeSpecGetter : public TypeLocVisitor<TypeSpecGetter, TypeSpecLoc> {
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"
165 TypeSpecLoc VisitTypeLoc(TypeLoc TyLoc) {
166 assert(0 && "A declarator loc wrapper was not handled!");
167 return TypeSpecLoc();
173 /// \brief Find the TypeSpecLoc that is part of this DeclaratorLoc.
174 TypeSpecLoc DeclaratorLoc::getTypeSpecLoc() const {
175 return TypeSpecGetter().Visit(*this);
180 class DeclaratorLocChecker : public TypeLocVisitor<DeclaratorLocChecker, bool> {
182 bool VisitDeclaratorLoc(DeclaratorLoc TyLoc) { return true; }
187 bool DeclaratorLoc::classof(const TypeLoc *TL) {
188 return DeclaratorLocChecker().Visit(*TL);
191 //===----------------------------------------------------------------------===//
192 // DefaultTypeSpecLoc Implementation
193 //===----------------------------------------------------------------------===//
197 class DefaultTypeSpecLocChecker :
198 public TypeLocVisitor<DefaultTypeSpecLocChecker, bool> {
200 bool VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) { return true; }
205 bool DefaultTypeSpecLoc::classof(const TypeLoc *TL) {
206 return DefaultTypeSpecLocChecker().Visit(*TL);
209 //===----------------------------------------------------------------------===//
210 // TypedefLoc Implementation
211 //===----------------------------------------------------------------------===//
215 class TypedefLocChecker : public TypeLocVisitor<TypedefLocChecker, bool> {
217 bool VisitTypedefLoc(TypedefLoc TyLoc) { return true; }
222 bool TypedefLoc::classof(const TypeLoc *TL) {
223 return TypedefLocChecker().Visit(*TL);
226 //===----------------------------------------------------------------------===//
227 // PointerLoc Implementation
228 //===----------------------------------------------------------------------===//
232 class PointerLocChecker : public TypeLocVisitor<PointerLocChecker, bool> {
234 bool VisitPointerLoc(PointerLoc TyLoc) { return true; }
239 bool PointerLoc::classof(const TypeLoc *TL) {
240 return PointerLocChecker().Visit(*TL);
243 //===----------------------------------------------------------------------===//
244 // BlockPointerLoc Implementation
245 //===----------------------------------------------------------------------===//
249 class BlockPointerLocChecker :
250 public TypeLocVisitor<BlockPointerLocChecker, bool> {
252 bool VisitBlockPointerLoc(BlockPointerLoc TyLoc) { return true; }
257 bool BlockPointerLoc::classof(const TypeLoc *TL) {
258 return BlockPointerLocChecker().Visit(*TL);
261 //===----------------------------------------------------------------------===//
262 // MemberPointerLoc Implementation
263 //===----------------------------------------------------------------------===//
267 class MemberPointerLocChecker :
268 public TypeLocVisitor<MemberPointerLocChecker, bool> {
270 bool VisitMemberPointerLoc(MemberPointerLoc TyLoc) { return true; }
275 bool MemberPointerLoc::classof(const TypeLoc *TL) {
276 return MemberPointerLocChecker().Visit(*TL);
279 //===----------------------------------------------------------------------===//
280 // ReferenceLoc Implementation
281 //===----------------------------------------------------------------------===//
285 class ReferenceLocChecker : public TypeLocVisitor<ReferenceLocChecker, bool> {
287 bool VisitReferenceLoc(ReferenceLoc TyLoc) { return true; }
292 bool ReferenceLoc::classof(const TypeLoc *TL) {
293 return ReferenceLocChecker().Visit(*TL);
296 //===----------------------------------------------------------------------===//
297 // FunctionLoc Implementation
298 //===----------------------------------------------------------------------===//
302 class FunctionLocChecker : public TypeLocVisitor<FunctionLocChecker, bool> {
304 bool VisitFunctionLoc(FunctionLoc TyLoc) { return true; }
309 bool FunctionLoc::classof(const TypeLoc *TL) {
310 return FunctionLocChecker().Visit(*TL);
313 //===----------------------------------------------------------------------===//
314 // ArrayLoc Implementation
315 //===----------------------------------------------------------------------===//
319 class ArrayLocChecker : public TypeLocVisitor<ArrayLocChecker, bool> {
321 bool VisitArrayLoc(ArrayLoc TyLoc) { return true; }
326 bool ArrayLoc::classof(const TypeLoc *TL) {
327 return ArrayLocChecker().Visit(*TL);