Class: Steep::Interface::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/interface/builder.rb

Defined Under Namespace

Classes: RecursiveDefinitionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signatures:) ⇒ Builder

Returns a new instance of Builder.



23
24
25
26
27
28
29
# File 'lib/steep/interface/builder.rb', line 23

def initialize(signatures:)
  @signatures = signatures
  @class_cache = {}
  @module_cache = {}
  @instance_cache = {}
  @interface_cache = {}
end

Instance Attribute Details

#class_cacheObject (readonly)

Returns the value of attribute class_cache.



18
19
20
# File 'lib/steep/interface/builder.rb', line 18

def class_cache
  @class_cache
end

#instance_cacheObject (readonly)

Returns the value of attribute instance_cache.



20
21
22
# File 'lib/steep/interface/builder.rb', line 20

def instance_cache
  @instance_cache
end

#interface_cacheObject (readonly)

Returns the value of attribute interface_cache.



21
22
23
# File 'lib/steep/interface/builder.rb', line 21

def interface_cache
  @interface_cache
end

#module_cacheObject (readonly)

Returns the value of attribute module_cache.



19
20
21
# File 'lib/steep/interface/builder.rb', line 19

def module_cache
  @module_cache
end

#signaturesObject (readonly)

Returns the value of attribute signatures.



17
18
19
# File 'lib/steep/interface/builder.rb', line 17

def signatures
  @signatures
end

Instance Method Details

#absolute_type(type, current:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/steep/interface/builder.rb', line 31

def absolute_type(type, current:)
  case type
  when AST::Types::Name::Instance
    signature = signatures.find_class_or_module(type.name, current_module: current)
    AST::Types::Name::Instance.new(
      name: signature.name,
      args: type.args.map {|arg| absolute_type(arg, current: current) },
      location: type.location
    )
  when AST::Types::Name::Class
    signature = signatures.find_class(type.name, current_module: current)
    AST::Types::Name::Class.new(
      name: signature.name,
      constructor: type.constructor,
      location: type.location
    )
  when AST::Types::Name::Module
    signature = signatures.find_class_or_module(type.name, current_module: current)
    AST::Types::Name::Module.new(
      name: signature.name,
      location: type.location
    )
  when AST::Types::Name::Interface
    signature = signatures.find_interface(type.name, namespace: current)
    AST::Types::Name::Interface.new(
      name: signature.name,
      args: type.args.map {|arg| absolute_type(arg, current: current) },
      location: type.location
    )
  when AST::Types::Name::Alias
    signature = signatures.find_alias(type.name, namespace: current)
    AST::Types::Name::Alias.new(
      name: signature.name,
      args: type.args.map {|arg| absolute_type(arg, current: current) },
      location: type.location
    )
  when AST::Types::Union
    AST::Types::Union.build(
      types: type.types.map {|ty| absolute_type(ty, current: current) },
      location: type.location
    )
  when AST::Types::Intersection
    AST::Types::Intersection.build(
      types: type.types.map {|ty| absolute_type(ty, current: current) },
      location: type.location
    )
  when AST::Types::Tuple
    AST::Types::Tuple.new(
      types: type.types.map {|ty| absolute_type(ty, current:current) },
      location: type.location
    )
  when AST::Types::Proc
    AST::Types::Proc.new(
      params: type.params.map_type {|ty| absolute_type(ty, current: current) },
      return_type: absolute_type(type.return_type, current: current),
      location: type.location
    )
  when AST::Types::Record
    AST::Types::Record.new(
      elements: type.elements.transform_values {|ty| absolute_type(ty, current: current) },
      location: type.location
    )
  else
    type
  end
end

#add_method(type_name, method, methods:, extra_attributes: [], current:) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/steep/interface/builder.rb', line 171

