Ë
    pPêju=  ã                  óÆ  — U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ d	Z d
e!d<   dd„Z"dd„Z#	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z$	 	 	 	 	 	 	 	 	 	 dd„Z%	 	 	 	 	 	 	 	 	 	 d d„Z&e'e(e)   e(e)   f   Z*d!d„Z+d!d„Z, G d„ de	e)   «      Z-	 	 	 	 	 	 	 	 	 	 d"d„Z. G d„ de	e)   «      Z/	 	 	 	 	 	 	 	 	 	 d#d„Z0dd„Z1dd„Z2y)$a_	  Always defined attribute analysis.

An always defined attribute has some statements in __init__ or the
class body that cause the attribute to be always initialized when an
instance is constructed. It must also not be possible to read the
attribute before initialization, and it can't be deletable.

We can assume that the value is always defined when reading an always
defined attribute. Otherwise we'll need to raise AttributeError if the
value is undefined (i.e. has the error value).

We use data flow analysis to figure out attributes that are always
defined. Example:

  class C:
      def __init__(self) -> None:
          self.x = 0
          if func():
              self.y = 1
          else:
              self.y = 2
              self.z = 3

In this example, the attributes 'x' and 'y' are always defined, but 'z'
is not. The analysis assumes that we know that there won't be any subclasses.

The analysis also works if there is a known, closed set of subclasses.
An attribute defined in a base class can only be always defined if it's
also always defined in all subclasses.

As soon as __init__ contains an op that can 'leak' self to another
function, we will stop inferring always defined attributes, since the
analysis is mostly intra-procedural and only looks at __init__ methods.
The called code could read an uninitialized attribute. Example:

  class C:
      def __init__(self) -> None:
          self.x = self.foo()

      def foo(self) -> int:
          ...

Now we won't infer 'x' as always defined, since 'foo' might read 'x'
before initialization.

As an exception to the above limitation, we perform inter-procedural
analysis of super().__init__ calls, since these are very common.

Our analysis is somewhat optimistic. We assume that nobody calls a
method of a partially uninitialized object through gc.get_objects(), in
particular. Code like this could potentially cause a segfault with a null
pointer dereference. This seems very unlikely to be an issue in practice,
however.

Accessing an attribute via getattr always checks for undefined attributes
and thus works if the object is partially uninitialized. This can be used
as a workaround if somebody ever needs to inspect partially uninitialized
objects via gc.get_objects().

The analysis runs after IR building as a separate pass. Since we only
run this on __init__ methods, this analysis pass will be fairly quick.
é    )Úannotations)ÚFinal)ÚCFGÚMAYBE_ANALYSISÚAnalysisResultÚBaseAnalysisVisitorÚget_cfgÚrun_analysis)Úanalyze_self_leaks)ÚClassIR)ÚAssignÚAssignMultiÚ
BasicBlockÚBranchÚCallÚ	ControlOpÚGetAttrÚRegisterÚ
RegisterOpÚReturnÚSetAttrÚSetMemÚUnreachable)Ú	RInstanceFr   Údump_always_definedc                ó²   — t        «       }| D ]  }t        ||«       Œ t        «       }| D ]  }t        ||«       Œ t        «       }| D ]  }t        ||«       Œ y)ak  Find always defined attributes all classes of a compilation unit.

    Also tag attribute initialization ops to not decref the previous
    value (as this would read a NULL pointer and segfault).

    Update the _always_initialized_attrs, _sometimes_initialized_attrs
    and init_self_leak attributes in ClassIR instances.

    This is the main entry point.
    N)ÚsetÚ%analyze_always_defined_attrs_in_classÚ,update_always_defined_attrs_using_subclassesÚdetect_undefined_bitmap)Ú	class_irsÚseenÚcls      ú_/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/mypyc/analysis/attrdefined.pyÚanalyze_always_defined_attrsr%   d   sj   € ô ›€Dð ò 8ˆÜ-¨b°$Õ7ð8ô ‹5€DØò ?ˆÜ4°R¸Õ>ð?ô ‹5€DØò *ˆÜ  DÕ)ñ*ó    c                ó>  — | |v ry |j                  | «       | j                  sQ| j                  sE| j                  s9| j                  €-| j
                  �!| j                  «       s| j                  d«      ry | j                  dd  D ]  }t        ||«       Œ | j                  d«      }|€?| j                  j                  «       | _        | j                  j                  «       | _        y |j                  d   }t!        |j"                  «      }t%        |j"                  ||«      }t'        |j"                  || j                  |«      }t)        «       }| j                  D ]  }|j+                  |j,                  «       Œ t/        |j"                  ||| j                  z
  |¬«      }	t1        |j"                  ||||	|«      }
