context.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/context.h"
  5. #include <string>
  6. #include <utility>
  7. #include "common/check.h"
  8. #include "toolchain/check/deferred_definition_worklist.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. Context::Context(DiagnosticEmitterBase* emitter,
  12. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter,
  13. SemIR::File* sem_ir, int imported_ir_count, int total_ir_count,
  14. llvm::raw_ostream* vlog_stream)
  15. : emitter_(emitter),
  16. tree_and_subtrees_getter_(tree_and_subtrees_getter),
  17. sem_ir_(sem_ir),
  18. total_ir_count_(total_ir_count),
  19. vlog_stream_(vlog_stream),
  20. node_stack_(sem_ir->parse_tree(), vlog_stream),
  21. inst_block_stack_("inst_block_stack_", *sem_ir, vlog_stream),
  22. pattern_block_stack_("pattern_block_stack_", *sem_ir, vlog_stream),
  23. param_and_arg_refs_stack_(*sem_ir, vlog_stream, node_stack_),
  24. args_type_info_stack_("args_type_info_stack_", *sem_ir, vlog_stream),
  25. decl_name_stack_(this),
  26. scope_stack_(sem_ir_),
  27. deferred_definition_worklist_(vlog_stream),
  28. vtable_stack_("vtable_stack_", *sem_ir, vlog_stream),
  29. check_ir_map_(
  30. FixedSizeValueStore<SemIR::CheckIRId, SemIR::ImportIRId>::
  31. MakeWithExplicitSize(total_ir_count_, SemIR::ImportIRId::None)),
  32. global_init_(this),
  33. region_stack_([this](SemIR::LocId loc_id, std::string label) {
  34. TODO(loc_id, label);
  35. }) {
  36. // Prepare fields which relate to the number of IRs available for import.
  37. import_irs().Reserve(imported_ir_count);
  38. import_ir_constant_values_.reserve(imported_ir_count);
  39. }
  40. auto Context::TODO(SemIR::LocId loc_id, std::string label) -> bool {
  41. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "semantics TODO: `{0}`", std::string);
  42. emitter_->Emit(loc_id, SemanticsTodo, std::move(label));
  43. return false;
  44. }
  45. auto Context::TODO(SemIR::InstId loc_inst_id, std::string label) -> bool {
  46. return TODO(SemIR::LocId(loc_inst_id), label);
  47. }
  48. auto Context::VerifyOnFinish() const -> void {
  49. // Information in all the various context objects should be cleaned up as
  50. // various pieces of context go out of scope. At this point, nothing should
  51. // remain, so we verify stacks are empty. `node_stack_` is an exception
  52. // because it ends containing all top-level entities.
  53. inst_block_stack_.VerifyOnFinish();
  54. pattern_block_stack_.VerifyOnFinish();
  55. param_and_arg_refs_stack_.VerifyOnFinish();
  56. args_type_info_stack_.VerifyOnFinish();
  57. CARBON_CHECK(struct_type_fields_stack_.empty());
  58. CARBON_CHECK(field_decls_stack_.empty());
  59. decl_name_stack_.VerifyOnFinish();
  60. decl_introducer_state_stack_.VerifyOnFinish();
  61. scope_stack_.VerifyOnFinish();
  62. generic_region_stack_.VerifyOnFinish();
  63. vtable_stack_.VerifyOnFinish();
  64. region_stack_.VerifyOnFinish();
  65. CARBON_CHECK(impl_lookup_stack_.empty());
  66. #ifndef NDEBUG
  67. if (auto verify = sem_ir_->Verify(); !verify.ok()) {
  68. CARBON_FATAL("{0}Built invalid semantics IR: {1}\n", sem_ir_,
  69. verify.error());
  70. }
  71. #endif
  72. }
  73. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  74. output << "Check::Context\n";
  75. // In a stack dump, this is probably indented by a tab. We treat that as 8
  76. // spaces then add a couple to indent past the Context label.
  77. constexpr int Indent = 10;
  78. output.indent(Indent);
  79. output << "filename: " << tokens().source().filename() << "\n";
  80. node_stack_.PrintForStackDump(Indent, output);
  81. inst_block_stack_.PrintForStackDump(Indent, output);
  82. pattern_block_stack_.PrintForStackDump(Indent, output);
  83. param_and_arg_refs_stack_.PrintForStackDump(Indent, output);
  84. args_type_info_stack_.PrintForStackDump(Indent, output);
  85. }
  86. } // namespace Carbon::Check