context.cpp 4.5 KB

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