B
    CrX[L9  ã               @   s    d dl mZ G dd„ deƒZdS )é   )Úc_astc               @   sà  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dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdvd!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;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^„ Z1d_d`„ Z2dadb„ Z3dcdd„ Z4dedf„ Z5dgdh„ Z6dwdidj„Z7dkdl„ Z8g fdmdn„Z9dodp„ Z:dqdr„ Z;dsdt„ Z<duS )xÚ
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.
    c             C   s
   d| _ d S )Né    )Úindent_level)Úself© r   ú4lib/python3.7/site-packages/pycparser/c_generator.pyÚ__init__   s    zCGenerator.__init__c             C   s
   d| j  S )Nú )r   )r   r   r   r   Ú_make_indent   s    zCGenerator._make_indentc             C   s   d|j j }t| || jƒ|ƒS )NZvisit_)Ú	__class__Ú__name__ÚgetattrÚgeneric_visit)r   ÚnodeÚmethodr   r   r   Úvisit   s    zCGenerator.visitc                s,   |d krdS d  ‡ fdd„| ¡ D ƒ¡S d S )NÚ c             3   s   | ]\}}ˆ   |¡V  qd S )N)r   )Ú.0Zc_nameÚc)r   r   r   ú	<genexpr>#   s    z+CGenerator.generic_visit.<locals>.<genexpr>)ÚjoinZchildren)r   r   r   )r   r   r      s    zCGenerator.generic_visitc             C   s   |j S )N)Úvalue)r   Únr   r   r   Úvisit_Constant%   s    zCGenerator.visit_Constantc             C   s   |j S )N)Úname)r   r   r   r   r   Úvisit_ID(   s    zCGenerator.visit_IDc             C   s   d}|j r|d|j  7 }|S )Nz#pragmar
   )Ústring)r   r   Zretr   r   r   Úvisit_Pragma+   s    zCGenerator.visit_Pragmac             C   s$   |   |j¡}|d |  |j¡ d S )Nú[ú])Ú_parenthesize_unless_simpler   r   Z	subscript)r   r   Zarrrefr   r   r   Úvisit_ArrayRef1   s    zCGenerator.visit_ArrayRefc             C   s"   |   |j¡}||j |  |j¡ S )N)r!   r   Útyper   Zfield)r   r   Zsrefr   r   r   Úvisit_StructRef5   s    zCGenerator.visit_StructRefc             C   s$   |   |j¡}|d |  |j¡ d S )Nú(ú))r!   r   r   Úargs)r   r   Zfrefr   r   r   Úvisit_FuncCall9   s    zCGenerator.visit_FuncCallc             C   s\   |   |j¡}|jdkrd| S |jdkr0d| S |jdkrJd|  |j¡ S d|j|f S d S )Nzp++z%s++zp--z%s--Zsizeofz
sizeof(%s)z%s%s)r!   ÚexprÚopr   )r   r   Zoperandr   r   r   Úvisit_UnaryOp=   s    


zCGenerator.visit_UnaryOpc                s<   ˆ   |j‡ fdd„¡}ˆ   |j‡ fdd„¡}d||j|f S )Nc                s   ˆ   | ¡ S )N)Ú_is_simple_node)Úd)r   r   r   Ú<lambda>L   s    z+CGenerator.visit_BinaryOp.<locals>.<lambda>c                s   ˆ   | ¡ S )N)r,   )r-   )r   r   r   r.   N   s    z%s %s %s)Ú_parenthesize_ifÚleftÚrightr*   )r   r   Zlval_strÚrval_strr   )r   r   Úvisit_BinaryOpJ   s
    zCGenerator.visit_BinaryOpc             C   s*   |   |jdd„ ¡}d|  |j¡|j|f S )Nc             S   s   t | tjƒS )N)Ú
isinstancer   Ú
Assignment)r   r   r   r   r.   T   s    z-CGenerator.visit_Assignment.<locals>.<lambda>z%s %s %s)r/   Zrvaluer   Zlvaluer*   )r   r   r2   r   r   r   Úvisit_AssignmentQ   s    
zCGenerator.visit_Assignmentc             C   s   d  |j¡S )Nr
   )r   Únames)r   r   r   r   r   Úvisit_IdentifierTypeW   s    zCGenerator.visit_IdentifierTypec             C   sJ   t |tjƒrd|  |¡ d S t |tjƒr<d|  |¡ d S |  |¡S d S )NÚ{Ú}r%   r&   )r4   r   ZInitListr   ÚExprList)r   r   r   r   r   Ú_visit_exprZ   s
    zCGenerator._visit_exprFc             C   sL   |r
|j n|  |¡}|jr.|d|  |j¡ 7 }|jrH|d|  |j¡ 7 }|S )Nz : z = )r   Ú_generate_declZbitsizer   Úinitr<   )r   r   Úno_typeÚsr   r   r   Ú
visit_Declb   s     zCGenerator.visit_Declc                sL   ˆ   |jd ¡}t|jƒdkrH|dd ‡ fdd„|jdd … D ƒ¡ 7 }|S )Nr   r   z, c             3   s   | ]}ˆ j |d dV  qdS )T)r?   N)rA   )r   Údecl)r   r   r   r   o   s   z,CGenerator.visit_DeclList.<locals>.<genexpr>)r   ÚdeclsÚlenr   )r   r   r@   r   )r   r   Úvisit_DeclListl   s
    zCGenerator.visit_DeclListc             C   s2   d}|j r|d |j ¡d 7 }||  |j¡7 }|S )Nr   r
   )Ústorager   Ú_generate_typer#   )r   r   r@   r   r   r   Úvisit_Typedefs   s
     zCGenerator.visit_Typedefc             C   s(   d|   |j¡ d }|d |  |j¡ S )Nr%   r&   r
   )rG   Zto_typer!   r)   )r   r   r@   r   r   r   Ú
visit_Casty   s    zCGenerator.visit_Castc             C   s.   g }x|j D ]}| |  |¡¡ qW d |¡S )Nz, )ÚexprsÚappendr<   r   )r   r   Úvisited_subexprsr)   r   r   r   Úvisit_ExprList}   s    zCGenerator.visit_ExprListc             C   s.   g }x|j D ]}| |  |¡¡ qW d |¡S )Nz, )rJ   rK   r<   r   )r   r   rL   r)   r   r   r   Úvisit_InitListƒ   s    zCGenerator.visit_InitListc             C   s   | j |ddS )NÚenum)r   )Ú_generate_struct_union_enum)r   r   r   r   r   Ú
visit_Enum‰   s    zCGenerator.visit_Enumc             C   s<   |j sdj|  ¡ |jdS dj|  ¡ |j|  |j ¡dS d S )Nz{indent}{name},
)Úindentr   z{indent}{name} = {value},
)rR   r   r   )r   Úformatr   r   r   )r   r   r   r   r   Úvisit_EnumeratorŒ   s    
zCGenerator.visit_Enumeratorc                sj   ˆ   |j¡}dˆ _ˆ   |j¡}|jrVd ‡ fdd„|jD ƒ¡}|d | d | d S |d | d S d S )Nr   z;
c             3   s   | ]}ˆ   |¡V  qd S )N)r   )r   Úp)r   r   r   r   ž   s    z+CGenerator.visit_FuncDef.<locals>.<genexpr>Ú
)r   rB   r   ÚbodyZparam_declsr   )r   r   rB   rW   Zknrdeclsr   )r   r   Úvisit_FuncDef™   s    zCGenerator.visit_FuncDefc             C   sf   d}x\|j D ]R}t|tjƒr,||  |¡7 }qt|tjƒrL||  |¡d 7 }q||  |¡d 7 }qW |S )Nr   rV   z;
)Úextr4   r   ZFuncDefr   ZPragma)r   r   r@   rY   r   r   r   Úvisit_FileAST£   s    zCGenerator.visit_FileASTc                s`   ˆ   ¡ d }ˆ  jd7  _|jr>|d ‡ fdd„|jD ƒ¡7 }ˆ  jd8  _|ˆ   ¡ d 7 }|S )Nz{
é   r   c             3   s   | ]}ˆ   |¡V  qd S )N)Ú_generate_stmt)r   Ústmt)r   r   r   r   ²   s    z,CGenerator.visit_Compound.<locals>.<genexpr>z}
)r   r   Zblock_itemsr   )r   r   r@   r   )r   r   Úvisit_Compound®   s    zCGenerator.visit_Compoundc             C   s$   d|   |j¡ d |   |j¡ d S )Nr%   z){r:   )r   r#   r>   )r   r   r   r   r   Úvisit_CompoundLiteral·   s    z CGenerator.visit_CompoundLiteralc             C   s   dS )Nú;r   )r   r   r   r   r   Úvisit_EmptyStatement»   s    zCGenerator.visit_EmptyStatementc                s   d  ‡ fdd„|jD ƒ¡S )Nz, c             3   s   | ]}ˆ   |¡V  qd S )N)r   )r   Zparam)r   r   r   r   ¿   s    z-CGenerator.visit_ParamList.<locals>.<genexpr>)r   Zparams)r   r   r   )r   r   Úvisit_ParamList¾   s    zCGenerator.visit_ParamListc             C   s&   d}|j r|d|  |j ¡ 7 }|d S )NÚreturnr
   r`   )r)   r   )r   r   r@   r   r   r   Úvisit_ReturnÁ   s     zCGenerator.visit_Returnc             C   s   dS )Nzbreak;r   )r   r   r   r   r   Úvisit_BreakÆ   s    zCGenerator.visit_Breakc             C   s   dS )Nz	continue;r   )r   r   r   r   r   Úvisit_ContinueÉ   s    zCGenerator.visit_Continuec             C   sH   d|   |j¡ d }|d|   |j¡ d 7 }|d|   |j¡ d 7 }|S )Nr%   z) ? z) : r&   )r<   ÚcondÚiftrueÚiffalse)r   r   r@   r   r   r   Úvisit_TernaryOpÌ   s    zCGenerator.visit_TernaryOpc             C   sd   d}|j r||  |j ¡7 }|d7 }|| j|jdd7 }|jr`||  ¡ d 7 }|| j|jdd7 }|S )Nzif (z)
