Skip to content

Use go/types in Go 1.5 standard library. #278

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

Merged
merged 3 commits into from
Jan 7, 2016
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
9 changes: 4 additions & 5 deletions compiler/analysis/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package analysis

import (
"go/ast"
"go/constant"
"go/token"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
"go/types"
)

func BoolValue(expr ast.Expr, info *types.Info) (bool, bool) {
v := info.Types[expr].Value
if v != nil && v.Kind() == exact.Bool {
return exact.BoolVal(v), true
if v != nil && v.Kind() == constant.Bool {
return constant.BoolVal(v), true
}
switch e := expr.(type) {
case *ast.BinaryExpr:
Expand Down
3 changes: 1 addition & 2 deletions compiler/analysis/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package analysis
import (
"go/ast"
"go/token"

"golang.org/x/tools/go/types"
"go/types"
)

func EscapingObjects(n ast.Node, info *types.Info) map[*types.Var]bool {
Expand Down
3 changes: 1 addition & 2 deletions compiler/analysis/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package analysis
import (
"go/ast"
"go/token"
"go/types"

"github.com/gopherjs/gopherjs/compiler/astutil"
"github.com/gopherjs/gopherjs/compiler/typesutil"

"golang.org/x/tools/go/types"
)

type continueStmt struct {
Expand Down
3 changes: 1 addition & 2 deletions compiler/analysis/sideeffect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package analysis
import (
"go/ast"
"go/token"

"golang.org/x/tools/go/types"
"go/types"
)

func HasSideEffect(n ast.Node, info *types.Info) bool {
Expand Down
3 changes: 1 addition & 2 deletions compiler/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package astutil

import (
"go/ast"

"golang.org/x/tools/go/types"
"go/types"
)

func RemoveParens(e ast.Expr) ast.Expr {
Expand Down
4 changes: 2 additions & 2 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"encoding/json"
"fmt"
"go/token"
"go/types"
"io"
"strings"

"github.com/gopherjs/gopherjs/compiler/prelude"
"golang.org/x/tools/go/importer"
"golang.org/x/tools/go/types"
"github.com/gopherjs/gopherjs/third_party/importer"
)

var sizes32 = &types.StdSizes{WordSize: 4, MaxAlign: 8}
Expand Down
35 changes: 17 additions & 18 deletions compiler/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"bytes"
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"sort"
"strconv"
"strings"

"github.com/gopherjs/gopherjs/compiler/analysis"
"github.com/gopherjs/gopherjs/compiler/astutil"
"github.com/gopherjs/gopherjs/compiler/typesutil"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)

type expression struct {
Expand All @@ -39,39 +38,39 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
basic := exprType.Underlying().(*types.Basic)
switch {
case isBoolean(basic):
return c.formatExpr("%s", strconv.FormatBool(exact.BoolVal(value)))
return c.formatExpr("%s", strconv.FormatBool(constant.BoolVal(value)))
case isInteger(basic):
if is64Bit(basic) {
if basic.Kind() == types.Int64 {
d, ok := exact.Int64Val(value)
d, ok := constant.Int64Val(value)
if !ok {
panic("could not get exact uint")
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatInt(d>>32, 10), strconv.FormatUint(uint64(d)&(1<<32-1), 10))
}
d, ok := exact.Uint64Val(value)
d, ok := constant.Uint64Val(value)
if !ok {
panic("could not get exact uint")
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatUint(d>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
d, ok := exact.Int64Val(value)
d, ok := constant.Int64Val(value)
if !ok {
panic("could not get exact int")
}
return c.formatExpr("%s", strconv.FormatInt(d, 10))
case isFloat(basic):
f, _ := exact.Float64Val(value)
f, _ := constant.Float64Val(value)
return c.formatExpr("%s", strconv.FormatFloat(f, 'g', -1, 64))
case isComplex(basic):
r, _ := exact.Float64Val(exact.Real(value))
i, _ := exact.Float64Val(exact.Imag(value))
r, _ := constant.Float64Val(constant.Real(value))
i, _ := constant.Float64Val(constant.Imag(value))
if basic.Kind() == types.UntypedComplex {
exprType = types.Typ[types.Complex128]
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatFloat(r, 'g', -1, 64), strconv.FormatFloat(i, 'g', -1, 64))
case isString(basic):
return c.formatExpr("%s", encodeString(exact.StringVal(value)))
return c.formatExpr("%s", encodeString(constant.StringVal(value)))
default:
panic("Unhandled constant type: " + basic.String())
}
Expand Down Expand Up @@ -111,7 +110,7 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
zero := c.translateExpr(c.zeroValue(elementType)).String()
for _, element := range e.Elts {
if kve, isKve := element.(*ast.KeyValueExpr); isKve {
key, ok := exact.Int64Val(c.p.Types[kve.Key].Value)
key, ok := constant.Int64Val(c.p.Types[kve.Key].Value)
if !ok {
panic("could not get exact int")
}
Expand Down Expand Up @@ -920,7 +919,7 @@ func (c *funcContext) identifierConstant(expr ast.Expr) (string, bool) {
if val == nil {
return "", false
}
s := exact.StringVal(val)
s := constant.StringVal(val)
if len(s) == 0 {
return "", false
}
Expand Down Expand Up @@ -1303,7 +1302,7 @@ func (c *funcContext) formatExprInternal(format string, a []interface{}, parens
case 'f':
e := a[n].(ast.Expr)
if val := c.p.Types[e].Value; val != nil {
d, _ := exact.Int64Val(val)
d, _ := constant.Int64Val(val)
out.WriteString(strconv.FormatInt(d, 10))
return
}
Expand All @@ -1317,7 +1316,7 @@ func (c *funcContext) formatExprInternal(format string, a []interface{}, parens
case 'h':
e := a[n].(ast.Expr)
if val := c.p.Types[e].Value; val != nil {
d, _ := exact.Uint64Val(val)
d, _ := constant.Uint64Val(val)
if c.p.TypeOf(e).Underlying().(*types.Basic).Kind() == types.Int64 {
out.WriteString(strconv.FormatInt(int64(d)>>32, 10))
return
Expand All @@ -1328,21 +1327,21 @@ func (c *funcContext) formatExprInternal(format string, a []interface{}, parens
writeExpr(".$high")
case 'l':
if val := c.p.Types[a[n].(ast.Expr)].Value; val != nil {
d, _ := exact.Uint64Val(val)
d, _ := constant.Uint64Val(val)
out.WriteString(strconv.FormatUint(d&(1<<32-1), 10))
return
}
writeExpr(".$low")
case 'r':
if val := c.p.Types[a[n].(ast.Expr)].Value; val != nil {
r, _ := exact.Float64Val(exact.Real(val))
r, _ := constant.Float64Val(constant.Real(val))
out.WriteString(strconv.FormatFloat(r, 'g', -1, 64))
return
}
writeExpr(".$real")
case 'i':
if val := c.p.Types[a[n].(ast.Expr)].Value; val != nil {
i, _ := exact.Float64Val(exact.Imag(val))
i, _ := constant.Float64Val(constant.Imag(val))
out.WriteString(strconv.FormatFloat(i, 'g', -1, 64))
return
}
Expand Down
7 changes: 3 additions & 4 deletions compiler/filter/incdecstmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package filter

import (
"go/ast"
"go/constant"
"go/token"
"go/types"

"github.com/gopherjs/gopherjs/compiler/analysis"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)

func IncDecStmt(stmt ast.Stmt, info *analysis.Info) ast.Stmt {
Expand All @@ -30,7 +29,7 @@ func IncDecStmt(stmt ast.Stmt, info *analysis.Info) ast.Stmt {
}

one := &ast.BasicLit{Kind: token.INT}
info.Types[one] = types.TypeAndValue{Type: t, Value: exact.MakeInt64(1)}
info.Types[one] = types.TypeAndValue{Type: t, Value: constant.MakeInt64(1)}

return &ast.AssignStmt{
Lhs: []ast.Expr{s.X},
Expand Down
33 changes: 22 additions & 11 deletions compiler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"fmt"
"go/ast"
"go/token"
"go/types"
"sort"
"strings"

"github.com/gopherjs/gopherjs/compiler/analysis"
"golang.org/x/tools/go/importer"
"golang.org/x/tools/go/types"
"github.com/gopherjs/gopherjs/third_party/importer"
"golang.org/x/tools/go/types/typeutil"
)

Expand Down Expand Up @@ -66,6 +66,23 @@ func NewImportContext(importFunc func(string) (*Archive, error)) *ImportContext
}
}

// packageImporter implements go/types.Importer interface.
type packageImporter struct {
importContext *ImportContext
importError *error // A pointer to importError in Compile.
}

func (pi packageImporter) Import(path string) (*types.Package, error) {
if _, err := pi.importContext.Import(path); err != nil {
if *pi.importError == nil {
// If import failed, show first error of import only (https://github.com/gopherjs/gopherjs/issues/119).
*pi.importError = err
}
return nil, err
}
return pi.importContext.Packages[path], nil
}

func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, importContext *ImportContext, minify bool) (*Archive, error) {
typesInfo := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Expand All @@ -80,15 +97,9 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
var errList ErrorList
var previousErr error
config := &types.Config{
Packages: importContext.Packages,
Import: func(_ map[string]*types.Package, path string) (*types.Package, error) {
if _, err := importContext.Import(path); err != nil {
if importError == nil {
importError = err
}
return nil, err
}
return importContext.Packages[path], nil
Importer: packageImporter{
importContext: importContext,
importError: &importError,
},
Sizes: sizes32,
Error: func(err error) {
Expand Down
9 changes: 4 additions & 5 deletions compiler/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package compiler
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"strings"

"github.com/gopherjs/gopherjs/compiler/analysis"
"github.com/gopherjs/gopherjs/compiler/astutil"
"github.com/gopherjs/gopherjs/compiler/filter"
"github.com/gopherjs/gopherjs/compiler/typesutil"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)

type this struct {
Expand Down Expand Up @@ -71,7 +70,7 @@ func (c *funcContext) translateStmt(stmt ast.Stmt, label *types.Label) {
tag := s.Tag
if tag == nil {
tag = ast.NewIdent("true")
c.p.Types[tag] = types.TypeAndValue{Type: types.Typ[types.Bool], Value: exact.MakeBool(true)}
c.p.Types[tag] = types.TypeAndValue{Type: types.Typ[types.Bool], Value: constant.MakeBool(true)}
}

if c.p.Types[tag].Value == nil {
Expand Down Expand Up @@ -455,7 +454,7 @@ func (c *funcContext) translateStmt(stmt ast.Stmt, label *types.Label) {
panic(fmt.Sprintf("unhandled: %T", comm))
}
indexLit := &ast.BasicLit{Kind: token.INT}
c.p.Types[indexLit] = types.TypeAndValue{Type: types.Typ[types.Int], Value: exact.MakeInt64(int64(i))}
c.p.Types[indexLit] = types.TypeAndValue{Type: types.Typ[types.Int], Value: constant.MakeInt64(int64(i))}
caseClauses = append(caseClauses, &ast.CaseClause{
List: []ast.Expr{indexLit},
Body: clause.Body,
Expand Down
2 changes: 1 addition & 1 deletion compiler/typesutil/typesutil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package typesutil

import "golang.org/x/tools/go/types"
import "go/types"

func IsJsPackage(pkg *types.Package) bool {
return pkg != nil && pkg.Path() == "github.com/gopherjs/gopherjs/js"
Expand Down
13 changes: 6 additions & 7 deletions compiler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (
"encoding/binary"
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"net/url"
"sort"
"strconv"
"strings"

"github.com/gopherjs/gopherjs/compiler/analysis"
"github.com/gopherjs/gopherjs/compiler/typesutil"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)

func (c *funcContext) Write(b []byte) (int, error) {
Expand Down Expand Up @@ -173,11 +172,11 @@ func (c *funcContext) zeroValue(ty types.Type) ast.Expr {
case *types.Basic:
switch {
case isBoolean(t):
return c.newConst(ty, exact.MakeBool(false))
return c.newConst(ty, constant.MakeBool(false))
case isNumeric(t):
return c.newConst(ty, exact.MakeInt64(0))
return c.newConst(ty, constant.MakeInt64(0))
case isString(t):
return c.newConst(ty, exact.MakeString(""))
return c.newConst(ty, constant.MakeString(""))
case t.Kind() == types.UnsafePointer:
// fall through to "nil"
case t.Kind() == types.UntypedNil:
Expand All @@ -197,7 +196,7 @@ func (c *funcContext) zeroValue(ty types.Type) ast.Expr {
return id
}

func (c *funcContext) newConst(t types.Type, value exact.Value) ast.Expr {
func (c *funcContext) newConst(t types.Type, value constant.Value) ast.Expr {
id := &ast.Ident{}
c.p.Types[id] = types.TypeAndValue{Type: t, Value: value}
return id
Expand Down
Loading