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

Unified Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 1375313006: For c++, Generate enum classes instead of enum from mojom. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/mojom_cpp_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
index ff673bc9f776160352b8b3afb3034c81b19680c2..f0fda1ed7b3d8cb5089f107808a30518ac2faa8c 100644
--- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
@@ -53,6 +53,9 @@ def DefaultValue(field):
return ExpressionToText(field.default, kind=field.kind)
return ""
+def IsEnumToken(token):
+ return isinstance(token, mojom.EnumValue)
+
def NamespaceToArray(namespace):
return namespace.split(".") if namespace else []
@@ -265,12 +268,21 @@ def TranslateConstants(token, kind):
name.extend(NamespaceToArray(token.namespace))
if token.parent_kind:
name.append(token.parent_kind.name)
- if isinstance(token, mojom.EnumValue):
- name.append(
- "%s_%s" % (generator.CamelCaseToAllCaps(token.enum.name), token.name))
+ if IsEnumToken(token):
+ name.extend([token.enum.name, token.name])
else:
name.append(token.name)
- return "::".join(name)
+
+ ret = "::".join(name)
+
+ # If we are translating an enum token for a non-enum (but defined) kind, or
+ # a non-enum token for an enum kind, we need an explicit cast.
+ # TODO(johngro) : should this be allowed at all?
viettrungluu 2015/10/01 17:49:55 Are we using this? Are there test cases?
johngro 2015/10/02 00:49:04 Unclear, and sort of. "Are we using this?" To acc
+ if IsEnumToken(token) and (None != kind) and (not mojom.IsEnumKind(kind)):
viettrungluu 2015/10/01 17:49:55 Can |kind| really be None? Side points: * Drop th
johngro 2015/10/02 00:49:04 Yes. In addition to the implication of the functi
+ return "static_cast<int32_t>(%s)" % (ret, )
+ elif not IsEnumToken(token) and mojom.IsEnumKind(kind):
+ return "static_cast<%s>(%s)" % (GetNameForKind(kind), ret)
+ return ret
if isinstance(token, mojom.BuiltinValue):
if token.value == "double.INFINITY" or token.value == "float.INFINITY":
@@ -299,7 +311,13 @@ def TranslateConstants(token, kind):
return "(-%d - 1) /* %s */" % (
2**31 - 1, "Workaround for MSVC bug; see https://crbug.com/445618")
- return "%s%s" % (token, _kind_to_cpp_literal_suffix.get(kind, ""))
+
+ # If we are translating a literal for an enum kind, we need an explicit cast.
+ # TODO(johngro) : should this be allowed at all?
+ ret = "%s%s" % (token, _kind_to_cpp_literal_suffix.get(kind, ""))
+ if mojom.IsEnumKind(kind):
+ return "static_cast<%s>(%s)" % (GetNameForKind(kind), ret)
+ return ret
def ExpressionToText(value, kind=None):
return TranslateConstants(value, kind)

Powered by Google App Engine
This is Rietveld 408576698