T)Ú
add_indentzelse
)rg   r   r\   rh   ri   r   )r   r   r@   r   r   r   Úvisit_IfÒ   s     zCGenerator.visit_Ifc             C   s~   d}|j r||  |j ¡7 }|d7 }|jr<|d|  |j¡ 7 }|d7 }|jr^|d|  |j¡ 7 }|d7 }|| j|jdd7 }|S )Nzfor (r`   r
   z)
T)rk   )r>   r   rg   Únextr\   r]   )r   r   r@   r   r   r   Ú	visit_ForÜ   s       zCGenerator.visit_Forc             C   s:   d}|j r||  |j ¡7 }|d7 }|| j|jdd7 }|S )Nzwhile (z)
T)rk   )rg   r   r\   r]   )r   r   r@   r   r   r   Úvisit_Whileç   s     zCGenerator.visit_Whilec             C   sJ   d}|| j |jdd7 }||  ¡ d 7 }|jr>||  |j¡7 }|d7 }|S )Nzdo
T)rk   zwhile (z);)r\   r]   r   rg   r   )r   r   r@   r   r   r   Úvisit_DoWhileî   s     zCGenerator.visit_DoWhilec             C   s,   d|   |j¡ d }|| j|jdd7 }|S )Nzswitch (z)
T)rk   )r   rg   r\   r]   )r   r   r@   r   r   r   Úvisit_Switchö   s    zCGenerator.visit_Switchc             C   s:   d|   |j¡ d }x |jD ]}|| j|dd7 }qW |S )Nzcase z:
T)rk   )r   r)   Ústmtsr\   )r   r   r@   r]   r   r   r   Ú
visit_Caseû   s    zCGenerator.visit_Casec             C   s*   d}x |j D ]}|| j|dd7 }qW |S )Nz	default:
T)rk   )rr   r\   )r   r   r@   r]   r   r   r   Úvisit_Default  s    zCGenerator.visit_Defaultc             C   s   |j d |  |j¡ S )Nz:
)r   r\   r]   )r   r   r   r   r   Úvisit_Label  s    zCGenerator.visit_Labelc             C   s   d|j  d S )Nzgoto r`   )r   )r   r   r   r   r   Ú
visit_Goto
  s    zCGenerator.visit_Gotoc             C   s   dS )Nz...r   )r   r   r   r   r   Úvisit_EllipsisParam  s    zCGenerator.visit_EllipsisParamc             C   s   |   |d¡S )NÚstruct)rP   )r   r   r   r   r   Úvisit_Struct  s    zCGenerator.visit_Structc             C   s   |   |j¡S )N)rG   r#   )r   r   r   r   r   Úvisit_Typename  s    zCGenerator.visit_Typenamec             C   s   |   |d¡S )NÚunion)rP   )r   r   r   r   r   Úvisit_Union  s    zCGenerator.visit_Unionc             C   s^   d}x@|j D ]6}t|tjƒr,|d|j  7 }q|d|  |¡ d 7 }qW |d|  |j¡ 7 }|S )Nr   Ú.r   r    z = )r   r4   r   ÚIDr   r<   r)   )r   r   r@   r   r   r   r   Úvisit_NamedInitializer  s    z!CGenerator.visit_NamedInitializerc             C   s
   |   |¡S )N)rG   )r   r   r   r   r   Úvisit_FuncDecl#  s    zCGenerator.visit_FuncDeclc             C   s°   |dkr|j }| j}n(|dks"t‚|jdkr0dn|jj}| j}|d |jpLd }|dk	r¬|d7 }||  ¡ 7 }|  jd7  _|d7 }|||ƒ7 }|  jd8  _||  ¡ d	 7 }|S )
