diff --git a/Gemfile-39 b/Gemfile-39 new file mode 100644 index 0000000000..49f3d9a86e --- /dev/null +++ b/Gemfile-39 @@ -0,0 +1,4 @@ +source 'https://rubygems.org' +gem 'rbs', "~> 3.9" +gem "benchmark-ips" +gem "csv" \ No newline at end of file diff --git a/Gemfile-39.lock b/Gemfile-39.lock new file mode 100644 index 0000000000..9b9846decd --- /dev/null +++ b/Gemfile-39.lock @@ -0,0 +1,20 @@ +GEM + remote: https://rubygems.org/ + specs: + benchmark-ips (2.14.0) + csv (3.3.5) + logger (1.7.0) + rbs (3.9.4) + logger + +PLATFORMS + arm64-darwin-23 + ruby + +DEPENDENCIES + benchmark-ips + csv + rbs (~> 3.9) + +BUNDLED WITH + 2.6.3 diff --git a/Rakefile b/Rakefile index 2a9541de83..f3f0777140 100644 --- a/Rakefile +++ b/Rakefile @@ -537,3 +537,11 @@ task :compile_c99 do ensure ENV.delete("TEST_NO_C23") end + + +task :prepare_bench do + ENV.delete("DEBUG") + Rake::Task[:"clobber"].invoke + Rake::Task[:"templates"].invoke + Rake::Task[:"compile"].invoke +end \ No newline at end of file diff --git a/benchmark.rb b/benchmark.rb new file mode 100644 index 0000000000..1dffe61e55 --- /dev/null +++ b/benchmark.rb @@ -0,0 +1,11 @@ +require "bundler/setup" + +require 'rbs' +require 'benchmark/ips' + +sig = Pathname('/Users/soutaro/Downloads/activerecord-generated.rbs').read +Benchmark.ips do |x| + x.report("rbs v#{RBS::VERSION} parse activerecord-generated.rbs") do + RBS::Parser.parse_signature(sig) + end +end diff --git a/benchmarks.rb b/benchmarks.rb new file mode 100644 index 0000000000..43e2066184 --- /dev/null +++ b/benchmarks.rb @@ -0,0 +1,35 @@ +require "rbs" +require "benchmark/ips" +require "csv" + +results = [] + +total = ARGV.size +ARGV.each_with_index do |file, index| + GC.start + + STDERR.puts "#{index}/#{total}\tBenchmarking with #{file}..." + content = File.read(file) + + benchmark = Benchmark.ips do |x| + x.report(file) do + RBS::Parser.parse_signature(content) + end + + x.quiet = true + end + + results << { + file: file, + size: content.bytesize, + ips: benchmark.entries[0].ips, + sd: benchmark.entries[0].ips_sd + } +end + +puts CSV.generate {|csv| + csv << ["File", "Size", "IPS", "SD"] + results.each do |result| + csv << [result[:file], result[:size], result[:ips], result[:sd]] + end +} diff --git a/benchmarks2.rb b/benchmarks2.rb new file mode 100644 index 0000000000..170cd4188b --- /dev/null +++ b/benchmarks2.rb @@ -0,0 +1,22 @@ +require "rbs" +require "benchmark/ips" +require "csv" +require "pathname" + +files = {} +ARGV.each do |file| + content = File.read(file) + files[file] = RBS::Buffer.new(content: content, name: Pathname(file)) +end + +puts "Benchmarking parsing #{files.size} files..." + +Benchmark.ips do |x| + x.report("parsing") do + files.each do |file, content| + RBS::Parser.parse_signature(content) + end + end + + x.compare! +end diff --git a/benchmarks3.rb b/benchmarks3.rb new file mode 100644 index 0000000000..53c38e28a2 --- /dev/null +++ b/benchmarks3.rb @@ -0,0 +1,20 @@ +require "rbs" +require "benchmark/ips" +require "csv" +require "pathname" + +files = {} +ARGV.each do |file| + content = File.read(file) + files[file] = RBS::Buffer.new(content: content, name: Pathname(file)) +end + +puts "Benchmarking parsing #{files.size} files..." + +Benchmark.ips do |x| + x.report("batch parsing") do + RBS::Parser._parse_signatures(files) + end + + x.compare! +end diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 37d0ea179e..12ebf1f073 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -11,6 +11,9 @@ #include "rbs_string_bridging.h" #include "legacy_location.h" +VALUE EMPTY_ARRAY; +VALUE EMPTY_HASH; + #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) { @@ -22,16 +25,32 @@ rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *co } VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { + if (!list->head) { + return EMPTY_ARRAY; + } + VALUE ruby_array = rb_ary_new(); + VALUE *values = ALLOCA_N(VALUE, list->length); - for (rbs_node_list_node_t *n = list->head; n != NULL; n = n->next) { - rb_ary_push(ruby_array, rbs_struct_to_ruby_value(ctx, n->node)); + size_t i = 0; + for (rbs_node_list_node_t *n = list->head; n != NULL; n = n->next, i++) { + values[i] = rbs_struct_to_ruby_value(ctx, n->node); } + rb_ary_cat(ruby_array, values, list->length); + return ruby_array; } VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) { + if (rbs_hash->length > 0) { + printf("Size of hash: %zu\n", rbs_hash->length); + } + + if (!rbs_hash->head) { + return EMPTY_HASH; + } + VALUE ruby_hash = rb_hash_new(); for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) { @@ -60,12 +79,12 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so } VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) { - VALUE ruby_array = rb_ary_new(); - if (list == NULL) { - return ruby_array; + return EMPTY_ARRAY; } + VALUE ruby_array = rb_ary_new(); + for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) { rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc)); } @@ -83,264 +102,1017 @@ VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_locatio rb_class_new_instance(argc, argv, receiver) #endif +VALUE rbs_ast_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Annotation, + 1, + &h + ); +} + +VALUE rbs_ast_comment_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_comment_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Comment, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_class_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_class_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Class, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_class_super_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_class_super_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Class_Super, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_class_alias_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_class_alias_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_ClassAlias, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_constant_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_constant_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Constant, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_global_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_global_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Global, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_interface_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_interface_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Interface, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_module_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_module_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(ctx, node->self_types)); + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Module, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_module_self_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_module_self_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Module_Self, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_module_alias_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_module_alias_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_ModuleAlias, + 1, + &h + ); +} + +VALUE rbs_ast_declarations_type_alias_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_declarations_type_alias_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_TypeAlias, + 1, + &h + ); +} + +VALUE rbs_ast_directives_use_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_directives_use_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(ctx, node->clauses)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Directives_Use, + 1, + &h + ); +} + +VALUE rbs_ast_directives_use_single_clause_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_directives_use_single_clause_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_AST_Directives_Use_SingleClause, + 1, + &h + ); +} + +VALUE rbs_ast_directives_use_wildcard_clause_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_directives_use_wildcard_clause_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace + + return CLASS_NEW_INSTANCE( + RBS_AST_Directives_Use_WildcardClause, + 1, + &h + ); +} + +VALUE rbs_ast_members_alias_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_alias_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Alias, + 1, + &h + ); +} + +VALUE rbs_ast_members_attr_accessor_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_attr_accessor_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_AttrAccessor, + 1, + &h + ); +} + +VALUE rbs_ast_members_attr_reader_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_attr_reader_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_AttrReader, + 1, + &h + ); +} + +VALUE rbs_ast_members_attr_writer_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_attr_writer_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_AttrWriter, + 1, + &h + ); +} + +VALUE rbs_ast_members_class_instance_variable_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_class_instance_variable_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_ClassInstanceVariable, + 1, + &h + ); +} + +VALUE rbs_ast_members_class_variable_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_class_variable_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_ClassVariable, + 1, + &h + ); +} + +VALUE rbs_ast_members_extend_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_extend_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Extend, + 1, + &h + ); +} + +VALUE rbs_ast_members_include_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_include_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Include, + 1, + &h + ); +} + +VALUE rbs_ast_members_instance_variable_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_instance_variable_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_InstanceVariable, + 1, + &h + ); +} + +VALUE rbs_ast_members_method_definition_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_method_definition_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("overloading")), node->overloading ? Qtrue : Qfalse); + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_MethodDefinition, + 1, + &h + ); +} + +VALUE rbs_ast_members_method_definition_overload_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_method_definition_overload_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->method_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_MethodDefinition_Overload, + 1, + &h + ); +} + +VALUE rbs_ast_members_prepend_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_prepend_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Prepend, + 1, + &h + ); +} + +VALUE rbs_ast_members_private_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_private_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Private, + 1, + &h + ); +} + +VALUE rbs_ast_members_public_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_members_public_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Public, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_class_alias_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_class_alias_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("keyword_location")), rbs_loc_to_ruby_location(ctx, node->keyword_location)); + rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type_name_location")), rbs_loc_to_ruby_location(ctx, node->type_name_location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_ClassAliasAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_colon_method_type_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_colon_method_type_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->method_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_instance_variable_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_instance_variable_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name_location")), rbs_loc_to_ruby_location(ctx, node->ivar_name_location)); + rb_hash_aset(h, ID2SYM(rb_intern("colon_location")), rbs_loc_to_ruby_location(ctx, node->colon_location)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_InstanceVariableAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_method_types_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_method_types_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); + rb_hash_aset(h, ID2SYM(rb_intern("vertical_bar_locations")), rbs_location_list_to_ruby_array(ctx, node->vertical_bar_locations)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_MethodTypesAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_module_alias_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_module_alias_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("keyword_location")), rbs_loc_to_ruby_location(ctx, node->keyword_location)); + rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("type_name_location")), rbs_loc_to_ruby_location(ctx, node->type_name_location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_ModuleAliasAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_node_type_assertion_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_node_type_assertion_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_NodeTypeAssertion, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_return_type_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_return_type_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("return_location")), rbs_loc_to_ruby_location(ctx, node->return_location)); + rb_hash_aset(h, ID2SYM(rb_intern("colon_location")), rbs_loc_to_ruby_location(ctx, node->colon_location)); + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_ReturnTypeAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_skip_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_skip_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("skip_location")), rbs_loc_to_ruby_location(ctx, node->skip_location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_SkipAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_ruby_annotations_type_application_annotation_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_ruby_annotations_type_application_annotation_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); + rb_hash_aset(h, ID2SYM(rb_intern("type_args")), rbs_node_list_to_ruby_array(ctx, node->type_args)); + rb_hash_aset(h, ID2SYM(rb_intern("close_bracket_location")), rbs_loc_to_ruby_location(ctx, node->close_bracket_location)); + rb_hash_aset(h, ID2SYM(rb_intern("comma_locations")), rbs_location_list_to_ruby_array(ctx, node->comma_locations)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Ruby_Annotations_TypeApplicationAnnotation, + 1, + &h + ); +} + +VALUE rbs_ast_type_param_t_to_ruby_value(rbs_translation_context_t ctx, rbs_ast_type_param_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("variance")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->variance)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->upper_bound)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("lower_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->lower_bound)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->default_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("unchecked")), node->unchecked ? Qtrue : Qfalse); + + return CLASS_NEW_INSTANCE( + RBS_AST_TypeParam, + 1, + &h + ); +} + +VALUE rbs_method_type_t_to_ruby_value(rbs_translation_context_t ctx, rbs_method_type_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + return CLASS_NEW_INSTANCE( + RBS_MethodType, + 1, + &h + ); +} + +VALUE rbs_namespace_t_to_ruby_value(rbs_translation_context_t ctx, rbs_namespace_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("path")), rbs_node_list_to_ruby_array(ctx, node->path)); + rb_hash_aset(h, ID2SYM(rb_intern("absolute")), node->absolute ? Qtrue : Qfalse); + + return CLASS_NEW_INSTANCE( + RBS_Namespace, + 1, + &h + ); +} + +VALUE rbs_type_name_t_to_ruby_value(rbs_translation_context_t ctx, rbs_type_name_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_TypeName, + 1, + &h + ); +} + +VALUE rbs_types_alias_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_alias_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Alias, + 1, + &h + ); +} + +VALUE rbs_types_bases_any_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_any_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo ? Qtrue : Qfalse); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Any, + 1, + &h + ); +} + +VALUE rbs_types_bases_bool_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_bool_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Bool, + 1, + &h + ); +} + +VALUE rbs_types_bases_bottom_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_bottom_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Bottom, + 1, + &h + ); +} + +VALUE rbs_types_bases_class_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_class_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Class, + 1, + &h + ); +} + +VALUE rbs_types_bases_instance_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_instance_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Instance, + 1, + &h + ); +} + +VALUE rbs_types_bases_nil_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_nil_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Nil, + 1, + &h + ); +} + +VALUE rbs_types_bases_self_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_self_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Self, + 1, + &h + ); +} + +VALUE rbs_types_bases_top_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_top_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Top, + 1, + &h + ); +} + +VALUE rbs_types_bases_void_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_bases_void_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Void, + 1, + &h + ); +} + +VALUE rbs_types_block_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_block_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("required")), node->required ? Qtrue : Qfalse); + rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Block, + 1, + &h + ); +} + +VALUE rbs_types_class_instance_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_class_instance_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + + return CLASS_NEW_INSTANCE( + RBS_Types_ClassInstance, + 1, + &h + ); +} + +VALUE rbs_types_class_singleton_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_class_singleton_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + + return CLASS_NEW_INSTANCE( + RBS_Types_ClassSingleton, + 1, + &h + ); +} + +VALUE rbs_types_function_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_function_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("required_positionals")), rbs_node_list_to_ruby_array(ctx, node->required_positionals)); + rb_hash_aset(h, ID2SYM(rb_intern("optional_positionals")), rbs_node_list_to_ruby_array(ctx, node->optional_positionals)); + rb_hash_aset(h, ID2SYM(rb_intern("rest_positionals")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rest_positionals)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("trailing_positionals")), rbs_node_list_to_ruby_array(ctx, node->trailing_positionals)); + rb_hash_aset(h, ID2SYM(rb_intern("required_keywords")), rbs_hash_to_ruby_hash(ctx, node->required_keywords)); + rb_hash_aset(h, ID2SYM(rb_intern("optional_keywords")), rbs_hash_to_ruby_hash(ctx, node->optional_keywords)); + rb_hash_aset(h, ID2SYM(rb_intern("rest_keywords")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rest_keywords)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Function, + 1, + &h + ); +} + +VALUE rbs_types_function_param_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_function_param_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_Types_Function_Param, + 1, + &h + ); +} + +VALUE rbs_types_interface_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_interface_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Interface, + 1, + &h + ); +} + +VALUE rbs_types_intersection_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_intersection_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Intersection, + 1, + &h + ); +} + +VALUE rbs_types_literal_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_literal_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("literal")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->literal)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Literal, + 1, + &h + ); +} + +VALUE rbs_types_optional_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_optional_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Optional, + 1, + &h + ); +} + +VALUE rbs_types_proc_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_proc_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block + rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Proc, + 1, + &h + ); +} + +VALUE rbs_types_record_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_record_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), rbs_hash_to_ruby_hash(ctx, node->all_fields)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Record, + 1, + &h + ); +} + +VALUE rbs_types_tuple_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_tuple_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Tuple, + 1, + &h + ); +} + +VALUE rbs_types_union_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_union_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Union, + 1, + &h + ); +} + +VALUE rbs_types_untyped_function_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_untyped_function_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_UntypedFunction, + 1, + &h + ); +} + +VALUE rbs_types_variable_t_to_ruby_value(rbs_translation_context_t ctx, rbs_types_variable_t *node) { + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_Types_Variable, + 1, + &h + ); +} + VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instance) { if (instance == NULL) return Qnil; switch (instance->type) { case RBS_AST_ANNOTATION: { rbs_ast_annotation_t *node = (rbs_ast_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Annotation, - 1, - &h - ); + return rbs_ast_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_BOOL: { return ((rbs_ast_bool_t *) instance)->value ? Qtrue : Qfalse; } case RBS_AST_COMMENT: { rbs_ast_comment_t *node = (rbs_ast_comment_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Comment, - 1, - &h - ); + return rbs_ast_comment_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_CLASS: { rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super - rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) - ); - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Class, - 1, - &h - ); + return rbs_ast_declarations_class_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_CLASS_SUPER: { rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Class_Super, - 1, - &h - ); + return rbs_ast_declarations_class_super_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_CLASS_ALIAS: { rbs_ast_declarations_class_alias_t *node = (rbs_ast_declarations_class_alias_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_ClassAlias, - 1, - &h - ); + return rbs_ast_declarations_class_alias_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_CONSTANT: { rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Constant, - 1, - &h - ); + return rbs_ast_declarations_constant_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_GLOBAL: { rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Global, - 1, - &h - ); + return rbs_ast_declarations_global_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_INTERFACE: { rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) - ); - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Interface, - 1, - &h - ); + return rbs_ast_declarations_interface_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_MODULE: { rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(ctx, node->self_types)); - rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) - ); - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Module, - 1, - &h - ); + return rbs_ast_declarations_module_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_MODULE_SELF: { rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Module_Self, - 1, - &h - ); + return rbs_ast_declarations_module_self_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_MODULE_ALIAS: { rbs_ast_declarations_module_alias_t *node = (rbs_ast_declarations_module_alias_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_ModuleAlias, - 1, - &h - ); + return rbs_ast_declarations_module_alias_t_to_ruby_value(ctx, node); } case RBS_AST_DECLARATIONS_TYPE_ALIAS: { rbs_ast_declarations_type_alias_t *node = (rbs_ast_declarations_type_alias_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) - ); - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_TypeAlias, - 1, - &h - ); + return rbs_ast_declarations_type_alias_t_to_ruby_value(ctx, node); } case RBS_AST_DIRECTIVES_USE: { rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(ctx, node->clauses)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Directives_Use, - 1, - &h - ); + return rbs_ast_directives_use_t_to_ruby_value(ctx, node); } case RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE: { rbs_ast_directives_use_single_clause_t *node = (rbs_ast_directives_use_single_clause_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol - - return CLASS_NEW_INSTANCE( - RBS_AST_Directives_Use_SingleClause, - 1, - &h - ); + return rbs_ast_directives_use_single_clause_t_to_ruby_value(ctx, node); } case RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE: { rbs_ast_directives_use_wildcard_clause_t *node = (rbs_ast_directives_use_wildcard_clause_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace - - return CLASS_NEW_INSTANCE( - RBS_AST_Directives_Use_WildcardClause, - 1, - &h - ); + return rbs_ast_directives_use_wildcard_clause_t_to_ruby_value(ctx, node); } case RBS_AST_INTEGER: { rbs_ast_integer_t *integer_node = (rbs_ast_integer_t *) instance; @@ -352,368 +1124,95 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_AST_MEMBERS_ALIAS: { rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Alias, - 1, - &h - ); + return rbs_ast_members_alias_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_ATTR_ACCESSOR: { rbs_ast_members_attr_accessor_t *node = (rbs_ast_members_attr_accessor_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_AttrAccessor, - 1, - &h - ); + return rbs_ast_members_attr_accessor_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_ATTR_READER: { rbs_ast_members_attr_reader_t *node = (rbs_ast_members_attr_reader_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_AttrReader, - 1, - &h - ); + return rbs_ast_members_attr_reader_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_ATTR_WRITER: { rbs_ast_members_attr_writer_t *node = (rbs_ast_members_attr_writer_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_AttrWriter, - 1, - &h - ); + return rbs_ast_members_attr_writer_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE: { rbs_ast_members_class_instance_variable_t *node = (rbs_ast_members_class_instance_variable_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_ClassInstanceVariable, - 1, - &h - ); + return rbs_ast_members_class_instance_variable_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_CLASS_VARIABLE: { rbs_ast_members_class_variable_t *node = (rbs_ast_members_class_variable_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_ClassVariable, - 1, - &h - ); + return rbs_ast_members_class_variable_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_EXTEND: { rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Extend, - 1, - &h - ); + return rbs_ast_members_extend_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_INCLUDE: { rbs_ast_members_include_t *node = (rbs_ast_members_include_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Include, - 1, - &h - ); + return rbs_ast_members_include_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_INSTANCE_VARIABLE: { rbs_ast_members_instance_variable_t *node = (rbs_ast_members_instance_variable_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_InstanceVariable, - 1, - &h - ); + return rbs_ast_members_instance_variable_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_METHOD_DEFINITION: { rbs_ast_members_method_definition_t *node = (rbs_ast_members_method_definition_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword - rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - rb_hash_aset(h, ID2SYM(rb_intern("overloading")), node->overloading ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_MethodDefinition, - 1, - &h - ); + return rbs_ast_members_method_definition_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD: { rbs_ast_members_method_definition_overload_t *node = (rbs_ast_members_method_definition_overload_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->method_type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_MethodDefinition_Overload, - 1, - &h - ); + return rbs_ast_members_method_definition_overload_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_PREPEND: { rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Prepend, - 1, - &h - ); + return rbs_ast_members_prepend_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_PRIVATE: { rbs_ast_members_private_t *node = (rbs_ast_members_private_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Private, - 1, - &h - ); + return rbs_ast_members_private_t_to_ruby_value(ctx, node); } case RBS_AST_MEMBERS_PUBLIC: { rbs_ast_members_public_t *node = (rbs_ast_members_public_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Public, - 1, - &h - ); + return rbs_ast_members_public_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_CLASS_ALIAS_ANNOTATION: { rbs_ast_ruby_annotations_class_alias_annotation_t *node = (rbs_ast_ruby_annotations_class_alias_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("keyword_location")), rbs_loc_to_ruby_location(ctx, node->keyword_location)); - rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type_name_location")), rbs_loc_to_ruby_location(ctx, node->type_name_location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_ClassAliasAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_class_alias_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_COLON_METHOD_TYPE_ANNOTATION: { rbs_ast_ruby_annotations_colon_method_type_annotation_t *node = (rbs_ast_ruby_annotations_colon_method_type_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->method_type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_colon_method_type_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_INSTANCE_VARIABLE_ANNOTATION: { rbs_ast_ruby_annotations_instance_variable_annotation_t *node = (rbs_ast_ruby_annotations_instance_variable_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name_location")), rbs_loc_to_ruby_location(ctx, node->ivar_name_location)); - rb_hash_aset(h, ID2SYM(rb_intern("colon_location")), rbs_loc_to_ruby_location(ctx, node->colon_location)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_InstanceVariableAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_instance_variable_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_METHOD_TYPES_ANNOTATION: { rbs_ast_ruby_annotations_method_types_annotation_t *node = (rbs_ast_ruby_annotations_method_types_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); - rb_hash_aset(h, ID2SYM(rb_intern("vertical_bar_locations")), rbs_location_list_to_ruby_array(ctx, node->vertical_bar_locations)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_MethodTypesAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_method_types_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_MODULE_ALIAS_ANNOTATION: { rbs_ast_ruby_annotations_module_alias_annotation_t *node = (rbs_ast_ruby_annotations_module_alias_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("keyword_location")), rbs_loc_to_ruby_location(ctx, node->keyword_location)); - rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("type_name_location")), rbs_loc_to_ruby_location(ctx, node->type_name_location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_ModuleAliasAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_module_alias_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_NODE_TYPE_ASSERTION: { rbs_ast_ruby_annotations_node_type_assertion_t *node = (rbs_ast_ruby_annotations_node_type_assertion_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_NodeTypeAssertion, - 1, - &h - ); + return rbs_ast_ruby_annotations_node_type_assertion_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_RETURN_TYPE_ANNOTATION: { rbs_ast_ruby_annotations_return_type_annotation_t *node = (rbs_ast_ruby_annotations_return_type_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("return_location")), rbs_loc_to_ruby_location(ctx, node->return_location)); - rb_hash_aset(h, ID2SYM(rb_intern("colon_location")), rbs_loc_to_ruby_location(ctx, node->colon_location)); - rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_ReturnTypeAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_return_type_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_SKIP_ANNOTATION: { rbs_ast_ruby_annotations_skip_annotation_t *node = (rbs_ast_ruby_annotations_skip_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("skip_location")), rbs_loc_to_ruby_location(ctx, node->skip_location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_SkipAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_skip_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_RUBY_ANNOTATIONS_TYPE_APPLICATION_ANNOTATION: { rbs_ast_ruby_annotations_type_application_annotation_t *node = (rbs_ast_ruby_annotations_type_application_annotation_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location)); - rb_hash_aset(h, ID2SYM(rb_intern("type_args")), rbs_node_list_to_ruby_array(ctx, node->type_args)); - rb_hash_aset(h, ID2SYM(rb_intern("close_bracket_location")), rbs_loc_to_ruby_location(ctx, node->close_bracket_location)); - rb_hash_aset(h, ID2SYM(rb_intern("comma_locations")), rbs_location_list_to_ruby_array(ctx, node->comma_locations)); - - return CLASS_NEW_INSTANCE( - RBS_AST_Ruby_Annotations_TypeApplicationAnnotation, - 1, - &h - ); + return rbs_ast_ruby_annotations_type_application_annotation_t_to_ruby_value(ctx, node); } case RBS_AST_STRING: { rbs_ast_string_t *string_node = (rbs_ast_string_t *) instance; @@ -723,55 +1222,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_AST_TYPE_PARAM: { rbs_ast_type_param_t *node = (rbs_ast_type_param_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("variance")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->variance)); // rbs_keyword - rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->upper_bound)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("lower_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->lower_bound)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->default_type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("unchecked")), node->unchecked ? Qtrue : Qfalse); - - return CLASS_NEW_INSTANCE( - RBS_AST_TypeParam, - 1, - &h - ); + return rbs_ast_type_param_t_to_ruby_value(ctx, node); } case RBS_METHOD_TYPE: { rbs_method_type_t *node = (rbs_method_type_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block - - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) - ); - return CLASS_NEW_INSTANCE( - RBS_MethodType, - 1, - &h - ); + return rbs_method_type_t_to_ruby_value(ctx, node); } case RBS_NAMESPACE: { rbs_namespace_t *node = (rbs_namespace_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("path")), rbs_node_list_to_ruby_array(ctx, node->path)); - rb_hash_aset(h, ID2SYM(rb_intern("absolute")), node->absolute ? Qtrue : Qfalse); - - return CLASS_NEW_INSTANCE( - RBS_Namespace, - 1, - &h - ); + return rbs_namespace_t_to_ruby_value(ctx, node); } case RBS_SIGNATURE: { rbs_signature_t *signature = (rbs_signature_t *) instance; @@ -783,295 +1242,91 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_TYPE_NAME: { rbs_type_name_t *node = (rbs_type_name_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - - return CLASS_NEW_INSTANCE( - RBS_TypeName, - 1, - &h - ); + return rbs_type_name_t_to_ruby_value(ctx, node); } case RBS_TYPES_ALIAS: { rbs_types_alias_t *node = (rbs_types_alias_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Alias, - 1, - &h - ); + return rbs_types_alias_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_ANY: { rbs_types_bases_any_t *node = (rbs_types_bases_any_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo ? Qtrue : Qfalse); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Any, - 1, - &h - ); + return rbs_types_bases_any_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_BOOL: { rbs_types_bases_bool_t *node = (rbs_types_bases_bool_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Bool, - 1, - &h - ); + return rbs_types_bases_bool_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_BOTTOM: { rbs_types_bases_bottom_t *node = (rbs_types_bases_bottom_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Bottom, - 1, - &h - ); + return rbs_types_bases_bottom_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_CLASS: { rbs_types_bases_class_t *node = (rbs_types_bases_class_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Class, - 1, - &h - ); + return rbs_types_bases_class_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_INSTANCE: { rbs_types_bases_instance_t *node = (rbs_types_bases_instance_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Instance, - 1, - &h - ); + return rbs_types_bases_instance_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_NIL: { rbs_types_bases_nil_t *node = (rbs_types_bases_nil_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Nil, - 1, - &h - ); + return rbs_types_bases_nil_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_SELF: { rbs_types_bases_self_t *node = (rbs_types_bases_self_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Self, - 1, - &h - ); + return rbs_types_bases_self_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_TOP: { rbs_types_bases_top_t *node = (rbs_types_bases_top_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Top, - 1, - &h - ); + return rbs_types_bases_top_t_to_ruby_value(ctx, node); } case RBS_TYPES_BASES_VOID: { rbs_types_bases_void_t *node = (rbs_types_bases_void_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Void, - 1, - &h - ); + return rbs_types_bases_void_t_to_ruby_value(ctx, node); } case RBS_TYPES_BLOCK: { rbs_types_block_t *node = (rbs_types_block_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("required")), node->required ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_Types_Block, - 1, - &h - ); + return rbs_types_block_t_to_ruby_value(ctx, node); } case RBS_TYPES_CLASS_INSTANCE: { rbs_types_class_instance_t *node = (rbs_types_class_instance_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - - return CLASS_NEW_INSTANCE( - RBS_Types_ClassInstance, - 1, - &h - ); + return rbs_types_class_instance_t_to_ruby_value(ctx, node); } case RBS_TYPES_CLASS_SINGLETON: { rbs_types_class_singleton_t *node = (rbs_types_class_singleton_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - - return CLASS_NEW_INSTANCE( - RBS_Types_ClassSingleton, - 1, - &h - ); + return rbs_types_class_singleton_t_to_ruby_value(ctx, node); } case RBS_TYPES_FUNCTION: { rbs_types_function_t *node = (rbs_types_function_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("required_positionals")), rbs_node_list_to_ruby_array(ctx, node->required_positionals)); - rb_hash_aset(h, ID2SYM(rb_intern("optional_positionals")), rbs_node_list_to_ruby_array(ctx, node->optional_positionals)); - rb_hash_aset(h, ID2SYM(rb_intern("rest_positionals")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rest_positionals)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("trailing_positionals")), rbs_node_list_to_ruby_array(ctx, node->trailing_positionals)); - rb_hash_aset(h, ID2SYM(rb_intern("required_keywords")), rbs_hash_to_ruby_hash(ctx, node->required_keywords)); - rb_hash_aset(h, ID2SYM(rb_intern("optional_keywords")), rbs_hash_to_ruby_hash(ctx, node->optional_keywords)); - rb_hash_aset(h, ID2SYM(rb_intern("rest_keywords")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rest_keywords)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_Types_Function, - 1, - &h - ); + return rbs_types_function_t_to_ruby_value(ctx, node); } case RBS_TYPES_FUNCTION_PARAM: { rbs_types_function_param_t *node = (rbs_types_function_param_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - - return CLASS_NEW_INSTANCE( - RBS_Types_Function_Param, - 1, - &h - ); + return rbs_types_function_param_t_to_ruby_value(ctx, node); } case RBS_TYPES_INTERFACE: { rbs_types_interface_t *node = (rbs_types_interface_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Interface, - 1, - &h - ); + return rbs_types_interface_t_to_ruby_value(ctx, node); } case RBS_TYPES_INTERSECTION: { rbs_types_intersection_t *node = (rbs_types_intersection_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Intersection, - 1, - &h - ); + return rbs_types_intersection_t_to_ruby_value(ctx, node); } case RBS_TYPES_LITERAL: { rbs_types_literal_t *node = (rbs_types_literal_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("literal")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->literal)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_Types_Literal, - 1, - &h - ); + return rbs_types_literal_t_to_ruby_value(ctx, node); } case RBS_TYPES_OPTIONAL: { rbs_types_optional_t *node = (rbs_types_optional_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_Types_Optional, - 1, - &h - ); + return rbs_types_optional_t_to_ruby_value(ctx, node); } case RBS_TYPES_PROC: { rbs_types_proc_t *node = (rbs_types_proc_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block - rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_Types_Proc, - 1, - &h - ); + return rbs_types_proc_t_to_ruby_value(ctx, node); } case RBS_TYPES_RECORD: { rbs_types_record_t *node = (rbs_types_record_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), rbs_hash_to_ruby_hash(ctx, node->all_fields)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Record, - 1, - &h - ); + return rbs_types_record_t_to_ruby_value(ctx, node); } case RBS_TYPES_RECORD_FIELD_TYPE: { rbs_types_record_field_type_t *record_fieldtype = (rbs_types_record_field_type_t *) instance; @@ -1083,54 +1338,19 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_TYPES_TUPLE: { rbs_types_tuple_t *node = (rbs_types_tuple_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Tuple, - 1, - &h - ); + return rbs_types_tuple_t_to_ruby_value(ctx, node); } case RBS_TYPES_UNION: { rbs_types_union_t *node = (rbs_types_union_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - - return CLASS_NEW_INSTANCE( - RBS_Types_Union, - 1, - &h - ); + return rbs_types_union_t_to_ruby_value(ctx, node); } case RBS_TYPES_UNTYPED_FUNCTION: { rbs_types_untyped_function_t *node = (rbs_types_untyped_function_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node - - return CLASS_NEW_INSTANCE( - RBS_Types_UntypedFunction, - 1, - &h - ); + return rbs_types_untyped_function_t_to_ruby_value(ctx, node); } case RBS_TYPES_VARIABLE: { rbs_types_variable_t *node = (rbs_types_variable_t *) instance; - - VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - - return CLASS_NEW_INSTANCE( - RBS_Types_Variable, - 1, - &h - ); + return rbs_types_variable_t_to_ruby_value(ctx, node); } case RBS_KEYWORD: { rbs_constant_t *constant = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, ((rbs_keyword_t *) instance)->constant_id); diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index c40a58d2b9..eac4f62380 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -31,4 +31,7 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *li VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance); +extern VALUE EMPTY_ARRAY; +extern VALUE EMPTY_HASH; + #endif diff --git a/ext/rbs_extension/extconf.rb b/ext/rbs_extension/extconf.rb index 88e971c3ec..eb25ec562a 100644 --- a/ext/rbs_extension/extconf.rb +++ b/ext/rbs_extension/extconf.rb @@ -18,7 +18,7 @@ '-Wc++-compat', ] -append_cflags ['-O0', '-g'] if ENV['DEBUG'] +append_cflags ['-O0', '-pg'] if ENV['DEBUG'] if ENV["TEST_NO_C23"] puts "Adding -Wc2x-extensions to CFLAGS" $CFLAGS << " -Werror -Wc2x-extensions" diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 766a6cc06b..7f23c189cc 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -1,3 +1,4 @@ +#include "rbs/lexer.h" #include "rbs_extension.h" #include "rbs/util/rbs_assert.h" #include "rbs/util/rbs_allocator.h" @@ -6,8 +7,11 @@ #include "legacy_location.h" #include "rbs_string_bridging.h" +#include "ruby/internal/gc.h" #include "ruby/vm.h" +rbs_allocator_t *shared_allocator; + /** * Raises `RBS::ParsingError` or `RuntimeError` on `tok` with message constructed with given `fmt`. * @@ -169,6 +173,7 @@ static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int e const char *encoding_name = rb_enc_name(encoding); return rbs_parser_new( + shared_allocator, rbs_string_from_ruby_string(string), rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))), start_pos, @@ -447,12 +452,65 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { return results; } +VALUE rbsparser_parse_signatures(VALUE self, VALUE files) { + Check_Type(files, T_HASH); + + VALUE result_hash = rb_hash_new(); + VALUE keys = rb_funcall(files, rb_intern("keys"), 0); + + for (long i = 0; i < RARRAY_LEN(keys); i++) { + VALUE file_path = rb_ary_entry(keys, i); + VALUE buffer = rb_hash_aref(files, file_path); + + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + rb_encoding *encoding = rb_enc_get(string); + + // Prepare parser + rbs_parser_t *parser = alloc_parser_from_buffer(buffer, 0, RSTRING_LEN(string)); + struct parse_signature_arg arg = { + .buffer = buffer, + .encoding = encoding, + .parser = parser, + .require_eof = false + }; + + VALUE entry = rb_hash_new(); + + int state = 0; + VALUE ast = rb_protect(parse_signature_try, (VALUE) &arg, &state); + + if (state == 0) { + // Success: store AST + rb_hash_aset(entry, ID2SYM(rb_intern("ast")), ast); + } else { + // Error: store exception + VALUE err = rb_errinfo(); + rb_set_errinfo(Qnil); // Clear the error info + rb_hash_aset(entry, ID2SYM(rb_intern("error")), err); + } + + rb_hash_aset(result_hash, file_path, entry); + + ensure_free_parser((VALUE) parser); + RB_GC_GUARD(string); + } + + return result_hash; +} + void rbs__init_parser(void) { RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject); rb_gc_register_mark_object(RBS_Parser); VALUE empty_array = rb_obj_freeze(rb_ary_new()); rb_gc_register_mark_object(empty_array); + EMPTY_ARRAY = rb_obj_freeze(rb_ary_new()); + rb_gc_register_mark_object(EMPTY_ARRAY); + + EMPTY_HASH = rb_obj_freeze(rb_hash_new()); + rb_gc_register_mark_object(EMPTY_HASH); + rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 7); rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5); rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3); @@ -460,13 +518,17 @@ void rbs__init_parser(void) { rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4); rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4); rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2); + rb_define_singleton_method(RBS_Parser, "_parse_signatures", rbsparser_parse_signatures, 1); } static void Deinit_rbs_extension(ruby_vm_t *_) { + rbs_allocator_free(shared_allocator); rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL); } void Init_rbs_extension(void) { + shared_allocator = rbs_allocator_init(); + #ifdef HAVE_RB_EXT_RACTOR_SAFE rb_ext_ractor_safe(true); #endif diff --git a/include/rbs/defines.h b/include/rbs/defines.h index 6dc2091a61..f193ab5f46 100644 --- a/include/rbs/defines.h +++ b/include/rbs/defines.h @@ -32,6 +32,24 @@ #define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) #endif +/** + * Support RBS_LIKELY and RBS_UNLIKELY to help the compiler optimize its + * branch predication. + */ +#if defined(__GNUC__) || defined(__clang__) +/** The compiler should predicate that this branch will be taken. */ +#define RBS_LIKELY(x) __builtin_expect(!!(x), 1) + +/** The compiler should predicate that this branch will not be taken. */ +#define RBS_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +/** Void because this platform does not support branch prediction hints. */ +#define RBS_LIKELY(x) (x) + +/** Void because this platform does not support branch prediction hints. */ +#define RBS_UNLIKELY(x) (x) +#endif + /** * We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch. * Use RBS_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional. diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index ec3bad468c..6fd5279596 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -126,20 +126,26 @@ typedef struct { * The lexer state is the curren token. * * ``` - * ... "a string token" - * ^ start position - * ^ current position - * ~~~~~~ Token => "a str + #. 0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6 + * ... " a s t r i n g t o k e n " + * ^ start position (0) + * ^ current position (6) + * ^ current character ('i', bytes = 1) + * ~~~~~~~~~~~ Token => "a str * ``` * */ typedef struct { rbs_string_t string; - int start_pos; /* The character position that defines the start of the input */ - int end_pos; /* The character position that defines the end of the input */ - rbs_position_t current; /* The current position */ - rbs_position_t start; /* The start position of the current token */ + int start_pos; /* The character position that defines the start of the input */ + int end_pos; /* The character position that defines the end of the input */ + rbs_position_t current; /* The current position: just before the current_character */ + rbs_position_t start; /* The start position of the current token */ + + unsigned int current_code_point; /* Current character code point */ + size_t current_character_bytes; /* Current character byte length (0 or 1~4) */ + bool first_token_of_line; /* This flag is used for tLINECOMMENT */ - unsigned int last_char; /* Last peeked character */ + const rbs_encoding_t *encoding; } rbs_lexer_t; @@ -159,15 +165,23 @@ int rbs_token_bytes(rbs_token_t tok); const char *rbs_token_type_str(enum RBSTokenType type); /** - * Read next character. + * Returns the next character. * */ unsigned int rbs_peek(rbs_lexer_t *lexer); /** - * Skip one character. + * Advances the current position by one character. * */ void rbs_skip(rbs_lexer_t *lexer); +/** + * Read next character and store the codepoint and byte length to the given pointers. + * + * This doesn't update the lexer state. + * Returns `true` if succeeded, or `false` if reached to EOF. + * */ +bool rbs_next_char(rbs_lexer_t *lexer, unsigned int *codepoint, size_t *bytes); + /** * Skip n characters. * */ @@ -187,4 +201,6 @@ rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer); void rbs_print_token(rbs_token_t tok); +void rbs_print_lexer(rbs_lexer_t *lexer); + #endif diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 339235deb8..26f3c6f1ac 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -99,7 +99,7 @@ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_enc * rbs_parser_new(buffer, string, encoding, 0, 1); * ``` * */ -rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); +rbs_parser_t *rbs_parser_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); void rbs_parser_free(rbs_parser_t *parser); /** diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h index c3356cfadd..61894b0525 100644 --- a/include/rbs/util/rbs_allocator.h +++ b/include/rbs/util/rbs_allocator.h @@ -42,6 +42,8 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment); +void rbs_allocator_reset(rbs_allocator_t *); + // Use this when allocating memory for a single instance of a type. #define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), rbs_alignof(type))) // Use this when allocating memory that will be immediately written to in full. diff --git a/include/rbs/util/rbs_assert.h b/include/rbs/util/rbs_assert.h index 6a6201f92d..9de8d45779 100644 --- a/include/rbs/util/rbs_assert.h +++ b/include/rbs/util/rbs_assert.h @@ -4,6 +4,12 @@ #include "rbs/defines.h" #include -void rbs_assert(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3); +void rbs_assert_impl(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3); + +#ifdef NDEBUG +#define rbs_assert(condition, fmt, ...) ((void) 0) +#else +#define rbs_assert(condition, fmt, ...) rbs_assert_impl(condition, fmt, ##__VA_ARGS__) +#endif #endif diff --git a/parse.rb b/parse.rb new file mode 100644 index 0000000000..7d0f799783 --- /dev/null +++ b/parse.rb @@ -0,0 +1,23 @@ +# require "bundler/setup" + +require 'rbs' +# require 'benchmark/ips' + +if (opt = ARGV[0]) == "--wait" + ARGV.shift + puts "⏯️ Waiting for enter to continue at #{Process.pid}..." + STDIN.gets +end + +file = ARGV.shift +sig = File.read(file) + +puts "#{file} -- #{sig.bytesize} bytes" + +started_at = Time.now +secs = 3 + +loop do + RBS::Parser.parse_signature(sig) + break if (Time.now - started_at) > secs +end \ No newline at end of file diff --git a/src/lexer.re b/src/lexer.re index ee55e93057..9aac441f79 100644 --- a/src/lexer.re +++ b/src/lexer.re @@ -14,7 +14,7 @@ rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer) { re2c:define:YYSKIP = "rbs_skip(lexer);"; re2c:define:YYBACKUP = "backup = *lexer;"; re2c:define:YYRESTORE = "*lexer = backup;"; - re2c:yyfill:enable = 0; + re2c:yyfill:enable = 0; word = [a-zA-Z0-9_]; diff --git a/src/lexstate.c b/src/lexstate.c index 8e9e3e03ac..41ed76b00f 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -1,4 +1,7 @@ +#include "rbs/defines.h" #include "rbs/lexer.h" +#include "rbs/util/rbs_assert.h" +#include static const char *RBS_TOKENTYPE_NAMES[] = { "NullType", @@ -112,17 +115,60 @@ int rbs_token_bytes(rbs_token_t tok) { } unsigned int rbs_peek(rbs_lexer_t *lexer) { - if (lexer->current.char_pos == lexer->end_pos) { - lexer->last_char = '\0'; - return 0; + return lexer->current_code_point; +} + +bool rbs_next_char(rbs_lexer_t *lexer, unsigned int *codepoint, size_t *byte_len) { + if (RBS_UNLIKELY(lexer->current.char_pos == lexer->end_pos)) { + return false; + } + + const char *start = lexer->string.start + lexer->current.byte_pos; + + // Fast path for ASCII (single-byte) characters + if ((unsigned int) *start < 128) { + *codepoint = (unsigned int) *start; + *byte_len = 1; + return true; + } + + *byte_len = lexer->encoding->char_width((const uint8_t *) start, (ptrdiff_t) (lexer->string.end - start)); + + if (*byte_len == 1) { + *codepoint = (unsigned int) *start; } else { - rbs_string_t str = rbs_string_new( - lexer->string.start + lexer->current.byte_pos, - lexer->string.end - ); - unsigned int c = rbs_utf8_string_to_codepoint(str); - lexer->last_char = c; - return c; + *codepoint = 12523; // Dummy data for "ル" from "ルビー" (Ruby) in Unicode + } + + return true; +} + +void rbs_skip(rbs_lexer_t *lexer) { + rbs_assert(lexer->current_character_bytes > 0, "rbs_skip called with current_character_bytes == 0"); + + if (RBS_UNLIKELY(lexer->current_code_point == '\0')) { + return; + } + + unsigned int codepoint; + size_t byte_len; + + lexer->current.byte_pos += lexer->current_character_bytes; + lexer->current.char_pos += 1; + if (lexer->current_code_point == '\n') { + lexer->current.line += 1; + lexer->current.column = 0; + lexer->first_token_of_line = true; + } else { + lexer->current.column += 1; + } + + if (rbs_next_char(lexer, &codepoint, &byte_len)) { + lexer->current_code_point = codepoint; + lexer->current_character_bytes = byte_len; + } else { + lexer->current_character_bytes = 1; + lexer->current_code_point = '\0'; } } @@ -156,35 +202,8 @@ rbs_token_t rbs_next_eof_token(rbs_lexer_t *lexer) { } } -void rbs_skip(rbs_lexer_t *lexer) { - if (!lexer->last_char) { - rbs_peek(lexer); - } - - size_t byte_len; - - if (lexer->last_char == '\0') { - byte_len = 1; - } else { - const char *start = lexer->string.start + lexer->current.byte_pos; - byte_len = lexer->encoding->char_width((const uint8_t *) start, (ptrdiff_t) (lexer->string.end - start)); - } - - lexer->current.char_pos += 1; - lexer->current.byte_pos += byte_len; - - if (lexer->last_char == '\n') { - lexer->current.line += 1; - lexer->current.column = 0; - lexer->first_token_of_line = true; - } else { - lexer->current.column += 1; - } -} - void rbs_skipn(rbs_lexer_t *lexer, size_t size) { for (size_t i = 0; i < size; i++) { - rbs_peek(lexer); rbs_skip(lexer); } } diff --git a/src/parser.c b/src/parser.c index 0cbb4bf124..575d4246a1 100644 --- a/src/parser.c +++ b/src/parser.c @@ -7,6 +7,7 @@ #include #include "rbs/defines.h" +#include "rbs/lexer.h" #include "rbs/string.h" #include "rbs/util/rbs_unescape.h" #include "rbs/util/rbs_buffer.h" @@ -3456,6 +3457,14 @@ void rbs_print_token(rbs_token_t tok) { ); } +void rbs_print_lexer(rbs_lexer_t *lexer) { + printf("Lexer: (range = %d...%d, encoding = %s\n", lexer->start_pos, lexer->end_pos, lexer->encoding->name); + printf(" start = { char_pos = %d, byte_pos = %d }\n", lexer->start.char_pos, lexer->start.byte_pos); + printf(" current = { char_pos = %d, byte_pos = %d }\n", lexer->current.char_pos, lexer->current.byte_pos); + printf(" character = { code_point = %d (%c), bytes = %d }\n", lexer->current_code_point, lexer->current_code_point < 256 ? lexer->current_code_point : '?', lexer->current_character_bytes); + printf(" first_token_of_line = %s\n", lexer->first_token_of_line ? "true" : "false"); +} + rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line) { int comment_line = subject_line - 1; @@ -3484,21 +3493,33 @@ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, cons .end_pos = end_pos, .current = start_position, .start = { 0 }, - .first_token_of_line = false, - .last_char = 0, + .first_token_of_line = true, + .current_character_bytes = 0, + .current_code_point = '\0', .encoding = encoding, }; - rbs_skipn(lexer, start_pos); + unsigned int codepoint; + size_t bytes; + + if (rbs_next_char(lexer, &codepoint, &bytes)) { + lexer->current_code_point = codepoint; + lexer->current_character_bytes = bytes; + } else { + lexer->current_code_point = '\0'; + lexer->current_character_bytes = 1; + } + + if (start_pos > 0) { + rbs_skipn(lexer, start_pos); + } + lexer->start = lexer->current; - lexer->first_token_of_line = lexer->current.column == 0; return lexer; } -rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { - rbs_allocator_t *allocator = rbs_allocator_init(); - +rbs_parser_t *rbs_parser_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos); rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t); @@ -3547,7 +3568,8 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding void rbs_parser_free(rbs_parser_t *parser) { rbs_constant_pool_free(&parser->constant_pool); - rbs_allocator_free(ALLOCATOR()); + rbs_allocator_reset(ALLOCATOR()); + // rbs_allocator_free(ALLOCATOR()); } void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) { diff --git a/src/string.c b/src/string.c index dc4e87f7b9..cc7de5e98a 100644 --- a/src/string.c +++ b/src/string.c @@ -1,4 +1,5 @@ #include "rbs/string.h" +#include "rbs/defines.h" #include #include @@ -14,7 +15,7 @@ unsigned int rbs_utf8_string_to_codepoint(const rbs_string_t string) { if (s >= end) return 0; // End of string - if ((*s & 0x80) == 0) { + if (RBS_LIKELY((*s & 0x80) == 0)) { // Single byte character (0xxxxxxx) return *s; } else if ((*s & 0xE0) == 0xC0) { diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index 0d53031b9b..3572596239 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -129,6 +129,10 @@ rbs_allocator_t *rbs_allocator_init(void) { return (rbs_allocator_t *) mem; } +void rbs_allocator_reset(rbs_allocator_t *allocator) { + allocator->heap_ptr = (uintptr_t) allocator + sizeof(rbs_allocator_t); +} + void rbs_allocator_free(rbs_allocator_t *allocator) { destroy_memory((void *) allocator, allocator->size); } @@ -154,11 +158,7 @@ void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t // It's assumed that callers to this function will immediately write to the allocated memory, anyway. void *rbs_allocator_calloc_impl(rbs_allocator_t *allocator, size_t count, size_t size, size_t alignment) { void *p = rbs_allocator_malloc_many_impl(allocator, count, size, alignment); -#if defined(__linux__) - // mmap with MAP_ANONYMOUS gives zero-filled pages. -#else memset(p, 0, count * size); -#endif return p; } diff --git a/src/util/rbs_assert.c b/src/util/rbs_assert.c index 63f17b09a7..e89b8125c5 100644 --- a/src/util/rbs_assert.c +++ b/src/util/rbs_assert.c @@ -5,7 +5,7 @@ #include #include -void rbs_assert(bool condition, const char *fmt, ...) { +void rbs_assert_impl(bool condition, const char *fmt, ...) { if (condition) { return; } diff --git a/src/util/rbs_encoding.c b/src/util/rbs_encoding.c index 9516e2a4e2..796373ebed 100644 --- a/src/util/rbs_encoding.c +++ b/src/util/rbs_encoding.c @@ -4621,7 +4621,38 @@ rbs_unicode_codepoint_match(rbs_unicode_codepoint_t codepoint, const rbs_unicode * SOFTWARE. */ static const uint8_t rbs_utf_8_dfa[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1f + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, // 00..1f 0, 0, 0, @@ -4999,7 +5030,7 @@ static const uint8_t rbs_utf_8_dfa[] = { */ static rbs_unicode_codepoint_t rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) { - rbs_assert(n >= 0, "n must be greater than or equal to 0. Got %ti", n); + rbs_assert(n >= 0, "[rbs_unicode_codepoint_t] n must be greater than or equal to 0. Got %ti", n); size_t maximum = (n > 4) ? 4 : ((size_t) n); uint32_t codepoint; @@ -5029,7 +5060,7 @@ rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) { */ size_t rbs_encoding_utf_8_char_width(const uint8_t *b, ptrdiff_t n) { - rbs_assert(n >= 0, "n must be greater than or equal to 0. Got %ti", n); + rbs_assert(n >= 0, "[rbs_encoding_utf_8_char_width] n must be greater than or equal to 0. Got %ti", n); size_t maximum = (n > 4) ? 4 : ((size_t) n); uint32_t state = 0; diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index b88ab09724..b0f157f0e3 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -4,6 +4,9 @@ #include "rbs_string_bridging.h" #include "legacy_location.h" +VALUE EMPTY_ARRAY; +VALUE EMPTY_HASH; + #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) { @@ -15,16 +18,32 @@ rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *co } VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { + if (!list->head) { + return EMPTY_ARRAY; + } + VALUE ruby_array = rb_ary_new(); + VALUE *values = ALLOCA_N(VALUE, list->length); - for (rbs_node_list_node_t *n = list->head; n != NULL; n = n->next) { - rb_ary_push(ruby_array, rbs_struct_to_ruby_value(ctx, n->node)); + size_t i = 0; + for (rbs_node_list_node_t *n = list->head; n != NULL; n = n->next, i++) { + values[i] = rbs_struct_to_ruby_value(ctx, n->node); } + rb_ary_cat(ruby_array, values, list->length); + return ruby_array; } VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) { + if (rbs_hash->length > 0) { + printf("Size of hash: %zu\n", rbs_hash->length); + } + + if (!rbs_hash->head) { + return EMPTY_HASH; + } + VALUE ruby_hash = rb_hash_new(); for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) { @@ -53,12 +72,12 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so } VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) { - VALUE ruby_array = rb_ary_new(); - if (list == NULL) { - return ruby_array; + return EMPTY_ARRAY; } + VALUE ruby_array = rb_ary_new(); + for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) { rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc)); } @@ -76,6 +95,62 @@ VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_locatio rb_class_new_instance(argc, argv, receiver) #endif +<%- nodes.each do |node| -%> + <%- case node.ruby_full_name -%> + <%- when "RBS::AST::Bool", "RBS::AST::Integer", "RBS::AST::String", "RBS::Types::Record::FieldType", "RBS::Signature" -%> + <%- else -%> +VALUE <%= node.c_type_name %>_to_ruby_value(rbs_translation_context_t ctx, <%= node.c_type_name %> *node) { + VALUE h = rb_hash_new(); + <%- if node.expose_location? -%> + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + <%- end -%> + <%- node.fields.each do |field| -%> + <%- case field.c_type -%> + <%- when "VALUE" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.c_name %>); + <%- when "rbs_node_list" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(ctx, node-><%= field.c_name %>)); + <%- when "rbs_hash" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.c_name %>)); + <%- when "rbs_ast_symbol" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_ast_symbol + <%- when "rbs_keyword" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_keyword + <%- when "rbs_string" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.c_name %>, ctx.encoding)); + <%- when "bool" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.c_name %> ? Qtrue : Qfalse); + <%- when "rbs_location" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(ctx, node-><%= field.name %>)); + <%- when "rbs_location_list" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_location_list_to_ruby_array(ctx, node-><%= field.name %>)); + <%- else -%> + <%- unless field.ast_node? -%> + #warning unexpected type <%= field.c_type -%> + <%- end -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // <%= field.c_type %> + <%- end -%> + <%- end -%> + + <%- case node.ruby_full_name -%> + <%- when "RBS::AST::Declarations::Class", "RBS::AST::Declarations::Module", "RBS::AST::Declarations::Interface", "RBS::AST::Declarations::TypeAlias", "RBS::MethodType" -%> + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + <%- end -%> + return CLASS_NEW_INSTANCE( + <%= node.c_constant_name %>, + 1, + &h + ); +} + + <%- end -%> +<%- end -%> + VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instance) { if (instance == NULL) return Qnil; @@ -116,53 +191,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan return array; <%- else -%> <%= node.c_type_name %> *node = (<%= node.c_type_name %> *) instance; - - VALUE h = rb_hash_new(); - <%- if node.expose_location? -%> - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - <%- end -%> - <%- node.fields.each do |field| -%> - <%- case field.c_type -%> - <%- when "VALUE" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.c_name %>); - <%- when "rbs_node_list" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(ctx, node-><%= field.c_name %>)); - <%- when "rbs_hash" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.c_name %>)); - <%- when "rbs_ast_symbol" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_ast_symbol - <%- when "rbs_keyword" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_keyword - <%- when "rbs_string" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.c_name %>, ctx.encoding)); - <%- when "bool" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.c_name %> ? Qtrue : Qfalse); - <%- when "rbs_location" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(ctx, node-><%= field.name %>)); - <%- when "rbs_location_list" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_location_list_to_ruby_array(ctx, node-><%= field.name %>)); - <%- else -%> - <%- unless field.ast_node? -%> - #warning unexpected type <%= field.c_type -%> - <%- end -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // <%= field.c_type %> - <%- end -%> - <%- end -%> - - <%- case node.ruby_full_name -%> - <%- when "RBS::AST::Declarations::Class", "RBS::AST::Declarations::Module", "RBS::AST::Declarations::Interface", "RBS::AST::Declarations::TypeAlias", "RBS::MethodType" -%> - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) - ); - <%- end -%> - return CLASS_NEW_INSTANCE( - <%= node.c_constant_name %>, - 1, - &h - ); + return <%= node.c_type_name %>_to_ruby_value(ctx, node); <%- end -%> } <%- end -%> diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index ddf5b00bab..a0ba1948c4 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -24,4 +24,7 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *li VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance); +extern VALUE EMPTY_ARRAY; +extern VALUE EMPTY_HASH; + #endif