Replace WTF::move with WTFMove
[WebKit-https.git] / Source / JavaScriptCore / inspector / scripts / codegen / generate_cpp_frontend_dispatcher_implementation.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2014, 2015 Apple Inc. All rights reserved.
4 # Copyright (c) 2014 University of Washington. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 # THE POSSIBILITY OF SUCH DAMAGE.
26
27
28 import logging
29 import string
30 from string import Template
31
32 from cpp_generator import CppGenerator
33 from cpp_generator_templates import CppGeneratorTemplates as CppTemplates
34 from generator import Generator, ucfirst
35 from models import ObjectType, ArrayType
36
37 log = logging.getLogger('global')
38
39
40 class CppFrontendDispatcherImplementationGenerator(Generator):
41     def __init__(self, model, input_filepath):
42         Generator.__init__(self, model, input_filepath)
43
44     def output_filename(self):
45         return "InspectorFrontendDispatchers.cpp"
46
47     def domains_to_generate(self):
48         return filter(lambda domain: len(domain.events) > 0, Generator.domains_to_generate(self))
49
50     def generate_output(self):
51         secondary_headers = [
52             '"InspectorFrontendRouter.h"',
53             '<wtf/text/CString.h>',
54         ]
55
56         header_args = {
57             'primaryInclude': '"InspectorFrontendDispatchers.h"',
58             'secondaryIncludes': "\n".join(['#include %s' % header for header in secondary_headers]),
59         }
60
61         sections = []
62         sections.append(self.generate_license())
63         sections.append(Template(CppTemplates.ImplementationPrelude).substitute(None, **header_args))
64         sections.extend(map(self._generate_dispatcher_implementations_for_domain, self.domains_to_generate()))
65         sections.append(Template(CppTemplates.ImplementationPostlude).substitute(None, **header_args))
66         return "\n\n".join(sections)
67
68     # Private methods.
69
70     def _generate_dispatcher_implementations_for_domain(self, domain):
71         implementations = []
72         for event in domain.events:
73             implementations.append(self._generate_dispatcher_implementation_for_event(event, domain))
74
75         return self.wrap_with_guard_for_domain(domain, '\n\n'.join(implementations))
76
77     def _generate_dispatcher_implementation_for_event(self, event, domain):
78         lines = []
79         parameter_assignments = []
80         formal_parameters = []
81
82         for parameter in event.event_parameters:
83
84             parameter_value = parameter.parameter_name
85             if parameter.is_optional and not CppGenerator.should_pass_by_copy_for_return_type(parameter.type):
86                 parameter_value = '*' + parameter_value
87             if parameter.type.is_enum():
88                 parameter_value = 'Inspector::Protocol::getEnumConstantValue(%s)' % parameter_value
89
90             parameter_args = {
91                 'parameterType': CppGenerator.cpp_type_for_stack_out_parameter(parameter),
92                 'parameterName': parameter.parameter_name,
93                 'parameterValue': parameter_value,
94                 'keyedSetMethod': CppGenerator.cpp_setter_method_for_type(parameter.type),
95             }
96
97             if parameter.is_optional:
98                 parameter_assignments.append('    if (%(parameterName)s)' % parameter_args)
99                 parameter_assignments.append('        paramsObject->%(keyedSetMethod)s(ASCIILiteral("%(parameterName)s"), %(parameterValue)s);' % parameter_args)
100             else:
101                 parameter_assignments.append('    paramsObject->%(keyedSetMethod)s(ASCIILiteral("%(parameterName)s"), %(parameterValue)s);' % parameter_args)
102
103             formal_parameters.append('%s %s' % (CppGenerator.cpp_type_for_checked_formal_event_parameter(parameter), parameter.parameter_name))
104
105         event_args = {
106             'domainName': domain.domain_name,
107             'eventName': event.event_name,
108             'formalParameters': ", ".join(formal_parameters)
109         }
110
111         lines.append('void %(domainName)sFrontendDispatcher::%(eventName)s(%(formalParameters)s)' % event_args)
112         lines.append('{')
113         lines.append('    Ref<InspectorObject> jsonMessage = InspectorObject::create();')
114         lines.append('    jsonMessage->setString(ASCIILiteral("method"), ASCIILiteral("%(domainName)s.%(eventName)s"));' % event_args)
115
116         if len(parameter_assignments) > 0:
117             lines.append('    Ref<InspectorObject> paramsObject = InspectorObject::create();')
118             lines.extend(parameter_assignments)
119             lines.append('    jsonMessage->setObject(ASCIILiteral("params"), WTFMove(paramsObject));')
120
121         lines.append('')
122         lines.append('    m_frontendRouter.sendEvent(jsonMessage->toJSONString());')
123         lines.append('}')
124         return "\n".join(lines)