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

Side by Side Diff: mojom/mojom_parser/serialization/serialization.go

Issue 1805743003: Mojom frontend: Compute, validate and populate struct field version info (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rename ComputeDataForGenerators to ComputeFinalData Created 4 years, 9 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 // 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"
11 "fmt" 11 "fmt"
12 "mojo/public/go/bindings" 12 "mojo/public/go/bindings"
13 "mojom/mojom_parser/generated/mojom_files" 13 "mojom/mojom_parser/generated/mojom_files"
14 "mojom/mojom_parser/generated/mojom_types" 14 "mojom/mojom_parser/generated/mojom_types"
15 "mojom/mojom_parser/mojom" 15 "mojom/mojom_parser/mojom"
16 myfmt "third_party/golang/src/fmt" 16 myfmt "third_party/golang/src/fmt"
17 ) 17 )
18 18
19 ////////////////////////////////////////////////// 19 //////////////////////////////////////////////////
20 /// Mojom Descriptor Serialization 20 /// Mojom Descriptor Serialization
21 ////////////////////////////////////////////////// 21 //////////////////////////////////////////////////
22 22
23 // This variable may be set to false in order to omit emitting line and 23 // This variable may be set to false in order to omit emitting line and
24 // column numbers. 24 // column numbers. This is useful in tests.
25 var emitLineAndColumnNumbers bool = true 25 var emitLineAndColumnNumbers bool = true
26 26
27 // This variable may be set to false in order to omit emitting computed
28 // packing. This is useful in tests.
29 var emitComputedPackingData bool = true
30
27 // This variable may be set to false in order to omit emitting serialized 31 // This variable may be set to false in order to omit emitting serialized
28 // runtime type info. 32 // runtime type info. This is useful in tests.
29 var emitSerializedRuntimeTypeInfo bool = true 33 var emitSerializedRuntimeTypeInfo bool = true
30 34
31 // By default we do not populate the complete type set of each top-level interfa ce 35 // By default we do not populate the complete type set of each top-level interfa ce
32 // because doing so is expensive and we are not currently using the the data. 36 // because doing so is expensive and we are not currently using the the data.
33 var populateCompleteTypeSet bool = false 37 var populateCompleteTypeSet bool = false
34 38
35 // Serialize serializes the MojomDescriptor into a binary form that is passed to the 39 // Serialize serializes the MojomDescriptor into a binary form that is passed to the
36 // backend of the compiler in order to invoke the code generators. 40 // backend of the compiler in order to invoke the code generators.
37 // To do this we use Mojo serialization. 41 // To do this we use Mojo serialization.
38 // If |debug| is true we also return a human-readable representation 42 // If |debug| is true we also return a human-readable representation
39 // of the serialized mojom_types.FileGraph. 43 // of the serialized mojom_types.FileGraph.
40 // This function is not thread safe. 44 // This function is not thread safe.
41 func Serialize(d *mojom.MojomDescriptor, debug bool) (bytes []byte, debugString string, err error) { 45 func Serialize(d *mojom.MojomDescriptor, debug bool) (bytes []byte, debugString string, err error) {
42 » return serialize(d, debug, true, true, false) 46 » return serialize(d, debug, true, true, true, false)
43 } 47 }
44 48
45 // serialize() is a package-private version of the public method Serialize(). 49 // serialize() is a package-private version of the public method Serialize().
46 // It is intended for use in tests because it allows setting of the variables 50 // It is intended for use in tests because it allows setting of the variables
47 // emitLineAndColumnNumbers, emitSerializedRuntimeTypeInfo and populateCompleteT ypeSet. 51 // emitLineAndColumnNumbers, emitComputedPackingData, emitSerializedRuntimeTypeI nfo
52 // and populateCompleteTypeSet.
48 // This function is not thread safe because it sets and accesses these global 53 // This function is not thread safe because it sets and accesses these global
49 // variables. 54 // variables.
50 func serialize(d *mojom.MojomDescriptor, debug, 55 func serialize(d *mojom.MojomDescriptor, debug,
51 » emitLineAndColumnNumbersParam, emitSerializedRuntimeTypeInfoParam, 56 » emitLineAndColumnNumbersParam, emitComputedPackingDataParam, emitSeriali zedRuntimeTypeInfoParam,
52 populateCompleteTypeSetParam bool) (bytes []byte, debugString string, er r error) { 57 populateCompleteTypeSetParam bool) (bytes []byte, debugString string, er r error) {
53 58
54 // Save the global state and then set it based on the parameters. 59 // Save the global state and then set it based on the parameters.
55 saveEmitLineAndColumnNumbers := emitLineAndColumnNumbers 60 saveEmitLineAndColumnNumbers := emitLineAndColumnNumbers
56 emitLineAndColumnNumbers = emitLineAndColumnNumbersParam 61 emitLineAndColumnNumbers = emitLineAndColumnNumbersParam
62 saveEmitComputedPackingData := emitComputedPackingData
63 emitComputedPackingData = emitComputedPackingDataParam
57 saveEmitSerializedRuntimeTypeInfo := emitSerializedRuntimeTypeInfo 64 saveEmitSerializedRuntimeTypeInfo := emitSerializedRuntimeTypeInfo
58 emitSerializedRuntimeTypeInfo = emitSerializedRuntimeTypeInfoParam 65 emitSerializedRuntimeTypeInfo = emitSerializedRuntimeTypeInfoParam
59 savePopulateCompleteTypeSet := populateCompleteTypeSet 66 savePopulateCompleteTypeSet := populateCompleteTypeSet
60 populateCompleteTypeSet = populateCompleteTypeSetParam 67 populateCompleteTypeSet = populateCompleteTypeSetParam
61 68
62 fileGraph := translateDescriptor(d) 69 fileGraph := translateDescriptor(d)
63 if debug { 70 if debug {
64 debugString = myfmt.Sprintf("%#v", fileGraph) 71 debugString = myfmt.Sprintf("%#v", fileGraph)
65 } 72 }
66 encoder := bindings.NewEncoder() 73 encoder := bindings.NewEncoder()
67 encoder.SetDeterministic(true) 74 encoder.SetDeterministic(true)
68 fileGraph.Encode(encoder) 75 fileGraph.Encode(encoder)
69 bytes, _, err = encoder.Data() 76 bytes, _, err = encoder.Data()
70 77
71 // Restore the original values of the global state. 78 // Restore the original values of the global state.
72 emitLineAndColumnNumbers = saveEmitLineAndColumnNumbers 79 emitLineAndColumnNumbers = saveEmitLineAndColumnNumbers
80 emitComputedPackingData = saveEmitComputedPackingData
73 emitSerializedRuntimeTypeInfo = saveEmitSerializedRuntimeTypeInfo 81 emitSerializedRuntimeTypeInfo = saveEmitSerializedRuntimeTypeInfo
74 populateCompleteTypeSet = savePopulateCompleteTypeSet 82 populateCompleteTypeSet = savePopulateCompleteTypeSet
75 return 83 return
76 } 84 }
77 85
78 // translateDescriptor translates from a mojom.MojomDescriptor (the pure Go 86 // translateDescriptor translates from a mojom.MojomDescriptor (the pure Go
79 // representation used by the parser) to a mojom_files.MojomFileGraph (the 87 // representation used by the parser) to a mojom_files.MojomFileGraph (the
80 // Mojo Go representation used for serialization.) 88 // Mojo Go representation used for serialization.)
81 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph { 89 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph {
82 fileGraph := mojom_files.MojomFileGraph{} 90 fileGraph := mojom_files.MojomFileGraph{}
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 291
284 func translateMojomStruct(s *mojom.MojomStruct) mojom_types.MojomStruct { 292 func translateMojomStruct(s *mojom.MojomStruct) mojom_types.MojomStruct {
285 mojomStruct := mojom_types.MojomStruct{} 293 mojomStruct := mojom_types.MojomStruct{}
286 mojomStruct.DeclData = translateDeclarationData(&s.DeclarationData) 294 mojomStruct.DeclData = translateDeclarationData(&s.DeclarationData)
287 mojomStruct.DeclData.ContainedDeclarations = translateContainedDeclarati ons(&s.NestedDeclarations) 295 mojomStruct.DeclData.ContainedDeclarations = translateContainedDeclarati ons(&s.NestedDeclarations)
288 296
289 for _, field := range s.FieldsInOrdinalOrder() { 297 for _, field := range s.FieldsInOrdinalOrder() {
290 mojomStruct.Fields = append(mojomStruct.Fields, translateStructF ield(field)) 298 mojomStruct.Fields = append(mojomStruct.Fields, translateStructF ield(field))
291 } 299 }
292 300
293 » // TODO(rudominer) Implement VersionInfo. 301 » if emitComputedPackingData {
294 » //mojomStruct.Value.VersionInfo = new([]mojom_types.StructVersion) 302 » » versionInfo := s.VersionInfo()
303 » » mojomStruct.VersionInfo = new([]mojom_types.StructVersion)
304 » » for _, version := range versionInfo {
305 » » » (*mojomStruct.VersionInfo) = append(*mojomStruct.Version Info,
306 » » » » translateStructVersion(version))
307 » » }
308 » }
295 309
296 return mojomStruct 310 return mojomStruct
297 } 311 }
298 312
313 func translateStructVersion(v mojom.StructVersion) mojom_types.StructVersion {
314 return mojom_types.StructVersion{
315 VersionNumber: v.VersionNumber,
316 NumFields: v.NumFields,
317 NumBytes: v.NumBytes,
318 }
319 }
320
299 func translateStructField(f *mojom.StructField) (field mojom_types.StructField) { 321 func translateStructField(f *mojom.StructField) (field mojom_types.StructField) {
300 field.DeclData = translateDeclarationData(&f.DeclarationData) 322 field.DeclData = translateDeclarationData(&f.DeclarationData)
301 field.Type = translateTypeRef(f.FieldType) 323 field.Type = translateTypeRef(f.FieldType)
302 if f.DefaultValue != nil { 324 if f.DefaultValue != nil {
303 field.DefaultValue = translateDefaultFieldValue(f.DefaultValue) 325 field.DefaultValue = translateDefaultFieldValue(f.DefaultValue)
304 } 326 }
305 » // TODO(rudominer) Implement field offsets. 327 » if emitComputedPackingData {
306 » field.Offset = f.Offset 328 » » // TODO(rudominer) Check the allowed size of offsets. The type u sed in
329 » » // mojom_types.mojom might need to be changed.
330 » » field.Offset = int32(f.Offset())
331 » » field.Bit = int8(f.Bit())
332 » » field.MinVersion = f.MinVersion()
333 » }
307 return 334 return
308 } 335 }
309 336
310 func translateDefaultFieldValue(v mojom.ValueRef) mojom_types.DefaultFieldValue { 337 func translateDefaultFieldValue(v mojom.ValueRef) mojom_types.DefaultFieldValue {
311 switch v := v.(type) { 338 switch v := v.(type) {
312 case mojom.LiteralValue: 339 case mojom.LiteralValue:
313 if v.IsDefault() { 340 if v.IsDefault() {
314 return &mojom_types.DefaultFieldValueDefaultKeyword{mojo m_types.DefaultKeyword{}} 341 return &mojom_types.DefaultFieldValueDefaultKeyword{mojo m_types.DefaultKeyword{}}
315 } 342 }
316 return &mojom_types.DefaultFieldValueValue{translateLiteralValue (v)} 343 return &mojom_types.DefaultFieldValueValue{translateLiteralValue (v)}
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 728
702 // stringPointer is a convenience function for creating a pointer to a string wh ose value 729 // stringPointer is a convenience function for creating a pointer to a string wh ose value
703 // is the specified string. It may be used in situations where the compiler will 730 // is the specified string. It may be used in situations where the compiler will
704 // not allow you to take the address of a string value directly, such as the 731 // not allow you to take the address of a string value directly, such as the
705 // return value of a function. It is necessary to create pointers to strings bec ause 732 // return value of a function. It is necessary to create pointers to strings bec ause
706 // that is how the Mojom type |string?| (i.e. nullable string) is represented in 733 // that is how the Mojom type |string?| (i.e. nullable string) is represented in
707 // in the Mojom Go bindings. 734 // in the Mojom Go bindings.
708 func stringPointer(s string) *string { 735 func stringPointer(s string) *string {
709 return &s 736 return &s
710 } 737 }
OLDNEW
« no previous file with comments | « mojom/mojom_parser/parser/parse_driver.go ('k') | mojom/mojom_parser/serialization/serialization_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698