|
D �ch c]  }| j3                  |«      rŒ|’Œ }
}|
| _        t4        rt7        | j8                  t;        |
«      «       t=        |j"                  |||«      | _        t?        |j"                  |||«       d}|j"                  D ]E  }tA        |jB                  «      D ]+  \  }}|jD                  ||f   sŒtG        |tH        «      rŒ)d} ŒE ŒG || _%        y c c}w )NÚ__new__é   Ú__init__r   )Úinitial_undefinedÚcfgFT)&ÚaddÚis_traitÚinherits_pythonÚallow_interpreted_subclassesÚbuiltin_baseÚchildrenÚis_serializableÚ
has_methodÚmror   Ú
get_methodÚattrs_with_defaultsÚcopyÚ_always_initialized_attrsÚ_sometimes_initialized_attrsÚarg_regsr	   Úblocksr   Ú#analyze_maybe_defined_attrs_in_initr   ÚupdateÚ
attributesÚ%analyze_maybe_undefined_attrs_in_initÚfind_always_defined_attributesÚis_deletabler   ÚprintÚnameÚsortedÚ!find_sometimes_defined_attributesÚmark_attr_initialization_opsÚ	enumerateÚopsÚafterÚ
isinstancer   Úinit_self_leak)r#   r"   ÚbaseÚmÚself_regr,   ÚdirtyÚmaybe_definedÚ	all_attrsÚmaybe_undefinedÚalways_definedÚaÚ	any_dirtyÚbÚiÚops                   r$   r   r   €   sY  € Ø	ˆT�zØà‡H�HˆR„Lð 	�ŠØ×ÒØ×*Ò*Ø�?‰?Ð&Ø�;‰;ÐØ×ÑÔØ�=‰=˜Ô#ð 	ð —‘�q�r�
ò :ˆÜ-¨d°DÕ9ð:ð 	�‰�jÓ!€AØ€yØ')×'=Ñ'=×'BÑ'BÓ'DˆÔ$Ø*,×*@Ñ*@×*EÑ*EÓ*GˆÔ'ØØ�z‰z˜!‰}€HÜ
�!—(‘(Ó
€CÜ˜qŸx™x¨°3Ó7€EÜ7Ø	�‰�(˜B×2Ñ2°Có€Mô ›%€IØ—‘ò *ˆØ×Ñ˜Ÿ™Õ)ð*ä;Ø	�‰�(¨i¸"×:PÑ:PÑ.PÐVYô€Oô 4Ø	�‰�(˜I }°oÀuó€Nð "0ÖJ˜A°r·±ÀqÕ7I’aÐJ€NÐJà#1€BÔ ÝÜˆb�g‰g”v˜nÓ-Ô.Ü&GØ	�‰�(˜M¨5ó'€BÔ#ô ! §¡¨8°]ÀEÔJð €IØ�X‰Xò ˆÜ˜qŸu™uÓ%ò 	‰EˆAˆrØ�{‰{˜1˜a˜4Ó ¬°B¼Õ)?Ø �	Ùñ	ðð
 "€BÕùò% Ks   Æ>JÇJc                ó€  — |j                  «       }| D �]§  }t        |j                  «      D �]‹  \  }}	t        |	t        «      rF|	j
                  |u r8|	j                  |j                  ||f   v r|j                  |	j                  «       t        |	t        «      rc|	j
                  |u rU|	j                  |j                  ||f   v r8|	j                  |j                  ||f   v r|j                  |	j                  «       |j                  ||f   r:|j                  ||f   s&||j                  ||f   |j                  ||f   z
  z  } �Œ4t        |	t        «      s�Œ-|	j                  «       D ]L  }
