]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/AST/ASTTypeTraits.cpp
Vendor import of stripped clang trunk r375505, the last commit before
[FreeBSD/FreeBSD.git] / lib / AST / ASTTypeTraits.cpp
1 //===--- ASTTypeTraits.cpp --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Provides a dynamic type identifier and a dynamically typed node container
10 //  that can be used to store an AST base node at runtime in the same storage in
11 //  a type safe way.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/AST/ASTTypeTraits.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/NestedNameSpecifier.h"
19
20 namespace clang {
21 namespace ast_type_traits {
22
23 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
24   { NKI_None, "<None>" },
25   { NKI_None, "TemplateArgument" },
26   { NKI_None, "TemplateName" },
27   { NKI_None, "NestedNameSpecifierLoc" },
28   { NKI_None, "QualType" },
29   { NKI_None, "TypeLoc" },
30   { NKI_None, "CXXCtorInitializer" },
31   { NKI_None, "NestedNameSpecifier" },
32   { NKI_None, "Decl" },
33 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
34 #include "clang/AST/DeclNodes.inc"
35   { NKI_None, "Stmt" },
36 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
37 #include "clang/AST/StmtNodes.inc"
38   { NKI_None, "Type" },
39 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
40 #include "clang/AST/TypeNodes.inc"
41   { NKI_None, "OMPClause" },
42 #define OPENMP_CLAUSE(TextualSpelling, Class) {NKI_OMPClause, #Class},
43 #include "clang/Basic/OpenMPKinds.def"
44 };
45
46 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
47   return isBaseOf(KindId, Other.KindId, Distance);
48 }
49
50 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
51                            unsigned *Distance) {
52   if (Base == NKI_None || Derived == NKI_None) return false;
53   unsigned Dist = 0;
54   while (Derived != Base && Derived != NKI_None) {
55     Derived = AllKindInfo[Derived].ParentId;
56     ++Dist;
57   }
58   if (Distance)
59     *Distance = Dist;
60   return Derived == Base;
61 }
62
63 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
64
65 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
66                                             ASTNodeKind Kind2) {
67   if (Kind1.isBaseOf(Kind2)) return Kind2;
68   if (Kind2.isBaseOf(Kind1)) return Kind1;
69   return ASTNodeKind();
70 }
71
72 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
73                                                       ASTNodeKind Kind2) {
74   NodeKindId Parent = Kind1.KindId;
75   while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
76     Parent = AllKindInfo[Parent].ParentId;
77   }
78   return ASTNodeKind(Parent);
79 }
80
81 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
82   switch (D.getKind()) {
83 #define DECL(DERIVED, BASE)                                                    \
84     case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
85 #define ABSTRACT_DECL(D)
86 #include "clang/AST/DeclNodes.inc"
87   };
88   llvm_unreachable("invalid decl kind");
89 }
90
91 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
92   switch (S.getStmtClass()) {
93     case Stmt::NoStmtClass: return NKI_None;
94 #define STMT(CLASS, PARENT)                                                    \
95     case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
96 #define ABSTRACT_STMT(S)
97 #include "clang/AST/StmtNodes.inc"
98   }
99   llvm_unreachable("invalid stmt kind");
100 }
101
102 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
103   switch (T.getTypeClass()) {
104 #define TYPE(Class, Base)                                                      \
105     case Type::Class: return ASTNodeKind(NKI_##Class##Type);
106 #define ABSTRACT_TYPE(Class, Base)
107 #include "clang/AST/TypeNodes.inc"
108   }
109   llvm_unreachable("invalid type kind");
110  }
111
112 ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) {
113   switch (C.getClauseKind()) {
114 #define OPENMP_CLAUSE(Name, Class)                                             \
115     case OMPC_##Name: return ASTNodeKind(NKI_##Class);
116 #include "clang/Basic/OpenMPKinds.def"
117   case OMPC_threadprivate:
118   case OMPC_uniform:
119   case OMPC_device_type:
120   case OMPC_match:
121   case OMPC_unknown:
122     llvm_unreachable("unexpected OpenMP clause kind");
123   }
124   llvm_unreachable("invalid stmt kind");
125 }
126
127 void DynTypedNode::print(llvm::raw_ostream &OS,
128                          const PrintingPolicy &PP) const {
129   if (const TemplateArgument *TA = get<TemplateArgument>())
130     TA->print(PP, OS);
131   else if (const TemplateName *TN = get<TemplateName>())
132     TN->print(OS, PP);
133   else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
134     NNS->print(OS, PP);
135   else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
136     if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
137       NNS->print(OS, PP);
138     else
139       OS << "(empty NestedNameSpecifierLoc)";
140   } else if (const QualType *QT = get<QualType>())
141     QT->print(OS, PP);
142   else if (const TypeLoc *TL = get<TypeLoc>())
143     TL->getType().print(OS, PP);
144   else if (const Decl *D = get<Decl>())
145     D->print(OS, PP);
146   else if (const Stmt *S = get<Stmt>())
147     S->printPretty(OS, nullptr, PP);
148   else if (const Type *T = get<Type>())
149     QualType(T, 0).print(OS, PP);
150   else
151     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
152 }
153
154 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
155   if (const Decl *D = get<Decl>())
156     D->dump(OS);
157   else if (const Stmt *S = get<Stmt>())
158     S->dump(OS, SM);
159   else if (const Type *T = get<Type>())
160     T->dump(OS);
161   else
162     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
163 }
164
165 SourceRange DynTypedNode::getSourceRange() const {
166   if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
167     return CCI->getSourceRange();
168   if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
169     return NNSL->getSourceRange();
170   if (const TypeLoc *TL = get<TypeLoc>())
171     return TL->getSourceRange();
172   if (const Decl *D = get<Decl>())
173     return D->getSourceRange();
174   if (const Stmt *S = get<Stmt>())
175     return S->getSourceRange();
176   if (const auto *C = get<OMPClause>())
177     return SourceRange(C->getBeginLoc(), C->getEndLoc());
178   return SourceRange();
179 }
180
181 } // end namespace ast_type_traits
182 } // end namespace clang