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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 if not _use_new_wrapper_types: | 145 if not _use_new_wrapper_types: |
146 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): | 146 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): |
147 return "nullptr"; | 147 return "nullptr"; |
148 if mojom.IsStringKind(field.kind): | 148 if mojom.IsStringKind(field.kind): |
149 return "" if _for_blink else "nullptr" | 149 return "" if _for_blink else "nullptr" |
150 return "" | 150 return "" |
151 | 151 |
152 def NamespaceToArray(namespace): | 152 def NamespaceToArray(namespace): |
153 return namespace.split(".") if namespace else [] | 153 return namespace.split(".") if namespace else [] |
154 | 154 |
155 def GetNameForKind(kind, internal=False, flatten_nested_kind=False): | 155 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, |
| 156 add_same_module_namespaces=False): |
156 return _NameFormatter(kind, _variant).FormatForCpp( | 157 return _NameFormatter(kind, _variant).FormatForCpp( |
157 internal=internal, flatten_nested_kind=flatten_nested_kind) | 158 internal=internal, flatten_nested_kind=flatten_nested_kind, |
| 159 add_same_module_namespaces=add_same_module_namespaces) |
158 | 160 |
159 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False): | 161 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False): |
160 return _NameFormatter(kind, _variant).FormatForCpp( | 162 return _NameFormatter(kind, _variant).FormatForCpp( |
161 internal=internal, add_same_module_namespaces=True, | 163 internal=internal, add_same_module_namespaces=True, |
162 flatten_nested_kind=flatten_nested_kind) | 164 flatten_nested_kind=flatten_nested_kind) |
163 | 165 |
164 def GetFullMojomNameForKind(kind): | 166 def GetFullMojomNameForKind(kind): |
165 return _NameFormatter(kind, _variant).FormatForMojom() | 167 return _NameFormatter(kind, _variant).FormatForMojom() |
166 | 168 |
167 def IsTypemappedKind(kind): | 169 def IsTypemappedKind(kind): |
168 return hasattr(kind, "name") and \ | 170 return hasattr(kind, "name") and \ |
169 GetFullMojomNameForKind(kind) in _current_typemap | 171 GetFullMojomNameForKind(kind) in _current_typemap |
170 | 172 |
171 def IsNativeOnlyKind(kind): | 173 def IsNativeOnlyKind(kind): |
172 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 174 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
173 kind.native_only | 175 kind.native_only |
174 | 176 |
| 177 |
| 178 def IsHashableKind(kind): |
| 179 """Check if the kind can be hashed. |
| 180 |
| 181 Args: |
| 182 kind: {Kind} The kind to check. |
| 183 |
| 184 Returns: |
| 185 {bool} True if a value of this kind can be hashed. |
| 186 """ |
| 187 checked = set() |
| 188 def Check(kind): |
| 189 if kind.spec in checked: |
| 190 return True |
| 191 checked.add(kind.spec) |
| 192 if mojom.IsNullableKind(kind): |
| 193 return False |
| 194 elif mojom.IsStructKind(kind): |
| 195 if (IsTypemappedKind(kind) and |
| 196 not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]): |
| 197 return False |
| 198 return all(Check(field.kind) for field in kind.fields) |
| 199 elif mojom.IsUnionKind(kind): |
| 200 return all(Check(field.kind) for field in kind.fields) |
| 201 elif mojom.IsAnyHandleKind(kind): |
| 202 return False |
| 203 elif mojom.IsAnyInterfaceKind(kind): |
| 204 return False |
| 205 # TODO(tibell): Arrays and maps could be made hashable. We just don't have a |
| 206 # use case yet. |
| 207 elif mojom.IsArrayKind(kind): |
| 208 return False |
| 209 elif mojom.IsMapKind(kind): |
| 210 return False |
| 211 else: |
| 212 return True |
| 213 return Check(kind) |
| 214 |
| 215 |
175 def GetNativeTypeName(typemapped_kind): | 216 def GetNativeTypeName(typemapped_kind): |
176 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 217 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
177 | 218 |
178 def GetCppPodType(kind): | 219 def GetCppPodType(kind): |
179 if mojom.IsStringKind(kind): | 220 if mojom.IsStringKind(kind): |
180 return "char*" | 221 return "char*" |
181 return _kind_to_cpp_type[kind] | 222 return _kind_to_cpp_type[kind] |
182 | 223 |
183 def GetCppWrapperType(kind): | 224 def GetCppWrapperType(kind, add_same_module_namespaces=False): |
184 def _AddOptional(type_name): | 225 def _AddOptional(type_name): |
185 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>" | 226 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>" |
186 return pattern % type_name | 227 return pattern % type_name |
187 | 228 |
188 if IsTypemappedKind(kind): | 229 if IsTypemappedKind(kind): |
189 type_name = GetNativeTypeName(kind) | 230 type_name = GetNativeTypeName(kind) |
190 if (mojom.IsNullableKind(kind) and | 231 if (mojom.IsNullableKind(kind) and |
191 not _current_typemap[GetFullMojomNameForKind(kind)][ | 232 not _current_typemap[GetFullMojomNameForKind(kind)][ |
192 "nullable_is_same_type"]): | 233 "nullable_is_same_type"]): |
193 type_name = _AddOptional(type_name) | 234 type_name = _AddOptional(type_name) |
194 return type_name | 235 return type_name |
195 if mojom.IsEnumKind(kind): | 236 if mojom.IsEnumKind(kind): |
196 return GetNameForKind(kind) | 237 return GetNameForKind( |
| 238 kind, add_same_module_namespaces=add_same_module_namespaces) |
197 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 239 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
198 return "%sPtr" % GetNameForKind(kind) | 240 return "%sPtr" % GetNameForKind( |
| 241 kind, add_same_module_namespaces=add_same_module_namespaces) |
199 if mojom.IsArrayKind(kind): | 242 if mojom.IsArrayKind(kind): |
200 pattern = None | 243 pattern = None |
201 if _use_new_wrapper_types: | 244 if _use_new_wrapper_types: |
202 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" | 245 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" |
203 if mojom.IsNullableKind(kind): | 246 if mojom.IsNullableKind(kind): |
204 pattern = _AddOptional(pattern) | 247 pattern = _AddOptional(pattern) |
205 else: | 248 else: |
206 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" | 249 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" |
207 return pattern % GetCppWrapperType(kind.kind) | 250 return pattern % GetCppWrapperType( |
| 251 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
208 if mojom.IsMapKind(kind): | 252 if mojom.IsMapKind(kind): |
209 pattern = None | 253 pattern = None |
210 if _use_new_wrapper_types: | 254 if _use_new_wrapper_types: |
211 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else | 255 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else |
212 "std::unordered_map<%s, %s>") | 256 "std::unordered_map<%s, %s>") |
213 if mojom.IsNullableKind(kind): | 257 if mojom.IsNullableKind(kind): |
214 pattern = _AddOptional(pattern) | 258 pattern = _AddOptional(pattern) |
215 else: | 259 else: |
216 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" | 260 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" |
217 return pattern % (GetCppWrapperType(kind.key_kind), | 261 return pattern % ( |
218 GetCppWrapperType(kind.value_kind)) | 262 GetCppWrapperType( |
| 263 kind.key_kind, |
| 264 add_same_module_namespaces=add_same_module_namespaces), |
| 265 GetCppWrapperType( |
| 266 kind.value_kind, |
| 267 add_same_module_namespaces=add_same_module_namespaces)) |
219 if mojom.IsInterfaceKind(kind): | 268 if mojom.IsInterfaceKind(kind): |
220 return "%sPtr" % GetNameForKind(kind) | 269 return "%sPtr" % GetNameForKind( |
| 270 kind, add_same_module_namespaces=add_same_module_namespaces) |
221 if mojom.IsInterfaceRequestKind(kind): | 271 if mojom.IsInterfaceRequestKind(kind): |
222 return "%sRequest" % GetNameForKind(kind.kind) | 272 return "%sRequest" % GetNameForKind( |
| 273 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
223 if mojom.IsAssociatedInterfaceKind(kind): | 274 if mojom.IsAssociatedInterfaceKind(kind): |
224 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) | 275 return "%sAssociatedPtrInfo" % GetNameForKind( |
| 276 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
225 if mojom.IsAssociatedInterfaceRequestKind(kind): | 277 if mojom.IsAssociatedInterfaceRequestKind(kind): |
226 return "%sAssociatedRequest" % GetNameForKind(kind.kind) | 278 return "%sAssociatedRequest" % GetNameForKind( |
| 279 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
227 if mojom.IsStringKind(kind): | 280 if mojom.IsStringKind(kind): |
228 if _for_blink: | 281 if _for_blink: |
229 return "WTF::String" | 282 return "WTF::String" |
230 if not _use_new_wrapper_types: | 283 if not _use_new_wrapper_types: |
231 return "mojo::String" | 284 return "mojo::String" |
232 type_name = "std::string" | 285 type_name = "std::string" |
233 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name | 286 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name |
234 if mojom.IsGenericHandleKind(kind): | 287 if mojom.IsGenericHandleKind(kind): |
235 return "mojo::ScopedHandle" | 288 return "mojo::ScopedHandle" |
236 if mojom.IsDataPipeConsumerKind(kind): | 289 if mojom.IsDataPipeConsumerKind(kind): |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 def GetCppUnionFieldType(kind): | 359 def GetCppUnionFieldType(kind): |
307 if mojom.IsUnionKind(kind): | 360 if mojom.IsUnionKind(kind): |
308 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) | 361 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) |
309 return GetCppFieldType(kind) | 362 return GetCppFieldType(kind) |
310 | 363 |
311 def GetUnionGetterReturnType(kind): | 364 def GetUnionGetterReturnType(kind): |
312 if mojom.IsReferenceKind(kind): | 365 if mojom.IsReferenceKind(kind): |
313 return "%s&" % GetCppWrapperType(kind) | 366 return "%s&" % GetCppWrapperType(kind) |
314 return GetCppWrapperType(kind) | 367 return GetCppWrapperType(kind) |
315 | 368 |
| 369 def GetUnionTraitGetterReturnType(kind): |
| 370 """Get field type used in UnionTraits template specialization. |
| 371 |
| 372 The type may be qualified as UnionTraits specializations live outside the |
| 373 namespace where e.g. structs are defined. |
| 374 |
| 375 Args: |
| 376 kind: {Kind} The type of the field. |
| 377 |
| 378 Returns: |
| 379 {str} The C++ type to use for the field. |
| 380 """ |
| 381 if mojom.IsReferenceKind(kind): |
| 382 return "%s&" % GetCppWrapperType(kind, add_same_module_namespaces=True) |
| 383 return GetCppWrapperType(kind, add_same_module_namespaces=True) |
| 384 |
316 def GetCppDataViewType(kind, qualified=False): | 385 def GetCppDataViewType(kind, qualified=False): |
317 def _GetName(input_kind): | 386 def _GetName(input_kind): |
318 return _NameFormatter(input_kind, None).FormatForCpp( | 387 return _NameFormatter(input_kind, None).FormatForCpp( |
319 add_same_module_namespaces=qualified, flatten_nested_kind=True) | 388 add_same_module_namespaces=qualified, flatten_nested_kind=True) |
320 | 389 |
321 if mojom.IsEnumKind(kind): | 390 if mojom.IsEnumKind(kind): |
322 return _GetName(kind) | 391 return _GetName(kind) |
323 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 392 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
324 return "%sDataView" % _GetName(kind) | 393 return "%sDataView" % _GetName(kind) |
325 if mojom.IsArrayKind(kind): | 394 if mojom.IsArrayKind(kind): |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 not mojom.IsStringKind(kind)): | 527 not mojom.IsStringKind(kind)): |
459 return "nullptr" | 528 return "nullptr" |
460 | 529 |
461 return "new mojo::internal::ContainerValidateParams(%s)" % ( | 530 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
462 GetContainerValidateParamsCtorArgs(kind)) | 531 GetContainerValidateParamsCtorArgs(kind)) |
463 | 532 |
464 class Generator(generator.Generator): | 533 class Generator(generator.Generator): |
465 | 534 |
466 cpp_filters = { | 535 cpp_filters = { |
467 "constant_value": ConstantValue, | 536 "constant_value": ConstantValue, |
| 537 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, |
468 "contains_move_only_members": ContainsMoveOnlyMembers, | 538 "contains_move_only_members": ContainsMoveOnlyMembers, |
469 "cpp_wrapper_param_type": GetCppWrapperParamType, | 539 "cpp_wrapper_param_type": GetCppWrapperParamType, |
470 "cpp_data_view_type": GetCppDataViewType, | 540 "cpp_data_view_type": GetCppDataViewType, |
471 "cpp_field_type": GetCppFieldType, | 541 "cpp_field_type": GetCppFieldType, |
472 "cpp_union_field_type": GetCppUnionFieldType, | 542 "cpp_union_field_type": GetCppUnionFieldType, |
473 "cpp_pod_type": GetCppPodType, | 543 "cpp_pod_type": GetCppPodType, |
474 "cpp_union_getter_return_type": GetUnionGetterReturnType, | 544 "cpp_union_getter_return_type": GetUnionGetterReturnType, |
| 545 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, |
475 "cpp_wrapper_type": GetCppWrapperType, | 546 "cpp_wrapper_type": GetCppWrapperType, |
476 "default_value": DefaultValue, | 547 "default_value": DefaultValue, |
477 "expression_to_text": ExpressionToText, | 548 "expression_to_text": ExpressionToText, |
478 "get_container_validate_params_ctor_args": | 549 "get_container_validate_params_ctor_args": |
479 GetContainerValidateParamsCtorArgs, | 550 GetContainerValidateParamsCtorArgs, |
480 "get_name_for_kind": GetNameForKind, | 551 "get_name_for_kind": GetNameForKind, |
481 "get_pad": pack.GetPad, | 552 "get_pad": pack.GetPad, |
482 "get_qualified_name_for_kind": GetQualifiedNameForKind, | 553 "get_qualified_name_for_kind": GetQualifiedNameForKind, |
483 "has_callbacks": mojom.HasCallbacks, | 554 "has_callbacks": mojom.HasCallbacks, |
484 "has_sync_methods": mojom.HasSyncMethods, | 555 "has_sync_methods": mojom.HasSyncMethods, |
485 "requires_context_for_data_view": RequiresContextForDataView, | 556 "requires_context_for_data_view": RequiresContextForDataView, |
486 "should_inline": ShouldInlineStruct, | 557 "should_inline": ShouldInlineStruct, |
487 "should_inline_union": ShouldInlineUnion, | 558 "should_inline_union": ShouldInlineUnion, |
488 "is_array_kind": mojom.IsArrayKind, | 559 "is_array_kind": mojom.IsArrayKind, |
489 "is_enum_kind": mojom.IsEnumKind, | 560 "is_enum_kind": mojom.IsEnumKind, |
490 "is_integral_kind": mojom.IsIntegralKind, | 561 "is_integral_kind": mojom.IsIntegralKind, |
491 "is_native_only_kind": IsNativeOnlyKind, | 562 "is_native_only_kind": IsNativeOnlyKind, |
492 "is_any_handle_kind": mojom.IsAnyHandleKind, | 563 "is_any_handle_kind": mojom.IsAnyHandleKind, |
493 "is_any_interface_kind": mojom.IsAnyInterfaceKind, | 564 "is_any_interface_kind": mojom.IsAnyInterfaceKind, |
494 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, | 565 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, |
495 "is_associated_kind": mojom.IsAssociatedKind, | 566 "is_associated_kind": mojom.IsAssociatedKind, |
| 567 "is_hashable": IsHashableKind, |
496 "is_map_kind": mojom.IsMapKind, | 568 "is_map_kind": mojom.IsMapKind, |
497 "is_nullable_kind": mojom.IsNullableKind, | 569 "is_nullable_kind": mojom.IsNullableKind, |
498 "is_object_kind": mojom.IsObjectKind, | 570 "is_object_kind": mojom.IsObjectKind, |
| 571 "is_reference_kind": mojom.IsReferenceKind, |
499 "is_string_kind": mojom.IsStringKind, | 572 "is_string_kind": mojom.IsStringKind, |
500 "is_struct_kind": mojom.IsStructKind, | 573 "is_struct_kind": mojom.IsStructKind, |
501 "is_typemapped_kind": IsTypemappedKind, | 574 "is_typemapped_kind": IsTypemappedKind, |
502 "is_union_kind": mojom.IsUnionKind, | 575 "is_union_kind": mojom.IsUnionKind, |
503 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 576 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
504 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 577 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
505 "stylize_method": generator.StudlyCapsToCamel, | 578 "stylize_method": generator.StudlyCapsToCamel, |
506 "under_to_camel": generator.UnderToCamel, | 579 "under_to_camel": generator.UnderToCamel, |
507 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, | 580 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, |
508 } | 581 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 global _use_new_wrapper_types | 666 global _use_new_wrapper_types |
594 _use_new_wrapper_types = self.use_new_wrapper_types | 667 _use_new_wrapper_types = self.use_new_wrapper_types |
595 global _variant | 668 global _variant |
596 _variant = self.variant | 669 _variant = self.variant |
597 suffix = "-%s" % self.variant if self.variant else "" | 670 suffix = "-%s" % self.variant if self.variant else "" |
598 self.Write(self.GenerateModuleHeader(), | 671 self.Write(self.GenerateModuleHeader(), |
599 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 672 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
600 self.Write( | 673 self.Write( |
601 self.GenerateModuleSource(), | 674 self.GenerateModuleSource(), |
602 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 675 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |