Class: Idl::MultiVariableDeclarationAst

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

Overview

represents the declaration of multiple variables

for example:

Bits<64> a, b;
Bits<64> a, b, c, d;

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

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_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, #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, type_name, var_names) ⇒ MultiVariableDeclarationAst

Returns a new instance of MultiVariableDeclarationAst.



3570
3571
3572
3573
3574
# File 'lib/idlc/ast.rb', line 3570

def initialize(input, interval, type_name, var_names)
  super(input, interval, [type_name] + var_names)

  @global = false
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
# File 'lib/idlc/ast.rb', line 3625

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  MultiVariableDeclarationAst.new(
    input, interval,
    T.cast(AstNode.from_h(yaml.fetch("type"), source_mapper), T.any(BuiltinTypeNameAst, UserTypeNameAst)),
    yaml.fetch("names").map { |n| T.cast(AstNode.from_h(n, source_mapper), IdAst) }
  )
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



3606
3607
3608
3609
3610
# File 'lib/idlc/ast.rb', line 3606

def add_symbol(symtab)
  var_name_nodes.each do |vname|
    symtab.add(vname.text_value, Var.new(vname.text_value, type(symtab), type(symtab).default))
  end
end

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


3558
3559
3560
3561
# File 'lib/idlc/ast.rb', line 3558

def const_eval?(symtab)
  add_symbol(symtab)
  true
end

#gen_adoc(indent = 0, indent_spaces: 2) ⇒ Object



185
186
187
# File 'lib/idlc/passes/gen_adoc.rb', line 185

def gen_adoc(indent = 0, indent_spaces: 2)
  "#{' ' * indent}#{type_name.gen_adoc(0, indent_spaces:)} #{var_name_nodes.map { |var| var.gen_adoc(0, indent_spaces:) }.join(', ')}"
end

#make_globalObject

mark this declaration as being in global scope



3577
3578
3579
# File 'lib/idlc/ast.rb', line 3577

def make_global
  @global = true
end

#prune(symtab, forced_type: nil) ⇒ Object



954
955
956
957
# File 'lib/idlc/passes/prune.rb', line 954

def prune(symtab, forced_type: nil)
  add_symbol(symtab)
  dup
end

#to_hObject



3617
3618
3619
3620
3621
3622
# File 'lib/idlc/ast.rb', line 3617

def to_h = {
  "kind" => "multi_var_decl",
  "names" => var_name_nodes.map(&:to_h),
  "type" => type_name.to_h,
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "#{type_name.to_idl} #{var_name_nodes.map(&:to_idl).join(', ')}"

#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:



3597
3598
3599
3600
3601
3602
3603
# File 'lib/idlc/ast.rb', line 3597

def type(symtab)
  if @global
    type_name.type(symtab).clone.make_global
  else
    type_name.type(symtab)
  end
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:



3587
3588
3589
3590
3591
3592
3593
3594
# File 'lib/idlc/ast.rb', line 3587

def type_check(symtab, strict:)
  type_name.type_check(symtab, strict:)
  var_names.each do |v|
    type_error "reserved keyword" if ReservedWords::RESERVED.include?(v)
  end

  add_symbol(symtab)
end

#type_nameAstNode

Returns Declared type.

Returns:



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

def type_name = @children[0]

#var_name_nodesArray<AstNode>

Returns Variable names.

Returns:

  • (Array<AstNode>)

    Variable names



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

def var_name_nodes = @children[1..]

#var_namesArray<String>

Returns Variables being declared.

Returns:

  • (Array<String>)

    Variables being declared



3582
3583
3584
# File 'lib/idlc/ast.rb', line 3582

def var_names
  var_name_nodes.map(&:text_value)
end