@@ -182,8 +182,10 @@ def __init__(self,
182182 self .size = size
183183 # TODO: For low-level integers, they actually don't have undefined values
184184 # we need to figure out some way to represent here.
185- if ctype in ( 'CPyTagged' , 'int32_t' , 'int64_t' ) :
185+ if ctype == 'CPyTagged' :
186186 self .c_undefined = 'CPY_INT_TAG'
187+ elif ctype in ('int32_t' , 'int64_t' , 'CPyPtr' ):
188+ self .c_undefined = '0'
187189 elif ctype == 'PyObject *' :
188190 # Boxed types use the null pointer as the error value.
189191 self .c_undefined = 'NULL'
@@ -254,6 +256,10 @@ def __repr__(self) -> str:
254256else :
255257 c_pyssize_t_rprimitive = int64_rprimitive
256258
259+ # low level pointer, represented as integer in C backends
260+ pointer_rprimitive = RPrimitive ('ptr' , is_unboxed = True , is_refcounted = False ,
261+ ctype = 'CPyPtr' ) # type: Final
262+
257263# Floats are represent as 'float' PyObject * values. (In the future
258264# we'll likely switch to a more efficient, unboxed representation.)
259265float_rprimitive = RPrimitive ('builtins.float' , is_unboxed = False ,
@@ -311,6 +317,10 @@ def is_c_py_ssize_t_rprimitive(rtype: RType) -> bool:
311317 return rtype is c_pyssize_t_rprimitive
312318
313319
320+ def is_pointer_rprimitive (rtype : RType ) -> bool :
321+ return rtype is pointer_rprimitive
322+
323+
314324def is_float_rprimitive (rtype : RType ) -> bool :
315325 return isinstance (rtype , RPrimitive ) and rtype .name == 'builtins.float'
316326
@@ -514,12 +524,8 @@ def compute_aligned_offsets_and_size(types: List[RType]) -> Tuple[List[int], int
514524 return offsets , final_size
515525
516526
517- class StructInfo :
518- """Struct-like type Infomation
519-
520- StructInfo should work with registry to ensure constraints like the unique naming
521- constraint for struct type
522- """
527+ class RStruct (RType ):
528+ """Represent CPython structs"""
523529 def __init__ (self ,
524530 name : str ,
525531 names : List [str ],
@@ -532,31 +538,7 @@ def __init__(self,
532538 for i in range (len (self .types ) - len (self .names )):
533539 self .names .append ('_item' + str (i ))
534540 self .offsets , self .size = compute_aligned_offsets_and_size (types )
535-
536-
537- class RStruct (RType ):
538- """Represent CPython structs"""
539- def __init__ (self ,
540- info : StructInfo ) -> None :
541- self .info = info
542- self .name = self .info .name
543- self ._ctype = self .info .name
544-
545- @property
546- def names (self ) -> List [str ]:
547- return self .info .names
548-
549- @property
550- def types (self ) -> List [RType ]:
551- return self .info .types
552-
553- @property
554- def offsets (self ) -> List [int ]:
555- return self .info .offsets
556-
557- @property
558- def size (self ) -> int :
559- return self .info .size
541+ self ._ctype = name
560542
561543 def accept (self , visitor : 'RTypeVisitor[T]' ) -> T :
562544 return visitor .visit_rstruct (self )
@@ -571,10 +553,11 @@ def __repr__(self) -> str:
571553 in zip (self .names , self .types )))
572554
573555 def __eq__ (self , other : object ) -> bool :
574- return isinstance (other , RStruct ) and self .info == other .info
556+ return (isinstance (other , RStruct ) and self .name == other .name
557+ and self .names == other .names and self .types == other .types )
575558
576559 def __hash__ (self ) -> int :
577- return hash (self .info )
560+ return hash (( self .name , tuple ( self . names ), tuple ( self . types )) )
578561
579562 def serialize (self ) -> JsonDict :
580563 assert False
@@ -687,3 +670,14 @@ def optional_value_type(rtype: RType) -> Optional[RType]:
687670def is_optional_type (rtype : RType ) -> bool :
688671 """Is rtype an optional type with exactly two union items?"""
689672 return optional_value_type (rtype ) is not None
673+
674+
675+ PyObject = RStruct (
676+ name = 'PyObject' ,
677+ names = ['ob_refcnt' , 'ob_type' ],
678+ types = [c_pyssize_t_rprimitive , pointer_rprimitive ])
679+
680+ PyVarObject = RStruct (
681+ name = 'PyVarObject' ,
682+ names = ['ob_base' , 'ob_size' ],
683+ types = [PyObject , c_pyssize_t_rprimitive ])
0 commit comments