decl_name_stack.h 8.1 KB

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