Skip to content

Commit

Permalink
Merge pull request #276 from xushiwei/q
Browse files Browse the repository at this point in the history
pkg.NewTypeDefs
  • Loading branch information
xushiwei authored Oct 7, 2023
2 parents 655e2d1 + 48a7c32 commit e13142e
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 100 deletions.
16 changes: 10 additions & 6 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,15 +590,15 @@ func (p *CodeBuilder) NewType(name string, pos ...token.Pos) *TypeDecl {
if debugInstr {
log.Println("NewType", name)
}
return p.pkg.doNewType(p.current.scope, getPos(pos), name, nil, 0)
return p.typeDefs().NewType(name, pos...)
}

// 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.pkg.doNewType(p.current.scope, getPos(pos), name, typ, 1)
decl := p.typeDefs().AliasType(name, typ, pos...)
return decl.typ
}

Expand All @@ -607,15 +607,17 @@ func (p *CodeBuilder) NewConstStart(typ types.Type, names ...string) *CodeBuilde
if debugInstr {
log.Println("NewConstStart", names)
}
return p.pkg.newValueDecl(nil, p.current.scope, token.NoPos, token.CONST, typ, names...).InitStart(p.pkg)
defs := p.valueDefs(token.CONST)
return p.pkg.newValueDecl(defs.NewPos(), defs.scope, token.NoPos, token.CONST, typ, names...).InitStart(p.pkg)
}

// NewVar func
func (p *CodeBuilder) NewVar(typ types.Type, names ...string) *CodeBuilder {
if debugInstr {
log.Println("NewVar", names)
}
p.pkg.newValueDecl(nil, p.current.scope, token.NoPos, token.VAR, typ, names...)
defs := p.valueDefs(token.VAR)
p.pkg.newValueDecl(defs.NewPos(), defs.scope, token.NoPos, token.VAR, typ, names...)
return p
}

Expand All @@ -624,15 +626,17 @@ func (p *CodeBuilder) NewVarStart(typ types.Type, names ...string) *CodeBuilder
if debugInstr {
log.Println("NewVarStart", names)
}
return p.pkg.newValueDecl(nil, p.current.scope, token.NoPos, token.VAR, typ, names...).InitStart(p.pkg)
defs := p.valueDefs(token.VAR)
return p.pkg.newValueDecl(defs.NewPos(), defs.scope, token.NoPos, token.VAR, typ, names...).InitStart(p.pkg)
}

// DefineVarStart func
func (p *CodeBuilder) DefineVarStart(pos token.Pos, names ...string) *CodeBuilder {
if debugInstr {
log.Println("DefineVarStart", names)
}
return p.pkg.newValueDecl(nil, p.current.scope, pos, token.DEFINE, nil, names...).InitStart(p.pkg)
return p.pkg.newValueDecl(
ValueAt{}, p.current.scope, pos, token.DEFINE, nil, names...).InitStart(p.pkg)
}

