o
    qoi@                     @   s  d Z ddlZddlmZmZmZmZmZ ddl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 ejd
dG dd dejZejd
dG dd dejZejd
dG dd dejZejd
dG dd dejZde	jej deej defddZej				d%de dee de!de!dejf
dd Z"dddddd!de d"eee ef  dee de!de!de fd#d$Z#dS )&zGenerates Fiddle code with the auto_config codegen codebase.

Currently this API is under development; please do not depend on it.
    N)AnyCallableDictOptionalType)newcg_symbolic_references)add_type_signatures)code_ir)experimental_top_level_api)make_symbolic_references)auto_configT)frozenc                   @   s   e Zd ZdedefddZdS )RemoveAutoConfigFnvaluereturnc                 C   s   t |tjsJ d |_|S )N)
isinstancer	   CodegenTaskauto_config_fn)selfr    r   S/home/ubuntu/.local/lib/python3.10/site-packages/fiddle/_src/codegen/new_codegen.py__call__$   s   zRemoveAutoConfigFn.__call__N)__name__
__module____qualname__r   r   r   r   r   r   r   !   s    r   c                   @   (   e Zd ZU dZejZedef e	d< dS )ImportSymbolsz8Preprocessing step to import symbols, for better naming..fnN)
r   r   r   __doc__r   import_symbolsr   r   r   __annotations__r   r   r   r   r   *      
 r   c                   @   s>   e Zd ZU dZejZedef e	d< e
jZedef e	d< dS )MakeSymbolicReferenceszConverts Buildable sub-objects to IR nodes representing function calls.

  In this case, we emit fdl.Config(), fdl.Partial(), and fdl.ArgFactory() calls,
  instead of calls to the configured functions/classes.
  .r   format_historyN)r   r   r   r   r   *replace_callables_and_configs_with_symbolsr   r   r   r    old_symbolic_referencesnoop_history_commentsr#   r   r   r   r   r"   1   s   
 r"   c                   @   r   )AddTypeSignaturesz(Adds return type signatures to fixtures..r   N)
r   r   r   r   r   add_return_typesr   r   r   r    r   r   r   r   r'   A   r!   r'   codegen_configclsr   c                 C   s:   t | jD ]\}}tt||r|  S qtd| )NzCould not find codegen pass )	enumeratepasses
issubclassfdlget_callable
ValueError)r)   r*   icodegen_passr   r   r   _get_pass_idxH   s
   r3   config_fixtureFtop_level_fixture_namemax_expression_complexityinclude_historydebug_printc                 C   s   t jj| |||d}|jdtt t|t j	}|j
| t|t j}t|j| t t|t j}t|j| t |j|tt |S )zEReturns a low-level Codegen instance; see experimental_top_level_api.r5   r6   r7   r8      )r
   code_generatoras_buildabler,   insertr.   Configr   r3   LowerArgFactoriespopr   update_callabler"   r'   )r5   r6   r7   r8   configidxr   r   r   r;   R   s   r;   )r5   sub_fixturesr6   r7   r8   rD   c                C   s   t ||||d}|| |djS )ay  Generates code for an auto_config fixture.

  Args:
    config: Configuration to generate auto_config code for.
    top_level_fixture_name: Name of the top-level fixture. When its as_buildable
      path is called, e.g. `config_fixture.as_buildable()`, this should return a
      config equivalent to `config`.
    sub_fixtures: Dictionary from function name to sub-configuration object,
      declaring sub-functions to create. This helps manage complexity of huge
      configurations.
    max_expression_complexity: Breaks complex expressions into variables for
      readability.
    include_history: Whether history should be included. These currently appear
      as trailing comments in the field of Buildable's.
    debug_print: Whether to use the IR printer to print intermediate
      representations as various passes run to generate code.

  Returns:
    Python module code.
  r9   )rD   )r;   code)rB   r5   rD   r6   r7   r8   codegen_objr   r   r   new_codegenw   s   rG   )r4   NFF)$r   dataclassestypingr   r   r   r   r   fiddler.   fiddle._src.codegenr   fiddle._src.codegen.auto_configr   r	   r
   r   r%   fiddle._src.experimentalr   	dataclassCodegenPassr   MutationCodegenPassr   r"   r'   r>   Codegenintr3   auto_unconfigstrboolr;   rG   r   r   r   r   <module>   sv   






'