def add_method(type_name, method, methods:, extra_attributes: [], current:)
  super_method = methods[method.name]
  new_method = Method.new(
    type_name: type_name,
    name: method.name,
    types: method.types.flat_map do |method_type|
      case method_type
      when AST::MethodType
        [method_type_to_method_type(method_type, current: current)]
      when AST::MethodType::Super
        if super_method
          super_method.types
        else
          Steep.logger.error "`super` specified in method type, but cannot find super method of `#{method.name}` in `#{type_name}` (#{method.location.name || "-"}:#{method.location})"
          []
        end
      end
    end,
    super_method: super_method,
    attributes: method.attributes + extra_attributes
  )

  methods[method.name] = if super_method&.include_in_chain?(new_method)
                           super_method
                         else
                           new_method
                         end
end

#assert_absolute_name!(name) ⇒ Object



115
116
117
# File 'lib/steep/interface/builder.rb', line 115

def assert_absolute_name!(name)
  raise "Name should be absolute: #{name}" unless name.absolute?
end

#build_class(module_name, constructor:) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/steep/interface/builder.rb', line 119

def build_class(module_name, constructor:)
  assert_absolute_name! module_name
  signature = signatures.find_class(module_name, current_module: AST::Namespace.root)
  cache_interface(class_cache, key: [signature.name, !!constructor]) do
    class_to_interface(signature, constructor: constructor)
  end
end

#build_instance(module_name) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/steep/interface/builder.rb', line 135

def build_instance(module_name)
  assert_absolute_name! module_name
  signature = signatures.find_class_or_module(module_name, current_module: AST::Namespace.root)
  cache_interface(instance_cache, key: signature.name) do
    instance_to_interface(signature)
  end
end

#build_interface(interface_name) ⇒ Object



143
144
145
146
147
148
# File 'lib/steep/interface/builder.rb', line 143

def build_interface(interface_name)
  signature = signatures.find_interface(interface_name)
  cache_interface(interface_cache, key: [signature.name]) do
    interface_to_interface(nil, signature)
  end
end

#build_module(module_name) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/steep/interface/builder.rb', line 127

def build_module(module_name)
  assert_absolute_name! module_name
  signature = signatures.find_module(module_name, current_module: AST::Namespace.root)
  cache_interface(module_cache, key: signature.name) do
    module_to_interface(signature)
  end
end

#cache_interface(cache, key:, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/steep/interface/builder.rb', line 98

def cache_interface(cache, key:, &block)
  cached = cache[key]

  case cached
  when nil
    cache[key] = key
    cache[key] = yield
  when key
    raise RecursiveDefinitionError, key
  else
    cached
  end
rescue RecursiveDefinitionError => exn
  cache.delete key
  raise exn
end

#class_to_interface(sig, constructor:) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/steep/interface/builder.rb', line 200

