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 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 if len(struct.fields) > 4: | 369 if len(struct.fields) > 4: |
370 return False | 370 return False |
371 for field in struct.fields: | 371 for field in struct.fields: |
372 if mojom.IsMoveOnlyKind(field.kind): | 372 if mojom.IsMoveOnlyKind(field.kind): |
373 return False | 373 return False |
374 return True | 374 return True |
375 | 375 |
376 def ShouldInlineUnion(union): | 376 def ShouldInlineUnion(union): |
377 return not any(mojom.IsMoveOnlyKind(field.kind) for field in union.fields) | 377 return not any(mojom.IsMoveOnlyKind(field.kind) for field in union.fields) |
378 | 378 |
379 def GetArrayValidateParamsCtorArgs(kind): | 379 def GetContainerValidateParamsCtorArgs(kind): |
380 if mojom.IsStringKind(kind): | 380 if mojom.IsStringKind(kind): |
381 expected_num_elements = 0 | 381 expected_num_elements = 0 |
382 element_is_nullable = False | 382 element_is_nullable = False |
383 key_validate_params = "nullptr" | 383 key_validate_params = "nullptr" |
384 element_validate_params = "nullptr" | 384 element_validate_params = "nullptr" |
385 enum_validate_func = "nullptr" | 385 enum_validate_func = "nullptr" |
386 elif mojom.IsMapKind(kind): | 386 elif mojom.IsMapKind(kind): |
387 expected_num_elements = 0 | 387 expected_num_elements = 0 |
388 element_is_nullable = False | 388 element_is_nullable = False |
389 key_validate_params = GetNewArrayValidateParams(mojom.Array( | 389 key_validate_params = GetNewContainerValidateParams(mojom.Array( |
390 kind=kind.key_kind)) | 390 kind=kind.key_kind)) |
391 element_validate_params = GetNewArrayValidateParams(mojom.Array( | 391 element_validate_params = GetNewContainerValidateParams(mojom.Array( |
392 kind=kind.value_kind)) | 392 kind=kind.value_kind)) |
393 enum_validate_func = "nullptr" | 393 enum_validate_func = "nullptr" |
394 else: # mojom.IsArrayKind(kind) | 394 else: # mojom.IsArrayKind(kind) |
395 expected_num_elements = generator.ExpectedArraySize(kind) or 0 | 395 expected_num_elements = generator.ExpectedArraySize(kind) or 0 |
396 element_is_nullable = mojom.IsNullableKind(kind.kind) | 396 element_is_nullable = mojom.IsNullableKind(kind.kind) |
397 key_validate_params = "nullptr" | 397 key_validate_params = "nullptr" |
398 element_validate_params = GetNewArrayValidateParams(kind.kind) | 398 element_validate_params = GetNewContainerValidateParams(kind.kind) |
399 if mojom.IsEnumKind(kind.kind): | 399 if mojom.IsEnumKind(kind.kind): |
400 enum_validate_func = ("%s::Validate" % | 400 enum_validate_func = ("%s::Validate" % |
401 GetQualifiedNameForKind(kind.kind, internal=True)) | 401 GetQualifiedNameForKind(kind.kind, internal=True)) |
402 else: | 402 else: |
403 enum_validate_func = "nullptr" | 403 enum_validate_func = "nullptr" |
404 | 404 |
405 if enum_validate_func == "nullptr": | 405 if enum_validate_func == "nullptr": |
406 if key_validate_params == "nullptr": | 406 if key_validate_params == "nullptr": |
407 return "%d, %s, %s" % (expected_num_elements, | 407 return "%d, %s, %s" % (expected_num_elements, |
408 "true" if element_is_nullable else "false", | 408 "true" if element_is_nullable else "false", |
409 element_validate_params) | 409 element_validate_params) |
410 else: | 410 else: |
411 return "%s, %s" % (key_validate_params, element_validate_params) | 411 return "%s, %s" % (key_validate_params, element_validate_params) |
412 else: | 412 else: |
413 return "%d, %s" % (expected_num_elements, enum_validate_func) | 413 return "%d, %s" % (expected_num_elements, enum_validate_func) |
414 | 414 |
415 def GetNewArrayValidateParams(kind): | 415 def GetNewContainerValidateParams(kind): |
416 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and | 416 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and |
417 not mojom.IsStringKind(kind)): | 417 not mojom.IsStringKind(kind)): |
418 return "nullptr" | 418 return "nullptr" |
419 | 419 |
420 return "new mojo::internal::ArrayValidateParams(%s)" % ( | 420 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
421 GetArrayValidateParamsCtorArgs(kind)) | 421 GetContainerValidateParamsCtorArgs(kind)) |
422 | 422 |
423 class Generator(generator.Generator): | 423 class Generator(generator.Generator): |
424 | 424 |
425 cpp_filters = { | 425 cpp_filters = { |
426 "constant_value": ConstantValue, | 426 "constant_value": ConstantValue, |
427 "cpp_wrapper_param_type": GetCppWrapperParamType, | 427 "cpp_wrapper_param_type": GetCppWrapperParamType, |
428 "cpp_field_type": GetCppFieldType, | 428 "cpp_field_type": GetCppFieldType, |
429 "cpp_union_field_type": GetCppUnionFieldType, | 429 "cpp_union_field_type": GetCppUnionFieldType, |
430 "cpp_pod_type": GetCppPodType, | 430 "cpp_pod_type": GetCppPodType, |
431 "cpp_union_getter_return_type": GetUnionGetterReturnType, | 431 "cpp_union_getter_return_type": GetUnionGetterReturnType, |
432 "cpp_wrapper_type": GetCppWrapperType, | 432 "cpp_wrapper_type": GetCppWrapperType, |
433 "default_value": DefaultValue, | 433 "default_value": DefaultValue, |
434 "expression_to_text": ExpressionToText, | 434 "expression_to_text": ExpressionToText, |
435 "get_array_validate_params_ctor_args": GetArrayValidateParamsCtorArgs, | 435 "get_container_validate_params_ctor_args": |
| 436 GetContainerValidateParamsCtorArgs, |
436 "get_name_for_kind": GetNameForKind, | 437 "get_name_for_kind": GetNameForKind, |
437 "get_pad": pack.GetPad, | 438 "get_pad": pack.GetPad, |
438 "get_qualified_name_for_kind": GetQualifiedNameForKind, | 439 "get_qualified_name_for_kind": GetQualifiedNameForKind, |
439 "has_callbacks": mojom.HasCallbacks, | 440 "has_callbacks": mojom.HasCallbacks, |
440 "has_sync_methods": mojom.HasSyncMethods, | 441 "has_sync_methods": mojom.HasSyncMethods, |
441 "should_inline": ShouldInlineStruct, | 442 "should_inline": ShouldInlineStruct, |
442 "should_inline_union": ShouldInlineUnion, | 443 "should_inline_union": ShouldInlineUnion, |
443 "is_array_kind": mojom.IsArrayKind, | 444 "is_array_kind": mojom.IsArrayKind, |
444 "is_enum_kind": mojom.IsEnumKind, | 445 "is_enum_kind": mojom.IsEnumKind, |
445 "is_integral_kind": mojom.IsIntegralKind, | 446 "is_integral_kind": mojom.IsIntegralKind, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 _for_blink = self.for_blink | 523 _for_blink = self.for_blink |
523 global _variant | 524 global _variant |
524 _variant = self.variant | 525 _variant = self.variant |
525 suffix = "-%s" % self.variant if self.variant else "" | 526 suffix = "-%s" % self.variant if self.variant else "" |
526 self.Write(self.GenerateModuleHeader(), | 527 self.Write(self.GenerateModuleHeader(), |
527 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 528 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
528 self.Write(self.GenerateModuleInternalHeader(), | 529 self.Write(self.GenerateModuleInternalHeader(), |
529 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) | 530 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) |
530 self.Write(self.GenerateModuleSource(), | 531 self.Write(self.GenerateModuleSource(), |
531 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 532 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |