context.cpp 3.3 KB

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