Skip to content

Commit 615ce40

Browse files
committed
unsafe type data field
1 parent 6afa6b4 commit 615ce40

File tree

4 files changed

+105
-50
lines changed

4 files changed

+105
-50
lines changed

internal/checker/checker.go

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

22388-
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data TypeData) *Type {
22389-
c.TypeCount++
22390-
t := data.AsType()
22391-
t.flags = flags
22392-
t.objectFlags = objectFlags &^ (ObjectFlagsCouldContainTypeVariablesComputed | ObjectFlagsCouldContainTypeVariables | ObjectFlagsMembersResolved)
22393-
t.id = TypeId(c.TypeCount)
22394-
t.checker = c
22395-
t.data = data
22396-
return t
22397-
}
22398-
2239922388
func (c *Checker) newIntrinsicType(flags TypeFlags, intrinsicName string) *Type {
2240022389
return c.newIntrinsicTypeEx(flags, intrinsicName, ObjectFlagsNone)
2240122390
}
@@ -22441,7 +22430,7 @@ func (c *Checker) newUniqueESSymbolType(symbol *ast.Symbol, name string) *Type {
2244122430
}
2244222431

2244322432
func (c *Checker) newObjectType(objectFlags ObjectFlags, symbol *ast.Symbol) *Type {
22444-
var data TypeData
22433+
var data typeData
2244522434
switch {
2244622435
case objectFlags&ObjectFlagsClassOrInterface != 0:
2244722436
data = &InterfaceType{}

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+
}

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+
}

internal/checker/types.go

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

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

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

606594
// Casts for embedded struct types
607595

608-
func (t *Type) AsConstrainedType() *ConstrainedType { return t.data.AsConstrainedType() }
609-
func (t *Type) AsStructuredType() *StructuredType { return t.data.AsStructuredType() }
610-
func (t *Type) AsObjectType() *ObjectType { return t.data.AsObjectType() }
611-
func (t *Type) AsTypeReference() *TypeReference { return t.data.AsTypeReference() }
612-
func (t *Type) AsInterfaceType() *InterfaceType { return t.data.AsInterfaceType() }
596+
func (t *Type) AsConstrainedType() *ConstrainedType { return t.data().AsConstrainedType() }
597+
func (t *Type) AsStructuredType() *StructuredType { return t.data().AsStructuredType() }
598+
func (t *Type) AsObjectType() *ObjectType { return t.data().AsObjectType() }
599+
func (t *Type) AsTypeReference() *TypeReference { return t.data().AsTypeReference() }
600+
func (t *Type) AsInterfaceType() *InterfaceType { return t.data().AsInterfaceType() }
613601
func (t *Type) AsUnionOrIntersectionType() *UnionOrIntersectionType {
614-
return t.data.AsUnionOrIntersectionType()
602+
return t.data().AsUnionOrIntersectionType()
615603
}
616604

617605
func (t *Type) Distributed() []*Type {
@@ -672,7 +660,7 @@ func (t *Type) TargetTupleType() *TupleType {
672660

673661
// TypeData
674662

675-
type TypeData interface {
663+
type typeData interface {
676664
AsType() *Type
677665
AsConstrainedType() *ConstrainedType
678666
AsStructuredType() *StructuredType

0 commit comments

Comments
 (0)