declaration_name_stack.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/node.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 either
  14. // Identifiers or full expressions. Expressions are expected to resolve to
  15. // types, such as how `fn Vector(i32).Clear() { ... }` uses the expression
  16. // `Vector(i32)` to indicate the type whose member is being declared.
  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. // Example state transitions:
  28. //
  29. // ```
  30. // // Empty -> Unresolved, because `MyNamespace` is newly declared.
  31. // namespace MyNamespace;
  32. //
  33. // // Empty -> Resolved -> Unresolved, because `MyType` is newly declared.
  34. // class MyNamespace.MyType;
  35. //
  36. // // Empty -> Resolved -> Resolved, because `MyType` was forward declared.
  37. // class MyNamespace.MyType {
  38. // // Empty -> Unresolved, because `DoSomething` is newly declared.
  39. // fn DoSomething();
  40. // }
  41. //
  42. // // Empty -> Resolved -> Resolved -> ResolvedNonScope, because `DoSomething`
  43. // // is forward declared in `MyType`, but is not a scope itself.
  44. // fn MyNamespace.MyType.DoSomething() { ... }
  45. // ```
  46. class DeclarationNameStack {
  47. public:
  48. // Context for declaration name construction.
  49. struct NameContext {
  50. enum class State : int8_t {
  51. // A context that has not processed any parts of the qualifier.
  52. Empty,
  53. // A node ID has been resolved, whether through an identifier or
  54. // expression. This provided a new scope, such as a type.
  55. Resolved,
  56. // A node ID has been resolved, whether through an identifier or
  57. // expression. It did not provide a new scope, so must be the final part,
  58. // such as an out-of-line function definition.
  59. ResolvedNonScope,
  60. // An identifier didn't resolve.
  61. Unresolved,
  62. // An error has occurred, such as an additional qualifier past an
  63. // unresolved name. No new diagnostics should be emitted.
  64. Error,
  65. };
  66. State state = State::Empty;
  67. // The scope which qualified names are added to. For unqualified names in
  68. // an unnamed scope, this will be Invalid to indicate the current scope
  69. // should be used.
  70. SemIR::NameScopeId target_scope_id;
  71. // The last parse node used.
  72. Parse::Node parse_node = Parse::Node::Invalid;
  73. union {
  74. // The ID of a resolved qualifier, including both identifiers and
  75. // expressions. Invalid indicates resolution failed.
  76. SemIR::NodeId resolved_node_id = SemIR::NodeId::Invalid;
  77. // The ID of an unresolved identifier.
  78. SemIR::StringId unresolved_name_id;
  79. };
  80. };
  81. explicit DeclarationNameStack(Context* context) : context_(context) {}
  82. // Pushes processing of a new declaration name, which will be used
  83. // contextually.
  84. auto Push() -> void;
  85. // Pops the current declaration name processing, returning the final context
  86. // for adding the name to lookup. This also pops the final name node from the
  87. // node stack, which will be applied to the declaration name if appropriate.
  88. auto Pop() -> NameContext;
  89. // Creates and returns a name context corresponding to declaring an
  90. // unqualified name in the current context. This is suitable for adding to
  91. // name lookup in situations where a qualified name is not permitted, such as
  92. // a pattern binding.
  93. auto MakeUnqualifiedName(Parse::Node parse_node, SemIR::StringId name_id)
  94. -> NameContext;
  95. // Applies an expression from the node stack to the top of the declaration
  96. // name stack.
  97. auto ApplyExpressionQualifier(Parse::Node parse_node, SemIR::NodeId node_id)
  98. -> void;
  99. // Applies a Name from the node stack to the top of the declaration name
  100. // stack.
  101. auto ApplyNameQualifier(Parse::Node parse_node, SemIR::StringId name_id)
  102. -> void;
  103. // Adds a name to name lookup. Prints a diagnostic for name conflicts.
  104. auto AddNameToLookup(NameContext name_context, SemIR::NodeId target_id)
  105. -> void;
  106. // Adds a name to name lookup, or returns the existing node if this name has
  107. // already been declared in this scope.
  108. auto LookupOrAddName(NameContext name_context, SemIR::NodeId target_id)
  109. -> SemIR::NodeId;
  110. private:
  111. // Returns a name context corresponding to an empty name.
  112. auto MakeEmptyNameContext() -> NameContext;
  113. // Applies a Name from the node stack to given name context.
  114. auto ApplyNameQualifierTo(NameContext& name_context, Parse::Node parse_node,
  115. SemIR::StringId name_id) -> void;
  116. // Returns true if the context is in a state where it can resolve qualifiers.
  117. // Updates name_context as needed.
  118. auto CanResolveQualifier(NameContext& name_context, Parse::Node parse_node)
  119. -> bool;
  120. // Updates the scope on name_context as needed. This is called after
  121. // resolution is complete, whether for Name or expression.
  122. auto UpdateScopeIfNeeded(NameContext& name_context) -> void;
  123. // The linked context.
  124. Context* context_;
  125. // Provides nesting for construction.
  126. llvm::SmallVector<NameContext> declaration_name_stack_;
  127. };
  128. } // namespace Carbon::Check
  129. #endif // CARBON_TOOLCHAIN_CHECK_DECLARATION_NAME_STACK_H_