Class: Idl::BuiltinEnumDefinitionAst

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

Overview

represents a builtin (auto-generated from config) enum definition

# this will result in a BuiltinEnumDefinitionAst
builtin enum ExtensionName

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, user_type) ⇒ BuiltinEnumDefinitionAst

Returns a new instance of BuiltinEnumDefinitionAst.



1094
1095
1096
1097
# File 'lib/idl/ast.rb', line 1094

def initialize(input, interval, user_type)
  super(input, interval, [user_type])
  @user_type = user_type
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



1151
1152
1153
1154
1155
# File 'lib/idl/ast.rb', line 1151

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

  symtab.add!(name, type(symtab))
end

#element_names(symtab) ⇒ Object



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/idl/ast.rb', line 1117

def element_names(symtab)
  case name
  when "ExtensionName"
    symtab.archdef.extensions.map(&:name)
  when "ExceptionCode"
    symtab.archdef.exception_codes.map(&:var)
  when "InterruptCode"
    symtab.archdef.interrupt_codes.map(&:var)
  else
    type_error "Unknown builtin enum type '#{name}'"
  end
end

#element_values(symtab) ⇒ Object



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
# File 'lib/idl/ast.rb', line 1130

def element_values(symtab)
  case name
  when "ExtensionName"
    (0...symtab.archdef.extensions.size).to_a
  when "ExceptionCode"
    symtab.archdef.exception_codes.map(&:num)
  when "InterruptCode"
    symtab.archdef.interrupt_codes.map(&:num)
  else
    type_error "Unknown builtin enum type '#{name}'"
  end
end

#freeze_tree(global_symtab) ⇒ Object



1099
1100
1101
1102
1103
1104
1105
# File 'lib/idl/ast.rb', line 1099

def freeze_tree(global_symtab)
  return if frozen?

  # call type to get it set before we freeze the object
  type(global_symtab)
  freeze
end

#nameString

Returns name of the enum class.

Returns:

  • (String)

    name of the enum class



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

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



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

def to_idl = "builtin enum #{@user_type.text_value}"

#type(symtab) ⇒ Object



1144
1145
1146
1147
1148
# File 'lib/idl/ast.rb', line 1144

def type(symtab)
  return @type unless @type.nil?

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

#type_check(_symtab) ⇒ Object



1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/idl/ast.rb', line 1108

def type_check(_symtab)
  case @user_type.text_value
  when "ExtensionName", "ExceptionCode", "InterruptCode"
    # OK
  else
    type_error "Unsupported builtin enum type '#{@user_type.text_value}'"
  end
end