declaration_name_stack.h 7.8 KB

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