Class: Idl::EnumCastAst

Inherits:
AstNode show all
Includes:
Rvalue
Defined in:
lib/idl/ast.rb

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, user_type_name, expression) ⇒ EnumCastAst

Returns a new instance of EnumCastAst.



896
897
898
# File 'lib/idl/ast.rb', line 896

def initialize(input, interval, user_type_name, expression)
  super(input, interval, [user_type_name, expression])
end

Instance Method Details

#enum_nameUserTypeAst

Returns Enum name.

Returns:

  • (UserTypeAst)

    Enum name



891
# File 'lib/idl/ast.rb', line 891

def enum_name = @children[0]

#expressionRvalue

Returns Value expression.

Returns:

  • (Rvalue)

    Value expression



894
# File 'lib/idl/ast.rb', line 894

def expression = @children[1]

#to_idlObject



925
# File 'lib/idl/ast.rb', line 925

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

#type(symtab) ⇒ Object



918
919
920
921
# File 'lib/idl/ast.rb', line 918

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) ⇒ Object



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/idl/ast.rb', line 900

def type_check(symtab)
  enum_name.type_check(symtab)
  expression.type_check(symtab)

  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



923
# File 'lib/idl/ast.rb', line 923

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

#values(symtab) ⇒ Array<Integer>, ... Originally defined in module Rvalue

Return a complete list of possible compile-time-known values of the node, or raise a ValueError if the full list cannot be determined

For most AstNodes, this will just be a single-entry array

Parameters:

  • symtab (SymbolTable)

    The context for the evaulation

Returns:

  • (Array<Integer>)

    The complete list of compile-time-known values, when they are integral

  • (Array<Boolean>)

    The complete list of compile-time-known values, when they are booleans

  • (AstNode::ValueError)

    if the list of values is not knowable at compile time