1 //===--- ASTTypeTraits.cpp --------------------------------------*- 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 // Provides a dynamic type identifier and a dynamically typed node container
11 // that can be used to store an AST base node at runtime in the same storage in
14 //===----------------------------------------------------------------------===//
16 #include "clang/AST/ASTTypeTraits.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
21 namespace ast_type_traits {
23 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
24 { NKI_None, "<None>" },
25 { NKI_None, "CXXCtorInitializer" },
26 { NKI_None, "TemplateArgument" },
27 { NKI_None, "NestedNameSpecifier" },
28 { NKI_None, "NestedNameSpecifierLoc" },
29 { NKI_None, "QualType" },
30 { NKI_None, "TypeLoc" },
32 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
33 #include "clang/AST/DeclNodes.inc"
35 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
36 #include "clang/AST/StmtNodes.inc"
38 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
39 #include "clang/AST/TypeNodes.def"
42 bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
43 return isBaseOf(KindId, Other.KindId);
46 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
47 return KindId != NKI_None && KindId == Other.KindId;
50 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived) {
51 if (Base == NKI_None || Derived == NKI_None) return false;
52 while (Derived != Base && Derived != NKI_None)
53 Derived = AllKindInfo[Derived].ParentId;
54 return Derived == Base;
57 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
59 void DynTypedNode::print(llvm::raw_ostream &OS,
60 const PrintingPolicy &PP) const {
61 if (const TemplateArgument *TA = get<TemplateArgument>())
63 else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
65 else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
66 NNSL->getNestedNameSpecifier()->print(OS, PP);
67 else if (const QualType *QT = get<QualType>())
69 else if (const TypeLoc *TL = get<TypeLoc>())
70 TL->getType().print(OS, PP);
71 else if (const Decl *D = get<Decl>())
73 else if (const Stmt *S = get<Stmt>())
74 S->printPretty(OS, 0, PP);
75 else if (const Type *T = get<Type>())
76 QualType(T, 0).print(OS, PP);
78 OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
81 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
82 if (const Decl *D = get<Decl>())
84 else if (const Stmt *S = get<Stmt>())
87 OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
90 SourceRange DynTypedNode::getSourceRange() const {
91 if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
92 return CCI->getSourceRange();
93 if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
94 return NNSL->getSourceRange();
95 if (const TypeLoc *TL = get<TypeLoc>())
96 return TL->getSourceRange();
97 if (const Decl *D = get<Decl>())
98 return D->getSourceRange();
99 if (const Stmt *S = get<Stmt>())
100 return S->getSourceRange();
101 return SourceRange();
104 } // end namespace ast_type_traits
105 } // end namespace clang