Skip to content

Commit 9708922

Browse files
committed
unsafe type data field
1 parent c2b4b2e commit 9708922

File tree

4 files changed

+105
-50
lines changed

4 files changed

+105
-50
lines changed

Diff for: internal/checker/checker.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -22937,17 +22937,6 @@ func isUnaryTupleTypeNode(node *ast.Node) bool {
2293722937
return ast.IsTupleTypeNode(node) && len(node.AsTupleTypeNode().Elements.Nodes) == 1
2293822938
}
2293922939

22940-
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data TypeData) *Type {
22941-
c.TypeCount++
22942-
t := data.AsType()
22943-
t.flags = flags
22944-
t.objectFlags = objectFlags &^ (ObjectFlagsCouldContainTypeVariablesComputed | ObjectFlagsCouldContainTypeVariables | ObjectFlagsMembersResolved)
22945-
t.id = TypeId(c.TypeCount)
22946-
t.checker = c
22947-
t.data = data
22948-
return t
22949-
}
22950-
2295122940
func (c *Checker) newIntrinsicType(flags TypeFlags, intrinsicName string) *Type {
2295222941
return c.newIntrinsicTypeEx(flags, intrinsicName, ObjectFlagsNone)
2295322942
}
@@ -22995,7 +22984,7 @@ func (c *Checker) newUniqueESSymbolType(symbol *ast.Symbol, name string) *Type {
2299522984
}
2299622985

2299722986
func (c *Checker) newObjectType(objectFlags ObjectFlags, symbol *ast.Symbol) *Type {
22998-
var data TypeData
22987+
var data typeData
2299922988
switch {
2300022989
case objectFlags&ObjectFlagsClassOrInterface != 0:
2300122990
data = &InterfaceType{}

Diff for: internal/checker/type_safe.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build !tsunsafe
2+
3+
package checker
4+
5+
import (
6+
"github.com/microsoft/typescript-go/internal/ast"
7+
)
8+
9+
// Type
10+
11+
type Type struct {
12+
flags TypeFlags
13+
objectFlags ObjectFlags
14+
id TypeId
15+
symbol *ast.Symbol
16+
alias *TypeAlias
17+
checker *Checker
18+
_data typeData // Type specific data
19+
}
20+
21+
func (t *Type) data() typeData {
22+
return t._data
23+
}
24+
25+
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data typeData) *Type {
26+
c.TypeCount++
27+
t := data.AsType()
28+
t.flags = flags
29+
t.objectFlags = objectFlags &^ (ObjectFlagsCouldContainTypeVariablesComputed | ObjectFlagsCouldContainTypeVariables | ObjectFlagsMembersResolved)
30+
t.id = TypeId(c.TypeCount)
31+
t.checker = c
32+
t._data = data
33+
return t
34+
}

Diff for: internal/checker/type_unsafe.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build tsunsafe
2+
3+
package checker
4+
5+
import (
6+
"unsafe"
7+
8+
"github.com/microsoft/typescript-go/internal/ast"
9+
)
10+
11+
// Type
12+
13+
type Type struct {
14+
flags TypeFlags
15+
objectFlags ObjectFlags
16+
id TypeId
17+
symbol *ast.Symbol
18+
alias *TypeAlias
19+
checker *Checker
20+
dataType unsafe.Pointer // Type specific data
21+
}
22+
23+
type interfaceValue struct {
24+
typ unsafe.Pointer
25+
value unsafe.Pointer
26+
}
27+
28+
func (t *Type) data() typeData {
29+
var data typeData
30+
(*interfaceValue)(unsafe.Pointer(&data)).typ = t.dataType
31+
(*interfaceValue)(unsafe.Pointer(&data)).value = unsafe.Pointer(t)
32+
return data
33+
}
34+
35+
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data typeData) *Type {
36+
c.TypeCount++
37+
t := data.AsType()
38+
t.flags = flags
39+
t.objectFlags = objectFlags &^ (ObjectFlagsCouldContainTypeVariablesComputed | ObjectFlagsCouldContainTypeVariables | ObjectFlagsMembersResolved)
40+
t.id = TypeId(c.TypeCount)
41+
t.checker = c
42+
t.dataType = (*interfaceValue)(unsafe.Pointer(&data)).typ
43+
return t
44+
}

Diff for: internal/checker/types.go

+26-38
Original file line numberDiff line numberDiff line change
@@ -569,50 +569,38 @@ func (a *TypeAlias) TypeArguments() []*Type {
569569
return a.typeArguments
570570
}
571571

572-
// Type
573-
574-
type Type struct {
575-
flags TypeFlags
576-
objectFlags ObjectFlags
577-
id TypeId
578-
symbol *ast.Symbol
579-
alias *TypeAlias
580-
checker *Checker
581-
data TypeData // Type specific data
582-
}
583-
584572
// Casts for concrete struct types
585573

