1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
package interp
import (
"strings"
"sync/atomic"
)
// adot produces an AST dot(1) directed acyclic graph for the given node. For debugging only.
// func (n *node) adot() { n.astDot(dotWriter(n.interp.dotCmd), n.ident) }
// genAST returns a new AST where generic types are replaced by instantiated types.
func genAST(sc *scope, root *node, types []*itype) (*node, bool, error) {
typeParam := map[string]*node{}
pindex := 0
tname := ""
rtname := ""
recvrPtr := false
fixNodes := []*node{}
var gtree func(*node, *node) (*node, error)
sname := root.child[0].ident + "["
if root.kind == funcDecl {
sname = root.child[1].ident + "["
}
// Input type parameters must be resolved prior AST generation, as compilation
// of generated AST may occur in a different scope.
for _, t := range types {
sname += t.id() + ","
}
sname = strings.TrimSuffix(sname, ",") + "]"
gtree = func(n, anc *node) (*node, error) {
nod := copyNode(n, anc, false)
switch n.kind {
case funcDecl, funcType:
nod.val = nod
case identExpr:
// Replace generic type by instantiated one.
nt, ok := typeParam[n.ident]
if !ok {
break
}
nod = copyNode(nt, anc, true)
nod.typ = nt.typ
case indexExpr:
// Catch a possible recursive generic type definition
if root.kind != typeSpec {
break
}
if root.child[0].ident != n.child[0].ident {
break
}
nod := copyNode(n.child[0], anc, false)
fixNodes = append(fixNodes, nod)
return nod, nil
case fieldList:
// Node is the type parameters list of a generic function.
if root.kind == funcDecl && n.anc == root.child[2] && childPos(n) == 0 {
// Fill the types lookup table used for type substitution.
for _, c := range n.child {
l := len(c.child) - 1
for _, cc := range c.child[:l] {
if pindex >= len(types) {
return nil, cc.cfgErrorf("undefined type for %s", cc.ident)
}
t, err := nodeType(c.interp, sc, c.child[l])
if err != nil {
return nil, err
}
if err := checkConstraint(types[pindex], t); err != nil {
return nil, err
}
typeParam[cc.ident] = copyNode(cc, cc.anc, false)
typeParam[cc.ident].ident = types[pindex].id()
typeParam[cc.ident].typ = types[pindex]
pindex++
}
}
// Skip type parameters specification, so generated func doesn't look generic.
return nod, nil
}
// Node is the receiver of a generic method.
if root.kind == funcDecl && n.anc == root && childPos(n) == 0 && len(n.child) > 0 {
rtn := n.child[0].child[1]
// Method receiver is a generic type if it takes some type parameters.
if rtn.kind == indexExpr || rtn.kind == indexListExpr || (rtn.kind == starExpr && (rtn.child[0].kind == indexExpr || rtn.child[0].kind == indexListExpr)) {
if rtn.kind == starExpr {
// Method receiver is a pointer on a generic type.
rtn = rtn.child[0]
recvrPtr = true
}
rtname = rtn.child[0].ident + "["
for _, cc := range rtn.child[1:] {
if pindex >= len(types) {
return nil, cc.cfgErrorf("undefined type for %s", cc.ident)
}
it := types[pindex]
typeParam[cc.ident] = copyNode(cc, cc.anc, false)
typeParam[cc.ident].ident = it.id()
typeParam[cc.ident].typ = it
rtname += it.id() + ","
pindex++
}
rtname = strings.TrimSuffix(rtname, ",") + "]"
}
}
// Node is the type parameters list of a generic type.
if root.kind == typeSpec && n.anc == root && childPos(n) == 1 {
// Fill the types lookup table used for type substitution.
tname = n.anc.child[0].ident + "["
for _, c := range n.child {
l := len(c.child) - 1
for _, cc := range c.child[:l] {
if pindex >= len(types) {
return nil, cc.cfgErrorf("undefined type for %s", cc.ident)
}
it := types[pindex]
t, err := nodeType(c.interp, sc, c.child[l])
if err != nil {
return nil, err
}
if err := checkConstraint(types[pindex], t); err != nil {
return nil, err
}
typeParam[cc.ident] = copyNode(cc, cc.anc, false)
typeParam[cc.ident].ident = it.id()
typeParam[cc.ident].typ = it
tname += it.id() + ","
pindex++
}
}
tname = strings.TrimSuffix(tname, ",") + "]"
return nod, nil
}
}
for _, c := range n.child {
gn, err := gtree(c, nod)
if err != nil {
return nil, err
}
nod.child = append(nod.child, gn)
}
return nod, nil
}
if nod, found := root.interp.generic[sname]; found {
return nod, true, nil
}
r, err := gtree(root, root.anc)
if err != nil {
return nil, false, err
}
root.interp.generic[sname] = r
r.param = append(r.param, types...)
if tname != "" {
for _, nod := range fixNodes {
nod.ident = tname
}
r.child[0].ident = tname
}
if rtname != "" {
// Replace method receiver type by synthetized ident.
nod := r.child[0].child[0].child[1]
if recvrPtr {
nod = nod.child[0]
}
nod.kind = identExpr
nod.ident = rtname
nod.child = nil
}
// r.adot() // Used for debugging only.
return r, false, nil
}
func copyNode(n, anc *node, recursive bool) *node {
var i interface{}
nindex := atomic.AddInt64(&n.interp.nindex, 1)
nod := &node{
debug: n.debug,
anc: anc,
interp: n.interp,
index: nindex,
level: n.level,
nleft: n.nleft,
nright: n.nright,
kind: n.kind,
pos: n.pos,
action: n.action,
gen: n.gen,
val: &i,
rval: n.rval,
ident: n.ident,
meta: n.meta,
}
nod.start = nod
if recursive {
for _, c := range n.child {
nod.child = append(nod.child, copyNode(c, nod, true))
}
}
return nod
}
func inferTypesFromCall(sc *scope, fun *node, args []*node) ([]*itype, error) {
ftn := fun.typ.node
// Fill the map of parameter types, indexed by type param ident.
paramTypes := map[string]*itype{}
for _, c := range ftn.child[0].child {
typ, err := nodeType(fun.interp, sc, c.lastChild())
if err != nil {
return nil, err
}
for _, cc := range c.child[:len(c.child)-1] {
paramTypes[cc.ident] = typ
}
}
var inferTypes func(*itype, *itype) ([]*itype, error)
inferTypes = func(param, input *itype) ([]*itype, error) {
switch param.cat {
case chanT, ptrT, sliceT:
return inferTypes(param.val, input.val)
case mapT:
k, err := inferTypes(param.key, input.key)
if err != nil {
return nil, err
}
v, err := inferTypes(param.val, input.val)
if err != nil {
return nil, err
}
return append(k, v...), nil
case structT:
lt := []*itype{}
for i, f := range param.field {
nl, err := inferTypes(f.typ, input.field[i].typ)
if err != nil {
return nil, err
}
lt = append(lt, nl...)
}
return lt, nil
case funcT:
lt := []*itype{}
for i, t := range param.arg {
if i >= len(input.arg) {
break
}
nl, err := inferTypes(t, input.arg[i])
if err != nil {
return nil, err
}
lt = append(lt, nl...)
}
for i, t := range param.ret {
if i >= len(input.ret) {
break
}
nl, err := inferTypes(t, input.ret[i])
if err != nil {
return nil, err
}
lt = append(lt, nl...)
}
return lt, nil
case nilT:
if paramTypes[param.name] != nil {
return []*itype{input}, nil
}
case genericT:
return []*itype{input}, nil
}
return nil, nil
}
types := []*itype{}
for i, c := range ftn.child[1].child {
typ, err := nodeType(fun.interp, sc, c.lastChild())
if err != nil {
return nil, err
}
lt, err := inferTypes(typ, args[i].typ)
if err != nil {
return nil, err
}
types = append(types, lt...)
}
return types, nil
}
func checkConstraint(it, ct *itype) error {
if len(ct.constraint) == 0 && len(ct.ulconstraint) == 0 {
return nil
}
for _, c := range ct.constraint {
if it.equals(c) || it.matchDefault(c) {
return nil
}
}
for _, c := range ct.ulconstraint {
if it.underlying().equals(c) || it.matchDefault(c) {
return nil
}
}
return it.node.cfgErrorf("%s does not implement %s", it.id(), ct.id())
}
|