Class: HDLRuby::Low::TypeStruct
- Defined in:
- lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2hdr.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/hruby_low_without_namespace.rb
Overview
Describes a structure type.
Direct Known Subclasses
Constant Summary
Constants included from Low2Symbol
Low2Symbol::Low2SymbolPrefix, Low2Symbol::Low2SymbolTable, Low2Symbol::Symbol2LowTable
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
Attributes inherited from Type
Attributes included from Hparent
Instance Method Summary collapse
-
#break_types!(types) ⇒ Object
Breaks the hierarchical types into sequences of type definitions.
-
#delete_type!(key) ⇒ Object
Deletes a sub type by +key+.
-
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
-
#each_key(&ruby_block) ⇒ Object
Iterates over the keys.
-
#each_name(&ruby_block) ⇒ Object
Iterates over the sub type names.
-
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
-
#each_type_deep(&ruby_block) ⇒ Object
(also: #each_deep)
Iterates over the types deeply if any.
-
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
-
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
-
#get_all_types ⇒ Object
Gets an array containing all the syb types.
-
#get_type(name) ⇒ Object
Gets a sub type by +name+.
-
#hash ⇒ Object
Hash function.
-
#initialize(name, dir, content) ⇒ TypeStruct
constructor
Creates a new structure type named +name+ with direction +dir+ and whose hierachy is given by +content+.
-
#map_types!(&ruby_block) ⇒ Object
Maps on the sub types.
-
#struct? ⇒ Boolean
Tells if the type has named sub types.
-
#to_c(res, level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby code.
-
#to_hdr(level = 0) ⇒ Object
Generates the text of the equivalent hdr text.
-
#to_high ⇒ Object
Creates a new high type struct.
-
#to_vhdl(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code.
-
#types? ⇒ Boolean
Tells if the type has sub types.
-
#width ⇒ Object
Gets the bitwidth of the type, nil for undefined.
Methods inherited from Type
#base, #base?, #boolean?, #fixed?, #float?, #hierarchical?, #leaf?, #max, #min, #range, #range?, #regular?, #set_name!, #signed?, #to_vector, #to_verilog, #unsigned?, #vector?
Methods included from Low2Symbol
Methods included from Hparent
#absolute_ref, #hierarchy, #no_parent!, #scope
Constructor Details
#initialize(name, dir, content) ⇒ TypeStruct
Creates a new structure type named +name+ with direction +dir+ and whose hierachy is given by +content+.
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 |
# File 'lib/HDLRuby/hruby_low.rb', line 2196 def initialize(name,dir,content) # Initialize the type. super(name) # Set the direction. @direction = dir.to_sym unless [:little, :big].include?(@direction) raise AnyError, "Invalid direction for a type: #{dir}" 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 |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
2192 2193 2194 |
# File 'lib/HDLRuby/hruby_low.rb', line 2192 def direction @direction end |
Instance Method Details
#break_types!(types) ⇒ Object
Breaks the hierarchical types into sequences of type definitions. Assumes to_upper_space! has been called before. +types+ include the resulting types.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 389 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 |
#delete_type!(key) ⇒ Object
Deletes a sub type by +key+.
356 357 358 359 360 361 362 363 364 |
# 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 |
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
Returns an enumerator if no ruby block is given.
2264 2265 2266 2267 2268 2269 |
# File 'lib/HDLRuby/hruby_low.rb', line 2264 def each(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each) unless ruby_block # A ruby block? Apply it on each sub name/type pair. @types.each(&ruby_block) end |
#each_key(&ruby_block) ⇒ Object
Iterates over the keys.
Returns an enumerator if no ruby block is given.
2284 2285 2286 2287 2288 2289 |
# File 'lib/HDLRuby/hruby_low.rb', line 2284 def each_key(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_key) unless ruby_block # A ruby block? Apply it on each key. @types.each_key(&ruby_block) end |
#each_name(&ruby_block) ⇒ Object
Iterates over the sub type names.
Returns an enumerator if no ruby block is given.
2294 2295 2296 2297 2298 2299 |
# File 'lib/HDLRuby/hruby_low.rb', line 2294 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 name. @types.each_key(&ruby_block) end |
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
Returns an enumerator if no ruby block is given.
2274 2275 2276 2277 2278 2279 |
# File 'lib/HDLRuby/hruby_low.rb', line 2274 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 sub type. @types.each_value(&ruby_block) end |
#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep
Iterates over the types deeply if any.
2302 2303 2304 2305 2306 2307 2308 2309 |
# File 'lib/HDLRuby/hruby_low.rb', line 2302 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 |
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 |
# File 'lib/HDLRuby/hruby_low.rb', line 2217 def eql?(obj) # General type comparison. # return false unless super(obj) return false unless obj.is_a?(TypeStruct) # 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 |
#equivalent?(type) ⇒ Boolean
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.
2362 2363 2364 2365 2366 2367 |
# File 'lib/HDLRuby/hruby_low.rb', line 2362 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 |
#get_all_types ⇒ Object
Gets an array containing all the syb types.
2247 2248 2249 |
# File 'lib/HDLRuby/hruby_low.rb', line 2247 def get_all_types return @types.values end |
#get_type(name) ⇒ Object
Gets a sub type by +name+. NOTE: +name+ can also be an index.
2253 2254 2255 2256 2257 2258 2259 |
# File 'lib/HDLRuby/hruby_low.rb', line 2253 def get_type(name) if name.respond_to?(:to_sym) then return @types[name.to_sym] else return @types.values[name.to_i] end end |
#hash ⇒ Object
Hash function.
2232 2233 2234 |
# File 'lib/HDLRuby/hruby_low.rb', line 2232 def hash return [super,@types].hash end |
#map_types!(&ruby_block) ⇒ Object
Maps on the sub types.
351 352 353 |
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 351 def map_types!(&ruby_block) @types.map(&ruby_block) end |
#struct? ⇒ Boolean
Tells if the type has named sub types.
2237 2238 2239 |
# File 'lib/HDLRuby/hruby_low.rb', line 2237 def struct? return true end |
#to_c(res, level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby code. +level+ is the hierachical level of the object. def to_c(level = 0)
646 647 648 649 650 651 652 |
# File 'lib/HDLRuby/hruby_low2c.rb', line 646 def to_c(res,level = 0) # res << "get_type_struct(#{self.each.map do |key,type| # "\"#{key.to_s}\",#{type.to_c("",level+1)}" # end.join(",")})" self.to_vector.to_c(res,level) return res end |
#to_hdr(level = 0) ⇒ Object
Generates the text of the equivalent hdr text. +level+ is the hierachical level of the object.
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/HDLRuby/hruby_low2hdr.rb', line 234 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 |
#to_high ⇒ Object
Creates a new high type struct.
128 129 130 131 |
# File 'lib/HDLRuby/hruby_low2high.rb', line 128 def to_high return HDLRuby::High::TypeString.new(self.name,self.direction, self.each {|name,typ| [name,typ.to_high]}) end |
#to_vhdl(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 728 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 |
#types? ⇒ Boolean
Tells if the type has sub types.
2242 2243 2244 |
# File 'lib/HDLRuby/hruby_low.rb', line 2242 def types? return true end |
#width ⇒ Object
Gets the bitwidth of the type, nil for undefined.
NOTE: must be redefined for specific types.
2316 2317 2318 2319 2320 2321 2322 |
# File 'lib/HDLRuby/hruby_low.rb', line 2316 def width if @types.is_a?(Array) then return @types.reduce(0) {|sum,type| sum + type.width } else return @types.each_value.reduce(0) {|sum,type| sum + type.width } end end |