| 1 | package interp |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "reflect" |
| 6 | "strconv" |
| 7 | ) |
| 8 | |
| 9 | // A sKind represents the kind of symbol. |
| 10 | type sKind uint |
| 11 | |
| 12 | // Symbol kinds for the Go interpreter. |
| 13 | const ( |
| 14 | undefSym sKind = iota |
| 15 | binSym // Binary from runtime |
| 16 | bltnSym // Builtin |
| 17 | constSym // Constant |
| 18 | funcSym // Function |
| 19 | labelSym // Label |
| 20 | pkgSym // Package |
| 21 | typeSym // Type |
| 22 | varTypeSym // Variable type (generic) |
| 23 | varSym // Variable |
| 24 | ) |
| 25 | |
| 26 | var symKinds = [...]string{ |
| 27 | undefSym: "undefSym", |
| 28 | binSym: "binSym", |
| 29 | bltnSym: "bltnSym", |
| 30 | constSym: "constSym", |
| 31 | funcSym: "funcSym", |
| 32 | labelSym: "labelSym", |
| 33 | pkgSym: "pkgSym", |
| 34 | typeSym: "typeSym", |
| 35 | varTypeSym: "varTypeSym", |
| 36 | varSym: "varSym", |
| 37 | } |
| 38 | |
| 39 | func (k sKind) String() string { |
| 40 | if k < sKind(len(symKinds)) { |
| 41 | return symKinds[k] |
| 42 | } |
| 43 | return "SymKind(" + strconv.Itoa(int(k)) + ")" |
| 44 | } |
| 45 | |
| 46 | // A symbol represents an interpreter object such as type, constant, var, func, |
| 47 | // label, builtin or binary object. Symbols are defined within a scope. |
| 48 | type symbol struct { |
| 49 | kind sKind |
| 50 | typ *itype // Type of value |
| 51 | node *node // Node value if index is negative |
| 52 | from []*node // list of goto nodes jumping to this label node, or nil |
| 53 | recv *receiver // receiver node value, if sym refers to a method |
| 54 | index int // index of value in frame or -1 |
| 55 | rval reflect.Value // default value (used for constants) |
| 56 | builtin bltnGenerator // Builtin function or nil |
| 57 | global bool // true if symbol is defined in global space |
| 58 | } |
| 59 | |
| 60 | // scope type stores symbols in maps, and frame layout as array of types |
| 61 | // The purposes of scopes are to manage the visibility of each symbol |
| 62 | // and to store the memory frame layout information (type and index in frame) |
| 63 | // at each level (global, package, functions) |
| 64 | // |
| 65 | // scopes are organized in a stack fashion: a first scope (universe) is created |
| 66 | // once at global level, and for each block (package, func, for, etc...), a new |
| 67 | // scope is pushed at entry, and poped at exit. |
| 68 | // |
| 69 | // Nested scopes with the same level value use the same frame: it allows to have |
| 70 | // exactly one frame per function, with a fixed position for each variable (named |
| 71 | // or not), no matter the inner complexity (number of nested blocks in the function) |
| 72 | // |
| 73 | // In symbols, the index value corresponds to the index in scope.types, and at |
| 74 | // execution to the index in frame, created exactly from the types layout. |
| 75 | type scope struct { |
| 76 | anc *scope // ancestor upper scope |
| 77 | child []*scope // included scopes |
| 78 | def *node // function definition node this scope belongs to, or nil |
| 79 | loop *node // loop exit node for break statement |
| 80 | loopRestart *node // loop restart node for continue statement |
| 81 | pkgID string // unique id of package in which scope is defined |
| 82 | pkgName string // package name for the package |
| 83 | types []reflect.Type // frame layout, may be shared by same level scopes |
| 84 | level int // frame level: number of frame indirections to access var during execution |
| 85 | sym map[string]*symbol // map of symbols defined in this current scope |
| 86 | global bool // true if scope refers to global space (single frame for universe and package level scopes) |
| 87 | iota int // iota value in this scope |
| 88 | } |
| 89 | |
| 90 | // push creates a new child scope and chain it to the current one. |
| 91 | func (s *scope) push(indirect bool) *scope { |
| 92 | sc := &scope{anc: s, level: s.level, sym: map[string]*symbol{}} |
| 93 | s.child = append(s.child, sc) |
| 94 | if indirect { |
| 95 | sc.types = []reflect.Type{} |
| 96 | sc.level = s.level + 1 |
| 97 | } else { |
| 98 | // Propagate size, types, def and global as scopes at same level share the same frame. |
| 99 | sc.types = s.types |
| 100 | sc.def = s.def |
| 101 | sc.global = s.global |
| 102 | sc.level = s.level |
| 103 | } |
| 104 | // inherit loop state and pkgID from ancestor |
| 105 | sc.loop, sc.loopRestart, sc.pkgID = s.loop, s.loopRestart, s.pkgID |
| 106 | return sc |
| 107 | } |
| 108 | |
| 109 | func (s *scope) pushBloc() *scope { return s.push(false) } |
| 110 | func (s *scope) pushFunc() *scope { return s.push(true) } |
| 111 | |
| 112 | func (s *scope) pop() *scope { |
| 113 | if s.level == s.anc.level { |
| 114 | // Propagate size and types, as scopes at same level share the same frame. |
| 115 | s.anc.types = s.types |
| 116 | } |
| 117 | return s.anc |
| 118 | } |
| 119 | |
| 120 | func (s *scope) upperLevel() *scope { |
| 121 | level := s.level |
| 122 | for s != nil && s.level == level { |
| 123 | s = s.anc |
| 124 | } |
| 125 | return s |
| 126 | } |
| 127 | |
| 128 | // lookup searches for a symbol in the current scope, and upper ones if not found |
| 129 | // it returns the symbol, the number of indirections level from the current scope |
| 130 | // and status (false if no result). |
| 131 | func (s *scope) lookup(ident string) (*symbol, int, bool) { |
| 132 | level := s.level |
| 133 | for { |
| 134 | if sym, ok := s.sym[ident]; ok { |
| 135 | if sym.global { |
| 136 | return sym, globalFrame, true |
| 137 | } |
| 138 | return sym, level - s.level, true |
| 139 | } |
| 140 | if s.anc == nil { |
| 141 | break |
| 142 | } |
| 143 | s = s.anc |
| 144 | } |
| 145 | return nil, 0, false |
| 146 | } |
| 147 | |
| 148 | func (s *scope) isRedeclared(n *node) bool { |
| 149 | if !isNewDefine(n, s) { |
| 150 | return false |
| 151 | } |
| 152 | // Existing symbol in the scope indicates a redeclaration. |
| 153 | return s.sym[n.ident] != nil |
| 154 | } |
| 155 | |
| 156 | func (s *scope) rangeChanType(n *node) *itype { |
| 157 | if sym, _, found := s.lookup(n.child[1].ident); found { |
| 158 | if t := sym.typ; len(n.child) == 3 && t != nil && (t.cat == chanT || t.cat == chanRecvT) { |
| 159 | return t |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | c := n.child[1] |
| 164 | if c.typ == nil { |
| 165 | return nil |
| 166 | } |
| 167 | switch { |
| 168 | case c.typ.cat == chanT, c.typ.cat == chanRecvT: |
| 169 | return c.typ |
| 170 | case c.typ.cat == valueT && c.typ.rtype.Kind() == reflect.Chan: |
| 171 | dir := chanSendRecv |
| 172 | switch c.typ.rtype.ChanDir() { |
| 173 | case reflect.RecvDir: |
| 174 | dir = chanRecv |
| 175 | case reflect.SendDir: |
| 176 | dir = chanSend |
| 177 | } |
| 178 | return chanOf(valueTOf(c.typ.rtype.Elem()), dir) |
| 179 | } |
| 180 | |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | // fixType returns the input type, or a valid default type for untyped constant. |
| 185 | func (s *scope) fixType(t *itype) *itype { |
| 186 | if !t.untyped || t.cat != valueT { |
| 187 | return t |
| 188 | } |
| 189 | switch typ := t.TypeOf(); typ.Kind() { |
| 190 | case reflect.Int64: |
| 191 | return s.getType("int") |
| 192 | case reflect.Uint64: |
| 193 | return s.getType("uint") |
| 194 | case reflect.Float64: |
| 195 | return s.getType("float64") |
| 196 | case reflect.Complex128: |
| 197 | return s.getType("complex128") |
| 198 | } |
| 199 | return t |
| 200 | } |
| 201 | |
| 202 | func (s *scope) getType(ident string) *itype { |
| 203 | var t *itype |
| 204 | if sym, _, found := s.lookup(ident); found { |
| 205 | if sym.kind == typeSym { |
| 206 | t = sym.typ |
| 207 | } |
| 208 | } |
| 209 | return t |
| 210 | } |
| 211 | |
| 212 | // add adds a type to the scope types array, and returns its index. |
| 213 | func (s *scope) add(typ *itype) (index int) { |
| 214 | if typ == nil { |
| 215 | log.Panic("nil type") |
| 216 | } |
| 217 | index = len(s.types) |
| 218 | t := typ.frameType() |
| 219 | if t == nil { |
| 220 | log.Panic("nil reflect type") |
| 221 | } |
| 222 | s.types = append(s.types, t) |
| 223 | return |
| 224 | } |
| 225 | |
| 226 | func (interp *Interpreter) initScopePkg(pkgID, pkgName string) *scope { |
| 227 | sc := interp.universe |
| 228 | |
| 229 | interp.mutex.Lock() |
| 230 | if _, ok := interp.scopes[pkgID]; !ok { |
| 231 | interp.scopes[pkgID] = sc.pushBloc() |
| 232 | } |
| 233 | sc = interp.scopes[pkgID] |
| 234 | sc.pkgID = pkgID |
| 235 | sc.pkgName = pkgName |
| 236 | interp.mutex.Unlock() |
| 237 | return sc |
| 238 | } |
| 239 | |
| 240 | // Globals returns a map of global variables and constants in the main package. |
| 241 | func (interp *Interpreter) Globals() map[string]reflect.Value { |
| 242 | syms := map[string]reflect.Value{} |
| 243 | interp.mutex.RLock() |
| 244 | defer interp.mutex.RUnlock() |
| 245 | |
| 246 | v, ok := interp.srcPkg["main"] |
| 247 | if !ok { |
| 248 | return syms |
| 249 | } |
| 250 | |
| 251 | for n, s := range v { |
| 252 | switch s.kind { |
| 253 | case constSym: |
| 254 | syms[n] = s.rval |
| 255 | case varSym: |
| 256 | syms[n] = interp.frame.data[s.index] |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return syms |
| 261 | } |