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.
1377 1378 1379 1380 1381 1382 1383 |
# File 'lib/idl/ast.rb', line 1377 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.
1375 1376 1377 |
# File 'lib/idl/ast.rb', line 1375 def member_names @member_names end |
#member_types ⇒ Array<AstNode> (readonly)
Returns Types of each member.
1372 1373 1374 |
# File 'lib/idl/ast.rb', line 1372 def member_types @member_types end |
#name ⇒ String (readonly)
Returns Struct name.
1369 1370 1371 |
# File 'lib/idl/ast.rb', line 1369 def name @name end |
Instance Method Details
#add_symbol(symtab) ⇒ Object
Add symbol(s) at the outermost scope of the symbol table
1405 1406 1407 1408 1409 1410 |
# File 'lib/idl/ast.rb', line 1405 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?
1416 1417 1418 1419 1420 |
# File 'lib/idl/ast.rb', line 1416 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.
1423 |
# File 'lib/idl/ast.rb', line 1423 def num_members = member_names.size |
#to_idl ⇒ Object
1425 1426 1427 1428 1429 1430 1431 |
# File 'lib/idl/ast.rb', line 1425 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
1394 1395 1396 1397 1398 1399 1400 1401 1402 |
# File 'lib/idl/ast.rb', line 1394 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
1386 1387 1388 1389 1390 1391 |
# File 'lib/idl/ast.rb', line 1386 def type_check(symtab) @member_types.each do |t| t.type_check(symtab) end add_symbol(symtab) end |