1 # Copyright (C) 2013 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 """Functions shared by various parts of the code generator.
31 FIXME: Not currently used in build.
32 This is a rewrite of the Perl IDL compiler in Python, but is not complete.
33 Once it is complete, we will switch all IDL files over to Python at once.
34 Until then, please work on the Perl IDL compiler.
35 For details, see bug http://crbug.com/239771
38 # FIXME: eliminate this file if possible
42 from v8_globals import includes
45 ACRONYMS = ['CSS', 'HTML', 'IME', 'JS', 'SVG', 'URL', 'WOFF', 'XML', 'XSLT']
48 ################################################################################
49 # Extended attribute parsing
50 ################################################################################
52 def extended_attribute_value_contains(extended_attribute_value, value):
53 return (extended_attribute_value and
54 value in re.split('[|&]', extended_attribute_value))
57 def has_extended_attribute(definition_or_member, extended_attribute_list):
58 return any(extended_attribute in definition_or_member.extended_attributes
59 for extended_attribute in extended_attribute_list)
62 def has_extended_attribute_value(definition_or_member, name, value):
63 extended_attributes = definition_or_member.extended_attributes
64 return (name in extended_attributes and
65 extended_attribute_value_contains(extended_attributes[name], value))
68 ################################################################################
70 ################################################################################
73 """Capitalize first letter or initial acronym (used in setter names)."""
74 for acronym in ACRONYMS:
75 if name.startswith(acronym.lower()):
76 return name.replace(acronym.lower(), acronym)
77 return name[0].upper() + name[1:]
80 def strip_suffix(string, suffix):
81 if not suffix or not string.endswith(suffix):
83 return string[:-len(suffix)]
86 def uncapitalize(name):
87 """Uncapitalizes first letter or initial acronym (used in method names).
89 E.g., 'SetURL' becomes 'setURL', but 'URLFoo' becomes 'urlFoo'.
91 for acronym in ACRONYMS:
92 if name.startswith(acronym):
93 return name.replace(acronym, acronym.lower())
94 return name[0].lower() + name[1:]
97 ################################################################################
99 ################################################################################
101 def enum_validation_expression(idl_type):
102 if not v8_types.is_enum(idl_type):
104 return ' || '.join(['string == "%s"' % enum_value
105 for enum_value in v8_types.enum_values(idl_type)])
108 def scoped_name(interface, definition, base_name):
109 implemented_by = definition.extended_attributes.get('ImplementedBy')
111 return '%s::%s' % (implemented_by, base_name)
112 if definition.is_static:
113 return '%s::%s' % (cpp_name(interface), base_name)
114 return 'imp->%s' % base_name
117 def v8_class_name(interface):
118 return v8_types.v8_type(interface.name)
121 ################################################################################
122 # Specific extended attributes
123 ################################################################################
126 def activity_logging_world_list(member, access_type=None):
127 """Returns a set of world suffixes for which a definition member has activity logging, for specified access type.
129 access_type can be 'Getter' or 'Setter' if only checking getting or setting.
131 if 'ActivityLogging' not in member.extended_attributes:
133 activity_logging = member.extended_attributes['ActivityLogging']
134 # [ActivityLogging=For*] (no prefix, starts with the worlds suffix) means
135 # "log for all use (method)/access (attribute)", otherwise check that value
136 # agrees with specified access_type (Getter/Setter).
137 has_logging = (activity_logging.startswith('For') or
138 (access_type and activity_logging.startswith(access_type)))
141 includes.add('bindings/v8/V8DOMActivityLogger.h')
142 if activity_logging.endswith('ForIsolatedWorlds'):
144 return set(['', 'ForMainWorld']) # endswith('ForAllWorlds')
148 CALL_WITH_ARGUMENTS = {
149 'ScriptState': '&state',
150 'ExecutionContext': 'scriptContext',
151 'ScriptArguments': 'scriptArguments.release()',
152 'ActiveWindow': 'activeDOMWindow(info.GetIsolate())',
153 'FirstWindow': 'firstDOMWindow(info.GetIsolate())',
155 # List because key order matters, as we want arguments in deterministic order
165 def call_with_arguments(member, call_with_values=None):
166 # Optional parameter so setter can override with [SetterCallWith]
167 call_with_values = call_with_values or member.extended_attributes.get('CallWith')
168 if not call_with_values:
170 return [CALL_WITH_ARGUMENTS[value]
171 for value in CALL_WITH_VALUES
172 if extended_attribute_value_contains(call_with_values, value)]
176 def conditional_string(definition_or_member):
177 extended_attributes = definition_or_member.extended_attributes
178 if 'Conditional' not in extended_attributes:
180 conditional = extended_attributes['Conditional']
181 for operator in '&|':
182 if operator in conditional:
183 conditions = conditional.split(operator)
184 operator_separator = ' %s%s ' % (operator, operator)
185 return operator_separator.join('ENABLE(%s)' % expression for expression in sorted(conditions))
186 return 'ENABLE(%s)' % conditional
190 def deprecate_as(member):
191 extended_attributes = member.extended_attributes
192 if 'DeprecateAs' not in extended_attributes:
194 includes.add('core/frame/UseCounter.h')
195 return extended_attributes['DeprecateAs']
199 def cpp_name(definition_or_member):
200 extended_attributes = definition_or_member.extended_attributes
201 if 'ImplementedAs' not in extended_attributes:
202 return definition_or_member.name
203 return extended_attributes['ImplementedAs']
207 def measure_as(definition_or_member):
208 extended_attributes = definition_or_member.extended_attributes
209 if 'MeasureAs' not in extended_attributes:
211 includes.add('core/frame/UseCounter.h')
212 return extended_attributes['MeasureAs']
215 # [PerContextEnabled]
216 def per_context_enabled_function_name(definition_or_member):
217 extended_attributes = definition_or_member.extended_attributes
218 if 'PerContextEnabled' not in extended_attributes:
220 feature_name = extended_attributes['PerContextEnabled']
221 return 'ContextFeatures::%sEnabled' % uncapitalize(feature_name)
225 def runtime_enabled_function_name(definition_or_member):
226 """Returns the name of the RuntimeEnabledFeatures function.
228 The returned function checks if a method/attribute is enabled.
229 Given extended attribute RuntimeEnabled=FeatureName, return:
230 RuntimeEnabledFeatures::{featureName}Enabled
232 extended_attributes = definition_or_member.extended_attributes
233 if 'RuntimeEnabled' not in extended_attributes:
235 feature_name = extended_attributes['RuntimeEnabled']
236 return 'RuntimeEnabledFeatures::%sEnabled' % uncapitalize(feature_name)