Skip to content
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

Add templating mechanism to automatically generate .c/.h files #2098

Merged
merged 2 commits into from
Nov 28, 2024

Conversation

amomchilov
Copy link
Contributor

This PR automates the creation of these pre-existing files:

  1. constants.h
  2. constants.c
  3. ruby_objs.h
  4. ruby_objs.c

In this initial PR, we aim to generate these files to match as closely to their original hand-authored versions. To facilitate this, we added some trickery to the ERB files, to get it as close as practical. These will be removed in a follow-up PR.

- name: RBS::Types::Alias
fields:
- name: name
c_name: typename # Temporary name to match existing C code. Will be removed in next PR.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The identifiers used in C don't always match those used in the Ruby API. Where necessary, we used this c_name: field to preserve the C code as it was.

Comment on lines 14 to +19
VALUE RBS_AST_Declarations;
VALUE RBS_AST_Directives;
VALUE RBS_AST_Members;
VALUE RBS_Parser;
VALUE RBS_Types;
VALUE RBS_Types_Bases;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are special-cased in the template. The rest of the file is code-genned, but in a different order than the original. All the same constants are still here, though.

@@ -963,25 +963,25 @@ static VALUE parse_simple(parserstate *state) {
return type;
}
case kBOOL:
return rbs_base_type(RBS_Types_Bases_Bool, rbs_location_current_token(state));
return rbs_bases_bool(rbs_location_current_token(state));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are just working names for now, we'll reconsider these once we decide what the new public C API looks like.

@@ -963,25 +963,25 @@ static VALUE parse_simple(parserstate *state) {
return type;
}
case kBOOL:
return rbs_base_type(RBS_Types_Bases_Bool, rbs_location_current_token(state));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All 9 of these types used to have their own Ruby classes, but shared this same "constructor" (rbs_base_type()).

We will eventually want 9 different C structs to model these (to keep a simple 1:1 correspondence to the Ruby classes), with 9 different constructor functions. We did that here, since it simplifies automation.

@@ -1836,14 +1839,18 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);

return rbs_ast_members_mixin(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Likewise, we split this one constructor function (rbs_ast_members_mixin()) into 3 new functions:

  1. rbs_ast_members_extend()
  2. rbs_ast_members_include()
  3. rbs_ast_members_prepend()

These will be modelled by 3 different C structs, to match the 3 different Ruby classes that exist for them.

rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);

return rbs_ast_members_variable(klass, name, type, location, comment);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same with rbs_ast_members_variable(), which gets split into:

  1. rbs_ast_members_instance_variable()
  2. rbs_ast_members_class_variable()
  3. rbs_ast_members_class_instance_variable()

default:
rbs_abort();
}

return rbs_ast_members_visibility(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Split into two:

  1. rbs_ast_members_public()
  2. rbs_ast_members_private()

@@ -2153,17 +2146,17 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
rbs_loc_add_optional_child(loc, rb_intern("ivar_name"), ivar_name_range);
rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);

return rbs_ast_members_attribute(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Split into 3 types and functions:

  1. rbs_ast_members_attr_reader
  2. rbs_ast_members_attr_writer
  3. rbs_ast_members_attr_accessor

Comment on lines +1224 to +1226
if (unchecked) {
rb_funcall(param, rb_intern("unchecked!"), 0);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was moved to here from rbs_ast_type_param(), since it doesn't fit the pattern used by the rest of the constructor functions. It's cleaner to just add it here, than to special-case it in generated code.

@soutaro
Copy link
Member

soutaro commented Nov 27, 2024

Thanks @amomchilov!

I hope #2101 fixes the windows compile issue.

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

🎉 Waiting for the build issue is fixed.

@soutaro soutaro added this to the RBS 3.7 milestone Nov 27, 2024
amomchilov and others added 2 commits November 27, 2024 17:36
Generate the c/h code for constant definitions and object initialization.

We make sure the Rake task builds the required templates so we can compile the extension.

Co-authored-by: Alexandre Terrasa <[email protected]>
@soutaro soutaro added this pull request to the merge queue Nov 28, 2024
Merged via the queue into ruby:master with commit 7c561d0 Nov 28, 2024
18 checks passed
@soutaro soutaro added the Released PRs already included in the released version label Dec 5, 2024
@amomchilov amomchilov mentioned this pull request Dec 6, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

2 participants