file.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/sem_ir/file.h"
  5. #include <optional>
  6. #include <string>
  7. #include <utility>
  8. #include "common/check.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "toolchain/base/kind_switch.h"
  12. #include "toolchain/base/shared_value_stores.h"
  13. #include "toolchain/base/yaml.h"
  14. #include "toolchain/parse/node_ids.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/inst.h"
  17. #include "toolchain/sem_ir/inst_kind.h"
  18. #include "toolchain/sem_ir/typed_insts.h"
  19. namespace Carbon::SemIR {
  20. File::File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
  21. const std::optional<Parse::Tree::PackagingDecl>& packaging_decl,
  22. SharedValueStores& value_stores, std::string filename)
  23. : parse_tree_(parse_tree),
  24. check_ir_id_(check_ir_id),
  25. package_id_(packaging_decl ? packaging_decl->names.package_id
  26. : PackageNameId::None),
  27. library_id_(packaging_decl ? LibraryNameId::ForStringLiteralValueId(
  28. packaging_decl->names.library_id)
  29. : LibraryNameId::Default),
  30. value_stores_(&value_stores),
  31. filename_(std::move(filename)),
  32. entity_names_(check_ir_id),
  33. cpp_global_vars_(check_ir_id),
  34. functions_(check_ir_id),
  35. cpp_overload_sets_(check_ir_id),
  36. classes_(check_ir_id),
  37. interfaces_(check_ir_id),
  38. named_constraints_(check_ir_id),
  39. require_impls_(check_ir_id),
  40. // 1 reserved id for `RequireImplsBlockId::Empty`.
  41. require_impls_blocks_(allocator_, check_ir_id, 1),
  42. associated_constants_(check_ir_id),
  43. facet_types_(check_ir_id),
  44. identified_facet_types_(check_ir_id),
  45. impls_(*this),
  46. specific_interfaces_(check_ir_id),
  47. generics_(check_ir_id),
  48. specifics_(check_ir_id),
  49. // The `2` prevents adding a tag for the global ids
  50. // `ImportIRId::{ApiForImpl,Cpp}`.
  51. import_irs_(check_ir_id, 2),
  52. clang_decls_(check_ir_id),
  53. // The `+1` prevents adding a tag to the global `NameSpace::PackageInstId`
  54. // instruction. It's not a "singleton" instruction, but it's a unique
  55. // instruction id that comes right after the singletons.
  56. insts_(this, SingletonInstKinds.size() + 1),
  57. vtables_(check_ir_id),
  58. constant_values_(ConstantId::NotConstant, &insts_),
  59. inst_blocks_(allocator_, check_ir_id),
  60. constants_(this),
  61. // 1 reserved id for `StructTypeFieldsId::Empty`.
  62. struct_type_fields_(allocator_, check_ir_id, 1),
  63. // 1 reserved id for `CustomLayoutId::Empty`.
  64. custom_layouts_(allocator_, check_ir_id, 1),
  65. expr_regions_(check_ir_id),
  66. clang_source_locs_(check_ir_id) {
  67. // `type`, `form`, and the error type are both complete & concrete types.
  68. // TODO: This duplicates the code in `check/type_completion.cpp`. Consider
  69. // requiring these types to be complete from Check initialization instead.
  70. types_.SetComplete(
  71. TypeType::TypeId,
  72. {.value_repr = {.kind = ValueRepr::Copy, .type_id = TypeType::TypeId},
  73. .object_layout = SemIR::ObjectLayout::Empty()});
  74. types_.SetComplete(
  75. FormType::TypeId,
  76. {.value_repr = {.kind = ValueRepr::Copy, .type_id = FormType::TypeId},
  77. .object_layout = SemIR::ObjectLayout::Empty()});
  78. types_.SetComplete(
  79. ErrorInst::TypeId,
  80. {.value_repr = {.kind = ValueRepr::Copy, .type_id = ErrorInst::TypeId},
  81. .object_layout = SemIR::ObjectLayout::Empty()});
  82. insts_.Reserve(SingletonInstKinds.size());
  83. for (auto kind : SingletonInstKinds) {
  84. auto inst_id =
  85. insts_.AddInNoBlock(LocIdAndInst::NoLoc(Inst::MakeSingleton(kind)));
  86. constant_values_.Set(inst_id, ConstantId::ForConcreteConstant(inst_id));
  87. }
  88. }
  89. File::~File() = default;
  90. auto File::Verify() const -> ErrorOr<Success> {
  91. // Invariants don't necessarily hold for invalid IR.
  92. if (has_errors_) {
  93. return Success();
  94. }
  95. // Check that every code block has a terminator sequence that appears at the
  96. // end of the block.
  97. for (const Function& function : functions_.values()) {
  98. for (InstBlockId block_id : function.body_block_ids) {
  99. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  100. for (InstId inst_id : inst_blocks().Get(block_id)) {
  101. TerminatorKind inst_kind =
  102. insts().Get(inst_id).kind().terminator_kind();
  103. if (prior_kind == TerminatorKind::Terminator) {
  104. return Error(llvm::formatv("Inst {0} in block {1} follows terminator",
  105. inst_id, block_id));
  106. }
  107. if (prior_kind > inst_kind) {
  108. return Error(
  109. llvm::formatv("Non-terminator inst {0} in block {1} follows "
  110. "terminator sequence",
  111. inst_id, block_id));
  112. }
  113. prior_kind = inst_kind;
  114. }
  115. if (prior_kind != TerminatorKind::Terminator) {
  116. return Error(llvm::formatv("No terminator in block {0}", block_id));
  117. }
  118. }
  119. }
  120. // TODO: Check that an instruction only references other instructions that are
  121. // either global or that dominate it.
  122. return Success();
  123. }
  124. auto File::OutputYaml(bool include_singletons) const -> Yaml::OutputMapping {
  125. return Yaml::OutputMapping([this, include_singletons](
  126. Yaml::OutputMapping::Map map) {
  127. map.Add("filename", filename_);
  128. map.Add("sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  129. map.Add("names", names().OutputYaml());
  130. map.Add("import_irs", import_irs_.OutputYaml());
  131. map.Add("import_ir_insts", import_ir_insts_.OutputYaml());
  132. map.Add("clang_decls", clang_decls_.OutputYaml());
  133. map.Add("name_scopes", name_scopes_.OutputYaml());
  134. map.Add("entity_names", entity_names_.OutputYaml());
  135. map.Add("cpp_global_vars", cpp_global_vars_.OutputYaml());
  136. map.Add("functions", functions_.OutputYaml());
  137. map.Add("classes", classes_.OutputYaml());
  138. map.Add("interfaces", interfaces_.OutputYaml());
  139. map.Add("associated_constants",
  140. associated_constants_.OutputYaml());
  141. map.Add("impls", impls_.OutputYaml());
  142. map.Add("generics", generics_.OutputYaml());
  143. map.Add("specifics", specifics_.OutputYaml());
  144. map.Add("specific_interfaces", specific_interfaces_.OutputYaml());
  145. map.Add("struct_type_fields", struct_type_fields_.OutputYaml());
  146. map.Add("types", types_.OutputYaml());
  147. map.Add("facet_types", facet_types_.OutputYaml());
  148. map.Add("insts",
  149. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  150. for (auto [id, inst] : insts_.enumerate()) {
  151. if (!include_singletons && IsSingletonInstId(id)) {
  152. continue;
  153. }
  154. map.Add(PrintToString(id), Yaml::OutputScalar(inst));
  155. }
  156. }));
  157. map.Add("constant_values",
  158. constant_values_.OutputYaml(include_singletons));
  159. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  160. map.Add("value_stores", value_stores_->OutputYaml());
  161. }));
  162. });
  163. }
  164. auto File::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  165. -> void {
  166. mem_usage.Collect(MemUsage::ConcatLabel(label, "allocator_"), allocator_);
  167. mem_usage.Collect(MemUsage::ConcatLabel(label, "entity_names_"),
  168. entity_names_);
  169. mem_usage.Collect(MemUsage::ConcatLabel(label, "cpp_global_vars_"),
  170. cpp_global_vars_);
  171. mem_usage.Collect(MemUsage::ConcatLabel(label, "functions_"), functions_);
  172. mem_usage.Collect(MemUsage::ConcatLabel(label, "classes_"), classes_);
  173. mem_usage.Collect(MemUsage::ConcatLabel(label, "interfaces_"), interfaces_);
  174. mem_usage.Collect(MemUsage::ConcatLabel(label, "impls_"), impls_);
  175. mem_usage.Collect(MemUsage::ConcatLabel(label, "generics_"), generics_);
  176. mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
  177. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_irs_"), import_irs_);
  178. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_ir_insts_"),
  179. import_ir_insts_);
  180. mem_usage.Collect(MemUsage::ConcatLabel(label, "clang_decls_"), clang_decls_);
  181. mem_usage.Collect(MemUsage::ConcatLabel(label, "struct_type_fields_"),
  182. struct_type_fields_);
  183. mem_usage.Collect(MemUsage::ConcatLabel(label, "insts_"), insts_);
  184. mem_usage.Collect(MemUsage::ConcatLabel(label, "name_scopes_"), name_scopes_);
  185. mem_usage.Collect(MemUsage::ConcatLabel(label, "constant_values_"),
  186. constant_values_);
  187. mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_blocks_"), inst_blocks_);
  188. mem_usage.Collect(MemUsage::ConcatLabel(label, "constants_"), constants_);
  189. mem_usage.Collect(MemUsage::ConcatLabel(label, "types_"), types_);
  190. }
  191. auto File::set_cpp_file(std::unique_ptr<SemIR::CppFile> cpp_file) -> void {
  192. cpp_file_ = std::move(cpp_file);
  193. }
  194. } // namespace Carbon::SemIR