Skip to content

Commit a5f73b9

Browse files
committed
Rename Name.Name() to Name.Ident().
Name.Name makes embedding of Name difficult.
1 parent b64ab35 commit a5f73b9

14 files changed

+75
-75
lines changed

asyncpi.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import (
1212
type Name interface {
1313
FreeNames() []Name
1414
FreeVars() []Name
15-
Name() string
15+
Ident() string
1616
}
1717

1818
type names []Name
1919

20-
func (n names) Less(i, j int) bool { return n[i].Name() < n[j].Name() }
20+
func (n names) Less(i, j int) bool { return n[i].Ident() < n[j].Ident() }
2121

2222
// remDup removes duplicate Names from sorted []Name.
2323
func remDup(names []Name) []Name {
2424
m := make(map[string]bool)
2525
for _, name := range names {
26-
if _, seen := m[name.Name()]; !seen {
26+
if _, seen := m[name.Ident()]; !seen {
2727
names[len(m)] = name
28-
m[name.Name()] = true
28+
m[name.Ident()] = true
2929
}
3030
}
3131
return names[:len(m)]
@@ -147,10 +147,10 @@ func (r *Recv) FreeVars() []Name {
147147

148148
ffv := fv[:0] // filtered
149149
for i, j := 0, 0; i < len(fv); i++ {
150-
for j < len(r.Vars) && r.Vars[j].Name() < fv[i].Name() {
150+
for j < len(r.Vars) && r.Vars[j].Ident() < fv[i].Ident() {
151151
j++
152152
}
153-
if j < len(r.Vars) && r.Vars[j].Name() != fv[i].Name() { // overshoot
153+
if j < len(r.Vars) && r.Vars[j].Ident() != fv[i].Ident() { // overshoot
154154
ffv = append(ffv, fv[i])
155155
} else if i >= len(r.Vars) { // remaining
156156
ffv = append(ffv, fv[i])
@@ -162,7 +162,7 @@ func (r *Recv) FreeVars() []Name {
162162
}
163163

164164
func (r *Recv) String() string {
165-
return fmt.Sprintf("recv(%s,%s).%s", r.Chan.Name(), r.Vars, r.Cont)
165+
return fmt.Sprintf("recv(%s,%s).%s", r.Chan.Ident(), r.Vars, r.Cont)
166166
}
167167

168168
// Repeat is a replicated Process.
@@ -273,5 +273,5 @@ func (s *Send) FreeVars() []Name {
273273
}
274274

275275
func (s *Send) String() string {
276-
return fmt.Sprintf("send(%s,%s)", s.Chan.Name(), s.Vals)
276+
return fmt.Sprintf("send(%s,%s)", s.Chan.Ident(), s.Vals)
277277
}

asyncpi_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func init() {
5555
func TestFreeName(t *testing.T) {
5656
name := newPiName("a")
5757
freeNames := name.FreeNames()
58-
if len(freeNames) == 1 && freeNames[0].Name() != name.Name() {
58+
if len(freeNames) == 1 && freeNames[0].Ident() != name.Ident() {
5959
t.Errorf("FreeName: fn(a) does not match a: `%s` vs `%s`", freeNames, name)
6060
}
6161
}
@@ -74,7 +74,7 @@ func TestFreeNames(t *testing.T) {
7474
t.Fail()
7575
}
7676
for i := range piNames {
77-
if piNames[i].Name() != freeNames[i].Name() {
77+
if piNames[i].Ident() != freeNames[i].Ident() {
7878
t.Errorf("FreeNames: fn(a...) does not match a...: `%s` vs `%s`", freeNames, piNames)
7979
}
8080
}
@@ -119,7 +119,7 @@ func TestParFreeVar(t *testing.T) {
119119
}
120120
for i := range test.FreeNames {
121121
fn := proc.FreeNames()[i]
122-
if fn.Name() != test.FreeNames[i].Name() {
122+
if fn.Ident() != test.FreeNames[i].Ident() {
123123
t.Errorf("FreeNames(par): parsed and test case do not match: `%s` vs `%s`",
124124
fn, test.FreeNames[i])
125125
}

bind.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func bind(p Process, boundNames []Name) Process {
3333
for _, v := range proc.Vars {
3434
for j := 0; j < len(names)-len(proc.Vars); j++ {
3535
if IsSameName(v, names[j]) {
36-
log.Println("Warning: rebinding name", v.Name(), "in recv")
36+
log.Println("Warning: rebinding name", v.Ident(), "in recv")
3737
names = append(names[:j], names[j+1:]...)
3838
}
3939
}
@@ -74,7 +74,7 @@ func bind(p Process, boundNames []Name) Process {
7474
names := append(boundNames, proc.Name)
7575
for i := 0; i < len(names)-1; i++ {
7676
if IsSameName(proc.Name, names[i]) {
77-
log.Println("Warning: rebinding name", proc.Name.Name(), "in restrict")
77+
log.Println("Warning: rebinding name", proc.Name.Ident(), "in restrict")
7878
names = append(names[:i], names[i+1:]...)
7979
}
8080
}

calculi.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ const parTmpl = `(
2525
{{- if $i }} | {{ end -}}{{- $p.Calculi -}}
2626
{{- end -}})`
2727

28-
const recvTmpl = `{{- .Chan.Name -}}(
28+
const recvTmpl = `{{- .Chan.Ident -}}(
2929
{{- range $i, $v := .Vars -}}
30-
{{- if $i -}},{{- end -}}{{- $v.Name -}}
30+
{{- if $i -}},{{- end -}}{{- $v.Ident -}}
3131
{{- end -}}).{{ .Cont.Calculi }}`
3232

33-
const sendTmpl = `{{- .Chan.Name -}}<
33+
const sendTmpl = `{{- .Chan.Ident -}}<
3434
{{- range $i, $v := .Vals -}}
35-
{{- if $i -}},{{- end -}}{{- $v.Name -}}
35+
{{- if $i -}},{{- end -}}{{- $v.Ident -}}
3636
{{- end -}}>`
3737

3838
const repTmpl = `!{{- .Proc.Calculi -}}`
3939

40-
const resTmpl = `(new {{ .Name.Name -}}){{- .Proc.Calculi -}}`
40+
const resTmpl = `(new {{ .Name.Ident -}}){{- .Proc.Calculi -}}`
4141

4242
var (
4343
parT = template.Must(template.New("").Parse(parTmpl))

codegen/golang/gen.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ func gen(p asyncpi.Process, w io.Writer) error {
6363
return nil
6464
case *asyncpi.Restrict:
6565
if chType, ok := p.Name.(types.TypedName).Type().(*types.Chan); ok { // channel is treated differently.
66-
w.Write([]byte(fmt.Sprintf("%s := make(%s); ", p.Name.(types.TypedName).Name(), chType.String())))
66+
w.Write([]byte(fmt.Sprintf("%s := make(%s); ", p.Name.(types.TypedName).Ident(), chType.String())))
6767
if err := gen(p.Proc, w); err != nil {
6868
return err
6969
}
7070
return nil
7171
}
72-
w.Write([]byte(fmt.Sprintf("var %s %s; ", p.Name.Name(), p.Name.(types.TypedName).Type())))
72+
w.Write([]byte(fmt.Sprintf("var %s %s; ", p.Name.Ident(), p.Name.(types.TypedName).Type())))
7373
if err := gen(p.Proc, w); err != nil {
7474
return err
7575
}
@@ -78,16 +78,16 @@ func gen(p asyncpi.Process, w io.Writer) error {
7878
var buf bytes.Buffer
7979
switch len(p.Vars) {
8080
case 0:
81-
buf.WriteString(fmt.Sprintf("<-%s;", p.Chan.Name()))
81+
buf.WriteString(fmt.Sprintf("<-%s;", p.Chan.Ident()))
8282
case 1:
83-
buf.WriteString(fmt.Sprintf("%s := <-%s;", p.Vars[0].Name(), p.Chan.Name()))
83+
buf.WriteString(fmt.Sprintf("%s := <-%s;", p.Vars[0].Ident(), p.Chan.Ident()))
8484
default:
85-
buf.WriteString(fmt.Sprintf("rcvd := <-%s;", p.Chan.Name()))
85+
buf.WriteString(fmt.Sprintf("rcvd := <-%s;", p.Chan.Ident()))
8686
for i, v := range p.Vars {
8787
if i != 0 {
8888
buf.WriteRune(',')
8989
}
90-
buf.WriteString(fmt.Sprintf("%s", v.Name()))
90+
buf.WriteString(fmt.Sprintf("%s", v.Ident()))
9191
}
9292
buf.WriteString(":=")
9393
for i := 0; i < len(p.Vars); i++ {
@@ -107,11 +107,11 @@ func gen(p asyncpi.Process, w io.Writer) error {
107107
var buf bytes.Buffer
108108
switch len(p.Vals) {
109109
case 0:
110-
buf.WriteString(fmt.Sprintf("%s <- struct{}{};", p.Chan.Name()))
110+
buf.WriteString(fmt.Sprintf("%s <- struct{}{};", p.Chan.Ident()))
111111
case 1:
112-
buf.WriteString(fmt.Sprintf("%s <- %s;", p.Chan.Name(), p.Vals[0].Name()))
112+
buf.WriteString(fmt.Sprintf("%s <- %s;", p.Chan.Ident(), p.Vals[0].Ident()))
113113
default:
114-
buf.WriteString(fmt.Sprintf("%s <- struct {", p.Chan.Name()))
114+
buf.WriteString(fmt.Sprintf("%s <- struct {", p.Chan.Ident()))
115115
for i := 0; i < len(p.Vals); i++ {
116116
if i != 0 {
117117
buf.WriteRune(';')
@@ -123,7 +123,7 @@ func gen(p asyncpi.Process, w io.Writer) error {
123123
if i != 0 {
124124
buf.WriteRune(',')
125125
}
126-
buf.WriteString(v.Name())
126+
buf.WriteString(v.Ident())
127127
}
128128
buf.WriteString(fmt.Sprintf("}"))
129129
}

errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type ImmutableNameError struct {
2222
}
2323

2424
func (e ImmutableNameError) Error() string {
25-
return fmt.Sprintf("cannot modify name %v: immutable implementation of Name", e.Name.Name())
25+
return fmt.Sprintf("cannot modify name %v: immutable implementation of Name", e.Name.Ident())
2626
}
2727

2828
// UnknownProcessTypeError is the type of error for an unknown process type.

names.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func (n *piName) FreeVars() []Name {
4343
return []Name{}
4444
}
4545

46-
// Name is the string identifier of a name.
47-
func (n *piName) Name() string {
46+
// Ident is the string identifier of a name.
47+
func (n *piName) Ident() string {
4848
return n.name
4949
}
5050

@@ -86,8 +86,8 @@ func (n *hintedName) FreeVars() []Name {
8686
return n.name.FreeVars()
8787
}
8888

89-
func (n *hintedName) Name() string {
90-
return n.name.Name()
89+
func (n *hintedName) Ident() string {
90+
return n.name.Ident()
9191
}
9292

9393
// TypeHint returns the type hint attached to given Name.
@@ -115,7 +115,7 @@ func (u *Uniquefier) visit(n Name) string {
115115
if un, ok := u.names[n]; ok {
116116
return un
117117
}
118-
s := fmt.Sprintf("%s_%d", n.Name(), len(u.names))
118+
s := fmt.Sprintf("%s_%d", n.Ident(), len(u.names))
119119
u.names[n] = s
120120
return s
121121
}
@@ -176,9 +176,9 @@ func UpdateName(proc Process, a NameVisitor) error {
176176
// A Name x is equal with another Name y when x and y has the same name.
177177
// This comparison ignores the underlying representation (sort, type, etc.).
178178
func IsSameName(x, y Name) bool {
179-
return x.Name() == y.Name()
179+
return x.Ident() == y.Ident()
180180
}
181181

182182
func IsFreeName(x Name) bool {
183-
return len(x.FreeNames()) == 1 && x.FreeNames()[0].Name() == x.Name()
183+
return len(x.FreeNames()) == 1 && x.FreeNames()[0].Ident() == x.Ident()
184184
}

reduce.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func Subst(p Process, vs, xs []Name) error {
2323
for i, x := range xs {
2424
if IsSameName(p.Chan, x) {
2525
if ch, canSetName := p.Chan.(nameSetter); canSetName {
26-
ch.setName(vs[i].Name())
26+
ch.setName(vs[i].Ident())
2727
}
2828
}
2929
for _, rv := range p.Vars {
3030
if IsSameName(rv, x) {
3131
if ch, canSetName := rv.(nameSetter); canSetName {
32-
ch.setName(vs[i].Name())
32+
ch.setName(vs[i].Ident())
3333
}
3434
}
3535
}
@@ -43,7 +43,7 @@ func Subst(p Process, vs, xs []Name) error {
4343
for i, x := range xs {
4444
if IsSameName(p.Chan, x) {
4545
if ch, canSetName := p.Chan.(nameSetter); canSetName {
46-
ch.setName(vs[i].Name())
46+
ch.setName(vs[i].Ident())
4747
}
4848
}
4949
}
@@ -75,7 +75,7 @@ func reduceOnce(p Process) (changed bool, err error) {
7575
recvs = make(map[string]*Process)
7676
}
7777
if IsFreeName(proc.Chan) {
78-
ch := proc.Chan.Name()
78+
ch := proc.Chan.Ident()
7979
// Do not overwrite existing subprocess with same channel.
8080
// Substitution only consider leftmost available names.
8181
if _, exists := recvs[ch]; !exists {
@@ -87,7 +87,7 @@ func reduceOnce(p Process) (changed bool, err error) {
8787
sends = make(map[string]*Process)
8888
}
8989
if IsFreeName(proc.Chan) {
90-
ch := proc.Chan.Name()
90+
ch := proc.Chan.Ident()
9191
// Do not overwrite existing subprocess with same channel.
9292
// Substitution only consider leftmost available names.
9393
if _, exists := sends[ch]; !exists {
@@ -166,26 +166,26 @@ func findUnusedRestrict(p Process) (unused []Name, err error) {
166166
case *Par:
167167
procs = append(procs, p.Procs...)
168168
case *Recv:
169-
if rc, exists := resUses[p.Chan.Name()]; exists {
169+
if rc, exists := resUses[p.Chan.Ident()]; exists {
170170
rc.Count++
171171
}
172172
for _, v := range p.Vars {
173-
if rc, exists := resUses[v.Name()]; exists {
173+
if rc, exists := resUses[v.Ident()]; exists {
174174
rc.Count++
175175
}
176176
}
177177
procs = append(procs, p.Cont)
178178
case *Repeat:
179179
procs = append(procs, p.Proc)
180180
case *Restrict:
181-
resUses[p.Name.Name()] = &rescount{ResName: p.Name, Count: 1}
181+
resUses[p.Name.Ident()] = &rescount{ResName: p.Name, Count: 1}
182182
procs = append(procs, p.Proc)
183183
case *Send:
184-
if rc, exists := resUses[p.Chan.Name()]; exists {
184+
if rc, exists := resUses[p.Chan.Ident()]; exists {
185185
rc.Count++
186186
}
187187
for _, v := range p.Vals {
188-
if rc, exists := resUses[v.Name()]; exists {
188+
if rc, exists := resUses[v.Ident()]; exists {
189189
rc.Count++
190190

191191
}

reduce_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestReduceSendRecv(t *testing.T) {
2626
if !ok {
2727
t.Fatalf("expects *Recv but got %s (type %T)", p.Calculi(), p)
2828
}
29-
if want, got := "b", p2.Chan.Name(); want != got {
29+
if want, got := "b", p2.Chan.Ident(); want != got {
3030
t.Fatalf("expects receive on %s but got %s", want, got)
3131
}
3232
_, ok = p2.Cont.(*NilProcess)
@@ -56,7 +56,7 @@ func TestReduceRecvSend(t *testing.T) {
5656
if !ok {
5757
t.Fatalf("expects *Send but got %s (type %T)", p.Calculi(), p)
5858
}
59-
if want, got := "b", p2.Chan.Name(); want != got {
59+
if want, got := "b", p2.Chan.Ident(); want != got {
6060
t.Fatalf("expects send on %s but got %s", want, got)
6161
}
6262
}
@@ -82,14 +82,14 @@ func TestReduceBoundRecvSend(t *testing.T) {
8282
if !ok {
8383
t.Fatalf("expects a *Restrict but got %s (type %T)", p.Calculi(), p)
8484
}
85-
if want, got := "b", p2.Name.Name(); want != got {
85+
if want, got := "b", p2.Name.Ident(); want != got {
8686
t.Fatalf("expects restrict on %s but got %s", want, got)
8787
}
8888
p3, ok := p2.Proc.(*Send)
8989
if !ok {
9090
t.Fatalf("expects *Send but got %s (type %T)", p2.Calculi(), p2)
9191
}
92-
if want, got := "b", p3.Chan.Name(); want != got {
92+
if want, got := "b", p3.Chan.Ident(); want != got {
9393
t.Fatalf("expects send on %s but got %s", want, got)
9494
}
9595
}

sorts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func ResetSorts(proc Process) {
104104
type NameVarSorter struct{}
105105

106106
func (s *NameVarSorter) visit(n Name) string {
107-
r, _ := utf8.DecodeRuneInString(n.Name())
107+
r, _ := utf8.DecodeRuneInString(n.Ident())
108108
if strings.ContainsRune("nopqrstuvwxyz", r) {
109109
if s, ok := n.(sortSetter); ok {
110110
s.setSort(varSort)
111111
}
112112
}
113-
return n.Name()
113+
return n.Ident()
114114
}

sorts_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ func TestDefaultSort(t *testing.T) {
66
n := newPiName("name")
77
if expect, got := 1, len(n.FreeNames()); expect != got {
88
t.Errorf("Expecting %s to have %d free names but got %d. fn(%s) = %s",
9-
n.Name(), expect, got, n.Name(), n.FreeNames())
9+
n.Ident(), expect, got, n.Ident(), n.FreeNames())
1010
}
1111
if expect, got := 0, len(n.FreeVars()); expect != got {
1212
t.Errorf("Expecting %s to have %d free vars but got %d. fv(%s) = %s",
13-
n.Name(), expect, got, n.Name(), n.FreeVars())
13+
n.Ident(), expect, got, n.Ident(), n.FreeVars())
1414
}
1515
}
1616

@@ -149,18 +149,18 @@ func TestNameVarSort(t *testing.T) {
149149
p.Vars = append(p.Vars, b, x, y, z)
150150
UpdateName(p, new(NameVarSorter))
151151
if expect, got := nameSort, a.s; expect != got {
152-
t.Errorf("Expecting %s sort to be %d but got %d.", a.Name(), expect, got)
152+
t.Errorf("Expecting %s sort to be %d but got %d.", a.Ident(), expect, got)
153153
}
154154
if expect, got := nameSort, b.s; expect != got {
155-
t.Errorf("Expecting %s sort to be %d but got %d.", b.Name(), expect, got)
155+
t.Errorf("Expecting %s sort to be %d but got %d.", b.Ident(), expect, got)
156156
}
157157
if expect, got := varSort, x.s; expect != got {
158-
t.Errorf("Expecting %s sort to be %d but got %d.", x.Name(), expect, got)
158+
t.Errorf("Expecting %s sort to be %d but got %d.", x.Ident(), expect, got)
159159
}
160160
if expect, got := varSort, y.s; expect != got {
161-
t.Errorf("Expecting %s sort to be %d but got %d.", y.Name(), expect, got)
161+
t.Errorf("Expecting %s sort to be %d but got %d.", y.Ident(), expect, got)
162162
}
163163
if expect, got := varSort, z.s; expect != got {
164-
t.Errorf("Expecting %s sort to be %d but got %d.", z.Name(), expect, got)
164+
t.Errorf("Expecting %s sort to be %d but got %d.", z.Ident(), expect, got)
165165
}
166166
}

0 commit comments

Comments
 (0)