Skip to content

Fix C++ function params and return values printed in SemIR #5468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

ivanaivanovska
Copy link
Contributor

@ivanaivanovska ivanaivanovska commented May 13, 2025

Fix pattern_block_id and call_params_id for C++ function decl import, to get the correct SemIR printout for the C++ functions i.e.

  1. Fill in missing parameter info in the function decls:
// CHECK:STDOUT:   %foo.decl: %foo.type = fn_decl @foo [concrete = constants.%foo] {
// CHECK:STDOUT:     %a.patt: %pattern_type.2f8 = binding_pattern a [concrete]
// CHECK:STDOUT:     %a.param_patt: %pattern_type.2f8 = value_param_pattern %a.patt, call_param0 [concrete]
// CHECK:STDOUT:   } {
// CHECK:STDOUT:     %a.param: %i16 = value_param call_param0
// CHECK:STDOUT:     %.1: type = splice_block %i16 [concrete = constants.%i16] {
// CHECK:STDOUT:       %int_16: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
// CHECK:STDOUT:       %i16: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
// CHECK:STDOUT:     }
// CHECK:STDOUT:     %a: %i16 = bind_name a, %a.param
// CHECK:STDOUT:   }
  1. Print the params and return values:
// CHECK:STDOUT: fn @foo_short() -> %i16;
// CHECK:STDOUT: fn @foo(%a.param: %i16);

Closes #5449

@ivanaivanovska ivanaivanovska marked this pull request as ready for review June 3, 2025 09:11
@github-actions github-actions bot requested a review from chandlerc June 3, 2025 09:12
@ivanaivanovska
Copy link
Contributor Author

CC: @geoffromer @bricknerb

@bricknerb bricknerb self-requested a review June 3, 2025 09:42
Copy link
Contributor

@bricknerb bricknerb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract more logic from HandleAnyBindingPattern() so we can reuse it instead of doing it here, so we can more easily maintain this in case the logic changes?

Comment on lines +367 to +368
// Marks the start a region of insts, needed for the type
// expression created later with the call of EndSubpatternAsExpr.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Marks the start a region of insts, needed for the type
// expression created later with the call of EndSubpatternAsExpr.
// Mark the start of a region of insts, needed for the type expression
// created later with the call of `EndSubpatternAsExpr()`.


// TODO: Fix this once templates are supported.
bool is_template = false;
// TODO: Fix this once templates are supported.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: Fix this once templates are supported.
// TODO: Fix this once generics are supported.

Comment on lines +468 to +472
context.pattern_block_stack().Pop();
return SemIR::ErrorInst::InstId;
}
auto return_slot_pattern_id = GetReturnType(context, loc_id, clang_decl);
auto pattern_block_id = context.pattern_block_stack().Pop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can't rearrange the code so that Pop() is called in one place, consider using llvm::make_scope_exit() to make sure we call Pop().

if (SemIR::ErrorInst::InstId == return_slot_pattern_id) {
return SemIR::ErrorInst::InstId;
}

context.inst_block_stack().Push();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to move this up, at least above GetReturnType, but preferably to the beginning of the function, because this block needs to hold all the non-pattern insts emitted as part of the function declaration. Right now the type insts for the return type are getting lost.

(Those insts don't really belong here, but we don't have a better place for them yet, so this code should be consistent with the native-Carbon code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C++ function params and return values not printed in the SemIR anymore
3 participants