Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CastFromBool: support untyped bool #168

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,13 @@ func matchRcast(pkg *Package, fn *internal.Elem, m types.Object, typ types.Type,
// CastFromBool tries to cast a bool expression into integer. typ must be an integer type.
func CastFromBool(cb *CodeBuilder, typ types.Type, v *Element) (ret *Element, ok bool) {
if ok = isBool(cb, v); ok {
if v.CVal != nil { // untyped bool
var val int
if constant.BoolVal(v.CVal) {
val = 1
}
return toExpr(nil, val, v.Src), true
}
pkg := cb.pkg
results := types.NewTuple(types.NewParam(token.NoPos, pkg.Types, "", typ))
ret = cb.NewClosure(nil, results, false).BodyStart(pkg).
Expand Down
17 changes: 17 additions & 0 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,4 +950,21 @@ func TestTypeAST(t *testing.T) {
}
}

func TestCastFromBool(t *testing.T) {
ret, ok := CastFromBool(nil, types.Typ[types.Uint], &Element{
Type: types.Typ[types.UntypedBool],
CVal: constant.MakeBool(true),
})
if !ok || constant.Val(ret.CVal).(int64) != 1 {
t.Fatal("CastFromBool failed:", ret.CVal, ok)
}
ret, ok = CastFromBool(nil, types.Typ[types.Uint], &Element{
Type: types.Typ[types.Bool],
CVal: constant.MakeBool(false),
})
if !ok || constant.Val(ret.CVal).(int64) != 0 {
t.Fatal("CastFromBool failed:", ret.CVal, ok)
}
}

// ----------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,17 @@ func TestDeleteType(t *testing.T) {
pkg := newMainPackage()
typ := types.NewStruct(nil, nil)
decl := pkg.NewType("foo")
if decl.State() != gox.TyStateUninited {
t.Fatal("TypeDecl.State failed")
}
decl.InitType(pkg, typ)
if decl.State() != gox.TyStateInited {
t.Fatal("TypeDecl.State failed")
}
decl.Delete()
if decl.State() != gox.TyStateDeleted {
t.Fatal("TypeDecl.State failed")
}
domTest(t, pkg, `package main


Expand Down
25 changes: 24 additions & 1 deletion type_var_and_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (p *CodeBuilder) EndConst() *Element {

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

type TyState int

const (
TyStateUninited TyState = iota
TyStateInited
TyStateDeleted
)

// TypeDecl type
type TypeDecl struct {
typ *types.Named
Expand All @@ -56,13 +64,28 @@ func (p *TypeDecl) Type() *types.Named {
return p.typ
}

// State checkes state of this type.
// If Delete is called, it returns TyStateDeleted.
// If InitType is called (but not deleted), it returns TyStateInited.
// Otherwise it returns TyStateUninited.
func (p *TypeDecl) State() TyState {
if spec := p.decl.Specs; len(spec) > 0 {
if spec[0].(*ast.TypeSpec).Type != nil {
return TyStateInited
}
return TyStateUninited
}
return TyStateDeleted
}

// Delete deletes this type.
// NOTE: It panics if you call InitType after Delete.
func (p *TypeDecl) Delete() {
p.decl.Specs = p.decl.Specs[:0]
}

// Inited checkes if `InitType` is called or not.
// Inited checkes if InitType is called or not.
// Will panic if this type is deleted (please use State to check).
func (p *TypeDecl) Inited() bool {
return p.decl.Specs[0].(*ast.TypeSpec).Type != nil
}
Expand Down