5"""This module generates Dart APIs from the IDL database."""
13from generator
import *
14from idlnode
import IDLType, IDLInterface, resolveTypedef
16_logger = logging.getLogger(
'dartgenerator')
20 node.operations.extend(other.operations)
21 for attribute
in other.attributes:
22 if not node.has_attribute(attribute):
23 node.attributes.append(attribute)
25 node.constants.extend(other.constants)
29 """Utilities to generate Dart APIs and corresponding JavaScript."""
31 def __init__(self, logging_level=logging.WARNING):
34 _logger.setLevel(logging_level)
37 return type_name.split(
'::')[-1]
40 if IsRegisteredType(type_name):
44 if database.HasTypeDef(type_name):
47 if type_name.endswith(
'?'):
50 if type_name.endswith(
'[]'):
54 if (database.HasInterface(stripped_type_name)
or
55 database.HasDictionary(stripped_type_name)):
58 if database.HasEnum(stripped_type_name):
62 if dart_template_match:
64 parent_type_name = type_name[0:dart_template_match.start(1) - 1]
65 sub_type_name = dart_template_match.group(1)
71 return '.' in type_name
75 for (dirname, _, names)
in os.walk(auxiliary_dir):
77 if name.endswith(
'.dart'):
82 """Removes unidentified types.
84 Removes constants, attributes, operations and parents with unidentified
88 for interface
in database.GetInterfaces():
90 def IsIdentified(idl_node):
91 node_name = idl_node.id
if idl_node.id
else 'parent'
93 type_name = idl_type.id
94 if (type_name
is not None and
98 if not (interface.id
in [
99 'Window',
'WorkerContext',
'WorkerGlobalScope'
100 ]
and type_name.endswith(
'Constructor')):
102 'removing %s in %s which has unidentified type %s' %
103 (node_name, interface.id, type_name))
107 interface.constants = list(filter(IsIdentified,
108 interface.constants))
109 interface.attributes = list(
110 filter(IsIdentified, interface.attributes))
111 interface.operations = list(
112 filter(IsIdentified, interface.operations))
113 interface.parents = list(filter(IsIdentified, interface.parents))
119 exclude_displaced=[],
120 exclude_suppressed=[]):
121 """Filters a database to remove interfaces and members that are missing
124 The FremontCut IDLs use annotations to specify implementation
125 status in various platforms. For example, if a member is annotated
126 with @WebKit, this means that the member is supported by WebKit.
129 database -- the database to filter
130 all_annotations -- a list of annotation names a member has to
131 have or it will be filtered.
132 or_annotations -- if a member has one of these annotations, it
133 won't be filtered even if it is missing some of the
135 exclude_displaced -- if a member has this annotation and it
136 is marked as displaced it will always be filtered.
137 exclude_suppressed -- if a member has this annotation and it
138 is marked as suppressed it will always be filtered.
142 for interface
in database.GetInterfaces():
144 def HasAnnotations(idl_node):
145 """Utility for determining if an IDLNode has all
146 the required annotations"""
147 for a
in exclude_displaced:
148 if (a
in idl_node.annotations
and
149 'via' in idl_node.annotations[a]):
151 for a
in exclude_suppressed:
152 if (a
in idl_node.annotations
and
153 'suppressed' in idl_node.annotations[a]):
155 for a
in or_annotations:
156 if a
in idl_node.annotations:
158 if and_annotations == []:
160 for a
in and_annotations:
161 if a
not in idl_node.annotations:
165 if HasAnnotations(interface):
166 interface.constants = filter(HasAnnotations,
168 interface.attributes = filter(HasAnnotations,
169 interface.attributes)
170 interface.operations = filter(HasAnnotations,
171 interface.operations)
172 interface.parents = filter(HasAnnotations, interface.parents)
174 database.DeleteInterface(interface.id)
178 def Generate(self, database, super_database, generate_interface):
183 for interface
in database.GetInterfaces():
184 if not MatchSourceFilter(interface):
186 _logger.info(
'Omitting interface - %s' % interface.id)
188 interfaces.append(interface)
192 web_gl_constants = []
196 interface_name = interface.id
198 if auxiliary_file
is not None:
199 _logger.info(
'Skipping %s because %s exists' % (interface_name,
203 _logger.info(
'Generating %s' % interface.id)
204 generate_interface(interface, gl_constants=web_gl_constants)
207 web_gl_constants_interface =
IDLInterface(
None,
"WebGL")
208 web_gl_constants_interface.constants = web_gl_constants
209 self.
_database._all_interfaces[
'WebGL'] = web_gl_constants_interface
210 generate_interface(web_gl_constants_interface)
213 """Returns the interfaces in pre-order, i.e. parents first."""
217 def visit(interface):
218 if interface.id
in seen:
220 seen.add(interface.id)
221 for parent
in interface.parents:
222 if IsDartCollectionType(parent.type.id):
224 if self.
_database.HasInterface(parent.type.id):
225 parent_interface = self.
_database.GetInterface(
227 visit(parent_interface)
228 ordered.append(interface)
230 for interface
in interfaces:
235 if interface.id ==
'EventTarget':
237 for parent
in interface.parents:
238 parent_name = parent.type.id
239 if database.HasInterface(parent_name):
240 parent_interface = database.GetInterface(parent.type.id)
246 for interface
in database.GetInterfaces():
249 interface.ext_attrs[
'EventTarget'] =
None
250 elif 'EventTarget' in interface.ext_attrs:
253 ast = [(
'Annotation', [(
'Id',
'WebKit')]),
254 (
'InterfaceType', (
'ScopedName',
'EventTarget'))]
260 for interface
in database.GetInterfaces():
261 for operation
in interface.operations:
262 call_with = operation.ext_attrs.get(
'CallWith', [])
263 if not (isinstance(call_with, list)):
264 call_with = [call_with]
265 constructor_with = operation.ext_attrs.get(
266 'ConstructorCallWith', [])
267 if not (isinstance(constructor_with, list)):
268 constructor_with = [constructor_with]
269 call_with = call_with + constructor_with
271 if 'ScriptArguments' in call_with:
272 operation.arguments.append(ARG)
275 for interface
in database.GetInterfaces():
276 for operation
in interface.operations:
278 if ((operation.id ==
'setInterval' or operation.id ==
'setTimeout')
and \
279 operation.arguments[0].type.id ==
'any'):
280 operation.arguments.pop(2)
283 for index, argument
in enumerate(operation.arguments):
284 type_name = argument.type.id
285 if database.HasEnum(type_name):
286 operation.arguments[index].type =
IDLType(
'DOMString')
static bool match(const char *needle, const char *haystack)
LoadAuxiliary(self, auxiliary_dir)
_IsDartType(self, type_name)
Generate(self, database, super_database, generate_interface)
FilterInterfaces(self, database, and_annotations=[], or_annotations=[], exclude_displaced=[], exclude_suppressed=[])
FixEventTargets(self, database)
FilterMembersWithUnidentifiedTypes(self, database)
_StripModules(self, type_name)
_PreOrderInterfaces(self, interfaces)
_IsCompoundType(self, database, type_name)
IsEventTarget(self, database, interface)
__init__(self, logging_level=logging.WARNING)
AddMissingArguments(self, database)
CleanupOperationArguments(self, database)