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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 if token.value == "float.NAN": | 274 if token.value == "float.NAN": |
275 return "java.lang.Float.NaN" | 275 return "java.lang.Float.NaN" |
276 return token | 276 return token |
277 | 277 |
278 def IsPointerArrayKind(kind): | 278 def IsPointerArrayKind(kind): |
279 if not mojom.IsAnyArrayKind(kind): | 279 if not mojom.IsAnyArrayKind(kind): |
280 return False | 280 return False |
281 sub_kind = kind.kind | 281 sub_kind = kind.kind |
282 return mojom.IsObjectKind(sub_kind) | 282 return mojom.IsObjectKind(sub_kind) |
283 | 283 |
| 284 def GetResponseStructFromMethod(method): |
| 285 return generator.GetDataHeader( |
| 286 False, generator.GetResponseStructFromMethod(method)) |
| 287 |
| 288 def GetStructFromMethod(method): |
| 289 return generator.GetDataHeader( |
| 290 False, generator.GetStructFromMethod(method)) |
| 291 |
284 def GetConstantsMainEntityName(module): | 292 def GetConstantsMainEntityName(module): |
285 if 'JavaConstantsClassName' in module.attributes: | 293 if 'JavaConstantsClassName' in module.attributes: |
286 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) | 294 return ParseStringAttribute(module.attributes['JavaConstantsClassName']) |
287 # This constructs the name of the embedding classes for module level constants | 295 # This constructs the name of the embedding classes for module level constants |
288 # by extracting the mojom's filename and prepending it to Constants. | 296 # by extracting the mojom's filename and prepending it to Constants. |
289 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + | 297 return (UpperCamelCase(module.path.split('/')[-1].rsplit('.', 1)[0]) + |
290 'Constants') | 298 'Constants') |
291 | 299 |
| 300 def GetMethodOrdinalName(method): |
| 301 return ConstantStyle(method.name) + "_ORDINAL" |
| 302 |
| 303 def HasMethodWithResponse(interface): |
| 304 for method in interface.methods: |
| 305 if method.response_parameters: |
| 306 return True |
| 307 return False |
| 308 |
| 309 def HasMethodWithoutResponse(interface): |
| 310 for method in interface.methods: |
| 311 if not method.response_parameters: |
| 312 return True |
| 313 return False |
| 314 |
292 class Generator(generator.Generator): | 315 class Generator(generator.Generator): |
293 | 316 |
294 java_filters = { | 317 java_filters = { |
295 "interface_response_name": GetInterfaceResponseName, | 318 "interface_response_name": GetInterfaceResponseName, |
296 "constant_value": ConstantValue, | 319 "constant_value": ConstantValue, |
297 "default_value": DefaultValue, | 320 "default_value": DefaultValue, |
298 "decode_method": DecodeMethod, | 321 "decode_method": DecodeMethod, |
299 "expression_to_text": ExpressionToText, | 322 "expression_to_text": ExpressionToText, |
300 "encode_method": EncodeMethod, | 323 "encode_method": EncodeMethod, |
| 324 "has_method_with_response": HasMethodWithResponse, |
| 325 "has_method_without_response": HasMethodWithoutResponse, |
301 "is_handle": mojom.IsNonInterfaceHandleKind, | 326 "is_handle": mojom.IsNonInterfaceHandleKind, |
302 "is_pointer_array_kind": IsPointerArrayKind, | 327 "is_pointer_array_kind": IsPointerArrayKind, |
303 "is_struct_kind": mojom.IsStructKind, | 328 "is_struct_kind": mojom.IsStructKind, |
304 "java_type": GetJavaType, | 329 "java_type": GetJavaType, |
| 330 "method_ordinal_name": GetMethodOrdinalName, |
305 "name": GetNameForElement, | 331 "name": GetNameForElement, |
306 "new_array": NewArray, | 332 "new_array": NewArray, |
| 333 "response_struct_from_method": GetResponseStructFromMethod, |
| 334 "struct_from_method": GetStructFromMethod, |
307 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 335 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
308 } | 336 } |
309 | 337 |
310 def GetJinjaExports(self): | 338 def GetJinjaExports(self): |
311 return { | 339 return { |
312 "module": self.module, | 340 "module": self.module, |
313 "package": GetPackage(self.module), | 341 "package": GetPackage(self.module), |
314 } | 342 } |
315 | 343 |
| 344 def GetJinjaExportsForInterface(self, interface): |
| 345 exports = self.GetJinjaExports() |
| 346 exports.update({"interface": interface}) |
| 347 if interface.client: |
| 348 for client in self.module.interfaces: |
| 349 if client.name == interface.client: |
| 350 exports.update({"client": client}) |
| 351 return exports |
| 352 |
316 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) | 353 @UseJinja("java_templates/enum.java.tmpl", filters=java_filters) |
317 def GenerateEnumSource(self, enum): | 354 def GenerateEnumSource(self, enum): |
318 exports = self.GetJinjaExports() | 355 exports = self.GetJinjaExports() |
319 exports.update({"enum": enum}) | 356 exports.update({"enum": enum}) |
320 return exports | 357 return exports |
321 | 358 |
322 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) | 359 @UseJinja("java_templates/struct.java.tmpl", filters=java_filters) |
323 def GenerateStructSource(self, struct): | 360 def GenerateStructSource(self, struct): |
324 exports = self.GetJinjaExports() | 361 exports = self.GetJinjaExports() |
325 exports.update({"struct": struct}) | 362 exports.update({"struct": struct}) |
326 return exports | 363 return exports |
327 | 364 |
328 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) | 365 @UseJinja("java_templates/interface.java.tmpl", filters=java_filters) |
329 def GenerateInterfaceSource(self, interface): | 366 def GenerateInterfaceSource(self, interface): |
330 exports = self.GetJinjaExports() | 367 return self.GetJinjaExportsForInterface(interface) |
331 exports.update({"interface": interface}) | 368 |
332 if interface.client: | 369 @UseJinja("java_templates/interface_internal.java.tmpl", filters=java_filters) |
333 for client in self.module.interfaces: | 370 def GenerateInterfaceInternalSource(self, interface): |
334 if client.name == interface.client: | 371 return self.GetJinjaExportsForInterface(interface) |
335 exports.update({"client": client}) | |
336 return exports | |
337 | 372 |
338 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) | 373 @UseJinja("java_templates/constants.java.tmpl", filters=java_filters) |
339 def GenerateConstantsSource(self, module): | 374 def GenerateConstantsSource(self, module): |
340 exports = self.GetJinjaExports() | 375 exports = self.GetJinjaExports() |
341 exports.update({"main_entity": GetConstantsMainEntityName(module), | 376 exports.update({"main_entity": GetConstantsMainEntityName(module), |
342 "constants": module.constants}) | 377 "constants": module.constants}) |
343 return exports | 378 return exports |
344 | 379 |
345 def GenerateFiles(self, unparsed_args): | 380 def GenerateFiles(self, unparsed_args): |
346 parser = argparse.ArgumentParser() | 381 parser = argparse.ArgumentParser() |
(...skipping 13 matching lines...) Expand all Loading... |
360 self.Write(self.GenerateEnumSource(enum), | 395 self.Write(self.GenerateEnumSource(enum), |
361 "%s.java" % GetNameForElement(enum)) | 396 "%s.java" % GetNameForElement(enum)) |
362 | 397 |
363 for struct in self.module.structs: | 398 for struct in self.module.structs: |
364 self.Write(self.GenerateStructSource(struct), | 399 self.Write(self.GenerateStructSource(struct), |
365 "%s.java" % GetNameForElement(struct)) | 400 "%s.java" % GetNameForElement(struct)) |
366 | 401 |
367 for interface in self.module.interfaces: | 402 for interface in self.module.interfaces: |
368 self.Write(self.GenerateInterfaceSource(interface), | 403 self.Write(self.GenerateInterfaceSource(interface), |
369 "%s.java" % GetNameForElement(interface)) | 404 "%s.java" % GetNameForElement(interface)) |
| 405 self.Write(self.GenerateInterfaceInternalSource(interface), |
| 406 "%s_Internal.java" % GetNameForElement(interface)) |
370 | 407 |
371 if self.module.constants: | 408 if self.module.constants: |
372 self.Write(self.GenerateConstantsSource(self.module), | 409 self.Write(self.GenerateConstantsSource(self.module), |
373 "%s.java" % GetConstantsMainEntityName(self.module)) | 410 "%s.java" % GetConstantsMainEntityName(self.module)) |
374 | 411 |
375 def GetJinjaParameters(self): | 412 def GetJinjaParameters(self): |
376 return { | 413 return { |
377 'lstrip_blocks': True, | 414 'lstrip_blocks': True, |
378 'trim_blocks': True, | 415 'trim_blocks': True, |
379 } | 416 } |
380 | 417 |
381 def GetGlobals(self): | 418 def GetGlobals(self): |
382 return { | 419 return { |
383 'module': self.module, | 420 'module': self.module, |
384 } | 421 } |
OLD | NEW |