context.cpp 3.9 KB

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