Merge pull request #219 from xushiwei/q

runtime: Struct
This commit is contained in:
xushiwei
2024-05-20 13:53:52 +08:00
committed by GitHub
4 changed files with 44 additions and 3 deletions

Binary file not shown.

View File

@@ -50,8 +50,6 @@ type Type struct {
PtrToThis TypeOff // type for pointer to this type, may be zero
}
func (t *Type) Kind() Kind { return Kind(t.Kind_ & KindMask) }
// A Kind represents the specific kind of type that a Type represents.
// The zero Kind is not a valid kind.
type Kind uint
@@ -86,12 +84,14 @@ const (
UnsafePointer
)
/*
const (
// TODO (khr, drchase) why aren't these in TFlag? Investigate, fix if possible.
KindDirectIface = 1 << 5
KindGCProg = 1 << 6 // Type.gc points to GC program
KindMask = (1 << 5) - 1
)
*/
// TFlag is used by a Type to signal what extra type information is
// available in the memory directly following the Type value.
@@ -207,6 +207,10 @@ type StructField struct {
Offset uintptr // byte offset of field
}
func (f *StructField) Embedded() bool {
return f.Name.IsEmbedded()
}
type StructType struct {
Type
PkgPath Name
@@ -225,6 +229,16 @@ type Imethod struct {
Typ TypeOff // .(*FuncType) underneath
}
func (t *Type) Kind() Kind { return Kind(t.Kind_) }
// Size returns the size of data with type t.
func (t *Type) Size() uintptr { return t.Size_ }
// Align returns the alignment of data with type t.
func (t *Type) Align() int { return int(t.Align_) }
func (t *Type) FieldAlign() int { return int(t.FieldAlign_) }
func (t *Type) Common() *Type {
return t
}

Binary file not shown.

View File

@@ -78,9 +78,36 @@ var (
func basicType(kind abi.Kind) *Type {
return &Type{
Size_: sizeBasicTypes[kind],
Hash: uint32(kind),
Hash: uint32(kind), // TODO(xsw): hash
Kind_: uint8(kind),
}
}
// -----------------------------------------------------------------------------
// StructField returns a struct field.
func StructField(name string, typ *Type, off uintptr, tag string, exported, embedded bool) abi.StructField {
n := abi.NewName(name, tag, exported, embedded)
return abi.StructField{
Name: n,
Typ: typ,
Offset: off,
}
}
// Struct returns a struct type.
func Struct(size uintptr, pkgPath string, fields ...abi.StructField) *Type {
npkg := abi.NewName(pkgPath, "", false, false)
ret := &abi.StructType{
Type: Type{
Size_: size,
Hash: uint32(abi.Struct), // TODO(xsw): hash
Kind_: uint8(abi.Struct),
},
PkgPath: npkg,
Fields: fields,
}
return &ret.Type
}
// -----------------------------------------------------------------------------