OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generates java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
6 | 6 |
7 import argparse | 7 import argparse |
8 import ast | 8 import ast |
9 import os | 9 import os |
10 import re | 10 import re |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 if mojom.IsInterfaceRequestKind(kind): | 146 if mojom.IsInterfaceRequestKind(kind): |
147 return "readInterfaceRequest" | 147 return "readInterfaceRequest" |
148 if mojom.IsInterfaceKind(kind): | 148 if mojom.IsInterfaceKind(kind): |
149 return "readServiceInterface" | 149 return "readServiceInterface" |
150 return _spec_to_decode_method[kind.spec] | 150 return _spec_to_decode_method[kind.spec] |
151 methodName = _DecodeMethodName(kind) | 151 methodName = _DecodeMethodName(kind) |
152 additionalParams = '' | 152 additionalParams = '' |
153 if (kind == mojom.BOOL): | 153 if (kind == mojom.BOOL): |
154 additionalParams = ', %d' % bit | 154 additionalParams = ', %d' % bit |
155 if mojom.IsInterfaceKind(kind): | 155 if mojom.IsInterfaceKind(kind): |
156 additionalParams = ', %s.BUILDER' % GetJavaType(context, kind) | 156 additionalParams = ', %s.MANAGER' % GetJavaType(context, kind) |
157 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): | 157 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): |
158 additionalParams = ', %s.BUILDER' % GetJavaType(context, kind.kind) | 158 additionalParams = ', %s.MANAGER' % GetJavaType(context, kind.kind) |
159 return '%s(%s%s)' % (methodName, offset, additionalParams) | 159 return '%s(%s%s)' % (methodName, offset, additionalParams) |
160 | 160 |
161 @contextfilter | 161 @contextfilter |
162 def EncodeMethod(context, kind, variable, offset, bit): | 162 def EncodeMethod(context, kind, variable, offset, bit): |
163 additionalParams = '' | 163 additionalParams = '' |
164 if (kind == mojom.BOOL): | 164 if (kind == mojom.BOOL): |
165 additionalParams = ', %d' % bit | 165 additionalParams = ', %d' % bit |
166 if mojom.IsInterfaceKind(kind): | 166 if mojom.IsInterfaceKind(kind): |
167 additionalParams = ', %s.BUILDER' % GetJavaType(context, kind) | 167 additionalParams = ', %s.MANAGER' % GetJavaType(context, kind) |
168 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): | 168 if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): |
169 additionalParams = ', %s.BUILDER' % GetJavaType(context, kind.kind) | 169 additionalParams = ', %s.MANAGER' % GetJavaType(context, kind.kind) |
170 return 'encode(%s, %s%s)' % (variable, offset, additionalParams) | 170 return 'encode(%s, %s%s)' % (variable, offset, additionalParams) |
171 | 171 |
172 def GetPackage(module): | 172 def GetPackage(module): |
173 if 'JavaPackage' in module.attributes: | 173 if 'JavaPackage' in module.attributes: |
174 return ParseStringAttribute(module.attributes['JavaPackage']) | 174 return ParseStringAttribute(module.attributes['JavaPackage']) |
175 # Default package. | 175 # Default package. |
176 return "org.chromium.mojom." + module.namespace | 176 return "org.chromium.mojom." + module.namespace |
177 | 177 |
178 def GetNameForKind(context, kind): | 178 def GetNameForKind(context, kind): |
179 def _GetNameHierachy(kind): | 179 def _GetNameHierachy(kind): |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 number -= 2 ** 64 | 261 number -= 2 ** 64 |
262 return '%dL' % number | 262 return '%dL' % number |
263 return token | 263 return token |
264 | 264 |
265 def IsPointerArrayKind(kind): | 265 def IsPointerArrayKind(kind): |
266 if not mojom.IsAnyArrayKind(kind): | 266 if not mojom.IsAnyArrayKind(kind): |
267 return False | 267 return False |
268 sub_kind = kind.kind | 268 sub_kind = kind.kind |
269 return mojom.IsObjectKind(sub_kind) | 269 return mojom.IsObjectKind(sub_kind) |
270 | 270 |
| 271 def GetResponseStructFromMethod(method): |
| 272 return generator.GetDataHeader( |
| 273 False, generator.GetResponseStructFromMethod(method)) |
| 274 |
| 275 def GetStructFromMethod(method): |
| 276 return generator.GetDataHeader( |
| 277 False, generator.GetStructFromMethod(method)) |
| 278 |
271 def GetConstantsMainEntityName(module): | 279 def GetConstantsMainEntityName(module): |
272 if 'JavaConstantsClassName' in module.attributes: | 280 if 'JavaConstantsClassName' in module.attributes: |
273 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 281 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
274 # This constructs the name of the embedding classes for module level constants | 282 # This constructs the name of the embedding classes for module level constants |
275 # by extracting the mojom's filename and prepending it to Constants. | 283 # by extracting the mojom's filename and prepending it to Constants. |
276 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 284 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
277 'Constants') | 285 'Constants') |
278 | 286 |
| 287 def GetMethodOrdinalName(method): |
| 288 return ConstantStyle(method.name) + "_ORDINAL" |
| 289 |
| 290 def HasMethodWithResponse(interface): |
| 291 for method in interface.methods: |
| 292 if method.response_parameters: |
| 293 return True |
| 294 return False |
| 295 |
| 296 def HasMethodWithoutResponse(interface): |
| 297 for method in interface.methods: |
| 298 if not method.response_parameters: |
| 299 return True |
| 300 return False |
| 301 |
279 class Generator(generator.Generator): | 302 class Generator(generator.Generator): |
280 | 303 |
281 java_filters = { | 304 java_filters = { |
282 "interface_response_name": GetInterfaceResponseName, | 305 "interface_response_name": GetInterfaceResponseName, |
283 "constant_value": ConstantValue, | 306 "constant_value": ConstantValue, |
284 "default_value": DefaultValue, | 307 "default_value": DefaultValue, |
285 "decode_method": DecodeMethod, | 308 "decode_method": DecodeMethod, |
286 "expression_to_text": ExpressionToText, | 309 "expression_to_text": ExpressionToText, |
287 "encode_method": EncodeMethod, | 310 "encode_method": EncodeMethod, |
| 311 "has_method_with_response": HasMethodWithResponse, |
| 312 "has_method_without_response": HasMethodWithoutResponse, |
288 "is_handle": mojom.IsNonInterfaceHandleKind, | 313 "is_handle": mojom.IsNonInterfaceHandleKind, |
289 "is_pointer_array_kind": IsPointerArrayKind, | 314 "is_pointer_array_kind": IsPointerArrayKind, |
290 "is_struct_kind": mojom.IsStructKind, | 315 "is_struct_kind": mojom.IsStructKind, |
291 "java_type": GetJavaType, | 316 "java_type": GetJavaType, |
| 317 "method_ordinal_name": GetMethodOrdinalName, |
292 "name": GetNameForElement, | 318 "name": GetNameForElement, |
293 "new_array": NewArray, | 319 "new_array": NewArray, |
| 320 "response_struct_from_method": GetResponseStructFromMethod, |
| 321 "struct_from_method": GetStructFromMethod, |
294 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 322 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
295 } | 323 } |
296 | 324 |
297 def GetJinjaExports(self): | 325 def GetJinjaExports(self): |
298 return { | 326 return { |
299 "module": self.module, | 327 "module": self.module, |
300 "package": GetPackage(self.module), | 328 "package": GetPackage(self.module), |
301 } | 329 } |
302 | 330 |
| 331 def GetJinjaExportsForInterface(self, interface): |
| 332 exports = self.GetJinjaExports() |
| 333 exports.update({"interface": interface}) |
| 334 if interface.client: |
| 335 for client in self.module.interfaces: |
| 336 if client.name == interface.client: |
| 337 exports.update({"client": client}) |
| 338 return exports |
| 339 |
303 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) | 340 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) |
304 def GenerateEnumSource(self, enum): | 341 def GenerateEnumSource(self, enum): |
305 exports = self.GetJinjaExports() | 342 exports = self.GetJinjaExports() |
306 exports.update({"enum": enum}) | 343 exports.update({"enum": enum}) |
307 return exports | 344 return exports |
308 | 345 |
309 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) | 346 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) |
310 def GenerateStructSource(self, struct): | 347 def GenerateStructSource(self, struct): |
311 exports = self.GetJinjaExports() | 348 exports = self.GetJinjaExports() |
312 exports.update({"struct": struct}) | 349 exports.update({"struct": struct}) |
313 return exports | 350 return exports |
314 | 351 |
315 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) | 352 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) |
316 def GenerateInterfaceSource(self, interface): | 353 def GenerateInterfaceSource(self, interface): |
317 exports = self.GetJinjaExports() | 354 return self.GetJinjaExportsForInterface(interface) |
318 exports.update({"interface": interface}) | 355 |
319 if interface.client: | 356 @UseJinja("java_templates/interface_internal.java.tmpl", filters=java_filters) |
320 for client in self.module.interfaces: | 357 def GenerateInterfaceInternalSource(self, interface): |
321 if client.name == interface.client: | 358 return self.GetJinjaExportsForInterface(interface) |
322 exports.update({"client": client}) | |
323 return exports | |
324 | 359 |
325 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) | 360 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) |
326 def GenerateConstantsSource(self, module): | 361 def GenerateConstantsSource(self, module): |
327 exports = self.GetJinjaExports() | 362 exports = self.GetJinjaExports() |
328 exports.update({"main_entity": GetConstantsMainEntityName(module), | 363 exports.update({"main_entity": GetConstantsMainEntityName(module), |
329 "constants": module.constants}) | 364 "constants": module.constants}) |
330 return exports | 365 return exports |
331 | 366 |
332 def GenerateFiles(self, unparsed_args): | 367 def GenerateFiles(self, unparsed_args): |
333 parser = argparse.ArgumentParser() | 368 parser = argparse.ArgumentParser() |
(...skipping 13 matching lines...) Expand all Loading... |
347 self.Write(self.GenerateEnumSource(enum), | 382 self.Write(self.GenerateEnumSource(enum), |
348 "%s.java" % GetNameForElement(enum)) | 383 "%s.java" % GetNameForElement(enum)) |
349 | 384 |
350 for struct in self.module.structs: | 385 for struct in self.module.structs: |
351 self.Write(self.GenerateStructSource(struct), | 386 self.Write(self.GenerateStructSource(struct), |
352 "%s.java" % GetNameForElement(struct)) | 387 "%s.java" % GetNameForElement(struct)) |
353 | 388 |
354 for interface in self.module.interfaces: | 389 for interface in self.module.interfaces: |
355 self.Write(self.GenerateInterfaceSource(interface), | 390 self.Write(self.GenerateInterfaceSource(interface), |
356 "%s.java" % GetNameForElement(interface)) | 391 "%s.java" % GetNameForElement(interface)) |
| 392 self.Write(self.GenerateInterfaceInternalSource(interface), |
| 393 "%sInternal.java" % GetNameForElement(interface)) |
357 | 394 |
358 if self.module.constants: | 395 if self.module.constants: |
359 self.Write(self.GenerateConstantsSource(self.module), | 396 self.Write(self.GenerateConstantsSource(self.module), |
360 "%s.java" % GetConstantsMainEntityName(self.module)) | 397 "%s.java" % GetConstantsMainEntityName(self.module)) |
361 | 398 |
362 def GetJinjaParameters(self): | 399 def GetJinjaParameters(self): |
363 return { | 400 return { |
364 'lstrip_blocks': True, | 401 'lstrip_blocks': True, |
365 'trim_blocks': True, | 402 'trim_blocks': True, |
366 } | 403 } |
367 | 404 |
368 def GetGlobals(self): | 405 def GetGlobals(self): |
369 return { | 406 return { |
370 'module': self.module, | 407 'module': self.module, |
371 } | 408 } |
OLD | NEW |