o
    …9jé'  ã                   @   sX  d Z ddlZddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ ddlZg d¢Zdd„ ZG dd„ dƒZeƒ ZG d	d
„ d
ƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZedkr¨ddlZddlZeejd ƒ�Ze  ¡ Z!W d  ƒ n1 s~w   Y  ee!ej" #ejd ¡d dƒZ$e$ %¡ D ]Z&e$ 'e&¡Z(e)e(e( *¡ e( +¡ ƒ q–dS dS )z2Interface to the compiler's internal symbol tablesé    N)ÚUSEÚ
DEF_GLOBALÚDEF_NONLOCALÚ	DEF_LOCALÚ	DEF_PARAMÚ
DEF_IMPORTÚ	DEF_BOUNDÚ	DEF_ANNOTÚ	SCOPE_OFFÚ
SCOPE_MASKÚFREEÚLOCALÚGLOBAL_IMPLICITÚGLOBAL_EXPLICITÚCELL)ÚsymtableÚSymbolTableÚClassÚFunctionÚSymbolc                 C   s   t  | ||¡}t||ƒS )z« Return the toplevel *SymbolTable* for the source code.

    *filename* is the name of the file with the code
    and *compile_type* is the *compile()* mode argument.
    )Ú	_symtabler   Ú_newSymbolTable)ÚcodeÚfilenameÚcompile_typeÚtop© r   ú/usr/lib/python3.10/symtable.pyr      s   
r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSymbolTableFactoryc                 C   s   t  ¡ | _d S ©N)ÚweakrefÚWeakValueDictionaryÚ_SymbolTableFactory__memo©Úselfr   r   r   Ú__init__   s   zSymbolTableFactory.__init__c                 C   s6   |j tjkrt||ƒS |j tjkrt||ƒS t||ƒS r   )Útyper   ÚTYPE_FUNCTIONr   Ú
TYPE_CLASSr   r   )r$   Útabler   r   r   r   Únew   s
   


zSymbolTableFactory.newc                 C   s8   ||f}| j  |d ¡}|d u r|  ||¡ }| j |< |S r   )r"   Úgetr*   )r$   r)   r   ÚkeyÚobjr   r   r   Ú__call__    s
   zSymbolTableFactory.__call__N)Ú__name__Ú
__module__Ú__qualname__r%   r*   r.   r   r   r   r   r      s    r   c                   @   s|   e 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S )r   c                 C   s   || _ || _i | _d S r   )Ú_tableÚ	_filenameÚ_symbols)r$   Ú	raw_tabler   r   r   r   r%   ,   s   
zSymbolTable.__init__c                 C   sJ   | j tkrd}nd| j j }| jjdkrd || j¡S d || jj| j¡S )NÚ z%s r   z<{0}SymbolTable for module {1}>z<{0}SymbolTable for {1} in {2}>)Ú	__class__r   r/   r2   ÚnameÚformatr3   )r$   Úkindr   r   r   Ú__repr__1   s   
þzSymbolTable.__repr__c                 C   sX   | j jtjkr	dS | j jtjkrdS | j jtjkrdS | j jdv s*J d | j j¡ƒ‚dS )zwReturn the type of the symbol table.

        The values retuned are 'class', 'module' and
        'function'.
        ÚmoduleÚfunctionÚclass)é   é   é   zunexpected type: {0}N)r2   r&   r   ÚTYPE_MODULEr'   r(   r9   r#   r   r   r   Úget_type>   s   ÿzSymbolTable.get_typec                 C   ó   | j jS )z,Return an identifier for the table.
        )r2   Úidr#   r   r   r   Úget_idM   s   zSymbolTable.get_idc                 C   rD   )zµReturn the table's name.

        This corresponds to the name of the class, function
        or 'top' if the table is for a class, function or
        global respectively.
        )r2   r8   r#   r   r   r   Úget_nameR   s   zSymbolTable.get_namec                 C   rD   )zPReturn the number of the first line in the
        block for the table.
        )r2   Úlinenor#   r   r   r   Ú
get_lineno[   s   zSymbolTable.get_linenoc                 C   s   t | jjtjkƒS )zJReturn *True* if the locals in the table
        are optimizable.
        )Úboolr2   r&   r   r'   r#   r   r   r   Úis_optimizeda   s   zSymbolTable.is_optimizedc                 C   ó   t | jjƒS )zAReturn *True* if the block is a nested class
        or function.)rJ   r2   Únestedr#   r   r   r   Ú	is_nestedg   ó   zSymbolTable.is_nestedc                 C   rL   )z:Return *True* if the block has nested namespaces.
        )rJ   r2   Úchildrenr#   r   r   r   Úhas_childrenl   rO   zSymbolTable.has_childrenc                 C   s   | j j ¡ S )zKReturn a view object containing the names of symbols in the table.
        )r2   ÚsymbolsÚkeysr#   r   r   r   Úget_identifiersq   rO   zSymbolTable.get_identifiersc                 C   sT   | j  |¡}|du r(| jj| }|  |¡}| jjdk}t||||d� }| j |< |S )zLLookup a *name* in the table.

        Returns a *Symbol* instance.
        Nr   ©Úmodule_scope)r4   r+   r2   rR   Ú_SymbolTable__check_childrenr8   r   )r$   r8   ÚsymÚflagsÚ
