Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/module.py

Issue 2855263002: Mojo bindings: support generating identifers using different style rules for different target langu…
Patch Set: . Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 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 # This module's classes provide an interface to mojo modules. Modules are 5 # This module's classes provide an interface to mojo modules. Modules are
6 # collections of interfaces and structs to be used by mojo ipc clients and 6 # collections of interfaces and structs to be used by mojo ipc clients and
7 # servers. 7 # servers.
8 # 8 #
9 # A simple interface would be created this way: 9 # A simple interface would be created this way:
10 # module = mojom.generate.module.Module('Foo') 10 # module = mojom.generate.module.Module('Foo')
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 self.enum.name + '.' + self.name) 257 self.enum.name + '.' + self.name)
258 258
259 259
260 class Constant(object): 260 class Constant(object):
261 def __init__(self, name=None, kind=None, value=None, parent_kind=None): 261 def __init__(self, name=None, kind=None, value=None, parent_kind=None):
262 self.name = name 262 self.name = name
263 self.kind = kind 263 self.kind = kind
264 self.value = value 264 self.value = value
265 self.parent_kind = parent_kind 265 self.parent_kind = parent_kind
266 266
267 def Stylize(self, stylizer):
268 self.stylized_name = stylizer.StylizeConstant(self.name)
269
267 270
268 class Field(object): 271 class Field(object):
269 def __init__(self, name=None, kind=None, ordinal=None, default=None, 272 def __init__(self, name=None, kind=None, ordinal=None, default=None,
270 attributes=None): 273 attributes=None):
271 if self.__class__.__name__ == 'Field': 274 if self.__class__.__name__ == 'Field':
272 raise Exception() 275 raise Exception()
273 self.name = name 276 self.name = name
274 self.kind = kind 277 self.kind = kind
275 self.ordinal = ordinal 278 self.ordinal = ordinal
276 self.default = default 279 self.default = default
277 self.attributes = attributes 280 self.attributes = attributes
278 281
279 def Repr(self, as_ref=True): 282 def Repr(self, as_ref=True):
280 # Fields are only referenced by objects which define them and thus 283 # Fields are only referenced by objects which define them and thus
281 # they are always displayed as non-references. 284 # they are always displayed as non-references.
282 return GenericRepr(self, {'name': False, 'kind': True}) 285 return GenericRepr(self, {'name': False, 'kind': True})
283 286
284 @property 287 @property
285 def min_version(self): 288 def min_version(self):
286 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ 289 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \
287 if self.attributes else None 290 if self.attributes else None
288 291
292 def Stylize(self, stylizer):
293 self.stylized_name = stylizer.StylizeField(self.name)
294
289 295
290 class StructField(Field): pass 296 class StructField(Field): pass
291 297
292 298
293 class UnionField(Field): pass 299 class UnionField(Field): pass
294 300
295 301
296 class Struct(ReferenceKind): 302 class Struct(ReferenceKind):
297 """A struct with typed fields. 303 """A struct with typed fields.
298 304
299 Attributes: 305 Attributes:
300 name: {str} The name of the struct type. 306 name: {str} The name of the struct type.
301 native_only: {bool} Does the struct have a body (i.e. any fields) or is it 307 native_only: {bool} Does the struct have a body (i.e. any fields) or is it
302 purely a native struct. 308 purely a native struct.
303 module: {Module} The defining module. 309 module: {Module} The defining module.
304 imported_from: {dict} Information about where this union was 310 imported_from: {dict} Information about where this union was
305 imported from. 311 imported from.
306 fields: {List[StructField]} The members of the struct. 312 fields: {List[StructField]} The members of the struct.
307 attributes: {dict} Additional information about the struct, such as 313 attributes: {dict} Additional information about the struct, such as
308 if it's a native struct. 314 if it's a native struct.
309 """ 315 """
310 316
311 ReferenceKind.AddSharedProperty('name') 317 ReferenceKind.AddSharedProperty('name')
318 ReferenceKind.AddSharedProperty('stylized_name')
312 ReferenceKind.AddSharedProperty('native_only') 319 ReferenceKind.AddSharedProperty('native_only')
313 ReferenceKind.AddSharedProperty('module') 320 ReferenceKind.AddSharedProperty('module')
314 ReferenceKind.AddSharedProperty('imported_from') 321 ReferenceKind.AddSharedProperty('imported_from')
315 ReferenceKind.AddSharedProperty('fields') 322 ReferenceKind.AddSharedProperty('fields')
316 ReferenceKind.AddSharedProperty('attributes') 323 ReferenceKind.AddSharedProperty('attributes')
317 324
318 def __init__(self, name=None, module=None, attributes=None): 325 def __init__(self, name=None, module=None, attributes=None):
319 if name is not None: 326 if name is not None:
320 spec = 'x:' + name 327 spec = 'x:' + name
321 else: 328 else:
322 spec = None 329 spec = None
323 ReferenceKind.__init__(self, spec) 330 ReferenceKind.__init__(self, spec)
324 self.name = name 331 self.name = name
325 self.native_only = False 332 self.native_only = False
326 self.module = module 333 self.module = module
327 self.imported_from = None 334 self.imported_from = None
328 self.fields = [] 335 self.fields = []
336 self.enums = []
337 self.constants = []
329 self.attributes = attributes 338 self.attributes = attributes
330 339
331 def Repr(self, as_ref=True): 340 def Repr(self, as_ref=True):
332 if as_ref: 341 if as_ref:
333 return '<%s name=%r imported_from=%s>' % ( 342 return '<%s name=%r imported_from=%s>' % (
334 self.__class__.__name__, self.name, 343 self.__class__.__name__, self.name,
335 Repr(self.imported_from, as_ref=True)) 344 Repr(self.imported_from, as_ref=True))
336 else: 345 else:
337 return GenericRepr(self, {'name': False, 'fields': False, 346 return GenericRepr(self, {'name': False, 'fields': False,
338 'imported_from': True}) 347 'imported_from': True})
339 348
340 def AddField(self, name, kind, ordinal=None, default=None, attributes=None): 349 def AddField(self, name, kind, ordinal=None, default=None, attributes=None):
341 field = StructField(name, kind, ordinal, default, attributes) 350 field = StructField(name, kind, ordinal, default, attributes)
342 self.fields.append(field) 351 self.fields.append(field)
343 return field 352 return field
344 353
354 def Stylize(self, stylizer):
355 self.stylized_name = stylizer.StylizeStruct(self.name)
356 for field in self.fields:
357 field.Stylize(stylizer)
358 for enum in self.enums:
359 enum.Stylize(stylizer)
360 for constant in self.constants:
361 constant.Stylize(stylizer)
362
345 363
346 class Union(ReferenceKind): 364 class Union(ReferenceKind):
347 """A union of several kinds. 365 """A union of several kinds.
348 366
349 Attributes: 367 Attributes:
350 name: {str} The name of the union type. 368 name: {str} The name of the union type.
351 module: {Module} The defining module. 369 module: {Module} The defining module.
352 imported_from: {dict} Information about where this union was 370 imported_from: {dict} Information about where this union was
353 imported from. 371 imported from.
354 fields: {List[UnionField]} The members of the union. 372 fields: {List[UnionField]} The members of the union.
355 attributes: {dict} Additional information about the union, such as 373 attributes: {dict} Additional information about the union, such as
356 which Java class name to use to represent it in the generated 374 which Java class name to use to represent it in the generated
357 bindings. 375 bindings.
358 """ 376 """
359 ReferenceKind.AddSharedProperty('name') 377 ReferenceKind.AddSharedProperty('name')
378 ReferenceKind.AddSharedProperty('stylized_name')
360 ReferenceKind.AddSharedProperty('module') 379 ReferenceKind.AddSharedProperty('module')
361 ReferenceKind.AddSharedProperty('imported_from') 380 ReferenceKind.AddSharedProperty('imported_from')
362 ReferenceKind.AddSharedProperty('fields') 381 ReferenceKind.AddSharedProperty('fields')
363 ReferenceKind.AddSharedProperty('attributes') 382 ReferenceKind.AddSharedProperty('attributes')
364 383
365 def __init__(self, name=None, module=None, attributes=None): 384 def __init__(self, name=None, module=None, attributes=None):
366 if name is not None: 385 if name is not None:
367 spec = 'x:' + name 386 spec = 'x:' + name
368 else: 387 else:
369 spec = None 388 spec = None
(...skipping 10 matching lines...) Expand all
380 self.__class__.__name__, self.spec, self.is_nullable, 399 self.__class__.__name__, self.spec, self.is_nullable,
381 Repr(self.fields)) 400 Repr(self.fields))
382 else: 401 else:
383 return GenericRepr(self, {'fields': True, 'is_nullable': False}) 402 return GenericRepr(self, {'fields': True, 'is_nullable': False})
384 403
385 def AddField(self, name, kind, ordinal=None, attributes=None): 404 def AddField(self, name, kind, ordinal=None, attributes=None):
386 field = UnionField(name, kind, ordinal, None, attributes) 405 field = UnionField(name, kind, ordinal, None, attributes)
387 self.fields.append(field) 406 self.fields.append(field)
388 return field 407 return field
389 408
409 def Stylize(self, stylizer):
410 self.stylized_name = stylizer.StylizeUnion(self.name)
411 for field in self.fields:
412 field.Stylize(stylizer)
413
390 414
391 class Array(ReferenceKind): 415 class Array(ReferenceKind):
392 """An array. 416 """An array.
393 417
394 Attributes: 418 Attributes:
395 kind: {Kind} The type of the elements. May be None. 419 kind: {Kind} The type of the elements. May be None.
396 length: The number of elements. None if unknown. 420 length: The number of elements. None if unknown.
397 """ 421 """
398 422
399 ReferenceKind.AddSharedProperty('kind') 423 ReferenceKind.AddSharedProperty('kind')
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 525
502 def Repr(self, as_ref=True): 526 def Repr(self, as_ref=True):
503 return '<%s name=%r kind=%s>' % (self.__class__.__name__, self.name, 527 return '<%s name=%r kind=%s>' % (self.__class__.__name__, self.name,
504 self.kind.Repr(as_ref=True)) 528 self.kind.Repr(as_ref=True))
505 529
506 @property 530 @property
507 def min_version(self): 531 def min_version(self):
508 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ 532 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \
509 if self.attributes else None 533 if self.attributes else None
510 534
535 def Stylize(self, stylizer):
536 self.stylized_name = stylizer.StylizeParameter(self.name)
537
511 538
512 class Method(object): 539 class Method(object):
513 def __init__(self, interface, name, ordinal=None, attributes=None): 540 def __init__(self, interface, name, ordinal=None, attributes=None):
514 self.interface = interface 541 self.interface = interface
515 self.name = name 542 self.name = name
516 self.ordinal = ordinal 543 self.ordinal = ordinal
517 self.parameters = [] 544 self.parameters = []
518 self.response_parameters = None 545 self.response_parameters = None
519 self.attributes = attributes 546 self.attributes = attributes
520 547
(...skipping 21 matching lines...) Expand all
542 @property 569 @property
543 def min_version(self): 570 def min_version(self):
544 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ 571 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \
545 if self.attributes else None 572 if self.attributes else None
546 573
547 @property 574 @property
548 def sync(self): 575 def sync(self):
549 return self.attributes.get(ATTRIBUTE_SYNC) \ 576 return self.attributes.get(ATTRIBUTE_SYNC) \
550 if self.attributes else None 577 if self.attributes else None
551 578
579 def Stylize(self, stylizer):
580 self.stylized_name = stylizer.StylizeMethod(self.name)
581 for param in self.parameters:
582 param.Stylize(stylizer)
583 if self.response_parameters:
584 for param in self.response_parameters:
585 param.Stylize(stylizer)
586
552 587
553 class Interface(ReferenceKind): 588 class Interface(ReferenceKind):
554 ReferenceKind.AddSharedProperty('module') 589 ReferenceKind.AddSharedProperty('module')
555 ReferenceKind.AddSharedProperty('name') 590 ReferenceKind.AddSharedProperty('name')
591 ReferenceKind.AddSharedProperty('stylized_name')
556 ReferenceKind.AddSharedProperty('imported_from') 592 ReferenceKind.AddSharedProperty('imported_from')
557 ReferenceKind.AddSharedProperty('methods') 593 ReferenceKind.AddSharedProperty('methods')
558 ReferenceKind.AddSharedProperty('attributes') 594 ReferenceKind.AddSharedProperty('attributes')
559 595
560 def __init__(self, name=None, module=None, attributes=None): 596 def __init__(self, name=None, module=None, attributes=None):
561 if name is not None: 597 if name is not None:
562 spec = 'x:' + name 598 spec = 'x:' + name
563 else: 599 else:
564 spec = None 600 spec = None
565 ReferenceKind.__init__(self, spec) 601 ReferenceKind.__init__(self, spec)
566 self.module = module 602 self.module = module
567 self.name = name 603 self.name = name
568 self.imported_from = None 604 self.imported_from = None
569 self.methods = [] 605 self.methods = []
606 self.enums = []
607 self.constants = []
570 self.attributes = attributes 608 self.attributes = attributes
571 609
572 def Repr(self, as_ref=True): 610 def Repr(self, as_ref=True):
573 if as_ref: 611 if as_ref:
574 return '<%s name=%r>' % (self.__class__.__name__, self.name) 612 return '<%s name=%r>' % (self.__class__.__name__, self.name)
575 else: 613 else:
576 return GenericRepr(self, {'name': False, 'attributes': False, 614 return GenericRepr(self, {'name': False, 'attributes': False,
577 'methods': False}) 615 'methods': False})
578 616
579 def AddMethod(self, name, ordinal=None, attributes=None): 617 def AddMethod(self, name, ordinal=None, attributes=None):
580 method = Method(self, name, ordinal, attributes) 618 method = Method(self, name, ordinal, attributes)
581 self.methods.append(method) 619 self.methods.append(method)
582 return method 620 return method
583 621
584 # TODO(451323): Remove when the language backends no longer rely on this. 622 # TODO(451323): Remove when the language backends no longer rely on this.
585 @property 623 @property
586 def client(self): 624 def client(self):
587 return None 625 return None
588 626
627 def Stylize(self, stylizer):
628 self.stylized_name = stylizer.StylizeInterface(self.name)
629 for method in self.methods:
630 method.Stylize(stylizer)
631 for enum in self.enums:
632 enum.Stylize(stylizer)
633 for constant in self.constants:
634 constant.Stylize(stylizer)
635
589 636
590 class AssociatedInterface(ReferenceKind): 637 class AssociatedInterface(ReferenceKind):
591 ReferenceKind.AddSharedProperty('kind') 638 ReferenceKind.AddSharedProperty('kind')
592 639
593 def __init__(self, kind=None): 640 def __init__(self, kind=None):
594 if kind is not None: 641 if kind is not None:
595 if not isinstance(kind, Interface): 642 if not isinstance(kind, Interface):
596 raise Exception( 643 raise Exception(
597 "Associated interface requires %r to be an interface." % kind.spec) 644 "Associated interface requires %r to be an interface." % kind.spec)
598 assert not kind.is_nullable 645 assert not kind.is_nullable
599 ReferenceKind.__init__(self, 'asso:' + kind.spec) 646 ReferenceKind.__init__(self, 'asso:' + kind.spec)
600 else: 647 else:
601 ReferenceKind.__init__(self) 648 ReferenceKind.__init__(self)
602 self.kind = kind 649 self.kind = kind
603 650
604 651
605 class EnumField(object): 652 class EnumField(object):
606 def __init__(self, name=None, value=None, attributes=None, 653 def __init__(self, name=None, value=None, attributes=None,
607 numeric_value=None): 654 numeric_value=None):
608 self.name = name 655 self.name = name
609 self.value = value 656 self.value = value
610 self.attributes = attributes 657 self.attributes = attributes
611 self.numeric_value = numeric_value 658 self.numeric_value = numeric_value
612 659
613 @property 660 @property
614 def min_version(self): 661 def min_version(self):
615 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ 662 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \
616 if self.attributes else None 663 if self.attributes else None
617 664
665 def Stylize(self, stylizer):
666 self.stylized_name = stylizer.StylizeEnumField(self.name)
667
618 668
619 class Enum(Kind): 669 class Enum(Kind):
620 def __init__(self, name=None, module=None, attributes=None): 670 def __init__(self, name=None, module=None, attributes=None):
621 self.module = module 671 self.module = module
622 self.name = name 672 self.name = name
623 self.native_only = False 673 self.native_only = False
624 self.imported_from = None 674 self.imported_from = None
625 if name is not None: 675 if name is not None:
626 spec = 'x:' + name 676 spec = 'x:' + name
627 else: 677 else:
628 spec = None 678 spec = None
629 Kind.__init__(self, spec) 679 Kind.__init__(self, spec)
630 self.fields = [] 680 self.fields = []
631 self.attributes = attributes 681 self.attributes = attributes
632 682
633 def Repr(self, as_ref=True): 683 def Repr(self, as_ref=True):
634 if as_ref: 684 if as_ref:
635 return '<%s name=%r>' % (self.__class__.__name__, self.name) 685 return '<%s name=%r>' % (self.__class__.__name__, self.name)
636 else: 686 else:
637 return GenericRepr(self, {'name': False, 'fields': False}) 687 return GenericRepr(self, {'name': False, 'fields': False})
638 688
639 @property 689 @property
640 def extensible(self): 690 def extensible(self):
641 return self.attributes.get(ATTRIBUTE_EXTENSIBLE, False) \ 691 return self.attributes.get(ATTRIBUTE_EXTENSIBLE, False) \
642 if self.attributes else False 692 if self.attributes else False
643 693
694 def Stylize(self, stylizer):
695 self.stylized_name = stylizer.StylizeEnum(self.name)
696 for field in self.fields:
697 field.Stylize(stylizer)
698
644 699
645 class Module(object): 700 class Module(object):
646 def __init__(self, name=None, namespace=None, attributes=None): 701 def __init__(self, name=None, namespace=None, attributes=None):
647 self.name = name 702 self.name = name
648 self.path = name 703 self.path = name
649 self.namespace = namespace 704 self.namespace = namespace
650 self.structs = [] 705 self.structs = []
651 self.unions = [] 706 self.unions = []
652 self.interfaces = [] 707 self.interfaces = []
708 self.enums = []
709 self.constants = []
653 self.kinds = {} 710 self.kinds = {}
654 self.attributes = attributes 711 self.attributes = attributes
712 self.imports = []
655 713
656 def __repr__(self): 714 def __repr__(self):
657 # Gives us a decent __repr__ for modules. 715 # Gives us a decent __repr__ for modules.
658 return self.Repr() 716 return self.Repr()
659 717
660 def Repr(self, as_ref=True): 718 def Repr(self, as_ref=True):
661 if as_ref: 719 if as_ref:
662 return '<%s name=%r namespace=%r>' % ( 720 return '<%s name=%r namespace=%r>' % (
663 self.__class__.__name__, self.name, self.namespace) 721 self.__class__.__name__, self.name, self.namespace)
664 else: 722 else:
665 return GenericRepr(self, {'name': False, 'namespace': False, 723 return GenericRepr(self, {'name': False, 'namespace': False,
666 'attributes': False, 'structs': False, 724 'attributes': False, 'structs': False,
667 'interfaces': False, 'unions': False}) 725 'interfaces': False, 'unions': False})
668 726
669 def AddInterface(self, name, attributes=None): 727 def AddInterface(self, name, attributes=None):
670 interface = Interface(name, self, attributes) 728 interface = Interface(name, self, attributes)
671 self.interfaces.append(interface) 729 self.interfaces.append(interface)
672 return interface 730 return interface
673 731
674 def AddStruct(self, name, attributes=None): 732 def AddStruct(self, name, attributes=None):
675 struct = Struct(name, self, attributes) 733 struct = Struct(name, self, attributes)
676 self.structs.append(struct) 734 self.structs.append(struct)
677 return struct 735 return struct
678 736
679 def AddUnion(self, name, attributes=None): 737 def AddUnion(self, name, attributes=None):
680 union = Union(name, self, attributes) 738 union = Union(name, self, attributes)
681 self.unions.append(union) 739 self.unions.append(union)
682 return union 740 return union
683 741
742 def Stylize(self, stylizer):
743 self.stylized_namespace = stylizer.StylizeModule(self.namespace)
744 for struct in self.structs:
745 struct.Stylize(stylizer)
746 for union in self.unions:
747 union.Stylize(stylizer)
748 for interface in self.interfaces:
749 interface.Stylize(stylizer)
750 for enum in self.enums:
751 enum.Stylize(stylizer)
752 for constant in self.constants:
753 constant.Stylize(stylizer)
754 for imported_module in self.imports:
755 imported_module['module'].Stylize(stylizer)
756
684 757
685 def IsBoolKind(kind): 758 def IsBoolKind(kind):
686 return kind.spec == BOOL.spec 759 return kind.spec == BOOL.spec
687 760
688 761
689 def IsFloatKind(kind): 762 def IsFloatKind(kind):
690 return kind.spec == FLOAT.spec 763 return kind.spec == FLOAT.spec
691 764
692 765
693 def IsDoubleKind(kind): 766 def IsDoubleKind(kind):
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 return True 966 return True
894 elif IsAnyInterfaceKind(kind): 967 elif IsAnyInterfaceKind(kind):
895 return True 968 return True
896 elif IsArrayKind(kind): 969 elif IsArrayKind(kind):
897 return Check(kind.kind) 970 return Check(kind.kind)
898 elif IsMapKind(kind): 971 elif IsMapKind(kind):
899 return Check(kind.key_kind) or Check(kind.value_kind) 972 return Check(kind.key_kind) or Check(kind.value_kind)
900 else: 973 else:
901 return False 974 return False
902 return Check(kind) 975 return Check(kind)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698