| 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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 268 |
| 269 def ShouldPassParamByValue(kind): | 269 def ShouldPassParamByValue(kind): |
| 270 return ((not mojom.IsReferenceKind(kind)) or IsMoveOnlyKind(kind) or | 270 return ((not mojom.IsReferenceKind(kind)) or IsMoveOnlyKind(kind) or |
| 271 IsCopyablePassByValue(kind)) | 271 IsCopyablePassByValue(kind)) |
| 272 | 272 |
| 273 def GetCppWrapperParamType(kind): | 273 def GetCppWrapperParamType(kind): |
| 274 cpp_wrapper_type = GetCppWrapperType(kind) | 274 cpp_wrapper_type = GetCppWrapperType(kind) |
| 275 return (cpp_wrapper_type if ShouldPassParamByValue(kind) | 275 return (cpp_wrapper_type if ShouldPassParamByValue(kind) |
| 276 else "const %s&" % cpp_wrapper_type) | 276 else "const %s&" % cpp_wrapper_type) |
| 277 | 277 |
| 278 def GetCppDataViewType(kind): | |
| 279 if mojom.IsEnumKind(kind): | |
| 280 return GetNameForKind(kind) | |
| 281 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | |
| 282 return "%sDataView" % GetNameForKind(kind) | |
| 283 if mojom.IsArrayKind(kind): | |
| 284 return "mojo::ArrayDataView<%s>" % GetCppDataViewType(kind.kind) | |
| 285 if mojom.IsMapKind(kind): | |
| 286 return ("mojo::MapDataView<%s, %s>" % (GetCppDataViewType(kind.key_kind), | |
| 287 GetCppDataViewType(kind.value_kind))) | |
| 288 if mojom.IsStringKind(kind): | |
| 289 return "mojo::StringDataView" | |
| 290 return GetCppWrapperType(kind) | |
| 291 | |
| 292 def GetCppFieldType(kind): | 278 def GetCppFieldType(kind): |
| 293 if mojom.IsStructKind(kind): | 279 if mojom.IsStructKind(kind): |
| 294 return ("mojo::internal::Pointer<%s>" % | 280 return ("mojo::internal::Pointer<%s>" % |
| 295 GetNameForKind(kind, internal=True)) | 281 GetNameForKind(kind, internal=True)) |
| 296 if mojom.IsUnionKind(kind): | 282 if mojom.IsUnionKind(kind): |
| 297 return "%s" % GetNameForKind(kind, internal=True) | 283 return "%s" % GetNameForKind(kind, internal=True) |
| 298 if mojom.IsArrayKind(kind): | 284 if mojom.IsArrayKind(kind): |
| 299 return ("mojo::internal::Pointer<mojo::internal::Array_Data<%s>>" % | 285 return ("mojo::internal::Pointer<mojo::internal::Array_Data<%s>>" % |
| 300 GetCppFieldType(kind.kind)) | 286 GetCppFieldType(kind.kind)) |
| 301 if mojom.IsMapKind(kind): | 287 if mojom.IsMapKind(kind): |
| (...skipping 18 matching lines...) Expand all Loading... |
| 320 def GetCppUnionFieldType(kind): | 306 def GetCppUnionFieldType(kind): |
| 321 if mojom.IsUnionKind(kind): | 307 if mojom.IsUnionKind(kind): |
| 322 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) | 308 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) |
| 323 return GetCppFieldType(kind) | 309 return GetCppFieldType(kind) |
| 324 | 310 |
| 325 def GetUnionGetterReturnType(kind): | 311 def GetUnionGetterReturnType(kind): |
| 326 if mojom.IsReferenceKind(kind): | 312 if mojom.IsReferenceKind(kind): |
| 327 return "%s&" % GetCppWrapperType(kind) | 313 return "%s&" % GetCppWrapperType(kind) |
| 328 return GetCppWrapperType(kind) | 314 return GetCppWrapperType(kind) |
| 329 | 315 |
| 330 def GetUnmappedTypeForSerializer(kind): | 316 def GetCppDataViewType(kind, qualified=False): |
| 317 def _GetName(input_kind): |
| 318 return _NameFormatter(input_kind, None).FormatForCpp( |
| 319 add_same_module_namespaces=qualified, flatten_nested_kind=True) |
| 320 |
| 331 if mojom.IsEnumKind(kind): | 321 if mojom.IsEnumKind(kind): |
| 332 return GetQualifiedNameForKind(kind) | 322 return _GetName(kind) |
| 333 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 323 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
| 334 return "%sPtr" % GetQualifiedNameForKind(kind) | 324 return "%sDataView" % _GetName(kind) |
| 335 if mojom.IsArrayKind(kind): | 325 if mojom.IsArrayKind(kind): |
| 336 return "mojo::Array<%s>" % GetUnmappedTypeForSerializer(kind.kind) | 326 return "mojo::ArrayDataView<%s>" % GetCppDataViewType(kind.kind, qualified) |
| 337 if mojom.IsMapKind(kind): | 327 if mojom.IsMapKind(kind): |
| 338 return "mojo::Map<%s, %s>" % ( | 328 return ("mojo::MapDataView<%s, %s>" % ( |
| 339 GetUnmappedTypeForSerializer(kind.key_kind), | 329 GetCppDataViewType(kind.key_kind, qualified), |
| 340 GetUnmappedTypeForSerializer(kind.value_kind)) | 330 GetCppDataViewType(kind.value_kind, qualified))) |
| 331 if mojom.IsStringKind(kind): |
| 332 return "mojo::StringDataView" |
| 341 if mojom.IsInterfaceKind(kind): | 333 if mojom.IsInterfaceKind(kind): |
| 342 return "%sPtr" % GetQualifiedNameForKind(kind) | 334 return "%sPtrDataView" % _GetName(kind) |
| 343 if mojom.IsInterfaceRequestKind(kind): | 335 if mojom.IsInterfaceRequestKind(kind): |
| 344 return "%sRequest" % GetQualifiedNameForKind(kind.kind) | 336 return "%sRequestDataView" % _GetName(kind.kind) |
| 345 if mojom.IsAssociatedInterfaceKind(kind): | 337 if mojom.IsAssociatedInterfaceKind(kind): |
| 346 return "%sAssociatedPtrInfo" % GetQualifiedNameForKind(kind.kind) | 338 return "%sAssociatedPtrInfoDataView" % _GetName(kind.kind) |
| 347 if mojom.IsAssociatedInterfaceRequestKind(kind): | 339 if mojom.IsAssociatedInterfaceRequestKind(kind): |
| 348 return "%sAssociatedRequest" % GetQualifiedNameForKind(kind.kind) | 340 return "%sAssociatedRequestDataView" % _GetName(kind.kind) |
| 349 if mojom.IsStringKind(kind): | |
| 350 return "mojo::String" | |
| 351 if mojom.IsGenericHandleKind(kind): | 341 if mojom.IsGenericHandleKind(kind): |
| 352 return "mojo::ScopedHandle" | 342 return "mojo::ScopedHandle" |
| 353 if mojom.IsDataPipeConsumerKind(kind): | 343 if mojom.IsDataPipeConsumerKind(kind): |
| 354 return "mojo::ScopedDataPipeConsumerHandle" | 344 return "mojo::ScopedDataPipeConsumerHandle" |
| 355 if mojom.IsDataPipeProducerKind(kind): | 345 if mojom.IsDataPipeProducerKind(kind): |
| 356 return "mojo::ScopedDataPipeProducerHandle" | 346 return "mojo::ScopedDataPipeProducerHandle" |
| 357 if mojom.IsMessagePipeKind(kind): | 347 if mojom.IsMessagePipeKind(kind): |
| 358 return "mojo::ScopedMessagePipeHandle" | 348 return "mojo::ScopedMessagePipeHandle" |
| 359 if mojom.IsSharedBufferKind(kind): | 349 if mojom.IsSharedBufferKind(kind): |
| 360 return "mojo::ScopedSharedBufferHandle" | 350 return "mojo::ScopedSharedBufferHandle" |
| 361 return _kind_to_cpp_type[kind] | 351 return _kind_to_cpp_type[kind] |
| 362 | 352 |
| 353 def GetUnmappedTypeForSerializer(kind): |
| 354 return GetCppDataViewType(kind, qualified=True) |
| 355 |
| 363 def TranslateConstants(token, kind): | 356 def TranslateConstants(token, kind): |
| 364 if isinstance(token, mojom.NamedValue): | 357 if isinstance(token, mojom.NamedValue): |
| 365 return _NameFormatter(token, _variant).FormatForCpp( | 358 return _NameFormatter(token, _variant).FormatForCpp( |
| 366 flatten_nested_kind=True) | 359 flatten_nested_kind=True) |
| 367 | 360 |
| 368 if isinstance(token, mojom.BuiltinValue): | 361 if isinstance(token, mojom.BuiltinValue): |
| 369 if token.value == "double.INFINITY" or token.value == "float.INFINITY": | 362 if token.value == "double.INFINITY" or token.value == "float.INFINITY": |
| 370 return "INFINITY"; | 363 return "INFINITY"; |
| 371 if token.value == "double.NEGATIVE_INFINITY" or \ | 364 if token.value == "double.NEGATIVE_INFINITY" or \ |
| 372 token.value == "float.NEGATIVE_INFINITY": | 365 token.value == "float.NEGATIVE_INFINITY": |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 "get_qualified_name_for_kind": GetQualifiedNameForKind, | 482 "get_qualified_name_for_kind": GetQualifiedNameForKind, |
| 490 "has_callbacks": mojom.HasCallbacks, | 483 "has_callbacks": mojom.HasCallbacks, |
| 491 "has_sync_methods": mojom.HasSyncMethods, | 484 "has_sync_methods": mojom.HasSyncMethods, |
| 492 "requires_context_for_data_view": RequiresContextForDataView, | 485 "requires_context_for_data_view": RequiresContextForDataView, |
| 493 "should_inline": ShouldInlineStruct, | 486 "should_inline": ShouldInlineStruct, |
| 494 "should_inline_union": ShouldInlineUnion, | 487 "should_inline_union": ShouldInlineUnion, |
| 495 "is_array_kind": mojom.IsArrayKind, | 488 "is_array_kind": mojom.IsArrayKind, |
| 496 "is_enum_kind": mojom.IsEnumKind, | 489 "is_enum_kind": mojom.IsEnumKind, |
| 497 "is_integral_kind": mojom.IsIntegralKind, | 490 "is_integral_kind": mojom.IsIntegralKind, |
| 498 "is_native_only_kind": IsNativeOnlyKind, | 491 "is_native_only_kind": IsNativeOnlyKind, |
| 492 "is_any_handle_kind": mojom.IsAnyHandleKind, |
| 493 "is_any_interface_kind": mojom.IsAnyInterfaceKind, |
| 499 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, | 494 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, |
| 500 "is_associated_kind": mojom.IsAssociatedKind, | 495 "is_associated_kind": mojom.IsAssociatedKind, |
| 501 "is_map_kind": mojom.IsMapKind, | 496 "is_map_kind": mojom.IsMapKind, |
| 502 "is_nullable_kind": mojom.IsNullableKind, | 497 "is_nullable_kind": mojom.IsNullableKind, |
| 503 "is_object_kind": mojom.IsObjectKind, | 498 "is_object_kind": mojom.IsObjectKind, |
| 504 "is_string_kind": mojom.IsStringKind, | 499 "is_string_kind": mojom.IsStringKind, |
| 505 "is_struct_kind": mojom.IsStructKind, | 500 "is_struct_kind": mojom.IsStructKind, |
| 506 "is_typemapped_kind": IsTypemappedKind, | 501 "is_typemapped_kind": IsTypemappedKind, |
| 507 "is_union_kind": mojom.IsUnionKind, | 502 "is_union_kind": mojom.IsUnionKind, |
| 508 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 503 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 global _use_new_wrapper_types | 593 global _use_new_wrapper_types |
| 599 _use_new_wrapper_types = self.use_new_wrapper_types | 594 _use_new_wrapper_types = self.use_new_wrapper_types |
| 600 global _variant | 595 global _variant |
| 601 _variant = self.variant | 596 _variant = self.variant |
| 602 suffix = "-%s" % self.variant if self.variant else "" | 597 suffix = "-%s" % self.variant if self.variant else "" |
| 603 self.Write(self.GenerateModuleHeader(), | 598 self.Write(self.GenerateModuleHeader(), |
| 604 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 599 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
| 605 self.Write( | 600 self.Write( |
| 606 self.GenerateModuleSource(), | 601 self.GenerateModuleSource(), |
| 607 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 602 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
| OLD | NEW |