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 "bytes" | 8 "bytes" |
9 "compress/gzip" | 9 "compress/gzip" |
10 "encoding/base64" | 10 "encoding/base64" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // Mojo Go representation used for serialization.) | 80 // Mojo Go representation used for serialization.) |
81 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph { | 81 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph { |
82 fileGraph := mojom_files.MojomFileGraph{} | 82 fileGraph := mojom_files.MojomFileGraph{} |
83 | 83 |
84 // Add |resolved_types| field. | 84 // Add |resolved_types| field. |
85 fileGraph.ResolvedTypes = make(map[string]mojom_types.UserDefinedType) | 85 fileGraph.ResolvedTypes = make(map[string]mojom_types.UserDefinedType) |
86 for key, userDefinedType := range d.TypesByKey { | 86 for key, userDefinedType := range d.TypesByKey { |
87 fileGraph.ResolvedTypes[key] = translateUserDefinedType(userDefi
nedType) | 87 fileGraph.ResolvedTypes[key] = translateUserDefinedType(userDefi
nedType) |
88 } | 88 } |
89 | 89 |
90 » // Add |resolved_values| field. | 90 » // Add |resolved_constants| field. |
91 » fileGraph.ResolvedValues = make(map[string]mojom_types.UserDefinedValue) | 91 » fileGraph.ResolvedConstants = make(map[string]mojom_types.DeclaredConsta
nt) |
92 for key, userDefinedValue := range d.ValuesByKey { | 92 for key, userDefinedValue := range d.ValuesByKey { |
93 » » fileGraph.ResolvedValues[key] = translateUserDefinedValue(userDe
finedValue) | 93 » » switch c := userDefinedValue.(type) { |
| 94 » » // Note that our representation of values in mojom_types.mojom i
s a little different than our |
| 95 » » // pure Go representation. In the latter we use value keys to re
fer to both constants and |
| 96 » » // enum values but in the former we only use value keys to refer
to constants. Enum values |
| 97 » » // are stored as part of their enum types and they are are refer
red to not directly using |
| 98 » » // value keyes but rather via the type key of their enum and an
index into the |values| array |
| 99 » » // of that enum. For this reason we are only looking for the con
stants here and ignoring the |
| 100 » » // enum values. Thos will get translated when the |
| 101 » » case *mojom.UserDefinedConstant: |
| 102 » » » fileGraph.ResolvedConstants[key] = translateUserDefinedC
onstant(c) |
| 103 » » } |
94 } | 104 } |
95 | 105 |
96 // Add |files| field. | 106 // Add |files| field. |
97 fileGraph.Files = make(map[string]mojom_files.MojomFile) | 107 fileGraph.Files = make(map[string]mojom_files.MojomFile) |
98 for name, file := range d.MojomFilesByName { | 108 for name, file := range d.MojomFilesByName { |
99 fileGraph.Files[name] = translateMojomFile(file, &fileGraph) | 109 fileGraph.Files[name] = translateMojomFile(file, &fileGraph) |
100 } | 110 } |
101 | 111 |
102 return &fileGraph | 112 return &fileGraph |
103 } | 113 } |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 } | 410 } |
401 | 411 |
402 func translateUnionField(unionField *mojom.UnionField) mojom_types.UnionField { | 412 func translateUnionField(unionField *mojom.UnionField) mojom_types.UnionField { |
403 outUnionField := mojom_types.UnionField{} | 413 outUnionField := mojom_types.UnionField{} |
404 outUnionField.DeclData = translateDeclarationData(&unionField.Declaratio
nData) | 414 outUnionField.DeclData = translateDeclarationData(&unionField.Declaratio
nData) |
405 outUnionField.Type = translateTypeRef(unionField.FieldType) | 415 outUnionField.Type = translateTypeRef(unionField.FieldType) |
406 outUnionField.Tag = unionField.Tag | 416 outUnionField.Tag = unionField.Tag |
407 return outUnionField | 417 return outUnionField |
408 } | 418 } |
409 | 419 |
410 // WARNING: Do not invoke this function on a UserDefinedValue of type BuiltInCon
stantValue because | 420 func translateUserDefinedConstant(t *mojom.UserDefinedConstant) mojom_types.Decl
aredConstant { |
411 // objects of those types do not have a type_key and do not correspond to a mojo
m_types.UserDefinedValue. | 421 » declaredConstant := mojom_types.DeclaredConstant{} |
412 func translateUserDefinedValue(v mojom.UserDefinedValue) mojom_types.UserDefined
Value { | 422 » declaredConstant.Type = translateTypeRef(t.DeclaredType()) |
413 » switch t := v.(type) { | 423 » declaredConstant.DeclData = *translateDeclarationData(&t.DeclarationData
) |
414 » case *mojom.UserDefinedConstant: | 424 » declaredConstant.Value = translateValueRef(t.ValueRef()) |
415 » » return translateUserDefinedConstant(t) | 425 » // We set the |resolved_concrete_value| field only in the case that the
|value| field is a ConstantReference. |
416 » case *mojom.EnumValue: | 426 » // See the comments for this field in mojom_types.mojom. |
417 » » return &mojom_types.UserDefinedValueEnumValue{translateEnumValue
(t)} | 427 » if _, ok := declaredConstant.Value.(*mojom_types.ValueConstantReference)
; ok { |
418 » case *mojom.BuiltInConstantValue: | 428 » » declaredConstant.ResolvedConcreteValue = translateConcreteValue(
t.ValueRef().ResolvedConcreteValue()) |
419 » » panic("Do not invoke translateUserDefinedValue on BuiltInConstan
tValue.") | |
420 » default: | |
421 » » panic(fmt.Sprintf("Unexpected UserDefinedValue type %T", v)) | |
422 | |
423 » } | |
424 } | |
425 | |
426 func translateUserDefinedConstant(t *mojom.UserDefinedConstant) *mojom_types.Use
rDefinedValueDeclaredConstant { | |
427 » declaredConstant := mojom_types.UserDefinedValueDeclaredConstant{} | |
428 » declaredConstant.Value.Type = translateTypeRef(t.DeclaredType()) | |
429 » declaredConstant.Value.DeclData = *translateDeclarationData(&t.Declarati
onData) | |
430 » declaredConstant.Value.Value = translateValueRef(t.ValueRef()) | |
431 » // We set the |resolved_concrete_value| field only in the following situ
ation. | |
432 » // See the comments in mojom_types.mojom. | |
433 » if _, ok := declaredConstant.Value.Value.(*mojom_types.ValueUserValueRef
erence); ok { | |
434 » » // If the type of the |value| field is a UserValueReference... | |
435 » » userValueRef := t.ValueRef().(*mojom.UserValueRef) | |
436 » » if _, ok := userValueRef.ResolvedDeclaredValue().(*mojom.UserDef
inedConstant); ok { | |
437 » » » // and if that reference resolves to a user-defined cons
tant. | |
438 » » » declaredConstant.Value.ResolvedConcreteValue = translate
ConcreteValue(t.ValueRef().ResolvedConcreteValue()) | |
439 » » } | |
440 } | 429 } |
441 | 430 |
442 » return &declaredConstant | 431 » return declaredConstant |
443 } | 432 } |
444 | 433 |
445 func translateEnumValue(v *mojom.EnumValue) mojom_types.EnumValue { | 434 func translateEnumValue(v *mojom.EnumValue) mojom_types.EnumValue { |
446 enumValue := mojom_types.EnumValue{} | 435 enumValue := mojom_types.EnumValue{} |
447 enumValue.DeclData = translateDeclarationData(&v.DeclarationData) | 436 enumValue.DeclData = translateDeclarationData(&v.DeclarationData) |
448 enumValue.EnumTypeKey = v.EnumType().TypeKey() | |
449 if v.ValueRef() != nil { | 437 if v.ValueRef() != nil { |
450 enumValue.InitializerValue = translateValueRef(v.ValueRef()) | 438 enumValue.InitializerValue = translateValueRef(v.ValueRef()) |
451 } | 439 } |
452 if !v.IntValueComputed { | 440 if !v.IntValueComputed { |
453 panic(fmt.Sprintf("IntValueComputed is false for %v.", v)) | 441 panic(fmt.Sprintf("IntValueComputed is false for %v.", v)) |
454 } | 442 } |
455 enumValue.IntValue = v.ComputedIntValue | 443 enumValue.IntValue = v.ComputedIntValue |
456 return enumValue | 444 return enumValue |
457 } | 445 } |
458 | 446 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 | 554 |
567 func translateConcreteValue(cv mojom.ConcreteValue) mojom_types.Value { | 555 func translateConcreteValue(cv mojom.ConcreteValue) mojom_types.Value { |
568 switch cv := cv.(type) { | 556 switch cv := cv.(type) { |
569 case mojom.LiteralValue: | 557 case mojom.LiteralValue: |
570 return translateLiteralValue(cv) | 558 return translateLiteralValue(cv) |
571 // NOTE: See the comments at the top of types.go for a discussion of the
difference | 559 // NOTE: See the comments at the top of types.go for a discussion of the
difference |
572 // between a value and a value reference. In this function we are transl
ating a | 560 // between a value and a value reference. In this function we are transl
ating a |
573 // value, not a value reference. In the case of a LiteralValue or a | 561 // value, not a value reference. In the case of a LiteralValue or a |
574 // BuiltInConstantValue the distinction is immaterial. But in the case o
f an | 562 // BuiltInConstantValue the distinction is immaterial. But in the case o
f an |
575 // enum value the distinction is important. Here we are building and ret
urning | 563 // enum value the distinction is important. Here we are building and ret
urning |
576 » // a synthetic mojom_types.UserValueReference to represent the enum valu
e. | 564 » // a synthetic mojom_types.EnumValueReference to represent the enum valu
e. |
577 » // It is only the |value_key| field that needs to be populated. It does
not | 565 » // It does not make sense to populate the |identifier| field for because
we |
578 » // make sense to populate the |identifier| field for example because we | |
579 // aren't representing any actual occrence in the .mojom file. | 566 // aren't representing any actual occrence in the .mojom file. |
580 case *mojom.EnumValue: | 567 case *mojom.EnumValue: |
581 » » return &mojom_types.ValueUserValueReference{mojom_types.UserValu
eReference{ | 568 » » return &mojom_types.ValueEnumValueReference{mojom_types.EnumValu
eReference{ |
582 » » » ValueKey: stringPointer(cv.ValueKey())}} | 569 » » » EnumTypeKey: cv.EnumType().TypeKey(), |
| 570 » » » EnumValueIndex: cv.ValueIndex()}} |
583 case mojom.BuiltInConstantValue: | 571 case mojom.BuiltInConstantValue: |
584 return translateBuiltInConstantValue(cv) | 572 return translateBuiltInConstantValue(cv) |
585 default: | 573 default: |
586 panic(fmt.Sprintf("Unexpected ConcreteValue type %T", cv)) | 574 panic(fmt.Sprintf("Unexpected ConcreteValue type %T", cv)) |
587 } | 575 } |
588 } | 576 } |
589 | 577 |
590 func translateLiteralValue(v mojom.LiteralValue) *mojom_types.ValueLiteralValue
{ | 578 func translateLiteralValue(v mojom.LiteralValue) *mojom_types.ValueLiteralValue
{ |
591 var lv mojom_types.LiteralValue | 579 var lv mojom_types.LiteralValue |
592 switch v.ValueType() { | 580 switch v.ValueType() { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity | 623 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNega
tiveInfinity |
636 case mojom.DoubleNAN: | 624 case mojom.DoubleNAN: |
637 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNan | 625 builtInValue.Value = mojom_types.BuiltinConstantValue_DoubleNan |
638 default: | 626 default: |
639 panic(fmt.Sprintf("Unrecognized BuiltInConstantValue %v", t)) | 627 panic(fmt.Sprintf("Unrecognized BuiltInConstantValue %v", t)) |
640 } | 628 } |
641 return &builtInValue | 629 return &builtInValue |
642 } | 630 } |
643 | 631 |
644 func translateUserValueRef(r *mojom.UserValueRef) mojom_types.Value { | 632 func translateUserValueRef(r *mojom.UserValueRef) mojom_types.Value { |
645 » switch t := r.ResolvedConcreteValue().(type) { | 633 » switch t := r.ResolvedDeclaredValue().(type) { |
646 case mojom.BuiltInConstantValue: | 634 case mojom.BuiltInConstantValue: |
647 return translateBuiltInConstantValue(t) | 635 return translateBuiltInConstantValue(t) |
| 636 case *mojom.UserDefinedConstant: |
| 637 return &mojom_types.ValueConstantReference{mojom_types.ConstantR
eference{ |
| 638 Identifier: r.Identifier(), |
| 639 ConstantKey: t.ValueKey()}} |
| 640 case *mojom.EnumValue: |
| 641 return &mojom_types.ValueEnumValueReference{mojom_types.EnumValu
eReference{ |
| 642 Identifier: r.Identifier(), |
| 643 EnumTypeKey: t.EnumType().TypeKey(), |
| 644 EnumValueIndex: t.ValueIndex()}} |
648 default: | 645 default: |
649 » » valueKey := stringPointer(r.ResolvedDeclaredValue().ValueKey()) | 646 » » panic(fmt.Sprintf("Unrecognized UserDefinedValueType %T", r.Reso
lvedDeclaredValue())) |
650 » » return &mojom_types.ValueUserValueReference{mojom_types.UserValu
eReference{ | |
651 » » » Identifier: r.Identifier(), | |
652 » » » ValueKey: valueKey}} | |
653 } | 647 } |
654 } | 648 } |
655 | 649 |
656 func translateDeclarationData(d *mojom.DeclarationData) *mojom_types.Declaration
Data { | 650 func translateDeclarationData(d *mojom.DeclarationData) *mojom_types.Declaration
Data { |
657 declData := mojom_types.DeclarationData{} | 651 declData := mojom_types.DeclarationData{} |
658 | 652 |
659 // attributes field | 653 // attributes field |
660 if d.Attributes() != nil { | 654 if d.Attributes() != nil { |
661 declData.Attributes = new([]mojom_types.Attribute) | 655 declData.Attributes = new([]mojom_types.Attribute) |
662 for _, attr := range d.Attributes().List { | 656 for _, attr := range d.Attributes().List { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 | 747 |
754 // stringPointer is a convenience function for creating a pointer to a string wh
ose value | 748 // stringPointer is a convenience function for creating a pointer to a string wh
ose value |
755 // is the specified string. It may be used in situations where the compiler will | 749 // is the specified string. It may be used in situations where the compiler will |
756 // not allow you to take the address of a string value directly, such as the | 750 // not allow you to take the address of a string value directly, such as the |
757 // return value of a function. It is necessary to create pointers to strings bec
ause | 751 // return value of a function. It is necessary to create pointers to strings bec
ause |
758 // that is how the Mojom type |string?| (i.e. nullable string) is represented in | 752 // that is how the Mojom type |string?| (i.e. nullable string) is represented in |
759 // in the Mojom Go bindings. | 753 // in the Mojom Go bindings. |
760 func stringPointer(s string) *string { | 754 func stringPointer(s string) *string { |
761 return &s | 755 return &s |
762 } | 756 } |
OLD | NEW |