Class: Duby::AST::Raise

Inherits:
Node show all
Includes:
Valued
Defined in:
lib/duby/ast/flow.rb,
lib/duby/compiler.rb,
lib/duby/jvm/source_generator/precompile.rb

Instance Attribute Summary

Attributes included from Valued

#value

Attributes included from Typed

#type

Attributes inherited from Node

#children, #inferred_type, #newline, #parent, #position

Instance Method Summary collapse

Methods inherited from Node

#[], #each, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

#initialize(parent, line_number, &block) ⇒ Raise

Returns a new instance of Raise.



206
207
208
# File 'lib/duby/ast/flow.rb', line 206

def initialize(parent, line_number, &block)
  super(parent, line_number, &block)
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



241
242
243
244
# File 'lib/duby/compiler.rb', line 241

def compile(compiler, expression)
  compiler.line(line_number)
  compiler._raise(@exception)
end

#expr?(compiler) ⇒ Boolean

Returns:



126
127
128
# File 'lib/duby/jvm/source_generator/precompile.rb', line 126

def expr?(compiler)
  false
end

#infer(typer) ⇒ Object



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
# File 'lib/duby/ast/flow.rb', line 210

def infer(typer)
  unless resolved?
    @inferred_type = AST.unreachable_type
    throwable = AST.type('java.lang.Throwable')
    if children.size == 1
      arg_type = typer.infer(children[0])
      unless arg_type
        typer.defer(self)
        return
      end
      if throwable.assignable_from?(arg_type) && !arg_type.meta?
        @exception = children[0]
        resolved!
        return @inferred_type
      end
    end

    arg_types = children.map {|c| typer.infer(c)}
    if arg_types.any? {|c| c.nil?}
      typer.defer(self)
    else
      if arg_types[0] && throwable.assignable_from?(arg_types[0])
        klass = children.shift
      else
        klass = Constant.new(self, position, 'RuntimeException')
      end
      @exception = Call.new(self, position, 'new') do
        [klass, children, nil]
      end
      resolved!
      @children = [@exception]
      typer.infer(@exception)
    end
  end
  @inferred_type
end