file.cpp 7.6 KB

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