context.cpp 3.8 KB

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