node_stack.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/node_stack.h"
  5. #include "llvm/ADT/STLExtras.h"
  6. namespace Carbon::Check {
  7. auto NodeStack::PrintForStackDump(int indent, llvm::raw_ostream& output) const
  8. -> void {
  9. auto print_id = [&]<Id::Kind Kind>(Id id) {
  10. if constexpr (Kind == Id::Kind::None) {
  11. output << "no value";
  12. } else if constexpr (Kind == Id::Kind::Invalid) {
  13. CARBON_FATAL("Should not be in node stack");
  14. } else {
  15. output << id.As<Kind>();
  16. }
  17. };
  18. output.indent(indent);
  19. output << "NodeStack:\n";
  20. for (auto [i, entry] : llvm::enumerate(stack_)) {
  21. auto node_kind = parse_tree_->node_kind(entry.node_id);
  22. output.indent(indent + 2);
  23. output << i << ". " << node_kind << ": ";
  24. switch (node_kind) {
  25. #define CARBON_PARSE_NODE_KIND(Kind) \
  26. case Parse::NodeKind::Kind: \
  27. print_id.operator()<NodeKindToIdKind(Parse::NodeKind::Kind)>(entry.id); \
  28. break;
  29. #include "toolchain/parse/node_kind.def"
  30. }
  31. output << "\n";
  32. }
  33. }
  34. auto NodeStack::CheckIdKindTable() -> void {
  35. #define CARBON_PARSE_NODE_KIND(Name) \
  36. { \
  37. constexpr auto from_category = \
  38. NodeCategoryToIdKind(Parse::Name::Kind.category(), true); \
  39. constexpr auto from_kind = \
  40. NodeKindToIdKindSpecialCases(Parse::Name::Kind); \
  41. static_assert(from_category || from_kind, \
  42. "Id::Kind not specified for " #Name \
  43. "; add to NodeStack::NodeKindToIdKindSpecialCases or " \
  44. "specify a node category in typed_nodes.h"); \
  45. static_assert(!from_category || !from_kind, \
  46. "Special case Id::Kind specified for " #Name \
  47. ", but node category has an Id::Kind; remove from " \
  48. "NodeStack::NodeKindToIdKindSpecialCases"); \
  49. }
  50. #include "toolchain/parse/node_kind.def"
  51. }
  52. } // namespace Carbon::Check