Class: Idl::Type
- Inherits:
-
Object
show all
- Extended by:
- T::Sig
- Defined in:
- lib/idlc/type.rb,
lib/idlc/type.rb
Overview
Constant Summary
collapse
- KINDS =
[
:void, :boolean, :bits, :enum, :enum_ref, :bitfield, :struct, :array, :tuple, :function, :csr, :dontcare, :string ].freeze
- QUALIFIERS =
[
:const,
:signed,
:global,
:known
].freeze
- TYPE_FROM_KIND =
[:boolean, :void, :dontcare].map { |k| [k, Type.new(k)] }.to_h.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#ary? ⇒ Boolean
-
#ary_type(ary) ⇒ Object
given an N-dimensional array type, return the primitive type.
-
#clone ⇒ Object
-
#comparable_to?(type) ⇒ Boolean
returns true if ‘type’ can be compared (e.g., >=, <, etc) to self ‘type’ can be a Type object or a kind (as a Symbol).
-
#const? ⇒ Boolean
-
#convertable_to?(type) ⇒ Boolean
returns true if self can be converted to ‘type’ ‘type’ can be a Type object or a kind (as a Symbol).
-
#default ⇒ Object
-
#enum_class ⇒ Object
-
#equal_to?(type) ⇒ Boolean
returns true if identical to type, excluding qualifiers.
-
#global? ⇒ Boolean
-
#initialize(kind, qualifiers: [], width: nil, width_ast: nil, max_width: nil, sub_type: nil, name: nil, tuple_types: nil, return_type: nil, enum_class: nil, csr: nil) ⇒ Type
constructor
-
#integral? ⇒ Boolean
-
#known? ⇒ Boolean
-
#make_const ⇒ Object
-
#make_const! ⇒ Object
-
#make_global ⇒ Object
-
#make_known ⇒ Object
-
#make_signed ⇒ Object
-
#mutable? ⇒ Boolean
-
#name ⇒ Object
-
#qualify(qualifier) ⇒ Object
-
#runtime? ⇒ Boolean
-
#signed? ⇒ Boolean
-
#sub_type ⇒ Object
-
#to_idl ⇒ Object
-
#to_s ⇒ Object
(also: #fully_qualified_name)
-
#tuple_types ⇒ Object
-
#width ⇒ Object
Constructor Details
#initialize(kind, qualifiers: [], width: nil, width_ast: nil, max_width: nil, sub_type: nil, name: nil, tuple_types: nil, return_type: nil, enum_class: nil, csr: nil) ⇒ Type
Returns a new instance of Type.
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/idlc/type.rb', line 150
def initialize(kind, qualifiers: [], width: nil, width_ast: nil, max_width: nil, sub_type: nil, name: nil, tuple_types: nil, return_type: nil, enum_class: nil, csr: nil)
raise "Invalid kind '#{kind}'" unless KINDS.include?(kind)
@kind = kind
raise "Invalid qualifier" unless qualifiers.intersection(QUALIFIERS) == qualifiers
@qualifiers = qualifiers
raise "Should be a FunctionType" if kind == :function && !self.is_a?(FunctionType)
@width = width
@width_ast = width_ast
@max_width = max_width
@sub_type = sub_type
raise "Tuples need a type list" if kind == :tuple && tuple_types.nil?
@tuple_types = tuple_types
@enum_class = enum_class
@name = name
if kind == :bits
raise "Bits type must have width" unless @width
raise "Bits type must have positive width (has #{@width})" unless @width == :unknown || T.cast(@width, Integer).positive?
end
if kind == :enum
raise "Enum type must have width" unless @width
end
if kind == :array && width != 0
raise "Array must have a subtype" unless @sub_type
end
if kind == :csr
raise "CSR type must have a csr argument" if csr.nil?
@csr = csr
raise "CSR types must have a width" if width.nil?
@width = width
end
end
|
Instance Attribute Details
#kind ⇒ Object
Returns the value of attribute kind.
96
97
98
|
# File 'lib/idlc/type.rb', line 96
def kind
@kind
end
|
#max_width ⇒ Object
Returns the value of attribute max_width.
108
109
110
|
# File 'lib/idlc/type.rb', line 108
def max_width
@max_width
end
|
#qualifiers ⇒ Object
Returns the value of attribute qualifiers.
99
100
101
|
# File 'lib/idlc/type.rb', line 99
def qualifiers
@qualifiers
end
|
#width_ast ⇒ Object
Returns the value of attribute width_ast.
105
106
107
|
# File 'lib/idlc/type.rb', line 105
def width_ast
@width_ast
end
|
Class Method Details
.from_json_schema(schema) ⇒ Object
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
# File 'lib/idlc/type.rb', line 595
def self.from_json_schema(schema)
hsh = schema.to_h
if hsh.key?("type")
case hsh["type"]
when "boolean", "integer", "string"
from_json_schema_scalar_type(hsh)
when "array"
from_json_schema_array_type(hsh)
else
raise "unexpected"
end
else
from_json_schema_scalar_type(hsh)
end
end
|
.from_typename(type_name, cfg_arch) ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/idlc/type.rb', line 125
def self.from_typename(type_name, cfg_arch)
case type_name
when "XReg"
return Type.new(:bits, width: cfg_arch.param_values.key?("MXLEN") ? cfg_arch.param_values.fetch("MXLEN") : :unknown, max_width: 64)
when /Bits<((?:0x)?[0-9a-fA-F]+)>/
Type.new(:bits, width: $1.to_i)
end
end
|
Instance Method Details
#==(other) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/idlc/type.rb', line 51
def ==(other)
return false unless other.is_a?(Type)
case other.kind
when :bits
@kind == :bits && @width == other.width
when :enum_ref
@kind == :enum_ref && T.must(@enum_class).name == other.name
else
raise "TODO: Type == for #{other.kind}"
end
end
|
#ary? ⇒ Boolean
414
415
416
|
# File 'lib/idlc/type.rb', line 414
def ary?
@kind == :array
end
|
#ary_type(ary) ⇒ Object
given an N-dimensional array type, return the primitive type
265
266
267
268
269
270
271
|
# File 'lib/idlc/type.rb', line 265
def ary_type(ary)
if ary.sub_type == :array
ary_type(ary.sub_type)
else
ary.sub_type
end
end
|
#clone ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/idlc/type.rb', line 190
def clone
Type.new(
@kind,
qualifiers: @qualifiers.map(&:clone),
width: @width,
sub_type: @sub_type&.clone,
name: @name.dup,
tuple_types: @tuple_types&.map(&:clone),
enum_class: @enum_class&.clone,
csr: @csr
)
end
|
#comparable_to?(type) ⇒ Boolean
returns true if ‘type’ can be compared (e.g., >=, <, etc) to self ‘type’ can be a Type object or a kind (as a Symbol)
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/idlc/type.rb', line 205
def comparable_to?(type)
if type.is_a?(Symbol)
raise "#{type} is not a kind" unless KINDS.include?(type)
type = Type.new(type)
end
case @kind
when :boolean
return type.kind == :boolean
when :enum_ref
return \
(type.kind == :enum_ref && type.enum_class.name == T.must(@enum_class).name) \
|| (type.kind == :enum && type.name == T.must(@enum_class).name)
when :bits
return type.convertable_to?(self) && (signed? == type.signed?)
when :enum
return type.convertable_to?(:bits)
when :function
return false
when :csr
return ((type.kind == :csr) && (type.csr.name == @csr.name)) ||
type.convertable_to?(Type.new(:bits, width: type.csr.width))
when :string
return type.kind == :string
else
raise "unimplemented #{@kind}"
end
end
|
#const? ⇒ Boolean
418
419
420
|
# File 'lib/idlc/type.rb', line 418
def const?
@qualifiers.include?(:const)
end
|
#convertable_to?(type) ⇒ Boolean
returns true if self can be converted to ‘type’ ‘type’ can be a Type object or a kind (as a Symbol)
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/idlc/type.rb', line 275
def convertable_to?(type)
if type.is_a?(Symbol)
raise "#{type} is not a kind" unless KINDS.include?(type)
type = TYPE_FROM_KIND[type]
end
case @kind
when :boolean
return type.kind == :boolean
when :enum_ref
return \
(type.kind == :enum && type.name == T.must(@enum_class).name) || \
(type.kind == :enum_ref && type.enum_class.name == T.must(@enum_class).name)
when :dontcare
return true
when :bits
if type.kind == :enum_ref
warn "You seem to be missing an $enum cast"
return false
end
return type.kind == :bits || type.kind == :bitfield
when :enum
if type.kind == :bits
return false
elsif type.kind == :enum
return type.enum_class == enum_class
else
return false
end
when :tuple
is_tuple_of_same_size = (type.kind == :tuple) && (T.must(@tuple_types).size == type.tuple_types.size)
if is_tuple_of_same_size
T.must(@tuple_types).each_index do |i|
unless T.must(@tuple_types).fetch(i).convertable_to?(type.tuple_types.fetch(i))
return false
end
end
return true
else
return false
end
when :csr
return (type.kind == :csr && type.csr.name == @csr.name) || type.convertable_to?(Type.new(:bits, width:))
when :bitfield
if (type.kind == :bitfield && name == type.name)
return true
elsif (type.kind == :bits && type.width == @width)
return true
else
return false
end
when :array
return type.kind == :array && type.sub_type.convertable_to?(sub_type) && type.width == @width
when :string
return type.kind == :string
when :void
return type.kind == :void
when :struct
return type.kind == :struct && (T.cast(type, StructType).name == T.cast(self, StructType).type_name)
else
raise "unimplemented type '#{@kind}'"
end
end
|
#default ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/idlc/type.rb', line 72
def default
case @kind
when :bits, :bitfield
0
when :boolean
false
when :array
if @width == :unknown
Array.new
else
Array.new(T.cast(@width, Integer), sub_type.default)
end
when :string
""
when :enum_ref
T.must(@enum_class).element_values.min
when :enum
raise "?"
else
raise "No default for #{@kind}"
end
end
|
#enum_class ⇒ Object
117
|
# File 'lib/idlc/type.rb', line 117
def enum_class = T.must(@enum_class)
|
#equal_to?(type) ⇒ Boolean
returns true if identical to type, excluding qualifiers
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/idlc/type.rb', line 237
def equal_to?(type)
if type.is_a?(Symbol)
raise "#{type} is not a kind" unless KINDS.include?(type)
type = TYPE_FROM_KIND[type]
end
case @kind
when :boolean
type.kind == :boolean
when :enum_ref
type.kind == :enum_ref && type.name == T.must(@enum_class).name
when :dontcare
true
when :bits
type.kind == :bits && type.width == @width
when :string
type.kind == :string && type.width == @width
when :array
type.kind == :array && type.sub_type.equal_to?(@sub_type)
when :struct
type.kind == :struct && (T.cast(type, StructType).type_name == T.cast(self, StructType).type_name)
else
raise "unimplemented type '#{@kind}'"
end
end
|
#global? ⇒ Boolean
430
431
432
|
# File 'lib/idlc/type.rb', line 430
def global?
@qualifiers.include?(:global)
end
|
#integral? ⇒ Boolean
47
48
49
|
# File 'lib/idlc/type.rb', line 47
def integral?
@kind == :bits
end
|
#known? ⇒ Boolean
434
435
436
|
# File 'lib/idlc/type.rb', line 434
def known?
@qualifiers.include?(:known)
end
|
#make_const ⇒ Object
452
453
454
455
|
# File 'lib/idlc/type.rb', line 452
def make_const
new_t = clone
new_t.make_const!
end
|
#make_const! ⇒ Object
445
446
447
448
|
# File 'lib/idlc/type.rb', line 445
def make_const!
@qualifiers.append(:const).uniq!
self
end
|
#make_global ⇒ Object
457
458
459
460
|
# File 'lib/idlc/type.rb', line 457
def make_global
@qualifiers.append(:global).uniq!
self
end
|
#make_known ⇒ Object
462
463
464
465
|
# File 'lib/idlc/type.rb', line 462
def make_known
@qualifiers.append(:known).uniq!
self
end
|
#make_signed ⇒ Object
438
439
440
441
|
# File 'lib/idlc/type.rb', line 438
def make_signed
@qualifiers.append(:signed).uniq!
self
end
|
#mutable? ⇒ Boolean
422
423
424
|
# File 'lib/idlc/type.rb', line 422
def mutable?
!const?
end
|
#name ⇒ Object
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
# File 'lib/idlc/type.rb', line 396
def name
if @kind == :bits
"Bits<#{@width}>"
elsif @kind == :enum
@name
elsif @kind == :bitfield
@name
elsif @kind == :function
@name
elsif @kind == :csr
@csr.name
elsif @kind == :enum_ref
T.must(@enum_class).name
else
raise @kind.to_s
end
end
|
#qualify(qualifier) ⇒ Object
119
120
121
122
123
|
# File 'lib/idlc/type.rb', line 119
def qualify(qualifier)
@qualifiers << qualifier
@qualifiers.uniq!
self
end
|
#runtime? ⇒ Boolean
64
65
66
67
68
69
70
|
# File 'lib/idlc/type.rb', line 64
def runtime?
if @kind == :array
T.must(@sub_type).runtime?
else
@kind == :bits && !@width.is_a?(Integer)
end
end
|
#signed? ⇒ Boolean
426
427
428
|
# File 'lib/idlc/type.rb', line 426
def signed?
@qualifiers.include?(:signed)
end
|
#sub_type ⇒ Object
111
|
# File 'lib/idlc/type.rb', line 111
def sub_type = T.must(@sub_type)
|
#to_idl ⇒ Object
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
# File 'lib/idlc/type.rb', line 344
def to_idl
case @kind
when :bits
if @width == :unknown
raise "Cannot generate an IDL type with an unknown width"
end
if signed?
raise "Cannot directly represent a signed bits"
else
"Bits<#{@width}"
end
when :String
"String"
when :boolean
"Boolean"
else
raise "TODO"
end
end
|
#to_s ⇒ Object
Also known as:
fully_qualified_name
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/idlc/type.rb', line 364
def to_s
((@qualifiers.empty?) ? "" : "#{@qualifiers.map(&:to_s).join(' ')} ") + \
if @kind == :bits
"Bits<#{@width}>"
elsif @kind == :enum
"enum definition #{@name}"
elsif @kind == :boolean
"Boolean"
elsif @kind == :enum_ref
"enum #{T.must(@enum_class).name}"
elsif @kind == :tuple
"(#{T.must(@tuple_types).map { |t| t.to_s }.join(',')})"
elsif @kind == :bitfield
"bitfield #{@name}"
elsif @kind == :array
"array of #{@sub_type}"
elsif @kind == :csr
"CSR[#{@csr.name}]"
elsif @kind == :void
"void"
elsif @kind == :string
"string"
elsif @kind == :struct
"struct #{T.cast(self, StructType).type_name}"
elsif @kind == :function
"function #{name}"
else
raise @kind.to_s
end
end
|
#tuple_types ⇒ Object
114
|
# File 'lib/idlc/type.rb', line 114
def tuple_types = T.must(@tuple_types)
|
#width ⇒ Object
102
|
# File 'lib/idlc/type.rb', line 102
def width = T.must(@width)
|