Files
carbon-lang/toolchain/semantics/semantics_handle_call_expression.cpp
T
Jon Ross-Perkins 67da700dd5 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
2023-08-23 22:51:23 +00:00

73 lines
2.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/semantics/semantics_context.h"
#include "toolchain/semantics/semantics_node.h"
namespace Carbon::Check {
auto HandleCallExpression(Context& context, ParseTree::Node parse_node)
-> bool {
auto refs_id = context.ParamOrArgEnd(
/*for_args=*/true, ParseNodeKind::CallExpressionStart);
// TODO: Convert to call expression.
auto [call_expr_parse_node, name_id] =
context.node_stack()
.PopWithParseNode<ParseNodeKind::CallExpressionStart>();
auto name_node = context.semantics_ir().GetNode(name_id);
if (name_node.kind() != SemIR::NodeKind::FunctionDeclaration) {
// TODO: Work on error.
context.TODO(parse_node, "Not a callable name");
context.node_stack().Push(parse_node, name_id);
return true;
}
auto function_id = name_node.GetAsFunctionDeclaration();
const auto& callable = context.semantics_ir().GetFunction(function_id);
CARBON_DIAGNOSTIC(NoMatchingCall, Error, "No matching callable was found.");
auto diagnostic =
context.emitter().Build(call_expr_parse_node, NoMatchingCall);
if (!context.ImplicitAsForArgs(refs_id, name_node.parse_node(),
callable.param_refs_id, &diagnostic)) {
diagnostic.Emit();
context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
return true;
}
CARBON_CHECK(context.ImplicitAsForArgs(refs_id, name_node.parse_node(),
callable.param_refs_id,
/*diagnostic=*/nullptr));
// TODO: Propagate return types from callable.
SemIR::TypeId type_id = callable.return_type_id;
// For functions with an implicit return type, set the return type to empty
// tuple type.
if (type_id == SemIR::TypeId::Invalid) {
type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
}
auto call_node_id = context.AddNode(SemIR::Node::Call::Make(
call_expr_parse_node, type_id, refs_id, function_id));
context.node_stack().Push(parse_node, call_node_id);
return true;
}
auto HandleCallExpressionComma(Context& context, ParseTree::Node /*parse_node*/)
-> bool {
context.ParamOrArgComma(/*for_args=*/true);
return true;
}
auto HandleCallExpressionStart(Context& context, ParseTree::Node parse_node)
-> bool {
auto name_id = context.node_stack().PopExpression();
context.node_stack().Push(parse_node, name_id);
context.ParamOrArgStart();
return true;
}
} // namespace Carbon::Check