namespacesrV   r   r   r   Úlookupv   s   
ÿzSymbolTable.lookupc                    s   ‡ fdd„ˆ   ¡ D ƒS )zLReturn a list of *Symbol* instances for
        names in the table.
        c                    s   g | ]}ˆ   |¡‘qS r   )r[   ©Ú.0Úidentr#   r   r   Ú
<listcomp>ˆ   s    z+SymbolTable.get_symbols.<locals>.<listcomp>)rT   r#   r   r#   r   Úget_symbols„   s   zSymbolTable.get_symbolsc                    s   ‡ ‡fdd„ˆj jD ƒS )Nc                    s"   g | ]}|j ˆ krt|ˆjƒ‘qS r   )r8   r   r3   ©r]   Úst©r8   r$   r   r   r_   ‹   s
    

þz0SymbolTable.__check_children.<locals>.<listcomp>©r2   rP   )r$   r8   r   rc   r   Ú__check_childrenŠ   s   ÿzSymbolTable.__check_childrenc                    s   ‡ fdd„ˆ j jD ƒS )z3Return a list of the nested symbol tables.
        c                    s   g | ]}t |ˆ jƒ‘qS r   )r   r3   ra   r#   r   r   r_   ’   s    ÿz,SymbolTable.get_children.<locals>.<listcomp>rd   r#   r   r#   r   Úget_children�   s   
