Skip to content

Commit

Permalink
generate the step enum
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Jan 28, 2025
1 parent 0f2d61f commit eb1fbad
Show file tree
Hide file tree
Showing 16 changed files with 728 additions and 50 deletions.
88 changes: 70 additions & 18 deletions go/tools/asthelpergen/ast_path_gen.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package asthelpergen

import (
"github.com/dave/jennifer/jen"
"go/types"

"github.com/dave/jennifer/jen"
)

type (
Expand All @@ -11,8 +12,10 @@ type (
steps []step
}
step struct {
name string
typ types.Type
container string // the type of the container
name string // the name of the field
slice bool // whether the field is a slice
typ types.Type // the type of the field
}
)

Expand All @@ -29,48 +32,97 @@ func newPathGen(pkgname string) *pathGen {
}

func (p *pathGen) genFile() (string, *jen.File) {
p.close()
return "ast_path.go", p.file
}

func (p *pathGen) interfaceMethod(t types.Type, iface *types.Interface, spi generatorSPI) error {
// don't think we need to do anything here
return nil
}

func (p *pathGen) structMethod(t types.Type, strct *types.Struct, spi generatorSPI) error {
typeString := types.TypeString(t, noQualifier)
p.addStructFields(t, strct, spi)
return nil
}

func (p *pathGen) ptrToStructMethod(t types.Type, strct *types.Struct, spi generatorSPI) error {
p.addStructFields(t, strct, spi)
return nil
}

func (p *pathGen) addStructFields(t types.Type, strct *types.Struct, spi generatorSPI) {
typeString := printableTypeName(t)
for i := range strct.NumFields() {
field := strct.Field(i)
if types.Implements(field.Type(), spi.iface()) {

p.steps = append(p.steps, step{
container: typeString,
name: field.Name(),
typ: field.Type(),
})
continue
}
slice, isSlice := field.Type().(*types.Slice)
if isSlice && types.Implements(slice.Elem(), spi.iface()) {
spi.addType(slice.Elem())
output = append(output, jen.For(jen.Id("_, el := range in."+field.Name())).Block(
visitChild(slice.Elem(), jen.Id("el")),
))
p.steps = append(p.steps, step{
container: typeString,
slice: true,
name: field.Name(),
typ: slice.Elem(),
})
}
}

return nil
}

func (p *pathGen) ptrToStructMethod(t types.Type, strct *types.Struct, spi generatorSPI) error {
return nil
}

func (p *pathGen) ptrToBasicMethod(t types.Type, basic *types.Basic, spi generatorSPI) error {
return nil
}

func (p *pathGen) sliceMethod(t types.Type, slice *types.Slice, spi generatorSPI) error {
func (p *pathGen) sliceMethod(t types.Type, _ *types.Slice, _ generatorSPI) error {
p.steps = append(p.steps, step{
container: printableTypeName(t),
name: "Offset",
slice: true,
typ: t,
})

return nil
}

func (p *pathGen) basicMethod(t types.Type, basic *types.Basic, spi generatorSPI) error {
return nil
}

func (*pathGen) close(generatorSPI) error { return nil }
func (p *pathGen) close() {
// Declare the ASTStep type with underlying type uint16
astStepType := jen.Type().Id("ASTStep").Uint16()

// Initialize a slice to hold the constant definitions
var constDefs []jen.Code
addStep := func(step string) {
if constDefs == nil {
// Use iota for the first constant
constDefs = append(constDefs, jen.Id(step).Id("ASTStep").Op("=").Id("iota"))
return
}

constDefs = append(constDefs, jen.Id(step))
}
for _, step := range p.steps {
stepName := step.container + step.name
if step.slice {
addStep(stepName + "8")
addStep(stepName + "64")
continue
}

addStep(stepName)
}

// Create the const block with all step constants
constBlock := jen.Const().Defs(constDefs...)

// Add the type declaration and const block as separate statements to the file
p.file.Add(astStepType)
p.file.Add(constBlock)
}
1 change: 0 additions & 1 deletion go/tools/asthelpergen/asthelpergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type (
ptrToBasicMethod(t types.Type, basic *types.Basic, spi generatorSPI) error
sliceMethod(t types.Type, slice *types.Slice, spi generatorSPI) error
basicMethod(t types.Type, basic *types.Basic, spi generatorSPI) error
close(spi generatorSPI) error
}
// astHelperGen finds implementations of the given interface,
// and uses the supplied `generator`s to produce the output code
Expand Down
2 changes: 0 additions & 2 deletions go/tools/asthelpergen/clone_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ func (c *cloneGen) basicMethod(t types.Type, basic *types.Basic, spi generatorSP
return nil
}

func (*cloneGen) close(generatorSPI) error { return nil }

func (c *cloneGen) copySliceElement(t types.Type, elType types.Type, spi generatorSPI) jen.Code {
if !isNamed(t) && isBasic(elType) {
// copy(res, n)
Expand Down
2 changes: 0 additions & 2 deletions go/tools/asthelpergen/copy_on_rewrite_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ func (c *cowGen) basicMethod(t types.Type, basic *types.Basic, spi generatorSPI)
return nil
}

func (*cowGen) close(generatorSPI) error { return nil }

func ifNotNil(id string, stmts ...jen.Code) *jen.Statement {
return jen.If(jen.Id(id).Op("!=").Nil()).Block(stmts...)
}
Expand Down
2 changes: 0 additions & 2 deletions go/tools/asthelpergen/equals_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,3 @@ func (e *equalsGen) sliceMethod(t types.Type, slice *types.Slice, spi generatorS
}

func (*equalsGen) basicMethod(types.Type, *types.Basic, generatorSPI) error { return nil }

func (*equalsGen) close(generatorSPI) error { return nil }
2 changes: 0 additions & 2 deletions go/tools/asthelpergen/rewrite_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ func (r *rewriteGen) basicMethod(t types.Type, _ *types.Basic, spi generatorSPI)
return nil
}

func (*rewriteGen) close(generatorSPI) error { return nil }

func (r *rewriteGen) rewriteFunc(t types.Type, stmts []jen.Code) {

/*
Expand Down
2 changes: 0 additions & 2 deletions go/tools/asthelpergen/visit_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ func (v *visitGen) basicMethod(t types.Type, basic *types.Basic, spi generatorSP
return v.visitNoChildren(t, spi)
}

func (*visitGen) close(generatorSPI) error { return nil }

func (v *visitGen) visitNoChildren(t types.Type, spi generatorSPI) error {
stmts := []jen.Code{
jen.Id("_, err := f(in)"),
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_copy_on_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eb1fbad

Please sign in to comment.