zq Generates code for structs, unions, and enums. name should be
            'struct', 'union', or 'enum'.
        )rx   r{   rO   Nr
   r   rV   r[   z{
r:   )	rC   Ú_generate_struct_union_bodyÚAssertionErrorÚvaluesZenumeratorsÚ_generate_enum_bodyr   r   r   )r   r   r   ÚmembersZbody_functionr@   r   r   r   rP   &  s     z&CGenerator._generate_struct_union_enumc                s   d  ‡ fdd„|D ƒ¡S )Nr   c             3   s   | ]}ˆ   |¡V  qd S )N)r\   )r   rB   )r   r   r   r   ?  s    z9CGenerator._generate_struct_union_body.<locals>.<genexpr>)r   )r   r…   r   )r   r   r   >  s    z&CGenerator._generate_struct_union_bodyc                s$   d  ‡ fdd„|D ƒ¡d d… d S )Nr   c             3   s   | ]}ˆ   |¡V  qd S )N)r   )r   r   )r   r   r   r   C  s    z1CGenerator._generate_enum_body.<locals>.<genexpr>éþÿÿÿrV   )r   )r   r…   r   )r   r   r„   A  s    zCGenerator._generate_enum_bodyc             C   s®   t |ƒ}|r|  jd7  _|  ¡ }|r4|  jd8  _|tjtjtjtjtjtj	tj
tjtjtjtjtjtjfkr‚||  |¡ d S |tjfkr˜|  |¡S ||  |¡ d S dS )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.
        r[   z;
rV   N)r#   r   r   r   ÚDeclr5   ZCastZUnaryOpZBinaryOpZ	TernaryOpÚFuncCallÚArrayRefÚ	StructRefÚConstantr~   ZTypedefr;   r   ZCompound)r   r   rk   ÚtyprR   r   r   r   r\   E  s      

