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>
This commit is contained in:
Robert Griesemer 2017-04-04 17:54:02 -07:00
parent 19bd145d07
commit f68f292820
48 changed files with 2433 additions and 2005 deletions

View file

@ -34,6 +34,8 @@ func main() {
fmt.Fprintln(&b, "// Code generated by mkbuiltin.go. DO NOT EDIT.")
fmt.Fprintln(&b)
fmt.Fprintln(&b, "package gc")
fmt.Fprintln(&b)
fmt.Fprintln(&b, `import "cmd/compile/internal/types"`)
mkbuiltin(&b, "runtime")
@ -98,8 +100,8 @@ func mkbuiltin(w io.Writer, name string) {
fmt.Fprintln(w, "}")
fmt.Fprintln(w)
fmt.Fprintf(w, "func %sTypes() []*Type {\n", name)
fmt.Fprintf(w, "var typs [%d]*Type\n", len(interner.typs))
fmt.Fprintf(w, "func %sTypes() []*types.Type {\n", name)
fmt.Fprintf(w, "var typs [%d]*types.Type\n", len(interner.typs))
for i, typ := range interner.typs {
fmt.Fprintf(w, "typs[%d] = %s\n", i, typ)
}
@ -138,42 +140,42 @@ func (i *typeInterner) mktype(t ast.Expr) string {
case *ast.Ident:
switch t.Name {
case "byte":
return "bytetype"
return "types.Bytetype"
case "rune":
return "runetype"
return "types.Runetype"
}
return fmt.Sprintf("Types[T%s]", strings.ToUpper(t.Name))
return fmt.Sprintf("types.Types[T%s]", strings.ToUpper(t.Name))
case *ast.SelectorExpr:
if t.X.(*ast.Ident).Name != "unsafe" || t.Sel.Name != "Pointer" {
log.Fatalf("unhandled type: %#v", t)
}
return "Types[TUNSAFEPTR]"
return "types.Types[TUNSAFEPTR]"
case *ast.ArrayType:
if t.Len == nil {
return fmt.Sprintf("typSlice(%s)", i.subtype(t.Elt))
return fmt.Sprintf("types.NewSlice(%s)", i.subtype(t.Elt))
}
return fmt.Sprintf("typArray(%s, %d)", i.subtype(t.Elt), intconst(t.Len))
return fmt.Sprintf("types.NewArray(%s, %d)", i.subtype(t.Elt), intconst(t.Len))
case *ast.ChanType:
dir := "Cboth"
dir := "types.Cboth"
switch t.Dir {
case ast.SEND:
dir = "Csend"
dir = "types.Csend"
case ast.RECV:
dir = "Crecv"
dir = "types.Crecv"
}
return fmt.Sprintf("typChan(%s, %s)", i.subtype(t.Value), dir)
return fmt.Sprintf("types.NewChan(%s, %s)", i.subtype(t.Value), dir)
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")
}
return "Types[TINTER]"
return "types.Types[TINTER]"
case *ast.MapType:
return fmt.Sprintf("typMap(%s, %s)", i.subtype(t.Key), i.subtype(t.Value))
return fmt.Sprintf("types.NewMap(%s, %s)", i.subtype(t.Key), i.subtype(t.Value))
case *ast.StarExpr:
return fmt.Sprintf("typPtr(%s)", i.subtype(t.X))
return fmt.Sprintf("types.NewPtr(%s)", i.subtype(t.X))
case *ast.StructType:
return fmt.Sprintf("tostruct(%s)", i.fields(t.Fields, true))