context.cpp 3.2 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(DiagnosticEmitter<SemIRLoc>* 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. // TODO: Remove this and add a `VerifyOnFinish` once we properly push and pop
  31. // in the right places.
  32. generic_region_stack().Push();
  33. }
  34. auto Context::TODO(SemIRLoc loc, std::string label) -> bool {
  35. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "semantics TODO: `{0}`", std::string);
  36. emitter_->Emit(loc, 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.
  43. // node_stack_ will still contain 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. // TODO: Add verification for decl_name_stack_ and
  50. // decl_introducer_state_stack_.
  51. scope_stack_.VerifyOnFinish();
  52. // TODO: Add verification for generic_region_stack_.
  53. #ifndef NDEBUG
  54. if (auto verify = sem_ir_->Verify(); !verify.ok()) {
  55. CARBON_FATAL("{0}Built invalid semantics IR: {1}\n", sem_ir_,
  56. verify.error());
  57. }
  58. #endif
  59. }
  60. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  61. output << "Check::Context\n";
  62. // In a stack dump, this is probably indented by a tab. We treat that as 8
  63. // spaces then add a couple to indent past the Context label.
  64. constexpr int Indent = 10;
  65. node_stack_.PrintForStackDump(Indent, output);
  66. inst_block_stack_.PrintForStackDump(Indent, output);
  67. pattern_block_stack_.PrintForStackDump(Indent, output);
  68. param_and_arg_refs_stack_.PrintForStackDump(Indent, output);
  69. args_type_info_stack_.PrintForStackDump(Indent, output);
  70. }
  71. } // namespace Carbon::Check