U °–_5ã@s ddlmZGdd„deƒZdS)é)Úc_astc@s¸eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdldd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Zd;d<„Z d=d>„Z!d?d@„Z"dAdB„Z#dCdD„Z$dEdF„Z%dGdH„Z&dIdJ„Z'dKdL„Z(dMdN„Z)dOdP„Z*dQdR„Z+dSdT„Z,dUdV„Z-dWdX„Z.dYdZ„Z/d[d\„Z0d]d^„Z1dmd_d`„Z2dadb„Z3gfdcdd„Z4dedf„Z5dgdh„Z6didj„Z7dkS)nÚ CGeneratorz¬ Uses the same visitor pattern as c_ast.NodeVisitor, but modified to return a value from each visit method, using string accumulation in generic_visit. cCs d|_dS)Né©Ú indent_level©Úself©r ú/c_generator.pyÚ__init__szCGenerator.__init__cCs d|jS©Nú rrr r r Ú _make_indentszCGenerator._make_indentcCsd|jj}t|||jƒ|ƒS)NZvisit_)Ú __class__Ú__name__ÚgetattrÚ generic_visit)rÚnodeÚmethodr r r Úvisits zCGenerator.visitcs,|dkr dSd ‡fdd„| ¡Dƒ¡SdS)NÚc3s|]\}}ˆ |¡VqdS©N©r)Ú.0Zc_nameÚcrr r Ú #sz+CGenerator.generic_visit..)ÚjoinZchildren)rrr rr rszCGenerator.generic_visitcCs|jSr)Úvalue©rÚnr r r Úvisit_Constant%szCGenerator.visit_ConstantcCs|jSr©Únamerr r r Úvisit_ID(szCGenerator.visit_IDcCs$| |j¡}|d| |j¡dS)Nú[ú])Ú_parenthesize_unless_simpler"rZ subscript)rrZarrrefr r r Úvisit_ArrayRef+s zCGenerator.visit_ArrayRefcCs"| |j¡}||j| |j¡Sr)r&r"ÚtyperZfield)rrZsrefr r r Úvisit_StructRef/s zCGenerator.visit_StructRefcCs$| |j¡}|d| |j¡dS)Nú(ú))r&r"rÚargs)rrZfrefr r r Úvisit_FuncCall3s zCGenerator.visit_FuncCallcCs\| |j¡}|jdkrd|S|jdkr0d|S|jdkrJd| |j¡Sd|j|fSdS)Nzp++z%s++zp--z%s--Zsizeofz sizeof(%s)z%s%s)r&ÚexprÚopr)rrZoperandr r r Ú visit_UnaryOp7s    zCGenerator.visit_UnaryOpcs<ˆ |j‡fdd„¡}ˆ |j‡fdd„¡}d||j|fS)Ncs ˆ |¡ Sr©Ú_is_simple_node©Údrr r ÚFóz+CGenerator.visit_BinaryOp..cs ˆ |¡ Srr1r3rr r r5Hr6ú%s %s %s)Ú_parenthesize_ifÚleftÚrightr/)rrZlval_strÚrval_strr rr Úvisit_BinaryOpDs ÿ ÿzCGenerator.visit_BinaryOpcCs*| |jdd„¡}d| |j¡|j|fS)NcSs t|tjƒSr)Ú isinstancerÚ Assignment)rr r r r5Nr6z-CGenerator.visit_Assignment..r7)r8ZrvaluerZlvaluer/)rrr;r r r Úvisit_AssignmentKs þzCGenerator.visit_AssignmentcCs d |j¡Sr )rÚnamesrr r r Úvisit_IdentifierTypeQszCGenerator.visit_IdentifierTypecCsJt|tjƒrd| |¡dSt|tjƒr.)rÚdeclsÚlenr©rrrKr rr Úvisit_DeclListfs  ÿ zCGenerator.visit_DeclListcCs2d}|jr|d |j¡d7}|| |j¡7}|S)Nrr )ÚstoragerÚ_generate_typer(rQr r r Ú visit_Typedefms zCGenerator.visit_TypedefcCs(d| |j¡d}|d| |j¡S)Nr*r+r )rTZto_typer&r.rQr r r Ú visit_CastsszCGenerator.visit_CastcCs*g}|jD]}| | |¡¡q d |¡S©NrM©ZexprsÚappendrEr©rrZvisited_subexprsr.r r r Úvisit_ExprListws zCGenerator.visit_ExprListcCs*g}|jD]}| | |¡¡q d |¡SrWrXrZr r r Úvisit_InitList}s zCGenerator.visit_InitListcCsˆd}|jr|d|j7}|jr„|d7}t|jjƒD]H\}}||j7}|jr^|d| |j¡7}|t|jjƒdkr2|d7}q2|d7}|S)NÚenumr z {rGrrMrC)r"ÚvaluesÚ enumerateZ enumeratorsrrrP)rrrKÚiZ enumeratorr r r Ú visit_Enumƒs  zCGenerator.visit_Enumcsjˆ |j¡}dˆ_ˆ |j¡}|jrVd ‡fdd„|jDƒ¡}|d|d|dS|d|dSdS)Nrú; c3s|]}ˆ |¡VqdSrr)rÚprr r r–sz+CGenerator.visit_FuncDef..Ú )rrNrÚbodyZ param_declsr)rrrNreZknrdeclsr rr Ú visit_FuncDef‘s  zCGenerator.visit_FuncDefcCsBd}|jD]2}t|tjƒr*|| |¡7}q || |¡d7}q |S)Nrrb)Úextr=rZFuncDefr)rrrKrgr r r Ú visit_FileAST›s   zCGenerator.visit_FileASTcs`ˆ ¡d}ˆjd7_|jr>|d ‡fdd„|jDƒ¡7}ˆjd8_|ˆ ¡d7}|S)Nú{ érc3s|]}ˆ |¡VqdSr)Ú_generate_stmt)rÚstmtrr r r¨sz,CGenerator.visit_Compound..z} )rrZ block_itemsrrQr rr Úvisit_Compound¤s zCGenerator.visit_CompoundcCsdS)Nú;r rr r r Úvisit_EmptyStatement­szCGenerator.visit_EmptyStatementcsd ‡fdd„|jDƒ¡S)NrMc3s|]}ˆ |¡VqdSrr)rZparamrr r r±sz-CGenerator.visit_ParamList..)rZparamsrr rr Úvisit_ParamList°szCGenerator.visit_ParamListcCs&d}|jr|d| |j¡7}|dS)NÚreturnr rn)r.rrQr r r Ú visit_Return³szCGenerator.visit_ReturncCsdS)Nzbreak;r rr r r Ú visit_Break¸szCGenerator.visit_BreakcCsdS)Nz continue;r rr r r Úvisit_Continue»szCGenerator.visit_ContinuecCs8| |j¡d}|| |j¡d7}|| |j¡7}|S)Nz ? rF)rEÚcondÚiftrueÚiffalserQr r r Úvisit_TernaryOp¾szCGenerator.visit_TernaryOpcCsdd}|jr|| |j¡7}|d7}||j|jdd7}|jr`|| ¡d7}||j|jdd7}|S)Nzif (ú) T©Ú add_indentzelse )rurrkrvrwrrQr r r Úvisit_IfÄszCGenerator.visit_IfcCs~d}|jr|| |j¡7}|d7}|jr<|d| |j¡7}|d7}|jr^|d| |j¡7}|d7}||j|jdd7}|S)Nzfor (rnr ryTrz)rIrruÚnextrkrlrQr r r Ú visit_ForÎszCGenerator.visit_ForcCs:d}|jr|| |j¡7}|d7}||j|jdd7}|S)Núwhile (ryTrz)rurrkrlrQr r r Ú visit_WhileÙs zCGenerator.visit_WhilecCsJd}||j|jdd7}|| ¡d7}|jr>|| |j¡7}|d7}|S)Nzdo Trzrz);)rkrlrrurrQr r r Ú visit_DoWhileàszCGenerator.visit_DoWhilecCs,d| |j¡d}||j|jdd7}|S)Nzswitch (ryTrz)rrurkrlrQr r r Ú visit_SwitchèszCGenerator.visit_SwitchcCs6d| |j¡d}|jD]}||j|dd7}q|S)Nzcase ú: Trz)rr.Ústmtsrk©rrrKrlr r r Ú visit_Caseís zCGenerator.visit_CasecCs&d}|jD]}||j|dd7}q |S)Nz default: Trz)r„rkr…r r r Ú visit_Defaultós zCGenerator.visit_DefaultcCs|jd| |j¡S)Nrƒ)r"rkrlrr r r Ú visit_LabelùszCGenerator.visit_LabelcCsd|jdS)Nzgoto rnr!rr r r Ú visit_GotoüszCGenerator.visit_GotocCsdS)Nz...r rr r r Úvisit_EllipsisParamÿszCGenerator.visit_EllipsisParamcCs | |d¡S)NÚstruct©Ú_generate_struct_unionrr r r Ú visit_StructszCGenerator.visit_StructcCs | |j¡Sr)rTr(rr r r Úvisit_TypenameszCGenerator.visit_TypenamecCs | |d¡S)NÚunionrŒrr r r Ú visit_UnionszCGenerator.visit_UnioncCsbd}|jD]>}t|tjƒr*|d|j7}q t|tjƒr |d|jd7}q |d| |j¡7}|S)NrÚ.r$r%rG)r"r=rÚIDÚConstantrrr.)rrrKr"r r r Úvisit_NamedInitializer s   z!CGenerator.visit_NamedInitializercCs | |¡Sr)rTrr r r Úvisit_FuncDeclszCGenerator.visit_FuncDeclcCs~|d|jpd}|jrz|d7}|| ¡7}|jd7_|d7}|jD]}|| |¡7}qH|jd8_|| ¡d7}|S)ze Generates code for structs and unions. name should be either 'struct' or union. r rrdrjrirC)r"rOrrrk)rrr"rKrNr r r rs  z!CGenerator._generate_struct_unioncCs®t|ƒ}|r|jd7_| ¡}|r4|jd8_|tjtjtjtjtjtj tj tj tj tj tjtjtjf kr‚|| |¡dS|tjfkr˜| |¡S|| |¡dSdS)zÄ Generation from a statement node. This method exists as a wrapper for individual visit_* methods to handle different treatment of some statements in this context. rjrbrdN)r(rrrÚDeclr>ZCastZUnaryOpZBinaryOpZ TernaryOpÚFuncCallÚArrayRefÚ StructRefr”r“ZTypedefrDrZCompound)rrr{ÚtypÚindentr r r rk(s2ü  zCGenerator._generate_stmtcCsHd}|jrd |j¡d}|jr4|d |j¡d7}|| |j¡7}|S)z& Generation from a Decl node. rr )ZfuncspecrrSrTr(rQr r r rHDszCGenerator._generate_declcCsÌt|ƒ}|tjkrHd}|jr2|d |j¡d7}|| |j¡7}|jrN|jnd}t|ƒD]Ö\}}t|tj ƒr°|dkr–t||dtj ƒr–d|d}|d| |j ¡d7}qZt|tj ƒrþ|dkrät||dtj ƒräd|d}|d| |j ¡d7}qZt|tj ƒrZ|jr(d d |j¡|f}qZd |}qZ|rD|d|7}|S|tjkr`| |j¡S|tjkrx| |j¡S|tjkr”d |j¡dS|tj tj tj fkr¾| |j||g¡S| |¡Sd S) zø Recursive generation from a type node. n is the type node. modifiers collects the PtrDecl, ArrayDecl and FuncDecl modifiers encountered on the way down to a TypeDecl, to allow proper generation from it. rr rrr*r+r$r%z* %s %sÚ*N)r(rZTypeDeclZqualsrrZdeclnamer_r=Z ArrayDeclZPtrDeclZdimZFuncDeclr,r—rHZTypenamerTZIdentifierTyper@)rrZ modifiersr›rKZnstrr`Zmodifierr r r rTMs@             zCGenerator._generate_typecCs&| |¡}||ƒrd|dS|SdS)z‘ Visits 'n' and returns its string representation, parenthesized if the condition function applied to the node returns True. r*r+N)rE)rrZ conditionrKr r r r8{s  zCGenerator._parenthesize_ifcsˆ |‡fdd„¡S)z. Common use case for _parenthesize_if cs ˆ |¡ Srr1r3rr r r5ˆr6z8CGenerator._parenthesize_unless_simple..)r8rr rr r&…sz&CGenerator._parenthesize_unless_simplecCst|tjtjtjtjtjfƒS)z~ Returns True for nodes that are "simple" - i.e. nodes that always have higher precedence than operators. )r=rr”r“r™ršr˜rr r r r2ŠsÿzCGenerator._is_simple_nodeN)F)F)8rÚ __module__Ú __qualname__Ú__doc__r rrrr r#r'r)r-r0r<r?rArErLrRrUrVr[r\rarfrhrmrorprrrsrtrxr|r~r€rr‚r†r‡rˆr‰rŠrŽrr‘r•r–rrkrHrTr8r&r2r r r r r sj          . rN)rrÚobjectrr r r r Ú s