declaration_name_stack.h 5.6 KB

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