mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Switch AddInst struct init style. (#4012)
Trying to conform with #4009. Changes SemIR::LocIdAndInst construction to root out struct init cases with AddInst and related functions. I'm using templating of AddInst functions in order to avoid `AddInst(loc_id, InstName{...})` and instead have `AddInst<InstName>(loc_id, {...})` with I think similar readability results. There are a couple cases where inst construction is templated and so designated initializers couldn't be used, so this may be better for those in particular due to the extra type enforcement. This probably doesn't clean up every last case, but I was trying to get the bulk at once without bleeding over into less related changes. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
6e9d0b15c8
commit
5bb318cae6
@@ -44,30 +44,30 @@ auto HandleAlias(Context& context, Parse::AliasId /*node_id*/) -> bool {
|
||||
.enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(),
|
||||
.bind_index = SemIR::CompileTimeBindIndex::Invalid});
|
||||
|
||||
auto alias_id = SemIR::InstId::Invalid;
|
||||
auto alias_type_id = SemIR::TypeId::Invalid;
|
||||
auto alias_value_id = SemIR::InstId::Invalid;
|
||||
if (expr_id.is_builtin()) {
|
||||
// Type (`bool`) and value (`false`) literals provided by the builtin
|
||||
// structure should be turned into name references.
|
||||
// TODO: Look into handling `false`, this doesn't do it right now because it
|
||||
// sees a value instruction instead of a builtin.
|
||||
alias_id = context.AddInst(
|
||||
{name_context.loc_id,
|
||||
SemIR::BindAlias{context.insts().Get(expr_id).type_id(), bind_name_id,
|
||||
expr_id}});
|
||||
alias_type_id = context.insts().Get(expr_id).type_id();
|
||||
alias_value_id = expr_id;
|
||||
} else if (auto inst = context.insts().TryGetAs<SemIR::NameRef>(expr_id)) {
|
||||
// Pass through name references, albeit changing the name in use.
|
||||
alias_id = context.AddInst(
|
||||
{name_context.loc_id,
|
||||
SemIR::BindAlias{inst->type_id, bind_name_id, inst->value_id}});
|
||||
alias_type_id = inst->type_id;
|
||||
alias_value_id = inst->value_id;
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(AliasRequiresNameRef, Error,
|
||||
"Alias initializer must be a name reference.");
|
||||
context.emitter().Emit(expr_node, AliasRequiresNameRef);
|
||||
alias_id =
|
||||
context.AddInst({name_context.loc_id,
|
||||
SemIR::BindAlias{SemIR::TypeId::Error, bind_name_id,
|
||||
SemIR::InstId::BuiltinError}});
|
||||
alias_type_id = SemIR::TypeId::Error;
|
||||
alias_value_id = SemIR::InstId::BuiltinError;
|
||||
}
|
||||
auto alias_id = context.AddInst<SemIR::BindAlias>(
|
||||
name_context.loc_id, {.type_id = alias_type_id,
|
||||
.bind_name_id = bind_name_id,
|
||||
.value_id = alias_value_id});
|
||||
|
||||
// Add the name of the binding to the current scope.
|
||||
context.decl_name_stack().PopScope();
|
||||
|
||||
Reference in New Issue
Block a user