586-
func (t *Type) AsIntrinsicType() *IntrinsicType { return t.data.(*IntrinsicType) }
587-
func (t *Type) AsLiteralType() *LiteralType { return t.data.(*LiteralType) }
588-
func (t *Type) AsUniqueESSymbolType() *UniqueESSymbolType { return t.data.(*UniqueESSymbolType) }
589-
func (t *Type) AsTupleType() *TupleType { return t.data.(*TupleType) }
590-
func (t *Type) AsSingleSignatureType() *SingleSignatureType { return t.data.(*SingleSignatureType) }
574+
func (t *Type) AsIntrinsicType() *IntrinsicType { return t.data().(*IntrinsicType) }
575+
func (t *Type) AsLiteralType() *LiteralType { return t.data().(*LiteralType) }
576+
func (t *Type) AsUniqueESSymbolType() *UniqueESSymbolType { return t.data().(*UniqueESSymbolType) }
577+
func (t *Type) AsTupleType() *TupleType { return t.data().(*TupleType) }
578+
func (t *Type) AsSingleSignatureType() *SingleSignatureType { return t.data().(*SingleSignatureType) }
591579
func (t *Type) AsInstantiationExpressionType() *InstantiationExpressionType {
592-
return t.data.(*InstantiationExpressionType)
593-
}
594-
func (t *Type) AsMappedType() *MappedType { return t.data.(*MappedType) }
595-
func (t *Type) AsReverseMappedType() *ReverseMappedType { return t.data.(*ReverseMappedType) }
596-
func (t *Type) AsEvolvingArrayType() *EvolvingArrayType { return t.data.(*EvolvingArrayType) }
597-
func (t *Type) AsTypeParameter() *TypeParameter { return t.data.(*TypeParameter) }
598-
func (t *Type) AsUnionType() *UnionType { return t.data.(*UnionType) }
599-
func (t *Type) AsIntersectionType() *IntersectionType { return t.data.(*IntersectionType) }
600-
func (t *Type) AsIndexType() *IndexType { return t.data.(*IndexType) }
601-
func (t *Type) AsIndexedAccessType() *IndexedAccessType { return t.data.(*IndexedAccessType) }
602-
func (t *Type) AsTemplateLiteralType() *TemplateLiteralType { return t.data.(*TemplateLiteralType) }
603-
func (t *Type) AsStringMappingType() *StringMappingType { return t.data.(*StringMappingType) }
604-
func (t *Type) AsSubstitutionType() *SubstitutionType { return t.data.(*SubstitutionType) }
605-
func (t *Type) AsConditionalType() *ConditionalType { return t.data.(*ConditionalType) }
580+
return t.data().(*InstantiationExpressionType)
581+
}
582+
func (t *Type) AsMappedType() *MappedType { return t.data().(*MappedType) }
583+
func (t *Type) AsReverseMappedType() *ReverseMappedType { return t.data().(*ReverseMappedType) }
584+
func (t *Type) AsEvolvingArrayType() *EvolvingArrayType { return t.data().(*EvolvingArrayType) }
585+
func (t *Type) AsTypeParameter() *TypeParameter { return t.data().(*TypeParameter) }
586+
func (t *Type) AsUnionType() *UnionType { return t.data().(*UnionType) }
587+
func (t *Type) AsIntersectionType() *IntersectionType { return t.data().(*IntersectionType) }
588+
func (t *Type) AsIndexType() *IndexType { return t.data().(*IndexType) }
589+
func (t *Type) AsIndexedAccessType() *IndexedAccessType { return t.data().(*IndexedAccessType) }
590+
func (t *Type) AsTemplateLiteralType() *TemplateLiteralType { return t.data().(*TemplateLiteralType) }
591+
func (t *Type) AsStringMappingType() *StringMappingType { return t.data().(*StringMappingType) }
592+
func (t *Type) AsSubstitutionType() *SubstitutionType { return t.data().(*SubstitutionType) }
593+
func (t *Type) AsConditionalType() *ConditionalType { return t.data().(*ConditionalType) }
606594

607595
// Casts for embedded struct types
608596

609-
func (t *Type) AsConstrainedType() *ConstrainedType { return t.data.AsConstrainedType() }
610-
func (t *Type) AsStructuredType() *StructuredType { return t.data.AsStructuredType() }
611-
func (t *Type) AsObjectType() *ObjectType { return t.data.AsObjectType() }
612-
func (t *Type) AsTypeReference() *TypeReference { return t.data.AsTypeReference() }
613-
func (t *Type) AsInterfaceType() *InterfaceType { return t.data.AsInterfaceType() }
597+
func (t *Type) AsConstrainedType() *ConstrainedType { return t.data().AsConstrainedType() }
598+
func (t *Type) AsStructuredType() *StructuredType { return t.data().AsStructuredType() }
599+
func (t *Type) AsObjectType() *ObjectType { return t.data().AsObjectType() }
600+
func (t *Type) AsTypeReference() *TypeReference { return t.data().AsTypeReference() }
601+
func (t *Type) AsInterfaceType() *InterfaceType { return t.data().AsInterfaceType() }
614602
func (t *Type) AsUnionOrIntersectionType() *UnionOrIntersectionType {
615-
return t.data.AsUnionOrIntersectionType()
603+
return t.data().AsUnionOrIntersectionType()
616604
}
617605

618606
func (t *Type) Distributed() []*Type {
@@ -673,7 +661,7 @@ func (t *Type) TargetTupleType() *TupleType {
673661

674662
// TypeData
675663

676-
type TypeData interface {
664+
type typeData interface {
677665
AsType() *Type
678666
AsConstrainedType() *ConstrainedType
679667
AsStructuredType() *StructuredType

0 commit comments

Comments
 (0)