decl_name_stack.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_CHECK_DECL_NAME_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_DECL_NAME_STACK_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/check/scope_index.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. class Context;
  11. // Provides support and stacking for qualified declaration name handling.
  12. //
  13. // A qualified declaration name will consist of entries, which are `Name`s
  14. // optionally followed by generic parameter lists, such as `Vector(T:! type)`
  15. // in `fn Vector(T:! type).Clear();`, but parameter lists aren't supported yet.
  16. // Identifiers such as `Clear` will be resolved to a name if possible, for
  17. // example when declaring things that are in a non-generic type or namespace,
  18. // and are otherwise marked as an unresolved identifier.
  19. //
  20. // Unresolved identifiers are valid if and only if they are the last step of a
  21. // qualified name; all resolved qualifiers must resolve to an entity with
  22. // members, such as a namespace. Resolved identifiers in the last step will
  23. // occur for both out-of-line definitions and new declarations, depending on
  24. // context.
  25. //
  26. // For each name component that is processed and denotes a scope, the
  27. // corresponding scope is also entered. This is important for unqualified name
  28. // lookup both in the definition of the entity being declared, and for names
  29. // appearing later in the declaration name itself. For example, in:
  30. //
  31. // ```
  32. // fn ClassA.ClassB(T:! U).Fn() { var x: V; }
  33. // ```
  34. //
  35. // the lookup for `U` looks in `ClassA`, and the lookup for `V` looks in
  36. // `ClassA.ClassB` then in its enclosing scope `ClassA`. Scopes entered as part
  37. // of processing the name are exited when the name is popped from the stack.
  38. //
  39. // Example state transitions:
  40. //
  41. // ```
  42. // // Empty -> Unresolved, because `MyNamespace` is newly declared.
  43. // namespace MyNamespace;
  44. //
  45. // // Empty -> Resolved -> Unresolved, because `MyType` is newly declared.
  46. // class MyNamespace.MyType;
  47. //
  48. // // Empty -> Resolved -> Resolved, because `MyType` was forward declared.
  49. // class MyNamespace.MyType {
  50. // // Empty -> Unresolved, because `DoSomething` is newly declared.
  51. // fn DoSomething();
  52. // }
  53. //
  54. // // Empty -> Resolved -> Resolved -> ResolvedNonScope, because `DoSomething`
  55. // // is forward declared in `MyType`, but is not a scope itself.
  56. // fn MyNamespace.MyType.DoSomething() { ... }
  57. // ```
  58. class DeclNameStack {
  59. public:
  60. // Context for declaration name construction.
  61. struct NameContext {
  62. enum class State : int8_t {
  63. // A context that has not processed any parts of the qualifier.
  64. Empty,
  65. // An instruction ID has been resolved, whether through an identifier or
  66. // expression. This provided a new scope, such as a type.
  67. Resolved,
  68. // An instruction ID has been resolved, whether through an identifier or
  69. // expression. It did not provide a new scope, so must be the final part,
  70. // such as an out-of-line function definition.
  71. ResolvedNonScope,
  72. // An identifier didn't resolve.
  73. Unresolved,
  74. // The name has already been finished. This is not set in the name
  75. // returned by `FinishName`, but is used internally to track that
  76. // `FinishName` has already been called.
  77. Finished,
  78. // An error has occurred, such as an additional qualifier past an
  79. // unresolved name. No new diagnostics should be emitted.
  80. Error,
  81. };
  82. // Returns whether the name resolved to an existing entity.
  83. auto is_resolved() -> bool {
  84. return state != State::Unresolved && state != State::Empty;
  85. }
  86. // Returns the name_id for a new instruction. This is invalid when the name
  87. // resolved.
  88. auto name_id_for_new_inst() -> SemIR::NameId {
  89. return !is_resolved() ? unresolved_name_id : SemIR::NameId::Invalid;
  90. }
  91. // Returns the enclosing_scope_id for a new instruction. This is invalid
  92. // when the name resolved. Note this is distinct from the enclosing_scope of
  93. // the NameContext, which refers to the scope of the introducer rather than
  94. // the scope of the name.
  95. auto enclosing_scope_id_for_new_inst() -> SemIR::NameScopeId {
  96. return !is_resolved() ? target_scope_id : SemIR::NameScopeId::Invalid;
  97. }
  98. // The current scope when this name began. This is the scope that we will
  99. // return to at the end of the declaration.
  100. ScopeIndex enclosing_scope;
  101. State state = State::Empty;
  102. // Whether there have been qualifiers in the name.
  103. bool has_qualifiers = false;
  104. // The scope which qualified names are added to. For unqualified names in
  105. // an unnamed scope, this will be Invalid to indicate the current scope
  106. // should be used.
  107. SemIR::NameScopeId target_scope_id;
  108. // The last location ID used.
  109. SemIR::LocationId loc_id = SemIR::LocationId::Invalid;
  110. union {
  111. // The ID of a resolved qualifier, including both identifiers and
  112. // expressions. Invalid indicates resolution failed.
  113. SemIR::InstId resolved_inst_id = SemIR::InstId::Invalid;
  114. // The ID of an unresolved identifier.
  115. SemIR::NameId unresolved_name_id;
  116. };
  117. };
  118. explicit DeclNameStack(Context* context) : context_(context) {}
  119. // Pushes processing of a new declaration name, which will be used
  120. // contextually, and prepares to enter scopes for that name. To pop this
  121. // state, `FinishName` and `PopScope` must be called, in that order.
  122. auto PushScopeAndStartName() -> void;
  123. // Peeks the current target scope of the name on top of the stack. Note that
  124. // if we're still processing the name qualifiers, this can change before the
  125. // name is completed.
  126. auto PeekTargetScope() -> SemIR::NameScopeId {
  127. return decl_name_stack_.back().target_scope_id;
  128. }
  129. // Finishes the current declaration name processing, returning the final
  130. // context for adding the name to lookup.
  131. //
  132. // This also pops the final name instruction from the instruction stack,
  133. // which will be applied to the declaration name if appropriate.
  134. auto FinishName() -> NameContext;
  135. // Finishes the current declaration name processing for an `impl`, returning
  136. // the final context for adding the name to lookup.
  137. //
  138. // `impl`s don't actually have names, but want the rest of the name processing
  139. // logic such as building parameter scopes, so are a special case.
  140. auto FinishImplName() -> NameContext;
  141. // Pops the declaration name from the declaration name stack, and pops all
  142. // scopes that were entered as part of handling the declaration name. These
  143. // are the scopes corresponding to name qualifiers in the name, for example
  144. // the `A.B` in `fn A.B.F()`.
  145. //
  146. // This should be called at the end of the declaration.
  147. auto PopScope() -> void;
  148. // Creates and returns a name context corresponding to declaring an
  149. // unqualified name in the current context. This is suitable for adding to
  150. // name lookup in situations where a qualified name is not permitted, such as
  151. // a pattern binding.
  152. auto MakeUnqualifiedName(SemIR::LocationId loc_id, SemIR::NameId name_id)
  153. -> NameContext;
  154. // Applies a Name from the name stack to the top of the declaration name
  155. // stack. This will enter the scope corresponding to the name if the name
  156. // describes an existing scope, such as a namespace or a defined class.
  157. auto ApplyNameQualifier(SemIR::LocationId loc_id, SemIR::NameId name_id)
  158. -> void;
  159. // Adds a name to name lookup. Prints a diagnostic for name conflicts.
  160. auto AddNameToLookup(NameContext name_context, SemIR::InstId target_id)
  161. -> void;
  162. // Adds a name to name lookup, or returns the existing instruction if this
  163. // name has already been declared in this scope.
  164. auto LookupOrAddName(NameContext name_context, SemIR::InstId target_id)
  165. -> SemIR::InstId;
  166. private:
  167. // Returns a name context corresponding to an empty name.
  168. auto MakeEmptyNameContext() -> NameContext;
  169. // Applies a Name from the name stack to given name context.
  170. auto ApplyNameQualifierTo(NameContext& name_context, SemIR::LocationId loc_id,
  171. SemIR::NameId name_id, bool is_unqualified) -> void;
  172. // Returns true if the context is in a state where it can resolve qualifiers.
  173. // Updates name_context as needed.
  174. auto TryResolveQualifier(NameContext& name_context, SemIR::LocationId loc_id)
  175. -> bool;
  176. // Updates the scope on name_context as needed. This is called after
  177. // resolution is complete, whether for Name or expression. When updating for
  178. // an unqualified name, the resolution is noted without pushing scopes; it's
  179. // instead expected this will become a name conflict.
  180. auto UpdateScopeIfNeeded(NameContext& name_context, bool is_unqualified)
  181. -> void;
  182. // The linked context.
  183. Context* context_;
  184. // Provides nesting for construction.
  185. llvm::SmallVector<NameContext> decl_name_stack_;
  186. };
  187. } // namespace Carbon::Check
  188. #endif // CARBON_TOOLCHAIN_CHECK_DECL_NAME_STACK_H_