context.cpp 3.7 KB

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