| 1 | package interp |
| 2 | |
| 3 | import ( |
| 4 | "path" |
| 5 | ) |
| 6 | |
| 7 | // gta performs a global types analysis on the AST, registering types, |
| 8 | // variables and functions symbols at package level, prior to CFG. |
| 9 | // All function bodies are skipped. GTA is necessary to handle out of |
| 10 | // order declarations and multiple source files packages. |
| 11 | // rpath is the relative path to the directory containing the source for the package. |
| 12 | func (interp *Interpreter) gta(root *node, rpath, importPath, pkgName string) ([]*node, error) { |
| 13 | sc := interp.initScopePkg(importPath, pkgName) |
| 14 | var err error |
| 15 | var revisit []*node |
| 16 | |
| 17 | baseName := path.Base(interp.fset.Position(root.pos).Filename) |
| 18 | |
| 19 | root.Walk(func(n *node) bool { |
| 20 | if err != nil { |
| 21 | return false |
| 22 | } |
| 23 | if n.scope == nil { |
| 24 | n.scope = sc |
| 25 | } |
| 26 | switch n.kind { |
| 27 | case constDecl: |
| 28 | // Early parse of constDecl subtree, to compute all constant |
| 29 | // values which may be used in further declarations. |
| 30 | if _, err = interp.cfg(n, sc, importPath, pkgName); err != nil { |
| 31 | // No error processing here, to allow recovery in subtree nodes. |
| 32 | // TODO(marc): check for a non recoverable error and return it for better diagnostic. |
| 33 | err = nil |
| 34 | } |
| 35 | |
| 36 | case blockStmt: |
| 37 | if n != root { |
| 38 | return false // skip statement block if not the entry point |
| 39 | } |
| 40 | |
| 41 | case defineStmt: |
| 42 | var ( |
| 43 | atyp *itype |
| 44 | err2 error |
| 45 | ) |
| 46 | if n.nleft+n.nright < len(n.child) { |
| 47 | // Type is declared explicitly in the assign expression. |
| 48 | if atyp, err2 = nodeType(interp, sc, n.child[n.nleft]); err2 != nil { |
| 49 | // The type does not exist yet, stash the error and come back |
| 50 | // when the type is known. |
| 51 | n.meta = err2 |
| 52 | revisit = append(revisit, n) |
| 53 | return false |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | var sbase int |
| 58 | if n.nright > 0 { |
| 59 | sbase = len(n.child) - n.nright |
| 60 | } |
| 61 | |
| 62 | for i := 0; i < n.nleft; i++ { |
| 63 | dest, src := n.child[i], n.child[sbase+i] |
| 64 | if isBlank(src) { |
| 65 | err = n.cfgErrorf("cannot use _ as value") |
| 66 | } |
| 67 | val := src.rval |
| 68 | if n.anc.kind == constDecl { |
| 69 | if _, err2 := interp.cfg(n, sc, importPath, pkgName); err2 != nil { |
| 70 | // Constant value can not be computed yet. |
| 71 | // Come back when child dependencies are known. |
| 72 | revisit = append(revisit, n) |
| 73 | return false |
| 74 | } |
| 75 | } |
| 76 | typ := atyp |
| 77 | if typ == nil { |
| 78 | if typ, err2 = nodeType(interp, sc, src); err2 != nil || typ == nil { |
| 79 | // The type does is not known yet, stash the error and come back |
| 80 | // when the type is known. |
| 81 | n.meta = err2 |
| 82 | revisit = append(revisit, n) |
| 83 | return false |
| 84 | } |
| 85 | val = src.rval |
| 86 | } |
| 87 | if !typ.isComplete() { |
| 88 | // Come back when type is known. |
| 89 | revisit = append(revisit, n) |
| 90 | return false |
| 91 | } |
| 92 | if typ.cat == nilT { |
| 93 | err = n.cfgErrorf("use of untyped nil") |
| 94 | return false |
| 95 | } |
| 96 | if typ.isBinMethod { |
| 97 | typ = valueTOf(typ.methodCallType(), isBinMethod(), withScope(sc)) |
| 98 | } |
| 99 | sc.sym[dest.ident] = &symbol{kind: varSym, global: true, index: sc.add(typ), typ: typ, rval: val, node: n} |
| 100 | if n.anc.kind == constDecl { |
| 101 | sc.sym[dest.ident].kind = constSym |
| 102 | if childPos(n) == len(n.anc.child)-1 { |
| 103 | sc.iota = 0 |
| 104 | } else { |
| 105 | sc.iota++ |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return false |
| 110 | |
| 111 | case defineXStmt: |
| 112 | err = compDefineX(sc, n) |
| 113 | |
| 114 | case valueSpec: |
| 115 | l := len(n.child) - 1 |
| 116 | if n.typ = n.child[l].typ; n.typ == nil { |
| 117 | if n.typ, err = nodeType(interp, sc, n.child[l]); err != nil { |
| 118 | return false |
| 119 | } |
| 120 | if !n.typ.isComplete() { |
| 121 | // Come back when type is known. |
| 122 | revisit = append(revisit, n) |
| 123 | return false |
| 124 | } |
| 125 | } |
| 126 | for _, c := range n.child[:l] { |
| 127 | asImportName := path.Join(c.ident, baseName) |
| 128 | sym, exists := sc.sym[asImportName] |
| 129 | if !exists { |
| 130 | sc.sym[c.ident] = &symbol{index: sc.add(n.typ), kind: varSym, global: true, typ: n.typ, node: n} |
| 131 | continue |
| 132 | } |
| 133 | c.level = globalFrame |
| 134 | |
| 135 | // redeclaration error |
| 136 | if sym.typ.node != nil && sym.typ.node.anc != nil { |
| 137 | prevDecl := n.interp.fset.Position(sym.typ.node.anc.pos) |
| 138 | err = n.cfgErrorf("%s redeclared in this block\n\tprevious declaration at %v", c.ident, prevDecl) |
| 139 | return false |
| 140 | } |
| 141 | err = n.cfgErrorf("%s redeclared in this block", c.ident) |
| 142 | return false |
| 143 | } |
| 144 | |
| 145 | case funcDecl: |
| 146 | if n.typ, err = nodeType(interp, sc, n.child[2]); err != nil { |
| 147 | return false |
| 148 | } |
| 149 | genericMethod := false |
| 150 | ident := n.child[1].ident |
| 151 | switch { |
| 152 | case isMethod(n): |
| 153 | // Add a method symbol in the receiver type name space |
| 154 | var rcvrtype *itype |
| 155 | n.ident = ident |
| 156 | rcvr := n.child[0].child[0] |
| 157 | rtn := rcvr.lastChild() |
| 158 | typName, typPtr := rtn.ident, false |
| 159 | // Identifies the receiver type name. It could be an ident, a |
| 160 | // generic type (indexExpr), or a pointer on either lasts. |
| 161 | if typName == "" { |
| 162 | typName = rtn.child[0].ident |
| 163 | switch rtn.kind { |
| 164 | case starExpr: |
| 165 | typPtr = true |
| 166 | switch c := rtn.child[0]; c.kind { |
| 167 | case indexExpr, indexListExpr: |
| 168 | typName = c.child[0].ident |
| 169 | genericMethod = true |
| 170 | } |
| 171 | case indexExpr, indexListExpr: |
| 172 | genericMethod = true |
| 173 | } |
| 174 | } |
| 175 | sym, _, found := sc.lookup(typName) |
| 176 | if !found { |
| 177 | n.meta = n.cfgErrorf("undefined: %s", typName) |
| 178 | revisit = append(revisit, n) |
| 179 | return false |
| 180 | } |
| 181 | if sym.typ.path != pkgName { |
| 182 | err = n.cfgErrorf("cannot define new methods on non-local type %s", baseType(sym.typ).id()) |
| 183 | return false |
| 184 | } |
| 185 | rcvrtype = sym.typ |
| 186 | if typPtr { |
| 187 | elementType := sym.typ |
| 188 | rcvrtype = ptrOf(elementType, withNode(rtn), withScope(sc)) |
| 189 | rcvrtype.incomplete = elementType.incomplete |
| 190 | elementType.addMethod(n) |
| 191 | } |
| 192 | rcvrtype.addMethod(n) |
| 193 | rtn.typ = rcvrtype |
| 194 | if rcvrtype.cat == genericT { |
| 195 | // generate methods for already instantiated receivers |
| 196 | for _, it := range rcvrtype.instance { |
| 197 | if err = genMethod(interp, sc, it, n, it.node.anc.param); err != nil { |
| 198 | return false |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | case ident == "init": |
| 203 | // init functions do not get declared as per the Go spec. |
| 204 | default: |
| 205 | asImportName := path.Join(ident, baseName) |
| 206 | if _, exists := sc.sym[asImportName]; exists { |
| 207 | // redeclaration error |
| 208 | err = n.cfgErrorf("%s redeclared in this block", ident) |
| 209 | return false |
| 210 | } |
| 211 | // Add a function symbol in the package name space except for init |
| 212 | sc.sym[ident] = &symbol{kind: funcSym, typ: n.typ, node: n, index: -1} |
| 213 | } |
| 214 | if !n.typ.isComplete() && !genericMethod { |
| 215 | revisit = append(revisit, n) |
| 216 | } |
| 217 | return false |
| 218 | |
| 219 | case importSpec: |
| 220 | var name, ipath string |
| 221 | if len(n.child) == 2 { |
| 222 | ipath = constToString(n.child[1].rval) |
| 223 | name = n.child[0].ident |
| 224 | } else { |
| 225 | ipath = constToString(n.child[0].rval) |
| 226 | } |
| 227 | // Try to import a binary package first, or a source package |
| 228 | var pkgName string |
| 229 | if packageName := path.Base(ipath); path.Dir(ipath) == packageName { |
| 230 | ipath = packageName |
| 231 | } |
| 232 | if pkg := interp.binPkg[ipath]; pkg != nil { |
| 233 | switch name { |
| 234 | case "_": // no import of symbols |
| 235 | case ".": // import symbols in current scope |
| 236 | for n, v := range pkg { |
| 237 | typ := v.Type() |
| 238 | kind := binSym |
| 239 | if isBinType(v) { |
| 240 | typ = typ.Elem() |
| 241 | kind = typeSym |
| 242 | } |
| 243 | sc.sym[n] = &symbol{kind: kind, typ: valueTOf(typ, withScope(sc)), rval: v} |
| 244 | } |
| 245 | default: // import symbols in package namespace |
| 246 | if name == "" { |
| 247 | name = interp.pkgNames[ipath] |
| 248 | } |
| 249 | |
| 250 | // If an incomplete type exists, delete it |
| 251 | if sym, exists := sc.sym[name]; exists && sym.kind == typeSym && sym.typ.incomplete { |
| 252 | delete(sc.sym, name) |
| 253 | } |
| 254 | |
| 255 | // Imports of a same package are all mapped in the same scope, so we cannot just |
| 256 | // map them by their names, otherwise we could have collisions from same-name |
| 257 | // imports in different source files of the same package. Therefore, we suffix |
| 258 | // the key with the basename of the source file. |
| 259 | name = path.Join(name, baseName) |
| 260 | if sym, exists := sc.sym[name]; !exists { |
| 261 | sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: binPkgT, path: ipath, scope: sc}} |
| 262 | break |
| 263 | } else if sym.kind == pkgSym && sym.typ.cat == srcPkgT && sym.typ.path == ipath { |
| 264 | // ignore re-import of identical package |
| 265 | break |
| 266 | } |
| 267 | |
| 268 | // redeclaration error. Not caught by the parser. |
| 269 | err = n.cfgErrorf("%s redeclared in this block", name) |
| 270 | return false |
| 271 | } |
| 272 | } else if pkgName, err = interp.importSrc(rpath, ipath, NoTest); err == nil { |
| 273 | sc.types = interp.universe.types |
| 274 | switch name { |
| 275 | case "_": // no import of symbols |
| 276 | case ".": // import symbols in current namespace |
| 277 | for k, v := range interp.srcPkg[ipath] { |
| 278 | if canExport(k) { |
| 279 | sc.sym[k] = v |
| 280 | } |
| 281 | } |
| 282 | default: // import symbols in package namespace |
| 283 | if name == "" { |
| 284 | name = pkgName |
| 285 | } |
| 286 | name = path.Join(name, baseName) |
| 287 | if sym, exists := sc.sym[name]; !exists { |
| 288 | sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: srcPkgT, path: ipath, scope: sc}} |
| 289 | break |
| 290 | } else if sym.kind == pkgSym && sym.typ.cat == srcPkgT && sym.typ.path == ipath { |
| 291 | // ignore re-import of identical package |
| 292 | break |
| 293 | } |
| 294 | |
| 295 | // redeclaration error |
| 296 | err = n.cfgErrorf("%s redeclared as imported package name", name) |
| 297 | return false |
| 298 | } |
| 299 | } else { |
| 300 | err = n.cfgErrorf("import %q error: %v", ipath, err) |
| 301 | } |
| 302 | |
| 303 | case typeSpec, typeSpecAssign: |
| 304 | if isBlank(n.child[0]) { |
| 305 | err = n.cfgErrorf("cannot use _ as value") |
| 306 | return false |
| 307 | } |
| 308 | typeName := n.child[0].ident |
| 309 | if len(n.child) > 2 { |
| 310 | // Handle a generic type: skip definition as parameter is not instantiated yet. |
| 311 | n.typ = genericOf(nil, typeName, pkgName, withNode(n.child[0]), withScope(sc)) |
| 312 | if _, exists := sc.sym[typeName]; !exists { |
| 313 | sc.sym[typeName] = &symbol{kind: typeSym, node: n} |
| 314 | } |
| 315 | sc.sym[typeName].typ = n.typ |
| 316 | return false |
| 317 | } |
| 318 | var typ *itype |
| 319 | if typ, err = nodeType(interp, sc, n.child[1]); err != nil { |
| 320 | err = nil |
| 321 | revisit = append(revisit, n) |
| 322 | return false |
| 323 | } |
| 324 | |
| 325 | if n.kind == typeSpecAssign { |
| 326 | // Create an aliased type in the current scope |
| 327 | sc.sym[typeName] = &symbol{kind: typeSym, node: n, typ: typ} |
| 328 | n.typ = typ |
| 329 | break |
| 330 | } |
| 331 | |
| 332 | // else we are not an alias (typeSpec) |
| 333 | |
| 334 | switch n.child[1].kind { |
| 335 | case identExpr, selectorExpr: |
| 336 | n.typ = namedOf(typ, pkgName, typeName, withNode(n.child[0]), withScope(sc)) |
| 337 | n.typ.incomplete = typ.incomplete |
| 338 | n.typ.field = typ.field |
| 339 | copy(n.typ.method, typ.method) |
| 340 | default: |
| 341 | n.typ = typ |
| 342 | n.typ.name = typeName |
| 343 | n.typ.path = pkgName |
| 344 | } |
| 345 | n.typ.str = n.typ.path + "." + n.typ.name |
| 346 | |
| 347 | asImportName := path.Join(typeName, baseName) |
| 348 | if _, exists := sc.sym[asImportName]; exists { |
| 349 | // redeclaration error |
| 350 | err = n.cfgErrorf("%s redeclared in this block", typeName) |
| 351 | return false |
| 352 | } |
| 353 | sym, exists := sc.sym[typeName] |
| 354 | if !exists { |
| 355 | sym = &symbol{kind: typeSym, node: n} |
| 356 | sc.sym[typeName] = sym |
| 357 | } else if sym.typ != nil && (len(sym.typ.method) > 0) { |
| 358 | // Type has already been seen as a receiver in a method function |
| 359 | for _, m := range sym.typ.method { |
| 360 | n.typ.addMethod(m) |
| 361 | } |
| 362 | } |
| 363 | sym.typ = n.typ |
| 364 | if !n.typ.isComplete() { |
| 365 | revisit = append(revisit, n) |
| 366 | } |
| 367 | return false |
| 368 | } |
| 369 | return true |
| 370 | }, nil) |
| 371 | |
| 372 | if sc != interp.universe { |
| 373 | sc.pop() |
| 374 | } |
| 375 | return revisit, err |
| 376 | } |
| 377 | |
| 378 | func baseType(t *itype) *itype { |
| 379 | for { |
| 380 | switch t.cat { |
| 381 | case ptrT, linkedT: |
| 382 | t = t.val |
| 383 | default: |
| 384 | return t |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // gtaRetry (re)applies gta until all global constants and types are defined. |
| 390 | func (interp *Interpreter) gtaRetry(nodes []*node, importPath, pkgName string) error { |
| 391 | revisit := []*node{} |
| 392 | for { |
| 393 | for _, n := range nodes { |
| 394 | list, err := interp.gta(n, importPath, importPath, pkgName) |
| 395 | if err != nil { |
| 396 | return err |
| 397 | } |
| 398 | revisit = append(revisit, list...) |
| 399 | } |
| 400 | |
| 401 | if len(revisit) == 0 || equalNodes(nodes, revisit) { |
| 402 | break |
| 403 | } |
| 404 | |
| 405 | nodes = revisit |
| 406 | revisit = []*node{} |
| 407 | } |
| 408 | |
| 409 | if len(revisit) > 0 { |
| 410 | n := revisit[0] |
| 411 | switch n.kind { |
| 412 | case typeSpec, typeSpecAssign: |
| 413 | if err := definedType(n.typ); err != nil { |
| 414 | return err |
| 415 | } |
| 416 | case defineStmt, funcDecl: |
| 417 | if err, ok := n.meta.(error); ok { |
| 418 | return err |
| 419 | } |
| 420 | } |
| 421 | return n.cfgErrorf("constant definition loop") |
| 422 | } |
| 423 | return nil |
| 424 | } |
| 425 | |
| 426 | func definedType(typ *itype) error { |
| 427 | if !typ.incomplete { |
| 428 | return nil |
| 429 | } |
| 430 | switch typ.cat { |
| 431 | case interfaceT, structT: |
| 432 | for _, f := range typ.field { |
| 433 | if err := definedType(f.typ); err != nil { |
| 434 | return err |
| 435 | } |
| 436 | } |
| 437 | case funcT: |
| 438 | for _, t := range typ.arg { |
| 439 | if err := definedType(t); err != nil { |
| 440 | return err |
| 441 | } |
| 442 | } |
| 443 | for _, t := range typ.ret { |
| 444 | if err := definedType(t); err != nil { |
| 445 | return err |
| 446 | } |
| 447 | } |
| 448 | case mapT: |
| 449 | if err := definedType(typ.key); err != nil { |
| 450 | return err |
| 451 | } |
| 452 | fallthrough |
| 453 | case linkedT, arrayT, chanT, chanSendT, chanRecvT, ptrT, variadicT: |
| 454 | if err := definedType(typ.val); err != nil { |
| 455 | return err |
| 456 | } |
| 457 | case nilT: |
| 458 | return typ.node.cfgErrorf("undefined: %s", typ.node.ident) |
| 459 | } |
| 460 | return nil |
| 461 | } |
| 462 | |
| 463 | // equalNodes returns true if two slices of nodes are identical. |
| 464 | func equalNodes(a, b []*node) bool { |
| 465 | if len(a) != len(b) { |
| 466 | return false |
| 467 | } |
| 468 | for i, n := range a { |
| 469 | if n != b[i] { |
| 470 | return false |
| 471 | } |
| 472 | } |
| 473 | return true |
| 474 | } |