OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import math | 6 import math |
7 import sys | 7 import sys |
8 | 8 |
9 import json5_generator | 9 import json5_generator |
10 import template_expander | 10 import template_expander |
11 import make_style_builder | 11 import make_style_builder |
12 | 12 |
13 from name_utilities import camel_case, lower_first | 13 from name_utilities import camel_case, lower_first, upper_first_letter |
| 14 |
| 15 |
| 16 # Temporary hard-coded list of fields that are not CSS properties. |
| 17 # Ideally these would be specified in a .in or .json5 file. |
| 18 NONPROPERTY_FIELDS = set([ |
| 19 # Style can not be shared. |
| 20 'unique', |
| 21 ]) |
14 | 22 |
15 | 23 |
16 class Field(object): | 24 class Field(object): |
17 """ | 25 """ |
18 The generated ComputedStyle object is made up of a series of Fields. | 26 The generated ComputedStyle object is made up of a series of Fields. |
19 Each Field has a name, size, type, etc, and a bunch of attributes to | 27 Each Field has a name, size, type, etc, and a bunch of attributes to |
20 determine which methods it will be used in. | 28 determine which methods it will be used in. |
21 | 29 |
22 A Field also has enough information to use any storage type in C++, such as | 30 A Field also has enough information to use any storage type in C++, such as |
23 regular member variables, or more complex storage like vectors or hashmaps. | 31 regular member variables, or more complex storage like vectors or hashmaps. |
(...skipping 29 matching lines...) Expand all Loading... |
53 | 61 |
54 def __init__(self, field_family, **kwargs): | 62 def __init__(self, field_family, **kwargs): |
55 # Values common to all fields | 63 # Values common to all fields |
56 # Set attributes from the keyword arguments | 64 # Set attributes from the keyword arguments |
57 for attrib in Field.REQUIRED_ATTRIBUTES: | 65 for attrib in Field.REQUIRED_ATTRIBUTES: |
58 setattr(self, attrib, kwargs.pop(attrib)) | 66 setattr(self, attrib, kwargs.pop(attrib)) |
59 | 67 |
60 # Field family: one of these must be true | 68 # Field family: one of these must be true |
61 self.is_property = field_family == 'property' | 69 self.is_property = field_family == 'property' |
62 self.is_inherited_flag = field_family == 'inherited_flag' | 70 self.is_inherited_flag = field_family == 'inherited_flag' |
63 assert (self.is_property, self.is_inherited_flag).count(True) == 1, \ | 71 self.is_nonproperty = field_family == 'nonproperty' |
64 'Field family has to be exactly one of: property, inherited_flag' | 72 assert (self.is_property, self.is_inherited_flag, self.is_nonproperty).c
ount(True) == 1, \ |
| 73 'Field family has to be exactly one of: property, inherited_flag, no
nproperty' |
65 | 74 |
66 if self.is_property: | 75 if self.is_property: |
67 self.is_inherited = kwargs.pop('inherited') | 76 self.is_inherited = kwargs.pop('inherited') |
68 self.is_independent = kwargs.pop('independent') | 77 self.is_independent = kwargs.pop('independent') |
69 assert self.is_inherited or not self.is_independent, 'Only inherited
fields can be independent' | 78 assert self.is_inherited or not self.is_independent, 'Only inherited
fields can be independent' |
70 | 79 |
71 self.is_inherited_method_name = kwargs.pop('is_inherited_method_name
') | 80 self.is_inherited_method_name = kwargs.pop('is_inherited_method_name
') |
72 elif self.is_inherited_flag: | 81 elif self.is_inherited_flag: |
73 # Inherited flag-only fields | 82 # Inherited flag-only fields |
74 pass | 83 pass |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 storage_type_path=None, | 170 storage_type_path=None, |
162 size=1, | 171 size=1, |
163 default_value='true', | 172 default_value='true', |
164 getter_method_name=field_name_suffix_lower, | 173 getter_method_name=field_name_suffix_lower, |
165 setter_method_name='set' + field_name_suffix_upper, | 174 setter_method_name='set' + field_name_suffix_upper, |
166 initial_method_name='initial' + field_name_suffix_upper, | 175 initial_method_name='initial' + field_name_suffix_upper, |
167 resetter_method_name='reset' + field_name_suffix_upper, | 176 resetter_method_name='reset' + field_name_suffix_upper, |
168 ) | 177 ) |
169 | 178 |
170 | 179 |
| 180 def _create_nonproperty_field(field_name): |
| 181 """ |
| 182 Create a nonproperty field from its name and return the Field object. |
| 183 """ |
| 184 member_name = 'm_' + field_name |
| 185 field_name_upper = upper_first_letter(field_name) |
| 186 |
| 187 return Field( |
| 188 'nonproperty', |
| 189 name=member_name, |
| 190 property_name=field_name, |
| 191 storage_type='bool', |
| 192 storage_type_path=None, |
| 193 size=1, |
| 194 default_value='false', |
| 195 getter_method_name=field_name, |
| 196 setter_method_name='set' + field_name_upper, |
| 197 initial_method_name='initial' + field_name_upper, |
| 198 resetter_method_name='reset' + field_name_upper, |
| 199 ) |
| 200 |
| 201 |
171 def _create_fields(properties): | 202 def _create_fields(properties): |
172 """ | 203 """ |
173 Create ComputedStyle fields from CSS properties and return a list of Field o
bjects. | 204 Create ComputedStyle fields from CSS properties and return a list of Field o
bjects. |
174 """ | 205 """ |
175 fields = [] | 206 fields = [] |
176 for property_ in properties: | 207 for property_ in properties: |
177 # Keywords only means we generate an enum field. | 208 # Keywords only means we generate an enum field. |
178 if property_['keyword_only']: | 209 if property_['keyword_only']: |
179 # If the property is independent, add the single-bit sized isInherit
ed flag | 210 # If the property is independent, add the single-bit sized isInherit
ed flag |
180 # to the list of Fields as well. | 211 # to the list of Fields as well. |
181 if property_['independent']: | 212 if property_['independent']: |
182 fields.append(_create_inherited_flag_field(property_)) | 213 fields.append(_create_inherited_flag_field(property_)) |
183 | 214 |
184 fields.append(_create_property_field(property_)) | 215 fields.append(_create_property_field(property_)) |
185 | 216 |
| 217 for field_name in NONPROPERTY_FIELDS: |
| 218 fields.append(_create_nonproperty_field(field_name)) |
| 219 |
186 return fields | 220 return fields |
187 | 221 |
188 | 222 |
189 def _pack_fields(fields): | 223 def _pack_fields(fields): |
190 """ | 224 """ |
191 Group a list of fields into buckets to minimise padding. | 225 Group a list of fields into buckets to minimise padding. |
192 Returns a list of buckets, where each bucket is a list of Field objects. | 226 Returns a list of buckets, where each bucket is a list of Field objects. |
193 """ | 227 """ |
194 # Since fields cannot cross word boundaries, in order to minimize | 228 # Since fields cannot cross word boundaries, in order to minimize |
195 # padding, group fields into buckets so that as many buckets as possible | 229 # padding, group fields into buckets so that as many buckets as possible |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 def generate_base_computed_style_constants(self): | 320 def generate_base_computed_style_constants(self): |
287 return { | 321 return { |
288 'properties': self._properties, | 322 'properties': self._properties, |
289 'enums': self._generated_enums, | 323 'enums': self._generated_enums, |
290 'fields': self._fields, | 324 'fields': self._fields, |
291 'expected_total_field_bytes': self._expected_total_field_bytes, | 325 'expected_total_field_bytes': self._expected_total_field_bytes, |
292 } | 326 } |
293 | 327 |
294 if __name__ == '__main__': | 328 if __name__ == '__main__': |
295 json5_generator.Maker(ComputedStyleBaseWriter).main() | 329 json5_generator.Maker(ComputedStyleBaseWriter).main() |
OLD | NEW |