]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/AST/ASTTypeTraits.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / clang / lib / AST / ASTTypeTraits.cpp
1 //===--- ASTTypeTraits.cpp --------------------------------------*- 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 //  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
12 //  a type safe way.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "clang/AST/ASTTypeTraits.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
19
20 namespace clang {
21 namespace ast_type_traits {
22
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" },
31   { NKI_None, "Decl" },
32 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
33 #include "clang/AST/DeclNodes.inc"
34   { NKI_None, "Stmt" },
35 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
36 #include "clang/AST/StmtNodes.inc"
37   { NKI_None, "Type" },
38 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
39 #include "clang/AST/TypeNodes.def"
40 };
41
42 bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
43   return isBaseOf(KindId, Other.KindId);
44 }
45
46 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
47   return KindId != NKI_None && KindId == Other.KindId;
48 }
49
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;
55 }
56
57 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
58
59 void DynTypedNode::print(llvm::raw_ostream &OS,
60                          const PrintingPolicy &PP) const {
61   if (const TemplateArgument *TA = get<TemplateArgument>())
62     TA->print(PP, OS);
63   else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
64     NNS->print(OS, PP);
65   else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
66     NNSL->getNestedNameSpecifier()->print(OS, PP);
67   else if (const QualType *QT = get<QualType>())
68     QT->print(OS, PP);
69   else if (const TypeLoc *TL = get<TypeLoc>())
70     TL->getType().print(OS, PP);
71   else if (const Decl *D = get<Decl>())
72     D->print(OS, PP);
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);
77   else
78     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
79 }
80
81 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
82   if (const Decl *D = get<Decl>())
83     D->dump(OS);
84   else if (const Stmt *S = get<Stmt>())
85     S->dump(OS, SM);
86   else
87     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
88 }
89
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();
102 }
103
104 } // end namespace ast_type_traits
105 } // end namespace clang