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 #2

Closed
wants to merge 1 commit into from

Conversation

amomchilov
Copy link

@amomchilov amomchilov commented Oct 31, 2024

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.

config.yml Show resolved Hide resolved
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
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.

Comment on lines +57 to +65
VALUE rbs_bases_void(VALUE location);
VALUE rbs_bases_top(VALUE location);
VALUE rbs_bases_self(VALUE location);
VALUE rbs_bases_nil(VALUE location);
VALUE rbs_bases_instance(VALUE location);
VALUE rbs_bases_class(VALUE location);
VALUE rbs_bases_bottom(VALUE location);
VALUE rbs_bases_bool(VALUE location);
VALUE rbs_bases_any(VALUE location);
Copy link
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 the 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.

VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility);
VALUE rbs_ast_members_method_definition_overload(VALUE annotations, VALUE method_type);
VALUE rbs_ast_members_mixin(VALUE klass, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment);
Copy link
Author

Choose a reason for hiding this comment

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

This was split into 3 functions:

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

config.yml Show resolved Hide resolved
@@ -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

Choose a reason for hiding this comment

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

Let's add a note to rename these methods later to a name that is easier to generate so we can avoid some of the complexity in template.rb

include/rbs/constants.h Outdated Show resolved Hide resolved
templates/ext/rbs_extension/constants.c.erb Show resolved Hide resolved
templates/ext/rbs_extension/constants.c.erb Show resolved Hide resolved
@name = yaml["name"].split("::").last
@c_function_name = if @full_name =~ /^RBS::Types/
name = @full_name.gsub("::", "_").underscore
name.gsub("_types_", "_") # TODO: should we rename these methods?
Copy link

Choose a reason for hiding this comment

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

Let's change this TODO to explain that we're going to do this next and remove this whole if statement

templates/template.rb Outdated Show resolved Hide resolved
templates/template.rb Outdated Show resolved Hide resolved
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]>
@amomchilov
Copy link
Author

Opened ruby#2098

@amomchilov amomchilov closed this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants