OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package serialization | 5 package serialization |
6 | 6 |
7 import ( | 7 import ( |
| 8 "fmt" |
| 9 "mojo/public/go/bindings" |
| 10 "mojom/mojom_parser/generated/mojom_files" |
| 11 "mojom/mojom_parser/generated/mojom_types" |
8 "mojom/mojom_parser/mojom" | 12 "mojom/mojom_parser/mojom" |
9 ) | 13 ) |
10 | 14 |
11 ////////////////////////////////////////////////// | 15 ////////////////////////////////////////////////// |
12 /// Mojom Descriptor Serialization | 16 /// Mojom Descriptor Serialization |
13 ////////////////////////////////////////////////// | 17 ////////////////////////////////////////////////// |
14 | 18 |
| 19 // This variable may be set to false in order to omit emitting line and |
| 20 // column numbers. |
| 21 var EmitLineAndColumnNumbers bool = true |
| 22 |
15 // Serializes the MojomDescriptor into a binary form that is passed to the | 23 // Serializes the MojomDescriptor into a binary form that is passed to the |
16 // backend of the compiler in order to invoke the code generators. | 24 // backend of the compiler in order to invoke the code generators. |
17 // To do this we use Mojo serialization. | 25 // To do this we use Mojo serialization. |
18 func Serialize(d *mojom.MojomDescriptor) (bytes []byte, err error) { | 26 func Serialize(d *mojom.MojomDescriptor) (bytes []byte, err error) { |
19 » // TODO(rudominer) This is a stub. It will be implemented in a later | 27 » fileGraph := translateDescriptor(d) |
20 » // CL. | 28 » encoder := bindings.NewEncoder() |
21 » bytes = []byte("DummyOutput") | 29 » fileGraph.Encode(encoder) |
| 30 » bytes, _, err = encoder.Data() |
22 return | 31 return |
23 } | 32 } |
| 33 |
| 34 // translateDescriptor translates from a mojom.MojomDescriptor (the pure Go |
| 35 // representation used by the parser) to a mojom_files.MojomFileGraph (the |
| 36 // Mojo Go representation used for serialization.) |
| 37 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph { |
| 38 fileGraph := mojom_files.MojomFileGraph{} |
| 39 |
| 40 // Add |files| field. |
| 41 fileGraph.Files = make(map[string]mojom_files.MojomFile) |
| 42 for name, file := range d.MojomFilesByName { |
| 43 fileGraph.Files[name] = translateMojomFile(file) |
| 44 } |
| 45 |
| 46 // Add |resolved_types| field. |
| 47 fileGraph.ResolvedTypes = make(map[string]mojom_types.UserDefinedType) |
| 48 for key, userDefinedType := range d.TypesByKey { |
| 49 fileGraph.ResolvedTypes[key] = translateUserDefinedType(userDefi
nedType) |
| 50 } |
| 51 |
| 52 // Add |resolved_values| field. |
| 53 fileGraph.ResolvedValues = make(map[string]mojom_types.UserDefinedValue) |
| 54 for key, userDefinedValue := range d.ValuesByKey { |
| 55 fileGraph.ResolvedValues[key] = translateUserDefinedValue(userDe
finedValue) |
| 56 } |
| 57 |
| 58 return &fileGraph |
| 59 } |
| 60 |
| 61 // translateMojomFile translates from a mojom.MojomFile (the pure Go |
| 62 // representation used by the parser) to a mojom_files.MojomFile (the |
| 63 // Mojo Go representation used for serialization.) |
| 64 func translateMojomFile(f *mojom.MojomFile) (file mojom_files.MojomFile) { |
| 65 // file_name field |
| 66 file.FileName = f.CanonicalFileName |
| 67 |
| 68 // module_namespace field |
| 69 file.ModuleNamespace = newString(f.ModuleNamespace) |
| 70 |
| 71 // attributes field |
| 72 if f.Attributes != nil { |
| 73 file.Attributes = new([]mojom_types.Attribute) |
| 74 for _, attr := range f.Attributes.List { |
| 75 *(file.Attributes) = append(*(file.Attributes), translat
eMojomAttribute(&attr)) |
| 76 } |
| 77 } |
| 78 |
| 79 // imports field |
| 80 if len(f.Imports) > 0 { |
| 81 file.Imports = new([]string) |
| 82 for _, importName := range f.Imports { |
| 83 *(file.Imports) = append(*(file.Imports), importName.Can
onicalFileName) |
| 84 } |
| 85 } |
| 86 |
| 87 // declared_mojom_objects field... |
| 88 |
| 89 // Interfaces |
| 90 if f.Interfaces != nil && len(f.Interfaces) > 0 { |
| 91 file.DeclaredMojomObjects.Interfaces = new([]string) |
| 92 for _, intrfc := range f.Interfaces { |
| 93 *(file.DeclaredMojomObjects.Interfaces) = append(*(file.
DeclaredMojomObjects.Interfaces), intrfc.TypeKey()) |
| 94 } |
| 95 } |
| 96 |
| 97 // Structs |
| 98 if f.Structs != nil && len(f.Structs) > 0 { |
| 99 file.DeclaredMojomObjects.Structs = new([]string) |
| 100 for _, strct := range f.Structs { |
| 101 *(file.DeclaredMojomObjects.Structs) = append(*(file.Dec
laredMojomObjects.Structs), strct.TypeKey()) |
| 102 } |
| 103 } |
| 104 |
| 105 // Unions |
| 106 if f.Unions != nil && len(f.Unions) > 0 { |
| 107 file.DeclaredMojomObjects.Unions = new([]string) |
| 108 for _, union := range f.Unions { |
| 109 *(file.DeclaredMojomObjects.Unions) = append(*(file.Decl
aredMojomObjects.Unions), union.TypeKey()) |
| 110 } |
| 111 } |
| 112 |
| 113 // TopLevelEnums |
| 114 if f.Enums != nil && len(f.Enums) > 0 { |
| 115 file.DeclaredMojomObjects.TopLevelEnums = new([]string) |
| 116 for _, enum := range f.Enums { |
| 117 *(file.DeclaredMojomObjects.TopLevelEnums) = append(*(fi
le.DeclaredMojomObjects.TopLevelEnums), enum.TypeKey()) |
| 118 } |
| 119 } |
| 120 |
| 121 // TopLevelConstants |
| 122 if f.Constants != nil && len(f.Constants) > 0 { |
| 123 file.DeclaredMojomObjects.TopLevelConstants = new([]string) |
| 124 for _, constant := range f.Constants { |
| 125 *(file.DeclaredMojomObjects.TopLevelConstants) = append(
*(file.DeclaredMojomObjects.TopLevelConstants), constant.ValueKey()) |
| 126 } |
| 127 } |
| 128 |
| 129 // TODO(rudominer) Do we need the EmbeddedEnums and EmbeddedConstants |
| 130 // fields in KeysByType. It seems these fields are not currently being |
| 131 // used in mojom_translator.py. |
| 132 |
| 133 return |
| 134 } |
| 135 |
| 136 // translateUserDefinedType translates from a mojom.UserDefinedType (the pure Go |
| 137 // representation used by the parser) to a mojom_types.UserDefinedType (the |
| 138 // Mojo Go representation used for serialization.) |
| 139 func translateUserDefinedType(t mojom.UserDefinedType) mojom_types.UserDefinedTy
pe { |
| 140 switch p := t.(type) { |
| 141 case *mojom.MojomStruct: |
| 142 return &mojom_types.UserDefinedTypeStructType{translateMojomStru
ct(p)} |
| 143 case *mojom.MojomInterface: |
| 144 return translateMojomInterface(p) |
| 145 case *mojom.MojomEnum: |
| 146 return translateMojomEnum(p) |
| 147 case *mojom.MojomUnion: |
| 148 return translateMojomUnion(p) |
| 149 default: |
| 150 panic(fmt.Sprintf("Unexpected type: %T", t)) |
| 151 |
| 152 } |
| 153 } |
| 154 |
| 155 func translateMojomStruct(s *mojom.MojomStruct) mojom_types.MojomStruct { |
| 156 mojomStruct := mojom_types.MojomStruct{} |
| 157 mojomStruct.DeclData = translateDeclarationData(&s.DeclarationData) |
| 158 mojomStruct.DeclData.ContainedDeclarations = translateContainedDeclarati
ons(&s.DeclarationContainer) |
| 159 |
| 160 for _, field := range s.Fields { |
| 161 mojomStruct.Fields = append(mojomStruct.Fields, translateStructF
ield(field)) |
| 162 } |
| 163 |
| 164 // TODO(rudominer) Implement VersionInfo. |
| 165 //mojomStruct.Value.VersionInfo = new([]mojom_types.StructVersion) |
| 166 |
| 167 return mojomStruct |
| 168 } |
| 169 |
| 170 func translateStructField(f *mojom.StructField) (field mojom_types.StructField)
{ |
| 171 field.DeclData = translateDeclarationData(&f.DeclarationData) |
| 172 field.Type = translateTypeRef(f.FieldType) |
| 173 if f.DefaultValue != nil { |
| 174 field.DefaultValue = translateDefaultFieldValue(f.DefaultValue) |
| 175 } |
| 176 // TODO(rudominer) Implement field offsets. |
| 177 field.Offset = f.Offset |
| 178 return |
| 179 } |
| 180 |
| 181 func translateDefaultFieldValue(v mojom.ValueRef) mojom_types.DefaultFieldValue
{ |
| 182 switch v := v.(type) { |
| 183 case mojom.LiteralValue: |
| 184 if v.IsDefault() { |
| 185 return &mojom_types.DefaultFieldValueDefaultKeyword{mojo
m_types.DefaultKeyword{}} |
| 186 } |
| 187 return &mojom_types.DefaultFieldValueValue{translateLiteralValue
(v)} |
| 188 case *mojom.UserValueRef: |
| 189 switch t := v.ResolvedConcreteValue().(type) { |
| 190 case mojom.BuiltInConstantValue: |
| 191 return &mojom_types.DefaultFieldValueValue{translateBuil
tInConstantValue(t)} |
| 192 default: |
| 193 return &mojom_types.DefaultFieldValueValue{translateUser
ValueRef(v)} |
| 194 } |
| 195 default: |
| 196 panic(fmt.Sprintf("Unexpected ValueRef type: %T", v)) |
| 197 |
| 198 } |
| 199 } |
| 200 |
| 201 func translateMojomInterface(intrfc *mojom.MojomInterface) *mojom_types.UserDefi
nedTypeInterfaceType { |
| 202 mojomInterface := mojom_types.UserDefinedTypeInterfaceType{} |
| 203 |
| 204 mojomInterface.Value.DeclData = translateDeclarationData(&intrfc.Declara
tionData) |
| 205 mojomInterface.Value.DeclData.ContainedDeclarations = translateContained
Declarations(&intrfc.DeclarationContainer) |
| 206 |
| 207 // TODO(rudominer) The Interface name field need not be the name from th
e .mojom file. |
| 208 mojomInterface.Value.InterfaceName = intrfc.SimpleName() |
| 209 |
| 210 mojomInterface.Value.Methods = make(map[uint32]mojom_types.MojomMethod) |
| 211 for ordinal, method := range intrfc.MethodsByOrdinal { |
| 212 mojomInterface.Value.Methods[ordinal] = translateMojomMethod(met
hod) |
| 213 } |
| 214 |
| 215 return &mojomInterface |
| 216 } |
| 217 |
| 218 func translateMojomMethod(method *mojom.MojomMethod) mojom_types.MojomMethod { |
| 219 mojomMethod := mojom_types.MojomMethod{} |
| 220 |
| 221 // decl_data |
| 222 mojomMethod.DeclData = translateDeclarationData(&method.DeclarationData) |
| 223 |
| 224 // parameters |
| 225 mojomMethod.Parameters = translateMojomStruct(method.Parameters) |
| 226 |
| 227 // response_params |
| 228 if method.ResponseParameters != nil { |
| 229 responseParams := translateMojomStruct(method.ResponseParameters
) |
| 230 mojomMethod.ResponseParams = &responseParams |
| 231 } |
| 232 |
| 233 // ordinal |
| 234 mojomMethod.Ordinal = method.Ordinal |
| 235 return mojomMethod |
| 236 } |
| 237 |
| 238 func translateMojomEnum(enum *mojom.MojomEnum) *mojom_types.UserDefinedTypeEnumT
ype { |
| 239 mojomEnum := mojom_types.UserDefinedTypeEnumType{} |
| 240 mojomEnum.Value.DeclData = translateDeclarationData(&enum.DeclarationDat
a) |
| 241 for _, value := range enum.Values { |
| 242 mojomEnum.Value.Values = append(mojomEnum.Value.Values, translat
eEnumValue(value)) |
| 243 } |
| 244 return &mojomEnum |
| 245 } |
| 246 |
| 247 func translateMojomUnion(union *mojom.MojomUnion) *mojom_types.UserDefinedTypeUn
ionType { |
| 248 mojomUnion := mojom_types.UserDefinedTypeUnionType{} |
| 249 mojomUnion.Value.DeclData = translateDeclarationData(&union.DeclarationD
ata) |
| 250 for _, field := range union.Fields { |
| 251 mojomUnion.Value.Fields = append(mojomUnion.Value.Fields, |
| 252 translateUnionField(field)) |
| 253 } |
| 254 return &mojomUnion |
| 255 } |
| 256 |
| 257 func translateUnionField(unionField mojom.UnionField) mojom_types.UnionField { |
| 258 outUnionField := mojom_types.UnionField{} |
| 259 outUnionField.DeclData = translateDeclarationData(&unionField.Declaratio
nData) |
| 260 outUnionField.Type = translateTypeRef(unionField.FieldType) |
| 261 outUnionField.Tag = unionField.Tag |
| 262 return outUnionField |
| 263 } |
| 264 |
| 265 // WARNING: Do not invoke this function on a UserDefinedValue of type BuiltInCon
stantValue because |
| 266 // objects of those types do not have a type_key and do not correspond to a mojo
m_types.UserDefinedValue. |
| 267 func translateUserDefinedValue(v mojom.UserDefinedValue) mojom_types.UserDefined
Value { |
| 268 switch t := v.(type) { |
| 269 case *mojom.UserDefinedConstant: |
| 270 return translateUserDefinedConstant(t) |
| 271 case *mojom.EnumValue: |
| 272 return &mojom_types.UserDefinedValueEnumValue{translateEnumValue
(t)} |
| 273 case *mojom.BuiltInConstantValue: |
| 274 panic("Do not invoke translateUserDefinedValue on BuiltInConstan
tValue.") |
| 275 default: |
| 276 panic(fmt.Sprintf("Unexpected UserDefinedValue type %T", v)) |
| 277 |
| 278 } |
| 279 } |
| 280 |
| 281 func translateUserDefinedConstant(t *mojom.UserDefinedConstant) *mojom_types.Use
rDefinedValueDeclaredConstant { |
| 282 declaredConstant := mojom_types.UserDefinedValueDeclaredConstant{} |
| 283 declaredConstant.Value.Type = translateTypeRef(t.DeclaredType()) |
| 284 declaredConstant.Value.DeclData = *translateDeclarationData(&t.Declarati
onData) |
| 285 declaredConstant.Value.Value = translateValueRef(t.ValueRef()) |
| 286 return &declaredConstant |
| 287 } |
| 288 |
| 289 func translateEnumValue(v *mojom.EnumValue) mojom_types.EnumValue { |
| 290 enumValue := mojom_types.EnumValue{} |
| 291 enumValue.DeclData = translateDeclarationData(&v.DeclarationData) |
| 292 enumValue.EnumTypeKey = v.EnumType().TypeKey() |
| 293 if v.ValueRef() != nil { |
| 294 enumValue.InitializerValue = translateValueRef(v.ValueRef()) |
| 295 } |
| 296 // TODO(rudominer) enumValue.IntValue should be set to v.ComputedIntValu
e |
| 297 // once MojomDescriptor.ComputeEnumValueIntegers() is implemented. |
| 298 enumValue.IntValue = v.Int32Value() |
| 299 return enumValue |
| 300 } |
| 301 |
| 302 func translateTypeRef(typeRef mojom.TypeRef) mojom_types.Type { |
| 303 switch t := typeRef.(type) { |
| 304 case mojom.SimpleType: |
| 305 return translateSimpleType(t) |
| 306 case mojom.StringType: |
| 307 return translateStringType(t) |
| 308 case mojom.HandleTypeRef: |
| 309 return translateHandleType(t) |
| 310 case mojom.ArrayTypeRef: |
| 311 return translateArrayType(t) |
| 312 case mojom.MapTypeRef: |
| 313 return translateMapType(t) |
| 314 case *mojom.UserTypeRef: |
| 315 return translateUserTypeRef(t) |
| 316 default: |
| 317 panic(fmt.Sprintf("Unexpected TypeRef type %T", t)) |
| 318 } |
| 319 } |
| 320 |
| 321 func translateSimpleType(simpleType mojom.SimpleType) *mojom_types.TypeSimpleTyp
e { |
| 322 var value mojom_types.SimpleType |
| 323 switch simpleType { |
| 324 case mojom.SimpleTypeBool: |
| 325 value = mojom_types.SimpleType_Bool |
| 326 case mojom.SimpleTypeDouble: |
| 327 value = mojom_types.SimpleType_Double |
| 328 case mojom.SimpleTypeFloat: |
| 329 value = mojom_types.SimpleType_Float |
| 330 case mojom.SimpleTypeInt8: |
| 331 value = mojom_types.SimpleType_InT8 |
| 332 case mojom.SimpleTypeInt16: |
| 333 value = mojom_types.SimpleType_InT16 |
| 334 case mojom.SimpleTypeInt32: |
| 335 value = mojom_types.SimpleType_InT32 |
| 336 case mojom.SimpleTypeInt64: |
| 337 value = mojom_types.SimpleType_InT64 |
| 338 case mojom.SimpleTypeUInt8: |
| 339 value = mojom_types.SimpleType_UinT8 |
| 340 case mojom.SimpleTypeUInt16: |
| 341 value = mojom_types.SimpleType_UinT16 |
| 342 case mojom.SimpleTypeUInt32: |
| 343 value = mojom_types.SimpleType_UinT32 |
| 344 case mojom.SimpleTypeUInt64: |
| 345 value = mojom_types.SimpleType_UinT64 |
| 346 } |
| 347 return &mojom_types.TypeSimpleType{value} |
| 348 } |
| 349 |
| 350 func translateStringType(stringType mojom.StringType) *mojom_types.TypeStringTyp
e { |
| 351 return &mojom_types.TypeStringType{mojom_types.StringType{stringType.Nul
lable()}} |
| 352 } |
| 353 |
| 354 func translateHandleType(handleType mojom.HandleTypeRef) *mojom_types.TypeHandle
Type { |
| 355 var kind mojom_types.HandleType_Kind |
| 356 switch handleType.HandleKind() { |
| 357 case mojom.HandleKindUnspecified: |
| 358 kind = mojom_types.HandleType_Kind_Unspecified |
| 359 case mojom.HandleKindMessagePipe: |
| 360 kind = mojom_types.HandleType_Kind_MessagePipe |
| 361 case mojom.HandleKindDataPipeConsumer: |
| 362 kind = mojom_types.HandleType_Kind_DataPipeConsumer |
| 363 case mojom.HandleKindDataPipeProducer: |
| 364 kind = mojom_types.HandleType_Kind_DataPipeProducer |
| 365 case mojom.HandleKindSharedBuffer: |
| 366 kind = mojom_types.HandleType_Kind_SharedBuffer |
| 367 } |
| 368 return &mojom_types.TypeHandleType{mojom_types.HandleType{handleType.Nul
lable(), kind}} |
| 369 } |
| 370 |
| 371 func translateArrayType(arrayType mojom.ArrayTypeRef) *mojom_types.TypeArrayType
{ |
| 372 return &mojom_types.TypeArrayType{mojom_types.ArrayType{ |
| 373 Nullable: arrayType.Nullable(), |
| 374 FixedLength: int32(arrayType.FixedLength()), |
| 375 ElementType: translateTypeRef(arrayType.ElementType())}} |
| 376 } |
| 377 |
| 378 func translateMapType(mapType mojom.MapTypeRef) *mojom_types.TypeMapType { |
| 379 return &mojom_types.TypeMapType{mojom_types.MapType{ |
| 380 Nullable: mapType.Nullable(), |
| 381 KeyType: translateTypeRef(mapType.KeyType()), |
| 382 ValueType: translateTypeRef(mapType.ValueType())}} |
| 383 } |
| 384 |
| 385 func translateUserTypeRef(userType *mojom.UserTypeRef) *mojom_types.TypeTypeRefe
rence { |
| 386 typeKey := newString(userType.ResolvedType().TypeKey()) |
| 387 identifier := newString(userType.Identifier()) |
| 388 return &mojom_types.TypeTypeReference{mojom_types.TypeReference{ |
| 389 Nullable: userType.Nullable(), |
| 390 IsInterfaceRequest: userType.IsInterfaceRequest(), |
| 391 Identifier: identifier, |
| 392 TypeKey: typeKey}} |
| 393 } |
| 394 |
| 395 func translateValueRef(valueRef mojom.ValueRef) mojom_types.Value { |
| 396 switch valueRef := valueRef.(type) { |
| 397 case mojom.LiteralValue: |
| 398 return translateLiteralValue(valueRef) |
| 399 case *mojom.UserValueRef: |
| 400 return translateUserValueRef(valueRef) |
| 401 default: |
| 402 panic(fmt.Sprintf("Unexpected ValueRef type %T", valueRef)) |
| 403 } |
| 404 } |
| 405 |
| 406 func translateLiteralValue(v mojom.LiteralValue) *mojom_types.ValueLiteralValue
{ |
| 407 var lv mojom_types.LiteralValue |
| 408 switch v.ValueType() { |
| 409 case mojom.SimpleTypeBool: |
| 410 lv = &mojom_types.LiteralValueBoolValue{v.Value().(bool)} |
| 411 case mojom.SimpleTypeDouble: |
| 412 lv = &mojom_types.LiteralValueDoubleValue{v.Value().(float64)} |
| 413 case mojom.SimpleTypeFloat: |
| 414 lv = &mojom_types.LiteralValueFloatValue{v.Value().(float32)} |
| 415 case mojom.SimpleTypeInt8: |
| 416 lv = &mojom_types.LiteralValueInt8Value{v.Value().(int8)} |
| 417 case mojom.SimpleTypeInt16: |
| 418 lv = &mojom_types.LiteralValueInt16Value{v.Value().(int16)} |
| 419 case mojom.SimpleTypeInt32: |
| 420 lv = &mojom_types.LiteralValueInt32Value{v.Value().(int32)} |
| 421 case mojom.SimpleTypeInt64: |
| 422 lv = &mojom_types.LiteralValueInt64Value{v.Value().(int64)} |
| 423 case mojom.SimpleTypeUInt8: |
| 424 lv = &mojom_types.LiteralValueUint8Value{v.Value().(uint8)} |
| 425 case mojom.SimpleTypeUInt16: |
| 426 lv = &mojom_types.LiteralValueUint16Value{v.Value().(uint16)} |
| 427 case mojom.SimpleTypeUInt32: |
| 428 lv = &mojom_types.LiteralValueUint32Value{v.Value().(uint32)} |
| 429 case mojom.SimpleTypeUInt64: |
| 430 lv = &mojom_types.LiteralValueUint64Value{v.Value().(uint64)} |
| 431 case mojom.StringLiteralType: |
| 432 lv = &mojom_types.LiteralValueStringValue{v.Value().(string)} |
| 433 default: |
| 434 panic(fmt.Sprintf("Unexpected literal value type %d", v.ValueTyp
e())) |
| 435 } |
| 436 return &mojom_types.ValueLiteralValue{lv} |
| 437 } |
| 438 |
| 439 func translateBuiltInConstantValue(t mojom.BuiltInConstantValue) *mojom_types.Va
lueBuiltinValue { |
| 440 builtInValue := mojom_types.ValueBuiltinValue{} |
| 441 switch t { |
| 442 case mojom.SimpleTypeFloat_INFINITY: |
| 443 builtInValue.Value = mojom_types.BuiltinConstantValue_FloatInfin
ity |
| 444 case mojom.SimpleTypeFloat_NEGATIVE_INFINITY: |
| 445 builtInValue.Value = mojom_types.BuiltinConstantValue_FloatNegat
iveInfinity |
| 446 case mojom.SimpleTypeFloat_NAN: |
| 447 builtInValue.Value = mojom_types.BuiltinConstantValue_FloatNan |
| 448 case mojom.SimpleTypeDouble_INFINITY: |
| 449 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleInfi
nity |
| 450 case mojom.SimpleTypeDouble_NEGATIVE_INFINITY: |
| 451 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity |
| 452 case mojom.SimpleTypeDouble_NAN: |
| 453 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity |
| 454 default: |
| 455 panic(fmt.Sprintf("Unrecognized BuiltInConstantValue %v", t)) |
| 456 } |
| 457 return &builtInValue |
| 458 } |
| 459 |
| 460 func translateUserValueRef(r *mojom.UserValueRef) *mojom_types.ValueUserValueRef
erence { |
| 461 valueKey := newString(r.ResolvedDeclaredValue().ValueKey()) |
| 462 return &mojom_types.ValueUserValueReference{mojom_types.UserValueReferen
ce{ |
| 463 Identifier: r.Identifier(), |
| 464 ValueKey: valueKey, |
| 465 ResolvedConcreteValue: translateConcreteValue(r.ResolvedConcrete
Value())}} |
| 466 } |
| 467 |
| 468 func translateConcreteValue(v mojom.ConcreteValue) mojom_types.Value { |
| 469 switch t := v.(type) { |
| 470 case mojom.BuiltInConstantValue: |
| 471 return translateBuiltInConstantValue(t) |
| 472 case mojom.LiteralValue: |
| 473 return translateLiteralValue(t) |
| 474 case *mojom.EnumValue: |
| 475 // Note that in the pure Go representation we support the abstra
ction |
| 476 // that an EnumValue is a type of ConcreteValue. This makes sens
e because |
| 477 // an Enum is abstractly a finite set of values that need not ha
ve |
| 478 // a corresponding integer value. However in mojom_types.mojom w
e currently |
| 479 // do not support this abstraction: the only concrete values are
the literal |
| 480 // values and the BuiltInConstantValues. Consequently here we re
turn the |
| 481 // Int32Value() of the EnumValue. |
| 482 return &mojom_types.ValueLiteralValue{&mojom_types.LiteralValueI
nt32Value{t.Int32Value()}} |
| 483 default: |
| 484 panic(fmt.Sprintf("Unexpected type %T", v)) |
| 485 } |
| 486 } |
| 487 |
| 488 func translateDeclarationData(d *mojom.DeclarationData) *mojom_types.Declaration
Data { |
| 489 declData := mojom_types.DeclarationData{} |
| 490 |
| 491 // attributes field |
| 492 if d.Attributes() != nil { |
| 493 declData.Attributes = new([]mojom_types.Attribute) |
| 494 for _, attr := range d.Attributes().List { |
| 495 *(declData.Attributes) = append(*(declData.Attributes),
translateMojomAttribute(&attr)) |
| 496 } |
| 497 } |
| 498 |
| 499 // min_version field |
| 500 // TODO(rudominer) Eliminate the min_version field from struct Declarati
onData |
| 501 |
| 502 // short_name field |
| 503 declData.ShortName = newString(d.SimpleName()) |
| 504 |
| 505 // full_identifier field |
| 506 declData.FullIdentifier = newString(d.FullyQualifiedName()) |
| 507 |
| 508 // declared_ordinal field |
| 509 if d.DeclaredOrdinal() < 0 { |
| 510 declData.DeclaredOrdinal = -1 |
| 511 } else { |
| 512 declData.DeclaredOrdinal = int32(d.DeclaredOrdinal()) |
| 513 } |
| 514 |
| 515 // declaration_order |
| 516 // TODO(rudominer) DeclarationOrder is currently not populated. |
| 517 declData.DeclarationOrder = -1 |
| 518 |
| 519 // source_file_info |
| 520 declData.SourceFileInfo = new(mojom_types.SourceFileInfo) |
| 521 declData.SourceFileInfo.FileName = d.OwningFile().CanonicalFileName |
| 522 if EmitLineAndColumnNumbers { |
| 523 declData.SourceFileInfo.LineNumber = d.LineNumber() |
| 524 declData.SourceFileInfo.ColumnNumber = d.ColumnNumber() |
| 525 } |
| 526 return &declData |
| 527 } |
| 528 |
| 529 // Returns nil if there are no contained declarations |
| 530 func translateContainedDeclarations(container *mojom.DeclarationContainer) *mojo
m_types.ContainedDeclarations { |
| 531 if container.Enums == nil && container.Constants == nil { |
| 532 return nil |
| 533 } |
| 534 declarations := mojom_types.ContainedDeclarations{} |
| 535 if container.Enums != nil { |
| 536 declarations.Enums = new([]string) |
| 537 for _, enum := range container.Enums { |
| 538 *declarations.Enums = append(*declarations.Enums, enum.T
ypeKey()) |
| 539 } |
| 540 } |
| 541 if container.Constants != nil { |
| 542 declarations.Constants = new([]string) |
| 543 for _, constant := range container.Constants { |
| 544 *declarations.Constants = append(*declarations.Constants
, constant.ValueKey()) |
| 545 } |
| 546 } |
| 547 return &declarations |
| 548 } |
| 549 |
| 550 func translateMojomAttribute(a *mojom.MojomAttribute) (attribute mojom_types.Att
ribute) { |
| 551 // TODO(rudominer) Improve representation of attribute values. |
| 552 return mojom_types.Attribute{a.Key, fmt.Sprintf("%v", a.Value.Value())} |
| 553 } |
| 554 |
| 555 // newString is a convenience function for creating a pointer to a string whose
value |
| 556 // is the specified string. It is necessary to create pointers to strings becaus
e |
| 557 // that is how the Mojom type string? (i.e. nullable string) is represented in |
| 558 // in the Mojom Go bindings. |
| 559 func newString(s string) *string { |
| 560 t := new(string) |
| 561 *t = s |
| 562 return t |
| 563 } |
OLD | NEW |