Source file src/go/types/universe.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/universe.go
     3  
     4  // Copyright 2011 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // This file sets up the universe scope and the unsafe package.
     9  
    10  package types
    11  
    12  import (
    13  	"go/constant"
    14  	"strings"
    15  )
    16  
    17  // The Universe scope contains all predeclared objects of Go.
    18  // It is the outermost scope of any chain of nested scopes.
    19  var Universe *Scope
    20  
    21  // The Unsafe package is the package returned by an importer
    22  // for the import path "unsafe".
    23  var Unsafe *Package
    24  
    25  var (
    26  	universeIota       Object
    27  	universeByte       Type // uint8 alias, but has name "byte"
    28  	universeRune       Type // int32 alias, but has name "rune"
    29  	universeAny        Object
    30  	universeError      Type
    31  	universeComparable Object
    32  )
    33  
    34  // Typ contains the predeclared *Basic types indexed by their
    35  // corresponding BasicKind.
    36  //
    37  // The *Basic type for Typ[Byte] will have the name "uint8".
    38  // Use Universe.Lookup("byte").Type() to obtain the specific
    39  // alias basic type named "byte" (and analogous for "rune").
    40  var Typ = []*Basic{
    41  	Invalid: {Invalid, 0, "invalid type"},
    42  
    43  	Bool:          {Bool, IsBoolean, "bool"},
    44  	Int:           {Int, IsInteger, "int"},
    45  	Int8:          {Int8, IsInteger, "int8"},
    46  	Int16:         {Int16, IsInteger, "int16"},
    47  	Int32:         {Int32, IsInteger, "int32"},
    48  	Int64:         {Int64, IsInteger, "int64"},
    49  	Uint:          {Uint, IsInteger | IsUnsigned, "uint"},
    50  	Uint8:         {Uint8, IsInteger | IsUnsigned, "uint8"},
    51  	Uint16:        {Uint16, IsInteger | IsUnsigned, "uint16"},
    52  	Uint32:        {Uint32, IsInteger | IsUnsigned, "uint32"},
    53  	Uint64:        {Uint64, IsInteger | IsUnsigned, "uint64"},
    54  	Uintptr:       {Uintptr, IsInteger | IsUnsigned, "uintptr"},
    55  	Float32:       {Float32, IsFloat, "float32"},
    56  	Float64:       {Float64, IsFloat, "float64"},
    57  	Complex64:     {Complex64, IsComplex, "complex64"},
    58  	Complex128:    {Complex128, IsComplex, "complex128"},
    59  	String:        {String, IsString, "string"},
    60  	UnsafePointer: {UnsafePointer, 0, "Pointer"},
    61  
    62  	UntypedBool:    {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
    63  	UntypedInt:     {UntypedInt, IsInteger | IsUntyped, "untyped int"},
    64  	UntypedRune:    {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
    65  	UntypedFloat:   {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
    66  	UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
    67  	UntypedString:  {UntypedString, IsString | IsUntyped, "untyped string"},
    68  	UntypedNil:     {UntypedNil, IsUntyped, "untyped nil"},
    69  }
    70  
    71  var aliases = [...]*Basic{
    72  	{Byte, IsInteger | IsUnsigned, "byte"},
    73  	{Rune, IsInteger, "rune"},
    74  }
    75  
    76  func defPredeclaredTypes() {
    77  	for _, t := range Typ {
    78  		def(NewTypeName(nopos, nil, t.name, t))
    79  	}
    80  	for _, t := range aliases {
    81  		def(NewTypeName(nopos, nil, t.name, t))
    82  	}
    83  
    84  	// type any = interface{}
    85  	// Note: don't use &emptyInterface for the type of any. Using a unique
    86  	// pointer allows us to detect any and format it as "any" rather than
    87  	// interface{}, which clarifies user-facing error messages significantly.
    88  	def(NewTypeName(nopos, nil, "any", &Interface{complete: true, tset: &topTypeSet}))
    89  
    90  	// type error interface{ Error() string }
    91  	{
    92  		obj := NewTypeName(nopos, nil, "error", nil)
    93  		obj.setColor(black)
    94  		typ := NewNamed(obj, nil, nil)
    95  
    96  		// error.Error() string
    97  		recv := NewVar(nopos, nil, "", typ)
    98  		res := NewVar(nopos, nil, "", Typ[String])
    99  		sig := NewSignatureType(recv, nil, nil, nil, NewTuple(res), false)
   100  		err := NewFunc(nopos, nil, "Error", sig)
   101  
   102  		// interface{ Error() string }
   103  		ityp := &Interface{methods: []*Func{err}, complete: true}
   104  		computeInterfaceTypeSet(nil, nopos, ityp) // prevent races due to lazy computation of tset
   105  
   106  		typ.SetUnderlying(ityp)
   107  		def(obj)
   108  	}
   109  
   110  	// type comparable interface{} // marked as comparable
   111  	{
   112  		obj := NewTypeName(nopos, nil, "comparable", nil)
   113  		obj.setColor(black)
   114  		typ := NewNamed(obj, nil, nil)
   115  
   116  		// interface{} // marked as comparable
   117  		ityp := &Interface{complete: true, tset: &_TypeSet{nil, allTermlist, true}}
   118  
   119  		typ.SetUnderlying(ityp)
   120  		def(obj)
   121  	}
   122  }
   123  
   124  var predeclaredConsts = [...]struct {
   125  	name string
   126  	kind BasicKind
   127  	val  constant.Value
   128  }{
   129  	{"true", UntypedBool, constant.MakeBool(true)},
   130  	{"false", UntypedBool, constant.MakeBool(false)},
   131  	{"iota", UntypedInt, constant.MakeInt64(0)},
   132  }
   133  
   134  func defPredeclaredConsts() {
   135  	for _, c := range predeclaredConsts {
   136  		def(NewConst(nopos, nil, c.name, Typ[c.kind], c.val))
   137  	}
   138  }
   139  
   140  func defPredeclaredNil() {
   141  	def(&Nil{object{name: "nil", typ: Typ[UntypedNil], color_: black}})
   142  }
   143  
   144  // A builtinId is the id of a builtin function.
   145  type builtinId int
   146  
   147  const (
   148  	// universe scope
   149  	_Append builtinId = iota
   150  	_Cap
   151  	_Clear
   152  	_Close
   153  	_Complex
   154  	_Copy
   155  	_Delete
   156  	_Imag
   157  	_Len
   158  	_Make
   159  	_Max
   160  	_Min
   161  	_New
   162  	_Panic
   163  	_Print
   164  	_Println
   165  	_Real
   166  	_Recover
   167  
   168  	// package unsafe
   169  	_Add
   170  	_Alignof
   171  	_Offsetof
   172  	_Sizeof
   173  	_Slice
   174  	_SliceData
   175  	_String
   176  	_StringData
   177  
   178  	// testing support
   179  	_Assert
   180  	_Trace
   181  )
   182  
   183  var predeclaredFuncs = [...]struct {
   184  	name     string
   185  	nargs    int
   186  	variadic bool
   187  	kind     exprKind
   188  }{
   189  	_Append:  {"append", 1, true, expression},
   190  	_Cap:     {"cap", 1, false, expression},
   191  	_Clear:   {"clear", 1, false, statement},
   192  	_Close:   {"close", 1, false, statement},
   193  	_Complex: {"complex", 2, false, expression},
   194  	_Copy:    {"copy", 2, false, statement},
   195  	_Delete:  {"delete", 2, false, statement},
   196  	_Imag:    {"imag", 1, false, expression},
   197  	_Len:     {"len", 1, false, expression},
   198  	_Make:    {"make", 1, true, expression},
   199  	// To disable max/min, remove the next two lines.
   200  	_Max:     {"max", 1, true, expression},
   201  	_Min:     {"min", 1, true, expression},
   202  	_New:     {"new", 1, false, expression},
   203  	_Panic:   {"panic", 1, false, statement},
   204  	_Print:   {"print", 0, true, statement},
   205  	_Println: {"println", 0, true, statement},
   206  	_Real:    {"real", 1, false, expression},
   207  	_Recover: {"recover", 0, false, statement},
   208  
   209  	_Add:        {"Add", 2, false, expression},
   210  	_Alignof:    {"Alignof", 1, false, expression},
   211  	_Offsetof:   {"Offsetof", 1, false, expression},
   212  	_Sizeof:     {"Sizeof", 1, false, expression},
   213  	_Slice:      {"Slice", 2, false, expression},
   214  	_SliceData:  {"SliceData", 1, false, expression},
   215  	_String:     {"String", 2, false, expression},
   216  	_StringData: {"StringData", 1, false, expression},
   217  
   218  	_Assert: {"assert", 1, false, statement},
   219  	_Trace:  {"trace", 0, true, statement},
   220  }
   221  
   222  func defPredeclaredFuncs() {
   223  	for i := range predeclaredFuncs {
   224  		id := builtinId(i)
   225  		if id == _Assert || id == _Trace {
   226  			continue // only define these in testing environment
   227  		}
   228  		def(newBuiltin(id))
   229  	}
   230  }
   231  
   232  // DefPredeclaredTestFuncs defines the assert and trace built-ins.
   233  // These built-ins are intended for debugging and testing of this
   234  // package only.
   235  func DefPredeclaredTestFuncs() {
   236  	if Universe.Lookup("assert") != nil {
   237  		return // already defined
   238  	}
   239  	def(newBuiltin(_Assert))
   240  	def(newBuiltin(_Trace))
   241  }
   242  
   243  func init() {
   244  	Universe = NewScope(nil, nopos, nopos, "universe")
   245  	Unsafe = NewPackage("unsafe", "unsafe")
   246  	Unsafe.complete = true
   247  
   248  	defPredeclaredTypes()
   249  	defPredeclaredConsts()
   250  	defPredeclaredNil()
   251  	defPredeclaredFuncs()
   252  
   253  	universeIota = Universe.Lookup("iota")
   254  	universeByte = Universe.Lookup("byte").Type()
   255  	universeRune = Universe.Lookup("rune").Type()
   256  	universeAny = Universe.Lookup("any")
   257  	universeError = Universe.Lookup("error").Type()
   258  	universeComparable = Universe.Lookup("comparable")
   259  }
   260  
   261  // Objects with names containing blanks are internal and not entered into
   262  // a scope. Objects with exported names are inserted in the unsafe package
   263  // scope; other objects are inserted in the universe scope.
   264  func def(obj Object) {
   265  	assert(obj.color() == black)
   266  	name := obj.Name()
   267  	if strings.Contains(name, " ") {
   268  		return // nothing to do
   269  	}
   270  	// fix Obj link for named types
   271  	if typ := asNamed(obj.Type()); typ != nil {
   272  		typ.obj = obj.(*TypeName)
   273  	}
   274  	// exported identifiers go into package unsafe
   275  	scope := Universe
   276  	if obj.Exported() {
   277  		scope = Unsafe.scope
   278  		// set Pkg field
   279  		switch obj := obj.(type) {
   280  		case *TypeName:
   281  			obj.pkg = Unsafe
   282  		case *Builtin:
   283  			obj.pkg = Unsafe
   284  		default:
   285  			panic("unreachable")
   286  		}
   287  	}
   288  	if scope.Insert(obj) != nil {
   289  		panic("double declaration of predeclared identifier")
   290  	}
   291  }
   292  

View as plain text