|j                  ||f   rŒ|j                  |
df   sŒ'||j                  |
df   |j                  |
df   z
  z  }ŒN �ŒŽ �Œª |S )z¾Find attributes that are always initialized in some basic blocks.

    The analysis results are expected to be up-to-date for the blocks.

    Return a set of always defined attributes.
    r   )r8   rH   rI   rK   r   ÚobjÚattrÚbeforeÚdiscardr   rJ   r   Útargets)r<   rO   rR   rQ   rS   rP   ÚattrsÚblockrX   rY   Útargets              r$   rA   rA   À   s©  € ð �N‰NÓ€EØó ˆÜ˜uŸy™yÓ)ó 	‰EˆAˆrä˜"œgÔ&¨2¯6©6°XÑ+=Ø—7‘7˜o×4Ñ4°U¸A°XÑ>Ñ>Ø—M‘M "§'¡'Ô*ô
 ˜"œgÔ&¨2¯6©6°XÑ+=à—G‘G˜×5Ñ5°e¸Q°hÑ?Ñ?ØŸ™ =×#7Ñ#7¸¸q¸Ñ#AÑAà—M‘M "§'¡'Ô*ð �{‰{˜5 !˜8Ò$Ø—|‘| E¨1 HÒ-Ø!Ø%×+Ñ+¨E°1¨HÑ5¸×8MÑ8MÈeÐUVÈhÑ8WÑWñ�Eò Ü˜"œiÖ(Ø Ÿj™j›lò �Fà Ÿ;™; u¨a xÓ0°U·\±\À&È!À)Ó5LØ %Ø)×/Ñ/°¸°	Ñ:¸_×=RÑ=RÐSYÐ[\ÐS\Ñ=]Ñ]ñ!™òò3	ðð@ €Lr&   c                óŽ  — t        «       }| D ]µ  }t        |j                  «      D ]›  \  }}|j                  ||f   r'|j                  ||f   s||j                  ||f   z  } ŒVt        |t        «      sŒO|j                  «       D ]:  }|j                  ||f   rŒ|j                  |df   sŒ'||j                  |df   z  }Œ< Œ� Œ· |S )zDFind attributes that are sometimes initialized in some basic blocks.r   )r   rH   rI   rJ   r]   rK   r   r_   )	r<   rO   rQ   rP   r`   ra   rX   rY   rb   s	            r$   rF   rF   ò   sÖ   € ô “e€EØò 
GˆÜ˜uŸy™yÓ)ò 		G‰EˆAˆrà�{‰{˜5 !˜8Ò$Ø—|‘| E¨1 HÒ-Ø! M×$7Ñ$7¸¸q¸Ñ$AÑA�EÙÜ˜"œiÕ(Ø Ÿj™j›lò G�FØ Ÿ;™; u¨a xÓ0°U·\±\À&È!À)Ó5LØ %¨×(;Ñ(;¸FÀA¸IÑ(FÑ F™ñGñ		Gð
Gð €Lr&   c                ó  — | D ]�  }t        |j                  «      D ]g  \  }}t        |t        «      sŒ|j                  |u sŒ&|j
                  }||j                  ||f   vsŒF|j                  ||f   rŒX|j                  «        Œi Œƒ y)zØTag all SetAttr ops in the basic blocks that initialize attributes.

    Initialization ops assume that the previous attribute value is the error value,
    so there's no need to decref or check for definedness.
    N)	rH   rI   rK   r   r[   r\   r]   rJ   Úmark_as_initializer)r<   rO   rQ   rP   ra   rX   rY   r\   s           r$   rG   rG     s‚   € ð ò -ˆÜ˜uŸy™yÓ)ò 	-‰EˆAˆrÜ˜"œgÕ&¨2¯6©6°XÒ+=Ø—w‘w�Ø˜}×3Ñ3°E¸1°HÑ=Ò=ÀeÇkÁkÐRWÐYZÐRZÓF[Ø×*Ñ*Õ,ñ		-ñ-r&   c                ó,  — | j                   j                  j                  d   j                  }t	        |t
        «      sJ |«       ‚|j                  }|j                  D ��ch c]'  }|j                  D ]  }|j                  |«      sŒ|’Œ Œ) c}}S c c}}w )zLCalculate attributes that are always initialized by a super().__init__ call.r   )
ÚfnÚsigÚargsÚtyperK   r   Úclass_irr5   r?   Úis_always_defined)rY   Ú	self_typer#   rM   rU   s        r$   Ú#attributes_initialized_by_init_callrn     st   € à—‘—	‘	—‘˜qÑ!×&Ñ&€IÜ�i¤Ô+Ð6¨YÓ6Ð+Ø	×	Ñ	€BØŸ&™&×W�$¨$¯/©/ÒW Q¸T×=SÑ=SÐTUÕ=VŠAÐWˆAÓWÐWùÓWs   Á %BÂBc                óÒ   — | j                   j                  j                  d   j                  }t	        |t
        «      sJ |«       ‚|j                  }t        | «      |j                  z  S )zHCalculate attributes that may be initialized by a super().__init__ call.r   )	rg   rh   ri   rj   rK   r   rk   rn   r:   )rY   rm   r#   s      r$   Ú)attributes_maybe_initialized_by_init_callrp   &  sT   € à—‘—	‘	—‘˜qÑ!×&Ñ&€IÜ�i¤Ô+Ð6¨YÓ6Ð+Ø	×	Ñ	€BÜ.¨rÓ2°R×5TÑ5TÑTÐTr&   c                  óP   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z	dd„Z
