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

Side by Side Diff: mojom/generators/go/translator/translator.go

Issue 2045063002: [New go generator] Implement declaring unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 6 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 translator 5 package translator
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "log" 9 "log"
10 "sort" 10 "sort"
(...skipping 23 matching lines...) Expand all
34 tmplFile = new(TmplFile) 34 tmplFile = new(TmplFile)
35 file := t.fileGraph.Files[fileName] 35 file := t.fileGraph.Files[fileName]
36 36
37 tmplFile.PackageName = fileNameToPackageName(fileName) 37 tmplFile.PackageName = fileNameToPackageName(fileName)
38 38
39 tmplFile.Structs = make([]*StructTemplate, len(*file.DeclaredMojomObject s.Structs)) 39 tmplFile.Structs = make([]*StructTemplate, len(*file.DeclaredMojomObject s.Structs))
40 for i, typeKey := range *file.DeclaredMojomObjects.Structs { 40 for i, typeKey := range *file.DeclaredMojomObjects.Structs {
41 tmplFile.Structs[i] = t.translateMojomStruct(typeKey) 41 tmplFile.Structs[i] = t.translateMojomStruct(typeKey)
42 } 42 }
43 43
44 tmplFile.Unions = make([]*UnionTemplate, len(*file.DeclaredMojomObjects. Unions))
45 for i, typeKey := range *file.DeclaredMojomObjects.Unions {
46 tmplFile.Unions[i] = t.translateMojomUnion(typeKey)
47 }
48
44 tmplFile.Imports = []Import{ 49 tmplFile.Imports = []Import{
45 Import{PackagePath: "mojo/public/go/bindings", PackageName: "bin dings"}, 50 Import{PackagePath: "mojo/public/go/bindings", PackageName: "bin dings"},
46 Import{PackagePath: "fmt", PackageName: "fmt"}, 51 Import{PackagePath: "fmt", PackageName: "fmt"},
47 Import{PackagePath: "sort", PackageName: "sort"}, 52 Import{PackagePath: "sort", PackageName: "sort"},
48 } 53 }
49 return tmplFile 54 return tmplFile
50 } 55 }
51 56
52 func (t *translator) GetUserDefinedType(typeKey string) (mojomType mojom_types.U serDefinedType) { 57 func (t *translator) GetUserDefinedType(typeKey string) (mojomType mojom_types.U serDefinedType) {
53 return t.fileGraph.ResolvedTypes[typeKey] 58 return t.fileGraph.ResolvedTypes[typeKey]
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 91
87 func (t *translator) translateStructField(mojomField *mojom_types.StructField) ( field StructFieldTemplate) { 92 func (t *translator) translateStructField(mojomField *mojom_types.StructField) ( field StructFieldTemplate) {
88 field.Name = formatName(*mojomField.DeclData.ShortName) 93 field.Name = formatName(*mojomField.DeclData.ShortName)
89 field.Type = t.translateType(mojomField.Type) 94 field.Type = t.translateType(mojomField.Type)
90 field.MinVersion = mojomField.MinVersion 95 field.MinVersion = mojomField.MinVersion
91 field.EncodingInfo = t.encodingInfo(mojomField.Type) 96 field.EncodingInfo = t.encodingInfo(mojomField.Type)
92 field.EncodingInfo.setIdentifier("s." + field.Name) 97 field.EncodingInfo.setIdentifier("s." + field.Name)
93 return 98 return
94 } 99 }
95 100
101 func (t *translator) translateMojomUnion(typeKey string) (m *UnionTemplate) {
102 m = new(UnionTemplate)
103 u := t.GetUserDefinedType(typeKey)
104 union, ok := u.Interface().(mojom_types.MojomUnion)
105 if !ok {
106 panic(fmt.Sprintf("%s is not a union.", userDefinedTypeShortName (u)))
107 }
108 m.Name = t.goTypeName(typeKey)
109
110 for _, field := range union.Fields {
111 m.Fields = append(m.Fields, t.translateUnionField(&field))
112 m.Fields[len(m.Fields)-1].Union = m
113 }
114
115 return m
116 }
117
118 func (t *translator) translateUnionField(mojomField *mojom_types.UnionField) (fi eld UnionFieldTemplate) {
119 field.Name = formatName(*mojomField.DeclData.ShortName)
120 field.Type = t.translateType(mojomField.Type)
121 field.Tag = mojomField.Tag
122 return field
123 }
124
96 func (t *translator) encodingInfo(mojomType mojom_types.Type) EncodingInfo { 125 func (t *translator) encodingInfo(mojomType mojom_types.Type) EncodingInfo {
97 return t.encodingInfoNested(mojomType, 0) 126 return t.encodingInfoNested(mojomType, 0)
98 } 127 }
99 128
100 func (t *translator) encodingInfoNested(mojomType mojom_types.Type, level int) ( info EncodingInfo) { 129 func (t *translator) encodingInfoNested(mojomType mojom_types.Type, level int) ( info EncodingInfo) {
101 switch m := mojomType.(type) { 130 switch m := mojomType.(type) {
102 default: 131 default:
103 panic("This should never happen.") 132 panic("This should never happen.")
104 case *mojom_types.TypeSimpleType: 133 case *mojom_types.TypeSimpleType:
105 info = t.simpleTypeEncodingInfo(m.Value) 134 info = t.simpleTypeEncodingInfo(m.Value)
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 if s[i].Offset == s[j].Offset && s[i].Bit < s[j].Bit { 277 if s[i].Offset == s[j].Offset && s[i].Bit < s[j].Bit {
249 return true 278 return true
250 } 279 }
251 280
252 return false 281 return false
253 } 282 }
254 283
255 func (s structFieldSerializationSorter) Swap(i, j int) { 284 func (s structFieldSerializationSorter) Swap(i, j int) {
256 s[i], s[j] = s[j], s[i] 285 s[i], s[j] = s[j], s[i]
257 } 286 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698