ridl 2.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,507 @@
1
+ #--------------------------------------------------------------------
2
+ # type.rb - IDL types
3
+ #
4
+ # Author: Martin Corino
5
+ #
6
+ # This program is free software; you can redistribute it and/or
7
+ # modify it under the terms of the RIDL LICENSE which is
8
+ # included with this program.
9
+ #
10
+ # Copyright (c) Remedy IT Expertise BV
11
+ # Chamber of commerce Rotterdam nr.276339, The Netherlands
12
+ #--------------------------------------------------------------------
13
+ module IDL
14
+ class Type
15
+ def typename
16
+ self.class.name
17
+ end
18
+ def typeerror(val)
19
+ raise RuntimeError, "#{val.inspect} cannot narrow to #{self.typename}"
20
+ end
21
+ def narrow(obj)
22
+ obj
23
+ end
24
+ def resolved_type
25
+ self
26
+ end
27
+ def is_complete?
28
+ true
29
+ end
30
+ def is_local?(recurstk = nil)
31
+ false
32
+ end
33
+ def is_anonymous?
34
+ false
35
+ end
36
+ def is_node?(node_class)
37
+ false
38
+ end
39
+ def resolved_node
40
+ nil
41
+ end
42
+ def is_template?
43
+ false
44
+ end
45
+
46
+ def instantiate(_context)
47
+ self
48
+ end
49
+
50
+ class UndefinedType
51
+ def initialize(*args)
52
+ raise RuntimeError, "#{self.class.name}'s not implemented yet."
53
+ end
54
+ end
55
+
56
+ class Void < Type
57
+ def narrow(obj)
58
+ typeerror(obj) unless obj.nil?
59
+ obj
60
+ end
61
+ end
62
+
63
+ class NodeType < Type
64
+ attr_reader :node
65
+ def initialize(node)
66
+ @node = node
67
+ end
68
+ def is_local?(recurstk = nil)
69
+ @node.is_local?
70
+ end
71
+ def is_node?(node_class)
72
+ @node.is_a?(node_class)
73
+ end
74
+ def resolved_node
75
+ @node
76
+ end
77
+ end
78
+
79
+ class ScopedName < NodeType
80
+ def typename
81
+ @node.name
82
+ end
83
+ def narrow(obj)
84
+ @node.idltype.narrow(obj)
85
+ end
86
+ def resolved_type
87
+ @node.idltype.resolved_type
88
+ end
89
+ def is_complete?
90
+ resolved_type.is_complete?
91
+ end
92
+ def is_local?(recurstk = [])
93
+ resolved_type.is_local?(recurstk)
94
+ end
95
+ def is_node?(node_class)
96
+ @node.is_a?(IDL::AST::Typedef) ? @node.idltype.is_node?(node_class) : @node.is_a?(node_class)
97
+ end
98
+ def resolved_node
99
+ @node.is_a?(IDL::AST::Typedef) ? @node.idltype.resolved_node : @node
100
+ end
101
+ def is_template?
102
+ @node.is_template?
103
+ end
104
+ def instantiate(_context)
105
+ if self.is_template?
106
+ cp = IDL::AST::TemplateParam.concrete_param(_context, @node)
107
+ cp.is_a?(Type) ? cp : ScopedName.new(cp)
108
+ else
109
+ self
110
+ end
111
+ end
112
+ end
113
+
114
+ class Integer < Type
115
+ def narrow(obj)
116
+ typeerror(obj) unless ::Integer === obj
117
+ typeerror(obj) unless self.class::Range === obj
118
+ obj
119
+ end
120
+
121
+ def self.is_unsigned?
122
+ self::Range.first == 0
123
+ end
124
+
125
+ def self.bits
126
+ self::BITS
127
+ end
128
+
129
+ def range_length
130
+ 1 + (self.class::Range.last - self.class::Range.first)
131
+ end
132
+
133
+ def min
134
+ self.class::Range.first
135
+ end
136
+
137
+ def max
138
+ self.class::Range.last
139
+ end
140
+
141
+ def in_range?(val)
142
+ val >= self.min && val <= self.max
143
+ end
144
+
145
+ def next(val)
146
+ val < self.max ? val + 1 : self.min
147
+ end
148
+
149
+ def Integer.newclass(range, bits)
150
+ k = Class.new(self)
151
+ k.const_set('Range', range)
152
+ k.const_set('BITS', bits)
153
+ k
154
+ end
155
+ end
156
+ Octet = Integer.newclass(0..0xFF, 8)
157
+ UShort = Integer.newclass(0..0xFFFF, 16)
158
+ ULong = Integer.newclass(0..0xFFFFFFFF, 32)
159
+ ULongLong = Integer.newclass(0..0xFFFFFFFFFFFFFFFF, 64)
160
+ Short = Integer.newclass(-0x8000...0x8000, 16)
161
+ Long = Integer.newclass(-0x80000000...0x80000000, 32)
162
+ LongLong = Integer.newclass(-0x8000000000000000...0x8000000000000000, 64)
163
+
164
+ class Boolean < Type
165
+ Range = [true, false]
166
+ def narrow(obj)
167
+ typeerror(obj) unless [TrueClass, FalseClass].include? obj.class
168
+ obj
169
+ end
170
+ def range_length
171
+ 2
172
+ end
173
+ def min
174
+ false
175
+ end
176
+ def max
177
+ true
178
+ end
179
+ def in_range?(val)
180
+ Range.include?(val)
181
+ end
182
+ def next(val)
183
+ !val
184
+ end
185
+ end
186
+ class Char < Type
187
+ def narrow(obj)
188
+ typeerror(obj) unless ::Integer === obj
189
+ typeerror(obj) unless (0..255) === obj
190
+ obj
191
+ end
192
+ def range_length
193
+ 256
194
+ end
195
+ def min
196
+ 0
197
+ end
198
+ def in_range?(val)
199
+ val >= self.min && val <= self.max
200
+ end
201
+ def max
202
+ 255
203
+ end
204
+
205
+ def next(val)
206
+ val < self.max ? val + 1 : self.min
207
+ end
208
+ end
209
+ class Float < Type
210
+ def narrow(obj)
211
+ typeerror(obj) unless ::Float === obj
212
+ obj
213
+ end
214
+ end
215
+ class Double < Float; end
216
+ class LongDouble < Float; end
217
+ class Fixed < Type
218
+ attr_reader :digits, :scale
219
+ def initialize(digits=nil, scale=nil)
220
+ raise RuntimeError, "significant digits for Fixed should be in the range 0-31" unless digits.nil? || (0..31) === digits.to_i
221
+ @digits = digits.nil? ? digits : digits.to_i
222
+ @scale = scale.nil? ? scale : scale.to_i
223
+ end
224
+ def narrow(obj)
225
+ #typeerror(obj)
226
+ obj
227
+ end
228
+ def is_anonymous?
229
+ false
230
+ end
231
+ def is_template?
232
+ (@size && @size.is_a?(IDL::Expression) && @size.is_template?)
233
+ end
234
+ def instantiate(_context)
235
+ self.is_template? ? (Type::Fixed.new(@size.instantiate(_context).value)) : self
236
+ end
237
+ end
238
+
239
+ class String < Type
240
+ attr_reader :size
241
+ def length; @size; end
242
+
243
+ def initialize(size = nil)
244
+ @size = size
245
+ end
246
+ def narrow(obj)
247
+ typeerror(obj) unless ::String === obj
248
+ if @size.nil?
249
+ obj
250
+ elsif @size < obj.size
251
+ typeerror(obj)
252
+ else
253
+ obj
254
+ end
255
+ end
256
+ def is_anonymous?
257
+ @size ? true : false
258
+ end
259
+ def is_template?
260
+ (@size && @size.is_a?(IDL::Expression) && @size.is_template?)
261
+ end
262
+ def instantiate(_context)
263
+ self.is_template? ? (Type::String.new(@size.instantiate(_context).value)) : self
264
+ end
265
+ end
266
+
267
+ class Sequence < Type
268
+ attr_reader :size, :basetype
269
+ attr_accessor :recursive
270
+ def length; @size; end
271
+ def initialize(t, size)
272
+ raise RuntimeError, "Anonymous type definitions are not allowed!" if t.is_anonymous?
273
+ @basetype = t
274
+ @size = size
275
+ @typename = format("sequence<%s%s>", t.typename,
276
+ if @size.nil? then
277
+ ""
278
+ else
279
+ ", #{IDL::Expression::ScopedName === size ? size.node.name : size.to_s}"
280
+ end)
281
+ @recursive = false
282
+ end
283
+ def typename
284
+ @typename
285
+ end
286
+ def narrow(obj)
287
+ typeerror(obj)
288
+ end
289
+ def is_complete?
290
+ @basetype.resolved_type.is_complete?
291
+ end
292
+ def is_local?(recurstk = [])
293
+ @basetype.resolved_type.is_local?(recurstk)
294
+ end
295
+ def is_recursive?
296
+ @recursive
297
+ end
298
+ def is_anonymous?
299
+ true
300
+ end
301
+ def is_template?
302
+ (@size && @size.is_a?(IDL::Expression::ScopedName) && @size.node.is_a?(IDL::AST::TemplateParam)) || @basetype.is_template?
303
+ end
304
+ def instantiate(_context)
305
+ self.is_template? ? Type::Sequence.new(@basetype.instantiate(_context), @size.instantiate(_context).value) : self
306
+ end
307
+ end
308
+
309
+ class Array < Type
310
+ attr_reader :basetype
311
+ attr_reader :sizes
312
+ def initialize(t, sizes)
313
+ raise RuntimeError, "Anonymous type definitions are not allowed!" if t.is_anonymous?
314
+ @basetype = t
315
+ if sizes.nil?
316
+ @sizes = []
317
+ @typename = t.typename + "[]"
318
+ else
319
+ @sizes = sizes
320
+ @typename = t.typename + sizes.collect { |s| "[#{IDL::Expression::ScopedName === s ? s.node.name : s.to_s}]" }.join
321
+ end
322
+ end
323
+ def typename
324
+ @typename
325
+ end
326
+ def narrow(obj)
327
+ typeerror(obj)
328
+ end
329
+ def is_complete?
330
+ @basetype.resolved_type.is_complete?
331
+ end
332
+ def is_local?(recurstk = [])
333
+ @basetype.resolved_type.is_local?(recurstk)
334
+ end
335
+ def is_anonymous?
336
+ true
337
+ end
338
+ def is_template?
339
+ @sizes.any? { |sz| (sz.is_a?(IDL::Expression::ScopedName) && sz.node.is_a?(IDL::AST::TemplateParam)) } || @basetype.is_template?
340
+ end
341
+ def instantiate(_context)
342
+ self.is_template? ? Type::Array.new(@basetype.instantiate(_context), @sizes.collect { |sz| sz.instantiate(_context).value }) : self
343
+ end
344
+ end
345
+
346
+ class WString < Type
347
+ attr_reader :size
348
+ def length; @size; end
349
+
350
+ def initialize(size = nil)
351
+ @size = size
352
+ end
353
+ def narrow(obj)
354
+ typeerror(obj) unless ::Array === obj
355
+ if @size.nil?
356
+ obj
357
+ elsif @size < obj.size
358
+ typeerror(obj)
359
+ else
360
+ obj
361
+ end
362
+ end
363
+ def is_anonymous?
364
+ @size ? true : false
365
+ end
366
+ def is_template?
367
+ (@size && @size.is_a?(IDL::Expression::ScopedName) && @size.node.is_a?(IDL::AST::TemplateParam))
368
+ end
369
+ def instantiate(_context)
370
+ self.is_template? ? Type::WString.new(@size.instantiate(_context).value) : self
371
+ end
372
+ end
373
+
374
+ class WChar < Type
375
+ def narrow(obj)
376
+ typeerror(obj) unless ::Array === obj
377
+ typeerror(obj) unless obj.size == 2
378
+ obj
379
+ end
380
+ end
381
+
382
+ class Any < Type
383
+ end
384
+
385
+ class Object < Type
386
+ end
387
+
388
+ class ValueBase < Type
389
+ end
390
+
391
+ class Native < Type
392
+ end
393
+
394
+ class TemplateModule < NodeType
395
+ end
396
+
397
+ class Interface < NodeType
398
+ end
399
+
400
+ class Home < NodeType
401
+ end
402
+
403
+ class Component < NodeType
404
+ end
405
+
406
+ class Porttype < NodeType
407
+ end
408
+
409
+ class Valuebox < NodeType
410
+ def is_local?(recurstk = [])
411
+ node.is_local?(recurstk)
412
+ end
413
+ end
414
+
415
+ class Valuetype < NodeType
416
+ def is_complete?
417
+ node.is_defined?
418
+ end
419
+ def is_local?(recurstk = [])
420
+ node.is_local?(recurstk)
421
+ end
422
+ end
423
+
424
+ class Eventtype < Valuetype
425
+ end
426
+
427
+ class Struct < NodeType
428
+ def is_complete?
429
+ node.is_defined?
430
+ end
431
+ def is_local?(recurstk = [])
432
+ node.is_local?(recurstk)
433
+ end
434
+ end
435
+
436
+ class Exception < Struct
437
+ end
438
+
439
+ class Union < NodeType
440
+ def is_complete?
441
+ node.is_defined?
442
+ end
443
+ def is_local?(recurstk = [])
444
+ node.is_local?(recurstk)
445
+ end
446
+ end
447
+
448
+ class Enum < NodeType
449
+ def narrow(obj)
450
+ typeerror(obj) unless ::Integer === obj
451
+ typeerror(obj) unless (0...@node.enumerators.length) === obj
452
+ obj
453
+ end
454
+ def range_length
455
+ @node.enumerators.length
456
+ end
457
+ def min
458
+ 0
459
+ end
460
+ def max
461
+ @node.enumerators.length-1
462
+ end
463
+ def in_range?(val)
464
+ val >= self.min && val <= self.max
465
+ end
466
+ def next(val)
467
+ val < self.max ? val + 1 : self.min
468
+ end
469
+ end
470
+
471
+ class Const < Type
472
+ attr_reader :type
473
+ def initialize(t)
474
+ @type = t
475
+ @typename = "const #{t.typename}"
476
+ end
477
+ def typename
478
+ @typename
479
+ end
480
+ def narrow(obj)
481
+ @type.narrow(obj)
482
+ end
483
+ def is_complete?
484
+ @type.resolved_type.is_complete?
485
+ end
486
+ def is_local?(recurstk = [])
487
+ @type.resolved_type.is_local?(recurstk)
488
+ end
489
+ def is_anonymous?
490
+ t.resolved_type.is_anonymous?
491
+ end
492
+ def is_template?
493
+ @type.is_template?
494
+ end
495
+ def instantiate(_context)
496
+ self.is_template? ? Type::Const.new(@type.instantiate(_context)) : self
497
+ end
498
+ def is_node?(node_class)
499
+ @type.is_node?(node_class)
500
+ end
501
+ def resolved_node
502
+ @type.resolved_node
503
+ end
504
+ end
505
+
506
+ end
507
+ end