Commit 6fbdadf3 authored by Takuto Ikuta's avatar Takuto Ikuta Committed by Commit Bot

[mojo] introduce mojo forwarding header

This CL introduce mojom-forward.h and mojom-import-headers.h

mojo-forward.h is introduced for forward enum/class/struct declaration with some using aliases.
I replaced some mojom.h includes with mojom-foward.h in mojom.h except files using inlined struct.

mojom-import-headers.h is used to include all mojom.h of all (indirectly) imported header for mojo's cc files.

Bug: 922875
Change-Id: I386528b1059516dc1d52b18a4778a79a2289c063
Reviewed-on: https://chromium-review.googlesource.com/c/1438975
Commit-Queue: Takuto Ikuta <tikuta@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#628544}
parent d7decf5f
...@@ -17,6 +17,8 @@ action("precompile_templates") { ...@@ -17,6 +17,8 @@ action("precompile_templates") {
"$mojom_generator_root/generators/cpp_templates/interface_request_validator_declaration.tmpl", "$mojom_generator_root/generators/cpp_templates/interface_request_validator_declaration.tmpl",
"$mojom_generator_root/generators/cpp_templates/interface_response_validator_declaration.tmpl", "$mojom_generator_root/generators/cpp_templates/interface_response_validator_declaration.tmpl",
"$mojom_generator_root/generators/cpp_templates/interface_stub_declaration.tmpl", "$mojom_generator_root/generators/cpp_templates/interface_stub_declaration.tmpl",
"$mojom_generator_root/generators/cpp_templates/module-forward.h.tmpl",
"$mojom_generator_root/generators/cpp_templates/module-import-headers.h.tmpl",
"$mojom_generator_root/generators/cpp_templates/module-params-data.h.tmpl", "$mojom_generator_root/generators/cpp_templates/module-params-data.h.tmpl",
"$mojom_generator_root/generators/cpp_templates/module-shared-internal.h.tmpl", "$mojom_generator_root/generators/cpp_templates/module-shared-internal.h.tmpl",
"$mojom_generator_root/generators/cpp_templates/module-shared-message-ids.h.tmpl", "$mojom_generator_root/generators/cpp_templates/module-shared-message-ids.h.tmpl",
......
...@@ -2,8 +2,14 @@ ...@@ -2,8 +2,14 @@
Macro for enum definition, and the declaration of associated functions. Macro for enum definition, and the declaration of associated functions.
---#} ---#}
{%- macro enum_forward(enum) %}
{%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %}
enum class {{enum_name}} : int32_t;
{%- endmacro %}
{%- macro enum_decl(enum, export_attribute) %} {%- macro enum_decl(enum, export_attribute) %}
{%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %} {%- set enum_name = enum|get_name_for_kind(flatten_nested_kind=True) %}
enum class {{enum_name}} : int32_t { enum class {{enum_name}} : int32_t {
{%- for field in enum.fields %} {%- for field in enum.fields %}
{%- if field.value %} {%- if field.value %}
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{%- if variant -%}
{%- set variant_path = "%s-%s"|format(module.path, variant) -%}
{%- else -%}
{%- set variant_path = module.path -%}
{%- endif -%}
{%- set header_guard = "%s_FORWARD_H_"|format(
variant_path|upper|replace("/","_")|replace(".","_")|
replace("-", "_")) %}
{%- macro namespace_begin() %}
{%- for namespace in namespaces_as_array %}
namespace {{namespace}} {
{%- endfor %}
{%- if variant %}
namespace {{variant}} {
{%- endif %}
{%- endmacro %}
{%- macro namespace_end() %}
{%- if variant %}
} // namespace {{variant}}
{%- endif %}
{%- for namespace in namespaces_as_array|reverse %}
} // namespace {{namespace}}
{%- endfor %}
{%- endmacro %}
#ifndef {{header_guard}}
#define {{header_guard}}
#include "mojo/public/cpp/bindings/struct_ptr.h"
{% if not disallow_interfaces -%}
#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
#include "mojo/public/cpp/bindings/associated_interface_ptr_info.h"
#include "mojo/public/cpp/bindings/associated_interface_request.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/lib/control_message_handler.h"
#include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h"
#include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
{%- if for_blink %}
#include "third_party/blink/renderer/platform/mojo/revocable_interface_ptr.h"
{%- endif %}
{%- endif %}
{% if not disallow_native_types %}
#include "mojo/public/cpp/bindings/lib/native_enum_serialization.h"
#include "mojo/public/cpp/bindings/lib/native_struct_serialization.h"
{%- endif %}
{%- if export_header %}
#include "{{export_header}}"
{%- endif %}
{{namespace_begin()}}
{#- These are non-variant header only. #}
{%- if not variant %}
{#--- Struct Forward Declarations -#}
{%- for struct in structs %}
{%- if struct|is_native_only_kind %}
using {{struct.name}}DataView = mojo::native::NativeStructDataView;
{%- else %}
class {{struct.name}}DataView;
{%- endif %}
{% endfor %}
{#--- Union Forward Declarations -#}
{%- for union in unions %}
class {{union.name}}DataView;
{%- endfor %}
{#--- Enums #}
{%- from "enum_macros.tmpl" import enum_forward%}
{%- for enum in all_enums %}
{%- if enum|is_native_only_kind %}
using {{enum|get_name_for_kind(flatten_nested_kind=True)}} = mojo::NativeEnum;
{%- else %}
{{enum_forward(enum)}}
{%- endif %}
{%- endfor %}
{%- endif %}
{#--- Enums #}
{%- if variant %}
{%- for enum in enums %}
using {{enum.name}} = {{enum.name}}; // Alias for definition in the parent namespace.
{%- endfor %}
{%- endif %}
{#--- Constants #}
{%- for constant in module.constants %}
{{constant|format_constant_declaration}};
{%- endfor %}
{#--- Struct Forward Declarations -#}
{% for struct in structs %}
{%- if struct|is_native_only_kind %}
using {{struct.name}} = mojo::native::NativeStruct;
using {{struct.name}}Ptr = mojo::native::NativeStructPtr;
{%- else %}
class {{struct.name}};
{%- if struct|should_inline %}
using {{struct.name}}Ptr = mojo::InlinedStructPtr<{{struct.name}}>;
{%- else %}
using {{struct.name}}Ptr = mojo::StructPtr<{{struct.name}}>;
{%- endif %}
{%- endif %}
{% endfor %}
{#--- Union Forward Declarations -#}
{% for union in unions %}
class {{union.name}};
{% if union|should_inline_union %}
typedef mojo::InlinedStructPtr<{{union.name}}> {{union.name}}Ptr;
{% else %}
typedef mojo::StructPtr<{{union.name}}> {{union.name}}Ptr;
{% endif %}
{%- endfor %}
{#--- Interface Forward Declarations -#}
{% for interface in interfaces %}
class {{interface.name}};
using {{interface.name}}Ptr = mojo::InterfacePtr<{{interface.name}}>;
{%- if for_blink %}
using Revocable{{interface.name}}Ptr = ::blink::RevocableInterfacePtr<{{interface.name}}>;
{%- endif %}
using {{interface.name}}PtrInfo = mojo::InterfacePtrInfo<{{interface.name}}>;
using ThreadSafe{{interface.name}}Ptr =
mojo::ThreadSafeInterfacePtr<{{interface.name}}>;
using {{interface.name}}Request = mojo::InterfaceRequest<{{interface.name}}>;
using {{interface.name}}AssociatedPtr =
mojo::AssociatedInterfacePtr<{{interface.name}}>;
using ThreadSafe{{interface.name}}AssociatedPtr =
mojo::ThreadSafeAssociatedInterfacePtr<{{interface.name}}>;
using {{interface.name}}AssociatedPtrInfo =
mojo::AssociatedInterfacePtrInfo<{{interface.name}}>;
using {{interface.name}}AssociatedRequest =
mojo::AssociatedInterfaceRequest<{{interface.name}}>;
{% endfor %}
{{namespace_end()}}
{#- TODO(tikuta): Use forward declaration of native enum/struct here. #}
{%- for header in extra_public_headers %}
#include "{{header}}"
{%- endfor %}
#endif // {{header_guard}}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{%- if variant -%}
{%- set variant_path = "%s-%s"|format(module.path, variant) -%}
{%- else -%}
{%- set variant_path = module.path -%}
{%- endif -%}
{%- set header_guard = "%s_IMPORT_HEADERS_H_"|format(
variant_path|upper|replace("/","_")|replace(".","_")|
replace("-", "_")) %}
#ifndef {{header_guard}}
#define {{header_guard}}
{%- for import in imports %}
{%- if variant %}
#include "{{"%s-%s.h"|format(import.path, variant)}}"
#include "{{"%s-%s-import-headers.h"|format(import.path, variant)}}"
{%- else %}
#include "{{import.path}}.h"
#include "{{import.path}}-import-headers.h"
{%- endif %}
{%- endfor %}
#endif // {{header_guard}}
...@@ -52,6 +52,14 @@ namespace {{variant}} { ...@@ -52,6 +52,14 @@ namespace {{variant}} {
#include "mojo/public/cpp/bindings/lib/wtf_serialization.h" #include "mojo/public/cpp/bindings/lib/wtf_serialization.h"
{%- endif %} {%- endif %}
{%- for import in imports %}
{%- if variant %}
#include "{{"%s-%s.h"|format(import.path, variant)}}"
{%- else %}
#include "{{import.path}}.h"
{%- endif %}
{%- endfor %}
{# This is include guard for jumbo build. #} {# This is include guard for jumbo build. #}
#ifndef {{header_guard_for_jumbo}} #ifndef {{header_guard_for_jumbo}}
#define {{header_guard_for_jumbo}} #define {{header_guard_for_jumbo}}
......
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
#include "{{module.path}}-params-data.h" #include "{{module.path}}-params-data.h"
#include "{{module.path}}-shared-message-ids.h" #include "{{module.path}}-shared-message-ids.h"
#include "{{variant_path}}-import-headers.h"
{%- if for_blink %} {%- if for_blink %}
#include "mojo/public/cpp/bindings/lib/wtf_serialization.h" #include "mojo/public/cpp/bindings/lib/wtf_serialization.h"
{%- endif %} {%- endif %}
......
...@@ -54,13 +54,35 @@ namespace {{variant}} { ...@@ -54,13 +54,35 @@ namespace {{variant}} {
#include "mojo/public/cpp/bindings/struct_traits.h" #include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/cpp/bindings/union_traits.h" #include "mojo/public/cpp/bindings/union_traits.h"
#include "{{module.path}}-shared.h" #include "{{module.path}}-shared.h"
#include "{{variant_path}}-forward.h"
{#- Imported struct and union types must be fully defined if used in this
mojom. #}
{%- set import_definition_header = namespace(need=false) %}
{%- for struct in structs %}
{%- for field in struct.fields %}
{%- if field.kind|is_object_kind or field.kind|is_union_kind %}
{%- set import_definition_header.need = true %}
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- for import in imports %} {%- for import in imports %}
{%- if variant %} {%- if import_definition_header.need %}
{%- if variant %}
#include "{{"%s-%s.h"|format(import.path, variant)}}" #include "{{"%s-%s.h"|format(import.path, variant)}}"
{%- else %} {%- else %}
#include "{{import.path}}.h" #include "{{import.path}}.h"
{%- endif %}
{%- else %}
{%- if variant %}
#include "{{"%s-%s-forward.h"|format(import.path, variant)}}"
{%- else %}
#include "{{import.path}}-forward.h"
{%- endif %}
{%- endif %} {%- endif %}
{%- endfor %} {%- endfor %}
{%- if not for_blink %} {%- if not for_blink %}
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -113,64 +135,6 @@ namespace {{variant}} { ...@@ -113,64 +135,6 @@ namespace {{variant}} {
{{namespace_begin()}} {{namespace_begin()}}
{#--- Enums #}
{%- if variant %}
{%- for enum in enums %}
using {{enum.name}} = {{enum.name}}; // Alias for definition in the parent namespace.
{%- endfor %}
{%- endif %}
{#--- Constants #}
{%- for constant in module.constants %}
{{constant|format_constant_declaration}};
{%- endfor %}
{#--- Interface Forward Declarations -#}
{% for interface in interfaces %}
class {{interface.name}};
using {{interface.name}}Ptr = mojo::InterfacePtr<{{interface.name}}>;
{%- if for_blink %}
using Revocable{{interface.name}}Ptr = ::blink::RevocableInterfacePtr<{{interface.name}}>;
{%- endif %}
using {{interface.name}}PtrInfo = mojo::InterfacePtrInfo<{{interface.name}}>;
using ThreadSafe{{interface.name}}Ptr =
mojo::ThreadSafeInterfacePtr<{{interface.name}}>;
using {{interface.name}}Request = mojo::InterfaceRequest<{{interface.name}}>;
using {{interface.name}}AssociatedPtr =
mojo::AssociatedInterfacePtr<{{interface.name}}>;
using ThreadSafe{{interface.name}}AssociatedPtr =
mojo::ThreadSafeAssociatedInterfacePtr<{{interface.name}}>;
using {{interface.name}}AssociatedPtrInfo =
mojo::AssociatedInterfacePtrInfo<{{interface.name}}>;
using {{interface.name}}AssociatedRequest =
mojo::AssociatedInterfaceRequest<{{interface.name}}>;
{% endfor %}
{#--- Struct Forward Declarations -#}
{% for struct in structs %}
{%- if struct|is_native_only_kind %}
using {{struct.name}} = mojo::native::NativeStruct;
using {{struct.name}}Ptr = mojo::native::NativeStructPtr;
{%- else %}
class {{struct.name}};
{%- if struct|should_inline %}
using {{struct.name}}Ptr = mojo::InlinedStructPtr<{{struct.name}}>;
{%- else %}
using {{struct.name}}Ptr = mojo::StructPtr<{{struct.name}}>;
{%- endif %}
{%- endif %}
{% endfor %}
{#--- Union Forward Declarations -#}
{% for union in unions %}
class {{union.name}};
{% if union|should_inline_union %}
typedef mojo::InlinedStructPtr<{{union.name}}> {{union.name}}Ptr;
{% else %}
typedef mojo::StructPtr<{{union.name}}> {{union.name}}Ptr;
{% endif %}
{%- endfor %}
{#--- Interfaces -#} {#--- Interfaces -#}
{% for interface in interfaces %} {% for interface in interfaces %}
{% include "interface_declaration.tmpl" %} {% include "interface_declaration.tmpl" %}
......
...@@ -376,10 +376,18 @@ class Generator(generator.Generator): ...@@ -376,10 +376,18 @@ class Generator(generator.Generator):
def _GenerateModuleHeader(self): def _GenerateModuleHeader(self):
return self._GetJinjaExports() return self._GetJinjaExports()
@UseJinja("module-forward.h.tmpl")
def _GenerateModuleForwardHeader(self):
return self._GetJinjaExports()
@UseJinja("module.cc.tmpl") @UseJinja("module.cc.tmpl")
def _GenerateModuleSource(self): def _GenerateModuleSource(self):
return self._GetJinjaExports() return self._GetJinjaExports()
@UseJinja("module-import-headers.h.tmpl")
def _GenerateModuleImportHeadersHeader(self):
return self._GetJinjaExports()
@UseJinja("module-shared.h.tmpl") @UseJinja("module-shared.h.tmpl")
def _GenerateModuleSharedHeader(self): def _GenerateModuleSharedHeader(self):
return self._GetJinjaExports() return self._GetJinjaExports()
...@@ -428,8 +436,12 @@ class Generator(generator.Generator): ...@@ -428,8 +436,12 @@ class Generator(generator.Generator):
suffix = "-%s" % self.variant if self.variant else "" suffix = "-%s" % self.variant if self.variant else ""
self.Write(self._GenerateModuleHeader(), self.Write(self._GenerateModuleHeader(),
"%s%s.h" % (self.module.path, suffix)) "%s%s.h" % (self.module.path, suffix))
self.Write(self._GenerateModuleForwardHeader(),
"%s%s-forward.h" % (self.module.path, suffix))
self.Write(self._GenerateModuleSource(), self.Write(self._GenerateModuleSource(),
"%s%s.cc" % (self.module.path, suffix)) "%s%s.cc" % (self.module.path, suffix))
self.Write(self._GenerateModuleImportHeadersHeader(),
"%s%s-import-headers.h" % (self.module.path, suffix))
self.Write(self._GenerateModuleTestUtilsHeader(), self.Write(self._GenerateModuleTestUtilsHeader(),
"%s%s-test-utils.h" % (self.module.path, suffix)) "%s%s-test-utils.h" % (self.module.path, suffix))
self.Write(self._GenerateModuleTestUtilsSource(), self.Write(self._GenerateModuleTestUtilsSource(),
......
...@@ -754,10 +754,12 @@ template("mojom") { ...@@ -754,10 +754,12 @@ template("mojom") {
variant_dash_suffix = "-${variant}" variant_dash_suffix = "-${variant}"
} }
generator_cpp_outputs += [ generator_cpp_outputs += [
"{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}.cc", "{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}-forward.h",
"{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}.h", "{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}-import-headers.h",
"{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}-test-utils.cc", "{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}-test-utils.cc",
"{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}-test-utils.h", "{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}-test-utils.h",
"{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}.cc",
"{{source_gen_dir}}/{{source_file_part}}${variant_dash_suffix}.h",
] ]
enabled_sources = [] enabled_sources = []
if (defined(bindings_configuration.blacklist)) { if (defined(bindings_configuration.blacklist)) {
...@@ -830,10 +832,12 @@ template("mojom") { ...@@ -830,10 +832,12 @@ template("mojom") {
foreach(source, invoker.sources) { foreach(source, invoker.sources) {
filelist += [ rebase_path("$source", root_build_dir) ] filelist += [ rebase_path("$source", root_build_dir) ]
outputs += [ outputs += [
"$target_gen_dir/${source}${variant_dash_suffix}.cc", "$target_gen_dir/${source}${variant_dash_suffix}-forward.h",
"$target_gen_dir/${source}${variant_dash_suffix}.h", "$target_gen_dir/${source}${variant_dash_suffix}-import-headers.h",
"$target_gen_dir/${source}${variant_dash_suffix}-test-utils.cc", "$target_gen_dir/${source}${variant_dash_suffix}-test-utils.cc",
"$target_gen_dir/${source}${variant_dash_suffix}-test-utils.h", "$target_gen_dir/${source}${variant_dash_suffix}-test-utils.h",
"$target_gen_dir/${source}${variant_dash_suffix}.cc",
"$target_gen_dir/${source}${variant_dash_suffix}.h",
] ]
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment