2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
|
|
|
|
import (
|
2016-03-11 13:39:20 -05:00
|
|
|
"bufio"
|
2015-08-13 19:05:37 -07:00
|
|
|
"bytes"
|
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
|
|
|
"cmd/compile/internal/types"
|
2016-04-06 21:45:29 -07:00
|
|
|
"cmd/internal/bio"
|
2015-02-13 14:40:36 -05:00
|
|
|
"fmt"
|
|
|
|
|
"unicode"
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
)
|
|
|
|
|
|
2015-08-13 19:05:37 -07:00
|
|
|
var (
|
2016-08-16 12:55:17 -07:00
|
|
|
Debug_export int // if set, print debugging information about export data
|
2015-08-13 19:05:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func exportf(format string, args ...interface{}) {
|
2017-03-13 14:30:44 +11:00
|
|
|
fmt.Fprintf(bout, format, args...)
|
2015-08-13 19:05:37 -07:00
|
|
|
if Debug_export != 0 {
|
|
|
|
|
fmt.Printf(format, args...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 19:04:50 -08:00
|
|
|
var asmlist []*Node
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Mark n's symbol as exported
|
|
|
|
|
func exportsym(n *Node) {
|
|
|
|
|
if n == nil || n.Sym == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-03-06 20:00:54 +02:00
|
|
|
if n.Sym.Export() || n.Sym.Package() {
|
|
|
|
|
if n.Sym.Package() {
|
2017-03-14 09:46:45 -07:00
|
|
|
Fatalf("export/package mismatch: %v", n.Sym)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-06 20:00:54 +02:00
|
|
|
n.Sym.SetExport(true)
|
2015-02-13 14:40:36 -05:00
|
|
|
if Debug['E'] != 0 {
|
2015-04-17 12:03:22 -04:00
|
|
|
fmt.Printf("export symbol %v\n", n.Sym)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-11-02 09:47:43 -07:00
|
|
|
|
2017-01-11 11:24:35 -08:00
|
|
|
// Ensure original types are on exportlist before type aliases.
|
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
|
|
|
if IsAlias(n.Sym) {
|
|
|
|
|
exportlist = append(exportlist, asNode(n.Sym.Def))
|
2016-11-02 09:47:43 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 14:29:50 -07:00
|
|
|
exportlist = append(exportlist, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exportname(s string) bool {
|
2015-08-13 19:05:37 -07:00
|
|
|
if r := s[0]; r < utf8.RuneSelf {
|
|
|
|
|
return 'A' <= r && r <= 'Z'
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
r, _ := utf8.DecodeRuneInString(s)
|
|
|
|
|
return unicode.IsUpper(r)
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
func initname(s string) bool {
|
|
|
|
|
return s == "init"
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// exportedsym reports whether a symbol will be visible
|
|
|
|
|
// to files that import our package.
|
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
|
|
|
func exportedsym(sym *types.Sym) bool {
|
2015-02-13 14:40:36 -05:00
|
|
|
// Builtins are visible everywhere.
|
|
|
|
|
if sym.Pkg == builtinpkg || sym.Origpkg == builtinpkg {
|
2015-02-17 22:13:49 -05:00
|
|
|
return true
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-17 22:13:49 -05:00
|
|
|
return sym.Pkg == localpkg && exportname(sym.Name)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-10-26 14:57:36 -07:00
|
|
|
func autoexport(n *Node, ctxt Class) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n == nil || n.Sym == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (ctxt != PEXTERN && ctxt != PFUNC) || dclcontext != PEXTERN {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-01-23 14:24:24 -08:00
|
|
|
if n.Type != nil && n.Type.IsKind(TFUNC) && n.Type.Recv() != nil { // method
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 12:58:16 -07:00
|
|
|
if exportname(n.Sym.Name) || initname(n.Sym.Name) {
|
2015-02-13 14:40:36 -05:00
|
|
|
exportsym(n)
|
|
|
|
|
}
|
2017-03-06 20:00:54 +02:00
|
|
|
if asmhdr != "" && n.Sym.Pkg == localpkg && !n.Sym.Asm() {
|
|
|
|
|
n.Sym.SetAsm(true)
|
2016-03-04 19:04:50 -08:00
|
|
|
asmlist = append(asmlist, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look for anything we need for the inline body
|
2016-03-08 10:26:20 -08:00
|
|
|
func reexportdeplist(ll Nodes) {
|
2016-03-08 15:10:26 -08:00
|
|
|
for _, n := range ll.Slice() {
|
|
|
|
|
reexportdep(n)
|
2016-02-27 14:31:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
func reexportdep(n *Node) {
|
2015-02-17 22:13:49 -05:00
|
|
|
if n == nil {
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-15 10:47:57 -07:00
|
|
|
//print("reexportdep %+hN\n", n);
|
2015-02-13 14:40:36 -05:00
|
|
|
switch n.Op {
|
|
|
|
|
case ONAME:
|
2017-04-25 18:14:12 -07:00
|
|
|
switch n.Class() {
|
2015-02-13 14:40:36 -05:00
|
|
|
// methods will be printed along with their type
|
|
|
|
|
// nodes for T.Method expressions
|
|
|
|
|
case PFUNC:
|
|
|
|
|
if n.Left != nil && n.Left.Op == OTYPE {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// nodes for method calls.
|
2016-09-11 14:43:37 -07:00
|
|
|
if n.Type == nil || n.IsMethod() {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
case PEXTERN:
|
2015-02-17 22:13:49 -05:00
|
|
|
if n.Sym != nil && !exportedsym(n.Sym) {
|
2015-02-13 14:40:36 -05:00
|
|
|
if Debug['E'] != 0 {
|
2015-04-17 12:03:22 -04:00
|
|
|
fmt.Printf("reexport name %v\n", n.Sym)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-08-12 14:29:50 -07:00
|
|
|
exportlist = append(exportlist, n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-15 10:47:57 -07:00
|
|
|
|
|
|
|
|
// Local variables in the bodies need their type.
|
|
|
|
|
case ODCL:
|
|
|
|
|
t := n.Left.Type
|
|
|
|
|
|
|
|
|
|
if t != types.Types[t.Etype] && t != types.Idealbool && t != types.Idealstring {
|
|
|
|
|
if t.IsPtr() {
|
|
|
|
|
t = t.Elem()
|
|
|
|
|
}
|
|
|
|
|
if t != nil && t.Sym != nil && t.Sym.Def != nil && !exportedsym(t.Sym) {
|
|
|
|
|
if Debug['E'] != 0 {
|
|
|
|
|
fmt.Printf("reexport type %v from declaration\n", t.Sym)
|
|
|
|
|
}
|
|
|
|
|
exportlist = append(exportlist, asNode(t.Sym.Def))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case OLITERAL:
|
|
|
|
|
t := n.Type
|
|
|
|
|
if t != types.Types[n.Type.Etype] && t != types.Idealbool && t != types.Idealstring {
|
|
|
|
|
if t.IsPtr() {
|
|
|
|
|
t = t.Elem()
|
|
|
|
|
}
|
|
|
|
|
if t != nil && t.Sym != nil && t.Sym.Def != nil && !exportedsym(t.Sym) {
|
|
|
|
|
if Debug['E'] != 0 {
|
|
|
|
|
fmt.Printf("reexport literal type %v\n", t.Sym)
|
|
|
|
|
}
|
|
|
|
|
exportlist = append(exportlist, asNode(t.Sym.Def))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
case OTYPE:
|
|
|
|
|
if n.Sym != nil && n.Sym.Def != nil && !exportedsym(n.Sym) {
|
|
|
|
|
if Debug['E'] != 0 {
|
|
|
|
|
fmt.Printf("reexport literal/type %v\n", n.Sym)
|
|
|
|
|
}
|
|
|
|
|
exportlist = append(exportlist, n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for operations that need a type when rendered, put the type on the export list.
|
|
|
|
|
case OCONV,
|
|
|
|
|
OCONVIFACE,
|
|
|
|
|
OCONVNOP,
|
|
|
|
|
ORUNESTR,
|
|
|
|
|
OARRAYBYTESTR,
|
|
|
|
|
OARRAYRUNESTR,
|
|
|
|
|
OSTRARRAYBYTE,
|
|
|
|
|
OSTRARRAYRUNE,
|
|
|
|
|
ODOTTYPE,
|
|
|
|
|
ODOTTYPE2,
|
|
|
|
|
OSTRUCTLIT,
|
|
|
|
|
OARRAYLIT,
|
|
|
|
|
OSLICELIT,
|
|
|
|
|
OPTRLIT,
|
|
|
|
|
OMAKEMAP,
|
|
|
|
|
OMAKESLICE,
|
|
|
|
|
OMAKECHAN:
|
|
|
|
|
t := n.Type
|
|
|
|
|
|
|
|
|
|
switch t.Etype {
|
|
|
|
|
case TARRAY, TCHAN, TPTR32, TPTR64, TSLICE:
|
|
|
|
|
if t.Sym == nil {
|
|
|
|
|
t = t.Elem()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if t != nil && t.Sym != nil && t.Sym.Def != nil && !exportedsym(t.Sym) {
|
|
|
|
|
if Debug['E'] != 0 {
|
|
|
|
|
fmt.Printf("reexport type for expression %v\n", t.Sym)
|
|
|
|
|
}
|
|
|
|
|
exportlist = append(exportlist, asNode(t.Sym.Def))
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reexportdep(n.Left)
|
|
|
|
|
reexportdep(n.Right)
|
|
|
|
|
reexportdeplist(n.List)
|
|
|
|
|
reexportdeplist(n.Rlist)
|
|
|
|
|
reexportdeplist(n.Ninit)
|
2016-03-04 13:16:48 -08:00
|
|
|
reexportdeplist(n.Nbody)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-14 20:53:44 +02:00
|
|
|
// methodbyname sorts types by symbol name.
|
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
|
|
|
type methodbyname []*types.Field
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-09-14 20:53:44 +02:00
|
|
|
func (x methodbyname) Len() int { return len(x) }
|
|
|
|
|
func (x methodbyname) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
|
|
|
func (x methodbyname) Less(i, j int) bool { return x[i].Sym.Name < x[j].Sym.Name }
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
func dumpexport() {
|
2015-04-19 23:56:30 -04:00
|
|
|
if buildid != "" {
|
2015-08-13 19:05:37 -07:00
|
|
|
exportf("build id %q\n", buildid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size := 0 // size of export section without enclosing markers
|
2016-08-16 12:55:17 -07:00
|
|
|
// The linker also looks for the $$ marker - use char after $$ to distinguish format.
|
|
|
|
|
exportf("\n$$B\n") // indicate binary export format
|
|
|
|
|
if debugFormat {
|
|
|
|
|
// save a copy of the export data
|
|
|
|
|
var copy bytes.Buffer
|
|
|
|
|
bcopy := bufio.NewWriter(©)
|
|
|
|
|
size = export(bcopy, Debug_export != 0)
|
|
|
|
|
bcopy.Flush() // flushing to bytes.Buffer cannot fail
|
|
|
|
|
if n, err := bout.Write(copy.Bytes()); n != size || err != nil {
|
|
|
|
|
Fatalf("error writing export data: got %d bytes, want %d bytes, err = %v", n, size, err)
|
2015-08-13 19:05:37 -07:00
|
|
|
}
|
2016-08-16 12:55:17 -07:00
|
|
|
// export data must contain no '$' so that we can find the end by searching for "$$"
|
|
|
|
|
// TODO(gri) is this still needed?
|
|
|
|
|
if bytes.IndexByte(copy.Bytes(), '$') >= 0 {
|
|
|
|
|
Fatalf("export data contains $")
|
2015-08-13 19:05:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-16 12:55:17 -07:00
|
|
|
// verify that we can read the copied export data back in
|
|
|
|
|
// (use empty package map to avoid collisions)
|
2017-04-19 11:32:09 -07:00
|
|
|
types.CleanroomDo(func() {
|
|
|
|
|
Import(types.NewPkg("", ""), bufio.NewReader(©)) // must not die
|
|
|
|
|
})
|
2016-08-16 12:55:17 -07:00
|
|
|
} else {
|
|
|
|
|
size = export(bout.Writer, Debug_export != 0)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-08-16 12:55:17 -07:00
|
|
|
exportf("\n$$\n")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-08-13 19:05:37 -07:00
|
|
|
if Debug_export != 0 {
|
|
|
|
|
fmt.Printf("export data size = %d bytes\n", size)
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-04 15:41:56 -07:00
|
|
|
// importsym declares symbol s as an imported object representable by op.
|
2017-03-23 17:39:28 -07:00
|
|
|
// pkg is the package being imported
|
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
|
|
|
func importsym(pkg *types.Pkg, s *types.Sym, op Op) {
|
|
|
|
|
if asNode(s.Def) != nil && asNode(s.Def).Op != op {
|
2017-03-23 17:39:28 -07:00
|
|
|
pkgstr := fmt.Sprintf("during import %q", pkg.Path)
|
2015-02-13 14:40:36 -05:00
|
|
|
redeclare(s, pkgstr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark the symbol so it is not reexported
|
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
|
|
|
if asNode(s.Def) == nil {
|
2016-10-19 12:58:16 -07:00
|
|
|
if exportname(s.Name) || initname(s.Name) {
|
2017-03-06 20:00:54 +02:00
|
|
|
s.SetExport(true)
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2017-03-06 20:00:54 +02:00
|
|
|
s.SetPackage(true) // package scope
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 15:41:56 -07:00
|
|
|
// pkgtype returns the named type declared by symbol s.
|
|
|
|
|
// If no such type has been declared yet, a forward declaration is returned.
|
2017-03-23 17:39:28 -07:00
|
|
|
// pkg is the package being imported
|
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
|
|
|
func pkgtype(pkg *types.Pkg, s *types.Sym) *types.Type {
|
2017-03-23 17:39:28 -07:00
|
|
|
importsym(pkg, s, OTYPE)
|
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
|
|
|
if asNode(s.Def) == nil || asNode(s.Def).Op != OTYPE {
|
|
|
|
|
t := types.New(TFORW)
|
2015-02-13 14:40:36 -05:00
|
|
|
t.Sym = s
|
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
|
|
|
s.Def = asTypesNode(typenod(t))
|
|
|
|
|
asNode(s.Def).Name = new(Name)
|
2015-02-13 14:40:36 -05: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
|
|
|
if asNode(s.Def).Type == nil {
|
2017-03-14 09:46:45 -07:00
|
|
|
Fatalf("pkgtype %v", s)
|
2015-02-13 14:40:36 -05: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 asNode(s.Def).Type
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-04 15:41:56 -07:00
|
|
|
// importconst declares symbol s as an imported constant with type t and value n.
|
2017-03-23 17:39:28 -07:00
|
|
|
// pkg is the package being imported
|
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
|
|
|
func importconst(pkg *types.Pkg, s *types.Sym, t *types.Type, n *Node) {
|
2017-03-23 17:39:28 -07:00
|
|
|
importsym(pkg, s, OLITERAL)
|
cmd/compile: reduce use of **Node parameters
Escape analysis has a hard time with tree-like
structures (see #13493 and #14858).
This is unlikely to change.
As a result, when invoking a function that accepts
a **Node parameter, we usually allocate a *Node
on the heap. This happens a whole lot.
This CL changes functions from taking a **Node
to acting more like append: It both modifies
the input and returns a replacement for it.
Because of the cascading nature of escape analysis,
in order to get the benefits, I had to modify
almost all such functions. The remaining functions
are in racewalk and the backend. I would be happy
to update them as well in a separate CL.
This CL was created by manually updating the
function signatures and the directly impacted
bits of code. The callsites were then automatically
updated using a bespoke script:
https://gist.github.com/josharian/046b1be7aceae244de39
For ease of reviewing and future understanding,
this CL is also broken down into four CLs,
mailed separately, which show the manual
and the automated changes separately.
They are CLs 20990, 20991, 20992, and 20993.
Passes toolstash -cmp.
name old time/op new time/op delta
Template 335ms ± 5% 324ms ± 5% -3.35% (p=0.000 n=23+24)
Unicode 176ms ± 9% 165ms ± 6% -6.12% (p=0.000 n=23+24)
GoTypes 1.10s ± 4% 1.07s ± 2% -2.77% (p=0.000 n=24+24)
Compiler 5.31s ± 3% 5.15s ± 3% -2.95% (p=0.000 n=24+24)
MakeBash 41.6s ± 1% 41.7s ± 2% ~ (p=0.586 n=23+23)
name old alloc/op new alloc/op delta
Template 63.3MB ± 0% 62.4MB ± 0% -1.36% (p=0.000 n=25+23)
Unicode 42.4MB ± 0% 41.6MB ± 0% -1.99% (p=0.000 n=24+25)
GoTypes 220MB ± 0% 217MB ± 0% -1.11% (p=0.000 n=25+25)
Compiler 994MB ± 0% 973MB ± 0% -2.08% (p=0.000 n=24+25)
name old allocs/op new allocs/op delta
Template 681k ± 0% 574k ± 0% -15.71% (p=0.000 n=24+25)
Unicode 518k ± 0% 413k ± 0% -20.34% (p=0.000 n=25+24)
GoTypes 2.08M ± 0% 1.78M ± 0% -14.62% (p=0.000 n=25+25)
Compiler 9.26M ± 0% 7.64M ± 0% -17.48% (p=0.000 n=25+25)
name old text-bytes new text-bytes delta
HelloSize 578k ± 0% 578k ± 0% ~ (all samples are equal)
CmdGoSize 6.46M ± 0% 6.46M ± 0% ~ (all samples are equal)
name old data-bytes new data-bytes delta
HelloSize 128k ± 0% 128k ± 0% ~ (all samples are equal)
CmdGoSize 281k ± 0% 281k ± 0% ~ (all samples are equal)
name old exe-bytes new exe-bytes delta
HelloSize 921k ± 0% 921k ± 0% ~ (all samples are equal)
CmdGoSize 9.86M ± 0% 9.86M ± 0% ~ (all samples are equal)
Change-Id: I277d95bd56d51c166ef7f560647aeaa092f3f475
Reviewed-on: https://go-review.googlesource.com/20959
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-03-20 08:03:31 -07:00
|
|
|
n = convlit(n, t)
|
2015-02-13 14:40:36 -05: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
|
|
|
if asNode(s.Def) != nil { // TODO: check if already the same.
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Op != OLITERAL {
|
2016-09-15 15:45:10 +10:00
|
|
|
yyerror("expression must be a constant")
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Sym != nil {
|
2016-03-23 16:01:15 +11:00
|
|
|
n1 := *n
|
|
|
|
|
n = &n1
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.Orig = newname(s)
|
|
|
|
|
n.Sym = s
|
|
|
|
|
declare(n, PEXTERN)
|
|
|
|
|
|
|
|
|
|
if Debug['E'] != 0 {
|
2015-04-17 12:03:22 -04:00
|
|
|
fmt.Printf("import const %v\n", s)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 15:41:56 -07:00
|
|
|
// importvar declares symbol s as an imported variable with type t.
|
2017-03-23 17:39:28 -07:00
|
|
|
// pkg is the package being imported
|
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
|
|
|
func importvar(pkg *types.Pkg, s *types.Sym, t *types.Type) {
|
2017-03-23 17:39:28 -07:00
|
|
|
importsym(pkg, s, ONAME)
|
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
|
|
|
if asNode(s.Def) != nil && asNode(s.Def).Op == ONAME {
|
|
|
|
|
if eqtype(t, asNode(s.Def).Type) {
|
2015-02-13 14:40:36 -05:00
|
|
|
return
|
|
|
|
|
}
|
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
|
|
|
yyerror("inconsistent definition for var %v during import\n\t%v (in %q)\n\t%v (in %q)", s, asNode(s.Def).Type, s.Importdef.Path, t, pkg.Path)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
n := newname(s)
|
2017-03-23 17:39:28 -07:00
|
|
|
s.Importdef = pkg
|
2015-02-13 14:40:36 -05:00
|
|
|
n.Type = t
|
|
|
|
|
declare(n, PEXTERN)
|
|
|
|
|
|
|
|
|
|
if Debug['E'] != 0 {
|
2016-09-09 21:08:46 -07:00
|
|
|
fmt.Printf("import var %v %L\n", s, t)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 11:24:35 -08:00
|
|
|
// importalias declares symbol s as an imported type alias with type t.
|
2017-03-23 17:39:28 -07:00
|
|
|
// pkg is the package being imported
|
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
|
|
|
func importalias(pkg *types.Pkg, s *types.Sym, t *types.Type) {
|
2017-03-23 17:39:28 -07:00
|
|
|
importsym(pkg, s, OTYPE)
|
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
|
|
|
if asNode(s.Def) != nil && asNode(s.Def).Op == OTYPE {
|
|
|
|
|
if eqtype(t, asNode(s.Def).Type) {
|
2017-01-11 11:24:35 -08:00
|
|
|
return
|
|
|
|
|
}
|
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
|
|
|
yyerror("inconsistent definition for type alias %v during import\n\t%v (in %q)\n\t%v (in %q)", s, asNode(s.Def).Type, s.Importdef.Path, t, pkg.Path)
|
2017-01-11 11:24:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n := newname(s)
|
|
|
|
|
n.Op = OTYPE
|
2017-03-23 17:39:28 -07:00
|
|
|
s.Importdef = pkg
|
2017-01-11 11:24:35 -08:00
|
|
|
n.Type = t
|
|
|
|
|
declare(n, PEXTERN)
|
|
|
|
|
|
|
|
|
|
if Debug['E'] != 0 {
|
|
|
|
|
fmt.Printf("import type %v = %L\n", s, t)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
func dumpasmhdr() {
|
2016-04-06 21:45:29 -07:00
|
|
|
b, err := bio.Create(asmhdr)
|
2015-02-13 14:40:36 -05:00
|
|
|
if err != nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("%v", err)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-04 19:04:50 -08:00
|
|
|
fmt.Fprintf(b, "// generated by compile -asmhdr from package %s\n\n", localpkg.Name)
|
|
|
|
|
for _, n := range asmlist {
|
2017-04-21 07:51:41 -07:00
|
|
|
if n.Sym.IsBlank() {
|
2015-02-13 14:40:36 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
switch n.Op {
|
|
|
|
|
case OLITERAL:
|
2016-08-30 15:01:48 -07:00
|
|
|
fmt.Fprintf(b, "#define const_%s %#v\n", n.Sym.Name, n.Val())
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case OTYPE:
|
2016-03-04 19:04:50 -08:00
|
|
|
t := n.Type
|
2016-04-01 20:11:30 -07:00
|
|
|
if !t.IsStruct() || t.StructType().Map != nil || t.IsFuncArgStruct() {
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(b, "#define %s__size %d\n", t.Sym.Name, int(t.Width))
|
2016-03-17 01:32:18 -07:00
|
|
|
for _, t := range t.Fields().Slice() {
|
2017-04-21 07:51:41 -07:00
|
|
|
if !t.Sym.IsBlank() {
|
2016-03-28 09:40:53 -07:00
|
|
|
fmt.Fprintf(b, "#define %s_%s %d\n", n.Sym.Name, t.Sym.Name, int(t.Offset))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 21:45:29 -07:00
|
|
|
b.Close()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|