OLD | NEW |
| (Empty) |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 """Resolve interface dependencies, producing a merged IdlDefinitions object. | |
30 | |
31 This library computes interface dependencies (partial interfaces and | |
32 implements), reads the dependency files, and merges them to the IdlDefinitions | |
33 for the main IDL file, producing an IdlDefinitions object representing the | |
34 entire interface. | |
35 | |
36 It also checks whether a file should have bindings generated, or whether | |
37 instead it is just a dependency. | |
38 """ | |
39 | |
40 import os.path | |
41 import cPickle as pickle | |
42 | |
43 # The following extended attributes can be applied to a dependency interface, | |
44 # and are then applied to the individual members when merging. | |
45 # Note that this moves the extended attribute from the interface to the member, | |
46 # which changes the semantics and yields different code than the same extended | |
47 # attribute on the main interface. | |
48 DEPENDENCY_EXTENDED_ATTRIBUTES = set([ | |
49 'Conditional', | |
50 'PerContextEnabled', | |
51 'RuntimeEnabled', | |
52 ]) | |
53 | |
54 | |
55 class InterfaceNotFoundError(Exception): | |
56 """Raised if (partial) interface not found in target IDL file.""" | |
57 pass | |
58 | |
59 | |
60 class InvalidPartialInterfaceError(Exception): | |
61 """Raised if a file listed as a partial interface is not in fact so.""" | |
62 pass | |
63 | |
64 | |
65 class InterfaceDependencyResolver(object): | |
66 def __init__(self, interfaces_info, reader): | |
67 """Initialize dependency resolver. | |
68 | |
69 Args: | |
70 interfaces_info: | |
71 dict of interfaces information, from compute_dependencies.py | |
72 reader: | |
73 IdlReader, used for reading dependency files | |
74 """ | |
75 self.interfaces_info = interfaces_info | |
76 self.reader = reader | |
77 | |
78 def resolve_dependencies(self, definitions, interface_name): | |
79 """Resolve dependencies, merging them into IDL definitions of main file. | |
80 | |
81 Dependencies consist of 'partial interface' for the same interface as | |
82 in the main file, and other interfaces that this interface 'implements'. | |
83 | |
84 Modifies definitions in place by adding parsed dependencies. | |
85 | |
86 Args: | |
87 definitions: IdlDefinitions object, modified in place | |
88 interface_name: | |
89 name of interface whose dependencies are being resolved | |
90 """ | |
91 # The Blink IDL filenaming convention is that the file | |
92 # <interface_name>.idl MUST contain the interface "interface_name" or | |
93 # exception "interface_name", unless it is a dependency (e.g., | |
94 # 'partial interface Foo' can be in FooBar.idl). | |
95 try: | |
96 target_interface = definitions.interfaces[interface_name] | |
97 except KeyError: | |
98 raise InterfaceNotFoundError('Could not find interface or exception
"{0}" in {0}.idl'.format(interface_name)) | |
99 | |
100 if interface_name not in self.interfaces_info: | |
101 # No dependencies, nothing to do | |
102 return | |
103 | |
104 interface_info = self.interfaces_info[interface_name] | |
105 if 'inherited_extended_attributes' in interface_info: | |
106 target_interface.extended_attributes.update( | |
107 interface_info['inherited_extended_attributes']) | |
108 | |
109 merge_interface_dependencies(target_interface, | |
110 interface_info['dependencies_full_paths'], | |
111 self.reader) | |
112 | |
113 for referenced_interface_name in interface_info['referenced_interfaces']
: | |
114 referenced_definitions = self.reader.read_idl_definitions( | |
115 self.interfaces_info[referenced_interface_name]['full_path']) | |
116 definitions.interfaces.update(referenced_definitions.interfaces) | |
117 | |
118 | |
119 def merge_interface_dependencies(target_interface, dependency_idl_filenames, rea
der): | |
120 """Merge dependencies ('partial interface' and 'implements') in dependency_i
dl_filenames into target_interface. | |
121 | |
122 No return: modifies target_interface in place. | |
123 """ | |
124 # Sort so order consistent, so can compare output from run to run. | |
125 for dependency_idl_filename in sorted(dependency_idl_filenames): | |
126 dependency_interface_basename, _ = os.path.splitext(os.path.basename(dep
endency_idl_filename)) | |
127 definitions = reader.read_idl_file(dependency_idl_filename) | |
128 | |
129 for dependency_interface in definitions.interfaces.itervalues(): | |
130 # Dependency files contain either partial interfaces for | |
131 # the (single) target interface, in which case the interface names | |
132 # must agree, or interfaces that are implemented by the target | |
133 # interface, in which case the interface names differ. | |
134 if (dependency_interface.is_partial and | |
135 dependency_interface.name != target_interface.name): | |
136 raise InvalidPartialInterfaceError('%s is not a partial interfac
e of %s. There maybe a bug in the the dependency generator (compute_dependencies
.py).' % (dependency_idl_filename, target_interface.name)) | |
137 merge_dependency_interface(target_interface, dependency_interface, d
ependency_interface_basename) | |
138 | |
139 | |
140 def merge_dependency_interface(target_interface, dependency_interface, dependenc
y_interface_basename): | |
141 """Merge dependency_interface into target_interface. | |
142 | |
143 Merging consists of storing certain interface-level data in extended | |
144 attributes of the *members* (because there is no separate dependency | |
145 interface post-merging), then concatenating the lists. | |
146 | |
147 The data storing consists of: | |
148 * applying certain extended attributes from the dependency interface | |
149 to its members | |
150 * storing the C++ class of the implementation in an internal | |
151 extended attribute of each member, [ImplementedBy] | |
152 | |
153 No return: modifies target_interface in place. | |
154 """ | |
155 merged_extended_attributes = dict( | |
156 (key, value) | |
157 for key, value in dependency_interface.extended_attributes.iteritems() | |
158 if key in DEPENDENCY_EXTENDED_ATTRIBUTES) | |
159 | |
160 # C++ class name of the implementation, stored in [ImplementedBy], which | |
161 # defaults to the basename of dependency IDL file. | |
162 # This can be overridden by [ImplementedAs] on the dependency interface. | |
163 # Note that [ImplementedAs] is used with different meanings on interfaces | |
164 # and members: | |
165 # for Blink class name and function name (or constant name), respectively. | |
166 # Thus we do not want to copy this from the interface to the member, but | |
167 # instead extract it and handle it separately. | |
168 merged_extended_attributes['ImplementedBy'] = ( | |
169 dependency_interface.extended_attributes.get( | |
170 'ImplementedAs', dependency_interface_basename)) | |
171 | |
172 def merge_lists(source_list, target_list): | |
173 for member in source_list: | |
174 member.extended_attributes.update(merged_extended_attributes) | |
175 target_list.extend(source_list) | |
176 | |
177 merge_lists(dependency_interface.attributes, target_interface.attributes) | |
178 merge_lists(dependency_interface.constants, target_interface.constants) | |
179 merge_lists(dependency_interface.operations, target_interface.operations) | |
OLD | NEW |