zCGenerator._generate_stmtc             C   sH   d}|j rd |j ¡d }|jr4|d |j¡d 7 }||  |j¡7 }|S )z& Generation from a Decl node.
        r   r
   )Zfuncspecr   rF   rG   r#   )r   r   r@   r   r   r   r=   a  s      zCGenerator._generate_declc             C   sÒ  t |ƒ}|tjkrNd}|jr2|d |j¡d 7 }||  |j ¡7 }|jrN|jnd}xät|ƒD ]Ø\}}t|tj	ƒr²|dkr˜t||d  tj
ƒr˜d| d }|d|  |j¡ d 7 }q\t|tjƒr|dkrèt||d  tj
ƒrèd| d }|d|  |j¡ d 7 }q\t|tj
ƒr\|jr,d	d |j¡|f }q\d
| }q\W |rJ|d| 7 }|S |tjkrf|  |j ¡S |tjkr~|  |j ¡S |tjkršd |j¡d S |tj	tj
tjfkrÄ|  |j ||g ¡S |  |¡S d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.
        r   r
   r   r   r%   r&   r   r    z* %s %sÚ*N)r#   r   ZTypeDeclZqualsr   r   ZdeclnameÚ	enumerater4   Z	ArrayDeclZPtrDeclZdimZFuncDeclr'   r‡   r=   ZTypenamerG   ZIdentifierTyper7   )r   r   Z	modifiersrŒ   r@   ZnstrÚiZmodifierr   r   r   rG   j  s@      zCGenerator._generate_typec             C   s&   |   |¡}||ƒrd| d S |S dS )z‘ Visits 'n' and returns its string representation, parenthesized
            if the condition function applied to the node returns True.
        r%   r&   N)r<   )r   r   Z	conditionr@   r   r   r   r/   ˜  s    
zCGenerator._parenthesize_ifc                s   ˆ   |‡ fdd„¡S )z. Common use case for _parenthesize_if
        c                s   ˆ   | ¡ S )N)r,   )r-   )r   r   r   r.   ¥  s    z8CGenerator._parenthesize_unless_simple.<locals>.<lambda>)r/   )r   r   r   )r   r   r!   ¢  s    z&CGenerator._parenthesize_unless_simplec             C   s   t |tjtjtjtjtjfƒS )z~ Returns True for nodes that are "simple" - i.e. nodes that always
            have higher precedence than operators.
        )r4   r   r‹   r~   r‰   rŠ   rˆ   )r   r   r   r   r   r,   §  s    zCGenerator._is_simple_nodeN)F)F)=r   Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r"   r$   r(   r+   r3   r6   r8   r<   rA   rE   rH   rI   rM   rN   rQ   rT   rX   rZ   r^   r_   ra   rb   rd   re   rf   rj   rl   rn   ro   rp   rq   rs   rt   ru   rv   rw   ry   rz   r|   r   r€   rP   r   r„   r\   r=   rG   r/   r!   r,   r   r   r   r   r      st   


	


	.
r   N)r   r   Úobjectr   r   r   r   r   Ú<module>	   s   