Split Semantics into Check and SemIR namespaces (#3138)

Splits IR files into SemIR, and logic files into Check. These will be
split into separate directories as part of a later move; the namespaces
are being done first in order to vet the switch, and hopefully make
conflicts a little easier to manage due to the substantial renames.

A lot of this is just automated removal of Semantics prefixes from
names, adding namespace references where needed. A few special-cases
are:

- SemanticsIR -> SemIR::File
- A few things were discussed, like Unit, CompileUnit, or CompiledUnit.
Unit was too vague for chandlerc, and I thought CompileUnit might lead
to incorrect inferences (CompilationUnit would be more precise, but
typically written as SemIR::CompilationUnit which is pretty long). File
seemed to be a short name that we could agree on.
- SemanticsIRFormatter -> SemIR::Formatter
- FormatSemanticsIR -> SemIR::FormatFile
- SemanticsFileTest -> CheckFileTest
- It remains in the Testing namespace, where just "FileTest" might be
too broad a name.
- SemanticsDeclarationNameStack::Context ->
Check::DeclarationNameStack::NameContext
  - This avoids a Check::Context name shadowing.

Changes check_internal.h to include ostream.h to improve finding of
Print/operator<< (otherwise it didn't compile).

This is part of #3070
This commit is contained in:
Jon Ross-Perkins
2023-08-23 22:51:23 +00:00
committed by GitHub
parent b2db2ca7cf
commit 67da700dd5
53 changed files with 1337 additions and 1466 deletions
@@ -4,19 +4,19 @@
#include "toolchain/semantics/semantics_context.h"
namespace Carbon {
namespace Carbon::Check {
// Build a FunctionDeclaration describing the signature of a function. This
// handles the common logic shared by function declaration syntax and function
// definition syntax.
static auto BuildFunctionDeclaration(SemanticsContext& context)
-> std::pair<SemanticsFunctionId, SemanticsNodeId> {
SemanticsTypeId return_type_id = SemanticsTypeId::Invalid;
static auto BuildFunctionDeclaration(Context& context)
-> std::pair<SemIR::FunctionId, SemIR::NodeId> {
SemIR::TypeId return_type_id = SemIR::TypeId::Invalid;
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
ParseNodeKind::ReturnType) {
return_type_id = context.node_stack().Pop<ParseNodeKind::ReturnType>();
}
SemanticsNodeBlockId param_refs_id =
SemIR::NodeBlockId param_refs_id =
context.node_stack().Pop<ParseNodeKind::ParameterList>();
auto name_context = context.declaration_name_stack().Pop();
auto fn_node = context.node_stack()
@@ -27,30 +27,28 @@ static auto BuildFunctionDeclaration(SemanticsContext& context)
// Add the callable.
auto function_id = context.semantics_ir().AddFunction(
{.name_id =
name_context.state ==
SemanticsDeclarationNameStack::Context::State::Unresolved
? name_context.unresolved_name_id
: SemanticsStringId(SemanticsStringId::InvalidIndex),
{.name_id = name_context.state ==
DeclarationNameStack::NameContext::State::Unresolved
? name_context.unresolved_name_id
: SemIR::StringId(SemIR::StringId::InvalidIndex),
.param_refs_id = param_refs_id,
.return_type_id = return_type_id,
.body_block_ids = {}});
auto decl_id = context.AddNode(
SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
SemIR::Node::FunctionDeclaration::Make(fn_node, function_id));
context.declaration_name_stack().AddNameToLookup(name_context, decl_id);
return {function_id, decl_id};
}
auto SemanticsHandleFunctionDeclaration(SemanticsContext& context,
ParseTree::Node /*parse_node*/)
auto HandleFunctionDeclaration(Context& context, ParseTree::Node /*parse_node*/)
-> bool {
BuildFunctionDeclaration(context);
return true;
}
auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
SemanticsFunctionId function_id =
auto HandleFunctionDefinition(Context& context, ParseTree::Node parse_node)
-> bool {
SemIR::FunctionId function_id =
context.node_stack().Pop<ParseNodeKind::FunctionDefinitionStart>();
// If the `}` of the function is reachable, reject if we need a return value
@@ -64,7 +62,7 @@ auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
"Missing `return` at end of function with declared return type.");
context.emitter().Emit(parse_node, MissingReturnStatement);
} else {
context.AddNode(SemanticsNode::Return::Make(parse_node));
context.AddNode(SemIR::Node::Return::Make(parse_node));
}
}
@@ -74,8 +72,7 @@ auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
return true;
}
auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
ParseTree::Node parse_node)
auto HandleFunctionDefinitionStart(Context& context, ParseTree::Node parse_node)
-> bool {
// Process the declaration portion of the function.
auto [function_id, decl_id] = BuildFunctionDeclaration(context);
@@ -99,8 +96,8 @@ auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
return true;
}
auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto HandleFunctionIntroducer(Context& context, ParseTree::Node parse_node)
-> bool {
// Push the bracketing node.
context.node_stack().Push(parse_node);
// A name should always follow.
@@ -108,8 +105,7 @@ auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
return true;
}
auto SemanticsHandleReturnType(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto HandleReturnType(Context& context, ParseTree::Node parse_node) -> bool {
// Propagate the type expression.
auto [type_parse_node, type_node_id] =
context.node_stack().PopExpressionWithParseNode();
@@ -118,4 +114,4 @@ auto SemanticsHandleReturnType(SemanticsContext& context,
return true;
}
} // namespace Carbon
} // namespace Carbon::Check