Source file src/cmd/compile/internal/types/scope.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import (
     8  	"cmd/compile/internal/base"
     9  	"cmd/internal/src"
    10  )
    11  
    12  // Declaration stack & operations
    13  
    14  var blockgen int32 = 1 // max block number
    15  var Block int32 = 1    // current block number
    16  
    17  // A dsym stores a symbol's shadowed declaration so that it can be
    18  // restored once the block scope ends.
    19  type dsym struct {
    20  	sym        *Sym // sym == nil indicates stack mark
    21  	def        Object
    22  	block      int32
    23  	lastlineno src.XPos // last declaration for diagnostic
    24  }
    25  
    26  // dclstack maintains a stack of shadowed symbol declarations so that
    27  // Popdcl can restore their declarations when a block scope ends.
    28  var dclstack []dsym
    29  
    30  // Pushdcl pushes the current declaration for symbol s (if any) so that
    31  // it can be shadowed by a new declaration within a nested block scope.
    32  func Pushdcl(s *Sym) {
    33  	dclstack = append(dclstack, dsym{
    34  		sym:        s,
    35  		def:        s.Def,
    36  		block:      s.Block,
    37  		lastlineno: s.Lastlineno,
    38  	})
    39  }
    40  
    41  // Popdcl pops the innermost block scope and restores all symbol declarations
    42  // to their previous state.
    43  func Popdcl() {
    44  	for i := len(dclstack); i > 0; i-- {
    45  		d := &dclstack[i-1]
    46  		s := d.sym
    47  		if s == nil {
    48  			// pop stack mark
    49  			Block = d.block
    50  			dclstack = dclstack[:i-1]
    51  			return
    52  		}
    53  
    54  		s.Def = d.def
    55  		s.Block = d.block
    56  		s.Lastlineno = d.lastlineno
    57  
    58  		// Clear dead pointer fields.
    59  		d.sym = nil
    60  		d.def = nil
    61  	}
    62  	base.Fatalf("popdcl: no stack mark")
    63  }
    64  
    65  // Markdcl records the start of a new block scope for declarations.
    66  func Markdcl() {
    67  	dclstack = append(dclstack, dsym{
    68  		sym:   nil, // stack mark
    69  		block: Block,
    70  	})
    71  	blockgen++
    72  	Block = blockgen
    73  }
    74  
    75  func isDclstackValid() bool {
    76  	for _, d := range dclstack {
    77  		if d.sym == nil {
    78  			return false
    79  		}
    80  	}
    81  	return true
    82  }
    83  
    84  // PkgDef returns the definition associated with s at package scope.
    85  func (s *Sym) PkgDef() Object {
    86  	return *s.pkgDefPtr()
    87  }
    88  
    89  // SetPkgDef sets the definition associated with s at package scope.
    90  func (s *Sym) SetPkgDef(n Object) {
    91  	*s.pkgDefPtr() = n
    92  }
    93  
    94  func (s *Sym) pkgDefPtr() *Object {
    95  	// Look for outermost saved declaration, which must be the
    96  	// package scope definition, if present.
    97  	for i := range dclstack {
    98  		d := &dclstack[i]
    99  		if s == d.sym {
   100  			return &d.def
   101  		}
   102  	}
   103  
   104  	// Otherwise, the declaration hasn't been shadowed within a
   105  	// function scope.
   106  	return &s.Def
   107  }
   108  
   109  func CheckDclstack() {
   110  	if !isDclstackValid() {
   111  		base.Fatalf("mark left on the dclstack")
   112  	}
   113  }
   114  

View as plain text