dd	„Zy
)ÚAttributeMaybeDefinedVisitorz³Find attributes that may have been defined via some code path.

    Consider initializations in class body and assignments to 'self.x'
    and calls to base class '__init__'.
    c                ó   — || _         y ©N©rO   ©ÚselfrO   s     r$   r*   z%AttributeMaybeDefinedVisitor.__init__5  ó	   € Ø ˆ�r&   c                ó*   — t        «       t        «       fS rt   ©r   ©rw   rY   s     r$   Úvisit_branchz)AttributeMaybeDefinedVisitor.visit_branch8  ó   € Ü‹u”c“eˆ|Ðr&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   Úvisit_returnz)AttributeMaybeDefinedVisitor.visit_return;  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   Úvisit_unreachablez.AttributeMaybeDefinedVisitor.visit_unreachable>  r}   r&   c                óP  — t        |t        «      r/|j                  | j                  u r|j                  ht        «       fS t        |t        «      rD|j                  j                  r.|j                  j                  dk(  rt        |«      t        «       fS t        «       t        «       fS ©Nr*   )rK   r   r[   rO   r\   r   r   rg   Ú
class_namerD   rp   r{   s     r$   Úvisit_register_opz.AttributeMaybeDefinedVisitor.visit_register_opA  ss   € Ü�bœ'Ô" r§v¡v°·±Ñ'>Ø—G‘G�9œc›eÐ#Ð#Ü�bœ$Ô B§E¡E×$4Ò$4¸¿¹¿¹ÀzÒ9QÜ<¸RÓ@Ä#Ã%ÐGÐGÜ‹u”c“eˆ|Ðr&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   Úvisit_assignz)AttributeMaybeDefinedVisitor.visit_assignH  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   Úvisit_assign_multiz/AttributeMaybeDefinedVisitor.visit_assign_multiK  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   Úvisit_set_memz*AttributeMaybeDefinedVisitor.visit_set_memN  r}   r&   N©rO   r   ÚreturnÚNone©rY   r   r�   útuple[set[str], set[str]]©rY   r   r�   r�   ©rY   r   r�   r�   ©rY   r   r�   r�   ©rY   r   r�   r�   ©rY   r   r�   r�   ©rY   r   r�   r�   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   r|   r   r�   r…   r‡   r‰   r‹   © r&   r$   rr   rr   .  ó/   „ ñó!óóóóóóôr&   rr   c                ó>   — t        | |t        |«      |dt        ¬«      S ©NF)r<   r,   Úgen_and_killÚinitialÚbackwardÚkind)r
   rr   r   )r<   rO   r7   r,   s       r$   r=   r=   R  s(   € ô ØØÜ1°(Ó;Ø#ØÜôð r&   c                  óP   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z	dd„Z
dd	„Zy
)ÚAttributeMaybeUndefinedVisitorz«Find attributes that may be undefined via some code path.

    Consider initializations in class body, assignments to 'self.x'
    and calls to base class '__init__'.
    c                ó   — || _         y rt   ru   rv   s     r$   r*   z'AttributeMaybeUndefinedVisitor.__init__f  rx   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   r|   z+AttributeMaybeUndefinedVisitor.visit_branchi  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   r   z+AttributeMaybeUndefinedVisitor.visit_returnl  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   r�   z0AttributeMaybeUndefinedVisitor.visit_unreachableo  r}   r&   c                óP  — t        |t        «      r/|j                  | j                  u rt	        «       |j
                  hfS t        |t        «      rD|j                  j                  r.|j                  j                  dk(  rt	        «       t        |«      fS t	        «       t	        «       fS rƒ   )rK   r   r[   rO   r   r\   r   rg   r„   rD   rn   r{   s     r$   r…   z0AttributeMaybeUndefinedVisitor.visit_register_opr  st   € Ü�bœ'Ô" r§v¡v°·±Ñ'>Ü“5˜2Ÿ7™7˜)Ð#Ð#Ü�bœ$Ô B§E¡E×$4Ò$4¸¿¹¿¹ÀzÒ9QÜ“5Ô=¸bÓAÐAÐAÜ‹u”c“eˆ|Ðr&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   r‡   z+AttributeMaybeUndefinedVisitor.visit_assigny  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   r‰   z1AttributeMaybeUndefinedVisitor.visit_assign_multi|  r}   r&   c                ó*   — t        «       t        «       fS rt   rz   r{   s     r$   r‹   z,AttributeMaybeUndefinedVisitor.visit_set_mem  r}   r&   NrŒ   r�   r‘   r’   r“   r”   r•   r–   r—   rœ   r&   r$   r¥   r¥   _  r�   r&   r¥   c                ó>   — t        | |t        |«      |dt        ¬«      S rŸ   )r
   r¥   r   )r<   rO   r+   r,   s       r$   r@   r@   ƒ  s(   € ô ØØÜ3°HÓ=Ø!ØÜôð r&   c                ó$  — | |v ry| j                   €yt        «       }| j                  D ]?  }| j                   D ].  }t        ||«       ||j                  vsŒ|j	                  |«       Œ0 ŒA | xj                  |z  c_        |j	                  | «       y)zJRemove attributes not defined in all subclasses from always defined attrs.N)r2   r   r9   r   r-   )r#   r"   Úremovedr\   Úchilds        r$   r   r   �  sŠ   € à	ˆT�zØØ	‡{�{ÐàÜ‹e€GØ×,Ñ,ò "ˆØ—[‘[ò 	"ˆEÜ8¸ÀÔEØ˜5×:Ñ:Ò:Ø—‘˜DÕ!ñ	"ð"ð
 × Ò  GÑ+Õ Ø‡H�HˆR…Lr&   c                ó°  — | j                   ry | |v ry |j                  | «       | j                  dd  D ]  }t        ||«       Œ g }t	        | j                  «      dkD  r(|j                  | j                  d   j                  «       | j                  j                  «       D ]5  \  }}|j                  sŒ| j                  |«      rŒ%|j                  |«       Œ7 | j                  dd  D ]f  }|j                   sŒ|j                  j                  «       D ]:  \  }}|j                  sŒ| j                  |«      rŒ%||vsŒ*|j                  |«       Œ< Œh || _        y )Nr)   )r.   r-   Úbase_mror    ÚlenÚextendÚbitmap_attrsr?   ÚitemsÚerror_overlaprl   Úappendr5   )r#   r"   rM   Ú	new_attrsÚnÚts         r$   r    r    ¡  s/  € Ø	‡{‚{Øà	ˆT�zØØ‡H�HˆR„LØ—‘˜A˜B�ò ,ˆÜ  dÕ+ð,ð €IÜ
