Class: C::Declarator

Inherits:
Node
  • Object
show all
Defined in:
lib/cast/to_s.rb,
lib/cast/parse.rb,
lib/cast/c_nodes.rb,
lib/cast/c_nodes.rb

Constant Summary

Constants inherited from Node

Node::INSPECT_TAB

Instance Attribute Summary

Attributes inherited from Node

#parent, #pos, #subclasses

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#==, #=~, abstract, add_field, #assert_invariants, #attached?, child, #clone, #depth_first, #detach, #detached?, #dup, #each, #eql?, field, #fields, fields, #hash, inherited, initializer, #insert_next, #insert_prev, #inspect, inspect1, #list_next, #list_prev, #match?, #method_missing, new_at, #next, #node_after, #node_before, #postorder, #preorder, #prev, #remove_node, #replace_node, #replace_with, #reverse_depth_first, #reverse_each, #reverse_postorder, #reverse_preorder, subclasses_recursive, #swap_with

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class C::Node

Class Method Details

.parse(s, parser = nil) ⇒ Object

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cast/parse.rb', line 95

def self.parse(s, parser=nil)
  parser ||= C.default_parser
  # if there's a ':', declare in a struct so we can populate num_bits
  if s =~ /:/
    ents = parser.parse("struct {int #{s};};").entities
    if ents.length == 1 &&                              # i:1;}; struct {int i
        ents[0].type.members.length == 1 &&             # i:1; int j
        ents[0].type.members[0].declarators.length == 1 # i:1,j
      return ents[0].type.members[0].declarators[0].detach
    end
  else
    ents = parser.parse("int #{s};").entities
    if ents.length == 1 &&               # f; int f;
        ents[0].declarators.length == 1  # i,j
      return ents[0].declarators[0].detach
    end
  end
  raise ParseError, "invalid Declarator"
end

Instance Method Details

#declarationObject



170
171
172
# File 'lib/cast/c_nodes.rb', line 170

def declaration
  parent and parent.parent
end

#to_sObject



47
48
49
50
51
# File 'lib/cast/to_s.rb', line 47

def to_s
  (indirect_type ? indirect_type.to_s(name) : name.dup) <<
    (init ? " = #{init}" : '') <<
    (num_bits ? " : #{num_bits}" : '')
end

#typeObject

Return (a copy of) the type of the variable this Declarator declares.



177
178
179
180
181
182
183
184
185
# File 'lib/cast/c_nodes.rb', line 177

def type
  if indirect_type
    ret = indirect_type.clone
    ret.direct_type = declaration.type.clone
    return ret
  else
    declaration.type.clone
  end
end