context.cpp 3.5 KB

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