B
    0ªai  ã               @   sh   d dl mZ d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ ddlm	Z	 G d	d
„ d
e
ƒZdS )é    )Úwith_statement)Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)Úcontextmanageré   )Úastc               @   s|   e Zd Zg g g g g fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	e
dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚCCodeGeneratorc	       	      C   s:   || _ || _|| _|| _|| _|| _|| _i | _|| _d S )N)	Úout_h_filenameÚout_c_filenameÚfunction_decorationÚinclude_first_headerÚinclude_last_headerÚinclude_first_srcÚinclude_last_srcÚ_function_bodiesÚ	namespace)	Úselfr   r   r   r   r   r   r   r   © r   ú3/usr/lib/gobject-introspection/giscanner/codegen.pyÚ__init__!   s    zCCodeGenerator.__init__c             C   s    |  dd¡}d| jjd |f S )Nú Ú_z%s_%sr   )Úreplacer   Zsymbol_prefixes)r   Únamer   r   r   Ú
gen_symbol3   s    zCCodeGenerator.gen_symbolc             C   s\   t |tjƒr$|jtjtjfkr$d}nd}|j tjtj	f¡rP|j
tjkrPd| S |jj| S )NÚ*Ú zconst gchar*)Ú
isinstancer	   Z	ParameterÚ	directionÚPARAM_DIRECTION_OUTÚPARAM_DIRECTION_INOUTÚtypeZis_equivZTYPE_STRINGZTYPE_FILENAMEÚtransferZPARAM_TRANSFER_NONEÚctype)r   ÚparamÚsuffixr   r   r   Ú_typecontainer_to_ctype7   s    z&CCodeGenerator._typecontainer_to_ctypec             C   s®   | j r| dd | j ¡ ¡ | d|  |j¡|jf ¡ t|jƒ}|jr–xVt|jƒD ]<\}}|  |¡}| d||j	f ¡ ||d k rT| d¡ qTW n
| d¡ | d¡ d S )	Nz
%sr   z
%s
%s (z%s %sr   z, Zvoidú))
r   ÚwriteÚjoinr(   ÚretvalÚsymbolÚlenÚ
parametersÚ	enumerateÚargname)r   ÚoutÚfuncÚlÚir&   r%   r   r   r   Ú_write_preludeD   s    


zCCodeGenerator._write_preludec             C   s   |   | j|¡ | j d¡ d S )Nz;

)r6   Úout_hr*   )r   r3   r   r   r   Ú_write_prototypeW   s    zCCodeGenerator._write_prototypec             C   s2   |j tjks|j j d¡r.| j d|jf ¡ d S )Nr   z (transfer %s))r#   r	   ZBASIC_TYPESr%   ÚendswithÚout_cr*   r$   )r   Únoder   r   r   Ú_write_annotation_transfer[   s    z)CCodeGenerator._write_annotation_transferc             C   sà   | j  d|jf ¡ xp|jD ]f}| j  d|jf ¡ |jtjtjfkrv|j	rRd}nd}| j  d|j|f ¡ |  
|¡ | j  d¡ qW | j  d¡ | j  d¡ |jjtjkrÐ| j  d	¡ | j  d
¡ |  
|j¡ | j  d¡ d S )Nz/**
 * %s:
z * @%sz caller-allocatesr   z	: (%s%s) z:
z *
z * Undocumented.z
 *
z * Returns: z
 */)r:   r*   r-   r/   r1   r    r	   r!   r"   Zcaller_allocatesr<   r,   r#   Z	TYPE_NONE)r   r3   r&   Zallocate_stringr   r   r   Ú_write_docs`   s&    


zCCodeGenerator._write_docsc             c   sD   |   |¡ |  |¡ |  | j|¡ | j d¡ d V  | j d¡ d S )Nz
{
z}

)r8   r=   r6   r:   r*   )r   r3   r   r   r   Ú	_functionv   s    

zCCodeGenerator._functionc             C   sÔ   d}| j  |¡ | jj ¡ }x| jD ]}| j  d| ¡ q$W | j  d||f ¡ x| jD ]}| j  d| ¡ qXW | j |¡ x| jD ]}| j d| ¡ q„W | j d| j	f ¡ x| j
D ]}| j d| ¡ q¸W d S )Nz0/* GENERATED BY testcodegen.py; DO NOT EDIT */

z#include "%s"
z>
#ifndef __%s_H__
#define __%s_H__

#include <glib-object.h>

z#include "%s"

)r7   r*   r   r   Úupperr   r   r:   r   r   r   )r   ZwarningZnsupperÚheaderr   r   r   Ú_codegen_start   s    zCCodeGenerator._codegen_startc             C   s$   | j  d¡ | j  ¡  | j ¡  d S )Nz#endif
)r7   r*   Úcloser:   )r   r   r   r   Ú_codegen_endœ   s    
zCCodeGenerator._codegen_endc             C   s   t |tjƒst‚|| j|< d S )N)r   r	   ÚFunctionÚAssertionErrorr   )r   r;   Úbodyr   r   r   Úset_function_body¢   s    z CCodeGenerator.set_function_bodyc          
   C   s†   t | jdƒ| _t | jdƒ| _|  ¡  xT| j ¡ D ]F}t|t	j
ƒr0|  |¡& | j |¡}|s`d}| j |¡ W d Q R X q0W |  ¡  d S )NÚwr   )Úopenr   r7   r   r:   rA   r   Úvaluesr   r	   rD   r>   r   Úgetr*   rC   )r   r;   rF   r   r   r   Úcodegen¦   s    zCCodeGenerator.codegenN)Ú__name__Ú
__module__Ú__qualname__r   r   r(   r6   r8   r<   r=   r   r>   rA   rC   rG   rL   r   r   r   r   r
       s    	r
   N)Z
__future__r   r   r   r   r   Ú
contextlibr   r   r	   Úobjectr
   r   r   r   r   Ú<module>   s   