def class_to_interface(sig, constructor:)
  module_name = sig.name
  namespace = module_name.namespace.append(module_name.name)

  supers = []
  methods = {
    new: Method.new(
      type_name: Names::Module.parse(name: "__Builtin__"),
      name: :new,
      types: [
        MethodType.new(type_params: [],
                       params: Params.empty,
                       block: nil,
                       return_type: AST::Types::Instance.new,
                       location: nil
        )
      ],
      super_method: nil,
      attributes: [:incompatible]
    )
  }

  klass = build_instance(AST::Builtin::Class.module_name)
  instantiated = klass.instantiate(
    type: AST::Types::Self.new,
    args: [],
    instance_type: AST::Types::Instance.new,
    module_type: AST::Types::Class.new
  )
  methods.merge!(instantiated.methods)

  unless module_name == AST::Builtin::BasicObject.module_name
    super_class_name = sig.super_class&.name&.yield_self {|name| signatures.find_class(name, current_module: namespace).name } || AST::Builtin::Object.module_name
    class_interface = build_class(super_class_name, constructor: constructor)
    merge_mixin(class_interface, [], methods: methods, ivars: {}, supers: supers, current: namespace)
  end

  sig.members.each do |member|
    case member
    when AST::Signature::Members::Include
      member_name = signatures.find_module(member.name, current_module: namespace).name
      build_module(member_name).yield_self do |module_interface|
        merge_mixin(module_interface,
                    [],
                    methods: methods,
                    supers: supers,
                    ivars: {},
                    current: namespace)
      end
    when AST::Signature::Members::Extend
      member_name = signatures.find_module(member.name, current_module: namespace).name
      build_instance(member_name).yield_self do |module_interface|
        merge_mixin(module_interface,
                    member.args.map {|type| absolute_type(type, current: namespace) },
                    methods: methods,
                    ivars: {},
                    supers: supers,
                    current: namespace)
      end
    end
  end

  sig.members.each do |member|
    case member
    when AST::Signature::Members::Method
      case
      when member.module_method?
        add_method(module_name, member, methods: methods, current: namespace)
      when member.instance_method? && member.name == :initialize
        if constructor
          methods[:new] = Method.new(
            type_name: module_name,
            name: :new,
            types: member.types.map do |method_type_sig|
              method_type = method_type_to_method_type(method_type_sig, current: namespace).with(return_type: AST::Types::Instance.new)
              args = (sig.params&.variables || []) + method_type.type_params

              method_type.with(
                type_params: args,
                return_type: AST::Types::Instance.new
              )
            end,
            super_method: nil,
            attributes: [:incompatible]
          )
        end
      end
    end
  end

  signatures.find_extensions(sig.name).each do |ext|
    ext.members.each do |member|
      case member
      when AST::Signature::Members::Method
        if member.module_method?
          add_method(module_name, member, methods: methods, current: namespace)
        end
      end
    end
  end

  if methods[:new]&.type_name == AST::Builtin::Class.module_name
    new_types = [MethodType.new(type_params: [],
                                params: Params.empty,
                                block: nil,
                                return_type: AST::Types::Instance.new,
                                location: nil)]
    methods[:new] = methods[:new].with_types(new_types)
  end

  unless constructor
    methods.delete(:new)
  end

  Abstract.new(
    name: module_name,
    params: [],
    methods: methods,
    supers: supers,
    ivar_chains: {}
  )
end

#instance_to_interface(sig) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/steep/interface/builder.rb', line 400

def instance_to_interface(sig)
  module_name = sig.name
  namespace = module_name.namespace.append(module_name.name)

  params = sig.params&.variables || []
  supers = []
  methods = {}
  ivar_chains = {}

  if sig.is_a?(AST::Signature::Class)
    unless sig.name == AST::Builtin::BasicObject.module_name
      super_class_name = sig.super_class&.name || AST::Builtin::Object.module_name
      if super_class_name.relative?
        super_class_name = signatures.find_class(super_class_name, current_module: namespace).name
      end
      super_class_interface = build_instance(super_class_name)

      supers.push(*super_class_interface.supers)
      instantiated = super_class_interface.instantiate(
        type: AST::Types::Self.new,
        args: (sig.super_class&.args || []).map {|type| absolute_type(type, current: namespace) },
        instance_type: AST::Types::Instance.new,
        module_type: AST::Types::Class.new
      )

      methods.merge!(instantiated.methods)
      merge_ivars(ivar_chains, instantiated.ivars)
    end
  end

  if sig.is_a?(AST::Signature::Module)
    if sig.self_type
      supers << absolute_type(sig.self_type, current: namespace)
    end
  end

  sig.members.each do |member|
    case member
    when AST::Signature::Members::Include
      member_name = signatures.find_module(member.name, current_module: namespace).name
      build_instance(member_name).yield_self do |module_interface|
        merge_mixin(module_interface,
                    member.args.map {|type| absolute_type(type, current: namespace) },
                    methods: methods,
                    ivars: ivar_chains,
                    supers: supers,
                    current: namespace)
      end
    end
  end

  sig.members.each do |member|
    case member
    when AST::Signature::Members::Method
      if member.instance_method?
        extra_attrs = member.name == :initialize ? [:incompatible, :private] : []
        add_method(module_name, member, methods: methods, extra_attributes: extra_attrs, current: namespace)
      end
    when AST::Signature::Members::Ivar
      merge_ivars(ivar_chains,
                  { member.name => absolute_type(member.type, current: namespace) })
    when AST::Signature::Members::Attr
      merge_attribute(sig, ivar_chains, methods, module_name, member, current: namespace)
    end
  end

  sig.members.each do |member|
    case member
    when AST::Signature::Members::MethodAlias
      method = methods[member.original_name]
      if method
        methods[member.new_name] =  Method.new(
          type_name: module_name,
          name: member.new_name,
          types: method.types,
          super_method: nil,
          attributes: method.attributes
        )
      else
        Steep.logger.error "Cannot alias find original method `#{member.original_name}` for `#{member.new_name}` in #{module_name} (#{member.location.name || '-'}:#{member.location})"
      end
    end
  end

  signatures.find_extensions(sig.name).each do |ext|
    ext.members.each do |member|
      case member
      when AST::Signature::Members::Method
        if member.instance_method?
          add_method(module_name, member, methods: methods, current: namespace)
        end
      end
    end
  end

  Abstract.new(
    name: module_name,
    params: params,
    methods: methods,
    supers: supers,
    ivar_chains: ivar_chains
  )
