Class: RBS::DefinitionBuilder::MethodBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/definition_builder/method_builder.rb

Defined Under Namespace

Classes: Methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ MethodBuilder

Returns a new instance of MethodBuilder.



89
90
91
92
93
94
95
# File 'lib/rbs/definition_builder/method_builder.rb', line 89

def initialize(env:)
  @env = env

  @instance_methods = {}
  @singleton_methods = {}
  @interface_methods = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



84
85
86
# File 'lib/rbs/definition_builder/method_builder.rb', line 84

def env
  @env
end

#instance_methodsObject (readonly)

Returns the value of attribute instance_methods.



85
86
87
# File 'lib/rbs/definition_builder/method_builder.rb', line 85

def instance_methods
  @instance_methods
end

#interface_methodsObject (readonly)

Returns the value of attribute interface_methods.



87
88
89
# File 'lib/rbs/definition_builder/method_builder.rb', line 87

def interface_methods
  @interface_methods
end

#singleton_methodsObject (readonly)

Returns the value of attribute singleton_methods.



86
87
88
# File 'lib/rbs/definition_builder/method_builder.rb', line 86

def singleton_methods
  @singleton_methods
end

Instance Method Details

#build_alias(methods, type, member:) ⇒ Object



192
193
194
195
# File 'lib/rbs/definition_builder/method_builder.rb', line 192

def build_alias(methods, type, member:)
  defn = methods.methods[member.new_name] ||= Methods::Definition.empty(type: type, name: member.new_name)
  defn.originals << member
end

#build_attribute(methods, type, member:, accessibility:) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rbs/definition_builder/method_builder.rb', line 197

def build_attribute(methods, type, member:, accessibility:)
  if member.is_a?(AST::Members::AttrReader) || member.is_a?(AST::Members::AttrAccessor)
    defn = methods.methods[member.name] ||= Methods::Definition.empty(type: type, name: member.name)

    defn.accessibilities << accessibility
    defn.originals << member
  end

  if member.is_a?(AST::Members::AttrWriter) || member.is_a?(AST::Members::AttrAccessor)
    defn = methods.methods[:"#{member.name}="] ||= Methods::Definition.empty(type: type, name: :"#{member.name}=")

    defn.accessibilities << accessibility
    defn.originals << member
  end
end

#build_instance(type_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rbs/definition_builder/method_builder.rb', line 97

def build_instance(type_name)
  instance_methods[type_name] ||=
    begin
      entry = env.class_decls[type_name]
      args = Types::Variable.build(entry.type_params.each.map(&:name))
      type = Types::ClassInstance.new(name: type_name, args: args, location: nil)
      Methods.new(type: type).tap do |methods|
        entry.decls.each do |d|
          subst = Substitution.build(d.decl.type_params.each.map(&:name), args)
          each_member_with_accessibility(d.decl.members) do |member, accessibility|
            case member
            when AST::Members::MethodDefinition
              case member.kind
              when :instance
                build_method(
                  methods,
                  type,
                  member: member.update(types: member.types.map {|type| type.sub(subst) }),
                  accessibility: member.visibility || accessibility
                )
              when :singleton_instance
                build_method(
                  methods,
                  type,
                  member: member.update(types: member.types.map {|type| type.sub(subst) }),
                  accessibility: :private
                )
              end
            when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
              if member.kind == :instance
                build_attribute(methods,
                                type,
                                member: member.update(type: member.type.sub(subst)),
                                accessibility: member.visibility || accessibility)
              end
            when AST::Members::Alias
              if member.kind == :instance
                build_alias(methods, type, member: member)
              end
            end
          end
        end
      end.validate!
    end
end

#build_interface(type_name) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rbs/definition_builder/method_builder.rb', line 172

def build_interface(type_name)
  interface_methods[type_name] ||=
    begin
      entry = env.interface_decls[type_name]
      args = Types::Variable.build(entry.decl.type_params.each.map(&:name))
      type = Types::Interface.new(name: type_name, args: args, location: nil)

      Methods.new(type: type).tap do |methods|
        entry.decl.members.each do |member|
          case member
          when AST::Members::MethodDefinition
            build_method(methods, type, member: member, accessibility: :public)
          when AST::Members::Alias
            build_alias(methods, type, member: member)
          end
        end
      end.validate!
    end
end

#build_method(methods, type, member:, accessibility:) ⇒ Object



213
214
215
216
217
218
219
220
221
222
# File 'lib/rbs/definition_builder/method_builder.rb', line 213

def build_method(methods, type, member:, accessibility:)
  defn = methods.methods[member.name] ||= Methods::Definition.empty(type: type, name: member.name)

  if member.overload?
    defn.overloads << member
  else
    defn.accessibilities << accessibility
    defn.originals << member
  end
end

#build_singleton(type_name) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rbs/definition_builder/method_builder.rb', line 143

def build_singleton(type_name)
  singleton_methods[type_name] ||=
    begin
      entry = env.class_decls[type_name]
      type = Types::ClassSingleton.new(name: type_name, location: nil)

      Methods.new(type: type).tap do |methods|
        entry.decls.each do |d|
          d.decl.members.each do |member|
            case member
            when AST::Members::MethodDefinition
              if member.singleton?
                build_method(methods, type, member: member, accessibility: member.visibility || :public)
              end
            when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
              if member.kind == :singleton
                build_attribute(methods, type, member: member, accessibility: member.visibility || :public)
              end
            when AST::Members::Alias
              if member.kind == :singleton
                build_alias(methods, type, member: member)
              end
            end
          end
        end
      end.validate!
    end
end

#each_member_with_accessibility(members, accessibility: :public) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rbs/definition_builder/method_builder.rb', line 224

def each_member_with_accessibility(members, accessibility: :public)
  members.each do |member|
    case member
    when AST::Members::Public
      accessibility = :public
    when AST::Members::Private
      accessibility = :private
    else
      yield member, accessibility
    end
  end
end

#update(env:, except:) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/rbs/definition_builder/method_builder.rb', line 237

def update(env:, except:)
  MethodBuilder.new(env: env).tap do |copy|
    copy.instance_methods.merge!(instance_methods)
    copy.singleton_methods.merge!(singleton_methods)
    copy.interface_methods.merge!(interface_methods)

    except.each do |type_name|
      copy.instance_methods.delete(type_name)
      copy.singleton_methods.delete(type_name)
      copy.interface_methods.delete(type_name)
    end
  end
end