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 provides base functionality for systems to generate | |
7 Dart APIs from the IDL database.""" | |
8 | |
9 import os | |
10 #import re | |
11 import generator | |
12 | |
13 def MassagePath(path): | |
14 # The most robust way to emit path separators is to use / always. | |
15 return path.replace('\\', '/') | |
16 | |
17 class System(object): | |
18 """A System generates all the files for one implementation. | |
19 | |
20 This is a base class for all the specific systems. | |
21 The life-cycle of a System is: | |
22 - construction (__init__) | |
23 - (InterfaceGenerator | ProcessCallback)* # for each IDL interface | |
24 - GenerateLibraries | |
25 - Finish | |
26 """ | |
27 | |
28 def __init__(self, templates, database, emitters, output_dir): | |
29 self._templates = templates | |
30 self._database = database | |
31 self._emitters = emitters | |
32 self._output_dir = output_dir | |
33 self._dart_callback_file_paths = [] | |
34 | |
35 def InterfaceGenerator(self, | |
36 interface, | |
37 common_prefix, | |
38 super_interface_name, | |
39 source_filter): | |
40 """Returns an interface generator for |interface|. | |
41 | |
42 Called once for each interface that is not a callback function. | |
43 """ | |
44 return None | |
45 | |
46 def ProcessCallback(self, interface, info): | |
47 """Processes an interface that is a callback function.""" | |
48 pass | |
49 | |
50 def GenerateLibraries(self, lib_dir): | |
51 pass | |
52 | |
53 def Finish(self): | |
54 pass | |
55 | |
56 | |
57 # Helper methods used by several systems. | |
58 | |
59 def _ProcessCallback(self, interface, info, file_path): | |
60 """Generates a typedef for the callback interface.""" | |
61 self._dart_callback_file_paths.append(file_path) | |
62 code = self._emitters.FileEmitter(file_path) | |
63 | |
64 code.Emit(self._templates.Load('callback.darttemplate')) | |
65 code.Emit('typedef $TYPE $NAME($PARAMS);\n', | |
66 NAME=interface.id, | |
67 TYPE=info.type_name, | |
68 PARAMS=info.ParametersImplementationDeclaration()) | |
69 | |
70 | |
71 def _GenerateLibFile(self, lib_template, lib_file_path, file_paths, | |
72 **template_args): | |
73 """Generates a lib file from a template and a list of files. | |
74 | |
75 Additional keyword arguments are passed to the template. | |
76 Typically called from self.GenerateLibraries. | |
77 """ | |
78 # Load template. | |
79 template = self._templates.Load(lib_template) | |
80 # Generate the .lib file. | |
81 lib_file_contents = self._emitters.FileEmitter(lib_file_path) | |
82 | |
83 # Emit the list of #source directives. | |
84 list_emitter = lib_file_contents.Emit(template, **template_args) | |
85 lib_file_dir = os.path.dirname(lib_file_path) | |
86 for path in sorted(file_paths): | |
87 relpath = os.path.relpath(path, lib_file_dir) | |
88 list_emitter.Emit("#source('$PATH');\n", PATH=MassagePath(relpath)) | |
89 | |
90 | |
91 def _BaseDefines(self, interface): | |
92 """Returns a set of names (strings) for members defined in a base class. | |
93 """ | |
94 def WalkParentChain(interface): | |
95 if interface.parents: | |
96 # Only consider primary parent, secondary parents are not on the | |
97 # implementation class inheritance chain. | |
98 parent = interface.parents[0] | |
99 if generator.IsDartCollectionType(parent.type.id): | |
100 return | |
101 if self._database.HasInterface(parent.type.id): | |
102 parent_interface = self._database.GetInterface(parent.type.id) | |
103 for attr in parent_interface.attributes: | |
104 result.add(attr.id) | |
105 for op in parent_interface.operations: | |
106 result.add(op.id) | |
107 WalkParentChain(parent_interface) | |
108 | |
109 result = set() | |
110 WalkParentChain(interface) | |
111 return result; | |
OLD | NEW |