context.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <optional>
  6. #include <string>
  7. #include <utility>
  8. #include "common/check.h"
  9. #include "common/vlog.h"
  10. #include "llvm/ADT/Sequence.h"
  11. #include "toolchain/check/convert.h"
  12. #include "toolchain/check/decl_name_stack.h"
  13. #include "toolchain/check/eval.h"
  14. #include "toolchain/check/generic.h"
  15. #include "toolchain/check/generic_region_stack.h"
  16. #include "toolchain/check/import.h"
  17. #include "toolchain/check/import_ref.h"
  18. #include "toolchain/check/inst_block_stack.h"
  19. #include "toolchain/check/interface.h"
  20. #include "toolchain/check/merge.h"
  21. #include "toolchain/check/type_completion.h"
  22. #include "toolchain/diagnostics/diagnostic_emitter.h"
  23. #include "toolchain/diagnostics/format_providers.h"
  24. #include "toolchain/lex/tokenized_buffer.h"
  25. #include "toolchain/parse/node_ids.h"
  26. #include "toolchain/parse/node_kind.h"
  27. #include "toolchain/sem_ir/file.h"
  28. #include "toolchain/sem_ir/formatter.h"
  29. #include "toolchain/sem_ir/generic.h"
  30. #include "toolchain/sem_ir/ids.h"
  31. #include "toolchain/sem_ir/import_ir.h"
  32. #include "toolchain/sem_ir/inst.h"
  33. #include "toolchain/sem_ir/inst_kind.h"
  34. #include "toolchain/sem_ir/name_scope.h"
  35. #include "toolchain/sem_ir/type_info.h"
  36. #include "toolchain/sem_ir/typed_insts.h"
  37. namespace Carbon::Check {
  38. Context::Context(DiagnosticEmitter* emitter,
  39. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter,
  40. SemIR::File* sem_ir, int imported_ir_count, int total_ir_count,
  41. llvm::raw_ostream* vlog_stream)
  42. : emitter_(emitter),
  43. tree_and_subtrees_getter_(tree_and_subtrees_getter),
  44. sem_ir_(sem_ir),
  45. vlog_stream_(vlog_stream),
  46. node_stack_(sem_ir->parse_tree(), vlog_stream),
  47. inst_block_stack_("inst_block_stack_", *sem_ir, vlog_stream),
  48. pattern_block_stack_("pattern_block_stack_", *sem_ir, vlog_stream),
  49. param_and_arg_refs_stack_(*sem_ir, vlog_stream, node_stack_),
  50. args_type_info_stack_("args_type_info_stack_", *sem_ir, vlog_stream),
  51. decl_name_stack_(this),
  52. scope_stack_(sem_ir_),
  53. vtable_stack_("vtable_stack_", *sem_ir, vlog_stream),
  54. global_init_(this),
  55. region_stack_(
  56. [this](SemIRLoc loc, std::string label) { TODO(loc, label); }) {
  57. // Prepare fields which relate to the number of IRs available for import.
  58. import_irs().Reserve(imported_ir_count);
  59. import_ir_constant_values_.reserve(imported_ir_count);
  60. check_ir_map_.resize(total_ir_count, SemIR::ImportIRId::None);
  61. // TODO: Remove this and add a `VerifyOnFinish` once we properly push and pop
  62. // in the right places.
  63. generic_region_stack().Push();
  64. }
  65. auto Context::TODO(SemIRLoc loc, std::string label) -> bool {
  66. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "semantics TODO: `{0}`", std::string);
  67. emitter_->Emit(loc, SemanticsTodo, std::move(label));
  68. return false;
  69. }
  70. auto Context::VerifyOnFinish() -> void {
  71. // Information in all the various context objects should be cleaned up as
  72. // various pieces of context go out of scope. At this point, nothing should
  73. // remain.
  74. // node_stack_ will still contain top-level entities.
  75. inst_block_stack_.VerifyOnFinish();
  76. pattern_block_stack_.VerifyOnFinish();
  77. param_and_arg_refs_stack_.VerifyOnFinish();
  78. args_type_info_stack_.VerifyOnFinish();
  79. CARBON_CHECK(struct_type_fields_stack_.empty());
  80. // TODO: Add verification for decl_name_stack_ and
  81. // decl_introducer_state_stack_.
  82. scope_stack_.VerifyOnFinish();
  83. // TODO: Add verification for generic_region_stack_.
  84. }
  85. auto Context::Finalize() -> void {
  86. // Pop information for the file-level scope.
  87. sem_ir().set_top_inst_block_id(inst_block_stack().Pop());
  88. scope_stack().Pop();
  89. // Finalizes the list of exports on the IR.
  90. inst_blocks().Set(SemIR::InstBlockId::Exports, exports_);
  91. // Finalizes the ImportRef inst block.
  92. inst_blocks().Set(SemIR::InstBlockId::ImportRefs, import_ref_ids_);
  93. // Finalizes __global_init.
  94. global_init_.Finalize();
  95. }
  96. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  97. output << "Check::Context\n";
  98. // In a stack dump, this is probably indented by a tab. We treat that as 8
  99. // spaces then add a couple to indent past the Context label.
  100. constexpr int Indent = 10;
  101. node_stack_.PrintForStackDump(Indent, output);
  102. inst_block_stack_.PrintForStackDump(Indent, output);
  103. pattern_block_stack_.PrintForStackDump(Indent, output);
  104. param_and_arg_refs_stack_.PrintForStackDump(Indent, output);
  105. args_type_info_stack_.PrintForStackDump(Indent, output);
  106. }
  107. } // namespace Carbon::Check