Gen: Add functions for classes
- commit
- 8fc492911b9c142c3bd94663893beae07d214256
- author
- Peter Fern · · 2023-12-29 04:29 UTC
- committer
- Peter Fern · · 2024-01-03 01:44 UTC
- parent
- c57ccf3
- signature
- unsigned
3 files · +33 -12
M internal/gir/pass/pass.go +17 -12
| @@ -212,6 +212,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 212 | 212 | for _, cls := range ns.Classes { | |
| 213 | 213 | implemented := make(map[string]bool) | |
| 214 | 214 | constructors := make([]types.FuncTemplate, len(cls.Constructors)) | |
| 215 | + | functions := make([]types.FuncTemplate, len(cls.Functions)) | |
| 215 | 216 | fn := cls.FilenameSafe() | |
| 216 | 217 | files = append(files, fn) | |
| 217 | 218 | ||
| @@ -248,6 +249,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 248 | 249 | } | |
| 249 | 250 | } | |
| 250 | 251 | var interfaces []types.InterfaceTemplate | |
| 252 | + | for i, f := range cls.Functions { | |
| 253 | + | name := fmt.Sprintf("%s%s", util.SnakeToCamel(cls.Name), util.SnakeToCamel(f.Name)) | |
| 254 | + | functions[i] = types.FuncTemplate{ | |
| 255 | + | Name: name, | |
| 256 | + | CName: f.CIdentifier, | |
| 257 | + | Doc: f.Doc.StringSafe(), | |
| 258 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |
| 259 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |
| 260 | + | } | |
| 261 | + | } | |
| 251 | 262 | for _, impl := range cls.Implements { | |
| 252 | 263 | interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types)) | |
| 253 | 264 | } | |
| @@ -258,6 +269,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 258 | 269 | Constructors: constructors, | |
| 259 | 270 | Receivers: receivers, | |
| 260 | 271 | Interfaces: interfaces, | |
| 272 | + | Functions: functions, | |
| 261 | 273 | Signals: signals, | |
| 262 | 274 | }) | |
| 263 | 275 | } | |
| @@ -275,22 +287,15 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 275 | 287 | //} | |
| 276 | 288 | methods := 0 | |
| 277 | 289 | for _, i := range interfaces[fn] { | |
| 278 | - | for range i.Methods { | |
| 279 | - | methods += 1 | |
| 280 | - | } | |
| 290 | + | methods += len(i.Methods) | |
| 281 | 291 | } | |
| 282 | 292 | for _, i := range records[fn] { | |
| 283 | - | for range i.Constructors { | |
| 284 | - | methods += 1 | |
| 285 | - | } | |
| 293 | + | methods += len(i.Constructors) | |
| 286 | 294 | } | |
| 287 | 295 | for _, i := range classes[fn] { | |
| 288 | - | for range i.Constructors { | |
| 289 | - | methods += 1 | |
| 290 | - | } | |
| 291 | - | for range i.Receivers { | |
| 292 | - | methods += 1 | |
| 293 | - | } | |
| 296 | + | methods += len(i.Constructors) | |
| 297 | + | methods += len(i.Receivers) | |
| 298 | + | methods += len(i.Functions) | |
| 294 | 299 | } | |
| 295 | 300 | // we do not need to add the length of interfaces in here | |
| 296 | 301 | // as they should only be loaded when there are classes | |
| @@ -212,6 +212,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 212 | for _, cls := range ns.Classes { | 212 | for _, cls := range ns.Classes { | ||
| 213 | implemented := make(map[string]bool) | 213 | implemented := make(map[string]bool) | ||
| 214 | constructors := make([]types.FuncTemplate, len(cls.Constructors)) | 214 | constructors := make([]types.FuncTemplate, len(cls.Constructors)) | ||
| 215 | + | functions := make([]types.FuncTemplate, len(cls.Functions)) | |||
| 215 | fn := cls.FilenameSafe() | 216 | fn := cls.FilenameSafe() | ||
| 216 | files = append(files, fn) | 217 | files = append(files, fn) | ||
| 217 | 218 | ||||
| @@ -248,6 +249,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 248 | } | 249 | } | ||
| 249 | } | 250 | } | ||
| 250 | var interfaces []types.InterfaceTemplate | 251 | var interfaces []types.InterfaceTemplate | ||
| 252 | + | for i, f := range cls.Functions { | |||
| 253 | + | name := fmt.Sprintf("%s%s", util.SnakeToCamel(cls.Name), util.SnakeToCamel(f.Name)) | |||
| 254 | + | functions[i] = types.FuncTemplate{ | |||
| 255 | + | Name: name, | |||
| 256 | + | CName: f.CIdentifier, | |||
| 257 | + | Doc: f.Doc.StringSafe(), | |||
| 258 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |||
| 259 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |||
| 260 | + | } | |||
| 261 | + | } | |||
| 251 | for _, impl := range cls.Implements { | 262 | for _, impl := range cls.Implements { | ||
| 252 | interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types)) | 263 | interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types)) | ||
| 253 | } | 264 | } | ||
| @@ -258,6 +269,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 258 | Constructors: constructors, | 269 | Constructors: constructors, | ||
| 259 | Receivers: receivers, | 270 | Receivers: receivers, | ||
| 260 | Interfaces: interfaces, | 271 | Interfaces: interfaces, | ||
| 272 | + | Functions: functions, | |||
| 261 | Signals: signals, | 273 | Signals: signals, | ||
| 262 | }) | 274 | }) | ||
| 263 | } | 275 | } | ||
| @@ -275,22 +287,15 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 275 | //} | 287 | //} | ||
| 276 | methods := 0 | 288 | methods := 0 | ||
| 277 | for _, i := range interfaces[fn] { | 289 | for _, i := range interfaces[fn] { | ||
| 278 | - | for range i.Methods { | 290 | + | methods += len(i.Methods) |
| 279 | - | methods += 1 | |||
| 280 | - | } | |||
| 281 | } | 291 | } | ||
| 282 | for _, i := range records[fn] { | 292 | for _, i := range records[fn] { | ||
| 283 | - | for range i.Constructors { | 293 | + | methods += len(i.Constructors) |
| 284 | - | methods += 1 | |||
| 285 | - | } | |||
| 286 | } | 294 | } | ||
| 287 | for _, i := range classes[fn] { | 295 | for _, i := range classes[fn] { | ||
| 288 | - | for range i.Constructors { | 296 | + | methods += len(i.Constructors) |
| 289 | - | methods += 1 | 297 | + | methods += len(i.Receivers) |
| 290 | - | } | 298 | + | methods += len(i.Functions) |
| 291 | - | for range i.Receivers { | |||
| 292 | - | methods += 1 | |||
| 293 | - | } | |||
| 294 | } | 299 | } | ||
| 295 | // we do not need to add the length of interfaces in here | 300 | // we do not need to add the length of interfaces in here | ||
| 296 | // as they should only be loaded when there are classes | 301 | // as they should only be loaded when there are classes | ||
M internal/gir/types/template.go +2 -0
| @@ -345,6 +345,8 @@ type ClassTemplate struct { | |||
| 345 | 345 | Receivers []FuncTemplate | |
| 346 | 346 | // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface | |
| 347 | 347 | Interfaces []InterfaceTemplate | |
| 348 | + | // Functions are the Go function declarations | |
| 349 | + | Functions []FuncTemplate | |
| 348 | 350 | // Signals are helpers for ConnectX receivers | |
| 349 | 351 | Signals []SignalsTemplate | |
| 350 | 352 | } | |
| @@ -345,6 +345,8 @@ type ClassTemplate struct { | |||||
| 345 | Receivers []FuncTemplate | 345 | Receivers []FuncTemplate | ||
| 346 | // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface | 346 | // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface | ||
| 347 | Interfaces []InterfaceTemplate | 347 | Interfaces []InterfaceTemplate | ||
| 348 | + | // Functions are the Go function declarations | |||
| 349 | + | Functions []FuncTemplate | |||
| 348 | // Signals are helpers for ConnectX receivers | 350 | // Signals are helpers for ConnectX receivers | ||
| 349 | Signals []SignalsTemplate | 351 | Signals []SignalsTemplate | ||
| 350 | } | 352 | } | ||
M templates/go +14 -0
| @@ -190,6 +190,17 @@ func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 190 | 190 | {{end}} | |
| 191 | 191 | {{end}} | |
| 192 | 192 | ||
| 193 | + | {{range .Functions -}} | |
| 194 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 195 | + | ||
| 196 | + | {{.Doc}} | |
| 197 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 198 | + | {{.Ret.Preamble $NotGLib}} | |
| 199 | + | {{if .Ret.Value}}cret := {{end}}x{{.Name}}({{conv .Args.API.Call}}) | |
| 200 | + | {{.Ret.Fmt $NotGObject}} | |
| 201 | + | } | |
| 202 | + | {{end}} | |
| 203 | + | ||
| 193 | 204 | {{end}} | |
| 194 | 205 | ||
| 195 | 206 | ||
| @@ -219,6 +230,9 @@ func init() { | |||
| 219 | 230 | {{range .Receivers -}} | |
| 220 | 231 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | |
| 221 | 232 | {{end}} | |
| 233 | + | {{range .Functions -}} | |
| 234 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |
| 235 | + | {{end}} | |
| 222 | 236 | {{end}} | |
| 223 | 237 | ||
| 224 | 238 | {{range .Interfaces -}} | |
| @@ -190,6 +190,17 @@ func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||||
| 190 | {{end}} | 190 | {{end}} | ||
| 191 | {{end}} | 191 | {{end}} | ||
| 192 | 192 | ||||
| 193 | + | {{range .Functions -}} | |||
| 194 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |||
| 195 | + | ||||
| 196 | + | {{.Doc}} | |||
| 197 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 198 | + | {{.Ret.Preamble $NotGLib}} | |||
| 199 | + | {{if .Ret.Value}}cret := {{end}}x{{.Name}}({{conv .Args.API.Call}}) | |||
| 200 | + | {{.Ret.Fmt $NotGObject}} | |||
| 201 | + | } | |||
| 202 | + | {{end}} | |||
| 203 | + | ||||
| 193 | {{end}} | 204 | {{end}} | ||
| 194 | 205 | ||||
| 195 | 206 | ||||
| @@ -219,6 +230,9 @@ func init() { | |||||
| 219 | {{range .Receivers -}} | 230 | {{range .Receivers -}} | ||
| 220 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | 231 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | ||
| 221 | {{end}} | 232 | {{end}} | ||
| 233 | + | {{range .Functions -}} | |||
| 234 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |||
| 235 | + | {{end}} | |||
| 222 | {{end}} | 236 | {{end}} | ||
| 223 | 237 | ||||
| 224 | {{range .Interfaces -}} | 238 | {{range .Interfaces -}} | ||