-
-
Notifications
You must be signed in to change notification settings - Fork 1k
compiler: include the scope in the type code name #5183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 9 commits
ff58c50
8bd2233
1fe934e
1876b65
707d37a
a0069b6
5d8e071
7b86f95
332e2b9
d91670c
e56e3e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "go/types" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "golang.org/x/tools/go/ssa" | ||
| "tinygo.org/x/go-llvm" | ||
|
|
@@ -511,14 +512,57 @@ var basicTypeNames = [...]string{ | |
| types.UnsafePointer: "unsafe.Pointer", | ||
| } | ||
|
|
||
| var scopeIDCache = struct { | ||
| sync.Mutex | ||
| scopeid map[*types.Scope]string | ||
| }{ | ||
| sync.Mutex{}, | ||
| make(map[*types.Scope]string), | ||
| } | ||
|
|
||
| // return an integer representing this scope in a package. | ||
| func scopeID(pkg *types.Scope, scope *types.Scope) string { | ||
| scopeIDCache.Lock() | ||
| defer scopeIDCache.Unlock() | ||
|
|
||
| if id := scopeIDCache.scopeid[scope]; id != "" { | ||
| return id | ||
| } | ||
|
|
||
| entry := scope | ||
|
|
||
| var ids []int | ||
| for scope != pkg { | ||
| parent := scope.Parent() | ||
| for i := range parent.NumChildren() { | ||
| if parent.Child(i) == scope { | ||
|
||
| ids = append(ids, i) | ||
| scope = scope.Parent() | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var buf []byte | ||
| for _, v := range ids { | ||
| buf = strconv.AppendInt(buf, int64(v), 10) | ||
| buf = append(buf, ':') | ||
| } | ||
|
|
||
| id := string(buf) | ||
| scopeIDCache.scopeid[entry] = id | ||
| return id | ||
| } | ||
|
|
||
| // getTypeCodeName returns a name for this type that can be used in the | ||
| // interface lowering pass to assign type codes as expected by the reflect | ||
| // package. See getTypeCodeNum. | ||
| func getTypeCodeName(t types.Type) (string, bool) { | ||
| switch t := types.Unalias(t).(type) { | ||
| case *types.Named: | ||
| if t.Obj().Parent() != t.Obj().Pkg().Scope() { | ||
| return "named:" + t.String() + "$local", true | ||
dgryski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| parent, pkg := t.Obj().Parent(), t.Obj().Pkg().Scope() | ||
| if parent != pkg { | ||
| return fmt.Sprintf("named:%s$local:%s", t.String(), scopeID(pkg, parent)), true | ||
| } | ||
| return "named:" + t.String(), false | ||
| case *types.Array: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this into the compiler context to get rid of the lock and the memory leak.