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

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: Gets rid of ConcreteValue and fixes mojom_translator. 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' #TODO(azani) Is this right?
rudominer 2015/10/20 05:54:25 How do we represent the 'default' keyword in a moj
azani 2015/10/20 21:54:58 As the string "default". Yes this is right.
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 #TODO(azani) What is the difference between value and numeric_value?
rudominer 2015/10/20 05:54:25 What is the difference between value and numeric_v
azani 2015/10/20 21:54:58 In the data.py module, value is not necessarily re
235 field.numeric_value = self.EvalConst(mojom_field.value) 239 field.value = mojom_field.int_value
240 field.numeric_value = mojom_field.int_value
236 return field 241 return field
237 242
238 def AttributesFromMojom(self, mojom): 243 def AttributesFromMojom(self, mojom):
239 """Extracts the attributes from a Mojom object into a dict. 244 """Extracts the attributes from a Mojom object into a dict.
240 245
241 Args: 246 Args:
242 mojom: A type in mojom_types_mojom containing a decl_data field. 247 mojom: A type in mojom_types_mojom containing a decl_data field.
243 248
244 Returns: 249 Returns:
245 {dict<str, str>} of the attributes. 250 {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. 340 mojom_const: {mojom_types_mojom.DeclaredConstant} to be translated.
336 parent_kind: {module.Struct|Interface} if the constant is nested in a 341 parent_kind: {module.Struct|Interface} if the constant is nested in a
337 struct or interface. 342 struct or interface.
338 343
339 Returns: 344 Returns:
340 {module.Constant} translated from mojom_const. 345 {module.Constant} translated from mojom_const.
341 """ 346 """
342 const = module.Constant() 347 const = module.Constant()
343 const.name = mojom_const.decl_data.short_name 348 const.name = mojom_const.decl_data.short_name
344 const.kind = self.KindFromMojom(mojom_const.type) 349 const.kind = self.KindFromMojom(mojom_const.type)
345 const.value = self.EvalConst(mojom_const.value) 350 const.value = self.EvalValue(mojom_const.value)
346 const.parent_kind = parent_kind 351 const.parent_kind = parent_kind
347 return const 352 return const
348 353
349 def EvalConst(self, const): 354 def EvalValue(self, value):
350 """Evaluates a mojom_types_mojom.ConstantValue. 355 """Evaluates a mojom_types_mojom.Value.
351 356
352 Args: 357 Args:
353 const: {mojom_types_mojom.ConstantValue} to be evaluated. 358 value: {mojom_types_mojom.Value} to be evaluated.
rudominer 2015/10/20 05:54:25 This was incorrect in the existing code. The argum
azani 2015/10/20 21:54:58 Acknowledged.
354 359
355 Returns: 360 Returns:
356 {int|float|str|bool} either the value of the constant or a string 361 {int|float|str|bool|None} the literal value if value is a LiteralValue,
357 referencing a built-in constant value. 362 the string name of the built-in constant if value is a
363 BuiltinConstantValue, the result of invoking EvalValue() on the
364 resolved concrete value of the user value reference if value is a
365 UserValueReference and its concrete value is not None, or else None
358 """ 366 """
359 if const.value.tag == mojom_types_mojom.ConstantValue.Tags.builtin_value: 367 if value.tag == mojom_types_mojom.Value.Tags.literal_value:
368 return value.literal_value.data
369 elif value.tag == mojom_types_mojom.Value.Tags.builtin_value:
360 mojom_to_builtin = { 370 mojom_to_builtin = {
361 mojom_types_mojom.BuiltinConstantValue.DOUBLE_INFINITY: 371 mojom_types_mojom.BuiltinConstantValue.DOUBLE_INFINITY:
362 'double.INFINITY', 372 'double.INFINITY',
363 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY: 373 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY:
364 'double.NEGATIVE_INFINITY', 374 'double.NEGATIVE_INFINITY',
365 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN: 375 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN:
366 'double.DOUBLE_NAN', 376 'double.DOUBLE_NAN',
367 mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY: 377 mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY:
368 'float.INFINITY', 378 'float.INFINITY',
369 mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY: 379 mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY:
370 'float.NEGATIVE_INFINITY', 380 'float.NEGATIVE_INFINITY',
371 mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN', 381 mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN',
372 } 382 }
373 return module.BuiltinValue(mojom_to_builtin[const.value.builtin_value]) 383 return module.BuiltinValue(mojom_to_builtin[value.builtin_value])
374 384
375 return const.value.data 385 assert value.tag == mojom_types_mojom.Value.Tags.user_value_reference
386 concrete_value = value.user_value_reference.resolved_concrete_value
387 if concrete_value == None:
388 return None
389 assert (concrete_value.tag !=
390 mojom_types_mojom.Value.Tags.user_value_reference)
391 return self.EvalValue(concrete_value)
376 392
377 def KindFromMojom(self, mojom_type): 393 def KindFromMojom(self, mojom_type):
378 """Translates a mojom_types_mojom.Type to its equivalent module type. 394 """Translates a mojom_types_mojom.Type to its equivalent module type.
379 395
380 It is guaranteed that two calls to KindFromMojom with the same input yield 396 It is guaranteed that two calls to KindFromMojom with the same input yield
381 the same object. 397 the same object.
382 398
383 Args: 399 Args:
384 mojom_type: {mojom_types_mojom.Type} to be translated. 400 mojom_type: {mojom_types_mojom.Type} to be translated.
385 401
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 592
577 Args: 593 Args:
578 graph: {mojom_types_mojom.MojomFileGraph} to be translated. 594 graph: {mojom_types_mojom.MojomFileGraph} to be translated.
579 595
580 Return: 596 Return:
581 {dict<str, module.Module>} mapping the file's name to its module.Module 597 {dict<str, module.Module>} mapping the file's name to its module.Module
582 translation for all files in graph.files. 598 translation for all files in graph.files.
583 """ 599 """
584 return {file_name: FileTranslator(graph, file_name).Translate() 600 return {file_name: FileTranslator(graph, file_name).Translate()
585 for file_name in graph.files} 601 for file_name in graph.files}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698