Skip to content

Commit

Permalink
go/types: add a compiler param to SizesFor
Browse files Browse the repository at this point in the history
The current StdSizes most closely matches
the gc compiler, and the uses I know of that care
which compiler the sizes are for are all for
the gc compiler, so call the existing
implementation "gc".

Updates #17586
Fixes #19351

Change-Id: I2bdd694518fbe233473896321a1f9758b46ed79b
Reviewed-on: https://go-review.googlesource.com/37666
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
josharian committed Mar 2, 2017
1 parent 542a60f commit 32a1736
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/go/internal/srcimporter/srcimporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Pa
return &Importer{
ctxt: ctxt,
fset: fset,
sizes: types.SizesFor(ctxt.GOARCH), // uses go/types default if GOARCH not found
sizes: types.SizesFor(ctxt.Compiler, ctxt.GOARCH), // uses go/types default if GOARCH not found
packages: packages,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type Config struct {
Importer Importer

// If Sizes != nil, it provides the sizing functions for package unsafe.
// Otherwise SizesFor("amd64") is used instead.
// Otherwise SizesFor("gc", "amd64") is used instead.
Sizes Sizes

// If DisableUnusedImportCheck is set, packages are not checked
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/gotype.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func checkPkgFiles(files []*ast.File) {
report(err)
},
Importer: importer.For(*compiler, nil),
Sizes: types.SizesFor(build.Default.GOARCH),
Sizes: types.SizesFor(build.Default.Compiler, build.Default.GOARCH),
}

defer func() {
Expand Down
17 changes: 11 additions & 6 deletions src/go/types/sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *StdSizes) Sizeof(T Type) int64 {
}

// common architecture word sizes and alignments
var archSizes = map[string]*StdSizes{
var gcArchSizes = map[string]*StdSizes{
"386": {4, 4},
"arm": {4, 4},
"arm64": {8, 8},
Expand All @@ -171,16 +171,21 @@ var archSizes = map[string]*StdSizes{
// update the doc string of SizesFor below.
}

// SizesFor returns the Sizes for one of these architectures:
// SizesFor returns the Sizes used by a compiler for an architecture.
// The result is nil if a compiler/architecture pair is not known.
//
// Supported architectures for compiler "gc":
// "386", "arm", "arm64", "amd64", "amd64p32", "mips", "mipsle",
// "mips64", "mips64le", "ppc64", "ppc64le", "s390x".
// The result is nil if an architecture is not known.
func SizesFor(arch string) Sizes {
return archSizes[arch]
func SizesFor(compiler, arch string) Sizes {
if compiler != "gc" {
return nil
}
return gcArchSizes[arch]
}

// stdSizes is used if Config.Sizes == nil.
var stdSizes = SizesFor("amd64")
var stdSizes = SizesFor("gc", "amd64")

func (conf *Config) alignof(T Type) int64 {
if s := conf.Sizes; s != nil {
Expand Down

0 comments on commit 32a1736

Please sign in to comment.