end

#interface_to_interface(_, sig) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/steep/interface/builder.rb', line 553

def interface_to_interface(_, sig)
  type_name = sig.name

  variables = sig.params&.variables || []
  methods = sig.methods.each.with_object({}) do |method, methods|
    methods[method.name] = Method.new(
      type_name: type_name,
      name: method.name,
      types: method.types.map do |method_type|
        method_type_to_method_type(method_type, current: type_name.namespace)
      end,
      super_method: nil,
      attributes: []
    )
  end

  Abstract.new(
    name: type_name,
    params: variables,
    methods: methods,
    supers: [],
    ivar_chains: {}
  )
end

#merge_attribute(sig, ivar_chains, methods, type_name, member, current:) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/steep/interface/builder.rb', line 504

def merge_attribute(sig, ivar_chains, methods, type_name, member, current:)
  if member.ivar != false
    ivar_name = member.ivar || "@#{member.name}".to_sym
    merge_ivars(ivar_chains,
                { ivar_name => absolute_type(member.type, current: current) })
  end

  reader_method = AST::Signature::Members::Method.new(
    location: member.location,
    name: member.name,
    kind: :instance,
    types: [
      AST::MethodType.new(location: member.type.location,
                          type_params: nil,
                          params: nil,
                          block: nil,
                          return_type: member.type)
    ],
    attributes: []
  )
  add_method(type_name, reader_method, methods: methods, current: current)

  if member.accessor?
    writer_method = AST::Signature::Members::Method.new(
      location: member.location,
      name: "#{member.name}=".to_sym,
      kind: :instance,
      types: [
        AST::MethodType.new(location: member.type.location,
                            type_params: nil,
                            params: AST::MethodType::Params::Required.new(
                              location: member.type.location,
                              type: member.type
                            ),
                            block: nil,
                            return_type: member.type)
      ],
      attributes: []
    )
    add_method(type_name, writer_method, methods: methods, current: current)
  end
end

#merge_ivars(dest, new_vars) ⇒ Object



547
548
549
550
551
# File 'lib/steep/interface/builder.rb', line 547

def merge_ivars(dest, new_vars)
  new_vars.each do |name, new_type|
    dest[name] = IvarChain.new(type: new_type, parent: dest[name])
  end
end

#merge_mixin(interface, args, methods:, ivars:, supers:, current:) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/steep/interface/builder.rb', line 150

def merge_mixin(interface, args, methods:, ivars:, supers:, current:)
  supers.push(*interface.supers)

  instantiated = interface.instantiate(
    type: AST::Types::Self.new,
    args: args,
    instance_type: AST::Types::Instance.new,
    module_type: AST::Types::Class.new
  )

  methods.merge!(instantiated.methods) do |_, super_method, new_method|
    if super_method.include_in_chain?(new_method)
      super_method
    else
      new_method.with_super(super_method)
    end
  end

  merge_ivars ivars, instantiated.ivars
end

#method_type_to_method_type(method_type, current:) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/steep/interface/builder.rb', line 578

