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

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

Issue 2233963003: Add support for constants in the new go generator. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Merge branch 'master' of github.com:domokit/mojo into const Created 4 years, 4 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"
11 11
12 "mojom/generated/mojom_files" 12 "mojom/generated/mojom_files"
13 "mojom/generated/mojom_types" 13 "mojom/generated/mojom_types"
14 "mojom/generators/common" 14 "mojom/generators/common"
15 ) 15 )
16 16
17 type Translator interface { 17 type Translator interface {
18 TranslateMojomFile(fileName string) *TmplFile 18 TranslateMojomFile(fileName string) *TmplFile
19 } 19 }
20 20
21 type translator struct { 21 type translator struct {
22 fileGraph *mojom_files.MojomFileGraph 22 fileGraph *mojom_files.MojomFileGraph
23 // goTypeCache maps type keys to go type strings. 23 // goTypeCache maps type keys to go type strings.
24 » goTypeCache map[string]string 24 » goTypeCache map[string]string
25 » imports map[string]string 25 » goConstNameCache map[string]string
26 » currentFileName string 26 » imports map[string]string
27 » pkgName string 27 » currentFileName string
28 » Config common.GeneratorConfig 28 » pkgName string
29 » Config common.GeneratorConfig
29 } 30 }
30 31
31 func NewTranslator(fileGraph *mojom_files.MojomFileGraph) (t *translator) { 32 func NewTranslator(fileGraph *mojom_files.MojomFileGraph) (t *translator) {
32 t = new(translator) 33 t = new(translator)
33 t.fileGraph = fileGraph 34 t.fileGraph = fileGraph
34 t.goTypeCache = map[string]string{} 35 t.goTypeCache = map[string]string{}
36 t.goConstNameCache = map[string]string{}
35 t.imports = map[string]string{} 37 t.imports = map[string]string{}
36 return t 38 return t
37 } 39 }
38 40
39 func (t *translator) TranslateMojomFile(fileName string) (tmplFile *TmplFile) { 41 func (t *translator) TranslateMojomFile(fileName string) (tmplFile *TmplFile) {
40 t.currentFileName = fileName 42 t.currentFileName = fileName
41 t.imports = map[string]string{} 43 t.imports = map[string]string{}
42 44
43 tmplFile = new(TmplFile) 45 tmplFile = new(TmplFile)
44 file := t.fileGraph.Files[fileName] 46 file := t.fileGraph.Files[fileName]
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 file.DeclaredMojomObjects.Interfaces = &[]string{} 89 file.DeclaredMojomObjects.Interfaces = &[]string{}
88 } 90 }
89 tmplFile.Interfaces = make([]*InterfaceTemplate, len(*file.DeclaredMojom Objects.Interfaces)) 91 tmplFile.Interfaces = make([]*InterfaceTemplate, len(*file.DeclaredMojom Objects.Interfaces))
90 for i, typeKey := range *file.DeclaredMojomObjects.Interfaces { 92 for i, typeKey := range *file.DeclaredMojomObjects.Interfaces {
91 tmplFile.Interfaces[i] = t.translateMojomInterface(typeKey) 93 tmplFile.Interfaces[i] = t.translateMojomInterface(typeKey)
92 if len(tmplFile.Interfaces[i].Methods) > 0 { 94 if len(tmplFile.Interfaces[i].Methods) > 0 {
93 methodDefined = true 95 methodDefined = true
94 } 96 }
95 } 97 }
96 98
99 if file.DeclaredMojomObjects.TopLevelConstants == nil {
100 file.DeclaredMojomObjects.TopLevelConstants = &[]string{}
101 }
102 if file.DeclaredMojomObjects.EmbeddedConstants == nil {
103 file.DeclaredMojomObjects.EmbeddedConstants = &[]string{}
104 }
105 for _, constKey := range *file.DeclaredMojomObjects.TopLevelConstants {
106 c := t.translateDeclaredConstant(constKey)
107 if c != nil {
108 tmplFile.Constants = append(tmplFile.Constants, c)
109 }
110 }
111 for _, constKey := range *file.DeclaredMojomObjects.EmbeddedConstants {
112 c := t.translateDeclaredConstant(constKey)
113 if c != nil {
114 tmplFile.Constants = append(tmplFile.Constants, c)
115 }
116 }
117
97 tmplFile.Imports = []Import{} 118 tmplFile.Imports = []Import{}
98 tmplFile.MojomImports = []string{} 119 tmplFile.MojomImports = []string{}
99 120
100 for pkgName, pkgPath := range t.imports { 121 for pkgName, pkgPath := range t.imports {
101 if pkgName != "service_describer" && pkgName != "mojom_types" && pkgPath != "mojo/public/go/system" { 122 if pkgName != "service_describer" && pkgName != "mojom_types" && pkgPath != "mojo/public/go/system" {
102 tmplFile.MojomImports = append(tmplFile.MojomImports, pk gName) 123 tmplFile.MojomImports = append(tmplFile.MojomImports, pk gName)
103 } 124 }
104 } 125 }
105 126
106 if len(tmplFile.Structs) > 0 || len(tmplFile.Unions) > 0 || len(tmplFile .Interfaces) > 0 { 127 if len(tmplFile.Structs) > 0 || len(tmplFile.Unions) > 0 || len(tmplFile .Interfaces) > 0 {
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 m.Params.Name = fmt.Sprintf("%s_Params", m.FullName) 313 m.Params.Name = fmt.Sprintf("%s_Params", m.FullName)
293 m.Params.PrivateName = privateName(m.Params.Name) 314 m.Params.PrivateName = privateName(m.Params.Name)
294 if mojomMethod.ResponseParams != nil { 315 if mojomMethod.ResponseParams != nil {
295 m.ResponseParams = t.translateMojomStructObject(*mojomMethod.Res ponseParams) 316 m.ResponseParams = t.translateMojomStructObject(*mojomMethod.Res ponseParams)
296 m.ResponseParams.Name = fmt.Sprintf("%s_ResponseParams", m.FullN ame) 317 m.ResponseParams.Name = fmt.Sprintf("%s_ResponseParams", m.FullN ame)
297 m.ResponseParams.PrivateName = privateName(m.ResponseParams.Name ) 318 m.ResponseParams.PrivateName = privateName(m.ResponseParams.Name )
298 } 319 }
299 return m 320 return m
300 } 321 }
301 322
323 func (t *translator) translateDeclaredConstant(constKey string) (c *ConstantTemp late) {
324 declaredConstant := t.fileGraph.ResolvedConstants[constKey]
325 resolvedValue := t.resolveConstRef(declaredConstant.Value)
326 if _, ok := resolvedValue.(*mojom_types.ValueBuiltinValue); ok {
327 // There is no way to have a constant NaN, or infinity in go.
328 return nil
329 }
330 c = new(ConstantTemplate)
331 c.Name = t.goConstName(constKey)
332 c.Type = t.translateType(declaredConstant.Type)
333 c.Value = t.translateValue(declaredConstant.Value)
334 return c
335 }
336
302 //////////////////////////////////////////////////////////////////////////////// 337 ////////////////////////////////////////////////////////////////////////////////
303 338
304 // Implements sort.Interface. 339 // Implements sort.Interface.
305 type structFieldSerializationSorter []mojom_types.StructField 340 type structFieldSerializationSorter []mojom_types.StructField
306 341
307 func (s structFieldSerializationSorter) Len() int { 342 func (s structFieldSerializationSorter) Len() int {
308 return len(s) 343 return len(s)
309 } 344 }
310 345
311 func (s structFieldSerializationSorter) Less(i, j int) bool { 346 func (s structFieldSerializationSorter) Less(i, j int) bool {
312 if s[i].Offset < s[j].Offset { 347 if s[i].Offset < s[j].Offset {
313 return true 348 return true
314 } 349 }
315 350
316 if s[i].Offset == s[j].Offset && s[i].Bit < s[j].Bit { 351 if s[i].Offset == s[j].Offset && s[i].Bit < s[j].Bit {
317 return true 352 return true
318 } 353 }
319 354
320 return false 355 return false
321 } 356 }
322 357
323 func (s structFieldSerializationSorter) Swap(i, j int) { 358 func (s structFieldSerializationSorter) Swap(i, j int) {
324 s[i], s[j] = s[j], s[i] 359 s[i], s[j] = s[j], s[i]
325 } 360 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698