Class: Idl::EnumCastAst

Inherits:
AstNode show all
Includes:
Rvalue
Defined in:
lib/idlc/ast.rb,
lib/idlc/passes/gen_adoc.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 Rvalue

#max_value, #min_value, #truncate, #values

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_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, #prune, #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, type_name, expression) ⇒ EnumCastAst

Returns a new instance of EnumCastAst.



1764
1765
1766
1767
# File 'lib/idlc/ast.rb', line 1764

def initialize(input, interval, type_name, expression)
  user_type_name = type_name.is_a?(UserTypeNameAst) ? type_name : UserTypeNameAst.new(type_name.input, type_name.interval, type_name.name)
  super(input, interval, [user_type_name, expression])
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
# File 'lib/idlc/ast.rb', line 1806

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  EnumCastAst.new(
    input, interval,
    T.cast(AstNode.from_h(yaml.fetch("enum_class_name"), source_mapper), UserTypeNameAst),
    T.cast(AstNode.from_h(yaml.fetch("expr"), source_mapper), RvalueAst)
  )
end

Instance Method Details

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


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

def const_eval?(symtab) = true

#enum_nameUserTypeAst

Returns Enum name.

Returns:

  • (UserTypeAst)

    Enum name



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

def enum_name = @children.fetch(0)

#expressionRvalue

Returns Value expression.

Returns:

  • (Rvalue)

    Value expression



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

def expression = @children.fetch(1)

#gen_adoc(indent, indent_spaces: 2) ⇒ Object



112
113
114
# File 'lib/idlc/passes/gen_adoc.rb', line 112

def gen_adoc(indent, indent_spaces: 2)
  "#{' ' * indent}$enum(#{enum_name.gen_adoc(0, indent_spaces:)}, #{expression.gen_adoc(0, indent_spaces:)})"
end

#to_hObject



1798
1799
1800
1801
1802
1803
# File 'lib/idlc/ast.rb', line 1798

def to_h = {
  "kind" => "bits_to_enum_cast",
  "enum_class_name" => enum_name.to_h,
  "expr" => expression.to_h,
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "$enum(#{enum_name.to_idl}, #{expression.to_idl})"

#type(symtab) ⇒ Object



1787
1788
1789
1790
# File 'lib/idlc/ast.rb', line 1787

def type(symtab)
  enum_def_type = symtab.get(enum_name.text_value)
  Type.new(:enum_ref, enum_class: enum_def_type)
end

#type_check(symtab, strict:) ⇒ Object



1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
# File 'lib/idlc/ast.rb', line 1769

def type_check(symtab, strict:)
  enum_name.type_check(symtab, strict:)
  expression.type_check(symtab, strict:)

  if expression.type(symtab).kind != :bits
    type_error "Can only cast from Bits<N> to enum"
  end

  enum_def_type = symtab.get(enum_name.text_value)
  type_error "No enum named #{enum_name.text_value}" if enum_def_type.nil?

  value_try do
    unless enum_def_type.element_values.include?(expression.value(symtab))
      type_error "#{expression.value(symtab)} is not a value in enum #{enum_name.text_value}"
    end
  end
end

#value(symtab) ⇒ Object



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

def value(symtab) = expression.value(symtab)