def test_constant(self): file_name = 'a.mojom' graph = mojom_files_mojom.MojomFileGraph() mojom_const = mojom_types_mojom.DeclaredConstant() mojom_const.decl_data = mojom_types_mojom.DeclarationData( short_name='foo', container_type_key='struct_key') mojom_const.type = mojom_types_mojom.Type( simple_type=mojom_types_mojom.SimpleType.INT64) mojom_const.value = mojom_types_mojom.Value() mojom_const.value.literal_value = mojom_types_mojom.LiteralValue( int64_value=20) mojom_struct = mojom_types_mojom.MojomStruct( fields=[], decl_data=mojom_types_mojom.DeclarationData( short_name='AStruct', source_file_info=mojom_types_mojom.SourceFileInfo( file_name=file_name))) add_version_info(mojom_struct, 0) graph.resolved_types = { 'struct_key': mojom_types_mojom.UserDefinedType(struct_type=mojom_struct) } const = module.Constant() translator = mojom_translator.FileTranslator(graph, file_name) translator.ConstantFromMojom(const, mojom_const) self.assertEquals(mojom_const.decl_data.short_name, const.name) self.assertEquals(module.INT64, const.kind) self.assertEquals('20', const.value) self.assertEquals(translator.UserDefinedFromTypeKey('struct_key'), const.parent_kind)
def EnumFieldFromData(module, enum, data, parent_kind): field = mojom.EnumField() field.name = data['name'] if parent_kind: field.value = FixupExpression(module, data['value'], (module.namespace, parent_kind.name)) else: field.value = FixupExpression(module, data['value'], (module.namespace, )) constant = mojom.Constant(module, enum, field) module.constants[constant.GetSpec()] = constant return field
def ConstantFromData(module, data, parent_kind): constant = mojom.Constant() constant.name = data['name'] if parent_kind: scope = (module.namespace, parent_kind.name) else: scope = (module.namespace, ) # TODO(mpcomplete): maybe we should only support POD kinds. constant.kind = KindFromData(module.kinds, data['kind'], scope) constant.value = FixupExpression(module, data.get('value'), scope, None) value = mojom.ConstantValue(module, parent_kind, constant) module.values[value.GetSpec()] = value return constant
def ConstantFromValueKey(self, value_key): """Takes a value key into a graph.resolved_values referring to a constant and returns the module equivalent. Args: value_key: {str} the value key referring to the value to be returned. Returns: {module.Constant} translated. """ if value_key in self._constant_cache: return self._constant_cache[value_key] mojom_const = self._graph.resolved_values[value_key].declared_constant const = module.Constant() self._constant_cache[value_key] = const self.ConstantFromMojom(const, mojom_const) return const
def ConstantFromKey(self, constant_key): """Takes a key into the map graph.resolved_constants and returns the module equivalent constant. Args: constant_key: {str} the key referring to the constant whose translation is to be returned. Returns: {module.Constant} translated. """ if constant_key in self._constant_cache: return self._constant_cache[constant_key] mojom_const = self._graph.resolved_constants[constant_key] const = module.Constant() self._constant_cache[constant_key] = const self.ConstantFromMojom(const, mojom_const) return const
def _Constant(module, parsed_const, parent_kind): """ Args: module: {mojom.Module} Module currently being constructed. parsed_const: {ast.Const} Parsed constant. Returns: {mojom.Constant} AST constant. """ constant = mojom.Constant() constant.mojom_name = parsed_const.mojom_name if parent_kind: scope = (module.mojom_namespace, parent_kind.mojom_name) else: scope = (module.mojom_namespace, ) # TODO(mpcomplete): maybe we should only support POD kinds. constant.kind = _Kind(module.kinds, _MapKind(parsed_const.typename), scope) constant.parent_kind = parent_kind constant.value = _FixupExpression(module, parsed_const.value, scope, None) value = mojom.ConstantValue(module, parent_kind, constant) module.values[value.GetSpec()] = value return constant