mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Start treating function calls as initializing expressions instead of as value expressions. This required adding support for expression categories. Value bindings and temporary materialization conversions are created where necessary to transition between expression categories. For a function call with a return slot, we speculatively create a materialized temporary before the call and either commit to it or replace it with something else later, once we see how the function call expression is actually used. This change follows the direction suggested in #3133 for initializing expressions: depending on the return type of a function, the return value will either be initialized in-place or returned directly. This is visible in the semantics IR, which is a little unfortunate but is probably necessary as this is part of the semantics of the program. --------- Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
94 lines
3.9 KiB
C++
94 lines
3.9 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 "llvm/ADT/APSInt.h"
|
|
#include "toolchain/semantics/semantics_context.h"
|
|
#include "toolchain/semantics/semantics_node.h"
|
|
#include "toolchain/semantics/semantics_node_kind.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleIndexExpressionStart(Context& /*context*/,
|
|
ParseTree::Node /*parse_node*/) -> bool {
|
|
// Leave the expression on the stack for IndexExpression.
|
|
return true;
|
|
}
|
|
|
|
auto HandleIndexExpression(Context& context, ParseTree::Node parse_node)
|
|
-> bool {
|
|
CARBON_DIAGNOSTIC(OutOfBoundsAccess, Error,
|
|
"Index `{0}` is past the end of `{1}`.", llvm::APSInt,
|
|
std::string);
|
|
|
|
auto index_node_id = context.node_stack().PopExpression();
|
|
auto index_node = context.semantics_ir().GetNode(index_node_id);
|
|
auto name_node_id = context.node_stack().PopExpression();
|
|
name_node_id = context.MaterializeIfInitializing(name_node_id);
|
|
auto name_node = context.semantics_ir().GetNode(name_node_id);
|
|
auto name_type_id =
|
|
context.semantics_ir().GetTypeAllowBuiltinTypes(name_node.type_id());
|
|
auto name_type_node = context.semantics_ir().GetNode(name_type_id);
|
|
|
|
if (name_type_node.kind() == SemIR::NodeKind::ArrayType) {
|
|
auto [bound_id, type_id] = name_type_node.GetAsArrayType();
|
|
if (index_node.kind() == SemIR::NodeKind::IntegerLiteral) {
|
|
const auto& index_val = context.semantics_ir().GetIntegerLiteral(
|
|
index_node.GetAsIntegerLiteral());
|
|
if (index_val.uge(context.semantics_ir().GetArrayBoundValue(bound_id))) {
|
|
context.emitter().Emit(
|
|
parse_node, OutOfBoundsAccess,
|
|
llvm::APSInt(index_val, /*isUnsigned=*/true),
|
|
context.semantics_ir().StringifyType(name_node.type_id()));
|
|
} else {
|
|
context.AddNodeAndPush(
|
|
parse_node, SemIR::Node::ArrayIndex::Make(
|
|
parse_node, type_id, name_node_id, index_node_id));
|
|
return true;
|
|
}
|
|
} else if (context.ImplicitAsRequired(
|
|
index_node.parse_node(), index_node_id,
|
|
context.CanonicalizeType(
|
|
SemIR::NodeId::BuiltinIntegerType)) !=
|
|
SemIR::NodeId::BuiltinError) {
|
|
context.AddNodeAndPush(
|
|
parse_node, SemIR::Node::ArrayIndex::Make(
|
|
parse_node, type_id, name_node_id, index_node_id));
|
|
return true;
|
|
}
|
|
} else if (name_type_node.kind() == SemIR::NodeKind::TupleType) {
|
|
if (index_node.kind() == SemIR::NodeKind::IntegerLiteral) {
|
|
const auto& index_val = context.semantics_ir().GetIntegerLiteral(
|
|
index_node.GetAsIntegerLiteral());
|
|
auto type_block =
|
|
context.semantics_ir().GetTypeBlock(name_type_node.GetAsTupleType());
|
|
|
|
if (index_val.uge(static_cast<uint64_t>(type_block.size()))) {
|
|
context.emitter().Emit(
|
|
parse_node, OutOfBoundsAccess,
|
|
llvm::APSInt(index_val, /*isUnsigned=*/true),
|
|
context.semantics_ir().StringifyType(name_node.type_id()));
|
|
} else {
|
|
context.AddNodeAndPush(
|
|
parse_node, SemIR::Node::TupleIndex::Make(
|
|
parse_node, type_block[index_val.getZExtValue()],
|
|
name_node_id, index_node_id));
|
|
return true;
|
|
}
|
|
} else {
|
|
CARBON_DIAGNOSTIC(NondeterministicType, Error,
|
|
"Type cannot be determined at compile time.");
|
|
context.emitter().Emit(parse_node, NondeterministicType);
|
|
}
|
|
} else if (name_type_id != SemIR::NodeId::BuiltinError) {
|
|
CARBON_DIAGNOSTIC(InvalidIndexExpression, Error,
|
|
"Invalid index expression.");
|
|
context.emitter().Emit(parse_node, InvalidIndexExpression);
|
|
}
|
|
|
|
context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|