OLD | NEW |
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 23 matching lines...) Expand all Loading... |
34 | 34 |
35 | 35 |
36 class _NameFormatter(object): | 36 class _NameFormatter(object): |
37 """A formatter for the names of kinds or values.""" | 37 """A formatter for the names of kinds or values.""" |
38 | 38 |
39 def __init__(self, token, variant): | 39 def __init__(self, token, variant): |
40 self._token = token | 40 self._token = token |
41 self._variant = variant | 41 self._variant = variant |
42 | 42 |
43 def Format(self, separator, prefixed=False, internal=False, | 43 def Format(self, separator, prefixed=False, internal=False, |
44 include_variant=False, add_same_module_namespaces=False, | 44 include_variant=False, omit_namespace_for_module=None, |
45 flatten_nested_kind=False): | 45 flatten_nested_kind=False): |
46 """Formats the name according to the given configuration. | 46 """Formats the name according to the given configuration. |
47 | 47 |
48 Args: | 48 Args: |
49 separator: Separator between different parts of the name. | 49 separator: Separator between different parts of the name. |
50 prefixed: Whether a leading separator should be added. | 50 prefixed: Whether a leading separator should be added. |
51 internal: Returns the name in the "internal" namespace. | 51 internal: Returns the name in the "internal" namespace. |
52 include_variant: Whether to include variant as namespace. If |internal| is | 52 include_variant: Whether to include variant as namespace. If |internal| is |
53 True, then this flag is ignored and variant is not included. | 53 True, then this flag is ignored and variant is not included. |
54 add_same_module_namespaces: Includes all namespaces even if the token is | 54 omit_namespace_for_module: If the token is from the specified module, |
55 from the same module as the current mojom file. | 55 don't add the namespaces of the module to the name. |
56 flatten_nested_kind: It is allowed to define enums inside structs and | 56 flatten_nested_kind: It is allowed to define enums inside structs and |
57 interfaces. If this flag is set to True, this method concatenates the | 57 interfaces. If this flag is set to True, this method concatenates the |
58 parent kind and the nested kind with '_', instead of treating the | 58 parent kind and the nested kind with '_', instead of treating the |
59 parent kind as a scope.""" | 59 parent kind as a scope.""" |
60 | 60 |
61 parts = [] | 61 parts = [] |
62 if self._ShouldIncludeNamespace(add_same_module_namespaces): | 62 if self._ShouldIncludeNamespace(omit_namespace_for_module): |
63 if prefixed: | 63 if prefixed: |
64 parts.append("") | 64 parts.append("") |
65 parts.extend(self._GetNamespace()) | 65 parts.extend(self._GetNamespace()) |
66 if include_variant and self._variant and not internal: | 66 if include_variant and self._variant and not internal: |
67 parts.append(self._variant) | 67 parts.append(self._variant) |
68 parts.extend(self._GetName(internal, flatten_nested_kind)) | 68 parts.extend(self._GetName(internal, flatten_nested_kind)) |
69 return separator.join(parts) | 69 return separator.join(parts) |
70 | 70 |
71 def FormatForCpp(self, add_same_module_namespaces=False, internal=False, | 71 def FormatForCpp(self, omit_namespace_for_module=None, internal=False, |
72 flatten_nested_kind=False): | 72 flatten_nested_kind=False): |
73 return self.Format( | 73 return self.Format( |
74 "::", prefixed=True, | 74 "::", prefixed=True, |
75 add_same_module_namespaces=add_same_module_namespaces, | 75 omit_namespace_for_module=omit_namespace_for_module, |
76 internal=internal, include_variant=True, | 76 internal=internal, include_variant=True, |
77 flatten_nested_kind=flatten_nested_kind) | 77 flatten_nested_kind=flatten_nested_kind) |
78 | 78 |
79 def FormatForMojom(self): | 79 def FormatForMojom(self): |
80 return self.Format(".", add_same_module_namespaces=True) | 80 return self.Format(".") |
81 | 81 |
82 def _MapKindName(self, token, internal): | 82 def _MapKindName(self, token, internal): |
83 if not internal: | 83 if not internal: |
84 return token.name | 84 return token.name |
85 if (mojom.IsStructKind(token) or mojom.IsUnionKind(token) or | 85 if (mojom.IsStructKind(token) or mojom.IsUnionKind(token) or |
86 mojom.IsEnumKind(token)): | 86 mojom.IsEnumKind(token)): |
87 return token.name + "_Data" | 87 return token.name + "_Data" |
88 return token.name | 88 return token.name |
89 | 89 |
90 def _GetName(self, internal, flatten_nested_kind): | 90 def _GetName(self, internal, flatten_nested_kind): |
(...skipping 12 matching lines...) Expand all Loading... |
103 name = "%s_%s" % (self._token.parent_kind.name, | 103 name = "%s_%s" % (self._token.parent_kind.name, |
104 self._MapKindName(self._token, internal)) | 104 self._MapKindName(self._token, internal)) |
105 name_parts.append(name) | 105 name_parts.append(name) |
106 return name_parts | 106 return name_parts |
107 | 107 |
108 if self._token.parent_kind: | 108 if self._token.parent_kind: |
109 name_parts.append(self._MapKindName(self._token.parent_kind, internal)) | 109 name_parts.append(self._MapKindName(self._token.parent_kind, internal)) |
110 name_parts.append(self._MapKindName(self._token, internal)) | 110 name_parts.append(self._MapKindName(self._token, internal)) |
111 return name_parts | 111 return name_parts |
112 | 112 |
113 def _ShouldIncludeNamespace(self, add_same_module_namespaces): | 113 def _ShouldIncludeNamespace(self, omit_namespace_for_module): |
114 return add_same_module_namespaces or self._token.imported_from | 114 return self._token.module and ( |
| 115 not omit_namespace_for_module or |
| 116 self._token.module.path != omit_namespace_for_module.path) |
115 | 117 |
116 def _GetNamespace(self): | 118 def _GetNamespace(self): |
117 if self._token.imported_from: | 119 if self._token.module: |
118 return NamespaceToArray(self._token.imported_from["namespace"]) | |
119 elif hasattr(self._token, "module"): | |
120 return NamespaceToArray(self._token.module.namespace) | 120 return NamespaceToArray(self._token.module.namespace) |
121 return [] | |
122 | 121 |
123 | 122 |
124 def NamespaceToArray(namespace): | 123 def NamespaceToArray(namespace): |
125 return namespace.split(".") if namespace else [] | 124 return namespace.split(".") if namespace else [] |
126 | 125 |
127 | 126 |
128 def GetWtfHashFnNameForEnum(enum): | 127 def GetWtfHashFnNameForEnum(enum): |
129 return _NameFormatter( | 128 return _NameFormatter(enum, None).Format("_", internal=True, |
130 enum, None).Format("_", internal=True, add_same_module_namespaces=True, | 129 flatten_nested_kind=True) + "HashFn" |
131 flatten_nested_kind=True) + "HashFn" | |
132 | 130 |
133 | 131 |
134 def IsNativeOnlyKind(kind): | 132 def IsNativeOnlyKind(kind): |
135 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 133 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
136 kind.native_only | 134 kind.native_only |
137 | 135 |
138 | 136 |
139 def AllEnumValues(enum): | 137 def AllEnumValues(enum): |
140 """Return all enum values associated with an enum. | 138 """Return all enum values associated with an enum. |
141 | 139 |
142 Args: | 140 Args: |
143 enum: {mojom.Enum} The enum type. | 141 enum: {mojom.Enum} The enum type. |
144 | 142 |
145 Returns: | 143 Returns: |
146 {Set[int]} The values. | 144 {Set[int]} The values. |
147 """ | 145 """ |
148 return set(field.numeric_value for field in enum.fields) | 146 return set(field.numeric_value for field in enum.fields) |
149 | 147 |
150 | 148 |
151 def GetCppPodType(kind): | 149 def GetCppPodType(kind): |
152 return _kind_to_cpp_type[kind] | 150 return _kind_to_cpp_type[kind] |
153 | 151 |
154 | 152 |
155 def GetCppDataViewType(kind, qualified=False): | |
156 def _GetName(input_kind): | |
157 return _NameFormatter(input_kind, None).FormatForCpp( | |
158 add_same_module_namespaces=qualified, flatten_nested_kind=True) | |
159 | |
160 if mojom.IsEnumKind(kind): | |
161 return _GetName(kind) | |
162 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | |
163 return "%sDataView" % _GetName(kind) | |
164 if mojom.IsArrayKind(kind): | |
165 return "mojo::ArrayDataView<%s>" % GetCppDataViewType(kind.kind, qualified) | |
166 if mojom.IsMapKind(kind): | |
167 return ("mojo::MapDataView<%s, %s>" % ( | |
168 GetCppDataViewType(kind.key_kind, qualified), | |
169 GetCppDataViewType(kind.value_kind, qualified))) | |
170 if mojom.IsStringKind(kind): | |
171 return "mojo::StringDataView" | |
172 if mojom.IsInterfaceKind(kind): | |
173 return "%sPtrDataView" % _GetName(kind) | |
174 if mojom.IsInterfaceRequestKind(kind): | |
175 return "%sRequestDataView" % _GetName(kind.kind) | |
176 if mojom.IsAssociatedInterfaceKind(kind): | |
177 return "%sAssociatedPtrInfoDataView" % _GetName(kind.kind) | |
178 if mojom.IsAssociatedInterfaceRequestKind(kind): | |
179 return "%sAssociatedRequestDataView" % _GetName(kind.kind) | |
180 if mojom.IsGenericHandleKind(kind): | |
181 return "mojo::ScopedHandle" | |
182 if mojom.IsDataPipeConsumerKind(kind): | |
183 return "mojo::ScopedDataPipeConsumerHandle" | |
184 if mojom.IsDataPipeProducerKind(kind): | |
185 return "mojo::ScopedDataPipeProducerHandle" | |
186 if mojom.IsMessagePipeKind(kind): | |
187 return "mojo::ScopedMessagePipeHandle" | |
188 if mojom.IsSharedBufferKind(kind): | |
189 return "mojo::ScopedSharedBufferHandle" | |
190 return _kind_to_cpp_type[kind] | |
191 | |
192 | |
193 def GetUnmappedTypeForSerializer(kind): | |
194 return GetCppDataViewType(kind, qualified=True) | |
195 | |
196 | |
197 def RequiresContextForDataView(kind): | 153 def RequiresContextForDataView(kind): |
198 for field in kind.fields: | 154 for field in kind.fields: |
199 if mojom.IsReferenceKind(field.kind): | 155 if mojom.IsReferenceKind(field.kind): |
200 return True | 156 return True |
201 return False | 157 return False |
202 | 158 |
203 | 159 |
204 def ShouldInlineStruct(struct): | 160 def ShouldInlineStruct(struct): |
205 # TODO(darin): Base this on the size of the wrapper class. | 161 # TODO(darin): Base this on the size of the wrapper class. |
206 if len(struct.fields) > 4: | 162 if len(struct.fields) > 4: |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 def GetTemplatePrefix(): | 310 def GetTemplatePrefix(): |
355 return "cpp_templates" | 311 return "cpp_templates" |
356 | 312 |
357 def GetFilters(self): | 313 def GetFilters(self): |
358 cpp_filters = { | 314 cpp_filters = { |
359 "all_enum_values": AllEnumValues, | 315 "all_enum_values": AllEnumValues, |
360 "constant_value": self._ConstantValue, | 316 "constant_value": self._ConstantValue, |
361 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, | 317 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, |
362 "contains_move_only_members": self._ContainsMoveOnlyMembers, | 318 "contains_move_only_members": self._ContainsMoveOnlyMembers, |
363 "cpp_wrapper_param_type": self._GetCppWrapperParamType, | 319 "cpp_wrapper_param_type": self._GetCppWrapperParamType, |
364 "cpp_data_view_type": GetCppDataViewType, | 320 "cpp_data_view_type": self._GetCppDataViewType, |
365 "cpp_field_type": self._GetCppFieldType, | 321 "cpp_field_type": self._GetCppFieldType, |
366 "cpp_union_field_type": self._GetCppUnionFieldType, | 322 "cpp_union_field_type": self._GetCppUnionFieldType, |
367 "cpp_pod_type": GetCppPodType, | 323 "cpp_pod_type": GetCppPodType, |
368 "cpp_union_getter_return_type": self._GetUnionGetterReturnType, | 324 "cpp_union_getter_return_type": self._GetUnionGetterReturnType, |
369 "cpp_union_trait_getter_return_type": self._GetUnionTraitGetterReturnType, | 325 "cpp_union_trait_getter_return_type": self._GetUnionTraitGetterReturnType, |
370 "cpp_wrapper_type": self._GetCppWrapperType, | 326 "cpp_wrapper_type": self._GetCppWrapperType, |
371 "default_value": self._DefaultValue, | 327 "default_value": self._DefaultValue, |
372 "expression_to_text": self._ExpressionToText, | 328 "expression_to_text": self._ExpressionToText, |
373 "format_constant_declaration": self._FormatConstantDeclaration, | 329 "format_constant_declaration": self._FormatConstantDeclaration, |
374 "get_container_validate_params_ctor_args": | 330 "get_container_validate_params_ctor_args": |
(...skipping 19 matching lines...) Expand all Loading... |
394 "is_nullable_kind": mojom.IsNullableKind, | 350 "is_nullable_kind": mojom.IsNullableKind, |
395 "is_object_kind": mojom.IsObjectKind, | 351 "is_object_kind": mojom.IsObjectKind, |
396 "is_reference_kind": mojom.IsReferenceKind, | 352 "is_reference_kind": mojom.IsReferenceKind, |
397 "is_string_kind": mojom.IsStringKind, | 353 "is_string_kind": mojom.IsStringKind, |
398 "is_struct_kind": mojom.IsStructKind, | 354 "is_struct_kind": mojom.IsStructKind, |
399 "is_typemapped_kind": self._IsTypemappedKind, | 355 "is_typemapped_kind": self._IsTypemappedKind, |
400 "is_union_kind": mojom.IsUnionKind, | 356 "is_union_kind": mojom.IsUnionKind, |
401 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 357 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
402 "struct_constructors": self._GetStructConstructors, | 358 "struct_constructors": self._GetStructConstructors, |
403 "under_to_camel": generator.UnderToCamel, | 359 "under_to_camel": generator.UnderToCamel, |
404 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, | 360 "unmapped_type_for_serializer": self._GetUnmappedTypeForSerializer, |
405 "wtf_hash_fn_name_for_enum": GetWtfHashFnNameForEnum, | 361 "wtf_hash_fn_name_for_enum": GetWtfHashFnNameForEnum, |
406 } | 362 } |
407 return cpp_filters | 363 return cpp_filters |
408 | 364 |
409 @UseJinja("module.h.tmpl") | 365 @UseJinja("module.h.tmpl") |
410 def _GenerateModuleHeader(self): | 366 def _GenerateModuleHeader(self): |
411 return self._GetJinjaExports() | 367 return self._GetJinjaExports() |
412 | 368 |
413 @UseJinja("module.cc.tmpl") | 369 @UseJinja("module.cc.tmpl") |
414 def _GenerateModuleSource(self): | 370 def _GenerateModuleSource(self): |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 assert field.default == "default" | 410 assert field.default == "default" |
455 if not self._IsTypemappedKind(field.kind): | 411 if not self._IsTypemappedKind(field.kind): |
456 return "%s::New()" % self._GetNameForKind(field.kind) | 412 return "%s::New()" % self._GetNameForKind(field.kind) |
457 return self._ExpressionToText(field.default, kind=field.kind) | 413 return self._ExpressionToText(field.default, kind=field.kind) |
458 return "" | 414 return "" |
459 | 415 |
460 def _GetNameForKind(self, kind, internal=False, flatten_nested_kind=False, | 416 def _GetNameForKind(self, kind, internal=False, flatten_nested_kind=False, |
461 add_same_module_namespaces=False): | 417 add_same_module_namespaces=False): |
462 return _NameFormatter(kind, self.variant).FormatForCpp( | 418 return _NameFormatter(kind, self.variant).FormatForCpp( |
463 internal=internal, flatten_nested_kind=flatten_nested_kind, | 419 internal=internal, flatten_nested_kind=flatten_nested_kind, |
464 add_same_module_namespaces=add_same_module_namespaces) | 420 omit_namespace_for_module = (None if add_same_module_namespaces |
| 421 else self.module)) |
465 | 422 |
466 def _GetQualifiedNameForKind(self, kind, internal=False, | 423 def _GetQualifiedNameForKind(self, kind, internal=False, |
467 flatten_nested_kind=False, include_variant=True): | 424 flatten_nested_kind=False, include_variant=True): |
468 return _NameFormatter( | 425 return _NameFormatter( |
469 kind, self.variant if include_variant else None).FormatForCpp( | 426 kind, self.variant if include_variant else None).FormatForCpp( |
470 internal=internal, add_same_module_namespaces=True, | 427 internal=internal, flatten_nested_kind=flatten_nested_kind) |
471 flatten_nested_kind=flatten_nested_kind) | |
472 | 428 |
473 def _GetFullMojomNameForKind(self, kind): | 429 def _GetFullMojomNameForKind(self, kind): |
474 return _NameFormatter(kind, self.variant).FormatForMojom() | 430 return _NameFormatter(kind, self.variant).FormatForMojom() |
475 | 431 |
476 def _IsTypemappedKind(self, kind): | 432 def _IsTypemappedKind(self, kind): |
477 return hasattr(kind, "name") and \ | 433 return hasattr(kind, "name") and \ |
478 self._GetFullMojomNameForKind(kind) in self.typemap | 434 self._GetFullMojomNameForKind(kind) in self.typemap |
479 | 435 |
480 def _IsHashableKind(self, kind): | 436 def _IsHashableKind(self, kind): |
481 """Check if the kind can be hashed. | 437 """Check if the kind can be hashed. |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
799 else: | 755 else: |
800 return "%d, %s" % (expected_num_elements, enum_validate_func) | 756 return "%d, %s" % (expected_num_elements, enum_validate_func) |
801 | 757 |
802 def _GetNewContainerValidateParams(self, kind): | 758 def _GetNewContainerValidateParams(self, kind): |
803 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and | 759 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and |
804 not mojom.IsStringKind(kind)): | 760 not mojom.IsStringKind(kind)): |
805 return "nullptr" | 761 return "nullptr" |
806 | 762 |
807 return "new mojo::internal::ContainerValidateParams(%s)" % ( | 763 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
808 self._GetContainerValidateParamsCtorArgs(kind)) | 764 self._GetContainerValidateParamsCtorArgs(kind)) |
| 765 |
| 766 def _GetCppDataViewType(self, kind, qualified=False): |
| 767 def _GetName(input_kind): |
| 768 return _NameFormatter(input_kind, None).FormatForCpp( |
| 769 omit_namespace_for_module=(None if qualified else self.module), |
| 770 flatten_nested_kind=True) |
| 771 |
| 772 if mojom.IsEnumKind(kind): |
| 773 return _GetName(kind) |
| 774 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
| 775 return "%sDataView" % _GetName(kind) |
| 776 if mojom.IsArrayKind(kind): |
| 777 return "mojo::ArrayDataView<%s>" % ( |
| 778 self._GetCppDataViewType(kind.kind, qualified)) |
| 779 if mojom.IsMapKind(kind): |
| 780 return ("mojo::MapDataView<%s, %s>" % ( |
| 781 self._GetCppDataViewType(kind.key_kind, qualified), |
| 782 self._GetCppDataViewType(kind.value_kind, qualified))) |
| 783 if mojom.IsStringKind(kind): |
| 784 return "mojo::StringDataView" |
| 785 if mojom.IsInterfaceKind(kind): |
| 786 return "%sPtrDataView" % _GetName(kind) |
| 787 if mojom.IsInterfaceRequestKind(kind): |
| 788 return "%sRequestDataView" % _GetName(kind.kind) |
| 789 if mojom.IsAssociatedInterfaceKind(kind): |
| 790 return "%sAssociatedPtrInfoDataView" % _GetName(kind.kind) |
| 791 if mojom.IsAssociatedInterfaceRequestKind(kind): |
| 792 return "%sAssociatedRequestDataView" % _GetName(kind.kind) |
| 793 if mojom.IsGenericHandleKind(kind): |
| 794 return "mojo::ScopedHandle" |
| 795 if mojom.IsDataPipeConsumerKind(kind): |
| 796 return "mojo::ScopedDataPipeConsumerHandle" |
| 797 if mojom.IsDataPipeProducerKind(kind): |
| 798 return "mojo::ScopedDataPipeProducerHandle" |
| 799 if mojom.IsMessagePipeKind(kind): |
| 800 return "mojo::ScopedMessagePipeHandle" |
| 801 if mojom.IsSharedBufferKind(kind): |
| 802 return "mojo::ScopedSharedBufferHandle" |
| 803 return _kind_to_cpp_type[kind] |
| 804 |
| 805 def _GetUnmappedTypeForSerializer(self, kind): |
| 806 return self._GetCppDataViewType(kind, qualified=True) |
OLD | NEW |