Class: Idl::ElseIfAst

Inherits:
AstNode show all
Includes:
Returns
Defined in:
lib/idlc/ast.rb,
lib/idlc/passes/prune.rb

Constant Summary

Constants inherited from AstNode

AstNode::Bits1Type, AstNode::Bits32Type, AstNode::Bits64Type, AstNode::BoolType, AstNode::ConstBoolType, AstNode::PossiblyUnknownBits1Type, AstNode::PossiblyUnknownBits32Type, AstNode::PossiblyUnknownBits64Type, AstNode::ReachableFunctionCacheType, AstNode::StringType, AstNode::VoidType

Instance Attribute Summary

Attributes inherited from AstNode

#children, #input, #interval, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Returns

#expected_return_type

Methods inherited from AstNode

#always_terminates?, #declaration?, #executable?, extract_base_var_name, #find_ancestor, #find_dst_registers, #find_referenced_csrs, #find_src_registers, #freeze_tree, #gen_adoc, #gen_option_adoc, #input_file, input_from_source_yaml, #inspect, #internal_error, interval_from_source_yaml, #lineno, #lines_around, #nullify_assignments, #pass_find_return_values, #path, #print_ast, #reachable_exceptions, #reachable_functions, #set_input_file, #set_input_file_unless_already_set, #source_line_file_offsets, #source_starting_offset, #source_yaml, #starting_line, #text_value, #truncation_warn, #type_error, #unindent, value_else, #value_else, value_error, #value_error, value_try, #value_try, write_back_nested

Constructor Details

#initialize(input, interval, body_interval, cond, body_stmts) ⇒ ElseIfAst

Returns a new instance of ElseIfAst.



8937
8938
8939
8940
# File 'lib/idlc/ast.rb', line 8937

def initialize(input, interval, body_interval, cond, body_stmts)
  body = IfBodyAst.new(input, body_interval, body_stmts)
  super(input, interval, [cond, body])
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
# File 'lib/idlc/ast.rb', line 8999

def self.from_h(yaml, source_mapper)
  raise "Bad YAML" unless yaml.key?("kind") && yaml.fetch("kind") == "else_if_stmt"

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  body = T.cast(AstNode.from_h(yaml.fetch("body"), source_mapper), IfBodyAst)
  ElseIfAst.new(
    input, interval,
    body.interval,
    T.cast(AstNode.from_h(yaml.fetch("condition"), source_mapper), RvalueAst),
    body.stmts
  )
end

Instance Method Details

#bodyObject



8934
# File 'lib/idlc/ast.rb', line 8934

def body = T.cast(@children.fetch(1), IfBodyAst)

#condObject



8931
# File 'lib/idlc/ast.rb', line 8931

def cond = T.cast(@children.fetch(0), RvalueAst)

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


8926
8927
8928
# File 'lib/idlc/ast.rb', line 8926

def const_eval?(symtab)
  cond.const_eval?(symtab) && body.const_eval?(symtab)
end

#prune(symtab, forced_type: nil) ⇒ Object



514
515
516
517
518
519
520
521
# File 'lib/idlc/passes/prune.rb', line 514

def prune(symtab, forced_type: nil)
  ElseIfAst.new(
    input, interval,
    body.interval,
    cond.prune(symtab),
    body.prune(symtab).stmts
  )
end

#return_type(symtab) ⇒ Object



8958
8959
8960
8961
# File 'lib/idlc/ast.rb', line 8958

def return_type(symtab)
  # the return type is determined by the function
  t = expected_return_type(symtab)
end

#return_value(symtab) ⇒ Object



8963
8964
8965
8966
8967
8968
8969
8970
8971
# File 'lib/idlc/ast.rb', line 8963

def return_value(symtab)
  value_result = value_try do
    if cond.value(symtab)
      body.return_value(symtab)
    else
      nil
    end
  end
end

#return_values(symtab) ⇒ Array<Integer>, Array<Boolean>

Evaluate all possible compile-time return values of this node, or, if the node does not return (e.g., because it is an IfAst but there is no return on a possible path), execute the node and update the symtab

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Returns:

  • (Array<Integer>)

    The possible return values. Will be an empty array if there are no return values

  • (Array<Boolean>)

    The possible return values. Will be an empty array if there are no return values

Raises:

  • ValueError if, during evaluation, a node without a compile-time value is found



8974
8975
8976
8977
8978
8979
8980
8981
8982
# File 'lib/idlc/ast.rb', line 8974

def return_values(symtab)
  value_result = value_try do
    return cond.value(symtab) ? body.return_values(symtab) : EMPTY_ARRAY
  end
  value_else(value_result) do
    # might be taken, so add the possible return values
    body.return_values(symtab)
  end
end

#to_hObject



8991
8992
8993
8994
8995
8996
# File 'lib/idlc/ast.rb', line 8991

def to_h = {
  "kind" => "else_if_stmt",
  "condition" => cond.to_h,
  "body" => body.to_h,
  "source" => source_yaml
}

#to_idlObject



8986
8987
8988
# File 'lib/idlc/ast.rb', line 8986

def to_idl
  " else if (#{cond.to_idl}) { #{body.to_idl} }"
end

#type_check(symtab, strict:) ⇒ Object



8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
# File 'lib/idlc/ast.rb', line 8942

def type_check(symtab, strict:)
  cond.type_check(symtab, strict:)

  cond_value = T.let(nil, T.nilable(ValueRbType))
  value_try do
    cond_value = cond.value(symtab)
  end

  unless cond.type(symtab).convertable_to?(:boolean)
    type_error "'#{cond.text_value}' is not boolean"
  end

  body.type_check(symtab, strict:) unless cond_value == false
end