Class: Idl::StructDefinitionAst

Inherits:
AstNode
  • Object
show all
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

Instance Method Summary collapse

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_namesArray<String> (readonly)

Returns Member names.

Returns:

  • (Array<String>)

    Member names



1373
1374
1375
# File 'lib/idl/ast.rb', line 1373

def member_names
  @member_names
end

#member_typesArray<AstNode> (readonly)

Returns Types of each member.

Returns:

  • (Array<AstNode>)

    Types of each member



1370
1371
1372
# File 'lib/idl/ast.rb', line 1370

def member_types
  @member_types
end

#nameString (readonly)

Returns Struct name.

Returns:

  • (String)

    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

Parameters:

  • symtab (SymbolTable)

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



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?

Parameters:

  • name (String)

    Member name

  • symtab (SymbolTable)

    Context

Returns:

  • (Type)

    Type of member name

  • (nil)

    if there is no member name



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_membersInteger

Returns Number of members.

Returns:

  • (Integer)

    Number of members



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

def num_members = member_names.size

#to_idlObject



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

Parameters:

Returns:

  • (Type)

    The type of the node

Raises:



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

Parameters:

Raises:



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