context.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. bool gen_implicit_type_impls, 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. gen_implicit_type_impls_(gen_implicit_type_impls),
  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_(
  31. FixedSizeValueStore<SemIR::CheckIRId, SemIR::ImportIRId>::
  32. MakeWithExplicitSize(total_ir_count_, 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. // 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. #ifndef NDEBUG
  68. if (auto verify = sem_ir_->Verify(); !verify.ok()) {
  69. CARBON_FATAL("{0}Built invalid semantics IR: {1}\n", sem_ir_,
  70. verify.error());
  71. }
  72. #endif
  73. }
  74. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  75. output << "Check::Context\n";
  76. // In a stack dump, this is probably indented by a tab. We treat that as 8
  77. // spaces then add a couple to indent past the Context label.
  78. constexpr int Indent = 10;
  79. output.indent(Indent);
  80. output << "filename: " << tokens().source().filename() << "\n";
  81. node_stack_.PrintForStackDump(Indent, output);
  82. inst_block_stack_.PrintForStackDump(Indent, output);
  83. pattern_block_stack_.PrintForStackDump(Indent, output);
  84. param_and_arg_refs_stack_.PrintForStackDump(Indent, output);
  85. args_type_info_stack_.PrintForStackDump(Indent, output);
  86. }
  87. } // namespace Carbon::Check