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

View as plain text