2016-10-18 16:11:50 -07:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
2015-02-27 17:40:34 +09:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
|
2016-10-18 14:17:05 -07:00
|
|
|
// Generate builtin.go from builtin/runtime.go.
|
2016-10-18 16:11:50 -07:00
|
|
|
|
2015-02-27 17:40:34 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2015-12-01 11:52:32 -08:00
|
|
|
"bytes"
|
2016-02-18 16:44:06 -08:00
|
|
|
"flag"
|
2015-02-27 17:40:34 +09:00
|
|
|
"fmt"
|
2016-10-18 16:11:50 -07:00
|
|
|
"go/ast"
|
|
|
|
|
"go/format"
|
|
|
|
|
"go/parser"
|
|
|
|
|
"go/token"
|
2015-02-27 17:40:34 +09:00
|
|
|
"io"
|
2015-12-01 11:52:32 -08:00
|
|
|
"io/ioutil"
|
2015-02-27 17:40:34 +09:00
|
|
|
"log"
|
|
|
|
|
"os"
|
2016-10-18 16:11:50 -07:00
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2015-02-27 17:40:34 +09:00
|
|
|
)
|
|
|
|
|
|
2016-02-18 16:44:06 -08:00
|
|
|
var stdout = flag.Bool("stdout", false, "write to stdout instead of builtin.go")
|
|
|
|
|
|
2015-02-27 17:40:34 +09:00
|
|
|
func main() {
|
2016-02-18 16:44:06 -08:00
|
|
|
flag.Parse()
|
|
|
|
|
|
2015-12-01 11:52:32 -08:00
|
|
|
var b bytes.Buffer
|
2017-03-31 10:26:30 -07:00
|
|
|
fmt.Fprintln(&b, "// Code generated by mkbuiltin.go. DO NOT EDIT.")
|
2016-10-18 16:11:50 -07:00
|
|
|
fmt.Fprintln(&b)
|
2015-12-01 11:52:32 -08:00
|
|
|
fmt.Fprintln(&b, "package gc")
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
fmt.Fprintln(&b)
|
|
|
|
|
fmt.Fprintln(&b, `import "cmd/compile/internal/types"`)
|
2015-02-27 17:40:34 +09:00
|
|
|
|
2015-12-01 11:52:32 -08:00
|
|
|
mkbuiltin(&b, "runtime")
|
2015-02-27 17:40:34 +09:00
|
|
|
|
2016-10-18 16:11:50 -07:00
|
|
|
out, err := format.Source(b.Bytes())
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2016-02-18 16:44:06 -08:00
|
|
|
if *stdout {
|
2016-10-18 16:11:50 -07:00
|
|
|
_, err = os.Stdout.Write(out)
|
2016-02-18 16:44:06 -08:00
|
|
|
} else {
|
2016-10-18 16:11:50 -07:00
|
|
|
err = ioutil.WriteFile("builtin.go", out, 0666)
|
2016-02-18 16:44:06 -08:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2015-02-27 17:40:34 +09:00
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-21 13:28:17 -04:00
|
|
|
func mkbuiltin(w io.Writer, name string) {
|
2016-10-18 16:11:50 -07:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
|
f, err := parser.ParseFile(fset, filepath.Join("builtin", name+".go"), nil, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
2015-12-01 11:52:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 16:11:50 -07:00
|
|
|
var interner typeInterner
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "var %sDecls = [...]struct { name string; tag int; typ int }{\n", name)
|
|
|
|
|
for _, decl := range f.Decls {
|
|
|
|
|
switch decl := decl.(type) {
|
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
|
if decl.Recv != nil {
|
|
|
|
|
log.Fatal("methods unsupported")
|
|
|
|
|
}
|
|
|
|
|
if decl.Body != nil {
|
|
|
|
|
log.Fatal("unexpected function body")
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(w, "{%q, funcTag, %d},\n", decl.Name.Name, interner.intern(decl.Type))
|
|
|
|
|
case *ast.GenDecl:
|
2017-02-19 23:40:24 -05:00
|
|
|
if decl.Tok == token.IMPORT {
|
|
|
|
|
if len(decl.Specs) != 1 || decl.Specs[0].(*ast.ImportSpec).Path.Value != "\"unsafe\"" {
|
|
|
|
|
log.Fatal("runtime cannot import other package")
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-10-18 16:11:50 -07:00
|
|
|
if decl.Tok != token.VAR {
|
|
|
|
|
log.Fatal("unhandled declaration kind", decl.Tok)
|
|
|
|
|
}
|
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
|
spec := spec.(*ast.ValueSpec)
|
|
|
|
|
if len(spec.Values) != 0 {
|
|
|
|
|
log.Fatal("unexpected values")
|
|
|
|
|
}
|
|
|
|
|
typ := interner.intern(spec.Type)
|
|
|
|
|
for _, name := range spec.Names {
|
|
|
|
|
fmt.Fprintf(w, "{%q, varTag, %d},\n", name.Name, typ)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
log.Fatal("unhandled decl type", decl)
|
|
|
|
|
}
|
2015-02-27 17:40:34 +09:00
|
|
|
}
|
2016-10-18 16:11:50 -07:00
|
|
|
fmt.Fprintln(w, "}")
|
2015-02-27 17:40:34 +09:00
|
|
|
|
2016-10-18 16:11:50 -07:00
|
|
|
fmt.Fprintln(w)
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
fmt.Fprintf(w, "func %sTypes() []*types.Type {\n", name)
|
|
|
|
|
fmt.Fprintf(w, "var typs [%d]*types.Type\n", len(interner.typs))
|
2016-10-18 16:11:50 -07:00
|
|
|
for i, typ := range interner.typs {
|
|
|
|
|
fmt.Fprintf(w, "typs[%d] = %s\n", i, typ)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintln(w, "return typs[:]")
|
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// typeInterner maps Go type expressions to compiler code that
|
|
|
|
|
// constructs the denoted type. It recognizes and reuses common
|
|
|
|
|
// subtype expressions.
|
|
|
|
|
type typeInterner struct {
|
|
|
|
|
typs []string
|
|
|
|
|
hash map[string]int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *typeInterner) intern(t ast.Expr) int {
|
|
|
|
|
x := i.mktype(t)
|
|
|
|
|
v, ok := i.hash[x]
|
|
|
|
|
if !ok {
|
|
|
|
|
v = len(i.typs)
|
|
|
|
|
if i.hash == nil {
|
|
|
|
|
i.hash = make(map[string]int)
|
|
|
|
|
}
|
|
|
|
|
i.hash[x] = v
|
|
|
|
|
i.typs = append(i.typs, x)
|
2015-02-27 17:40:34 +09:00
|
|
|
}
|
2016-10-18 16:11:50 -07:00
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *typeInterner) subtype(t ast.Expr) string {
|
|
|
|
|
return fmt.Sprintf("typs[%d]", i.intern(t))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *typeInterner) mktype(t ast.Expr) string {
|
|
|
|
|
switch t := t.(type) {
|
|
|
|
|
case *ast.Ident:
|
|
|
|
|
switch t.Name {
|
|
|
|
|
case "byte":
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return "types.Bytetype"
|
2016-10-18 16:11:50 -07:00
|
|
|
case "rune":
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return "types.Runetype"
|
2016-10-18 16:11:50 -07:00
|
|
|
}
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return fmt.Sprintf("types.Types[T%s]", strings.ToUpper(t.Name))
|
2017-02-19 23:40:24 -05:00
|
|
|
case *ast.SelectorExpr:
|
|
|
|
|
if t.X.(*ast.Ident).Name != "unsafe" || t.Sel.Name != "Pointer" {
|
|
|
|
|
log.Fatalf("unhandled type: %#v", t)
|
|
|
|
|
}
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return "types.Types[TUNSAFEPTR]"
|
2016-10-18 16:11:50 -07:00
|
|
|
|
|
|
|
|
case *ast.ArrayType:
|
|
|
|
|
if t.Len == nil {
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return fmt.Sprintf("types.NewSlice(%s)", i.subtype(t.Elt))
|
2016-10-18 16:11:50 -07:00
|
|
|
}
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return fmt.Sprintf("types.NewArray(%s, %d)", i.subtype(t.Elt), intconst(t.Len))
|
2016-10-18 16:11:50 -07:00
|
|
|
case *ast.ChanType:
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
dir := "types.Cboth"
|
2016-10-18 16:11:50 -07:00
|
|
|
switch t.Dir {
|
|
|
|
|
case ast.SEND:
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
dir = "types.Csend"
|
2016-10-18 16:11:50 -07:00
|
|
|
case ast.RECV:
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
dir = "types.Crecv"
|
2016-10-18 16:11:50 -07:00
|
|
|
}
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return fmt.Sprintf("types.NewChan(%s, %s)", i.subtype(t.Value), dir)
|
2016-10-18 16:11:50 -07:00
|
|
|
case *ast.FuncType:
|
|
|
|
|
return fmt.Sprintf("functype(nil, %s, %s)", i.fields(t.Params, false), i.fields(t.Results, false))
|
|
|
|
|
case *ast.InterfaceType:
|
|
|
|
|
if len(t.Methods.List) != 0 {
|
|
|
|
|
log.Fatal("non-empty interfaces unsupported")
|
|
|
|
|
}
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return "types.Types[TINTER]"
|
2016-10-18 16:11:50 -07:00
|
|
|
case *ast.MapType:
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return fmt.Sprintf("types.NewMap(%s, %s)", i.subtype(t.Key), i.subtype(t.Value))
|
2016-10-18 16:11:50 -07:00
|
|
|
case *ast.StarExpr:
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
return fmt.Sprintf("types.NewPtr(%s)", i.subtype(t.X))
|
2016-10-18 16:11:50 -07:00
|
|
|
case *ast.StructType:
|
|
|
|
|
return fmt.Sprintf("tostruct(%s)", i.fields(t.Fields, true))
|
2015-02-27 17:40:34 +09:00
|
|
|
|
2016-10-18 16:11:50 -07:00
|
|
|
default:
|
|
|
|
|
log.Fatalf("unhandled type: %#v", t)
|
|
|
|
|
panic("unreachable")
|
2015-02-27 17:40:34 +09:00
|
|
|
}
|
2016-10-18 16:11:50 -07:00
|
|
|
}
|
2015-02-27 17:40:34 +09:00
|
|
|
|
2016-10-18 16:11:50 -07:00
|
|
|
func (i *typeInterner) fields(fl *ast.FieldList, keepNames bool) string {
|
|
|
|
|
if fl == nil || len(fl.List) == 0 {
|
|
|
|
|
return "nil"
|
|
|
|
|
}
|
|
|
|
|
var res []string
|
|
|
|
|
for _, f := range fl.List {
|
|
|
|
|
typ := i.subtype(f.Type)
|
|
|
|
|
if len(f.Names) == 0 {
|
|
|
|
|
res = append(res, fmt.Sprintf("anonfield(%s)", typ))
|
|
|
|
|
} else {
|
|
|
|
|
for _, name := range f.Names {
|
|
|
|
|
if keepNames {
|
|
|
|
|
res = append(res, fmt.Sprintf("namedfield(%q, %s)", name.Name, typ))
|
|
|
|
|
} else {
|
|
|
|
|
res = append(res, fmt.Sprintf("anonfield(%s)", typ))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-01 11:52:32 -08:00
|
|
|
}
|
2016-10-18 16:11:50 -07:00
|
|
|
return fmt.Sprintf("[]*Node{%s}", strings.Join(res, ", "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func intconst(e ast.Expr) int64 {
|
|
|
|
|
switch e := e.(type) {
|
|
|
|
|
case *ast.BasicLit:
|
|
|
|
|
if e.Kind != token.INT {
|
|
|
|
|
log.Fatalf("expected INT, got %v", e.Kind)
|
|
|
|
|
}
|
|
|
|
|
x, err := strconv.ParseInt(e.Value, 0, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
2015-10-22 18:56:45 -07:00
|
|
|
}
|
2016-10-18 16:11:50 -07:00
|
|
|
return x
|
|
|
|
|
default:
|
|
|
|
|
log.Fatalf("unhandled expr: %#v", e)
|
|
|
|
|
panic("unreachable")
|
2015-02-27 17:40:34 +09:00
|
|
|
}
|
|
|
|
|
}
|