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  	universeAnyNoAlias *TypeName
    30  	universeAnyAlias   *TypeName
    31  	universeError      Type
    32  	universeComparable Object
    33  )
    34  
    35  // Typ contains the predeclared *Basic types indexed by their
    36  // corresponding BasicKind.
    37  //
    38  // The *Basic type for Typ[Byte] will have the name "uint8".
    39  // Use Universe.Lookup("byte").Type() to obtain the specific
    40  // alias basic type named "byte" (and analogous for "rune").
    41  var Typ = []*Basic{
    42  	Invalid: {Invalid, 0, "invalid type"},
    43  
    44  	Bool:          {Bool, IsBoolean, "bool"},
    45  	Int:           {Int, IsInteger, "int"},
    46  	Int8:          {Int8, IsInteger, "int8"},
    47  	Int16:         {Int16, IsInteger, "int16"},
    48  	Int32:         {Int32, IsInteger, "int32"},
    49  	Int64:         {Int64, IsInteger, "int64"},
    50  	Uint:          {Uint, IsInteger | IsUnsigned, "uint"},
    51  	Uint8:         {Uint8, IsInteger | IsUnsigned, "uint8"},
    52  	Uint16:        {Uint16, IsInteger | IsUnsigned, "uint16"},
    53  	Uint32:        {Uint32, IsInteger | IsUnsigned, "uint32"},
    54  	Uint64:        {Uint64, IsInteger | IsUnsigned, "uint64"},
    55  	Uintptr:       {Uintptr, IsInteger | IsUnsigned, "uintptr"},
    56  	Float32:       {Float32, IsFloat, "float32"},
    57  	Float64:       {Float64, IsFloat, "float64"},
    58  	Complex64:     {Complex64, IsComplex, "complex64"},
    59  	Complex128:    {Complex128, IsComplex, "complex128"},
    60  	String:        {String, IsString, "string"},
    61  	UnsafePointer: {UnsafePointer, 0, "Pointer"},
    62  
    63  	UntypedBool:    {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
    64  	UntypedInt:     {UntypedInt, IsInteger | IsUntyped, "untyped int"},
    65  	UntypedRune:    {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
    66  	UntypedFloat:   {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
    67  	UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
    68  	UntypedString:  {UntypedString, IsString | IsUntyped, "untyped string"},
    69  	UntypedNil:     {UntypedNil, IsUntyped, "untyped nil"},
    70  }
    71  
    72  var basicAliases = [...]*Basic{
    73  	{Byte, IsInteger | IsUnsigned, "byte"},
    74  	{Rune, IsInteger, "rune"},
    75  }
    76  
    77  func defPredeclaredTypes() {
    78  	for _, t := range Typ {
    79  		def(NewTypeName(nopos, nil, t.name, t))
    80  	}
    81  	for _, t := range basicAliases {
    82  		def(NewTypeName(nopos, nil, t.name, t))
    83  	}
    84  
    85  	// type any = interface{}
    86  	//
    87  	// Implement two representations of any: one for the legacy gotypesalias=0,
    88  	// and one for gotypesalias=1. This is necessary for consistent
    89  	// representation of interface aliases during type checking, and is
    90  	// implemented via hijacking [Scope.Lookup] for the [Universe] scope.
    91  	//
    92  	// Both representations use the same distinguished pointer for their RHS
    93  	// interface type, allowing us to detect any (even with the legacy
    94  	// representation), and format it as "any" rather than interface{}, which
    95  	// clarifies user-facing error messages significantly.
    96  	//
    97  	// TODO(rfindley): once the gotypesalias GODEBUG variable is obsolete (and we
    98  	// consistently use the Alias node), we should be able to clarify user facing
    99  	// error messages without using a distinguished pointer for the any
   100  	// interface.
   101  	{
   102  		universeAnyNoAlias = NewTypeName(nopos, nil, "any", &Interface{complete: true, tset: &topTypeSet})
   103  		universeAnyNoAlias.setColor(black)
   104  		// ensure that the any TypeName reports a consistent Parent, after
   105  		// hijacking Universe.Lookup with gotypesalias=0.
   106  		universeAnyNoAlias.setParent(Universe)
   107  
   108  		// It shouldn't matter which representation of any is actually inserted
   109  		// into the Universe, but we lean toward the future and insert the Alias
   110  		// representation.
   111  		universeAnyAlias = NewTypeName(nopos, nil, "any", nil)
   112  		universeAnyAlias.setColor(black)
   113  		_ = NewAlias(universeAnyAlias, universeAnyNoAlias.Type().Underlying()) // Link TypeName and Alias
   114  		def(universeAnyAlias)
   115  	}
   116  
   117  	// type error interface{ Error() string }
   118  	{
   119  		obj := NewTypeName(nopos, nil, "error", nil)
   120  		obj.setColor(black)
   121  		typ := NewNamed(obj, nil, nil)
   122  
   123  		// error.Error() string
   124  		recv := NewVar(nopos, nil, "", typ)
   125  		res := NewVar(nopos, nil, "", Typ[String])
   126  		sig := NewSignatureType(recv, nil, nil, nil, NewTuple(res), false)
   127  		err := NewFunc(nopos, nil, "Error", sig)
   128  
   129  		// interface{ Error() string }
   130  		ityp := &Interface{methods: []*Func{err}, complete: true}
   131  		computeInterfaceTypeSet(nil, nopos, ityp) // prevent races due to lazy computation of tset
   132  
   133  		typ.SetUnderlying(ityp)
   134  		def(obj)
   135  	}
   136  
   137  	// type comparable interface{} // marked as comparable
   138  	{
   139  		obj := NewTypeName(nopos, nil, "comparable", nil)
   140  		obj.setColor(black)
   141  		typ := NewNamed(obj, nil, nil)
   142  
   143  		// interface{} // marked as comparable
   144  		ityp := &Interface{complete: true, tset: &_TypeSet{nil, allTermlist, true}}
   145  
   146  		typ.SetUnderlying(ityp)
   147  		def(obj)
   148  	}
   149  }
   150  
   151  var predeclaredConsts = [...]struct {
   152  	name string
   153  	kind BasicKind
   154  	val  constant.Value
   155  }{
   156  	{"true", UntypedBool, constant.MakeBool(true)},
   157  	{"false", UntypedBool, constant.MakeBool(false)},
   158  	{"iota", UntypedInt, constant.MakeInt64(0)},
   159  }
   160  
   161  func defPredeclaredConsts() {
   162  	for _, c := range predeclaredConsts {
   163  		def(NewConst(nopos, nil, c.name, Typ[c.kind], c.val))
   164  	}
   165  }
   166  
   167  func defPredeclaredNil() {
   168  	def(&Nil{object{name: "nil", typ: Typ[UntypedNil], color_: black}})
   169  }
   170  
   171  // A builtinId is the id of a builtin function.
   172  type builtinId int
   173  
   174  const (
   175  	// universe scope
   176  	_Append builtinId = iota
   177  	_Cap
   178  	_Clear
   179  	_Close
   180  	_Complex
   181  	_Copy
   182  	_Delete
   183  	_Imag
   184  	_Len
   185  	_Make
   186  	_Max
   187  	_Min
   188  	_New
   189  	_Panic
   190  	_Print
   191  	_Println
   192  	_Real
   193  	_Recover
   194  
   195  	// package unsafe
   196  	_Add
   197  	_Alignof
   198  	_Offsetof
   199  	_Sizeof
   200  	_Slice
   201  	_SliceData
   202  	_String
   203  	_StringData
   204  
   205  	// testing support
   206  	_Assert
   207  	_Trace
   208  )
   209  
   210  var predeclaredFuncs = [...]struct {
   211  	name     string
   212  	nargs    int
   213  	variadic bool
   214  	kind     exprKind
   215  }{
   216  	_Append:  {"append", 1, true, expression},
   217  	_Cap:     {"cap", 1, false, expression},
   218  	_Clear:   {"clear", 1, false, statement},
   219  	_Close:   {"close", 1, false, statement},
   220  	_Complex: {"complex", 2, false, expression},
   221  	_Copy:    {"copy", 2, false, statement},
   222  	_Delete:  {"delete", 2, false, statement},
   223  	_Imag:    {"imag", 1, false, expression},
   224  	_Len:     {"len", 1, false, expression},
   225  	_Make:    {"make", 1, true, expression},
   226  	// To disable max/min, remove the next two lines.
   227  	_Max:     {"max", 1, true, expression},
   228  	_Min:     {"min", 1, true, expression},
   229  	_New:     {"new", 1, false, expression},
   230  	_Panic:   {"panic", 1, false, statement},
   231  	_Print:   {"print", 0, true, statement},
   232  	_Println: {"println", 0, true, statement},
   233  	_Real:    {"real", 1, false, expression},
   234  	_Recover: {"recover", 0, false, statement},
   235  
   236  	_Add:        {"Add", 2, false, expression},
   237  	_Alignof:    {"Alignof", 1, false, expression},
   238  	_Offsetof:   {"Offsetof", 1, false, expression},
   239  	_Sizeof:     {"Sizeof", 1, false, expression},
   240  	_Slice:      {"Slice", 2, false, expression},
   241  	_SliceData:  {"SliceData", 1, false, expression},
   242  	_String:     {"String", 2, false, expression},
   243  	_StringData: {"StringData", 1, false, expression},
   244  
   245  	_Assert: {"assert", 1, false, statement},
   246  	_Trace:  {"trace", 0, true, statement},
   247  }
   248  
   249  func defPredeclaredFuncs() {
   250  	for i := range predeclaredFuncs {
   251  		id := builtinId(i)
   252  		if id == _Assert || id == _Trace {
   253  			continue // only define these in testing environment
   254  		}
   255  		def(newBuiltin(id))
   256  	}
   257  }
   258  
   259  // DefPredeclaredTestFuncs defines the assert and trace built-ins.
   260  // These built-ins are intended for debugging and testing of this
   261  // package only.
   262  func DefPredeclaredTestFuncs() {
   263  	if Universe.Lookup("assert") != nil {
   264  		return // already defined
   265  	}
   266  	def(newBuiltin(_Assert))
   267  	def(newBuiltin(_Trace))
   268  }
   269  
   270  func init() {
   271  	Universe = NewScope(nil, nopos, nopos, "universe")
   272  	Unsafe = NewPackage("unsafe", "unsafe")
   273  	Unsafe.complete = true
   274  
   275  	defPredeclaredTypes()
   276  	defPredeclaredConsts()
   277  	defPredeclaredNil()
   278  	defPredeclaredFuncs()
   279  
   280  	universeIota = Universe.Lookup("iota")
   281  	universeByte = Universe.Lookup("byte").Type()
   282  	universeRune = Universe.Lookup("rune").Type()
   283  	universeError = Universe.Lookup("error").Type()
   284  	universeComparable = Universe.Lookup("comparable")
   285  }
   286  
   287  // Objects with names containing blanks are internal and not entered into
   288  // a scope. Objects with exported names are inserted in the unsafe package
   289  // scope; other objects are inserted in the universe scope.
   290  func def(obj Object) {
   291  	assert(obj.color() == black)
   292  	name := obj.Name()
   293  	if strings.Contains(name, " ") {
   294  		return // nothing to do
   295  	}
   296  	// fix Obj link for named types
   297  	if typ := asNamed(obj.Type()); typ != nil {
   298  		typ.obj = obj.(*TypeName)
   299  	}
   300  	// exported identifiers go into package unsafe
   301  	scope := Universe
   302  	if obj.Exported() {
   303  		scope = Unsafe.scope
   304  		// set Pkg field
   305  		switch obj := obj.(type) {
   306  		case *TypeName:
   307  			obj.pkg = Unsafe
   308  		case *Builtin:
   309  			obj.pkg = Unsafe
   310  		default:
   311  			panic("unreachable")
   312  		}
   313  	}
   314  	if scope.Insert(obj) != nil {
   315  		panic("double declaration of predeclared identifier")
   316  	}
   317  }
   318  

View as plain text