Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env python
      2 
      3 import os.path
      4 import re
      5 import subprocess
      6 import sys
      7 
      8 from in_file import InFile
      9 import in_generator
     10 import license
     11 
     12 
     13 HEADER_TEMPLATE = """
     14 %(license)s
     15 
     16 #ifndef %(class_name)s_h
     17 #define %(class_name)s_h
     18 
     19 #include <string.h>
     20 #include "wtf/HashFunctions.h"
     21 #include "wtf/HashTraits.h"
     22 
     23 namespace WTF {
     24 class AtomicString;
     25 class String;
     26 }
     27 
     28 namespace WebCore {
     29 
     30 enum CSSPropertyID {
     31     CSSPropertyInvalid = 0,
     32     CSSPropertyVariable = 1,
     33 %(property_enums)s
     34 };
     35 
     36 const int firstCSSProperty = %(first_property_id)s;
     37 const int numCSSProperties = %(properties_count)s;
     38 const int lastCSSProperty = %(last_property_id)d;
     39 const size_t maxCSSPropertyNameLength = %(max_name_length)d;
     40 
     41 const char* getPropertyName(CSSPropertyID);
     42 const WTF::AtomicString& getPropertyNameAtomicString(CSSPropertyID);
     43 WTF::String getPropertyNameString(CSSPropertyID);
     44 WTF::String getJSPropertyName(CSSPropertyID);
     45 
     46 inline CSSPropertyID convertToCSSPropertyID(int value)
     47 {
     48     ASSERT((value >= firstCSSProperty && value <= lastCSSProperty) || value == CSSPropertyInvalid);
     49     return static_cast<CSSPropertyID>(value);
     50 }
     51 
     52 } // namespace WebCore
     53 
     54 namespace WTF {
     55 template<> struct DefaultHash<WebCore::CSSPropertyID> { typedef IntHash<unsigned> Hash; };
     56 template<> struct HashTraits<WebCore::CSSPropertyID> : GenericHashTraits<WebCore::CSSPropertyID> {
     57     static const bool emptyValueIsZero = true;
     58     static const bool needsDestruction = false;
     59     static void constructDeletedValue(WebCore::CSSPropertyID& slot) { slot = static_cast<WebCore::CSSPropertyID>(WebCore::lastCSSProperty + 1); }
     60     static bool isDeletedValue(WebCore::CSSPropertyID value) { return value == (WebCore::lastCSSProperty + 1); }
     61 };
     62 }
     63 
     64 #endif // %(class_name)s_h
     65 """
     66 
     67 GPERF_TEMPLATE = """
     68 %%{
     69 %(license)s
     70 
     71 #include "config.h"
     72 #include "%(class_name)s.h"
     73 #include "core/platform/HashTools.h"
     74 #include <string.h>
     75 
     76 #include "wtf/ASCIICType.h"
     77 #include "wtf/text/AtomicString.h"
     78 #include "wtf/text/WTFString.h"
     79 
     80 namespace WebCore {
     81 static const char propertyNameStringsPool[] = {
     82 %(property_name_strings)s
     83 };
     84 
     85 static const unsigned short propertyNameStringsOffsets[] = {
     86 %(property_name_offsets)s
     87 };
     88 
     89 %%}
     90 %%struct-type
     91 struct Property;
     92 %%omit-struct-type
     93 %%language=C++
     94 %%readonly-tables
     95 %%global-table
     96 %%compare-strncmp
     97 %%define class-name %(class_name)sHash
     98 %%define lookup-function-name findPropertyImpl
     99 %%define hash-function-name propery_hash_function
    100 %%define slot-name nameOffset
    101 %%define word-array-name property_wordlist
    102 %%enum
    103 %%%%
    104 %(property_to_enum_map)s
    105 %%%%
    106 const Property* findProperty(register const char* str, register unsigned int len)
    107 {
    108     return %(class_name)sHash::findPropertyImpl(str, len);
    109 }
    110 
    111 const char* getPropertyName(CSSPropertyID id)
    112 {
    113     if (id < firstCSSProperty)
    114         return 0;
    115     int index = id - firstCSSProperty;
    116     if (index >= numCSSProperties)
    117         return 0;
    118     return propertyNameStringsPool + propertyNameStringsOffsets[index];
    119 }
    120 
    121 const AtomicString& getPropertyNameAtomicString(CSSPropertyID id)
    122 {
    123     if (id < firstCSSProperty)
    124         return nullAtom;
    125     int index = id - firstCSSProperty;
    126     if (index >= numCSSProperties)
    127         return nullAtom;
    128 
    129     static AtomicString* propertyStrings = new AtomicString[numCSSProperties]; // Intentionally never destroyed.
    130     AtomicString& propertyString = propertyStrings[index];
    131     if (propertyString.isNull()) {
    132         const char* propertyName = propertyNameStringsPool + propertyNameStringsOffsets[index];
    133         propertyString = AtomicString(propertyName, strlen(propertyName), AtomicString::ConstructFromLiteral);
    134     }
    135     return propertyString;
    136 }
    137 
    138 String getPropertyNameString(CSSPropertyID id)
    139 {
    140     // We share the StringImpl with the AtomicStrings.
    141     return getPropertyNameAtomicString(id).string();
    142 }
    143 
    144 String getJSPropertyName(CSSPropertyID id)
    145 {
    146     char result[maxCSSPropertyNameLength + 1];
    147     const char* cssPropertyName = getPropertyName(id);
    148     const char* propertyNamePointer = cssPropertyName;
    149     if (!propertyNamePointer)
    150         return emptyString();
    151 
    152     char* resultPointer = result;
    153     while (char character = *propertyNamePointer++) {
    154         if (character == '-') {
    155             char nextCharacter = *propertyNamePointer++;
    156             if (!nextCharacter)
    157                 break;
    158             character = (propertyNamePointer - 2 != cssPropertyName) ? toASCIIUpper(nextCharacter) : nextCharacter;
    159         }
    160         *resultPointer++ = character;
    161     }
    162     *resultPointer = '\\0';
    163     return String(result);
    164 }
    165 
    166 } // namespace WebCore
    167 """
    168 
    169 
    170 class CSSPropertiesWriter(in_generator.Writer):
    171     class_name = "CSSPropertyNames"
    172     defaults = {
    173         'alias_for': None,
    174         'condition': None,
    175     }
    176 
    177     def __init__(self, file_paths, enabled_conditions):
    178         in_generator.Writer.__init__(self, file_paths, enabled_conditions)
    179         self._outputs = {(self.class_name + ".h"): self.generate_header,
    180                          (self.class_name + ".cpp"): self.generate_implementation,
    181                         }
    182 
    183         all_properties = self.in_file.name_dictionaries
    184         self._aliases = filter(lambda property: property['alias_for'], all_properties)
    185         for offset, property in enumerate(self._aliases):
    186             # Aliases use the enum_name that they are an alias for.
    187             property['enum_name'] = self._enum_name_from_property_name(property['alias_for'])
    188             # Aliases do not get an enum_value.
    189 
    190         self._properties = filter(lambda property: not property['alias_for'] and not property['condition'] or property['condition'] in self._enabled_conditions, all_properties)
    191         if len(self._properties) > 1024:
    192             print "ERROR : There is more than 1024 CSS Properties, you need to update CSSProperty.h/StylePropertyMetadata m_propertyID accordingly."
    193             exit(1)
    194         self._first_property_id = 2  # We start after CSSPropertyInvalid and CSSPropertyVariable.
    195         property_id = self._first_property_id
    196         for offset, property in enumerate(self._properties):
    197             property['enum_name'] = self._enum_name_from_property_name(property['name'])
    198             property['enum_value'] = self._first_property_id + offset
    199 
    200     def _enum_name_from_property_name(self, property_name):
    201         return "CSSProperty" + re.sub(r'(^[^-])|-(.)', lambda match: (match.group(1) or match.group(2)).upper(), property_name)
    202 
    203     def _enum_declaration(self, property):
    204         return "    %(enum_name)s = %(enum_value)s," % property
    205 
    206     def generate_header(self):
    207         return HEADER_TEMPLATE % {
    208             'license': license.license_for_generated_cpp(),
    209             'class_name': self.class_name,
    210             'property_enums': "\n".join(map(self._enum_declaration, self._properties)),
    211             'first_property_id': self._first_property_id,
    212             'properties_count': len(self._properties),
    213             'last_property_id': self._first_property_id + len(self._properties) - 1,
    214             'max_name_length': reduce(max, map(len, map(lambda property: property['name'], self._properties))),
    215         }
    216 
    217     def generate_implementation(self):
    218         property_offsets = []
    219         current_offset = 0
    220         for property in self._properties:
    221             property_offsets.append(current_offset)
    222             current_offset += len(property["name"]) + 1
    223 
    224         gperf_input = GPERF_TEMPLATE % {
    225             'license': license.license_for_generated_cpp(),
    226             'class_name': self.class_name,
    227             'property_name_strings': '\n'.join(map(lambda property: '    "%(name)s\\0"' % property, self._properties)),
    228             'property_name_offsets': '\n'.join(map(lambda offset: '    %d,' % offset, property_offsets)),
    229             'property_to_enum_map': '\n'.join(map(lambda property: '%(name)s, %(enum_name)s' % property, self._properties + self._aliases)),
    230         }
    231         # FIXME: If we could depend on Python 2.7, we would use subprocess.check_output
    232         gperf_args = ['gperf', '--key-positions=*', '-P', '-D', '-n', '-s', '2']
    233         gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    234         return gperf.communicate(gperf_input)[0]
    235 
    236 
    237 if __name__ == "__main__":
    238     in_generator.Maker(CSSPropertiesWriter).main(sys.argv)
    239