Gen: Add constants
No SnakeCase for constants because GTK exports a lot of overlapping mixed-case const names that would produce conflicts.
- commit
- fd80be9e41f580421fac9eaaff0b4ce424b48039
- author
- Peter Fern · · 2023-12-31 05:38 UTC
- committer
- Peter Fern · · 2024-01-03 08:08 UTC
- parent
- dfc009c
- signature
- unsigned
4 files · +48 -0
M internal/gir/pass/pass.go +8 -0
| @@ -92,6 +92,13 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 92 | 92 | enums[fn] = append(enums[fn], temp) | |
| 93 | 93 | } | |
| 94 | 94 | ||
| 95 | + | constants := make(map[string][]types.ConstantTemplate) | |
| 96 | + | for _, con := range ns.Constants { | |
| 97 | + | fn := con.FilenameSafe() | |
| 98 | + | files = append(files, fn) | |
| 99 | + | constants[fn] = append(constants[fn], con.Template(ns.Name, p.Types)) | |
| 100 | + | } | |
| 101 | + | ||
| 95 | 102 | records := make(map[string][]types.RecordTemplate) | |
| 96 | 103 | recordLookup := make(map[string]bool) | |
| 97 | 104 | for _, rec := range ns.Records { | |
| @@ -332,6 +339,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 332 | 339 | Callbacks: callbacks[fn], | |
| 333 | 340 | Records: records[fn], | |
| 334 | 341 | Enums: enums[fn], | |
| 342 | + | Constants: constants[fn], | |
| 335 | 343 | Functions: functions[fn], | |
| 336 | 344 | Interfaces: interfaces[fn], | |
| 337 | 345 | Classes: classes[fn], | |
| @@ -92,6 +92,13 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 92 | enums[fn] = append(enums[fn], temp) | 92 | enums[fn] = append(enums[fn], temp) | ||
| 93 | } | 93 | } | ||
| 94 | 94 | ||||
| 95 | + | constants := make(map[string][]types.ConstantTemplate) | |||
| 96 | + | for _, con := range ns.Constants { | |||
| 97 | + | fn := con.FilenameSafe() | |||
| 98 | + | files = append(files, fn) | |||
| 99 | + | constants[fn] = append(constants[fn], con.Template(ns.Name, p.Types)) | |||
| 100 | + | } | |||
| 101 | + | ||||
| 95 | records := make(map[string][]types.RecordTemplate) | 102 | records := make(map[string][]types.RecordTemplate) | ||
| 96 | recordLookup := make(map[string]bool) | 103 | recordLookup := make(map[string]bool) | ||
| 97 | for _, rec := range ns.Records { | 104 | for _, rec := range ns.Records { | ||
| @@ -332,6 +339,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 332 | Callbacks: callbacks[fn], | 339 | Callbacks: callbacks[fn], | ||
| 333 | Records: records[fn], | 340 | Records: records[fn], | ||
| 334 | Enums: enums[fn], | 341 | Enums: enums[fn], | ||
| 342 | + | Constants: constants[fn], | |||
| 335 | Functions: functions[fn], | 343 | Functions: functions[fn], | ||
| 336 | Interfaces: interfaces[fn], | 344 | Interfaces: interfaces[fn], | ||
| 337 | Classes: classes[fn], | 345 | Classes: classes[fn], | ||
M internal/gir/types/template.go +13 -0
| @@ -202,6 +202,17 @@ type EnumTemplate struct { | |||
| 202 | 202 | Values []enumValues | |
| 203 | 203 | } | |
| 204 | 204 | ||
| 205 | + | type ConstantTemplate struct { | |
| 206 | + | // Name is the name of the constant | |
| 207 | + | Name string | |
| 208 | + | // Doc is the documentation for the constant | |
| 209 | + | Doc string | |
| 210 | + | // Type is the Go type for the constant | |
| 211 | + | Type string | |
| 212 | + | // Values are the list of values for the constant | |
| 213 | + | Value string | |
| 214 | + | } | |
| 215 | + | ||
| 205 | 216 | type funcRetTemplate struct { | |
| 206 | 217 | // Raw is the raw value for the underlying purego function | |
| 207 | 218 | Raw string | |
| @@ -380,6 +391,8 @@ type TemplateArg struct { | |||
| 380 | 391 | Callbacks []CallbackTemplate | |
| 381 | 392 | // Enums are enumerations declared as const ... .... = .... | |
| 382 | 393 | Enums []EnumTemplate | |
| 394 | + | // Constants are declared as const ... .... = .... | |
| 395 | + | Constants []ConstantTemplate | |
| 383 | 396 | // Functions are the Go function declarations | |
| 384 | 397 | Functions []FuncTemplate | |
| 385 | 398 | // Interfaces is the list of interfaces that this package implements | |
| @@ -202,6 +202,17 @@ type EnumTemplate struct { | |||||
| 202 | Values []enumValues | 202 | Values []enumValues | ||
| 203 | } | 203 | } | ||
| 204 | 204 | ||||
| 205 | + | type ConstantTemplate struct { | |||
| 206 | + | // Name is the name of the constant | |||
| 207 | + | Name string | |||
| 208 | + | // Doc is the documentation for the constant | |||
| 209 | + | Doc string | |||
| 210 | + | // Type is the Go type for the constant | |||
| 211 | + | Type string | |||
| 212 | + | // Values are the list of values for the constant | |||
| 213 | + | Value string | |||
| 214 | + | } | |||
| 215 | + | ||||
| 205 | type funcRetTemplate struct { | 216 | type funcRetTemplate struct { | ||
| 206 | // Raw is the raw value for the underlying purego function | 217 | // Raw is the raw value for the underlying purego function | ||
| 207 | Raw string | 218 | Raw string | ||
| @@ -380,6 +391,8 @@ type TemplateArg struct { | |||||
| 380 | Callbacks []CallbackTemplate | 391 | Callbacks []CallbackTemplate | ||
| 381 | // Enums are enumerations declared as const ... .... = .... | 392 | // Enums are enumerations declared as const ... .... = .... | ||
| 382 | Enums []EnumTemplate | 393 | Enums []EnumTemplate | ||
| 394 | + | // Constants are declared as const ... .... = .... | |||
| 395 | + | Constants []ConstantTemplate | |||
| 383 | // Functions are the Go function declarations | 396 | // Functions are the Go function declarations | ||
| 384 | Functions []FuncTemplate | 397 | Functions []FuncTemplate | ||
| 385 | // Interfaces is the list of interfaces that this package implements | 398 | // Interfaces is the list of interfaces that this package implements | ||
M internal/gir/types/types.go +18 -0
| @@ -4,6 +4,7 @@ package types | |||
| 4 | 4 | ||
| 5 | 5 | import ( | |
| 6 | 6 | "encoding/xml" | |
| 7 | + | "fmt" | |
| 7 | 8 | "strconv" | |
| 8 | 9 | "strings" | |
| 9 | 10 | ||
| @@ -333,6 +334,23 @@ type Constant struct { | |||
| 333 | 334 | InfoElements | |
| 334 | 335 | } | |
| 335 | 336 | ||
| 337 | + | func (c *Constant) Template(ns string, kinds KindMap) ConstantTemplate { | |
| 338 | + | t := c.Type.Template(ns, kinds) | |
| 339 | + | v := c.Value | |
| 340 | + | ||
| 341 | + | switch t { | |
| 342 | + | case "string": | |
| 343 | + | v = fmt.Sprintf(`"%s"`, v) | |
| 344 | + | } | |
| 345 | + | ||
| 346 | + | return ConstantTemplate{ | |
| 347 | + | Name: c.Name, | |
| 348 | + | Doc: c.Doc.StringSafe(), | |
| 349 | + | Type: t, | |
| 350 | + | Value: v, | |
| 351 | + | } | |
| 352 | + | } | |
| 353 | + | ||
| 336 | 354 | type Constructor struct { | |
| 337 | 355 | XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"` | |
| 338 | 356 | CallableAttrs | |
| @@ -4,6 +4,7 @@ package types | |||||
| 4 | 4 | ||||
| 5 | import ( | 5 | import ( | ||
| 6 | "encoding/xml" | 6 | "encoding/xml" | ||
| 7 | + | "fmt" | |||
| 7 | "strconv" | 8 | "strconv" | ||
| 8 | "strings" | 9 | "strings" | ||
| 9 | 10 | ||||
| @@ -333,6 +334,23 @@ type Constant struct { | |||||
| 333 | InfoElements | 334 | InfoElements | ||
| 334 | } | 335 | } | ||
| 335 | 336 | ||||
| 337 | + | func (c *Constant) Template(ns string, kinds KindMap) ConstantTemplate { | |||
| 338 | + | t := c.Type.Template(ns, kinds) | |||
| 339 | + | v := c.Value | |||
| 340 | + | ||||
| 341 | + | switch t { | |||
| 342 | + | case "string": | |||
| 343 | + | v = fmt.Sprintf(`"%s"`, v) | |||
| 344 | + | } | |||
| 345 | + | ||||
| 346 | + | return ConstantTemplate{ | |||
| 347 | + | Name: c.Name, | |||
| 348 | + | Doc: c.Doc.StringSafe(), | |||
| 349 | + | Type: t, | |||
| 350 | + | Value: v, | |||
| 351 | + | } | |||
| 352 | + | } | |||
| 353 | + | ||||
| 336 | type Constructor struct { | 354 | type Constructor struct { | ||
| 337 | XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"` | 355 | XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"` | ||
| 338 | CallableAttrs | 356 | CallableAttrs | ||
M templates/go +9 -0
| @@ -104,6 +104,15 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret | |||
| 104 | 104 | type {{.Name}} = {{.Value}} | |
| 105 | 105 | {{end}} | |
| 106 | 106 | ||
| 107 | + | {{if .Constants -}} | |
| 108 | + | const ( | |
| 109 | + | {{range .Constants -}} | |
| 110 | + | {{.Doc}} | |
| 111 | + | {{.Name}} {{.Type}} = {{.Value}} | |
| 112 | + | {{end}} | |
| 113 | + | ) | |
| 114 | + | {{end}} | |
| 115 | + | ||
| 107 | 116 | {{range .Enums -}} | |
| 108 | 117 | {{.Doc}} | |
| 109 | 118 | type {{.Name}} int | |
| @@ -104,6 +104,15 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret | |||||
| 104 | type {{.Name}} = {{.Value}} | 104 | type {{.Name}} = {{.Value}} | ||
| 105 | {{end}} | 105 | {{end}} | ||
| 106 | 106 | ||||
| 107 | + | {{if .Constants -}} | |||
| 108 | + | const ( | |||
| 109 | + | {{range .Constants -}} | |||
| 110 | + | {{.Doc}} | |||
| 111 | + | {{.Name}} {{.Type}} = {{.Value}} | |||
| 112 | + | {{end}} | |||
| 113 | + | ) | |||
| 114 | + | {{end}} | |||
| 115 | + | ||||
| 107 | {{range .Enums -}} | 116 | {{range .Enums -}} | ||
| 108 | {{.Doc}} | 117 | {{.Doc}} | ||
| 109 | type {{.Name}} int | 118 | type {{.Name}} int | ||