OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
3 # for details. All rights reserved. Use of this source code is governed by a | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 | |
6 """This module providesfunctionality for systems to generate | |
7 Dart interfaces from the IDL database.""" | |
8 | |
9 import os | |
10 import systembase | |
11 from generator import * | |
12 | |
13 class InterfacesSystem(systembase.System): | |
14 | |
15 def __init__(self, templates, database, emitters, output_dir): | |
16 super(InterfacesSystem, self).__init__( | |
17 templates, database, emitters, output_dir) | |
18 self._dart_interface_file_paths = [] | |
19 | |
20 | |
21 def InterfaceGenerator(self, | |
22 interface, | |
23 common_prefix, | |
24 super_interface_name, | |
25 source_filter): | |
26 """.""" | |
27 interface_name = interface.id | |
28 dart_interface_file_path = self._FilePathForDartInterface(interface_name) | |
29 | |
30 self._dart_interface_file_paths.append(dart_interface_file_path) | |
31 | |
32 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) | |
33 | |
34 template_file = 'interface_%s.darttemplate' % interface_name | |
35 template = self._templates.TryLoad(template_file) | |
36 if not template: | |
37 template = self._templates.Load('interface.darttemplate') | |
38 | |
39 return DartInterfaceGenerator( | |
40 interface, dart_interface_code, | |
41 template, | |
42 common_prefix, super_interface_name, | |
43 source_filter) | |
44 | |
45 def ProcessCallback(self, interface, info): | |
46 """Generates a typedef for the callback interface.""" | |
47 interface_name = interface.id | |
48 file_path = self._FilePathForDartInterface(interface_name) | |
49 self._ProcessCallback(interface, info, file_path) | |
50 | |
51 def GenerateLibraries(self, lib_dir): | |
52 pass | |
53 | |
54 | |
55 def _FilePathForDartInterface(self, interface_name): | |
56 """Returns the file path of the Dart interface definition.""" | |
57 return os.path.join(self._output_dir, 'src', 'interface', | |
58 '%s.dart' % interface_name) | |
59 | |
60 # ------------------------------------------------------------------------------ | |
61 | |
62 class DartInterfaceGenerator(object): | |
63 """Generates Dart Interface definition for one DOM IDL interface.""" | |
64 | |
65 def __init__(self, interface, emitter, template, | |
66 common_prefix, super_interface, source_filter): | |
67 """Generates Dart code for the given interface. | |
68 | |
69 Args: | |
70 interface -- an IDLInterface instance. It is assumed that all types have | |
71 been converted to Dart types (e.g. int, String), unless they are in the | |
72 same package as the interface. | |
73 common_prefix -- the prefix for the common library, if any. | |
74 super_interface -- the name of the common interface that this interface | |
75 implements, if any. | |
76 source_filter -- if specified, rewrites the names of any superinterfaces | |
77 that are not from these sources to use the common prefix. | |
78 """ | |
79 self._interface = interface | |
80 self._emitter = emitter | |
81 self._template = template | |
82 self._common_prefix = common_prefix | |
83 self._super_interface = super_interface | |
84 self._source_filter = source_filter | |
85 | |
86 | |
87 def StartInterface(self): | |
88 if self._super_interface: | |
89 typename = self._super_interface | |
90 else: | |
91 typename = self._interface.id | |
92 | |
93 | |
94 extends = [] | |
95 suppressed_extends = [] | |
96 | |
97 for parent in self._interface.parents: | |
98 # TODO(vsm): Remove source_filter. | |
99 if MatchSourceFilter(self._source_filter, parent): | |
100 # Parent is a DOM type. | |
101 extends.append(DartType(parent.type.id)) | |
102 elif '<' in parent.type.id: | |
103 # Parent is a Dart collection type. | |
104 # TODO(vsm): Make this check more robust. | |
105 extends.append(parent.type.id) | |
106 else: | |
107 suppressed_extends.append('%s.%s' % | |
108 (self._common_prefix, parent.type.id)) | |
109 | |
110 comment = ' extends' | |
111 extends_str = '' | |
112 if extends: | |
113 extends_str += ' extends ' + ', '.join(extends) | |
114 comment = ',' | |
115 if suppressed_extends: | |
116 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends)) | |
117 | |
118 factory_provider = None | |
119 constructor_info = AnalyzeConstructor(self._interface) | |
120 if constructor_info: | |
121 factory_provider = '_' + typename + 'FactoryProvider'; | |
122 | |
123 if typename in interface_factories: | |
124 factory_provider = interface_factories[typename] | |
125 | |
126 if factory_provider: | |
127 extends_str += ' default ' + factory_provider | |
128 | |
129 # TODO(vsm): Add appropriate package / namespace syntax. | |
130 (self._members_emitter, | |
131 self._top_level_emitter) = self._emitter.Emit( | |
132 self._template + '$!TOP_LEVEL', | |
133 ID=typename, | |
134 EXTENDS=extends_str) | |
135 | |
136 if constructor_info: | |
137 self._members_emitter.Emit( | |
138 '\n' | |
139 ' $CTOR($PARAMS);\n', | |
140 CTOR=typename, | |
141 PARAMS=constructor_info.ParametersInterfaceDeclaration()); | |
142 | |
143 element_type = MaybeTypedArrayElementType(self._interface) | |
144 if element_type: | |
145 self._members_emitter.Emit( | |
146 '\n' | |
147 ' $CTOR(int length);\n' | |
148 '\n' | |
149 ' $CTOR.fromList(List<$TYPE> list);\n' | |
150 '\n' | |
151 ' $CTOR.fromBuffer(ArrayBuffer buffer);\n', | |
152 CTOR=self._interface.id, | |
153 TYPE=DartType(element_type)) | |
154 | |
155 | |
156 def FinishInterface(self): | |
157 # TODO(vsm): Use typedef if / when that is supported in Dart. | |
158 # Define variant as subtype. | |
159 if (self._super_interface and | |
160 self._interface.id is not self._super_interface): | |
161 consts_emitter = self._top_level_emitter.Emit( | |
162 '\n' | |
163 'interface $NAME extends $BASE {\n' | |
164 '$!CONSTS' | |
165 '}\n', | |
166 NAME=self._interface.id, | |
167 BASE=self._super_interface) | |
168 for const in sorted(self._interface.constants, ConstantOutputOrder): | |
169 self._EmitConstant(consts_emitter, const) | |
170 | |
171 def AddConstant(self, constant): | |
172 if (not self._super_interface or | |
173 self._interface.id is self._super_interface): | |
174 self._EmitConstant(self._members_emitter, constant) | |
175 | |
176 def _EmitConstant(self, emitter, constant): | |
177 emitter.Emit('\n static final $TYPE $NAME = $VALUE;\n', | |
178 NAME=constant.id, | |
179 TYPE=DartType(constant.type.id), | |
180 VALUE=constant.value) | |
181 | |
182 def AddAttribute(self, getter, setter): | |
183 if getter and setter and getter.type.id == setter.type.id: | |
184 self._members_emitter.Emit('\n $TYPE $NAME;\n', | |
185 NAME=DartDomNameOfAttribute(getter), | |
186 TYPE=DartType(getter.type.id)); | |
187 return | |
188 if getter and not setter: | |
189 self._members_emitter.Emit('\n final $TYPE $NAME;\n', | |
190 NAME=DartDomNameOfAttribute(getter), | |
191 TYPE=DartType(getter.type.id)); | |
192 return | |
193 raise Exception('Unexpected getter/setter combination %s %s' % | |
194 (getter, setter)) | |
195 | |
196 def AddIndexer(self, element_type): | |
197 # Interface inherits all operations from List<element_type>. | |
198 pass | |
199 | |
200 def AddOperation(self, info): | |
201 """ | |
202 Arguments: | |
203 operations - contains the overloads, one or more operations with the same | |
204 name. | |
205 """ | |
206 self._members_emitter.Emit('\n' | |
207 ' $TYPE $NAME($PARAMS);\n', | |
208 TYPE=info.type_name, | |
209 NAME=info.name, | |
210 PARAMS=info.ParametersInterfaceDeclaration()) | |
211 | |
212 # Interfaces get secondary members directly via the superinterfaces. | |
213 def AddSecondaryAttribute(self, interface, getter, setter): | |
214 pass | |
215 | |
216 def AddSecondaryOperation(self, interface, attr): | |
217 pass | |
OLD | NEW |