// NewAutoVar func
Expand Down
6 changes: 3 additions & 3 deletions error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestErrConst(t *testing.T) {
codeErrorTest(t, "./foo.gop:2:9: a redeclared in this block\n\tprevious declaration at ./foo.gop:1:5",
func(pkg *gox.Package) {
pkg.NewVarStart(position(1, 5), nil, "a").Val(1).EndInit(1)
pkg.NewConstDecl(pkg.Types.Scope()).
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gox.CodeBuilder) int {
cb.Val(2)
return 1
Expand All @@ -306,7 +306,7 @@ func TestErrConst(t *testing.T) {
})
codeErrorTest(t, "./foo.gop:2:9: extra expression in const declaration",
func(pkg *gox.Package) {
pkg.NewConstDecl(pkg.Types.Scope()).
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gox.CodeBuilder) int {
cb.Val(2)
cb.Val(ctxRef(pkg, "iota"))
Expand All @@ -316,7 +316,7 @@ func TestErrConst(t *testing.T) {
})
codeErrorTest(t, "./foo.gop:2:9: missing value in const declaration",
func(pkg *gox.Package) {
pkg.NewConstDecl(pkg.Types.Scope()).
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gox.CodeBuilder) int {
cb.Val(2)
cb.Val(ctxRef(pkg, "iota"))
Expand Down
24 changes: 16 additions & 8 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ func main() {
func TestTypeDoc(t *testing.T) {
pkg := newMainPackage()
typ := types.NewStruct(nil, nil)
pkg.NewType("foo").SetComments(comment("\n//go:notinheap")).InitType(pkg, typ)
def := pkg.NewTypeDefs().SetComments(nil)
def.NewType("foo").SetComments(comment("\n//go:notinheap")).InitType(pkg, typ)
def.Complete()
domTest(t, pkg, `package main
//go:notinheap
Expand All @@ -613,7 +615,8 @@ type foo struct {
func TestDeleteType(t *testing.T) {
pkg := newMainPackage()
typ := types.NewStruct(nil, nil)
decl := pkg.NewType("foo")
def := pkg.NewTypeDefs()
decl := def.NewType("foo")
if decl.State() != gox.TyStateUninited {
t.Fatal("TypeDecl.State failed")
}
Expand All @@ -625,7 +628,12 @@ func TestDeleteType(t *testing.T) {
if decl.State() != gox.TyStateDeleted {
t.Fatal("TypeDecl.State failed")
}
def.NewType("t").InitType(def.Pkg(), gox.TyByte)
def.NewType("bar").Delete()
def.Complete()
domTest(t, pkg, `package main
type t byte
`)
}

Expand Down Expand Up @@ -666,11 +674,11 @@ func TestTypeCycleDef(t *testing.T) {
foo.InitType(pkg, types.NewStruct(fields, nil))
domTest(t, pkg, `package main
type a = foo
type b = a
type foo struct {
p *b
}
type a = foo
type b = a
`)
}

Expand Down Expand Up @@ -1209,7 +1217,7 @@ func TestConstLenCap(t *testing.T) {
func TestConstDecl(t *testing.T) {
pkg := newMainPackage()
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg)
pkg.NewConstDefs(pkg.CB().Scope()).New(func(cb *gox.CodeBuilder) int {
pkg.NewConstDefs(pkg.CB().Scope()).SetComments(nil).New(func(cb *gox.CodeBuilder) int {
cb.Val(1).Val(2).BinaryOp(token.ADD)
return 1
}, 0, token.NoPos, nil, "n")
Expand All @@ -1230,7 +1238,7 @@ func main() {

func TestConstDecl2(t *testing.T) {
pkg := newMainPackage()
pkg.NewConstDecl(pkg.Types.Scope()).
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gox.CodeBuilder) int {
cb.Val(ctxRef(pkg, "iota"))
return 1
Expand Down Expand Up @@ -1264,7 +1272,7 @@ const (

func TestConstDecl3(t *testing.T) {
pkg := newMainPackage()
pkg.NewConstDecl(pkg.Types.Scope()).
pkg.NewConstDefs(pkg.Types.Scope()).
New(func(cb *gox.CodeBuilder) int {
cb.Val(1).Val(ctxRef(pkg, "iota")).BinaryOp(token.SHL)
return 1
Expand All @@ -1291,7 +1299,7 @@ func TestDeleteVarDecl(t *testing.T) {
pkg := newMainPackage()
pkg.SetRedeclarable(true)
scope := pkg.CB().Scope()
defs := pkg.NewVarDefs(scope)
defs := pkg.NewVarDefs(scope).SetComments(nil)
decl := defs.New(token.NoPos, types.Typ[types.Int], "a", "b")
defs.New(token.NoPos, types.Typ[types.String], "c")
defs.New(token.NoPos, types.Typ[types.String], "s")
Expand Down
Loading

0 comments on commit e13142e

Please sign in to comment.