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

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 2689513003: Add field-initializing constructors to generated mojo structs. (Closed)
Patch Set: rebase Created 3 years, 9 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
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_definition.tmpl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 """Generates C++ source files from a mojom.Module.""" 5 """Generates C++ source files from a mojom.Module."""
6 6
7 import mojom.generate.generator as generator 7 import mojom.generate.generator as generator
8 import mojom.generate.module as mojom 8 import mojom.generate.module as mojom
9 import mojom.generate.pack as pack 9 import mojom.generate.pack as pack
10 from mojom.generate.template_expander import UseJinja 10 from mojom.generate.template_expander import UseJinja
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 for field in struct.fields: 475 for field in struct.fields:
476 if IsMoveOnlyKind(field.kind): 476 if IsMoveOnlyKind(field.kind):
477 return True 477 return True
478 return False 478 return False
479 479
480 def ShouldInlineUnion(union): 480 def ShouldInlineUnion(union):
481 return not any( 481 return not any(
482 mojom.IsReferenceKind(field.kind) and not mojom.IsStringKind(field.kind) 482 mojom.IsReferenceKind(field.kind) and not mojom.IsStringKind(field.kind)
483 for field in union.fields) 483 for field in union.fields)
484 484
485
486 class StructConstructor(object):
487 """Represents a constructor for a generated struct.
488
489 Fields:
490 fields: {[Field]} All struct fields in order.
491 params: {[Field]} The fields that are passed as params.
492 """
493
494 def __init__(self, fields, params):
495 self._fields = fields
496 self._params = set(params)
497
498 @property
499 def params(self):
500 return [field for field in self._fields if field in self._params]
501
502 @property
503 def fields(self):
504 for field in self._fields:
505 yield (field, field in self._params)
506
507
508 def GetStructConstructors(struct):
509 """Returns a list of constructors for a struct.
510
511 Params:
512 struct: {Struct} The struct to return constructors for.
513
514 Returns:
515 {[StructConstructor]} A list of StructConstructors that should be generated
516 for |struct|.
517 """
518 if not mojom.IsStructKind(struct):
519 raise TypeError
520 # Types that are neither copyable nor movable can't be passed to a struct
521 # constructor so only generate a default constructor.
522 if any(IsTypemappedKind(field.kind) and _current_typemap[
523 GetFullMojomNameForKind(field.kind)]["non_copyable_non_movable"]
524 for field in struct.fields):
525 return [StructConstructor(struct.fields, [])]
526
527 param_counts = [0]
528 for version in struct.versions:
529 if param_counts[-1] != version.num_fields:
530 param_counts.append(version.num_fields)
531
532 ordinal_fields = sorted(struct.fields, key=lambda field: field.ordinal)
533 return (StructConstructor(struct.fields, ordinal_fields[:param_count])
534 for param_count in param_counts)
535
536
485 def GetContainerValidateParamsCtorArgs(kind): 537 def GetContainerValidateParamsCtorArgs(kind):
486 if mojom.IsStringKind(kind): 538 if mojom.IsStringKind(kind):
487 expected_num_elements = 0 539 expected_num_elements = 0
488 element_is_nullable = False 540 element_is_nullable = False
489 key_validate_params = "nullptr" 541 key_validate_params = "nullptr"
490 element_validate_params = "nullptr" 542 element_validate_params = "nullptr"
491 enum_validate_func = "nullptr" 543 enum_validate_func = "nullptr"
492 elif mojom.IsMapKind(kind): 544 elif mojom.IsMapKind(kind):
493 expected_num_elements = 0 545 expected_num_elements = 0
494 element_is_nullable = False 546 element_is_nullable = False
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 "is_hashable": IsHashableKind, 617 "is_hashable": IsHashableKind,
566 "is_map_kind": mojom.IsMapKind, 618 "is_map_kind": mojom.IsMapKind,
567 "is_nullable_kind": mojom.IsNullableKind, 619 "is_nullable_kind": mojom.IsNullableKind,
568 "is_object_kind": mojom.IsObjectKind, 620 "is_object_kind": mojom.IsObjectKind,
569 "is_reference_kind": mojom.IsReferenceKind, 621 "is_reference_kind": mojom.IsReferenceKind,
570 "is_string_kind": mojom.IsStringKind, 622 "is_string_kind": mojom.IsStringKind,
571 "is_struct_kind": mojom.IsStructKind, 623 "is_struct_kind": mojom.IsStructKind,
572 "is_typemapped_kind": IsTypemappedKind, 624 "is_typemapped_kind": IsTypemappedKind,
573 "is_union_kind": mojom.IsUnionKind, 625 "is_union_kind": mojom.IsUnionKind,
574 "passes_associated_kinds": mojom.PassesAssociatedKinds, 626 "passes_associated_kinds": mojom.PassesAssociatedKinds,
627 "struct_constructors": GetStructConstructors,
575 "stylize_method": generator.StudlyCapsToCamel, 628 "stylize_method": generator.StudlyCapsToCamel,
576 "under_to_camel": generator.UnderToCamel, 629 "under_to_camel": generator.UnderToCamel,
577 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, 630 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer,
578 } 631 }
579 632
580 def GetExtraTraitsHeaders(self): 633 def GetExtraTraitsHeaders(self):
581 extra_headers = set() 634 extra_headers = set()
582 for typemap in self._GetAllUsedTypemaps(): 635 for typemap in self._GetAllUsedTypemaps():
583 extra_headers.update(typemap.get("traits_headers", [])) 636 extra_headers.update(typemap.get("traits_headers", []))
584 return sorted(extra_headers) 637 return sorted(extra_headers)
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 global _use_once_callback 782 global _use_once_callback
730 _use_once_callback = self.use_once_callback 783 _use_once_callback = self.use_once_callback
731 global _variant 784 global _variant
732 _variant = self.variant 785 _variant = self.variant
733 suffix = "-%s" % self.variant if self.variant else "" 786 suffix = "-%s" % self.variant if self.variant else ""
734 self.Write(self.GenerateModuleHeader(), 787 self.Write(self.GenerateModuleHeader(),
735 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 788 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
736 self.Write( 789 self.Write(
737 self.GenerateModuleSource(), 790 self.GenerateModuleSource(),
738 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 791 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_definition.tmpl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698