import (
"bytes"
- "fmt"
"go/token"
)
// TODO(rFindley) decide error codes for the errors in this file, and check
// if error spans can be improved
-type substMap struct {
- // The targs field is currently needed for *Named type substitution.
- // TODO(gri) rewrite that code, get rid of this field, and make this
- // struct just the map (proj)
- targs []Type
- proj map[*TypeParam]Type
-}
+type substMap map[*TypeParam]Type
// makeSubstMap creates a new substitution map mapping tpars[i] to targs[i].
// If targs[i] is nil, tpars[i] is not substituted.
-func makeSubstMap(tpars []*TypeName, targs []Type) *substMap {
+func makeSubstMap(tpars []*TypeName, targs []Type) substMap {
assert(len(tpars) == len(targs))
- proj := make(map[*TypeParam]Type, len(tpars))
+ proj := make(substMap, len(tpars))
for i, tpar := range tpars {
proj[tpar.typ.(*TypeParam)] = targs[i]
}
- return &substMap{targs, proj}
-}
-
-func (m *substMap) String() string {
- return fmt.Sprintf("%s", m.proj)
+ return proj
}
-func (m *substMap) empty() bool {
- return len(m.proj) == 0
+func (m substMap) empty() bool {
+ return len(m) == 0
}
-func (m *substMap) lookup(tpar *TypeParam) Type {
- if t := m.proj[tpar]; t != nil {
+func (m substMap) lookup(tpar *TypeParam) Type {
+ if t := m[tpar]; t != nil {
return t
}
return tpar
// from the incoming type.
//
// If the given typMap is nil and check is non-nil, check.typMap is used.
-func (check *Checker) subst(pos token.Pos, typ Type, smap *substMap, typMap map[string]*Named) Type {
+func (check *Checker) subst(pos token.Pos, typ Type, smap substMap, typMap map[string]*Named) Type {
if smap.empty() {
return typ
}
type subster struct {
pos token.Pos
- smap *substMap
+ smap substMap
check *Checker // nil if called via Instantiate
typMap map[string]*Named
}
return t // type is not parameterized
}
- var newTargs []Type
-
- if len(t.targs) > 0 {
- // already instantiated
- dump(">>> %s already instantiated", t)
- assert(len(t.targs) == t.TParams().Len())
- // For each (existing) type argument targ, determine if it needs
- // to be substituted; i.e., if it is or contains a type parameter
- // that has a type argument for it.
- for i, targ := range t.targs {
- dump(">>> %d targ = %s", i, targ)
- newTarg := subst.typ(targ)
- if newTarg != targ {
- dump(">>> substituted %d targ %s => %s", i, targ, newTarg)
- if newTargs == nil {
- newTargs = make([]Type, t.TParams().Len())
- copy(newTargs, t.targs)
- }
- newTargs[i] = newTarg
+ var newTArgs []Type
+ assert(len(t.targs) == t.TParams().Len())
+
+ // already instantiated
+ dump(">>> %s already instantiated", t)
+ // For each (existing) type argument targ, determine if it needs
+ // to be substituted; i.e., if it is or contains a type parameter
+ // that has a type argument for it.
+ for i, targ := range t.targs {
+ dump(">>> %d targ = %s", i, targ)
+ new_targ := subst.typ(targ)
+ if new_targ != targ {
+ dump(">>> substituted %d targ %s => %s", i, targ, new_targ)
+ if newTArgs == nil {
+ newTArgs = make([]Type, t.TParams().Len())
+ copy(newTArgs, t.targs)
}
+ newTArgs[i] = new_targ
}
+ }
- if newTargs == nil {
- dump(">>> nothing to substitute in %s", t)
- return t // nothing to substitute
- }
- } else {
- // not yet instantiated
- dump(">>> first instantiation of %s", t)
- // TODO(rFindley) can we instead subst the tparam types here?
- newTargs = subst.smap.targs
+ if newTArgs == nil {
+ dump(">>> nothing to substitute in %s", t)
+ return t // nothing to substitute
}
// before creating a new named type, check if we have this one already
- h := instantiatedHash(t, newTargs)
+ h := instantiatedHash(t, newTArgs)
dump(">>> new type hash: %s", h)
if named, found := subst.typMap[h]; found {
dump(">>> found %s", named)
tname := NewTypeName(subst.pos, t.obj.pkg, t.obj.name, nil)
t.load()
named := subst.check.newNamed(tname, t.orig, t.underlying, t.TParams(), t.methods) // method signatures are updated lazily
- named.targs = newTargs
+ named.targs = newTArgs
subst.typMap[h] = named
t.expand(subst.typMap) // must happen after typMap update to avoid infinite recursion
// do the substitution
- dump(">>> subst %s with %s (new: %s)", t.underlying, subst.smap, newTargs)
+ dump(">>> subst %s with %s (new: %s)", t.underlying, subst.smap, newTArgs)
named.underlying = subst.typOrNil(t.underlying)
dump(">>> underlying: %v", named.underlying)
assert(named.underlying != nil)