11package compiler
22
33import (
4- "fmt"
5- "go/token"
64 "go/types"
7- "path/filepath"
8- "strings"
95
106 "github.com/gopherjs/gopherjs/compiler/internal/symbol"
117 "github.com/gopherjs/gopherjs/compiler/internal/typeparams"
128)
139
14- const (
15- varDeclNotUniqueFullName = `var:blank`
16- funcVarDeclNotUniqueFullName = `funcVar:init`
17- mainFuncDeclNotUniqueFullName = `init:main`
18- funcDeclNotUniqueFullName = `func:init`
19- typeDeclNestedPrefix = `type:nested:`
20- )
21-
2210// importDeclFullName returns a unique name for an import declaration.
2311// This import name may be duplicated in different packages if they both
2412// import the same package, they are only unique per package.
@@ -28,65 +16,36 @@ func importDeclFullName(importedPkg *types.Package) string {
2816
2917// varDeclFullName returns a name for a package-level variable declaration.
3018// This var name only references the first named variable in an assignment.
31- //
32- // If no variables are named, this will attempt to create a unique name
33- // using the integer value of the first valid position of the blank variables.
34- // Otherwise the name is not unique (i.e. varDeclNotUniqueFullName).
35- func varDeclFullName (init * types.Initializer , fSet * token.FileSet ) string {
19+ // If no variables are named, the name is `var:blank` and not unique.
20+ func varDeclFullName (init * types.Initializer ) string {
3621 for _ , lhs := range init .Lhs {
3722 if lhs .Name () != `_` {
3823 return `var:` + symbol .New (lhs ).String ()
3924 }
4025 }
41-
42- for _ , lhs := range init .Lhs {
43- if pos := lhs .Pos (); pos .IsValid () {
44- return `var:blank` + declFullNameDiscriminator (pos , fSet )
45- }
46- }
47-
48- return varDeclNotUniqueFullName
26+ return `var:blank`
4927}
5028
5129// funcVarDeclFullName returns a name for a package-level variable
5230// that is used for a function (without a receiver) declaration.
53- // If the function is generic, this declaration name is for the list
54- // of instantiations of the function.
55- //
5631// The name is unique unless the function is an `init` function.
57- // If the name is for an `init` function, the position of the function
58- // is used to create a unique name if it is valid,
59- // otherwise the name is not unique (i.e. funcVarDeclNotUniqueFullName).
60- func funcVarDeclFullName (o * types.Func , fSet * token.FileSet ) string {
61- name := `funcVar:` + symbol .New (o ).String ()
62- if o .Name () == `init` {
63- name += declFullNameDiscriminator (o .Pos (), fSet )
64- }
65- // In the case of an `init` function with an invalid position,
66- // the name is not unique and will be equal to funcVarDeclNotUnqueName.
67- return name
32+ // If the function is generic, this declaration name is also for the list
33+ // of instantiations of the function.
34+ func funcVarDeclFullName (o * types.Func ) string {
35+ return `funcVar:` + symbol .New (o ).String ()
6836}
6937
7038// mainFuncDeclFullName returns the name for the declaration used to invoke the
7139// main function of the program. There should only be one decl with this name.
72- // This will always return mainFuncDeclNotUniqueFullName.
7340func mainFuncDeclFullName () string {
74- return mainFuncDeclNotUniqueFullName
41+ return `init:main`
7542}
7643
7744// funcDeclFullName returns a name for a package-level function
7845// declaration for the given instance of a function.
79- // The name is unique unless the function is an `init` function
80- // then it tries to use the position of the function to create a unique name.
81- // Otherwise the name is not unique (i.e. funcDeclNotUniqueFullName).
82- func funcDeclFullName (inst typeparams.Instance , fSet * token.FileSet ) string {
83- name := `func:` + inst .String ()
84- if inst .IsTrivial () && inst .Object .Name () == `init` {
85- name += declFullNameDiscriminator (inst .Object .Pos (), fSet )
86- }
87- // In the case of an `init` function with an invalid position,
88- // the name is not unique and will be equal to funcDeclNotUniqueFullName.
89- return name
46+ // The name is unique unless the function is an `init` function.
47+ func funcDeclFullName (inst typeparams.Instance ) string {
48+ return `func:` + inst .String ()
9049}
9150
9251// typeVarDeclFullName returns a unique name for a package-level variable
@@ -98,17 +57,10 @@ func typeVarDeclFullName(o *types.TypeName) string {
9857}
9958
10059// typeDeclFullName returns a unique name for a package-level type declaration
101- // for the given instance of a type. Names are only unique per package
60+ // for the given instance of a type. Names are only unique per scope package
10261// unless the type is a nested type then the name is only unique per the
103- // function or method it is declared in. In that case, the position of the
104- // declaration is used to try to create a unique name.
105- // If the position is invalid, the nested name may be unique or not,
106- // we cannot guarantee uniqueness in that case.
107- func typeDeclFullName (inst typeparams.Instance , fSet * token.FileSet ) string {
108- if typeparams .FindNestingFunc (inst .Object ) != nil {
109- return typeDeclNestedPrefix + inst .String () +
110- declFullNameDiscriminator (inst .Object .Pos (), fSet )
111- }
62+ // function or method it is declared in.
63+ func typeDeclFullName (inst typeparams.Instance ) string {
11264 return `type:` + inst .String ()
11365}
11466
@@ -118,31 +70,3 @@ func typeDeclFullName(inst typeparams.Instance, fSet *token.FileSet) string {
11870func anonTypeDeclFullName (o types.Object ) string {
11971 return `anonType:` + symbol .New (o ).String ()
12072}
121-
122- // declFullNameDiscriminator returns a string representing the position of a
123- // declaration that is used to differentiate declarations with the same name.
124- // Since the package path will already be part of the full name, only the file,
125- // line, and column are used.
126- // If position is invalid, an empty string is returned.
127- func declFullNameDiscriminator (pos token.Pos , fSet * token.FileSet ) string {
128- if ! pos .IsValid () {
129- return ``
130- }
131- p := fSet .Position (pos )
132- fileName := filepath .Base (filepath .ToSlash (p .Filename ))
133- return fmt .Sprintf ("@%s:%d:%d" , fileName , p .Line , p .Column )
134- }
135-
136- // isUniqueDeclFullName reports whether the given declaration full name is unique.
137- // A unique declaration name can be safely cached and reused.
138- func isUniqueDeclFullName (name string ) bool {
139- switch name {
140- case `` , varDeclNotUniqueFullName , funcVarDeclNotUniqueFullName ,
141- mainFuncDeclNotUniqueFullName , funcDeclNotUniqueFullName :
142- return false // not unique since it equals one of the known non-unique names
143- }
144-
145- // If the name is for a nested type declaration without a position discriminator,
146- // then it is not unique, otherwise it should be unique.
147- return ! strings .HasPrefix (name , typeDeclNestedPrefix ) || strings .Contains (name , `@` )
148- }
0 commit comments