Class: Idl::StructDefinitionAst

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

Overview

Structure declaration

for example, this maps to a StructDefinitionAst:

struct TranslationResult

Bits<PHYS_ADDR_SIZE> paddr;
Pbmt pbmt;
...

Constant Summary

Constants inherited from AstNode

AstNode::Bits1Type, AstNode::Bits32Type, AstNode::Bits64Type, AstNode::BoolType, AstNode::ConstBoolType, AstNode::PossiblyUnknownBits1Type, AstNode::PossiblyUnknownBits32Type, AstNode::PossiblyUnknownBits64Type, AstNode::ReachableFunctionCacheType, AstNode::StringType, AstNode::VoidType

Instance Attribute Summary collapse

Attributes inherited from AstNode

#children, #input, #interval, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Declaration

#declaration?

Methods inherited from AstNode

#always_terminates?, #declaration?, #executable?, extract_base_var_name, #find_ancestor, #find_dst_registers, #find_referenced_csrs, #find_src_registers, #freeze_tree, #gen_adoc, #gen_option_adoc, #input_file, input_from_source_yaml, #inspect, #internal_error, interval_from_source_yaml, #lineno, #lines_around, #nullify_assignments, #pass_find_return_values, #path, #print_ast, #prune, #reachable_exceptions, #reachable_functions, #set_input_file, #set_input_file_unless_already_set, #source_line_file_offsets, #source_starting_offset, #source_yaml, #starting_line, #text_value, #truncation_warn, #type_error, #unindent, value_else, #value_else, value_error, #value_error, value_try, #value_try, write_back_nested

Constructor Details

#initialize(input, interval, name, member_types, member_names) ⇒ StructDefinitionAst

Returns a new instance of StructDefinitionAst.



2394
2395
2396
2397
2398
2399
2400
# File 'lib/idlc/ast.rb', line 2394

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



2389
2390
2391
# File 'lib/idlc/ast.rb', line 2389

def member_names
  @member_names
end

#member_typesArray<AstNode> (readonly)

Returns Types of each member.

Returns:

  • (Array<AstNode>)

    Types of each member



2386
2387
2388
# File 'lib/idlc/ast.rb', line 2386

def member_types
  @member_types
end

#nameString (readonly)

Returns Struct name.

Returns:

  • (String)

    Struct name



2383
2384
2385
# File 'lib/idlc/ast.rb', line 2383

def name
  @name
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
# File 'lib/idlc/ast.rb', line 2470

def self.from_h(yaml, source_mapper)
  raise "Bad YAML" unless yaml.key?("kind") && yaml.fetch("kind") == "struct_decl"

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  StructDefinitionAst.new(
    input, interval,
    yaml.fetch("name"),
    yaml.fetch("members").map { |m| AstNode.from_h(m.fetch("type"), source_mapper) },
    yaml.fetch("members").map { |m| m.fetch("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



2426
2427
2428
2429
2430
2431
2432
# File 'lib/idlc/ast.rb', line 2426

def add_symbol(symtab)
  type_error "Cannot use reserved word '#{@name}' as user-defined type name" if ReservedWords::RESERVED.include?(@name)
  internal_error "Structs should be declared at global scope" unless symtab.levels == 1

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

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


2392
# File 'lib/idlc/ast.rb', line 2392

def const_eval?(symtab) = true

#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



2438
2439
2440
2441
2442
# File 'lib/idlc/ast.rb', line 2438

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



2445
# File 'lib/idlc/ast.rb', line 2445

def num_members = member_names.size

#to_hObject



2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
# File 'lib/idlc/ast.rb', line 2457

def to_h = {
  "kind" => "struct_decl",
  "name" => name,
  "members" => num_members.times.map { |idx|
    {
      "name" => member_names[idx],
      "type" => member_types[idx].to_h
    }
  },
  "source" => source_yaml
}

#to_idlObject



2448
2449
2450
2451
2452
2453
2454
# File 'lib/idlc/ast.rb', line 2448

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:



2415
2416
2417
2418
2419
2420
2421
2422
2423
# File 'lib/idlc/ast.rb', line 2415

def type(symtab)
  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, strict:) ⇒ 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:



2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
# File 'lib/idlc/ast.rb', line 2403

def type_check(symtab, strict:)
  type_error "Cannot use reserved word '#{@name}' as user-defined type name" if ReservedWords::RESERVED.include?(@name)
  @member_names.each do |member_name|
    type_error "Cannot use reserved word '#{member_name}' as variable name" if ReservedWords::RESERVED.include?(member_name)
  end
  @member_types.each do |t|
    t.type_check(symtab, strict:)
  end
  add_symbol(symtab)
end