NL iMUdZddlmZddlmZddlmZmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZddlmZmZm Z ddl!m"Z"ddl#m$Z$m%Z%dd l&m'Z'm(Z(dd l)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7dd l8m9Z9m:Z:m;Z;mZ>m?Z?dd l@mAZAmBZBdd lCmDZDmEZEmFZFmGZGmHZHddlImJZJddlKmLZLmMZMmNZNmOZOddlPmQZQmRZRddlSmTZTmUZUmVZVmWZWmXZXddlYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_ddl`maZad4dZbGddZcGddecZdGddecZeGddeeZfGddefZgd5dZh d6d Zid!Zjd"ekd#<d5d$Zld7d%Zm d8d&Zn d9 d:d(Zo d;d)Zp d9 dd,Zsd4d-Ztd?d.Zu d@d/ZvdAd0Zw dBd1ZxdCd2Zy dD dEd3Zzy')Fz9Transform class definitions from the mypy AST form to IR.) annotations)abstractmethod)CallableFinal)EXCLUDED_ENUM_ATTRIBUTESTYPE_VAR_TUPLE_KINDAssignmentStmtCallExprClassDef Decorator EllipsisExprExpressionStmtFuncDefLvalue MemberExprNameExprOverloadedFuncDefPassStmtRefExprStrExprTempNodeTypeInfo TypeParam is_class_var)Instance UnboundTypeget_proper_type)PROPSET_PREFIX)ClassIRNonExtClassInfo)FuncDecl FuncSignature)NAMESPACE_TYPE BasicBlockBranchCall InitStatic LoadAddressLoadErrorValue LoadStatic MethodCallRegisterReturnSetAttrTupleSetValue)RTypebool_rprimitivedict_rprimitiveis_none_rprimitiveis_object_rprimitiveis_optional_typeobject_rprimitive) IRBuildercreate_type_params)gen_property_getter_irgen_property_setter_irhandle_ext_methodhandle_non_ext_method load_type)GENERATOR_HELPER_NAME)dataclass_type get_func_def is_constantis_dataclass_decorator) dict_new_opexact_dict_set_item_op)iter_opnext_oppy_get_item_op py_hasattr_op py_setattr_op)dataclass_sleight_of_hand import_opnot_implemented_oppy_calc_meta_oppytype_from_template_optype_object_op) is_subtypec |j|jjvr|jd|jy|jj|j t fdt t jdz Dr|jd|j jrS jD]D}|jr|jdj|j|jF jrHt|}| t||}n:|dvr t!||}n)|dk(r t#||}nt%|t'||}|j(j*D]}t-|t.t0t2fr=|j4t6k(r*|jd |j4d |jt-|t2r|j8r|t-|t&r|jd |j|j:D]@}|j=|j5|j?tA|dddBt-|t.t0t2fro|jjB|j4jDr>|j=|j5|j?tA|ddd~t-|tFs*t-|tHrt-|jJtLrt-|tNrt|jPdk7r|jd |j|jPd }t-|tRs|jd|j=|jU||Qt-|tHrt-|jJtVr}|jd|j jXj;D]\}} | jZs| j\s t_|| | j`} |jbje| | jf| jhj4<d} tj|z} | jXvrOtm| jX| | j`} |jbje| | jf| <| | f jn|<|jq y#1swYxYw#1swYxYw)aCreate IR for a class definition. This can generate both extension (native) and non-extension classes. These are generated in very different ways. In the latter case we construct a Python type object at runtime by doing the equivalent of "type(name, bases, dict)" in IR. Extension classes are defined via C structs that are generated later in mypyc.codegen.emitclass. This is the main entry point to this module. z&Nested class definitions not supportedNc3vK|]0}j|jj|dzk72yw)N)base_mrobase).0iirs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypyc/irbuild/classdef.py z&transform_class_def..ms1 Z2;;q>  "++a!e"4 4 Zs69rTz9Multiple inheritance is not supported (except for traits)z5Base class "{}" does not allow interpreted subclasses) dataclassesz attr-autoattrz Method name "z$" is reserved for mypyc internal usez7Property setters not supported in non-extension classesz1Multiple assignment in class bodies not supportedrz9Only assignment to variables is supported in class bodiesz#Unsupported statement in class body)9infomapper type_to_irerrorlineanyrangelenrUallow_interpreted_subclassesmroformatfullname is_ext_classr@ExtClassBuilderDataClassBuilderAttrsClassBuilder ValueErrorNonExtClassBuilderdefsbody isinstancerr rnamer? is_propertyitems catch_errors add_methodrAnamesplugin_generatedrrexprr r lvaluesradd_attrr method_declsimplicitis_prop_getterr:is_trait functionsappendmethodsdeclrr; propertiesfinalize)buildercdefparentcls_type cls_builderstmtitemlvaluersr getter_ir setter_ir setter_namerYs @rZtransform_class_defrXsR yy111 > J  " "499 -B  ZeC DTWXDX>Y ZZ QSWS\S\] &&ff F66 KRRII   !$'  (7(FK 5 5*7D9K  +GT:KX& &($7  /L tgy2CD E 22 MM {*NOQUQZQZ  d- .43C3C+'9: WY]YbYbc  ?))$))4?**<+=>?? ?w 3DE Fyytyy)::%%dii0 ;&&|D'9: ; ; h ' t^ ,DII|1T  n -4<< A% QSWS\S\]\\!_Ffh/ OQUQZQZ   . n -*TYY2P  MM? K_/Ldoo++-9 d ==T00.wdBKKPI    $ $Y /.7BJJy~~** +I(4/Kboo-2R__[94 !!((3*3 ;'#,i"8BMM$ 9&m?? ; ;sU!UU U cNeZdZdZddZeddZed dZed dZy) ClassBuilderzKCreate IR for a class definition. This is an abstract base class. c.||_||_g|_yN)rrattrs_to_cache)selfrrs rZ__init__zClassBuilder.__init__s  :<cy)zAdd a method to the class IRNrfdefs rZrwzClassBuilder.add_methodrcy)z Add an attribute to the class IRNrrrrs rZr|zClassBuilder.add_attrrrcy)z5Perform any final operations to complete the class IRNr)rrYs rZrzClassBuilder.finalizerrNrr8rr returnNonerrrrrrrr rrrYrrr) __name__ __module__ __qualname____doc__rrrwr|rrrrZrrsJ = ++//DDrrc@eZdZdfd ZddZddZd dZd dZxZS) rocPt||||j|_yrsuperrcreate_non_ext_infonon_extrrr __class__s rZrzNonExtClassBuilder.__init__" $'//1 rcNt|j|j}t|j|j|}t |j|j||}|jj t g|jj}t||||Sr) populate_non_ext_basesrrfind_non_ext_metaclasssetup_non_ext_dictcall_crDrbr )r non_ext_basesnon_ext_metaclass non_ext_dict non_ext_annss rZrz&NonExtClassBuilder.create_non_ext_infos}.t||TYYG 24<<MZ) LL$))%6  ||**;DIINNK |]LJ[\\rc\t|j|j|j|yr)r=rrrrs rZrwzNonExtClassBuilder.add_methodsdllDLL$))TJrct|j|j||t|j|j|||j|j yr)add_non_ext_class_attr_annrradd_non_ext_class_attrrrrs rZr|zNonExtClassBuilder.add_attrs?"4<<vtL LL$,,diiATAT rct|j||j|jj}t |j|j|}|jj ||jj|jjt||jj|jjt|jjt|jj|jj|jj|g|jjt!|j|j"|jyr)load_non_ext_classrrrrbload_decorated_classset_immortal_if_free_threadedaddr'rs module_namer#rrEload_globals_dictload_strcache_class_attrsr)rrY non_ext_classs rZrzNonExtClassBuilder.finalizes*4<<T\\499>>Z ,T\\499mT  22=$))..Q  }diinndll6N6NP^ _  " ..0 %%diinn5  IINN  $,,(;(;TYYGrrrr rrr) rrrrrrwr|r __classcell__rs@rZroros2 ]K Hrroc@eZdZdfd ZddZddZd dZd dZxZS) rkcHt|||t|||_yr)rrallocate_classtype_objrs rZrzExtClassBuilder.__init__!s  $'&4Wd&C rcy)z?Controls whether to skip generating a default for an attribute.Frrrsrs rZskip_attr_defaultz!ExtClassBuilder.skip_attr_default&srcFt|j|j|yr)r<rrrs rZrwzExtClassBuilder.add_method*s$,, 48rcft|jtryt|s |jsy|j j |jj}|j j|j}|j jt||j j|j|g|j|j jr?|jr2|j j!|||jjyyyr)rrrvaluerr is_final_defrload_native_type_objectrriaccept primitive_oprJrrsrbnon_function_scopeinit_final_static)rrrtypvalues rZr|zExtClassBuilder.add_attr-s dkk8 , V$(9(9 ll224993E3EF ##DKK0 !! C!6!6v{{!CUKTYY  << * * ,1B1B LL * *65$)).. I2C ,rct|j|j|j\}}|jj |t |j|j|t|j|jyr)find_attr_initializersrrrattrs_with_defaultsupdategenerate_attr_defaults_initcreate_ne_from_eq)rrYrdefault_assignmentss rZrzExtClassBuilder.finalize<sf3I LL$))T%;%;4 00 %%&9:#DLL$))=PQ$,, 2rrrsstrrr rboolrrr) rrrrrrwr|rrrs@rZrkrk sD 9 J3rrkcTeZdZdZdfd Zd dZd dZd dZd fd Zd fd Z xZ S)rlTcPt||||j|_yrrrs rZrzDataClassBuilder.__init__Krrc t|jjtg|jj |jj tg|jj |jjtg|jj |jj ttjtj|jj S)aSet up a NonExtClassInfo to track dataclass attributes. In addition to setting up a normal extension class for dataclasses, we also collect its class attributes like a non-extension class so that we can hand them to the dataclass decorator. ) r rrrDrrbrr/r(rPtypesrc)rs rZrz$DataClassBuilder.create_non_ext_infoOs LL   R @ LL  Xb$))..9 : LL   R @ LL  [)<)P>PRVR[R[R`R`a b   rc|jduSrrrs rZrz"DataClassBuilder.skip_attr_default]syy$$rcft|j}t|tr |jSyr)rrrrr)rrann_types rZget_type_annotationz$DataClassBuilder.get_type_annotation`s)#499- h )== rct|j|j|||jt |j|j|||j |j t|!||yr) rrrrrrrrr|)rrrrs rZr|zDataClassBuilder.add_attrhs^" LL$,,d6N6N   LL$,,diiATAT  &rczt|||jsJt|j|j |j j|j|jjtd|j jD}|jjt|j xsd}|jjt||j|j j |j j"|g|j jy)apGenerate code to finish instantiating a dataclass. This works by replacing all of the attributes on the class (which will be descriptors) with whatever they would be in a non-extension class, calling dataclass, then switching them back. The resulting class is an extension class and instances of it do not have a __dict__ (unless something else requires it). All methods written explicitly in the source are compiled and may be called through the vtable while the methods generated by dataclasses are interpreted and may not be. (If we just called dataclass without doing this, it would think that all of the descriptors for our attributes are default values and generate an incorrect constructor. We need to do the switch so that dataclass gets the appropriate defaults.) c38K|]}t|s|ywr)rC)rWds rZr[z,DataClassBuilder.finalize..sNq4J14MNsunknownN)rrradd_dunders_to_non_ext_dictrrrrbadd_annotations_to_dictrnext decoratorsrr@rrKdictanns)rrYdecdataclass_type_valrs rZrzDataClassBuilder.finalizeqs$ }}}# LL$,, 8T8T ll!! NDII00N N "\\22>$))3L3YPYZ  % $--!2!2DLL4E4EGY Z IINN rrrrrr rzTypeInfo | Nonerr) rrrrrrrrr|rrrs@rZrlrlEs.#2  %'  rrlc$eZdZdZdZddZddZy)rmaSCreate IR for an attrs class where auto_attribs=False (the default). When auto_attribs is enabled, attrs classes behave similarly to dataclasses (i.e. types are stored as annotations on the class) and are thus handled by DataClassBuilder, but when auto_attribs is disabled the types are provided via attr.ib(type=...) Fcy)NTrrs rZrz#AttrsClassBuilder.skip_attr_defaultsrct|jtr|jj}t|tr|j dvrd|jj vr|jj jd}|jj|}t|trLt|jtr2|jd}t|tsJ||jSy)N)zattr.ibz attr.attrrr) rrrr calleerri arg_namesindexargsrnoderr{)rrrr  type_namers rZrz%AttrsClassBuilder.get_type_annotations dkk8 ,[[''F6:.OO'??dkk333 --33F; KK,,U3 i2z)..RZ7[!\\!_F%fh7??7$>>)rNrr)rrrrrrrrrrZrmrms$rrmc |j|jz}|j}|r$t||j||j g}ng}|s|rK|j |Dcgc]}|j|c}|j||j }n |jttd}|j|j}|jtt|jdz|jt }|j#t$|||g|j } |j'| |j |j(j*|j,} | j.sY| j0sM|jt3t5|jdzd|jt7gt8gd|j;t<| |jdt?||j(j*|j,|j g|j |jtA| |j|jt |j#tB|jE|j|j| g|j | Scc}w)NT) is_borrowed _template_trait_vtable_setup__mypyc_attrs__)#base_type_exprsremoved_base_type_exprs type_argsmake_generic_base_classrirbextendr new_tuplerr)r7rrr*rsr#rrOrr_r`r^r builtin_baser&r!r"r2rrJcreate_mypyc_attrs_tupler'rEr) rr base_exprsnew_style_type_argsbasesxtp_basesmodnametemplatetprYs rZrrsH%%(D(DDJ..($--ATVZV_V_`a( .s010szbuiltins.object)ztyping.Sequenceztyping.Iterableztyping.Collectionztyping.Reversibleztyping.Containerz typing.Sizedtyping _TypedDictzbuiltins.tuple _NamedTuple.rTr)r^is_named_tupler_r`rrichildrenrr0get_module_attrrbrsplitcurrent_moduleload_global_strrsload_module_attr_by_fullnamer) rrr8rYrclsbase_irmodulersrV cls_modules rZrrs YY--N  " "499 -B E0 0& <<, ,  cll/    '..++ +nn//4G+  ''+ <<3 3FD**64CD  0@ @ D**8T499ED,,S!4Q7JW333..sxxC;;CLL$))T T <<3 3 M&N   UDII ..rc|jr|j|j}n|jj|j dd|j S|jj r|j dd|j S|jttjtj|j }|jt||g|j S)z6Find the metaclass of a class from its defs and bases.r4_TypedDictMetaNamedTupleMeta) metaclassrr^typeddict_typer:rbr8rr(rPrrrrN)rrrdeclared_metaclasss rZrrEs ~~$^^DNN; 99 # # /**85EtyyQ Q YY % %**85EtyyQ Q$[[ ++^-?-? K  >>/,>+F RRrc|jt||jdg|j}t t }t t t }}}|j||||j||j|j} |j|d|j} |j| | |g|j} |j|| |j|j||j||j||jtg|j|j|j||j||S)zInitialize the class dictionary for a non-extension class. This class dictionary is passed to the metaclass constructor. __prepare__)rrIrrbr,r3r$add_bool_branchactivate_blockrsr(py_callassigngotorrD) rrrFr has_preparer true_block false_block exit_blockcls_name prepare_meth prepare_dicts rZrrZs(&& 7#3#3M#BCTYYKO,L*4, jlZ J K[A :& *H&&y-KL??<(E1BDIINL NN<tyy9 LL ;' NN< R!KTYYW LL :& rNcjd}|z||}|rp|j|jk(r5|j|jkr|j|j}n"t |||j |j}|t|j}t|j tr<|j j&|j|j j}nt|tr-t ||j|j |j}nB|jttjtj |j}|j|j"}|j%t&|j(||g|jy)zBAdd a class attribute to __annotations__ of a non-extension class.N)r<rrbrrir>unanalyzed_typerrrrroriginal_str_exprrrr(rPrrsrrEr) rrrr get_type_infor type_inforkeys rZrrzsJC !$' %%)>)>>499y~~C]&&y'9'9:D4H4H$))T {"499- t++[ 9$$66B""4#7#7#I#IJC ( +GX]]D4H4H$))TC++k.*=*=~?Q?QSWS\S\]^C   6;; 'C NN)GLL#s+CTYYOrct|jts|j|j}|j ||j ||j |jjrY|jjdjjr+|j tvr|j|tfyyyyy)z;Add a class attribute to __dict__ of a non-extension class.rN)rrrrradd_to_non_ext_dictrsrbr^rris_enumrrr7)rrrrr attr_to_cachers rZrrs dkk8 , ,##GV[[&$))L IIOO #((00 #;;  &*;!< =<1  -rc|jj|j}|jr t gfSt }g}t |jj D]w}||jjvr|jjjD]7}t|tst|jdts3t|jdrLt|jt rg|jdj"}|dk(r|dk(rt%|||j&| |||r|j)|} t|jt*r<|jj,dk(r#t/| st1| s t3| s|j5||j7|:z||fS)a Find initializers of attributes in a class body. If provided, the skip arg should be a callable which will return whether to skip generating a default for an attribute. It will be passed the name of the attribute and the corresponding AssignmentStmt. r __slots__ __deletable__z builtins.None)r_r`r^rsetreversedrgdefnrprqrrr r{rrrrrscheck_deletable_declarationrb attr_typerrir6r5r4rr) rrskipr?rrr^rrsrhs rZrrs .. # #DII .C uby%'"1 w~~00 0 IINN'' 1D40t||A9$T\\!_5"4;;9||A++;&?*/diiH#T4(8MM$/ dkk73 8L8LP_8_,Y7 4Y ? 29 = #''-#**40? 1"1H  3 33rc.|sy|jj|j}|jry|j |dt 5|j }|D]}|jd}t|tsJ||js;t|js&|jd|jj|j|j }|j#|j%|j||j}t'||j |d} | j)|j+| |j+t-|j/dddy#1swYyxYw)zLGenerate an initialization method for default attr values (from class vars).N__mypyc_defaults_setuprz#Unsupported default attribute valuer)r_r`r^r enter_methodr2rr{rrrrrBrwarningrbrhrscoercerr.mark_as_initializerrr-true) rrrr?self_varrrrhvalinits rZrrs9  .. # #DII .C    c#;_ M,<<>' D\\!_Ffh/ 7 7/$$[-E Et{{GWGWX fkk2I.. !*+,,,s D7F  Fc|jD]}||jvs|j|s|jd|d|:|jD](}||j vs|jd|d|p|j |\}}|jdj||j|j|y)Nz Attribute "z " not definedzCannot make property "z " deletablez4Attribute "{}" not defined in "{}" (defined in "{}")) deletable attributeshas_attrrargproperty_types attr_detailsrhrs)rclrbr]rV_s rZrgrgs  r}} $;;t$ D6?F 4...MM$:4& "LdS  //$/4 PXXbggtyy rc|jj|j}|jdr*|jdst |||j yyy)zHCreate a "__ne__" method from a "__eq__" method (if only latter exists).__eq____ne__N)r_r`r^ has_methodgen_glue_ne_methodrb)rrr?s rZrr+sJ .. # #DII .C ~~hx(@7C3)Arc |jd}|sJ|jj}|jj}|j |d|j 5|r|jdjnt}|jd|}|jt|jd|g|}ttj|j } t|j t } |r| rt#t#} } |jt%tjtj&|} |jt)|j+|| d|| | t(j,|j/| | r|rt nt}|j1|j2j5||d||}|jt7||j/| |jt7| nN| r|rt nt}|j1|j9|d|||}|jt7|d d d y #1swYy xYw) z2Generate a "__ne__" method from a "__eq__" method.r}r~rrhsisT) likely_boolnotN) get_methodrsigoptionsstrict_dunders_typingrlret_typer rr7 add_argumentrr+rrQrMr2r$r(rr%translate_is_opBOOLrLrnr unary_notr-unary_op)rr?rbfunc_ireq_sig strict_typingrhs_typerhs_argeqvalcan_return_not_implemented return_boolnot_implemented_block regular_blocknot_implementedrettyperetvals rZrr2snnX&G N7 \\  FOO99M   c8V__ =!(*76;;q>&&=N&&uh7 Jw||~x'DQR%/0B0G0G%Y" /B  :3=<= !%kk.335G5K5KTRO KK++E?D$O)!KK    " "= 1)4oL]G^^))%4)H'SWF KKv '  " "#8 9 KK/ 0)4oL]G^^G$4$4UE4$H'SWXF KKv 'C!(!(!(s HI88Jc|j|j}t||||j|j||j |j g|}|Sr)rrsrrMrFrr)rrYrrbrTclass_type_objs rZrr\sV(H$7__HgmmW\\BDN rc|j}|}t|D]R}|j|j}t |t sJ||j ||g|j}T|S)amApply class decorators to create a decorated (non-extension) class object. Given a decorated ClassDef and a register containing a non-extension representation of the ClassDef created via the type constructor, applies the corresponding decorator functions on that decorated ClassDef and returns a register containing the decorated ClassDef. )rrervisitorrrr0rMrb)rrrr dec_classr decorators rZrrisoJI j !LHHW__- )U+6Y6+OOI {INNK L rc|j|jj}|D]b\}}t|tsJ||j ||j |j}|j|||j |dy)z6Add class attributes to be cached to the global cache.) type_overrideN) rr^rirrrr(rsrbr)rrrrlvalrtypervals rZrr{s  ) )$))*<*< =C%N e$)/4/)""3 499=!!$diiu!MNrc |jDcgc]}|jD]}|}}}|jr|jd|Dcgc]}|j |}}|j ||Scc}}wcc}w)N__dict__)rgrvinherits_pythonrrr)rrYrbancestorrsattrsr]rus rZrrs|"$&& IhX5H5H ITT IT IE I  Z 05 6W  d # 6E 6   UD )) J 7s A:Bc|r|j|d|j|d}|j|d|j|||j|d|j|j|y)N__annotations__zmypyc filler docstringrr)r^rrr)rrrbadd_annotationsfiller_doc_strs rZrrsm##G-> dS .N G4D4D^4TVZ[ w7G7GH[H[7\^bcrr)rr8rr rr0) rr8rirrzlist[TypeParam]rbintrr0)rr8rr rr0rr0) rr8rr rFr0rr0rr0r) rr8rr rrrr rZz2Callable[[AssignmentStmt], TypeInfo | None] | Nonerr)rr8rr rrrr rr r`list[tuple[Lvalue, RType]]rr)rr8rr riz,Callable[[str, AssignmentStmt], bool] | Nonerz%tuple[set[str], list[AssignmentStmt]])rr8rr rzlist[AssignmentStmt]rr)rr8rzrrbrrr)rr8r?rrbrrr) rr8rYrrr rbrrr0)rr8rr rr0rr0)rr8rrrr rr)rr8rYrrbrrr0)T) rr8rr rbrrrrr){r __future__rabcrr4rr mypy.nodesrrr r r r r rrrrrrrrrrrrr mypy.typesrrr mypyc.commonrmypyc.ir.class_irrr mypyc.ir.func_irr!r" mypyc.ir.opsr#r$r%r&r'r(r)r*r+r,r-r.r/r0mypyc.ir.rtypesr1r2r3r4r5r6r7mypyc.irbuild.builderr8r9mypyc.irbuild.functionr:r;r<r=r>mypyc.irbuild.preparer?mypyc.irbuild.utilr@rArBrCmypyc.primitives.dict_opsrDrEmypyc.primitives.generic_opsrFrGrHrIrJmypyc.primitives.misc_opsrKrLrMrNrOrP mypyc.subtyperQrrrorkrlrmrrr0rrrrrrrrrgrrrrrrrrrrZrs?"",>='64 @8``I%wtDD03H3Hl"3l"3JK K \(@=@ "%2AIL 440 //dS* &38AF JIM +P +P +P +P  +P F +P  +P\> > > >  >  > . > >8^b64 64&64.Z64*64r, ,&,=Q, ,6(4'(T   # .= EH   $N N(BNJRN N*VZ d  d!0 d8; dNR d  dr