<s> import ast , copy <EOL> from ast_utils import * <EOL> class Inliner : <EOL> def setup_inliner ( self , writer ) : <EOL> self . writer = writer <EOL> self . _with_inline = False <EOL> self . _inline = [ ] <EOL> self . _inline_ids = <NUM_LIT:0> <EOL> self . _inline_breakout = False <EOL> def inline_helper_remap_names ( self , remap ) : <EOL> return "<STR_LIT>" % '<STR_LIT:U+002C>' . join ( remap . values ( ) ) <EOL> def inline_helper_return_id ( self , return_id ) : <EOL> return "<STR_LIT>" % return_id <EOL> def inline_function ( self , node ) : <EOL> name = self . visit ( node . func ) <EOL> fnode = self . _global_functions [ name ] <EOL> fnode = copy . deepcopy ( fnode ) <EOL> finfo = inspect_function ( fnode ) <EOL> remap = { } <EOL> for n in finfo [ '<STR_LIT>' ] : <EOL> if n . id not in finfo [ '<STR_LIT>' ] : continue <EOL> if isinstance ( n . id , ast . Name ) : <EOL> raise RuntimeError <EOL> if n . id not in remap : <EOL> new_name = n . id + '<STR_LIT>' % self . _inline_ids <EOL> remap [ n . id ] = new_name <EOL> self . _inline_ids += <NUM_LIT:1> <EOL> n . id = remap [ n . id ] <EOL> if remap : <EOL> self . writer . write ( self . inline_helper_remap_names ( remap ) ) <EOL> for n in remap : <EOL> if n in finfo [ '<STR_LIT>' ] : <EOL> self . _func_typedefs [ remap [ n ] ] = finfo [ '<STR_LIT>' ] [ n ] <EOL> offset = len ( fnode . args . args ) - len ( fnode . args . defaults ) <EOL> for i , ad in enumerate ( fnode . args . args ) : <EOL> if i < len ( node . args ) : <EOL> ac = self . visit ( node . args [ i ] ) <EOL> else : <EOL> assert fnode . args . defaults <EOL> dindex = i - offset <EOL> ac = self . visit ( fnode . args . defaults [ dindex ] ) <EOL> ad = remap [ self . visit ( ad ) ] <EOL> self . writer . write ( "<STR_LIT>" % ( ad , ac ) ) <EOL> return_id = name + str ( self . _inline_ids ) <EOL> self . _inline . append ( return_id ) <EOL> self . writer . write ( self . inline_helper_return_id ( return_id ) ) <EOL> if True : <EOL> self . _inline_breakout = True <EOL> self . writer . write ( '<STR_LIT>' ) <EOL> self . writer . push ( ) <EOL> for b in fnode . body : <EOL> self . visit ( b ) <EOL> if not len ( finfo [ '<STR_LIT>' ] ) : <EOL> self . writer . write ( '<STR_LIT>' ) <EOL> self . writer . pull ( ) <EOL> else : <EOL> for b in fnode . body : <EOL> self . visit ( b ) <EOL> if self . _inline . pop ( ) != return_id : <EOL> raise RuntimeError <EOL> for n in remap : <EOL> gname = remap [ n ] <EOL> for n in finfo [ '<STR_LIT>' ] : <EOL> if n . id == gname : <EOL> n . id = n <EOL> return '<STR_LIT>' % return_id </s>
