U
    Ú²g;  ã                   @   s0   d dl mZ dd„ Zdd„ Zdd„ Zdd	„ Zd
S )é   )Úc_astc                 C   sª   t | tjƒst‚t | jtjƒs"| S t g | jj¡}d}| jjp@g D ]\}t |tjtj	fƒr||j 
|¡ t||jƒ |jd }qB|dkr’|j 
|¡ qB|j 
|¡ qB|| _| S )aÜ   The 'case' statements in a 'switch' come out of parsing with one
        child node, so subsequent statements are just tucked to the parent
        Compound. Additionally, consecutive (fall-through) case statements
        come out messy. This is a peculiarity of the C grammar. The following:

            switch (myvar) {
                case 10:
                    k = 10;
                    p = k + 1;
                    return 10;
                case 20:
                case 30:
                    return 20;
                default:
                    break;
            }

        Creates this tree (pseudo-dump):

            Switch
                ID: myvar
                Compound:
                    Case 10:
                        k = 10
                    p = k + 1
                    return 10
                    Case 20:
                        Case 30:
                            return 20
                    Default:
                        break

        The goal of this transform is to fix this mess, turning it into the
        following:

            Switch
                ID: myvar
                Compound:
                    Case 10:
                        k = 10
                        p = k + 1
                        return 10
                    Case 20:
                    Case 30:
                        return 20
                    Default:
                        break

        A fixed AST node is returned. The argument may be modified.
    Néÿÿÿÿ)Ú
isinstancer   ZSwitchÚAssertionErrorZstmtZCompoundZcoordZblock_itemsÚCaseÚDefaultÚappendÚ_extract_nested_caseÚstmts)Zswitch_nodeZnew_compoundZ	last_caseÚchild© r   ú</tmp/pip-unpacked-wheel-f3oyftnv/pycparser/ast_transforms.pyÚfix_switch_cases   s    3r   c                 C   s:   t | jd tjtjfƒr6| | j ¡ ¡ t|d |ƒ dS )z€ Recursively extract consecutive Case statements that are made nested
        by the parser and add them to the stmts_list.
    é    r   N)r   r
   r   r   r   r   Úpopr	   )Z	case_nodeZ
stmts_listr   r   r   r	   c   s    r	   c                 C   s€   t | ƒ\} }|s qq | }t|tjƒsJz
|j}W q tk
rF   |  Y S X qd|jkrjd| jkrj| j d¡ |jdkr|| j	|_| S )aK   Atomic specifiers like _Atomic(type) are unusually structured,
        conferring a qualifier upon the contained type.

        This function fixes a decl with atomic specifiers to have a sane AST
        structure, by removing spurious Typename->TypeDecl pairs and attaching
        the _Atomic qualifier in the right place.
    Ú_AtomicN)
Ú_fix_atomic_specifiers_oncer   r   ÚTypeDeclÚtypeÚAttributeErrorÚqualsr   ZdeclnameÚname)ÚdeclÚfoundÚtypr   r   r   Úfix_atomic_specifiersl   s    

r   c                 C   sš   | }d}| j }|dk	r`t|tjƒr.d|jkr.q`z|}|}|j }W q tk
r\   | df Y S X qt|tjƒspt‚|j |_ d|j jkr’|j j d¡ | dfS )zƒ Performs one 'fix' round of atomic specifiers.
        Returns (modified_decl, found) where found is True iff a fix was made.
    Nr   FT)	r   r   r   ZTypenamer   r   r   r   r   )r   ÚparentZgrandparentÚnoder   r   r   r   Œ   s"    
r   N)Ú r   r   r	   r   r   r   r   r   r   Ú<module>
   s   V	 