| 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.MANAGER' % GetJavaType(context, kind) | 156 additionalParams = ', %s.BUILDER' % 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.MANAGER' % GetJavaType(context, kind.kind) | 158 additionalParams = ', %s.BUILDER' % 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.MANAGER' % GetJavaType(context, kind) | 167 additionalParams = ', %s.BUILDER' % 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.MANAGER' % GetJavaType(context, kind.kind) | 169 additionalParams = ', %s.BUILDER' % 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 | |
| 279 def GetConstantsMainEntityName(module): | 271 def GetConstantsMainEntityName(module): |
| 280 if 'JavaConstantsClassName' in module.attributes: | 272 if 'JavaConstantsClassName' in module.attributes: |
| 281 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 273 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
| 282 # This constructs the name of the embedding classes for module level constants | 274 # This constructs the name of the embedding classes for module level constants |
| 283 # by extracting the mojom's filename and prepending it to Constants. | 275 # by extracting the mojom's filename and prepending it to Constants. |
| 284 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 276 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
| 285 'Constants') | 277 'Constants') |
| 286 | 278 |
| 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 | |
| 302 class Generator(generator.Generator): | 279 class Generator(generator.Generator): |
| 303 | 280 |
| 304 java_filters = { | 281 java_filters = { |
| 305 "interface_response_name": GetInterfaceResponseName, | 282 "interface_response_name": GetInterfaceResponseName, |
| 306 "constant_value": ConstantValue, | 283 "constant_value": ConstantValue, |
| 307 "default_value": DefaultValue, | 284 "default_value": DefaultValue, |
| 308 "decode_method": DecodeMethod, | 285 "decode_method": DecodeMethod, |
| 309 "expression_to_text": ExpressionToText, | 286 "expression_to_text": ExpressionToText, |
| 310 "encode_method": EncodeMethod, | 287 "encode_method": EncodeMethod, |
| 311 "has_method_with_response": HasMethodWithResponse, | |
| 312 "has_method_without_response": HasMethodWithoutResponse, | |
| 313 "is_handle": mojom.IsNonInterfaceHandleKind, | 288 "is_handle": mojom.IsNonInterfaceHandleKind, |
| 314 "is_pointer_array_kind": IsPointerArrayKind, | 289 "is_pointer_array_kind": IsPointerArrayKind, |
| 315 "is_struct_kind": mojom.IsStructKind, | 290 "is_struct_kind": mojom.IsStructKind, |
| 316 "java_type": GetJavaType, | 291 "java_type": GetJavaType, |
| 317 "method_ordinal_name": GetMethodOrdinalName, | |
| 318 "name": GetNameForElement, | 292 "name": GetNameForElement, |
| 319 "new_array": NewArray, | 293 "new_array": NewArray, |
| 320 "response_struct_from_method": GetResponseStructFromMethod, | |
| 321 "struct_from_method": GetStructFromMethod, | |
| 322 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 294 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
| 323 } | 295 } |
| 324 | 296 |
| 325 def GetJinjaExports(self): | 297 def GetJinjaExports(self): |
| 326 return { | 298 return { |
| 327 "module": self.module, | 299 "module": self.module, |
| 328 "package": GetPackage(self.module), | 300 "package": GetPackage(self.module), |
| 329 } | 301 } |
| 330 | 302 |
| 331 def GetJinjaExportsForInterface(self, interface): | 303 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) |
| 304 def GenerateEnumSource(self, enum): |
| 305 exports = self.GetJinjaExports() |
| 306 exports.update({"enum": enum}) |
| 307 return exports |
| 308 |
| 309 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) |
| 310 def GenerateStructSource(self, struct): |
| 311 exports = self.GetJinjaExports() |
| 312 exports.update({"struct": struct}) |
| 313 return exports |
| 314 |
| 315 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) |
| 316 def GenerateInterfaceSource(self, interface): |
| 332 exports = self.GetJinjaExports() | 317 exports = self.GetJinjaExports() |
| 333 exports.update({"interface": interface}) | 318 exports.update({"interface": interface}) |
| 334 if interface.client: | 319 if interface.client: |
| 335 for client in self.module.interfaces: | 320 for client in self.module.interfaces: |
| 336 if client.name == interface.client: | 321 if client.name == interface.client: |
| 337 exports.update({"client": client}) | 322 exports.update({"client": client}) |
| 338 return exports | 323 return exports |
| 339 | 324 |
| 340 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) | |
| 341 def GenerateEnumSource(self, enum): | |
| 342 exports = self.GetJinjaExports() | |
| 343 exports.update({"enum": enum}) | |
| 344 return exports | |
| 345 | |
| 346 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) | |
| 347 def GenerateStructSource(self, struct): | |
| 348 exports = self.GetJinjaExports() | |
| 349 exports.update({"struct": struct}) | |
| 350 return exports | |
| 351 | |
| 352 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) | |
| 353 def GenerateInterfaceSource(self, interface): | |
| 354 return self.GetJinjaExportsForInterface(interface) | |
| 355 | |
| 356 @UseJinja("java_templates/interface_internal.java.tmpl", filters=java_filters) | |
| 357 def GenerateInterfaceInternalSource(self, interface): | |
| 358 return self.GetJinjaExportsForInterface(interface) | |
| 359 | |
| 360 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) | 325 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) |
| 361 def GenerateConstantsSource(self, module): | 326 def GenerateConstantsSource(self, module): |
| 362 exports = self.GetJinjaExports() | 327 exports = self.GetJinjaExports() |
| 363 exports.update({"main_entity": GetConstantsMainEntityName(module), | 328 exports.update({"main_entity": GetConstantsMainEntityName(module), |
| 364 "constants": module.constants}) | 329 "constants": module.constants}) |
| 365 return exports | 330 return exports |
| 366 | 331 |
| 367 def GenerateFiles(self, unparsed_args): | 332 def GenerateFiles(self, unparsed_args): |
| 368 parser = argparse.ArgumentParser() | 333 parser = argparse.ArgumentParser() |
| 369 parser.add_argument("--java_output_directory", dest="java_output_directory") | 334 parser.add_argument("--java_output_directory", dest="java_output_directory") |
| (...skipping 12 matching lines...) Expand all Loading... |
| 382 self.Write(self.GenerateEnumSource(enum), | 347 self.Write(self.GenerateEnumSource(enum), |
| 383 "%s.java" % GetNameForElement(enum)) | 348 "%s.java" % GetNameForElement(enum)) |
| 384 | 349 |
| 385 for struct in self.module.structs: | 350 for struct in self.module.structs: |
| 386 self.Write(self.GenerateStructSource(struct), | 351 self.Write(self.GenerateStructSource(struct), |
| 387 "%s.java" % GetNameForElement(struct)) | 352 "%s.java" % GetNameForElement(struct)) |
| 388 | 353 |
| 389 for interface in self.module.interfaces: | 354 for interface in self.module.interfaces: |
| 390 self.Write(self.GenerateInterfaceSource(interface), | 355 self.Write(self.GenerateInterfaceSource(interface), |
| 391 "%s.java" % GetNameForElement(interface)) | 356 "%s.java" % GetNameForElement(interface)) |
| 392 self.Write(self.GenerateInterfaceInternalSource(interface), | |
| 393 "%s_Internal.java" % GetNameForElement(interface)) | |
| 394 | 357 |
| 395 if self.module.constants: | 358 if self.module.constants: |
| 396 self.Write(self.GenerateConstantsSource(self.module), | 359 self.Write(self.GenerateConstantsSource(self.module), |
| 397 "%s.java" % GetConstantsMainEntityName(self.module)) | 360 "%s.java" % GetConstantsMainEntityName(self.module)) |
| 398 | 361 |
| 399 def GetJinjaParameters(self): | 362 def GetJinjaParameters(self): |
| 400 return { | 363 return { |
| 401 'lstrip_blocks': True, | 364 'lstrip_blocks': True, |
| 402 'trim_blocks': True, | 365 'trim_blocks': True, |
| 403 } | 366 } |
| 404 | 367 |
| 405 def GetGlobals(self): | 368 def GetGlobals(self): |
| 406 return { | 369 return { |
| 407 'module': self.module, | 370 'module': self.module, |
| 408 } | 371 } |
| OLD | NEW |