o
    f'                     @   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rd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(+  qd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   )zReturn 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 )zReturn 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`

