Source file src/go/types/universe.go

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

View as plain text