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

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

Issue 2637393002: Make mojom-generated C++ string constants really constant. (Closed)
Patch Set: Created 3 years, 11 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_class_declaration.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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 return False 209 return False
210 else: 210 else:
211 return True 211 return True
212 return Check(kind) 212 return Check(kind)
213 213
214 214
215 def GetNativeTypeName(typemapped_kind): 215 def GetNativeTypeName(typemapped_kind):
216 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] 216 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"]
217 217
218 def GetCppPodType(kind): 218 def GetCppPodType(kind):
219 if mojom.IsStringKind(kind):
220 return "char*"
221 return _kind_to_cpp_type[kind] 219 return _kind_to_cpp_type[kind]
222 220
221 def FormatConstantDeclaration(constant, nested=False):
222 if mojom.IsStringKind(constant.kind):
223 if nested:
224 return "const char %s[]" % constant.name
225 return "extern const char %s[]" % constant.name
226 return "constexpr %s %s = %s" % (GetCppPodType(constant.kind), constant.name,
227 ConstantValue(constant))
228
223 def GetCppWrapperType(kind, add_same_module_namespaces=False): 229 def GetCppWrapperType(kind, add_same_module_namespaces=False):
224 def _AddOptional(type_name): 230 def _AddOptional(type_name):
225 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>" 231 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>"
226 return pattern % type_name 232 return pattern % type_name
227 233
228 if IsTypemappedKind(kind): 234 if IsTypemappedKind(kind):
229 type_name = GetNativeTypeName(kind) 235 type_name = GetNativeTypeName(kind)
230 if (mojom.IsNullableKind(kind) and 236 if (mojom.IsNullableKind(kind) and
231 not _current_typemap[GetFullMojomNameForKind(kind)][ 237 not _current_typemap[GetFullMojomNameForKind(kind)][
232 "nullable_is_same_type"]): 238 "nullable_is_same_type"]):
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 416
411 def GetUnmappedTypeForSerializer(kind): 417 def GetUnmappedTypeForSerializer(kind):
412 return GetCppDataViewType(kind, qualified=True) 418 return GetCppDataViewType(kind, qualified=True)
413 419
414 def TranslateConstants(token, kind): 420 def TranslateConstants(token, kind):
415 if isinstance(token, mojom.NamedValue): 421 if isinstance(token, mojom.NamedValue):
416 return _NameFormatter(token, _variant).FormatForCpp( 422 return _NameFormatter(token, _variant).FormatForCpp(
417 flatten_nested_kind=True) 423 flatten_nested_kind=True)
418 424
419 if isinstance(token, mojom.BuiltinValue): 425 if isinstance(token, mojom.BuiltinValue):
420 if token.value == "double.INFINITY" or token.value == "float.INFINITY": 426 if token.value == "double.INFINITY":
421 return "INFINITY"; 427 return "std::numeric_limits<double>::infinity()"
422 if token.value == "double.NEGATIVE_INFINITY" or \ 428 if token.value == "float.INFINITY":
423 token.value == "float.NEGATIVE_INFINITY": 429 return "std::numeric_limits<float>::infinity()"
424 return "-INFINITY"; 430 if token.value == "double.NEGATIVE_INFINITY":
425 if token.value == "double.NAN" or token.value == "float.NAN": 431 return "-std::numeric_limits<double>::infinity()"
426 return "NAN"; 432 if token.value == "float.NEGATIVE_INFINITY":
433 return "-std::numeric_limits<float>::infinity()"
434 if token.value == "double.NAN":
435 return "std::numeric_limits<double>::quiet_NaN()"
436 if token.value == "float.NAN":
437 return "std::numeric_limits<float>::quiet_NaN()"
427 438
428 if (kind is not None and mojom.IsFloatKind(kind)): 439 if (kind is not None and mojom.IsFloatKind(kind)):
429 return token if token.isdigit() else token + "f"; 440 return token if token.isdigit() else token + "f";
430 441
431 # Per C++11, 2.14.2, the type of an integer literal is the first of the 442 # Per C++11, 2.14.2, the type of an integer literal is the first of the
432 # corresponding list in Table 6 in which its value can be represented. In this 443 # corresponding list in Table 6 in which its value can be represented. In this
433 # case, the list for decimal constants with no suffix is: 444 # case, the list for decimal constants with no suffix is:
434 # int, long int, long long int 445 # int, long int, long long int
435 # The standard considers a program ill-formed if it contains an integer 446 # The standard considers a program ill-formed if it contains an integer
436 # literal that cannot be represented by any of the allowed types. 447 # literal that cannot be represented by any of the allowed types.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 "cpp_wrapper_param_type": GetCppWrapperParamType, 539 "cpp_wrapper_param_type": GetCppWrapperParamType,
529 "cpp_data_view_type": GetCppDataViewType, 540 "cpp_data_view_type": GetCppDataViewType,
530 "cpp_field_type": GetCppFieldType, 541 "cpp_field_type": GetCppFieldType,
531 "cpp_union_field_type": GetCppUnionFieldType, 542 "cpp_union_field_type": GetCppUnionFieldType,
532 "cpp_pod_type": GetCppPodType, 543 "cpp_pod_type": GetCppPodType,
533 "cpp_union_getter_return_type": GetUnionGetterReturnType, 544 "cpp_union_getter_return_type": GetUnionGetterReturnType,
534 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, 545 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType,
535 "cpp_wrapper_type": GetCppWrapperType, 546 "cpp_wrapper_type": GetCppWrapperType,
536 "default_value": DefaultValue, 547 "default_value": DefaultValue,
537 "expression_to_text": ExpressionToText, 548 "expression_to_text": ExpressionToText,
549 "format_constant_declaration": FormatConstantDeclaration,
538 "get_container_validate_params_ctor_args": 550 "get_container_validate_params_ctor_args":
539 GetContainerValidateParamsCtorArgs, 551 GetContainerValidateParamsCtorArgs,
540 "get_name_for_kind": GetNameForKind, 552 "get_name_for_kind": GetNameForKind,
541 "get_pad": pack.GetPad, 553 "get_pad": pack.GetPad,
542 "get_qualified_name_for_kind": GetQualifiedNameForKind, 554 "get_qualified_name_for_kind": GetQualifiedNameForKind,
543 "has_callbacks": mojom.HasCallbacks, 555 "has_callbacks": mojom.HasCallbacks,
544 "has_sync_methods": mojom.HasSyncMethods, 556 "has_sync_methods": mojom.HasSyncMethods,
545 "requires_context_for_data_view": RequiresContextForDataView, 557 "requires_context_for_data_view": RequiresContextForDataView,
546 "should_inline": ShouldInlineStruct, 558 "should_inline": ShouldInlineStruct,
547 "should_inline_union": ShouldInlineUnion, 559 "should_inline_union": ShouldInlineUnion,
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 global _use_once_callback 667 global _use_once_callback
656 _use_once_callback = self.use_once_callback 668 _use_once_callback = self.use_once_callback
657 global _variant 669 global _variant
658 _variant = self.variant 670 _variant = self.variant
659 suffix = "-%s" % self.variant if self.variant else "" 671 suffix = "-%s" % self.variant if self.variant else ""
660 self.Write(self.GenerateModuleHeader(), 672 self.Write(self.GenerateModuleHeader(),
661 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 673 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
662 self.Write( 674 self.Write(
663 self.GenerateModuleSource(), 675 self.GenerateModuleSource(),
664 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 676 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_declaration.tmpl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698