a
    Lg                     @   s  d dl Z d dlmZmZmZmZmZmZ dZde Z	e
e
dddZd.e
e
ee
 e
dd	d
Zd/e
ee
dddZG dd dZe Ze
ee
dddZe
ee
dddZe
e
dddZe
e
dddZe
e
dddZeee
ee
ef f  e
e
e
e
e
ddd Zeee
ee
ef f  e
d!d"d#Zeee
ee
ef f  e
d!d$d%Ze
e
d&d'd(Ze
e
d&d)d*Ze
e
ee
 d+d,d-ZdS )0    N)AnyDictMatchOptionalSequenceUnionz
EATSPACE. *)valuereturnc                 C   s   | d }| d   }| dd D ]l}|  |kr2nJ|rht|dkr||d  r||dd d |d  }n|d  r||d7 }||7 }|  }q t| dS )z
    Converts CamelCase to CAMEL_CASE.

    Examples::

        ENUMName -> ENUM_NAME
        EnumName1 -> ENUM_NAME1
        ENUM_NAME -> ENUM_NAME
        ENUM_NAME1 -> ENUM_NAME1
        ENUM_Name2 -> ENUM_NAME2
        ENUM24_Name -> ENUM24_NAME
    r      N   _)isupperlenisalnumc_nameupperlstrip)r	   retZupcZch r   >/home2/Puru_Virtio_Blk/virtio-demo/qemu/scripts/qapi/common.pycamel_to_upper   s    
r   )	type_name
const_nameprefixr
   c                 C   s&   |du rt | }|d t|d  S )z
    Generate a C enumeration constant name.

    :param type_name: The name of the enumeration.
    :param const_name: The name of this constant.
    :param prefix: Optional, prefix that overrides the type_name.
    Nr   F)r   r   r   )r   r   r   r   r   r   c_enum_constC   s    
r   T)nameprotectr
   c                 C   s   t g d}t g d}t g d}t ddg}t g d}t g d}tdd	| } |r| ||B |B |B |B |B v s| d
  rd|  S | S )a  
    Map ``name`` to a valid C identifier.

    Used for converting 'name' from a 'name':'type' qapi definition
    into a generated struct member, as well as converting type names
    into substrings of a generated C function name.

    '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
    protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'

    :param name: The name to map.
    :param protect: If true, avoid returning certain ticklish identifiers
                    (like C keywords) by prepending ``q_``.
    ) autobreakZcasecharconstcontinuedefaultZdoZdoubleelseenumZexternfloatforZgotoifintZlongregisterr
   ZshortZsignedZsizeofZstaticstructZswitchZtypedefunionZunsignedvoidZvolatilewhile)ZinlineZrestrictZ_BoolZ_ComplexZ
_Imaginary)Z_AlignasZ_AlignofZ_AtomicZ_GenericZ	_NoreturnZ_Static_assertZ_Thread_localZasmZtypeof)'boolZcatchclassZ
const_castdeleteZdynamic_castZexplicitZfalseZfriendZmutable	namespacenewoperatorZprivateZ	protectedZpublicZreinterpret_castZstatic_casttemplatethisthrowtruetryZtypeidtypenameZusingZvirtualZwchar_tandZand_eqZbitandZbitorZcomplnotZnot_eqorZor_eqxorZxor_eq)ZunixerrnoZmipsZsparcZi386Zlinux[^A-Za-z0-9_]r   r   Zq_)setresubisdigit)r   r   Z	c89_wordsZ	c99_wordsZ	c11_wordsZ	gcc_wordsZ	cpp_wordsZpolluted_wordsr   r   r   r   R   s     

