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 |
126 _spec_to_decode_method = { | 150 _spec_to_decode_method = { |
127 mojom.BOOL.spec: 'decodeBool', | 151 mojom.BOOL.spec: 'decodeBool', |
128 mojom.DCPIPE.spec: 'decodeConsumerHandle', | 152 mojom.DCPIPE.spec: 'decodeConsumerHandle', |
129 mojom.DOUBLE.spec: 'decodeDouble', | 153 mojom.DOUBLE.spec: 'decodeDouble', |
130 mojom.DPPIPE.spec: 'decodeProducerHandle', | 154 mojom.DPPIPE.spec: 'decodeProducerHandle', |
131 mojom.FLOAT.spec: 'decodeFloat', | 155 mojom.FLOAT.spec: 'decodeFloat', |
132 mojom.HANDLE.spec: 'decodeHandle', | 156 mojom.HANDLE.spec: 'decodeHandle', |
133 mojom.INT16.spec: 'decodeInt16', | 157 mojom.INT16.spec: 'decodeInt16', |
134 mojom.INT32.spec: 'decodeInt32', | 158 mojom.INT32.spec: 'decodeInt32', |
135 mojom.INT64.spec: 'decodeInt64', | 159 mojom.INT64.spec: 'decodeInt64', |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 mojom.NULLABLE_SHAREDBUFFER.spec: 'encodeSharedBufferHandle', | 192 mojom.NULLABLE_SHAREDBUFFER.spec: 'encodeSharedBufferHandle', |
169 mojom.NULLABLE_STRING.spec: 'encodeString', | 193 mojom.NULLABLE_STRING.spec: 'encodeString', |
170 mojom.SHAREDBUFFER.spec: 'encodeSharedBufferHandle', | 194 mojom.SHAREDBUFFER.spec: 'encodeSharedBufferHandle', |
171 mojom.STRING.spec: 'encodeString', | 195 mojom.STRING.spec: 'encodeString', |
172 mojom.UINT16.spec: 'encodeUint16', | 196 mojom.UINT16.spec: 'encodeUint16', |
173 mojom.UINT32.spec: 'encodeUint32', | 197 mojom.UINT32.spec: 'encodeUint32', |
174 mojom.UINT64.spec: 'encodeUint64', | 198 mojom.UINT64.spec: 'encodeUint64', |
175 mojom.UINT8.spec: 'encodeUint8', | 199 mojom.UINT8.spec: 'encodeUint8', |
176 } | 200 } |
177 | 201 |
| 202 # The mojom_types.mojom and service_describer.mojom files are special because |
| 203 # they are used to generate mojom Type's and ServiceDescription implementations. |
| 204 # They need to be imported, unless the file itself is being generated. |
| 205 _service_describer_pkg_short = "service_describer" |
| 206 _service_describer_pkg = "package:mojo/mojo/bindings/types/%s.mojom.dart" % \ |
| 207 _service_describer_pkg_short |
| 208 _mojom_types_pkg_short = "mojom_types" |
| 209 _mojom_types_pkg = "package:mojo/mojo/bindings/types/%s.mojom.dart" % \ |
| 210 _mojom_types_pkg_short |
| 211 |
178 def GetDartType(kind): | 212 def GetDartType(kind): |
179 if kind.imported_from: | 213 if kind.imported_from: |
180 return kind.imported_from["unique_name"] + "." + GetNameForElement(kind) | 214 return kind.imported_from["unique_name"] + "." + GetNameForElement(kind) |
181 return GetNameForElement(kind) | 215 return GetNameForElement(kind) |
182 | 216 |
183 def DartDefaultValue(field): | 217 def DartDefaultValue(field): |
184 if field.default: | 218 if field.default: |
185 if mojom.IsStructKind(field.kind): | 219 if mojom.IsStructKind(field.kind): |
186 assert field.default == "default" | 220 assert field.default == "default" |
187 return "new %s()" % GetDartType(field.kind) | 221 return "new %s()" % GetDartType(field.kind) |
(...skipping 30 matching lines...) Expand all Loading... |
218 if mojom.IsMapKind(kind): | 252 if mojom.IsMapKind(kind): |
219 key_type = DartDeclType(kind.key_kind) | 253 key_type = DartDeclType(kind.key_kind) |
220 value_type = DartDeclType(kind.value_kind) | 254 value_type = DartDeclType(kind.value_kind) |
221 return "Map<"+ key_type + ", " + value_type + ">" | 255 return "Map<"+ key_type + ", " + value_type + ">" |
222 if mojom.IsInterfaceKind(kind) or \ | 256 if mojom.IsInterfaceKind(kind) or \ |
223 mojom.IsInterfaceRequestKind(kind): | 257 mojom.IsInterfaceRequestKind(kind): |
224 return "Object" | 258 return "Object" |
225 if mojom.IsEnumKind(kind): | 259 if mojom.IsEnumKind(kind): |
226 return GetDartType(kind) | 260 return GetDartType(kind) |
227 | 261 |
| 262 def GetSimpleMojomTypeName(kind): |
| 263 return _kind_to_mojom_type[kind] |
| 264 |
228 def NameToComponent(name): | 265 def NameToComponent(name): |
229 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> | 266 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> |
230 # HTTP_Entry2_FooBar). Numbers terminate a string of lower-case characters. | 267 # HTTP_Entry2_FooBar). Numbers terminate a string of lower-case characters. |
231 name = re.sub('([^_])([A-Z][^A-Z1-9_]+)', r'\1_\2', name) | 268 name = re.sub('([^_])([A-Z][^A-Z1-9_]+)', r'\1_\2', name) |
232 # insert '_' between non upper and start of upper blocks (e.g., | 269 # insert '_' between non upper and start of upper blocks (e.g., |
233 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar). | 270 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar). |
234 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) | 271 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) |
235 return [x.lower() for x in name.split('_')] | 272 return [x.lower() for x in name.split('_')] |
236 | 273 |
237 def UpperCamelCase(name): | 274 def UpperCamelCase(name): |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 | 456 |
420 def IsPointerArrayKind(kind): | 457 def IsPointerArrayKind(kind): |
421 if not mojom.IsArrayKind(kind): | 458 if not mojom.IsArrayKind(kind): |
422 return False | 459 return False |
423 sub_kind = kind.kind | 460 sub_kind = kind.kind |
424 return mojom.IsObjectKind(sub_kind) | 461 return mojom.IsObjectKind(sub_kind) |
425 | 462 |
426 def IsEnumArrayKind(kind): | 463 def IsEnumArrayKind(kind): |
427 return mojom.IsArrayKind(kind) and mojom.IsEnumKind(kind.kind) | 464 return mojom.IsArrayKind(kind) and mojom.IsEnumKind(kind.kind) |
428 | 465 |
| 466 def IsImportedKind(kind): |
| 467 return hasattr(kind, 'imported_from') and kind.imported_from |
| 468 |
429 def ParseStringAttribute(attribute): | 469 def ParseStringAttribute(attribute): |
430 assert isinstance(attribute, basestring) | 470 assert isinstance(attribute, basestring) |
431 return attribute | 471 return attribute |
432 | 472 |
433 def GetPackage(module): | 473 def GetPackage(module): |
434 if module.attributes and 'DartPackage' in module.attributes: | 474 if module.attributes and 'DartPackage' in module.attributes: |
435 return ParseStringAttribute(module.attributes['DartPackage']) | 475 return ParseStringAttribute(module.attributes['DartPackage']) |
436 # Default package. | 476 # Default package. |
437 return 'mojom' | 477 return 'mojom' |
438 | 478 |
439 def GetImportUri(module): | 479 def GetImportUri(module): |
440 package = GetPackage(module); | 480 package = GetPackage(module); |
441 elements = module.namespace.split('.') | 481 elements = module.namespace.split('.') |
442 elements.append("%s" % module.name) | 482 elements.append("%s" % module.name) |
443 return os.path.join(package, *elements) | 483 return os.path.join(package, *elements) |
444 | 484 |
| 485 def RaiseHelper(msg): |
| 486 raise Exception(msg) |
| 487 |
445 class Generator(generator.Generator): | 488 class Generator(generator.Generator): |
446 | 489 |
447 dart_filters = { | 490 dart_filters = { |
448 'array_expected_length': GetArrayExpectedLength, | 491 'array_expected_length': GetArrayExpectedLength, |
449 'array': GetArrayKind, | 492 'array': GetArrayKind, |
450 'decode_method': DecodeMethod, | 493 'decode_method': DecodeMethod, |
451 'default_value': DartDefaultValue, | 494 'default_value': DartDefaultValue, |
452 'encode_method': EncodeMethod, | 495 '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, |
| 501 'is_array_kind': mojom.IsArrayKind, |
453 'is_map_kind': mojom.IsMapKind, | 502 'is_map_kind': mojom.IsMapKind, |
| 503 'is_numerical_kind': mojom.IsNumericalKind, |
| 504 'is_any_handle_kind': mojom.IsAnyHandleKind, |
| 505 'is_string_kind': mojom.IsStringKind, |
454 'is_nullable_kind': mojom.IsNullableKind, | 506 'is_nullable_kind': mojom.IsNullableKind, |
455 'is_pointer_array_kind': IsPointerArrayKind, | 507 'is_pointer_array_kind': IsPointerArrayKind, |
456 'is_enum_array_kind': IsEnumArrayKind, | 508 'is_enum_array_kind': IsEnumArrayKind, |
457 'is_struct_kind': mojom.IsStructKind, | 509 'is_struct_kind': mojom.IsStructKind, |
458 'is_union_kind': mojom.IsUnionKind, | 510 'is_union_kind': mojom.IsUnionKind, |
459 'is_enum_kind': mojom.IsEnumKind, | 511 'is_enum_kind': mojom.IsEnumKind, |
| 512 'is_interface_kind': mojom.IsInterfaceKind, |
| 513 'is_interface_request_kind': mojom.IsInterfaceRequestKind, |
460 'dart_true_false': GetDartTrueFalse, | 514 'dart_true_false': GetDartTrueFalse, |
461 'dart_type': DartDeclType, | 515 'dart_type': DartDeclType, |
462 'name': GetNameForElement, | 516 'name': GetNameForElement, |
463 'interface_response_name': GetInterfaceResponseName, | 517 'interface_response_name': GetInterfaceResponseName, |
464 'dot_to_underscore': DotToUnderscore, | 518 'dot_to_underscore': DotToUnderscore, |
465 'is_cloneable_kind': mojom.IsCloneableKind, | 519 'is_cloneable_kind': mojom.IsCloneableKind, |
| 520 'upper_camel': UpperCamelCase, |
| 521 'lower_camel': CamelCase, |
| 522 'raise': RaiseHelper, |
466 } | 523 } |
467 | 524 |
| 525 # If set to True, then mojom type information will be generated. |
| 526 should_gen_mojom_types = False |
| 527 |
468 def GetParameters(self, args): | 528 def GetParameters(self, args): |
469 return { | 529 package = self.module.name.split('.')[0] |
| 530 |
| 531 parameters = { |
470 "namespace": self.module.namespace, | 532 "namespace": self.module.namespace, |
471 "imports": self.GetImports(args), | 533 "imports": self.GetImports(args), |
472 "kinds": self.module.kinds, | 534 "kinds": self.module.kinds, |
473 "enums": self.module.enums, | 535 "enums": self.module.enums, |
474 "module": resolver.ResolveConstants(self.module, ExpressionToText), | 536 "module": resolver.ResolveConstants(self.module, ExpressionToText), |
475 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 537 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
476 "unions": self.GetUnions(), | 538 "unions": self.GetUnions(), |
477 "interfaces": self.GetInterfaces(), | 539 "interfaces": self.GetInterfaces(), |
478 "imported_interfaces": self.GetImportedInterfaces(), | 540 "imported_interfaces": self.GetImportedInterfaces(), |
479 "imported_from": self.ImportedFrom(), | 541 "imported_from": self.ImportedFrom(), |
| 542 "typepkg": '%s.' % _mojom_types_pkg_short, |
| 543 "descpkg": '%s.' % _service_describer_pkg_short, |
| 544 "mojom_types_import": 'import \'%s\' as %s;' % \ |
| 545 (_mojom_types_pkg, _mojom_types_pkg_short), |
| 546 "service_describer_import": 'import \'%s\' as %s;' % \ |
| 547 (_service_describer_pkg, _service_describer_pkg_short), |
| 548 } |
| 549 |
| 550 # If this is the mojom types package, clear the import-related params. |
| 551 if package == _mojom_types_pkg_short: |
| 552 parameters["typepkg"] = "" |
| 553 parameters["mojom_types_import"] = "" |
| 554 |
| 555 # If this is the service describer package, clear the import-related params. |
| 556 if package == _service_describer_pkg_short: |
| 557 parameters["descpkg"] = "" |
| 558 parameters["service_describer_import"] = "" |
| 559 |
| 560 # If no interfaces were defined, the service describer import isn't needed. |
| 561 if len(self.module.interfaces) == 0: |
| 562 parameters["service_describer_import"] = "" |
| 563 |
| 564 return parameters |
| 565 |
| 566 def GetGlobals(self): |
| 567 return { |
| 568 'should_gen_mojom_types': self.should_gen_mojom_types, |
480 } | 569 } |
481 | 570 |
482 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) | 571 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) |
483 def GenerateLibModule(self, args): | 572 def GenerateLibModule(self, args): |
484 return self.GetParameters(args) | 573 return self.GetParameters(args) |
485 | 574 |
486 | 575 |
487 def GenerateFiles(self, args): | 576 def GenerateFiles(self, args): |
| 577 self.should_gen_mojom_types = "--generate_type_info" in args |
| 578 |
488 elements = self.module.namespace.split('.') | 579 elements = self.module.namespace.split('.') |
489 elements.append("%s.dart" % self.module.name) | 580 elements.append("%s.dart" % self.module.name) |
490 | 581 |
491 lib_module = self.GenerateLibModule(args) | 582 lib_module = self.GenerateLibModule(args) |
492 | 583 |
493 # List of packages with checked in bindings. | 584 # List of packages with checked in bindings. |
494 # TODO(johnmccutchan): Stop generating bindings as part of build system | 585 # TODO(johnmccutchan): Stop generating bindings as part of build system |
495 # and then remove this. | 586 # and then remove this. |
496 packages_with_checked_in_bindings = [ | 587 packages_with_checked_in_bindings = [ |
497 '_mojo_for_test_only', | 588 '_mojo_for_test_only', |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 interface_to_import[name] = each_import["unique_name"] + "." + name | 652 interface_to_import[name] = each_import["unique_name"] + "." + name |
562 return interface_to_import | 653 return interface_to_import |
563 | 654 |
564 def ImportedFrom(self): | 655 def ImportedFrom(self): |
565 interface_to_import = {} | 656 interface_to_import = {} |
566 for each_import in self.module.imports: | 657 for each_import in self.module.imports: |
567 for each_interface in each_import["module"].interfaces: | 658 for each_interface in each_import["module"].interfaces: |
568 name = each_interface.name | 659 name = each_interface.name |
569 interface_to_import[name] = each_import["unique_name"] + "." | 660 interface_to_import[name] = each_import["unique_name"] + "." |
570 return interface_to_import | 661 return interface_to_import |
OLD | NEW |