context.cpp 4.5 KB

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