modernc.org/ccgo/v3@v3.16.14/lib/etc.go (about)

     1  // Copyright 2020 The CCGO 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 ccgo // import "modernc.org/ccgo/v3/lib"
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"math/big"
    11  
    12  	"modernc.org/cc/v3"
    13  )
    14  
    15  var (
    16  	reservedNames = map[string]bool{
    17  		"bool":        false, // ccgo can use
    18  		"break":       true,  // keyword
    19  		"case":        true,  // keyword
    20  		"chan":        true,  // keyword
    21  		"const":       true,  // keyword
    22  		"continue":    true,  // keyword
    23  		"default":     true,  // keyword
    24  		"defer":       true,  // keyword
    25  		"else":        true,  // keyword
    26  		"fallthrough": true,  // keyword
    27  		"false":       false, // ccgo can use
    28  		"float32":     false, // ccgo can use
    29  		"float64":     false, // ccgo can use
    30  		"for":         true,  // keyword
    31  		"func":        true,  // keyword
    32  		"go":          true,  // keyword
    33  		"goto":        true,  // keyword
    34  		"if":          true,  // keyword
    35  		"import":      true,  // keyword
    36  		"init":        false, // special name
    37  		"int16":       false, // ccgo can use
    38  		"int32":       false, // ccgo can use
    39  		"int64":       false, // ccgo can use
    40  		"int8":        false, // ccgo can use
    41  		"interface":   true,  // keyword
    42  		"map":         true,  // keyword
    43  		"math":        false, // package name
    44  		"nil":         false, // ccgo can use
    45  		"package":     true,  // keyword
    46  		"range":       true,  // keyword
    47  		"return":      true,  // keyword
    48  		"select":      true,  // keyword
    49  		"struct":      true,  // keyword
    50  		"switch":      true,  // keyword
    51  		"true":        false, // ccgo can use
    52  		"type":        true,  // keyword
    53  		"types":       false, // package name
    54  		"uint16":      false, // ccgo can use
    55  		"uint32":      false, // ccgo can use
    56  		"uint64":      false, // ccgo can use
    57  		"uint8":       false, // ccgo can use
    58  		"uintptr":     false, // ccgo can use
    59  		"unsafe":      false, // package name
    60  		"var":         true,  // keyword
    61  	}
    62  
    63  	reservedIds []cc.StringID
    64  
    65  	maxInt32  = big.NewInt(math.MaxInt32)
    66  	maxInt64  = big.NewInt(math.MaxInt64)
    67  	maxUint32 = big.NewInt(math.MaxUint32)
    68  	maxUint64 = big.NewInt(0).SetUint64(math.MaxUint64)
    69  	minInt32  = big.NewInt(math.MinInt32)
    70  	minInt64  = big.NewInt(math.MinInt64)
    71  )
    72  
    73  func init() {
    74  	for k := range reservedNames {
    75  		reservedIds = append(reservedIds, cc.String(k))
    76  	}
    77  }
    78  
    79  type scope map[cc.StringID]int32
    80  
    81  func newScope() scope {
    82  	s := scope{}
    83  	for _, k := range reservedIds {
    84  		s[k] = 0
    85  	}
    86  	return s
    87  }
    88  
    89  func (s scope) take(t cc.StringID) string {
    90  	if t == 0 {
    91  		panic(todo("internal error"))
    92  	}
    93  
    94  	n, ok := s[t]
    95  	if !ok {
    96  		s[t] = 0
    97  		return t.String()
    98  	}
    99  
   100  	for {
   101  		n++
   102  		s[t] = n
   103  		r := fmt.Sprintf("%s%d", t, n)
   104  		id := cc.String(r)
   105  		if _, ok := s[id]; !ok {
   106  			s[id] = 0
   107  			return r
   108  		}
   109  	}
   110  }