0

Allow mojo's cpp to generate mojo consts that are enums

Before this change the following:-
```
import "path/for/enum.mojom"
const enum.mojom.Enum foo = enum.mojom.Enum.kWhatever
```

would fail with a LookupError in GetCppPodType when filling the
module-forward.h template.

With this change, enum consts are now output in the main module.h
and pull in the full imported mojom that defined the enum.

This use of const enums is already tested by
mojo/public/tools/mojom/const_unittest.py but was not used anywhere
in cpp-generating code.

Bug: 1210301,1011660
Change-Id: I428ab67264353ac5544c0a9ad35eb432bd5aeb54
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3046531
Commit-Queue: Alex Gough <ajgo@chromium.org>
Reviewed-by: Oksana Zhuravlova <oksamyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#908203}
This commit is contained in:
Alex Gough
2021-08-03 23:29:22 +00:00
committed by Chromium LUCI CQ
parent d49af77405
commit 27bcc6718d
3 changed files with 25 additions and 1 deletions
mojo/public/tools/bindings/generators

@ -140,10 +140,12 @@ using {{interface.name}}InterfaceBase = {{interface.name}}InterfaceBase;
{%- endfor %}
{%- endif %}
{#--- Constants #}
{#--- Constants that do not rely on other headers (basic types) #}
{%- for constant in module.constants %}
{%- if not constant.kind|is_enum_kind %}
{{ kythe_annotation("%s.%s"|format(module_prefix, constant.name)) }}
{{constant|format_constant_declaration}};
{%- endif %}
{%- endfor %}
{#--- Struct Forward Declarations -#}

@ -134,6 +134,14 @@ namespace {{variant}} {
{%- endfor %}
{%- endif %}
{#--- Constants that need headers (enum types) (basic types go in forward.h) #}
{%- for constant in module.constants %}
{%- if constant.kind|is_enum_kind %}
{{ kythe_annotation("%s.%s"|format(module_prefix, constant.name)) }}
{{constant|format_enum_constant_declaration}};
{%- endif %}
{%- endfor %}
{{namespace_begin()}}
{%- set module_prefix = "%s"|format(namespaces_as_array|join(".")) %}

@ -389,6 +389,7 @@ class Generator(generator.Generator):
"default_value": self._DefaultValue,
"expression_to_text": self._ExpressionToText,
"format_constant_declaration": self._FormatConstantDeclaration,
"format_enum_constant_declaration": self._FormatEnumConstantDeclaration,
"get_container_validate_params_ctor_args":
self._GetContainerValidateParamsCtorArgs,
"get_full_mojom_name_for_kind": self._GetFullMojomNameForKind,
@ -625,6 +626,7 @@ class Generator(generator.Generator):
return self.typemap[self._GetFullMojomNameForKind(typemapped_kind)][
"typename"]
# Constants that go in module-forward.h.
def _FormatConstantDeclaration(self, constant, nested=False):
if mojom.IsStringKind(constant.kind):
if nested:
@ -636,6 +638,12 @@ class Generator(generator.Generator):
GetCppPodType(constant.kind), constant.name,
self._ConstantValue(constant))
# Constants that go in module.h.
def _FormatEnumConstantDeclaration(self, constant):
if mojom.IsEnumKind(constant.kind):
return "constexpr %s %s = %s" % (self._GetNameForKind(
constant.kind), constant.name, self._ConstantValue(constant))
def _GetCppWrapperType(self,
kind,
add_same_module_namespaces=False,
@ -774,6 +782,12 @@ class Generator(generator.Generator):
# For Blink, map values need the full definition for tracing.
return True
for constant in self.module.constants:
# Constants referencing enums need the full definition.
if mojom.IsEnumKind(
constant.kind) and constant.value.module == imported_module:
return True
return False
def _IsReceiverKind(self, kind):