ASTTypeTraits.cpp 6.2 KB

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