Class: Idl::StructDefinitionAst
- Includes:
- Declaration
- Defined in:
- lib/idl/ast.rb
Overview
Structure declaration
for example, this maps to a StructDefinitionAst:
struct TranslationResult
Bits<PHYS_ADDR_SIZE> paddr;
Pbmt pbmt;
...
Instance Attribute Summary collapse
-
#member_names ⇒ Array<String>
readonly
Member names.
-
#member_types ⇒ Array<AstNode>
readonly
Types of each member.
-
#name ⇒ String
readonly
Struct name.
Instance Method Summary collapse
-
#add_symbol(symtab) ⇒ Object
Add symbol(s) at the outermost scope of the symbol table.
-
#initialize(input, interval, name, member_types, member_names) ⇒ StructDefinitionAst
constructor
A new instance of StructDefinitionAst.
- #member_type(name, symtab) ⇒ Type?
-
#num_members ⇒ Integer
Number of members.
- #to_idl ⇒ Object
-
#type(symtab) ⇒ Type
Given a specific symbol table, return the type of this node.
-
#type_check(symtab) ⇒ void
type check this node and all children.
Constructor Details
#initialize(input, interval, name, member_types, member_names) ⇒ StructDefinitionAst
Returns a new instance of StructDefinitionAst.
1375 1376 1377 1378 1379 1380 1381 |
# File 'lib/idl/ast.rb', line 1375 def initialize(input, interval, name, member_types, member_names) super(input, interval, member_types) @name = name @member_types = member_types @member_names = member_names end |
Instance Attribute Details
#member_names ⇒ Array<String> (readonly)
Returns Member names.
1373 1374 1375 |
# File 'lib/idl/ast.rb', line 1373 def member_names @member_names end |
#member_types ⇒ Array<AstNode> (readonly)
Returns Types of each member.
1370 1371 1372 |
# File 'lib/idl/ast.rb', line 1370 def member_types @member_types end |
#name ⇒ String (readonly)
Returns Struct name.
1367 1368 1369 |
# File 'lib/idl/ast.rb', line 1367 def name @name end |
Instance Method Details
#add_symbol(symtab) ⇒ Object
Add symbol(s) at the outermost scope of the symbol table
1403 1404 1405 1406 1407 1408 |
# File 'lib/idl/ast.rb', line 1403 def add_symbol(symtab) internal_error "Structs should be declared at global scope" unless symtab.levels == 1 t = type(symtab) symtab.add!(name, t) end |
#member_type(name, symtab) ⇒ Type?
1414 1415 1416 1417 1418 |
# File 'lib/idl/ast.rb', line 1414 def member_type(name, symtab) idx = member_names.index(name) return nil if idx.nil? member_types[idx].type(symtab) end |
#num_members ⇒ Integer
Returns Number of members.
1421 |
# File 'lib/idl/ast.rb', line 1421 def num_members = member_names.size |
#to_idl ⇒ Object
1423 1424 1425 1426 1427 1428 1429 |
# File 'lib/idl/ast.rb', line 1423 def to_idl member_decls = [] num_members.times do |i| member_decls << "#{member_types[i].to_idl} #{member_names[i]}" end "struct #{name} { #{member_decls.join("; ")}; }" end |
#type(symtab) ⇒ Type
Given a specific symbol table, return the type of this node.
Should not be called until #type_check is called with the same arguments
1392 1393 1394 1395 1396 1397 1398 1399 1400 |
# File 'lib/idl/ast.rb', line 1392 def type(symtab) @type = StructType.new(@name, @member_types.map do |t| member_type = t.type(symtab) type_error "Type #{t.text_value} is not known" if member_type.nil? member_type = Type.new(:enum_ref, enum_class: member_type) if member_type.kind == :enum member_type end, @member_names) 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
1384 1385 1386 1387 1388 1389 |
# File 'lib/idl/ast.rb', line 1384 def type_check(symtab) @member_types.each do |t| t.type_check(symtab) end add_symbol(symtab) end |