ÿzSymbolTable.get_childrenN)r/   r0   r1   r%   r;   rC   rF   rG   rI   rK   rN   rQ   rT   r[   r`   rW   rf   r   r   r   r   r   *   s    	r   c                   @   sP   e Zd ZdZdZdZdZdZdd„ Zdd„ Z	dd„ Z
dd	„ Zd
d„ Zdd„ ZdS )r   Nc                    s   t ‡ ‡fdd„ˆ  ¡ D ƒƒS )Nc                 3   s$   � | ]}ˆˆ j j| ƒr|V  qd S r   )r2   rR   r\   ©r$   Ú	test_funcr   r   Ú	<genexpr>    s   € ÿz-Function.__idents_matching.<locals>.<genexpr>)ÚtuplerT   rg   r   rg   r   Ú__idents_matchingŸ   s   zFunction.__idents_matchingc                 C   ó    | j du r|  dd„ ¡| _ | j S )z6Return a tuple of parameters to the function.
        Nc                 S   ó   | t @ S r   )r   ©Úxr   r   r   Ú<lambda>§   ó    z)Function.get_parameters.<locals>.<lambda>)Ú_Function__paramsÚ_Function__idents_matchingr#   r   r   r   Úget_parameters£   ó   
zFunction.get_parametersc                    ó0   | j du rttf‰ ‡ fdd„}|  |¡| _ | j S )z2Return a tuple of locals in the function.
        Nc                    ó   | t ? t@ ˆ v S r   ©r
   r   rn   ©Úlocsr   r   rp   ¯   ó    z%Function.get_locals.<locals>.<lambda>)Ú_Function__localsr   r   rs   ©r$   Útestr   ry   r   Ú
get_localsª   ó
   
zFunction.get_localsc                    rv   )z3Return a tuple of globals in the function.
        Nc                    rw   r   rx   rn   ©Úglobr   r   rp   ¸   r{   z&Function.get_globals.<locals>.<lambda>)Ú_Function__globalsr   r   rs   r}   r   r�   r   Úget_globals³   r€   zFunction.get_globalsc                 C   rl   )z5Return a tuple of nonlocals in the function.
        Nc                 S   rm   r   )r   rn   r   r   r   rp   À   rq   z(Function.get_nonlocals.<locals>.<lambda>)Ú_Function__nonlocalsrs   r#   r   r   r   Úget_nonlocals¼   ru   zFunction.get_nonlocalsc                 C   s$   | j du rdd„ }|  |¡| _ | j S )z:Return a tuple of free variables in the function.
        Nc                 S   s   | t ? t@ tkS r   )r
   r   r   rn   r   r   r   rp   Ç   r{   z$Function.get_frees.<locals>.<lambda>)Ú_Function__freesrs   )r$   Úis_freer   r   r   Ú	get_freesÃ   s   
zFunction.get_frees)r/   r0   r1   rr   r|   r‡   rƒ   r…   rs   rt   r   r„   r†   r‰   r   r   r   r   r   –   s    		r   c                   @   s   e Zd ZdZdd„ ZdS )r   Nc                 C   s6   | j du ri }| jjD ]}d||j< qt|ƒ| _ | j S )z9Return a tuple of methods declared in the class.
        Nr?   )Ú_Class__methodsr2   rP   r8   rj   )r$   Údrb   r   r   r   Úget_methodsÐ   s   

zClass.get_methods)r/   r0   r1   rŠ   rŒ   r   r   r   r   r   Ì   s    r   c                   @   s”   e Zd Zd$ddœ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d"d#„ ZdS )%r   NFrU   c                C   s.   || _ || _|t? t@ | _|pd| _|| _d S )Nr   )Ú_Symbol__nameÚ_Symbol__flagsr
   r   Ú_Symbol__scopeÚ_Symbol__namespacesÚ_Symbol__module_scope)r$   r8   rY   rZ   rV   r   r   r   r%   Ý   s
   

zSymbol.__init__c                 C   s   d  | j¡S )Nz<symbol {0!r}>)r9   r�   r#   r   r   r   r;   ä   s   zSymbol.__repr__c                 C   ó   | j S )z#Return a name of a symbol.
        )r�   r#   r   r   r   rG   ç   s   zSymbol.get_namec                 C   s   t | jtj@ ƒS )zBReturn *True* if the symbol is used in
        its block.
        )rJ   rŽ   r   r   r#   r   r   r   Úis_referencedì   s   zSymbol.is_referencedc                 C   ó   t | jt@ ƒS )z4Return *True* if the symbol is a parameter.
        )rJ   rŽ   r   r#   r   r   r   Úis_parameterò   ó   zSymbol.is_parameterc                 C   ó"   t | jttfv p| jo| jt@ ƒS )z0Return *True* if the sysmbol is global.
        )rJ   r�   r   r   r‘   rŽ   r   r#   r   r   r   Ú	is_global÷   ó   ÿzSymbol.is_globalc                 C   r”   )z(Return *True* if the symbol is nonlocal.)rJ   rŽ   r   r#   r   r   r   Úis_nonlocalý   ó   zSymbol.is_nonlocalc                 C   ó   t | jtkƒS )zOReturn *True* if the symbol is declared global
        with a global statement.)rJ   r�   r   r#   r   r   r   Úis_declared_global  r–   zSymbol.is_declared_globalc                 C   r—   )z.Return *True* if the symbol is local.
        )rJ   r�   r   r   r‘   rŽ   r   r#   r   r   r   Úis_local  r™   zSymbol.is_localc                 C   r”   )z2Return *True* if the symbol is annotated.
        )rJ   rŽ   r	   r#   r   r   r   Úis_annotated  r–   zSymbol.is_annotatedc                 C   rœ   )zIReturn *True* if a referenced symbol is
        not assigned to.
        )rJ   r�   r   r#   r   r   r   rˆ     ó   zSymbol.is_freec                 C   r”   )zQReturn *True* if the symbol is created from
        an import statement.
        )rJ   rŽ   r   r#   r   r   r   Úis_imported  r    zSymbol.is_importedc                 C   r”   )z)Return *True* if a symbol is assigned to.)rJ   rŽ   r   r#   r   r   r   Úis_assigned  r›   zSymbol.is_assignedc                 C   s
   t | jƒS )a‹  Returns *True* if name binding introduces new namespace.

        If the name is used as the target of a function or class
        statement, this will be true.

        Note that a single name can be bound to multiple objects.  If
        is_namespace() is true, the name may also be bound to other
        objects, like an int or list, that does not introduce a new
        namespace.
        )rJ   r�   r#   r   r   r   Úis_namespace!  s   
zSymbol.is_namespacec                 C   r’   )z.Return a list of namespaces bound to this name)r�   r#   r   r   r   Úget_namespaces.  s   zSymbol.get_namespacesc                 C   s    t | jƒdkrtdƒ‚| jd S )z€Return the single namespace bound to this name.

        Raises ValueError if the name is bound to multiple namespaces.
        r?   z$name is bound to multiple namespacesr   )Úlenr�   Ú
ValueErrorr#   r   r   r   Úget_namespace2  s   
zSymbol.get_namespacer   )r/   r0   r1   r%   r;   rG   r“   r•   r˜   rš   r�   rž   rŸ   rˆ   r¡   r¢   r£   r¤   r§   r   r   r   r   r   Û   s"    r   Ú__main__r?   Úexec),Ú__doc__r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r    Ú__all__r   r   r   r   r   r   r   r/   ÚosÚsysÚopenÚargvÚfÚreadÚsrcÚpathÚsplitÚmodrT   r^   r[   ÚinfoÚprintrž   r£   r   r   r   r   Ú<module>   s.    D	l6`
ÿ
ù