mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-30 09:05:03 +01:00
This was previously discussed at https://discord.com/channels/655572317891461132/655578254970716160/1209975051588210729. I'm initiating this mainly because we typically use "id" suffixes to indicate an `IdBase` being passed around and the non-id suffix of `parse_node` suggests at it carrying more data than it actually does. There used to be more reason for avoiding `node_id` because `SemIR::InstId` used to be named `NodeId`, but that's no longer necessary. As a consequence, I'd like to rename `parse_node` to more precisely reflect its type. In full, this is doing: ``` parse_node_kind -> node_kind parse_node -> node_id ParseNodeCategory -> NodeCategory ParseNodeKind -> NodeKind ParseNode -> NodeId ``` This is primarily in check and sem_ir, but with some `parse_node_kind` references in parse too. Pluralization is consistent with name forms on both sides, so that wasn't part of my replacements.
109 lines
4.4 KiB
C++
109 lines
4.4 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_CONVERT_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_CONVERT_H_
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/pending_block.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Description of the target of a conversion.
|
|
struct ConversionTarget {
|
|
enum Kind : int8_t {
|
|
// Convert to a value of type `type`.
|
|
Value,
|
|
// Convert to either a value or a reference of type `type`.
|
|
ValueOrRef,
|
|
// Convert for an explicit `as` cast. This allows any expression category
|
|
// as the result, and uses the `As` interface instead of the `ImplicitAs`
|
|
// interface.
|
|
// TODO: Use of an interface for conversions is not yet supported.
|
|
ExplicitAs,
|
|
// The result of the conversion is discarded. It can't be an initializing
|
|
// expression, but can be anything else.
|
|
Discarded,
|
|
// Convert to an initializer for the object denoted by `init_id`.
|
|
Initializer,
|
|
// Convert to an initializer for the object denoted by `init_id`,
|
|
// including a final destination store if needed.
|
|
FullInitializer,
|
|
Last = FullInitializer
|
|
};
|
|
// The kind of the target for this conversion.
|
|
Kind kind;
|
|
// The target type for the conversion.
|
|
SemIR::TypeId type_id;
|
|
// For an initializer, the object being initialized.
|
|
SemIR::InstId init_id = SemIR::InstId::Invalid;
|
|
// For an initializer, a block of pending instructions that are needed to
|
|
// form the value of `init_id`, and that can be discarded if no
|
|
// initialization is needed.
|
|
PendingBlock* init_block = nullptr;
|
|
|
|
// Are we converting this value into an initializer for an object?
|
|
auto is_initializer() const -> bool {
|
|
return kind == Initializer || kind == FullInitializer;
|
|
}
|
|
};
|
|
|
|
// Convert a value to another type and expression category.
|
|
auto Convert(Context& context, Parse::NodeId node_id, SemIR::InstId expr_id,
|
|
ConversionTarget target) -> SemIR::InstId;
|
|
|
|
// Performs initialization of `target_id` from `value_id`. Returns the
|
|
// possibly-converted initializing expression, which should be assigned to the
|
|
// target using a suitable node for the kind of initialization.
|
|
auto Initialize(Context& context, Parse::NodeId node_id,
|
|
SemIR::InstId target_id, SemIR::InstId value_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Convert the given expression to a value expression of the same type.
|
|
auto ConvertToValueExpr(Context& context, SemIR::InstId expr_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Convert the given expression to a value or reference expression of the same
|
|
// type.
|
|
auto ConvertToValueOrRefExpr(Context& context, SemIR::InstId expr_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Converts `expr_id` to a value expression of type `type_id`.
|
|
auto ConvertToValueOfType(Context& context, Parse::NodeId node_id,
|
|
SemIR::InstId expr_id, SemIR::TypeId type_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Convert the given expression to a value or reference expression of the given
|
|
// type.
|
|
auto ConvertToValueOrRefOfType(Context& context, Parse::NodeId node_id,
|
|
SemIR::InstId expr_id, SemIR::TypeId type_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Converts `value_id` to a value expression of type `bool`.
|
|
auto ConvertToBoolValue(Context& context, Parse::NodeId node_id,
|
|
SemIR::InstId value_id) -> SemIR::InstId;
|
|
|
|
// Converts `value_id` to type `type_id` for an `as` expression.
|
|
auto ConvertForExplicitAs(Context& context, Parse::NodeId as_node,
|
|
SemIR::InstId value_id, SemIR::TypeId type_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Implicitly converts a set of arguments to match the parameter types in a
|
|
// function call. Returns a block containing the converted implicit and explicit
|
|
// argument values.
|
|
auto ConvertCallArgs(Context& context, Parse::NodeId call_node_id,
|
|
SemIR::InstId self_id,
|
|
llvm::ArrayRef<SemIR::InstId> arg_refs,
|
|
SemIR::InstId return_storage_id, SemIR::InstId callee_id,
|
|
SemIR::InstBlockId implicit_param_refs_id,
|
|
SemIR::InstBlockId param_refs_id) -> SemIR::InstBlockId;
|
|
|
|
// Converts an expression for use as a type.
|
|
auto ExprAsType(Context& context, Parse::NodeId node_id, SemIR::InstId value_id)
|
|
-> SemIR::TypeId;
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_CONVERT_H_
|