Commit 3896403e authored by Hitoshi Yoshida's avatar Hitoshi Yoshida Committed by Commit Bot

bindings: Make CodeGeneratorV8 inheritances use render_templates

To merge common processes into one place, this CL make CodeGeneratorV8
inheritances to use CodeGeneratorBase.render_templates(), which was
renamed from render_template().
This CL also make normalize_and_sort_includes() to use set() type
to avoid include duplicates.


Bug: 830255
Change-Id: I7e723153930ae55cab04d5a5fdb38b51c3494434
Reviewed-on: https://chromium-review.googlesource.com/1058743Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Commit-Queue: Hitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558634}
parent 9696714a
...@@ -108,14 +108,14 @@ def initialize_jinja_env(cache_dir): ...@@ -108,14 +108,14 @@ def initialize_jinja_env(cache_dir):
_BLINK_RELATIVE_PATH_PREFIXES = ('bindings/', 'core/', 'modules/', 'platform/') _BLINK_RELATIVE_PATH_PREFIXES = ('bindings/', 'core/', 'modules/', 'platform/')
def normalize_and_sort_includes(include_paths): def normalize_and_sort_includes(include_paths):
normalized_include_paths = [] normalized_include_paths = set()
for include_path in include_paths: for include_path in include_paths:
match = re.search(r'/gen/(third_party/blink/.*)$', posixpath.abspath(include_path)) match = re.search(r'/gen/(third_party/blink/.*)$', posixpath.abspath(include_path))
if match: if match:
include_path = match.group(1) include_path = match.group(1)
elif include_path.startswith(_BLINK_RELATIVE_PATH_PREFIXES): elif include_path.startswith(_BLINK_RELATIVE_PATH_PREFIXES):
include_path = 'third_party/blink/renderer/' + include_path include_path = 'third_party/blink/renderer/' + include_path
normalized_include_paths.append(include_path) normalized_include_paths.add(include_path)
return sorted(normalized_include_paths) return sorted(normalized_include_paths)
...@@ -150,24 +150,23 @@ class CodeGeneratorBase(object): ...@@ -150,24 +150,23 @@ class CodeGeneratorBase(object):
IdlType.set_garbage_collected_types(interfaces_info['garbage_collected_interfaces']) IdlType.set_garbage_collected_types(interfaces_info['garbage_collected_interfaces'])
set_component_dirs(interfaces_info['component_dirs']) set_component_dirs(interfaces_info['component_dirs'])
def render_template(self, include_paths, header_template, cpp_template, def render_templates(self, include_paths, header_template, cpp_template,
template_context, component=None): context, component=None):
template_context['code_generator'] = self.generator_name context['code_generator'] = self.generator_name
# Add includes for any dependencies # Add includes for any dependencies
template_context['header_includes'] = normalize_and_sort_includes(
template_context['header_includes'])
for include_path in include_paths: for include_path in include_paths:
if component: if component:
dependency = idl_filename_to_component(include_path) dependency = idl_filename_to_component(include_path)
assert is_valid_component_dependency(component, dependency) assert is_valid_component_dependency(component, dependency)
includes.add(include_path) includes.add(include_path)
template_context['cpp_includes'] = normalize_and_sort_includes(includes) cpp_includes = set(context.get('cpp_includes', []))
context['cpp_includes'] = normalize_and_sort_includes(cpp_includes | includes)
context['header_includes'] = normalize_and_sort_includes(context['header_includes'])
header_text = render_template(header_template, template_context) header_text = render_template(header_template, context)
cpp_text = render_template(cpp_template, template_context) cpp_text = render_template(cpp_template, context)
return header_text, cpp_text return header_text, cpp_text
def generate_code(self, definitions, definition_name): def generate_code(self, definitions, definition_name):
......
...@@ -48,7 +48,7 @@ Design doc: http://www.chromium.org/developers/design-documents/idl-compiler ...@@ -48,7 +48,7 @@ Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
import os import os
import posixpath import posixpath
from code_generator import CodeGeneratorBase, render_template, normalize_and_sort_includes from code_generator import CodeGeneratorBase
from idl_definitions import Visitor from idl_definitions import Visitor
from idl_types import IdlType from idl_types import IdlType
import v8_callback_function import v8_callback_function
...@@ -220,7 +220,7 @@ class CodeGeneratorV8(CodeGeneratorV8Base): ...@@ -220,7 +220,7 @@ class CodeGeneratorV8(CodeGeneratorV8Base):
template_context['this_include_header_path'] = posixpath.basename(header_path) template_context['this_include_header_path'] = posixpath.basename(header_path)
header_template = self.jinja_env.get_template(header_template_filename) header_template = self.jinja_env.get_template(header_template_filename)
cpp_template = self.jinja_env.get_template(cpp_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename)
header_text, cpp_text = self.render_template( header_text, cpp_text = self.render_templates(
include_paths, header_template, cpp_template, template_context, include_paths, header_template, cpp_template, template_context,
component) component)
return ( return (
...@@ -245,7 +245,7 @@ class CodeGeneratorV8(CodeGeneratorV8Base): ...@@ -245,7 +245,7 @@ class CodeGeneratorV8(CodeGeneratorV8Base):
template_context['exported'] = self.info_provider.specifier_for_export template_context['exported'] = self.info_provider.specifier_for_export
header_path, cpp_path = self.output_paths(dictionary_name) header_path, cpp_path = self.output_paths(dictionary_name)
template_context['this_include_header_path'] = posixpath.basename(header_path) template_context['this_include_header_path'] = posixpath.basename(header_path)
header_text, cpp_text = self.render_template( header_text, cpp_text = self.render_templates(
include_paths, header_template, cpp_template, template_context) include_paths, header_template, cpp_template, template_context)
return ( return (
(header_path, header_text), (header_path, header_text),
...@@ -284,7 +284,7 @@ class CodeGeneratorDictionaryImpl(CodeGeneratorV8Base): ...@@ -284,7 +284,7 @@ class CodeGeneratorDictionaryImpl(CodeGeneratorV8Base):
interface_info.get('additional_header_includes', [])) interface_info.get('additional_header_includes', []))
header_path, cpp_path = self.output_paths(definition_name, interface_info) header_path, cpp_path = self.output_paths(definition_name, interface_info)
template_context['this_include_header_path'] = posixpath.basename(header_path) template_context['this_include_header_path'] = posixpath.basename(header_path)
header_text, cpp_text = self.render_template( header_text, cpp_text = self.render_templates(
include_paths, header_template, cpp_template, template_context) include_paths, header_template, cpp_template, template_context)
return ( return (
(header_path, header_text), (header_path, header_text),
...@@ -310,6 +310,7 @@ class CodeGeneratorUnionType(CodeGeneratorBase): ...@@ -310,6 +310,7 @@ class CodeGeneratorUnionType(CodeGeneratorBase):
self.typedefs[name] = typedef.idl_type self.typedefs[name] = typedef.idl_type
def _generate_container_code(self, union_type): def _generate_container_code(self, union_type):
includes.clear()
union_type = union_type.resolve_typedefs(self.typedefs) union_type = union_type.resolve_typedefs(self.typedefs)
header_template = self.jinja_env.get_template('union_container.h.tmpl') header_template = self.jinja_env.get_template('union_container.h.tmpl')
cpp_template = self.jinja_env.get_template('union_container.cpp.tmpl') cpp_template = self.jinja_env.get_template('union_container.cpp.tmpl')
...@@ -317,18 +318,13 @@ class CodeGeneratorUnionType(CodeGeneratorBase): ...@@ -317,18 +318,13 @@ class CodeGeneratorUnionType(CodeGeneratorBase):
union_type, self.info_provider) union_type, self.info_provider)
template_context['header_includes'].append( template_context['header_includes'].append(
self.info_provider.include_path_for_export) self.info_provider.include_path_for_export)
template_context['header_includes'] = normalize_and_sort_includes(
template_context['header_includes'])
template_context['cpp_includes'] = normalize_and_sort_includes(
template_context['cpp_includes'])
template_context['code_generator'] = self.generator_name
template_context['exported'] = self.info_provider.specifier_for_export template_context['exported'] = self.info_provider.specifier_for_export
snake_base_name = to_snake_case(shorten_union_name(union_type)) snake_base_name = to_snake_case(shorten_union_name(union_type))
template_context['this_include_header_path'] = snake_base_name + '.h' template_context['this_include_header_path'] = snake_base_name + '.h'
header_text = render_template(header_template, template_context)
cpp_text = render_template(cpp_template, template_context)
header_path = posixpath.join(self.output_dir, '%s.h' % snake_base_name) header_path = posixpath.join(self.output_dir, '%s.h' % snake_base_name)
cpp_path = posixpath.join(self.output_dir, '%s.cc' % snake_base_name) cpp_path = posixpath.join(self.output_dir, '%s.cc' % snake_base_name)
header_text, cpp_text = self.render_templates(
[], header_template, cpp_template, template_context)
return ( return (
(header_path, header_text), (header_path, header_text),
(cpp_path, cpp_text), (cpp_path, cpp_text),
...@@ -385,13 +381,9 @@ class CodeGeneratorCallbackFunction(CodeGeneratorBase): ...@@ -385,13 +381,9 @@ class CodeGeneratorCallbackFunction(CodeGeneratorBase):
template_context['header_includes'].append( template_context['header_includes'].append(
self.info_provider.include_path_for_union_types(argument.idl_type)) self.info_provider.include_path_for_union_types(argument.idl_type))
template_context['header_includes'] = normalize_and_sort_includes(
template_context['header_includes'])
template_context['cpp_includes'] = normalize_and_sort_includes(
template_context['cpp_includes'])
template_context['code_generator'] = MODULE_PYNAME template_context['code_generator'] = MODULE_PYNAME
header_text = render_template(header_template, template_context) header_text, cpp_text = self.render_templates(
cpp_text = render_template(cpp_template, template_context) [], header_template, cpp_template, template_context)
snake_base_name = to_snake_case('V8%s' % callback_function.name) snake_base_name = to_snake_case('V8%s' % callback_function.name)
header_path = posixpath.join(self.output_dir, '%s.h' % snake_base_name) header_path = posixpath.join(self.output_dir, '%s.h' % snake_base_name)
cpp_path = posixpath.join(self.output_dir, '%s.cc' % snake_base_name) cpp_path = posixpath.join(self.output_dir, '%s.cc' % snake_base_name)
......
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