semantics_declaration_name_stack.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_SEMANTICS_SEMANTICS_DECLARATION_NAME_STACK_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_DECLARATION_NAME_STACK_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/parser/parse_tree.h"
  8. #include "toolchain/semantics/semantics_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. // // New -> Unresolved, because `MyNamespace` is newly declared.
  31. // namespace MyNamespace;
  32. //
  33. // // New -> Resolved -> Unresolved, because `MyType` is newly declared.
  34. // class MyNamespace.MyType;
  35. //
  36. // // New -> Resolved -> Resolved, because `MyType` was forward declared.
  37. // class MyNamespace.MyType {
  38. // // New -> Unresolved, because `DoSomething` is newly declared.
  39. // fn DoSomething();
  40. // }
  41. //
  42. // // New -> 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 new context which has not processed any parts of the qualifier.
  52. New,
  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::New;
  67. // The scope which qualified names are added to. For unqualified names,
  68. // this will be Invalid to indicate the current scope should be used.
  69. SemIR::NameScopeId target_scope_id = SemIR::NameScopeId::Invalid;
  70. // The last parse node used.
  71. ParseTree::Node parse_node = ParseTree::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. SemIR::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. // Applies an expression from the node stack to the top of the declaration
  89. // name stack.
  90. auto ApplyExpressionQualifier(ParseTree::Node parse_node,
  91. SemIR::NodeId node_id) -> void;
  92. // Applies a Name from the node stack to the top of the declaration name
  93. // stack.
  94. auto ApplyNameQualifier(ParseTree::Node parse_node, SemIR::StringId name_id)
  95. -> void;
  96. // Adds a name to name lookup. Prints a diagnostic for name conflicts.
  97. auto AddNameToLookup(NameContext name_context, SemIR::NodeId target_id)
  98. -> void;
  99. private:
  100. // Returns true if the context is in a state where it can resolve qualifiers.
  101. // Updates name_context as needed.
  102. auto CanResolveQualifier(NameContext& name_context,
  103. ParseTree::Node parse_node) -> bool;
  104. // Updates the scope on name_context as needed. This is called after
  105. // resolution is complete, whether for Name or expression.
  106. auto UpdateScopeIfNeeded(NameContext& name_context) -> void;
  107. // The linked context.
  108. Context* context_;
  109. // Provides nesting for construction.
  110. llvm::SmallVector<NameContext> declaration_name_stack_;
  111. };
  112. } // namespace Carbon::Check
  113. #endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_DECLARATION_NAME_STACK_H_