github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/go_generics/globals/scope.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package globals 16 17 import ( 18 "go/token" 19 ) 20 21 // SymKind specifies the kind of a global symbol. For example, a variable, const 22 // function, etc. 23 type SymKind int 24 25 // Constants for different kinds of symbols. 26 const ( 27 KindUnknown SymKind = iota 28 KindImport 29 KindType 30 KindVar 31 KindConst 32 KindFunction 33 KindReceiver 34 KindParameter 35 KindResult 36 KindTag 37 ) 38 39 type symbol struct { 40 kind SymKind 41 pos token.Pos 42 scope *scope 43 } 44 45 type scope struct { 46 outer *scope 47 syms map[string]*symbol 48 } 49 50 func newScope(outer *scope) *scope { 51 return &scope{ 52 outer: outer, 53 syms: make(map[string]*symbol), 54 } 55 } 56 57 func (s *scope) isGlobal() bool { 58 return s.outer == nil 59 } 60 61 func (s *scope) lookup(n string) *symbol { 62 return s.syms[n] 63 } 64 65 func (s *scope) deepLookup(n string) *symbol { 66 for x := s; x != nil; x = x.outer { 67 if sym := x.lookup(n); sym != nil { 68 return sym 69 } 70 } 71 return nil 72 } 73 74 func (s *scope) add(name string, kind SymKind, pos token.Pos) { 75 if s.syms[name] != nil { 76 return 77 } 78 79 s.syms[name] = &symbol{ 80 kind: kind, 81 pos: pos, 82 scope: s, 83 } 84 }