Class: Idl::ReturnStatementAst

Inherits:
AstNode
  • Object
show all
Includes:
Returns
Defined in:
lib/idlc/ast.rb,
lib/idlc/passes/prune.rb,
lib/idlc/passes/gen_adoc.rb,
lib/idlc/passes/gen_option_adoc.rb,
lib/idlc/passes/find_return_values.rb

Overview

represents a function return statement

for example:

return 5;
return X[rs1] + 1;

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 inherited from AstNode

#declaration?, #executable?, extract_base_var_name, #find_ancestor, #find_dst_registers, #find_referenced_csrs, #find_src_registers, #freeze_tree, #input_file, input_from_source_yaml, #inspect, #internal_error, interval_from_source_yaml, #lineno, #lines_around, #nullify_assignments, #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, return_expression) ⇒ ReturnStatementAst

Returns a new instance of ReturnStatementAst.



6827
6828
6829
# File 'lib/idlc/ast.rb', line 6827

def initialize(input, interval, return_expression)
  super(input, interval, [return_expression])
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
# File 'lib/idlc/ast.rb', line 6881

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  ReturnStatementAst.new(
    input, interval,
    AstNode.from_h(yaml.fetch("expr"), source_mapper)
  )
end

Instance Method Details

#always_terminates?Boolean

Returns:

  • (Boolean)


869
# File 'lib/idlc/passes/prune.rb', line 869

def always_terminates? = true

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


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

def const_eval?(symtab) = return_expression.const_eval?(symtab)

#enclosing_functionObject



6856
6857
6858
# File 'lib/idlc/ast.rb', line 6856

def enclosing_function
  return_expression.enclosing_function
end

#expected_return_type(symtab) ⇒ Type

Returns The expected return type (as defined by the encolsing function).

Returns:

  • (Type)

    The expected return type (as defined by the encolsing function)



6842
6843
6844
# File 'lib/idlc/ast.rb', line 6842

def expected_return_type(symtab)
  return_expression.expected_return_type(symtab)
end

#gen_adoc(indent = 0, indent_spaces: 2) ⇒ Object



276
277
278
# File 'lib/idlc/passes/gen_adoc.rb', line 276

def gen_adoc(indent = 0, indent_spaces: 2)
  "#{' ' * indent}return #{return_value_nodes.map { |v| v.gen_adoc(0, indent_spaces:) }.join(', ')};"
end

#gen_option_adocObject



77
78
79
# File 'lib/idlc/passes/gen_option_adoc.rb', line 77

def gen_option_adoc
  return_expression.gen_option_adoc
end

#pass_find_return_values(values, current_conditions, symtab) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/idlc/passes/find_return_values.rb', line 19

def pass_find_return_values(values, current_conditions, symtab)
  # if the action is a ternary operator, there is another condition to consider
  if first.is_a?(TernaryOperatorExpressionAst)
    current_conditions.push first.condition
    values << [first.true_expression, current_conditions.clone]
    current_conditions.pop
    current_conditions.push first.condition.invert(symtab)
    values << [first.false_expression, current_conditions.clone]
    current_conditions.pop
  else
    values << [self, current_conditions.clone]
  end
end

#prune(symtab, forced_type: nil) ⇒ Object



871
872
873
# File 'lib/idlc/passes/prune.rb', line 871

def prune(symtab, forced_type: nil)
  ReturnStatementAst.new(input, interval, return_expression.prune(symtab))
end

#return_expressionObject



6823
6824
6825
# File 'lib/idlc/ast.rb', line 6823

def return_expression
  @children[0]
end

#return_type(symtab) ⇒ Type

Returns The actual return type.

Returns:

  • (Type)

    The actual return type



6837
6838
6839
# File 'lib/idlc/ast.rb', line 6837

def return_type(symtab)
  return_expression.return_type(symtab)
end

#return_types(symtab) ⇒ Array<Type>

Returns List of actual return types.

Returns:

  • (Array<Type>)

    List of actual return types



6832
6833
6834
# File 'lib/idlc/ast.rb', line 6832

def return_types(symtab)
  return_expression.return_types(symtab)
end

#return_value(symtab) ⇒ Integer, ...

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

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Returns:

  • (Integer)

    The return value, if it is integral

  • (Boolean)

    The return value, if it is boolean

  • (nil)

    if the return value is not compile-time-known

Raises:

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



6861
6862
6863
# File 'lib/idlc/ast.rb', line 6861

def return_value(symtab)
  return_expression.return_value(symtab)
end

#return_value_nodesArray<AstNode>

Returns List of return value nodes.

Returns:

  • (Array<AstNode>)

    List of return value nodes



6852
6853
6854
# File 'lib/idlc/ast.rb', line 6852

def return_value_nodes
  return_expression.return_value_nodes
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



6866
6867
6868
# File 'lib/idlc/ast.rb', line 6866

def return_values(symtab)
  return_expression.return_values(symtab)
end

#to_hObject



6874
6875
6876
6877
6878
# File 'lib/idlc/ast.rb', line 6874

def to_h = {
  "kind" => "stmt",
  "expr" => return_expression.to_h,
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "#{return_expression.to_idl};"

#type_check(symtab, strict:) ⇒ void

This method returns an undefined value.

type check this node and all children

Calls to #type and/or #value may depend on type_check being called first with the same symtab. If not, those functions may raise an AstNode::InternalError

Parameters:

Raises:



6847
6848
6849
# File 'lib/idlc/ast.rb', line 6847

def type_check(symtab, strict:)
  return_expression.type_check(symtab, strict:)
end