Source file src/cmd/compile/internal/types/size.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  	"math"
     9  	"slices"
    10  
    11  	"cmd/compile/internal/base"
    12  	"cmd/internal/src"
    13  	"internal/buildcfg"
    14  	"internal/types/errors"
    15  )
    16  
    17  var PtrSize int
    18  
    19  var RegSize int
    20  
    21  // Slices in the runtime are represented by three components:
    22  //
    23  //	type slice struct {
    24  //		ptr unsafe.Pointer
    25  //		len int
    26  //		cap int
    27  //	}
    28  //
    29  // Strings in the runtime are represented by two components:
    30  //
    31  //	type string struct {
    32  //		ptr unsafe.Pointer
    33  //		len int
    34  //	}
    35  //
    36  // These variables are the offsets of fields and sizes of these structs.
    37  var (
    38  	SlicePtrOffset int64
    39  	SliceLenOffset int64
    40  	SliceCapOffset int64
    41  
    42  	SliceSize  int64
    43  	StringSize int64
    44  )
    45  
    46  var SkipSizeForTracing bool
    47  
    48  // typePos returns the position associated with t.
    49  // This is where t was declared or where it appeared as a type expression.
    50  func typePos(t *Type) src.XPos {
    51  	if pos := t.Pos(); pos.IsKnown() {
    52  		return pos
    53  	}
    54  	base.Fatalf("bad type: %v", t)
    55  	panic("unreachable")
    56  }
    57  
    58  // MaxWidth is the maximum size of a value on the target architecture.
    59  var MaxWidth int64
    60  
    61  // CalcSizeDisabled indicates whether it is safe
    62  // to calculate Types' widths and alignments. See CalcSize.
    63  var CalcSizeDisabled bool
    64  
    65  // machine size and rounding alignment is dictated around
    66  // the size of a pointer, set in gc.Main (see ../gc/main.go).
    67  var defercalc int
    68  
    69  // RoundUp rounds o to a multiple of r, r is a power of 2.
    70  func RoundUp(o int64, r int64) int64 {
    71  	if r < 1 || r > 16 || r&(r-1) != 0 {
    72  		base.Fatalf("Round %d", r)
    73  	}
    74  	return (o + r - 1) &^ (r - 1)
    75  }
    76  
    77  // expandiface computes the method set for interface type t by
    78  // expanding embedded interfaces.
    79  func expandiface(t *Type) {
    80  	seen := make(map[*Sym]*Field)
    81  	var methods []*Field
    82  
    83  	addMethod := func(m *Field, explicit bool) {
    84  		switch prev := seen[m.Sym]; {
    85  		case prev == nil:
    86  			seen[m.Sym] = m
    87  		case !explicit && Identical(m.Type, prev.Type):
    88  			return
    89  		default:
    90  			base.ErrorfAt(m.Pos, errors.DuplicateDecl, "duplicate method %s", m.Sym.Name)
    91  		}
    92  		methods = append(methods, m)
    93  	}
    94  
    95  	{
    96  		methods := t.Methods()
    97  		slices.SortStableFunc(methods, func(a, b *Field) int {
    98  			// Sort embedded types by type name (if any).
    99  			if a.Sym == nil && b.Sym == nil {
   100  				return CompareSyms(a.Type.Sym(), b.Type.Sym())
   101  			}
   102  
   103  			// Sort methods before embedded types.
   104  			if a.Sym == nil {
   105  				return -1
   106  			} else if b.Sym == nil {
   107  				return +1
   108  			}
   109  
   110  			// Sort methods by symbol name.
   111  			return CompareSyms(a.Sym, b.Sym)
   112  		})
   113  	}
   114  
   115  	for _, m := range t.Methods() {
   116  		if m.Sym == nil {
   117  			continue
   118  		}
   119  
   120  		CheckSize(m.Type)
   121  		addMethod(m, true)
   122  	}
   123  
   124  	for _, m := range t.Methods() {
   125  		if m.Sym != nil || m.Type == nil {
   126  			continue
   127  		}
   128  
   129  		// In 1.18, embedded types can be anything. In Go 1.17, we disallow
   130  		// embedding anything other than interfaces. This requirement was caught
   131  		// by types2 already, so allow non-interface here.
   132  		if !m.Type.IsInterface() {
   133  			continue
   134  		}
   135  
   136  		// Embedded interface: duplicate all methods
   137  		// and add to t's method set.
   138  		for _, t1 := range m.Type.AllMethods() {
   139  			f := NewField(m.Pos, t1.Sym, t1.Type)
   140  			addMethod(f, false)
   141  
   142  			// Clear position after typechecking, for consistency with types2.
   143  			f.Pos = src.NoXPos
   144  		}
   145  
   146  		// Clear position after typechecking, for consistency with types2.
   147  		m.Pos = src.NoXPos
   148  	}
   149  
   150  	slices.SortFunc(methods, CompareFields)
   151  
   152  	if int64(len(methods)) >= MaxWidth/int64(PtrSize) {
   153  		base.ErrorfAt(typePos(t), 0, "interface too large")
   154  	}
   155  	for i, m := range methods {
   156  		m.Offset = int64(i) * int64(PtrSize)
   157  	}
   158  
   159  	t.SetAllMethods(methods)
   160  }
   161  
   162  // calcStructOffset computes the offsets of a sequence of fields,
   163  // starting at the given offset. It returns the resulting offset and
   164  // maximum field alignment.
   165  func calcStructOffset(t *Type, fields []*Field, offset int64) int64 {
   166  	for _, f := range fields {
   167  		CalcSize(f.Type)
   168  		offset = RoundUp(offset, int64(f.Type.align))
   169  
   170  		if t.IsStruct() { // param offsets depend on ABI
   171  			f.Offset = offset
   172  
   173  			// If type T contains a field F marked as not-in-heap,
   174  			// then T must also be a not-in-heap type. Otherwise,
   175  			// you could heap allocate T and then get a pointer F,
   176  			// which would be a heap pointer to a not-in-heap type.
   177  			if f.Type.NotInHeap() {
   178  				t.SetNotInHeap(true)
   179  			}
   180  		}
   181  
   182  		offset += f.Type.width
   183  
   184  		maxwidth := MaxWidth
   185  		// On 32-bit systems, reflect tables impose an additional constraint
   186  		// that each field start offset must fit in 31 bits.
   187  		if maxwidth < 1<<32 {
   188  			maxwidth = 1<<31 - 1
   189  		}
   190  		// Only apply the MaxWidth constraint when t is a struct; skip it when
   191  		// calculating function argument offsets.
   192  		if t.IsStruct() && offset >= maxwidth {
   193  			// Use the field position if the struct position is unknown.
   194  			// Presumably this can happen for unnamed struct types since
   195  			// the struct position is its object position. But it appears
   196  			// to also happen for named struct types at the moment, so this
   197  			// is a work-around for those cases.
   198  			// Caused an internal compiler error; see go.dev/issue/81241.
   199  			//
   200  			// TODO: need to investigate why named struct types have a
   201  			//       nil Object which in turn produces a nil position.
   202  			pos := t.Pos()
   203  			if !pos.IsKnown() {
   204  				pos = f.Pos
   205  			}
   206  			base.ErrorfAt(pos, 0, "type %L too large", t)
   207  			offset = 8 // small but nonzero
   208  		}
   209  	}
   210  
   211  	return offset
   212  }
   213  
   214  func isAtomicStdPkg(p *Pkg) bool {
   215  	if p.Prefix == `""` {
   216  		panic("bad package prefix")
   217  	}
   218  	return p.Prefix == "sync/atomic" || p.Prefix == "internal/runtime/atomic"
   219  }
   220  
   221  // CalcSize calculates and stores the size, alignment, eq/hash algorithm,
   222  // and ptrBytes for t.
   223  // If CalcSizeDisabled is set, and the size/alignment
   224  // have not already been calculated, it calls Fatal.
   225  // This is used to prevent data races in the back end.
   226  func CalcSize(t *Type) {
   227  	// Calling CalcSize when typecheck tracing enabled is not safe.
   228  	// See issue #33658.
   229  	if base.EnableTrace && SkipSizeForTracing {
   230  		return
   231  	}
   232  	if PtrSize == 0 {
   233  		// Assume this is a test.
   234  		return
   235  	}
   236  
   237  	if t == nil {
   238  		return
   239  	}
   240  
   241  	if t.width == -2 {
   242  		t.width = 0
   243  		t.align = 1
   244  		base.Fatalf("invalid recursive type %v", t)
   245  		return
   246  	}
   247  
   248  	if t.widthCalculated() {
   249  		return
   250  	}
   251  
   252  	if CalcSizeDisabled {
   253  		base.Fatalf("width not calculated: %v", t)
   254  	}
   255  
   256  	// defer CheckSize calls until after we're done
   257  	DeferCheckSize()
   258  
   259  	lno := base.Pos
   260  	if pos := t.Pos(); pos.IsKnown() {
   261  		base.Pos = pos
   262  	}
   263  
   264  	t.width = -2
   265  	t.align = 0  // 0 means use t.Width, below
   266  	t.alg = AMEM // default
   267  	// default t.ptrBytes is 0.
   268  	if t.Noalg() {
   269  		t.setAlg(ANOALG)
   270  	}
   271  
   272  	et := t.Kind()
   273  	switch et {
   274  	case TFUNC, TCHAN, TMAP, TSTRING:
   275  		break
   276  
   277  	// SimType == 0 during bootstrap
   278  	default:
   279  		if SimType[t.Kind()] != 0 {
   280  			et = SimType[t.Kind()]
   281  		}
   282  	}
   283  
   284  	var w int64
   285  	switch et {
   286  	default:
   287  		base.Fatalf("CalcSize: unknown type: %v", t)
   288  
   289  	// compiler-specific stuff
   290  	case TINT8, TUINT8, TBOOL:
   291  		// bool is int8
   292  		w = 1
   293  		t.intRegs = 1
   294  
   295  	case TINT16, TUINT16:
   296  		w = 2
   297  		t.intRegs = 1
   298  
   299  	case TINT32, TUINT32:
   300  		w = 4
   301  		t.intRegs = 1
   302  
   303  	case TINT64, TUINT64:
   304  		w = 8
   305  		t.align = uint8(RegSize)
   306  		t.intRegs = uint8(8 / RegSize)
   307  
   308  	case TFLOAT32:
   309  		w = 4
   310  		t.floatRegs = 1
   311  		t.setAlg(AFLOAT32)
   312  
   313  	case TFLOAT64:
   314  		w = 8
   315  		t.align = uint8(RegSize)
   316  		t.floatRegs = 1
   317  		t.setAlg(AFLOAT64)
   318  
   319  	case TCOMPLEX64:
   320  		w = 8
   321  		t.align = 4
   322  		t.floatRegs = 2
   323  		t.setAlg(ACPLX64)
   324  
   325  	case TCOMPLEX128:
   326  		w = 16
   327  		t.align = uint8(RegSize)
   328  		t.floatRegs = 2
   329  		t.setAlg(ACPLX128)
   330  
   331  	case TPTR:
   332  		w = int64(PtrSize)
   333  		t.intRegs = 1
   334  		CheckSize(t.Elem())
   335  		t.ptrBytes = int64(PtrSize) // See PtrDataSize
   336  
   337  	case TUNSAFEPTR:
   338  		w = int64(PtrSize)
   339  		t.intRegs = 1
   340  		t.ptrBytes = int64(PtrSize)
   341  
   342  	case TINTER: // implemented as 2 pointers
   343  		w = 2 * int64(PtrSize)
   344  		t.align = uint8(PtrSize)
   345  		t.intRegs = 2
   346  		expandiface(t)
   347  		if len(t.allMethods.Slice()) == 0 {
   348  			t.setAlg(ANILINTER)
   349  		} else {
   350  			t.setAlg(AINTER)
   351  		}
   352  		t.ptrBytes = int64(2 * PtrSize)
   353  
   354  	case TCHAN: // implemented as pointer
   355  		w = int64(PtrSize)
   356  		t.intRegs = 1
   357  		t.ptrBytes = int64(PtrSize)
   358  
   359  		CheckSize(t.Elem())
   360  
   361  		// Make fake type to trigger channel element size check after
   362  		// any top-level recursive type has been completed.
   363  		t1 := NewChanArgs(t)
   364  		CheckSize(t1)
   365  
   366  	case TCHANARGS:
   367  		t1 := t.ChanArgs()
   368  		CalcSize(t1) // just in case
   369  		// Make sure size of t1.Elem() is calculated at this point. We can
   370  		// use CalcSize() here rather than CheckSize(), because the top-level
   371  		// (possibly recursive) type will have been calculated before the fake
   372  		// chanargs is handled.
   373  		CalcSize(t1.Elem())
   374  		if t1.Elem().width >= 1<<16 {
   375  			base.Errorf("channel element type too large (>64kB)")
   376  		}
   377  		w = 1 // anything will do
   378  
   379  	case TMAP: // implemented as pointer
   380  		w = int64(PtrSize)
   381  		t.intRegs = 1
   382  		CheckSize(t.Elem())
   383  		CheckSize(t.Key())
   384  		if t.Elem().width >= 1<<31 {
   385  			base.Errorf("map element type too large")
   386  		}
   387  		if t.Key().width >= 1<<31 {
   388  			base.Errorf("map key type too large")
   389  		}
   390  		t.setAlg(ANOEQ)
   391  		t.ptrBytes = int64(PtrSize)
   392  
   393  	case TFORW: // should have been filled in
   394  		base.Fatalf("invalid recursive type %v", t)
   395  
   396  	case TANY: // not a real type; should be replaced before use.
   397  		base.Fatalf("CalcSize any")
   398  
   399  	case TSTRING:
   400  		if StringSize == 0 {
   401  			base.Fatalf("early CalcSize string")
   402  		}
   403  		w = StringSize
   404  		t.align = uint8(PtrSize)
   405  		t.intRegs = 2
   406  		t.setAlg(ASTRING)
   407  		t.ptrBytes = int64(PtrSize)
   408  
   409  	case TARRAY:
   410  		if t.Elem() == nil {
   411  			break
   412  		}
   413  		CalcArraySize(t)
   414  		w = t.width
   415  
   416  	case TSLICE:
   417  		if t.Elem() == nil {
   418  			break
   419  		}
   420  		w = SliceSize
   421  		CheckSize(t.Elem())
   422  		t.align = uint8(PtrSize)
   423  		t.intRegs = 3
   424  		t.setAlg(ANOEQ)
   425  		if !t.Elem().NotInHeap() {
   426  			t.ptrBytes = int64(PtrSize)
   427  		}
   428  
   429  	case TSTRUCT:
   430  		if t.IsFuncArgStruct() {
   431  			base.Fatalf("CalcSize fn struct %v", t)
   432  		}
   433  		CalcStructSize(t)
   434  		w = t.width
   435  
   436  	// make fake type to check later to
   437  	// trigger function argument computation.
   438  	case TFUNC:
   439  		t1 := NewFuncArgs(t)
   440  		CheckSize(t1)
   441  		w = int64(PtrSize) // width of func type is pointer
   442  		t.intRegs = 1
   443  		t.setAlg(ANOEQ)
   444  		t.ptrBytes = int64(PtrSize)
   445  
   446  	// function is 3 cated structures;
   447  	// compute their widths as side-effect.
   448  	case TFUNCARGS:
   449  		t1 := t.FuncArgs()
   450  		// TODO(mdempsky): Should package abi be responsible for computing argwid?
   451  		w = calcStructOffset(t1, t1.Recvs(), 0)
   452  		w = calcStructOffset(t1, t1.Params(), w)
   453  		w = RoundUp(w, int64(PtrSize))
   454  		w = calcStructOffset(t1, t1.Results(), w)
   455  		w = RoundUp(w, int64(PtrSize))
   456  		t1.extra.(*Func).Argwid = w
   457  		t.align = 1
   458  	}
   459  
   460  	if PtrSize == 4 && w != int64(int32(w)) {
   461  		base.Errorf("type %v too large", t)
   462  	}
   463  
   464  	t.width = w
   465  	if t.align == 0 {
   466  		if w == 0 || w > 8 || w&(w-1) != 0 {
   467  			base.Fatalf("invalid alignment for %v", t)
   468  		}
   469  		t.align = uint8(w)
   470  	}
   471  
   472  	base.Pos = lno
   473  
   474  	ResumeCheckSize()
   475  }
   476  
   477  // simdify marks a type as a SIMD type. SIMD vector and mask types are
   478  // represented as structs whose first field is a SIMD tag, a zero-sized marker
   479  // type. However, at run time, they're allocated to vector registers (on amd64,
   480  // xmm, ymm, or zmm), so the "fields" of the struct are essentially ignored by
   481  // the compiler, except for their space, alignment, and equality algorithm.
   482  func simdify(st *Type) {
   483  	st.align = 8
   484  	st.intRegs = 0
   485  	st.floatRegs = 1
   486  	st.flags |= typeIsSIMD
   487  }
   488  
   489  // CalcStructSize calculates the size of t,
   490  // filling in t.width, t.align, t.intRegs, and t.floatRegs,
   491  // even if size calculation is otherwise disabled.
   492  func CalcStructSize(t *Type) {
   493  	var maxAlign uint8 = 1
   494  	isSIMDTag := false
   495  
   496  	// Recognize special types. This logic is duplicated in go/types and
   497  	// cmd/compile/internal/types2.
   498  	if sym := t.Sym(); sym != nil {
   499  		switch {
   500  		case sym.Name == "align64" && isAtomicStdPkg(sym.Pkg):
   501  			maxAlign = 8
   502  
   503  		case sym.Name == "align128" && isAtomicStdPkg(sym.Pkg):
   504  			maxAlign = 16
   505  
   506  		case buildcfg.Experiment.SIMD && sym.Pkg.Path == "simd/archsimd":
   507  			// This gates the experiment -- without it, no user-visible types can be "simd".
   508  			// The SSA-visible SIMD types remain.
   509  			switch sym.Name {
   510  			case "v128", "v256", "v512", "psve":
   511  				// A tag type. Lay it out like any other struct
   512  				// below, and then simdify it.
   513  				isSIMDTag = true
   514  			}
   515  		}
   516  	}
   517  
   518  	fields := t.Fields()
   519  
   520  	size := calcStructOffset(t, fields, 0)
   521  
   522  	// For non-zero-sized structs which end in a zero-sized field, we
   523  	// add an extra byte of padding to the type. This padding ensures
   524  	// that taking the address of a zero-sized field can't manufacture a
   525  	// pointer to the next object in the heap. See issue 9401.
   526  	if size > 0 && fields[len(fields)-1].Type.width == 0 {
   527  		size++
   528  	}
   529  
   530  	var intRegs, floatRegs uint64
   531  	for _, field := range fields {
   532  		typ := field.Type
   533  
   534  		// The alignment of a struct type is the maximum alignment of its
   535  		// field types.
   536  		if align := typ.align; align > maxAlign {
   537  			maxAlign = align
   538  		}
   539  
   540  		// Each field needs its own registers.
   541  		// We sum in uint64 to avoid possible overflows.
   542  		intRegs += uint64(typ.intRegs)
   543  		floatRegs += uint64(typ.floatRegs)
   544  	}
   545  
   546  	// Final size includes trailing padding.
   547  	size = RoundUp(size, int64(maxAlign))
   548  
   549  	if intRegs > math.MaxUint8 || floatRegs > math.MaxUint8 {
   550  		intRegs = math.MaxUint8
   551  		floatRegs = math.MaxUint8
   552  	}
   553  
   554  	t.width = size
   555  	t.align = maxAlign
   556  	t.intRegs = uint8(intRegs)
   557  	t.floatRegs = uint8(floatRegs)
   558  
   559  	// Compute eq/hash algorithm type.
   560  	t.alg = AMEM // default
   561  	if t.Noalg() {
   562  		t.setAlg(ANOALG)
   563  	}
   564  	if len(fields) == 1 && !fields[0].Sym.IsBlank() {
   565  		// One-field struct is same as that one field alone.
   566  		t.setAlg(fields[0].Type.alg)
   567  	} else {
   568  		for i, f := range fields {
   569  			a := f.Type.alg
   570  			switch a {
   571  			case ANOEQ, ANOALG:
   572  			case AMEM:
   573  				// Blank fields and padded fields need a special compare.
   574  				if f.Sym.IsBlank() || IsPaddedField(t, i) {
   575  					a = ASPECIAL
   576  				}
   577  			default:
   578  				// Fields with non-memory equality need a special compare.
   579  				a = ASPECIAL
   580  			}
   581  			t.setAlg(a)
   582  		}
   583  	}
   584  	// Compute ptrBytes.
   585  	for i := len(fields) - 1; i >= 0; i-- {
   586  		f := fields[i]
   587  		if size := PtrDataSize(f.Type); size > 0 {
   588  			t.ptrBytes = f.Offset + size
   589  			break
   590  		}
   591  	}
   592  
   593  	if isSIMDTag {
   594  		// A tag is laid out like any other struct, and is not itself a
   595  		// SIMD type: it only marks the struct whose first field it is as
   596  		// one. Every SIMD vector and mask type has a tag as its first
   597  		// field, followed by an array of its elements, and compares like
   598  		// a struct with one field per element, so it takes its algorithm
   599  		// from its fields. Thus a tag with an uncomparable field makes
   600  		// the vector and mask types that use it uncomparable, which is
   601  		// how the SVE tags, with their [0]func() fields, keep the SVE
   602  		// types uncomparable.
   603  		t.flags |= typeIsSIMDTag
   604  	} else if len(t.Fields()) >= 1 && t.Fields()[0].Type.flags&typeIsSIMDTag != 0 {
   605  		// A SIMD vector or mask type: a struct whose first field is a
   606  		// tag. This includes the types of simd/archsimd, like
   607  		// archsimd.Float32x4, as well as types defined in terms of them,
   608  		// like `type Foo archsimd.Float32x4`.
   609  		if t.Fields()[0].Type.Sym().Name == "psve" {
   610  			simdify(t)
   611  			// Force it to be passed via memory for now.
   612  			// TODO: support p registers in the ABI.
   613  			t.intRegs = math.MaxUint8
   614  			t.floatRegs = math.MaxUint8
   615  		} else {
   616  			simdify(t)
   617  		}
   618  	}
   619  }
   620  
   621  // CalcArraySize calculates the size of t,
   622  // filling in t.width, t.align, t.alg, and t.ptrBytes,
   623  // even if size calculation is otherwise disabled.
   624  func CalcArraySize(t *Type) {
   625  	elem := t.Elem()
   626  	n := t.NumElem()
   627  	CalcSize(elem)
   628  	t.SetNotInHeap(elem.NotInHeap())
   629  	if elem.width != 0 {
   630  		cap := (uint64(MaxWidth) - 1) / uint64(elem.width)
   631  		if uint64(n) > cap {
   632  			base.Errorf("type %L larger than address space", t)
   633  		}
   634  	}
   635  
   636  	t.width = elem.width * n
   637  	t.align = elem.align
   638  	// ABIInternal only allows "trivial" arrays (i.e., length 0 or 1)
   639  	// to be passed by register.
   640  	switch n {
   641  	case 0:
   642  		t.intRegs = 0
   643  		t.floatRegs = 0
   644  	case 1:
   645  		t.intRegs = elem.intRegs
   646  		t.floatRegs = elem.floatRegs
   647  	default:
   648  		t.intRegs = math.MaxUint8
   649  		t.floatRegs = math.MaxUint8
   650  	}
   651  	t.alg = AMEM // default
   652  	if t.Noalg() {
   653  		t.setAlg(ANOALG)
   654  	}
   655  	switch a := elem.alg; a {
   656  	case AMEM, ANOEQ, ANOALG:
   657  		t.setAlg(a)
   658  	default:
   659  		switch n {
   660  		case 0:
   661  			// We checked above that the element type is comparable.
   662  			t.setAlg(AMEM)
   663  		case 1:
   664  			// Single-element array is same as its lone element.
   665  			t.setAlg(a)
   666  		default:
   667  			t.setAlg(ASPECIAL)
   668  		}
   669  	}
   670  	if n > 0 {
   671  		x := PtrDataSize(elem)
   672  		if x > 0 {
   673  			t.ptrBytes = elem.width*(n-1) + x
   674  		}
   675  	}
   676  }
   677  
   678  func (t *Type) widthCalculated() bool {
   679  	return t.align > 0
   680  }
   681  
   682  // when a type's width should be known, we call CheckSize
   683  // to compute it.  during a declaration like
   684  //
   685  //	type T *struct { next T }
   686  //
   687  // it is necessary to defer the calculation of the struct width
   688  // until after T has been initialized to be a pointer to that struct.
   689  // similarly, during import processing structs may be used
   690  // before their definition.  in those situations, calling
   691  // DeferCheckSize() stops width calculations until
   692  // ResumeCheckSize() is called, at which point all the
   693  // CalcSizes that were deferred are executed.
   694  // CalcSize should only be called when the type's size
   695  // is needed immediately.  CheckSize makes sure the
   696  // size is evaluated eventually.
   697  
   698  var deferredTypeStack []*Type
   699  
   700  func CheckSize(t *Type) {
   701  	if t == nil {
   702  		return
   703  	}
   704  
   705  	// function arg structs should not be checked
   706  	// outside of the enclosing function.
   707  	if t.IsFuncArgStruct() {
   708  		base.Fatalf("CheckSize %v", t)
   709  	}
   710  
   711  	if defercalc == 0 {
   712  		CalcSize(t)
   713  		return
   714  	}
   715  
   716  	// if type has not yet been pushed on deferredTypeStack yet, do it now
   717  	if !t.Deferwidth() {
   718  		t.SetDeferwidth(true)
   719  		deferredTypeStack = append(deferredTypeStack, t)
   720  	}
   721  }
   722  
   723  func DeferCheckSize() {
   724  	defercalc++
   725  }
   726  
   727  func ResumeCheckSize() {
   728  	if defercalc == 1 {
   729  		for len(deferredTypeStack) > 0 {
   730  			t := deferredTypeStack[len(deferredTypeStack)-1]
   731  			deferredTypeStack = deferredTypeStack[:len(deferredTypeStack)-1]
   732  			t.SetDeferwidth(false)
   733  			CalcSize(t)
   734  		}
   735  	}
   736  
   737  	defercalc--
   738  }
   739  
   740  // PtrDataSize returns the length in bytes of the prefix of t
   741  // containing pointer data. Anything after this offset is scalar data.
   742  //
   743  // PtrDataSize is only defined for actual Go types. It's an error to
   744  // use it on compiler-internal types (e.g., TSSA, TRESULTS).
   745  func PtrDataSize(t *Type) int64 {
   746  	CalcSize(t)
   747  	x := t.ptrBytes
   748  	if t.Kind() == TPTR && t.Elem().NotInHeap() {
   749  		// Note: this is done here instead of when we're setting
   750  		// the ptrBytes field, because at that time (in NewPtr, usually)
   751  		// the NotInHeap bit of the element type might not be set yet.
   752  		x = 0
   753  	}
   754  	return x
   755  }
   756  

View as plain text