Class: Idl::StructDefinitionAst
- 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
-
#member_names ⇒ Array<String>
readonly
Member names.
-
#member_types ⇒ Array<AstNode>
readonly
Types of each member.
-
#name ⇒ String
readonly
Struct name.
Attributes inherited from AstNode
#children, #input, #interval, #parent
Class Method Summary collapse
Instance Method Summary collapse
-
#add_symbol(symtab) ⇒ Object
Add symbol(s) at the outermost scope of the symbol table.
- #const_eval?(symtab) ⇒ Boolean
-
#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_h ⇒ Object
- #to_idl ⇒ Object
-
#type(symtab) ⇒ Type
Given a specific symbol table, return the type of this node.
-
#type_check(symtab, strict:) ⇒ void
type check this node and all children.
Methods included from 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_names ⇒ Array<String> (readonly)
Returns Member names.
2389 2390 2391 |
# File 'lib/idlc/ast.rb', line 2389 def member_names @member_names end |
#member_types ⇒ Array<AstNode> (readonly)
Returns Types of each member.
2386 2387 2388 |
# File 'lib/idlc/ast.rb', line 2386 def member_types @member_types end |
#name ⇒ String (readonly)
Returns 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
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
2392 |
# File 'lib/idlc/ast.rb', line 2392 def const_eval?(symtab) = true |
#member_type(name, symtab) ⇒ Type?
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_members ⇒ Integer
Returns Number of members.
2445 |
# File 'lib/idlc/ast.rb', line 2445 def num_members = member_names.size |
#to_h ⇒ Object
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_idl ⇒ Object
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
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
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 |