Class: Steep::TypeInference::SendArgs

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/type_inference/send_args.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, block_pass_arg:) ⇒ SendArgs

Returns a new instance of SendArgs.



7
8
9
10
# File 'lib/steep/type_inference/send_args.rb', line 7

def initialize(args:, block_pass_arg:)
  @args = args
  @block_pass_arg = block_pass_arg
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/steep/type_inference/send_args.rb', line 4

def args
  @args
end

#block_pass_argObject (readonly)

Returns the value of attribute block_pass_arg.



5
6
7
# File 'lib/steep/type_inference/send_args.rb', line 5

def block_pass_arg
  @block_pass_arg
end

Class Method Details

.from_nodes(nodes) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/steep/type_inference/send_args.rb', line 12

def self.from_nodes(nodes)
  nodes = nodes.dup

  args = []
  block_pass_arg = nil

  if nodes.last&.type == :block_pass
    block_pass_arg = nodes.pop
  end

  nodes.each do |node|
    args << node
  end

  new(args: args, block_pass_arg: block_pass_arg)
end

Instance Method Details

#add_pair(pairs, pair) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/steep/type_inference/send_args.rb', line 92

def add_pair(pairs, pair)
  pairs.map do |ps|
    if block_given?
      yield ps, pair
    else
      [pair] + ps
    end
  end
end

#drop_firstObject



29
30
31
32
# File 'lib/steep/type_inference/send_args.rb', line 29

def drop_first
  raise "Cannot drop first from empty args" if args.empty?
  self.class.new(args: args.drop(1), block_pass_arg: block_pass_arg)
end

#drop_lastObject



34
35
36
37
# File 'lib/steep/type_inference/send_args.rb', line 34

def drop_last
  raise "Cannot drop last from empty args" if args.empty?
  self.class.new(args: args.take(args.size - 1), block_pass_arg: block_pass_arg)
end

#each_keyword_argObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/steep/type_inference/send_args.rb', line 39

def each_keyword_arg
  if block_given?
    if kw_args
      kw_args.children.each do |node|
        if node.type == :pair
          yield node
        end
      end
    end
  else
    enum_for :each_keyword_arg
  end
end

#group_pairs(pairs) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/steep/type_inference/send_args.rb', line 69

def group_pairs(pairs)
  types = pairs.each_with_object({}) do |pair, hash|
    case pair
    when Array
      node, type = pair
      hash[node.__id__] ||= [node]
      hash[node.__id__] << type
    else
      hash[node.__id__] = pair
    end
  end

  types.map do |_, array|
    case array
    when Array
      node, *types_ = array
      [node, AST::Types::Intersection.build(types: types_)]
    else
      array
    end
  end
end

#kwsplat_nodesObject



53
54
55
56
57
58
59
60
61
# File 'lib/steep/type_inference/send_args.rb', line 53

def kwsplat_nodes
  if kw_args
    kw_args.children.select do |node|
      node.type == :kwsplat
    end
  else
    []
  end
end

#zip0(params, block_type) ⇒ Object



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
142
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/steep/type_inference/send_args.rb', line 102

def zip0(params, block_type)
  case
  when params.empty? && args.empty?
    [[]]

  when params.required.any?
    if args.any?
      first_arg = args[0]

      case first_arg.type
      when :splat
        []
      else
        rest = drop_first.zip0(params.drop_first, block_type)
        pair = [first_arg, params.required[0]]

        add_pair(rest, pair)
      end
    else
      []
    end

  when params.has_keywords? && params.required_keywords.any?
    if args.any?
      rest = drop_last.zip0(params.without_keywords, block_type)
      last_arg = args.last

      return [] if last_arg.type == :splat

      add_pair(rest, last_arg) do |ps, p|
        ps + [p]
      end
    else
      []
    end

  when params.has_keywords? && params.required_keywords.empty?
    if args.any?
      rest = drop_last.zip0(params.without_keywords, block_type)
      last_arg = args.last

      no_keyword = zip0(params.without_keywords, block_type)

      if last_arg.type == :splat
        no_keyword
      else
        add_pair(rest, last_arg) do |ps, p|
          ps + [p]
        end + no_keyword
      end
    else
      zip0(params.without_keywords, block_type)
    end

  when params.optional.any?
    if args.any?
      first_arg = args[0]

      case first_arg.type
      when :splat
        rest = zip0(params.drop_first, block_type)
        pair = [args[0], AST::Builtin::Array.instance_type(params.optional[0])]
      else
        rest = drop_first.zip0(params.drop_first, block_type)
        pair = [args[0], params.optional[0]]
      end

      add_pair(rest, pair)
    else
      zip0(params.drop_first, block_type)
    end

  when params.rest
    if args.any?
      rest = drop_first.zip0(params, block_type)
      first_arg = args[0]

      case first_arg.type
      when :splat
        pair = [first_arg, AST::Builtin::Array.instance_type(params.rest)]
      else
        pair = [first_arg, params.rest]
      end

      add_pair(rest, pair)
    else
      zip0(params.drop_first, block_type)
    end
  else
    []
  end
end

#zips(params, block_type) ⇒ Object



63
64
65
66
67
# File 'lib/steep/type_inference/send_args.rb', line 63

def zips(params, block_type)
  zip0(params, block_type).map do |pairs|
    group_pairs(pairs)
  end
end