Skip to content

Commit c0b010e

Browse files
adapt code which autogens sigs to be able to autogen correct struct/data
1 parent 76e10e5 commit c0b010e

File tree

6 files changed

+47
-245
lines changed

6 files changed

+47
-245
lines changed

lib/rbs/prototype/runtime/value_object_generator.rb

+3-116
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(target_class)
1313
end
1414

1515
def build_decl
16-
decl = AST::Declarations::Class.new(
16+
AST::Declarations::Class.new(
1717
name: to_type_name(only_name(@target_class)),
1818
type_params: [],
1919
super_class: build_super_class,
@@ -22,10 +22,6 @@ def build_decl
2222
location: nil,
2323
comment: nil
2424
)
25-
26-
add_decl_members(decl)
27-
28-
decl
2925
end
3026

3127
private
@@ -100,112 +96,9 @@ def self.generatable?(target)
10096
CAN_CALL_KEYWORD_INIT_P = Struct.new(:tmp).respond_to?(:keyword_init?)
10197

10298
def build_super_class
103-
AST::Declarations::Class::Super.new(name: TypeName("::Struct"), args: [untyped], location: nil)
104-
end
105-
106-
def add_decl_members(decl)
107-
decl.members.concat build_s_new
108-
decl.members.concat build_s_keyword_init_p
109-
decl.members.concat build_s_members
110-
decl.members.concat build_member_accessors(AST::Members::AttrAccessor)
99+
AST::Declarations::Class::Super.new(name: TypeName("::Struct"), args: @target_class.members.to_h{ |m| [m, untyped] }, location: nil)
111100
end
112101

113-
# def self.new: (?untyped foo, ?untyped bar) -> instance
114-
# | (?foo: untyped, ?bar: untyped) -> instance
115-
def build_s_new
116-
[:new, :[]].map do |name|
117-
new_overloads = []
118-
119-
if CAN_CALL_KEYWORD_INIT_P
120-
case @target_class.keyword_init?
121-
when false
122-
new_overloads << build_overload_for_positional_arguments
123-
when true
124-
new_overloads << build_overload_for_keyword_arguments
125-
when nil
126-
new_overloads << build_overload_for_positional_arguments
127-
new_overloads << build_overload_for_keyword_arguments
128-
else
129-
raise
130-
end
131-
else
132-
new_overloads << build_overload_for_positional_arguments
133-
new_overloads << build_overload_for_keyword_arguments
134-
end
135-
136-
AST::Members::MethodDefinition.new(
137-
name: name,
138-
overloads: new_overloads,
139-
kind: :singleton,
140-
location: nil,
141-
comment: nil,
142-
annotations: [],
143-
overloading: false,
144-
visibility: nil
145-
)
146-
end
147-
end
148-
149-
def build_overload_for_positional_arguments
150-
AST::Members::MethodDefinition::Overload.new(
151-
annotations: [],
152-
method_type: MethodType.new(
153-
type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
154-
optional_positionals: @target_class.members.map { |m| Types::Function::Param.new(name: m, type: untyped) },
155-
),
156-
type_params: [],
157-
block: nil,
158-
location: nil,
159-
)
160-
)
161-
end
162-
163-
def build_overload_for_keyword_arguments
164-
AST::Members::MethodDefinition::Overload.new(
165-
annotations: [],
166-
method_type: MethodType.new(
167-
type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
168-
optional_keywords: @target_class.members.to_h { |m| [m, Types::Function::Param.new(name: nil, type: untyped)] },
169-
),
170-
type_params: [],
171-
block: nil,
172-
location: nil,
173-
)
174-
)
175-
end
176-
177-
# def self.keyword_init?: () -> bool?
178-
def build_s_keyword_init_p
179-
return [] unless CAN_CALL_KEYWORD_INIT_P
180-
181-
return_type = @target_class.keyword_init?.nil? \
182-
? Types::Bases::Nil.new(location: nil)
183-
: Types::Literal.new(literal: @target_class.keyword_init?, location: nil)
184-
type = Types::Function.empty(return_type)
185-
186-
[
187-
AST::Members::MethodDefinition.new(
188-
name: :keyword_init?,
189-
overloads: [
190-
AST::Members::MethodDefinition::Overload.new(
191-
annotations: [],
192-
method_type: MethodType.new(
193-
type: type,
194-
type_params: [],
195-
block: nil,
196-
location: nil,
197-
)
198-
)
199-
],
200-
kind: :singleton,
201-
location: nil,
202-
comment: nil,
203-
annotations: [],
204-
overloading: false,
205-
visibility: nil
206-
)
207-
]
208-
end
209102
end
210103

211104
class DataGenerator < ValueObjectBase
@@ -221,13 +114,7 @@ def self.generatable?(target)
221114
private
222115

223116
def build_super_class
224-
AST::Declarations::Class::Super.new(name: TypeName("::Data"), args: [], location: nil)
225-
end
226-
227-
def add_decl_members(decl)
228-
decl.members.concat build_s_new
229-
decl.members.concat build_s_members
230-
decl.members.concat build_member_accessors(AST::Members::AttrReader)
117+
AST::Declarations::Class::Super.new(name: TypeName("::Data"), args: @target_class.members.to_h{ |m| [m, untyped] }, location: nil)
231118
end
232119

233120
# def self.new: (untyped foo, untyped bar) -> instance

lib/rbs/types.rb

+28
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,22 @@ def ==(other)
886886

887887
alias eql? ==
888888

889+
def has_self_type?
890+
false
891+
end
892+
893+
def has_classish_type?
894+
false
895+
end
896+
897+
def each_type
898+
if block_given?
899+
# nop
900+
else
901+
enum_for :each_type
902+
end
903+
end
904+
889905
def hash
890906
self.class.hash ^ type.hash ^ name.hash
891907
end
@@ -913,6 +929,18 @@ def to_s
913929
"#{type}"
914930
end
915931
end
932+
933+
def map_type_name(&block)
934+
Param.new(
935+
name: name,
936+
type: type.map_type_name(&block),
937+
location: location
938+
)
939+
end
940+
941+
def with_nonreturn_void?
942+
type.with_nonreturn_void?
943+
end
916944
end
917945

918946
attr_reader :required_positionals

lib/rbs/writer.rb

+6
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ def name_and_args(name, args)
215215
if name && args
216216
if args.empty?
217217
"#{name}"
218+
elsif name.struct? || name.data?
219+
fields = args.map do |param|
220+
# same imple as Function::Param
221+
"#{param.name}: #{param.type}"
222+
end
223+
"#{name}{#{fields.join(", ")}}"
218224
else
219225
"#{name}[#{args.join(", ")}]"
220226
end

sig/prototype/runtime.rbs

-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ module RBS
4343

4444
def build_super_class: () -> AST::Declarations::Class::Super
4545

46-
def add_decl_members: (AST::Declarations::Class) -> void
47-
4846
def initialize: (Class target_class) -> void
4947
end
5048

@@ -53,8 +51,6 @@ module RBS
5351

5452
private
5553

56-
def add_decl_members: (AST::Declarations::Class decl) -> void
57-
5854
def build_overload_for_keyword_arguments: () -> AST::Members::MethodDefinition::Overload
5955

6056
def build_overload_for_positional_arguments: () -> AST::Members::MethodDefinition::Overload
@@ -73,8 +69,6 @@ module RBS
7369

7470
private
7571

76-
def add_decl_members: (AST::Declarations::Class decl) -> void
77-
7872
def build_s_new: () -> Array[AST::Members::MethodDefinition]
7973

8074
def build_super_class: () -> AST::Declarations::Class::Super

0 commit comments

Comments
 (0)