Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: client/dom/scripts/dartgenerator.py

Issue 9316096: Draft implementation of c++ bindings generation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 4 # BSD-style license that can be found in the LICENSE file.
5 5
6 """This module generates Dart APIs from the IDL database.""" 6 """This module generates Dart APIs from the IDL database."""
7 7
8 import emitter 8 import emitter
9 import idlnode 9 import idlnode
10 import logging 10 import logging
(...skipping 2088 matching lines...) Expand 10 before | Expand all | Expand 10 after
2099 2099
2100 class NativeImplementationSystem(System): 2100 class NativeImplementationSystem(System):
2101 2101
2102 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): 2102 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir):
2103 super(NativeImplementationSystem, self).__init__( 2103 super(NativeImplementationSystem, self).__init__(
2104 templates, database, emitters, output_dir) 2104 templates, database, emitters, output_dir)
2105 2105
2106 self._auxiliary_dir = auxiliary_dir 2106 self._auxiliary_dir = auxiliary_dir
2107 self._dom_public_files = [] 2107 self._dom_public_files = []
2108 self._dom_impl_files = [] 2108 self._dom_impl_files = []
2109 self._cpp_header_files = []
2110 self._cpp_impl_files = []
2109 2111
2110 def InterfaceGenerator(self, 2112 def InterfaceGenerator(self,
2111 interface, 2113 interface,
2112 common_prefix, 2114 common_prefix,
2113 super_interface_name, 2115 super_interface_name,
2114 source_filter): 2116 source_filter):
2115 interface_name = interface.id 2117 interface_name = interface.id
2116 2118
2117 dart_interface_path = self._FilePathForDartInterface(interface_name) 2119 dart_interface_path = self._FilePathForDartInterface(interface_name)
2118 self._dom_public_files.append(dart_interface_path) 2120 self._dom_public_files.append(dart_interface_path)
2119 2121
2120 dart_impl_path = self._FilePathForDartImplementation(interface_name) 2122 dart_impl_path = self._FilePathForDartImplementation(interface_name)
2121 self._dom_impl_files.append(dart_impl_path) 2123 self._dom_impl_files.append(dart_impl_path)
2122 2124
2125 cpp_header_path = self._FilePathForCppHeader(interface_name)
2126 self._cpp_header_files.append(cpp_header_path)
2127
2128 cpp_impl_path = self._FilePathForCppImplementation(interface_name)
2129 self._cpp_impl_files.append(cpp_impl_path)
2130
2123 return NativeImplementationGenerator(interface, super_interface_name, 2131 return NativeImplementationGenerator(interface, super_interface_name,
2124 self._emitters.FileEmitter(dart_impl_path), 2132 self._emitters.FileEmitter(dart_impl_path),
2133 self._emitters.FileEmitter(cpp_header_path),
2134 self._emitters.FileEmitter(cpp_impl_path),
2125 self._BaseDefines(interface), 2135 self._BaseDefines(interface),
2126 self._templates) 2136 self._templates)
2127 2137
2128 def ProcessCallback(self, interface, info): 2138 def ProcessCallback(self, interface, info):
2129 self._dom_public_files.append(self._FilePathForDartInterface(interface.id)) 2139 self._dom_public_files.append(self._FilePathForDartInterface(interface.id))
2130 2140
2131 def GenerateLibraries(self, lib_dir): 2141 def GenerateLibraries(self, lib_dir):
2132 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) 2142 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir)
2133 2143
2134 # Generate dom_public.dart. 2144 # Generate dom_public.dart.
2135 dom_public_path = os.path.join(self._output_dir, 'dom_public.dart') 2145 dom_public_path = os.path.join(self._output_dir, 'dom_public.dart')
2136 2146
2137 dom_public_imports_emitter = emitter.Emitter() 2147 dom_public_imports_emitter = emitter.Emitter()
2138 for file in self._dom_public_files: 2148 for file in self._dom_public_files:
2139 path = os.path.relpath(file, os.path.dirname(dom_public_path)) 2149 path = os.path.relpath(file, os.path.dirname(dom_public_path))
2140 dom_public_imports_emitter.Emit('#source("$PATH");\n', PATH=path) 2150 dom_public_imports_emitter.Emit('#source("$PATH");\n', PATH=path)
2141 2151
2142 dom_public_emitter = self._emitters.FileEmitter(dom_public_path) 2152 dom_public_emitter = self._emitters.FileEmitter(dom_public_path)
2143 dom_public_emitter.Emit(self._templates.Load('dom_public.darttemplate'), 2153 dom_public_emitter.Emit(self._templates.Load('dom_public.darttemplate'),
2144 AUXILIARY_DIR=auxiliary_dir, 2154 AUXILIARY_DIR=auxiliary_dir,
2145 SOURCES=dom_public_imports_emitter.Fragments()) 2155 SOURCES=dom_public_imports_emitter.Fragments())
2146 2156
2147 # Generate dom_impl.dart. 2157 # Generate dom_impl.dart.
2148 dom_impl_path = os.path.join(self._output_dir, 'dom_impl.dart') 2158 dom_impl_path = os.path.join(self._output_dir, 'dom_impl.dart')
2149 2159
2150 dom_impl_imports_emitter = emitter.Emitter() 2160 dom_impl_imports_emitter = emitter.Emitter()
2151 for file in self._dom_impl_files: 2161 for f in self._dom_impl_files:
2152 path = os.path.relpath(file, os.path.dirname(dom_impl_path)) 2162 path = os.path.relpath(f, os.path.dirname(dom_impl_path))
2153 dom_impl_imports_emitter.Emit('#source("$PATH");\n', PATH=path) 2163 dom_impl_imports_emitter.Emit('#source("$PATH");\n', PATH=path)
2154 2164
2155 dom_impl_emitter = self._emitters.FileEmitter(dom_impl_path) 2165 dom_impl_emitter = self._emitters.FileEmitter(dom_impl_path)
2156 dom_impl_emitter.Emit(self._templates.Load('dom_impl.darttemplate'), 2166 dom_impl_emitter.Emit(self._templates.Load('dom_impl.darttemplate'),
2157 AUXILIARY_DIR=auxiliary_dir, 2167 AUXILIARY_DIR=auxiliary_dir,
2158 SOURCES=dom_impl_imports_emitter.Fragments()) 2168 SOURCES=dom_impl_imports_emitter.Fragments())
2159 2169
2170 # Generate DartDerivedSourcesAll.cpp.
2171 cpp_all_in_one_path = os.path.join(self._output_dir,
2172 'DartDerivedSourcesAll.cpp')
2173
2174 includes_emitter = emitter.Emitter()
2175 for f in self._cpp_impl_files:
2176 path = os.path.relpath(f, os.path.dirname(cpp_all_in_one_path))
2177 includes_emitter.Emit('#include "$PATH"\n', PATH=path)
2178
2179 cpp_all_in_one_emitter = self._emitters.FileEmitter(cpp_all_in_one_path)
2180 cpp_all_in_one_emitter.Emit(
2181 self._templates.Load('cpp_all_in_one.template'),
2182 INCLUDES=includes_emitter.Fragments())
2183
2184 # Generate DartResolver.cpp.
2185 cpp_resolver_path = os.path.join(self._output_dir, 'DartResolver.cpp')
2186
2187 includes_emitter = emitter.Emitter()
2188 resolver_body_emitter = emitter.Emitter()
2189 for f in self._cpp_header_files:
2190 path = os.path.relpath(f, os.path.dirname(cpp_resolver_path))
2191 includes_emitter.Emit('#include "$PATH"\n', PATH=path)
2192 resolver_body_emitter.Emit(
2193 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argume ntCount))\n'
2194 ' return func;\n',
2195 CLASS_NAME=os.path.splitext(os.path.basename(path))[0])
antonm 2012/02/06 13:45:51 up to you, but I find those basename/splitext game
podivilov 2012/02/10 15:59:37 not really...
2196
2197 cpp_resolver_emitter = self._emitters.FileEmitter(cpp_resolver_path)
2198 cpp_resolver_emitter.Emit(
2199 self._templates.Load('cpp_resolver.template'),
2200 INCLUDES=includes_emitter.Fragments(),
2201 RESOLVER_BODY=resolver_body_emitter.Fragments())
2202
2160 def Finish(self): 2203 def Finish(self):
2161 pass 2204 pass
2162 2205
2163 def _FilePathForDartInterface(self, interface_name): 2206 def _FilePathForDartInterface(self, interface_name):
2164 return os.path.join(self._output_dir, 'src', 'interface', 2207 return os.path.join(self._output_dir, 'src', 'interface',
2165 '%s.dart' % interface_name) 2208 '%s.dart' % interface_name)
2166 2209
2167 def _FilePathForDartImplementation(self, interface_name): 2210 def _FilePathForDartImplementation(self, interface_name):
2168 return os.path.join(self._output_dir, 'dart', 2211 return os.path.join(self._output_dir, 'dart',
2169 '%sImplementation.dart' % interface_name) 2212 '%sImplementation.dart' % interface_name)
2170 2213
2214 def _FilePathForCppHeader(self, interface_name):
2215 return os.path.join(self._output_dir, 'cpp', 'Dart%s.h' % interface_name)
2216
2217 def _FilePathForCppImplementation(self, interface_name):
2218 return os.path.join(self._output_dir, 'cpp', 'Dart%s.cpp' % interface_name)
2219
2171 2220
2172 class NativeImplementationGenerator(WrappingInterfaceGenerator): 2221 class NativeImplementationGenerator(WrappingInterfaceGenerator):
2173 """Generates Dart implementation for one DOM IDL interface.""" 2222 """Generates Dart implementation for one DOM IDL interface."""
2174 2223
2175 def __init__(self, interface, super_interface, dart_impl_emitter, 2224 def __init__(self, interface, super_interface,
2225 dart_impl_emitter, cpp_header_emitter, cpp_impl_emitter,
2176 base_members, templates): 2226 base_members, templates):
2177 """Generates Dart code for the given interface. 2227 """Generates Dart and c++ code for the given interface.
antonm 2012/02/06 13:45:51 nit: C++
podivilov 2012/02/10 15:59:37 Done.
2178 2228
2179 Args: 2229 Args:
2180 2230
2181 interface: an IDLInterface instance. It is assumed that all types have 2231 interface: an IDLInterface instance. It is assumed that all types have
2182 been converted to Dart types (e.g. int, String), unless they are in 2232 been converted to Dart types (e.g. int, String), unless they are in
2183 the same package as the interface. 2233 the same package as the interface.
2184 super_interface: A string or None, the name of the common interface that 2234 super_interface: A string or None, the name of the common interface that
2185 this interface implements, if any. 2235 this interface implements, if any.
2186 dart_impl_emitter: an Emitter for the file containing the Dart 2236 dart_impl_emitter: an Emitter for the file containing the Dart
2187 implementation class. 2237 implementation class.
2238 cpp_header_emitter: an Emitter for the file containing the c++ header.
antonm 2012/02/06 13:45:51 again (here and below), C++ imho is preferred.
podivilov 2012/02/10 15:59:37 Done.
2239 cpp_impl_emitter: an Emitter for the file containing the c++
2240 implementation.
2188 base_members: a set of names of members defined in a base class. This is 2241 base_members: a set of names of members defined in a base class. This is
2189 used to avoid static member 'overriding' in the generated Dart code. 2242 used to avoid static member 'overriding' in the generated Dart code.
2190 """ 2243 """
2191 self._interface = interface 2244 self._interface = interface
2192 self._super_interface = super_interface 2245 self._super_interface = super_interface
2193 self._dart_impl_emitter = dart_impl_emitter 2246 self._dart_impl_emitter = dart_impl_emitter
2247 self._cpp_header_emitter = cpp_header_emitter
2248 self._cpp_impl_emitter = cpp_impl_emitter
2194 self._base_members = base_members 2249 self._base_members = base_members
2195 self._templates = templates 2250 self._templates = templates
2196 self._current_secondary_parent = None 2251 self._current_secondary_parent = None
2197 2252
2198 def StartInterface(self): 2253 def StartInterface(self):
2199 self._class_name = self._ImplClassName(self._interface.id) 2254 self._class_name = self._ImplClassName(self._interface.id)
2200 self._members_emitter = emitter.Emitter() 2255 self._members_emitter = emitter.Emitter()
2201 2256
2202 def _ImplClassName(self, type_name): 2257 def _ImplClassName(self, interface_name):
2203 return type_name + 'Implementation' 2258 return interface_name + 'Implementation'
2204 2259
2205 def FinishInterface(self): 2260 def FinishInterface(self):
2206 interface = self._interface 2261 base = self._BaseClassName(self._interface)
2207 interface_name = interface.id
2208
2209 base = self._BaseClassName(interface)
2210 self._dart_impl_emitter.Emit( 2262 self._dart_impl_emitter.Emit(
2211 self._templates.Load('dart_implementation.darttemplate'), 2263 self._templates.Load('dart_implementation.darttemplate'),
2212 CLASS=self._class_name, BASE=base, INTERFACE=interface_name, 2264 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id,
2213 MEMBERS=self._members_emitter.Fragments()) 2265 MEMBERS=self._members_emitter.Fragments())
2214 2266
2267 self._cpp_header_emitter.Emit(
2268 self._templates.Load('cpp_header.template'),
2269 INTERFACE=self._interface.id)
2270
2271 self._cpp_impl_emitter.Emit(
2272 self._templates.Load('cpp_implementation.template'),
2273 INTERFACE=self._interface.id)
2274
2215 def AddAttribute(self, getter, setter): 2275 def AddAttribute(self, getter, setter):
2216 if getter: 2276 if getter:
2217 self._AddGetter(getter) 2277 self._AddGetter(getter)
2218 if setter: 2278 if setter:
2219 self._AddSetter(setter) 2279 self._AddSetter(setter)
2220 2280
2221 def _AddGetter(self, attr): 2281 def _AddGetter(self, attr):
2222 self._members_emitter.Emit( 2282 self._members_emitter.Emit(
2223 '\n' 2283 '\n'
2224 ' $TYPE get $NAME() native "$(INTERFACE)_$(NAME)_Getter";\n', 2284 ' $TYPE get $NAME() native "$(INTERFACE)_$(NAME)_Getter";\n',
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2312 INDENT=indent, 2372 INDENT=indent,
2313 NATIVENAME=native_name, 2373 NATIVENAME=native_name,
2314 ARGS=argument_expressions) 2374 ARGS=argument_expressions)
2315 2375
2316 self._members_emitter.Emit(' $TYPE $NATIVE_NAME($PARAMS) native ' 2376 self._members_emitter.Emit(' $TYPE $NATIVE_NAME($PARAMS) native '
2317 '"$(INTERFACE)$(NATIVE_NAME)_Callback";\n', 2377 '"$(INTERFACE)$(NATIVE_NAME)_Callback";\n',
2318 NATIVE_NAME=native_name, 2378 NATIVE_NAME=native_name,
2319 TYPE=info.type_name, 2379 TYPE=info.type_name,
2320 PARAMS=', '.join(arg_names), 2380 PARAMS=', '.join(arg_names),
2321 INTERFACE=self._interface.id) 2381 INTERFACE=self._interface.id)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698