Synopsis - Cross-Reference
File:
Synopsis/Parsers/IDL/idlast.py 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124"""Classes and functions for handling the IDL Abstract Syntax Tree
125
126Function:
127
128 findDecl(scopedName) -- find a Decl object given a fully-scoped
129 name, represented as a list of strings.
130 eg. ::foo::bar::baz is represented as
131 ['foo', 'bar', 'baz'].
132Classes:
133
134 AST -- top level of Abstract Syntax Tree.
135 Decl -- base of all declarations.
136 DeclRepoId -- mixin class for Decls with repository ids.
137 Pragma -- record of an unknown pragma
138 Comment -- record of a comment
139 Module -- module declaration.
140 Interface -- interface declaration.
141 Forward -- forward-declared interface.
142 Const -- constant declaration.
143 Declarator -- declarator used in typedef, struct members, etc.
144 Typedef -- typedef.
145 Member -- member of a struct or exception.
146 Struct -- struct declaration.
147 Exception -- exception declaration.
148 CaseLabel -- case label within a union.
149 UnionCase -- one case within a union.
150 Union -- union declaration.
151 Enumerator -- enumerator of an enum.
152 Enum -- enum declaration.
153 Attribute -- attribute declaration.
154 Parameter -- parameter of an operation of factory.
155 Operation -- operation declaration.
156 Native -- native declaration.
157 StateMember -- state member of a valuetype.
158 Factory -- factory method of a valuetype.
159 ValueForward -- forward-declared valuetype.
160 ValueBox -- boxed value declaration.
161 ValueAbs -- abstract valuetype declaration.
162 Value -- valuetype declaration."""
163
164import idlutil
165import idlvisitor
166
167class AST:
168 """Class for top-level Abstract Syntax Tree.
169
170Functions:
171
172 file() -- the file name of the main IDL file.
173 declarations() -- list of Decl objects corresponding to declarations
174 at file scope.
175 pragmas() -- list of Pragma objects containing #pragmas which
176 occurred before any declarations. Later #pragmas
177 are attached to Decl objects.
178 comments() -- list of Comment objects containing comments which
179 occurred before any declarations.
180 accept(visitor) -- visitor pattern accept. See idlvisitor.py."""
181
182 def __init__(self, file, declarations, pragmas, comments):
183 self.__file = file
184 self.__declarations = declarations
185 self.__pragmas = pragmas
186 self.__comments = comments
187
188 def file(self): return self.__file
189 def declarations(self): return self.__declarations
190 def pragmas(self): return self.__pragmas
191 def comments(self): return self.__comments
192 def accept(self, visitor): visitor.visitAST(self)
193
194
195class Decl:
196 """Base class for all declarations.
197
198Functions:
199
200 file() -- the IDL file this declaration came from.
201 line() -- the line number within the file.
202 mainFile() -- boolean: true if the file was the main IDL file;
203 false if it was an included file.
204 pragmas() -- list of Pragma objects containing #pragmas which
205 immediately followed this declaration.
206 comments() -- list of Comment objects containing comments which
207 immediately followed this declaration.
208 fullDecl() -- the 'full' Decl for typedefs, forwards, etc.
209
210 accept(visitor) -- visitor pattern accept. See idlvisitor.py."""
211
212 def __init__(self, file, line, mainFile, pragmas, comments):
213 self.__file = file
214 self.__line = line
215 self.__mainFile = mainFile
216 self.__builtIn = 0
217 self.__pragmas = pragmas
218 self.__comments = comments
219
220 def accept(self, visitor): pass
221
222 def file(self): return self.__file
223 def line(self): return self.__line
224 def mainFile(self): return self.__mainFile
225 def builtIn(self): return self.__builtIn
226 def pragmas(self): return self.__pragmas
227 def comments(self): return self.__comments
228 def fullDecl(self): return self
229
230
231class DeclRepoId :
232 """Mixin class for Decls which have a Repository Id
233
234Functions:
235
236 identifier() -- name of the declaration as a string
237 scopedName() -- list of strings forming the fully-scoped name of the
238 declaration. e.g. ::foo::bar::baz is represented as
239 ['foo', 'bar', 'baz'].
240 repoId() -- repository identifier for this declaration."""
241
242 def __init__(self, identifier, scopedName, repoId):
243 self.__identifier = identifier
244 self.__scopedName = scopedName
245 self.__repoId = repoId
246
247 def identifier(self): return self.__identifier
248 def scopedName(self): return self.__scopedName
249 def repoId(self): return self.__repoId
250
251
252
253
254class Pragma :
255 """Class containing information about an unknown pragma
256
257Functions:
258
259 text() -- text of the pragma
260 __str__() -- same as text()
261 file() -- file containing the pragma
262 line() -- line number in file"""
263
264 def __init__(self, text, file, line):
265 self.__text = text
266 self.__file = file
267 self.__line = line
268
269 def text(self) : return self.__text
270 def __str__(self) : return self.__text
271 def file(self) : return self.__file
272 def line(self) : return self.__line
273
274class Comment :
275 """Class containing information about a comment
276
277Functions:
278
279 text() -- text of the comment
280 __str__() -- same as text()
281 file() -- file containing the comment
282 line() -- line number in file"""
283
284 def __init__(self, text, file, line):
285 self.__text = text
286 self.__file = file
287 self.__line = line
288
289 def text(self) : return self.__text
290 def __str__(self) : return self.__text
291 def file(self) : return self.__file
292 def line(self) : return self.__line
293
294
295
296
297
298
299class Module (Decl, DeclRepoId):
300 """Module declaration (Decl, DeclRepoId)
301
302Functions:
303
304 definitions() -- list of Decl objects declared within this module.
305 continuations() -- list containing continuations of this module.
306 When modules are re-opened, multiple Module
307 objects with the same name appear in the
308 enclosing Module or AST object. In case it's
309 useful, the first Module object for a particular
310 module has a list containing continuations of
311 that module. You will probably not have any use
312 for this."""
313
314 def __init__(self, file, line, mainFile, pragmas, comments,
315 identifier, scopedName, repoId,
316 definitions):
317
318 Decl.__init__(self, file, line, mainFile, pragmas, comments)
319 DeclRepoId.__init__(self, identifier, scopedName, repoId)
320
321 self.__definitions = definitions
322 self._continuations = []
323
324 def accept(self, visitor): visitor.visitModule(self)
325
326 def definitions(self): return self.__definitions
327 def continuations(self): return self._continuations
328
329
330class Interface (Decl, DeclRepoId):
331 """Interface declaration (Decl, DeclRepoId)
332
333Functions:
334
335 abstract() -- boolean: true if the interface is declared abstract.
336 local() -- boolean: true if the interface is declared local.
337 inherits() -- list of Interface objects from which this one
338 inherits.
339 contents() -- list of Decl objects for all items declared within
340 this interface.
341 declarations() -- subset of contents() containing types, constants
342 and exceptions.
343 callables() -- subset of contents() containing Operations and
344 Attributes.
345 all_callables()-- callables of this and inherited interfaces."""
346
347 def __init__(self, file, line, mainFile, pragmas, comments,
348 identifier, scopedName, repoId,
349 abstract, local, inherits):
350
351 Decl.__init__(self, file, line, mainFile, pragmas, comments)
352 DeclRepoId.__init__(self, identifier, scopedName, repoId)
353
354 self.__abstract = abstract
355 self.__local = local
356 self.__inherits = inherits
357 self.__contents = []
358 self.__declarations = []
359 self.__callables = []
360
361 def _setContents(self, contents):
362 self.__contents = contents
363 self.__declarations = filter(lambda c: not (isinstance(c, Attribute) or
364 isinstance(c, Operation)),
365 contents)
366 self.__callables = filter(lambda c: (isinstance(c, Attribute) or
367 isinstance(c, Operation)),
368 contents)
369
370 def accept(self, visitor): visitor.visitInterface(self)
371
372 def abstract(self): return self.__abstract
373 def local(self): return self.__local
374 def inherits(self): return self.__inherits
375 def contents(self): return self.__contents
376 def declarations(self): return self.__declarations
377 def callables(self): return self.__callables
378 def all_callables(self):
379 r = []
380
381
382 for b in self.__inherits:
383 for c in b.all_callables():
384 if c not in r:
385 r.append(c)
386 r.extend(self.__callables)
387 return r
388
389
390class Forward (Decl, DeclRepoId):
391 """Forward-declared interface (Decl, DeclRepoId)
392
393Functions:
394
395 abstract() -- boolean: true if the interface is declared abstract.
396 local() -- boolean: true if the interface is declared local.
397 fullDecl() -- Interface object corresponding to full interface
398 declaration or None if there is no full declaration."""
399
400 def __init__(self, file, line, mainFile, pragmas, comments,
401 identifier, scopedName, repoId,
402 abstract, local):
403
404 Decl.__init__(self, file, line, mainFile, pragmas, comments)
405 DeclRepoId.__init__(self, identifier, scopedName, repoId)
406
407 self.__abstract = abstract
408 self.__local = local
409 self._fullDecl = None
410 self._more = []
411
412 def accept(self, visitor): visitor.visitForward(self)
413
414 def abstract(self): return self.__abstract
415 def local(self): return self.__abstract
416 def fullDecl(self): return self._fullDecl
417
418
419class Const (Decl, DeclRepoId):
420 """Constant declaration (Decl, DeclRepoId)
421
422Functions:
423
424 constType() -- IdlType.Type object of this constant. Aliases not
425 stripped.
426 constKind() -- TypeCode kind of constant with aliases stripped.
427 value() -- value of the constant. Either an integer or an
428 Enumerator object."""
429
430 def __init__(self, file, line, mainFile, pragmas, comments,
431 identifier, scopedName, repoId,
432 constType, constKind, value):
433
434 Decl.__init__(self, file, line, mainFile, pragmas, comments)
435 DeclRepoId.__init__(self, identifier, scopedName, repoId)
436
437 self.__constType = constType
438 self.__constKind = constKind
439 self.__value = value
440
441
442 def accept(self, visitor): visitor.visitConst(self)
443
444 def constType(self): return self.__constType
445 def constKind(self): return self.__constKind
446 def value(self): return self.__value
447
448
449class Declarator (Decl, DeclRepoId):
450 """Declarator used in typedefs, struct members, etc. (Decl, DeclRepoId)
451
452Functions:
453
454 sizes() -- list of array sizes, or None if this is a simple
455 declarator.
456 alias() -- Typedef object for this declarator if this is a typedef
457 declarator. None otherwise."""
458
459 def __init__(self, file, line, mainFile, pragmas, comments,
460 identifier, scopedName, repoId,
461 sizes):
462
463 Decl.__init__(self, file, line, mainFile, pragmas, comments)
464 DeclRepoId.__init__(self, identifier, scopedName, repoId)
465
466 self.__sizes = sizes
467 self.__alias = None
468
469 def _setAlias(self, alias): self.__alias = alias
470
471 def accept(self, visitor): visitor.visitDeclarator(self)
472
473 def sizes(self): return self.__sizes
474 def alias(self): return self.__alias
475
476 def fullDecl(self):
477 if self.__alias is not None:
478 try:
479 return self.__alias.aliasType().decl()
480 except AttributeError:
481 pass
482 return self
483
484
485class Typedef (Decl):
486 """Typedef (Decl)
487
488Functions:
489
490 aliasType() -- IdlType.Type object that this is an alias to.
491 constrType() -- boolean: true if the alias type was constructed
492 within this typedef declaration.
493 declarators() -- list of Declarator objects."""
494
495 def __init__(self, file, line, mainFile, pragmas, comments,
496 aliasType, constrType, declarators):
497
498 Decl.__init__(self, file, line, mainFile, pragmas, comments)
499
500 self.__aliasType = aliasType
501 self.__constrType = constrType
502 self.__declarators = declarators
503
504 def accept(self, visitor): visitor.visitTypedef(self)
505
506 def aliasType(self): return self.__aliasType
507 def constrType(self): return self.__constrType
508 def declarators(self): return self.__declarators
509
510
511class Member (Decl):
512 """Member of a struct or exception (Decl)
513
514Functions:
515
516 memberType() -- IdlType.Type object for the type of this member.
517 constrType() -- boolean: true if the member type was constructed
518 within the member declaration.
519 declarators() -- list of Declarator objects."""
520
521 def __init__(self, file, line, mainFile, pragmas, comments,
522 memberType, constrType, declarators):
523
524 Decl.__init__(self, file, line, mainFile, pragmas, comments)
525
526 self.__memberType = memberType
527 self.__constrType = constrType
528 self.__declarators = declarators
529
530 def accept(self, visitor): visitor.visitMember(self)
531
532 def memberType(self): return self.__memberType
533 def constrType(self): return self.__constrType
534 def declarators(self): return self.__declarators
535
536
537class Struct (Decl, DeclRepoId):
538 """Struct declaration (Decl, DeclRepoId)
539
540Functions:
541
542 members() -- list of Member objects for the struct contents.
543 recursive() -- boolean: true if the struct is recursive."""
544
545 def __init__(self, file, line, mainFile, pragmas, comments,
546 identifier, scopedName, repoId,
547 recursive):
548
549 Decl.__init__(self, file, line, mainFile, pragmas, comments)
550 DeclRepoId.__init__(self, identifier, scopedName, repoId)
551
552 self.__recursive = recursive
553
554 def _setMembers(self, members):
555 self.__members = members
556
557 def accept(self, visitor): visitor.visitStruct(self)
558
559 def members(self): return self.__members
560 def recursive(self): return self.__recursive
561
562
563class StructForward (Decl, DeclRepoId):
564 """Struct forward declaration (Decl, DeclRepoId)
565
566Functions:
567
568 fullDecl() -- full definition of the struct."""
569
570 def __init__(self, file, line, mainFile, pragmas, comments,
571 identifier, scopedName, repoId):
572
573 Decl.__init__(self, file, line, mainFile, pragmas, comments)
574 DeclRepoId.__init__(self, identifier, scopedName, repoId)
575
576 self._fullDecl = None
577 self._more = []
578
579 def accept(self, visitor): visitor.visitStructForward(self)
580
581 def fullDecl(self): return self._fullDecl
582
583
584class Exception (Decl, DeclRepoId):
585 """Exception declaration (Decl, DeclRepoId)
586
587Function:
588
589 members() -- list of Member objects for the exception contents."""
590
591 def __init__(self, file, line, mainFile, pragmas, comments,
592 identifier, scopedName, repoId,
593 members):
594
595 Decl.__init__(self, file, line, mainFile, pragmas, comments)
596 DeclRepoId.__init__(self, identifier, scopedName, repoId)
597
598 self.__members = members
599
600
601 def accept(self, visitor): visitor.visitException(self)
602
603 def members(self): return self.__members
604
605
606class CaseLabel (Decl):
607 """Case label within a union (Decl)
608
609Functions:
610
611 default() -- boolean: true if this is the default label.
612 value() -- label value. Either an integer or an Enumerator
613 object. If default() is true, returns a value used by
614 none of the other union labels.
615 labelKind() -- TypeCode kind of label."""
616
617 def __init__(self, file, line, mainFile, pragmas, comments,
618 default, value, labelKind):
619
620 Decl.__init__(self, file, line, mainFile, pragmas, comments)
621
622 self.__default = default
623 self.__value = value
624 self.__labelKind = labelKind
625
626 def accept(self, visitor): visitor.visitCaseLabel(self)
627
628 def default(self): return self.__default
629 def value(self): return self.__value
630 def labelKind(self): return self.__labelKind
631
632
633class UnionCase (Decl):
634 """One case within a union (Decl)
635
636Functions:
637
638 labels() -- list of CaseLabel objects.
639 caseType() -- IdlType.Type object for the case type.
640 constrType() -- boolean: true if the case type was constructed
641 within the case.
642 declarator() -- Declarator object"""
643
644 def __init__(self, file, line, mainFile, pragmas, comments,
645 labels, caseType, constrType, declarator):
646
647 Decl.__init__(self, file, line, mainFile, pragmas, comments)
648
649 self.__labels = labels
650 self.__caseType = caseType
651 self.__constrType = constrType
652 self.__declarator = declarator
653
654 def accept(self, visitor): visitor.visitUnionCase(self)
655
656 def labels(self): return self.__labels
657 def caseType(self): return self.__caseType
658 def constrType(self): return self.__constrType
659 def declarator(self): return self.__declarator
660
661
662class Union (Decl, DeclRepoId):
663 """Union declaration (Decl, DeclRepoId)
664
665Functions:
666
667 switchType() -- IdlType.Type object corresponding to the switch type.
668 constrType() -- boolean: true if the switch type was declared within
669 the switch statement. Only possible for Enums.
670 cases() -- list of UnionCase objects.
671 recursive() -- boolean: true if the union is recursive."""
672
673
674 def __init__(self, file, line, mainFile, pragmas, comments,
675 identifier, scopedName, repoId,
676 switchType, constrType, recursive):
677
678 Decl.__init__(self, file, line, mainFile, pragmas, comments)
679 DeclRepoId.__init__(self, identifier, scopedName, repoId)
680
681 self.__switchType = switchType
682 self.__constrType = constrType
683 self.__recursive = recursive
684
685 def _setCases(self, cases):
686 self.__cases = cases
687
688 def accept(self, visitor): visitor.visitUnion(self)
689
690 def switchType(self): return self.__switchType
691 def constrType(self): return self.__constrType
692 def cases(self): return self.__cases
693 def recursive(self): return self.__recursive
694
695
696class UnionForward (Decl, DeclRepoId):
697 """Union forward declaration (Decl, DeclRepoId)
698
699Functions:
700
701 fullDecl() -- full definition of the union."""
702
703 def __init__(self, file, line, mainFile, pragmas, comments,
704 identifier, scopedName, repoId):
705
706 Decl.__init__(self, file, line, mainFile, pragmas, comments)
707 DeclRepoId.__init__(self, identifier, scopedName, repoId)
708
709 self._fullDecl = None
710 self._more = []
711
712 def accept(self, visitor): visitor.visitUnionForward(self)
713
714 def fullDecl(self): return self._fullDecl
715
716
717class Enumerator (Decl, DeclRepoId):
718 """Enumerator of an Enum (Decl, DeclRepoId)
719
720Function:
721
722 value() -- integer value of enumerator, as marshalled."""
723
724 def __init__(self, file, line, mainFile, pragmas, comments,
725 identifier, scopedName, repoId, value):
726
727 Decl.__init__(self, file, line, mainFile, pragmas, comments)
728 DeclRepoId.__init__(self, identifier, scopedName, repoId)
729
730 self.__value = value
731
732 def accept(self, visitor): visitor.visitEnumerator(self)
733
734 def value(self): return self.__value
735
736
737class Enum (Decl, DeclRepoId):
738 """Enum declaration (Decl, DeclRepoId)
739
740Function:
741
742 enumerators() -- list of Enumerator objects."""
743
744 def __init__(self, file, line, mainFile, pragmas, comments,
745 identifier, scopedName, repoId,
746 enumerators):
747
748 Decl.__init__(self, file, line, mainFile, pragmas, comments)
749 DeclRepoId.__init__(self, identifier, scopedName, repoId)
750
751 self.__enumerators = enumerators
752
753 def accept(self, visitor): visitor.visitEnum(self)
754
755 def enumerators(self): return self.__enumerators
756
757
758class Attribute (Decl):
759 """Attribute declaration (Decl)
760
761Functions:
762
763 readonly() -- boolean: true if the attribute is read only.
764 attrType() -- IdlType.Type object for the attribute's type.
765 declarators() -- list of the attribute's declarators.
766 identifiers() -- list of strings containing the attribute identifiers
767 (equivalent to the identifiers inside the declarators)."""
768
769 def __init__(self, file, line, mainFile, pragmas, comments,
770 readonly, attrType, declarators):
771
772 Decl.__init__(self, file, line, mainFile, pragmas, comments)
773
774 self.__readonly = readonly
775 self.__attrType = attrType
776 self.__declarators = declarators
777 self.__identifiers = map(lambda d: d.identifier(), declarators)
778
779
780 def accept(self, visitor): visitor.visitAttribute(self)
781
782 def readonly(self): return self.__readonly
783 def attrType(self): return self.__attrType
784 def declarators(self): return self.__declarators
785 def identifiers(self): return self.__identifiers
786
787
788class Parameter (Decl):
789 """A Parameter of an operation or factory specifier (Decl)
790
791Functions:
792
793 direction() -- integer: 0 == in, 1 == out, 2 == inout.
794 is_in() -- boolean: true if in or inout.
795 is_out() -- boolean: true if out or inout.
796 paramType() -- IdlType.Type object for the parameter type.
797 identifier() -- string of parameter identifier."""
798
799 def __init__(self, file, line, mainFile, pragmas, comments,
800 direction, paramType, identifier):
801
802 Decl.__init__(self, file, line, mainFile, pragmas, comments)
803
804 self.__direction = direction
805 self.__is_in = (direction == 0 or direction == 2)
806 self.__is_out = (direction == 1 or direction == 2)
807 self.__paramType = paramType
808 self.__identifier = identifier
809
810
811 def accept(self, visitor): visitor.visitParameter(self)
812
813 def direction(self): return self.__direction
814 def is_in(self): return self.__is_in
815 def is_out(self): return self.__is_out
816 def paramType(self): return self.__paramType
817 def identifier(self): return self.__identifier
818
819
820class Operation (Decl, DeclRepoId):
821 """Operation declaration (Decl, DeclRepoId)
822
823Functions:
824
825 oneway() -- boolean: true if operation is one way.
826 returnType() -- IdlType.Type object for return type.
827 parameters() -- list of Parameter objects.
828 raises() -- list of Exception objects.
829 contexts() -- list of strings for context expressions."""
830
831 def __init__(self, file, line, mainFile, pragmas, comments,
832 oneway, returnType, identifier, scopedName, repoId,
833 parameters, raises, contexts):
834
835 Decl.__init__(self, file, line, mainFile, pragmas, comments)
836 DeclRepoId.__init__(self, identifier, scopedName, repoId)
837
838 self.__oneway = oneway
839 self.__returnType = returnType
840 self.__parameters = parameters
841 self.__raises = raises
842 self.__contexts = contexts
843
844
845 def accept(self, visitor): visitor.visitOperation(self)
846
847 def oneway(self): return self.__oneway
848 def returnType(self): return self.__returnType
849 def parameters(self): return self.__parameters
850 def raises(self): return self.__raises
851 def