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