context.cpp 3.3 KB

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