<s> """<STR_LIT>""" <EOL> from llvmlite import ir <EOL> import llvmlite . llvmpy . core as lc <EOL> from . imputils import ( lower_builtin , lower_getattr_generic , lower_cast , <EOL> lower_constant , <EOL> iternext_impl , impl_ret_borrowed , impl_ret_untracked ) <EOL> from . . import typing , types , cgutils <EOL> @ lower_builtin ( types . NamedTupleClass , types . VarArg ( types . Any ) ) <EOL> def namedtuple_constructor ( context , builder , sig , args ) : <EOL> res = context . make_tuple ( builder , sig . return_type , args ) <EOL> return impl_ret_borrowed ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( len , types . BaseTuple ) <EOL> def tuple_len ( context , builder , sig , args ) : <EOL> tupty , = sig . args <EOL> retty = sig . return_type <EOL> res = context . get_constant ( retty , len ( tupty . types ) ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( bool , types . BaseTuple ) <EOL> def tuple_bool ( context , builder , sig , args ) : <EOL> tupty , = sig . args <EOL> if len ( tupty ) : <EOL> return cgutils . true_bit <EOL> else : <EOL> return cgutils . false_bit <EOL> @ lower_builtin ( '<STR_LIT:+>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_add ( context , builder , sig , args ) : <EOL> left , right = [ cgutils . unpack_tuple ( builder , x ) for x in args ] <EOL> return context . make_tuple ( builder , sig . return_type , left + right ) <EOL> def tuple_cmp_ordered ( context , builder , op , sig , args ) : <EOL> tu , tv = sig . args <EOL> u , v = args <EOL> res = cgutils . alloca_once_value ( builder , cgutils . true_bit ) <EOL> bbend = builder . append_basic_block ( "<STR_LIT>" ) <EOL> for i , ( ta , tb ) in enumerate ( zip ( tu . types , tv . types ) ) : <EOL> a = builder . extract_value ( u , i ) <EOL> b = builder . extract_value ( v , i ) <EOL> not_equal = context . generic_compare ( builder , '<STR_LIT>' , ( ta , tb ) , ( a , b ) ) <EOL> with builder . if_then ( not_equal ) : <EOL> pred = context . generic_compare ( builder , op , ( ta , tb ) , ( a , b ) ) <EOL> builder . store ( pred , res ) <EOL> builder . branch ( bbend ) <EOL> len_compare = eval ( "<STR_LIT>" % ( len ( tu . types ) , op , len ( tv . types ) ) ) <EOL> pred = context . get_constant ( types . boolean , len_compare ) <EOL> builder . store ( pred , res ) <EOL> builder . branch ( bbend ) <EOL> builder . position_at_end ( bbend ) <EOL> return builder . load ( res ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_eq ( context , builder , sig , args ) : <EOL> tu , tv = sig . args <EOL> u , v = args <EOL> if len ( tu . types ) != len ( tv . types ) : <EOL> res = context . get_constant ( types . boolean , False ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> res = context . get_constant ( types . boolean , True ) <EOL> for i , ( ta , tb ) in enumerate ( zip ( tu . types , tv . types ) ) : <EOL> a = builder . extract_value ( u , i ) <EOL> b = builder . extract_value ( v , i ) <EOL> pred = context . generic_compare ( builder , "<STR_LIT>" , ( ta , tb ) , ( a , b ) ) <EOL> res = builder . and_ ( res , pred ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_ne ( context , builder , sig , args ) : <EOL> res = builder . not_ ( tuple_eq ( context , builder , sig , args ) ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT:<>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_lt ( context , builder , sig , args ) : <EOL> res = tuple_cmp_ordered ( context , builder , '<STR_LIT:<>' , sig , args ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_le ( context , builder , sig , args ) : <EOL> res = tuple_cmp_ordered ( context , builder , '<STR_LIT>' , sig , args ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT:>>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_gt ( context , builder , sig , args ) : <EOL> res = tuple_cmp_ordered ( context , builder , '<STR_LIT:>>' , sig , args ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . BaseTuple , types . BaseTuple ) <EOL> def tuple_ge ( context , builder , sig , args ) : <EOL> res = tuple_cmp_ordered ( context , builder , '<STR_LIT>' , sig , args ) <EOL> return impl_ret_untracked ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( hash , types . BaseTuple ) <EOL> def hash_tuple ( context , builder , sig , args ) : <EOL> tupty , = sig . args <EOL> tup , = args <EOL> lty = context . get_value_type ( sig . return_type ) <EOL> h = ir . Constant ( lty , <NUM_LIT> ) <EOL> mult = ir . Constant ( lty , <NUM_LIT> ) <EOL> n = ir . Constant ( lty , len ( tupty ) ) <EOL> for i , ty in enumerate ( tupty . types ) : <EOL> h = builder . mul ( h , mult ) <EOL> val = builder . extract_value ( tup , i ) <EOL> hash_impl = context . get_function ( hash , <EOL> typing . signature ( sig . return_type , ty ) ) <EOL> h_val = hash_impl ( builder , ( val , ) ) <EOL> h = builder . xor ( h , h_val ) <EOL> mult = builder . add ( mult , n ) <EOL> return h <EOL> @ lower_getattr_generic ( types . BaseNamedTuple ) <EOL> def namedtuple_getattr ( context , builder , typ , value , attr ) : <EOL> """<STR_LIT>""" <EOL> index = typ . fields . index ( attr ) <EOL> res = builder . extract_value ( value , index ) <EOL> return impl_ret_borrowed ( context , builder , typ [ index ] , res ) <EOL> @ lower_constant ( types . UniTuple ) <EOL> @ lower_constant ( types . NamedUniTuple ) <EOL> def unituple_constant ( context , builder , ty , pyval ) : <EOL> """<STR_LIT>""" <EOL> consts = [ context . get_constant_generic ( builder , ty . dtype , v ) <EOL> for v in pyval ] <EOL> return ir . ArrayType ( consts [ <NUM_LIT:0> ] . type , len ( consts ) ) ( consts ) <EOL> @ lower_constant ( types . Tuple ) <EOL> @ lower_constant ( types . NamedTuple ) <EOL> def unituple_constant ( context , builder , ty , pyval ) : <EOL> """<STR_LIT>""" <EOL> consts = [ context . get_constant_generic ( builder , ty . types [ i ] , v ) <EOL> for i , v in enumerate ( pyval ) ] <EOL> return ir . Constant . literal_struct ( consts ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . UniTuple ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . NamedUniTuple ) <EOL> def getiter_unituple ( context , builder , sig , args ) : <EOL> [ tupty ] = sig . args <EOL> [ tup ] = args <EOL> iterval = context . make_helper ( builder , types . UniTupleIter ( tupty ) ) <EOL> index0 = context . get_constant ( types . intp , <NUM_LIT:0> ) <EOL> indexptr = cgutils . alloca_once ( builder , index0 . type ) <EOL> builder . store ( index0 , indexptr ) <EOL> iterval . index = indexptr <EOL> iterval . tuple = tup <EOL> res = iterval . _getvalue ( ) <EOL> return impl_ret_borrowed ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . UniTupleIter ) <EOL> @ iternext_impl <EOL> def iternext_unituple ( context , builder , sig , args , result ) : <EOL> [ tupiterty ] = sig . args <EOL> [ tupiter ] = args <EOL> iterval = context . make_helper ( builder , tupiterty , value = tupiter ) <EOL> tup = iterval . tuple <EOL> idxptr = iterval . index <EOL> idx = builder . load ( idxptr ) <EOL> count = context . get_constant ( types . intp , tupiterty . container . count ) <EOL> is_valid = builder . icmp ( lc . ICMP_SLT , idx , count ) <EOL> result . set_valid ( is_valid ) <EOL> with builder . if_then ( is_valid ) : <EOL> getitem_sig = typing . signature ( tupiterty . container . dtype , <EOL> tupiterty . container , <EOL> types . intp ) <EOL> getitem_out = getitem_unituple ( context , builder , getitem_sig , <EOL> [ tup , idx ] ) <EOL> result . yield_ ( getitem_out ) <EOL> nidx = builder . add ( idx , context . get_constant ( types . intp , <NUM_LIT:1> ) ) <EOL> builder . store ( nidx , iterval . index ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . UniTuple , types . intp ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . NamedUniTuple , types . intp ) <EOL> def getitem_unituple ( context , builder , sig , args ) : <EOL> tupty , _ = sig . args <EOL> tup , idx = args <EOL> bbelse = builder . append_basic_block ( "<STR_LIT>" ) <EOL> bbend = builder . append_basic_block ( "<STR_LIT>" ) <EOL> switch = builder . switch ( idx , bbelse ) <EOL> with builder . goto_block ( bbelse ) : <EOL> context . call_conv . return_user_exc ( builder , IndexError , <EOL> ( "<STR_LIT>" , ) ) <EOL> lrtty = context . get_value_type ( tupty . dtype ) <EOL> with builder . goto_block ( bbend ) : <EOL> phinode = builder . phi ( lrtty ) <EOL> for i in range ( tupty . count ) : <EOL> ki = context . get_constant ( types . intp , i ) <EOL> bbi = builder . append_basic_block ( "<STR_LIT>" % i ) <EOL> switch . add_case ( ki , bbi ) <EOL> with builder . goto_block ( bbi ) : <EOL> value = builder . extract_value ( tup , i ) <EOL> builder . branch ( bbend ) <EOL> phinode . add_incoming ( value , bbi ) <EOL> builder . position_at_end ( bbend ) <EOL> res = phinode <EOL> assert sig . return_type == tupty . dtype <EOL> return impl_ret_borrowed ( context , builder , sig . return_type , res ) <EOL> @ lower_builtin ( '<STR_LIT>' , types . BaseTuple , types . Const ) <EOL> def static_getitem_tuple ( context , builder , sig , args ) : <EOL> tupty , _ = sig . args <EOL> tup , idx = args <EOL> if isinstance ( idx , int ) : <EOL> if idx < <NUM_LIT:0> : <EOL> idx += len ( tupty ) <EOL> if not <NUM_LIT:0> <= idx < len ( tupty ) : <EOL> raise IndexError ( "<STR_LIT>" % ( idx , tupty ) ) <EOL> res = builder . extract_value ( tup , idx ) <EOL> elif isinstance ( idx , slice ) : <EOL> items = cgutils . unpack_tuple ( builder , tup ) [ idx ] <EOL> res = context . make_tuple ( builder , sig . return_type , items ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> % ( idx , sig . args [ <NUM_LIT:0> ] ) ) <EOL> return impl_ret_borrowed ( context , builder , sig . return_type , res ) <EOL> @ lower_cast ( types . BaseTuple , types . BaseTuple ) <EOL> def tuple_to_tuple ( context , builder , fromty , toty , val ) : <EOL> if ( isinstance ( fromty , types . BaseNamedTuple ) <EOL> or isinstance ( toty , types . BaseNamedTuple ) ) : <EOL> raise NotImplementedError <EOL> if len ( fromty ) != len ( toty ) : <EOL> raise NotImplementedError <EOL> olditems = cgutils . unpack_tuple ( builder , val , len ( fromty ) ) <EOL> items = [ context . cast ( builder , v , f , t ) <EOL> for v , f , t in zip ( olditems , fromty , toty ) ] <EOL> return context . make_tuple ( builder , toty , items ) </s>
