Files
carbon-lang/toolchain/semantics/semantics_handle_call_expression.cpp
T
Jon Ross-Perkins b2084ea15d Shift Parser from 'Identifier' to 'Name' naming (#2947)
This PR renames parse nodes on a Name/NameExpression taxonomy. NameExpressions occur in a name context. The difference is that in non-expression contexts it's useful to return the identifier / string ID for adding to name lookup, whereas in expression contexts it's useful to return the resolved node ID for consistency with other expressions.

In the code, I do note SelfValueName is returned in the expression context: I'd expect this to change, as `self` in `[self: Self]` versus `self.Foo()` will probably be best handled similarly to the above. That means that, in the proposed taxonomy, both `SelfValueName` and `SelfValueNameExpression` will exist in order to assist semantics.

To contrast choices:

Original | Current | [zygoloid suggestion](https://discord.com/channels/655572317891461132/655578254970716160/1121581663399464970) | [This PR](https://discord.com/channels/655572317891461132/655578254970716160/1121814551789318215)
--- | --- | --- | ---
DeclaredName/DesignatedName | Identifier | NameComponent | Name
NameReference | NameReference | NameReference | NameExpression
SelfValueIdentifier | SelfValueIdentifier | SelfValueReference | SelfValueName
SelfTypeIdentifier | SelfTypeIdentiifer | SelfTypeReference | SelfTypeNameExpression
2023-06-26 15:49:57 -07:00

68 lines
2.5 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"
namespace Carbon {
auto SemanticsHandleCallExpression(SemanticsContext& 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<SemanticsNodeId>(
ParseNodeKind::CallExpressionStart);
auto name_node = context.semantics_ir().GetNode(name_id);
if (name_node.kind() != SemanticsNodeKind::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();
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, SemanticsNodeId::BuiltinInvalidType);
return true;
}
CARBON_CHECK(context.ImplicitAsForArgs(refs_id, name_node.parse_node(),
callable.param_refs_id,
/*diagnostic=*/nullptr));
// TODO: Propagate return types from callable.
auto call_node_id = context.AddNode(SemanticsNode::Call::Make(
call_expr_parse_node, callable.return_type_id, refs_id, function_id));
context.node_stack().Push(parse_node, call_node_id);
return true;
}
auto SemanticsHandleCallExpressionComma(SemanticsContext& context,
ParseTree::Node /*parse_node*/)
-> bool {
context.ParamOrArgComma(/*for_args=*/true);
return true;
}
auto SemanticsHandleCallExpressionStart(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto name_id =
context.node_stack().Pop<SemanticsNodeId>(ParseNodeKind::NameExpression);
context.node_stack().Push(parse_node, name_id);
context.ParamOrArgStart();
return true;
}
} // namespace Carbon