name_scope.h 5.6 KB

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