clang  14.0.0git
ASTTypeTraits.cpp
Go to the documentation of this file.
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 
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/OpenMPClause.h"
21 #include "clang/AST/TypeLoc.h"
22 
23 using namespace clang;
24 
25 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
26  {NKI_None, "<None>"},
27  {NKI_None, "TemplateArgument"},
28  {NKI_None, "TemplateArgumentLoc"},
29  {NKI_None, "LambdaCapture"},
30  {NKI_None, "TemplateName"},
31  {NKI_None, "NestedNameSpecifierLoc"},
32  {NKI_None, "QualType"},
33 #define TYPELOC(CLASS, PARENT) {NKI_##PARENT, #CLASS "TypeLoc"},
34 #include "clang/AST/TypeLocNodes.def"
35  {NKI_None, "TypeLoc"},
36  {NKI_None, "CXXBaseSpecifier"},
37  {NKI_None, "CXXCtorInitializer"},
38  {NKI_None, "NestedNameSpecifier"},
39  {NKI_None, "Decl"},
40 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
41 #include "clang/AST/DeclNodes.inc"
42  {NKI_None, "Stmt"},
43 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
44 #include "clang/AST/StmtNodes.inc"
45  {NKI_None, "Type"},
46 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
47 #include "clang/AST/TypeNodes.inc"
48  {NKI_None, "OMPClause"},
49 #define GEN_CLANG_CLAUSE_CLASS
50 #define CLAUSE_CLASS(Enum, Str, Class) {NKI_OMPClause, #Class},
51 #include "llvm/Frontend/OpenMP/OMP.inc"
52  {NKI_None, "Attr"},
53 #define ATTR(A) {NKI_Attr, #A "Attr"},
54 #include "clang/Basic/AttrList.inc"
55 };
56 
57 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
58  return isBaseOf(KindId, Other.KindId, Distance);
59 }
60 
61 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
62  unsigned *Distance) {
63  if (Base == NKI_None || Derived == NKI_None) return false;
64  unsigned Dist = 0;
65  while (Derived != Base && Derived != NKI_None) {
66  Derived = AllKindInfo[Derived].ParentId;
67  ++Dist;
68  }
69  if (Distance)
70  *Distance = Dist;
71  return Derived == Base;
72 }
73 
75  NodeKindId LastId = KindId;
76  while (LastId) {
77  NodeKindId ParentId = AllKindInfo[LastId].ParentId;
78  if (ParentId == NKI_None)
79  return LastId;
80  LastId = ParentId;
81  }
82  return NKI_None;
83 }
84 
85 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
86 
88  ASTNodeKind Kind2) {
89  if (Kind1.isBaseOf(Kind2)) return Kind2;
90  if (Kind2.isBaseOf(Kind1)) return Kind1;
91  return ASTNodeKind();
92 }
93 
95  ASTNodeKind Kind2) {
96  NodeKindId Parent = Kind1.KindId;
97  while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
98  Parent = AllKindInfo[Parent].ParentId;
99  }
100  return ASTNodeKind(Parent);
101 }
102 
104  switch (D.getKind()) {
105 #define DECL(DERIVED, BASE) \
106  case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
107 #define ABSTRACT_DECL(D)
108 #include "clang/AST/DeclNodes.inc"
109  };
110  llvm_unreachable("invalid decl kind");
111 }
112 
114  switch (S.getStmtClass()) {
115  case Stmt::NoStmtClass: return NKI_None;
116 #define STMT(CLASS, PARENT) \
117  case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
118 #define ABSTRACT_STMT(S)
119 #include "clang/AST/StmtNodes.inc"
120  }
121  llvm_unreachable("invalid stmt kind");
122 }
123 
125  switch (T.getTypeClass()) {
126 #define TYPE(Class, Base) \
127  case Type::Class: return ASTNodeKind(NKI_##Class##Type);
128 #define ABSTRACT_TYPE(Class, Base)
129 #include "clang/AST/TypeNodes.inc"
130  }
131  llvm_unreachable("invalid type kind");
132  }
133 
135  switch (T.getTypeLocClass()) {
136 #define ABSTRACT_TYPELOC(CLASS, PARENT)
137 #define TYPELOC(CLASS, PARENT) \
138  case TypeLoc::CLASS: \
139  return ASTNodeKind(NKI_##CLASS##TypeLoc);
140 #include "clang/AST/TypeLocNodes.def"
141  }
142  llvm_unreachable("invalid typeloc kind");
143  }
144 
146  switch (C.getClauseKind()) {
147 #define GEN_CLANG_CLAUSE_CLASS
148 #define CLAUSE_CLASS(Enum, Str, Class) \
149  case llvm::omp::Clause::Enum: \
150  return ASTNodeKind(NKI_##Class);
151 #define CLAUSE_NO_CLASS(Enum, Str) \
152  case llvm::omp::Clause::Enum: \
153  llvm_unreachable("unexpected OpenMP clause kind");
154 #include "llvm/Frontend/OpenMP/OMP.inc"
155  }
156  llvm_unreachable("invalid omp clause kind");
157 }
158 
160  switch (A.getKind()) {
161 #define ATTR(A) \
162  case attr::A: \
163  return ASTNodeKind(NKI_##A##Attr);
164 #include "clang/Basic/AttrList.inc"
165  }
166  llvm_unreachable("invalid attr kind");
167 }
168 
169 void DynTypedNode::print(llvm::raw_ostream &OS,
170  const PrintingPolicy &PP) const {
171  if (const TemplateArgument *TA = get<TemplateArgument>())
172  TA->print(PP, OS, /*IncludeType*/ true);
173  else if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
174  TAL->getArgument().print(PP, OS, /*IncludeType*/ true);
175  else if (const TemplateName *TN = get<TemplateName>())
176  TN->print(OS, PP);
177  else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
178  NNS->print(OS, PP);
179  else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
180  if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
181  NNS->print(OS, PP);
182  else
183  OS << "(empty NestedNameSpecifierLoc)";
184  } else if (const QualType *QT = get<QualType>())
185  QT->print(OS, PP);
186  else if (const TypeLoc *TL = get<TypeLoc>())
187  TL->getType().print(OS, PP);
188  else if (const Decl *D = get<Decl>())
189  D->print(OS, PP);
190  else if (const Stmt *S = get<Stmt>())
191  S->printPretty(OS, nullptr, PP);
192  else if (const Type *T = get<Type>())
193  QualType(T, 0).print(OS, PP);
194  else if (const Attr *A = get<Attr>())
195  A->printPretty(OS, PP);
196  else
197  OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
198 }
199 
200 void DynTypedNode::dump(llvm::raw_ostream &OS,
201  const ASTContext &Context) const {
202  if (const Decl *D = get<Decl>())
203  D->dump(OS);
204  else if (const Stmt *S = get<Stmt>())
205  S->dump(OS, Context);
206  else if (const Type *T = get<Type>())
207  T->dump(OS, Context);
208  else
209  OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
210 }
211 
213  if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
214  return CCI->getSourceRange();
215  if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
216  return NNSL->getSourceRange();
217  if (const TypeLoc *TL = get<TypeLoc>())
218  return TL->getSourceRange();
219  if (const Decl *D = get<Decl>())
220  return D->getSourceRange();
221  if (const Stmt *S = get<Stmt>())
222  return S->getSourceRange();
223  if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
224  return TAL->getSourceRange();
225  if (const auto *C = get<OMPClause>())
226  return SourceRange(C->getBeginLoc(), C->getEndLoc());
227  if (const auto *CBS = get<CXXBaseSpecifier>())
228  return CBS->getSourceRange();
229  if (const auto *A = get<Attr>())
230  return A->getRange();
231  return SourceRange();
232 }
clang::ASTNodeKind::isBaseOf
bool isBaseOf(ASTNodeKind Other, unsigned *Distance=nullptr) const
Returns true if this is a base kind of (or same as) Other.
Definition: ASTTypeTraits.cpp:57
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:210
clang::DynTypedNode::dump
void dump(llvm::raw_ostream &OS, const ASTContext &Context) const
Dumps the node to the given output stream.
Definition: ASTTypeTraits.cpp:200
clang::ASTNodeKind::getMostDerivedCommonAncestor
static ASTNodeKind getMostDerivedCommonAncestor(ASTNodeKind Kind1, ASTNodeKind Kind2)
Return the most derived common ancestor between Kind1 and Kind2.
Definition: ASTTypeTraits.cpp:94
clang::DynTypedNode::getSourceRange
SourceRange getSourceRange() const
For nodes which represent textual entities in the source code, return their SourceRange.
Definition: ASTTypeTraits.cpp:212
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:674
Attr.h
clang::NestedNameSpecifier
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
Definition: NestedNameSpecifier.h:50
DeclCXX.h
clang::PrintingPolicy
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
clang::ASTNodeKind::ASTNodeKind
ASTNodeKind()
Empty identifier. It matches nothing.
Definition: ASTTypeTraits.h:54
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1491
clang::Stmt::NoStmtClass
@ NoStmtClass
Definition: Stmt.h:72
clang::Decl::getKind
Kind getKind() const
Definition: DeclBase.h:427
clang::OMPClause
This is a basic class for representing single OpenMP clause.
Definition: OpenMPClause.h:55
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
NestedNameSpecifier.h
clang::TemplateArgument
Represents a template argument.
Definition: TemplateBase.h:61
clang::Attr::getKind
attr::Kind getKind() const
Definition: Attr.h:81
clang::TemplateArgumentLoc
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:457
ASTContext.h
clang::NestedNameSpecifierLoc
A C++ nested-name-specifier augmented with source location information.
Definition: NestedNameSpecifier.h:243
Base
clang::Type::getTypeClass
TypeClass getTypeClass() const
Definition: Type.h:1859
clang::TemplateName
Represents a C++ template name within the type system.
Definition: TemplateName.h:190
clang::DynTypedNode::print
void print(llvm::raw_ostream &OS, const PrintingPolicy &PP) const
Prints the node to the given output stream.
Definition: ASTTypeTraits.cpp:169
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
OpenMPClause.h
clang::TypeLoc
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:58
ASTTypeTraits.h
clang::ASTNodeKind
Kind identifier.
Definition: ASTTypeTraits.h:51
clang::TypeLoc::getTypeLocClass
TypeLocClass getTypeLocClass() const
Definition: TypeLoc.h:115
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::Attr
Attr - This represents one attribute.
Definition: Attr.h:41
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
clang::QualType::print
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
Definition: TypePrinter.cpp:2267
clang::ASTNodeKind::getMostDerivedType
static ASTNodeKind getMostDerivedType(ASTNodeKind Kind1, ASTNodeKind Kind2)
Return the most derived type between Kind1 and Kind2.
Definition: ASTTypeTraits.cpp:87
clang::CXXCtorInitializer
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2171
clang::ASTNodeKind::getCladeKind
ASTNodeKind getCladeKind() const
Definition: ASTTypeTraits.cpp:74
clang::ASTNodeKind::asStringRef
StringRef asStringRef() const
String representation of the kind.
Definition: ASTTypeTraits.cpp:85
clang::ASTNodeKind::getFromNode
static ASTNodeKind getFromNode(const Decl &D)
Definition: ASTTypeTraits.cpp:103
TypeLoc.h