class HDLRuby::Low::TypeStruct
Describes a structure data type.
Describes a structure type.
Extends the TypeStruct
class with generation of C text.
Extends the TypeStruct
class with generation of hdr text.
Add the conversion to high.
Extends the TypeStruct
class with generation of HDLRuby::High
text.
Describes a structure type.
Extends the TypeStruct
class with functionality for breaking hierarchical types.
Public Class Methods
Creates a new structure type named name
with direction
and whose hierachy is given by content
.
# File lib/HDLRuby/hruby_low.rb, line 1941 def initialize(name,direction,content) # Initialize the type. super(name) # Set the direction. @direction = direction.to_sym unless [:little, :big].include?(@direction) raise AnyError, "Invalid direction for a type: #{direction}" end # Check and set the content. content = Hash[content] @types = content.map do |k,v| unless v.is_a?(Type) then raise AnyError, "Invalid class for a type: #{v.class}" end [ k.to_sym, v ] end.to_h end
Public Instance Methods
Breaks the hierarchical types into sequences of type definitions. Assumes to_upper_space! has been called before. types
include the resulting types.
# File lib/HDLRuby/hruby_low_without_namespace.rb, line 369 def break_types!(types) self.map_types! do |sub| if sub.is_a?(TypeVector) || sub.is_a?(TypeStruct) || sub.is_a?(TypeStruct) then # Need to break # First recurse on the sub. nsub = sub.break_types!(types) # Maybe such a type already exists. ndef = types[sub] if ndef then # Yes, use it. ndef.clone else # No change it to a type definition ndef = TypeDef.new(HDLRuby.uniq_name,nsub) # And add it to the types by structure. types[nsub] = ndef nsub end end end return self end
Deletes a sub type by key
.
# File lib/HDLRuby/hruby_low_mutable.rb, line 356 def delete_type!(key) if @types.include?(key) then # The type is present, delete it. type = @types.delete(key) # And remove its parent. type.parent = nil end type end
Iterates over the sub name/type pair.
Returns an enumerator if no ruby block is given.
# File lib/HDLRuby/hruby_low.rb, line 2003 def each(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each) unless ruby_block # A ruby block? Apply it on each input signal instance. @types.each(&ruby_block) end
Iterates over the sub type names.
Returns an enumerator if no ruby block is given.
# File lib/HDLRuby/hruby_low.rb, line 2023 def each_name(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_name) unless ruby_block # A ruby block? Apply it on each input signal instance. @types.each_key(&ruby_block) end
Iterates over the sub types.
Returns an enumerator if no ruby block is given.
# File lib/HDLRuby/hruby_low.rb, line 2013 def each_type(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_type) unless ruby_block # A ruby block? Apply it on each input signal instance. @types.each_value(&ruby_block) end
Iterates over the types deeply if any.
# File lib/HDLRuby/hruby_low.rb, line 2031 def each_type_deep(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_type_deep) unless ruby_block # A ruby block? First apply it to current. ruby_block.call(self) # And recurse on the sub types. @types.each_value { |type| type.each_type_deep(&ruby_block) } end
Comparison for hash: structural comparison.
# File lib/HDLRuby/hruby_low.rb, line 1962 def eql?(obj) # General type comparison. return false unless super(obj) # Specific comparison. idx = 0 obj.each_key do |name| return false unless @types[name].eql?(obj.get_type(name)) idx += 1 end return false unless idx == @types.size return true end
Tell if type
is equivalent to current type.
NOTE: type can be compatible while not being equivalent, please
refer to `hruby_types.rb` for type compatibility.
# File lib/HDLRuby/hruby_low.rb, line 2087 def equivalent?(type) return (type.is_a?(TypeStruct) and !@types.to_a.zip(type.types.to_a).index do |t0,t1| t0[0] != t1[0] or !t0[1].equivalent?(t1[1]) end) end
Gets an array containing all the syb types.
# File lib/HDLRuby/hruby_low.rb, line 1991 def get_all_types return @types.values end
Gets a sub type by name
.
# File lib/HDLRuby/hruby_low.rb, line 1996 def get_type(name) return @types[name.to_sym] end
Hash
function.
# File lib/HDLRuby/hruby_low.rb, line 1976 def hash return [super,@types].hash end
Maps on the sub types.
# File lib/HDLRuby/hruby_low_mutable.rb, line 351 def map_types!(&ruby_block) @types.map(&ruby_block) end
Tells if the type has named sub types.
# File lib/HDLRuby/hruby_low.rb, line 1981 def struct? return true end
Generates the text of the equivalent HDLRuby
code. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2c.rb, line 572 def to_c(level = 0) return "get_type_struct(#{self.each.join(",") do |key,type| "\"#{key.to_s}\",#{type.to_c(level+1)}" end})" end
Generates the text of the equivalent hdr text. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2hdr.rb, line 230 def to_hdr(level = 0) # The resulting string. res = "{ " # Generate each sub type. res << self.each.map do |key,type| "#{key}: " + type.to_hdr(level) end.join(", ") # Close the struct. res << " }" # Return the result. return res end
Creates a new high type struct.
# File lib/HDLRuby/hruby_low2high.rb, line 119 def to_high return HDLRuby::High::TypeString.new(self.name,self.direction, self.each {|name,typ| [name,typ.to_high]}) end
Generates the text of the equivalent HDLRuby::High
code. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2vhd.rb, line 724 def to_vhdl(level = 0) # The resulting string. res = "record \n" # Generate each sub type. self.each do |key,type| res << " " * ((level+1)*3) res << Low2VHDL.vhdl_name(key) res << ": " << type.to_vhdl(level+1) res << ";\n" end res << " " * (level*3) # Close the record. res << "end record" # Return the result. return res end
Tells if the type has sub types.
# File lib/HDLRuby/hruby_low.rb, line 1986 def types? return true end
Gets the bitwidth of the type, nil for undefined.
NOTE: must be redefined for specific types.
# File lib/HDLRuby/hruby_low.rb, line 2045 def width return @types.reduce(0) {|sum,type| sum + type.width } end