-
Notifications
You must be signed in to change notification settings - Fork 216
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
Conversation
- name: RBS::Types::Alias | ||
fields: | ||
- name: name | ||
c_name: typename # Temporary name to match existing C code. Will be removed in next PR. |
There was a problem hiding this comment.
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.
VALUE RBS_AST_Declarations; | ||
VALUE RBS_AST_Directives; | ||
VALUE RBS_AST_Members; | ||
VALUE RBS_Parser; | ||
VALUE RBS_Types; | ||
VALUE RBS_Types_Bases; |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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:
rbs_ast_members_extend()
rbs_ast_members_include()
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); |
There was a problem hiding this comment.
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:
rbs_ast_members_instance_variable()
rbs_ast_members_class_variable()
rbs_ast_members_class_instance_variable()
default: | ||
rbs_abort(); | ||
} | ||
|
||
return rbs_ast_members_visibility( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split into two:
rbs_ast_members_public()
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( |
There was a problem hiding this comment.
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:
rbs_ast_members_attr_reader
rbs_ast_members_attr_writer
rbs_ast_members_attr_accessor
if (unchecked) { | ||
rb_funcall(param, rb_intern("unchecked!"), 0); | ||
} |
There was a problem hiding this comment.
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.
b8d3eb3
to
a888013
Compare
3597db7
to
3831ac4
Compare
Thanks @amomchilov! I hope #2101 fixes the windows compile issue. |
There was a problem hiding this 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.
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]>
3831ac4
to
6ff4d34
Compare
This PR automates the creation of these pre-existing files:
constants.h
constants.c
ruby_objs.h
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.