Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: third_party/WebKit/Source/build/scripts/make_computed_style_base.py

Issue 2879563002: Refactor code generation to allow us to diff generic expressions and not just fields (Closed)
Patch Set: shend@'s changes Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 self.type_name = class_name(join_name('style', name, 'data')) 67 self.type_name = class_name(join_name('style', name, 'data'))
68 self.member_name = class_member_name(join_name(name, 'data')) 68 self.member_name = class_member_name(join_name(name, 'data'))
69 self.num_32_bit_words_for_bit_fields = _num_32_bit_words_for_bit_fields( 69 self.num_32_bit_words_for_bit_fields = _num_32_bit_words_for_bit_fields(
70 field for field in fields if field.is_bit_field 70 field for field in fields if field.is_bit_field
71 ) 71 )
72 72
73 # Recursively get all the fields in the subgroups as well 73 # Recursively get all the fields in the subgroups as well
74 self.all_fields = _flatten_list(subgroup.all_fields for subgroup in subg roups) + fields 74 self.all_fields = _flatten_list(subgroup.all_fields for subgroup in subg roups) + fields
75 75
76 76
77 class DiffGroup(object):
78 """Represents a group of expressions and subgroups that need to be diffed
79 for a function in ComputedStyle.
80
81 Attributes:
82 subgroups: List of DiffGroup instances that are stored as subgroups unde r this group.
83 expressions: List of expression that are on this group that need to be d iffed.
84 """
85 def __init__(self, group_name):
86 self.group_name = group_name
87 self.subgroups = []
88 self.expressions = []
89
77 class Field(object): 90 class Field(object):
78 """ 91 """
79 The generated ComputedStyle object is made up of a series of Fields. 92 The generated ComputedStyle object is made up of a series of Fields.
80 Each Field has a name, size, type, etc, and a bunch of attributes to 93 Each Field has a name, size, type, etc, and a bunch of attributes to
81 determine which methods it will be used in. 94 determine which methods it will be used in.
82 95
83 A Field also has enough information to use any storage type in C++, such as 96 A Field also has enough information to use any storage type in C++, such as
84 regular member variables, or more complex storage like vectors or hashmaps. 97 regular member variables, or more complex storage like vectors or hashmaps.
85 Almost all properties will have at least one Field, often more than one. 98 Almost all properties will have at least one Field, often more than one.
86 99
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 145
133 # Method names 146 # Method names
134 # TODO(nainar): Method name generation is inconsistent. Fix. 147 # TODO(nainar): Method name generation is inconsistent. Fix.
135 self.getter_method_name = getter_method_name 148 self.getter_method_name = getter_method_name
136 self.setter_method_name = setter_method_name 149 self.setter_method_name = setter_method_name
137 self.internal_getter_method_name = method_name(join_name(getter_method_n ame, 'Internal')) 150 self.internal_getter_method_name = method_name(join_name(getter_method_n ame, 'Internal'))
138 self.internal_mutable_method_name = method_name(join_name('Mutable', nam e_for_methods, 'Internal')) 151 self.internal_mutable_method_name = method_name(join_name('Mutable', nam e_for_methods, 'Internal'))
139 self.internal_setter_method_name = method_name(join_name(setter_method_n ame, 'Internal')) 152 self.internal_setter_method_name = method_name(join_name(setter_method_n ame, 'Internal'))
140 self.initial_method_name = initial_method_name 153 self.initial_method_name = initial_method_name
141 self.resetter_method_name = method_name(join_name('Reset', name_for_meth ods)) 154 self.resetter_method_name = method_name(join_name('Reset', name_for_meth ods))
155 if self.group_name:
156 self.getter_expression = self.group_name + '_data_->' + class_member _name(self.name)
shend 2017/05/11 06:11:27 I think you can use 'self.group_member_name' inste
nainar 2017/05/11 06:32:38 Done.
157 else:
158 self.getter_expression = class_member_name(self.name)
142 159
143 # If the size of the field is not None, it means it is a bit field 160 # If the size of the field is not None, it means it is a bit field
144 self.is_bit_field = self.size is not None 161 self.is_bit_field = self.size is not None
145 162
146 assert len(kwargs) == 0, 'Unexpected arguments provided to Field: ' + st r(kwargs) 163 assert len(kwargs) == 0, 'Unexpected arguments provided to Field: ' + st r(kwargs)
147 164
148 165
149 def _get_include_paths(properties): 166 def _get_include_paths(properties):
150 """ 167 """
151 Get a list of paths that need to be included for ComputedStyleBase. 168 Get a list of paths that need to be included for ComputedStyleBase.
152 """ 169 """
153 include_paths = set() 170 include_paths = set()
154 for property_ in properties: 171 for property_ in properties:
155 if property_['field_type_path'] is not None: 172 if property_['field_type_path'] is not None:
156 include_paths.add(property_['field_type_path'] + '.h') 173 include_paths.add(property_['field_type_path'] + '.h')
157 return list(sorted(include_paths)) 174 return list(sorted(include_paths))
158 175
159 176
160 def _group_fields(fields): 177 def _group_fields(fields):
161 """Groups a list of fields by their group_name and returns the root group."" " 178 """Groups a list of fields by their group_name and returns the root group."" "
162 groups = defaultdict(list) 179 groups = defaultdict(list)
163 for field in fields: 180 for field in fields:
164 groups[field.group_name].append(field) 181 groups[field.group_name].append(field)
165 182
166 no_group = groups.pop(None) 183 no_group = {}
shend 2017/05/11 06:11:27 No longer needed I believe.
nainar 2017/05/11 06:32:38 Done.
184 if groups.get(None):
185 no_group = groups.pop(None)
167 subgroups = [Group(group_name, [], _reorder_fields(fields)) for group_name, fields in groups.items()] 186 subgroups = [Group(group_name, [], _reorder_fields(fields)) for group_name, fields in groups.items()]
168 return Group('', subgroups=subgroups, fields=_reorder_fields(no_group)) 187 return Group('', subgroups=subgroups, fields=_reorder_fields(no_group))
169 188
170 189
190 def _create_diff_groups_map(diff_function_inputs, root_group):
191 diff_functions_map = {}
192 for entry in diff_function_inputs:
193 diff_functions_map[entry['name']] = _create_diff_groups(entry['fields'], root_group)
194 return diff_functions_map
195
196
197 def _create_diff_groups(fields_to_diff, root_group):
198 diff_group = DiffGroup(root_group.member_name)
199 for subgroup in root_group.subgroups:
200 for field in subgroup.all_fields:
shend 2017/05/11 06:11:27 This is the intersection code right? I think you c
nainar 2017/05/11 06:32:38 Done. With protest
201 if field.property_name in fields_to_diff:
202 diff_group.subgroups.append(_create_diff_groups(fields_to_diff, subgroup))
203 break
204 for field in root_group.fields:
205 if field.property_name in fields_to_diff:
206 diff_group.expressions.append(field.getter_expression)
207 return diff_group
208
209
171 def _create_enums(properties): 210 def _create_enums(properties):
172 """ 211 """
173 Returns an OrderedDict of enums to be generated, enum name -> [list of enum values] 212 Returns an OrderedDict of enums to be generated, enum name -> [list of enum values]
174 """ 213 """
175 enums = {} 214 enums = {}
176 for property_ in properties: 215 for property_ in properties:
177 # Only generate enums for keyword properties that use the default field_ type_path. 216 # Only generate enums for keyword properties that use the default field_ type_path.
178 if property_['field_template'] == 'keyword' and property_['field_type_pa th'] is None: 217 if property_['field_template'] == 'keyword' and property_['field_type_pa th'] is None:
179 enum_name = property_['type_name'] 218 enum_name = property_['type_name']
180 enum_values = [enum_value_name(k) for k in property_['keywords']] 219 enum_values = [enum_value_name(k) for k in property_['keywords']]
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 if property_['field_type_path']: 405 if property_['field_type_path']:
367 property_['type_name'] = property_['field_type_path'].split('/') [-1] 406 property_['type_name'] = property_['field_type_path'].split('/') [-1]
368 407
369 self._generated_enums = _create_enums(all_properties) 408 self._generated_enums = _create_enums(all_properties)
370 409
371 all_fields = _create_fields(all_properties) 410 all_fields = _create_fields(all_properties)
372 411
373 # Organise fields into a tree structure where the root group 412 # Organise fields into a tree structure where the root group
374 # is ComputedStyleBase. 413 # is ComputedStyleBase.
375 self._root_group = _group_fields(all_fields) 414 self._root_group = _group_fields(all_fields)
415 self._diff_functions_map = _create_diff_groups_map(json5_generator.Json5 File.load_from_files(
416 [json5_file_paths[2]]
417 ).name_dictionaries, self._root_group)
376 418
377 self._include_paths = _get_include_paths(all_properties) 419 self._include_paths = _get_include_paths(all_properties)
378 self._outputs = { 420 self._outputs = {
379 'ComputedStyleBase.h': self.generate_base_computed_style_h, 421 'ComputedStyleBase.h': self.generate_base_computed_style_h,
380 'ComputedStyleBase.cpp': self.generate_base_computed_style_cpp, 422 'ComputedStyleBase.cpp': self.generate_base_computed_style_cpp,
381 'ComputedStyleBaseConstants.h': self.generate_base_computed_style_co nstants, 423 'ComputedStyleBaseConstants.h': self.generate_base_computed_style_co nstants,
382 } 424 }
383 425
384 @template_expander.use_jinja('ComputedStyleBase.h.tmpl', tests={'in': lambda a, b: a in b}) 426 @template_expander.use_jinja('ComputedStyleBase.h.tmpl', tests={'in': lambda a, b: a in b})
385 def generate_base_computed_style_h(self): 427 def generate_base_computed_style_h(self):
386 return { 428 return {
387 'properties': self._properties, 429 'properties': self._properties,
388 'enums': self._generated_enums, 430 'enums': self._generated_enums,
389 'include_paths': self._include_paths, 431 'include_paths': self._include_paths,
390 'computed_style': self._root_group, 432 'computed_style': self._root_group,
391 } 433 }
392 434
393 @template_expander.use_jinja('ComputedStyleBase.cpp.tmpl', tests={'in': lamb da a, b: a in b}) 435 @template_expander.use_jinja('ComputedStyleBase.cpp.tmpl', tests={'in': lamb da a, b: a in b})
394 def generate_base_computed_style_cpp(self): 436 def generate_base_computed_style_cpp(self):
395 return { 437 return {
396 'properties': self._properties, 438 'properties': self._properties,
397 'enums': self._generated_enums, 439 'enums': self._generated_enums,
398 'computed_style': self._root_group, 440 'computed_style': self._root_group,
441 'diff_functions_map': self._diff_functions_map,
399 } 442 }
400 443
401 @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl') 444 @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl')
402 def generate_base_computed_style_constants(self): 445 def generate_base_computed_style_constants(self):
403 return { 446 return {
404 'properties': self._properties, 447 'properties': self._properties,
405 'enums': self._generated_enums, 448 'enums': self._generated_enums,
406 } 449 }
407 450
408 if __name__ == '__main__': 451 if __name__ == '__main__':
409 json5_generator.Maker(ComputedStyleBaseWriter).main() 452 json5_generator.Maker(ComputedStyleBaseWriter).main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698