Refactor declaration name context logic to its own class. (#2989)

This started with cleaning up the remaining Name/expression type punning
in the node stack, and grew. I'm factoring out a class because we've
previously expressed the desire to factor logic out of SemanticsContext
where possible, and this seemed like a reasonable cut.

NameExpression as the first node as a QualifiedExpression allows the
qualifier handling to consider Name in one less spot, an incremental
simplification. However, the additional complexity caused by this makes
me split ApplyNameQualifier/ApplyExpressionQualifier in order to avoid
repeat checks of the parse node's kind. The logic is still largely
shared, thus a couple helper functions. I think this is all fairly well
structured in the isolated class.

I can see that we may want to avoid passing SemanticsContext as an
argument in the future if it elides a step of lookup.
This commit is contained in:
Jon Ross-Perkins
2023-07-17 21:55:29 +00:00
committed by GitHub
parent 43065a1257
commit 446b0ce4ae
14 changed files with 399 additions and 294 deletions
@@ -21,7 +21,7 @@ static auto BuildFunctionDeclaration(SemanticsContext& context)
}
SemanticsNodeBlockId param_refs_id =
context.node_stack().Pop<ParseNodeKind::ParameterList>();
auto name_context = context.PopDeclarationName();
auto name_context = context.declaration_name_stack().Pop();
auto fn_node = context.node_stack()
.PopForSoloParseNode<ParseNodeKind::FunctionIntroducer>();
@@ -36,7 +36,7 @@ static auto BuildFunctionDeclaration(SemanticsContext& context)
.body_block_ids = {}});
auto decl_id = context.AddNode(
SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
context.AddNameToLookup(name_context, decl_id);
context.declaration_name_stack().AddNameToLookup(name_context, decl_id);
return {function_id, decl_id};
}
@@ -103,7 +103,7 @@ auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
// Push the bracketing node.
context.node_stack().Push(parse_node);
// A name should always follow.
context.PushDeclarationName();
context.declaration_name_stack().Push();
return true;
}