Class: Idl::EnumDefinitionAst

Inherits:
AstNode
  • Object
show all
Includes:
Declaration
Defined in:
lib/idl/ast.rb

Overview

Node representing an IDL enum defintion

# this will result in an EnumDefinitionAst
enum PrivilegeMode {
  M  0b011
  S  0b001
  HS 0b001 # alias for S when H extension is used
  U  0b000
  VS 0b101
  VU 0b100
}

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, user_type, element_names, element_values) ⇒ EnumDefinitionAst

Returns a new instance of EnumDefinitionAst.



1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/idl/ast.rb', line 1000

def initialize(input, interval, user_type, element_names, element_values)
  super(input, interval, [user_type] + element_names + element_values.reject{ |e| e.nil? })
  @user_type = user_type
  @element_name_asts = element_names
  @element_value_asts = element_values

  @type = EnumerationType.new(name, self.element_names, self.element_values)
end

Instance Method Details

#add_symbol(symtab) ⇒ Object

Add symbol(s) at the outermost scope of the symbol table

Parameters:

  • symtab (SymbolTable)

    Symbol table at the scope that the symbol(s) will be inserted



1051
1052
1053
1054
1055
1056
# File 'lib/idl/ast.rb', line 1051

def add_symbol(symtab)
  internal_error "All enums should be declared in global scope" unless symtab.levels == 1

  internal_error "Type is nil?" if type(symtab).nil?
  symtab.add!(name, type(symtab))
end

#element_namesArray<String>

Returns Array of all element names, in the same order as those from #element_values.

Returns:

  • (Array<String>)

    Array of all element names, in the same order as those from #element_values



1010
1011
1012
1013
1014
# File 'lib/idl/ast.rb', line 1010

def element_names
  return @element_names unless @element_names.nil?

  @element_names = @element_name_asts.map(&:text_value)
end

#element_valuesArray<Integer>

Returns Array of all element values, in the same order as those from #element_names. All values will be assigned their final values, even those with auto-numbers.

Returns:

  • (Array<Integer>)

    Array of all element values, in the same order as those from #element_names. All values will be assigned their final values, even those with auto-numbers



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/idl/ast.rb', line 1019

def element_values
  return @element_values unless @element_values.nil?

  next_auto_value = 0
  @element_values = []

  @element_value_asts.each do |e|
    if e.nil?
      @element_values << next_auto_value
      next_auto_value += 1
    else
      @element_values << e.value(nil)
      next_auto_value = @element_values.last + 1
    end
  end

  @element_values
end

#nameString

Returns enum name.

Returns:

  • (String)

    enum name



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

def name = @user_type.text_value

#to_idlString

Return valid IDL representation of the node (and its subtree)

Returns:

  • (String)

    IDL code for the node



1070
1071
1072
1073
1074
1075
1076
1077
# File 'lib/idl/ast.rb', line 1070

def to_idl
  idl = "enum #{name} { "
  element_names.each_index do |idx|
    idl << "#{element_names[idx]} #{element_values[idx]} "
  end
  idl << "}"
  idl
end

#type(symtab) ⇒ Type

Return the type of this node

Parameters:

Returns:

  • (Type)

    The type of the node



1059
1060
1061
# File 'lib/idl/ast.rb', line 1059

def type(symtab)
  @type
end

#type_check(symtab) ⇒ 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:



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/idl/ast.rb', line 1039

def type_check(symtab)
  @element_value_asts.each do |e|
    unless e.nil?
      e.type_check(symtab)
    end
  end

  add_symbol(symtab)
  @user_type.type_check(symtab)
end

#value(_symtab, _archdef) ⇒ Integer, Boolean

Return the compile-time-known value of the node

Parameters:

Returns:

  • (Integer)

    if the compile-time-known value is an integer

  • (Boolean)

    if the compile-time-known value is a boolean

Raises:



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

def value(_symtab, _archdef) = raise InternalError, "Enum defintions have no value"