Source file src/cmd/compile/internal/types/universe.go

     1  // Copyright 2009 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  package types
     6  
     7  import (
     8  	"cmd/compile/internal/base"
     9  	"cmd/internal/src"
    10  )
    11  
    12  var basicTypes = [...]struct {
    13  	name  string
    14  	etype Kind
    15  }{
    16  	{"int8", TINT8},
    17  	{"int16", TINT16},
    18  	{"int32", TINT32},
    19  	{"int64", TINT64},
    20  	{"uint8", TUINT8},
    21  	{"uint16", TUINT16},
    22  	{"uint32", TUINT32},
    23  	{"uint64", TUINT64},
    24  	{"float32", TFLOAT32},
    25  	{"float64", TFLOAT64},
    26  	{"complex64", TCOMPLEX64},
    27  	{"complex128", TCOMPLEX128},
    28  	{"bool", TBOOL},
    29  	{"string", TSTRING},
    30  }
    31  
    32  var typedefs = [...]struct {
    33  	name     string
    34  	etype    Kind
    35  	sameas32 Kind
    36  	sameas64 Kind
    37  }{
    38  	{"int", TINT, TINT32, TINT64},
    39  	{"uint", TUINT, TUINT32, TUINT64},
    40  	{"uintptr", TUINTPTR, TUINT32, TUINT64},
    41  }
    42  
    43  func InitTypes(defTypeName func(sym *Sym, typ *Type) Object) {
    44  	if PtrSize == 0 {
    45  		base.Fatalf("typeinit before betypeinit")
    46  	}
    47  
    48  	SlicePtrOffset = 0
    49  	SliceLenOffset = Rnd(SlicePtrOffset+int64(PtrSize), int64(PtrSize))
    50  	SliceCapOffset = Rnd(SliceLenOffset+int64(PtrSize), int64(PtrSize))
    51  	SliceSize = Rnd(SliceCapOffset+int64(PtrSize), int64(PtrSize))
    52  
    53  	// string is same as slice wo the cap
    54  	StringSize = Rnd(SliceLenOffset+int64(PtrSize), int64(PtrSize))
    55  
    56  	for et := Kind(0); et < NTYPE; et++ {
    57  		SimType[et] = et
    58  	}
    59  
    60  	Types[TANY] = newType(TANY) // note: an old placeholder type, NOT the new builtin 'any' alias for interface{}
    61  	Types[TINTER] = NewInterface(LocalPkg, nil, false)
    62  	CheckSize(Types[TINTER])
    63  
    64  	defBasic := func(kind Kind, pkg *Pkg, name string) *Type {
    65  		typ := newType(kind)
    66  		obj := defTypeName(pkg.Lookup(name), typ)
    67  		typ.sym = obj.Sym()
    68  		typ.nod = obj
    69  		if kind != TANY {
    70  			CheckSize(typ)
    71  		}
    72  		return typ
    73  	}
    74  
    75  	for _, s := range &basicTypes {
    76  		Types[s.etype] = defBasic(s.etype, BuiltinPkg, s.name)
    77  	}
    78  
    79  	for _, s := range &typedefs {
    80  		sameas := s.sameas32
    81  		if PtrSize == 8 {
    82  			sameas = s.sameas64
    83  		}
    84  		SimType[s.etype] = sameas
    85  
    86  		Types[s.etype] = defBasic(s.etype, BuiltinPkg, s.name)
    87  	}
    88  
    89  	// We create separate byte and rune types for better error messages
    90  	// rather than just creating type alias *Sym's for the uint8 and
    91  	// int32  Hence, (bytetype|runtype).Sym.isAlias() is false.
    92  	// TODO(gri) Should we get rid of this special case (at the cost
    93  	// of less informative error messages involving bytes and runes)?
    94  	// NOTE(rsc): No, the error message quality is important.
    95  	// (Alternatively, we could introduce an OTALIAS node representing
    96  	// type aliases, albeit at the cost of having to deal with it everywhere).
    97  	ByteType = defBasic(TUINT8, BuiltinPkg, "byte")
    98  	RuneType = defBasic(TINT32, BuiltinPkg, "rune")
    99  
   100  	// error type
   101  	DeferCheckSize()
   102  	ErrorType = defBasic(TFORW, BuiltinPkg, "error")
   103  	ErrorType.SetUnderlying(makeErrorInterface())
   104  	ResumeCheckSize()
   105  
   106  	// comparable type (interface)
   107  	DeferCheckSize()
   108  	ComparableType = defBasic(TFORW, BuiltinPkg, "comparable")
   109  	ComparableType.SetUnderlying(makeComparableInterface())
   110  	ResumeCheckSize()
   111  
   112  	// any type (interface)
   113  	DeferCheckSize()
   114  	AnyType = defBasic(TFORW, BuiltinPkg, "any")
   115  	AnyType.SetUnderlying(NewInterface(BuiltinPkg, []*Field{}, false))
   116  	ResumeCheckSize()
   117  
   118  	if base.Flag.G == 0 {
   119  		ComparableType.Sym().Def = nil
   120  	}
   121  
   122  	Types[TUNSAFEPTR] = defBasic(TUNSAFEPTR, UnsafePkg, "Pointer")
   123  
   124  	Types[TBLANK] = newType(TBLANK)
   125  	Types[TNIL] = newType(TNIL)
   126  
   127  	// simple aliases
   128  	SimType[TMAP] = TPTR
   129  	SimType[TCHAN] = TPTR
   130  	SimType[TFUNC] = TPTR
   131  	SimType[TUNSAFEPTR] = TPTR
   132  
   133  	for et := TINT8; et <= TUINT64; et++ {
   134  		IsInt[et] = true
   135  	}
   136  	IsInt[TINT] = true
   137  	IsInt[TUINT] = true
   138  	IsInt[TUINTPTR] = true
   139  
   140  	IsFloat[TFLOAT32] = true
   141  	IsFloat[TFLOAT64] = true
   142  
   143  	IsComplex[TCOMPLEX64] = true
   144  	IsComplex[TCOMPLEX128] = true
   145  }
   146  
   147  func makeErrorInterface() *Type {
   148  	sig := NewSignature(NoPkg, FakeRecv(), nil, nil, []*Field{
   149  		NewField(src.NoXPos, nil, Types[TSTRING]),
   150  	})
   151  	method := NewField(src.NoXPos, LocalPkg.Lookup("Error"), sig)
   152  	return NewInterface(NoPkg, []*Field{method}, false)
   153  }
   154  
   155  // makeComparableInterface makes the predefined "comparable" interface in the
   156  // built-in package. It has a unique name, but no methods.
   157  func makeComparableInterface() *Type {
   158  	return NewInterface(NoPkg, nil, false)
   159  }
   160  

View as plain text