mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 02:08:12 +00:00
The return type is deduced from the function's `return` statements (currently required to all have the same type), or from its first `returned var`. Calls to generic functions with deduced return types are not yet supported. When a function has a deduced return type, we defer the return type portion of the callee pattern match until deduction, and during deduction we extend the decl block and the pattern block with the return-related instructions. Conversions for `return expr;` statements are deferred until the return type has been deduced. The design requires that we take the common type of the returned types as the deduced return type, so we can't in general perform any conversions for returned expressions until we reach the end of the function. This is accomplished by inserting a `splice_block` where the conversions will go, and later updating that instruction to contain the conversion. Assisted-by: Claude Opus 5.5 via Antigravity
68 lines
2.5 KiB
C++
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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_NAME_COMPONENT_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_NAME_COMPONENT_H_
|
|
|
|
#include "toolchain/check/node_stack.h"
|
|
#include "toolchain/parse/node_ids.h"
|
|
#include "toolchain/sem_ir/function.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
class Context;
|
|
|
|
// A component in a declaration name, such as `C[T: type](N: T)` in
|
|
// `fn C[T: type](N: T).F() {}`.
|
|
struct NameComponent {
|
|
// The name of the declaration.
|
|
Parse::NodeId name_loc_id;
|
|
SemIR::NameId name_id;
|
|
|
|
// Parse tree bounds for the parameters, including both implicit and explicit
|
|
// parameters. These will be compared to match between declaration and
|
|
// definition.
|
|
Parse::NodeId first_param_node_id;
|
|
Parse::NodeId last_param_node_id;
|
|
|
|
// The implicit parameter list.
|
|
Parse::NodeId implicit_params_loc_id;
|
|
SemIR::InstBlockId implicit_param_patterns_id;
|
|
|
|
// The explicit parameter list.
|
|
Parse::NodeId params_loc_id;
|
|
SemIR::InstBlockId param_patterns_id;
|
|
|
|
// The `Call` parameters of the entity, if it's a function (see the
|
|
// corresponding members of SemIR::FunctionFields and
|
|
// SemIR::EntityWithParamsBase).
|
|
SemIR::InstBlockId call_param_patterns_id;
|
|
SemIR::InstBlockId call_params_id;
|
|
SemIR::Function::CallParamIndexRanges param_ranges;
|
|
|
|
// The pattern block.
|
|
SemIR::InstBlockId pattern_block_id;
|
|
};
|
|
|
|
// Pops a name component from the node stack (and pattern block stack, if it has
|
|
// parameters or a return specifier). `return_pattern_id` is the pattern
|
|
// declared by the return specifier, if any. `has_return_specifier` indicates
|
|
// whether there is a return specifier; this can be true even if there is no
|
|
// return pattern, if the return type is deduced.
|
|
auto PopNameComponent(Context& context,
|
|
SemIR::InstId return_pattern_id = SemIR::InstId::None,
|
|
bool has_return_specifier = false) -> NameComponent;
|
|
|
|
// Equivalent to PopNameComponent, but also diagnoses if the name component has
|
|
// parameters. Sets `*diagnosed_params` to true when diagnosing parameters if
|
|
// it's not null.
|
|
auto PopNameComponentWithoutParams(Context& context, Lex::TokenKind introducer,
|
|
bool* diagnosed_params = nullptr)
|
|
-> NameComponent;
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_NAME_COMPONENT_H_
|