r   c                   @   sb   e Zd ZdZdeddddZeddd	Zedd
dZdeddddZ	deddddZ
dS )Indentationza
    Indentation level management.

    :param initial: Initial number of spaces, default 0.
    r   N)initialr
   c                 C   s
   || _ d S N_level)selfrH   r   r   r   __init__   s    zIndentation.__init__)r
   c                 C   s   d t| j| jS )Nz{}({:d}))formattype__name__rK   rL   r   r   r   __repr__   s    zIndentation.__repr__c                 C   s
   d| j  S )z5Return the current indentation as a string of spaces. rJ   rQ   r   r   r   __str__   s    zIndentation.__str__   )amountr
   c                 C   s   |  j |7  _ dS )z8Increase the indentation level by ``amount``, default 4.NrJ   rL   rV   r   r   r   increase   s    zIndentation.increasec                 C   s    || j ksJ |  j |8  _ dS )z8Decrease the indentation level by ``amount``, default 4.NrJ   rW   r   r   r   decrease   s    zIndentation.decrease)r   )rU   )rU   )rP   
__module____qualname____doc__r+   rM   strrR   rT   rX   rY   r   r   r   r   rG      s   rG   )codekwdsr
   c                 K   s@   | | }t t}|r(tjd||tjd}tttd d|S )za
    Generate ``code`` with ``kwds`` interpolated.

    Obey `indent`, and strip `EATSPACE`.
    z
^(?!(#|$)))flagsr    )r]   indentrD   rE   	MULTILINEescapeEATSPACE)r^   r_   rawZpfxr   r   r   cgen   s
    rg   c                 K   s(   | d dkr| dd  } t | fi |S )Nr   
r   )rg   )r^   r_   r   r   r   mcgen   s    ri   )filenamer
   c                 C   s   t dd| S )NrB   r   )rD   rE   )rj   r   r   r   c_fname   s    rk   )r   r
   c                 C   s   t dt|  dS )Nz$
#ifndef %(name)s
#define %(name)s

r   ri   rk   r   rl   r   r   r   
guardstart   s    
rn   c                 C   s   t dt|  dS )Nz

#endif /* %(name)s */
rl   rm   rl   r   r   r   guardend   s    
ro   )ifcondcond_fmtnot_fmtall_operatorany_operatorr
   c                    s\   t ttttf f ttd fddttt tdfdd| sRdS | dS )	N)rp   need_parensr
   c                    s   t | tr|  S t | tr(t| dks,J d| v rF| d d S d| v r^ | d }n| d }|r|d| d }|S )Nr   r>   Tallany())
isinstancer]   dictr   )rp   ru   genrs   rt   rq   do_gen	gen_infixrr   r   r   r~      s    
zgen_ifcond.<locals>.do_gen)r6   operandsr
   c                    s   |   fdd|D S )Nc                    s   g | ]} |d qS )Tr   ).0or~   r   r   
<listcomp>       z1gen_ifcond.<locals>.gen_infix.<locals>.<listcomp>)join)r6   r   r   r   r   r      s    zgen_ifcond.<locals>.gen_infixra   F)r   r]   r   r   r1   r   )rp   rq   rr   rs   rt   r   r}   r   
gen_ifcond   s    r   )rp   r
   c                 C   s   t | ddddS )Nzdefined(%s)z!%sz && z || r   rp   r   r   r   cgen_ifcond   s    r   c                 C   s   t | ddddS )Nz%sznot %sz and z or r   r   r   r   r   docgen_ifcond   s    r   )condr
   c                 C   s   | sdS t d| dS )Nra   z
#if %(cond)s
r   ri   r   r   r   r   gen_if   s
    r   c                 C   s   | sdS t d| dS )Nra   z
#endif /* %(cond)s */
r   r   r   r   r   r   	gen_endif   s
    r   )patternstringr
   c                 C   s   t | |}|d usJ |S rI   )rD   match)r   r   r   r   r   r   
must_match   s    r   )N)T)rD   typingr   r   r   r   r   r   re   ZPOINTER_SUFFIXr]   r   r   r1   r   rG   rb   objectrg   ri   rk   rn   ro   r   r   r   r   r   r   r   r   r   r   <module>   s4    ' 2	$$