def method_type_to_method_type(method_type, current:)
  type_params = method_type.type_params&.variables || []
  params = params_to_params(method_type.params, current: current)
  block = method_type.block && Block.new(
    type: AST::Types::Proc.new(
      params: params_to_params(method_type.block.params, current: current),
      return_type: absolute_type(method_type.block.return_type, current: current),
      location: method_type.block.location,
      ),
    optional: method_type.block.optional
  )

  MethodType.new(
    type_params: type_params,
    return_type: absolute_type(method_type.return_type, current: current),
    block: block,
    params: params,
    location: method_type.location
  )
end

#module_to_interface(sig) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/steep/interface/builder.rb', line 323

def module_to_interface(sig)
  module_name = sig.name
  namespace = module_name.namespace.append(module_name.name)

  supers = [sig.self_type].compact.map {|type| absolute_type(type, current: namespace) }
  methods = {}
  ivar_chains = {}

  module_instance = build_instance(AST::Builtin::Module.module_name)
  instantiated = module_instance.instantiate(
    type: AST::Types::Self.new,
    args: [],
    instance_type: AST::Types::Instance.new,
    module_type: AST::Types::Class.new
  )
  methods.merge!(instantiated.methods)

  sig.members.each do |member|
    case member
    when AST::Signature::Members::Include
      member_name = signatures.find_module(member.name, current_module: namespace).name
      build_module(member_name).yield_self do |module_interface|
        merge_mixin(module_interface,
                    member.args.map {|type| absolute_type(type, current: namespace) },
                    methods: methods,
                    ivars: ivar_chains,
                    supers: supers,
                    current: namespace)
      end
    when AST::Signature::Members::Extend
      member_name = signatures.find_module(member.name, current_module: namespace).name
      build_instance(member_name).yield_self do |module_interface|
        merge_mixin(module_interface,
                    member.args.map {|type| absolute_type(type, current: namespace) },
                    methods: methods,
                    ivars: ivar_chains,
                    supers: supers,
                    current: namespace)

      end
    end
  end

  sig.members.each do |member|
    case member
    when AST::Signature::Members::Method
      if member.module_method?
        add_method(module_name, member, methods: methods, current: namespace)
      end
    when AST::Signature::Members::Ivar
      merge_ivars(ivar_chains,
                  { member.name => absolute_type(member.type, current: namespace) })
    when AST::Signature::Members::Attr
      merge_attribute(sig, ivar_chains, methods, module_name, member)
    end
  end

  signatures.find_extensions(module_name).each do |ext|
    ext.members.each do |member|
      case member
      when AST::Signature::Members::Method
        if member.module_method?
          add_method(module_name, member, methods: methods, current: namespace)
        end
      end
    end
  end

  Abstract.new(
    name: module_name,
    params: [],
    methods: methods,
    supers: supers,
    ivar_chains: ivar_chains
  )
end

#params_to_params(params, current:) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/steep/interface/builder.rb', line 599

def params_to_params(params, current:)
  required = []
  optional = []
  rest = nil
  required_keywords = {}
  optional_keywords = {}
  rest_keywords = nil

  while params
    case params
    when AST::MethodType::Params::Required
      required << absolute_type(params.type, current: current)
    when AST::MethodType::Params::Optional
      optional << absolute_type(params.type, current: current)
    when AST::MethodType::Params::Rest
      rest = absolute_type(params.type, current: current)
    when AST::MethodType::Params::RequiredKeyword
      required_keywords[params.name] = absolute_type(params.type, current: current)
    when AST::MethodType::Params::OptionalKeyword
      optional_keywords[params.name] = absolute_type(params.type, current: current)
    when AST::MethodType::Params::RestKeyword
      rest_keywords = absolute_type(params.type, current: current)
      break
    end
    params = params.next_params
  end

  Params.new(
    required: required,
    optional: optional,
    rest: rest,
    required_keywords: required_keywords,
    optional_keywords: optional_keywords,
    rest_keywords: rest_keywords
  )
end