OLD | NEW |
(Empty) | |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # |
| 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are |
| 5 # met: |
| 6 # |
| 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 |
| 12 # distribution. |
| 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. |
| 16 # |
| 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. |
| 28 |
| 29 import os.path |
| 30 import re |
| 31 |
| 32 import in_generator |
| 33 from in_generator import Maker |
| 34 import license |
| 35 |
| 36 |
| 37 HEADER_TEMPLATE = """%(license)s |
| 38 |
| 39 #ifndef %(class_name)sHeaders_h |
| 40 #define %(class_name)sHeaders_h |
| 41 |
| 42 %(includes)s |
| 43 |
| 44 #endif // %(class_name)sHeaders_h |
| 45 """ |
| 46 |
| 47 |
| 48 INTERFACES_HEADER_TEMPLATE = """%(license)s |
| 49 |
| 50 #ifndef %(class_name)sInterfaces_h |
| 51 #define %(class_name)sInterfaces_h |
| 52 |
| 53 %(declare_conditional_macros)s |
| 54 |
| 55 #define %(macro_style_name)s_INTERFACES_FOR_EACH(macro) \\ |
| 56 \\ |
| 57 %(unconditional_macros)s |
| 58 \\ |
| 59 %(conditional_macros)s |
| 60 |
| 61 #endif // %(class_name)sInterfaces_h |
| 62 """ |
| 63 |
| 64 |
| 65 def _to_macro_style(name): |
| 66 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) |
| 67 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper() |
| 68 |
| 69 |
| 70 def _name_for_entry(entry): |
| 71 if entry['interfaceName']: |
| 72 return entry['interfaceName'] |
| 73 return os.path.basename(entry['name']) |
| 74 |
| 75 class Writer(in_generator.Writer): |
| 76 def __init__(self, in_file_path, enabled_conditions): |
| 77 super(Writer, self).__init__(in_file_path, enabled_conditions) |
| 78 self.class_name = self.in_file.parameters['namespace'].strip('"') |
| 79 self._entries_by_conditional = {} |
| 80 self._unconditional_entries = [] |
| 81 self._sort_entries_by_conditional() |
| 82 |
| 83 def _sort_entries_by_conditional(self): |
| 84 unconditional_names = set() |
| 85 for entry in self.in_file.name_dictionaries: |
| 86 conditional = entry['conditional'] |
| 87 if not conditional: |
| 88 name = _name_for_entry(entry) |
| 89 if name in unconditional_names: |
| 90 continue |
| 91 unconditional_names.add(name) |
| 92 self._unconditional_entries.append(entry) |
| 93 continue |
| 94 for entry in self.in_file.name_dictionaries: |
| 95 name = _name_for_entry(entry) |
| 96 if name in unconditional_names: |
| 97 continue |
| 98 conditional = entry['conditional'] |
| 99 if not conditional in self._entries_by_conditional: |
| 100 self._entries_by_conditional[conditional] = [] |
| 101 self._entries_by_conditional[conditional].append(entry) |
| 102 |
| 103 def _headers_header_include(self, entry): |
| 104 path = entry['name'] |
| 105 name = _name_for_entry(entry) |
| 106 if entry['interfaceName']: |
| 107 path = entry['interfaceName'] # FIXME: This seems wrong |
| 108 include = '#include "%(path)s.h"\n#include "V8%(name)s.h"' % { |
| 109 'path': path, |
| 110 'name': name, |
| 111 } |
| 112 return self.wrap_with_condition(include, entry['conditional']) |
| 113 |
| 114 def generate_headers_header(self): |
| 115 return HEADER_TEMPLATE % { |
| 116 'license': license.license_for_generated_cpp(), |
| 117 'class_name': self.class_name, |
| 118 'includes': '\n'.join(map(self._headers_header_include, self.in_file
.name_dictionaries)), |
| 119 } |
| 120 |
| 121 def _declare_one_conditional_macro(self, conditional, entries): |
| 122 macro_name = '%(macro_style_name)s_INTERFACES_FOR_EACH_%(conditional)s'
% { |
| 123 'macro_style_name': _to_macro_style(self.class_name), |
| 124 'conditional': conditional, |
| 125 } |
| 126 return self.wrap_with_condition("""#define %(macro_name)s(macro) \\ |
| 127 %(declarations)s |
| 128 |
| 129 #else |
| 130 #define %(macro_name)s(macro)""" % { |
| 131 'macro_name': macro_name, |
| 132 'declarations': '\n'.join(sorted(set([ |
| 133 ' macro(%(name)s) \\' % {'name': _name_for_entry(entry)} |
| 134 for entry in entries]))), |
| 135 }, conditional) |
| 136 |
| 137 def _declare_conditional_macros(self): |
| 138 return '\n'.join([ |
| 139 self._declare_one_conditional_macro(conditional, entries) |
| 140 for conditional, entries in self._entries_by_conditional.items()]) |
| 141 |
| 142 def _unconditional_macro(self, entry): |
| 143 return ' macro(%(name)s) \\' % {'name': _name_for_entry(entry)} |
| 144 |
| 145 def _conditional_macros(self, conditional): |
| 146 return ' %(macro_style_name)s_INTERFACES_FOR_EACH_%(conditional)s(mac
ro) \\' % { |
| 147 'macro_style_name': _to_macro_style(self.class_name), |
| 148 'conditional': conditional, |
| 149 } |
| 150 |
| 151 def generate_interfaces_header(self): |
| 152 return INTERFACES_HEADER_TEMPLATE % { |
| 153 'license': license.license_for_generated_cpp(), |
| 154 'class_name': self.class_name, |
| 155 'macro_style_name': _to_macro_style(self.class_name), |
| 156 'declare_conditional_macros': self._declare_conditional_macros(), |
| 157 'unconditional_macros': '\n'.join(sorted(set(map(self._unconditional
_macro, self._unconditional_entries)))), |
| 158 'conditional_macros': '\n'.join(map(self._conditional_macros, self._
entries_by_conditional.keys())), |
| 159 } |
OLD | NEW |