diff options
author | Matthew Dempsky <mdempsky@google.com> | 2018-04-09 13:57:56 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2018-04-09 22:58:21 +0000 |
commit | c3473c4f10c93189cf312576df2ce05202948e52 (patch) | |
tree | 6e3867211a9eefdb81b7fbc4022b7891122f4293 /src/cmd/compile/internal/types/sym.go | |
parent | 71bac7efe45b18894415a169b669cb1df95d8079 (diff) | |
download | go-c3473c4f10c93189cf312576df2ce05202948e52.tar.gz go-c3473c4f10c93189cf312576df2ce05202948e52.zip |
cmd/compile: refactor symbol sorting logic
This used to be duplicated in methcmp and siglt, because Sig used its
own representation for Syms. Instead, just use Syms, and add a
(*Sym).Less method that both methcmp and siglt can use.
Also, prune some impossible cases purportedly related to blank
methods: the Go spec disallows blank methods in interface method sets,
and addmethod drops blank methods without actually recording them in
the type's method set.
Passes toolstash-check.
Updates #24693.
Change-Id: I24e981659b68504d71518160486989a82505f513
Reviewed-on: https://go-review.googlesource.com/105936
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/types/sym.go')
-rw-r--r-- | src/cmd/compile/internal/types/sym.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types/sym.go b/src/cmd/compile/internal/types/sym.go index e9b454d83a..fe6ddbf5a2 100644 --- a/src/cmd/compile/internal/types/sym.go +++ b/src/cmd/compile/internal/types/sym.go @@ -77,6 +77,32 @@ func (sym *Sym) Linksym() *obj.LSym { return Ctxt.Lookup(sym.LinksymName()) } +// Less reports whether symbol a is ordered before symbol b. +// +// Symbols are ordered exported before non-exported, then by name, and +// finally (for non-exported symbols) by package path. +func (a *Sym) Less(b *Sym) bool { + if a == b { + return false + } + + // Exported symbols before non-exported. + ea := IsExported(a.Name) + eb := IsExported(b.Name) + if ea != eb { + return ea + } + + // Order by name and then (for non-exported names) by package. + if a.Name != b.Name { + return a.Name < b.Name + } + if !ea { + return a.Pkg.Path < b.Pkg.Path + } + return false +} + // IsExported reports whether name is an exported Go symbol (that is, // whether it begins with an upper-case letter). func IsExported(name string) bool { |