Fix pow(float, -) for MSVC.
[cython.git] / Cython / Compiler / ExprNodes.py
1 #
2 #   Pyrex - Parse tree nodes for expressions
3 #
4
5 import operator
6
7 from Errors import error, warning, warn_once, InternalError
8 from Errors import hold_errors, release_errors, held_errors, report_error
9 from Code import UtilityCode
10 import StringEncoding
11 import Naming
12 import Nodes
13 from Nodes import Node
14 import PyrexTypes
15 from PyrexTypes import py_object_type, c_long_type, typecast, error_type, \
16      unspecified_type
17 from Builtin import list_type, tuple_type, set_type, dict_type, \
18      unicode_type, str_type, bytes_type, type_type
19 import Builtin
20 import Symtab
21 import Options
22 from Cython import Utils
23 from Annotate import AnnotationItem
24 from Cython import Utils
25
26 from Cython.Debugging import print_call_chain
27 from DebugFlags import debug_disposal_code, debug_temp_alloc, \
28     debug_coercion
29
30 try:
31     set
32 except NameError:
33     from sets import Set as set
34
35 class NotConstant(object):
36     def __repr__(self):
37         return "<NOT CONSTANT>"
38
39 not_a_constant = NotConstant()
40 constant_value_not_set = object()
41
42 # error messages when coercing from key[0] to key[1]
43 find_coercion_error = {
44     # string related errors
45     (Builtin.unicode_type, Builtin.bytes_type) : "Cannot convert Unicode string to 'bytes' implicitly, encoding required.",
46     (Builtin.unicode_type, Builtin.str_type)   : "Cannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.",
47     (Builtin.unicode_type, PyrexTypes.c_char_ptr_type) : "Unicode objects do not support coercion to C types.",
48     (Builtin.bytes_type, Builtin.unicode_type) : "Cannot convert 'bytes' object to unicode implicitly, decoding required",
49     (Builtin.bytes_type, Builtin.str_type) : "Cannot convert 'bytes' object to str implicitly. This is not portable to Py3.",
50     (Builtin.str_type, Builtin.unicode_type) : "str objects do not support coercion to unicode, use a unicode string literal instead (u'')",
51     (Builtin.str_type, Builtin.bytes_type) : "Cannot convert 'str' to 'bytes' implicitly. This is not portable.",
52     (Builtin.str_type, PyrexTypes.c_char_ptr_type) : "'str' objects do not support coercion to C types (use 'bytes'?).",
53     (PyrexTypes.c_char_ptr_type, Builtin.unicode_type) : "Cannot convert 'char*' to unicode implicitly, decoding required",
54     (PyrexTypes.c_uchar_ptr_type, Builtin.unicode_type) : "Cannot convert 'char*' to unicode implicitly, decoding required",
55     }.get
56
57
58 class ExprNode(Node):
59     #  subexprs     [string]     Class var holding names of subexpr node attrs
60     #  type         PyrexType    Type of the result
61     #  result_code  string       Code fragment
62     #  result_ctype string       C type of result_code if different from type
63     #  is_temp      boolean      Result is in a temporary variable
64     #  is_sequence_constructor  
65     #               boolean      Is a list or tuple constructor expression
66     #  is_starred   boolean      Is a starred expression (e.g. '*a')
67     #  saved_subexpr_nodes
68     #               [ExprNode or [ExprNode or None] or None]
69     #                            Cached result of subexpr_nodes()
70     #  use_managed_ref boolean   use ref-counted temps/assignments/etc.
71     
72     result_ctype = None
73     type = None
74     temp_code = None
75     old_temp = None # error checker for multiple frees etc.
76     use_managed_ref = True # can be set by optimisation transforms
77
78     #  The Analyse Expressions phase for expressions is split
79     #  into two sub-phases:
80     #
81     #    Analyse Types
82     #      Determines the result type of the expression based
83     #      on the types of its sub-expressions, and inserts
84     #      coercion nodes into the expression tree where needed.
85     #      Marks nodes which will need to have temporary variables
86     #      allocated.
87     #
88     #    Allocate Temps
89     #      Allocates temporary variables where needed, and fills
90     #      in the result_code field of each node.
91     #
92     #  ExprNode provides some convenience routines which
93     #  perform both of the above phases. These should only
94     #  be called from statement nodes, and only when no
95     #  coercion nodes need to be added around the expression
96     #  being analysed. In that case, the above two phases
97     #  should be invoked separately.
98     #
99     #  Framework code in ExprNode provides much of the common
100     #  processing for the various phases. It makes use of the
101     #  'subexprs' class attribute of ExprNodes, which should
102     #  contain a list of the names of attributes which can
103     #  hold sub-nodes or sequences of sub-nodes.
104     #  
105     #  The framework makes use of a number of abstract methods. 
106     #  Their responsibilities are as follows.
107     #
108     #    Declaration Analysis phase
109     #
110     #      analyse_target_declaration
111     #        Called during the Analyse Declarations phase to analyse
112     #        the LHS of an assignment or argument of a del statement.
113     #        Nodes which cannot be the LHS of an assignment need not
114     #        implement it.
115     #
116     #    Expression Analysis phase
117     #
118     #      analyse_types
119     #        - Call analyse_types on all sub-expressions.
120     #        - Check operand types, and wrap coercion nodes around
121     #          sub-expressions where needed.
122     #        - Set the type of this node.
123     #        - If a temporary variable will be required for the
124     #          result, set the is_temp flag of this node.
125     #
126     #      analyse_target_types
127     #        Called during the Analyse Types phase to analyse
128     #        the LHS of an assignment or argument of a del 
129     #        statement. Similar responsibilities to analyse_types.
130     #
131     #      target_code
132     #        Called by the default implementation of allocate_target_temps.
133     #        Should return a C lvalue for assigning to the node. The default
134     #        implementation calls calculate_result_code.
135     #
136     #      check_const
137     #        - Check that this node and its subnodes form a
138     #          legal constant expression. If so, do nothing,
139     #          otherwise call not_const. 
140     #
141     #        The default implementation of check_const 
142     #        assumes that the expression is not constant.
143     #
144     #      check_const_addr
145     #        - Same as check_const, except check that the
146     #          expression is a C lvalue whose address is
147     #          constant. Otherwise, call addr_not_const.
148     #
149     #        The default implementation of calc_const_addr
150     #        assumes that the expression is not a constant 
151     #        lvalue.
152     #
153     #   Code Generation phase
154     #
155     #      generate_evaluation_code
156     #        - Call generate_evaluation_code for sub-expressions.
157     #        - Perform the functions of generate_result_code
158     #          (see below).
159     #        - If result is temporary, call generate_disposal_code
160     #          on all sub-expressions.
161     #
162     #        A default implementation of generate_evaluation_code
163     #        is provided which uses the following abstract methods:
164     #
165     #          generate_result_code
166     #            - Generate any C statements necessary to calculate
167     #              the result of this node from the results of its
168     #              sub-expressions.
169     #
170     #          calculate_result_code
171     #            - Should return a C code fragment evaluating to the 
172     #              result. This is only called when the result is not 
173     #              a temporary.
174     #
175     #      generate_assignment_code
176     #        Called on the LHS of an assignment.
177     #        - Call generate_evaluation_code for sub-expressions.
178     #        - Generate code to perform the assignment.
179     #        - If the assignment absorbed a reference, call
180     #          generate_post_assignment_code on the RHS,
181     #          otherwise call generate_disposal_code on it.
182     #
183     #      generate_deletion_code
184     #        Called on an argument of a del statement.
185     #        - Call generate_evaluation_code for sub-expressions.
186     #        - Generate code to perform the deletion.
187     #        - Call generate_disposal_code on all sub-expressions.
188     #
189     #
190     
191     is_sequence_constructor = 0
192     is_attribute = 0
193     
194     saved_subexpr_nodes = None
195     is_temp = 0
196     is_target = 0
197     is_starred = 0
198
199     constant_result = constant_value_not_set
200
201     try:
202         _get_child_attrs = operator.attrgetter('subexprs')
203     except AttributeError:
204         # Python 2.3
205         def _get_child_attrs(self):
206             return self.subexprs
207     child_attrs = property(fget=_get_child_attrs)
208         
209     def not_implemented(self, method_name):
210         print_call_chain(method_name, "not implemented") ###
211         raise InternalError(
212             "%s.%s not implemented" %
213                 (self.__class__.__name__, method_name))
214                 
215     def is_lvalue(self):
216         return 0
217     
218     def is_ephemeral(self):
219         #  An ephemeral node is one whose result is in
220         #  a Python temporary and we suspect there are no
221         #  other references to it. Certain operations are
222         #  disallowed on such values, since they are
223         #  likely to result in a dangling pointer.
224         return self.type.is_pyobject and self.is_temp
225
226     def subexpr_nodes(self):
227         #  Extract a list of subexpression nodes based
228         #  on the contents of the subexprs class attribute.
229         nodes = []
230         for name in self.subexprs:
231             item = getattr(self, name)
232             if item is not None:
233                 if type(item) is list:
234                     nodes.extend(item)
235                 else:
236                     nodes.append(item)
237         return nodes
238         
239     def result(self):
240         if self.is_temp:
241             return self.temp_code
242         else:
243             return self.calculate_result_code()
244     
245     def result_as(self, type = None):
246         #  Return the result code cast to the specified C type.
247         return typecast(type, self.ctype(), self.result())
248     
249     def py_result(self):
250         #  Return the result code cast to PyObject *.
251         return self.result_as(py_object_type)
252     
253     def ctype(self):
254         #  Return the native C type of the result (i.e. the
255         #  C type of the result_code expression).
256         return self.result_ctype or self.type
257
258     def get_constant_c_result_code(self):
259         # Return the constant value of this node as a result code
260         # string, or None if the node is not constant.  This method
261         # can be called when the constant result code is required
262         # before the code generation phase.
263         #
264         # The return value is a string that can represent a simple C
265         # value, a constant C name or a constant C expression.  If the
266         # node type depends on Python code, this must return None.
267         return None
268
269     def calculate_constant_result(self):
270         # Calculate the constant compile time result value of this
271         # expression and store it in ``self.constant_result``.  Does
272         # nothing by default, thus leaving ``self.constant_result``
273         # unknown.  If valid, the result can be an arbitrary Python
274         # value.
275         #
276         # This must only be called when it is assured that all
277         # sub-expressions have a valid constant_result value.  The
278         # ConstantFolding transform will do this.
279         pass
280
281     def has_constant_result(self):
282         return self.constant_result is not constant_value_not_set and \
283                self.constant_result is not not_a_constant
284
285     def compile_time_value(self, denv):
286         #  Return value of compile-time expression, or report error.
287         error(self.pos, "Invalid compile-time expression")
288     
289     def compile_time_value_error(self, e):
290         error(self.pos, "Error in compile-time expression: %s: %s" % (
291             e.__class__.__name__, e))
292     
293     # ------------- Declaration Analysis ----------------
294     
295     def analyse_target_declaration(self, env):
296         error(self.pos, "Cannot assign to or delete this")
297     
298     # ------------- Expression Analysis ----------------
299     
300     def analyse_const_expression(self, env):
301         #  Called during the analyse_declarations phase of a
302         #  constant expression. Analyses the expression's type,
303         #  checks whether it is a legal const expression,
304         #  and determines its value.
305         self.analyse_types(env)
306         return self.check_const()
307     
308     def analyse_expressions(self, env):
309         #  Convenience routine performing both the Type
310         #  Analysis and Temp Allocation phases for a whole 
311         #  expression.
312         self.analyse_types(env)
313     
314     def analyse_target_expression(self, env, rhs):
315         #  Convenience routine performing both the Type
316         #  Analysis and Temp Allocation phases for the LHS of
317         #  an assignment.
318         self.analyse_target_types(env)
319     
320     def analyse_boolean_expression(self, env):
321         #  Analyse expression and coerce to a boolean.
322         self.analyse_types(env)
323         bool = self.coerce_to_boolean(env)
324         return bool
325     
326     def analyse_temp_boolean_expression(self, env):
327         #  Analyse boolean expression and coerce result into
328         #  a temporary. This is used when a branch is to be
329         #  performed on the result and we won't have an
330         #  opportunity to ensure disposal code is executed
331         #  afterwards. By forcing the result into a temporary,
332         #  we ensure that all disposal has been done by the
333         #  time we get the result.
334         self.analyse_types(env)
335         return self.coerce_to_boolean(env).coerce_to_simple(env)
336
337     # --------------- Type Inference -----------------
338     
339     def type_dependencies(self, env):
340         # Returns the list of entries whose types must be determined
341         # before the type of self can be infered.
342         if hasattr(self, 'type') and self.type is not None:
343             return ()
344         return sum([node.type_dependencies(env) for node in self.subexpr_nodes()], ())
345     
346     def infer_type(self, env):
347         # Attempt to deduce the type of self. 
348         # Differs from analyse_types as it avoids unnecessary 
349         # analysis of subexpressions, but can assume everything
350         # in self.type_dependencies() has been resolved.
351         if hasattr(self, 'type') and self.type is not None:
352             return self.type
353         elif hasattr(self, 'entry') and self.entry is not None:
354             return self.entry.type
355         else:
356             self.not_implemented("infer_type")
357     
358     # --------------- Type Analysis ------------------
359     
360     def analyse_as_module(self, env):
361         # If this node can be interpreted as a reference to a
362         # cimported module, return its scope, else None.
363         return None
364         
365     def analyse_as_type(self, env):
366         # If this node can be interpreted as a reference to a
367         # type, return that type, else None.
368         return None
369     
370     def analyse_as_extension_type(self, env):
371         # If this node can be interpreted as a reference to an
372         # extension type, return its type, else None.
373         return None
374     
375     def analyse_types(self, env):
376         self.not_implemented("analyse_types")
377     
378     def analyse_target_types(self, env):
379         self.analyse_types(env)
380
381     def nogil_check(self, env):
382         # By default, any expression based on Python objects is
383         # prevented in nogil environments.  Subtypes must override
384         # this if they can work without the GIL.
385         if self.type.is_pyobject:
386             self.gil_error()
387
388     def gil_assignment_check(self, env):
389         if env.nogil and self.type.is_pyobject:
390             error(self.pos, "Assignment of Python object not allowed without gil")
391
392     def check_const(self):
393         self.not_const()
394         return False
395     
396     def not_const(self):
397         error(self.pos, "Not allowed in a constant expression")
398     
399     def check_const_addr(self):
400         self.addr_not_const()
401         return False
402     
403     def addr_not_const(self):
404         error(self.pos, "Address is not constant")
405
406     # ----------------- Result Allocation -----------------
407     
408     def result_in_temp(self):
409         #  Return true if result is in a temporary owned by
410         #  this node or one of its subexpressions. Overridden
411         #  by certain nodes which can share the result of
412         #  a subnode.
413         return self.is_temp
414             
415     def target_code(self):
416         #  Return code fragment for use as LHS of a C assignment.
417         return self.calculate_result_code()
418     
419     def calculate_result_code(self):
420         self.not_implemented("calculate_result_code")
421     
422 #    def release_target_temp(self, env):
423 #        #  Release temporaries used by LHS of an assignment.
424 #        self.release_subexpr_temps(env)
425
426     def allocate_temp_result(self, code):
427         if self.temp_code:
428             raise RuntimeError("Temp allocated multiple times in %r: %r" % (self.__class__.__name__, self.pos))
429         type = self.type
430         if not type.is_void:
431             if type.is_pyobject:
432                 type = PyrexTypes.py_object_type
433             self.temp_code = code.funcstate.allocate_temp(
434                 type, manage_ref=self.use_managed_ref)
435         else:
436             self.temp_code = None
437
438     def release_temp_result(self, code):
439         if not self.temp_code:
440             if self.old_temp:
441                 raise RuntimeError("temp %s released multiple times in %s" % (
442                         self.old_temp, self.__class__.__name__))
443             else:
444                 raise RuntimeError("no temp, but release requested in %s" % (
445                         self.__class__.__name__))
446         code.funcstate.release_temp(self.temp_code)
447         self.old_temp = self.temp_code
448         self.temp_code = None
449
450     # ---------------- Code Generation -----------------
451     
452     def make_owned_reference(self, code):
453         #  If result is a pyobject, make sure we own
454         #  a reference to it.
455         if self.type.is_pyobject and not self.result_in_temp():
456             code.put_incref(self.result(), self.ctype())
457     
458     def generate_evaluation_code(self, code):
459         code.mark_pos(self.pos)
460         
461         #  Generate code to evaluate this node and
462         #  its sub-expressions, and dispose of any
463         #  temporary results of its sub-expressions.
464         self.generate_subexpr_evaluation_code(code)
465
466         if self.is_temp:
467             self.allocate_temp_result(code)
468
469         self.generate_result_code(code)
470         if self.is_temp:
471             # If we are temp we do not need to wait until this node is disposed
472             # before disposing children.
473             self.generate_subexpr_disposal_code(code)
474             self.free_subexpr_temps(code)
475
476     def generate_subexpr_evaluation_code(self, code):
477         for node in self.subexpr_nodes():
478             node.generate_evaluation_code(code)
479     
480     def generate_result_code(self, code):
481         self.not_implemented("generate_result_code")
482     
483     def generate_disposal_code(self, code):
484         if self.is_temp:
485             if self.type.is_pyobject:
486                 code.put_decref_clear(self.result(), self.ctype())
487         else:
488             # Already done if self.is_temp
489             self.generate_subexpr_disposal_code(code)
490
491     def generate_subexpr_disposal_code(self, code):
492         #  Generate code to dispose of temporary results
493         #  of all sub-expressions.
494         for node in self.subexpr_nodes():
495             node.generate_disposal_code(code)
496     
497     def generate_post_assignment_code(self, code):
498         if self.is_temp:
499             if self.type.is_pyobject:
500                 code.putln("%s = 0;" % self.result())
501         else:
502             self.generate_subexpr_disposal_code(code)
503
504     def generate_assignment_code(self, rhs, code):
505         #  Stub method for nodes which are not legal as
506         #  the LHS of an assignment. An error will have 
507         #  been reported earlier.
508         pass
509     
510     def generate_deletion_code(self, code):
511         #  Stub method for nodes that are not legal as
512         #  the argument of a del statement. An error
513         #  will have been reported earlier.
514         pass
515
516     def free_temps(self, code):
517         if self.is_temp:
518             if not self.type.is_void:
519                 self.release_temp_result(code)
520         else:
521             self.free_subexpr_temps(code)
522     
523     def free_subexpr_temps(self, code):
524         for sub in self.subexpr_nodes():
525             sub.free_temps(code)
526
527     def generate_function_definitions(self, env, code):
528         pass
529
530     # ---------------- Annotation ---------------------
531     
532     def annotate(self, code):
533         for node in self.subexpr_nodes():
534             node.annotate(code)
535     
536     # ----------------- Coercion ----------------------
537     
538     def coerce_to(self, dst_type, env):
539         #   Coerce the result so that it can be assigned to
540         #   something of type dst_type. If processing is necessary,
541         #   wraps this node in a coercion node and returns that.
542         #   Otherwise, returns this node unchanged.
543         #
544         #   This method is called during the analyse_expressions
545         #   phase of the src_node's processing.
546         #
547         #   Note that subclasses that override this (especially
548         #   ConstNodes) must not (re-)set their own .type attribute
549         #   here.  Since expression nodes may turn up in different
550         #   places in the tree (e.g. inside of CloneNodes in cascaded
551         #   assignments), this method must return a new node instance
552         #   if it changes the type.
553         #
554         src = self
555         src_type = self.type
556         src_is_py_type = src_type.is_pyobject
557         dst_is_py_type = dst_type.is_pyobject
558
559         if self.check_for_coercion_error(dst_type):
560             return self
561
562         if dst_type.is_pyobject:
563             if not src.type.is_pyobject:
564                 if dst_type is bytes_type and src.type.is_int:
565                     src = CoerceIntToBytesNode(src, env)
566                 else:
567                     src = CoerceToPyTypeNode(src, env)
568             if not src.type.subtype_of(dst_type):
569                 if not isinstance(src, NoneNode):
570                     src = PyTypeTestNode(src, dst_type, env)
571         elif src.type.is_pyobject:
572             src = CoerceFromPyTypeNode(dst_type, src, env)
573         elif (dst_type.is_complex 
574               and src_type != dst_type
575               and dst_type.assignable_from(src_type)):
576             src = CoerceToComplexNode(src, dst_type, env)
577         else: # neither src nor dst are py types
578             # Added the string comparison, since for c types that
579             # is enough, but Cython gets confused when the types are
580             # in different pxi files.
581             if not (str(src.type) == str(dst_type) or dst_type.assignable_from(src_type)):
582                 self.fail_assignment(dst_type)
583         return src
584
585     def fail_assignment(self, dst_type):
586         error(self.pos, "Cannot assign type '%s' to '%s'" % (self.type, dst_type))
587
588     def check_for_coercion_error(self, dst_type, fail=False, default=None):
589         if fail and not default:
590             default = "Cannot assign type '%(FROM)s' to '%(TO)s'"
591         message = find_coercion_error((self.type, dst_type), default)
592         if message is not None:
593             error(self.pos, message % {'FROM': self.type, 'TO': dst_type})
594             return True
595         if fail:
596             self.fail_assignment(dst_type)
597             return True
598         return False
599
600     def coerce_to_pyobject(self, env):
601         return self.coerce_to(PyrexTypes.py_object_type, env)
602
603     def coerce_to_boolean(self, env):
604         #  Coerce result to something acceptable as
605         #  a boolean value.
606
607         # if it's constant, calculate the result now
608         if self.has_constant_result():
609             bool_value = bool(self.constant_result)
610             return BoolNode(self.pos, value=bool_value,
611                             constant_result=bool_value)
612
613         type = self.type
614         if type.is_pyobject or type.is_ptr or type.is_float:
615             return CoerceToBooleanNode(self, env)
616         else:
617             if not (type.is_int or type.is_enum or type.is_error):
618                 error(self.pos, 
619                     "Type '%s' not acceptable as a boolean" % type)
620             return self
621     
622     def coerce_to_integer(self, env):
623         # If not already some C integer type, coerce to longint.
624         if self.type.is_int:
625             return self
626         else:
627             return self.coerce_to(PyrexTypes.c_long_type, env)
628     
629     def coerce_to_temp(self, env):
630         #  Ensure that the result is in a temporary.
631         if self.result_in_temp():
632             return self
633         else:
634             return CoerceToTempNode(self, env)
635     
636     def coerce_to_simple(self, env):
637         #  Ensure that the result is simple (see is_simple).
638         if self.is_simple():
639             return self
640         else:
641             return self.coerce_to_temp(env)
642     
643     def is_simple(self):
644         #  A node is simple if its result is something that can
645         #  be referred to without performing any operations, e.g.
646         #  a constant, local var, C global var, struct member
647         #  reference, or temporary.
648         return self.result_in_temp()
649
650     def may_be_none(self):
651         return self.type.is_pyobject
652
653     def as_cython_attribute(self):
654         return None
655
656     def as_none_safe_node(self, message, error="PyExc_TypeError"):
657         # Wraps the node in a NoneCheckNode if it is not known to be
658         # not-None (e.g. because it is a Python literal).
659         if self.may_be_none():
660             return NoneCheckNode(self, error, message)
661         else:
662             return self
663
664
665 class AtomicExprNode(ExprNode):
666     #  Abstract base class for expression nodes which have
667     #  no sub-expressions.
668     
669     subexprs = []
670
671     # Override to optimize -- we know we have no children
672     def generate_subexpr_evaluation_code(self, code):
673         pass
674     def generate_subexpr_disposal_code(self, code):
675         pass
676
677 class PyConstNode(AtomicExprNode):
678     #  Abstract base class for constant Python values.
679     
680     is_literal = 1
681     type = py_object_type
682     
683     def is_simple(self):
684         return 1
685
686     def may_be_none(self):
687         return False
688
689     def analyse_types(self, env):
690         pass
691     
692     def calculate_result_code(self):
693         return self.value
694
695     def generate_result_code(self, code):
696         pass
697
698
699 class NoneNode(PyConstNode):
700     #  The constant value None
701     
702     value = "Py_None"
703
704     constant_result = None
705     
706     nogil_check = None
707
708     def compile_time_value(self, denv):
709         return None
710
711     def may_be_none(self):
712         return True
713
714
715 class EllipsisNode(PyConstNode):
716     #  '...' in a subscript list.
717     
718     value = "Py_Ellipsis"
719
720     constant_result = Ellipsis
721
722     def compile_time_value(self, denv):
723         return Ellipsis
724
725
726 class ConstNode(AtomicExprNode):
727     # Abstract base type for literal constant nodes.
728     #
729     # value     string      C code fragment
730     
731     is_literal = 1
732     nogil_check = None
733
734     def is_simple(self):
735         return 1
736
737     def may_be_none(self):
738         return False
739
740     def analyse_types(self, env):
741         pass # Types are held in class variables
742     
743     def check_const(self):
744         return True
745     
746     def get_constant_c_result_code(self):
747         return self.calculate_result_code()
748
749     def calculate_result_code(self):
750         return str(self.value)
751
752     def generate_result_code(self, code):
753         pass
754
755
756 class BoolNode(ConstNode):
757     type = PyrexTypes.c_bint_type
758     #  The constant value True or False
759
760     def calculate_constant_result(self):
761         self.constant_result = self.value
762
763     def compile_time_value(self, denv):
764         return self.value
765     
766     def calculate_result_code(self):
767         return str(int(self.value))
768
769
770 class NullNode(ConstNode):
771     type = PyrexTypes.c_null_ptr_type
772     value = "NULL"
773     constant_result = 0
774
775     def get_constant_c_result_code(self):
776         return self.value
777
778
779 class CharNode(ConstNode):
780     type = PyrexTypes.c_char_type
781
782     def calculate_constant_result(self):
783         self.constant_result = ord(self.value)
784     
785     def compile_time_value(self, denv):
786         return ord(self.value)
787     
788     def calculate_result_code(self):
789         return "'%s'" % StringEncoding.escape_char(self.value)
790
791
792 class IntNode(ConstNode):
793
794     # unsigned     "" or "U"
795     # longness     "" or "L" or "LL"
796
797     unsigned = ""
798     longness = ""
799
800     def __init__(self, pos, **kwds):
801         ExprNode.__init__(self, pos, **kwds)
802         if 'type' not in kwds:
803             rank = max(1, len(self.longness))
804             sign = not self.unsigned
805             self.type = PyrexTypes.modifiers_and_name_to_type[sign, rank, "int"]
806
807     def coerce_to(self, dst_type, env):
808         if self.type is dst_type:
809             return self
810         elif dst_type.is_float:
811             float_value = float(self.value)
812             return FloatNode(self.pos, value=repr(float_value), constant_result=float_value)
813         node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
814                        unsigned=self.unsigned, longness=self.longness)
815         if dst_type.is_numeric and not dst_type.is_complex:
816             return node
817         if dst_type.is_pyobject:
818             node.type = PyrexTypes.py_object_type
819         # We still need to perform normal coerce_to processing on the
820         # result, because we might be coercing to an extension type,
821         # in which case a type test node will be needed.
822         return ConstNode.coerce_to(node, dst_type, env)
823
824     def coerce_to_boolean(self, env):
825         return IntNode(
826             self.pos, value=self.value,
827             type = PyrexTypes.c_bint_type,
828             unsigned=self.unsigned, longness=self.longness)
829
830     def generate_evaluation_code(self, code):
831         if self.type.is_pyobject:
832             # pre-allocate a Python version of the number
833             self.result_code = code.get_py_num(self.value, self.longness)
834         else:
835             self.result_code = self.get_constant_c_result_code()
836     
837     def get_constant_c_result_code(self):
838         value = self.value
839         if isinstance(value, basestring) and len(value) > 2:
840             # must convert C-incompatible Py3 oct/bin notations
841             if value[1] in 'oO':
842                 value = value[0] + value[2:] # '0o123' => '0123'
843             elif value[1] in 'bB':
844                 value = int(value[2:], 2)
845         return str(value) + self.unsigned + self.longness
846
847     def calculate_result_code(self):
848         return self.result_code
849
850     def calculate_constant_result(self):
851         self.constant_result = Utils.str_to_number(self.value)
852
853     def compile_time_value(self, denv):
854         return Utils.str_to_number(self.value)
855
856
857 class FloatNode(ConstNode):
858     type = PyrexTypes.c_double_type
859
860     def calculate_constant_result(self):
861         self.constant_result = float(self.value)
862
863     def compile_time_value(self, denv):
864         return float(self.value)
865     
866     def calculate_result_code(self):
867         strval = self.value
868         assert isinstance(strval, (str, unicode))
869         cmpval = repr(float(strval))
870         if cmpval == 'nan':
871             return "(Py_HUGE_VAL * 0)"
872         elif cmpval == 'inf':
873             return "Py_HUGE_VAL"
874         elif cmpval == '-inf':
875             return "(-Py_HUGE_VAL)"
876         else:
877             return strval
878
879
880 class BytesNode(ConstNode):
881     # A char* or bytes literal
882     #
883     # value      BytesLiteral
884
885     type = PyrexTypes.c_char_ptr_type
886
887     def compile_time_value(self, denv):
888         return self.value
889
890     def analyse_as_type(self, env):
891         type = PyrexTypes.parse_basic_type(self.value)
892         if type is not None:    
893             return type
894         from TreeFragment import TreeFragment
895         pos = (self.pos[0], self.pos[1], self.pos[2]-7)
896         declaration = TreeFragment(u"sizeof(%s)" % self.value, name=pos[0].filename, initial_pos=pos)
897         sizeof_node = declaration.root.stats[0].expr
898         sizeof_node.analyse_types(env)
899         if isinstance(sizeof_node, SizeofTypeNode):
900             return sizeof_node.arg_type
901
902     def can_coerce_to_char_literal(self):
903         return len(self.value) == 1
904
905     def coerce_to_boolean(self, env):
906         # This is special because we start off as a C char*.  Testing
907         # that for truth directly would yield the wrong result.
908         return BoolNode(self.pos, value=bool(self.value))
909
910     def coerce_to(self, dst_type, env):
911         if dst_type.is_int:
912             if not self.can_coerce_to_char_literal():
913                 error(self.pos, "Only single-character string literals can be coerced into ints.")
914                 return self
915             if dst_type is PyrexTypes.c_py_unicode_type:
916                 error(self.pos, "Bytes literals cannot coerce to Py_UNICODE, use a unicode literal instead.")
917                 return self
918             return CharNode(self.pos, value=self.value)
919
920         node = BytesNode(self.pos, value=self.value)
921         if dst_type == PyrexTypes.c_char_ptr_type:
922             node.type = PyrexTypes.c_char_ptr_type
923             return node
924         elif dst_type == PyrexTypes.c_uchar_ptr_type:
925             node.type = PyrexTypes.c_char_ptr_type
926             return CastNode(node, PyrexTypes.c_uchar_ptr_type)
927
928         if not self.type.is_pyobject:
929             if dst_type in (py_object_type, Builtin.bytes_type):
930                 node.type = Builtin.bytes_type
931             elif dst_type.is_pyobject:
932                 self.fail_assignment(dst_type)
933                 return self
934         elif dst_type.is_pyobject and dst_type is not py_object_type:
935             self.check_for_coercion_error(dst_type, fail=True)
936             return node
937
938         # We still need to perform normal coerce_to processing on the
939         # result, because we might be coercing to an extension type,
940         # in which case a type test node will be needed.
941         return ConstNode.coerce_to(node, dst_type, env)
942
943     def as_py_string_node(self, env):
944         # Return a new BytesNode with the same value as this node
945         # but whose type is a Python type instead of a C type.
946         return BytesNode(self.pos, value = self.value, type = Builtin.bytes_type)
947
948     def generate_evaluation_code(self, code):
949         if self.type.is_pyobject:
950             self.result_code = code.get_py_string_const(self.value)
951         else:
952             self.result_code = code.get_string_const(self.value)
953
954     def get_constant_c_result_code(self):
955         return None # FIXME
956     
957     def calculate_result_code(self):
958         return self.result_code
959
960
961 class UnicodeNode(PyConstNode):
962     # A Python unicode object
963     #
964     # value        EncodedString
965     # bytes_value  BytesLiteral    the literal parsed as bytes string ('-3' unicode literals only)
966
967     bytes_value = None
968     type = unicode_type
969
970     def coerce_to(self, dst_type, env):
971         if dst_type is self.type:
972             pass
973         elif dst_type is PyrexTypes.c_py_unicode_type:
974             if not self.can_coerce_to_char_literal():
975                 error(self.pos, "Only single-character Unicode string literals can be coerced into Py_UNICODE.")
976                 return self
977             int_value = ord(self.value)
978             return IntNode(self.pos, value=int_value, constant_result=int_value)
979         elif not dst_type.is_pyobject:
980             if dst_type.is_string and self.bytes_value is not None:
981                 # special case: '-3' enforced unicode literal used in a C char* context
982                 return BytesNode(self.pos, value=self.bytes_value).coerce_to(dst_type, env)
983             error(self.pos, "Unicode literals do not support coercion to C types other than Py_UNICODE.")
984         elif dst_type is not py_object_type:
985             if not self.check_for_coercion_error(dst_type):
986                 self.fail_assignment(dst_type)
987         return self
988
989     def can_coerce_to_char_literal(self):
990         return len(self.value) == 1
991
992     def contains_surrogates(self):
993         # Check if the unicode string contains surrogate code points
994         # on a CPython platform with wide (UCS-4) or narrow (UTF-16)
995         # Unicode, i.e. characters that would be spelled as two
996         # separate code units on a narrow platform.
997         for c in map(ord, self.value):
998             if c > 65535: # can only happen on wide platforms
999                 return True
1000             # We only look for the first code unit (D800-DBFF) of a
1001             # surrogate pair - if we find one, the other one
1002             # (DC00-DFFF) is likely there, too.  If we don't find it,
1003             # any second code unit cannot make for a surrogate pair by
1004             # itself.
1005             if c >= 0xD800 and c <= 0xDBFF:
1006                 return True
1007         return False
1008
1009     def generate_evaluation_code(self, code):
1010         self.result_code = code.get_py_string_const(self.value)
1011
1012     def calculate_result_code(self):
1013         return self.result_code
1014         
1015     def compile_time_value(self, env):
1016         return self.value
1017
1018
1019 class StringNode(PyConstNode):
1020     # A Python str object, i.e. a byte string in Python 2.x and a
1021     # unicode string in Python 3.x
1022     #
1023     # value          BytesLiteral
1024     # unicode_value  EncodedString
1025     # is_identifier  boolean
1026
1027     type = str_type
1028     is_identifier = None
1029     unicode_value = None
1030
1031     def coerce_to(self, dst_type, env):
1032         if dst_type is not py_object_type and not str_type.subtype_of(dst_type):
1033 #            if dst_type is Builtin.bytes_type:
1034 #                # special case: bytes = 'str literal'
1035 #                return BytesNode(self.pos, value=self.value)
1036             if not dst_type.is_pyobject:
1037                 return BytesNode(self.pos, value=self.value).coerce_to(dst_type, env)
1038             self.check_for_coercion_error(dst_type, fail=True)
1039
1040         # this will be a unicode string in Py3, so make sure we can decode it
1041         if self.value.encoding:
1042             encoding = self.value.encoding
1043             try:
1044                 self.value.decode(encoding)
1045             except UnicodeDecodeError:
1046                 error(self.pos, "String decoding as '%s' failed. Consider using a byte string or unicode string explicitly, or adjust the source code encoding." % encoding)
1047
1048         return self
1049
1050     def can_coerce_to_char_literal(self):
1051         return not self.is_identifier and len(self.value) == 1
1052
1053     def generate_evaluation_code(self, code):
1054         self.result_code = code.get_py_string_const(
1055             self.value, identifier=self.is_identifier, is_str=True)
1056
1057     def get_constant_c_result_code(self):
1058         return None
1059
1060     def calculate_result_code(self):
1061         return self.result_code
1062         
1063     def compile_time_value(self, env):
1064         return self.value
1065
1066
1067 class IdentifierStringNode(StringNode):
1068     # A special str value that represents an identifier (bytes in Py2,
1069     # unicode in Py3).
1070     is_identifier = True
1071
1072
1073 class LongNode(AtomicExprNode):
1074     #  Python long integer literal
1075     #
1076     #  value   string
1077
1078     type = py_object_type
1079
1080     def calculate_constant_result(self):
1081         self.constant_result = Utils.str_to_number(self.value)
1082     
1083     def compile_time_value(self, denv):
1084         return Utils.str_to_number(self.value)
1085     
1086     def analyse_types(self, env):
1087         self.is_temp = 1
1088
1089     def may_be_none(self):
1090         return False
1091
1092     gil_message = "Constructing Python long int"
1093
1094     def generate_result_code(self, code):
1095         code.putln(
1096             '%s = PyLong_FromString((char *)"%s", 0, 0); %s' % (
1097                 self.result(),
1098                 self.value,
1099                 code.error_goto_if_null(self.result(), self.pos)))
1100         code.put_gotref(self.py_result())
1101
1102
1103 class ImagNode(AtomicExprNode):
1104     #  Imaginary number literal
1105     #
1106     #  value   float    imaginary part
1107     
1108     type = PyrexTypes.c_double_complex_type
1109
1110     def calculate_constant_result(self):
1111         self.constant_result = complex(0.0, self.value)
1112     
1113     def compile_time_value(self, denv):
1114         return complex(0.0, self.value)
1115     
1116     def analyse_types(self, env):
1117         self.type.create_declaration_utility_code(env)
1118
1119     def may_be_none(self):
1120         return False
1121
1122     def coerce_to(self, dst_type, env):
1123         if self.type is dst_type:
1124             return self
1125         node = ImagNode(self.pos, value=self.value)
1126         if dst_type.is_pyobject:
1127             node.is_temp = 1
1128             node.type = PyrexTypes.py_object_type
1129         # We still need to perform normal coerce_to processing on the
1130         # result, because we might be coercing to an extension type,
1131         # in which case a type test node will be needed.
1132         return AtomicExprNode.coerce_to(node, dst_type, env)
1133
1134     gil_message = "Constructing complex number"
1135
1136     def calculate_result_code(self):
1137         if self.type.is_pyobject:
1138             return self.result()
1139         else:
1140             return "%s(0, %r)" % (self.type.from_parts, float(self.value))
1141
1142     def generate_result_code(self, code):
1143         if self.type.is_pyobject:
1144             code.putln(
1145                 "%s = PyComplex_FromDoubles(0.0, %r); %s" % (
1146                     self.result(),
1147                     float(self.value),
1148                     code.error_goto_if_null(self.result(), self.pos)))
1149             code.put_gotref(self.py_result())
1150         
1151
1152 class NewExprNode(AtomicExprNode):
1153
1154     # C++ new statement
1155     #
1156     # cppclass              node                 c++ class to create
1157     
1158     type = None
1159     
1160     def infer_type(self, env):
1161         type = self.cppclass.analyse_as_type(env)
1162         if type is None or not type.is_cpp_class:
1163             error(self.pos, "new operator can only be applied to a C++ class")
1164             self.type = error_type
1165             return
1166         self.cpp_check(env)
1167         constructor = type.scope.lookup(u'<init>')
1168         if constructor is None:
1169             return_type = PyrexTypes.CFuncType(type, [])
1170             return_type = PyrexTypes.CPtrType(return_type)
1171             type.scope.declare_cfunction(u'<init>', return_type, self.pos)
1172             constructor = type.scope.lookup(u'<init>')
1173         self.class_type = type
1174         self.entry = constructor
1175         self.type = constructor.type
1176         return self.type
1177     
1178     def analyse_types(self, env):
1179         if self.type is None:
1180             self.infer_type(env)
1181
1182     def may_be_none(self):
1183         return False
1184
1185     def generate_result_code(self, code):
1186         pass
1187    
1188     def calculate_result_code(self):
1189         return "new " + self.class_type.declaration_code("")
1190
1191
1192 class NameNode(AtomicExprNode):
1193     #  Reference to a local or global variable name.
1194     #
1195     #  name            string    Python name of the variable
1196     #  entry           Entry     Symbol table entry
1197     #  type_entry      Entry     For extension type names, the original type entry
1198     
1199     is_name = True
1200     is_cython_module = False
1201     cython_attribute = None
1202     lhs_of_first_assignment = False
1203     is_used_as_rvalue = 0
1204     entry = None
1205     type_entry = None
1206
1207     def create_analysed_rvalue(pos, env, entry):
1208         node = NameNode(pos)
1209         node.analyse_types(env, entry=entry)
1210         return node
1211         
1212     def as_cython_attribute(self):
1213         return self.cython_attribute
1214     
1215     create_analysed_rvalue = staticmethod(create_analysed_rvalue)
1216     
1217     def type_dependencies(self, env):
1218         if self.entry is None:
1219             self.entry = env.lookup(self.name)
1220         if self.entry is not None and self.entry.type.is_unspecified:
1221             return (self.entry,)
1222         else:
1223             return ()
1224     
1225     def infer_type(self, env):
1226         if self.entry is None:
1227             self.entry = env.lookup(self.name)
1228         if self.entry is None:
1229             return py_object_type
1230         elif (self.entry.type.is_extension_type or self.entry.type.is_builtin_type) and \
1231                 self.name == self.entry.type.name:
1232             # Unfortunately the type attribute of type objects
1233             # is used for the pointer to the type they represent.
1234             return type_type
1235         else:
1236             return self.entry.type
1237     
1238     def compile_time_value(self, denv):
1239         try:
1240             return denv.lookup(self.name)
1241         except KeyError:
1242             error(self.pos, "Compile-time name '%s' not defined" % self.name)
1243
1244     def get_constant_c_result_code(self):
1245         if not self.entry or self.entry.type.is_pyobject:
1246             return None
1247         return self.entry.cname
1248     
1249     def coerce_to(self, dst_type, env):
1250         #  If coercing to a generic pyobject and this is a builtin
1251         #  C function with a Python equivalent, manufacture a NameNode
1252         #  referring to the Python builtin.
1253         #print "NameNode.coerce_to:", self.name, dst_type ###
1254         if dst_type is py_object_type:
1255             entry = self.entry
1256             if entry and entry.is_cfunction:
1257                 var_entry = entry.as_variable
1258                 if var_entry:
1259                     if var_entry.is_builtin and Options.cache_builtins:
1260                         var_entry = env.declare_builtin(var_entry.name, self.pos)
1261                     node = NameNode(self.pos, name = self.name)
1262                     node.entry = var_entry
1263                     node.analyse_rvalue_entry(env)
1264                     return node
1265         return super(NameNode, self).coerce_to(dst_type, env)
1266     
1267     def analyse_as_module(self, env):
1268         # Try to interpret this as a reference to a cimported module.
1269         # Returns the module scope, or None.
1270         entry = self.entry
1271         if not entry:
1272             entry = env.lookup(self.name)
1273         if entry and entry.as_module:
1274             return entry.as_module
1275         return None
1276         
1277     def analyse_as_type(self, env):
1278         if self.cython_attribute:
1279             type = PyrexTypes.parse_basic_type(self.cython_attribute)
1280         else:
1281             type = PyrexTypes.parse_basic_type(self.name)
1282         if type:
1283             return type
1284         entry = self.entry
1285         if not entry:
1286             entry = env.lookup(self.name)
1287         if entry and entry.is_type:
1288             return entry.type
1289         else:
1290             return None
1291     
1292     def analyse_as_extension_type(self, env):
1293         # Try to interpret this as a reference to an extension type.
1294         # Returns the extension type, or None.
1295         entry = self.entry
1296         if not entry:
1297             entry = env.lookup(self.name)
1298         if entry and entry.is_type and entry.type.is_extension_type:
1299             return entry.type
1300         else:
1301             return None
1302     
1303     def analyse_target_declaration(self, env):
1304         if not self.entry:
1305             self.entry = env.lookup_here(self.name)
1306         if not self.entry:
1307             if env.directives['warn.undeclared']:
1308                 warning(self.pos, "implicit declaration of '%s'" % self.name, 1)
1309             if env.directives['infer_types'] != False:
1310                 type = unspecified_type
1311             else:
1312                 type = py_object_type
1313             self.entry = env.declare_var(self.name, type, self.pos)
1314         env.control_flow.set_state(self.pos, (self.name, 'initialized'), True)
1315         env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment')
1316         if self.entry.is_declared_generic:
1317             self.result_ctype = py_object_type
1318     
1319     def analyse_types(self, env):
1320         if self.entry is None:
1321             self.entry = env.lookup(self.name)
1322         if not self.entry:
1323             self.entry = env.declare_builtin(self.name, self.pos)
1324         if not self.entry:
1325             self.type = PyrexTypes.error_type
1326             return
1327         entry = self.entry
1328         if entry:
1329             entry.used = 1
1330             if entry.type.is_buffer:
1331                 import Buffer
1332                 Buffer.used_buffer_aux_vars(entry)
1333             if entry.utility_code:
1334                 env.use_utility_code(entry.utility_code)
1335         self.analyse_rvalue_entry(env)
1336         
1337     def analyse_target_types(self, env):
1338         self.analyse_entry(env)
1339         if not self.is_lvalue():
1340             error(self.pos, "Assignment to non-lvalue '%s'"
1341                 % self.name)
1342             self.type = PyrexTypes.error_type
1343         self.entry.used = 1
1344         if self.entry.type.is_buffer:
1345             import Buffer
1346             Buffer.used_buffer_aux_vars(self.entry)
1347                 
1348     def analyse_rvalue_entry(self, env):
1349         #print "NameNode.analyse_rvalue_entry:", self.name ###
1350         #print "Entry:", self.entry.__dict__ ###
1351         self.analyse_entry(env)
1352         entry = self.entry
1353         if entry.is_declared_generic:
1354             self.result_ctype = py_object_type
1355         if entry.is_pyglobal or entry.is_builtin:
1356             if Options.cache_builtins and entry.is_builtin:
1357                 self.is_temp = 0
1358             else:
1359                 self.is_temp = 1
1360                 env.use_utility_code(get_name_interned_utility_code)
1361             self.is_used_as_rvalue = 1
1362
1363     def nogil_check(self, env):
1364         if self.is_used_as_rvalue:
1365             entry = self.entry
1366             if entry.is_builtin:
1367                 # if not Options.cache_builtins: # cached builtins are ok
1368                 self.gil_error()
1369             elif entry.is_pyglobal:
1370                 self.gil_error()
1371
1372     gil_message = "Accessing Python global or builtin"
1373
1374     def analyse_entry(self, env):
1375         #print "NameNode.analyse_entry:", self.name ###
1376         self.check_identifier_kind()
1377         entry = self.entry
1378         type = entry.type
1379         self.type = type
1380
1381     def check_identifier_kind(self):
1382         # Check that this is an appropriate kind of name for use in an
1383         # expression.  Also finds the variable entry associated with
1384         # an extension type.
1385         entry = self.entry
1386         if entry.is_type and entry.type.is_extension_type:
1387             self.type_entry = entry
1388         if not (entry.is_const or entry.is_variable 
1389             or entry.is_builtin or entry.is_cfunction
1390             or entry.is_cpp_class):
1391                 if self.entry.as_variable:
1392                     self.entry = self.entry.as_variable
1393                 else:
1394                     error(self.pos, 
1395                           "'%s' is not a constant, variable or function identifier" % self.name)
1396
1397     def is_simple(self):
1398         #  If it's not a C variable, it'll be in a temp.
1399         return 1
1400     
1401     def calculate_target_results(self, env):
1402         pass
1403     
1404     def check_const(self):
1405         entry = self.entry
1406         if entry is not None and not (entry.is_const or entry.is_cfunction or entry.is_builtin):
1407             self.not_const()
1408             return False
1409         return True
1410     
1411     def check_const_addr(self):
1412         entry = self.entry
1413         if not (entry.is_cglobal or entry.is_cfunction or entry.is_builtin):
1414             self.addr_not_const()
1415             return False
1416         return True
1417
1418     def is_lvalue(self):
1419         return self.entry.is_variable and \
1420             not self.entry.type.is_array and \
1421             not self.entry.is_readonly
1422     
1423     def is_ephemeral(self):
1424         #  Name nodes are never ephemeral, even if the
1425         #  result is in a temporary.
1426         return 0
1427     
1428     def calculate_result_code(self):
1429         entry = self.entry
1430         if not entry:
1431             return "<error>" # There was an error earlier
1432         return entry.cname
1433     
1434     def generate_result_code(self, code):
1435         assert hasattr(self, 'entry')
1436         entry = self.entry
1437         if entry is None:
1438             return # There was an error earlier
1439         if entry.is_builtin and Options.cache_builtins:
1440             return # Lookup already cached
1441         elif entry.is_pyglobal or entry.is_builtin:
1442             assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1443             interned_cname = code.intern_identifier(self.entry.name)
1444             if entry.is_builtin:
1445                 namespace = Naming.builtins_cname
1446             else: # entry.is_pyglobal
1447                 namespace = entry.scope.namespace_cname
1448             code.globalstate.use_utility_code(get_name_interned_utility_code)
1449             code.putln(
1450                 '%s = __Pyx_GetName(%s, %s); %s' % (
1451                 self.result(),
1452                 namespace, 
1453                 interned_cname,
1454                 code.error_goto_if_null(self.result(), self.pos)))
1455             code.put_gotref(self.py_result())
1456             
1457         elif entry.is_local and False:
1458             # control flow not good enough yet
1459             assigned = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
1460             if assigned is False:
1461                 error(self.pos, "local variable '%s' referenced before assignment" % entry.name)
1462             elif not Options.init_local_none and assigned is None:
1463                 code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' %
1464                            (entry.cname, entry.name, code.error_goto(self.pos)))
1465                 entry.scope.control_flow.set_state(self.pos, (entry.name, 'initialized'), True)
1466
1467     def generate_assignment_code(self, rhs, code):
1468         #print "NameNode.generate_assignment_code:", self.name ###
1469         entry = self.entry
1470         if entry is None:
1471             return # There was an error earlier
1472
1473         if (self.entry.type.is_ptr and isinstance(rhs, ListNode)
1474             and not self.lhs_of_first_assignment):
1475             error(self.pos, "Literal list must be assigned to pointer at time of declaration")
1476         
1477         # is_pyglobal seems to be True for module level-globals only.
1478         # We use this to access class->tp_dict if necessary.
1479         if entry.is_pyglobal:
1480             assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1481             interned_cname = code.intern_identifier(self.entry.name)
1482             namespace = self.entry.scope.namespace_cname
1483             if entry.is_member:
1484                 # if the entry is a member we have to cheat: SetAttr does not work
1485                 # on types, so we create a descriptor which is then added to tp_dict
1486                 code.put_error_if_neg(self.pos,
1487                     'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
1488                         namespace,
1489                         interned_cname,
1490                         rhs.py_result()))
1491                 rhs.generate_disposal_code(code)
1492                 rhs.free_temps(code)
1493                 # in Py2.6+, we need to invalidate the method cache
1494                 code.putln("PyType_Modified(%s);" %
1495                            entry.scope.parent_type.typeptr_cname)
1496             else: 
1497                 code.put_error_if_neg(self.pos,
1498                     'PyObject_SetAttr(%s, %s, %s)' % (
1499                         namespace,
1500                         interned_cname,
1501                         rhs.py_result()))
1502                 if debug_disposal_code:
1503                     print("NameNode.generate_assignment_code:")
1504                     print("...generating disposal code for %s" % rhs)
1505                 rhs.generate_disposal_code(code)
1506                 rhs.free_temps(code)
1507         else:
1508             if self.type.is_buffer:
1509                 # Generate code for doing the buffer release/acquisition.
1510                 # This might raise an exception in which case the assignment (done
1511                 # below) will not happen.
1512                 #
1513                 # The reason this is not in a typetest-like node is because the
1514                 # variables that the acquired buffer info is stored to is allocated
1515                 # per entry and coupled with it.
1516                 self.generate_acquire_buffer(rhs, code)
1517
1518             if self.type.is_pyobject:
1519                 #print "NameNode.generate_assignment_code: to", self.name ###
1520                 #print "...from", rhs ###
1521                 #print "...LHS type", self.type, "ctype", self.ctype() ###
1522                 #print "...RHS type", rhs.type, "ctype", rhs.ctype() ###
1523                 if self.use_managed_ref:
1524                     rhs.make_owned_reference(code)
1525                     if entry.is_cglobal:
1526                         code.put_gotref(self.py_result())
1527                     if not self.lhs_of_first_assignment:
1528                         if entry.is_local and not Options.init_local_none:
1529                             initialized = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
1530                             if initialized is True:
1531                                 code.put_decref(self.result(), self.ctype())
1532                             elif initialized is None:
1533                                 code.put_xdecref(self.result(), self.ctype())
1534                         else:
1535                             code.put_decref(self.result(), self.ctype())
1536                     if entry.is_cglobal:
1537                         code.put_giveref(rhs.py_result())
1538
1539             code.putln('%s = %s;' % (self.result(),
1540                                      rhs.result_as(self.ctype())))
1541             if debug_disposal_code:
1542                 print("NameNode.generate_assignment_code:")
1543                 print("...generating post-assignment code for %s" % rhs)
1544             rhs.generate_post_assignment_code(code)
1545             rhs.free_temps(code)
1546
1547     def generate_acquire_buffer(self, rhs, code):
1548         # rhstmp is only used in case the rhs is a complicated expression leading to
1549         # the object, to avoid repeating the same C expression for every reference
1550         # to the rhs. It does NOT hold a reference.
1551         pretty_rhs = isinstance(rhs, NameNode) or rhs.is_temp
1552         if pretty_rhs:
1553             rhstmp = rhs.result_as(self.ctype())
1554         else:
1555             rhstmp = code.funcstate.allocate_temp(self.entry.type, manage_ref=False)
1556             code.putln('%s = %s;' % (rhstmp, rhs.result_as(self.ctype())))
1557
1558         buffer_aux = self.entry.buffer_aux
1559         bufstruct = buffer_aux.buffer_info_var.cname
1560         import Buffer
1561         Buffer.put_assign_to_buffer(self.result(), rhstmp, buffer_aux, self.entry.type,
1562                                     is_initialized=not self.lhs_of_first_assignment,
1563                                     pos=self.pos, code=code)
1564         
1565         if not pretty_rhs:
1566             code.putln("%s = 0;" % rhstmp)
1567             code.funcstate.release_temp(rhstmp)
1568     
1569     def generate_deletion_code(self, code):
1570         if self.entry is None:
1571             return # There was an error earlier
1572         if not self.entry.is_pyglobal:
1573             error(self.pos, "Deletion of local or C global name not supported")
1574             return
1575         code.put_error_if_neg(self.pos, 
1576             '__Pyx_DelAttrString(%s, "%s")' % (
1577                 Naming.module_cname,
1578                 self.entry.name))
1579                 
1580     def annotate(self, code):
1581         if hasattr(self, 'is_called') and self.is_called:
1582             pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
1583             if self.type.is_pyobject:
1584                 code.annotate(pos, AnnotationItem('py_call', 'python function', size=len(self.name)))
1585             else:
1586                 code.annotate(pos, AnnotationItem('c_call', 'c function', size=len(self.name)))
1587             
1588 class BackquoteNode(ExprNode):
1589     #  `expr`
1590     #
1591     #  arg    ExprNode
1592     
1593     type = py_object_type
1594     
1595     subexprs = ['arg']
1596     
1597     def analyse_types(self, env):
1598         self.arg.analyse_types(env)
1599         self.arg = self.arg.coerce_to_pyobject(env)
1600         self.is_temp = 1
1601
1602     gil_message = "Backquote expression"
1603
1604     def calculate_constant_result(self):
1605         self.constant_result = repr(self.arg.constant_result)
1606
1607     def generate_result_code(self, code):
1608         code.putln(
1609             "%s = PyObject_Repr(%s); %s" % (
1610                 self.result(),
1611                 self.arg.py_result(),
1612                 code.error_goto_if_null(self.result(), self.pos)))
1613         code.put_gotref(self.py_result())
1614         
1615
1616
1617 class ImportNode(ExprNode):
1618     #  Used as part of import statement implementation.
1619     #  Implements result = 
1620     #    __import__(module_name, globals(), None, name_list)
1621     #
1622     #  module_name   StringNode            dotted name of module
1623     #  name_list     ListNode or None      list of names to be imported
1624     
1625     type = py_object_type
1626     
1627     subexprs = ['module_name', 'name_list']
1628     
1629     def analyse_types(self, env):
1630         self.module_name.analyse_types(env)
1631         self.module_name = self.module_name.coerce_to_pyobject(env)
1632         if self.name_list:
1633             self.name_list.analyse_types(env)
1634             self.name_list.coerce_to_pyobject(env)
1635         self.is_temp = 1
1636         env.use_utility_code(import_utility_code)
1637
1638     gil_message = "Python import"
1639
1640     def generate_result_code(self, code):
1641         if self.name_list:
1642             name_list_code = self.name_list.py_result()
1643         else:
1644             name_list_code = "0"
1645         code.putln(
1646             "%s = __Pyx_Import(%s, %s); %s" % (
1647                 self.result(),
1648                 self.module_name.py_result(),
1649                 name_list_code,
1650                 code.error_goto_if_null(self.result(), self.pos)))
1651         code.put_gotref(self.py_result())
1652
1653
1654 class IteratorNode(ExprNode):
1655     #  Used as part of for statement implementation.
1656     #
1657     #  allocate_counter_temp/release_counter_temp needs to be called
1658     #  by parent (ForInStatNode)
1659     #
1660     #  Implements result = iter(sequence)
1661     #
1662     #  sequence   ExprNode
1663     
1664     type = py_object_type
1665     
1666     subexprs = ['sequence']
1667     
1668     def analyse_types(self, env):
1669         self.sequence.analyse_types(env)
1670         if (self.sequence.type.is_array or self.sequence.type.is_ptr) and \
1671                 not self.sequence.type.is_string:
1672             # C array iteration will be transformed later on
1673             self.type = self.sequence.type
1674         else:
1675             self.sequence = self.sequence.coerce_to_pyobject(env)
1676         self.is_temp = 1
1677
1678     gil_message = "Iterating over Python object"
1679
1680     def allocate_counter_temp(self, code):
1681         self.counter_cname = code.funcstate.allocate_temp(
1682             PyrexTypes.c_py_ssize_t_type, manage_ref=False)
1683
1684     def release_counter_temp(self, code):
1685         code.funcstate.release_temp(self.counter_cname)
1686
1687     def generate_result_code(self, code):
1688         if self.sequence.type.is_array or self.sequence.type.is_ptr:
1689             raise InternalError("for in carray slice not transformed")
1690         is_builtin_sequence = self.sequence.type is list_type or \
1691                               self.sequence.type is tuple_type
1692         may_be_a_sequence = is_builtin_sequence or not self.sequence.type.is_builtin_type
1693         if is_builtin_sequence:
1694             code.putln(
1695                 "if (likely(%s != Py_None)) {" % self.sequence.py_result())
1696         elif may_be_a_sequence:
1697             code.putln(
1698                 "if (PyList_CheckExact(%s) || PyTuple_CheckExact(%s)) {" % (
1699                     self.sequence.py_result(),
1700                     self.sequence.py_result()))
1701         if may_be_a_sequence:
1702             code.putln(
1703                 "%s = 0; %s = %s; __Pyx_INCREF(%s);" % (
1704                     self.counter_cname,
1705                     self.result(),
1706                     self.sequence.py_result(),
1707                     self.result()))
1708             code.putln("} else {")
1709         if is_builtin_sequence:
1710             code.putln(
1711                 'PyErr_SetString(PyExc_TypeError, "\'NoneType\' object is not iterable"); %s' %
1712                 code.error_goto(self.pos))
1713         else:
1714             code.putln("%s = -1; %s = PyObject_GetIter(%s); %s" % (
1715                     self.counter_cname,
1716                     self.result(),
1717                     self.sequence.py_result(),
1718                     code.error_goto_if_null(self.result(), self.pos)))
1719             code.put_gotref(self.py_result())
1720         if may_be_a_sequence:
1721             code.putln("}")
1722
1723
1724 class NextNode(AtomicExprNode):
1725     #  Used as part of for statement implementation.
1726     #  Implements result = iterator.next()
1727     #  Created during analyse_types phase.
1728     #  The iterator is not owned by this node.
1729     #
1730     #  iterator   ExprNode
1731     
1732     type = py_object_type
1733     
1734     def __init__(self, iterator, env):
1735         self.pos = iterator.pos
1736         self.iterator = iterator
1737         if iterator.type.is_ptr or iterator.type.is_array:
1738             self.type = iterator.type.base_type
1739         self.is_temp = 1
1740     
1741     def generate_result_code(self, code):
1742         sequence_type = self.iterator.sequence.type
1743         if sequence_type is list_type:
1744             type_checks = [(list_type, "List")]
1745         elif sequence_type is tuple_type:
1746             type_checks = [(tuple_type, "Tuple")]
1747         elif not sequence_type.is_builtin_type:
1748             type_checks = [(list_type, "List"), (tuple_type, "Tuple")]
1749         else:
1750             type_checks = []
1751
1752         for py_type, prefix in type_checks:
1753             if len(type_checks) > 1:
1754                 code.putln(
1755                     "if (likely(Py%s_CheckExact(%s))) {" % (
1756                         prefix, self.iterator.py_result()))
1757             code.putln(
1758                 "if (%s >= Py%s_GET_SIZE(%s)) break;" % (
1759                     self.iterator.counter_cname,
1760                     prefix,
1761                     self.iterator.py_result()))
1762             code.putln(
1763                 "%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s++;" % (
1764                     self.result(),
1765                     prefix,
1766                     self.iterator.py_result(),
1767                     self.iterator.counter_cname,
1768                     self.result(),
1769                     self.iterator.counter_cname))
1770             if len(type_checks) > 1:
1771                 code.put("} else ")
1772         if len(type_checks) == 1:
1773             return
1774         code.putln("{")
1775         code.putln(
1776             "%s = PyIter_Next(%s);" % (
1777                 self.result(),
1778                 self.iterator.py_result()))
1779         code.putln(
1780             "if (!%s) {" %
1781                 self.result())
1782         code.putln(code.error_goto_if_PyErr(self.pos))
1783         code.putln("break;")
1784         code.putln("}")
1785         code.put_gotref(self.py_result())
1786         code.putln("}")
1787
1788
1789 class ExcValueNode(AtomicExprNode):
1790     #  Node created during analyse_types phase
1791     #  of an ExceptClauseNode to fetch the current
1792     #  exception value.
1793     
1794     type = py_object_type
1795     
1796     def __init__(self, pos, env):
1797         ExprNode.__init__(self, pos)
1798
1799     def set_var(self, var):
1800         self.var = var
1801     
1802     def calculate_result_code(self):
1803         return self.var
1804
1805     def generate_result_code(self, code):
1806         pass
1807
1808     def analyse_types(self, env):
1809         pass
1810
1811
1812 class TempNode(ExprNode):
1813     # Node created during analyse_types phase
1814     # of some nodes to hold a temporary value.
1815     #
1816     # Note: One must call "allocate" and "release" on
1817     # the node during code generation to get/release the temp.
1818     # This is because the temp result is often used outside of
1819     # the regular cycle.
1820
1821     subexprs = []
1822     
1823     def __init__(self, pos, type, env):
1824         ExprNode.__init__(self, pos)
1825         self.type = type
1826         if type.is_pyobject:
1827             self.result_ctype = py_object_type
1828         self.is_temp = 1
1829         
1830     def analyse_types(self, env):
1831         return self.type
1832     
1833     def generate_result_code(self, code):
1834         pass
1835
1836     def allocate(self, code):
1837         self.temp_cname = code.funcstate.allocate_temp(self.type, manage_ref=True)
1838
1839     def release(self, code):
1840         code.funcstate.release_temp(self.temp_cname)
1841         self.temp_cname = None
1842
1843     def result(self):
1844         try:
1845             return self.temp_cname
1846         except:
1847             assert False, "Remember to call allocate/release on TempNode"
1848             raise
1849
1850     # Do not participate in normal temp alloc/dealloc:
1851     def allocate_temp_result(self, code):
1852         pass
1853     
1854     def release_temp_result(self, code):
1855         pass
1856
1857 class PyTempNode(TempNode):
1858     #  TempNode holding a Python value.
1859     
1860     def __init__(self, pos, env):
1861         TempNode.__init__(self, pos, PyrexTypes.py_object_type, env)
1862
1863 class RawCNameExprNode(ExprNode):
1864     subexprs = []
1865     
1866     def __init__(self, pos, type=None):
1867         self.pos = pos
1868         self.type = type
1869
1870     def analyse_types(self, env):
1871         return self.type
1872
1873     def set_cname(self, cname):
1874         self.cname = cname
1875
1876     def result(self):
1877         return self.cname
1878
1879     def generate_result_code(self, code):
1880         pass
1881
1882
1883 #-------------------------------------------------------------------
1884 #
1885 #  Trailer nodes
1886 #
1887 #-------------------------------------------------------------------
1888
1889 class IndexNode(ExprNode):
1890     #  Sequence indexing.
1891     #
1892     #  base     ExprNode
1893     #  index    ExprNode
1894     #  indices  [ExprNode]
1895     #  is_buffer_access boolean Whether this is a buffer access.
1896     #
1897     #  indices is used on buffer access, index on non-buffer access.
1898     #  The former contains a clean list of index parameters, the
1899     #  latter whatever Python object is needed for index access.
1900     
1901     subexprs = ['base', 'index', 'indices']
1902     indices = None
1903
1904     def __init__(self, pos, index, *args, **kw):
1905         ExprNode.__init__(self, pos, index=index, *args, **kw)
1906         self._index = index
1907
1908     def calculate_constant_result(self):
1909         self.constant_result = \
1910             self.base.constant_result[self.index.constant_result]
1911
1912     def compile_time_value(self, denv):
1913         base = self.base.compile_time_value(denv)
1914         index = self.index.compile_time_value(denv)
1915         try:
1916             return base[index]
1917         except Exception, e:
1918             self.compile_time_value_error(e)
1919     
1920     def is_ephemeral(self):
1921         return self.base.is_ephemeral()
1922     
1923     def analyse_target_declaration(self, env):
1924         pass
1925         
1926     def analyse_as_type(self, env):
1927         base_type = self.base.analyse_as_type(env)
1928         if base_type and not base_type.is_pyobject:
1929             if base_type.is_cpp_class:
1930                 if isinstance(self.index, TupleNode):
1931                     template_values = self.index.args
1932                 else:
1933                     template_values = [self.index]
1934                 import Nodes
1935                 type_node = Nodes.TemplatedTypeNode(
1936                     pos = self.pos, 
1937                     positional_args = template_values, 
1938                     keyword_args = None)
1939                 return type_node.analyse(env, base_type = base_type)
1940             else:
1941                 return PyrexTypes.CArrayType(base_type, int(self.index.compile_time_value(env)))
1942         return None
1943     
1944     def type_dependencies(self, env):
1945         return self.base.type_dependencies(env)
1946     
1947     def infer_type(self, env):
1948         base_type = self.base.infer_type(env)
1949         if isinstance(self.index, SliceNode):
1950             # slicing!
1951             if base_type.is_string:
1952                 # sliced C strings must coerce to Python 
1953                 return bytes_type
1954             elif base_type in (unicode_type, bytes_type, str_type, list_type, tuple_type):
1955                 # slicing these returns the same type
1956                 return base_type
1957             else:
1958                 # TODO: Handle buffers (hopefully without too much redundancy).
1959                 return py_object_type
1960
1961         index_type = self.index.infer_type(env)
1962         if index_type and index_type.is_int or isinstance(self.index, (IntNode, LongNode)):
1963             # indexing!
1964             if base_type is unicode_type:
1965                 # Py_UNICODE will automatically coerce to a unicode string
1966                 # if required, so this is safe. We only infer Py_UNICODE
1967                 # when the index is a C integer type. Otherwise, we may
1968                 # need to use normal Python item access, in which case
1969                 # it's faster to return the one-char unicode string than
1970                 # to receive it, throw it away, and potentially rebuild it
1971                 # on a subsequent PyObject coercion.
1972                 return PyrexTypes.c_py_unicode_type
1973             elif isinstance(self.base, BytesNode):
1974                 #if env.global_scope().context.language_level >= 3:
1975                 #    # infering 'char' can be made to work in Python 3 mode
1976                 #    return PyrexTypes.c_char_type
1977                 # Py2/3 return different types on indexing bytes objects
1978                 return py_object_type
1979             elif base_type.is_ptr or base_type.is_array:
1980                 return base_type.base_type
1981
1982         # may be slicing or indexing, we don't know
1983         if base_type is unicode_type:
1984             # this type always returns its own type on Python indexing/slicing
1985             return base_type
1986         else:
1987             # TODO: Handle buffers (hopefully without too much redundancy).
1988             return py_object_type
1989     
1990     def analyse_types(self, env):
1991         self.analyse_base_and_index_types(env, getting = 1)
1992     
1993     def analyse_target_types(self, env):
1994         self.analyse_base_and_index_types(env, setting = 1)
1995
1996     def analyse_base_and_index_types(self, env, getting = 0, setting = 0):
1997         # Note: This might be cleaned up by having IndexNode
1998         # parsed in a saner way and only construct the tuple if
1999         # needed.
2000
2001         # Note that this function must leave IndexNode in a cloneable state.
2002         # For buffers, self.index is packed out on the initial analysis, and
2003         # when cloning self.indices is copied.
2004         self.is_buffer_access = False
2005
2006         self.base.analyse_types(env)
2007         if self.base.type.is_error:
2008             # Do not visit child tree if base is undeclared to avoid confusing
2009             # error messages
2010             self.type = PyrexTypes.error_type
2011             return
2012         
2013         is_slice = isinstance(self.index, SliceNode)
2014         # Potentially overflowing index value.
2015         if not is_slice and isinstance(self.index, IntNode) and Utils.long_literal(self.index.value):
2016             self.index = self.index.coerce_to_pyobject(env)
2017
2018         # Handle the case where base is a literal char* (and we expect a string, not an int)
2019         if isinstance(self.base, BytesNode) or is_slice:
2020             if self.base.type.is_string or not (self.base.type.is_ptr or self.base.type.is_array):
2021                 self.base = self.base.coerce_to_pyobject(env)
2022
2023         skip_child_analysis = False
2024         buffer_access = False
2025         if self.base.type.is_buffer:
2026             assert hasattr(self.base, "entry") # Must be a NameNode-like node
2027             if self.indices:
2028                 indices = self.indices
2029             else:
2030                 if isinstance(self.index, TupleNode):
2031                     indices = self.index.args
2032                 else:
2033                     indices = [self.index]
2034             if len(indices) == self.base.type.ndim:
2035                 buffer_access = True
2036                 skip_child_analysis = True
2037                 for x in indices:
2038                     x.analyse_types(env)
2039                     if not x.type.is_int:
2040                         buffer_access = False
2041
2042         # On cloning, indices is cloned. Otherwise, unpack index into indices
2043         assert not (buffer_access and isinstance(self.index, CloneNode))
2044
2045         if buffer_access:
2046             self.indices = indices
2047             self.index = None
2048             self.type = self.base.type.dtype
2049             self.is_buffer_access = True
2050             self.buffer_type = self.base.entry.type
2051
2052             if getting and self.type.is_pyobject:
2053                 self.is_temp = True
2054             if setting:
2055                 if not self.base.entry.type.writable:
2056                     error(self.pos, "Writing to readonly buffer")
2057                 else:
2058                     self.base.entry.buffer_aux.writable_needed = True
2059         else:
2060             base_type = self.base.type
2061             if isinstance(self.index, TupleNode):
2062                 self.index.analyse_types(env, skip_children=skip_child_analysis)
2063             elif not skip_child_analysis:
2064                 self.index.analyse_types(env)
2065             self.original_index_type = self.index.type
2066             if base_type is PyrexTypes.c_py_unicode_type:
2067                 # we infer Py_UNICODE for unicode strings in some
2068                 # cases, but indexing must still work for them
2069                 if self.index.constant_result in (0, -1):
2070                     # FIXME: we know that this node is redundant -
2071                     # currently, this needs to get handled in Optimize.py
2072                     pass
2073                 self.base = self.base.coerce_to_pyobject(env)
2074                 base_type = self.base.type
2075             if base_type.is_pyobject:
2076                 if self.index.type.is_int:
2077                     if (not setting
2078                         and (base_type in (list_type, tuple_type, unicode_type))
2079                         and (not self.index.type.signed or isinstance(self.index, IntNode) and int(self.index.value) >= 0)
2080                         and not env.directives['boundscheck']):
2081                         self.is_temp = 0
2082                     else:
2083                         self.is_temp = 1
2084                     self.index = self.index.coerce_to(PyrexTypes.c_py_ssize_t_type, env).coerce_to_simple(env)
2085                 else:
2086                     self.index = self.index.coerce_to_pyobject(env)
2087                     self.is_temp = 1
2088                 if self.index.type.is_int and base_type is unicode_type:
2089                     # Py_UNICODE will automatically coerce to a unicode string
2090                     # if required, so this is fast and safe
2091                     self.type = PyrexTypes.c_py_unicode_type
2092                 elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type):
2093                     self.type = base_type
2094                 else:
2095                     self.type = py_object_type
2096             else:
2097                 if base_type.is_ptr or base_type.is_array:
2098                     self.type = base_type.base_type
2099                     if is_slice:
2100                         self.type = base_type
2101                     elif self.index.type.is_pyobject:
2102                         self.index = self.index.coerce_to(
2103                             PyrexTypes.c_py_ssize_t_type, env)
2104                     elif not self.index.type.is_int:
2105                         error(self.pos,
2106                             "Invalid index type '%s'" %
2107                                 self.index.type)
2108                 elif base_type.is_cpp_class:
2109                     function = env.lookup_operator("[]", [self.base, self.index])
2110                     if function is None:
2111                         error(self.pos, "Indexing '%s' not supported for index type '%s'" % (base_type, self.index.type))
2112                         self.type = PyrexTypes.error_type
2113                         self.result_code = "<error>"
2114                         return
2115                     func_type = function.type
2116                     if func_type.is_ptr:
2117                         func_type = func_type.base_type
2118                     self.index = self.index.coerce_to(func_type.args[0].type, env)
2119                     self.type = func_type.return_type
2120                     if setting and not func_type.return_type.is_reference:
2121                         error(self.pos, "Can't set non-reference result '%s'" % self.type)
2122                 else:
2123                     error(self.pos,
2124                         "Attempting to index non-array type '%s'" %
2125                             base_type)
2126                     self.type = PyrexTypes.error_type
2127
2128     gil_message = "Indexing Python object"
2129
2130     def nogil_check(self, env):
2131         if self.is_buffer_access:
2132             if env.directives['boundscheck']:
2133                 error(self.pos, "Cannot check buffer index bounds without gil; use boundscheck(False) directive")
2134                 return
2135             elif self.type.is_pyobject:
2136                 error(self.pos, "Cannot access buffer with object dtype without gil")
2137                 return
2138         super(IndexNode, self).nogil_check(env)
2139
2140
2141     def check_const_addr(self):
2142         return self.base.check_const_addr() and self.index.check_const()
2143     
2144     def is_lvalue(self):
2145         return 1
2146
2147     def calculate_result_code(self):
2148         if self.is_buffer_access:
2149             return "(*%s)" % self.buffer_ptr_code
2150         elif self.base.type is list_type:
2151             return "PyList_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
2152         elif self.base.type is tuple_type:
2153             return "PyTuple_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
2154         elif self.base.type is unicode_type and self.type is PyrexTypes.c_py_unicode_type:
2155             return "PyUnicode_AS_UNICODE(%s)[%s]" % (self.base.result(), self.index.result())
2156         elif (self.type.is_ptr or self.type.is_array) and self.type == self.base.type:
2157             error(self.pos, "Invalid use of pointer slice")
2158         else:
2159             return "(%s[%s])" % (
2160                 self.base.result(), self.index.result())
2161             
2162     def extra_index_params(self):
2163         if self.index.type.is_int:
2164             if self.original_index_type.signed:
2165                 size_adjustment = ""
2166             else:
2167                 size_adjustment = "+1"
2168             return ", sizeof(%s)%s, %s" % (self.original_index_type.declaration_code(""), size_adjustment, self.original_index_type.to_py_function)
2169         else:
2170             return ""
2171
2172     def generate_subexpr_evaluation_code(self, code):
2173         self.base.generate_evaluation_code(code)
2174         if not self.indices:
2175             self.index.generate_evaluation_code(code)
2176         else:
2177             for i in self.indices:
2178                 i.generate_evaluation_code(code)
2179         
2180     def generate_subexpr_disposal_code(self, code):
2181         self.base.generate_disposal_code(code)
2182         if not self.indices:
2183             self.index.generate_disposal_code(code)
2184         else:
2185             for i in self.indices:
2186                 i.generate_disposal_code(code)
2187
2188     def free_subexpr_temps(self, code):
2189         self.base.free_temps(code)
2190         if not self.indices:
2191             self.index.free_temps(code)
2192         else:
2193             for i in self.indices:
2194                 i.free_temps(code)
2195
2196     def generate_result_code(self, code):
2197         if self.is_buffer_access:
2198             if code.globalstate.directives['nonecheck']:
2199                 self.put_nonecheck(code)
2200             self.buffer_ptr_code = self.buffer_lookup_code(code)
2201             if self.type.is_pyobject:
2202                 # is_temp is True, so must pull out value and incref it.
2203                 code.putln("%s = *%s;" % (self.result(), self.buffer_ptr_code))
2204                 code.putln("__Pyx_INCREF((PyObject*)%s);" % self.result())
2205         elif self.is_temp:
2206             if self.type.is_pyobject:
2207                 if self.index.type.is_int:
2208                     index_code = self.index.result()
2209                     if self.base.type is list_type:
2210                         function = "__Pyx_GetItemInt_List"
2211                     elif self.base.type is tuple_type:
2212                         function = "__Pyx_GetItemInt_Tuple"
2213                     else:
2214                         function = "__Pyx_GetItemInt"
2215                     code.globalstate.use_utility_code(getitem_int_utility_code)
2216                 else:
2217                     index_code = self.index.py_result()
2218                     if self.base.type is dict_type:
2219                         function = "__Pyx_PyDict_GetItem"
2220                         code.globalstate.use_utility_code(getitem_dict_utility_code)
2221                     else:
2222                         function = "PyObject_GetItem"
2223                 code.putln(
2224                     "%s = %s(%s, %s%s); if (!%s) %s" % (
2225                         self.result(),
2226                         function,
2227                         self.base.py_result(),
2228                         index_code,
2229                         self.extra_index_params(),
2230                         self.result(),
2231                         code.error_goto(self.pos)))
2232                 code.put_gotref(self.py_result())
2233             elif self.type is PyrexTypes.c_py_unicode_type and self.base.type is unicode_type:
2234                 assert self.index.type.is_int
2235                 index_code = self.index.result()
2236                 function = "__Pyx_GetItemInt_Unicode"
2237                 code.globalstate.use_utility_code(getitem_int_pyunicode_utility_code)
2238                 code.putln(
2239                     "%s = %s(%s, %s%s); if (unlikely(%s == (Py_UNICODE)-1)) %s;" % (
2240                         self.result(),
2241                         function,
2242                         self.base.py_result(),
2243                         index_code,
2244                         self.extra_index_params(),
2245                         self.result(),
2246                         code.error_goto(self.pos)))
2247             
2248     def generate_setitem_code(self, value_code, code):
2249         if self.index.type.is_int:
2250             function = "__Pyx_SetItemInt"
2251             index_code = self.index.result()
2252             code.globalstate.use_utility_code(setitem_int_utility_code)
2253         else:
2254             index_code = self.index.py_result()
2255             if self.base.type is dict_type:
2256                 function = "PyDict_SetItem"
2257             # It would seem that we could specialized lists/tuples, but that
2258             # shouldn't happen here. 
2259             # Both PyList_SetItem PyTuple_SetItem and a Py_ssize_t as input, 
2260             # not a PyObject*, and bad conversion here would give the wrong 
2261             # exception. Also, tuples are supposed to be immutable, and raise 
2262             # TypeErrors when trying to set their entries (PyTuple_SetItem 
2263             # is for creating new tuples from). 
2264             else:
2265                 function = "PyObject_SetItem"
2266         code.putln(
2267             "if (%s(%s, %s, %s%s) < 0) %s" % (
2268                 function,
2269                 self.base.py_result(),
2270                 index_code,
2271                 value_code,
2272                 self.extra_index_params(),
2273                 code.error_goto(self.pos)))
2274
2275     def generate_buffer_setitem_code(self, rhs, code, op=""):
2276         # Used from generate_assignment_code and InPlaceAssignmentNode
2277         if code.globalstate.directives['nonecheck']:
2278             self.put_nonecheck(code)
2279         ptrexpr = self.buffer_lookup_code(code)
2280         if self.buffer_type.dtype.is_pyobject:
2281             # Must manage refcounts. Decref what is already there
2282             # and incref what we put in.
2283             ptr = code.funcstate.allocate_temp(self.buffer_type.buffer_ptr_type, manage_ref=False)
2284             rhs_code = rhs.result()
2285             code.putln("%s = %s;" % (ptr, ptrexpr))
2286             code.put_gotref("*%s" % ptr)
2287             code.putln("__Pyx_DECREF(*%s); __Pyx_INCREF(%s);" % (
2288                 ptr, rhs_code
2289                 ))
2290             code.putln("*%s %s= %s;" % (ptr, op, rhs_code))
2291             code.put_giveref("*%s" % ptr)
2292             code.funcstate.release_temp(ptr)
2293         else: 
2294             # Simple case
2295             code.putln("*%s %s= %s;" % (ptrexpr, op, rhs.result()))
2296
2297     def generate_assignment_code(self, rhs, code):
2298         self.generate_subexpr_evaluation_code(code)
2299         if self.is_buffer_access:
2300             self.generate_buffer_setitem_code(rhs, code)
2301         elif self.type.is_pyobject:
2302             self.generate_setitem_code(rhs.py_result(), code)
2303         else:
2304             code.putln(
2305                 "%s = %s;" % (
2306                     self.result(), rhs.result()))
2307         self.generate_subexpr_disposal_code(code)
2308         self.free_subexpr_temps(code)
2309         rhs.generate_disposal_code(code)
2310         rhs.free_temps(code)
2311     
2312     def generate_deletion_code(self, code):
2313         self.generate_subexpr_evaluation_code(code)
2314         #if self.type.is_pyobject:
2315         if self.index.type.is_int:
2316             function = "__Pyx_DelItemInt"
2317             index_code = self.index.result()
2318             code.globalstate.use_utility_code(delitem_int_utility_code)
2319         else:
2320             index_code = self.index.py_result()
2321             if self.base.type is dict_type:
2322                 function = "PyDict_DelItem"
2323             else:
2324                 function = "PyObject_DelItem"
2325         code.putln(
2326             "if (%s(%s, %s%s) < 0) %s" % (
2327                 function,
2328                 self.base.py_result(),
2329                 index_code,
2330                 self.extra_index_params(),
2331                 code.error_goto(self.pos)))
2332         self.generate_subexpr_disposal_code(code)
2333         self.free_subexpr_temps(code)
2334
2335     def buffer_lookup_code(self, code):
2336         # Assign indices to temps
2337         index_temps = [code.funcstate.allocate_temp(i.type, manage_ref=False) for i in self.indices]
2338         for temp, index in zip(index_temps, self.indices):
2339             code.putln("%s = %s;" % (temp, index.result()))
2340         # Generate buffer access code using these temps
2341         import Buffer
2342         # The above could happen because child_attrs is wrong somewhere so that
2343         # options are not propagated.
2344         return Buffer.put_buffer_lookup_code(entry=self.base.entry,
2345                                              index_signeds=[i.type.signed for i in self.indices],
2346                                              index_cnames=index_temps,
2347                                              directives=code.globalstate.directives,
2348                                              pos=self.pos, code=code)
2349
2350     def put_nonecheck(self, code):
2351         code.globalstate.use_utility_code(raise_noneindex_error_utility_code)
2352         code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.base.result_as(PyrexTypes.py_object_type))
2353         code.putln("__Pyx_RaiseNoneIndexingError();")
2354         code.putln(code.error_goto(self.pos))
2355         code.putln("}")
2356
2357 class SliceIndexNode(ExprNode):
2358     #  2-element slice indexing
2359     #
2360     #  base      ExprNode
2361     #  start     ExprNode or None
2362     #  stop      ExprNode or None
2363     
2364     subexprs = ['base', 'start', 'stop']
2365
2366     def infer_type(self, env):
2367         base_type = self.base.infer_type(env)
2368         if base_type.is_string:
2369             return bytes_type
2370         elif base_type in (bytes_type, str_type, unicode_type,
2371                            list_type, tuple_type):
2372             return base_type
2373         return py_object_type
2374
2375     def calculate_constant_result(self):
2376         self.constant_result = self.base.constant_result[
2377             self.start.constant_result : self.stop.constant_result]
2378
2379     def compile_time_value(self, denv):
2380         base = self.base.compile_time_value(denv)
2381         if self.start is None:
2382             start = 0
2383         else:
2384             start = self.start.compile_time_value(denv)
2385         if self.stop is None:
2386             stop = None
2387         else:
2388             stop = self.stop.compile_time_value(denv)
2389         try:
2390             return base[start:stop]
2391         except Exception, e:
2392             self.compile_time_value_error(e)
2393     
2394     def analyse_target_declaration(self, env):
2395         pass
2396     
2397     def analyse_target_types(self, env):
2398         self.analyse_types(env)
2399         # when assigning, we must accept any Python type
2400         if self.type.is_pyobject:
2401             self.type = py_object_type
2402
2403     def analyse_types(self, env):
2404         self.base.analyse_types(env)
2405         if self.start:
2406             self.start.analyse_types(env)
2407         if self.stop:
2408             self.stop.analyse_types(env)
2409         base_type = self.base.type
2410         if base_type.is_string:
2411             self.type = bytes_type
2412         elif base_type.is_ptr:
2413             self.type = base_type
2414         elif base_type.is_array:
2415             # we need a ptr type here instead of an array type, as
2416             # array types can result in invalid type casts in the C
2417             # code
2418             self.type = PyrexTypes.CPtrType(base_type.base_type)
2419         else:
2420             self.base = self.base.coerce_to_pyobject(env)
2421             self.type = py_object_type
2422         if base_type.is_builtin_type:
2423             # slicing builtin types returns something of the same type
2424             self.type = base_type
2425         c_int = PyrexTypes.c_py_ssize_t_type
2426         if self.start:
2427             self.start = self.start.coerce_to(c_int, env)
2428         if self.stop:
2429             self.stop = self.stop.coerce_to(c_int, env)
2430         self.is_temp = 1
2431
2432     nogil_check = Node.gil_error
2433     gil_message = "Slicing Python object"
2434
2435     def generate_result_code(self, code):
2436         if not self.type.is_pyobject:
2437             error(self.pos,
2438                   "Slicing is not currently supported for '%s'." % self.type)
2439             return
2440         if self.base.type.is_string:
2441             if self.stop is None:
2442                 code.putln(
2443                     "%s = PyBytes_FromString(%s + %s); %s" % (
2444                         self.result(),
2445                         self.base.result(),
2446                         self.start_code(),
2447                         code.error_goto_if_null(self.result(), self.pos)))
2448             else:
2449                 code.putln(
2450                     "%s = PyBytes_FromStringAndSize(%s + %s, %s - %s); %s" % (
2451                         self.result(),
2452                         self.base.result(),
2453                         self.start_code(),
2454                         self.stop_code(),
2455                         self.start_code(),
2456                         code.error_goto_if_null(self.result(), self.pos)))
2457         else:
2458             code.putln(
2459                 "%s = PySequence_GetSlice(%s, %s, %s); %s" % (
2460                     self.result(),
2461                     self.base.py_result(),
2462                     self.start_code(),
2463                     self.stop_code(),
2464                     code.error_goto_if_null(self.result(), self.pos)))
2465         code.put_gotref(self.py_result())
2466     
2467     def generate_assignment_code(self, rhs, code):
2468         self.generate_subexpr_evaluation_code(code)
2469         if self.type.is_pyobject:
2470             code.put_error_if_neg(self.pos, 
2471                 "PySequence_SetSlice(%s, %s, %s, %s)" % (
2472                     self.base.py_result(),
2473                     self.start_code(),
2474                     self.stop_code(),
2475                     rhs.py_result()))
2476         else:
2477             start_offset = ''
2478             if self.start:
2479                 start_offset = self.start_code()
2480                 if start_offset == '0':
2481                     start_offset = ''
2482                 else:
2483                     start_offset += '+'
2484             if rhs.type.is_array:
2485                 array_length = rhs.type.size
2486                 self.generate_slice_guard_code(code, array_length)
2487             else:
2488                 error(self.pos,
2489                       "Slice assignments from pointers are not yet supported.")
2490                 # FIXME: fix the array size according to start/stop
2491                 array_length = self.base.type.size
2492             for i in range(array_length):
2493                 code.putln("%s[%s%s] = %s[%d];" % (
2494                         self.base.result(), start_offset, i,
2495                         rhs.result(), i))
2496         self.generate_subexpr_disposal_code(code)
2497         self.free_subexpr_temps(code)
2498         rhs.generate_disposal_code(code)
2499         rhs.free_temps(code)
2500
2501     def generate_deletion_code(self, code):
2502         if not self.base.type.is_pyobject:
2503             error(self.pos,
2504                   "Deleting slices is only supported for Python types, not '%s'." % self.type)
2505             return
2506         self.generate_subexpr_evaluation_code(code)
2507         code.put_error_if_neg(self.pos,
2508             "PySequence_DelSlice(%s, %s, %s)" % (
2509                 self.base.py_result(),
2510                 self.start_code(),
2511                 self.stop_code()))
2512         self.generate_subexpr_disposal_code(code)
2513
2514     def generate_slice_guard_code(self, code, target_size):
2515         if not self.base.type.is_array:
2516             return
2517         slice_size = self.base.type.size
2518         start = stop = None
2519         if self.stop:
2520             stop = self.stop.result()
2521             try:
2522                 stop = int(stop)
2523                 if stop < 0:
2524                     slice_size = self.base.type.size + stop
2525                 else:
2526                     slice_size = stop
2527                 stop = None
2528             except ValueError:
2529                 pass
2530         if self.start:
2531             start = self.start.result()
2532             try:
2533                 start = int(start)
2534                 if start < 0:
2535                     start = self.base.type.size + start
2536                 slice_size -= start
2537                 start = None
2538             except ValueError:
2539                 pass
2540         check = None
2541         if slice_size < 0:
2542             if target_size > 0:
2543                 error(self.pos, "Assignment to empty slice.")
2544         elif start is None and stop is None:
2545             # we know the exact slice length
2546             if target_size != slice_size:
2547                 error(self.pos, "Assignment to slice of wrong length, expected %d, got %d" % (
2548                         slice_size, target_size))
2549         elif start is not None:
2550             if stop is None:
2551                 stop = slice_size
2552             check = "(%s)-(%s)" % (stop, start)
2553         else: # stop is not None:
2554             check = stop
2555         if check:
2556             code.putln("if (unlikely((%s) != %d)) {" % (check, target_size))
2557             code.putln('PyErr_Format(PyExc_ValueError, "Assignment to slice of wrong length, expected %%"PY_FORMAT_SIZE_T"d, got %%"PY_FORMAT_SIZE_T"d", (Py_ssize_t)%d, (Py_ssize_t)(%s));' % (
2558                         target_size, check))
2559             code.putln(code.error_goto(self.pos))
2560             code.putln("}")
2561     
2562     def start_code(self):
2563         if self.start:
2564             return self.start.result()
2565         else:
2566             return "0"
2567     
2568     def stop_code(self):
2569         if self.stop:
2570             return self.stop.result()
2571         elif self.base.type.is_array:
2572             return self.base.type.size
2573         else:
2574             return "PY_SSIZE_T_MAX"
2575     
2576     def calculate_result_code(self):
2577         # self.result() is not used, but this method must exist
2578         return "<unused>"
2579     
2580
2581 class SliceNode(ExprNode):
2582     #  start:stop:step in subscript list
2583     #
2584     #  start     ExprNode
2585     #  stop      ExprNode
2586     #  step      ExprNode
2587     
2588     type = py_object_type
2589     is_temp = 1
2590
2591     def calculate_constant_result(self):
2592         self.constant_result = self.base.constant_result[
2593             self.start.constant_result : \
2594                 self.stop.constant_result : \
2595                 self.step.constant_result]
2596
2597     def compile_time_value(self, denv):
2598         start = self.start.compile_time_value(denv)
2599         if self.stop is None:
2600             stop = None
2601         else:
2602             stop = self.stop.compile_time_value(denv)
2603         if self.step is None:
2604             step = None
2605         else:
2606             step = self.step.compile_time_value(denv)
2607         try:
2608             return slice(start, stop, step)
2609         except Exception, e:
2610             self.compile_time_value_error(e)
2611
2612     subexprs = ['start', 'stop', 'step']
2613     
2614     def analyse_types(self, env):
2615         self.start.analyse_types(env)
2616         self.stop.analyse_types(env)
2617         self.step.analyse_types(env)
2618         self.start = self.start.coerce_to_pyobject(env)
2619         self.stop = self.stop.coerce_to_pyobject(env)
2620         self.step = self.step.coerce_to_pyobject(env)
2621
2622     gil_message = "Constructing Python slice object"
2623
2624     def generate_result_code(self, code):
2625         code.putln(
2626             "%s = PySlice_New(%s, %s, %s); %s" % (
2627                 self.result(),
2628                 self.start.py_result(), 
2629                 self.stop.py_result(), 
2630                 self.step.py_result(),
2631                 code.error_goto_if_null(self.result(), self.pos)))
2632         code.put_gotref(self.py_result())
2633
2634
2635 class CallNode(ExprNode):
2636
2637     # allow overriding the default 'may_be_none' behaviour
2638     may_return_none = None
2639
2640     def may_be_none(self):
2641         if self.may_return_none is not None:
2642             return self.may_return_none
2643         return ExprNode.may_be_none(self)
2644
2645     def analyse_as_type_constructor(self, env):
2646         type = self.function.analyse_as_type(env)
2647         if type and type.is_struct_or_union:
2648             args, kwds = self.explicit_args_kwds()
2649             items = []
2650             for arg, member in zip(args, type.scope.var_entries):
2651                 items.append(DictItemNode(pos=arg.pos, key=StringNode(pos=arg.pos, value=member.name), value=arg))
2652             if kwds:
2653                 items += kwds.key_value_pairs
2654             self.key_value_pairs = items
2655             self.__class__ = DictNode
2656             self.analyse_types(env)
2657             self.coerce_to(type, env)
2658             return True
2659         elif type and type.is_cpp_class:
2660             for arg in self.args:
2661                 arg.analyse_types(env)
2662             constructor = type.scope.lookup("<init>")
2663             self.function = RawCNameExprNode(self.function.pos, constructor.type)
2664             self.function.entry = constructor
2665             self.function.set_cname(type.declaration_code(""))
2666             self.analyse_c_function_call(env)
2667             return True
2668     
2669     def is_lvalue(self):
2670         return self.type.is_reference
2671
2672     def nogil_check(self, env):
2673         func_type = self.function_type()
2674         if func_type.is_pyobject:
2675             self.gil_error()
2676         elif not getattr(func_type, 'nogil', False):
2677             self.gil_error()
2678
2679     gil_message = "Calling gil-requiring function"
2680
2681
2682 class SimpleCallNode(CallNode):
2683     #  Function call without keyword, * or ** args.
2684     #
2685     #  function       ExprNode
2686     #  args           [ExprNode]
2687     #  arg_tuple      ExprNode or None     used internally
2688     #  self           ExprNode or None     used internally
2689     #  coerced_self   ExprNode or None     used internally
2690     #  wrapper_call   bool                 used internally
2691     #  has_optional_args   bool            used internally
2692     
2693     subexprs = ['self', 'coerced_self', 'function', 'args', 'arg_tuple']
2694     
2695     self = None
2696     coerced_self = None
2697     arg_tuple = None
2698     wrapper_call = False
2699     has_optional_args = False
2700     
2701     def compile_time_value(self, denv):
2702         function = self.function.compile_time_value(denv)
2703         args = [arg.compile_time_value(denv) for arg in self.args]
2704         try:
2705             return function(*args)
2706         except Exception, e:
2707             self.compile_time_value_error(e)
2708             
2709     def type_dependencies(self, env):
2710         # TODO: Update when Danilo's C++ code merged in to handle the
2711         # the case of function overloading.
2712         return self.function.type_dependencies(env)
2713     
2714     def infer_type(self, env):
2715         function = self.function
2716         func_type = function.infer_type(env)
2717         if isinstance(self.function, NewExprNode):
2718             return PyrexTypes.CPtrType(self.function.class_type)
2719         if func_type.is_ptr:
2720             func_type = func_type.base_type
2721         if func_type.is_cfunction:
2722             return func_type.return_type
2723         elif func_type is type_type:
2724             if function.is_name and function.entry and function.entry.type:
2725                 result_type = function.entry.type
2726                 if result_type.is_extension_type:
2727                     return result_type
2728                 elif result_type.is_builtin_type:
2729                     if function.entry.name == 'float':
2730                         return PyrexTypes.c_double_type
2731                     elif function.entry.name in Builtin.types_that_construct_their_instance:
2732                         return result_type
2733         return py_object_type
2734
2735     def analyse_as_type(self, env):
2736         attr = self.function.as_cython_attribute()
2737         if attr == 'pointer':
2738             if len(self.args) != 1:
2739                 error(self.args.pos, "only one type allowed.")
2740             else:
2741                 type = self.args[0].analyse_as_type(env)
2742                 if not type:
2743                     error(self.args[0].pos, "Unknown type")
2744                 else:
2745                     return PyrexTypes.CPtrType(type)
2746
2747     def explicit_args_kwds(self):
2748         return self.args, None
2749
2750     def analyse_types(self, env):
2751         if self.analyse_as_type_constructor(env):
2752             return
2753         function = self.function
2754         function.is_called = 1
2755         self.function.analyse_types(env)
2756         if function.is_attribute and function.entry and function.entry.is_cmethod:
2757             # Take ownership of the object from which the attribute
2758             # was obtained, because we need to pass it as 'self'.
2759             self.self = function.obj
2760             function.obj = CloneNode(self.self)
2761         func_type = self.function_type()
2762         if func_type.is_pyobject:
2763             self.arg_tuple = TupleNode(self.pos, args = self.args)
2764             self.arg_tuple.analyse_types(env)
2765             self.args = None
2766             if func_type is Builtin.type_type and function.is_name and \
2767                    function.entry and \
2768                    function.entry.is_builtin and \
2769                    function.entry.name in Builtin.types_that_construct_their_instance:
2770                 # calling a builtin type that returns a specific object type
2771                 if function.entry.name == 'float':
2772                     # the following will come true later on in a transform
2773                     self.type = PyrexTypes.c_double_type
2774                     self.result_ctype = PyrexTypes.c_double_type
2775                 else:
2776                     self.type = Builtin.builtin_types[function.entry.name]
2777                     self.result_ctype = py_object_type
2778                 self.may_return_none = False
2779             elif function.is_name and function.type_entry:
2780                 # We are calling an extension type constructor.  As
2781                 # long as we do not support __new__(), the result type
2782                 # is clear
2783                 self.type = function.type_entry.type
2784                 self.result_ctype = py_object_type
2785                 self.may_return_none = False
2786             else:
2787                 self.type = py_object_type
2788             self.is_temp = 1
2789         else:
2790             for arg in self.args:
2791                 arg.analyse_types(env)
2792             if self.self and func_type.args:
2793                 # Coerce 'self' to the type expected by the method.
2794                 expected_type = func_type.args[0].type
2795                 self.coerced_self = CloneNode(self.self).coerce_to(
2796                     expected_type, env)
2797                 # Insert coerced 'self' argument into argument list.
2798                 self.args.insert(0, self.coerced_self)
2799             self.analyse_c_function_call(env)
2800     
2801     def function_type(self):
2802         # Return the type of the function being called, coercing a function
2803         # pointer to a function if necessary.
2804         func_type = self.function.type
2805         if func_type.is_ptr:
2806             func_type = func_type.base_type
2807         return func_type
2808     
2809     def analyse_c_function_call(self, env):
2810         if self.function.type is error_type:
2811             self.type = error_type
2812             return
2813         if self.function.type.is_cpp_class:
2814             overloaded_entry = self.function.type.scope.lookup("operator()")
2815             if overloaded_entry is None:
2816                 self.type = PyrexTypes.error_type
2817                 self.result_code = "<error>"
2818                 return
2819         elif hasattr(self.function, 'entry'):
2820             overloaded_entry = self.function.entry
2821         else:
2822             overloaded_entry = None
2823         if overloaded_entry:
2824             entry = PyrexTypes.best_match(self.args, overloaded_entry.all_alternatives(), self.pos)
2825             if not entry:
2826                 self.type = PyrexTypes.error_type
2827                 self.result_code = "<error>"
2828                 return
2829             self.function.entry = entry
2830             self.function.type = entry.type
2831             func_type = self.function_type()
2832         else:
2833             func_type = self.function_type()
2834             if not func_type.is_cfunction:
2835                 error(self.pos, "Calling non-function type '%s'" % func_type)
2836                 self.type = PyrexTypes.error_type
2837                 self.result_code = "<error>"
2838                 return
2839         # Check no. of args
2840         max_nargs = len(func_type.args)
2841         expected_nargs = max_nargs - func_type.optional_arg_count
2842         actual_nargs = len(self.args)
2843         if func_type.optional_arg_count and expected_nargs != actual_nargs:
2844             self.has_optional_args = 1
2845             self.is_temp = 1
2846         # Coerce arguments
2847         for i in range(min(max_nargs, actual_nargs)):
2848             formal_type = func_type.args[i].type
2849             self.args[i] = self.args[i].coerce_to(formal_type, env)
2850         for i in range(max_nargs, actual_nargs):
2851             if self.args[i].type.is_pyobject:
2852                 error(self.args[i].pos, 
2853                     "Python object cannot be passed as a varargs parameter")
2854         # Calc result type and code fragment
2855         if isinstance(self.function, NewExprNode):
2856             self.type = PyrexTypes.CPtrType(self.function.class_type)
2857         else:
2858             self.type = func_type.return_type
2859         if self.type.is_pyobject:
2860             self.result_ctype = py_object_type
2861             self.is_temp = 1
2862         elif func_type.exception_value is not None \
2863                  or func_type.exception_check:
2864             self.is_temp = 1
2865         # C++ exception handler
2866         if func_type.exception_check == '+':
2867             if func_type.exception_value is None:
2868                 env.use_utility_code(cpp_exception_utility_code)
2869
2870     def calculate_result_code(self):
2871         return self.c_call_code()
2872     
2873     def c_call_code(self):
2874         func_type = self.function_type()
2875         if self.type is PyrexTypes.error_type or not func_type.is_cfunction:
2876             return "<error>"
2877         formal_args = func_type.args
2878         arg_list_code = []
2879         args = zip(formal_args, self.args)
2880         max_nargs = len(func_type.args)
2881         expected_nargs = max_nargs - func_type.optional_arg_count
2882         actual_nargs = len(self.args)
2883         for formal_arg, actual_arg in args[:expected_nargs]:
2884                 arg_code = actual_arg.result_as(formal_arg.type)
2885                 arg_list_code.append(arg_code)
2886                 
2887         if func_type.is_overridable:
2888             arg_list_code.append(str(int(self.wrapper_call or self.function.entry.is_unbound_cmethod)))
2889                 
2890         if func_type.optional_arg_count:
2891             if expected_nargs == actual_nargs:
2892                 optional_args = 'NULL'
2893             else:
2894                 optional_args = "&%s" % self.opt_arg_struct
2895             arg_list_code.append(optional_args)
2896             
2897         for actual_arg in self.args[len(formal_args):]:
2898             arg_list_code.append(actual_arg.result())
2899         result = "%s(%s)" % (self.function.result(),
2900             ', '.join(arg_list_code))
2901         return result
2902     
2903     def generate_result_code(self, code):
2904         func_type = self.function_type()
2905         if func_type.is_pyobject:
2906             arg_code = self.arg_tuple.py_result()
2907             code.putln(
2908                 "%s = PyObject_Call(%s, %s, NULL); %s" % (
2909                     self.result(),
2910                     self.function.py_result(),
2911                     arg_code,
2912                     code.error_goto_if_null(self.result(), self.pos)))
2913             code.put_gotref(self.py_result())
2914         elif func_type.is_cfunction:
2915             if self.has_optional_args:
2916                 actual_nargs = len(self.args)
2917                 expected_nargs = len(func_type.args) - func_type.optional_arg_count
2918                 self.opt_arg_struct = code.funcstate.allocate_temp(
2919                     func_type.op_arg_struct.base_type, manage_ref=True)
2920                 code.putln("%s.%s = %s;" % (
2921                         self.opt_arg_struct,
2922                         Naming.pyrex_prefix + "n",
2923                         len(self.args) - expected_nargs))
2924                 args = zip(func_type.args, self.args)
2925                 for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
2926                     code.putln("%s.%s = %s;" % (
2927                             self.opt_arg_struct,
2928                             func_type.opt_arg_cname(formal_arg.name),
2929                             actual_arg.result_as(formal_arg.type)))
2930             exc_checks = []
2931             if self.type.is_pyobject and self.is_temp:
2932                 exc_checks.append("!%s" % self.result())
2933             else:
2934                 exc_val = func_type.exception_value
2935                 exc_check = func_type.exception_check
2936                 if exc_val is not None:
2937                     exc_checks.append("%s == %s" % (self.result(), exc_val))
2938                 if exc_check:
2939                     exc_checks.append("PyErr_Occurred()")
2940             if self.is_temp or exc_checks:
2941                 rhs = self.c_call_code()
2942                 if self.result():
2943                     lhs = "%s = " % self.result()
2944                     if self.is_temp and self.type.is_pyobject:
2945                         #return_type = self.type # func_type.return_type
2946                         #print "SimpleCallNode.generate_result_code: casting", rhs, \
2947                         #    "from", return_type, "to pyobject" ###
2948                         rhs = typecast(py_object_type, self.type, rhs)
2949                 else:
2950                     lhs = ""
2951                 if func_type.exception_check == '+':
2952                     if func_type.exception_value is None:
2953                         raise_py_exception = "__Pyx_CppExn2PyErr()"
2954                     elif func_type.exception_value.type.is_pyobject:
2955                         raise_py_exception = ' try { throw; } catch(const std::exception& exn) { PyErr_SetString(%s, exn.what()); } catch(...) { PyErr_SetNone(%s); }' % (
2956                             func_type.exception_value.entry.cname,
2957                             func_type.exception_value.entry.cname)
2958                     else:
2959                         raise_py_exception = '%s(); if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError , "Error converting c++ exception.")' % func_type.exception_value.entry.cname
2960                     code.putln(
2961                     "try {%s%s;} catch(...) {%s; %s}" % (
2962                         lhs,
2963                         rhs,
2964                         raise_py_exception,
2965                         code.error_goto(self.pos)))
2966                 else:
2967                     if exc_checks:
2968                         goto_error = code.error_goto_if(" && ".join(exc_checks), self.pos)
2969                     else:
2970                         goto_error = ""
2971                     code.putln("%s%s; %s" % (lhs, rhs, goto_error))
2972                 if self.type.is_pyobject and self.result():
2973                     code.put_gotref(self.py_result())
2974             if self.has_optional_args:
2975                 code.funcstate.release_temp(self.opt_arg_struct)
2976
2977
2978 class PythonCapiFunctionNode(ExprNode):
2979     subexprs = []
2980     def __init__(self, pos, py_name, cname, func_type, utility_code = None):
2981         self.pos = pos
2982         self.name = py_name
2983         self.cname = cname
2984         self.type = func_type
2985         self.utility_code = utility_code
2986
2987     def analyse_types(self, env):
2988         pass
2989
2990     def generate_result_code(self, code):
2991         if self.utility_code:
2992             code.globalstate.use_utility_code(self.utility_code)
2993
2994     def calculate_result_code(self):
2995         return self.cname
2996
2997 class PythonCapiCallNode(SimpleCallNode):
2998     # Python C-API Function call (only created in transforms)
2999
3000     # By default, we assume that the call never returns None, as this
3001     # is true for most C-API functions in CPython.  If this does not
3002     # apply to a call, set the following to True (or None to inherit
3003     # the default behaviour).
3004     may_return_none = False
3005
3006     def __init__(self, pos, function_name, func_type,
3007                  utility_code = None, py_name=None, **kwargs):
3008         self.type = func_type.return_type
3009         self.result_ctype = self.type
3010         self.function = PythonCapiFunctionNode(
3011             pos, py_name, function_name, func_type,
3012             utility_code = utility_code)
3013         # call this last so that we can override the constructed
3014         # attributes above with explicit keyword arguments if required
3015         SimpleCallNode.__init__(self, pos, **kwargs)
3016
3017
3018 class GeneralCallNode(CallNode):
3019     #  General Python function call, including keyword,
3020     #  * and ** arguments.
3021     #
3022     #  function         ExprNode
3023     #  positional_args  ExprNode          Tuple of positional arguments
3024     #  keyword_args     ExprNode or None  Dict of keyword arguments
3025     #  starstar_arg     ExprNode or None  Dict of extra keyword args
3026     
3027     type = py_object_type
3028     
3029     subexprs = ['function', 'positional_args', 'keyword_args', 'starstar_arg']
3030
3031     nogil_check = Node.gil_error
3032
3033     def compile_time_value(self, denv):
3034         function = self.function.compile_time_value(denv)
3035         positional_args = self.positional_args.compile_time_value(denv)
3036         keyword_args = self.keyword_args.compile_time_value(denv)
3037         starstar_arg = self.starstar_arg.compile_time_value(denv)
3038         try:
3039             keyword_args.update(starstar_arg)
3040             return function(*positional_args, **keyword_args)
3041         except Exception, e:
3042             self.compile_time_value_error(e)
3043             
3044     def explicit_args_kwds(self):
3045         if self.starstar_arg or not isinstance(self.positional_args, TupleNode):
3046             raise PostParseError(self.pos,
3047                 'Compile-time keyword arguments must be explicit.')
3048         return self.positional_args.args, self.keyword_args
3049
3050     def analyse_types(self, env):
3051         if self.analyse_as_type_constructor(env):
3052             return
3053         self.function.analyse_types(env)
3054         self.positional_args.analyse_types(env)
3055         if self.keyword_args:
3056             self.keyword_args.analyse_types(env)
3057         if self.starstar_arg:
3058             self.starstar_arg.analyse_types(env)
3059         if not self.function.type.is_pyobject:
3060             if self.function.type.is_error:
3061                 self.type = error_type
3062                 return
3063             if hasattr(self.function, 'entry') and not self.function.entry.as_variable:
3064                 error(self.pos, "Keyword and starred arguments not allowed in cdef functions.")
3065             else:
3066                 self.function = self.function.coerce_to_pyobject(env)
3067         self.positional_args = \
3068             self.positional_args.coerce_to_pyobject(env)
3069         if self.starstar_arg:
3070             self.starstar_arg = \
3071                 self.starstar_arg.coerce_to_pyobject(env)
3072         function = self.function
3073         if function.is_name and function.type_entry:
3074             # We are calling an extension type constructor.  As long
3075             # as we do not support __new__(), the result type is clear
3076             self.type = function.type_entry.type
3077             self.result_ctype = py_object_type
3078             self.may_return_none = False
3079         else:
3080             self.type = py_object_type
3081         self.is_temp = 1
3082         
3083     def generate_result_code(self, code):
3084         if self.type.is_error: return
3085         kwargs_call_function = "PyEval_CallObjectWithKeywords"
3086         if self.keyword_args and self.starstar_arg:
3087             code.put_error_if_neg(self.pos, 
3088                 "PyDict_Update(%s, %s)" % (
3089                     self.keyword_args.py_result(), 
3090                     self.starstar_arg.py_result()))
3091             keyword_code = self.keyword_args.py_result()
3092         elif self.keyword_args:
3093             keyword_code = self.keyword_args.py_result()
3094         elif self.starstar_arg:
3095             keyword_code = self.starstar_arg.py_result()
3096             if self.starstar_arg.type is not Builtin.dict_type:
3097                 # CPython supports calling functions with non-dicts, so do we
3098                 code.globalstate.use_utility_code(kwargs_call_utility_code)
3099                 kwargs_call_function = "__Pyx_PyEval_CallObjectWithKeywords"
3100         else:
3101             keyword_code = None
3102         if not keyword_code:
3103             call_code = "PyObject_Call(%s, %s, NULL)" % (
3104                 self.function.py_result(),
3105                 self.positional_args.py_result())
3106         else:
3107             call_code = "%s(%s, %s, %s)" % (
3108                 kwargs_call_function,
3109                 self.function.py_result(),
3110                 self.positional_args.py_result(),
3111                 keyword_code)
3112         code.putln(
3113             "%s = %s; %s" % (
3114                 self.result(),
3115                 call_code,
3116                 code.error_goto_if_null(self.result(), self.pos)))
3117         code.put_gotref(self.py_result())
3118
3119
3120 class AsTupleNode(ExprNode):
3121     #  Convert argument to tuple. Used for normalising
3122     #  the * argument of a function call.
3123     #
3124     #  arg    ExprNode
3125     
3126     subexprs = ['arg']
3127
3128     def calculate_constant_result(self):
3129         self.constant_result = tuple(self.base.constant_result)
3130     
3131     def compile_time_value(self, denv):
3132         arg = self.arg.compile_time_value(denv)
3133         try:
3134             return tuple(arg)
3135         except Exception, e:
3136             self.compile_time_value_error(e)
3137
3138     def analyse_types(self, env):
3139         self.arg.analyse_types(env)
3140         self.arg = self.arg.coerce_to_pyobject(env)
3141         self.type = tuple_type
3142         self.is_temp = 1
3143
3144     def may_be_none(self):
3145         return False
3146
3147     nogil_check = Node.gil_error
3148     gil_message = "Constructing Python tuple"
3149
3150     def generate_result_code(self, code):
3151         code.putln(
3152             "%s = PySequence_Tuple(%s); %s" % (
3153                 self.result(),
3154                 self.arg.py_result(),
3155                 code.error_goto_if_null(self.result(), self.pos)))
3156         code.put_gotref(self.py_result())
3157     
3158
3159 class AttributeNode(ExprNode):
3160     #  obj.attribute
3161     #
3162     #  obj          ExprNode
3163     #  attribute    string
3164     #  needs_none_check boolean        Used if obj is an extension type.
3165     #                                  If set to True, it is known that the type is not None.
3166     #
3167     #  Used internally:
3168     #
3169     #  is_py_attr           boolean   Is a Python getattr operation
3170     #  member               string    C name of struct member
3171     #  is_called            boolean   Function call is being done on result
3172     #  entry                Entry     Symbol table entry of attribute
3173     
3174     is_attribute = 1
3175     subexprs = ['obj']
3176     
3177     type = PyrexTypes.error_type
3178     entry = None
3179     is_called = 0
3180     needs_none_check = True
3181
3182     def as_cython_attribute(self):
3183         if isinstance(self.obj, NameNode) and self.obj.is_cython_module:
3184             return self.attribute
3185         cy = self.obj.as_cython_attribute()
3186         if cy:
3187             return "%s.%s" % (cy, self.attribute)
3188
3189     def coerce_to(self, dst_type, env):
3190         #  If coercing to a generic pyobject and this is a cpdef function
3191         #  we can create the corresponding attribute
3192         if dst_type is py_object_type:
3193             entry = self.entry
3194             if entry and entry.is_cfunction and entry.as_variable:
3195                 # must be a cpdef function
3196                 self.is_temp = 1
3197                 self.entry = entry.as_variable
3198                 self.analyse_as_python_attribute(env) 
3199                 return self
3200         return ExprNode.coerce_to(self, dst_type, env)
3201
3202     def calculate_constant_result(self):
3203         attr = self.attribute
3204         if attr.startswith("__") and attr.endswith("__"):
3205             return
3206         self.constant_result = getattr(self.obj.constant_result, attr)
3207
3208     def compile_time_value(self, denv):
3209         attr = self.attribute
3210         if attr.startswith("__") and attr.endswith("__"):
3211             error(self.pos,
3212                   "Invalid attribute name '%s' in compile-time expression" % attr)
3213             return None
3214         obj = self.obj.compile_time_value(denv)
3215         try:
3216             return getattr(obj, attr)
3217         except Exception, e:
3218             self.compile_time_value_error(e)
3219     
3220     def type_dependencies(self, env):
3221         return self.obj.type_dependencies(env)
3222     
3223     def infer_type(self, env):
3224         if self.analyse_as_cimported_attribute(env, 0):
3225             return self.entry.type
3226         elif self.analyse_as_unbound_cmethod(env):
3227             return self.entry.type
3228         else:
3229             self.analyse_attribute(env, obj_type = self.obj.infer_type(env))
3230             return self.type
3231
3232     def analyse_target_declaration(self, env):
3233         pass
3234     
3235     def analyse_target_types(self, env):
3236         self.analyse_types(env, target = 1)
3237     
3238     def analyse_types(self, env, target = 0):
3239         if self.analyse_as_cimported_attribute(env, target):
3240             return
3241         if not target and self.analyse_as_unbound_cmethod(env):
3242             return
3243         self.analyse_as_ordinary_attribute(env, target)
3244     
3245     def analyse_as_cimported_attribute(self, env, target):
3246         # Try to interpret this as a reference to an imported
3247         # C const, type, var or function. If successful, mutates
3248         # this node into a NameNode and returns 1, otherwise
3249         # returns 0.
3250         module_scope = self.obj.analyse_as_module(env)
3251         if module_scope:
3252             entry = module_scope.lookup_here(self.attribute)
3253             if entry and (
3254                 entry.is_cglobal or entry.is_cfunction
3255                 or entry.is_type or entry.is_const):
3256                     self.mutate_into_name_node(env, entry, target)
3257                     return 1
3258         return 0
3259     
3260     def analyse_as_unbound_cmethod(self, env):
3261         # Try to interpret this as a reference to an unbound
3262         # C method of an extension type. If successful, mutates
3263         # this node into a NameNode and returns 1, otherwise
3264         # returns 0.
3265         type = self.obj.analyse_as_extension_type(env)
3266         if type:
3267             entry = type.scope.lookup_here(self.attribute)
3268             if entry and entry.is_cmethod:
3269                 # Create a temporary entry describing the C method
3270                 # as an ordinary function.
3271                 ubcm_entry = Symtab.Entry(entry.name,
3272                     "%s->%s" % (type.vtabptr_cname, entry.cname),
3273                     entry.type)
3274                 ubcm_entry.is_cfunction = 1
3275                 ubcm_entry.func_cname = entry.func_cname
3276                 ubcm_entry.is_unbound_cmethod = 1
3277                 self.mutate_into_name_node(env, ubcm_entry, None)
3278                 return 1
3279         return 0
3280         
3281     def analyse_as_type(self, env):
3282         module_scope = self.obj.analyse_as_module(env)
3283         if module_scope:
3284             return module_scope.lookup_type(self.attribute)
3285         if not isinstance(self.obj, (UnicodeNode, StringNode, BytesNode)):
3286             base_type = self.obj.analyse_as_type(env)
3287             if base_type and hasattr(base_type, 'scope'):
3288                 return base_type.scope.lookup_type(self.attribute)
3289         return None
3290     
3291     def analyse_as_extension_type(self, env):
3292         # Try to interpret this as a reference to an extension type
3293         # in a cimported module. Returns the extension type, or None.
3294         module_scope = self.obj.analyse_as_module(env)
3295         if module_scope:
3296             entry = module_scope.lookup_here(self.attribute)
3297             if entry and entry.is_type and entry.type.is_extension_type:
3298                 return entry.type
3299         return None
3300     
3301     def analyse_as_module(self, env):
3302         # Try to interpret this as a reference to a cimported module
3303         # in another cimported module. Returns the module scope, or None.
3304         module_scope = self.obj.analyse_as_module(env)
3305         if module_scope:
3306             entry = module_scope.lookup_here(self.attribute)
3307             if entry and entry.as_module:
3308                 return entry.as_module
3309         return None
3310                 
3311     def mutate_into_name_node(self, env, entry, target):
3312         # Mutate this node into a NameNode and complete the
3313         # analyse_types phase.
3314         self.__class__ = NameNode
3315         self.name = self.attribute
3316         self.entry = entry
3317         del self.obj
3318         del self.attribute
3319         if target:
3320             NameNode.analyse_target_types(self, env)
3321         else:
3322             NameNode.analyse_rvalue_entry(self, env)
3323     
3324     def analyse_as_ordinary_attribute(self, env, target):
3325         self.obj.analyse_types(env)
3326         self.analyse_attribute(env)
3327         if self.entry and self.entry.is_cmethod and not self.is_called:
3328 #            error(self.pos, "C method can only be called")
3329             pass
3330         ## Reference to C array turns into pointer to first element.
3331         #while self.type.is_array:
3332         #    self.type = self.type.element_ptr_type()
3333         if self.is_py_attr:
3334             if not target:
3335                 self.is_temp = 1
3336                 self.result_ctype = py_object_type
3337     
3338     def analyse_attribute(self, env, obj_type = None):
3339         # Look up attribute and set self.type and self.member.
3340         self.is_py_attr = 0
3341         self.member = self.attribute
3342         if obj_type is None:
3343             if self.obj.type.is_string:
3344                 self.obj = self.obj.coerce_to_pyobject(env)
3345             obj_type = self.obj.type
3346         else:
3347             if obj_type.is_string:
3348                 obj_type = py_object_type
3349         if obj_type.is_ptr or obj_type.is_array:
3350             obj_type = obj_type.base_type
3351             self.op = "->"
3352         elif obj_type.is_extension_type:
3353             self.op = "->"
3354         else:
3355             self.op = "."
3356         if obj_type.has_attributes:
3357             entry = None
3358             if obj_type.attributes_known():
3359                 entry = obj_type.scope.lookup_here(self.attribute)
3360                 if entry and entry.is_member:
3361                     entry = None
3362             else:
3363                 error(self.pos, 
3364                     "Cannot select attribute of incomplete type '%s'" 
3365                     % obj_type)
3366                 self.type = PyrexTypes.error_type
3367                 return
3368             self.entry = entry
3369             if entry:
3370                 if obj_type.is_extension_type and entry.name == "__weakref__":
3371                     error(self.pos, "Illegal use of special attribute __weakref__")
3372                 # methods need the normal attribute lookup
3373                 # because they do not have struct entries
3374                 if entry.is_variable or entry.is_cmethod:
3375                     self.type = entry.type
3376                     self.member = entry.cname
3377                     return
3378                 else:
3379                     # If it's not a variable or C method, it must be a Python
3380                     # method of an extension type, so we treat it like a Python
3381                     # attribute.
3382                     pass
3383         # If we get here, the base object is not a struct/union/extension 
3384         # type, or it is an extension type and the attribute is either not
3385         # declared or is declared as a Python method. Treat it as a Python
3386         # attribute reference.
3387         self.analyse_as_python_attribute(env, obj_type)
3388
3389     def analyse_as_python_attribute(self, env, obj_type = None):
3390         if obj_type is None:
3391             obj_type = self.obj.type
3392         self.member = self.attribute
3393         self.type = py_object_type
3394         self.is_py_attr = 1
3395         if not obj_type.is_pyobject and not obj_type.is_error:
3396             if obj_type.can_coerce_to_pyobject(env):
3397                 self.obj = self.obj.coerce_to_pyobject(env)
3398             else:
3399                 error(self.pos,
3400                       "Object of type '%s' has no attribute '%s'" %
3401                       (obj_type, self.attribute))
3402
3403     def nogil_check(self, env):
3404         if self.is_py_attr:
3405             self.gil_error()
3406
3407     gil_message = "Accessing Python attribute"
3408
3409     def is_simple(self):
3410         if self.obj:
3411             return self.result_in_temp() or self.obj.is_simple()
3412         else:
3413             return NameNode.is_simple(self)
3414
3415     def is_lvalue(self):
3416         if self.obj:
3417             return 1
3418         else:
3419             return NameNode.is_lvalue(self)
3420     
3421     def is_ephemeral(self):
3422         if self.obj:
3423             return self.obj.is_ephemeral()
3424         else:
3425             return NameNode.is_ephemeral(self)
3426     
3427     def calculate_result_code(self):
3428         #print "AttributeNode.calculate_result_code:", self.member ###
3429         #print "...obj node =", self.obj, "code", self.obj.result() ###
3430         #print "...obj type", self.obj.type, "ctype", self.obj.ctype() ###
3431         obj = self.obj
3432         obj_code = obj.result_as(obj.type)
3433         #print "...obj_code =", obj_code ###
3434         if self.entry and self.entry.is_cmethod:
3435             if obj.type.is_extension_type:
3436                 return "((struct %s *)%s%s%s)->%s" % (
3437                     obj.type.vtabstruct_cname, obj_code, self.op, 
3438                     obj.type.vtabslot_cname, self.member)
3439             else:
3440                 return self.member
3441         elif obj.type.is_complex:
3442             return "__Pyx_C%s(%s)" % (self.member.upper(), obj_code)
3443         else:
3444             return "%s%s%s" % (obj_code, self.op, self.member)
3445     
3446     def generate_result_code(self, code):
3447         interned_attr_cname = code.intern_identifier(self.attribute)
3448         if self.is_py_attr:
3449             code.putln(
3450                 '%s = PyObject_GetAttr(%s, %s); %s' % (
3451                     self.result(),
3452                     self.obj.py_result(),
3453                     interned_attr_cname,
3454                     code.error_goto_if_null(self.result(), self.pos)))
3455             code.put_gotref(self.py_result())
3456         else:
3457             # result_code contains what is needed, but we may need to insert
3458             # a check and raise an exception
3459             if (self.obj.type.is_extension_type
3460                   and self.needs_none_check
3461                   and code.globalstate.directives['nonecheck']):
3462                 self.put_nonecheck(code)
3463     
3464     def generate_assignment_code(self, rhs, code):
3465         interned_attr_cname = code.intern_identifier(self.attribute)
3466         self.obj.generate_evaluation_code(code)
3467         if self.is_py_attr:
3468             code.put_error_if_neg(self.pos, 
3469                 'PyObject_SetAttr(%s, %s, %s)' % (
3470                     self.obj.py_result(),
3471                     interned_attr_cname,
3472                     rhs.py_result()))
3473             rhs.generate_disposal_code(code)
3474             rhs.free_temps(code)
3475         elif self.obj.type.is_complex:
3476             code.putln("__Pyx_SET_C%s(%s, %s);" % (
3477                 self.member.upper(),
3478                 self.obj.result_as(self.obj.type),
3479                 rhs.result_as(self.ctype())))
3480         else:
3481             if (self.obj.type.is_extension_type
3482                   and self.needs_none_check
3483                   and code.globalstate.directives['nonecheck']):
3484                 self.put_nonecheck(code)
3485
3486             select_code = self.result()
3487             if self.type.is_pyobject and self.use_managed_ref:
3488                 rhs.make_owned_reference(code)
3489                 code.put_giveref(rhs.py_result())
3490                 code.put_gotref(select_code)
3491                 code.put_decref(select_code, self.ctype())
3492             code.putln(
3493                 "%s = %s;" % (
3494                     select_code,
3495                     rhs.result_as(self.ctype())))
3496                     #rhs.result()))
3497             rhs.generate_post_assignment_code(code)
3498             rhs.free_temps(code)
3499         self.obj.generate_disposal_code(code)
3500         self.obj.free_temps(code)
3501     
3502     def generate_deletion_code(self, code):
3503         interned_attr_cname = code.intern_identifier(self.attribute)
3504         self.obj.generate_evaluation_code(code)
3505         if self.is_py_attr or (isinstance(self.entry.scope, Symtab.PropertyScope)
3506                                and self.entry.scope.entries.has_key(u'__del__')):
3507             code.put_error_if_neg(self.pos,
3508                 'PyObject_DelAttr(%s, %s)' % (
3509                     self.obj.py_result(),
3510                     interned_attr_cname))
3511         else:
3512             error(self.pos, "Cannot delete C attribute of extension type")
3513         self.obj.generate_disposal_code(code)
3514         self.obj.free_temps(code)
3515         
3516     def annotate(self, code):
3517         if self.is_py_attr:
3518             code.annotate(self.pos, AnnotationItem('py_attr', 'python attribute', size=len(self.attribute)))
3519         else:
3520             code.annotate(self.pos, AnnotationItem('c_attr', 'c attribute', size=len(self.attribute)))
3521
3522     def put_nonecheck(self, code):
3523         code.globalstate.use_utility_code(raise_noneattr_error_utility_code)
3524         code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.obj.result_as(PyrexTypes.py_object_type))
3525         code.putln("__Pyx_RaiseNoneAttributeError(\"%s\");" % self.attribute)
3526         code.putln(code.error_goto(self.pos))
3527         code.putln("}")
3528
3529
3530 #-------------------------------------------------------------------
3531 #
3532 #  Constructor nodes
3533 #
3534 #-------------------------------------------------------------------
3535
3536 class StarredTargetNode(ExprNode):
3537     #  A starred expression like "*a"
3538     #
3539     #  This is only allowed in sequence assignment targets such as
3540     #
3541     #      a, *b = (1,2,3,4)    =>     a = 1 ; b = [2,3,4]
3542     #
3543     #  and will be removed during type analysis (or generate an error
3544     #  if it's found at unexpected places).
3545     #
3546     #  target          ExprNode
3547
3548     subexprs = ['target']
3549     is_starred = 1
3550     type = py_object_type
3551     is_temp = 1
3552
3553     def __init__(self, pos, target):
3554         self.pos = pos
3555         self.target = target
3556
3557     def analyse_declarations(self, env):
3558         error(self.pos, "can use starred expression only as assignment target")
3559         self.target.analyse_declarations(env)
3560
3561     def analyse_types(self, env):
3562         error(self.pos, "can use starred expression only as assignment target")
3563         self.target.analyse_types(env)
3564         self.type = self.target.type
3565
3566     def analyse_target_declaration(self, env):
3567         self.target.analyse_target_declaration(env)
3568
3569     def analyse_target_types(self, env):
3570         self.target.analyse_target_types(env)
3571         self.type = self.target.type
3572
3573     def calculate_result_code(self):
3574         return ""
3575
3576     def generate_result_code(self, code):
3577         pass
3578
3579
3580 class SequenceNode(ExprNode):
3581     #  Base class for list and tuple constructor nodes.
3582     #  Contains common code for performing sequence unpacking.
3583     #
3584     #  args                    [ExprNode]
3585     #  iterator                ExprNode
3586     #  unpacked_items          [ExprNode] or None
3587     #  coerced_unpacked_items  [ExprNode] or None
3588     
3589     subexprs = ['args']
3590     
3591     is_sequence_constructor = 1
3592     unpacked_items = None
3593
3594     def compile_time_value_list(self, denv):
3595         return [arg.compile_time_value(denv) for arg in self.args]
3596
3597     def replace_starred_target_node(self):
3598         # replace a starred node in the targets by the contained expression
3599         self.starred_assignment = False
3600         args = []
3601         for arg in self.args:
3602             if arg.is_starred:
3603                 if self.starred_assignment:
3604                     error(arg.pos, "more than 1 starred expression in assignment")
3605                 self.starred_assignment = True
3606                 arg = arg.target
3607                 arg.is_starred = True
3608             args.append(arg)
3609         self.args = args
3610
3611     def analyse_target_declaration(self, env):
3612         self.replace_starred_target_node()
3613         for arg in self.args:
3614             arg.analyse_target_declaration(env)
3615
3616     def analyse_types(self, env, skip_children=False):
3617         for i in range(len(self.args)):
3618             arg = self.args[i]
3619             if not skip_children: arg.analyse_types(env)
3620             self.args[i] = arg.coerce_to_pyobject(env)
3621         self.type = py_object_type
3622         self.is_temp = 1
3623
3624     def may_be_none(self):
3625         return False
3626
3627     def analyse_target_types(self, env):
3628         self.iterator = PyTempNode(self.pos, env)
3629         self.unpacked_items = []
3630         self.coerced_unpacked_items = []
3631         for arg in self.args:
3632             arg.analyse_target_types(env)
3633             if arg.is_starred:
3634                 if not arg.type.assignable_from(Builtin.list_type):
3635                     error(arg.pos,
3636                           "starred target must have Python object (list) type")
3637                 if arg.type is py_object_type:
3638                     arg.type = Builtin.list_type
3639             unpacked_item = PyTempNode(self.pos, env)
3640             coerced_unpacked_item = unpacked_item.coerce_to(arg.type, env)
3641             self.unpacked_items.append(unpacked_item)
3642             self.coerced_unpacked_items.append(coerced_unpacked_item)
3643         self.type = py_object_type
3644
3645     def generate_result_code(self, code):
3646         self.generate_operation_code(code)
3647     
3648     def generate_assignment_code(self, rhs, code):
3649         if self.starred_assignment:
3650             self.generate_starred_assignment_code(rhs, code)
3651         else:
3652             self.generate_parallel_assignment_code(rhs, code)
3653
3654         for item in self.unpacked_items:
3655             item.release(code)
3656         rhs.free_temps(code)
3657
3658     def generate_parallel_assignment_code(self, rhs, code):
3659         # Need to work around the fact that generate_evaluation_code
3660         # allocates the temps in a rather hacky way -- the assignment
3661         # is evaluated twice, within each if-block.
3662
3663         if rhs.type is tuple_type:
3664             tuple_check = "likely(%s != Py_None)"
3665         else:
3666             tuple_check = "PyTuple_CheckExact(%s)"
3667         code.putln(
3668             "if (%s && likely(PyTuple_GET_SIZE(%s) == %s)) {" % (
3669                 tuple_check % rhs.py_result(), 
3670                 rhs.py_result(), 
3671                 len(self.args)))
3672         code.putln("PyObject* tuple = %s;" % rhs.py_result())
3673         for item in self.unpacked_items:
3674             item.allocate(code)
3675         for i in range(len(self.args)):
3676             item = self.unpacked_items[i]
3677             code.put(
3678                 "%s = PyTuple_GET_ITEM(tuple, %s); " % (
3679                     item.result(),
3680                     i))
3681             code.put_incref(item.result(), item.ctype())
3682             value_node = self.coerced_unpacked_items[i]
3683             value_node.generate_evaluation_code(code)
3684         rhs.generate_disposal_code(code)
3685
3686         for i in range(len(self.args)):
3687             self.args[i].generate_assignment_code(
3688                 self.coerced_unpacked_items[i], code)
3689                  
3690         code.putln("} else {")
3691
3692         if rhs.type is tuple_type:
3693             code.globalstate.use_utility_code(tuple_unpacking_error_code)
3694             code.putln("__Pyx_UnpackTupleError(%s, %s);" % (
3695                         rhs.py_result(), len(self.args)))
3696             code.putln(code.error_goto(self.pos))
3697         else:
3698             code.globalstate.use_utility_code(unpacking_utility_code)
3699
3700             self.iterator.allocate(code)
3701             code.putln(
3702                 "%s = PyObject_GetIter(%s); %s" % (
3703                     self.iterator.result(),
3704                     rhs.py_result(),
3705                     code.error_goto_if_null(self.iterator.result(), self.pos)))
3706             code.put_gotref(self.iterator.py_result())
3707             rhs.generate_disposal_code(code)
3708             for i in range(len(self.args)):
3709                 item = self.unpacked_items[i]
3710                 unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
3711                     self.iterator.py_result(), i)
3712                 code.putln(
3713                     "%s = %s; %s" % (
3714                         item.result(),
3715                         typecast(item.ctype(), py_object_type, unpack_code),
3716                         code.error_goto_if_null(item.result(), self.pos)))
3717                 code.put_gotref(item.py_result())
3718                 value_node = self.coerced_unpacked_items[i]
3719                 value_node.generate_evaluation_code(code)
3720             code.put_error_if_neg(self.pos, "__Pyx_EndUnpack(%s, %d)" % (
3721                 self.iterator.py_result(),
3722                 len(self.args)))
3723             if debug_disposal_code:
3724                 print("UnpackNode.generate_assignment_code:")
3725                 print("...generating disposal code for %s" % self.iterator)
3726             self.iterator.generate_disposal_code(code)
3727             self.iterator.free_temps(code)
3728             self.iterator.release(code)
3729
3730             for i in range(len(self.args)):
3731                 self.args[i].generate_assignment_code(
3732                     self.coerced_unpacked_items[i], code)
3733
3734         code.putln("}")
3735
3736     def generate_starred_assignment_code(self, rhs, code):
3737         code.globalstate.use_utility_code(unpacking_utility_code)
3738
3739         for i, arg in enumerate(self.args):
3740             if arg.is_starred:
3741                 starred_target = self.unpacked_items[i]
3742                 fixed_args_left  = self.args[:i]
3743                 fixed_args_right = self.args[i+1:]
3744                 break
3745
3746         self.iterator.allocate(code)
3747         code.putln(
3748             "%s = PyObject_GetIter(%s); %s" % (
3749                 self.iterator.result(),
3750                 rhs.py_result(),
3751                 code.error_goto_if_null(self.iterator.result(), self.pos)))
3752         code.put_gotref(self.iterator.py_result())
3753         rhs.generate_disposal_code(code)
3754
3755         for item in self.unpacked_items:
3756             item.allocate(code)
3757         for i in range(len(fixed_args_left)):
3758             item = self.unpacked_items[i]
3759             unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
3760                 self.iterator.py_result(), i)
3761             code.putln(
3762                 "%s = %s; %s" % (
3763                     item.result(),
3764                     typecast(item.ctype(), py_object_type, unpack_code),
3765                     code.error_goto_if_null(item.result(), self.pos)))
3766             code.put_gotref(item.py_result())
3767             value_node = self.coerced_unpacked_items[i]
3768             value_node.generate_evaluation_code(code)
3769
3770         target_list = starred_target.result()
3771         code.putln("%s = PySequence_List(%s); %s" % (
3772             target_list, self.iterator.py_result(),
3773             code.error_goto_if_null(target_list, self.pos)))
3774         code.put_gotref(target_list)
3775         if fixed_args_right:
3776             code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
3777             unpacked_right_args = self.unpacked_items[-len(fixed_args_right):]
3778             code.putln("if (unlikely(PyList_GET_SIZE(%s) < %d)) {" % (
3779                 (target_list, len(unpacked_right_args))))
3780             code.put("__Pyx_RaiseNeedMoreValuesError(%d+PyList_GET_SIZE(%s)); %s" % (
3781                      len(fixed_args_left), target_list,
3782                      code.error_goto(self.pos)))
3783             code.putln('}')
3784             for i, (arg, coerced_arg) in enumerate(zip(unpacked_right_args[::-1],
3785                                                        self.coerced_unpacked_items[::-1])):
3786                 code.putln(
3787                     "%s = PyList_GET_ITEM(%s, PyList_GET_SIZE(%s)-1); " % (
3788                         arg.py_result(),
3789                         target_list, target_list))
3790                 # resize the list the hard way
3791                 code.putln("((PyVarObject*)%s)->ob_size--;" % target_list)
3792                 code.put_gotref(arg.py_result())
3793                 coerced_arg.generate_evaluation_code(code)
3794
3795         self.iterator.generate_disposal_code(code)
3796         self.iterator.free_temps(code)
3797         self.iterator.release(code)
3798
3799         for i in range(len(self.args)):
3800             self.args[i].generate_assignment_code(
3801                 self.coerced_unpacked_items[i], code)
3802
3803     def annotate(self, code):
3804         for arg in self.args:
3805             arg.annotate(code)
3806         if self.unpacked_items:
3807             for arg in self.unpacked_items:
3808                 arg.annotate(code)
3809             for arg in self.coerced_unpacked_items:
3810                 arg.annotate(code)
3811
3812
3813 class TupleNode(SequenceNode):
3814     #  Tuple constructor.
3815     
3816     type = tuple_type
3817
3818     gil_message = "Constructing Python tuple"
3819
3820     def analyse_types(self, env, skip_children=False):
3821         if len(self.args) == 0:
3822             self.is_temp = 0
3823             self.is_literal = 1
3824         else:
3825             SequenceNode.analyse_types(self, env, skip_children)
3826             
3827     def calculate_result_code(self):
3828         if len(self.args) > 0:
3829             error(self.pos, "Positive length tuples must be constructed.")
3830         else:
3831             return Naming.empty_tuple
3832
3833     def calculate_constant_result(self):
3834         self.constant_result = tuple([
3835                 arg.constant_result for arg in self.args])
3836
3837     def compile_time_value(self, denv):
3838         values = self.compile_time_value_list(denv)
3839         try:
3840             return tuple(values)
3841         except Exception, e:
3842             self.compile_time_value_error(e)
3843     
3844     def generate_operation_code(self, code):
3845         if len(self.args) == 0:
3846             # result_code is Naming.empty_tuple
3847             return
3848         code.putln(
3849             "%s = PyTuple_New(%s); %s" % (
3850                 self.result(),
3851                 len(self.args),
3852                 code.error_goto_if_null(self.result(), self.pos)))
3853         code.put_gotref(self.py_result())
3854         for i in range(len(self.args)):
3855             arg = self.args[i]
3856             if not arg.result_in_temp():
3857                 code.put_incref(arg.result(), arg.ctype())
3858             code.putln(
3859                 "PyTuple_SET_ITEM(%s, %s, %s);" % (
3860                     self.result(),
3861                     i,
3862                     arg.py_result()))
3863             code.put_giveref(arg.py_result())
3864     
3865     def generate_subexpr_disposal_code(self, code):
3866         # We call generate_post_assignment_code here instead
3867         # of generate_disposal_code, because values were stored
3868         # in the tuple using a reference-stealing operation.
3869         for arg in self.args:
3870             arg.generate_post_assignment_code(code)
3871             # Should NOT call free_temps -- this is invoked by the default
3872             # generate_evaluation_code which will do that.
3873
3874
3875 class ListNode(SequenceNode):
3876     #  List constructor.
3877     
3878     # obj_conversion_errors    [PyrexError]   used internally
3879     # orignial_args            [ExprNode]     used internally
3880
3881     obj_conversion_errors = []
3882
3883     gil_message = "Constructing Python list"
3884     
3885     def type_dependencies(self, env):
3886         return ()
3887     
3888     def infer_type(self, env):
3889         # TOOD: Infer non-object list arrays.
3890         return list_type
3891
3892     def analyse_expressions(self, env):
3893         SequenceNode.analyse_expressions(self, env)
3894         self.coerce_to_pyobject(env)
3895
3896     def analyse_types(self, env):
3897         hold_errors()
3898         self.original_args = list(self.args)
3899         SequenceNode.analyse_types(self, env)
3900         self.type = list_type
3901         self.obj_conversion_errors = held_errors()
3902         release_errors(ignore=True)
3903         
3904     def coerce_to(self, dst_type, env):
3905         if dst_type.is_pyobject:
3906             for err in self.obj_conversion_errors:
3907                 report_error(err)
3908             self.obj_conversion_errors = []
3909             if not self.type.subtype_of(dst_type):
3910                 error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
3911         elif dst_type.is_ptr:
3912             base_type = dst_type.base_type
3913             self.type = PyrexTypes.CArrayType(base_type, len(self.args))
3914             for i in range(len(self.original_args)):
3915                 arg = self.args[i]
3916                 if isinstance(arg, CoerceToPyTypeNode):
3917                     arg = arg.arg
3918                 self.args[i] = arg.coerce_to(base_type, env)
3919         elif dst_type.is_struct:
3920             if len(self.args) > len(dst_type.scope.var_entries):
3921                 error(self.pos, "Too may members for '%s'" % dst_type)
3922             else:
3923                 if len(self.args) < len(dst_type.scope.var_entries):
3924                     warning(self.pos, "Too few members for '%s'" % dst_type, 1)
3925                 for i, (arg, member) in enumerate(zip(self.original_args, dst_type.scope.var_entries)):
3926                     if isinstance(arg, CoerceToPyTypeNode):
3927                         arg = arg.arg
3928                     self.args[i] = arg.coerce_to(member.type, env)
3929             self.type = dst_type
3930         else:
3931             self.type = error_type
3932             error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
3933         return self
3934         
3935     def release_temp(self, env):
3936         if self.type.is_array:
3937             # To be valid C++, we must allocate the memory on the stack 
3938             # manually and be sure not to reuse it for something else. 
3939             pass
3940         else:
3941             SequenceNode.release_temp(self, env)
3942
3943     def calculate_constant_result(self):
3944         self.constant_result = [
3945             arg.constant_result for arg in self.args]
3946
3947     def compile_time_value(self, denv):
3948         return self.compile_time_value_list(denv)
3949
3950     def generate_operation_code(self, code):
3951         if self.type.is_pyobject:
3952             for err in self.obj_conversion_errors:
3953                 report_error(err)
3954             code.putln("%s = PyList_New(%s); %s" %
3955                 (self.result(),
3956                 len(self.args),
3957                 code.error_goto_if_null(self.result(), self.pos)))
3958             code.put_gotref(self.py_result())
3959             for i in range(len(self.args)):
3960                 arg = self.args[i]
3961                 #if not arg.is_temp:
3962                 if not arg.result_in_temp():
3963                     code.put_incref(arg.result(), arg.ctype())
3964                 code.putln("PyList_SET_ITEM(%s, %s, %s);" %
3965                     (self.result(),
3966                     i,
3967                     arg.py_result()))
3968                 code.put_giveref(arg.py_result())
3969         elif self.type.is_array:
3970             for i, arg in enumerate(self.args):
3971                 code.putln("%s[%s] = %s;" % (
3972                                 self.result(),
3973                                 i,
3974                                 arg.result()))
3975         elif self.type.is_struct:
3976             for arg, member in zip(self.args, self.type.scope.var_entries):
3977                 code.putln("%s.%s = %s;" % (
3978                         self.result(),
3979                         member.cname,
3980                         arg.result()))
3981         else:
3982             raise InternalError("List type never specified")
3983
3984     def generate_subexpr_disposal_code(self, code):
3985         # We call generate_post_assignment_code here instead
3986         # of generate_disposal_code, because values were stored
3987         # in the list using a reference-stealing operation.
3988         for arg in self.args:
3989             arg.generate_post_assignment_code(code)
3990             # Should NOT call free_temps -- this is invoked by the default
3991             # generate_evaluation_code which will do that.
3992
3993
3994 class ScopedExprNode(ExprNode):
3995     # Abstract base class for ExprNodes that have their own local
3996     # scope, such as generator expressions.
3997     #
3998     # expr_scope    Scope  the inner scope of the expression
3999
4000     subexprs = []
4001     expr_scope = None
4002
4003     def analyse_types(self, env):
4004         # nothing to do here, the children will be analysed separately
4005         pass
4006
4007     def analyse_expressions(self, env):
4008         # nothing to do here, the children will be analysed separately
4009         pass
4010
4011     def analyse_scoped_expressions(self, env):
4012         # this is called with the expr_scope as env
4013         pass
4014
4015     def init_scope(self, outer_scope, expr_scope=None):
4016         self.expr_scope = expr_scope
4017
4018
4019 class ComprehensionNode(ScopedExprNode):
4020     subexprs = ["target"]
4021     child_attrs = ["loop", "append"]
4022
4023     # leak loop variables or not?  non-leaking Py3 behaviour is
4024     # default, except for list comprehensions where the behaviour
4025     # differs in Py2 and Py3 (see Parsing.py)
4026     has_local_scope = True
4027
4028     def infer_type(self, env):
4029         return self.target.infer_type(env)
4030
4031     def analyse_declarations(self, env):
4032         self.append.target = self # this is used in the PyList_Append of the inner loop
4033         self.init_scope(env)
4034         if self.expr_scope is not None:
4035             self.loop.analyse_declarations(self.expr_scope)
4036         else:
4037             self.loop.analyse_declarations(env)
4038
4039     def init_scope(self, outer_scope, expr_scope=None):
4040         if expr_scope is not None:
4041             self.expr_scope = expr_scope
4042         elif self.has_local_scope:
4043             self.expr_scope = Symtab.GeneratorExpressionScope(outer_scope)
4044         else:
4045             self.expr_scope = None
4046
4047     def analyse_types(self, env):
4048         self.target.analyse_expressions(env)
4049         self.type = self.target.type
4050         if not self.has_local_scope:
4051             self.loop.analyse_expressions(env)
4052
4053     def analyse_expressions(self, env):
4054         self.analyse_types(env)
4055
4056     def analyse_scoped_expressions(self, env):
4057         if self.has_local_scope:
4058             self.loop.analyse_expressions(env)
4059
4060     def may_be_none(self):
4061         return False
4062
4063     def calculate_result_code(self):
4064         return self.target.result()
4065     
4066     def generate_result_code(self, code):
4067         self.generate_operation_code(code)
4068
4069     def generate_operation_code(self, code):
4070         self.loop.generate_execution_code(code)
4071
4072     def annotate(self, code):
4073         self.loop.annotate(code)
4074
4075
4076 class ComprehensionAppendNode(Node):
4077     # Need to be careful to avoid infinite recursion:
4078     # target must not be in child_attrs/subexprs
4079
4080     child_attrs = ['expr']
4081
4082     type = PyrexTypes.c_int_type
4083     
4084     def analyse_expressions(self, env):
4085         self.expr.analyse_expressions(env)
4086         if not self.expr.type.is_pyobject:
4087             self.expr = self.expr.coerce_to_pyobject(env)
4088
4089     def generate_execution_code(self, code):
4090         if self.target.type is list_type:
4091             function = "PyList_Append"
4092         elif self.target.type is set_type:
4093             function = "PySet_Add"
4094         else:
4095             raise InternalError(
4096                 "Invalid type for comprehension node: %s" % self.target.type)
4097
4098         self.expr.generate_evaluation_code(code)
4099         code.putln(code.error_goto_if("%s(%s, (PyObject*)%s)" % (
4100             function,
4101             self.target.result(),
4102             self.expr.result()
4103             ), self.pos))
4104         self.expr.generate_disposal_code(code)
4105         self.expr.free_temps(code)
4106
4107     def generate_function_definitions(self, env, code):
4108         self.expr.generate_function_definitions(env, code)
4109
4110     def annotate(self, code):
4111         self.expr.annotate(code)
4112
4113 class DictComprehensionAppendNode(ComprehensionAppendNode):
4114     child_attrs = ['key_expr', 'value_expr']
4115
4116     def analyse_expressions(self, env):
4117         self.key_expr.analyse_expressions(env)
4118         if not self.key_expr.type.is_pyobject:
4119             self.key_expr = self.key_expr.coerce_to_pyobject(env)
4120         self.value_expr.analyse_expressions(env)
4121         if not self.value_expr.type.is_pyobject:
4122             self.value_expr = self.value_expr.coerce_to_pyobject(env)
4123
4124     def generate_execution_code(self, code):
4125         self.key_expr.generate_evaluation_code(code)
4126         self.value_expr.generate_evaluation_code(code)
4127         code.putln(code.error_goto_if("PyDict_SetItem(%s, (PyObject*)%s, (PyObject*)%s)" % (
4128             self.target.result(),
4129             self.key_expr.result(),
4130             self.value_expr.result()
4131             ), self.pos))
4132         self.key_expr.generate_disposal_code(code)
4133         self.key_expr.free_temps(code)
4134         self.value_expr.generate_disposal_code(code)
4135         self.value_expr.free_temps(code)
4136
4137     def generate_function_definitions(self, env, code):
4138         self.key_expr.generate_function_definitions(env, code)
4139         self.value_expr.generate_function_definitions(env, code)
4140
4141     def annotate(self, code):
4142         self.key_expr.annotate(code)
4143         self.value_expr.annotate(code)
4144
4145
4146 class GeneratorExpressionNode(ScopedExprNode):
4147     # A generator expression, e.g.  (i for i in range(10))
4148     #
4149     # Result is a generator.
4150     #
4151     # loop      ForStatNode   the for-loop, containing a YieldExprNode
4152
4153     child_attrs = ["loop"]
4154
4155     type = py_object_type
4156
4157     def analyse_declarations(self, env):
4158         self.init_scope(env)
4159         self.loop.analyse_declarations(self.expr_scope)
4160
4161     def init_scope(self, outer_scope, expr_scope=None):
4162         if expr_scope is not None:
4163             self.expr_scope = expr_scope
4164         else:
4165             self.expr_scope = Symtab.GeneratorExpressionScope(outer_scope)
4166
4167     def analyse_types(self, env):
4168         self.is_temp = True
4169
4170     def analyse_scoped_expressions(self, env):
4171         self.loop.analyse_expressions(env)
4172
4173     def may_be_none(self):
4174         return False
4175
4176     def annotate(self, code):
4177         self.loop.annotate(code)
4178
4179
4180 class InlinedGeneratorExpressionNode(GeneratorExpressionNode):
4181     # An inlined generator expression for which the result is
4182     # calculated inside of the loop.  This will only be created by
4183     # transforms when replacing builtin calls on generator
4184     # expressions.
4185     #
4186     # loop           ForStatNode      the for-loop, not containing any YieldExprNodes
4187     # result_node    ResultRefNode    the reference to the result value temp
4188     # orig_func      String           the name of the builtin function this node replaces
4189
4190     child_attrs = ["loop"]
4191
4192     def analyse_types(self, env):
4193         self.type = self.result_node.type
4194         self.is_temp = True
4195
4196     def coerce_to(self, dst_type, env):
4197         if self.orig_func == 'sum' and dst_type.is_numeric:
4198             # we can optimise by dropping the aggregation variable into C
4199             self.result_node.type = self.type = dst_type
4200             return self
4201         return GeneratorExpressionNode.coerce_to(self, dst_type, env)
4202
4203     def generate_result_code(self, code):
4204         self.result_node.result_code = self.result()
4205         self.loop.generate_execution_code(code)
4206
4207
4208 class SetNode(ExprNode):
4209     #  Set constructor.
4210
4211     type = set_type
4212
4213     subexprs = ['args']
4214
4215     gil_message = "Constructing Python set"
4216     
4217     def analyse_types(self, env):
4218         for i in range(len(self.args)):
4219             arg = self.args[i]
4220             arg.analyse_types(env)
4221             self.args[i] = arg.coerce_to_pyobject(env)
4222         self.type = set_type
4223         self.is_temp = 1
4224
4225     def may_be_none(self):
4226         return False
4227
4228     def calculate_constant_result(self):
4229         self.constant_result = set([
4230                 arg.constant_result for arg in self.args])
4231
4232     def compile_time_value(self, denv):
4233         values = [arg.compile_time_value(denv) for arg in self.args]
4234         try:
4235             return set(values)
4236         except Exception, e:
4237             self.compile_time_value_error(e)
4238
4239     def generate_evaluation_code(self, code):
4240         code.globalstate.use_utility_code(Builtin.py23_set_utility_code)
4241         self.allocate_temp_result(code)
4242         code.putln(
4243             "%s = PySet_New(0); %s" % (
4244                 self.result(),
4245                 code.error_goto_if_null(self.result(), self.pos)))
4246         code.put_gotref(self.py_result())
4247         for arg in self.args:
4248             arg.generate_evaluation_code(code)
4249             code.putln(
4250                 code.error_goto_if_neg(
4251                     "PySet_Add(%s, %s)" % (self.result(), arg.py_result()),
4252                     self.pos))
4253             arg.generate_disposal_code(code)
4254             arg.free_temps(code)
4255
4256
4257 class DictNode(ExprNode):
4258     #  Dictionary constructor.
4259     #
4260     #  key_value_pairs  [DictItemNode]
4261     #
4262     # obj_conversion_errors    [PyrexError]   used internally
4263     
4264     subexprs = ['key_value_pairs']
4265     is_temp = 1
4266     type = dict_type
4267
4268     obj_conversion_errors = []
4269
4270     def calculate_constant_result(self):
4271         self.constant_result = dict([
4272                 item.constant_result for item in self.key_value_pairs])
4273     
4274     def compile_time_value(self, denv):
4275         pairs = [(item.key.compile_time_value(denv), item.value.compile_time_value(denv))
4276             for item in self.key_value_pairs]
4277         try:
4278             return dict(pairs)
4279         except Exception, e:
4280             self.compile_time_value_error(e)
4281     
4282     def type_dependencies(self, env):
4283         return ()
4284     
4285     def infer_type(self, env):
4286         # TOOD: Infer struct constructors.
4287         return dict_type
4288
4289     def analyse_types(self, env):
4290         hold_errors()
4291         for item in self.key_value_pairs:
4292             item.analyse_types(env)
4293         self.obj_conversion_errors = held_errors()
4294         release_errors(ignore=True)
4295
4296     def may_be_none(self):
4297         return False
4298         
4299     def coerce_to(self, dst_type, env):
4300         if dst_type.is_pyobject:
4301             self.release_errors()
4302             if not self.type.subtype_of(dst_type):
4303                 error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
4304         elif dst_type.is_struct_or_union:
4305             self.type = dst_type
4306             if not dst_type.is_struct and len(self.key_value_pairs) != 1:
4307                 error(self.pos, "Exactly one field must be specified to convert to union '%s'" % dst_type)
4308             elif dst_type.is_struct and len(self.key_value_pairs) < len(dst_type.scope.var_entries):
4309                 warning(self.pos, "Not all members given for struct '%s'" % dst_type, 1)
4310             for item in self.key_value_pairs:
4311                 if isinstance(item.key, CoerceToPyTypeNode):
4312                     item.key = item.key.arg
4313                 if not isinstance(item.key, (UnicodeNode, StringNode, BytesNode)):
4314                     error(item.key.pos, "Invalid struct field identifier")
4315                     item.key = StringNode(item.key.pos, value="<error>")
4316                 else:
4317                     key = str(item.key.value) # converts string literals to unicode in Py3
4318                     member = dst_type.scope.lookup_here(key)
4319                     if not member:
4320                         error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, key))
4321                     else:
4322                         value = item.value
4323                         if isinstance(value, CoerceToPyTypeNode):
4324                             value = value.arg
4325                         item.value = value.coerce_to(member.type, env)
4326         else:
4327             self.type = error_type
4328             error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
4329         return self
4330     
4331     def release_errors(self):
4332         for err in self.obj_conversion_errors:
4333             report_error(err)
4334         self.obj_conversion_errors = []
4335
4336     gil_message = "Constructing Python dict"
4337
4338     def generate_evaluation_code(self, code):
4339         #  Custom method used here because key-value
4340         #  pairs are evaluated and used one at a time.
4341         code.mark_pos(self.pos)
4342         self.allocate_temp_result(code)
4343         if self.type.is_pyobject:
4344             self.release_errors()
4345             code.putln(
4346                 "%s = PyDict_New(); %s" % (
4347                     self.result(),
4348                     code.error_goto_if_null(self.result(), self.pos)))
4349             code.put_gotref(self.py_result())
4350         for item in self.key_value_pairs:
4351             item.generate_evaluation_code(code)
4352             if self.type.is_pyobject:
4353                 code.put_error_if_neg(self.pos, 
4354                     "PyDict_SetItem(%s, %s, %s)" % (
4355                         self.result(),
4356                         item.key.py_result(),
4357                         item.value.py_result()))
4358             else:
4359                 code.putln("%s.%s = %s;" % (
4360                         self.result(),
4361                         item.key.value,
4362                         item.value.result()))
4363             item.generate_disposal_code(code)
4364             item.free_temps(code)
4365             
4366     def annotate(self, code):
4367         for item in self.key_value_pairs:
4368             item.annotate(code)
4369             
4370 class DictItemNode(ExprNode):
4371     # Represents a single item in a DictNode
4372     #
4373     # key          ExprNode
4374     # value        ExprNode
4375     subexprs = ['key', 'value']
4376
4377     nogil_check = None # Parent DictNode takes care of it
4378
4379     def calculate_constant_result(self):
4380         self.constant_result = (
4381             self.key.constant_result, self.value.constant_result)
4382             
4383     def analyse_types(self, env):
4384         self.key.analyse_types(env)
4385         self.value.analyse_types(env)
4386         self.key = self.key.coerce_to_pyobject(env)
4387         self.value = self.value.coerce_to_pyobject(env)
4388         
4389     def generate_evaluation_code(self, code):
4390         self.key.generate_evaluation_code(code)
4391         self.value.generate_evaluation_code(code)
4392
4393     def generate_disposal_code(self, code):
4394         self.key.generate_disposal_code(code)
4395         self.value.generate_disposal_code(code)
4396
4397     def free_temps(self, code):
4398         self.key.free_temps(code)
4399         self.value.free_temps(code)
4400         
4401     def __iter__(self):
4402         return iter([self.key, self.value])
4403
4404
4405 class ClassNode(ExprNode):
4406     #  Helper class used in the implementation of Python
4407     #  class definitions. Constructs a class object given
4408     #  a name, tuple of bases and class dictionary.
4409     #
4410     #  name         EncodedString      Name of the class
4411     #  bases        ExprNode           Base class tuple
4412     #  dict         ExprNode           Class dict (not owned by this node)
4413     #  doc          ExprNode or None   Doc string
4414     #  module_name  string             Name of defining module
4415     
4416     subexprs = ['bases', 'doc']
4417
4418     def analyse_types(self, env):
4419         self.bases.analyse_types(env)
4420         if self.doc:
4421             self.doc.analyse_types(env)
4422             self.doc = self.doc.coerce_to_pyobject(env)
4423         self.module_name = env.global_scope().qualified_name
4424         self.type = py_object_type
4425         self.is_temp = 1
4426         env.use_utility_code(create_class_utility_code);
4427
4428     def may_be_none(self):
4429         return False
4430
4431     gil_message = "Constructing Python class"
4432
4433     def generate_result_code(self, code):
4434         cname = code.intern_identifier(self.name)
4435         if self.doc:
4436             code.put_error_if_neg(self.pos, 
4437                 'PyDict_SetItemString(%s, "__doc__", %s)' % (
4438                     self.dict.py_result(),
4439                     self.doc.py_result()))
4440         code.putln(
4441             '%s = __Pyx_CreateClass(%s, %s, %s, "%s"); %s' % (
4442                 self.result(),
4443                 self.bases.py_result(),
4444                 self.dict.py_result(),
4445                 cname,
4446                 self.module_name,
4447                 code.error_goto_if_null(self.result(), self.pos)))
4448         code.put_gotref(self.py_result())
4449
4450 class BoundMethodNode(ExprNode):
4451     #  Helper class used in the implementation of Python
4452     #  class definitions. Constructs an bound method
4453     #  object from a class and a function.
4454     #
4455     #  function      ExprNode   Function object
4456     #  self_object   ExprNode   self object
4457     
4458     subexprs = ['function']
4459     
4460     def analyse_types(self, env):
4461         self.function.analyse_types(env)
4462         self.type = py_object_type
4463         self.is_temp = 1
4464
4465     gil_message = "Constructing an bound method"
4466
4467     def generate_result_code(self, code):
4468         code.putln(
4469             "%s = PyMethod_New(%s, %s, (PyObject*)%s->ob_type); %s" % (
4470                 self.result(),
4471                 self.function.py_result(),
4472                 self.self_object.py_result(),
4473                 self.self_object.py_result(),
4474                 code.error_goto_if_null(self.result(), self.pos)))
4475         code.put_gotref(self.py_result())
4476
4477 class UnboundMethodNode(ExprNode):
4478     #  Helper class used in the implementation of Python
4479     #  class definitions. Constructs an unbound method
4480     #  object from a class and a function.
4481     #
4482     #  function      ExprNode   Function object
4483     
4484     type = py_object_type
4485     is_temp = 1
4486     
4487     subexprs = ['function']
4488     
4489     def analyse_types(self, env):
4490         self.function.analyse_types(env)
4491
4492     def may_be_none(self):
4493         return False
4494
4495     gil_message = "Constructing an unbound method"
4496
4497     def generate_result_code(self, code):
4498         class_cname = code.pyclass_stack[-1].classobj.result()
4499         code.putln(
4500             "%s = PyMethod_New(%s, 0, %s); %s" % (
4501                 self.result(),
4502                 self.function.py_result(),
4503                 class_cname,
4504                 code.error_goto_if_null(self.result(), self.pos)))
4505         code.put_gotref(self.py_result())
4506
4507
4508 class PyCFunctionNode(ExprNode):
4509     #  Helper class used in the implementation of Python
4510     #  class definitions. Constructs a PyCFunction object
4511     #  from a PyMethodDef struct.
4512     #
4513     #  pymethdef_cname   string   PyMethodDef structure
4514     #  self_object       ExprNode or None
4515     #  binding           bool
4516
4517     subexprs = []
4518     self_object = None
4519     binding = False
4520     
4521     type = py_object_type
4522     is_temp = 1
4523     
4524     def analyse_types(self, env):
4525         if self.binding:
4526             env.use_utility_code(binding_cfunc_utility_code)
4527
4528     def may_be_none(self):
4529         return False
4530     
4531     gil_message = "Constructing Python function"
4532
4533     def self_result_code(self):
4534         if self.self_object is None:
4535             self_result = "NULL"
4536         else:
4537             self_result = self.self_object.py_result()
4538         return self_result
4539
4540     def generate_result_code(self, code):
4541         if self.binding:
4542             constructor = "%s_New" % Naming.binding_cfunc
4543         else:
4544             constructor = "PyCFunction_New"
4545         code.putln(
4546             "%s = %s(&%s, %s); %s" % (
4547                 self.result(),
4548                 constructor,
4549                 self.pymethdef_cname,
4550                 self.self_result_code(),
4551                 code.error_goto_if_null(self.result(), self.pos)))
4552         code.put_gotref(self.py_result())
4553
4554 class InnerFunctionNode(PyCFunctionNode):
4555     # Special PyCFunctionNode that depends on a closure class
4556     #
4557     binding = True
4558     
4559     def self_result_code(self):
4560         return "((PyObject*)%s)" % Naming.cur_scope_cname
4561
4562 class LambdaNode(InnerFunctionNode):
4563     # Lambda expression node (only used as a function reference)
4564     #
4565     # args          [CArgDeclNode]         formal arguments
4566     # star_arg      PyArgDeclNode or None  * argument
4567     # starstar_arg  PyArgDeclNode or None  ** argument
4568     # lambda_name   string                 a module-globally unique lambda name
4569     # result_expr   ExprNode
4570     # def_node      DefNode                the underlying function 'def' node
4571
4572     child_attrs = ['def_node']
4573
4574     def_node = None
4575     name = StringEncoding.EncodedString('<lambda>')
4576
4577     def analyse_declarations(self, env):
4578         #self.def_node.needs_closure = self.needs_closure
4579         self.def_node.analyse_declarations(env)
4580         self.pymethdef_cname = self.def_node.entry.pymethdef_cname
4581         env.add_lambda_def(self.def_node)
4582
4583 class YieldExprNode(ExprNode):
4584     # Yield expression node
4585     #
4586     # arg         ExprNode   the value to return from the generator
4587     # label_name  string     name of the C label used for this yield
4588
4589     subexprs = ['arg']
4590     type = py_object_type
4591
4592     def analyse_types(self, env):
4593         self.is_temp = 1
4594         if self.arg is not None:
4595             self.arg.analyse_types(env)
4596             if not self.arg.type.is_pyobject:
4597                 self.arg = self.arg.coerce_to_pyobject(env)
4598         error(self.pos, "Generators are not supported")
4599
4600     def generate_result_code(self, code):
4601         self.label_name = code.new_label('resume_from_yield')
4602         code.use_label(self.label_name)
4603         code.putln("/* FIXME: save temporary variables */")
4604         code.putln("/* FIXME: return from function, yielding value */")
4605         code.put_label(self.label_name)
4606         code.putln("/* FIXME: restore temporary variables and  */")
4607         code.putln("/* FIXME: extract sent value from closure */")
4608
4609
4610 #-------------------------------------------------------------------
4611 #
4612 #  Unary operator nodes
4613 #
4614 #-------------------------------------------------------------------
4615
4616 compile_time_unary_operators = {
4617     'not': operator.not_,
4618     '~': operator.inv,
4619     '-': operator.neg,
4620     '+': operator.pos,
4621 }
4622
4623 class UnopNode(ExprNode):
4624     #  operator     string
4625     #  operand      ExprNode
4626     #
4627     #  Processing during analyse_expressions phase:
4628     #
4629     #    analyse_c_operation
4630     #      Called when the operand is not a pyobject.
4631     #      - Check operand type and coerce if needed.
4632     #      - Determine result type and result code fragment.
4633     #      - Allocate temporary for result if needed.
4634     
4635     subexprs = ['operand']
4636     infix = True
4637
4638     def calculate_constant_result(self):
4639         func = compile_time_unary_operators[self.operator]
4640         self.constant_result = func(self.operand.constant_result)
4641     
4642     def compile_time_value(self, denv):
4643         func = compile_time_unary_operators.get(self.operator)
4644         if not func:
4645             error(self.pos,
4646                 "Unary '%s' not supported in compile-time expression"
4647                     % self.operator)
4648         operand = self.operand.compile_time_value(denv)
4649         try:
4650             return func(operand)
4651         except Exception, e:
4652             self.compile_time_value_error(e)
4653     
4654     def infer_type(self, env):
4655         operand_type = self.operand.infer_type(env)
4656         if operand_type.is_pyobject:
4657             return py_object_type
4658         else:
4659             return operand_type
4660
4661     def analyse_types(self, env):
4662         self.operand.analyse_types(env)
4663         if self.is_py_operation():
4664             self.coerce_operand_to_pyobject(env)
4665             self.type = py_object_type
4666             self.is_temp = 1
4667         elif self.is_cpp_operation():
4668             self.analyse_cpp_operation(env)
4669         else:
4670             self.analyse_c_operation(env)
4671     
4672     def check_const(self):
4673         return self.operand.check_const()
4674     
4675     def is_py_operation(self):
4676         return self.operand.type.is_pyobject
4677
4678     def nogil_check(self, env):
4679         if self.is_py_operation():
4680             self.gil_error()
4681
4682     def is_cpp_operation(self):
4683         type = self.operand.type
4684         return type.is_cpp_class
4685     
4686     def coerce_operand_to_pyobject(self, env):
4687         self.operand = self.operand.coerce_to_pyobject(env)
4688     
4689     def generate_result_code(self, code):
4690         if self.operand.type.is_pyobject:
4691             self.generate_py_operation_code(code)
4692     
4693     def generate_py_operation_code(self, code):
4694         function = self.py_operation_function()
4695         code.putln(
4696             "%s = %s(%s); %s" % (
4697                 self.result(), 
4698                 function, 
4699                 self.operand.py_result(),
4700                 code.error_goto_if_null(self.result(), self.pos)))
4701         code.put_gotref(self.py_result())
4702         
4703     def type_error(self):
4704         if not self.operand.type.is_error:
4705             error(self.pos, "Invalid operand type for '%s' (%s)" %
4706                 (self.operator, self.operand.type))
4707         self.type = PyrexTypes.error_type
4708
4709     def analyse_cpp_operation(self, env):
4710         type = self.operand.type
4711         if type.is_ptr:
4712             type = type.base_type
4713         function = type.scope.lookup("operator%s" % self.operator)
4714         if not function:
4715             error(self.pos, "'%s' operator not defined for %s"
4716                 % (self.operator, type))
4717             self.type_error()
4718             return
4719         func_type = function.type
4720         if func_type.is_ptr:
4721             func_type = func_type.base_type
4722         self.type = func_type.return_type
4723
4724
4725 class NotNode(ExprNode):
4726     #  'not' operator
4727     #
4728     #  operand   ExprNode
4729     
4730     type = PyrexTypes.c_bint_type
4731
4732     subexprs = ['operand']
4733     
4734     def calculate_constant_result(self):
4735         self.constant_result = not self.operand.constant_result
4736
4737     def compile_time_value(self, denv):
4738         operand = self.operand.compile_time_value(denv)
4739         try:
4740             return not operand
4741         except Exception, e:
4742             self.compile_time_value_error(e)
4743
4744     def infer_type(self, env):
4745         return PyrexTypes.c_bint_type
4746     
4747     def analyse_types(self, env):
4748         self.operand.analyse_types(env)
4749         self.operand = self.operand.coerce_to_boolean(env)
4750     
4751     def calculate_result_code(self):
4752         return "(!%s)" % self.operand.result()
4753     
4754     def generate_result_code(self, code):
4755         pass
4756
4757
4758 class UnaryPlusNode(UnopNode):
4759     #  unary '+' operator
4760     
4761     operator = '+'
4762     
4763     def analyse_c_operation(self, env):
4764         self.type = self.operand.type
4765     
4766     def py_operation_function(self):
4767         return "PyNumber_Positive"
4768     
4769     def calculate_result_code(self):
4770         if self.is_cpp_operation():
4771             return "(+%s)" % self.operand.result()
4772         else:
4773             return self.operand.result()
4774
4775
4776 class UnaryMinusNode(UnopNode):
4777     #  unary '-' operator
4778     
4779     operator = '-'
4780     
4781     def analyse_c_operation(self, env):
4782         if self.operand.type.is_numeric:
4783             self.type = self.operand.type
4784         else:
4785             self.type_error()
4786         if self.type.is_complex:
4787             self.infix = False
4788     
4789     def py_operation_function(self):
4790         return "PyNumber_Negative"
4791     
4792     def calculate_result_code(self):
4793         if self.infix:
4794             return "(-%s)" % self.operand.result()
4795         else:
4796             return "%s(%s)" % (self.operand.type.unary_op('-'), self.operand.result())
4797
4798     def get_constant_c_result_code(self):
4799         value = self.operand.get_constant_c_result_code()
4800         if value:
4801             return "(-%s)" % (value)
4802
4803 class TildeNode(UnopNode):
4804     #  unary '~' operator
4805
4806     def analyse_c_operation(self, env):
4807         if self.operand.type.is_int:
4808             self.type = self.operand.type
4809         else:
4810             self.type_error()
4811
4812     def py_operation_function(self):
4813         return "PyNumber_Invert"
4814     
4815     def calculate_result_code(self):
4816         return "(~%s)" % self.operand.result()
4817
4818
4819 class CUnopNode(UnopNode):
4820
4821     def is_py_operation(self):
4822         return False
4823
4824 class DereferenceNode(CUnopNode):
4825     #  unary * operator
4826
4827     operator = '*'
4828     
4829     def analyse_c_operation(self, env):
4830         if self.operand.type.is_ptr:
4831             self.type = self.operand.type.base_type
4832         else:
4833             self.type_error()
4834
4835     def calculate_result_code(self):
4836         return "(*%s)" % self.operand.result()
4837
4838
4839 class DecrementIncrementNode(CUnopNode):
4840     #  unary ++/-- operator
4841     
4842     def analyse_c_operation(self, env):
4843         if self.operand.type.is_ptr or self.operand.type.is_numeric:
4844             self.type = self.operand.type
4845         else:
4846             self.type_error()
4847
4848     def calculate_result_code(self):
4849         if self.is_prefix:
4850             return "(%s%s)" % (self.operator, self.operand.result())
4851         else:
4852             return "(%s%s)" % (self.operand.result(), self.operator)
4853
4854 def inc_dec_constructor(is_prefix, operator):
4855     return lambda pos, **kwds: DecrementIncrementNode(pos, is_prefix=is_prefix, operator=operator, **kwds)
4856
4857
4858 class AmpersandNode(ExprNode):
4859     #  The C address-of operator.
4860     #
4861     #  operand  ExprNode
4862     
4863     subexprs = ['operand']
4864     
4865     def infer_type(self, env):
4866         return PyrexTypes.c_ptr_type(self.operand.infer_type(env))
4867
4868     def analyse_types(self, env):
4869         self.operand.analyse_types(env)
4870         argtype = self.operand.type
4871         if not (argtype.is_cfunction or self.operand.is_lvalue()):
4872             self.error("Taking address of non-lvalue")
4873             return
4874         if argtype.is_pyobject:
4875             self.error("Cannot take address of Python variable")
4876             return
4877         self.type = PyrexTypes.c_ptr_type(argtype)
4878     
4879     def check_const(self):
4880         return self.operand.check_const_addr()
4881     
4882     def error(self, mess):
4883         error(self.pos, mess)
4884         self.type = PyrexTypes.error_type
4885         self.result_code = "<error>"
4886     
4887     def calculate_result_code(self):
4888         return "(&%s)" % self.operand.result()
4889
4890     def generate_result_code(self, code):
4891         pass
4892     
4893
4894 unop_node_classes = {
4895     "+":  UnaryPlusNode,
4896     "-":  UnaryMinusNode,
4897     "~":  TildeNode,
4898 }
4899
4900 def unop_node(pos, operator, operand):
4901     # Construct unnop node of appropriate class for 
4902     # given operator.
4903     if isinstance(operand, IntNode) and operator == '-':
4904         return IntNode(pos = operand.pos, value = str(-int(operand.value, 0)))
4905     elif isinstance(operand, UnopNode) and operand.operator == operator:
4906         warning(pos, "Python has no increment/decrement operator: %s%sx = %s(%sx) = x" % ((operator,)*4), 5)
4907     return unop_node_classes[operator](pos, 
4908         operator = operator, 
4909         operand = operand)
4910
4911
4912 class TypecastNode(ExprNode):
4913     #  C type cast
4914     #
4915     #  operand      ExprNode
4916     #  base_type    CBaseTypeNode
4917     #  declarator   CDeclaratorNode
4918     #
4919     #  If used from a transform, one can if wanted specify the attribute
4920     #  "type" directly and leave base_type and declarator to None
4921     
4922     subexprs = ['operand']
4923     base_type = declarator = type = None
4924     
4925     def type_dependencies(self, env):
4926         return ()
4927     
4928     def infer_type(self, env):
4929         if self.type is None:
4930             base_type = self.base_type.analyse(env)
4931             _, self.type = self.declarator.analyse(base_type, env)
4932         return self.type
4933     
4934     def analyse_types(self, env):
4935         if self.type is None:
4936             base_type = self.base_type.analyse(env)
4937             _, self.type = self.declarator.analyse(base_type, env)
4938         if self.type.is_cfunction:
4939             error(self.pos,
4940                 "Cannot cast to a function type")
4941             self.type = PyrexTypes.error_type
4942         self.operand.analyse_types(env)
4943         to_py = self.type.is_pyobject
4944         from_py = self.operand.type.is_pyobject
4945         if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
4946             error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
4947         if to_py and not from_py:
4948             if self.type is bytes_type and self.operand.type.is_int:
4949                 # FIXME: the type cast node isn't needed in this case
4950                 # and can be dropped once analyse_types() can return a
4951                 # different node
4952                 self.operand = CoerceIntToBytesNode(self.operand, env)
4953             elif self.operand.type.can_coerce_to_pyobject(env):
4954                 self.result_ctype = py_object_type
4955                 self.operand = self.operand.coerce_to_pyobject(env)
4956             else:
4957                 if self.operand.type.is_ptr:
4958                     if not (self.operand.type.base_type.is_void or self.operand.type.base_type.is_struct):
4959                         error(self.pos, "Python objects cannot be cast from pointers of primitive types")
4960                 else:
4961                     # Should this be an error? 
4962                     warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.operand.type, self.type))
4963                 self.operand = self.operand.coerce_to_simple(env)
4964         elif from_py and not to_py:
4965             if self.type.create_from_py_utility_code(env):
4966                 self.operand = self.operand.coerce_to(self.type, env)
4967             elif self.type.is_ptr:
4968                 if not (self.type.base_type.is_void or self.type.base_type.is_struct):
4969                     error(self.pos, "Python objects cannot be cast to pointers of primitive types")
4970             else:
4971                 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
4972         elif from_py and to_py:
4973             if self.typecheck and self.type.is_extension_type:
4974                 self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True)
4975         elif self.type.is_complex and self.operand.type.is_complex:
4976             self.operand = self.operand.coerce_to_simple(env)
4977
4978     def nogil_check(self, env):
4979         if self.type and self.type.is_pyobject and self.is_temp:
4980             self.gil_error()
4981
4982     def check_const(self):
4983         return self.operand.check_const()
4984
4985     def calculate_constant_result(self):
4986         # we usually do not know the result of a type cast at code
4987         # generation time
4988         pass
4989     
4990     def calculate_result_code(self):
4991         if self.type.is_complex:
4992             operand_result = self.operand.result()
4993             if self.operand.type.is_complex:
4994                 real_part = self.type.real_type.cast_code("__Pyx_CREAL(%s)" % operand_result)
4995                 imag_part = self.type.real_type.cast_code("__Pyx_CIMAG(%s)" % operand_result)
4996             else:
4997                 real_part = self.type.real_type.cast_code(operand_result)
4998                 imag_part = "0"
4999             return "%s(%s, %s)" % (
5000                     self.type.from_parts,
5001                     real_part,
5002                     imag_part)    
5003         else:
5004             return self.type.cast_code(self.operand.result())
5005     
5006     def get_constant_c_result_code(self):
5007         operand_result = self.operand.get_constant_c_result_code()
5008         if operand_result:
5009             return self.type.cast_code(operand_result)
5010     
5011     def result_as(self, type):
5012         if self.type.is_pyobject and not self.is_temp:
5013             #  Optimise away some unnecessary casting
5014             return self.operand.result_as(type)
5015         else:
5016             return ExprNode.result_as(self, type)
5017
5018     def generate_result_code(self, code):
5019         if self.is_temp:
5020             code.putln(
5021                 "%s = (PyObject *)%s;" % (
5022                     self.result(),
5023                     self.operand.result()))
5024             code.put_incref(self.result(), self.ctype())
5025
5026
5027 class SizeofNode(ExprNode):
5028     #  Abstract base class for sizeof(x) expression nodes.
5029     
5030     type = PyrexTypes.c_size_t_type
5031
5032     def check_const(self):
5033         return True
5034
5035     def generate_result_code(self, code):
5036         pass
5037
5038
5039 class SizeofTypeNode(SizeofNode):
5040     #  C sizeof function applied to a type
5041     #
5042     #  base_type   CBaseTypeNode
5043     #  declarator  CDeclaratorNode
5044     
5045     subexprs = []
5046     arg_type = None
5047     
5048     def analyse_types(self, env):
5049         # we may have incorrectly interpreted a dotted name as a type rather than an attribute
5050         # this could be better handled by more uniformly treating types as runtime-available objects
5051         if 0 and self.base_type.module_path:
5052             path = self.base_type.module_path
5053             obj = env.lookup(path[0])
5054             if obj.as_module is None:
5055                 operand = NameNode(pos=self.pos, name=path[0])
5056                 for attr in path[1:]:
5057                     operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr)
5058                 operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name)
5059                 self.operand = operand
5060                 self.__class__ = SizeofVarNode
5061                 self.analyse_types(env)
5062                 return
5063         if self.arg_type is None:
5064             base_type = self.base_type.analyse(env)
5065             _, arg_type = self.declarator.analyse(base_type, env)
5066             self.arg_type = arg_type
5067         self.check_type()
5068         
5069     def check_type(self):
5070         arg_type = self.arg_type
5071         if arg_type.is_pyobject and not arg_type.is_extension_type:
5072             error(self.pos, "Cannot take sizeof Python object")
5073         elif arg_type.is_void:
5074             error(self.pos, "Cannot take sizeof void")
5075         elif not arg_type.is_complete():
5076             error(self.pos, "Cannot take sizeof incomplete type '%s'" % arg_type)
5077         
5078     def calculate_result_code(self):
5079         if self.arg_type.is_extension_type:
5080             # the size of the pointer is boring
5081             # we want the size of the actual struct
5082             arg_code = self.arg_type.declaration_code("", deref=1)
5083         else:
5084             arg_code = self.arg_type.declaration_code("")
5085         return "(sizeof(%s))" % arg_code
5086     
5087
5088 class SizeofVarNode(SizeofNode):
5089     #  C sizeof function applied to a variable
5090     #
5091     #  operand   ExprNode
5092     
5093     subexprs = ['operand']
5094     
5095     def analyse_types(self, env):
5096         # We may actually be looking at a type rather than a variable...
5097         # If we are, traditional analysis would fail...
5098         operand_as_type = self.operand.analyse_as_type(env)
5099         if operand_as_type:
5100             self.arg_type = operand_as_type
5101             self.__class__ = SizeofTypeNode
5102             self.check_type()
5103         else:
5104             self.operand.analyse_types(env)
5105     
5106     def calculate_result_code(self):
5107         return "(sizeof(%s))" % self.operand.result()
5108     
5109     def generate_result_code(self, code):
5110         pass
5111
5112 class TypeofNode(ExprNode):
5113     #  Compile-time type of an expression, as a string.
5114     #
5115     #  operand   ExprNode
5116     #  literal   StringNode # internal
5117     
5118     literal = None
5119     type = py_object_type
5120     
5121     subexprs = ['literal'] # 'operand' will be ignored after type analysis!
5122     
5123     def analyse_types(self, env):
5124         self.operand.analyse_types(env)
5125         self.literal = StringNode(
5126             self.pos, value=StringEncoding.EncodedString(str(self.operand.type)))
5127         self.literal.analyse_types(env)
5128         self.literal = self.literal.coerce_to_pyobject(env)
5129
5130     def may_be_none(self):
5131         return False
5132
5133     def generate_evaluation_code(self, code):
5134         self.literal.generate_evaluation_code(code)
5135     
5136     def calculate_result_code(self):
5137         return self.literal.calculate_result_code()
5138
5139 #-------------------------------------------------------------------
5140 #
5141 #  Binary operator nodes
5142 #
5143 #-------------------------------------------------------------------
5144
5145 def _not_in(x, seq):
5146     return x not in seq
5147
5148 compile_time_binary_operators = {
5149     '<': operator.lt,
5150     '<=': operator.le,
5151     '==': operator.eq,
5152     '!=': operator.ne,
5153     '>=': operator.ge,
5154     '>': operator.gt,
5155     'is': operator.is_,
5156     'is_not': operator.is_not,
5157     '+': operator.add,
5158     '&': operator.and_,
5159     '/': operator.truediv,
5160     '//': operator.floordiv,
5161     '<<': operator.lshift,
5162     '%': operator.mod,
5163     '*': operator.mul,
5164     '|': operator.or_,
5165     '**': operator.pow,
5166     '>>': operator.rshift,
5167     '-': operator.sub,
5168     '^': operator.xor,
5169     'in': operator.contains,
5170     'not_in': _not_in,
5171 }
5172
5173 def get_compile_time_binop(node):
5174     func = compile_time_binary_operators.get(node.operator)
5175     if not func:
5176         error(node.pos,
5177             "Binary '%s' not supported in compile-time expression"
5178                 % node.operator)
5179     return func
5180
5181 class BinopNode(ExprNode):
5182     #  operator     string
5183     #  operand1     ExprNode
5184     #  operand2     ExprNode
5185     #
5186     #  Processing during analyse_expressions phase:
5187     #
5188     #    analyse_c_operation
5189     #      Called when neither operand is a pyobject.
5190     #      - Check operand types and coerce if needed.
5191     #      - Determine result type and result code fragment.
5192     #      - Allocate temporary for result if needed.
5193     
5194     subexprs = ['operand1', 'operand2']
5195
5196     def calculate_constant_result(self):
5197         func = compile_time_binary_operators[self.operator]
5198         self.constant_result = func(
5199             self.operand1.constant_result,
5200             self.operand2.constant_result)
5201
5202     def compile_time_value(self, denv):
5203         func = get_compile_time_binop(self)
5204         operand1 = self.operand1.compile_time_value(denv)
5205         operand2 = self.operand2.compile_time_value(denv)
5206         try:
5207             return func(operand1, operand2)
5208         except Exception, e:
5209             self.compile_time_value_error(e)
5210     
5211     def infer_type(self, env):
5212         return self.result_type(self.operand1.infer_type(env),
5213                                 self.operand2.infer_type(env))
5214     
5215     def analyse_types(self, env):
5216         self.operand1.analyse_types(env)
5217         self.operand2.analyse_types(env)
5218         if self.is_py_operation():
5219             self.coerce_operands_to_pyobjects(env)
5220             self.type = self.result_type(self.operand1.type,
5221                                          self.operand2.type)
5222             assert self.type.is_pyobject
5223             self.is_temp = 1
5224         elif self.is_cpp_operation():
5225             self.analyse_cpp_operation(env)
5226         else:
5227             self.analyse_c_operation(env)
5228     
5229     def is_py_operation(self):
5230         return self.is_py_operation_types(self.operand1.type, self.operand2.type)
5231     
5232     def is_py_operation_types(self, type1, type2):
5233         return type1.is_pyobject or type2.is_pyobject
5234
5235     def is_cpp_operation(self):
5236         return (self.operand1.type.is_cpp_class
5237             or self.operand2.type.is_cpp_class)
5238     
5239     def analyse_cpp_operation(self, env):
5240         type1 = self.operand1.type
5241         type2 = self.operand2.type
5242         entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
5243         if not entry:
5244             self.type_error()
5245             return
5246         func_type = entry.type
5247         if func_type.is_ptr:
5248             func_type = func_type.base_type
5249         if len(func_type.args) == 1:
5250             self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
5251         else:
5252             self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
5253             self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
5254         self.type = func_type.return_type
5255     
5256     def result_type(self, type1, type2):
5257         if self.is_py_operation_types(type1, type2):
5258             if type2.is_string:
5259                 type2 = Builtin.bytes_type
5260             if type1.is_string:
5261                 type1 = Builtin.bytes_type
5262             elif self.operator == '%' \
5263                      and type1 in (Builtin.str_type, Builtin.unicode_type):
5264                 # note that  b'%s' % b'abc'  doesn't work in Py3
5265                 return type1
5266             if type1.is_builtin_type:
5267                 if type1 is type2:
5268                     if self.operator in '**%+|&^':
5269                         # FIXME: at least these operators should be safe - others?
5270                         return type1
5271                 elif self.operator == '*':
5272                     if type1 in (Builtin.bytes_type, Builtin.str_type, Builtin.unicode_type):
5273                         return type1
5274                     # multiplication of containers/numbers with an
5275                     # integer value always (?) returns the same type
5276                     if type2.is_int:
5277                         return type1
5278             elif type2.is_builtin_type and type1.is_int and self.operator == '*':
5279                 # multiplication of containers/numbers with an
5280                 # integer value always (?) returns the same type
5281                 return type2
5282             return py_object_type
5283         else:
5284             return self.compute_c_result_type(type1, type2)
5285
5286     def nogil_check(self, env):
5287         if self.is_py_operation():
5288             self.gil_error()
5289         
5290     def coerce_operands_to_pyobjects(self, env):
5291         self.operand1 = self.operand1.coerce_to_pyobject(env)
5292         self.operand2 = self.operand2.coerce_to_pyobject(env)
5293     
5294     def check_const(self):
5295         return self.operand1.check_const() and self.operand2.check_const()
5296     
5297     def generate_result_code(self, code):
5298         #print "BinopNode.generate_result_code:", self.operand1, self.operand2 ###
5299         if self.operand1.type.is_pyobject:
5300             function = self.py_operation_function()
5301             if function == "PyNumber_Power":
5302                 extra_args = ", Py_None"
5303             else:
5304                 extra_args = ""
5305             code.putln(
5306                 "%s = %s(%s, %s%s); %s" % (
5307                     self.result(), 
5308                     function, 
5309                     self.operand1.py_result(),
5310                     self.operand2.py_result(),
5311                     extra_args,
5312                     code.error_goto_if_null(self.result(), self.pos)))
5313             code.put_gotref(self.py_result())
5314     
5315     def type_error(self):
5316         if not (self.operand1.type.is_error
5317                 or self.operand2.type.is_error):
5318             error(self.pos, "Invalid operand types for '%s' (%s; %s)" %
5319                 (self.operator, self.operand1.type, 
5320                     self.operand2.type))
5321         self.type = PyrexTypes.error_type
5322
5323
5324 class CBinopNode(BinopNode):
5325     
5326     def analyse_types(self, env):
5327         BinopNode.analyse_types(self, env)
5328         if self.is_py_operation():
5329             self.type = PyrexTypes.error_type
5330     
5331     def py_operation_function():
5332         return ""
5333         
5334     def calculate_result_code(self):
5335         return "(%s %s %s)" % (
5336             self.operand1.result(), 
5337             self.operator, 
5338             self.operand2.result())
5339
5340
5341 def c_binop_constructor(operator):
5342     def make_binop_node(pos, **operands):
5343         return CBinopNode(pos, operator=operator, **operands)
5344     return make_binop_node
5345
5346 class NumBinopNode(BinopNode):
5347     #  Binary operation taking numeric arguments.
5348     
5349     infix = True
5350     
5351     def analyse_c_operation(self, env):
5352         type1 = self.operand1.type
5353         type2 = self.operand2.type
5354         self.type = self.compute_c_result_type(type1, type2)
5355         if not self.type:
5356             self.type_error()
5357             return
5358         if self.type.is_complex:
5359             self.infix = False
5360         if not self.infix or (type1.is_numeric and type2.is_numeric):
5361             self.operand1 = self.operand1.coerce_to(self.type, env)
5362             self.operand2 = self.operand2.coerce_to(self.type, env)
5363     
5364     def compute_c_result_type(self, type1, type2):
5365         if self.c_types_okay(type1, type2):
5366             return PyrexTypes.widest_numeric_type(type1, type2)
5367         else:
5368             return None
5369
5370     def get_constant_c_result_code(self):
5371         value1 = self.operand1.get_constant_c_result_code()
5372         value2 = self.operand2.get_constant_c_result_code()
5373         if value1 and value2:
5374             return "(%s %s %s)" % (value1, self.operator, value2)
5375         else:
5376             return None
5377     
5378     def c_types_okay(self, type1, type2):
5379         #print "NumBinopNode.c_types_okay:", type1, type2 ###
5380         return (type1.is_numeric  or type1.is_enum) \
5381             and (type2.is_numeric  or type2.is_enum)
5382
5383     def calculate_result_code(self):
5384         if self.infix:
5385             return "(%s %s %s)" % (
5386                 self.operand1.result(), 
5387                 self.operator, 
5388                 self.operand2.result())
5389         else:
5390             func = self.type.binary_op(self.operator)
5391             if func is None:
5392                 error(self.pos, "binary operator %s not supported for %s" % (self.operator, self.type))
5393             return "%s(%s, %s)" % (
5394                 func,
5395                 self.operand1.result(),
5396                 self.operand2.result())
5397     
5398     def is_py_operation_types(self, type1, type2):
5399         return (type1 is PyrexTypes.c_py_unicode_type or
5400                 type2 is PyrexTypes.c_py_unicode_type or
5401                 BinopNode.is_py_operation_types(self, type1, type2))
5402     
5403     def py_operation_function(self):
5404         return self.py_functions[self.operator]
5405
5406     py_functions = {
5407         "|":        "PyNumber_Or",
5408         "^":        "PyNumber_Xor",
5409         "&":        "PyNumber_And",
5410         "<<":       "PyNumber_Lshift",
5411         ">>":       "PyNumber_Rshift",
5412         "+":        "PyNumber_Add",
5413         "-":        "PyNumber_Subtract",
5414         "*":        "PyNumber_Multiply",
5415         "/":        "__Pyx_PyNumber_Divide",
5416         "//":       "PyNumber_FloorDivide",
5417         "%":        "PyNumber_Remainder",
5418         "**":       "PyNumber_Power"
5419     }
5420
5421
5422 class IntBinopNode(NumBinopNode):
5423     #  Binary operation taking integer arguments.
5424     
5425     def c_types_okay(self, type1, type2):
5426         #print "IntBinopNode.c_types_okay:", type1, type2 ###
5427         return (type1.is_int or type1.is_enum) \
5428             and (type2.is_int or type2.is_enum)
5429
5430     
5431 class AddNode(NumBinopNode):
5432     #  '+' operator.
5433     
5434     def is_py_operation_types(self, type1, type2):
5435         if type1.is_string and type2.is_string:
5436             return 1
5437         else:
5438             return NumBinopNode.is_py_operation_types(self, type1, type2)
5439
5440     def compute_c_result_type(self, type1, type2):
5441         #print "AddNode.compute_c_result_type:", type1, self.operator, type2 ###
5442         if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
5443             return type1
5444         elif (type2.is_ptr or type2.is_array) and (type1.is_int or type1.is_enum):
5445             return type2
5446         else:
5447             return NumBinopNode.compute_c_result_type(
5448                 self, type1, type2)
5449
5450
5451 class SubNode(NumBinopNode):
5452     #  '-' operator.
5453     
5454     def compute_c_result_type(self, type1, type2):
5455         if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
5456             return type1
5457         elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
5458             return PyrexTypes.c_int_type
5459         else:
5460             return NumBinopNode.compute_c_result_type(
5461                 self, type1, type2)
5462
5463
5464 class MulNode(NumBinopNode):
5465     #  '*' operator.
5466     
5467     def is_py_operation_types(self, type1, type2):
5468         if (type1.is_string and type2.is_int) \
5469             or (type2.is_string and type1.is_int):
5470                 return 1
5471         else:
5472             return NumBinopNode.is_py_operation_types(self, type1, type2)
5473
5474
5475 class DivNode(NumBinopNode):
5476     #  '/' or '//' operator.
5477     
5478     cdivision = None
5479     truedivision = None   # == "unknown" if operator == '/'
5480     ctruedivision = False
5481     cdivision_warnings = False
5482     zerodivision_check = None
5483
5484     def find_compile_time_binary_operator(self, op1, op2):
5485         func = compile_time_binary_operators[self.operator]
5486         if self.operator == '/' and self.truedivision is None:
5487             # => true div for floats, floor div for integers
5488             if isinstance(op1, (int,long)) and isinstance(op2, (int,long)):
5489                 func = compile_time_binary_operators['//']
5490         return func
5491
5492     def calculate_constant_result(self):
5493         op1 = self.operand1.constant_result
5494         op2 = self.operand2.constant_result
5495         func = self.find_compile_time_binary_operator(op1, op2)
5496         self.constant_result = func(
5497             self.operand1.constant_result,
5498             self.operand2.constant_result)
5499
5500     def compile_time_value(self, denv):
5501         operand1 = self.operand1.compile_time_value(denv)
5502         operand2 = self.operand2.compile_time_value(denv)
5503         try:
5504             func = self.find_compile_time_binary_operator(
5505                 self, operand1, operand2)
5506             return func(operand1, operand2)
5507         except Exception, e:
5508             self.compile_time_value_error(e)
5509
5510     def analyse_types(self, env):
5511         if self.cdivision or env.directives['cdivision']:
5512             self.ctruedivision = False
5513         else:
5514             self.ctruedivision = self.truedivision
5515         NumBinopNode.analyse_types(self, env)
5516         if self.is_cpp_operation():
5517             self.cdivision = True
5518         if not self.type.is_pyobject:
5519             self.zerodivision_check = (
5520                 self.cdivision is None and not env.directives['cdivision']
5521                 and (not self.operand2.has_constant_result() or
5522                      self.operand2.constant_result == 0))
5523             if self.zerodivision_check or env.directives['cdivision_warnings']:
5524                 # Need to check ahead of time to warn or raise zero division error
5525                 self.operand1 = self.operand1.coerce_to_simple(env)
5526                 self.operand2 = self.operand2.coerce_to_simple(env)
5527                 if env.nogil:
5528                     error(self.pos, "Pythonic division not allowed without gil, consider using cython.cdivision(True)")
5529
5530     def compute_c_result_type(self, type1, type2):
5531         if self.operator == '/' and self.ctruedivision:
5532             if not type1.is_float and not type2.is_float:
5533                 widest_type = PyrexTypes.widest_numeric_type(type1, PyrexTypes.c_double_type)
5534                 widest_type = PyrexTypes.widest_numeric_type(type2, widest_type)
5535                 return widest_type
5536         return NumBinopNode.compute_c_result_type(self, type1, type2)
5537
5538     def zero_division_message(self):
5539         if self.type.is_int:
5540             return "integer division or modulo by zero"
5541         else:
5542             return "float division"
5543
5544     def generate_evaluation_code(self, code):
5545         if not self.type.is_pyobject and not self.type.is_complex:
5546             if self.cdivision is None:
5547                 self.cdivision = (code.globalstate.directives['cdivision'] 
5548                                     or not self.type.signed
5549                                     or self.type.is_float)
5550             if not self.cdivision:
5551                 code.globalstate.use_utility_code(div_int_utility_code.specialize(self.type))
5552         NumBinopNode.generate_evaluation_code(self, code)
5553         self.generate_div_warning_code(code)
5554     
5555     def generate_div_warning_code(self, code):
5556         if not self.type.is_pyobject:
5557             if self.zerodivision_check:
5558                 if not self.infix:
5559                     zero_test = "%s(%s)" % (self.type.unary_op('zero'), self.operand2.result())
5560                 else:
5561                     zero_test = "%s == 0" % self.operand2.result()
5562                 code.putln("if (unlikely(%s)) {" % zero_test)
5563                 code.putln('PyErr_Format(PyExc_ZeroDivisionError, "%s");' % self.zero_division_message())
5564                 code.putln(code.error_goto(self.pos))
5565                 code.putln("}")
5566                 if self.type.is_int and self.type.signed and self.operator != '%':
5567                     code.globalstate.use_utility_code(division_overflow_test_code)
5568                     code.putln("else if (sizeof(%s) == sizeof(long) && unlikely(%s == -1) && unlikely(UNARY_NEG_WOULD_OVERFLOW(%s))) {" % (
5569                                     self.type.declaration_code(''), 
5570                                     self.operand2.result(),
5571                                     self.operand1.result()))
5572                     code.putln('PyErr_Format(PyExc_OverflowError, "value too large to perform division");')
5573                     code.putln(code.error_goto(self.pos))
5574                     code.putln("}")
5575             if code.globalstate.directives['cdivision_warnings'] and self.operator != '/':
5576                 code.globalstate.use_utility_code(cdivision_warning_utility_code)
5577                 code.putln("if ((%s < 0) ^ (%s < 0)) {" % (
5578                                 self.operand1.result(),
5579                                 self.operand2.result()))
5580                 code.putln(code.set_error_info(self.pos));
5581                 code.put("if (__Pyx_cdivision_warning()) ")
5582                 code.put_goto(code.error_label)
5583                 code.putln("}")
5584     
5585     def calculate_result_code(self):
5586         if self.type.is_complex:
5587             return NumBinopNode.calculate_result_code(self)
5588         elif self.type.is_float and self.operator == '//':
5589             return "floor(%s / %s)" % (
5590                 self.operand1.result(),
5591                 self.operand2.result())
5592         elif self.truedivision or self.cdivision:
5593             op1 = self.operand1.result()
5594             op2 = self.operand2.result()
5595             if self.truedivision:
5596                 if self.type != self.operand1.type:
5597                     op1 = self.type.cast_code(op1)
5598                 if self.type != self.operand2.type:
5599                     op2 = self.type.cast_code(op2)
5600             return "(%s / %s)" % (op1, op2)
5601         else:
5602             return "__Pyx_div_%s(%s, %s)" % (
5603                     self.type.specialization_name(),
5604                     self.operand1.result(), 
5605                     self.operand2.result())
5606
5607
5608 class ModNode(DivNode):
5609     #  '%' operator.
5610
5611     def is_py_operation_types(self, type1, type2):
5612         return (type1.is_string
5613             or type2.is_string
5614             or NumBinopNode.is_py_operation_types(self, type1, type2))
5615
5616     def zero_division_message(self):
5617         if self.type.is_int:
5618             return "integer division or modulo by zero"
5619         else:
5620             return "float divmod()"
5621     
5622     def generate_evaluation_code(self, code):
5623         if not self.type.is_pyobject:
5624             if self.cdivision is None:
5625                 self.cdivision = code.globalstate.directives['cdivision'] or not self.type.signed
5626             if not self.cdivision:
5627                 if self.type.is_int:
5628                     code.globalstate.use_utility_code(mod_int_utility_code.specialize(self.type))
5629                 else:
5630                     code.globalstate.use_utility_code(
5631                         mod_float_utility_code.specialize(self.type, math_h_modifier=self.type.math_h_modifier))
5632         NumBinopNode.generate_evaluation_code(self, code)
5633         self.generate_div_warning_code(code)
5634     
5635     def calculate_result_code(self):
5636         if self.cdivision:
5637             if self.type.is_float:
5638                 return "fmod%s(%s, %s)" % (
5639                     self.type.math_h_modifier,
5640                     self.operand1.result(), 
5641                     self.operand2.result())
5642             else:
5643                 return "(%s %% %s)" % (
5644                     self.operand1.result(), 
5645                     self.operand2.result())
5646         else:
5647             return "__Pyx_mod_%s(%s, %s)" % (
5648                     self.type.specialization_name(),
5649                     self.operand1.result(), 
5650                     self.operand2.result())
5651
5652 class PowNode(NumBinopNode):
5653     #  '**' operator.
5654     
5655     def analyse_c_operation(self, env):
5656         NumBinopNode.analyse_c_operation(self, env)
5657         if self.type.is_complex:
5658             error(self.pos, "complex powers not yet supported")
5659             self.pow_func = "<error>"
5660         elif self.type.is_float:
5661             self.pow_func = "pow" + self.type.math_h_modifier
5662         else:
5663             self.pow_func = "__Pyx_pow_%s" % self.type.declaration_code('').replace(' ', '_')
5664             env.use_utility_code(
5665                     int_pow_utility_code.specialize(func_name=self.pow_func, 
5666                                                 type=self.type.declaration_code('')))
5667
5668     def calculate_result_code(self):
5669         # Work around MSVC overloading ambiguity.
5670         def typecast(operand):
5671             if self.type == operand.type:
5672                 return operand.result()
5673             else:
5674                 return self.type.cast_code(operand.result())
5675         return "%s(%s, %s)" % (
5676             self.pow_func, 
5677             typecast(self.operand1), 
5678             typecast(self.operand2))
5679
5680
5681 # Note: This class is temporarily "shut down" into an ineffective temp
5682 # allocation mode.
5683 #
5684 # More sophisticated temp reuse was going on before, one could have a
5685 # look at adding this again after /all/ classes are converted to the
5686 # new temp scheme. (The temp juggling cannot work otherwise).
5687 class BoolBinopNode(ExprNode):
5688     #  Short-circuiting boolean operation.
5689     #
5690     #  operator     string
5691     #  operand1     ExprNode
5692     #  operand2     ExprNode
5693     
5694     subexprs = ['operand1', 'operand2']
5695     
5696     def infer_type(self, env):
5697         type1 = self.operand1.infer_type(env)
5698         type2 = self.operand2.infer_type(env)
5699         return PyrexTypes.independent_spanning_type(type1, type2)
5700
5701     def may_be_none(self):
5702         if self.operator == 'or':
5703             return self.operand2.may_be_none()
5704         else:
5705             return self.operand1.may_be_none() or self.operand2.may_be_none()
5706
5707     def calculate_constant_result(self):
5708         if self.operator == 'and':
5709             self.constant_result = \
5710                 self.operand1.constant_result and \
5711                 self.operand2.constant_result
5712         else:
5713             self.constant_result = \
5714                 self.operand1.constant_result or \
5715                 self.operand2.constant_result
5716     
5717     def compile_time_value(self, denv):
5718         if self.operator == 'and':
5719             return self.operand1.compile_time_value(denv) \
5720                 and self.operand2.compile_time_value(denv)
5721         else:
5722             return self.operand1.compile_time_value(denv) \
5723                 or self.operand2.compile_time_value(denv)
5724     
5725     def coerce_to_boolean(self, env):
5726         return BoolBinopNode(
5727             self.pos,
5728             operator = self.operator,
5729             operand1 = self.operand1.coerce_to_boolean(env),
5730             operand2 = self.operand2.coerce_to_boolean(env),
5731             type = PyrexTypes.c_bint_type,
5732             is_temp = self.is_temp)
5733
5734     def analyse_types(self, env):
5735         self.operand1.analyse_types(env)
5736         self.operand2.analyse_types(env)
5737         self.type = PyrexTypes.independent_spanning_type(self.operand1.type, self.operand2.type)
5738         self.operand1 = self.operand1.coerce_to(self.type, env)
5739         self.operand2 = self.operand2.coerce_to(self.type, env)
5740         
5741         # For what we're about to do, it's vital that
5742         # both operands be temp nodes.
5743         self.operand1 = self.operand1.coerce_to_simple(env)
5744         self.operand2 = self.operand2.coerce_to_simple(env)
5745         self.is_temp = 1
5746
5747     gil_message = "Truth-testing Python object"
5748
5749     def check_const(self):
5750         return self.operand1.check_const() and self.operand2.check_const()
5751     
5752     def generate_evaluation_code(self, code):
5753         code.mark_pos(self.pos)
5754         self.operand1.generate_evaluation_code(code)
5755         test_result, uses_temp = self.generate_operand1_test(code)
5756         if self.operator == 'and':
5757             sense = ""
5758         else:
5759             sense = "!"
5760         code.putln(
5761             "if (%s%s) {" % (
5762                 sense,
5763                 test_result))
5764         if uses_temp:
5765             code.funcstate.release_temp(test_result)
5766         self.operand1.generate_disposal_code(code)
5767         self.operand2.generate_evaluation_code(code)
5768         self.allocate_temp_result(code)
5769         self.operand2.make_owned_reference(code)
5770         code.putln("%s = %s;" % (self.result(), self.operand2.result()))
5771         self.operand2.generate_post_assignment_code(code)
5772         self.operand2.free_temps(code)
5773         code.putln("} else {")
5774         self.operand1.make_owned_reference(code)
5775         code.putln("%s = %s;" % (self.result(), self.operand1.result()))
5776         self.operand1.generate_post_assignment_code(code)
5777         self.operand1.free_temps(code)
5778         code.putln("}")
5779     
5780     def generate_operand1_test(self, code):
5781         #  Generate code to test the truth of the first operand.
5782         if self.type.is_pyobject:
5783             test_result = code.funcstate.allocate_temp(PyrexTypes.c_bint_type,
5784                                                        manage_ref=False)
5785             code.putln(
5786                 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
5787                     test_result,
5788                     self.operand1.py_result(),
5789                     code.error_goto_if_neg(test_result, self.pos)))
5790         else:
5791             test_result = self.operand1.result()
5792         return (test_result, self.type.is_pyobject)
5793
5794
5795 class CondExprNode(ExprNode):
5796     #  Short-circuiting conditional expression.
5797     #
5798     #  test        ExprNode
5799     #  true_val    ExprNode
5800     #  false_val   ExprNode
5801     
5802     true_val = None
5803     false_val = None
5804     
5805     subexprs = ['test', 'true_val', 'false_val']
5806     
5807     def type_dependencies(self, env):
5808         return self.true_val.type_dependencies(env) + self.false_val.type_dependencies(env)
5809     
5810     def infer_type(self, env):
5811         return PyrexTypes.independent_spanning_type(self.true_val.infer_type(env),
5812                                                     self.false_val.infer_type(env))
5813
5814     def calculate_constant_result(self):
5815         if self.test.constant_result:
5816             self.constant_result = self.true_val.constant_result
5817         else:
5818             self.constant_result = self.false_val.constant_result
5819
5820     def analyse_types(self, env):
5821         self.test.analyse_types(env)
5822         self.test = self.test.coerce_to_boolean(env)
5823         self.true_val.analyse_types(env)
5824         self.false_val.analyse_types(env)
5825         self.type = PyrexTypes.independent_spanning_type(self.true_val.type, self.false_val.type)
5826         if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject:
5827             self.true_val = self.true_val.coerce_to(self.type, env)
5828             self.false_val = self.false_val.coerce_to(self.type, env)
5829         self.is_temp = 1
5830         if self.type == PyrexTypes.error_type:
5831             self.type_error()
5832         
5833     def type_error(self):
5834         if not (self.true_val.type.is_error or self.false_val.type.is_error):
5835             error(self.pos, "Incompatable types in conditional expression (%s; %s)" %
5836                 (self.true_val.type, self.false_val.type))
5837         self.type = PyrexTypes.error_type
5838     
5839     def check_const(self):
5840         return (self.test.check_const() 
5841             and self.true_val.check_const()
5842             and self.false_val.check_const())
5843     
5844     def generate_evaluation_code(self, code):
5845         # Because subexprs may not be evaluated we can use a more optimal
5846         # subexpr allocation strategy than the default, so override evaluation_code.
5847         
5848         code.mark_pos(self.pos)
5849         self.allocate_temp_result(code)
5850         self.test.generate_evaluation_code(code)
5851         code.putln("if (%s) {" % self.test.result() )
5852         self.eval_and_get(code, self.true_val)
5853         code.putln("} else {")
5854         self.eval_and_get(code, self.false_val)
5855         code.putln("}")
5856         self.test.generate_disposal_code(code)
5857         self.test.free_temps(code)
5858
5859     def eval_and_get(self, code, expr):
5860         expr.generate_evaluation_code(code)
5861         expr.make_owned_reference(code)
5862         code.putln("%s = %s;" % (self.result(), expr.result()))
5863         expr.generate_post_assignment_code(code)
5864         expr.free_temps(code)
5865
5866 richcmp_constants = {
5867     "<" : "Py_LT",
5868     "<=": "Py_LE",
5869     "==": "Py_EQ",
5870     "!=": "Py_NE",
5871     "<>": "Py_NE",
5872     ">" : "Py_GT",
5873     ">=": "Py_GE",
5874 }
5875
5876 class CmpNode(object):
5877     #  Mixin class containing code common to PrimaryCmpNodes
5878     #  and CascadedCmpNodes.
5879     
5880     def infer_type(self, env):
5881         # TODO: Actually implement this (after merging with -unstable).
5882         return py_object_type
5883
5884     def calculate_cascaded_constant_result(self, operand1_result):
5885         func = compile_time_binary_operators[self.operator]
5886         operand2_result = self.operand2.constant_result
5887         result = func(operand1_result, operand2_result)
5888         if self.cascade:
5889             self.cascade.calculate_cascaded_constant_result(operand2_result)
5890             if self.cascade.constant_result:
5891                 self.constant_result = result and self.cascade.constant_result
5892         else:
5893             self.constant_result = result
5894
5895     def cascaded_compile_time_value(self, operand1, denv):
5896         func = get_compile_time_binop(self)
5897         operand2 = self.operand2.compile_time_value(denv)
5898         try:
5899             result = func(operand1, operand2)
5900         except Exception, e:
5901             self.compile_time_value_error(e)
5902             result = None
5903         if result:
5904             cascade = self.cascade
5905             if cascade:
5906                 # FIXME: I bet this must call cascaded_compile_time_value()
5907                 result = result and cascade.cascaded_compile_time_value(operand2, denv)
5908         return result
5909
5910     def is_cpp_comparison(self):
5911         return self.operand1.type.is_cpp_class or self.operand2.type.is_cpp_class
5912
5913     def find_common_int_type(self, env, op, operand1, operand2):
5914         # type1 != type2 and at least one of the types is not a C int
5915         type1 = operand1.type
5916         type2 = operand2.type
5917         type1_can_be_int = False
5918         type2_can_be_int = False
5919
5920         if isinstance(operand1, (StringNode, BytesNode, UnicodeNode)) \
5921                and operand1.can_coerce_to_char_literal():
5922             type1_can_be_int = True
5923         if isinstance(operand2, (StringNode, BytesNode, UnicodeNode)) \
5924                  and operand2.can_coerce_to_char_literal():
5925             type2_can_be_int = True
5926
5927         if type1.is_int:
5928             if type2_can_be_int:
5929                 return type1
5930         elif type2.is_int:
5931             if type1_can_be_int:
5932                 return type2
5933         elif type1_can_be_int:
5934             if type2_can_be_int:
5935                 return PyrexTypes.c_uchar_type
5936
5937         return None
5938
5939     def find_common_type(self, env, op, operand1, common_type=None):
5940         operand2 = self.operand2
5941         type1 = operand1.type
5942         type2 = operand2.type
5943
5944         new_common_type = None
5945
5946         # catch general errors
5947         if type1 == str_type and (type2.is_string or type2 in (bytes_type, unicode_type)) or \
5948                type2 == str_type and (type1.is_string or type1 in (bytes_type, unicode_type)):
5949             error(self.pos, "Comparisons between bytes/unicode and str are not portable to Python 3")
5950             new_common_type = error_type
5951
5952         # try to use numeric comparisons where possible
5953         elif type1.is_complex or type2.is_complex:
5954             if op not in ('==', '!='):
5955                 error(self.pos, "complex types are unordered")
5956                 new_common_type = error_type
5957             if type1.is_pyobject:
5958                 new_common_type = type1
5959             elif type2.is_pyobject:
5960                 new_common_type = type2
5961             else:
5962                 new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
5963         elif type1.is_numeric and type2.is_numeric:
5964             new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
5965         elif common_type is None or not common_type.is_pyobject:
5966             new_common_type = self.find_common_int_type(env, op, operand1, operand2)
5967
5968         if new_common_type is None:
5969             # fall back to generic type compatibility tests
5970             if type1 == type2:
5971                 new_common_type = type1
5972             elif type1.is_pyobject or type2.is_pyobject:
5973                 if type2.is_numeric or type2.is_string:
5974                     if operand2.check_for_coercion_error(type1):
5975                         new_common_type = error_type
5976                     else:
5977                         new_common_type = py_object_type
5978                 elif type1.is_numeric or type1.is_string:
5979                     if operand1.check_for_coercion_error(type2):
5980                         new_common_type = error_type
5981                     else:
5982                         new_common_type = py_object_type
5983                 elif py_object_type.assignable_from(type1) and py_object_type.assignable_from(type2):
5984                     new_common_type = py_object_type
5985                 else:
5986                     # one Python type and one non-Python type, not assignable
5987                     self.invalid_types_error(operand1, op, operand2)
5988                     new_common_type = error_type
5989             elif type1.assignable_from(type2):
5990                 new_common_type = type1
5991             elif type2.assignable_from(type1):
5992                 new_common_type = type2
5993             else:
5994                 # C types that we couldn't handle up to here are an error
5995                 self.invalid_types_error(operand1, op, operand2)
5996                 new_common_type = error_type
5997
5998         # recursively merge types
5999         if common_type is None or new_common_type.is_error:
6000             common_type = new_common_type
6001         else:
6002             # we could do a lot better by splitting the comparison
6003             # into a non-Python part and a Python part, but this is
6004             # safer for now
6005             common_type = PyrexTypes.spanning_type(common_type, new_common_type)
6006
6007         if self.cascade:
6008             common_type = self.cascade.find_common_type(env, self.operator, operand2, common_type)
6009
6010         return common_type
6011
6012     def invalid_types_error(self, operand1, op, operand2):
6013         error(self.pos, "Invalid types for '%s' (%s, %s)" %
6014               (op, operand1.type, operand2.type))
6015
6016     def is_python_comparison(self):
6017         return (not self.is_ptr_contains()
6018             and not self.is_c_string_contains()
6019             and (self.has_python_operands()
6020                  or (self.cascade and self.cascade.is_python_comparison())
6021                  or self.operator in ('in', 'not_in')))
6022
6023     def coerce_operands_to(self, dst_type, env):
6024         operand2 = self.operand2
6025         if operand2.type != dst_type:
6026             self.operand2 = operand2.coerce_to(dst_type, env)
6027         if self.cascade:
6028             self.cascade.coerce_operands_to(dst_type, env)
6029
6030     def is_python_result(self):
6031         return ((self.has_python_operands() and
6032                  self.operator not in ('is', 'is_not', 'in', 'not_in') and
6033                  not self.is_c_string_contains() and
6034                  not self.is_ptr_contains())
6035             or (self.cascade and self.cascade.is_python_result()))
6036
6037     def is_c_string_contains(self):
6038         return self.operator in ('in', 'not_in') and \
6039                ((self.operand1.type.is_int
6040                  and (self.operand2.type.is_string or self.operand2.type is bytes_type)) or
6041                 (self.operand1.type is PyrexTypes.c_py_unicode_type
6042                  and self.operand2.type is unicode_type))
6043     
6044     def is_ptr_contains(self):
6045         if self.operator in ('in', 'not_in'):
6046             container_type = self.operand2.type
6047             return (container_type.is_ptr or container_type.is_array) \
6048                 and not container_type.is_string
6049
6050     def generate_operation_code(self, code, result_code, 
6051             operand1, op , operand2):
6052         if self.type.is_pyobject:
6053             coerce_result = "__Pyx_PyBool_FromLong"
6054         else:
6055             coerce_result = ""
6056         if 'not' in op: 
6057             negation = "!"
6058         else: 
6059             negation = ""
6060         if op == 'in' or op == 'not_in':
6061             code.globalstate.use_utility_code(contains_utility_code)
6062             if self.type.is_pyobject:
6063                 coerce_result = "__Pyx_PyBoolOrNull_FromLong"
6064             if op == 'not_in':
6065                 negation = "__Pyx_NegateNonNeg"
6066             if operand2.type is dict_type:
6067                 code.globalstate.use_utility_code(
6068                     raise_none_iter_error_utility_code)
6069                 code.putln("if (unlikely(%s == Py_None)) {" % operand2.py_result())
6070                 code.putln("__Pyx_RaiseNoneNotIterableError(); %s" %
6071                            code.error_goto(self.pos))
6072                 code.putln("} else {")
6073                 method = "PyDict_Contains"
6074             else:
6075                 method = "PySequence_Contains"
6076             if self.type.is_pyobject:
6077                 error_clause = code.error_goto_if_null
6078                 got_ref = "__Pyx_XGOTREF(%s); " % result_code
6079             else:
6080                 error_clause = code.error_goto_if_neg
6081                 got_ref = ""
6082             code.putln(
6083                 "%s = %s(%s(%s(%s, %s))); %s%s" % (
6084                     result_code,
6085                     coerce_result,
6086                     negation,
6087                     method,
6088                     operand2.py_result(), 
6089                     operand1.py_result(), 
6090                     got_ref,
6091                     error_clause(result_code, self.pos)))
6092             if operand2.type is dict_type:
6093                 code.putln("}")
6094                     
6095         elif (operand1.type.is_pyobject
6096             and op not in ('is', 'is_not')):
6097                 code.putln("%s = PyObject_RichCompare(%s, %s, %s); %s" % (
6098                         result_code, 
6099                         operand1.py_result(), 
6100                         operand2.py_result(), 
6101                         richcmp_constants[op],
6102                         code.error_goto_if_null(result_code, self.pos)))
6103                 code.put_gotref(result_code)
6104         elif operand1.type.is_complex:
6105             if op == "!=": 
6106                 negation = "!"
6107             else: 
6108                 negation = ""
6109             code.putln("%s = %s(%s%s(%s, %s));" % (
6110                 result_code, 
6111                 coerce_result,
6112                 negation,
6113                 operand1.type.unary_op('eq'), 
6114                 operand1.result(), 
6115                 operand2.result()))
6116         else:
6117             type1 = operand1.type
6118             type2 = operand2.type
6119             if (type1.is_extension_type or type2.is_extension_type) \
6120                     and not type1.same_as(type2):
6121                 common_type = py_object_type
6122             elif type1.is_numeric:
6123                 common_type = PyrexTypes.widest_numeric_type(type1, type2)
6124             else:
6125                 common_type = type1
6126             code1 = operand1.result_as(common_type)
6127             code2 = operand2.result_as(common_type)
6128             code.putln("%s = %s(%s %s %s);" % (
6129                 result_code, 
6130                 coerce_result, 
6131                 code1, 
6132                 self.c_operator(op), 
6133                 code2))
6134
6135     def c_operator(self, op):
6136         if op == 'is':
6137             return "=="
6138         elif op == 'is_not':
6139             return "!="
6140         else:
6141             return op
6142     
6143 contains_utility_code = UtilityCode(
6144 proto="""
6145 static CYTHON_INLINE long __Pyx_NegateNonNeg(long b) { return unlikely(b < 0) ? b : !b; }
6146 static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) {
6147     return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b);
6148 }
6149 """)
6150
6151 char_in_bytes_utility_code = UtilityCode(
6152 proto="""
6153 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character); /*proto*/
6154 """,
6155 impl="""
6156 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character) {
6157     const Py_ssize_t length = PyBytes_GET_SIZE(bytes);
6158     char* char_start = PyBytes_AS_STRING(bytes);
6159     char* pos;
6160     for (pos=char_start; pos < char_start+length; pos++) {
6161         if (character == pos[0]) return 1;
6162     }
6163     return 0;
6164 }
6165 """)
6166
6167 pyunicode_in_unicode_utility_code = UtilityCode(
6168 proto="""
6169 static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character); /*proto*/
6170 """,
6171 impl="""
6172 static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character) {
6173     const Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
6174     Py_UNICODE* char_start = PyUnicode_AS_UNICODE(unicode);
6175     Py_UNICODE* pos;
6176     for (pos=char_start; pos < char_start+length; pos++) {
6177         if (character == pos[0]) return 1;
6178     }
6179     return 0;
6180 }
6181 """)
6182
6183
6184 class PrimaryCmpNode(ExprNode, CmpNode):
6185     #  Non-cascaded comparison or first comparison of
6186     #  a cascaded sequence.
6187     #
6188     #  operator      string
6189     #  operand1      ExprNode
6190     #  operand2      ExprNode
6191     #  cascade       CascadedCmpNode
6192     
6193     #  We don't use the subexprs mechanism, because
6194     #  things here are too complicated for it to handle.
6195     #  Instead, we override all the framework methods
6196     #  which use it.
6197     
6198     child_attrs = ['operand1', 'operand2', 'cascade']
6199     
6200     cascade = None
6201
6202     def infer_type(self, env):
6203         # TODO: Actually implement this (after merging with -unstable).
6204         return py_object_type
6205
6206     def type_dependencies(self, env):
6207         return ()
6208
6209     def calculate_constant_result(self):
6210         self.calculate_cascaded_constant_result(self.operand1.constant_result)
6211     
6212     def compile_time_value(self, denv):
6213         operand1 = self.operand1.compile_time_value(denv)
6214         return self.cascaded_compile_time_value(operand1, denv)
6215
6216     def analyse_types(self, env):
6217         self.operand1.analyse_types(env)
6218         self.operand2.analyse_types(env)
6219         if self.is_cpp_comparison():
6220             self.analyse_cpp_comparison(env)
6221             if self.cascade:
6222                 error(self.pos, "Cascading comparison not yet supported for cpp types.")
6223             return
6224         if self.cascade:
6225             self.cascade.analyse_types(env)
6226
6227         if self.operator in ('in', 'not_in'):
6228             if self.is_c_string_contains():
6229                 self.is_pycmp = False
6230                 common_type = None
6231                 if self.cascade:
6232                     error(self.pos, "Cascading comparison not yet supported for 'int_val in string'.")
6233                     return
6234                 if self.operand2.type is unicode_type:
6235                     env.use_utility_code(pyunicode_in_unicode_utility_code)
6236                 else:
6237                     if self.operand1.type is PyrexTypes.c_uchar_type:
6238                         self.operand1 = self.operand1.coerce_to(PyrexTypes.c_char_type, env)
6239                     if self.operand2.type is not bytes_type:
6240                         self.operand2 = self.operand2.coerce_to(bytes_type, env)
6241                     env.use_utility_code(char_in_bytes_utility_code)
6242                 self.operand2 = self.operand2.as_none_safe_node(
6243                     "argument of type 'NoneType' is not iterable")
6244             elif self.is_ptr_contains():
6245                 if self.cascade:
6246                     error(self.pos, "Cascading comparison not yet supported for 'val in sliced pointer'.")
6247                 self.type = PyrexTypes.c_bint_type
6248                 # Will be transformed by IterationTransform
6249                 return
6250             else:
6251                 common_type = py_object_type
6252                 self.is_pycmp = True
6253         else:
6254             common_type = self.find_common_type(env, self.operator, self.operand1)
6255             self.is_pycmp = common_type.is_pyobject
6256
6257         if common_type is not None and not common_type.is_error:
6258             if self.operand1.type != common_type:
6259                 self.operand1 = self.operand1.coerce_to(common_type, env)
6260             self.coerce_operands_to(common_type, env)
6261
6262         if self.cascade:
6263             self.operand2 = self.operand2.coerce_to_simple(env)
6264             self.cascade.coerce_cascaded_operands_to_temp(env)
6265         if self.is_python_result():
6266             self.type = PyrexTypes.py_object_type
6267         else:
6268             self.type = PyrexTypes.c_bint_type
6269         cdr = self.cascade
6270         while cdr:
6271             cdr.type = self.type
6272             cdr = cdr.cascade
6273         if self.is_pycmp or self.cascade:
6274             self.is_temp = 1
6275     
6276     def analyse_cpp_comparison(self, env):
6277         type1 = self.operand1.type
6278         type2 = self.operand2.type
6279         entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
6280         if entry is None:
6281             error(self.pos, "Invalid types for '%s' (%s, %s)" %
6282                 (self.operator, type1, type2))
6283             self.type = PyrexTypes.error_type
6284             self.result_code = "<error>"
6285             return
6286         func_type = entry.type
6287         if func_type.is_ptr:
6288             func_type = func_type.base_type
6289         if len(func_type.args) == 1:
6290             self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
6291         else:
6292             self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
6293             self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
6294         self.type = func_type.return_type
6295     
6296     def has_python_operands(self):
6297         return (self.operand1.type.is_pyobject
6298             or self.operand2.type.is_pyobject)
6299     
6300     def check_const(self):
6301         if self.cascade:
6302             self.not_const()
6303             return False
6304         else:
6305             return self.operand1.check_const() and self.operand2.check_const()
6306
6307     def calculate_result_code(self):
6308         if self.operand1.type.is_complex:
6309             if self.operator == "!=":
6310                 negation = "!"
6311             else:
6312                 negation = ""
6313             return "(%s%s(%s, %s))" % (
6314                 negation,
6315                 self.operand1.type.binary_op('=='), 
6316                 self.operand1.result(), 
6317                 self.operand2.result())
6318         elif self.is_c_string_contains():
6319             if self.operand2.type is bytes_type:
6320                 method = "__Pyx_BytesContains"
6321             else:
6322                 method = "__Pyx_UnicodeContains"
6323             if self.operator == "not_in":
6324                 negation = "!"
6325             else:
6326                 negation = ""
6327             return "(%s%s(%s, %s))" % (
6328                 negation,
6329                 method,
6330                 self.operand2.result(), 
6331                 self.operand1.result())
6332         else:
6333             return "(%s %s %s)" % (
6334                 self.operand1.result(),
6335                 self.c_operator(self.operator),
6336                 self.operand2.result())
6337
6338     def generate_evaluation_code(self, code):
6339         self.operand1.generate_evaluation_code(code)
6340         self.operand2.generate_evaluation_code(code)
6341         if self.is_temp:
6342             self.allocate_temp_result(code)
6343             self.generate_operation_code(code, self.result(), 
6344                 self.operand1, self.operator, self.operand2)
6345             if self.cascade:
6346                 self.cascade.generate_evaluation_code(code,
6347                     self.result(), self.operand2)
6348             self.operand1.generate_disposal_code(code)
6349             self.operand1.free_temps(code)
6350             self.operand2.generate_disposal_code(code)
6351             self.operand2.free_temps(code)
6352
6353     def generate_subexpr_disposal_code(self, code):
6354         #  If this is called, it is a non-cascaded cmp,
6355         #  so only need to dispose of the two main operands.
6356         self.operand1.generate_disposal_code(code)
6357         self.operand2.generate_disposal_code(code)
6358         
6359     def free_subexpr_temps(self, code):
6360         #  If this is called, it is a non-cascaded cmp,
6361         #  so only need to dispose of the two main operands.
6362         self.operand1.free_temps(code)
6363         self.operand2.free_temps(code)
6364         
6365     def annotate(self, code):
6366         self.operand1.annotate(code)
6367         self.operand2.annotate(code)
6368         if self.cascade:
6369             self.cascade.annotate(code)
6370
6371
6372 class CascadedCmpNode(Node, CmpNode):
6373     #  A CascadedCmpNode is not a complete expression node. It 
6374     #  hangs off the side of another comparison node, shares 
6375     #  its left operand with that node, and shares its result 
6376     #  with the PrimaryCmpNode at the head of the chain.
6377     #
6378     #  operator      string
6379     #  operand2      ExprNode
6380     #  cascade       CascadedCmpNode
6381
6382     child_attrs = ['operand2', 'cascade']
6383
6384     cascade = None
6385     constant_result = constant_value_not_set # FIXME: where to calculate this?
6386
6387     def infer_type(self, env):
6388         # TODO: Actually implement this (after merging with -unstable).
6389         return py_object_type
6390
6391     def type_dependencies(self, env):
6392         return ()
6393
6394     def has_constant_result(self):
6395         return self.constant_result is not constant_value_not_set and \
6396                self.constant_result is not not_a_constant
6397
6398     def analyse_types(self, env):
6399         self.operand2.analyse_types(env)
6400         if self.cascade:
6401             self.cascade.analyse_types(env)
6402
6403     def has_python_operands(self):
6404         return self.operand2.type.is_pyobject
6405         
6406     def coerce_operands_to_pyobjects(self, env):
6407         self.operand2 = self.operand2.coerce_to_pyobject(env)
6408         if self.cascade:
6409             self.cascade.coerce_operands_to_pyobjects(env)
6410
6411     def coerce_cascaded_operands_to_temp(self, env):
6412         if self.cascade:
6413             #self.operand2 = self.operand2.coerce_to_temp(env) #CTT
6414             self.operand2 = self.operand2.coerce_to_simple(env)
6415             self.cascade.coerce_cascaded_operands_to_temp(env)
6416     
6417     def generate_evaluation_code(self, code, result, operand1):
6418         if self.type.is_pyobject:
6419             code.putln("if (__Pyx_PyObject_IsTrue(%s)) {" % result)
6420             code.put_decref(result, self.type)
6421         else:
6422             code.putln("if (%s) {" % result)
6423         self.operand2.generate_evaluation_code(code)
6424         self.generate_operation_code(code, result, 
6425             operand1, self.operator, self.operand2)
6426         if self.cascade:
6427             self.cascade.generate_evaluation_code(
6428                 code, result, self.operand2)
6429         # Cascaded cmp result is always temp
6430         self.operand2.generate_disposal_code(code)
6431         self.operand2.free_temps(code)
6432         code.putln("}")
6433
6434     def annotate(self, code):
6435         self.operand2.annotate(code)
6436         if self.cascade:
6437             self.cascade.annotate(code)
6438
6439
6440 binop_node_classes = {
6441     "or":       BoolBinopNode,
6442     "and":      BoolBinopNode,
6443     "|":        IntBinopNode,
6444     "^":        IntBinopNode,
6445     "&":        IntBinopNode,
6446     "<<":       IntBinopNode,
6447     ">>":       IntBinopNode,
6448     "+":        AddNode,
6449     "-":        SubNode,
6450     "*":        MulNode,
6451     "/":        DivNode,
6452     "//":       DivNode,
6453     "%":        ModNode,
6454     "**":       PowNode
6455 }
6456
6457 def binop_node(pos, operator, operand1, operand2):
6458     # Construct binop node of appropriate class for 
6459     # given operator.
6460     return binop_node_classes[operator](pos, 
6461         operator = operator, 
6462         operand1 = operand1, 
6463         operand2 = operand2)
6464
6465 #-------------------------------------------------------------------
6466 #
6467 #  Coercion nodes
6468 #
6469 #  Coercion nodes are special in that they are created during
6470 #  the analyse_types phase of parse tree processing.
6471 #  Their __init__ methods consequently incorporate some aspects
6472 #  of that phase.
6473 #
6474 #-------------------------------------------------------------------
6475
6476 class CoercionNode(ExprNode):
6477     #  Abstract base class for coercion nodes.
6478     #
6479     #  arg       ExprNode       node being coerced
6480     
6481     subexprs = ['arg']
6482     constant_result = not_a_constant
6483     
6484     def __init__(self, arg):
6485         self.pos = arg.pos
6486         self.arg = arg
6487         if debug_coercion:
6488             print("%s Coercing %s" % (self, self.arg))
6489
6490     def calculate_constant_result(self):
6491         # constant folding can break type coercion, so this is disabled
6492         pass
6493             
6494     def annotate(self, code):
6495         self.arg.annotate(code)
6496         if self.arg.type != self.type:
6497             file, line, col = self.pos
6498             code.annotate((file, line, col-1), AnnotationItem(style='coerce', tag='coerce', text='[%s] to [%s]' % (self.arg.type, self.type)))
6499
6500
6501 class CastNode(CoercionNode):
6502     #  Wrap a node in a C type cast.
6503     
6504     def __init__(self, arg, new_type):
6505         CoercionNode.__init__(self, arg)
6506         self.type = new_type
6507
6508     def may_be_none(self):
6509         return self.arg.may_be_none()
6510     
6511     def calculate_result_code(self):
6512         return self.arg.result_as(self.type)
6513
6514     def generate_result_code(self, code):
6515         self.arg.generate_result_code(code)
6516
6517
6518 class PyTypeTestNode(CoercionNode):
6519     #  This node is used to check that a generic Python
6520     #  object is an instance of a particular extension type.
6521     #  This node borrows the result of its argument node.
6522
6523     def __init__(self, arg, dst_type, env, notnone=False):
6524         #  The arg is know to be a Python object, and
6525         #  the dst_type is known to be an extension type.
6526         assert dst_type.is_extension_type or dst_type.is_builtin_type, "PyTypeTest on non extension type"
6527         CoercionNode.__init__(self, arg)
6528         self.type = dst_type
6529         self.result_ctype = arg.ctype()
6530         self.notnone = notnone
6531
6532     nogil_check = Node.gil_error
6533     gil_message = "Python type test"
6534     
6535     def analyse_types(self, env):
6536         pass
6537
6538     def may_be_none(self):
6539         if self.notnone:
6540             return False
6541         return self.arg.may_be_none()
6542     
6543     def result_in_temp(self):
6544         return self.arg.result_in_temp()
6545     
6546     def is_ephemeral(self):
6547         return self.arg.is_ephemeral()
6548
6549     def calculate_constant_result(self):
6550         # FIXME
6551         pass
6552
6553     def calculate_result_code(self):
6554         return self.arg.result()
6555     
6556     def generate_result_code(self, code):
6557         if self.type.typeobj_is_available():
6558             if not self.type.is_builtin_type:
6559                 code.globalstate.use_utility_code(type_test_utility_code)
6560             code.putln(
6561                 "if (!(%s)) %s" % (
6562                     self.type.type_test_code(self.arg.py_result(), self.notnone),
6563                     code.error_goto(self.pos)))
6564         else:
6565             error(self.pos, "Cannot test type of extern C class "
6566                 "without type object name specification")
6567                 
6568     def generate_post_assignment_code(self, code):
6569         self.arg.generate_post_assignment_code(code)
6570
6571     def free_temps(self, code):
6572         self.arg.free_temps(code)
6573
6574
6575 class NoneCheckNode(CoercionNode):
6576     # This node is used to check that a Python object is not None and
6577     # raises an appropriate exception (as specified by the creating
6578     # transform).
6579
6580     def __init__(self, arg, exception_type_cname, exception_message):
6581         CoercionNode.__init__(self, arg)
6582         self.type = arg.type
6583         self.result_ctype = arg.ctype()
6584         self.exception_type_cname = exception_type_cname
6585         self.exception_message = exception_message
6586
6587     def analyse_types(self, env):
6588         pass
6589
6590     def may_be_none(self):
6591         return False
6592
6593     def result_in_temp(self):
6594         return self.arg.result_in_temp()
6595
6596     def calculate_result_code(self):
6597         return self.arg.result()
6598     
6599     def generate_result_code(self, code):
6600         code.putln(
6601             "if (unlikely(%s == Py_None)) {" % self.arg.result())
6602         code.putln('PyErr_SetString(%s, "%s"); %s ' % (
6603             self.exception_type_cname,
6604             StringEncoding.escape_byte_string(
6605                 self.exception_message.encode('UTF-8')),
6606             code.error_goto(self.pos)))
6607         code.putln("}")
6608
6609     def generate_post_assignment_code(self, code):
6610         self.arg.generate_post_assignment_code(code)
6611
6612     def free_temps(self, code):
6613         self.arg.free_temps(code)
6614
6615
6616 class CoerceToPyTypeNode(CoercionNode):
6617     #  This node is used to convert a C data type
6618     #  to a Python object.
6619     
6620     type = py_object_type
6621     is_temp = 1
6622
6623     def __init__(self, arg, env, type=py_object_type):
6624         CoercionNode.__init__(self, arg)
6625         if not arg.type.create_to_py_utility_code(env):
6626             error(arg.pos,
6627                   "Cannot convert '%s' to Python object" % arg.type)
6628         if type is not py_object_type:
6629             self.type = py_object_type
6630         elif arg.type.is_string:
6631             self.type = bytes_type
6632         elif arg.type is PyrexTypes.c_py_unicode_type:
6633             self.type = unicode_type
6634
6635     gil_message = "Converting to Python object"
6636
6637     def may_be_none(self):
6638         # FIXME: is this always safe?
6639         return False
6640
6641     def coerce_to_boolean(self, env):
6642         arg_type = self.arg.type
6643         if (arg_type == PyrexTypes.c_bint_type or
6644             (arg_type.is_pyobject and arg_type.name == 'bool')):
6645             return self.arg.coerce_to_temp(env)
6646         else:
6647             return CoerceToBooleanNode(self, env)
6648     
6649     def coerce_to_integer(self, env):
6650         # If not already some C integer type, coerce to longint.
6651         if self.arg.type.is_int:
6652             return self.arg
6653         else:
6654             return self.arg.coerce_to(PyrexTypes.c_long_type, env)
6655
6656     def analyse_types(self, env):
6657         # The arg is always already analysed
6658         pass
6659
6660     def generate_result_code(self, code):
6661         function = self.arg.type.to_py_function
6662         code.putln('%s = %s(%s); %s' % (
6663             self.result(), 
6664             function, 
6665             self.arg.result(), 
6666             code.error_goto_if_null(self.result(), self.pos)))
6667         code.put_gotref(self.py_result())
6668
6669
6670 class CoerceIntToBytesNode(CoerceToPyTypeNode):
6671     #  This node is used to convert a C int type to a Python bytes
6672     #  object.
6673
6674     is_temp = 1
6675
6676     def __init__(self, arg, env):
6677         arg = arg.coerce_to_simple(env)
6678         CoercionNode.__init__(self, arg)
6679         self.type = Builtin.bytes_type
6680
6681     def generate_result_code(self, code):
6682         arg = self.arg
6683         arg_result = arg.result()
6684         if arg.type not in (PyrexTypes.c_char_type,
6685                             PyrexTypes.c_uchar_type,
6686                             PyrexTypes.c_schar_type):
6687             if arg.type.signed:
6688                 code.putln("if ((%s < 0) || (%s > 255)) {" % (
6689                     arg_result, arg_result))
6690             else:
6691                 code.putln("if (%s > 255) {" % arg_result)
6692             code.putln('PyErr_Format(PyExc_OverflowError, '
6693                        '"value too large to pack into a byte"); %s' % (
6694                            code.error_goto(self.pos)))
6695             code.putln('}')
6696         temp = None
6697         if arg.type is not PyrexTypes.c_char_type:
6698             temp = code.funcstate.allocate_temp(PyrexTypes.c_char_type, manage_ref=False)
6699             code.putln("%s = (char)%s;" % (temp, arg_result))
6700             arg_result = temp
6701         code.putln('%s = PyBytes_FromStringAndSize(&%s, 1); %s' % (
6702             self.result(),
6703             arg_result,
6704             code.error_goto_if_null(self.result(), self.pos)))
6705         if temp is not None:
6706             code.funcstate.release_temp(temp)
6707         code.put_gotref(self.py_result())
6708
6709
6710 class CoerceFromPyTypeNode(CoercionNode):
6711     #  This node is used to convert a Python object
6712     #  to a C data type.
6713
6714     def __init__(self, result_type, arg, env):
6715         CoercionNode.__init__(self, arg)
6716         self.type = result_type
6717         self.is_temp = 1
6718         if not result_type.create_from_py_utility_code(env):
6719             error(arg.pos,
6720                   "Cannot convert Python object to '%s'" % result_type)
6721         if self.type.is_string and self.arg.is_ephemeral():
6722             error(arg.pos,
6723                   "Obtaining char * from temporary Python value")
6724     
6725     def analyse_types(self, env):
6726         # The arg is always already analysed
6727         pass
6728
6729     def generate_result_code(self, code):
6730         function = self.type.from_py_function
6731         operand = self.arg.py_result()
6732         rhs = "%s(%s)" % (function, operand)
6733         if self.type.is_enum:
6734             rhs = typecast(self.type, c_long_type, rhs)
6735         code.putln('%s = %s; %s' % (
6736             self.result(), 
6737             rhs,
6738             code.error_goto_if(self.type.error_condition(self.result()), self.pos)))
6739         if self.type.is_pyobject:
6740             code.put_gotref(self.py_result())
6741
6742
6743 class CoerceToBooleanNode(CoercionNode):
6744     #  This node is used when a result needs to be used
6745     #  in a boolean context.
6746     
6747     type = PyrexTypes.c_bint_type
6748
6749     _special_builtins = {
6750         Builtin.list_type    : 'PyList_GET_SIZE',
6751         Builtin.tuple_type   : 'PyTuple_GET_SIZE',
6752         Builtin.bytes_type   : 'PyBytes_GET_SIZE',
6753         Builtin.unicode_type : 'PyUnicode_GET_SIZE',
6754         }
6755
6756     def __init__(self, arg, env):
6757         CoercionNode.__init__(self, arg)
6758         if arg.type.is_pyobject:
6759             self.is_temp = 1
6760
6761     def nogil_check(self, env):
6762         if self.arg.type.is_pyobject and self._special_builtins.get(self.arg.type) is None:
6763             self.gil_error()
6764
6765     gil_message = "Truth-testing Python object"
6766     
6767     def check_const(self):
6768         if self.is_temp:
6769             self.not_const()
6770             return False
6771         return self.arg.check_const()
6772     
6773     def calculate_result_code(self):
6774         return "(%s != 0)" % self.arg.result()
6775
6776     def generate_result_code(self, code):
6777         if not self.is_temp:
6778             return
6779         test_func = self._special_builtins.get(self.arg.type)
6780         if test_func is not None:
6781             code.putln("%s = (%s != Py_None) && (%s(%s) != 0);" % (
6782                        self.result(),
6783                        self.arg.py_result(),
6784                        test_func,
6785                        self.arg.py_result()))
6786         else:
6787             code.putln(
6788                 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
6789                     self.result(), 
6790                     self.arg.py_result(), 
6791                     code.error_goto_if_neg(self.result(), self.pos)))
6792
6793 class CoerceToComplexNode(CoercionNode):
6794
6795     def __init__(self, arg, dst_type, env):
6796         if arg.type.is_complex:
6797             arg = arg.coerce_to_simple(env)
6798         self.type = dst_type
6799         CoercionNode.__init__(self, arg)
6800         dst_type.create_declaration_utility_code(env)
6801
6802     def calculate_result_code(self):
6803         if self.arg.type.is_complex:
6804             real_part = "__Pyx_CREAL(%s)" % self.arg.result()
6805             imag_part = "__Pyx_CIMAG(%s)" % self.arg.result()
6806         else:
6807             real_part = self.arg.result()
6808             imag_part = "0"
6809         return "%s(%s, %s)" % (
6810                 self.type.from_parts,
6811                 real_part,
6812                 imag_part)
6813     
6814     def generate_result_code(self, code):
6815         pass
6816
6817 class CoerceToTempNode(CoercionNode):
6818     #  This node is used to force the result of another node
6819     #  to be stored in a temporary. It is only used if the
6820     #  argument node's result is not already in a temporary.
6821
6822     def __init__(self, arg, env):
6823         CoercionNode.__init__(self, arg)
6824         self.type = self.arg.type
6825         self.constant_result = self.arg.constant_result
6826         self.is_temp = 1
6827         if self.type.is_pyobject:
6828             self.result_ctype = py_object_type
6829
6830     gil_message = "Creating temporary Python reference"
6831
6832     def analyse_types(self, env):
6833         # The arg is always already analysed
6834         pass
6835         
6836     def coerce_to_boolean(self, env):
6837         self.arg = self.arg.coerce_to_boolean(env)
6838         if self.arg.is_simple():
6839             return self.arg
6840         self.type = self.arg.type
6841         self.result_ctype = self.type
6842         return self
6843
6844     def generate_result_code(self, code):
6845         #self.arg.generate_evaluation_code(code) # Already done
6846         # by generic generate_subexpr_evaluation_code!
6847         code.putln("%s = %s;" % (
6848             self.result(), self.arg.result_as(self.ctype())))
6849         if self.type.is_pyobject and self.use_managed_ref:
6850             code.put_incref(self.result(), self.ctype())
6851
6852
6853 class CloneNode(CoercionNode):
6854     #  This node is employed when the result of another node needs
6855     #  to be used multiple times. The argument node's result must
6856     #  be in a temporary. This node "borrows" the result from the
6857     #  argument node, and does not generate any evaluation or
6858     #  disposal code for it. The original owner of the argument 
6859     #  node is responsible for doing those things.
6860     
6861     subexprs = [] # Arg is not considered a subexpr
6862     nogil_check = None
6863     
6864     def __init__(self, arg):
6865         CoercionNode.__init__(self, arg)
6866         if hasattr(arg, 'type'):
6867             self.type = arg.type
6868             self.result_ctype = arg.result_ctype
6869         if hasattr(arg, 'entry'):
6870             self.entry = arg.entry
6871             
6872     def result(self):
6873         return self.arg.result()
6874     
6875     def type_dependencies(self, env):
6876         return self.arg.type_dependencies(env)
6877     
6878     def infer_type(self, env):
6879         return self.arg.infer_type(env)
6880
6881     def analyse_types(self, env):
6882         self.type = self.arg.type
6883         self.result_ctype = self.arg.result_ctype
6884         self.is_temp = 1
6885         if hasattr(self.arg, 'entry'):
6886             self.entry = self.arg.entry
6887     
6888     def generate_evaluation_code(self, code):
6889         pass
6890
6891     def generate_result_code(self, code):
6892         pass
6893         
6894     def generate_disposal_code(self, code):
6895         pass
6896                 
6897     def free_temps(self, code):
6898         pass
6899
6900
6901 class ModuleRefNode(ExprNode):
6902     # Simple returns the module object
6903     
6904     type = py_object_type
6905     is_temp = False
6906     subexprs = []
6907     
6908     def analyse_types(self, env):
6909         pass
6910
6911     def may_be_none(self):
6912         return False
6913
6914     def calculate_result_code(self):
6915         return Naming.module_cname
6916
6917     def generate_result_code(self, code):
6918         pass
6919
6920 class DocstringRefNode(ExprNode):
6921     # Extracts the docstring of the body element
6922     
6923     subexprs = ['body']
6924     type = py_object_type
6925     is_temp = True
6926     
6927     def __init__(self, pos, body):
6928         ExprNode.__init__(self, pos)
6929         assert body.type.is_pyobject
6930         self.body = body
6931
6932     def analyse_types(self, env):
6933         pass
6934
6935     def generate_result_code(self, code):
6936         code.putln('%s = __Pyx_GetAttrString(%s, "__doc__"); %s' % (
6937             self.result(), self.body.result(),
6938             code.error_goto_if_null(self.result(), self.pos)))
6939         code.put_gotref(self.result())
6940
6941
6942
6943 #------------------------------------------------------------------------------------
6944 #
6945 #  Runtime support code
6946 #
6947 #------------------------------------------------------------------------------------
6948
6949 get_name_interned_utility_code = UtilityCode(
6950 proto = """
6951 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
6952 """,
6953 impl = """
6954 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
6955     PyObject *result;
6956     result = PyObject_GetAttr(dict, name);
6957     if (!result)
6958         PyErr_SetObject(PyExc_NameError, name);
6959     return result;
6960 }
6961 """)
6962
6963 #------------------------------------------------------------------------------------
6964
6965 import_utility_code = UtilityCode(
6966 proto = """
6967 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
6968 """,
6969 impl = """
6970 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
6971     PyObject *py_import = 0;
6972     PyObject *empty_list = 0;
6973     PyObject *module = 0;
6974     PyObject *global_dict = 0;
6975     PyObject *empty_dict = 0;
6976     PyObject *list;
6977     py_import = __Pyx_GetAttrString(%(BUILTINS)s, "__import__");
6978     if (!py_import)
6979         goto bad;
6980     if (from_list)
6981         list = from_list;
6982     else {
6983         empty_list = PyList_New(0);
6984         if (!empty_list)
6985             goto bad;
6986         list = empty_list;
6987     }
6988     global_dict = PyModule_GetDict(%(GLOBALS)s);
6989     if (!global_dict)
6990         goto bad;
6991     empty_dict = PyDict_New();
6992     if (!empty_dict)
6993         goto bad;
6994     module = PyObject_CallFunctionObjArgs(py_import,
6995         name, global_dict, empty_dict, list, NULL);
6996 bad:
6997     Py_XDECREF(empty_list);
6998     Py_XDECREF(py_import);
6999     Py_XDECREF(empty_dict);
7000     return module;
7001 }
7002 """ % {
7003     "BUILTINS": Naming.builtins_cname,
7004     "GLOBALS":  Naming.module_cname,
7005 })
7006
7007 #------------------------------------------------------------------------------------
7008
7009 get_exception_utility_code = UtilityCode(
7010 proto = """
7011 static PyObject *__Pyx_GetExcValue(void); /*proto*/
7012 """,
7013 impl = """
7014 static PyObject *__Pyx_GetExcValue(void) {
7015     PyObject *type = 0, *value = 0, *tb = 0;
7016     PyObject *tmp_type, *tmp_value, *tmp_tb;
7017     PyObject *result = 0;
7018     PyThreadState *tstate = PyThreadState_Get();
7019     PyErr_Fetch(&type, &value, &tb);
7020     PyErr_NormalizeException(&type, &value, &tb);
7021     if (PyErr_Occurred())
7022         goto bad;
7023     if (!value) {
7024         value = Py_None;
7025         Py_INCREF(value);
7026     }
7027     tmp_type = tstate->exc_type;
7028     tmp_value = tstate->exc_value;
7029     tmp_tb = tstate->exc_traceback;
7030     tstate->exc_type = type;
7031     tstate->exc_value = value;
7032     tstate->exc_traceback = tb;
7033     /* Make sure tstate is in a consistent state when we XDECREF
7034     these objects (XDECREF may run arbitrary code). */
7035     Py_XDECREF(tmp_type);
7036     Py_XDECREF(tmp_value);
7037     Py_XDECREF(tmp_tb);
7038     result = value;
7039     Py_XINCREF(result);
7040     type = 0;
7041     value = 0;
7042     tb = 0;
7043 bad:
7044     Py_XDECREF(type);
7045     Py_XDECREF(value);
7046     Py_XDECREF(tb);
7047     return result;
7048 }
7049 """)
7050
7051 #------------------------------------------------------------------------------------
7052
7053 type_test_utility_code = UtilityCode(
7054 proto = """
7055 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
7056 """,
7057 impl = """
7058 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
7059     if (unlikely(!type)) {
7060         PyErr_Format(PyExc_SystemError, "Missing type object");
7061         return 0;
7062     }
7063     if (likely(PyObject_TypeCheck(obj, type)))
7064         return 1;
7065     PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
7066                  Py_TYPE(obj)->tp_name, type->tp_name);
7067     return 0;
7068 }
7069 """)
7070
7071 #------------------------------------------------------------------------------------
7072
7073 create_class_utility_code = UtilityCode(
7074 proto = """
7075 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, const char *modname); /*proto*/
7076 """,
7077 impl = """
7078 static PyObject *__Pyx_CreateClass(
7079     PyObject *bases, PyObject *dict, PyObject *name, const char *modname)
7080 {
7081     PyObject *py_modname;
7082     PyObject *result = 0;
7083
7084     #if PY_MAJOR_VERSION < 3
7085     py_modname = PyString_FromString(modname);
7086     #else
7087     py_modname = PyUnicode_FromString(modname);
7088     #endif
7089     if (!py_modname)
7090         goto bad;
7091     if (PyDict_SetItemString(dict, "__module__", py_modname) < 0)
7092         goto bad;
7093     #if PY_MAJOR_VERSION < 3
7094     result = PyClass_New(bases, dict, name);
7095     #else
7096     result = PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, name, bases, dict, NULL);
7097     #endif
7098 bad:
7099     Py_XDECREF(py_modname);
7100     return result;
7101 }
7102 """)
7103
7104 #------------------------------------------------------------------------------------
7105
7106 cpp_exception_utility_code = UtilityCode(
7107 proto = """
7108 #ifndef __Pyx_CppExn2PyErr
7109 static void __Pyx_CppExn2PyErr() {
7110   try {
7111     if (PyErr_Occurred())
7112       ; // let the latest Python exn pass through and ignore the current one
7113     else
7114       throw;
7115   } catch (const std::invalid_argument& exn) {
7116     // Catch a handful of different errors here and turn them into the
7117     // equivalent Python errors.
7118     // Change invalid_argument to ValueError
7119     PyErr_SetString(PyExc_ValueError, exn.what());
7120   } catch (const std::out_of_range& exn) {
7121     // Change out_of_range to IndexError
7122     PyErr_SetString(PyExc_IndexError, exn.what());
7123   } catch (const std::exception& exn) {
7124     PyErr_SetString(PyExc_RuntimeError, exn.what());
7125   }
7126   catch (...)
7127   {
7128     PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
7129   }
7130 }
7131 #endif
7132 """,
7133 impl = ""
7134 )
7135
7136 #------------------------------------------------------------------------------------
7137
7138 raise_noneattr_error_utility_code = UtilityCode(
7139 proto = """
7140 static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname);
7141 """,
7142 impl = '''
7143 static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname) {
7144     PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", attrname);
7145 }
7146 ''')
7147
7148 raise_noneindex_error_utility_code = UtilityCode(
7149 proto = """
7150 static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void);
7151 """,
7152 impl = '''
7153 static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void) {
7154     PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
7155 }
7156 ''')
7157
7158 raise_none_iter_error_utility_code = UtilityCode(
7159 proto = """
7160 static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);
7161 """,
7162 impl = '''
7163 static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
7164     PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
7165 }
7166 ''')
7167
7168 #------------------------------------------------------------------------------------
7169
7170 getitem_dict_utility_code = UtilityCode(
7171 proto = """
7172
7173 #if PY_MAJOR_VERSION >= 3
7174 static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
7175     PyObject *value;
7176     if (unlikely(d == Py_None)) {
7177         __Pyx_RaiseNoneIndexingError();
7178         return NULL;
7179     }
7180     value = PyDict_GetItemWithError(d, key);
7181     if (unlikely(!value)) {
7182         if (!PyErr_Occurred())
7183             PyErr_SetObject(PyExc_KeyError, key);
7184         return NULL;
7185     }
7186     Py_INCREF(value);
7187     return value;
7188 }
7189 #else
7190     #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
7191 #endif
7192 """, 
7193 requires = [raise_noneindex_error_utility_code])
7194
7195 #------------------------------------------------------------------------------------
7196
7197 getitem_int_pyunicode_utility_code = UtilityCode(
7198 proto = '''
7199 #define __Pyx_GetItemInt_Unicode(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7200                                                __Pyx_GetItemInt_Unicode_Fast(o, i) : \\
7201                                                __Pyx_GetItemInt_Unicode_Generic(o, to_py_func(i)))
7202
7203 static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Fast(PyObject* ustring, Py_ssize_t i) {
7204     if (likely((0 <= i) & (i < PyUnicode_GET_SIZE(ustring)))) {
7205         return PyUnicode_AS_UNICODE(ustring)[i];
7206     } else if ((-PyUnicode_GET_SIZE(ustring) <= i) & (i < 0)) {
7207         i += PyUnicode_GET_SIZE(ustring);
7208         return PyUnicode_AS_UNICODE(ustring)[i];
7209     } else {
7210         PyErr_SetString(PyExc_IndexError, "string index out of range");
7211         return (Py_UNICODE)-1;
7212     }
7213 }
7214
7215 static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Generic(PyObject* ustring, PyObject* j) {
7216     Py_UNICODE uchar;
7217     PyObject *uchar_string;
7218     if (!j) return (Py_UNICODE)-1;
7219     uchar_string = PyObject_GetItem(ustring, j);
7220     Py_DECREF(j);
7221     if (!uchar_string) return (Py_UNICODE)-1;
7222     uchar = PyUnicode_AS_UNICODE(uchar_string)[0];
7223     Py_DECREF(uchar_string);
7224     return uchar;
7225 }
7226 ''')
7227
7228 getitem_int_utility_code = UtilityCode(
7229 proto = """
7230
7231 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
7232     PyObject *r;
7233     if (!j) return NULL;
7234     r = PyObject_GetItem(o, j);
7235     Py_DECREF(j);
7236     return r;
7237 }
7238
7239 """ + ''.join([
7240 """
7241 #define __Pyx_GetItemInt_%(type)s(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7242                                                     __Pyx_GetItemInt_%(type)s_Fast(o, i) : \\
7243                                                     __Pyx_GetItemInt_Generic(o, to_py_func(i)))
7244
7245 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_%(type)s_Fast(PyObject *o, Py_ssize_t i) {
7246     if (likely(o != Py_None)) {
7247         if (likely((0 <= i) & (i < Py%(type)s_GET_SIZE(o)))) {
7248             PyObject *r = Py%(type)s_GET_ITEM(o, i);
7249             Py_INCREF(r);
7250             return r;
7251         }
7252         else if ((-Py%(type)s_GET_SIZE(o) <= i) & (i < 0)) {
7253             PyObject *r = Py%(type)s_GET_ITEM(o, Py%(type)s_GET_SIZE(o) + i);
7254             Py_INCREF(r);
7255             return r;
7256         }
7257     }
7258     return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
7259 }
7260 """ % {'type' : type_name} for type_name in ('List', 'Tuple')
7261 ]) + """
7262
7263 #define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7264                                                     __Pyx_GetItemInt_Fast(o, i) : \\
7265                                                     __Pyx_GetItemInt_Generic(o, to_py_func(i)))
7266
7267 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) {
7268     PyObject *r;
7269     if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
7270         r = PyList_GET_ITEM(o, i);
7271         Py_INCREF(r);
7272     }
7273     else if (PyTuple_CheckExact(o) && ((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
7274         r = PyTuple_GET_ITEM(o, i);
7275         Py_INCREF(r);
7276     }
7277     else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0))) {
7278         r = PySequence_GetItem(o, i);
7279     }
7280     else {
7281         r = __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
7282     }
7283     return r;
7284 }
7285 """,
7286 impl = """
7287 """)
7288
7289
7290
7291 #------------------------------------------------------------------------------------
7292
7293 setitem_int_utility_code = UtilityCode(
7294 proto = """
7295 #define __Pyx_SetItemInt(o, i, v, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7296                                                     __Pyx_SetItemInt_Fast(o, i, v) : \\
7297                                                     __Pyx_SetItemInt_Generic(o, to_py_func(i), v))
7298
7299 static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
7300     int r;
7301     if (!j) return -1;
7302     r = PyObject_SetItem(o, j, v);
7303     Py_DECREF(j);
7304     return r;
7305 }
7306
7307 static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v) {
7308     if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
7309         Py_INCREF(v);
7310         Py_DECREF(PyList_GET_ITEM(o, i));
7311         PyList_SET_ITEM(o, i, v);
7312         return 1;
7313     }
7314     else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0)))
7315         return PySequence_SetItem(o, i, v);
7316     else {
7317         PyObject *j = PyInt_FromSsize_t(i);
7318         return __Pyx_SetItemInt_Generic(o, j, v);
7319     }
7320 }
7321 """,
7322 impl = """
7323 """)
7324
7325 #------------------------------------------------------------------------------------
7326
7327 delitem_int_utility_code = UtilityCode(
7328 proto = """
7329 #define __Pyx_DelItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7330                                                     __Pyx_DelItemInt_Fast(o, i) : \\
7331                                                     __Pyx_DelItem_Generic(o, to_py_func(i)))
7332
7333 static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
7334     int r;
7335     if (!j) return -1;
7336     r = PyObject_DelItem(o, j);
7337     Py_DECREF(j);
7338     return r;
7339 }
7340
7341 static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i) {
7342     if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && likely(i >= 0))
7343         return PySequence_DelItem(o, i);
7344     else {
7345         PyObject *j = PyInt_FromSsize_t(i);
7346         return __Pyx_DelItem_Generic(o, j);
7347     }
7348 }
7349 """,
7350 impl = """
7351 """)
7352
7353 #------------------------------------------------------------------------------------
7354
7355 raise_too_many_values_to_unpack = UtilityCode(
7356 proto = """
7357 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
7358 """,
7359 impl = '''
7360 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
7361     PyErr_Format(PyExc_ValueError,
7362         #if PY_VERSION_HEX < 0x02050000
7363             "too many values to unpack (expected %d)", (int)expected);
7364         #else
7365             "too many values to unpack (expected %zd)", expected);
7366         #endif
7367 }
7368 ''')
7369
7370 raise_need_more_values_to_unpack = UtilityCode(
7371 proto = """
7372 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
7373 """,
7374 impl = '''
7375 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
7376     PyErr_Format(PyExc_ValueError,
7377         #if PY_VERSION_HEX < 0x02050000
7378                  "need more than %d value%s to unpack", (int)index,
7379         #else
7380                  "need more than %zd value%s to unpack", index,
7381         #endif
7382                  (index == 1) ? "" : "s");
7383 }
7384 ''')
7385
7386 #------------------------------------------------------------------------------------
7387
7388 tuple_unpacking_error_code = UtilityCode(
7389 proto = """
7390 static void __Pyx_UnpackTupleError(PyObject *, Py_ssize_t index); /*proto*/
7391 """, 
7392 impl = """
7393 static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
7394     if (t == Py_None) {
7395       __Pyx_RaiseNoneNotIterableError();
7396     } else if (PyTuple_GET_SIZE(t) < index) {
7397       __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
7398     } else {
7399       __Pyx_RaiseTooManyValuesError(index);
7400     }
7401 }
7402 """, 
7403 requires = [raise_none_iter_error_utility_code,
7404             raise_need_more_values_to_unpack,
7405             raise_too_many_values_to_unpack]
7406 )
7407
7408 unpacking_utility_code = UtilityCode(
7409 proto = """
7410 static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
7411 static int __Pyx_EndUnpack(PyObject *, Py_ssize_t expected); /*proto*/
7412 """,
7413 impl = """
7414 static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
7415     PyObject *item;
7416     if (!(item = PyIter_Next(iter))) {
7417         if (!PyErr_Occurred()) {
7418             __Pyx_RaiseNeedMoreValuesError(index);
7419         }
7420     }
7421     return item;
7422 }
7423
7424 static int __Pyx_EndUnpack(PyObject *iter, Py_ssize_t expected) {
7425     PyObject *item;
7426     if ((item = PyIter_Next(iter))) {
7427         Py_DECREF(item);
7428         __Pyx_RaiseTooManyValuesError(expected);
7429         return -1;
7430     }
7431     else if (!PyErr_Occurred())
7432         return 0;
7433     else
7434         return -1;
7435 }
7436 """,
7437 requires = [raise_need_more_values_to_unpack,
7438             raise_too_many_values_to_unpack]
7439 )
7440
7441 #------------------------------------------------------------------------------------
7442
7443 # CPython supports calling functions with non-dict kwargs by
7444 # converting them to a dict first
7445
7446 kwargs_call_utility_code = UtilityCode(
7447 proto = """
7448 static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject*, PyObject*, PyObject*); /*proto*/
7449 """,
7450 impl = """
7451 static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject *callable, PyObject *args, PyObject *kwargs) {
7452     PyObject* result;
7453     if (likely(PyDict_Check(kwargs))) {
7454         return PyEval_CallObjectWithKeywords(callable, args, kwargs);
7455     } else {
7456         PyObject* real_dict;
7457         real_dict = PyObject_CallFunctionObjArgs((PyObject*)&PyDict_Type, kwargs, NULL);
7458         if (unlikely(!real_dict))
7459             return NULL;
7460         result = PyEval_CallObjectWithKeywords(callable, args, real_dict);
7461         Py_DECREF(real_dict);
7462         return result; /* may be NULL */
7463     }
7464 }
7465 """, 
7466 )
7467
7468
7469 #------------------------------------------------------------------------------------
7470
7471 int_pow_utility_code = UtilityCode(
7472 proto="""
7473 static CYTHON_INLINE %(type)s %(func_name)s(%(type)s, %(type)s); /* proto */
7474 """,
7475 impl="""
7476 static CYTHON_INLINE %(type)s %(func_name)s(%(type)s b, %(type)s e) {
7477     %(type)s t = b;
7478     switch (e) {
7479         case 3:
7480             t *= b;
7481         case 2:
7482             t *= b;
7483         case 1:
7484             return t;
7485         case 0:
7486             return 1;
7487     }
7488     if (unlikely(e<0)) return 0;
7489     t = 1;
7490     while (likely(e)) {
7491         t *= (b * (e&1)) | ((~e)&1);    /* 1 or b */
7492         b *= b;
7493         e >>= 1;
7494     }
7495     return t;
7496 }
7497 """)
7498
7499 # ------------------------------ Division ------------------------------------
7500
7501 div_int_utility_code = UtilityCode(
7502 proto="""
7503 static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s, %(type)s); /* proto */
7504 """,
7505 impl="""
7506 static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s a, %(type)s b) {
7507     %(type)s q = a / b;
7508     %(type)s r = a - q*b;
7509     q -= ((r != 0) & ((r ^ b) < 0));
7510     return q;
7511 }
7512 """)
7513
7514 mod_int_utility_code = UtilityCode(
7515 proto="""
7516 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
7517 """,
7518 impl="""
7519 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
7520     %(type)s r = a %% b;
7521     r += ((r != 0) & ((r ^ b) < 0)) * b;
7522     return r;
7523 }
7524 """)
7525
7526 mod_float_utility_code = UtilityCode(
7527 proto="""
7528 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
7529 """,
7530 impl="""
7531 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
7532     %(type)s r = fmod%(math_h_modifier)s(a, b);
7533     r += ((r != 0) & ((r < 0) ^ (b < 0))) * b;
7534     return r;
7535 }
7536 """)
7537
7538 cdivision_warning_utility_code = UtilityCode(
7539 proto="""
7540 static int __Pyx_cdivision_warning(void); /* proto */
7541 """,
7542 impl="""
7543 static int __Pyx_cdivision_warning(void) {
7544     return PyErr_WarnExplicit(PyExc_RuntimeWarning, 
7545                               "division with oppositely signed operands, C and Python semantics differ",
7546                               %(FILENAME)s, 
7547                               %(LINENO)s,
7548                               __Pyx_MODULE_NAME,
7549                               NULL);
7550 }
7551 """ % {
7552     'FILENAME': Naming.filename_cname,
7553     'LINENO':  Naming.lineno_cname,
7554 })
7555
7556 # from intobject.c
7557 division_overflow_test_code = UtilityCode(
7558 proto="""
7559 #define UNARY_NEG_WOULD_OVERFLOW(x)     \
7560         (((x) < 0) & ((unsigned long)(x) == 0-(unsigned long)(x)))
7561 """)
7562
7563
7564 binding_cfunc_utility_code = UtilityCode(
7565 proto="""
7566 #define %(binding_cfunc)s_USED 1
7567
7568 typedef struct {
7569     PyCFunctionObject func;
7570 } %(binding_cfunc)s_object;
7571
7572 PyTypeObject %(binding_cfunc)s_type;
7573 PyTypeObject *%(binding_cfunc)s = NULL;
7574
7575 PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module); /* proto */
7576 #define %(binding_cfunc)s_New(ml, self) %(binding_cfunc)s_NewEx(ml, self, NULL)
7577
7578 int %(binding_cfunc)s_init(void); /* proto */
7579 """ % Naming.__dict__,
7580 impl="""
7581
7582 PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
7583         %(binding_cfunc)s_object *op = PyObject_GC_New(%(binding_cfunc)s_object, %(binding_cfunc)s);
7584     if (op == NULL)
7585         return NULL;
7586         op->func.m_ml = ml;
7587         Py_XINCREF(self);
7588         op->func.m_self = self;
7589         Py_XINCREF(module);
7590         op->func.m_module = module;
7591         PyObject_GC_Track(op);
7592         return (PyObject *)op;
7593 }
7594
7595 static void %(binding_cfunc)s_dealloc(%(binding_cfunc)s_object *m) {
7596         PyObject_GC_UnTrack(m);
7597         Py_XDECREF(m->func.m_self);
7598         Py_XDECREF(m->func.m_module);
7599     PyObject_GC_Del(m);
7600 }
7601
7602 static PyObject *%(binding_cfunc)s_descr_get(PyObject *func, PyObject *obj, PyObject *type) {
7603         if (obj == Py_None)
7604                 obj = NULL;
7605         return PyMethod_New(func, obj, type);
7606 }
7607
7608 int %(binding_cfunc)s_init(void) {
7609     %(binding_cfunc)s_type = PyCFunction_Type;
7610     %(binding_cfunc)s_type.tp_name = __Pyx_NAMESTR("cython_binding_builtin_function_or_method");
7611     %(binding_cfunc)s_type.tp_dealloc = (destructor)%(binding_cfunc)s_dealloc;
7612     %(binding_cfunc)s_type.tp_descr_get = %(binding_cfunc)s_descr_get;
7613     if (PyType_Ready(&%(binding_cfunc)s_type) < 0) {
7614         return -1;
7615     }
7616     %(binding_cfunc)s = &%(binding_cfunc)s_type;
7617     return 0;
7618
7619 }
7620 """ % Naming.__dict__)