name_scope.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_NAME_SCOPE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_NAME_SCOPE_H_
  6. #include "common/map.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. namespace Carbon::SemIR {
  10. // Access control for an entity.
  11. enum class AccessKind : int8_t {
  12. Public,
  13. Protected,
  14. Private,
  15. };
  16. struct NameScope : Printable<NameScope> {
  17. struct Entry {
  18. NameId name_id;
  19. InstId inst_id;
  20. AccessKind access_kind;
  21. };
  22. auto Print(llvm::raw_ostream& out) const -> void {
  23. out << "{inst: " << inst_id << ", parent_scope: " << parent_scope_id
  24. << ", has_error: " << (has_error ? "true" : "false");
  25. out << ", extended_scopes: [";
  26. llvm::ListSeparator scope_sep;
  27. for (auto id : extended_scopes) {
  28. out << scope_sep << id;
  29. }
  30. out << "]";
  31. out << ", names: {";
  32. llvm::ListSeparator sep;
  33. for (auto entry : names) {
  34. out << sep << entry.name_id << ": " << entry.inst_id;
  35. }
  36. out << "}";
  37. out << "}";
  38. }
  39. // Adds a name to the scope that must not already exist.
  40. auto AddRequired(Entry name_entry) -> void {
  41. auto add_name = [&] {
  42. int index = names.size();
  43. names.push_back(name_entry);
  44. return index;
  45. };
  46. auto result = name_map.Insert(name_entry.name_id, add_name);
  47. CARBON_CHECK(result.is_inserted(), "Failed to add required name: {0}",
  48. name_entry.name_id);
  49. }
  50. // Returns true if this name scope describes an imported package.
  51. auto is_imported_package() const -> bool {
  52. return is_closed_import && parent_scope_id == NameScopeId::Package;
  53. }
  54. // Names in the scope. We store both an insertion-ordered vector for iterating
  55. // and a map from `NameId` to the index of that vector for name lookup.
  56. //
  57. // Optimization notes: this is somewhat memory inefficient. If this ends up
  58. // either hot or a significant source of memory allocation, we should consider
  59. // switching to a SOA model where the `AccessKind` is stored in a separate
  60. // vector so that these can pack densely. If this ends up both cold and memory
  61. // intensive, we can also switch the lookup to a set of indices into the
  62. // vector rather than a map from `NameId` to index.
  63. llvm::SmallVector<Entry> names;
  64. Map<NameId, int> name_map;
  65. // Scopes extended by this scope.
  66. //
  67. // TODO: A `NameScopeId` is currently insufficient to describe an extended
  68. // scope in general. For example:
  69. //
  70. // class A(T:! type) {
  71. // extend base: B(T*);
  72. // }
  73. //
  74. // needs to describe the `T*` argument.
  75. //
  76. // Small vector size is set to 1: we expect that there will rarely be more
  77. // than a single extended scope. Currently the only kind of extended scope is
  78. // a base class, and there can be only one of those per scope.
  79. // TODO: Revisit this once we have more kinds of extended scope and data.
  80. // TODO: Consider using something like `TinyPtrVector` for this.
  81. llvm::SmallVector<NameScopeId, 1> extended_scopes;
  82. // The instruction which owns the scope.
  83. InstId inst_id;
  84. // When the scope is a namespace, the name. Otherwise, invalid.
  85. NameId name_id;
  86. // The parent scope.
  87. NameScopeId parent_scope_id;
  88. // Whether we have diagnosed an error in a construct that would have added
  89. // names to this scope. For example, this can happen if an `import` failed or
  90. // an `extend` declaration was ill-formed. If true, the `names` map is assumed
  91. // to be missing names as a result of the error, and no further errors are
  92. // produced for lookup failures in this scope.
  93. bool has_error = false;
  94. // True if this is a closed namespace created by importing a package.
  95. bool is_closed_import = false;
  96. // Imported IR scopes that compose this namespace. This will be empty for
  97. // scopes that correspond to the current package.
  98. llvm::SmallVector<std::pair<SemIR::ImportIRId, SemIR::NameScopeId>, 0>
  99. import_ir_scopes;
  100. };
  101. // Provides a ValueStore wrapper for an API specific to name scopes.
  102. class NameScopeStore {
  103. public:
  104. explicit NameScopeStore(InstStore* insts) : insts_(insts) {}
  105. // Adds a name scope, returning an ID to reference it.
  106. auto Add(InstId inst_id, NameId name_id, NameScopeId parent_scope_id)
  107. -> NameScopeId {
  108. return values_.Add({.inst_id = inst_id,
  109. .name_id = name_id,
  110. .parent_scope_id = parent_scope_id});
  111. }
  112. // Adds a name that is required to exist in a name scope, such as `Self`.
  113. // These must never conflict.
  114. auto AddRequiredName(NameScopeId scope_id, NameId name_id, InstId inst_id)
  115. -> void {
  116. Get(scope_id).AddRequired({.name_id = name_id,
  117. .inst_id = inst_id,
  118. .access_kind = AccessKind::Public});
  119. }
  120. // Returns the requested name scope.
  121. auto Get(NameScopeId scope_id) -> NameScope& { return values_.Get(scope_id); }
  122. // Returns the requested name scope.
  123. auto Get(NameScopeId scope_id) const -> const NameScope& {
  124. return values_.Get(scope_id);
  125. }
  126. // Returns the instruction owning the requested name scope, or Invalid with
  127. // nullopt if the scope is either invalid or has no associated instruction.
  128. auto GetInstIfValid(NameScopeId scope_id) const
  129. -> std::pair<InstId, std::optional<Inst>> {
  130. if (!scope_id.is_valid()) {
  131. return {InstId::Invalid, std::nullopt};
  132. }
  133. auto inst_id = Get(scope_id).inst_id;
  134. if (!inst_id.is_valid()) {
  135. return {InstId::Invalid, std::nullopt};
  136. }
  137. return {inst_id, insts_->Get(inst_id)};
  138. }
  139. auto OutputYaml() const -> Yaml::OutputMapping {
  140. return values_.OutputYaml();
  141. }
  142. // Collects memory usage of members.
  143. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  144. -> void {
  145. mem_usage.Collect(label, values_);
  146. }
  147. private:
  148. InstStore* insts_;
  149. ValueStore<NameScopeId> values_;
  150. };
  151. } // namespace Carbon::SemIR
  152. #endif // CARBON_TOOLCHAIN_SEM_IR_NAME_SCOPE_H_