Class: Rubinius::AST::FormalArguments

Inherits:
Node
  • Object
show all
Defined in:
lib/compiler/ast/definitions.rb

Direct Known Subclasses

FormalArguments19

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, node_name, #pos, #set_child, transform, #transform, transform_comment, transform_kind, transform_kind=, transform_name, #visit, #walk

Constructor Details

#initialize(line, args, defaults, splat) ⇒ FormalArguments

Returns a new instance of FormalArguments.



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
# File 'lib/compiler/ast/definitions.rb', line 159

def initialize(line, args, defaults, splat)
  @line = line
  @defaults = nil
  @block_arg = nil

  if defaults
    defaults = DefaultArguments.new line, defaults
    @defaults = defaults
    @optional = defaults.names

    stop = defaults.names.first
    last = args.each_with_index { |a, i| break i if a == stop }
    @required = args[0, last]
  else
    @required = args.dup
    @optional = []
  end

  if splat.kind_of? Symbol
    args << splat
  elsif splat
    splat = :@unnamed_splat
    args << splat
  end
  @names = args
  @splat = splat
end

Instance Attribute Details

#block_argObject

Returns the value of attribute block_arg.



157
158
159
# File 'lib/compiler/ast/definitions.rb', line 157

def block_arg
  @block_arg
end

#defaultsObject

Returns the value of attribute defaults.



156
157
158
# File 'lib/compiler/ast/definitions.rb', line 156

def defaults
  @defaults
end

#namesObject

Returns the value of attribute names.



156
157
158
# File 'lib/compiler/ast/definitions.rb', line 156

def names
  @names
end

#optionalObject

Returns the value of attribute optional.



156
157
158
# File 'lib/compiler/ast/definitions.rb', line 156

def optional
  @optional
end

#requiredObject

Returns the value of attribute required.



156
157
158
# File 'lib/compiler/ast/definitions.rb', line 156

def required
  @required
end

#splatObject

Returns the value of attribute splat.



156
157
158
# File 'lib/compiler/ast/definitions.rb', line 156

def splat
  @splat
end

Instance Method Details

#map_arguments(scope) ⇒ Object



215
216
217
218
219
220
# File 'lib/compiler/ast/definitions.rb', line 215

def map_arguments(scope)
  @required.each { |arg| scope.new_local arg }
  @defaults.map_arguments scope if @defaults
  scope.new_local @splat if @splat.kind_of? Symbol
  scope.assign_local_reference @block_arg if @block_arg
end

#post_argsObject



198
199
200
# File 'lib/compiler/ast/definitions.rb', line 198

def post_args
  0
end

#required_argsObject Also known as: arity



192
193
194
# File 'lib/compiler/ast/definitions.rb', line 192

def required_args
  @required.size
end

#splat_indexObject



206
207
208
209
210
211
212
213
# File 'lib/compiler/ast/definitions.rb', line 206

def splat_index
  if @splat
    index = @names.size
    index -= 1 if @block_arg
    index -= 1 if @splat.kind_of? Symbol
    index
  end
end

#to_actual(line) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/compiler/ast/definitions.rb', line 222

def to_actual(line)
  arguments = ActualArguments.new line

  last = -1
  last -= 1 if @block_arg and @block_arg.name == names[last]
  last -= 1 if @splat == names[last]

  arguments.array = @names[0..last].map { |name| LocalVariableAccess.new line, name }

  if @splat.kind_of? Symbol
    arguments.splat = SplatValue.new(line, LocalVariableAccess.new(line, @splat))
  end

  arguments
end

#to_sexpObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/compiler/ast/definitions.rb', line 238

def to_sexp
  sexp = [:args]

  @required.each { |x| sexp << x }
  sexp += @defaults.names if @defaults

  if @splat == :@unnamed_splat
    sexp << :*
  elsif @splat
    sexp << :"*#{@splat}"
  end

  sexp += @post if @post

  sexp << :"&#{@block_arg.name}" if @block_arg

  sexp << [:block] + @defaults.to_sexp if @defaults

  sexp
end

#total_argsObject



202
203
204
# File 'lib/compiler/ast/definitions.rb', line 202

def total_args
  @required.size + @optional.size
end