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 """Read an IDL file, producing an IdlDocument object. | |
30 | |
31 This library calls the parser, which produces a generic Web IDL AST. | |
32 The AST is then converted into the IR, a Python object of class IdlDocument. | |
33 This is the last phase of the frontend, after the lexer and the parser. | |
34 | |
35 It also provides functions to merges interface dependencies (partial interfaces | |
36 and implements), and to validate extended attributes. | |
37 After calling these functions, the result is an IdlDocument representing a | |
38 complete, validated interface. | |
39 The IdlDocument is then used by the code generator to produce .h/.cpp files. | |
40 | |
41 Ideally the parser would generate the IdlDocument directly, rather then | |
42 requiring this additional phase, but that would require major changes to the | |
43 parser, which currently produces a generic AST instead. | |
44 | |
45 FIXME: Currently a stub, as part of landing the parser incrementally. | |
46 Just computes dependencies, and should always raise an exception, | |
47 indicating no file found, so no actual parsing done. | |
48 """ | |
49 | |
50 import os.path | |
51 | |
52 | |
53 class IdlDocument(): | |
54 """Top-level IDL class. | |
55 | |
56 In Web IDL spec terms, represents a set of definitions, obtained by parsing | |
57 a set of IDL fragments (i.e., the contents of .idl files).""" | |
58 # FIXME: Dummy class; full class hierarchy will be added with parser | |
59 pass | |
60 | |
61 | |
62 def read_idl_file(idl_filename, verbose=False): | |
63 """Returns an IdlDocument for an IDL file, without any dependencies.""" | |
64 # FIXME: Currently returns dummy object, as parser not present yet. | |
65 # parser = BlinkIDLParser(verbose=verbose) | |
66 # file_node = ParseFile(parser, idl_filename) | |
67 # return file_node_to_idl_document(file_node) | |
68 return IdlDocument() | |
69 | |
70 | |
71 def merge_interface_dependencies(idl_document, idl_filename, interface_dependenc ies_file, additional_idl_files): | |
72 """Merges dependencies into an existing IDL document. | |
73 | |
74 Modifies idl_document in place by adding parsed dependencies, and checks | |
75 whether bindings should be generated, returning bool. | |
76 | |
77 FIXME: stub: parser not implemented yet | |
78 | |
79 Arguments: | |
80 idl_document: IdlDocument object, modified in place | |
81 idl_filename: filename of main IDL file | |
haraken
2013/07/10 04:06:50
Nit: idl_filename => idl_file (for consistency wit
Nils Barth (inactive)
2013/07/10 06:00:15
Ack, I've been using _file for the File object its
| |
82 interface_dependencies_file: filename of dependencies file (produced by compute_dependencies.py) | |
83 additional_idl_files: list of additional files, not listed in interface_ dependencies_file, for which bindings should nonetheless be generated | |
84 Returns: | |
85 bool, whether bindings should be generated or not. | |
86 """ | |
87 basename = os.path.basename(idl_filename) | |
88 interface_name, _ = os.path.splitext(basename) | |
89 | |
90 dependency_idl_files = compute_dependency_idl_files(basename, interface_depe ndencies_file, additional_idl_files) | |
91 if dependency_idl_files is None: | |
92 return False | |
93 # FIXME: currently dependency_idl_files *must* be None (indicating that | |
94 # dummy .cpp and .h files should be generated), as actual parser not | |
95 # present yet. | |
96 raise RuntimeError('Stub: parser not implemented yet') | |
97 | |
98 | |
99 def compute_dependency_idl_files(target_idl_basename, interface_dependencies_fil ename, additional_idl_files): | |
haraken
2013/07/10 04:06:50
compute_dependency_idl_files => compute_interface_
Nils Barth (inactive)
2013/07/10 06:00:15
This actually returns the file names:
since partia
| |
100 """Returns list of IDL file dependencies for a given main IDL file. | |
101 | |
102 - Returns a list of IDL files on which a given IDL file depends, | |
103 possibly empty. | |
104 Dependencies consist of partial interface files and files for other | |
105 interfaces that the given interface implements. | |
106 - Returns an empty list also if the given IDL file is an additional IDL | |
107 file. | |
108 - Otherwise, return None. This happens when the given IDL file is a | |
109 dependency, for which we don't want to generate bindings. | |
110 """ | |
111 if interface_dependencies_filename is None: | |
112 return [] | |
113 | |
114 # The format of the interface dependencies file is: | |
115 # | |
116 # Document.idl P.idl | |
117 # Event.idl | |
118 # Window.idl Q.idl R.idl S.idl | |
haraken
2013/07/10 04:06:50
Probably the format of the dependency file should
Nils Barth (inactive)
2013/07/10 06:00:15
Yeah, this definitely needs cleaning up, but it's
| |
119 # ... | |
120 # | |
121 # The above indicates that: | |
122 # Document.idl depends on P.idl, | |
123 # Event.idl depends on no other IDL files, and | |
124 # Window.idl depends on Q.idl, R.idl, and S.idl. | |
125 # | |
126 # The head entries (first IDL file on a line) are the files that should | |
127 # have bindings generated. | |
128 # | |
129 # An IDL file that is a dependency of another IDL file (e.g., P.idl in the | |
130 # above example) does not have its own line in the dependency file: | |
131 # dependencies do not have bindings generated, and do not have their | |
132 # own dependencies. | |
133 with open(interface_dependencies_filename) as interface_dependencies_file: | |
134 for line in interface_dependencies_file: | |
135 idl_filename, _, dependency_files = line.partition(' ') | |
136 if os.path.basename(idl_filename) == target_idl_basename: | |
137 return dependency_files.split() | |
138 | |
139 # additional_idl_files is a list of IDL files which should not be included | |
140 # in DerivedSources*.cpp, and hence are not listed in the interface | |
141 # dependencies file, but for which we should generate .cpp and .h files. | |
142 additional_idl_basenames = set(map(os.path.basename, additional_idl_files)) | |
143 if target_idl_basename in additional_idl_basenames: | |
144 return [] | |
145 | |
146 return None | |
OLD | NEW |