ˆ2�;‰;Ó˜!ÒØ×Ñ˜Ÿ™ Q™×4Ñ4Ô5Ø—‘×#Ñ#Ó%ò  ‰ˆˆ1Ø�?‹? 2×#7Ñ#7¸Õ#:Ø×Ñ˜QÕð ð —‘�q�r�
ò (ˆØ�=‹=ØŸ™×-Ñ-Ó/ò (‘��1Ø—?“?¨2×+?Ñ+?ÀÕ+BÀqÐPYÒGYØ×$Ñ$ QÕ'ñ(ð(ð
  €B…Or&   N)r!   zlist[ClassIR]r�   rŽ   )r#   r   r"   zset[ClassIR]r�   rŽ   )r<   úlist[BasicBlock]rO   r   rR   úset[str]rQ   úAnalysisResult[str]rS   r¿   rP   úAnalysisResult[None]r�   r¾   )
r<   r½   rO   r   rQ   r¿   rP   rÀ   r�   r¾   )
r<   r½   rO   r   rQ   r¿   rP   rÀ   r�   rŽ   )rY   r   r�   r¾   )
r<   r½   rO   r   r7   r¾   r,   r   r�   r¿   )
r<   r½   rO   r   r+   r¾   r,   r   r�   r¿   )3r›   Ú
__future__r   Útypingr   Úmypyc.analysis.dataflowr   r   r   r   r	   r
   Úmypyc.analysis.selfleaksr   Úmypyc.ir.class_irr   Úmypyc.ir.opsr   r   r   r   r   r   r   r   r   r   r   r   r   Úmypyc.ir.rtypesr   r   Ú__annotations__r%   r   rA   rF   rG   Útupler   ÚstrÚ
GenAndKillrn   rp   rr   r=   r¥   r@   r   r    rœ   r&   r$   ú<module>rÌ      s³  ðò=õ~ #å ÷÷ õ 8Ý %÷÷ ÷ õ õ &ð #Ð �UÓ "ó*ó8="ð@/Øð/àð/ð ð/ð 'ð	/ð
 )ð/ð  ð/ð ó/ðdØðàðð 'ðð  ð	ð
 óð,-Øð-àð-ð 'ð-ð  ð	-ð
 
ó-ð& �3�s‘8˜S ™XÐ%Ñ&€
óXóUô!Ð#6°sÑ#;ô !ðH
Øð
Ø(0ð
ØGOð
ØVYð
àó
ô!Ð%8¸Ñ%=ô !ðH
Øð
Ø(0ð
ØEMð
ØTWð
àó
óô" r&   