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

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

Issue 1417473002: Improves the representation of values in Mojom. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Remove some TODOs. Created 5 years, 2 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 mod = self._module 58 mod = self._module
59 self.PopulateModuleMetadata(mod, mojom_file) 59 self.PopulateModuleMetadata(mod, mojom_file)
60 mod.imports = [self.ImportFromMojom(imp) for imp in mojom_file.imports] 60 mod.imports = [self.ImportFromMojom(imp) for imp in mojom_file.imports]
61 # TODO(azani): The key should be equal to SourceFileInfo.file_name of 61 # TODO(azani): The key should be equal to SourceFileInfo.file_name of
62 # imported types. 62 # imported types.
63 self._imports = {imp['module'].path: imp for imp in mod.imports} 63 self._imports = {imp['module'].path: imp for imp in mod.imports}
64 64
65 if mojom_file.declared_mojom_objects: 65 if mojom_file.declared_mojom_objects:
66 if mojom_file.declared_mojom_objects.top_level_constants: 66 if mojom_file.declared_mojom_objects.top_level_constants:
67 mod.constants = [ 67 mod.constants = [
68 self.ConstFromMojom(self._graph.resolved_constants[key], None) 68 self.ConstFromMojom(
69 self._graph.resolved_values[key].declared_constant, None)
69 for key in mojom_file.declared_mojom_objects.top_level_constants] 70 for key in mojom_file.declared_mojom_objects.top_level_constants]
70 71
71 user_defined_types = ['interfaces', 'structs', 'unions'] 72 user_defined_types = ['interfaces', 'structs', 'unions']
72 for user_defined_type in user_defined_types: 73 for user_defined_type in user_defined_types:
73 if getattr(mojom_file.declared_mojom_objects, user_defined_type): 74 if getattr(mojom_file.declared_mojom_objects, user_defined_type):
74 setattr(mod, user_defined_type, [self.UserDefinedFromTypeKey(key) 75 setattr(mod, user_defined_type, [self.UserDefinedFromTypeKey(key)
75 for key in getattr( 76 for key in getattr(
76 mojom_file.declared_mojom_objects, user_defined_type)]) 77 mojom_file.declared_mojom_objects, user_defined_type)])
77 if mojom_file.declared_mojom_objects.top_level_enums: 78 if mojom_file.declared_mojom_objects.top_level_enums:
78 mod.enums = [self.UserDefinedFromTypeKey(key) 79 mod.enums = [self.UserDefinedFromTypeKey(key)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 Args: 164 Args:
164 mojom_field: {mojom_types_mojom.StructField} to be translated. 165 mojom_field: {mojom_types_mojom.StructField} to be translated.
165 166
166 Returns: 167 Returns:
167 {module.StructField} translated from mojom_field. 168 {module.StructField} translated from mojom_field.
168 """ 169 """
169 struct_field = module.StructField() 170 struct_field = module.StructField()
170 self.PopulateCommonFieldValues(struct_field, mojom_field) 171 self.PopulateCommonFieldValues(struct_field, mojom_field)
171 struct_field.ordinal = self.OrdinalFromMojom(mojom_field) 172 struct_field.ordinal = self.OrdinalFromMojom(mojom_field)
172 if mojom_field.default_value: 173 if mojom_field.default_value:
173 struct_field.default = self.EvalConst(mojom_field.default_value) 174 if (mojom_field.default_value.tag ==
175 mojom_types_mojom.DefaultFieldValue.Tags.default_keyword):
176 struct_field.default = 'default'
177 else:
178 struct_field.default = self.EvalValue(
179 mojom_field.default_value.value)
174 180
175 return struct_field 181 return struct_field
176 182
177 def ParamFromMojom(self, mojom): 183 def ParamFromMojom(self, mojom):
178 """Translates a mojom_types_mojom.StructField to a module.Parameter. 184 """Translates a mojom_types_mojom.StructField to a module.Parameter.
179 185
180 The parameters passed to and returned from a method are expressed as a 186 The parameters passed to and returned from a method are expressed as a
181 struct. The struct fields in the struct are the parameters. 187 struct. The struct fields in the struct are the parameters.
182 188
183 Args: 189 Args:
184 mojom: {mojom_types_mojom.StructField} representing a method parameter. 190 mojom: {mojom_types_mojom.StructField} representing a method parameter.
185 191
186 Returns: 192 Returns:
187 {module.Parameter} translated from the mojom. 193 {module.Parameter} translated from the mojom.
188 """ 194 """
189 param = module.Parameter() 195 param = module.Parameter()
190 param.ordinal = self.OrdinalFromMojom(mojom) 196 param.ordinal = self.OrdinalFromMojom(mojom)
191 if mojom.default_value:
192 param.default = self.EvalConst(mojom.default_value)
193 self.PopulateCommonFieldValues(param, mojom) 197 self.PopulateCommonFieldValues(param, mojom)
194 return param 198 return param
195 199
196 def PopulateCommonFieldValues(self, field, mojom_field): 200 def PopulateCommonFieldValues(self, field, mojom_field):
197 """Populates a number of common field values based on a mojom field. 201 """Populates a number of common field values based on a mojom field.
198 202
199 Args: 203 Args:
200 field: {module.Field|module.Parameter} to be populated. 204 field: {module.Field|module.Parameter} to be populated.
201 mojom_field: {StructField|UnionField} to be translated. 205 mojom_field: {StructField|UnionField} to be translated.
202 """ 206 """
(...skipping 21 matching lines...) Expand all
224 """Translates an mojom_types_mojom.EnumValue to a module.EnumField. 228 """Translates an mojom_types_mojom.EnumValue to a module.EnumField.
225 229
226 mojom_field: {mojom_types_mojom.EnumValue} to be translated. 230 mojom_field: {mojom_types_mojom.EnumValue} to be translated.
227 231
228 Returns: 232 Returns:
229 {module.EnumField} translated from mojom_field. 233 {module.EnumField} translated from mojom_field.
230 """ 234 """
231 field = module.EnumField() 235 field = module.EnumField()
232 field.name = mojom_field.decl_data.short_name 236 field.name = mojom_field.decl_data.short_name
233 field.attributes = self.AttributesFromMojom(mojom_field) 237 field.attributes = self.AttributesFromMojom(mojom_field)
234 field.value = self.EvalConst(mojom_field.value) 238 field.value = mojom_field.int_value
235 field.numeric_value = self.EvalConst(mojom_field.value) 239 field.numeric_value = mojom_field.int_value
236 return field 240 return field
237 241
238 def AttributesFromMojom(self, mojom): 242 def AttributesFromMojom(self, mojom):
239 """Extracts the attributes from a Mojom object into a dict. 243 """Extracts the attributes from a Mojom object into a dict.
240 244
241 Args: 245 Args:
242 mojom: A type in mojom_types_mojom containing a decl_data field. 246 mojom: A type in mojom_types_mojom containing a decl_data field.
243 247
244 Returns: 248 Returns:
245 {dict<str, str>} of the attributes. 249 {dict<str, str>} of the attributes.
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 mojom_const: {mojom_types_mojom.DeclaredConstant} to be translated. 339 mojom_const: {mojom_types_mojom.DeclaredConstant} to be translated.
336 parent_kind: {module.Struct|Interface} if the constant is nested in a 340 parent_kind: {module.Struct|Interface} if the constant is nested in a
337 struct or interface. 341 struct or interface.
338 342
339 Returns: 343 Returns:
340 {module.Constant} translated from mojom_const. 344 {module.Constant} translated from mojom_const.
341 """ 345 """
342 const = module.Constant() 346 const = module.Constant()
343 const.name = mojom_const.decl_data.short_name 347 const.name = mojom_const.decl_data.short_name
344 const.kind = self.KindFromMojom(mojom_const.type) 348 const.kind = self.KindFromMojom(mojom_const.type)
345 const.value = self.EvalConst(mojom_const.value) 349 const.value = self.EvalValue(mojom_const.value)
346 const.parent_kind = parent_kind 350 const.parent_kind = parent_kind
347 return const 351 return const
348 352
349 def EvalConst(self, const): 353 def EvalValue(self, value):
350 """Evaluates a mojom_types_mojom.ConstantValue. 354 """Evaluates a mojom_types_mojom.Value.
351 355
352 Args: 356 Args:
353 const: {mojom_types_mojom.ConstantValue} to be evaluated. 357 value: {mojom_types_mojom.Value} to be evaluated.
354 358
355 Returns: 359 Returns:
356 {int|float|str|bool} either the value of the constant or a string 360 {int|float|str|bool|None} the literal value if value is a LiteralValue,
357 referencing a built-in constant value. 361 the string name of the built-in constant if value is a
362 BuiltinConstantValue, the result of invoking EvalValue() on the
363 resolved concrete value of the user value reference if value is a
364 UserValueReference and its concrete value is not None, or else None
358 """ 365 """
359 if const.value.tag == mojom_types_mojom.ConstantValue.Tags.builtin_value: 366 if value.tag == mojom_types_mojom.Value.Tags.literal_value:
367 return value.literal_value.data
368 elif value.tag == mojom_types_mojom.Value.Tags.builtin_value:
360 mojom_to_builtin = { 369 mojom_to_builtin = {
361 mojom_types_mojom.BuiltinConstantValue.DOUBLE_INFINITY: 370 mojom_types_mojom.BuiltinConstantValue.DOUBLE_INFINITY:
362 'double.INFINITY', 371 'double.INFINITY',
363 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY: 372 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY:
364 'double.NEGATIVE_INFINITY', 373 'double.NEGATIVE_INFINITY',
365 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN: 374 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN:
366 'double.DOUBLE_NAN', 375 'double.DOUBLE_NAN',
367 mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY: 376 mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY:
368 'float.INFINITY', 377 'float.INFINITY',
369 mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY: 378 mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY:
370 'float.NEGATIVE_INFINITY', 379 'float.NEGATIVE_INFINITY',
371 mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN', 380 mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN',
372 } 381 }
373 return module.BuiltinValue(mojom_to_builtin[const.value.builtin_value]) 382 return module.BuiltinValue(mojom_to_builtin[value.builtin_value])
374 383
375 return const.value.data 384 assert value.tag == mojom_types_mojom.Value.Tags.user_value_reference
385 concrete_value = value.user_value_reference.resolved_concrete_value
386 if concrete_value == None:
387 return None
388 assert (concrete_value.tag !=
389 mojom_types_mojom.Value.Tags.user_value_reference)
390 return self.EvalValue(concrete_value)
376 391
377 def KindFromMojom(self, mojom_type): 392 def KindFromMojom(self, mojom_type):
378 """Translates a mojom_types_mojom.Type to its equivalent module type. 393 """Translates a mojom_types_mojom.Type to its equivalent module type.
379 394
380 It is guaranteed that two calls to KindFromMojom with the same input yield 395 It is guaranteed that two calls to KindFromMojom with the same input yield
381 the same object. 396 the same object.
382 397
383 Args: 398 Args:
384 mojom_type: {mojom_types_mojom.Type} to be translated. 399 mojom_type: {mojom_types_mojom.Type} to be translated.
385 400
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 591
577 Args: 592 Args:
578 graph: {mojom_types_mojom.MojomFileGraph} to be translated. 593 graph: {mojom_types_mojom.MojomFileGraph} to be translated.
579 594
580 Return: 595 Return:
581 {dict<str, module.Module>} mapping the file's name to its module.Module 596 {dict<str, module.Module>} mapping the file's name to its module.Module
582 translation for all files in graph.files. 597 translation for all files in graph.files.
583 """ 598 """
584 return {file_name: FileTranslator(graph, file_name).Translate() 599 return {file_name: FileTranslator(graph, file_name).Translate()
585 for file_name in graph.files} 600 for file_name in graph.files}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698