Skip to content

Commit

Permalink
Generating copy-on-rewrite logic (#12135)
Browse files Browse the repository at this point in the history
* add code for generating copy-on-rewrite logic

Signed-off-by: Andres Taylor <[email protected]>

* added more tests and started calling cloned

Signed-off-by: Andres Taylor <[email protected]>

* reformat

Signed-off-by: Andres Taylor <[email protected]>

* change uses of Clone & Rewrite to CopyOnRewrite

Signed-off-by: Andres Taylor <[email protected]>

* regenerate test code

Signed-off-by: Andres Taylor <[email protected]>

Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay authored Jan 24, 2023
1 parent 784d79c commit ca8c233
Show file tree
Hide file tree
Showing 28 changed files with 8,056 additions and 168 deletions.
3 changes: 2 additions & 1 deletion go/tools/asthelpergen/asthelpergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"golang.org/x/tools/go/packages"
)

const licenseFileHeader = `Copyright 2021 The Vitess Authors.
const licenseFileHeader = `Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -244,6 +244,7 @@ func GenerateASTHelpers(options *Options) (map[string]*jen.File, error) {
newCloneGen(pName, &options.Clone),
newVisitGen(pName),
newRewriterGen(pName, types.TypeString(nt, noQualifier)),
newCOWGen(pName, nt),
)

it, err := generator.GenerateCode()
Expand Down
20 changes: 10 additions & 10 deletions go/tools/asthelpergen/clone_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *cloneGen) sliceMethod(t types.Type, slice *types.Slice, spi generatorSP
funcName := cloneName + name

c.addFunc(funcName,
//func (n Bytes) Clone() Bytes {
// func (n Bytes) Clone() Bytes {
jen.Func().Id(funcName).Call(jen.Id("n").Id(typeString)).Id(typeString).Block(
// if n == nil { return nil }
ifNilReturnNil("n"),
Expand All @@ -116,9 +116,9 @@ func (c *cloneGen) copySliceElement(t types.Type, elType types.Type, spi generat
return jen.Id("copy").Call(jen.Id("res"), jen.Id("n"))
}

//for i := range n {
// for i := range n {
// res[i] = CloneAST(x)
//}
// }
spi.addType(elType)

return jen.For(jen.List(jen.Id("i"), jen.Id("x"))).Op(":=").Range().Id("n").Block(
Expand All @@ -128,17 +128,17 @@ func (c *cloneGen) copySliceElement(t types.Type, elType types.Type, spi generat

func (c *cloneGen) interfaceMethod(t types.Type, iface *types.Interface, spi generatorSPI) error {

//func CloneAST(in AST) AST {
// func CloneAST(in AST) AST {
// if in == nil {
// return nil
//}
// }
// switch in := in.(type) {
//case *RefContainer:
// case *RefContainer:
// return in.CloneRefOfRefContainer()
//}
// }
// // this should never happen
// return nil
//}
// }

typeString := types.TypeString(t, noQualifier)
typeName := printableTypeName(t)
Expand Down Expand Up @@ -196,7 +196,7 @@ func (c *cloneGen) ptrToBasicMethod(t types.Type, _ *types.Basic, spi generatorS
func (c *cloneGen) ptrToOtherMethod(t types.Type, ptr *types.Pointer, spi generatorSPI) error {
receiveType := types.TypeString(t, noQualifier)

funcName := "Clone" + printableTypeName(t)
funcName := cloneName + printableTypeName(t)
c.addFunc(funcName,
jen.Func().Id(funcName).Call(jen.Id("n").Id(receiveType)).Id(receiveType).Block(
ifNilReturnNil("n"),
Expand Down Expand Up @@ -224,7 +224,7 @@ func (c *cloneGen) ptrToStructMethod(t types.Type, strct *types.Struct, spi gene
receiveType := types.TypeString(t, noQualifier)
funcName := cloneName + printableTypeName(t)

//func CloneRefOfType(n *Type) *Type
// func CloneRefOfType(n *Type) *Type
funcDeclaration := jen.Func().Id(funcName).Call(jen.Id("n").Id(receiveType)).Id(receiveType)

if slices.Contains(c.exclude, receiveType) {
Expand Down
Loading

0 comments on commit ca8c233

Please sign in to comment.