From fe24ebc0217e7e93d0688842ff981eb243ca2310 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 21 Dec 2023 16:12:16 -0800 Subject: [PATCH] Create a BindName for function parameters. (#3535) The goal here is to make the representation more uniform so that we can start adding different kinds of binding -- checked generic bindings and template bindings -- across both function parameters and local `let` declarations. With this change, the entry in the parameter list for the function is the name binding, not the Param itself, which has some ripple effects on consumers of that list that want to access the parameter rather than the binding. This is expected to change again when we start adding more of the pattern matching SemIR, but this seems good enough for now. --- toolchain/check/convert.cpp | 15 ++- toolchain/check/handle_binding_pattern.cpp | 31 ++++-- toolchain/check/handle_function.cpp | 2 +- toolchain/check/handle_name.cpp | 9 +- .../testdata/basics/raw_and_textual_ir.carbon | 96 ++++++++++--------- toolchain/check/testdata/basics/raw_ir.carbon | 96 ++++++++++--------- toolchain/lower/file_context.cpp | 51 +++++----- toolchain/sem_ir/file.cpp | 18 ++++ toolchain/sem_ir/file.h | 6 ++ 9 files changed, 182 insertions(+), 142 deletions(-) diff --git a/toolchain/check/convert.cpp b/toolchain/check/convert.cpp index dc0ef6c8eb05..5f3b99e0edb1 100644 --- a/toolchain/check/convert.cpp +++ b/toolchain/check/convert.cpp @@ -1146,16 +1146,15 @@ auto ConvertCallArgs(Context& context, Parse::NodeId call_parse_node, // Check implicit parameters. for (auto implicit_param_id : implicit_param_refs) { - auto pattern = context.insts().Get(implicit_param_id); - auto addr_pattern = pattern.TryAs(); - if (addr_pattern) { - pattern = context.insts().Get(addr_pattern->inner_id); - } - if (auto param = pattern.TryAs(); - param && param->name_id == SemIR::NameId::SelfValue) { + auto addr_pattern = + context.insts().TryGetAs(implicit_param_id); + auto param = SemIR::Function::GetParamFromParamRefId(context.sem_ir(), + implicit_param_id) + .second; + if (param.name_id == SemIR::NameId::SelfValue) { auto converted_self_id = ConvertSelf(context, call_parse_node, callee_parse_node, addr_pattern, - *param, self_id); + param, self_id); if (converted_self_id == SemIR::InstId::BuiltinError) { return SemIR::InstBlockId::Invalid; } diff --git a/toolchain/check/handle_binding_pattern.cpp b/toolchain/check/handle_binding_pattern.cpp index 1856e993bd6a..6b722ce27e51 100644 --- a/toolchain/check/handle_binding_pattern.cpp +++ b/toolchain/check/handle_binding_pattern.cpp @@ -12,7 +12,7 @@ namespace Carbon::Check { auto HandleAddress(Context& context, Parse::NodeId parse_node) -> bool { auto self_param_id = context.node_stack().Pop(); - auto self_param = context.insts().TryGetAs(self_param_id); + auto self_param = context.insts().TryGetAs(self_param_id); if (self_param && self_param->name_id == SemIR::NameId::SelfValue) { // TODO: The type of an `addr_pattern` should probably be the non-pointer // type, because that's the type that the pattern matches. @@ -44,6 +44,15 @@ auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool { // Every other kind of pattern binding has a name. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode(); + // Create the appropriate kind of binding for this pattern. + // + // TODO: Update this to create a generic or template binding as needed. + auto make_bind_name = [name_node = name_node, name_id = name_id]( + SemIR::TypeId type_id, + SemIR::InstId value_id) -> SemIR::Inst { + return SemIR::BindName{name_node, type_id, name_id, value_id}; + }; + // A `self` binding can only appear in an implicit parameter list. if (name_id == SemIR::NameId::SelfValue && !context.node_stack().PeekIs()) { @@ -76,6 +85,8 @@ auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool { SemIR::InstId value_id = SemIR::InstId::Invalid; SemIR::TypeId value_type_id = cast_type_id; if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) { + // TODO: Should we check this for the `var` as a whole, rather than for + // the name binding? CARBON_CHECK(!enclosing_class_decl) << "`returned var` at class scope"; value_id = CheckReturnedVar(context, context.node_stack().PeekParseNode(), @@ -100,8 +111,7 @@ auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool { value_id = context.AddInst( SemIR::VarStorage{name_node, value_type_id, name_id}); } - auto bind_id = context.AddInst( - SemIR::BindName{name_node, value_type_id, name_id, value_id}); + auto bind_id = context.AddInst(make_bind_name(value_type_id, value_id)); context.node_stack().Push(parse_node, bind_id); if (context_parse_node_kind == Parse::NodeKind::ReturnedModifier) { @@ -111,15 +121,17 @@ auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool { } case Parse::NodeKind::ImplicitParamListStart: - case Parse::NodeKind::TuplePatternStart: + case Parse::NodeKind::TuplePatternStart: { // Parameters can have incomplete types in a function declaration, but not // in a function definition. We don't know which kind we have here. // TODO: A tuple pattern can appear in other places than function // parameters. + auto param_id = + context.AddInst(SemIR::Param{name_node, cast_type_id, name_id}); context.AddInstAndPush(parse_node, - SemIR::Param{name_node, cast_type_id, name_id}); - // TODO: Create a `BindName` instruction. + make_bind_name(cast_type_id, param_id)); break; + } case Parse::NodeKind::LetIntroducer: cast_type_id = context.AsCompleteType(cast_type_id, [&] { @@ -134,10 +146,9 @@ auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool { // formed its initializer. // TODO: For general pattern parsing, we'll need to create a block to hold // the `let` pattern before we see the initializer. - context.node_stack().Push( - parse_node, - context.insts().AddInNoBlock(SemIR::BindName{ - name_node, cast_type_id, name_id, SemIR::InstId::Invalid})); + context.node_stack().Push(parse_node, + context.insts().AddInNoBlock(make_bind_name( + cast_type_id, SemIR::InstId::Invalid))); break; default: diff --git a/toolchain/check/handle_function.cpp b/toolchain/check/handle_function.cpp index 26ed32843e26..2c3fcc5f2b29 100644 --- a/toolchain/check/handle_function.cpp +++ b/toolchain/check/handle_function.cpp @@ -258,7 +258,7 @@ auto HandleFunctionDefinitionStart(Context& context, Parse::NodeId parse_node) context.sem_ir().StringifyType(param.type_id())); }); - if (auto fn_param = param.TryAs()) { + if (auto fn_param = param.TryAs()) { context.AddNameToLookup(fn_param->parse_node, fn_param->name_id, param_id); } else { diff --git a/toolchain/check/handle_name.cpp b/toolchain/check/handle_name.cpp index a4c1195f2f31..ad823ca0d362 100644 --- a/toolchain/check/handle_name.cpp +++ b/toolchain/check/handle_name.cpp @@ -79,12 +79,9 @@ static auto IsInstanceMethod(const SemIR::File& sem_ir, auto& function = sem_ir.functions().Get(function_id); for (auto param_id : sem_ir.inst_blocks().Get(function.implicit_param_refs_id)) { - auto inst = sem_ir.insts().Get(param_id); - if (auto addr = inst.TryAs()) { - inst = sem_ir.insts().Get(addr->inner_id); - } - if (auto param = inst.TryAs(); - param && param->name_id == SemIR::NameId::SelfValue) { + auto param = + SemIR::Function::GetParamFromParamRefId(sem_ir, param_id).second; + if (param.name_id == SemIR::NameId::SelfValue) { return true; } } diff --git a/toolchain/check/testdata/basics/raw_and_textual_ir.carbon b/toolchain/check/testdata/basics/raw_and_textual_ir.carbon index db22b5fac236..486f94b051e1 100644 --- a/toolchain/check/testdata/basics/raw_and_textual_ir.carbon +++ b/toolchain/check/testdata/basics/raw_and_textual_ir.carbon @@ -17,15 +17,15 @@ fn Foo(n: i32) -> (i32, i32, f64) { // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: cross_ref_irs_size: 1 // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, param_refs: block1, return_type: type4, return_slot: inst+6, body: [block4]} +// CHECK:STDOUT: function0: {name: name0, param_refs: block1, return_type: type4, return_slot: inst+7, body: [block4]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {inst: instNamespaceType, value_rep: {kind: copy, type: type0}} // CHECK:STDOUT: type1: {inst: instIntType, value_rep: {kind: copy, type: type1}} -// CHECK:STDOUT: type2: {inst: inst+2, value_rep: {kind: unknown, type: type}} +// CHECK:STDOUT: type2: {inst: inst+3, value_rep: {kind: unknown, type: type}} // CHECK:STDOUT: type3: {inst: instFloatType, value_rep: {kind: copy, type: type3}} -// CHECK:STDOUT: type4: {inst: inst+4, value_rep: {kind: pointer, type: type5}} -// CHECK:STDOUT: type5: {inst: inst+7, value_rep: {kind: copy, type: type5}} +// CHECK:STDOUT: type4: {inst: inst+5, value_rep: {kind: pointer, type: type5}} +// CHECK:STDOUT: type5: {inst: inst+8, value_rep: {kind: copy, type: type5}} // CHECK:STDOUT: type6: {inst: instFunctionType, value_rep: {kind: copy, type: type6}} // CHECK:STDOUT: type_blocks: // CHECK:STDOUT: typeBlock0: @@ -39,64 +39,66 @@ fn Foo(n: i32) -> (i32, i32, f64) { // CHECK:STDOUT: insts: // CHECK:STDOUT: inst+0: {kind: Namespace, arg0: name_scope0, type: type0} // CHECK:STDOUT: inst+1: {kind: Param, arg0: name1, type: type1} -// CHECK:STDOUT: inst+2: {kind: TupleType, arg0: typeBlock0, type: typeTypeType} -// CHECK:STDOUT: inst+3: {kind: TupleLiteral, arg0: block2, type: type2} -// CHECK:STDOUT: inst+4: {kind: TupleType, arg0: typeBlock1, type: typeTypeType} -// CHECK:STDOUT: inst+5: {kind: Converted, arg0: inst+3, arg1: inst+4, type: typeTypeType} -// CHECK:STDOUT: inst+6: {kind: VarStorage, arg0: nameReturnSlot, type: type4} -// CHECK:STDOUT: inst+7: {kind: PointerType, arg0: type4, type: typeTypeType} -// CHECK:STDOUT: inst+8: {kind: FunctionDecl, arg0: function0, type: type6} -// CHECK:STDOUT: inst+9: {kind: NameRef, arg0: name1, arg1: inst+1, type: type1} -// CHECK:STDOUT: inst+10: {kind: IntLiteral, arg0: int4, type: type1} -// CHECK:STDOUT: inst+11: {kind: RealLiteral, arg0: real0, type: type3} -// CHECK:STDOUT: inst+12: {kind: TupleLiteral, arg0: block5, type: type4} -// CHECK:STDOUT: inst+13: {kind: TupleAccess, arg0: inst+6, arg1: element0, type: type1} -// CHECK:STDOUT: inst+14: {kind: InitializeFrom, arg0: inst+9, arg1: inst+13, type: type1} -// CHECK:STDOUT: inst+15: {kind: TupleAccess, arg0: inst+6, arg1: element1, type: type1} -// CHECK:STDOUT: inst+16: {kind: InitializeFrom, arg0: inst+10, arg1: inst+15, type: type1} -// CHECK:STDOUT: inst+17: {kind: TupleAccess, arg0: inst+6, arg1: element2, type: type3} -// CHECK:STDOUT: inst+18: {kind: InitializeFrom, arg0: inst+11, arg1: inst+17, type: type3} -// CHECK:STDOUT: inst+19: {kind: TupleInit, arg0: block6, arg1: inst+6, type: type4} -// CHECK:STDOUT: inst+20: {kind: Converted, arg0: inst+12, arg1: inst+19, type: type4} -// CHECK:STDOUT: inst+21: {kind: ReturnExpr, arg0: inst+20} +// CHECK:STDOUT: inst+2: {kind: BindName, arg0: name1, arg1: inst+1, type: type1} +// CHECK:STDOUT: inst+3: {kind: TupleType, arg0: typeBlock0, type: typeTypeType} +// CHECK:STDOUT: inst+4: {kind: TupleLiteral, arg0: block2, type: type2} +// CHECK:STDOUT: inst+5: {kind: TupleType, arg0: typeBlock1, type: typeTypeType} +// CHECK:STDOUT: inst+6: {kind: Converted, arg0: inst+4, arg1: inst+5, type: typeTypeType} +// CHECK:STDOUT: inst+7: {kind: VarStorage, arg0: nameReturnSlot, type: type4} +// CHECK:STDOUT: inst+8: {kind: PointerType, arg0: type4, type: typeTypeType} +// CHECK:STDOUT: inst+9: {kind: FunctionDecl, arg0: function0, type: type6} +// CHECK:STDOUT: inst+10: {kind: NameRef, arg0: name1, arg1: inst+2, type: type1} +// CHECK:STDOUT: inst+11: {kind: IntLiteral, arg0: int4, type: type1} +// CHECK:STDOUT: inst+12: {kind: RealLiteral, arg0: real0, type: type3} +// CHECK:STDOUT: inst+13: {kind: TupleLiteral, arg0: block5, type: type4} +// CHECK:STDOUT: inst+14: {kind: TupleAccess, arg0: inst+7, arg1: element0, type: type1} +// CHECK:STDOUT: inst+15: {kind: InitializeFrom, arg0: inst+10, arg1: inst+14, type: type1} +// CHECK:STDOUT: inst+16: {kind: TupleAccess, arg0: inst+7, arg1: element1, type: type1} +// CHECK:STDOUT: inst+17: {kind: InitializeFrom, arg0: inst+11, arg1: inst+16, type: type1} +// CHECK:STDOUT: inst+18: {kind: TupleAccess, arg0: inst+7, arg1: element2, type: type3} +// CHECK:STDOUT: inst+19: {kind: InitializeFrom, arg0: inst+12, arg1: inst+18, type: type3} +// CHECK:STDOUT: inst+20: {kind: TupleInit, arg0: block6, arg1: inst+7, type: type4} +// CHECK:STDOUT: inst+21: {kind: Converted, arg0: inst+13, arg1: inst+20, type: type4} +// CHECK:STDOUT: inst+22: {kind: ReturnExpr, arg0: inst+21} // CHECK:STDOUT: inst_blocks: // CHECK:STDOUT: block0: {} // CHECK:STDOUT: block1: -// CHECK:STDOUT: 0: inst+1 +// CHECK:STDOUT: 0: inst+2 // CHECK:STDOUT: block2: // CHECK:STDOUT: 0: instIntType // CHECK:STDOUT: 1: instIntType // CHECK:STDOUT: 2: instFloatType // CHECK:STDOUT: block3: // CHECK:STDOUT: 0: inst+1 -// CHECK:STDOUT: 1: inst+3 -// CHECK:STDOUT: 2: inst+5 +// CHECK:STDOUT: 1: inst+2 +// CHECK:STDOUT: 2: inst+4 // CHECK:STDOUT: 3: inst+6 +// CHECK:STDOUT: 4: inst+7 // CHECK:STDOUT: block4: -// CHECK:STDOUT: 0: inst+9 -// CHECK:STDOUT: 1: inst+10 -// CHECK:STDOUT: 2: inst+11 -// CHECK:STDOUT: 3: inst+12 -// CHECK:STDOUT: 4: inst+13 -// CHECK:STDOUT: 5: inst+14 -// CHECK:STDOUT: 6: inst+15 -// CHECK:STDOUT: 7: inst+16 -// CHECK:STDOUT: 8: inst+17 -// CHECK:STDOUT: 9: inst+18 -// CHECK:STDOUT: 10: inst+19 -// CHECK:STDOUT: 11: inst+20 -// CHECK:STDOUT: 12: inst+21 +// CHECK:STDOUT: 0: inst+10 +// CHECK:STDOUT: 1: inst+11 +// CHECK:STDOUT: 2: inst+12 +// CHECK:STDOUT: 3: inst+13 +// CHECK:STDOUT: 4: inst+14 +// CHECK:STDOUT: 5: inst+15 +// CHECK:STDOUT: 6: inst+16 +// CHECK:STDOUT: 7: inst+17 +// CHECK:STDOUT: 8: inst+18 +// CHECK:STDOUT: 9: inst+19 +// CHECK:STDOUT: 10: inst+20 +// CHECK:STDOUT: 11: inst+21 +// CHECK:STDOUT: 12: inst+22 // CHECK:STDOUT: block5: -// CHECK:STDOUT: 0: inst+9 -// CHECK:STDOUT: 1: inst+10 -// CHECK:STDOUT: 2: inst+11 +// CHECK:STDOUT: 0: inst+10 +// CHECK:STDOUT: 1: inst+11 +// CHECK:STDOUT: 2: inst+12 // CHECK:STDOUT: block6: -// CHECK:STDOUT: 0: inst+14 -// CHECK:STDOUT: 1: inst+16 -// CHECK:STDOUT: 2: inst+18 +// CHECK:STDOUT: 0: inst+15 +// CHECK:STDOUT: 1: inst+17 +// CHECK:STDOUT: 2: inst+19 // CHECK:STDOUT: block7: // CHECK:STDOUT: 0: inst+0 -// CHECK:STDOUT: 1: inst+8 +// CHECK:STDOUT: 1: inst+9 // CHECK:STDOUT: ... // CHECK:STDOUT: // CHECK:STDOUT: --- raw_and_textual_ir.carbon diff --git a/toolchain/check/testdata/basics/raw_ir.carbon b/toolchain/check/testdata/basics/raw_ir.carbon index 51e9ebb412c1..5b85efc94e7d 100644 --- a/toolchain/check/testdata/basics/raw_ir.carbon +++ b/toolchain/check/testdata/basics/raw_ir.carbon @@ -17,15 +17,15 @@ fn Foo(n: i32) -> (i32, i32, f64) { // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: cross_ref_irs_size: 1 // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, param_refs: block1, return_type: type4, return_slot: inst+6, body: [block4]} +// CHECK:STDOUT: function0: {name: name0, param_refs: block1, return_type: type4, return_slot: inst+7, body: [block4]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {inst: instNamespaceType, value_rep: {kind: copy, type: type0}} // CHECK:STDOUT: type1: {inst: instIntType, value_rep: {kind: copy, type: type1}} -// CHECK:STDOUT: type2: {inst: inst+2, value_rep: {kind: unknown, type: type}} +// CHECK:STDOUT: type2: {inst: inst+3, value_rep: {kind: unknown, type: type}} // CHECK:STDOUT: type3: {inst: instFloatType, value_rep: {kind: copy, type: type3}} -// CHECK:STDOUT: type4: {inst: inst+4, value_rep: {kind: pointer, type: type5}} -// CHECK:STDOUT: type5: {inst: inst+7, value_rep: {kind: copy, type: type5}} +// CHECK:STDOUT: type4: {inst: inst+5, value_rep: {kind: pointer, type: type5}} +// CHECK:STDOUT: type5: {inst: inst+8, value_rep: {kind: copy, type: type5}} // CHECK:STDOUT: type6: {inst: instFunctionType, value_rep: {kind: copy, type: type6}} // CHECK:STDOUT: type_blocks: // CHECK:STDOUT: typeBlock0: @@ -39,62 +39,64 @@ fn Foo(n: i32) -> (i32, i32, f64) { // CHECK:STDOUT: insts: // CHECK:STDOUT: inst+0: {kind: Namespace, arg0: name_scope0, type: type0} // CHECK:STDOUT: inst+1: {kind: Param, arg0: name1, type: type1} -// CHECK:STDOUT: inst+2: {kind: TupleType, arg0: typeBlock0, type: typeTypeType} -// CHECK:STDOUT: inst+3: {kind: TupleLiteral, arg0: block2, type: type2} -// CHECK:STDOUT: inst+4: {kind: TupleType, arg0: typeBlock1, type: typeTypeType} -// CHECK:STDOUT: inst+5: {kind: Converted, arg0: inst+3, arg1: inst+4, type: typeTypeType} -// CHECK:STDOUT: inst+6: {kind: VarStorage, arg0: nameReturnSlot, type: type4} -// CHECK:STDOUT: inst+7: {kind: PointerType, arg0: type4, type: typeTypeType} -// CHECK:STDOUT: inst+8: {kind: FunctionDecl, arg0: function0, type: type6} -// CHECK:STDOUT: inst+9: {kind: NameRef, arg0: name1, arg1: inst+1, type: type1} -// CHECK:STDOUT: inst+10: {kind: IntLiteral, arg0: int4, type: type1} -// CHECK:STDOUT: inst+11: {kind: RealLiteral, arg0: real0, type: type3} -// CHECK:STDOUT: inst+12: {kind: TupleLiteral, arg0: block5, type: type4} -// CHECK:STDOUT: inst+13: {kind: TupleAccess, arg0: inst+6, arg1: element0, type: type1} -// CHECK:STDOUT: inst+14: {kind: InitializeFrom, arg0: inst+9, arg1: inst+13, type: type1} -// CHECK:STDOUT: inst+15: {kind: TupleAccess, arg0: inst+6, arg1: element1, type: type1} -// CHECK:STDOUT: inst+16: {kind: InitializeFrom, arg0: inst+10, arg1: inst+15, type: type1} -// CHECK:STDOUT: inst+17: {kind: TupleAccess, arg0: inst+6, arg1: element2, type: type3} -// CHECK:STDOUT: inst+18: {kind: InitializeFrom, arg0: inst+11, arg1: inst+17, type: type3} -// CHECK:STDOUT: inst+19: {kind: TupleInit, arg0: block6, arg1: inst+6, type: type4} -// CHECK:STDOUT: inst+20: {kind: Converted, arg0: inst+12, arg1: inst+19, type: type4} -// CHECK:STDOUT: inst+21: {kind: ReturnExpr, arg0: inst+20} +// CHECK:STDOUT: inst+2: {kind: BindName, arg0: name1, arg1: inst+1, type: type1} +// CHECK:STDOUT: inst+3: {kind: TupleType, arg0: typeBlock0, type: typeTypeType} +// CHECK:STDOUT: inst+4: {kind: TupleLiteral, arg0: block2, type: type2} +// CHECK:STDOUT: inst+5: {kind: TupleType, arg0: typeBlock1, type: typeTypeType} +// CHECK:STDOUT: inst+6: {kind: Converted, arg0: inst+4, arg1: inst+5, type: typeTypeType} +// CHECK:STDOUT: inst+7: {kind: VarStorage, arg0: nameReturnSlot, type: type4} +// CHECK:STDOUT: inst+8: {kind: PointerType, arg0: type4, type: typeTypeType} +// CHECK:STDOUT: inst+9: {kind: FunctionDecl, arg0: function0, type: type6} +// CHECK:STDOUT: inst+10: {kind: NameRef, arg0: name1, arg1: inst+2, type: type1} +// CHECK:STDOUT: inst+11: {kind: IntLiteral, arg0: int4, type: type1} +// CHECK:STDOUT: inst+12: {kind: RealLiteral, arg0: real0, type: type3} +// CHECK:STDOUT: inst+13: {kind: TupleLiteral, arg0: block5, type: type4} +// CHECK:STDOUT: inst+14: {kind: TupleAccess, arg0: inst+7, arg1: element0, type: type1} +// CHECK:STDOUT: inst+15: {kind: InitializeFrom, arg0: inst+10, arg1: inst+14, type: type1} +// CHECK:STDOUT: inst+16: {kind: TupleAccess, arg0: inst+7, arg1: element1, type: type1} +// CHECK:STDOUT: inst+17: {kind: InitializeFrom, arg0: inst+11, arg1: inst+16, type: type1} +// CHECK:STDOUT: inst+18: {kind: TupleAccess, arg0: inst+7, arg1: element2, type: type3} +// CHECK:STDOUT: inst+19: {kind: InitializeFrom, arg0: inst+12, arg1: inst+18, type: type3} +// CHECK:STDOUT: inst+20: {kind: TupleInit, arg0: block6, arg1: inst+7, type: type4} +// CHECK:STDOUT: inst+21: {kind: Converted, arg0: inst+13, arg1: inst+20, type: type4} +// CHECK:STDOUT: inst+22: {kind: ReturnExpr, arg0: inst+21} // CHECK:STDOUT: inst_blocks: // CHECK:STDOUT: block0: {} // CHECK:STDOUT: block1: -// CHECK:STDOUT: 0: inst+1 +// CHECK:STDOUT: 0: inst+2 // CHECK:STDOUT: block2: // CHECK:STDOUT: 0: instIntType // CHECK:STDOUT: 1: instIntType // CHECK:STDOUT: 2: instFloatType // CHECK:STDOUT: block3: // CHECK:STDOUT: 0: inst+1 -// CHECK:STDOUT: 1: inst+3 -// CHECK:STDOUT: 2: inst+5 +// CHECK:STDOUT: 1: inst+2 +// CHECK:STDOUT: 2: inst+4 // CHECK:STDOUT: 3: inst+6 +// CHECK:STDOUT: 4: inst+7 // CHECK:STDOUT: block4: -// CHECK:STDOUT: 0: inst+9 -// CHECK:STDOUT: 1: inst+10 -// CHECK:STDOUT: 2: inst+11 -// CHECK:STDOUT: 3: inst+12 -// CHECK:STDOUT: 4: inst+13 -// CHECK:STDOUT: 5: inst+14 -// CHECK:STDOUT: 6: inst+15 -// CHECK:STDOUT: 7: inst+16 -// CHECK:STDOUT: 8: inst+17 -// CHECK:STDOUT: 9: inst+18 -// CHECK:STDOUT: 10: inst+19 -// CHECK:STDOUT: 11: inst+20 -// CHECK:STDOUT: 12: inst+21 +// CHECK:STDOUT: 0: inst+10 +// CHECK:STDOUT: 1: inst+11 +// CHECK:STDOUT: 2: inst+12 +// CHECK:STDOUT: 3: inst+13 +// CHECK:STDOUT: 4: inst+14 +// CHECK:STDOUT: 5: inst+15 +// CHECK:STDOUT: 6: inst+16 +// CHECK:STDOUT: 7: inst+17 +// CHECK:STDOUT: 8: inst+18 +// CHECK:STDOUT: 9: inst+19 +// CHECK:STDOUT: 10: inst+20 +// CHECK:STDOUT: 11: inst+21 +// CHECK:STDOUT: 12: inst+22 // CHECK:STDOUT: block5: -// CHECK:STDOUT: 0: inst+9 -// CHECK:STDOUT: 1: inst+10 -// CHECK:STDOUT: 2: inst+11 +// CHECK:STDOUT: 0: inst+10 +// CHECK:STDOUT: 1: inst+11 +// CHECK:STDOUT: 2: inst+12 // CHECK:STDOUT: block6: -// CHECK:STDOUT: 0: inst+14 -// CHECK:STDOUT: 1: inst+16 -// CHECK:STDOUT: 2: inst+18 +// CHECK:STDOUT: 0: inst+15 +// CHECK:STDOUT: 1: inst+17 +// CHECK:STDOUT: 2: inst+19 // CHECK:STDOUT: block7: // CHECK:STDOUT: 0: inst+0 -// CHECK:STDOUT: 1: inst+8 +// CHECK:STDOUT: 1: inst+9 // CHECK:STDOUT: ... diff --git a/toolchain/lower/file_context.cpp b/toolchain/lower/file_context.cpp index 36f63301b3fb..a0f2f8eabe06 100644 --- a/toolchain/lower/file_context.cpp +++ b/toolchain/lower/file_context.cpp @@ -73,21 +73,6 @@ auto FileContext::GetGlobal(SemIR::InstId inst_id) -> llvm::Value* { CARBON_FATAL() << "Missing value: " << inst_id << " " << target; } -// Returns the parameter for the given instruction from a function parameter -// list. -static auto GetAsParam(const SemIR::File& sem_ir, SemIR::InstId pattern_id) - -> std::pair { - auto pattern = sem_ir.insts().Get(pattern_id); - - // Step over `addr`. - if (auto addr = pattern.TryAs()) { - pattern_id = addr->inner_id; - pattern = sem_ir.insts().Get(pattern_id); - } - - return {pattern_id, pattern.As()}; -} - auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id) -> llvm::Function* { const auto& function = sem_ir().functions().Get(function_id); @@ -118,7 +103,9 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id) } for (auto param_ref_id : llvm::concat(implicit_param_refs, param_refs)) { - auto param_type_id = GetAsParam(sem_ir(), param_ref_id).second.type_id; + auto param_type_id = + SemIR::Function::GetParamFromParamRefId(sem_ir(), param_ref_id) + .second.type_id; switch (auto value_rep = SemIR::GetValueRepr(sem_ir(), param_type_id); value_rep.kind) { case SemIR::ValueRepr::Unknown: @@ -169,7 +156,8 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id) arg.addAttr(llvm::Attribute::getWithStructRetType( llvm_context(), GetType(function.return_type_id))); } else { - name_id = GetAsParam(sem_ir(), inst_id).second.name_id; + name_id = SemIR::Function::GetParamFromParamRefId(sem_ir(), inst_id) + .second.name_id; } arg.setName(sem_ir().names().GetIRBaseName(name_id)); } @@ -206,16 +194,33 @@ auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id) } for (auto param_ref_id : llvm::concat(implicit_param_refs, param_refs)) { - auto [param_id, param] = GetAsParam(sem_ir(), param_ref_id); + auto [param_id, param] = + SemIR::Function::GetParamFromParamRefId(sem_ir(), param_ref_id); + + // Get the value of the parameter from the function argument. auto param_type_id = param.type_id; - if (SemIR::GetValueRepr(sem_ir(), param_type_id).kind == + llvm::Value* param_value = llvm::PoisonValue::get(GetType(param_type_id)); + if (SemIR::GetValueRepr(sem_ir(), param_type_id).kind != SemIR::ValueRepr::None) { - function_lowering.SetLocal( - param_id, llvm::PoisonValue::get(GetType(param_type_id))); - } else { - function_lowering.SetLocal(param_id, llvm_function->getArg(param_index)); + param_value = llvm_function->getArg(param_index); ++param_index; } + + // The value of the parameter is the value of the argument. + function_lowering.SetLocal(param_id, param_value); + + // Match the portion of the pattern corresponding to the parameter against + // the parameter value. For now this is always a single name binding, + // possibly wrapped in `addr`. + // + // TODO: Support general patterns here. + auto bind_name_id = param_ref_id; + if (auto addr = + sem_ir().insts().TryGetAs(param_ref_id)) { + bind_name_id = addr->inner_id; + } + CARBON_CHECK(sem_ir().insts().TryGetAs(bind_name_id)); + function_lowering.SetLocal(bind_name_id, param_value); } // Lower all blocks. diff --git a/toolchain/sem_ir/file.cpp b/toolchain/sem_ir/file.cpp index 2ec788e23d43..4eadc1cc98d6 100644 --- a/toolchain/sem_ir/file.cpp +++ b/toolchain/sem_ir/file.cpp @@ -13,9 +13,27 @@ #include "toolchain/sem_ir/ids.h" #include "toolchain/sem_ir/inst.h" #include "toolchain/sem_ir/inst_kind.h" +#include "toolchain/sem_ir/typed_insts.h" namespace Carbon::SemIR { +auto Function::GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id) + -> std::pair { + auto ref = sem_ir.insts().Get(param_ref_id); + + if (auto addr_pattern = ref.TryAs()) { + param_ref_id = addr_pattern->inner_id; + ref = sem_ir.insts().Get(param_ref_id); + } + + if (auto bind_name = ref.TryAs()) { + param_ref_id = bind_name->value_id; + ref = sem_ir.insts().Get(param_ref_id); + } + + return {param_ref_id, ref.As()}; +} + auto ValueRepr::Print(llvm::raw_ostream& out) const -> void { out << "{kind: "; switch (kind) { diff --git a/toolchain/sem_ir/file.h b/toolchain/sem_ir/file.h index f82a7605fa73..2929f5d33b5c 100644 --- a/toolchain/sem_ir/file.h +++ b/toolchain/sem_ir/file.h @@ -36,6 +36,12 @@ struct Function : public Printable { out << "}"; } + // Given a parameter reference instruction from `param_refs_id` or + // `implicit_param_refs_id`, returns the corresponding `Param` instruction + // and its ID. + static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id) + -> std::pair; + // The function name. NameId name_id; // The first declaration of the function. This is a FunctionDecl.