OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 Go source files from a mojom.Module.''' | 5 '''Generates Go source files from a mojom.Module.''' |
6 | 6 |
7 from itertools import chain | 7 from itertools import chain |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 path = GetPackagePath(imported['module']) | 300 path = GetPackagePath(imported['module']) |
301 if path in imports: | 301 if path in imports: |
302 return | 302 return |
303 name = GetPackageName(imported['module']) | 303 name = GetPackageName(imported['module']) |
304 while name in imports.values(): # This avoids repeated names. | 304 while name in imports.values(): # This avoids repeated names. |
305 name += '_' | 305 name += '_' |
306 imported['go_name'] = name | 306 imported['go_name'] = name |
307 imports[path] = name | 307 imports[path] = name |
308 mojom_imports[path] = name | 308 mojom_imports[path] = name |
309 | 309 |
310 # The identifier cache is used by the Type generator to determine if a type has | |
311 # already been generated or not. This prevents over-generation of the same type | |
312 # when it is referred to in multiple ways. | |
313 identifier_cache = {} | |
314 def GetIdentifier(kind): | 310 def GetIdentifier(kind): |
315 # Use the kind's module to determine the package name. | 311 """Use the kind's module to determine the package and name.""" |
| 312 # Note: InterfaceRequest's should use the Interface inside them. |
316 if hasattr(kind, 'module'): | 313 if hasattr(kind, 'module'): |
317 package = GetPackageName(kind.module) | 314 package = GetPackageName(kind.module) |
| 315 name = kind.name |
318 elif mojom.IsInterfaceRequestKind(kind): | 316 elif mojom.IsInterfaceRequestKind(kind): |
319 package = GetPackageName(kind.kind.module) | 317 package = GetPackageName(kind.kind.module) |
| 318 name = kind.kind.name |
320 else: | 319 else: |
321 return '' | 320 # These kinds (e.g., simple kinds, maps, and arrays) lack identifiers. |
| 321 raise Exception('Unexpected kind: %s' % kind) |
322 | 322 |
323 # Most kinds have a name, but those that don't should rely on their spec. | 323 return '%s_%s' % (package, name) |
324 # Since spec can have : and ? characters, these must be replaced. Since ? is | |
325 # replaced with '', the caller must keep track of optionality on its own. | |
326 name_or_spec = (kind.name if hasattr(kind, 'name') else kind.spec) | |
327 package_unique = name_or_spec.replace(':', '_').replace('?', '') | |
328 return '%s_%s' % (package, package_unique) | |
329 | |
330 def StoreIdentifier(identifier, cache_name): | |
331 if not cache_name in identifier_cache: | |
332 identifier_cache[cache_name] = {} | |
333 identifier_cache[cache_name][identifier] = True | |
334 return '' | |
335 | |
336 def CheckIdentifier(identifier, cache_name): | |
337 if not cache_name in identifier_cache: | |
338 identifier_cache[cache_name] = {} | |
339 return identifier in identifier_cache[cache_name] | |
340 | 324 |
341 # Get the mojom type's identifier suffix. | 325 # Get the mojom type's identifier suffix. |
342 def GetMojomTypeIdentifier(kind): | 326 def GetMojomTypeIdentifier(kind): |
343 # Since this should be unique, it is based on the type's identifier. | 327 # Since this should be unique, it is based on the type's identifier. |
344 return "%s__" % GetIdentifier(kind) | 328 return "%s__" % GetIdentifier(kind) |
345 | 329 |
346 class Generator(generator.Generator): | 330 class Generator(generator.Generator): |
347 go_filters = { | 331 go_filters = { |
348 'array': lambda kind: mojom.Array(kind), | 332 'array': lambda kind: mojom.Array(kind), |
349 'bit_size': GetBitSize, | 333 'bit_size': GetBitSize, |
350 'decode_suffix': DecodeSuffix, | 334 'decode_suffix': DecodeSuffix, |
351 'encode_suffix': EncodeSuffix, | 335 'encode_suffix': EncodeSuffix, |
352 'go_type': GetGoType, | 336 'go_type': GetGoType, |
353 'expression_to_text': ExpressionToText, | 337 'expression_to_text': ExpressionToText, |
354 'has_response': lambda method: method.response_parameters is not None, | 338 'has_response': lambda method: method.response_parameters is not None, |
355 'identifier': GetIdentifier, | 339 'identifier': GetIdentifier, |
356 'identifier_check': CheckIdentifier, | |
357 'identifier_store': StoreIdentifier, | |
358 'is_array': mojom.IsArrayKind, | 340 'is_array': mojom.IsArrayKind, |
359 'is_enum': mojom.IsEnumKind, | 341 'is_enum': mojom.IsEnumKind, |
360 'is_handle': mojom.IsAnyHandleKind, | 342 'is_handle': mojom.IsAnyHandleKind, |
361 'is_interface': mojom.IsInterfaceKind, | 343 'is_interface': mojom.IsInterfaceKind, |
362 'is_interface_request': mojom.IsInterfaceRequestKind, | 344 'is_interface_request': mojom.IsInterfaceRequestKind, |
363 'is_map': mojom.IsMapKind, | 345 'is_map': mojom.IsMapKind, |
364 'is_none_or_empty': lambda array: array is None or len(array) == 0, | 346 'is_none_or_empty': lambda array: array is None or len(array) == 0, |
365 'is_nullable': mojom.IsNullableKind, | 347 'is_nullable': mojom.IsNullableKind, |
366 'is_pointer': IsPointer, | 348 'is_pointer': IsPointer, |
367 'is_object': mojom.IsObjectKind, | 349 'is_object': mojom.IsObjectKind, |
368 'is_struct': mojom.IsStructKind, | 350 'is_struct': mojom.IsStructKind, |
369 'is_union': mojom.IsUnionKind, | 351 'is_union': mojom.IsUnionKind, |
370 'qualified': GetQualifiedName, | 352 'qualified': GetQualifiedName, |
371 'fullidentifier': GetFullIdentifier, | 353 'fullidentifier': GetFullIdentifier, |
372 'mojom_type': GetMojomTypeValue, | 354 'mojom_type': GetMojomTypeValue, |
373 'mojom_type_identifier': GetMojomTypeIdentifier, | 355 'mojom_type_identifier': GetMojomTypeIdentifier, |
374 'name': GetNameForElement, | 356 'name': GetNameForElement, |
375 'unqualified_name': GetUnqualifiedNameForElement, | 357 'unqualified_name': GetUnqualifiedNameForElement, |
376 'package': GetPackageNameForElement, | 358 'package': GetPackageNameForElement, |
377 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) | 359 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
378 } | 360 } |
379 | 361 |
380 # TODO: This value should be settable via arguments. If False, then mojom type | 362 # If set to True, then mojom type information will be generated. |
381 # information will not be generated. | 363 should_gen_mojom_types = False |
382 should_gen_mojom_types = True | |
383 | 364 |
384 def GetParameters(self): | 365 def GetParameters(self): |
385 package = GetPackageName(self.module) | 366 package = GetPackageName(self.module) |
386 imports, mojom_imports = self.GetImports() | 367 imports, mojom_imports = self.GetImports() |
387 return { | 368 return { |
388 'enums': GetAllEnums(self.module), | 369 'enums': GetAllEnums(self.module), |
389 'imports': imports, | 370 'imports': imports, |
390 'interfaces': self.GetInterfaces(), | 371 'interfaces': self.GetInterfaces(), |
391 'mojom_imports': mojom_imports, | 372 'mojom_imports': mojom_imports, |
392 'package': package, | 373 'package': package, |
393 'structs': self.GetStructs(), | 374 'structs': self.GetStructs(), |
394 'descpkg': '%s.' % _service_describer_pkg_short \ | 375 'descpkg': '%s.' % _service_describer_pkg_short \ |
395 if package != _service_describer_pkg_short else '', | 376 if package != _service_describer_pkg_short else '', |
396 'typepkg': '%s.' % _mojom_types_pkg_short \ | 377 'typepkg': '%s.' % _mojom_types_pkg_short \ |
397 if package != _mojom_types_pkg_short else '', | 378 if package != _mojom_types_pkg_short else '', |
398 'unions': self.GetUnions() | 379 'unions': self.GetUnions() |
399 } | 380 } |
400 | 381 |
401 @UseJinja('go_templates/source.tmpl', filters=go_filters) | 382 @UseJinja('go_templates/source.tmpl', filters=go_filters) |
402 def GenerateSource(self): | 383 def GenerateSource(self): |
403 return self.GetParameters() | 384 return self.GetParameters() |
404 | 385 |
405 def GenerateFiles(self, args): | 386 def GenerateFiles(self, args): |
| 387 self.should_gen_mojom_types = "--generate_type_info" in args |
| 388 |
406 self.Write(self.GenerateSource(), os.path.join("go", "src", | 389 self.Write(self.GenerateSource(), os.path.join("go", "src", |
407 GetPackagePath(self.module), "%s.go" % self.module.name)) | 390 GetPackagePath(self.module), "%s.go" % self.module.name)) |
408 | 391 |
409 def GetJinjaParameters(self): | 392 def GetJinjaParameters(self): |
410 return { | 393 return { |
411 'lstrip_blocks': True, | 394 'lstrip_blocks': True, |
412 'trim_blocks': True, | 395 'trim_blocks': True, |
413 } | 396 } |
414 | 397 |
415 def GetGlobals(self): | 398 def GetGlobals(self): |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 # Overrides the implementation from the base class in order to customize the | 487 # Overrides the implementation from the base class in order to customize the |
505 # struct and field names. | 488 # struct and field names. |
506 def _GetResponseStructFromMethod(self, method): | 489 def _GetResponseStructFromMethod(self, method): |
507 params_class = "%s_%s_ResponseParams" % ( | 490 params_class = "%s_%s_ResponseParams" % ( |
508 GetNameForElement(method.interface), GetNameForElement(method)) | 491 GetNameForElement(method.interface), GetNameForElement(method)) |
509 struct = mojom.Struct(params_class, module=method.interface.module) | 492 struct = mojom.Struct(params_class, module=method.interface.module) |
510 for param in method.response_parameters: | 493 for param in method.response_parameters: |
511 struct.AddField("out%s" % GetNameForElement(param), | 494 struct.AddField("out%s" % GetNameForElement(param), |
512 param.kind, param.ordinal, attributes=param.attributes) | 495 param.kind, param.ordinal, attributes=param.attributes) |
513 return self._AddStructComputedData(False, struct) | 496 return self._AddStructComputedData(False, struct) |
OLD | NEW |