summaryrefslogtreecommitdiff
path: root/Source/JavaScriptCore/inspector/scripts/codegen/generate_objc_conversion_helpers.py
blob: c31e991e3841e36450a8eb1f31c48f5e3f9b47c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
#
# Copyright (c) 2014 Apple Inc. All rights reserved.
# Copyright (c) 2014 University of Washington. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.


import logging
import string
from string import Template

from generator import Generator
from models import EnumType
from objc_generator import ObjCGenerator
from objc_generator_templates import ObjCGeneratorTemplates as ObjCTemplates

log = logging.getLogger('global')


def add_newline(lines):
    if lines and lines[-1] == '':
        return
    lines.append('')


class ObjCConversionHelpersGenerator(Generator):
    def __init__(self, model, input_filepath):
        Generator.__init__(self, model, input_filepath)

    def output_filename(self):
        return '%sEnumConversionHelpers.h' % ObjCGenerator.OBJC_PREFIX

    def domains_to_generate(self):
        return filter(ObjCGenerator.should_generate_domain_types_filter(self.model()), Generator.domains_to_generate(self))

    def generate_output(self):
        headers = [
            '"%sArrayConversionHelpers.h"' % ObjCGenerator.OBJC_PREFIX,
        ]

        header_args = {
            'includes': '\n'.join(['#import ' + header for header in headers]),
        }

        domains = self.domains_to_generate()
        sections = []
        sections.append(self.generate_license())
        sections.append(Template(ObjCTemplates.ConversionHelpersPrelude).substitute(None, **header_args))
        sections.append(Template(ObjCTemplates.ConversionHelpersStandard).substitute(None))
        sections.extend(map(self._generate_enum_conversion_functions, domains))
        sections.append(Template(ObjCTemplates.ConversionHelpersPostlude).substitute(None, **header_args))
        return '\n\n'.join(sections)

    def _generate_enum_conversion_functions(self, domain):
        lines = []

        # Type enums and member enums.
        for declaration in domain.type_declarations:
            if isinstance(declaration.type, EnumType):
                add_newline(lines)
                lines.append(self._generate_anonymous_enum_conversion_for_declaration(domain, declaration))
            else:
                for member in declaration.type_members:
                    if (isinstance(member.type, EnumType) and member.type.is_anonymous):
                        add_newline(lines)
                        lines.append(self._generate_anonymous_enum_conversion_for_member(domain, declaration, member))

        # Anonymous command enums.
        for command in domain.commands:
            for parameter in command.call_parameters:
                if (isinstance(parameter.type, EnumType) and parameter.type.is_anonymous):
                    add_newline(lines)
                    lines.append(self._generate_anonymous_enum_conversion_for_parameter(domain, command.command_name, parameter))
            for parameter in command.return_parameters:
                if (isinstance(parameter.type, EnumType) and parameter.type.is_anonymous):
                    add_newline(lines)
                    lines.append(self._generate_anonymous_enum_conversion_for_parameter(domain, command.command_name, parameter))

        # Anonymous event enums.
        for event in domain.events:
            for parameter in event.event_parameters:
                if (isinstance(parameter.type, EnumType) and parameter.type.is_anonymous):
                    add_newline(lines)
                    lines.append(self._generate_anonymous_enum_conversion_for_parameter(domain, event.event_name, parameter))

        return '\n'.join(lines)

    def _generate_anonymous_enum_conversion_for_declaration(self, domain, declaration):
        objc_enum_name = ObjCGenerator.objc_enum_name_for_anonymous_enum_declaration(declaration)
        enum_values = declaration.type.enum_values()
        lines = []
        lines.append(self._generate_enum_objc_to_protocol_string(objc_enum_name, enum_values))
        lines.append(self._generate_enum_from_protocol_string(objc_enum_name, enum_values))
        return '\n\n'.join(lines)

    def _generate_anonymous_enum_conversion_for_member(self, domain, declaration, member):
        objc_enum_name = ObjCGenerator.objc_enum_name_for_anonymous_enum_member(declaration, member)
        enum_values = member.type.enum_values()
        lines = []
        lines.append(self._generate_enum_objc_to_protocol_string(objc_enum_name, enum_values))
        lines.append(self._generate_enum_from_protocol_string(objc_enum_name, enum_values))
        return '\n\n'.join(lines)

    def _generate_anonymous_enum_conversion_for_parameter(self, domain, event_or_command_name, parameter):
        objc_enum_name = ObjCGenerator.objc_enum_name_for_anonymous_enum_parameter(domain, event_or_command_name, parameter)
        enum_values = parameter.type.enum_values()
        lines = []
        lines.append(self._generate_enum_objc_to_protocol_string(objc_enum_name, enum_values))
        lines.append(self._generate_enum_from_protocol_string(objc_enum_name, enum_values))
        return '\n\n'.join(lines)

    def _generate_enum_objc_to_protocol_string(self, objc_enum_name, enum_values):
        lines = []
        lines.append('inline String toProtocolString(%s value)' % objc_enum_name)
        lines.append('{')
        lines.append('    switch(value) {')
        for enum_value in enum_values:
            lines.append('    case %s%s:' % (objc_enum_name, Generator.stylized_name_for_enum_value(enum_value)))
            lines.append('        return ASCIILiteral("%s");' % enum_value)
        lines.append('    }')
        lines.append('}')
        return '\n'.join(lines)

    def _generate_enum_from_protocol_string(self, objc_enum_name, enum_values):
        lines = []
        lines.append('template<>')
        lines.append('inline %s fromProtocolString(const String& value)' % objc_enum_name)
        lines.append('{')
        for enum_value in enum_values:
            lines.append('    if (value == "%s")' % enum_value)
            lines.append('        return %s%s;' % (objc_enum_name, Generator.stylized_name_for_enum_value(enum_value)))
        lines.append('    ASSERT_NOT_REACHED();')
        lines.append('    return %s%s;' % (objc_enum_name, Generator.stylized_name_for_enum_value(enum_values[0])))
        lines.append('}')
        return '\n'.join(lines)