| 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 dart source files from a mojom.Module.""" | 5 """Generates dart source files from a mojom.Module.""" |
| 6 | 6 |
| 7 import errno | 7 import errno |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 import shutil | 10 import shutil |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 mojom.NULLABLE_DPPIPE: "core.MojoDataPipeProducer", | 116 mojom.NULLABLE_DPPIPE: "core.MojoDataPipeProducer", |
| 117 mojom.NULLABLE_MSGPIPE: "core.MojoMessagePipeEndpoint", | 117 mojom.NULLABLE_MSGPIPE: "core.MojoMessagePipeEndpoint", |
| 118 mojom.NULLABLE_SHAREDBUFFER: "core.MojoSharedBuffer", | 118 mojom.NULLABLE_SHAREDBUFFER: "core.MojoSharedBuffer", |
| 119 mojom.INT64: "int", | 119 mojom.INT64: "int", |
| 120 mojom.UINT64: "int", | 120 mojom.UINT64: "int", |
| 121 mojom.DOUBLE: "double", | 121 mojom.DOUBLE: "double", |
| 122 mojom.STRING: "String", | 122 mojom.STRING: "String", |
| 123 mojom.NULLABLE_STRING: "String" | 123 mojom.NULLABLE_STRING: "String" |
| 124 } | 124 } |
| 125 | 125 |
| 126 _kind_to_mojom_type = { | |
| 127 mojom.BOOL: "bool", | |
| 128 mojom.INT8: "int8", | |
| 129 mojom.UINT8: "uint8", | |
| 130 mojom.INT16: "int16", | |
| 131 mojom.UINT16: "uint16", | |
| 132 mojom.INT32: "int32", | |
| 133 mojom.UINT32: "uint32", | |
| 134 mojom.FLOAT: "float", | |
| 135 mojom.HANDLE: "unspecified", | |
| 136 mojom.DCPIPE: "dataPipeConsumer", | |
| 137 mojom.DPPIPE: "dataPipeProducer", | |
| 138 mojom.MSGPIPE: "messagePipe", | |
| 139 mojom.SHAREDBUFFER: "sharedBuffer", | |
| 140 mojom.NULLABLE_HANDLE: "unspecified", | |
| 141 mojom.NULLABLE_DCPIPE: "dataPipeConsumer", | |
| 142 mojom.NULLABLE_DPPIPE: "dataPipeProducer", | |
| 143 mojom.NULLABLE_MSGPIPE: "messagePipe", | |
| 144 mojom.NULLABLE_SHAREDBUFFER: "sharedBuffer", | |
| 145 mojom.INT64: "int64", | |
| 146 mojom.UINT64: "uint64", | |
| 147 mojom.DOUBLE: "double" | |
| 148 } | |
| 149 | |
| 150 _spec_to_decode_method = { | 126 _spec_to_decode_method = { |
| 151 mojom.BOOL.spec: 'decodeBool', | 127 mojom.BOOL.spec: 'decodeBool', |
| 152 mojom.DCPIPE.spec: 'decodeConsumerHandle', | 128 mojom.DCPIPE.spec: 'decodeConsumerHandle', |
| 153 mojom.DOUBLE.spec: 'decodeDouble', | 129 mojom.DOUBLE.spec: 'decodeDouble', |
| 154 mojom.DPPIPE.spec: 'decodeProducerHandle', | 130 mojom.DPPIPE.spec: 'decodeProducerHandle', |
| 155 mojom.FLOAT.spec: 'decodeFloat', | 131 mojom.FLOAT.spec: 'decodeFloat', |
| 156 mojom.HANDLE.spec: 'decodeHandle', | 132 mojom.HANDLE.spec: 'decodeHandle', |
| 157 mojom.INT16.spec: 'decodeInt16', | 133 mojom.INT16.spec: 'decodeInt16', |
| 158 mojom.INT32.spec: 'decodeInt32', | 134 mojom.INT32.spec: 'decodeInt32', |
| 159 mojom.INT64.spec: 'decodeInt64', | 135 mojom.INT64.spec: 'decodeInt64', |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 if mojom.IsMapKind(kind): | 228 if mojom.IsMapKind(kind): |
| 253 key_type = DartDeclType(kind.key_kind) | 229 key_type = DartDeclType(kind.key_kind) |
| 254 value_type = DartDeclType(kind.value_kind) | 230 value_type = DartDeclType(kind.value_kind) |
| 255 return "Map<"+ key_type + ", " + value_type + ">" | 231 return "Map<"+ key_type + ", " + value_type + ">" |
| 256 if mojom.IsInterfaceKind(kind) or \ | 232 if mojom.IsInterfaceKind(kind) or \ |
| 257 mojom.IsInterfaceRequestKind(kind): | 233 mojom.IsInterfaceRequestKind(kind): |
| 258 return "Object" | 234 return "Object" |
| 259 if mojom.IsEnumKind(kind): | 235 if mojom.IsEnumKind(kind): |
| 260 return GetDartType(kind) | 236 return GetDartType(kind) |
| 261 | 237 |
| 262 def GetSimpleMojomTypeName(kind): | |
| 263 return _kind_to_mojom_type[kind] | |
| 264 | |
| 265 def NameToComponent(name): | 238 def NameToComponent(name): |
| 266 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> | 239 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> |
| 267 # HTTP_Entry2_FooBar). Numbers terminate a string of lower-case characters. | 240 # HTTP_Entry2_FooBar). Numbers terminate a string of lower-case characters. |
| 268 name = re.sub('([^_])([A-Z][^A-Z1-9_]+)', r'\1_\2', name) | 241 name = re.sub('([^_])([A-Z][^A-Z1-9_]+)', r'\1_\2', name) |
| 269 # insert '_' between non upper and start of upper blocks (e.g., | 242 # insert '_' between non upper and start of upper blocks (e.g., |
| 270 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar). | 243 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar). |
| 271 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) | 244 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) |
| 272 return [x.lower() for x in name.split('_')] | 245 return [x.lower() for x in name.split('_')] |
| 273 | 246 |
| 274 def UpperCamelCase(name): | 247 def UpperCamelCase(name): |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 451 |
| 479 def GetImportUri(module): | 452 def GetImportUri(module): |
| 480 package = GetPackage(module); | 453 package = GetPackage(module); |
| 481 elements = module.namespace.split('.') | 454 elements = module.namespace.split('.') |
| 482 elements.append("%s" % module.name) | 455 elements.append("%s" % module.name) |
| 483 return os.path.join(package, *elements) | 456 return os.path.join(package, *elements) |
| 484 | 457 |
| 485 def RaiseHelper(msg): | 458 def RaiseHelper(msg): |
| 486 raise Exception(msg) | 459 raise Exception(msg) |
| 487 | 460 |
| 461 def GetSerializedRuntimeTypeInfoLiteral(module, enabled): |
| 462 """ Constructs a string that represents a literal definition in Dart of |
| 463 an array of bytes corresponding to |module.serialized_runtime_type_info|. |
| 464 |
| 465 Args: |
| 466 module: {mojom.Module} the module being processed. |
| 467 enabled: {bool} Is this feature enabled. |
| 468 |
| 469 Returns: A string of the form 'b0, b1, b2,...' where the 'bi' are |
| 470 the decimal representation of the bytes of |
| 471 |module.serialized_runtime_type_info| or the empty string if either |
| 472 |enabled| is false or |module.serialized_runtime_type_info| is None. |
| 473 Furthermore the returned string will have embedded newline characters inserted |
| 474 every 1000 characters to make the generated source code more tractable. |
| 475 """ |
| 476 if not enabled or not module.serialized_runtime_type_info: |
| 477 return '' |
| 478 return '%s' % ','.join('%s%d' % |
| 479 ('\n' if index > 0 and index%1000 == 0 else '', b) |
| 480 for index, b in enumerate(module.serialized_runtime_type_info)) |
| 481 |
| 488 class Generator(generator.Generator): | 482 class Generator(generator.Generator): |
| 489 | 483 |
| 490 dart_filters = { | 484 dart_filters = { |
| 491 'array_expected_length': GetArrayExpectedLength, | 485 'array_expected_length': GetArrayExpectedLength, |
| 492 'array': GetArrayKind, | 486 'array': GetArrayKind, |
| 493 'decode_method': DecodeMethod, | 487 'decode_method': DecodeMethod, |
| 494 'default_value': DartDefaultValue, | 488 'default_value': DartDefaultValue, |
| 495 'encode_method': EncodeMethod, | 489 'encode_method': EncodeMethod, |
| 496 'fullidentifier': mojom.GetMojomTypeFullIdentifier, | |
| 497 'simple_mojom_type_name': GetSimpleMojomTypeName, | |
| 498 'mojom_type_name': mojom.GetMojomTypeName, | |
| 499 'mojom_type_identifier': mojom.GetMojomTypeIdentifier, | |
| 500 'is_imported_kind': IsImportedKind, | 490 'is_imported_kind': IsImportedKind, |
| 501 'is_array_kind': mojom.IsArrayKind, | 491 'is_array_kind': mojom.IsArrayKind, |
| 502 'is_map_kind': mojom.IsMapKind, | 492 'is_map_kind': mojom.IsMapKind, |
| 503 'is_numerical_kind': mojom.IsNumericalKind, | 493 'is_numerical_kind': mojom.IsNumericalKind, |
| 504 'is_any_handle_kind': mojom.IsAnyHandleKind, | 494 'is_any_handle_kind': mojom.IsAnyHandleKind, |
| 505 'is_string_kind': mojom.IsStringKind, | 495 'is_string_kind': mojom.IsStringKind, |
| 506 'is_nullable_kind': mojom.IsNullableKind, | 496 'is_nullable_kind': mojom.IsNullableKind, |
| 507 'is_pointer_array_kind': IsPointerArrayKind, | 497 'is_pointer_array_kind': IsPointerArrayKind, |
| 508 'is_enum_array_kind': IsEnumArrayKind, | 498 'is_enum_array_kind': IsEnumArrayKind, |
| 509 'is_struct_kind': mojom.IsStructKind, | 499 'is_struct_kind': mojom.IsStructKind, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 538 "namespace": self.module.namespace, | 528 "namespace": self.module.namespace, |
| 539 "imports": self.GetImports(args), | 529 "imports": self.GetImports(args), |
| 540 "kinds": self.module.kinds, | 530 "kinds": self.module.kinds, |
| 541 "enums": self.module.enums, | 531 "enums": self.module.enums, |
| 542 "module": resolver.ResolveConstants(self.module, ExpressionToText), | 532 "module": resolver.ResolveConstants(self.module, ExpressionToText), |
| 543 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 533 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
| 544 "unions": self.GetUnions(), | 534 "unions": self.GetUnions(), |
| 545 "interfaces": self.GetInterfaces(), | 535 "interfaces": self.GetInterfaces(), |
| 546 "imported_interfaces": self.GetImportedInterfaces(), | 536 "imported_interfaces": self.GetImportedInterfaces(), |
| 547 "imported_from": self.ImportedFrom(), | 537 "imported_from": self.ImportedFrom(), |
| 538 "serialized_runtime_type_info_literal" : ( |
| 539 GetSerializedRuntimeTypeInfoLiteral(self.module, |
| 540 self.should_gen_mojom_types)), |
| 548 "typepkg": '%s.' % _mojom_types_pkg_short, | 541 "typepkg": '%s.' % _mojom_types_pkg_short, |
| 549 "descpkg": '%s.' % _service_describer_pkg_short, | 542 "descpkg": '%s.' % _service_describer_pkg_short, |
| 550 "mojom_types_import": 'import \'%s\' as %s;' % \ | 543 "mojom_types_import": 'import \'%s\' as %s;' % \ |
| 551 (_mojom_types_pkg, _mojom_types_pkg_short), | 544 (_mojom_types_pkg, _mojom_types_pkg_short), |
| 552 "service_describer_import": 'import \'%s\' as %s;' % \ | 545 "service_describer_import": 'import \'%s\' as %s;' % \ |
| 553 (_service_describer_pkg, _service_describer_pkg_short), | 546 (_service_describer_pkg, _service_describer_pkg_short), |
| 554 "has_handles": has_handles, | 547 "has_handles": has_handles, |
| 555 } | 548 } |
| 556 | 549 |
| 557 # If this is the mojom types package, clear the import-related params. | 550 # If this is the mojom types package, clear the import-related params. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 interface_to_import[name] = each_import["unique_name"] + "." + name | 652 interface_to_import[name] = each_import["unique_name"] + "." + name |
| 660 return interface_to_import | 653 return interface_to_import |
| 661 | 654 |
| 662 def ImportedFrom(self): | 655 def ImportedFrom(self): |
| 663 interface_to_import = {} | 656 interface_to_import = {} |
| 664 for each_import in self.module.imports: | 657 for each_import in self.module.imports: |
| 665 for each_interface in each_import["module"].interfaces: | 658 for each_interface in each_import["module"].interfaces: |
| 666 name = each_interface.name | 659 name = each_interface.name |
| 667 interface_to_import[name] = each_import["unique_name"] + "." | 660 interface_to_import[name] = each_import["unique_name"] + "." |
| 668 return interface_to_import | 661 return interface_to_import |
| OLD | NEW |