def remove_called_lambdas_and_blocks(comp): """Removes any called lambdas and blocks from `comp`. This function first resolves any higher-order functions, so that replacing called lambdas with blocks and then inlining the block locals cannot result in more called lambdas. It then performs this sequence of transformations, taking care to inline selections from tuples before inlining the rest of the block locals to prevent possible combinatorial growth of the generated AST. Args: comp: Instance of `building_blocks.ComputationBuildingBlock` from which we want to remove called lambdas and blocks. Returns: A transformed version of `comp` which has no called lambdas or blocks, and no extraneous selections from tuples. """ py_typecheck.check_type(comp, building_blocks.ComputationBuildingBlock) comp, names_uniquified = tree_transformations.uniquify_reference_names(comp) comp, fns_resolved = tree_transformations.resolve_higher_order_functions(comp) comp, lambdas_replaced = tree_transformations.replace_called_lambda_with_block( comp) if fns_resolved or lambdas_replaced: comp, _ = tree_transformations.uniquify_reference_names(comp) comp, sels_removed = tree_transformations.inline_selections_from_tuple(comp) if sels_removed: comp, _ = tree_transformations.uniquify_reference_names(comp) comp, locals_inlined = tree_transformations.inline_block_locals(comp) modified = names_uniquified or fns_resolved or lambdas_replaced or sels_removed or locals_inlined return comp, modified
def remove_called_lambdas_and_blocks(comp): """Removes any called lambdas and blocks from `comp`. This function first resolves any higher-order functions, so that replacing called lambdas with blocks and then inlining the block locals cannot result in more called lambdas. It then performs this sequence of transformations, taking care to inline selections from tuples at appropriate stages to prevent possible combinatorial growth of the generated AST. Args: comp: Instance of `building_blocks.ComputationBuildingBlock` from which we want to remove called lambdas and blocks. Returns: A transformed version of `comp` which has no called lambdas or blocks, and no extraneous selections from tuples. """ py_typecheck.check_type(comp, building_blocks.ComputationBuildingBlock) comp, names_uniquified = tree_transformations.uniquify_reference_names( comp) # TODO(b/162888191): Remove this gating when `resolve_higher_order_functions` # is more efficient, or avoided. if _contains_higher_order_fns(comp): # `resolve_higher_order_functions` can be expensive, so we only call into it # when necessary. comp, fns_resolved = tree_transformations.resolve_higher_order_functions( comp) else: # We must still inline any functional references. We first inline selections # from tuples to prevent the AST from becoming unnecessarly large. comp, sels_removed = tree_transformations.inline_selections_from_tuple( comp) if sels_removed: comp, _ = tree_transformations.uniquify_reference_names(comp) comp, locals_inlined = tree_transformations.inline_block_locals(comp) fns_resolved = sels_removed or locals_inlined comp, lambdas_replaced = tree_transformations.replace_called_lambda_with_block( comp) if fns_resolved or lambdas_replaced: comp, _ = tree_transformations.uniquify_reference_names(comp) comp, sels_removed = tree_transformations.inline_selections_from_tuple( comp) if sels_removed: comp, _ = tree_transformations.uniquify_reference_names(comp) comp, locals_inlined = tree_transformations.inline_block_locals(comp) if locals_inlined: # Inlining local symbols may reintroduce selection-from-tuple pattern, # combinatorially increasing build times in the worst case. We ensure # here that remove_called_lambdas_and_blocks respects the postcondition that # selections from tuples are always collapsed. comp, _ = tree_transformations.inline_selections_from_tuple(comp) comp, _ = tree_transformations.uniquify_reference_names(comp) modified = names_uniquified or fns_resolved or lambdas_replaced or sels_removed or locals_inlined return comp, modified
def _resolve_calls_to_concrete_functions(comp): """Removes symbol bindings which contain functional types.""" comp, refs_renamed = tree_transformations.uniquify_reference_names(comp) comp, fns_resolved = tree_transformations.resolve_higher_order_functions( comp) comp, called_lambdas_replaced = tree_transformations.replace_called_lambda_with_block( comp) comp, selections_inlined = tree_transformations.inline_selections_from_tuple( comp) if selections_inlined: comp, _ = tree_transformations.uniquify_reference_names(comp) comp, fns_inlined = _inline_functions(comp) comp, locals_removed = tree_transformations.remove_unused_block_locals(comp) modified = ( refs_renamed or fns_resolved or called_lambdas_replaced or selections_inlined or fns_inlined or locals_removed) return comp, modified