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 #include "clang/AST/OpenMPClause.h"
20 
21 using namespace clang;
22 
23 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
24   { NKI_None, "<None>" },
25   { NKI_None, "TemplateArgument" },
26   { NKI_None, "TemplateArgumentLoc" },
27   { NKI_None, "TemplateName" },
28   { NKI_None, "NestedNameSpecifierLoc" },
29   { NKI_None, "QualType" },
30   { NKI_None, "TypeLoc" },
31   { NKI_None, "CXXBaseSpecifier" },
32   { NKI_None, "CXXCtorInitializer" },
33   { NKI_None, "NestedNameSpecifier" },
34   { NKI_None, "Decl" },
35 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
36 #include "clang/AST/DeclNodes.inc"
37   { NKI_None, "Stmt" },
38 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
39 #include "clang/AST/StmtNodes.inc"
40   { NKI_None, "Type" },
41 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
42 #include "clang/AST/TypeNodes.inc"
43   { NKI_None, "OMPClause" },
44 #define OMP_CLAUSE_CLASS(Enum, Str, Class) {NKI_OMPClause, #Class},
45 #include "llvm/Frontend/OpenMP/OMPKinds.def"
46 };
47 
48 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
49   return isBaseOf(KindId, Other.KindId, Distance);
50 }
51 
52 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
53                            unsigned *Distance) {
54   if (Base == NKI_None || Derived == NKI_None) return false;
55   unsigned Dist = 0;
56   while (Derived != Base && Derived != NKI_None) {
57     Derived = AllKindInfo[Derived].ParentId;
58     ++Dist;
59   }
60   if (Distance)
61     *Distance = Dist;
62   return Derived == Base;
63 }
64 
65 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
66 
67 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
68                                             ASTNodeKind Kind2) {
69   if (Kind1.isBaseOf(Kind2)) return Kind2;
70   if (Kind2.isBaseOf(Kind1)) return Kind1;
71   return ASTNodeKind();
72 }
73 
74 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
75                                                       ASTNodeKind Kind2) {
76   NodeKindId Parent = Kind1.KindId;
77   while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
78     Parent = AllKindInfo[Parent].ParentId;
79   }
80   return ASTNodeKind(Parent);
81 }
82 
83 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
84   switch (D.getKind()) {
85 #define DECL(DERIVED, BASE)                                                    \
86     case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
87 #define ABSTRACT_DECL(D)
88 #include "clang/AST/DeclNodes.inc"
89   };
90   llvm_unreachable("invalid decl kind");
91 }
92 
93 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
94   switch (S.getStmtClass()) {
95     case Stmt::NoStmtClass: return NKI_None;
96 #define STMT(CLASS, PARENT)                                                    \
97     case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
98 #define ABSTRACT_STMT(S)
99 #include "clang/AST/StmtNodes.inc"
100   }
101   llvm_unreachable("invalid stmt kind");
102 }
103 
104 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
105   switch (T.getTypeClass()) {
106 #define TYPE(Class, Base)                                                      \
107     case Type::Class: return ASTNodeKind(NKI_##Class##Type);
108 #define ABSTRACT_TYPE(Class, Base)
109 #include "clang/AST/TypeNodes.inc"
110   }
111   llvm_unreachable("invalid type kind");
112  }
113 
114 ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) {
115   switch (C.getClauseKind()) {
116 #define OMP_CLAUSE_CLASS(Enum, Str, Class)                                     \
117   case llvm::omp::Clause::Enum:                                                \
118     return ASTNodeKind(NKI_##Class);
119 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
120   case llvm::omp::Clause::Enum:                                                \
121     llvm_unreachable("unexpected OpenMP clause kind");
122   default:
123     break;
124 #include "llvm/Frontend/OpenMP/OMPKinds.def"
125   }
126   llvm_unreachable("invalid stmt kind");
127 }
128 
129 void DynTypedNode::print(llvm::raw_ostream &OS,
130                          const PrintingPolicy &PP) const {
131   if (const TemplateArgument *TA = get<TemplateArgument>())
132     TA->print(PP, OS);
133   else if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
134     TAL->getArgument().print(PP, OS);
135   else if (const TemplateName *TN = get<TemplateName>())
136     TN->print(OS, PP);
137   else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
138     NNS->print(OS, PP);
139   else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
140     if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
141       NNS->print(OS, PP);
142     else
143       OS << "(empty NestedNameSpecifierLoc)";
144   } else if (const QualType *QT = get<QualType>())
145     QT->print(OS, PP);
146   else if (const TypeLoc *TL = get<TypeLoc>())
147     TL->getType().print(OS, PP);
148   else if (const Decl *D = get<Decl>())
149     D->print(OS, PP);
150   else if (const Stmt *S = get<Stmt>())
151     S->printPretty(OS, nullptr, PP);
152   else if (const Type *T = get<Type>())
153     QualType(T, 0).print(OS, PP);
154   else
155     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
156 }
157 
158 void DynTypedNode::dump(llvm::raw_ostream &OS,
159                         const ASTContext &Context) const {
160   if (const Decl *D = get<Decl>())
161     D->dump(OS);
162   else if (const Stmt *S = get<Stmt>())
163     S->dump(OS, Context);
164   else if (const Type *T = get<Type>())
165     T->dump(OS, Context);
166   else
167     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
168 }
169 
170 SourceRange DynTypedNode::getSourceRange() const {
171   if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
172     return CCI->getSourceRange();
173   if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
174     return NNSL->getSourceRange();
175   if (const TypeLoc *TL = get<TypeLoc>())
176     return TL->getSourceRange();
177   if (const Decl *D = get<Decl>())
178     return D->getSourceRange();
179   if (const Stmt *S = get<Stmt>())
180     return S->getSourceRange();
181   if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
182     return TAL->getSourceRange();
183   if (const auto *C = get<OMPClause>())
184     return SourceRange(C->getBeginLoc(), C->getEndLoc());
185   return SourceRange();
186 }
187