context.cpp 3.3 KB

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