Skip to content

Commit

Permalink
Merge pull request #307 from xushiwei/refactor
Browse files Browse the repository at this point in the history
cb.TypeDecls; NewType with src ast.Node (not pos token.Pos)
  • Loading branch information
xushiwei authored Nov 2, 2023
2 parents 674962e + 4e9f8a9 commit 7ebd369
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 34 deletions.
10 changes: 9 additions & 1 deletion builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ func getConf() *Config {
return &Config{Fset: fset, Importer: imp}
}

func TestNewPosNode(t *testing.T) {
if ret := NewPosNode(1); ret.Pos() != 1 || ret.End() != 1 {
t.Fatal("NewPosNode(1): end -", ret.End())
}
if ret := NewPosNode(1, 2); ret.End() != 2 {
t.Fatal("NewPosNode(1, 2): end -", ret.End())
}
}

func TestGetSrcPos(t *testing.T) {
if getSrcPos(nil) != token.NoPos {
t.Fatal("TestGetSrcPos: not nopos?")
Expand Down Expand Up @@ -453,7 +462,6 @@ func TestToFields(t *testing.T) {
}

func TestToVariadic(t *testing.T) {
getPos([]token.Pos{1})
defer func() {
if e := recover(); e == nil {
t.Fatal("TestToVariadic: no error?")
Expand Down
38 changes: 28 additions & 10 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ func getSrcPos(node ast.Node) token.Pos {
return token.NoPos
}

func getPos(src []ast.Node) token.Pos {
if src == nil {
return token.NoPos
}
return getSrcPos(src[0])
}

// ----------------------------------------------------------------------------

type posNode struct {
pos, end token.Pos
}

func NewPosNode(pos token.Pos, end ...token.Pos) ast.Node {
ret := &posNode{pos: pos, end: pos}
if end != nil {
ret.end = end[0]
}
return ret
}

func (p *posNode) Pos() token.Pos { return p.pos }
func (p *posNode) End() token.Pos { return p.end }

// ----------------------------------------------------------------------------

type codeBlock interface {
Expand Down Expand Up @@ -576,19 +600,13 @@ func (p *CodeBuilder) NewClosureWith(sig *types.Signature) *Func {
}

// NewType func
func (p *CodeBuilder) NewType(name string, pos ...token.Pos) *TypeDecl {
if debugInstr {
log.Println("NewType", name)
}
return p.typeDefs().NewType(name, pos...)
func (p *CodeBuilder) NewType(name string, src ...ast.Node) *TypeDecl {
return p.NewTypeDefs().NewType(name, src...)
}

// AliasType func
func (p *CodeBuilder) AliasType(name string, typ types.Type, pos ...token.Pos) *types.Named {
if debugInstr {
log.Println("AliasType", name, typ)
}
decl := p.typeDefs().AliasType(name, typ, pos...)
func (p *CodeBuilder) AliasType(name string, typ types.Type, src ...ast.Node) *types.Named {
decl := p.NewTypeDefs().AliasType(name, typ, src...)
return decl.typ
}

Expand Down
4 changes: 2 additions & 2 deletions error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ func TestErrSwitch(t *testing.T) {

func TestErrTypeRedefined(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:5: foo redeclared in this block\n\tprevious declaration at ./foo.gop:1:5", func(pkg *gox.Package) {
typ := pkg.NewType("foo", position(1, 5))
typ := pkg.NewType("foo", source("foo", 1, 5))
if typ.Inited() {
t.Fatal("NewType failed: inited?")
}
pkg.NewType("foo", position(2, 5))
pkg.NewType("foo", source("foo", 2, 5))
})
}

Expand Down
18 changes: 18 additions & 0 deletions internal/go/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,25 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
}
}

func checkSpecs(d *ast.GenDecl) {
if d.Tok == token.TYPE { // for gox.TypeDecl.Delete
for i, spec := range d.Specs {
if spec.(*ast.TypeSpec).Name == nil {
leftSpecs := d.Specs[i+1:]
d.Specs = d.Specs[:i]
for _, spec := range leftSpecs {
if spec.(*ast.TypeSpec).Name != nil {
d.Specs = append(d.Specs, spec)
}
}
return
}
}
}
}

func (p *printer) genDecl(d *ast.GenDecl) {
checkSpecs(d)
n := len(d.Specs)
if n == 0 { // no values, ignore
return
Expand Down
44 changes: 23 additions & 21 deletions type_var_and_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ func (p *TypeDefs) SetComments(doc *ast.CommentGroup) *TypeDefs {
}

// NewType creates a new type (which need to call InitType later).
func (p *TypeDefs) NewType(name string, pos ...token.Pos) *TypeDecl {
func (p *TypeDefs) NewType(name string, src ...ast.Node) *TypeDecl {
if debugInstr {
log.Println("NewType", name)
}
return p.pkg.doNewType(p, getPos(pos), name, nil, 0)
return p.pkg.doNewType(p, getPos(src), name, nil, 0)
}

// AliasType gives a specified type with a new name.
func (p *TypeDefs) AliasType(name string, typ types.Type, pos ...token.Pos) *TypeDecl {
func (p *TypeDefs) AliasType(name string, typ types.Type, src ...ast.Node) *TypeDecl {
if debugInstr {
log.Println("AliasType", name, typ)
}
return p.pkg.doNewType(p, getPos(pos), name, typ, 1)
return p.pkg.doNewType(p, getPos(src), name, typ, 1)
}

// Complete checks type declarations & marks completed.
Expand Down Expand Up @@ -176,16 +176,16 @@ func (p *TypeDefs) Complete() {
// AliasType gives a specified type with a new name.
//
// Deprecated: use NewTypeDefs instead.
func (p *Package) AliasType(name string, typ types.Type, pos ...token.Pos) *types.Named {
decl := p.NewTypeDefs().AliasType(name, typ, pos...)
func (p *Package) AliasType(name string, typ types.Type, src ...ast.Node) *types.Named {
decl := p.NewTypeDefs().AliasType(name, typ, src...)
return decl.typ
}

// NewType creates a new type (which need to call InitType later).
//
// Deprecated: use NewTypeDefs instead.
func (p *Package) NewType(name string, pos ...token.Pos) *TypeDecl {
return p.NewTypeDefs().NewType(name, pos...)
func (p *Package) NewType(name string, src ...ast.Node) *TypeDecl {
return p.NewTypeDefs().NewType(name, src...)
}

// NewTypeDefs starts a type declaration block.
Expand All @@ -195,22 +195,24 @@ func (p *Package) NewTypeDefs() *TypeDefs {
return &TypeDefs{decl: decl, scope: p.Types.Scope(), pkg: p}
}

func (p *CodeBuilder) typeDefs() *TypeDefs {
pkg, scope := p.pkg, p.current.scope
decl := &ast.GenDecl{Tok: token.TYPE}
if scope == pkg.Types.Scope() {
pkg.file.decls = append(pkg.file.decls, decl)
} else {
p.emitStmt(&ast.DeclStmt{Decl: decl})
}
return &TypeDefs{decl: decl, scope: scope, pkg: pkg}
// NewTypeDefs starts a type declaration block.
func (p *CodeBuilder) NewTypeDefs() *TypeDefs {
ret, defineHere := p.NewTypeDecls()
defineHere()
return ret
}

func getPos(pos []token.Pos) token.Pos {
if pos == nil {
return 0
// NewTypeDecls starts a type declaration block but delay to define it.
func (p *CodeBuilder) NewTypeDecls() (ret *TypeDefs, defineHere func()) {
pkg, scope := p.pkg, p.current.scope
decl := &ast.GenDecl{Tok: token.TYPE}
return &TypeDefs{decl: decl, scope: scope, pkg: pkg}, func() {
if scope == pkg.Types.Scope() {
pkg.file.decls = append(pkg.file.decls, decl)
} else {
p.emitStmt(&ast.DeclStmt{Decl: decl})
}
}
return pos[0]
}

func (p *Package) doNewType(tdecl *TypeDefs, pos token.Pos, name string, typ types.Type, alias token.Pos) *TypeDecl {
Expand Down

0 comments on commit 7ebd369

Please sign in to comment.