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.



1096
1097
1098
1099
# File 'lib/idl/ast.rb', line 1096

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



1153
1154
1155
1156
1157
# File 'lib/idl/ast.rb', line 1153

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



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

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

#element_values(symtab) ⇒ Object



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

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

#freeze_tree(global_symtab) ⇒ Object



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

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



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

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



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

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

#type(symtab) ⇒ Object



1146
1147
1148
1149
1150
# File 'lib/idl/ast.rb', line 1146

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

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

#type_check(_symtab) ⇒ Object



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

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