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

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/mojom_translator.py

Issue 1767033002: Mojom parser: Compute and validate struct field ordinals. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Maintain the property that the fields of a module.Struct are sorted in declaration order. Created 4 years, 9 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 2015 The Chromium Authors. All rights reserved. 2 # Copyright 2015 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 # This module is responsible for translating a MojomFileGraph (see 6 # This module is responsible for translating a MojomFileGraph (see
7 # mojom_files.mojom) to one or more module.Module. 7 # mojom_files.mojom) to one or more module.Module.
8 # 8 #
9 # This module takes the output of the mojom parser, a MojomFileGraph and 9 # This module takes the output of the mojom parser, a MojomFileGraph and
10 # translates it to the input of the code generators, a module.Module object. 10 # translates it to the input of the code generators, a module.Module object.
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 """Populates a module.Struct based on a MojomStruct. 183 """Populates a module.Struct based on a MojomStruct.
184 184
185 Args: 185 Args:
186 struct: {module.Struct} to be populated. 186 struct: {module.Struct} to be populated.
187 mojom_type: {UserDefinedType} referring to the MojomStruct to be 187 mojom_type: {UserDefinedType} referring to the MojomStruct to be
188 translated. 188 translated.
189 """ 189 """
190 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.struct_type 190 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.struct_type
191 mojom_struct = mojom_type.struct_type 191 mojom_struct = mojom_type.struct_type
192 self.PopulateUserDefinedType(struct, mojom_struct) 192 self.PopulateUserDefinedType(struct, mojom_struct)
193 struct.fields = [self.StructFieldFromMojom(f) for f in mojom_struct.fields] 193 # mojom_struct.fields is indexed by the field ordinals. We want
194 # to capture these ordinals but sort struct.fields by declaration_order.
195 struct.fields = [self.StructFieldFromMojom(ordinal, f) for (ordinal, f) in
196 enumerate(mojom_struct.fields)]
197 struct.fields.sort(key=lambda field: field.declaration_order)
194 self.PopulateContainedDeclarationsFromMojom( 198 self.PopulateContainedDeclarationsFromMojom(
195 struct, mojom_struct.decl_data.contained_declarations) 199 struct, mojom_struct.decl_data.contained_declarations)
196 200
197 def UnionFieldFromMojom(self, mojom_field): 201 def UnionFieldFromMojom(self, mojom_field):
198 """Translates a mojom_types_mojom.UnionField to a module.UnionField. 202 """Translates a mojom_types_mojom.UnionField to a module.UnionField.
199 203
200 Args: 204 Args:
201 mojom_field: {mojom_types_mojom.UnionField} to be translated. 205 mojom_field: {mojom_types_mojom.UnionField} to be translated.
202 206
203 Returns: 207 Returns:
204 {module.UnionField} translated from mojom_field. 208 {module.UnionField} translated from mojom_field.
205 """ 209 """
206 union_field = module.UnionField() 210 union_field = module.UnionField()
207 self.PopulateCommonFieldValues(union_field, mojom_field) 211 self.PopulateCommonFieldValues(union_field, mojom_field)
208 union_field.ordinal = self.OrdinalFromMojom(mojom_field) 212 union_field.ordinal = self.OrdinalFromMojom(mojom_field)
209 return union_field 213 return union_field
210 214
211 def StructFieldFromMojom(self, mojom_field): 215 def StructFieldFromMojom(self, ordinal, mojom_field):
212 """Translates a mojom_types_mojom.StructField to a module.StructField. 216 """Translates a mojom_types_mojom.StructField to a module.StructField.
213 217
214 Args: 218 Args:
219 ordinal: {int} The 0-based ordinal position of the field within the
220 struct. Note that this is not necessarily the same as the lexical
221 order or the packing order.
215 mojom_field: {mojom_types_mojom.StructField} to be translated. 222 mojom_field: {mojom_types_mojom.StructField} to be translated.
216 223
217 Returns: 224 Returns:
218 {module.StructField} translated from mojom_field. 225 {module.StructField} translated from mojom_field.
219 """ 226 """
220 struct_field = module.StructField() 227 struct_field = module.StructField()
221 self.PopulateCommonFieldValues(struct_field, mojom_field) 228 self.PopulateCommonFieldValues(struct_field, mojom_field)
229 # Note that the |ordinal| attribute of |struct_field| records only the
230 # *declared* ordinal and as such is not defined for every field whereas
231 # the |computed_ordinal| attribute is defined for every field. If
232 # |ordinal| is defined then it is equal to |computed_ordinal|.
222 struct_field.ordinal = self.OrdinalFromMojom(mojom_field) 233 struct_field.ordinal = self.OrdinalFromMojom(mojom_field)
234 struct_field.computed_ordinal = ordinal
235 struct_field.declaration_order = mojom_field.decl_data.declaration_order
223 if mojom_field.default_value: 236 if mojom_field.default_value:
224 if (mojom_field.default_value.tag == 237 if (mojom_field.default_value.tag ==
225 mojom_types_mojom.DefaultFieldValue.Tags.default_keyword): 238 mojom_types_mojom.DefaultFieldValue.Tags.default_keyword):
226 struct_field.default = 'default' 239 struct_field.default = 'default'
227 else: 240 else:
228 struct_field.default = self.ValueFromMojom( 241 struct_field.default = self.ValueFromMojom(
229 mojom_field.default_value.value) 242 mojom_field.default_value.value)
230 243
231 return struct_field 244 return struct_field
232 245
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 821
809 Args: 822 Args:
810 graph: {mojom_types_mojom.MojomFileGraph} to be translated. 823 graph: {mojom_types_mojom.MojomFileGraph} to be translated.
811 824
812 Return: 825 Return:
813 {dict<str, module.Module>} mapping the file's name to its module.Module 826 {dict<str, module.Module>} mapping the file's name to its module.Module
814 translation for all files in graph.files. 827 translation for all files in graph.files.
815 """ 828 """
816 return {file_name: FileTranslator(graph, file_name).Translate() 829 return {file_name: FileTranslator(graph, file_name).Translate()
817 for file_name in graph.files} 830 for file_name in graph.files}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698