ExprNodes.py 321 KB
Newer Older
William Stein's avatar
William Stein committed
1 2 3 4
#
#   Pyrex - Parse tree nodes for expressions
#

5 6 7 8 9 10 11 12 13 14
import cython
from cython import set
cython.declare(error=object, warning=object, warn_once=object, InternalError=object,
               CompileError=object, UtilityCode=object, StringEncoding=object, operator=object,
               Naming=object, Nodes=object, PyrexTypes=object, py_object_type=object,
               list_type=object, tuple_type=object, set_type=object, dict_type=object, \
               unicode_type=object, str_type=object, bytes_type=object, type_type=object,
               Builtin=object, Symtab=object, Utils=object, find_coercion_error=object,
               debug_disposal_code=object, debug_temp_alloc=object, debug_coercion=object)

15
import operator
William Stein's avatar
William Stein committed
16

17
from Errors import error, warning, warn_once, InternalError, CompileError
18
from Errors import hold_errors, release_errors, held_errors, report_error
19
from Code import UtilityCode
20
import StringEncoding
William Stein's avatar
William Stein committed
21
import Naming
Robert Bradshaw's avatar
Robert Bradshaw committed
22
import Nodes
William Stein's avatar
William Stein committed
23 24
from Nodes import Node
import PyrexTypes
25 26
from PyrexTypes import py_object_type, c_long_type, typecast, error_type, \
     unspecified_type
27 28
from Builtin import list_type, tuple_type, set_type, dict_type, \
     unicode_type, str_type, bytes_type, type_type
29
import Builtin
William Stein's avatar
William Stein committed
30 31
import Symtab
import Options
32
from Cython import Utils
33
from Annotate import AnnotationItem
William Stein's avatar
William Stein committed
34

William Stein's avatar
William Stein committed
35
from Cython.Debugging import print_call_chain
William Stein's avatar
William Stein committed
36 37 38
from DebugFlags import debug_disposal_code, debug_temp_alloc, \
    debug_coercion

39 40 41 42 43
try:
    from __builtin__ import basestring
except ImportError:
    basestring = str # Python 3

Stefan Behnel's avatar
Stefan Behnel committed
44 45 46 47
class NotConstant(object):
    def __repr__(self):
        return "<NOT CONSTANT>"

48
not_a_constant = NotConstant()
49
constant_value_not_set = object()
50

51 52 53 54 55 56 57 58 59 60
# error messages when coercing from key[0] to key[1]
find_coercion_error = {
    # string related errors
    (Builtin.unicode_type, Builtin.bytes_type) : "Cannot convert Unicode string to 'bytes' implicitly, encoding required.",
    (Builtin.unicode_type, Builtin.str_type)   : "Cannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.",
    (Builtin.unicode_type, PyrexTypes.c_char_ptr_type) : "Unicode objects do not support coercion to C types.",
    (Builtin.bytes_type, Builtin.unicode_type) : "Cannot convert 'bytes' object to unicode implicitly, decoding required",
    (Builtin.bytes_type, Builtin.str_type) : "Cannot convert 'bytes' object to str implicitly. This is not portable to Py3.",
    (Builtin.str_type, Builtin.unicode_type) : "str objects do not support coercion to unicode, use a unicode string literal instead (u'')",
    (Builtin.str_type, Builtin.bytes_type) : "Cannot convert 'str' to 'bytes' implicitly. This is not portable.",
61
    (Builtin.str_type, PyrexTypes.c_char_ptr_type) : "'str' objects do not support coercion to C types (use 'bytes'?).",
62 63 64 65 66
    (PyrexTypes.c_char_ptr_type, Builtin.unicode_type) : "Cannot convert 'char*' to unicode implicitly, decoding required",
    (PyrexTypes.c_uchar_ptr_type, Builtin.unicode_type) : "Cannot convert 'char*' to unicode implicitly, decoding required",
    }.get


William Stein's avatar
William Stein committed
67 68 69 70 71 72
class ExprNode(Node):
    #  subexprs     [string]     Class var holding names of subexpr node attrs
    #  type         PyrexType    Type of the result
    #  result_code  string       Code fragment
    #  result_ctype string       C type of result_code if different from type
    #  is_temp      boolean      Result is in a temporary variable
73
    #  is_sequence_constructor
William Stein's avatar
William Stein committed
74
    #               boolean      Is a list or tuple constructor expression
75
    #  is_starred   boolean      Is a starred expression (e.g. '*a')
William Stein's avatar
William Stein committed
76 77 78
    #  saved_subexpr_nodes
    #               [ExprNode or [ExprNode or None] or None]
    #                            Cached result of subexpr_nodes()
79
    #  use_managed_ref boolean   use ref-counted temps/assignments/etc.
80 81 82
    #  result_is_used  boolean   indicates that the result will be dropped and the
    #                            result_code/temp_result can safely be set to None

William Stein's avatar
William Stein committed
83
    result_ctype = None
84
    type = None
85 86
    temp_code = None
    old_temp = None # error checker for multiple frees etc.
87
    use_managed_ref = True # can be set by optimisation transforms
88
    result_is_used = True
William Stein's avatar
William Stein committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    #  The Analyse Expressions phase for expressions is split
    #  into two sub-phases:
    #
    #    Analyse Types
    #      Determines the result type of the expression based
    #      on the types of its sub-expressions, and inserts
    #      coercion nodes into the expression tree where needed.
    #      Marks nodes which will need to have temporary variables
    #      allocated.
    #
    #    Allocate Temps
    #      Allocates temporary variables where needed, and fills
    #      in the result_code field of each node.
    #
    #  ExprNode provides some convenience routines which
    #  perform both of the above phases. These should only
    #  be called from statement nodes, and only when no
    #  coercion nodes need to be added around the expression
    #  being analysed. In that case, the above two phases
    #  should be invoked separately.
    #
    #  Framework code in ExprNode provides much of the common
    #  processing for the various phases. It makes use of the
    #  'subexprs' class attribute of ExprNodes, which should
    #  contain a list of the names of attributes which can
    #  hold sub-nodes or sequences of sub-nodes.
116 117
    #
    #  The framework makes use of a number of abstract methods.
William Stein's avatar
William Stein committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    #  Their responsibilities are as follows.
    #
    #    Declaration Analysis phase
    #
    #      analyse_target_declaration
    #        Called during the Analyse Declarations phase to analyse
    #        the LHS of an assignment or argument of a del statement.
    #        Nodes which cannot be the LHS of an assignment need not
    #        implement it.
    #
    #    Expression Analysis phase
    #
    #      analyse_types
    #        - Call analyse_types on all sub-expressions.
    #        - Check operand types, and wrap coercion nodes around
    #          sub-expressions where needed.
    #        - Set the type of this node.
    #        - If a temporary variable will be required for the
    #          result, set the is_temp flag of this node.
    #
    #      analyse_target_types
    #        Called during the Analyse Types phase to analyse
140
    #        the LHS of an assignment or argument of a del
William Stein's avatar
William Stein committed
141 142
    #        statement. Similar responsibilities to analyse_types.
    #
143 144 145 146
    #      target_code
    #        Called by the default implementation of allocate_target_temps.
    #        Should return a C lvalue for assigning to the node. The default
    #        implementation calls calculate_result_code.
William Stein's avatar
William Stein committed
147 148 149 150
    #
    #      check_const
    #        - Check that this node and its subnodes form a
    #          legal constant expression. If so, do nothing,
151
    #          otherwise call not_const.
William Stein's avatar
William Stein committed
152
    #
153
    #        The default implementation of check_const
William Stein's avatar
William Stein committed
154 155 156 157 158 159 160 161
    #        assumes that the expression is not constant.
    #
    #      check_const_addr
    #        - Same as check_const, except check that the
    #          expression is a C lvalue whose address is
    #          constant. Otherwise, call addr_not_const.
    #
    #        The default implementation of calc_const_addr
162
    #        assumes that the expression is not a constant
William Stein's avatar
William Stein committed
163 164 165 166 167 168 169 170 171 172 173 174
    #        lvalue.
    #
    #   Code Generation phase
    #
    #      generate_evaluation_code
    #        - Call generate_evaluation_code for sub-expressions.
    #        - Perform the functions of generate_result_code
    #          (see below).
    #        - If result is temporary, call generate_disposal_code
    #          on all sub-expressions.
    #
    #        A default implementation of generate_evaluation_code
175
    #        is provided which uses the following abstract methods:
William Stein's avatar
William Stein committed
176 177 178 179 180 181
    #
    #          generate_result_code
    #            - Generate any C statements necessary to calculate
    #              the result of this node from the results of its
    #              sub-expressions.
    #
182
    #          calculate_result_code
183 184
    #            - Should return a C code fragment evaluating to the
    #              result. This is only called when the result is not
185 186
    #              a temporary.
    #
William Stein's avatar
William Stein committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    #      generate_assignment_code
    #        Called on the LHS of an assignment.
    #        - Call generate_evaluation_code for sub-expressions.
    #        - Generate code to perform the assignment.
    #        - If the assignment absorbed a reference, call
    #          generate_post_assignment_code on the RHS,
    #          otherwise call generate_disposal_code on it.
    #
    #      generate_deletion_code
    #        Called on an argument of a del statement.
    #        - Call generate_evaluation_code for sub-expressions.
    #        - Generate code to perform the deletion.
    #        - Call generate_disposal_code on all sub-expressions.
    #
    #
202

William Stein's avatar
William Stein committed
203 204
    is_sequence_constructor = 0
    is_attribute = 0
205

William Stein's avatar
William Stein committed
206 207
    saved_subexpr_nodes = None
    is_temp = 0
208
    is_target = 0
209
    is_starred = 0
William Stein's avatar
William Stein committed
210

211 212
    constant_result = constant_value_not_set

213 214 215 216
    try:
        _get_child_attrs = operator.attrgetter('subexprs')
    except AttributeError:
        # Python 2.3
217
        def __get_child_attrs(self):
218
            return self.subexprs
219
        _get_child_attrs = __get_child_attrs
220
    child_attrs = property(fget=_get_child_attrs)
221

William Stein's avatar
William Stein committed
222 223 224 225
    def not_implemented(self, method_name):
        print_call_chain(method_name, "not implemented") ###
        raise InternalError(
            "%s.%s not implemented" %
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
226
                (self.__class__.__name__, method_name))
227

William Stein's avatar
William Stein committed
228 229
    def is_lvalue(self):
        return 0
230

William Stein's avatar
William Stein committed
231 232 233 234 235 236 237 238 239 240 241
    def is_ephemeral(self):
        #  An ephemeral node is one whose result is in
        #  a Python temporary and we suspect there are no
        #  other references to it. Certain operations are
        #  disallowed on such values, since they are
        #  likely to result in a dangling pointer.
        return self.type.is_pyobject and self.is_temp

    def subexpr_nodes(self):
        #  Extract a list of subexpression nodes based
        #  on the contents of the subexprs class attribute.
242 243 244
        nodes = []
        for name in self.subexprs:
            item = getattr(self, name)
Stefan Behnel's avatar
Stefan Behnel committed
245 246
            if item is not None:
                if type(item) is list:
247
                    nodes.extend(item)
Stefan Behnel's avatar
Stefan Behnel committed
248 249
                else:
                    nodes.append(item)
250
        return nodes
251

252
    def result(self):
253 254 255
        if self.is_temp:
            return self.temp_code
        else:
256
            return self.calculate_result_code()
257

William Stein's avatar
William Stein committed
258 259
    def result_as(self, type = None):
        #  Return the result code cast to the specified C type.
260
        return typecast(type, self.ctype(), self.result())
261

William Stein's avatar
William Stein committed
262 263 264
    def py_result(self):
        #  Return the result code cast to PyObject *.
        return self.result_as(py_object_type)
265

William Stein's avatar
William Stein committed
266 267 268 269
    def ctype(self):
        #  Return the native C type of the result (i.e. the
        #  C type of the result_code expression).
        return self.result_ctype or self.type
270

271
    def get_constant_c_result_code(self):
272
        # Return the constant value of this node as a result code
273 274 275 276 277 278 279
        # string, or None if the node is not constant.  This method
        # can be called when the constant result code is required
        # before the code generation phase.
        #
        # The return value is a string that can represent a simple C
        # value, a constant C name or a constant C expression.  If the
        # node type depends on Python code, this must return None.
280 281
        return None

282
    def calculate_constant_result(self):
283 284 285 286 287
        # Calculate the constant compile time result value of this
        # expression and store it in ``self.constant_result``.  Does
        # nothing by default, thus leaving ``self.constant_result``
        # unknown.  If valid, the result can be an arbitrary Python
        # value.
288 289 290 291 292 293
        #
        # This must only be called when it is assured that all
        # sub-expressions have a valid constant_result value.  The
        # ConstantFolding transform will do this.
        pass

294 295 296 297
    def has_constant_result(self):
        return self.constant_result is not constant_value_not_set and \
               self.constant_result is not not_a_constant

298 299 300
    def compile_time_value(self, denv):
        #  Return value of compile-time expression, or report error.
        error(self.pos, "Invalid compile-time expression")
301

302 303 304
    def compile_time_value_error(self, e):
        error(self.pos, "Error in compile-time expression: %s: %s" % (
            e.__class__.__name__, e))
305

William Stein's avatar
William Stein committed
306
    # ------------- Declaration Analysis ----------------
307

William Stein's avatar
William Stein committed
308 309
    def analyse_target_declaration(self, env):
        error(self.pos, "Cannot assign to or delete this")
310

William Stein's avatar
William Stein committed
311
    # ------------- Expression Analysis ----------------
312

William Stein's avatar
William Stein committed
313 314 315 316 317 318
    def analyse_const_expression(self, env):
        #  Called during the analyse_declarations phase of a
        #  constant expression. Analyses the expression's type,
        #  checks whether it is a legal const expression,
        #  and determines its value.
        self.analyse_types(env)
319
        return self.check_const()
320

William Stein's avatar
William Stein committed
321 322
    def analyse_expressions(self, env):
        #  Convenience routine performing both the Type
323
        #  Analysis and Temp Allocation phases for a whole
William Stein's avatar
William Stein committed
324 325
        #  expression.
        self.analyse_types(env)
326

327
    def analyse_target_expression(self, env, rhs):
William Stein's avatar
William Stein committed
328 329 330 331
        #  Convenience routine performing both the Type
        #  Analysis and Temp Allocation phases for the LHS of
        #  an assignment.
        self.analyse_target_types(env)
332

William Stein's avatar
William Stein committed
333 334 335 336 337
    def analyse_boolean_expression(self, env):
        #  Analyse expression and coerce to a boolean.
        self.analyse_types(env)
        bool = self.coerce_to_boolean(env)
        return bool
338

William Stein's avatar
William Stein committed
339 340 341 342 343 344 345 346 347
    def analyse_temp_boolean_expression(self, env):
        #  Analyse boolean expression and coerce result into
        #  a temporary. This is used when a branch is to be
        #  performed on the result and we won't have an
        #  opportunity to ensure disposal code is executed
        #  afterwards. By forcing the result into a temporary,
        #  we ensure that all disposal has been done by the
        #  time we get the result.
        self.analyse_types(env)
Stefan Behnel's avatar
Stefan Behnel committed
348 349
        return self.coerce_to_boolean(env).coerce_to_simple(env)

350
    # --------------- Type Inference -----------------
351

Robert Bradshaw's avatar
Robert Bradshaw committed
352
    def type_dependencies(self, env):
353 354 355 356
        # Returns the list of entries whose types must be determined
        # before the type of self can be infered.
        if hasattr(self, 'type') and self.type is not None:
            return ()
Robert Bradshaw's avatar
Robert Bradshaw committed
357
        return sum([node.type_dependencies(env) for node in self.subexpr_nodes()], ())
358

359
    def infer_type(self, env):
360 361
        # Attempt to deduce the type of self.
        # Differs from analyse_types as it avoids unnecessary
362 363 364 365 366 367 368 369
        # analysis of subexpressions, but can assume everything
        # in self.type_dependencies() has been resolved.
        if hasattr(self, 'type') and self.type is not None:
            return self.type
        elif hasattr(self, 'entry') and self.entry is not None:
            return self.entry.type
        else:
            self.not_implemented("infer_type")
370

371 372 373
    def nonlocally_immutable(self):
        # Returns whether this variable is a safe reference, i.e.
        # can't be modified as part of globals or closures.
374
        return self.is_temp or self.type.is_array or self.type.is_cfunction
375

William Stein's avatar
William Stein committed
376
    # --------------- Type Analysis ------------------
377

William Stein's avatar
William Stein committed
378 379 380 381
    def analyse_as_module(self, env):
        # If this node can be interpreted as a reference to a
        # cimported module, return its scope, else None.
        return None
382

383 384 385 386
    def analyse_as_type(self, env):
        # If this node can be interpreted as a reference to a
        # type, return that type, else None.
        return None
387

William Stein's avatar
William Stein committed
388 389 390 391
    def analyse_as_extension_type(self, env):
        # If this node can be interpreted as a reference to an
        # extension type, return its type, else None.
        return None
392

William Stein's avatar
William Stein committed
393 394
    def analyse_types(self, env):
        self.not_implemented("analyse_types")
395

William Stein's avatar
William Stein committed
396 397
    def analyse_target_types(self, env):
        self.analyse_types(env)
398

399
    def nogil_check(self, env):
400 401 402 403
        # By default, any expression based on Python objects is
        # prevented in nogil environments.  Subtypes must override
        # this if they can work without the GIL.
        if self.type.is_pyobject:
404
            self.gil_error()
405

406 407 408 409
    def gil_assignment_check(self, env):
        if env.nogil and self.type.is_pyobject:
            error(self.pos, "Assignment of Python object not allowed without gil")

William Stein's avatar
William Stein committed
410 411
    def check_const(self):
        self.not_const()
412
        return False
413

William Stein's avatar
William Stein committed
414 415
    def not_const(self):
        error(self.pos, "Not allowed in a constant expression")
416

William Stein's avatar
William Stein committed
417 418
    def check_const_addr(self):
        self.addr_not_const()
419
        return False
420

William Stein's avatar
William Stein committed
421 422
    def addr_not_const(self):
        error(self.pos, "Address is not constant")
423

William Stein's avatar
William Stein committed
424
    # ----------------- Result Allocation -----------------
425

William Stein's avatar
William Stein committed
426 427 428 429 430 431
    def result_in_temp(self):
        #  Return true if result is in a temporary owned by
        #  this node or one of its subexpressions. Overridden
        #  by certain nodes which can share the result of
        #  a subnode.
        return self.is_temp
432

William Stein's avatar
William Stein committed
433 434 435
    def target_code(self):
        #  Return code fragment for use as LHS of a C assignment.
        return self.calculate_result_code()
436

William Stein's avatar
William Stein committed
437 438
    def calculate_result_code(self):
        self.not_implemented("calculate_result_code")
439

Robert Bradshaw's avatar
Robert Bradshaw committed
440 441 442
#    def release_target_temp(self, env):
#        #  Release temporaries used by LHS of an assignment.
#        self.release_subexpr_temps(env)
William Stein's avatar
William Stein committed
443

444 445
    def allocate_temp_result(self, code):
        if self.temp_code:
446
            raise RuntimeError("Temp allocated multiple times in %r: %r" % (self.__class__.__name__, self.pos))
447 448 449 450 451
        type = self.type
        if not type.is_void:
            if type.is_pyobject:
                type = PyrexTypes.py_object_type
            self.temp_code = code.funcstate.allocate_temp(
452
                type, manage_ref=self.use_managed_ref)
453 454 455 456 457
        else:
            self.temp_code = None

    def release_temp_result(self, code):
        if not self.temp_code:
458 459 460
            if not self.result_is_used:
                # not used anyway, so ignore if not set up
                return
461 462 463 464 465 466 467 468 469 470
            if self.old_temp:
                raise RuntimeError("temp %s released multiple times in %s" % (
                        self.old_temp, self.__class__.__name__))
            else:
                raise RuntimeError("no temp, but release requested in %s" % (
                        self.__class__.__name__))
        code.funcstate.release_temp(self.temp_code)
        self.old_temp = self.temp_code
        self.temp_code = None

William Stein's avatar
William Stein committed
471
    # ---------------- Code Generation -----------------
472

William Stein's avatar
William Stein committed
473 474 475 476
    def make_owned_reference(self, code):
        #  If result is a pyobject, make sure we own
        #  a reference to it.
        if self.type.is_pyobject and not self.result_in_temp():
477
            code.put_incref(self.result(), self.ctype())
478

William Stein's avatar
William Stein committed
479
    def generate_evaluation_code(self, code):
480
        code.mark_pos(self.pos)
481

William Stein's avatar
William Stein committed
482 483 484 485
        #  Generate code to evaluate this node and
        #  its sub-expressions, and dispose of any
        #  temporary results of its sub-expressions.
        self.generate_subexpr_evaluation_code(code)
486 487 488 489

        if self.is_temp:
            self.allocate_temp_result(code)

William Stein's avatar
William Stein committed
490 491
        self.generate_result_code(code)
        if self.is_temp:
492 493
            # If we are temp we do not need to wait until this node is disposed
            # before disposing children.
William Stein's avatar
William Stein committed
494
            self.generate_subexpr_disposal_code(code)
495
            self.free_subexpr_temps(code)
496

William Stein's avatar
William Stein committed
497 498 499
    def generate_subexpr_evaluation_code(self, code):
        for node in self.subexpr_nodes():
            node.generate_evaluation_code(code)
500

William Stein's avatar
William Stein committed
501 502
    def generate_result_code(self, code):
        self.not_implemented("generate_result_code")
503

504 505
    def generate_disposal_code(self, code):
        if self.is_temp:
506
            if self.type.is_pyobject and self.result():
507
                code.put_decref_clear(self.result(), self.ctype())
William Stein's avatar
William Stein committed
508
        else:
509
            # Already done if self.is_temp
510
            self.generate_subexpr_disposal_code(code)
511

William Stein's avatar
William Stein committed
512 513 514 515 516
    def generate_subexpr_disposal_code(self, code):
        #  Generate code to dispose of temporary results
        #  of all sub-expressions.
        for node in self.subexpr_nodes():
            node.generate_disposal_code(code)
517

William Stein's avatar
William Stein committed
518 519 520
    def generate_post_assignment_code(self, code):
        if self.is_temp:
            if self.type.is_pyobject:
521
                code.putln("%s = 0;" % self.result())
William Stein's avatar
William Stein committed
522 523
        else:
            self.generate_subexpr_disposal_code(code)
524

William Stein's avatar
William Stein committed
525 526
    def generate_assignment_code(self, rhs, code):
        #  Stub method for nodes which are not legal as
527
        #  the LHS of an assignment. An error will have
William Stein's avatar
William Stein committed
528 529
        #  been reported earlier.
        pass
530

William Stein's avatar
William Stein committed
531 532 533 534 535
    def generate_deletion_code(self, code):
        #  Stub method for nodes that are not legal as
        #  the argument of a del statement. An error
        #  will have been reported earlier.
        pass
536 537

    def free_temps(self, code):
538 539 540 541
        if self.is_temp:
            if not self.type.is_void:
                self.release_temp_result(code)
        else:
542
            self.free_subexpr_temps(code)
543

544 545 546 547
    def free_subexpr_temps(self, code):
        for sub in self.subexpr_nodes():
            sub.free_temps(code)

548 549 550
    def generate_function_definitions(self, env, code):
        pass

551
    # ---------------- Annotation ---------------------
552

553 554 555
    def annotate(self, code):
        for node in self.subexpr_nodes():
            node.annotate(code)
556

William Stein's avatar
William Stein committed
557
    # ----------------- Coercion ----------------------
558

William Stein's avatar
William Stein committed
559 560 561 562 563 564 565 566
    def coerce_to(self, dst_type, env):
        #   Coerce the result so that it can be assigned to
        #   something of type dst_type. If processing is necessary,
        #   wraps this node in a coercion node and returns that.
        #   Otherwise, returns this node unchanged.
        #
        #   This method is called during the analyse_expressions
        #   phase of the src_node's processing.
567 568 569 570 571 572 573 574
        #
        #   Note that subclasses that override this (especially
        #   ConstNodes) must not (re-)set their own .type attribute
        #   here.  Since expression nodes may turn up in different
        #   places in the tree (e.g. inside of CloneNodes in cascaded
        #   assignments), this method must return a new node instance
        #   if it changes the type.
        #
William Stein's avatar
William Stein committed
575 576 577 578
        src = self
        src_type = self.type
        src_is_py_type = src_type.is_pyobject
        dst_is_py_type = dst_type.is_pyobject
579

580 581 582
        if self.check_for_coercion_error(dst_type):
            return self

583 584
        if dst_type.is_reference:
            dst_type = dst_type.ref_base_type
585

William Stein's avatar
William Stein committed
586 587
        if dst_type.is_pyobject:
            if not src.type.is_pyobject:
588 589 590 591
                if dst_type is bytes_type and src.type.is_int:
                    src = CoerceIntToBytesNode(src, env)
                else:
                    src = CoerceToPyTypeNode(src, env)
William Stein's avatar
William Stein committed
592
            if not src.type.subtype_of(dst_type):
593 594
                if not isinstance(src, NoneNode):
                    src = PyTypeTestNode(src, dst_type, env)
William Stein's avatar
William Stein committed
595 596
        elif src.type.is_pyobject:
            src = CoerceFromPyTypeNode(dst_type, src, env)
597
        elif (dst_type.is_complex
598 599
              and src_type != dst_type
              and dst_type.assignable_from(src_type)):
600
            src = CoerceToComplexNode(src, dst_type, env)
William Stein's avatar
William Stein committed
601
        else: # neither src nor dst are py types
602
            # Added the string comparison, since for c types that
603
            # is enough, but Cython gets confused when the types are
604
            # in different pxi files.
605
            if not (str(src.type) == str(dst_type) or dst_type.assignable_from(src_type)):
606
                self.fail_assignment(dst_type)
William Stein's avatar
William Stein committed
607 608
        return src

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    def fail_assignment(self, dst_type):
        error(self.pos, "Cannot assign type '%s' to '%s'" % (self.type, dst_type))

    def check_for_coercion_error(self, dst_type, fail=False, default=None):
        if fail and not default:
            default = "Cannot assign type '%(FROM)s' to '%(TO)s'"
        message = find_coercion_error((self.type, dst_type), default)
        if message is not None:
            error(self.pos, message % {'FROM': self.type, 'TO': dst_type})
            return True
        if fail:
            self.fail_assignment(dst_type)
            return True
        return False

William Stein's avatar
William Stein committed
624 625 626 627 628 629
    def coerce_to_pyobject(self, env):
        return self.coerce_to(PyrexTypes.py_object_type, env)

    def coerce_to_boolean(self, env):
        #  Coerce result to something acceptable as
        #  a boolean value.
630 631 632 633 634 635 636

        # if it's constant, calculate the result now
        if self.has_constant_result():
            bool_value = bool(self.constant_result)
            return BoolNode(self.pos, value=bool_value,
                            constant_result=bool_value)

William Stein's avatar
William Stein committed
637 638 639 640
        type = self.type
        if type.is_pyobject or type.is_ptr or type.is_float:
            return CoerceToBooleanNode(self, env)
        else:
641
            if not (type.is_int or type.is_enum or type.is_error):
642
                error(self.pos,
William Stein's avatar
William Stein committed
643 644
                    "Type '%s' not acceptable as a boolean" % type)
            return self
645

William Stein's avatar
William Stein committed
646 647 648 649 650 651
    def coerce_to_integer(self, env):
        # If not already some C integer type, coerce to longint.
        if self.type.is_int:
            return self
        else:
            return self.coerce_to(PyrexTypes.c_long_type, env)
652

William Stein's avatar
William Stein committed
653 654 655 656 657 658
    def coerce_to_temp(self, env):
        #  Ensure that the result is in a temporary.
        if self.result_in_temp():
            return self
        else:
            return CoerceToTempNode(self, env)
659

William Stein's avatar
William Stein committed
660 661 662 663 664 665
    def coerce_to_simple(self, env):
        #  Ensure that the result is simple (see is_simple).
        if self.is_simple():
            return self
        else:
            return self.coerce_to_temp(env)
666

William Stein's avatar
William Stein committed
667 668 669 670 671 672
    def is_simple(self):
        #  A node is simple if its result is something that can
        #  be referred to without performing any operations, e.g.
        #  a constant, local var, C global var, struct member
        #  reference, or temporary.
        return self.result_in_temp()
673 674

    def may_be_none(self):
675 676 677 678 679
        if not self.type.is_pyobject:
            return False
        if self.constant_result not in (not_a_constant, constant_value_not_set):
            return self.constant_result is not None
        return True
680

681
    def as_cython_attribute(self):
682
        return None
William Stein's avatar
William Stein committed
683

684
    def as_none_safe_node(self, message, error="PyExc_TypeError"):
685 686 687 688 689 690 691 692
        # Wraps the node in a NoneCheckNode if it is not known to be
        # not-None (e.g. because it is a Python literal).
        if self.may_be_none():
            return NoneCheckNode(self, error, message)
        else:
            return self


William Stein's avatar
William Stein committed
693
class AtomicExprNode(ExprNode):
694 695
    #  Abstract base class for expression nodes which have
    #  no sub-expressions.
696

697 698 699
    subexprs = []

    # Override to optimize -- we know we have no children
700 701 702 703
    def generate_subexpr_evaluation_code(self, code):
        pass
    def generate_subexpr_disposal_code(self, code):
        pass
704

705
class PyConstNode(AtomicExprNode):
William Stein's avatar
William Stein committed
706
    #  Abstract base class for constant Python values.
707

708
    is_literal = 1
709
    type = py_object_type
710

William Stein's avatar
William Stein committed
711 712
    def is_simple(self):
        return 1
713 714 715 716

    def may_be_none(self):
        return False

William Stein's avatar
William Stein committed
717
    def analyse_types(self, env):
718
        pass
719

William Stein's avatar
William Stein committed
720 721 722 723 724 725 726 727 728
    def calculate_result_code(self):
        return self.value

    def generate_result_code(self, code):
        pass


class NoneNode(PyConstNode):
    #  The constant value None
729

William Stein's avatar
William Stein committed
730
    value = "Py_None"
731 732

    constant_result = None
733

734
    nogil_check = None
735

736 737
    def compile_time_value(self, denv):
        return None
738 739 740 741 742

    def may_be_none(self):
        return True


William Stein's avatar
William Stein committed
743 744
class EllipsisNode(PyConstNode):
    #  '...' in a subscript list.
745

William Stein's avatar
William Stein committed
746 747
    value = "Py_Ellipsis"

748 749
    constant_result = Ellipsis

750 751 752
    def compile_time_value(self, denv):
        return Ellipsis

William Stein's avatar
William Stein committed
753

754
class ConstNode(AtomicExprNode):
William Stein's avatar
William Stein committed
755 756 757
    # Abstract base type for literal constant nodes.
    #
    # value     string      C code fragment
758

William Stein's avatar
William Stein committed
759
    is_literal = 1
760
    nogil_check = None
761

William Stein's avatar
William Stein committed
762 763
    def is_simple(self):
        return 1
764

765 766 767
    def nonlocally_immutable(self):
        return 1

768 769 770
    def may_be_none(self):
        return False

William Stein's avatar
William Stein committed
771 772
    def analyse_types(self, env):
        pass # Types are held in class variables
773

William Stein's avatar
William Stein committed
774
    def check_const(self):
775
        return True
776

777
    def get_constant_c_result_code(self):
778 779
        return self.calculate_result_code()

William Stein's avatar
William Stein committed
780 781 782 783 784 785 786
    def calculate_result_code(self):
        return str(self.value)

    def generate_result_code(self, code):
        pass


787 788 789
class BoolNode(ConstNode):
    type = PyrexTypes.c_bint_type
    #  The constant value True or False
790 791 792 793

    def calculate_constant_result(self):
        self.constant_result = self.value

794 795
    def compile_time_value(self, denv):
        return self.value
796

797
    def calculate_result_code(self):
798
        return str(int(self.value))
799

800

William Stein's avatar
William Stein committed
801 802
class NullNode(ConstNode):
    type = PyrexTypes.c_null_ptr_type
803
    value = "NULL"
804
    constant_result = 0
William Stein's avatar
William Stein committed
805

806
    def get_constant_c_result_code(self):
807 808
        return self.value

William Stein's avatar
William Stein committed
809 810 811

class CharNode(ConstNode):
    type = PyrexTypes.c_char_type
812 813 814

    def calculate_constant_result(self):
        self.constant_result = ord(self.value)
815

816
    def compile_time_value(self, denv):
817
        return ord(self.value)
818

William Stein's avatar
William Stein committed
819
    def calculate_result_code(self):
820
        return "'%s'" % StringEncoding.escape_char(self.value)
William Stein's avatar
William Stein committed
821 822 823


class IntNode(ConstNode):
824 825 826

    # unsigned     "" or "U"
    # longness     "" or "L" or "LL"
827
    # is_c_literal   True/False/None   creator considers this a C integer literal
828 829 830

    unsigned = ""
    longness = ""
831
    is_c_literal = None # unknown
832 833 834

    def __init__(self, pos, **kwds):
        ExprNode.__init__(self, pos, **kwds)
Robert Bradshaw's avatar
Robert Bradshaw committed
835
        if 'type' not in kwds:
836 837 838 839 840 841 842 843
            self.type = self.find_suitable_type_for_value()

    def find_suitable_type_for_value(self):
        if self.constant_result is constant_value_not_set:
            try:
                self.calculate_constant_result()
            except ValueError:
                pass
844 845 846 847
        # we ignore 'is_c_literal = True' and instead map signed 32bit
        # integers as C long values
        if self.is_c_literal or \
               self.constant_result in (constant_value_not_set, not_a_constant) or \
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
               self.unsigned or self.longness == 'LL':
            # clearly a C literal
            rank = (self.longness == 'LL') and 2 or 1
            suitable_type = PyrexTypes.modifiers_and_name_to_type[not self.unsigned, rank, "int"]
            if self.type:
                suitable_type = PyrexTypes.widest_numeric_type(suitable_type, self.type)
        else:
            # C literal or Python literal - split at 32bit boundary
            if self.constant_result >= -2**31 and self.constant_result < 2**31:
                if self.type and self.type.is_int:
                    suitable_type = self.type
                else:
                    suitable_type = PyrexTypes.c_long_type
            else:
                suitable_type = PyrexTypes.py_object_type
        return suitable_type
William Stein's avatar
William Stein committed
864

865
    def coerce_to(self, dst_type, env):
866
        if self.type is dst_type:
867
            return self
868
        elif dst_type.is_float:
869
            if self.constant_result is not not_a_constant:
870 871
                return FloatNode(self.pos, value='%d.0' % int(self.constant_result), type=dst_type,
                                 constant_result=float(self.constant_result))
872 873 874
            else:
                return FloatNode(self.pos, value=self.value, type=dst_type,
                                 constant_result=not_a_constant)
875
        if dst_type.is_numeric and not dst_type.is_complex:
876
            node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
877 878
                           type = dst_type, is_c_literal = True,
                           unsigned=self.unsigned, longness=self.longness)
879
            return node
880 881
        elif dst_type.is_pyobject:
            node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
882 883
                           type = PyrexTypes.py_object_type, is_c_literal = False,
                           unsigned=self.unsigned, longness=self.longness)
884
        else:
885 886
            # FIXME: not setting the type here to keep it working with
            # complex numbers. Should they be special cased?
887 888
            node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
                           unsigned=self.unsigned, longness=self.longness)
889 890 891
        # We still need to perform normal coerce_to processing on the
        # result, because we might be coercing to an extension type,
        # in which case a type test node will be needed.
892 893
        return ConstNode.coerce_to(node, dst_type, env)

894
    def coerce_to_boolean(self, env):
895 896 897 898
        return IntNode(
            self.pos, value=self.value,
            type = PyrexTypes.c_bint_type,
            unsigned=self.unsigned, longness=self.longness)
899

900
    def generate_evaluation_code(self, code):
901
        if self.type.is_pyobject:
902
            # pre-allocate a Python version of the number
903 904
            plain_integer_string = self.value_as_c_integer_string(plain_digits=True)
            self.result_code = code.get_py_num(plain_integer_string, self.longness)
905
        else:
906
            self.result_code = self.get_constant_c_result_code()
907

908
    def get_constant_c_result_code(self):
909 910 911
        return self.value_as_c_integer_string() + self.unsigned + self.longness

    def value_as_c_integer_string(self, plain_digits=False):
912 913 914 915
        value = self.value
        if isinstance(value, basestring) and len(value) > 2:
            # must convert C-incompatible Py3 oct/bin notations
            if value[1] in 'oO':
916 917 918 919
                if plain_digits:
                    value = int(value[2:], 8)
                else:
                    value = value[0] + value[2:] # '0o123' => '0123'
920 921
            elif value[1] in 'bB':
                value = int(value[2:], 2)
922 923 924
            elif plain_digits and value[1] in 'xX':
                value = int(value[2:], 16)
        return str(value)
925 926 927

    def calculate_result_code(self):
        return self.result_code
William Stein's avatar
William Stein committed
928

929
    def calculate_constant_result(self):
930
        self.constant_result = Utils.str_to_number(self.value)
931

932
    def compile_time_value(self, denv):
933
        return Utils.str_to_number(self.value)
934 935


William Stein's avatar
William Stein committed
936 937 938
class FloatNode(ConstNode):
    type = PyrexTypes.c_double_type

939
    def calculate_constant_result(self):
940
        self.constant_result = float(self.value)
941

942 943
    def compile_time_value(self, denv):
        return float(self.value)
944

Stefan Behnel's avatar
Stefan Behnel committed
945
    def calculate_result_code(self):
946 947 948 949
        strval = self.value
        assert isinstance(strval, (str, unicode))
        cmpval = repr(float(strval))
        if cmpval == 'nan':
950
            return "(Py_HUGE_VAL * 0)"
951
        elif cmpval == 'inf':
952
            return "Py_HUGE_VAL"
953
        elif cmpval == '-inf':
954
            return "(-Py_HUGE_VAL)"
Stefan Behnel's avatar
Stefan Behnel committed
955 956
        else:
            return strval
957

William Stein's avatar
William Stein committed
958

959
class BytesNode(ConstNode):
960 961 962 963
    # A char* or bytes literal
    #
    # value      BytesLiteral

964 965
    # start off as Python 'bytes' to support len() in O(1)
    type = bytes_type
966 967

    def compile_time_value(self, denv):
968
        return self.value
969

970
    def analyse_as_type(self, env):
971
        type = PyrexTypes.parse_basic_type(self.value)
972
        if type is not None:
973
            return type
974 975 976 977 978 979 980
        from TreeFragment import TreeFragment
        pos = (self.pos[0], self.pos[1], self.pos[2]-7)
        declaration = TreeFragment(u"sizeof(%s)" % self.value, name=pos[0].filename, initial_pos=pos)
        sizeof_node = declaration.root.stats[0].expr
        sizeof_node.analyse_types(env)
        if isinstance(sizeof_node, SizeofTypeNode):
            return sizeof_node.arg_type
981

982 983 984
    def can_coerce_to_char_literal(self):
        return len(self.value) == 1

985
    def coerce_to_boolean(self, env):
986 987
        # This is special because testing a C char* for truth directly
        # would yield the wrong result.
988 989
        bool_value = bool(self.value)
        return BoolNode(self.pos, value=bool_value, constant_result=bool_value)
990

William Stein's avatar
William Stein committed
991
    def coerce_to(self, dst_type, env):
992 993
        if self.type == dst_type:
            return self
994
        if dst_type.is_int:
995
            if not self.can_coerce_to_char_literal():
996 997
                error(self.pos, "Only single-character string literals can be coerced into ints.")
                return self
Stefan Behnel's avatar
Stefan Behnel committed
998 999
            if dst_type.is_unicode_char:
                error(self.pos, "Bytes literals cannot coerce to Py_UNICODE/Py_UCS4, use a unicode literal instead.")
1000
                return self
1001 1002
            return CharNode(self.pos, value=self.value)

1003
        node = BytesNode(self.pos, value=self.value)
1004 1005 1006 1007 1008 1009 1010 1011
        if dst_type.is_pyobject:
            if dst_type in (py_object_type, Builtin.bytes_type):
                node.type = Builtin.bytes_type
            else:
                self.check_for_coercion_error(dst_type, fail=True)
                return node
        elif dst_type == PyrexTypes.c_char_ptr_type:
            node.type = dst_type
1012 1013 1014 1015
            return node
        elif dst_type == PyrexTypes.c_uchar_ptr_type:
            node.type = PyrexTypes.c_char_ptr_type
            return CastNode(node, PyrexTypes.c_uchar_ptr_type)
1016 1017
        elif dst_type.assignable_from(PyrexTypes.c_char_ptr_type):
            node.type = dst_type
1018
            return node
1019

William Stein's avatar
William Stein committed
1020 1021 1022 1023 1024
        # We still need to perform normal coerce_to processing on the
        # result, because we might be coercing to an extension type,
        # in which case a type test node will be needed.
        return ConstNode.coerce_to(node, dst_type, env)

1025
    def generate_evaluation_code(self, code):
William Stein's avatar
William Stein committed
1026
        if self.type.is_pyobject:
1027
            self.result_code = code.get_py_string_const(self.value)
William Stein's avatar
William Stein committed
1028
        else:
1029
            self.result_code = code.get_string_const(self.value)
1030

1031
    def get_constant_c_result_code(self):
1032
        return None # FIXME
1033

1034 1035
    def calculate_result_code(self):
        return self.result_code
William Stein's avatar
William Stein committed
1036 1037


1038
class UnicodeNode(PyConstNode):
1039 1040
    # A Python unicode object
    #
1041 1042
    # value        EncodedString
    # bytes_value  BytesLiteral    the literal parsed as bytes string ('-3' unicode literals only)
Robert Bradshaw's avatar
Robert Bradshaw committed
1043

1044
    bytes_value = None
1045
    type = unicode_type
1046

1047
    def coerce_to(self, dst_type, env):
1048 1049
        if dst_type is self.type:
            pass
Stefan Behnel's avatar
Stefan Behnel committed
1050
        elif dst_type.is_unicode_char:
1051
            if not self.can_coerce_to_char_literal():
Stefan Behnel's avatar
Stefan Behnel committed
1052
                error(self.pos, "Only single-character Unicode string literals or surrogate pairs can be coerced into Py_UCS4/Py_UNICODE.")
1053 1054
                return self
            int_value = ord(self.value)
Stefan Behnel's avatar
Stefan Behnel committed
1055
            return IntNode(self.pos, type=dst_type, value=str(int_value), constant_result=int_value)
1056
        elif not dst_type.is_pyobject:
1057 1058 1059
            if dst_type.is_string and self.bytes_value is not None:
                # special case: '-3' enforced unicode literal used in a C char* context
                return BytesNode(self.pos, value=self.bytes_value).coerce_to(dst_type, env)
Stefan Behnel's avatar
Stefan Behnel committed
1060
            error(self.pos, "Unicode literals do not support coercion to C types other than Py_UNICODE or Py_UCS4.")
1061 1062 1063 1064
        elif dst_type is not py_object_type:
            if not self.check_for_coercion_error(dst_type):
                self.fail_assignment(dst_type)
        return self
1065

1066 1067
    def can_coerce_to_char_literal(self):
        return len(self.value) == 1
Stefan Behnel's avatar
Stefan Behnel committed
1068 1069 1070
            ## or (len(self.value) == 2
            ##     and (0xD800 <= self.value[0] <= 0xDBFF)
            ##     and (0xDC00 <= self.value[1] <= 0xDFFF))
1071

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    def contains_surrogates(self):
        # Check if the unicode string contains surrogate code points
        # on a CPython platform with wide (UCS-4) or narrow (UTF-16)
        # Unicode, i.e. characters that would be spelled as two
        # separate code units on a narrow platform.
        for c in map(ord, self.value):
            if c > 65535: # can only happen on wide platforms
                return True
            # We only look for the first code unit (D800-DBFF) of a
            # surrogate pair - if we find one, the other one
            # (DC00-DFFF) is likely there, too.  If we don't find it,
            # any second code unit cannot make for a surrogate pair by
            # itself.
            if c >= 0xD800 and c <= 0xDBFF:
                return True
        return False

1089
    def generate_evaluation_code(self, code):
1090
        self.result_code = code.get_py_string_const(self.value)
1091 1092 1093

    def calculate_result_code(self):
        return self.result_code
1094

1095 1096
    def compile_time_value(self, env):
        return self.value
1097 1098


1099 1100 1101 1102
class StringNode(PyConstNode):
    # A Python str object, i.e. a byte string in Python 2.x and a
    # unicode string in Python 3.x
    #
1103 1104
    # value          BytesLiteral (or EncodedString with ASCII content)
    # unicode_value  EncodedString or None
1105
    # is_identifier  boolean
1106

1107
    type = str_type
1108
    is_identifier = None
1109
    unicode_value = None
1110

1111
    def coerce_to(self, dst_type, env):
1112
        if dst_type is not py_object_type and not str_type.subtype_of(dst_type):
1113 1114 1115 1116 1117
#            if dst_type is Builtin.bytes_type:
#                # special case: bytes = 'str literal'
#                return BytesNode(self.pos, value=self.value)
            if not dst_type.is_pyobject:
                return BytesNode(self.pos, value=self.value).coerce_to(dst_type, env)
1118
            self.check_for_coercion_error(dst_type, fail=True)
1119
        return self
1120

1121 1122
    def can_coerce_to_char_literal(self):
        return not self.is_identifier and len(self.value) == 1
1123

1124
    def generate_evaluation_code(self, code):
1125
        self.result_code = code.get_py_string_const(
1126 1127
            self.value, identifier=self.is_identifier, is_str=True,
            unicode_value=self.unicode_value)
1128

1129
    def get_constant_c_result_code(self):
1130 1131
        return None

1132
    def calculate_result_code(self):
1133
        return self.result_code
1134

1135 1136
    def compile_time_value(self, env):
        return self.value
1137 1138


1139 1140 1141 1142
class IdentifierStringNode(StringNode):
    # A special str value that represents an identifier (bytes in Py2,
    # unicode in Py3).
    is_identifier = True
1143 1144


1145
class LongNode(AtomicExprNode):
William Stein's avatar
William Stein committed
1146 1147 1148
    #  Python long integer literal
    #
    #  value   string
1149

1150 1151
    type = py_object_type

1152
    def calculate_constant_result(self):
1153
        self.constant_result = Utils.str_to_number(self.value)
1154

1155
    def compile_time_value(self, denv):
1156
        return Utils.str_to_number(self.value)
1157

William Stein's avatar
William Stein committed
1158 1159
    def analyse_types(self, env):
        self.is_temp = 1
1160

1161 1162 1163
    def may_be_none(self):
        return False

1164 1165
    gil_message = "Constructing Python long int"

1166
    def generate_result_code(self, code):
William Stein's avatar
William Stein committed
1167
        code.putln(
1168
            '%s = PyLong_FromString((char *)"%s", 0, 0); %s' % (
1169
                self.result(),
William Stein's avatar
William Stein committed
1170
                self.value,
1171
                code.error_goto_if_null(self.result(), self.pos)))
1172
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
1173 1174


1175
class ImagNode(AtomicExprNode):
William Stein's avatar
William Stein committed
1176 1177 1178
    #  Imaginary number literal
    #
    #  value   float    imaginary part
1179

1180
    type = PyrexTypes.c_double_complex_type
1181 1182 1183

    def calculate_constant_result(self):
        self.constant_result = complex(0.0, self.value)
1184

1185 1186
    def compile_time_value(self, denv):
        return complex(0.0, self.value)
1187

William Stein's avatar
William Stein committed
1188
    def analyse_types(self, env):
1189 1190
        self.type.create_declaration_utility_code(env)

1191 1192 1193
    def may_be_none(self):
        return False

1194
    def coerce_to(self, dst_type, env):
1195 1196 1197
        if self.type is dst_type:
            return self
        node = ImagNode(self.pos, value=self.value)
1198
        if dst_type.is_pyobject:
1199 1200
            node.is_temp = 1
            node.type = PyrexTypes.py_object_type
1201 1202 1203
        # We still need to perform normal coerce_to processing on the
        # result, because we might be coercing to an extension type,
        # in which case a type test node will be needed.
1204
        return AtomicExprNode.coerce_to(node, dst_type, env)
1205 1206 1207

    gil_message = "Constructing complex number"

1208 1209 1210 1211 1212 1213
    def calculate_result_code(self):
        if self.type.is_pyobject:
            return self.result()
        else:
            return "%s(0, %r)" % (self.type.from_parts, float(self.value))

1214
    def generate_result_code(self, code):
1215 1216 1217 1218 1219 1220 1221
        if self.type.is_pyobject:
            code.putln(
                "%s = PyComplex_FromDoubles(0.0, %r); %s" % (
                    self.result(),
                    float(self.value),
                    code.error_goto_if_null(self.result(), self.pos)))
            code.put_gotref(self.py_result())
1222

William Stein's avatar
William Stein committed
1223

Danilo Freitas's avatar
Danilo Freitas committed
1224
class NewExprNode(AtomicExprNode):
1225 1226 1227

    # C++ new statement
    #
Robert Bradshaw's avatar
Robert Bradshaw committed
1228
    # cppclass              node                 c++ class to create
1229

Robert Bradshaw's avatar
Robert Bradshaw committed
1230
    type = None
1231

1232
    def infer_type(self, env):
Robert Bradshaw's avatar
Robert Bradshaw committed
1233 1234
        type = self.cppclass.analyse_as_type(env)
        if type is None or not type.is_cpp_class:
Danilo Freitas's avatar
Danilo Freitas committed
1235
            error(self.pos, "new operator can only be applied to a C++ class")
Robert Bradshaw's avatar
Robert Bradshaw committed
1236
            self.type = error_type
Danilo Freitas's avatar
Danilo Freitas committed
1237
            return
Robert Bradshaw's avatar
Robert Bradshaw committed
1238
        self.cpp_check(env)
1239
        constructor = type.scope.lookup(u'<init>')
Danilo Freitas's avatar
Danilo Freitas committed
1240
        if constructor is None:
1241 1242
            return_type = PyrexTypes.CFuncType(type, [])
            return_type = PyrexTypes.CPtrType(return_type)
1243 1244
            type.scope.declare_cfunction(u'<init>', return_type, self.pos)
            constructor = type.scope.lookup(u'<init>')
1245
        self.class_type = type
DaniloFreitas's avatar
DaniloFreitas committed
1246
        self.entry = constructor
Robert Bradshaw's avatar
Robert Bradshaw committed
1247
        self.type = constructor.type
1248
        return self.type
1249

1250
    def analyse_types(self, env):
Robert Bradshaw's avatar
Robert Bradshaw committed
1251 1252
        if self.type is None:
            self.infer_type(env)
1253 1254 1255 1256

    def may_be_none(self):
        return False

Danilo Freitas's avatar
Danilo Freitas committed
1257 1258
    def generate_result_code(self, code):
        pass
1259

Danilo Freitas's avatar
Danilo Freitas committed
1260
    def calculate_result_code(self):
1261
        return "new " + self.class_type.declaration_code("")
Danilo Freitas's avatar
Danilo Freitas committed
1262

William Stein's avatar
William Stein committed
1263

1264
class NameNode(AtomicExprNode):
William Stein's avatar
William Stein committed
1265 1266 1267 1268
    #  Reference to a local or global variable name.
    #
    #  name            string    Python name of the variable
    #  entry           Entry     Symbol table entry
1269
    #  type_entry      Entry     For extension type names, the original type entry
1270 1271
    #  cf_is_null      boolean   Is uninitialized before this node
    #  cf_maybe_null   boolean   Maybe uninitialized before this node
Vitja Makarov's avatar
Vitja Makarov committed
1272
    #  allow_null      boolean   Don't raise UnboundLocalError
1273

1274 1275
    is_name = True
    is_cython_module = False
Robert Bradshaw's avatar
Robert Bradshaw committed
1276
    cython_attribute = None
1277
    lhs_of_first_assignment = False # TODO: remove me
1278
    is_used_as_rvalue = 0
1279
    entry = None
1280
    type_entry = None
1281 1282
    cf_maybe_null = True
    cf_is_null = False
Vitja Makarov's avatar
Vitja Makarov committed
1283
    allow_null = False
1284 1285 1286 1287 1288

    def create_analysed_rvalue(pos, env, entry):
        node = NameNode(pos)
        node.analyse_types(env, entry=entry)
        return node
1289

1290
    def as_cython_attribute(self):
Robert Bradshaw's avatar
Robert Bradshaw committed
1291
        return self.cython_attribute
1292

1293
    create_analysed_rvalue = staticmethod(create_analysed_rvalue)
1294

Robert Bradshaw's avatar
Robert Bradshaw committed
1295 1296 1297 1298 1299 1300 1301
    def type_dependencies(self, env):
        if self.entry is None:
            self.entry = env.lookup(self.name)
        if self.entry is not None and self.entry.type.is_unspecified:
            return (self.entry,)
        else:
            return ()
1302

Robert Bradshaw's avatar
Robert Bradshaw committed
1303 1304 1305 1306 1307
    def infer_type(self, env):
        if self.entry is None:
            self.entry = env.lookup(self.name)
        if self.entry is None:
            return py_object_type
Robert Bradshaw's avatar
Robert Bradshaw committed
1308 1309 1310
        elif (self.entry.type.is_extension_type or self.entry.type.is_builtin_type) and \
                self.name == self.entry.type.name:
            # Unfortunately the type attribute of type objects
1311
            # is used for the pointer to the type they represent.
Robert Bradshaw's avatar
Robert Bradshaw committed
1312
            return type_type
1313
        elif self.entry.type.is_cfunction:
Stefan Behnel's avatar
typo  
Stefan Behnel committed
1314
            # special case: referring to a C function must return its pointer
1315
            return PyrexTypes.CPtrType(self.entry.type)
Robert Bradshaw's avatar
Robert Bradshaw committed
1316 1317
        else:
            return self.entry.type
1318

1319 1320 1321 1322
    def compile_time_value(self, denv):
        try:
            return denv.lookup(self.name)
        except KeyError:
Stefan Behnel's avatar
Stefan Behnel committed
1323
            error(self.pos, "Compile-time name '%s' not defined" % self.name)
1324 1325 1326 1327 1328

    def get_constant_c_result_code(self):
        if not self.entry or self.entry.type.is_pyobject:
            return None
        return self.entry.cname
1329

1330 1331 1332 1333 1334 1335 1336
    def coerce_to(self, dst_type, env):
        #  If coercing to a generic pyobject and this is a builtin
        #  C function with a Python equivalent, manufacture a NameNode
        #  referring to the Python builtin.
        #print "NameNode.coerce_to:", self.name, dst_type ###
        if dst_type is py_object_type:
            entry = self.entry
1337
            if entry and entry.is_cfunction:
1338 1339
                var_entry = entry.as_variable
                if var_entry:
1340
                    if var_entry.is_builtin and var_entry.is_const:
1341
                        var_entry = env.declare_builtin(var_entry.name, self.pos)
1342 1343 1344 1345
                    node = NameNode(self.pos, name = self.name)
                    node.entry = var_entry
                    node.analyse_rvalue_entry(env)
                    return node
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
1346
        return super(NameNode, self).coerce_to(dst_type, env)
1347

William Stein's avatar
William Stein committed
1348 1349 1350
    def analyse_as_module(self, env):
        # Try to interpret this as a reference to a cimported module.
        # Returns the module scope, or None.
1351 1352 1353
        entry = self.entry
        if not entry:
            entry = env.lookup(self.name)
William Stein's avatar
William Stein committed
1354 1355 1356
        if entry and entry.as_module:
            return entry.as_module
        return None
1357

1358
    def analyse_as_type(self, env):
1359 1360 1361 1362
        if self.cython_attribute:
            type = PyrexTypes.parse_basic_type(self.cython_attribute)
        else:
            type = PyrexTypes.parse_basic_type(self.name)
1363 1364
        if type:
            return type
1365 1366 1367 1368 1369 1370 1371
        entry = self.entry
        if not entry:
            entry = env.lookup(self.name)
        if entry and entry.is_type:
            return entry.type
        else:
            return None
1372

William Stein's avatar
William Stein committed
1373 1374 1375
    def analyse_as_extension_type(self, env):
        # Try to interpret this as a reference to an extension type.
        # Returns the extension type, or None.
1376 1377 1378
        entry = self.entry
        if not entry:
            entry = env.lookup(self.name)
William Stein's avatar
William Stein committed
1379
        if entry and entry.is_type and entry.type.is_extension_type:
1380 1381 1382
            return entry.type
        else:
            return None
1383

William Stein's avatar
William Stein committed
1384
    def analyse_target_declaration(self, env):
1385 1386
        if not self.entry:
            self.entry = env.lookup_here(self.name)
William Stein's avatar
William Stein committed
1387
        if not self.entry:
1388 1389
            if env.directives['warn.undeclared']:
                warning(self.pos, "implicit declaration of '%s'" % self.name, 1)
1390
            if env.directives['infer_types'] != False:
1391 1392 1393 1394
                type = unspecified_type
            else:
                type = py_object_type
            self.entry = env.declare_var(self.name, type, self.pos)
Craig Citro's avatar
Craig Citro committed
1395
        env.control_flow.set_state(self.pos, (self.name, 'initialized'), True)
Robert Bradshaw's avatar
Robert Bradshaw committed
1396
        env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment')
1397 1398
        if self.entry.is_declared_generic:
            self.result_ctype = py_object_type
1399

1400 1401 1402
    def analyse_types(self, env):
        if self.entry is None:
            self.entry = env.lookup(self.name)
William Stein's avatar
William Stein committed
1403 1404
        if not self.entry:
            self.entry = env.declare_builtin(self.name, self.pos)
1405 1406 1407
        if not self.entry:
            self.type = PyrexTypes.error_type
            return
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
1408 1409 1410 1411 1412 1413 1414 1415
        entry = self.entry
        if entry:
            entry.used = 1
            if entry.type.is_buffer:
                import Buffer
                Buffer.used_buffer_aux_vars(entry)
            if entry.utility_code:
                env.use_utility_code(entry.utility_code)
1416
        self.analyse_rvalue_entry(env)
1417

1418
    def analyse_target_types(self, env):
William Stein's avatar
William Stein committed
1419
        self.analyse_entry(env)
1420 1421 1422 1423
        if not self.is_lvalue():
            error(self.pos, "Assignment to non-lvalue '%s'"
                % self.name)
            self.type = PyrexTypes.error_type
Stefan Behnel's avatar
Stefan Behnel committed
1424
        self.entry.used = 1
1425
        if self.entry.type.is_buffer:
1426 1427
            import Buffer
            Buffer.used_buffer_aux_vars(self.entry)
1428

1429 1430 1431 1432
    def analyse_rvalue_entry(self, env):
        #print "NameNode.analyse_rvalue_entry:", self.name ###
        #print "Entry:", self.entry.__dict__ ###
        self.analyse_entry(env)
1433 1434
        entry = self.entry
        if entry.is_declared_generic:
William Stein's avatar
William Stein committed
1435
            self.result_ctype = py_object_type
1436
        if entry.is_pyglobal or entry.is_builtin:
1437
            if entry.is_builtin and entry.is_const:
1438 1439 1440
                self.is_temp = 0
            else:
                self.is_temp = 1
1441
                env.use_utility_code(get_name_interned_utility_code)
1442 1443
            self.is_used_as_rvalue = 1

1444
    def nogil_check(self, env):
1445 1446 1447
        if self.is_used_as_rvalue:
            entry = self.entry
            if entry.is_builtin:
1448
                if not entry.is_const: # cached builtins are ok
1449
                    self.gil_error()
1450
            elif entry.is_pyglobal:
1451
                self.gil_error()
1452 1453 1454

    gil_message = "Accessing Python global or builtin"

1455 1456
    def analyse_entry(self, env):
        #print "NameNode.analyse_entry:", self.name ###
William Stein's avatar
William Stein committed
1457
        self.check_identifier_kind()
1458 1459 1460 1461
        entry = self.entry
        type = entry.type
        self.type = type

William Stein's avatar
William Stein committed
1462
    def check_identifier_kind(self):
1463 1464 1465
        # Check that this is an appropriate kind of name for use in an
        # expression.  Also finds the variable entry associated with
        # an extension type.
William Stein's avatar
William Stein committed
1466
        entry = self.entry
1467 1468
        if entry.is_type and entry.type.is_extension_type:
            self.type_entry = entry
1469
        if not (entry.is_const or entry.is_variable
Danilo Freitas's avatar
Danilo Freitas committed
1470 1471
            or entry.is_builtin or entry.is_cfunction
            or entry.is_cpp_class):
William Stein's avatar
William Stein committed
1472 1473 1474
                if self.entry.as_variable:
                    self.entry = self.entry.as_variable
                else:
1475
                    error(self.pos,
1476 1477
                          "'%s' is not a constant, variable or function identifier" % self.name)

William Stein's avatar
William Stein committed
1478 1479 1480
    def is_simple(self):
        #  If it's not a C variable, it'll be in a temp.
        return 1
1481

1482
    def nonlocally_immutable(self):
1483 1484
        if ExprNode.nonlocally_immutable(self):
            return True
1485 1486 1487
        entry = self.entry
        return entry and (entry.is_local or entry.is_arg) and not entry.in_closure

William Stein's avatar
William Stein committed
1488 1489
    def calculate_target_results(self, env):
        pass
1490

William Stein's avatar
William Stein committed
1491 1492
    def check_const(self):
        entry = self.entry
Robert Bradshaw's avatar
Robert Bradshaw committed
1493
        if entry is not None and not (entry.is_const or entry.is_cfunction or entry.is_builtin):
William Stein's avatar
William Stein committed
1494
            self.not_const()
1495 1496
            return False
        return True
1497

William Stein's avatar
William Stein committed
1498 1499
    def check_const_addr(self):
        entry = self.entry
1500
        if not (entry.is_cglobal or entry.is_cfunction or entry.is_builtin):
William Stein's avatar
William Stein committed
1501
            self.addr_not_const()
1502 1503
            return False
        return True
William Stein's avatar
William Stein committed
1504 1505 1506 1507 1508

    def is_lvalue(self):
        return self.entry.is_variable and \
            not self.entry.type.is_array and \
            not self.entry.is_readonly
1509

William Stein's avatar
William Stein committed
1510 1511 1512 1513
    def is_ephemeral(self):
        #  Name nodes are never ephemeral, even if the
        #  result is in a temporary.
        return 0
1514

William Stein's avatar
William Stein committed
1515
    def calculate_result_code(self):
Stefan Behnel's avatar
Stefan Behnel committed
1516 1517
        entry = self.entry
        if not entry:
William Stein's avatar
William Stein committed
1518
            return "<error>" # There was an error earlier
Stefan Behnel's avatar
Stefan Behnel committed
1519
        return entry.cname
1520

William Stein's avatar
William Stein committed
1521
    def generate_result_code(self, code):
1522
        assert hasattr(self, 'entry')
William Stein's avatar
William Stein committed
1523 1524 1525
        entry = self.entry
        if entry is None:
            return # There was an error earlier
1526
        if entry.is_builtin and entry.is_const:
1527
            return # Lookup already cached
Stefan Behnel's avatar
Stefan Behnel committed
1528
        elif entry.is_pyclass_attr:
Vitja Makarov's avatar
Vitja Makarov committed
1529 1530 1531 1532 1533 1534 1535
            assert entry.type.is_pyobject, "Python global or builtin not a Python object"
            interned_cname = code.intern_identifier(self.entry.name)
            if entry.is_builtin:
                namespace = Naming.builtins_cname
            else: # entry.is_pyglobal
                namespace = entry.scope.namespace_cname
            code.putln(
Stefan Behnel's avatar
Stefan Behnel committed
1536
                '%s = PyObject_GetItem(%s, %s); %s' % (
Vitja Makarov's avatar
Vitja Makarov committed
1537 1538 1539 1540 1541
                self.result(),
                namespace,
                interned_cname,
                code.error_goto_if_null(self.result(), self.pos)))
            code.put_gotref(self.py_result())
1542

1543
        elif entry.is_pyglobal or entry.is_builtin:
1544 1545
            assert entry.type.is_pyobject, "Python global or builtin not a Python object"
            interned_cname = code.intern_identifier(self.entry.name)
William Stein's avatar
William Stein committed
1546 1547 1548
            if entry.is_builtin:
                namespace = Naming.builtins_cname
            else: # entry.is_pyglobal
1549
                namespace = entry.scope.namespace_cname
1550
            code.globalstate.use_utility_code(get_name_interned_utility_code)
1551 1552
            code.putln(
                '%s = __Pyx_GetName(%s, %s); %s' % (
1553
                self.result(),
1554
                namespace,
1555
                interned_cname,
1556
                code.error_goto_if_null(self.result(), self.pos)))
1557
            code.put_gotref(self.py_result())
1558

1559
        elif entry.is_local or entry.in_closure or entry.from_closure:
1560
            if entry.type.is_pyobject:
1561 1562 1563
                if (self.cf_maybe_null or self.cf_is_null) \
                       and not self.allow_null:
                    code.put_error_if_unbound(self.pos, entry)
William Stein's avatar
William Stein committed
1564 1565

    def generate_assignment_code(self, rhs, code):
1566
        #print "NameNode.generate_assignment_code:", self.name ###
William Stein's avatar
William Stein committed
1567 1568 1569
        entry = self.entry
        if entry is None:
            return # There was an error earlier
1570 1571 1572 1573

        if (self.entry.type.is_ptr and isinstance(rhs, ListNode)
            and not self.lhs_of_first_assignment):
            error(self.pos, "Literal list must be assigned to pointer at time of declaration")
1574

1575 1576
        # is_pyglobal seems to be True for module level-globals only.
        # We use this to access class->tp_dict if necessary.
William Stein's avatar
William Stein committed
1577
        if entry.is_pyglobal:
1578 1579
            assert entry.type.is_pyobject, "Python global or builtin not a Python object"
            interned_cname = code.intern_identifier(self.entry.name)
1580
            namespace = self.entry.scope.namespace_cname
1581
            if entry.is_member:
Stefan Behnel's avatar
Stefan Behnel committed
1582
                # if the entry is a member we have to cheat: SetAttr does not work
1583
                # on types, so we create a descriptor which is then added to tp_dict
1584 1585 1586
                code.put_error_if_neg(self.pos,
                    'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
                        namespace,
1587
                        interned_cname,
1588
                        rhs.py_result()))
1589 1590
                rhs.generate_disposal_code(code)
                rhs.free_temps(code)
1591
                # in Py2.6+, we need to invalidate the method cache
1592
                code.putln("PyType_Modified(%s);" %
Vitja Makarov's avatar
Vitja Makarov committed
1593
                            entry.scope.parent_type.typeptr_cname)
Stefan Behnel's avatar
Stefan Behnel committed
1594
            elif entry.is_pyclass_attr:
Vitja Makarov's avatar
Vitja Makarov committed
1595
                code.put_error_if_neg(self.pos,
Stefan Behnel's avatar
Stefan Behnel committed
1596
                    'PyObject_SetItem(%s, %s, %s)' % (
Vitja Makarov's avatar
Vitja Makarov committed
1597 1598 1599 1600 1601 1602
                        namespace,
                        interned_cname,
                        rhs.py_result()))
                rhs.generate_disposal_code(code)
                rhs.free_temps(code)
            else:
1603 1604 1605
                code.put_error_if_neg(self.pos,
                    'PyObject_SetAttr(%s, %s, %s)' % (
                        namespace,
1606
                        interned_cname,
1607
                        rhs.py_result()))
1608
                if debug_disposal_code:
Stefan Behnel's avatar
Stefan Behnel committed
1609 1610
                    print("NameNode.generate_assignment_code:")
                    print("...generating disposal code for %s" % rhs)
1611
                rhs.generate_disposal_code(code)
1612
                rhs.free_temps(code)
William Stein's avatar
William Stein committed
1613
        else:
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
            if self.type.is_buffer:
                # Generate code for doing the buffer release/acquisition.
                # This might raise an exception in which case the assignment (done
                # below) will not happen.
                #
                # The reason this is not in a typetest-like node is because the
                # variables that the acquired buffer info is stored to is allocated
                # per entry and coupled with it.
                self.generate_acquire_buffer(rhs, code)

1624
            if self.type.is_pyobject:
William Stein's avatar
William Stein committed
1625 1626 1627 1628
                #print "NameNode.generate_assignment_code: to", self.name ###
                #print "...from", rhs ###
                #print "...LHS type", self.type, "ctype", self.ctype() ###
                #print "...RHS type", rhs.type, "ctype", rhs.ctype() ###
1629 1630
                if self.use_managed_ref:
                    rhs.make_owned_reference(code)
1631
                    is_external_ref = entry.is_cglobal or self.entry.in_closure or self.entry.from_closure
1632 1633 1634 1635 1636 1637
                    if is_external_ref:
                        if not self.cf_is_null:
                            if self.cf_maybe_null:
                                code.put_xgotref(self.py_result())
                            else:
                                code.put_gotref(self.py_result())
1638 1639 1640
                    if entry.is_cglobal:
                        code.put_decref(self.result(), self.ctype())
                    else:
1641 1642
                        if not self.cf_is_null:
                            if self.cf_maybe_null:
1643
                                code.put_xdecref(self.result(), self.ctype())
1644 1645
                            else:
                                code.put_decref(self.result(), self.ctype())
1646
                    if is_external_ref:
1647
                        code.put_giveref(rhs.py_result())
1648 1649 1650

            code.putln('%s = %s;' % (self.result(),
                                     rhs.result_as(self.ctype())))
William Stein's avatar
William Stein committed
1651
            if debug_disposal_code:
Stefan Behnel's avatar
Stefan Behnel committed
1652 1653
                print("NameNode.generate_assignment_code:")
                print("...generating post-assignment code for %s" % rhs)
William Stein's avatar
William Stein committed
1654
            rhs.generate_post_assignment_code(code)
1655
            rhs.free_temps(code)
1656 1657

    def generate_acquire_buffer(self, rhs, code):
1658 1659 1660
        # rhstmp is only used in case the rhs is a complicated expression leading to
        # the object, to avoid repeating the same C expression for every reference
        # to the rhs. It does NOT hold a reference.
1661 1662 1663 1664 1665 1666 1667
        pretty_rhs = isinstance(rhs, NameNode) or rhs.is_temp
        if pretty_rhs:
            rhstmp = rhs.result_as(self.ctype())
        else:
            rhstmp = code.funcstate.allocate_temp(self.entry.type, manage_ref=False)
            code.putln('%s = %s;' % (rhstmp, rhs.result_as(self.ctype())))

1668 1669 1670
        buffer_aux = self.entry.buffer_aux
        bufstruct = buffer_aux.buffer_info_var.cname
        import Buffer
1671
        Buffer.put_assign_to_buffer(self.result(), rhstmp, buffer_aux, self.entry.type,
1672
                                    is_initialized=not self.lhs_of_first_assignment,
1673
                                    pos=self.pos, code=code)
1674

1675 1676 1677
        if not pretty_rhs:
            code.putln("%s = 0;" % rhstmp)
            code.funcstate.release_temp(rhstmp)
1678

William Stein's avatar
William Stein committed
1679 1680 1681
    def generate_deletion_code(self, code):
        if self.entry is None:
            return # There was an error earlier
1682
        elif self.entry.is_pyclass_attr:
Vitja Makarov's avatar
Vitja Makarov committed
1683 1684
            namespace = self.entry.scope.namespace_cname
            code.put_error_if_neg(self.pos,
Stefan Behnel's avatar
Stefan Behnel committed
1685
                'PyMapping_DelItemString(%s, "%s")' % (
Vitja Makarov's avatar
Vitja Makarov committed
1686 1687
                    namespace,
                    self.entry.name))
1688 1689 1690 1691 1692
        elif self.entry.is_pyglobal:
            code.put_error_if_neg(self.pos,
                '__Pyx_DelAttrString(%s, "%s")' % (
                    Naming.module_cname,
                    self.entry.name))
1693
        elif self.entry.type.is_pyobject:
1694 1695
            if not self.cf_is_null:
                if self.cf_maybe_null:
1696
                    code.put_error_if_unbound(self.pos, self.entry)
1697 1698
                code.put_decref(self.result(), self.ctype())
                code.putln('%s = NULL;' % self.result())
Vitja Makarov's avatar
Vitja Makarov committed
1699
        else:
1700
            error(self.pos, "Deletion of C names not supported")
1701

1702 1703 1704 1705 1706 1707 1708
    def annotate(self, code):
        if hasattr(self, 'is_called') and self.is_called:
            pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
            if self.type.is_pyobject:
                code.annotate(pos, AnnotationItem('py_call', 'python function', size=len(self.name)))
            else:
                code.annotate(pos, AnnotationItem('c_call', 'c function', size=len(self.name)))
1709

1710
class BackquoteNode(ExprNode):
William Stein's avatar
William Stein committed
1711 1712 1713
    #  `expr`
    #
    #  arg    ExprNode
1714

1715
    type = py_object_type
1716

William Stein's avatar
William Stein committed
1717
    subexprs = ['arg']
1718

William Stein's avatar
William Stein committed
1719 1720 1721 1722
    def analyse_types(self, env):
        self.arg.analyse_types(env)
        self.arg = self.arg.coerce_to_pyobject(env)
        self.is_temp = 1
1723 1724 1725

    gil_message = "Backquote expression"

1726 1727 1728
    def calculate_constant_result(self):
        self.constant_result = repr(self.arg.constant_result)

William Stein's avatar
William Stein committed
1729 1730
    def generate_result_code(self, code):
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
1731
            "%s = PyObject_Repr(%s); %s" % (
1732
                self.result(),
William Stein's avatar
William Stein committed
1733
                self.arg.py_result(),
1734
                code.error_goto_if_null(self.result(), self.pos)))
1735
        code.put_gotref(self.py_result())
1736

William Stein's avatar
William Stein committed
1737

1738
class ImportNode(ExprNode):
William Stein's avatar
William Stein committed
1739
    #  Used as part of import statement implementation.
1740
    #  Implements result =
Haoyu Bai's avatar
Haoyu Bai committed
1741
    #    __import__(module_name, globals(), None, name_list, level)
William Stein's avatar
William Stein committed
1742
    #
Haoyu Bai's avatar
Haoyu Bai committed
1743 1744 1745
    #  module_name   StringNode            dotted name of module. Empty module
    #                       name means importing the parent package accourding
    #                       to level
1746
    #  name_list     ListNode or None      list of names to be imported
Haoyu Bai's avatar
Haoyu Bai committed
1747 1748 1749 1750 1751
    #  level         int                   relative import level:
    #                       -1: attempt both relative import and absolute import;
    #                        0: absolute import;
    #                       >0: the number of parent directories to search
    #                           relative to the current module.
1752 1753
    #                     None: decide the level according to language level and
    #                           directives
1754

1755
    type = py_object_type
1756

William Stein's avatar
William Stein committed
1757
    subexprs = ['module_name', 'name_list']
1758

William Stein's avatar
William Stein committed
1759
    def analyse_types(self, env):
1760 1761 1762 1763 1764
        if self.level is None:
            if env.directives['language_level'] < 3 or env.directives['py2_import']:
                self.level = -1
            else:
                self.level = 0
William Stein's avatar
William Stein committed
1765 1766 1767 1768
        self.module_name.analyse_types(env)
        self.module_name = self.module_name.coerce_to_pyobject(env)
        if self.name_list:
            self.name_list.analyse_types(env)
1769
            self.name_list.coerce_to_pyobject(env)
William Stein's avatar
William Stein committed
1770 1771
        self.is_temp = 1
        env.use_utility_code(import_utility_code)
1772 1773 1774

    gil_message = "Python import"

William Stein's avatar
William Stein committed
1775 1776 1777 1778 1779 1780
    def generate_result_code(self, code):
        if self.name_list:
            name_list_code = self.name_list.py_result()
        else:
            name_list_code = "0"
        code.putln(
Haoyu Bai's avatar
Haoyu Bai committed
1781
            "%s = __Pyx_Import(%s, %s, %d); %s" % (
1782
                self.result(),
William Stein's avatar
William Stein committed
1783 1784
                self.module_name.py_result(),
                name_list_code,
Haoyu Bai's avatar
Haoyu Bai committed
1785
                self.level,
1786
                code.error_goto_if_null(self.result(), self.pos)))
1787
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
1788 1789


1790
class IteratorNode(ExprNode):
William Stein's avatar
William Stein committed
1791
    #  Used as part of for statement implementation.
1792
    #
William Stein's avatar
William Stein committed
1793 1794 1795
    #  Implements result = iter(sequence)
    #
    #  sequence   ExprNode
1796

1797
    type = py_object_type
1798
    iter_func_ptr = None
1799
    counter_cname = None
1800
    reversed = False      # currently only used for list/tuple types (see Optimize.py)
1801

William Stein's avatar
William Stein committed
1802
    subexprs = ['sequence']
1803

William Stein's avatar
William Stein committed
1804 1805
    def analyse_types(self, env):
        self.sequence.analyse_types(env)
1806 1807
        if (self.sequence.type.is_array or self.sequence.type.is_ptr) and \
                not self.sequence.type.is_string:
1808
            # C array iteration will be transformed later on
1809
            self.type = self.sequence.type
1810 1811
        else:
            self.sequence = self.sequence.coerce_to_pyobject(env)
1812 1813 1814
            if self.sequence.type is list_type or \
                   self.sequence.type is tuple_type:
                self.sequence = self.sequence.as_none_safe_node("'NoneType' object is not iterable")
William Stein's avatar
William Stein committed
1815
        self.is_temp = 1
1816 1817 1818

    gil_message = "Iterating over Python object"

1819 1820 1821 1822 1823
    _func_iternext_type = PyrexTypes.CPtrType(PyrexTypes.CFuncType(
        PyrexTypes.py_object_type, [
            PyrexTypes.CFuncTypeArg("it", PyrexTypes.py_object_type, None),
            ]))

William Stein's avatar
William Stein committed
1824
    def generate_result_code(self, code):
Stefan Behnel's avatar
Stefan Behnel committed
1825 1826
        sequence_type = self.sequence.type
        if sequence_type.is_array or sequence_type.is_ptr:
1827
            raise InternalError("for in carray slice not transformed")
Stefan Behnel's avatar
Stefan Behnel committed
1828 1829
        is_builtin_sequence = sequence_type is list_type or \
                              sequence_type is tuple_type
1830 1831 1832
        if not is_builtin_sequence:
            # reversed() not currently optimised (see Optimize.py)
            assert not self.reversed, "internal error: reversed() only implemented for list/tuple objects"
Stefan Behnel's avatar
Stefan Behnel committed
1833 1834
        self.may_be_a_sequence = not sequence_type.is_builtin_type
        if self.may_be_a_sequence:
1835 1836 1837 1838
            code.putln(
                "if (PyList_CheckExact(%s) || PyTuple_CheckExact(%s)) {" % (
                    self.sequence.py_result(),
                    self.sequence.py_result()))
Stefan Behnel's avatar
Stefan Behnel committed
1839
        if is_builtin_sequence or self.may_be_a_sequence:
1840 1841
            self.counter_cname = code.funcstate.allocate_temp(
                PyrexTypes.c_py_ssize_t_type, manage_ref=False)
1842 1843 1844 1845 1846 1847 1848
            if self.reversed:
                if sequence_type is list_type:
                    init_value = 'PyList_GET_SIZE(%s) - 1' % self.result()
                else:
                    init_value = 'PyTuple_GET_SIZE(%s) - 1' % self.result()
            else:
                init_value = '0'
1849
            code.putln(
1850
                "%s = %s; __Pyx_INCREF(%s); %s = %s;" % (
1851 1852
                    self.result(),
                    self.sequence.py_result(),
1853 1854 1855 1856
                    self.result(),
                    self.counter_cname,
                    init_value
                    ))
1857
        if not is_builtin_sequence:
Stefan Behnel's avatar
Stefan Behnel committed
1858
            self.iter_func_ptr = code.funcstate.allocate_temp(self._func_iternext_type, manage_ref=False)
Stefan Behnel's avatar
Stefan Behnel committed
1859
            if self.may_be_a_sequence:
Stefan Behnel's avatar
Stefan Behnel committed
1860
                code.putln("%s = NULL;" % self.iter_func_ptr)
1861
                code.putln("} else {")
1862 1863
                code.put("%s = -1; " % self.counter_cname)
            code.putln("%s = PyObject_GetIter(%s); %s" % (
1864 1865 1866
                    self.result(),
                    self.sequence.py_result(),
                    code.error_goto_if_null(self.result(), self.pos)))
1867
            code.put_gotref(self.py_result())
1868
            code.putln("%s = Py_TYPE(%s)->tp_iternext;" % (self.iter_func_ptr, self.py_result()))
Stefan Behnel's avatar
Stefan Behnel committed
1869 1870 1871 1872
        if self.may_be_a_sequence:
            code.putln("}")

    def generate_next_sequence_item(self, test_name, result_name, code):
1873
        assert self.counter_cname, "internal error: counter_cname temp not prepared"
Stefan Behnel's avatar
Stefan Behnel committed
1874 1875 1876 1877 1878
        code.putln(
            "if (%s >= Py%s_GET_SIZE(%s)) break;" % (
                self.counter_cname,
                test_name,
                self.py_result()))
1879 1880 1881 1882
        if self.reversed:
            inc_dec = '--'
        else:
            inc_dec = '++'
Stefan Behnel's avatar
Stefan Behnel committed
1883
        code.putln(
1884
            "%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s%s;" % (
Stefan Behnel's avatar
Stefan Behnel committed
1885 1886 1887 1888 1889
                result_name,
                test_name,
                self.py_result(),
                self.counter_cname,
                result_name,
1890 1891
                self.counter_cname,
                inc_dec))
Stefan Behnel's avatar
Stefan Behnel committed
1892 1893 1894

    def generate_iter_next_result_code(self, result_name, code):
        sequence_type = self.sequence.type
1895 1896
        if self.reversed:
            code.putln("if (%s < 0) break;" % self.counter_cname)
Stefan Behnel's avatar
Stefan Behnel committed
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
        if sequence_type is list_type:
            self.generate_next_sequence_item('List', result_name, code)
            return
        elif sequence_type is tuple_type:
            self.generate_next_sequence_item('Tuple', result_name, code)
            return

        if self.may_be_a_sequence:
            for test_name in ('List', 'Tuple'):
                code.putln("if (Py%s_CheckExact(%s)) {" % (test_name, self.py_result()))
                self.generate_next_sequence_item(test_name, result_name, code)
                code.put("} else ")

        code.putln("{")
        code.putln(
            "%s = %s(%s);" % (
                result_name,
                self.iter_func_ptr,
                self.py_result()))
        code.putln("if (unlikely(!%s)) {" % result_name)
        code.putln("if (PyErr_Occurred()) {")
        code.putln("if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) PyErr_Clear();")
        code.putln("else %s" % code.error_goto(self.pos))
        code.putln("}")
        code.putln("break;")
        code.putln("}")
        code.put_gotref(result_name)
        code.putln("}")
William Stein's avatar
William Stein committed
1925

1926
    def free_temps(self, code):
1927 1928
        if self.counter_cname:
            code.funcstate.release_temp(self.counter_cname)
1929 1930 1931 1932 1933
        if self.iter_func_ptr:
            code.funcstate.release_temp(self.iter_func_ptr)
            self.iter_func_ptr = None
        ExprNode.free_temps(self, code)

William Stein's avatar
William Stein committed
1934

1935
class NextNode(AtomicExprNode):
William Stein's avatar
William Stein committed
1936 1937 1938 1939 1940
    #  Used as part of for statement implementation.
    #  Implements result = iterator.next()
    #  Created during analyse_types phase.
    #  The iterator is not owned by this node.
    #
1941
    #  iterator   IteratorNode
1942

1943
    type = py_object_type
1944

1945
    def __init__(self, iterator):
William Stein's avatar
William Stein committed
1946 1947
        self.pos = iterator.pos
        self.iterator = iterator
1948 1949
        if iterator.type.is_ptr or iterator.type.is_array:
            self.type = iterator.type.base_type
William Stein's avatar
William Stein committed
1950
        self.is_temp = 1
1951

William Stein's avatar
William Stein committed
1952
    def generate_result_code(self, code):
Stefan Behnel's avatar
Stefan Behnel committed
1953
        self.iterator.generate_iter_next_result_code(self.result(), code)
1954

William Stein's avatar
William Stein committed
1955

1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
class WithExitCallNode(ExprNode):
    # The __exit__() call of a 'with' statement.  Used in both the
    # except and finally clauses.

    # with_stat  WithStatNode                the surrounding 'with' statement
    # args       TupleNode or ResultStatNode the exception info tuple

    subexprs = ['args']

    def analyse_types(self, env):
        self.args.analyse_types(env)
        self.type = PyrexTypes.c_bint_type
        self.is_temp = True

    def generate_result_code(self, code):
        if isinstance(self.args, TupleNode):
            # call only if it was not already called (and decref-cleared)
            code.putln("if (%s) {" % self.with_stat.exit_var)
        result_var = code.funcstate.allocate_temp(py_object_type, manage_ref=False)
        code.putln("%s = PyObject_Call(%s, %s, NULL);" % (
            result_var,
            self.with_stat.exit_var,
            self.args.result()))
        code.put_decref_clear(self.with_stat.exit_var, type=py_object_type)
        code.putln(code.error_goto_if_null(result_var, self.pos))
        code.put_gotref(result_var)
        code.putln("%s = __Pyx_PyObject_IsTrue(%s);" % (self.result(), result_var))
        code.put_decref_clear(result_var, type=py_object_type)
        code.putln(code.error_goto_if_neg(self.result(), self.pos))
        code.funcstate.release_temp(result_var)
        if isinstance(self.args, TupleNode):
            code.putln("}")


1990
class ExcValueNode(AtomicExprNode):
William Stein's avatar
William Stein committed
1991 1992 1993
    #  Node created during analyse_types phase
    #  of an ExceptClauseNode to fetch the current
    #  exception value.
1994

1995
    type = py_object_type
1996

1997
    def __init__(self, pos, env):
William Stein's avatar
William Stein committed
1998
        ExprNode.__init__(self, pos)
1999 2000

    def set_var(self, var):
2001
        self.var = var
2002

2003 2004 2005
    def calculate_result_code(self):
        return self.var

William Stein's avatar
William Stein committed
2006
    def generate_result_code(self, code):
2007
        pass
William Stein's avatar
William Stein committed
2008

2009 2010 2011
    def analyse_types(self, env):
        pass

William Stein's avatar
William Stein committed
2012

2013
class TempNode(ExprNode):
2014 2015 2016 2017 2018 2019 2020
    # Node created during analyse_types phase
    # of some nodes to hold a temporary value.
    #
    # Note: One must call "allocate" and "release" on
    # the node during code generation to get/release the temp.
    # This is because the temp result is often used outside of
    # the regular cycle.
2021 2022

    subexprs = []
2023

2024
    def __init__(self, pos, type, env=None):
William Stein's avatar
William Stein committed
2025 2026 2027 2028 2029
        ExprNode.__init__(self, pos)
        self.type = type
        if type.is_pyobject:
            self.result_ctype = py_object_type
        self.is_temp = 1
2030

2031 2032
    def analyse_types(self, env):
        return self.type
2033

2034 2035 2036
    def analyse_target_declaration(self, env):
        pass

William Stein's avatar
William Stein committed
2037 2038 2039
    def generate_result_code(self, code):
        pass

2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
    def allocate(self, code):
        self.temp_cname = code.funcstate.allocate_temp(self.type, manage_ref=True)

    def release(self, code):
        code.funcstate.release_temp(self.temp_cname)
        self.temp_cname = None

    def result(self):
        try:
            return self.temp_cname
        except:
            assert False, "Remember to call allocate/release on TempNode"
            raise

    # Do not participate in normal temp alloc/dealloc:
    def allocate_temp_result(self, code):
        pass
2057

2058 2059
    def release_temp_result(self, code):
        pass
William Stein's avatar
William Stein committed
2060 2061 2062

class PyTempNode(TempNode):
    #  TempNode holding a Python value.
2063

William Stein's avatar
William Stein committed
2064 2065 2066
    def __init__(self, pos, env):
        TempNode.__init__(self, pos, PyrexTypes.py_object_type, env)

2067 2068
class RawCNameExprNode(ExprNode):
    subexprs = []
2069

2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
    def __init__(self, pos, type=None):
        self.pos = pos
        self.type = type

    def analyse_types(self, env):
        return self.type

    def set_cname(self, cname):
        self.cname = cname

    def result(self):
        return self.cname

    def generate_result_code(self, code):
        pass

William Stein's avatar
William Stein committed
2086

Mark Florisson's avatar
Mark Florisson committed
2087 2088 2089 2090 2091 2092 2093 2094
#-------------------------------------------------------------------
#
#  Parallel nodes (cython.parallel.thread(savailable|id))
#
#-------------------------------------------------------------------

class ParallelThreadsAvailableNode(AtomicExprNode):
    """
Mark Florisson's avatar
Mark Florisson committed
2095 2096
    Note: this is disabled and not a valid directive at this moment

Mark Florisson's avatar
Mark Florisson committed
2097 2098 2099 2100 2101 2102 2103 2104 2105
    Implements cython.parallel.threadsavailable(). If we are called from the
    sequential part of the application, we need to call omp_get_max_threads(),
    and in the parallel part we can just call omp_get_num_threads()
    """

    type = PyrexTypes.c_int_type

    def analyse_types(self, env):
        self.is_temp = True
2106
        # env.add_include_file("omp.h")
Mark Florisson's avatar
Mark Florisson committed
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
        return self.type

    def generate_result_code(self, code):
        code.putln("#ifdef _OPENMP")
        code.putln("if (omp_in_parallel()) %s = omp_get_max_threads();" %
                                                            self.temp_code)
        code.putln("else %s = omp_get_num_threads();" % self.temp_code)
        code.putln("#else")
        code.putln("%s = 1;" % self.temp_code)
        code.putln("#endif")

    def result(self):
        return self.temp_code


class ParallelThreadIdNode(AtomicExprNode): #, Nodes.ParallelNode):
    """
    Implements cython.parallel.threadid()
    """

    type = PyrexTypes.c_int_type

    def analyse_types(self, env):
        self.is_temp = True
2131
        # env.add_include_file("omp.h")
Mark Florisson's avatar
Mark Florisson committed
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
        return self.type

    def generate_result_code(self, code):
        code.putln("#ifdef _OPENMP")
        code.putln("%s = omp_get_thread_num();" % self.temp_code)
        code.putln("#else")
        code.putln("%s = 0;" % self.temp_code)
        code.putln("#endif")

    def result(self):
        return self.temp_code


William Stein's avatar
William Stein committed
2145 2146 2147 2148 2149 2150
#-------------------------------------------------------------------
#
#  Trailer nodes
#
#-------------------------------------------------------------------

2151
class IndexNode(ExprNode):
William Stein's avatar
William Stein committed
2152 2153 2154 2155
    #  Sequence indexing.
    #
    #  base     ExprNode
    #  index    ExprNode
2156 2157 2158 2159 2160 2161
    #  indices  [ExprNode]
    #  is_buffer_access boolean Whether this is a buffer access.
    #
    #  indices is used on buffer access, index on non-buffer access.
    #  The former contains a clean list of index parameters, the
    #  latter whatever Python object is needed for index access.
2162

2163 2164 2165 2166 2167 2168
    subexprs = ['base', 'index', 'indices']
    indices = None

    def __init__(self, pos, index, *args, **kw):
        ExprNode.__init__(self, pos, index=index, *args, **kw)
        self._index = index
2169 2170 2171 2172 2173

    def calculate_constant_result(self):
        self.constant_result = \
            self.base.constant_result[self.index.constant_result]

2174 2175 2176 2177 2178 2179 2180
    def compile_time_value(self, denv):
        base = self.base.compile_time_value(denv)
        index = self.index.compile_time_value(denv)
        try:
            return base[index]
        except Exception, e:
            self.compile_time_value_error(e)
2181

William Stein's avatar
William Stein committed
2182 2183
    def is_ephemeral(self):
        return self.base.is_ephemeral()
2184

2185
    def is_simple(self):
2186 2187
        if self.is_buffer_access:
            return False
2188 2189 2190 2191
        base = self.base
        return (base.is_simple() and self.index.is_simple()
                and base.type and (base.type.is_ptr or base.type.is_array))

William Stein's avatar
William Stein committed
2192 2193
    def analyse_target_declaration(self, env):
        pass
2194

2195 2196 2197
    def analyse_as_type(self, env):
        base_type = self.base.analyse_as_type(env)
        if base_type and not base_type.is_pyobject:
2198
            if base_type.is_cpp_class:
2199
                if isinstance(self.index, TupleNode):
2200 2201 2202 2203 2204
                    template_values = self.index.args
                else:
                    template_values = [self.index]
                import Nodes
                type_node = Nodes.TemplatedTypeNode(
2205 2206
                    pos = self.pos,
                    positional_args = template_values,
2207 2208 2209 2210
                    keyword_args = None)
                return type_node.analyse(env, base_type = base_type)
            else:
                return PyrexTypes.CArrayType(base_type, int(self.index.compile_time_value(env)))
2211
        return None
2212

Robert Bradshaw's avatar
Robert Bradshaw committed
2213
    def type_dependencies(self, env):
2214
        return self.base.type_dependencies(env) + self.index.type_dependencies(env)
2215

2216
    def infer_type(self, env):
2217 2218 2219 2220
        base_type = self.base.infer_type(env)
        if isinstance(self.index, SliceNode):
            # slicing!
            if base_type.is_string:
2221
                # sliced C strings must coerce to Python
2222
                return bytes_type
2223 2224 2225
            elif base_type in (unicode_type, bytes_type, str_type, list_type, tuple_type):
                # slicing these returns the same type
                return base_type
2226
            else:
2227 2228 2229
                # TODO: Handle buffers (hopefully without too much redundancy).
                return py_object_type

2230 2231
        index_type = self.index.infer_type(env)
        if index_type and index_type.is_int or isinstance(self.index, (IntNode, LongNode)):
2232 2233
            # indexing!
            if base_type is unicode_type:
2234 2235 2236
                # Py_UCS4 will automatically coerce to a unicode string
                # if required, so this is safe.  We only infer Py_UCS4
                # when the index is a C integer type.  Otherwise, we may
2237 2238 2239 2240
                # need to use normal Python item access, in which case
                # it's faster to return the one-char unicode string than
                # to receive it, throw it away, and potentially rebuild it
                # on a subsequent PyObject coercion.
2241
                return PyrexTypes.c_py_ucs4_type
2242 2243 2244
            elif base_type is str_type:
                # always returns str - Py2: bytes, Py3: unicode
                return base_type
2245 2246 2247 2248 2249 2250
            elif isinstance(self.base, BytesNode):
                #if env.global_scope().context.language_level >= 3:
                #    # infering 'char' can be made to work in Python 3 mode
                #    return PyrexTypes.c_char_type
                # Py2/3 return different types on indexing bytes objects
                return py_object_type
2251 2252
            elif base_type.is_ptr or base_type.is_array:
                return base_type.base_type
2253

2254
        # may be slicing or indexing, we don't know
2255 2256
        if base_type in (unicode_type, str_type):
            # these types always returns their own type on Python indexing/slicing
2257
            return base_type
2258 2259 2260
        else:
            # TODO: Handle buffers (hopefully without too much redundancy).
            return py_object_type
2261

William Stein's avatar
William Stein committed
2262
    def analyse_types(self, env):
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2263
        self.analyse_base_and_index_types(env, getting = 1)
2264

Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2265 2266
    def analyse_target_types(self, env):
        self.analyse_base_and_index_types(env, setting = 1)
2267

Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2268
    def analyse_base_and_index_types(self, env, getting = 0, setting = 0):
2269 2270 2271
        # Note: This might be cleaned up by having IndexNode
        # parsed in a saner way and only construct the tuple if
        # needed.
2272 2273 2274 2275

        # Note that this function must leave IndexNode in a cloneable state.
        # For buffers, self.index is packed out on the initial analysis, and
        # when cloning self.indices is copied.
2276 2277
        self.is_buffer_access = False

William Stein's avatar
William Stein committed
2278
        self.base.analyse_types(env)
2279 2280 2281 2282 2283
        if self.base.type.is_error:
            # Do not visit child tree if base is undeclared to avoid confusing
            # error messages
            self.type = PyrexTypes.error_type
            return
2284

2285
        is_slice = isinstance(self.index, SliceNode)
2286
        # Potentially overflowing index value.
2287
        if not is_slice and isinstance(self.index, IntNode) and Utils.long_literal(self.index.value):
2288
            self.index = self.index.coerce_to_pyobject(env)
2289

2290
        # Handle the case where base is a literal char* (and we expect a string, not an int)
2291
        if isinstance(self.base, BytesNode) or is_slice:
Robert Bradshaw's avatar
Robert Bradshaw committed
2292
            if self.base.type.is_string or not (self.base.type.is_ptr or self.base.type.is_array):
2293
                self.base = self.base.coerce_to_pyobject(env)
2294 2295 2296

        skip_child_analysis = False
        buffer_access = False
2297
        if self.base.type.is_buffer:
2298 2299
            if self.indices:
                indices = self.indices
2300
            else:
2301 2302 2303 2304
                if isinstance(self.index, TupleNode):
                    indices = self.index.args
                else:
                    indices = [self.index]
2305
            if len(indices) == self.base.type.ndim:
2306 2307 2308 2309 2310 2311
                buffer_access = True
                skip_child_analysis = True
                for x in indices:
                    x.analyse_types(env)
                    if not x.type.is_int:
                        buffer_access = False
Robert Bradshaw's avatar
Robert Bradshaw committed
2312 2313
            if buffer_access:
                assert hasattr(self.base, "entry") # Must be a NameNode-like node
2314

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
2315 2316 2317
        # On cloning, indices is cloned. Otherwise, unpack index into indices
        assert not (buffer_access and isinstance(self.index, CloneNode))

2318 2319
        if buffer_access:
            self.indices = indices
2320
            self.index = None
2321 2322
            self.type = self.base.type.dtype
            self.is_buffer_access = True
2323
            self.buffer_type = self.base.entry.type
2324 2325

            if getting and self.type.is_pyobject:
2326
                self.is_temp = True
2327 2328 2329 2330 2331
            if setting:
                if not self.base.entry.type.writable:
                    error(self.pos, "Writing to readonly buffer")
                else:
                    self.base.entry.buffer_aux.writable_needed = True
2332
        else:
2333
            base_type = self.base.type
2334 2335 2336 2337
            if isinstance(self.index, TupleNode):
                self.index.analyse_types(env, skip_children=skip_child_analysis)
            elif not skip_child_analysis:
                self.index.analyse_types(env)
2338
            self.original_index_type = self.index.type
Stefan Behnel's avatar
Stefan Behnel committed
2339 2340
            if base_type.is_unicode_char:
                # we infer Py_UNICODE/Py_UCS4 for unicode strings in some
2341 2342 2343 2344 2345 2346 2347
                # cases, but indexing must still work for them
                if self.index.constant_result in (0, -1):
                    # FIXME: we know that this node is redundant -
                    # currently, this needs to get handled in Optimize.py
                    pass
                self.base = self.base.coerce_to_pyobject(env)
                base_type = self.base.type
2348
            if base_type.is_pyobject:
2349
                if self.index.type.is_int:
2350
                    if (not setting
2351
                        and (base_type in (list_type, tuple_type, unicode_type))
2352 2353 2354 2355 2356
                        and (not self.index.type.signed or isinstance(self.index, IntNode) and int(self.index.value) >= 0)
                        and not env.directives['boundscheck']):
                        self.is_temp = 0
                    else:
                        self.is_temp = 1
2357 2358 2359
                    self.index = self.index.coerce_to(PyrexTypes.c_py_ssize_t_type, env).coerce_to_simple(env)
                else:
                    self.index = self.index.coerce_to_pyobject(env)
2360
                    self.is_temp = 1
2361
                if self.index.type.is_int and base_type is unicode_type:
Stefan Behnel's avatar
Stefan Behnel committed
2362
                    # Py_UNICODE/Py_UCS4 will automatically coerce to a unicode string
2363
                    # if required, so this is fast and safe
2364
                    self.type = PyrexTypes.c_py_ucs4_type
2365 2366
                elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type):
                    self.type = base_type
2367 2368
                else:
                    self.type = py_object_type
William Stein's avatar
William Stein committed
2369
            else:
2370 2371
                if base_type.is_ptr or base_type.is_array:
                    self.type = base_type.base_type
2372 2373 2374
                    if is_slice:
                        self.type = base_type
                    elif self.index.type.is_pyobject:
Robert Bradshaw's avatar
Robert Bradshaw committed
2375 2376
                        self.index = self.index.coerce_to(
                            PyrexTypes.c_py_ssize_t_type, env)
2377
                    elif not self.index.type.is_int:
Robert Bradshaw's avatar
Robert Bradshaw committed
2378 2379 2380
                        error(self.pos,
                            "Invalid index type '%s'" %
                                self.index.type)
2381
                elif base_type.is_cpp_class:
2382
                    function = env.lookup_operator("[]", [self.base, self.index])
Robert Bradshaw's avatar
Robert Bradshaw committed
2383
                    if function is None:
2384
                        error(self.pos, "Indexing '%s' not supported for index type '%s'" % (base_type, self.index.type))
Robert Bradshaw's avatar
Robert Bradshaw committed
2385 2386 2387 2388 2389 2390 2391 2392 2393
                        self.type = PyrexTypes.error_type
                        self.result_code = "<error>"
                        return
                    func_type = function.type
                    if func_type.is_ptr:
                        func_type = func_type.base_type
                    self.index = self.index.coerce_to(func_type.args[0].type, env)
                    self.type = func_type.return_type
                    if setting and not func_type.return_type.is_reference:
Robert Bradshaw's avatar
Robert Bradshaw committed
2394
                        error(self.pos, "Can't set non-reference result '%s'" % self.type)
2395 2396 2397
                else:
                    error(self.pos,
                        "Attempting to index non-array type '%s'" %
2398
                            base_type)
2399
                    self.type = PyrexTypes.error_type
Stefan Behnel's avatar
Stefan Behnel committed
2400

2401 2402
    gil_message = "Indexing Python object"

2403 2404
    def nogil_check(self, env):
        if self.is_buffer_access:
2405 2406 2407 2408 2409 2410
            if env.directives['boundscheck']:
                error(self.pos, "Cannot check buffer index bounds without gil; use boundscheck(False) directive")
                return
            elif self.type.is_pyobject:
                error(self.pos, "Cannot access buffer with object dtype without gil")
                return
2411
        super(IndexNode, self).nogil_check(env)
2412 2413


William Stein's avatar
William Stein committed
2414
    def check_const_addr(self):
2415
        return self.base.check_const_addr() and self.index.check_const()
2416

William Stein's avatar
William Stein committed
2417 2418
    def is_lvalue(self):
        return 1
Dag Sverre Seljebotn's avatar
merge  
Dag Sverre Seljebotn committed
2419

William Stein's avatar
William Stein committed
2420
    def calculate_result_code(self):
2421
        if self.is_buffer_access:
2422
            return "(*%s)" % self.buffer_ptr_code
2423 2424 2425 2426
        elif self.base.type is list_type:
            return "PyList_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
        elif self.base.type is tuple_type:
            return "PyTuple_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
Stefan Behnel's avatar
Stefan Behnel committed
2427
        elif self.base.type is unicode_type and self.type.is_unicode_char:
2428
            return "PyUnicode_AS_UNICODE(%s)[%s]" % (self.base.result(), self.index.result())
2429 2430
        elif (self.type.is_ptr or self.type.is_array) and self.type == self.base.type:
            error(self.pos, "Invalid use of pointer slice")
2431 2432
        else:
            return "(%s[%s])" % (
2433
                self.base.result(), self.index.result())
2434

2435
    def extra_index_params(self):
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2436 2437
        if self.index.type.is_int:
            if self.original_index_type.signed:
2438
                size_adjustment = ""
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2439
            else:
2440 2441
                size_adjustment = "+1"
            return ", sizeof(%s)%s, %s" % (self.original_index_type.declaration_code(""), size_adjustment, self.original_index_type.to_py_function)
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2442 2443
        else:
            return ""
2444 2445 2446

    def generate_subexpr_evaluation_code(self, code):
        self.base.generate_evaluation_code(code)
2447
        if not self.indices:
2448 2449
            self.index.generate_evaluation_code(code)
        else:
2450 2451
            for i in self.indices:
                i.generate_evaluation_code(code)
2452

2453 2454
    def generate_subexpr_disposal_code(self, code):
        self.base.generate_disposal_code(code)
2455
        if not self.indices:
2456 2457
            self.index.generate_disposal_code(code)
        else:
2458 2459
            for i in self.indices:
                i.generate_disposal_code(code)
2460

2461 2462 2463 2464 2465 2466 2467 2468
    def free_subexpr_temps(self, code):
        self.base.free_temps(code)
        if not self.indices:
            self.index.free_temps(code)
        else:
            for i in self.indices:
                i.free_temps(code)

William Stein's avatar
William Stein committed
2469
    def generate_result_code(self, code):
2470
        if self.is_buffer_access:
2471 2472
            if code.globalstate.directives['nonecheck']:
                self.put_nonecheck(code)
2473 2474 2475 2476
            self.buffer_ptr_code = self.buffer_lookup_code(code)
            if self.type.is_pyobject:
                # is_temp is True, so must pull out value and incref it.
                code.putln("%s = *%s;" % (self.result(), self.buffer_ptr_code))
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
2477
                code.putln("__Pyx_INCREF((PyObject*)%s);" % self.result())
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
        elif self.is_temp:
            if self.type.is_pyobject:
                if self.index.type.is_int:
                    index_code = self.index.result()
                    if self.base.type is list_type:
                        function = "__Pyx_GetItemInt_List"
                    elif self.base.type is tuple_type:
                        function = "__Pyx_GetItemInt_Tuple"
                    else:
                        function = "__Pyx_GetItemInt"
                    code.globalstate.use_utility_code(getitem_int_utility_code)
2489
                else:
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
                    index_code = self.index.py_result()
                    if self.base.type is dict_type:
                        function = "__Pyx_PyDict_GetItem"
                        code.globalstate.use_utility_code(getitem_dict_utility_code)
                    else:
                        function = "PyObject_GetItem"
                code.putln(
                    "%s = %s(%s, %s%s); if (!%s) %s" % (
                        self.result(),
                        function,
                        self.base.py_result(),
                        index_code,
                        self.extra_index_params(),
                        self.result(),
                        code.error_goto(self.pos)))
                code.put_gotref(self.py_result())
Stefan Behnel's avatar
Stefan Behnel committed
2506
            elif self.type.is_unicode_char and self.base.type is unicode_type:
2507 2508 2509
                assert self.index.type.is_int
                index_code = self.index.result()
                function = "__Pyx_GetItemInt_Unicode"
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
                code.globalstate.use_utility_code(getitem_int_pyunicode_utility_code)
                code.putln(
                    "%s = %s(%s, %s%s); if (unlikely(%s == (Py_UNICODE)-1)) %s;" % (
                        self.result(),
                        function,
                        self.base.py_result(),
                        index_code,
                        self.extra_index_params(),
                        self.result(),
                        code.error_goto(self.pos)))
2520

Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2521 2522 2523
    def generate_setitem_code(self, value_code, code):
        if self.index.type.is_int:
            function = "__Pyx_SetItemInt"
2524
            index_code = self.index.result()
2525
            code.globalstate.use_utility_code(setitem_int_utility_code)
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2526 2527
        else:
            index_code = self.index.py_result()
2528 2529
            if self.base.type is dict_type:
                function = "PyDict_SetItem"
Craig Citro's avatar
Craig Citro committed
2530
            # It would seem that we could specialized lists/tuples, but that
2531 2532 2533 2534 2535 2536
            # shouldn't happen here.
            # Both PyList_SetItem PyTuple_SetItem and a Py_ssize_t as input,
            # not a PyObject*, and bad conversion here would give the wrong
            # exception. Also, tuples are supposed to be immutable, and raise
            # TypeErrors when trying to set their entries (PyTuple_SetItem
            # is for creating new tuples from).
2537 2538
            else:
                function = "PyObject_SetItem"
2539
        code.putln(
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2540 2541
            "if (%s(%s, %s, %s%s) < 0) %s" % (
                function,
2542
                self.base.py_result(),
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2543 2544
                index_code,
                value_code,
2545
                self.extra_index_params(),
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2546
                code.error_goto(self.pos)))
2547 2548 2549

    def generate_buffer_setitem_code(self, rhs, code, op=""):
        # Used from generate_assignment_code and InPlaceAssignmentNode
2550 2551
        if code.globalstate.directives['nonecheck']:
            self.put_nonecheck(code)
2552 2553 2554 2555
        ptrexpr = self.buffer_lookup_code(code)
        if self.buffer_type.dtype.is_pyobject:
            # Must manage refcounts. Decref what is already there
            # and incref what we put in.
2556
            ptr = code.funcstate.allocate_temp(self.buffer_type.buffer_ptr_type, manage_ref=False)
2557
            rhs_code = rhs.result()
2558
            code.putln("%s = %s;" % (ptr, ptrexpr))
2559
            code.put_gotref("*%s" % ptr)
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
2560
            code.putln("__Pyx_DECREF(*%s); __Pyx_INCREF(%s);" % (
2561 2562 2563
                ptr, rhs_code
                ))
            code.putln("*%s %s= %s;" % (ptr, op, rhs_code))
2564
            code.put_giveref("*%s" % ptr)
2565
            code.funcstate.release_temp(ptr)
2566
        else:
2567
            # Simple case
2568
            code.putln("*%s %s= %s;" % (ptrexpr, op, rhs.result()))
2569

William Stein's avatar
William Stein committed
2570 2571
    def generate_assignment_code(self, rhs, code):
        self.generate_subexpr_evaluation_code(code)
2572
        if self.is_buffer_access:
2573
            self.generate_buffer_setitem_code(rhs, code)
2574
        elif self.type.is_pyobject:
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2575
            self.generate_setitem_code(rhs.py_result(), code)
William Stein's avatar
William Stein committed
2576 2577 2578
        else:
            code.putln(
                "%s = %s;" % (
2579
                    self.result(), rhs.result()))
2580
        self.generate_subexpr_disposal_code(code)
2581
        self.free_subexpr_temps(code)
William Stein's avatar
William Stein committed
2582
        rhs.generate_disposal_code(code)
2583
        rhs.free_temps(code)
2584

William Stein's avatar
William Stein committed
2585 2586
    def generate_deletion_code(self, code):
        self.generate_subexpr_evaluation_code(code)
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2587 2588
        #if self.type.is_pyobject:
        if self.index.type.is_int:
2589
            function = "__Pyx_DelItemInt"
2590
            index_code = self.index.result()
2591
            code.globalstate.use_utility_code(delitem_int_utility_code)
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2592 2593
        else:
            index_code = self.index.py_result()
2594 2595 2596 2597
            if self.base.type is dict_type:
                function = "PyDict_DelItem"
            else:
                function = "PyObject_DelItem"
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2598
        code.putln(
2599
            "if (%s(%s, %s%s) < 0) %s" % (
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2600
                function,
William Stein's avatar
William Stein committed
2601
                self.base.py_result(),
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2602
                index_code,
2603
                self.extra_index_params(),
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
2604
                code.error_goto(self.pos)))
William Stein's avatar
William Stein committed
2605
        self.generate_subexpr_disposal_code(code)
2606
        self.free_subexpr_temps(code)
2607

2608
    def buffer_lookup_code(self, code):
2609
        # Assign indices to temps
2610
        index_temps = [code.funcstate.allocate_temp(i.type, manage_ref=False) for i in self.indices]
2611
        for temp, index in zip(index_temps, self.indices):
2612
            code.putln("%s = %s;" % (temp, index.result()))
2613 2614
        # Generate buffer access code using these temps
        import Buffer
2615 2616
        # The above could happen because child_attrs is wrong somewhere so that
        # options are not propagated.
2617 2618 2619
        return Buffer.put_buffer_lookup_code(entry=self.base.entry,
                                             index_signeds=[i.type.signed for i in self.indices],
                                             index_cnames=index_temps,
2620
                                             directives=code.globalstate.directives,
2621
                                             pos=self.pos, code=code)
William Stein's avatar
William Stein committed
2622

2623 2624 2625 2626 2627 2628 2629
    def put_nonecheck(self, code):
        code.globalstate.use_utility_code(raise_noneindex_error_utility_code)
        code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.base.result_as(PyrexTypes.py_object_type))
        code.putln("__Pyx_RaiseNoneIndexingError();")
        code.putln(code.error_goto(self.pos))
        code.putln("}")

2630
class SliceIndexNode(ExprNode):
William Stein's avatar
William Stein committed
2631 2632 2633 2634 2635
    #  2-element slice indexing
    #
    #  base      ExprNode
    #  start     ExprNode or None
    #  stop      ExprNode or None
2636

William Stein's avatar
William Stein committed
2637
    subexprs = ['base', 'start', 'stop']
2638

2639 2640 2641 2642 2643 2644 2645
    def infer_type(self, env):
        base_type = self.base.infer_type(env)
        if base_type.is_string:
            return bytes_type
        elif base_type in (bytes_type, str_type, unicode_type,
                           list_type, tuple_type):
            return base_type
2646 2647
        elif base_type.is_ptr or base_type.is_array:
            return PyrexTypes.c_array_type(base_type.base_type, None)
2648 2649
        return py_object_type

2650 2651 2652 2653
    def calculate_constant_result(self):
        self.constant_result = self.base.constant_result[
            self.start.constant_result : self.stop.constant_result]

2654 2655
    def compile_time_value(self, denv):
        base = self.base.compile_time_value(denv)
2656 2657 2658 2659 2660 2661 2662 2663
        if self.start is None:
            start = 0
        else:
            start = self.start.compile_time_value(denv)
        if self.stop is None:
            stop = None
        else:
            stop = self.stop.compile_time_value(denv)
2664 2665 2666 2667
        try:
            return base[start:stop]
        except Exception, e:
            self.compile_time_value_error(e)
2668

William Stein's avatar
William Stein committed
2669 2670
    def analyse_target_declaration(self, env):
        pass
2671

2672 2673 2674
    def analyse_target_types(self, env):
        self.analyse_types(env)
        # when assigning, we must accept any Python type
2675 2676
        if self.type.is_pyobject:
            self.type = py_object_type
William Stein's avatar
William Stein committed
2677 2678 2679 2680 2681 2682 2683

    def analyse_types(self, env):
        self.base.analyse_types(env)
        if self.start:
            self.start.analyse_types(env)
        if self.stop:
            self.stop.analyse_types(env)
2684 2685 2686
        base_type = self.base.type
        if base_type.is_string:
            self.type = bytes_type
2687 2688 2689
        elif base_type.is_ptr:
            self.type = base_type
        elif base_type.is_array:
2690 2691 2692
            # we need a ptr type here instead of an array type, as
            # array types can result in invalid type casts in the C
            # code
2693
            self.type = PyrexTypes.CPtrType(base_type.base_type)
2694 2695 2696
        else:
            self.base = self.base.coerce_to_pyobject(env)
            self.type = py_object_type
2697 2698 2699
        if base_type.is_builtin_type:
            # slicing builtin types returns something of the same type
            self.type = base_type
2700
        c_int = PyrexTypes.c_py_ssize_t_type
William Stein's avatar
William Stein committed
2701 2702 2703 2704 2705
        if self.start:
            self.start = self.start.coerce_to(c_int, env)
        if self.stop:
            self.stop = self.stop.coerce_to(c_int, env)
        self.is_temp = 1
2706

2707
    nogil_check = Node.gil_error
2708 2709
    gil_message = "Slicing Python object"

William Stein's avatar
William Stein committed
2710
    def generate_result_code(self, code):
2711 2712 2713 2714
        if not self.type.is_pyobject:
            error(self.pos,
                  "Slicing is not currently supported for '%s'." % self.type)
            return
Robert Bradshaw's avatar
Robert Bradshaw committed
2715 2716 2717
        if self.base.type.is_string:
            if self.stop is None:
                code.putln(
2718
                    "%s = PyBytes_FromString(%s + %s); %s" % (
Robert Bradshaw's avatar
Robert Bradshaw committed
2719 2720 2721 2722 2723 2724
                        self.result(),
                        self.base.result(),
                        self.start_code(),
                        code.error_goto_if_null(self.result(), self.pos)))
            else:
                code.putln(
2725
                    "%s = PyBytes_FromStringAndSize(%s + %s, %s - %s); %s" % (
Robert Bradshaw's avatar
Robert Bradshaw committed
2726 2727 2728 2729 2730 2731 2732 2733
                        self.result(),
                        self.base.result(),
                        self.start_code(),
                        self.stop_code(),
                        self.start_code(),
                        code.error_goto_if_null(self.result(), self.pos)))
        else:
            code.putln(
2734
                "%s = __Pyx_PySequence_GetSlice(%s, %s, %s); %s" % (
Robert Bradshaw's avatar
Robert Bradshaw committed
2735 2736 2737 2738 2739
                    self.result(),
                    self.base.py_result(),
                    self.start_code(),
                    self.stop_code(),
                    code.error_goto_if_null(self.result(), self.pos)))
2740
        code.put_gotref(self.py_result())
2741

William Stein's avatar
William Stein committed
2742 2743
    def generate_assignment_code(self, rhs, code):
        self.generate_subexpr_evaluation_code(code)
2744
        if self.type.is_pyobject:
2745
            code.put_error_if_neg(self.pos,
2746
                "__Pyx_PySequence_SetSlice(%s, %s, %s, %s)" % (
2747 2748 2749
                    self.base.py_result(),
                    self.start_code(),
                    self.stop_code(),
Lisandro Dalcin's avatar
Lisandro Dalcin committed
2750
                    rhs.py_result()))
2751 2752 2753 2754 2755 2756 2757 2758
        else:
            start_offset = ''
            if self.start:
                start_offset = self.start_code()
                if start_offset == '0':
                    start_offset = ''
                else:
                    start_offset += '+'
Stefan Behnel's avatar
Stefan Behnel committed
2759 2760
            if rhs.type.is_array:
                array_length = rhs.type.size
2761
                self.generate_slice_guard_code(code, array_length)
Stefan Behnel's avatar
Stefan Behnel committed
2762
            else:
Stefan Behnel's avatar
Stefan Behnel committed
2763 2764
                error(self.pos,
                      "Slice assignments from pointers are not yet supported.")
Stefan Behnel's avatar
Stefan Behnel committed
2765 2766
                # FIXME: fix the array size according to start/stop
                array_length = self.base.type.size
2767 2768 2769 2770
            for i in range(array_length):
                code.putln("%s[%s%s] = %s[%d];" % (
                        self.base.result(), start_offset, i,
                        rhs.result(), i))
William Stein's avatar
William Stein committed
2771
        self.generate_subexpr_disposal_code(code)
2772
        self.free_subexpr_temps(code)
William Stein's avatar
William Stein committed
2773
        rhs.generate_disposal_code(code)
2774
        rhs.free_temps(code)
William Stein's avatar
William Stein committed
2775 2776

    def generate_deletion_code(self, code):
Robert Bradshaw's avatar
Robert Bradshaw committed
2777
        if not self.base.type.is_pyobject:
2778 2779 2780
            error(self.pos,
                  "Deleting slices is only supported for Python types, not '%s'." % self.type)
            return
William Stein's avatar
William Stein committed
2781
        self.generate_subexpr_evaluation_code(code)
Robert Bradshaw's avatar
Robert Bradshaw committed
2782
        code.put_error_if_neg(self.pos,
2783
            "__Pyx_PySequence_DelSlice(%s, %s, %s)" % (
William Stein's avatar
William Stein committed
2784 2785
                self.base.py_result(),
                self.start_code(),
Robert Bradshaw's avatar
Robert Bradshaw committed
2786
                self.stop_code()))
William Stein's avatar
William Stein committed
2787
        self.generate_subexpr_disposal_code(code)
2788
        self.free_subexpr_temps(code)
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798

    def generate_slice_guard_code(self, code, target_size):
        if not self.base.type.is_array:
            return
        slice_size = self.base.type.size
        start = stop = None
        if self.stop:
            stop = self.stop.result()
            try:
                stop = int(stop)
Stefan Behnel's avatar
Stefan Behnel committed
2799
                if stop < 0:
2800
                    slice_size = self.base.type.size + stop
Stefan Behnel's avatar
Stefan Behnel committed
2801 2802
                else:
                    slice_size = stop
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
                stop = None
            except ValueError:
                pass
        if self.start:
            start = self.start.result()
            try:
                start = int(start)
                if start < 0:
                    start = self.base.type.size + start
                slice_size -= start
                start = None
            except ValueError:
                pass
        check = None
        if slice_size < 0:
            if target_size > 0:
                error(self.pos, "Assignment to empty slice.")
        elif start is None and stop is None:
            # we know the exact slice length
            if target_size != slice_size:
                error(self.pos, "Assignment to slice of wrong length, expected %d, got %d" % (
                        slice_size, target_size))
        elif start is not None:
            if stop is None:
                stop = slice_size
            check = "(%s)-(%s)" % (stop, start)
        else: # stop is not None:
            check = stop
        if check:
            code.putln("if (unlikely((%s) != %d)) {" % (check, target_size))
2833
            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));' % (
2834 2835 2836
                        target_size, check))
            code.putln(code.error_goto(self.pos))
            code.putln("}")
2837

William Stein's avatar
William Stein committed
2838 2839
    def start_code(self):
        if self.start:
2840
            return self.start.result()
William Stein's avatar
William Stein committed
2841 2842
        else:
            return "0"
2843

William Stein's avatar
William Stein committed
2844 2845
    def stop_code(self):
        if self.stop:
2846
            return self.stop.result()
2847 2848
        elif self.base.type.is_array:
            return self.base.type.size
William Stein's avatar
William Stein committed
2849
        else:
2850
            return "PY_SSIZE_T_MAX"
2851

William Stein's avatar
William Stein committed
2852
    def calculate_result_code(self):
2853
        # self.result() is not used, but this method must exist
William Stein's avatar
William Stein committed
2854
        return "<unused>"
2855

William Stein's avatar
William Stein committed
2856

2857
class SliceNode(ExprNode):
William Stein's avatar
William Stein committed
2858 2859 2860 2861 2862
    #  start:stop:step in subscript list
    #
    #  start     ExprNode
    #  stop      ExprNode
    #  step      ExprNode
2863

2864 2865
    subexprs = ['start', 'stop', 'step']

2866 2867
    type = py_object_type
    is_temp = 1
2868 2869

    def calculate_constant_result(self):
2870 2871 2872 2873
        self.constant_result = slice(
            self.start.constant_result,
            self.stop.constant_result,
            self.step.constant_result)
2874

2875 2876
    def compile_time_value(self, denv):
        start = self.start.compile_time_value(denv)
Stefan Behnel's avatar
Stefan Behnel committed
2877 2878
        stop = self.stop.compile_time_value(denv)
        step = self.step.compile_time_value(denv)
2879 2880 2881 2882 2883
        try:
            return slice(start, stop, step)
        except Exception, e:
            self.compile_time_value_error(e)

William Stein's avatar
William Stein committed
2884 2885 2886 2887 2888 2889 2890
    def analyse_types(self, env):
        self.start.analyse_types(env)
        self.stop.analyse_types(env)
        self.step.analyse_types(env)
        self.start = self.start.coerce_to_pyobject(env)
        self.stop = self.stop.coerce_to_pyobject(env)
        self.step = self.step.coerce_to_pyobject(env)
2891 2892 2893
        if self.start.is_literal and self.stop.is_literal and self.step.is_literal:
            self.is_literal = True
            self.is_temp = False
2894 2895 2896

    gil_message = "Constructing Python slice object"

2897 2898 2899
    def calculate_result_code(self):
        return self.result_code

William Stein's avatar
William Stein committed
2900
    def generate_result_code(self, code):
2901 2902 2903 2904 2905
        if self.is_literal:
            self.result_code = code.get_py_const(py_object_type, 'slice_', cleanup_level=2)
            code = code.get_cached_constants_writer()
            code.mark_pos(self.pos)

William Stein's avatar
William Stein committed
2906
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
2907
            "%s = PySlice_New(%s, %s, %s); %s" % (
2908
                self.result(),
2909 2910
                self.start.py_result(),
                self.stop.py_result(),
William Stein's avatar
William Stein committed
2911
                self.step.py_result(),
2912
                code.error_goto_if_null(self.result(), self.pos)))
2913
        code.put_gotref(self.py_result())
2914 2915
        if self.is_literal:
            code.put_giveref(self.py_result())
William Stein's avatar
William Stein committed
2916

2917

2918
class CallNode(ExprNode):
2919

Stefan Behnel's avatar
Stefan Behnel committed
2920 2921 2922 2923 2924 2925 2926 2927
    # allow overriding the default 'may_be_none' behaviour
    may_return_none = None

    def may_be_none(self):
        if self.may_return_none is not None:
            return self.may_return_none
        return ExprNode.may_be_none(self)

Robert Bradshaw's avatar
Robert Bradshaw committed
2928 2929 2930 2931 2932 2933
    def analyse_as_type_constructor(self, env):
        type = self.function.analyse_as_type(env)
        if type and type.is_struct_or_union:
            args, kwds = self.explicit_args_kwds()
            items = []
            for arg, member in zip(args, type.scope.var_entries):
2934
                items.append(DictItemNode(pos=arg.pos, key=StringNode(pos=arg.pos, value=member.name), value=arg))
Robert Bradshaw's avatar
Robert Bradshaw committed
2935 2936 2937 2938 2939 2940 2941
            if kwds:
                items += kwds.key_value_pairs
            self.key_value_pairs = items
            self.__class__ = DictNode
            self.analyse_types(env)
            self.coerce_to(type, env)
            return True
2942 2943 2944 2945 2946 2947 2948 2949 2950
        elif type and type.is_cpp_class:
            for arg in self.args:
                arg.analyse_types(env)
            constructor = type.scope.lookup("<init>")
            self.function = RawCNameExprNode(self.function.pos, constructor.type)
            self.function.entry = constructor
            self.function.set_cname(type.declaration_code(""))
            self.analyse_c_function_call(env)
            return True
2951

2952 2953
    def is_lvalue(self):
        return self.type.is_reference
2954

2955
    def nogil_check(self, env):
2956 2957
        func_type = self.function_type()
        if func_type.is_pyobject:
2958
            self.gil_error()
2959
        elif not getattr(func_type, 'nogil', False):
2960
            self.gil_error()
2961 2962 2963

    gil_message = "Calling gil-requiring function"

2964 2965

class SimpleCallNode(CallNode):
William Stein's avatar
William Stein committed
2966 2967 2968 2969 2970 2971 2972
    #  Function call without keyword, * or ** args.
    #
    #  function       ExprNode
    #  args           [ExprNode]
    #  arg_tuple      ExprNode or None     used internally
    #  self           ExprNode or None     used internally
    #  coerced_self   ExprNode or None     used internally
2973
    #  wrapper_call   bool                 used internally
2974
    #  has_optional_args   bool            used internally
2975
    #  nogil          bool                 used internally
2976

William Stein's avatar
William Stein committed
2977
    subexprs = ['self', 'coerced_self', 'function', 'args', 'arg_tuple']
2978

William Stein's avatar
William Stein committed
2979 2980 2981
    self = None
    coerced_self = None
    arg_tuple = None
2982
    wrapper_call = False
2983
    has_optional_args = False
2984
    nogil = False
2985
    analysed = False
2986

2987 2988 2989 2990 2991 2992 2993
    def compile_time_value(self, denv):
        function = self.function.compile_time_value(denv)
        args = [arg.compile_time_value(denv) for arg in self.args]
        try:
            return function(*args)
        except Exception, e:
            self.compile_time_value_error(e)
2994

Robert Bradshaw's avatar
Robert Bradshaw committed
2995
    def type_dependencies(self, env):
2996 2997
        # TODO: Update when Danilo's C++ code merged in to handle the
        # the case of function overloading.
Robert Bradshaw's avatar
Robert Bradshaw committed
2998
        return self.function.type_dependencies(env)
2999

3000
    def infer_type(self, env):
3001 3002
        function = self.function
        func_type = function.infer_type(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
3003 3004
        if isinstance(self.function, NewExprNode):
            return PyrexTypes.CPtrType(self.function.class_type)
3005 3006 3007 3008
        if func_type.is_ptr:
            func_type = func_type.base_type
        if func_type.is_cfunction:
            return func_type.return_type
3009 3010 3011 3012 3013 3014
        elif func_type is type_type:
            if function.is_name and function.entry and function.entry.type:
                result_type = function.entry.type
                if result_type.is_extension_type:
                    return result_type
                elif result_type.is_builtin_type:
3015 3016 3017
                    if function.entry.name == 'float':
                        return PyrexTypes.c_double_type
                    elif function.entry.name in Builtin.types_that_construct_their_instance:
3018 3019 3020
                        return result_type
        return py_object_type

3021
    def analyse_as_type(self, env):
3022
        attr = self.function.as_cython_attribute()
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
        if attr == 'pointer':
            if len(self.args) != 1:
                error(self.args.pos, "only one type allowed.")
            else:
                type = self.args[0].analyse_as_type(env)
                if not type:
                    error(self.args[0].pos, "Unknown type")
                else:
                    return PyrexTypes.CPtrType(type)

    def explicit_args_kwds(self):
        return self.args, None
3035

William Stein's avatar
William Stein committed
3036
    def analyse_types(self, env):
Robert Bradshaw's avatar
Robert Bradshaw committed
3037 3038
        if self.analyse_as_type_constructor(env):
            return
3039 3040 3041
        if self.analysed:
            return
        self.analysed = True
William Stein's avatar
William Stein committed
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
        function = self.function
        function.is_called = 1
        self.function.analyse_types(env)
        if function.is_attribute and function.entry and function.entry.is_cmethod:
            # Take ownership of the object from which the attribute
            # was obtained, because we need to pass it as 'self'.
            self.self = function.obj
            function.obj = CloneNode(self.self)
        func_type = self.function_type()
        if func_type.is_pyobject:
3052 3053
            self.arg_tuple = TupleNode(self.pos, args = self.args)
            self.arg_tuple.analyse_types(env)
William Stein's avatar
William Stein committed
3054
            self.args = None
3055 3056 3057
            if func_type is Builtin.type_type and function.is_name and \
                   function.entry and \
                   function.entry.is_builtin and \
3058 3059 3060 3061 3062 3063 3064 3065 3066
                   function.entry.name in Builtin.types_that_construct_their_instance:
                # calling a builtin type that returns a specific object type
                if function.entry.name == 'float':
                    # the following will come true later on in a transform
                    self.type = PyrexTypes.c_double_type
                    self.result_ctype = PyrexTypes.c_double_type
                else:
                    self.type = Builtin.builtin_types[function.entry.name]
                    self.result_ctype = py_object_type
Stefan Behnel's avatar
Stefan Behnel committed
3067
                self.may_return_none = False
3068
            elif function.is_name and function.type_entry:
3069 3070 3071 3072 3073
                # We are calling an extension type constructor.  As
                # long as we do not support __new__(), the result type
                # is clear
                self.type = function.type_entry.type
                self.result_ctype = py_object_type
Stefan Behnel's avatar
Stefan Behnel committed
3074
                self.may_return_none = False
3075 3076
            else:
                self.type = py_object_type
William Stein's avatar
William Stein committed
3077 3078 3079 3080 3081 3082
            self.is_temp = 1
        else:
            for arg in self.args:
                arg.analyse_types(env)
            if self.self and func_type.args:
                # Coerce 'self' to the type expected by the method.
3083 3084 3085
                self_arg = func_type.args[0]
                if self_arg.not_none: # C methods must do the None test for self at *call* time
                    self.self = self.self.as_none_safe_node(
3086 3087
                        "'NoneType' object has no attribute '%s'" % self.function.entry.name,
                        'PyExc_AttributeError')
3088
                expected_type = self_arg.type
William Stein's avatar
William Stein committed
3089 3090 3091 3092 3093
                self.coerced_self = CloneNode(self.self).coerce_to(
                    expected_type, env)
                # Insert coerced 'self' argument into argument list.
                self.args.insert(0, self.coerced_self)
            self.analyse_c_function_call(env)
3094

William Stein's avatar
William Stein committed
3095 3096 3097 3098 3099 3100 3101
    def function_type(self):
        # Return the type of the function being called, coercing a function
        # pointer to a function if necessary.
        func_type = self.function.type
        if func_type.is_ptr:
            func_type = func_type.base_type
        return func_type
3102

3103 3104 3105 3106 3107 3108 3109
    def is_simple(self):
        # C function calls could be considered simple, but they may
        # have side-effects that may hit when multiple operations must
        # be effected in order, e.g. when constructing the argument
        # sequence for a function call or comparing values.
        return False

William Stein's avatar
William Stein committed
3110
    def analyse_c_function_call(self, env):
Robert Bradshaw's avatar
Robert Bradshaw committed
3111
        if self.function.type is error_type:
3112
            self.type = error_type
Robert Bradshaw's avatar
Robert Bradshaw committed
3113
            return
Robert Bradshaw's avatar
Robert Bradshaw committed
3114
        if self.function.type.is_cpp_class:
3115 3116
            overloaded_entry = self.function.type.scope.lookup("operator()")
            if overloaded_entry is None:
Robert Bradshaw's avatar
Robert Bradshaw committed
3117 3118 3119
                self.type = PyrexTypes.error_type
                self.result_code = "<error>"
                return
3120 3121
        elif hasattr(self.function, 'entry'):
            overloaded_entry = self.function.entry
Robert Bradshaw's avatar
Robert Bradshaw committed
3122
        else:
3123 3124 3125 3126 3127 3128 3129 3130 3131
            overloaded_entry = None
        if overloaded_entry:
            entry = PyrexTypes.best_match(self.args, overloaded_entry.all_alternatives(), self.pos)
            if not entry:
                self.type = PyrexTypes.error_type
                self.result_code = "<error>"
                return
            self.function.entry = entry
            self.function.type = entry.type
3132 3133 3134 3135 3136 3137 3138 3139
            func_type = self.function_type()
        else:
            func_type = self.function_type()
            if not func_type.is_cfunction:
                error(self.pos, "Calling non-function type '%s'" % func_type)
                self.type = PyrexTypes.error_type
                self.result_code = "<error>"
                return
William Stein's avatar
William Stein committed
3140
        # Check no. of args
3141 3142
        max_nargs = len(func_type.args)
        expected_nargs = max_nargs - func_type.optional_arg_count
William Stein's avatar
William Stein committed
3143
        actual_nargs = len(self.args)
3144 3145 3146
        if func_type.optional_arg_count and expected_nargs != actual_nargs:
            self.has_optional_args = 1
            self.is_temp = 1
William Stein's avatar
William Stein committed
3147
        # Coerce arguments
3148
        some_args_in_temps = False
3149
        for i in xrange(min(max_nargs, actual_nargs)):
William Stein's avatar
William Stein committed
3150
            formal_type = func_type.args[i].type
3151
            arg = self.args[i].coerce_to(formal_type, env)
3152
            if arg.is_temp:
3153 3154
                if i > 0:
                    # first argument in temp doesn't impact subsequent arguments
3155
                    some_args_in_temps = True
3156
            elif arg.type.is_pyobject and not env.nogil:
3157 3158
                if i == 0 and self.self is not None:
                    # a method's cloned "self" argument is ok
3159
                    pass
3160
                elif arg.nonlocally_immutable():
3161 3162 3163
                    # plain local variables are ok
                    pass
                else:
3164 3165 3166 3167
                    # we do not safely own the argument's reference,
                    # but we must make sure it cannot be collected
                    # before we return from the function, so we create
                    # an owned temp reference to it
3168 3169
                    if i > 0: # first argument doesn't matter
                        some_args_in_temps = True
3170
                    arg = arg.coerce_to_temp(env)
3171
            self.args[i] = arg
3172
        # handle additional varargs parameters
3173
        for i in xrange(max_nargs, actual_nargs):
3174 3175 3176 3177 3178 3179 3180
            arg = self.args[i]
            if arg.type.is_pyobject:
                arg_ctype = arg.type.default_coerced_ctype()
                if arg_ctype is None:
                    error(self.args[i].pos,
                          "Python object cannot be passed as a varargs parameter")
                else:
3181
                    self.args[i] = arg = arg.coerce_to(arg_ctype, env)
3182 3183
            if arg.is_temp and i > 0:
                some_args_in_temps = True
3184 3185 3186
        if some_args_in_temps:
            # if some args are temps and others are not, they may get
            # constructed in the wrong order (temps first) => make
3187 3188 3189 3190
            # sure they are either all temps or all not temps (except
            # for the last argument, which is evaluated last in any
            # case)
            for i in xrange(actual_nargs-1):
3191 3192
                if i == 0 and self.self is not None:
                    continue # self is ok
3193
                arg = self.args[i]
3194 3195
                if arg.nonlocally_immutable():
                    # locals, C functions, unassignable types are safe.
3196
                    pass
3197 3198
                elif arg.type.is_cpp_class:
                    # Assignment has side effects, avoid.
3199 3200
                    pass
                elif env.nogil and arg.type.is_pyobject:
3201 3202 3203
                    # can't copy a Python reference into a temp in nogil
                    # env (this is safe: a construction would fail in
                    # nogil anyway)
3204 3205
                    pass
                else:
3206 3207 3208 3209 3210
                    #self.args[i] = arg.coerce_to_temp(env)
                    # instead: issue a warning
                    if i > 0 or i == 1 and self.self is not None: # skip first arg
                        warning(arg.pos, "Argument evaluation order in C function call is undefined and may not be as expected", 0)
                        break
William Stein's avatar
William Stein committed
3211
        # Calc result type and code fragment
Robert Bradshaw's avatar
Robert Bradshaw committed
3212
        if isinstance(self.function, NewExprNode):
3213
            self.type = PyrexTypes.CPtrType(self.function.class_type)
Robert Bradshaw's avatar
Robert Bradshaw committed
3214 3215
        else:
            self.type = func_type.return_type
Stefan Behnel's avatar
Stefan Behnel committed
3216 3217 3218 3219 3220 3221
        if self.type.is_pyobject:
            self.result_ctype = py_object_type
            self.is_temp = 1
        elif func_type.exception_value is not None \
                 or func_type.exception_check:
            self.is_temp = 1
3222
        # Called in 'nogil' context?
3223
        self.nogil = env.nogil
3224 3225 3226 3227 3228
        if (self.nogil and
            func_type.exception_check and
            func_type.exception_check != '+'):
            env.use_utility_code(pyerr_occurred_withgil_utility_code)
        # C++ exception handler
Robert Bradshaw's avatar
Robert Bradshaw committed
3229 3230 3231 3232
        if func_type.exception_check == '+':
            if func_type.exception_value is None:
                env.use_utility_code(cpp_exception_utility_code)

William Stein's avatar
William Stein committed
3233 3234
    def calculate_result_code(self):
        return self.c_call_code()
3235

William Stein's avatar
William Stein committed
3236 3237
    def c_call_code(self):
        func_type = self.function_type()
3238
        if self.type is PyrexTypes.error_type or not func_type.is_cfunction:
William Stein's avatar
William Stein committed
3239 3240 3241
            return "<error>"
        formal_args = func_type.args
        arg_list_code = []
3242
        args = list(zip(formal_args, self.args))
3243 3244 3245 3246
        max_nargs = len(func_type.args)
        expected_nargs = max_nargs - func_type.optional_arg_count
        actual_nargs = len(self.args)
        for formal_arg, actual_arg in args[:expected_nargs]:
William Stein's avatar
William Stein committed
3247 3248
                arg_code = actual_arg.result_as(formal_arg.type)
                arg_list_code.append(arg_code)
3249

3250 3251
        if func_type.is_overridable:
            arg_list_code.append(str(int(self.wrapper_call or self.function.entry.is_unbound_cmethod)))
3252

3253
        if func_type.optional_arg_count:
3254
            if expected_nargs == actual_nargs:
3255
                optional_args = 'NULL'
3256
            else:
3257
                optional_args = "&%s" % self.opt_arg_struct
3258
            arg_list_code.append(optional_args)
3259

William Stein's avatar
William Stein committed
3260
        for actual_arg in self.args[len(formal_args):]:
3261 3262
            arg_list_code.append(actual_arg.result())
        result = "%s(%s)" % (self.function.result(),
Stefan Behnel's avatar
Stefan Behnel committed
3263
            ', '.join(arg_list_code))
William Stein's avatar
William Stein committed
3264
        return result
3265

William Stein's avatar
William Stein committed
3266 3267 3268
    def generate_result_code(self, code):
        func_type = self.function_type()
        if func_type.is_pyobject:
3269
            arg_code = self.arg_tuple.py_result()
William Stein's avatar
William Stein committed
3270
            code.putln(
3271
                "%s = PyObject_Call(%s, %s, NULL); %s" % (
3272
                    self.result(),
William Stein's avatar
William Stein committed
3273
                    self.function.py_result(),
3274
                    arg_code,
3275
                    code.error_goto_if_null(self.result(), self.pos)))
3276
            code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
3277
        elif func_type.is_cfunction:
3278 3279 3280
            if self.has_optional_args:
                actual_nargs = len(self.args)
                expected_nargs = len(func_type.args) - func_type.optional_arg_count
3281 3282
                self.opt_arg_struct = code.funcstate.allocate_temp(
                    func_type.op_arg_struct.base_type, manage_ref=True)
3283 3284 3285 3286
                code.putln("%s.%s = %s;" % (
                        self.opt_arg_struct,
                        Naming.pyrex_prefix + "n",
                        len(self.args) - expected_nargs))
3287
                args = list(zip(func_type.args, self.args))
3288 3289 3290
                for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
                    code.putln("%s.%s = %s;" % (
                            self.opt_arg_struct,
3291
                            func_type.opt_arg_cname(formal_arg.name),
3292
                            actual_arg.result_as(formal_arg.type)))
William Stein's avatar
William Stein committed
3293
            exc_checks = []
3294
            if self.type.is_pyobject and self.is_temp:
3295
                exc_checks.append("!%s" % self.result())
William Stein's avatar
William Stein committed
3296
            else:
3297 3298
                exc_val = func_type.exception_value
                exc_check = func_type.exception_check
William Stein's avatar
William Stein committed
3299
                if exc_val is not None:
3300
                    exc_checks.append("%s == %s" % (self.result(), exc_val))
William Stein's avatar
William Stein committed
3301
                if exc_check:
3302 3303
                    if self.nogil:
                        exc_checks.append("__Pyx_ErrOccurredWithGIL()")
3304
                    else:
3305
                        exc_checks.append("PyErr_Occurred()")
William Stein's avatar
William Stein committed
3306 3307
            if self.is_temp or exc_checks:
                rhs = self.c_call_code()
3308 3309
                if self.result():
                    lhs = "%s = " % self.result()
William Stein's avatar
William Stein committed
3310 3311 3312
                    if self.is_temp and self.type.is_pyobject:
                        #return_type = self.type # func_type.return_type
                        #print "SimpleCallNode.generate_result_code: casting", rhs, \
Robert Bradshaw's avatar
Robert Bradshaw committed
3313
                        #    "from", return_type, "to pyobject" ###
William Stein's avatar
William Stein committed
3314 3315 3316
                        rhs = typecast(py_object_type, self.type, rhs)
                else:
                    lhs = ""
Felix Wu's avatar
Felix Wu committed
3317
                if func_type.exception_check == '+':
Robert Bradshaw's avatar
Robert Bradshaw committed
3318 3319 3320
                    if func_type.exception_value is None:
                        raise_py_exception = "__Pyx_CppExn2PyErr()"
                    elif func_type.exception_value.type.is_pyobject:
3321 3322 3323
                        raise_py_exception = ' try { throw; } catch(const std::exception& exn) { PyErr_SetString(%s, exn.what()); } catch(...) { PyErr_SetNone(%s); }' % (
                            func_type.exception_value.entry.cname,
                            func_type.exception_value.entry.cname)
Robert Bradshaw's avatar
Robert Bradshaw committed
3324 3325
                    else:
                        raise_py_exception = '%s(); if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError , "Error converting c++ exception.")' % func_type.exception_value.entry.cname
3326 3327
                    if self.nogil:
                        raise_py_exception = 'Py_BLOCK_THREADS; %s; Py_UNBLOCK_THREADS' % raise_py_exception
Felix Wu's avatar
Felix Wu committed
3328
                    code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
3329
                    "try {%s%s;} catch(...) {%s; %s}" % (
Felix Wu's avatar
Felix Wu committed
3330 3331
                        lhs,
                        rhs,
Robert Bradshaw's avatar
Robert Bradshaw committed
3332
                        raise_py_exception,
Felix Wu's avatar
Felix Wu committed
3333
                        code.error_goto(self.pos)))
3334 3335 3336 3337 3338 3339
                else:
                    if exc_checks:
                        goto_error = code.error_goto_if(" && ".join(exc_checks), self.pos)
                    else:
                        goto_error = ""
                    code.putln("%s%s; %s" % (lhs, rhs, goto_error))
3340
                if self.type.is_pyobject and self.result():
3341
                    code.put_gotref(self.py_result())
3342 3343
            if self.has_optional_args:
                code.funcstate.release_temp(self.opt_arg_struct)
3344 3345 3346 3347


class PythonCapiFunctionNode(ExprNode):
    subexprs = []
3348
    def __init__(self, pos, py_name, cname, func_type, utility_code = None):
3349
        self.pos = pos
3350 3351
        self.name = py_name
        self.cname = cname
3352 3353 3354
        self.type = func_type
        self.utility_code = utility_code

3355 3356 3357
    def analyse_types(self, env):
        pass

3358 3359 3360 3361 3362
    def generate_result_code(self, code):
        if self.utility_code:
            code.globalstate.use_utility_code(self.utility_code)

    def calculate_result_code(self):
3363
        return self.cname
3364 3365 3366 3367

class PythonCapiCallNode(SimpleCallNode):
    # Python C-API Function call (only created in transforms)

Stefan Behnel's avatar
Stefan Behnel committed
3368 3369 3370 3371 3372 3373
    # By default, we assume that the call never returns None, as this
    # is true for most C-API functions in CPython.  If this does not
    # apply to a call, set the following to True (or None to inherit
    # the default behaviour).
    may_return_none = False

3374
    def __init__(self, pos, function_name, func_type,
3375
                 utility_code = None, py_name=None, **kwargs):
3376 3377 3378
        self.type = func_type.return_type
        self.result_ctype = self.type
        self.function = PythonCapiFunctionNode(
3379
            pos, py_name, function_name, func_type,
3380 3381 3382 3383 3384
            utility_code = utility_code)
        # call this last so that we can override the constructed
        # attributes above with explicit keyword arguments if required
        SimpleCallNode.__init__(self, pos, **kwargs)

William Stein's avatar
William Stein committed
3385

3386
class GeneralCallNode(CallNode):
William Stein's avatar
William Stein committed
3387 3388 3389 3390 3391 3392 3393
    #  General Python function call, including keyword,
    #  * and ** arguments.
    #
    #  function         ExprNode
    #  positional_args  ExprNode          Tuple of positional arguments
    #  keyword_args     ExprNode or None  Dict of keyword arguments
    #  starstar_arg     ExprNode or None  Dict of extra keyword args
3394

3395
    type = py_object_type
3396

William Stein's avatar
William Stein committed
3397 3398
    subexprs = ['function', 'positional_args', 'keyword_args', 'starstar_arg']

3399
    nogil_check = Node.gil_error
3400

3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
    def compile_time_value(self, denv):
        function = self.function.compile_time_value(denv)
        positional_args = self.positional_args.compile_time_value(denv)
        keyword_args = self.keyword_args.compile_time_value(denv)
        starstar_arg = self.starstar_arg.compile_time_value(denv)
        try:
            keyword_args.update(starstar_arg)
            return function(*positional_args, **keyword_args)
        except Exception, e:
            self.compile_time_value_error(e)
3411

3412 3413
    def explicit_args_kwds(self):
        if self.starstar_arg or not isinstance(self.positional_args, TupleNode):
3414
            raise CompileError(self.pos,
3415 3416
                'Compile-time keyword arguments must be explicit.')
        return self.positional_args.args, self.keyword_args
3417

William Stein's avatar
William Stein committed
3418
    def analyse_types(self, env):
Robert Bradshaw's avatar
Robert Bradshaw committed
3419 3420
        if self.analyse_as_type_constructor(env):
            return
William Stein's avatar
William Stein committed
3421 3422 3423 3424 3425 3426
        self.function.analyse_types(env)
        self.positional_args.analyse_types(env)
        if self.keyword_args:
            self.keyword_args.analyse_types(env)
        if self.starstar_arg:
            self.starstar_arg.analyse_types(env)
3427
        if not self.function.type.is_pyobject:
3428 3429
            if self.function.type.is_error:
                self.type = error_type
Stefan Behnel's avatar
Stefan Behnel committed
3430
                return
3431
            if hasattr(self.function, 'entry') and not self.function.entry.as_variable:
3432
                error(self.pos, "Keyword and starred arguments not allowed in cdef functions.")
3433 3434
            else:
                self.function = self.function.coerce_to_pyobject(env)
William Stein's avatar
William Stein committed
3435 3436 3437 3438 3439
        self.positional_args = \
            self.positional_args.coerce_to_pyobject(env)
        if self.starstar_arg:
            self.starstar_arg = \
                self.starstar_arg.coerce_to_pyobject(env)
Stefan Behnel's avatar
Stefan Behnel committed
3440
        function = self.function
3441 3442 3443 3444 3445
        if function.is_name and function.type_entry:
            # We are calling an extension type constructor.  As long
            # as we do not support __new__(), the result type is clear
            self.type = function.type_entry.type
            self.result_ctype = py_object_type
Stefan Behnel's avatar
Stefan Behnel committed
3446
            self.may_return_none = False
3447 3448
        else:
            self.type = py_object_type
William Stein's avatar
William Stein committed
3449
        self.is_temp = 1
3450

William Stein's avatar
William Stein committed
3451
    def generate_result_code(self, code):
3452
        if self.type.is_error: return
3453
        kwargs_call_function = "PyEval_CallObjectWithKeywords"
William Stein's avatar
William Stein committed
3454
        if self.keyword_args and self.starstar_arg:
3455
            code.put_error_if_neg(self.pos,
Robert Bradshaw's avatar
Robert Bradshaw committed
3456
                "PyDict_Update(%s, %s)" % (
3457
                    self.keyword_args.py_result(),
Robert Bradshaw's avatar
Robert Bradshaw committed
3458
                    self.starstar_arg.py_result()))
William Stein's avatar
William Stein committed
3459 3460 3461 3462 3463
            keyword_code = self.keyword_args.py_result()
        elif self.keyword_args:
            keyword_code = self.keyword_args.py_result()
        elif self.starstar_arg:
            keyword_code = self.starstar_arg.py_result()
3464 3465
            if self.starstar_arg.type is not Builtin.dict_type:
                # CPython supports calling functions with non-dicts, so do we
3466 3467
                code.globalstate.use_utility_code(kwargs_call_utility_code)
                kwargs_call_function = "__Pyx_PyEval_CallObjectWithKeywords"
William Stein's avatar
William Stein committed
3468 3469 3470
        else:
            keyword_code = None
        if not keyword_code:
3471
            call_code = "PyObject_Call(%s, %s, NULL)" % (
William Stein's avatar
William Stein committed
3472 3473 3474
                self.function.py_result(),
                self.positional_args.py_result())
        else:
3475 3476
            call_code = "%s(%s, %s, %s)" % (
                kwargs_call_function,
William Stein's avatar
William Stein committed
3477 3478 3479 3480
                self.function.py_result(),
                self.positional_args.py_result(),
                keyword_code)
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
3481
            "%s = %s; %s" % (
3482
                self.result(),
William Stein's avatar
William Stein committed
3483
                call_code,
3484
                code.error_goto_if_null(self.result(), self.pos)))
3485
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
3486 3487


3488
class AsTupleNode(ExprNode):
William Stein's avatar
William Stein committed
3489 3490 3491 3492
    #  Convert argument to tuple. Used for normalising
    #  the * argument of a function call.
    #
    #  arg    ExprNode
3493

William Stein's avatar
William Stein committed
3494
    subexprs = ['arg']
3495 3496 3497

    def calculate_constant_result(self):
        self.constant_result = tuple(self.base.constant_result)
3498

3499 3500 3501 3502 3503 3504 3505
    def compile_time_value(self, denv):
        arg = self.arg.compile_time_value(denv)
        try:
            return tuple(arg)
        except Exception, e:
            self.compile_time_value_error(e)

William Stein's avatar
William Stein committed
3506 3507 3508
    def analyse_types(self, env):
        self.arg.analyse_types(env)
        self.arg = self.arg.coerce_to_pyobject(env)
3509
        self.type = tuple_type
William Stein's avatar
William Stein committed
3510
        self.is_temp = 1
3511

3512 3513 3514
    def may_be_none(self):
        return False

3515
    nogil_check = Node.gil_error
3516 3517
    gil_message = "Constructing Python tuple"

William Stein's avatar
William Stein committed
3518 3519
    def generate_result_code(self, code):
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
3520
            "%s = PySequence_Tuple(%s); %s" % (
3521
                self.result(),
William Stein's avatar
William Stein committed
3522
                self.arg.py_result(),
3523
                code.error_goto_if_null(self.result(), self.pos)))
3524
        code.put_gotref(self.py_result())
3525

William Stein's avatar
William Stein committed
3526

3527
class AttributeNode(ExprNode):
William Stein's avatar
William Stein committed
3528 3529 3530 3531
    #  obj.attribute
    #
    #  obj          ExprNode
    #  attribute    string
3532
    #  needs_none_check boolean        Used if obj is an extension type.
3533
    #                                  If set to True, it is known that the type is not None.
William Stein's avatar
William Stein committed
3534 3535 3536 3537 3538 3539 3540
    #
    #  Used internally:
    #
    #  is_py_attr           boolean   Is a Python getattr operation
    #  member               string    C name of struct member
    #  is_called            boolean   Function call is being done on result
    #  entry                Entry     Symbol table entry of attribute
3541

William Stein's avatar
William Stein committed
3542 3543
    is_attribute = 1
    subexprs = ['obj']
3544

William Stein's avatar
William Stein committed
3545 3546 3547
    type = PyrexTypes.error_type
    entry = None
    is_called = 0
3548
    needs_none_check = True
William Stein's avatar
William Stein committed
3549

3550
    def as_cython_attribute(self):
Mark Florisson's avatar
Mark Florisson committed
3551 3552 3553
        if (isinstance(self.obj, NameNode) and
                self.obj.is_cython_module and not
                self.attribute == u"parallel"):
3554
            return self.attribute
Mark Florisson's avatar
Mark Florisson committed
3555

3556 3557 3558
        cy = self.obj.as_cython_attribute()
        if cy:
            return "%s.%s" % (cy, self.attribute)
3559

3560 3561 3562 3563 3564 3565 3566 3567 3568
    def coerce_to(self, dst_type, env):
        #  If coercing to a generic pyobject and this is a cpdef function
        #  we can create the corresponding attribute
        if dst_type is py_object_type:
            entry = self.entry
            if entry and entry.is_cfunction and entry.as_variable:
                # must be a cpdef function
                self.is_temp = 1
                self.entry = entry.as_variable
3569
                self.analyse_as_python_attribute(env)
3570
                return self
3571
        return ExprNode.coerce_to(self, dst_type, env)
3572 3573 3574

    def calculate_constant_result(self):
        attr = self.attribute
3575
        if attr.startswith("__") and attr.endswith("__"):
3576 3577 3578
            return
        self.constant_result = getattr(self.obj.constant_result, attr)

3579 3580
    def compile_time_value(self, denv):
        attr = self.attribute
3581
        if attr.startswith("__") and attr.endswith("__"):
Stefan Behnel's avatar
Stefan Behnel committed
3582 3583
            error(self.pos,
                  "Invalid attribute name '%s' in compile-time expression" % attr)
3584
            return None
3585
        obj = self.obj.compile_time_value(denv)
3586 3587 3588 3589
        try:
            return getattr(obj, attr)
        except Exception, e:
            self.compile_time_value_error(e)
3590

Robert Bradshaw's avatar
Robert Bradshaw committed
3591 3592
    def type_dependencies(self, env):
        return self.obj.type_dependencies(env)
3593

3594 3595 3596 3597 3598 3599
    def infer_type(self, env):
        if self.analyse_as_cimported_attribute(env, 0):
            return self.entry.type
        elif self.analyse_as_unbound_cmethod(env):
            return self.entry.type
        else:
3600 3601 3602 3603 3604 3605 3606 3607
            obj_type = self.obj.infer_type(env)
            self.analyse_attribute(env, obj_type = obj_type)
            if obj_type.is_builtin_type and self.type.is_cfunction:
                # special case: C-API replacements for C methods of
                # builtin types cannot be inferred as C functions as
                # that would prevent their use as bound methods
                self.type = py_object_type
                return py_object_type
3608
            return self.type
3609

William Stein's avatar
William Stein committed
3610 3611
    def analyse_target_declaration(self, env):
        pass
3612

William Stein's avatar
William Stein committed
3613 3614
    def analyse_target_types(self, env):
        self.analyse_types(env, target = 1)
3615

William Stein's avatar
William Stein committed
3616 3617 3618 3619 3620 3621
    def analyse_types(self, env, target = 0):
        if self.analyse_as_cimported_attribute(env, target):
            return
        if not target and self.analyse_as_unbound_cmethod(env):
            return
        self.analyse_as_ordinary_attribute(env, target)
3622

William Stein's avatar
William Stein committed
3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
    def analyse_as_cimported_attribute(self, env, target):
        # Try to interpret this as a reference to an imported
        # C const, type, var or function. If successful, mutates
        # this node into a NameNode and returns 1, otherwise
        # returns 0.
        module_scope = self.obj.analyse_as_module(env)
        if module_scope:
            entry = module_scope.lookup_here(self.attribute)
            if entry and (
                entry.is_cglobal or entry.is_cfunction
                or entry.is_type or entry.is_const):
                    self.mutate_into_name_node(env, entry, target)
                    return 1
        return 0
3637

William Stein's avatar
William Stein committed
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
    def analyse_as_unbound_cmethod(self, env):
        # Try to interpret this as a reference to an unbound
        # C method of an extension type. If successful, mutates
        # this node into a NameNode and returns 1, otherwise
        # returns 0.
        type = self.obj.analyse_as_extension_type(env)
        if type:
            entry = type.scope.lookup_here(self.attribute)
            if entry and entry.is_cmethod:
                # Create a temporary entry describing the C method
                # as an ordinary function.
                ubcm_entry = Symtab.Entry(entry.name,
                    "%s->%s" % (type.vtabptr_cname, entry.cname),
                    entry.type)
                ubcm_entry.is_cfunction = 1
                ubcm_entry.func_cname = entry.func_cname
3654
                ubcm_entry.is_unbound_cmethod = 1
William Stein's avatar
William Stein committed
3655 3656 3657
                self.mutate_into_name_node(env, ubcm_entry, None)
                return 1
        return 0
3658

3659 3660 3661
    def analyse_as_type(self, env):
        module_scope = self.obj.analyse_as_module(env)
        if module_scope:
3662
            return module_scope.lookup_type(self.attribute)
Robert Bradshaw's avatar
Robert Bradshaw committed
3663 3664
        if not isinstance(self.obj, (UnicodeNode, StringNode, BytesNode)):
            base_type = self.obj.analyse_as_type(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
3665
            if base_type and hasattr(base_type, 'scope') and base_type.scope is not None:
Robert Bradshaw's avatar
Robert Bradshaw committed
3666
                return base_type.scope.lookup_type(self.attribute)
3667
        return None
3668

William Stein's avatar
William Stein committed
3669 3670 3671 3672 3673 3674 3675 3676 3677
    def analyse_as_extension_type(self, env):
        # Try to interpret this as a reference to an extension type
        # in a cimported module. Returns the extension type, or None.
        module_scope = self.obj.analyse_as_module(env)
        if module_scope:
            entry = module_scope.lookup_here(self.attribute)
            if entry and entry.is_type and entry.type.is_extension_type:
                return entry.type
        return None
3678

William Stein's avatar
William Stein committed
3679 3680 3681 3682 3683 3684 3685 3686 3687
    def analyse_as_module(self, env):
        # Try to interpret this as a reference to a cimported module
        # in another cimported module. Returns the module scope, or None.
        module_scope = self.obj.analyse_as_module(env)
        if module_scope:
            entry = module_scope.lookup_here(self.attribute)
            if entry and entry.as_module:
                return entry.as_module
        return None
3688

William Stein's avatar
William Stein committed
3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699
    def mutate_into_name_node(self, env, entry, target):
        # Mutate this node into a NameNode and complete the
        # analyse_types phase.
        self.__class__ = NameNode
        self.name = self.attribute
        self.entry = entry
        del self.obj
        del self.attribute
        if target:
            NameNode.analyse_target_types(self, env)
        else:
3700
            NameNode.analyse_rvalue_entry(self, env)
3701

William Stein's avatar
William Stein committed
3702 3703 3704 3705
    def analyse_as_ordinary_attribute(self, env, target):
        self.obj.analyse_types(env)
        self.analyse_attribute(env)
        if self.entry and self.entry.is_cmethod and not self.is_called:
3706 3707
#            error(self.pos, "C method can only be called")
            pass
3708 3709
        ## Reference to C array turns into pointer to first element.
        #while self.type.is_array:
Robert Bradshaw's avatar
Robert Bradshaw committed
3710
        #    self.type = self.type.element_ptr_type()
William Stein's avatar
William Stein committed
3711 3712 3713 3714
        if self.is_py_attr:
            if not target:
                self.is_temp = 1
                self.result_ctype = py_object_type
3715 3716 3717
        elif target and self.obj.type.is_builtin_type:
            error(self.pos, "Assignment to an immutable object field")

Robert Bradshaw's avatar
Robert Bradshaw committed
3718
    def analyse_attribute(self, env, obj_type = None):
William Stein's avatar
William Stein committed
3719 3720 3721
        # Look up attribute and set self.type and self.member.
        self.is_py_attr = 0
        self.member = self.attribute
Robert Bradshaw's avatar
Robert Bradshaw committed
3722 3723 3724 3725 3726 3727 3728
        if obj_type is None:
            if self.obj.type.is_string:
                self.obj = self.obj.coerce_to_pyobject(env)
            obj_type = self.obj.type
        else:
            if obj_type.is_string:
                obj_type = py_object_type
3729
        if obj_type.is_ptr or obj_type.is_array:
William Stein's avatar
William Stein committed
3730 3731
            obj_type = obj_type.base_type
            self.op = "->"
3732
        elif obj_type.is_extension_type or obj_type.is_builtin_type:
William Stein's avatar
William Stein committed
3733 3734 3735 3736 3737 3738 3739
            self.op = "->"
        else:
            self.op = "."
        if obj_type.has_attributes:
            entry = None
            if obj_type.attributes_known():
                entry = obj_type.scope.lookup_here(self.attribute)
Robert Bradshaw's avatar
Robert Bradshaw committed
3740 3741
                if entry and entry.is_member:
                    entry = None
William Stein's avatar
William Stein committed
3742
            else:
3743 3744
                error(self.pos,
                    "Cannot select attribute of incomplete type '%s'"
William Stein's avatar
William Stein committed
3745
                    % obj_type)
Robert Bradshaw's avatar
Robert Bradshaw committed
3746 3747
                self.type = PyrexTypes.error_type
                return
William Stein's avatar
William Stein committed
3748 3749
            self.entry = entry
            if entry:
3750 3751
                if obj_type.is_extension_type and entry.name == "__weakref__":
                    error(self.pos, "Illegal use of special attribute __weakref__")
3752 3753
                # methods need the normal attribute lookup
                # because they do not have struct entries
3754 3755 3756 3757
                if entry.is_variable or entry.is_cmethod:
                    self.type = entry.type
                    self.member = entry.cname
                    return
William Stein's avatar
William Stein committed
3758 3759 3760 3761 3762
                else:
                    # If it's not a variable or C method, it must be a Python
                    # method of an extension type, so we treat it like a Python
                    # attribute.
                    pass
3763
        # If we get here, the base object is not a struct/union/extension
William Stein's avatar
William Stein committed
3764 3765 3766
        # type, or it is an extension type and the attribute is either not
        # declared or is declared as a Python method. Treat it as a Python
        # attribute reference.
Robert Bradshaw's avatar
Robert Bradshaw committed
3767
        self.analyse_as_python_attribute(env, obj_type)
Stefan Behnel's avatar
Stefan Behnel committed
3768

Robert Bradshaw's avatar
Robert Bradshaw committed
3769 3770 3771
    def analyse_as_python_attribute(self, env, obj_type = None):
        if obj_type is None:
            obj_type = self.obj.type
3772
        self.member = self.attribute
3773 3774
        self.type = py_object_type
        self.is_py_attr = 1
3775
        if not obj_type.is_pyobject and not obj_type.is_error:
3776
            if obj_type.can_coerce_to_pyobject(env):
3777 3778 3779 3780 3781
                self.obj = self.obj.coerce_to_pyobject(env)
            else:
                error(self.pos,
                      "Object of type '%s' has no attribute '%s'" %
                      (obj_type, self.attribute))
3782

3783
    def nogil_check(self, env):
3784
        if self.is_py_attr:
3785
            self.gil_error()
3786

3787 3788
    gil_message = "Accessing Python attribute"

William Stein's avatar
William Stein committed
3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
    def is_simple(self):
        if self.obj:
            return self.result_in_temp() or self.obj.is_simple()
        else:
            return NameNode.is_simple(self)

    def is_lvalue(self):
        if self.obj:
            return 1
        else:
            return NameNode.is_lvalue(self)
3800

William Stein's avatar
William Stein committed
3801 3802 3803 3804 3805
    def is_ephemeral(self):
        if self.obj:
            return self.obj.is_ephemeral()
        else:
            return NameNode.is_ephemeral(self)
3806

William Stein's avatar
William Stein committed
3807 3808
    def calculate_result_code(self):
        #print "AttributeNode.calculate_result_code:", self.member ###
3809
        #print "...obj node =", self.obj, "code", self.obj.result() ###
William Stein's avatar
William Stein committed
3810 3811 3812 3813 3814
        #print "...obj type", self.obj.type, "ctype", self.obj.ctype() ###
        obj = self.obj
        obj_code = obj.result_as(obj.type)
        #print "...obj_code =", obj_code ###
        if self.entry and self.entry.is_cmethod:
Robert Bradshaw's avatar
Robert Bradshaw committed
3815 3816
            if obj.type.is_extension_type:
                return "((struct %s *)%s%s%s)->%s" % (
3817
                    obj.type.vtabstruct_cname, obj_code, self.op,
Robert Bradshaw's avatar
Robert Bradshaw committed
3818 3819 3820
                    obj.type.vtabslot_cname, self.member)
            else:
                return self.member
3821
        elif obj.type.is_complex:
3822
            return "__Pyx_C%s(%s)" % (self.member.upper(), obj_code)
William Stein's avatar
William Stein committed
3823
        else:
3824 3825 3826
            if obj.type.is_builtin_type and self.entry and self.entry.is_variable:
                # accessing a field of a builtin type, need to cast better than result_as() does
                obj_code = obj.type.cast_code(obj.result(), to_object_struct = True)
William Stein's avatar
William Stein committed
3827
            return "%s%s%s" % (obj_code, self.op, self.member)
3828

William Stein's avatar
William Stein committed
3829 3830
    def generate_result_code(self, code):
        if self.is_py_attr:
3831 3832
            code.putln(
                '%s = PyObject_GetAttr(%s, %s); %s' % (
3833
                    self.result(),
3834
                    self.obj.py_result(),
3835
                    code.intern_identifier(self.attribute),
3836
                    code.error_goto_if_null(self.result(), self.pos)))
3837
            code.put_gotref(self.py_result())
3838
        else:
3839 3840 3841 3842 3843 3844 3845 3846
            # result_code contains what is needed, but we may need to insert
            # a check and raise an exception
            if self.obj.type.is_extension_type:
                if self.needs_none_check and code.globalstate.directives['nonecheck']:
                    self.put_nonecheck(code)
            elif self.entry and self.entry.is_cmethod and self.entry.utility_code:
                # C method implemented as function call with utility code
                code.globalstate.use_utility_code(self.entry.utility_code)
3847

William Stein's avatar
William Stein committed
3848 3849 3850
    def generate_assignment_code(self, rhs, code):
        self.obj.generate_evaluation_code(code)
        if self.is_py_attr:
3851
            code.put_error_if_neg(self.pos,
3852 3853
                'PyObject_SetAttr(%s, %s, %s)' % (
                    self.obj.py_result(),
3854
                    code.intern_identifier(self.attribute),
3855
                    rhs.py_result()))
William Stein's avatar
William Stein committed
3856
            rhs.generate_disposal_code(code)
3857
            rhs.free_temps(code)
3858 3859 3860 3861 3862
        elif self.obj.type.is_complex:
            code.putln("__Pyx_SET_C%s(%s, %s);" % (
                self.member.upper(),
                self.obj.result_as(self.obj.type),
                rhs.result_as(self.ctype())))
William Stein's avatar
William Stein committed
3863
        else:
3864 3865 3866 3867 3868
            if (self.obj.type.is_extension_type
                  and self.needs_none_check
                  and code.globalstate.directives['nonecheck']):
                self.put_nonecheck(code)

3869
            select_code = self.result()
3870
            if self.type.is_pyobject and self.use_managed_ref:
William Stein's avatar
William Stein committed
3871
                rhs.make_owned_reference(code)
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
3872
                code.put_giveref(rhs.py_result())
3873
                code.put_gotref(select_code)
William Stein's avatar
William Stein committed
3874 3875 3876 3877
                code.put_decref(select_code, self.ctype())
            code.putln(
                "%s = %s;" % (
                    select_code,
3878
                    rhs.result_as(self.ctype())))
3879
                    #rhs.result()))
William Stein's avatar
William Stein committed
3880
            rhs.generate_post_assignment_code(code)
3881
            rhs.free_temps(code)
William Stein's avatar
William Stein committed
3882
        self.obj.generate_disposal_code(code)
3883
        self.obj.free_temps(code)
3884

William Stein's avatar
William Stein committed
3885 3886
    def generate_deletion_code(self, code):
        self.obj.generate_evaluation_code(code)
3887
        if self.is_py_attr or (isinstance(self.entry.scope, Symtab.PropertyScope)
3888
                               and u'__del__' in self.entry.scope.entries):
3889 3890 3891
            code.put_error_if_neg(self.pos,
                'PyObject_DelAttr(%s, %s)' % (
                    self.obj.py_result(),
3892
                    code.intern_identifier(self.attribute)))
William Stein's avatar
William Stein committed
3893 3894 3895
        else:
            error(self.pos, "Cannot delete C attribute of extension type")
        self.obj.generate_disposal_code(code)
3896
        self.obj.free_temps(code)
3897

3898 3899 3900 3901 3902
    def annotate(self, code):
        if self.is_py_attr:
            code.annotate(self.pos, AnnotationItem('py_attr', 'python attribute', size=len(self.attribute)))
        else:
            code.annotate(self.pos, AnnotationItem('c_attr', 'c attribute', size=len(self.attribute)))
William Stein's avatar
William Stein committed
3903

3904 3905 3906 3907 3908 3909 3910
    def put_nonecheck(self, code):
        code.globalstate.use_utility_code(raise_noneattr_error_utility_code)
        code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.obj.result_as(PyrexTypes.py_object_type))
        code.putln("__Pyx_RaiseNoneAttributeError(\"%s\");" % self.attribute)
        code.putln(code.error_goto(self.pos))
        code.putln("}")

3911

William Stein's avatar
William Stein committed
3912 3913 3914 3915 3916 3917
#-------------------------------------------------------------------
#
#  Constructor nodes
#
#-------------------------------------------------------------------

3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932
class StarredTargetNode(ExprNode):
    #  A starred expression like "*a"
    #
    #  This is only allowed in sequence assignment targets such as
    #
    #      a, *b = (1,2,3,4)    =>     a = 1 ; b = [2,3,4]
    #
    #  and will be removed during type analysis (or generate an error
    #  if it's found at unexpected places).
    #
    #  target          ExprNode

    subexprs = ['target']
    is_starred = 1
    type = py_object_type
Robert Bradshaw's avatar
Robert Bradshaw committed
3933
    is_temp = 1
3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961

    def __init__(self, pos, target):
        self.pos = pos
        self.target = target

    def analyse_declarations(self, env):
        error(self.pos, "can use starred expression only as assignment target")
        self.target.analyse_declarations(env)

    def analyse_types(self, env):
        error(self.pos, "can use starred expression only as assignment target")
        self.target.analyse_types(env)
        self.type = self.target.type

    def analyse_target_declaration(self, env):
        self.target.analyse_target_declaration(env)

    def analyse_target_types(self, env):
        self.target.analyse_target_types(env)
        self.type = self.target.type

    def calculate_result_code(self):
        return ""

    def generate_result_code(self, code):
        pass


3962
class SequenceNode(ExprNode):
William Stein's avatar
William Stein committed
3963 3964 3965 3966 3967 3968
    #  Base class for list and tuple constructor nodes.
    #  Contains common code for performing sequence unpacking.
    #
    #  args                    [ExprNode]
    #  unpacked_items          [ExprNode] or None
    #  coerced_unpacked_items  [ExprNode] or None
3969

William Stein's avatar
William Stein committed
3970
    subexprs = ['args']
3971

William Stein's avatar
William Stein committed
3972 3973
    is_sequence_constructor = 1
    unpacked_items = None
3974

3975 3976 3977
    def compile_time_value_list(self, denv):
        return [arg.compile_time_value(denv) for arg in self.args]

3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
    def replace_starred_target_node(self):
        # replace a starred node in the targets by the contained expression
        self.starred_assignment = False
        args = []
        for arg in self.args:
            if arg.is_starred:
                if self.starred_assignment:
                    error(arg.pos, "more than 1 starred expression in assignment")
                self.starred_assignment = True
                arg = arg.target
                arg.is_starred = True
            args.append(arg)
        self.args = args

William Stein's avatar
William Stein committed
3992
    def analyse_target_declaration(self, env):
3993
        self.replace_starred_target_node()
William Stein's avatar
William Stein committed
3994 3995 3996
        for arg in self.args:
            arg.analyse_target_declaration(env)

3997
    def analyse_types(self, env, skip_children=False):
William Stein's avatar
William Stein committed
3998 3999
        for i in range(len(self.args)):
            arg = self.args[i]
4000
            if not skip_children: arg.analyse_types(env)
William Stein's avatar
William Stein committed
4001 4002
            self.args[i] = arg.coerce_to_pyobject(env)
        self.is_temp = 1
Stefan Behnel's avatar
Stefan Behnel committed
4003
        # not setting self.type here, subtypes do this
4004

4005 4006 4007
    def may_be_none(self):
        return False

William Stein's avatar
William Stein committed
4008
    def analyse_target_types(self, env):
4009
        self.unpacked_items = []
William Stein's avatar
William Stein committed
4010
        self.coerced_unpacked_items = []
4011
        self.any_coerced_items = False
William Stein's avatar
William Stein committed
4012 4013
        for arg in self.args:
            arg.analyse_target_types(env)
4014 4015 4016 4017 4018 4019
            if arg.is_starred:
                if not arg.type.assignable_from(Builtin.list_type):
                    error(arg.pos,
                          "starred target must have Python object (list) type")
                if arg.type is py_object_type:
                    arg.type = Builtin.list_type
William Stein's avatar
William Stein committed
4020 4021
            unpacked_item = PyTempNode(self.pos, env)
            coerced_unpacked_item = unpacked_item.coerce_to(arg.type, env)
4022 4023
            if unpacked_item is not coerced_unpacked_item:
                self.any_coerced_items = True
William Stein's avatar
William Stein committed
4024 4025 4026
            self.unpacked_items.append(unpacked_item)
            self.coerced_unpacked_items.append(coerced_unpacked_item)
        self.type = py_object_type
4027

William Stein's avatar
William Stein committed
4028 4029
    def generate_result_code(self, code):
        self.generate_operation_code(code)
4030

William Stein's avatar
William Stein committed
4031
    def generate_assignment_code(self, rhs, code):
4032 4033 4034
        if self.starred_assignment:
            self.generate_starred_assignment_code(rhs, code)
        else:
4035
            self.generate_parallel_assignment_code(rhs, code)
4036 4037 4038 4039 4040

        for item in self.unpacked_items:
            item.release(code)
        rhs.free_temps(code)

4041 4042 4043 4044 4045
    _func_iternext_type = PyrexTypes.CPtrType(PyrexTypes.CFuncType(
        PyrexTypes.py_object_type, [
            PyrexTypes.CFuncTypeArg("it", PyrexTypes.py_object_type, None),
            ]))

4046
    def generate_parallel_assignment_code(self, rhs, code):
4047 4048 4049
        # Need to work around the fact that generate_evaluation_code
        # allocates the temps in a rather hacky way -- the assignment
        # is evaluated twice, within each if-block.
4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
        special_unpack = (rhs.type is py_object_type
                          or rhs.type in (tuple_type, list_type)
                          or not rhs.type.is_builtin_type)
        if special_unpack:
            tuple_check = 'likely(PyTuple_CheckExact(%s))' % rhs.py_result()
            list_check  = 'PyList_CheckExact(%s)' % rhs.py_result()
            if rhs.type is list_type:
                sequence_types = ['List']
                sequence_type_test = list_check
            elif rhs.type is tuple_type:
                sequence_types = ['Tuple']
                sequence_type_test = tuple_check
            else:
                sequence_types = ['Tuple', 'List']
                sequence_type_test = "(%s) || (%s)" % (tuple_check, list_check)
            code.putln("if (%s) {" % sequence_type_test)
            code.putln("PyObject* sequence = %s;" % rhs.py_result())
            for item in self.unpacked_items:
                item.allocate(code)
            if len(sequence_types) == 2:
                code.putln("if (likely(Py%s_CheckExact(sequence))) {" % sequence_types[0])
            self.generate_special_parallel_unpacking_code(code, sequence_types[0])
            if len(sequence_types) == 2:
                code.putln("} else {")
                self.generate_special_parallel_unpacking_code(code, sequence_types[1])
                code.putln("}")
            for item in self.unpacked_items:
                code.put_incref(item.result(), item.ctype())
            rhs.generate_disposal_code(code)
            code.putln("} else {")
Robert Bradshaw's avatar
Robert Bradshaw committed
4080

4081
        if special_unpack and rhs.type is tuple_type:
4082 4083 4084 4085
            code.globalstate.use_utility_code(tuple_unpacking_error_code)
            code.putln("__Pyx_UnpackTupleError(%s, %s);" % (
                        rhs.py_result(), len(self.args)))
            code.putln(code.error_goto(self.pos))
4086
        else:
4087 4088 4089
            self.generate_generic_parallel_unpacking_code(code, rhs)
        if special_unpack:
            code.putln("}")
4090

4091 4092 4093 4094 4095
        for value_node in self.coerced_unpacked_items:
            value_node.generate_evaluation_code(code)
        for i in range(len(self.args)):
            self.args[i].generate_assignment_code(
                self.coerced_unpacked_items[i], code)
4096

4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
    def generate_special_parallel_unpacking_code(self, code, sequence_type):
        code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
        code.globalstate.use_utility_code(raise_too_many_values_to_unpack)
        code.putln("if (unlikely(Py%s_GET_SIZE(sequence) != %d)) {" % (
            sequence_type, len(self.args)))
        code.putln("if (Py%s_GET_SIZE(sequence) > %d) __Pyx_RaiseTooManyValuesError(%d);" % (
            sequence_type, len(self.args), len(self.args)))
        code.putln("else __Pyx_RaiseNeedMoreValuesError(Py%s_GET_SIZE(sequence));" % sequence_type)
        code.putln(code.error_goto(self.pos))
        code.putln("}")
        for i, item in enumerate(self.unpacked_items):
            code.putln("%s = Py%s_GET_ITEM(sequence, %d); " % (item.result(), sequence_type, i))
4109

4110 4111 4112 4113
    def generate_generic_parallel_unpacking_code(self, code, rhs):
        code.globalstate.use_utility_code(iternext_unpacking_end_utility_code)
        code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
        code.putln("Py_ssize_t index = -1;")
4114

4115 4116 4117 4118 4119 4120 4121 4122
        iterator_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
        code.putln(
            "%s = PyObject_GetIter(%s); %s" % (
                iterator_temp,
                rhs.py_result(),
                code.error_goto_if_null(iterator_temp, self.pos)))
        code.put_gotref(iterator_temp)
        rhs.generate_disposal_code(code)
4123

4124 4125 4126
        iternext_func = code.funcstate.allocate_temp(self._func_iternext_type, manage_ref=False)
        code.putln("%s = Py_TYPE(%s)->tp_iternext;" % (
            iternext_func, iterator_temp))
William Stein's avatar
William Stein committed
4127

4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156
        unpacking_error_label = code.new_label('unpacking_failed')
        code.use_label(unpacking_error_label)
        unpack_code = "%s(%s)" % (iternext_func, iterator_temp)
        for i in range(len(self.args)):
            item = self.unpacked_items[i]
            code.putln(
                "index = %d; %s = %s; if (unlikely(!%s)) goto %s;" % (
                    i,
                    item.result(),
                    typecast(item.ctype(), py_object_type, unpack_code),
                    item.result(),
                    unpacking_error_label))
            code.put_gotref(item.py_result())
        code.put_error_if_neg(self.pos, "__Pyx_IternextUnpackEndCheck(%s(%s), %d)" % (
            iternext_func,
            iterator_temp,
            len(self.args)))
        code.put_decref_clear(iterator_temp, py_object_type)
        code.funcstate.release_temp(iterator_temp)
        code.funcstate.release_temp(iternext_func)
        unpacking_done_label = code.new_label('unpacking_done')
        code.put_goto(unpacking_done_label)

        code.put_label(unpacking_error_label)
        code.put_decref_clear(iterator_temp, py_object_type)
        code.putln("if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear();")
        code.putln("if (!PyErr_Occurred()) __Pyx_RaiseNeedMoreValuesError(index);")
        code.putln(code.error_goto(self.pos))
        code.put_label(unpacking_done_label)
4157 4158 4159 4160 4161 4162 4163 4164 4165

    def generate_starred_assignment_code(self, rhs, code):
        for i, arg in enumerate(self.args):
            if arg.is_starred:
                starred_target = self.unpacked_items[i]
                fixed_args_left  = self.args[:i]
                fixed_args_right = self.args[i+1:]
                break

4166
        iterator_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
4167 4168
        code.putln(
            "%s = PyObject_GetIter(%s); %s" % (
4169
                iterator_temp,
4170
                rhs.py_result(),
4171 4172
                code.error_goto_if_null(iterator_temp, self.pos)))
        code.put_gotref(iterator_temp)
4173 4174
        rhs.generate_disposal_code(code)

4175
        for item in self.unpacked_items:
4176
            item.allocate(code)
4177
        code.globalstate.use_utility_code(unpacking_utility_code)
4178 4179 4180
        for i in range(len(fixed_args_left)):
            item = self.unpacked_items[i]
            unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
4181
                iterator_temp, i)
4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
            code.putln(
                "%s = %s; %s" % (
                    item.result(),
                    typecast(item.ctype(), py_object_type, unpack_code),
                    code.error_goto_if_null(item.result(), self.pos)))
            code.put_gotref(item.py_result())
            value_node = self.coerced_unpacked_items[i]
            value_node.generate_evaluation_code(code)

        target_list = starred_target.result()
        code.putln("%s = PySequence_List(%s); %s" % (
4193
            target_list, iterator_temp,
4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
            code.error_goto_if_null(target_list, self.pos)))
        code.put_gotref(target_list)
        if fixed_args_right:
            code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
            unpacked_right_args = self.unpacked_items[-len(fixed_args_right):]
            code.putln("if (unlikely(PyList_GET_SIZE(%s) < %d)) {" % (
                (target_list, len(unpacked_right_args))))
            code.put("__Pyx_RaiseNeedMoreValuesError(%d+PyList_GET_SIZE(%s)); %s" % (
                     len(fixed_args_left), target_list,
                     code.error_goto(self.pos)))
            code.putln('}')
            for i, (arg, coerced_arg) in enumerate(zip(unpacked_right_args[::-1],
                                                       self.coerced_unpacked_items[::-1])):
                code.putln(
                    "%s = PyList_GET_ITEM(%s, PyList_GET_SIZE(%s)-1); " % (
                        arg.py_result(),
                        target_list, target_list))
                # resize the list the hard way
4212
                code.putln("((PyVarObject*)%s)->ob_size--;" % target_list)
4213 4214 4215
                code.put_gotref(arg.py_result())
                coerced_arg.generate_evaluation_code(code)

4216 4217
        code.put_decref_clear(iterator_temp, py_object_type)
        code.funcstate.release_temp(iterator_temp)
4218 4219 4220 4221 4222

        for i in range(len(self.args)):
            self.args[i].generate_assignment_code(
                self.coerced_unpacked_items[i], code)

4223 4224 4225 4226 4227 4228 4229 4230
    def annotate(self, code):
        for arg in self.args:
            arg.annotate(code)
        if self.unpacked_items:
            for arg in self.unpacked_items:
                arg.annotate(code)
            for arg in self.coerced_unpacked_items:
                arg.annotate(code)
William Stein's avatar
William Stein committed
4231 4232 4233 4234


class TupleNode(SequenceNode):
    #  Tuple constructor.
4235

4236
    type = tuple_type
4237 4238 4239

    gil_message = "Constructing Python tuple"

4240
    def analyse_types(self, env, skip_children=False):
Robert Bradshaw's avatar
Robert Bradshaw committed
4241 4242
        if len(self.args) == 0:
            self.is_temp = 0
4243
            self.is_literal = 1
Robert Bradshaw's avatar
Robert Bradshaw committed
4244
        else:
4245
            SequenceNode.analyse_types(self, env, skip_children)
4246 4247 4248 4249 4250 4251
            for child in self.args:
                if not child.is_literal:
                    break
            else:
                self.is_temp = 0
                self.is_literal = 1
4252

Stefan Behnel's avatar
Stefan Behnel committed
4253 4254 4255 4256
    def is_simple(self):
        # either temp or constant => always simple
        return True

4257 4258 4259 4260
    def nonlocally_immutable(self):
        # either temp or constant => always safe
        return True

Robert Bradshaw's avatar
Robert Bradshaw committed
4261 4262
    def calculate_result_code(self):
        if len(self.args) > 0:
4263
            return self.result_code
Robert Bradshaw's avatar
Robert Bradshaw committed
4264 4265
        else:
            return Naming.empty_tuple
William Stein's avatar
William Stein committed
4266

4267 4268 4269 4270
    def calculate_constant_result(self):
        self.constant_result = tuple([
                arg.constant_result for arg in self.args])

4271 4272 4273 4274 4275 4276
    def compile_time_value(self, denv):
        values = self.compile_time_value_list(denv)
        try:
            return tuple(values)
        except Exception, e:
            self.compile_time_value_error(e)
4277

William Stein's avatar
William Stein committed
4278
    def generate_operation_code(self, code):
Robert Bradshaw's avatar
Robert Bradshaw committed
4279 4280 4281
        if len(self.args) == 0:
            # result_code is Naming.empty_tuple
            return
4282 4283 4284
        if self.is_literal:
            # non-empty cached tuple => result is global constant,
            # creation code goes into separate code writer
4285
            self.result_code = code.get_py_const(py_object_type, 'tuple_', cleanup_level=2)
4286 4287 4288
            code = code.get_cached_constants_writer()
            code.mark_pos(self.pos)

William Stein's avatar
William Stein committed
4289
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
4290
            "%s = PyTuple_New(%s); %s" % (
4291
                self.result(),
William Stein's avatar
William Stein committed
4292
                len(self.args),
4293
                code.error_goto_if_null(self.result(), self.pos)))
4294
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
4295 4296 4297
        for i in range(len(self.args)):
            arg = self.args[i]
            if not arg.result_in_temp():
4298
                code.put_incref(arg.result(), arg.ctype())
William Stein's avatar
William Stein committed
4299 4300
            code.putln(
                "PyTuple_SET_ITEM(%s, %s, %s);" % (
4301
                    self.result(),
William Stein's avatar
William Stein committed
4302 4303
                    i,
                    arg.py_result()))
4304
            code.put_giveref(arg.py_result())
4305 4306
        if self.is_literal:
            code.put_giveref(self.py_result())
4307

William Stein's avatar
William Stein committed
4308 4309 4310 4311 4312
    def generate_subexpr_disposal_code(self, code):
        # We call generate_post_assignment_code here instead
        # of generate_disposal_code, because values were stored
        # in the tuple using a reference-stealing operation.
        for arg in self.args:
4313 4314 4315
            arg.generate_post_assignment_code(code)
            # Should NOT call free_temps -- this is invoked by the default
            # generate_evaluation_code which will do that.
William Stein's avatar
William Stein committed
4316 4317 4318 4319


class ListNode(SequenceNode):
    #  List constructor.
4320

4321 4322
    # obj_conversion_errors    [PyrexError]   used internally
    # orignial_args            [ExprNode]     used internally
4323

4324
    obj_conversion_errors = []
Stefan Behnel's avatar
Stefan Behnel committed
4325
    type = list_type
4326

4327
    gil_message = "Constructing Python list"
4328

Robert Bradshaw's avatar
Robert Bradshaw committed
4329
    def type_dependencies(self, env):
4330
        return ()
4331

4332 4333 4334
    def infer_type(self, env):
        # TOOD: Infer non-object list arrays.
        return list_type
4335

4336
    def analyse_expressions(self, env):
4337
        SequenceNode.analyse_expressions(self, env)
4338 4339
        self.coerce_to_pyobject(env)

Robert Bradshaw's avatar
Robert Bradshaw committed
4340
    def analyse_types(self, env):
4341 4342 4343 4344 4345
        hold_errors()
        self.original_args = list(self.args)
        SequenceNode.analyse_types(self, env)
        self.obj_conversion_errors = held_errors()
        release_errors(ignore=True)
4346

Robert Bradshaw's avatar
Robert Bradshaw committed
4347 4348
    def coerce_to(self, dst_type, env):
        if dst_type.is_pyobject:
4349 4350 4351
            for err in self.obj_conversion_errors:
                report_error(err)
            self.obj_conversion_errors = []
Robert Bradshaw's avatar
Robert Bradshaw committed
4352 4353
            if not self.type.subtype_of(dst_type):
                error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
4354
        elif dst_type.is_ptr and dst_type.base_type is not PyrexTypes.c_void_type:
Robert Bradshaw's avatar
Robert Bradshaw committed
4355
            base_type = dst_type.base_type
Robert Bradshaw's avatar
Robert Bradshaw committed
4356
            self.type = PyrexTypes.CArrayType(base_type, len(self.args))
4357
            for i in range(len(self.original_args)):
Robert Bradshaw's avatar
Robert Bradshaw committed
4358
                arg = self.args[i]
4359 4360
                if isinstance(arg, CoerceToPyTypeNode):
                    arg = arg.arg
Robert Bradshaw's avatar
Robert Bradshaw committed
4361
                self.args[i] = arg.coerce_to(base_type, env)
Robert Bradshaw's avatar
Robert Bradshaw committed
4362 4363 4364 4365 4366 4367
        elif dst_type.is_struct:
            if len(self.args) > len(dst_type.scope.var_entries):
                error(self.pos, "Too may members for '%s'" % dst_type)
            else:
                if len(self.args) < len(dst_type.scope.var_entries):
                    warning(self.pos, "Too few members for '%s'" % dst_type, 1)
4368 4369 4370
                for i, (arg, member) in enumerate(zip(self.original_args, dst_type.scope.var_entries)):
                    if isinstance(arg, CoerceToPyTypeNode):
                        arg = arg.arg
Robert Bradshaw's avatar
Robert Bradshaw committed
4371 4372
                    self.args[i] = arg.coerce_to(member.type, env)
            self.type = dst_type
Robert Bradshaw's avatar
Robert Bradshaw committed
4373 4374 4375 4376
        else:
            self.type = error_type
            error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
        return self
4377

Robert Bradshaw's avatar
Robert Bradshaw committed
4378 4379
    def release_temp(self, env):
        if self.type.is_array:
4380 4381
            # To be valid C++, we must allocate the memory on the stack
            # manually and be sure not to reuse it for something else.
Robert Bradshaw's avatar
Robert Bradshaw committed
4382 4383 4384
            pass
        else:
            SequenceNode.release_temp(self, env)
Robert Bradshaw's avatar
Robert Bradshaw committed
4385

4386 4387 4388 4389
    def calculate_constant_result(self):
        self.constant_result = [
            arg.constant_result for arg in self.args]

4390 4391 4392
    def compile_time_value(self, denv):
        return self.compile_time_value_list(denv)

William Stein's avatar
William Stein committed
4393
    def generate_operation_code(self, code):
Robert Bradshaw's avatar
Robert Bradshaw committed
4394
        if self.type.is_pyobject:
4395 4396
            for err in self.obj_conversion_errors:
                report_error(err)
Robert Bradshaw's avatar
Robert Bradshaw committed
4397
            code.putln("%s = PyList_New(%s); %s" %
4398
                (self.result(),
Robert Bradshaw's avatar
Robert Bradshaw committed
4399 4400
                len(self.args),
                code.error_goto_if_null(self.result(), self.pos)))
4401
            code.put_gotref(self.py_result())
Robert Bradshaw's avatar
Robert Bradshaw committed
4402 4403 4404 4405 4406 4407 4408 4409 4410
            for i in range(len(self.args)):
                arg = self.args[i]
                #if not arg.is_temp:
                if not arg.result_in_temp():
                    code.put_incref(arg.result(), arg.ctype())
                code.putln("PyList_SET_ITEM(%s, %s, %s);" %
                    (self.result(),
                    i,
                    arg.py_result()))
4411
                code.put_giveref(arg.py_result())
Robert Bradshaw's avatar
Robert Bradshaw committed
4412 4413 4414 4415 4416 4417
        elif self.type.is_array:
            for i, arg in enumerate(self.args):
                code.putln("%s[%s] = %s;" % (
                                self.result(),
                                i,
                                arg.result()))
Robert Bradshaw's avatar
Robert Bradshaw committed
4418
        elif self.type.is_struct:
Robert Bradshaw's avatar
Robert Bradshaw committed
4419 4420 4421 4422 4423
            for arg, member in zip(self.args, self.type.scope.var_entries):
                code.putln("%s.%s = %s;" % (
                        self.result(),
                        member.cname,
                        arg.result()))
4424 4425
        else:
            raise InternalError("List type never specified")
4426

William Stein's avatar
William Stein committed
4427 4428 4429 4430 4431
    def generate_subexpr_disposal_code(self, code):
        # We call generate_post_assignment_code here instead
        # of generate_disposal_code, because values were stored
        # in the list using a reference-stealing operation.
        for arg in self.args:
4432 4433 4434
            arg.generate_post_assignment_code(code)
            # Should NOT call free_temps -- this is invoked by the default
            # generate_evaluation_code which will do that.
William Stein's avatar
William Stein committed
4435

Robert Bradshaw's avatar
Robert Bradshaw committed
4436

4437 4438 4439 4440 4441 4442 4443 4444 4445
class ScopedExprNode(ExprNode):
    # Abstract base class for ExprNodes that have their own local
    # scope, such as generator expressions.
    #
    # expr_scope    Scope  the inner scope of the expression

    subexprs = []
    expr_scope = None

4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464
    # does this node really have a local scope, e.g. does it leak loop
    # variables or not?  non-leaking Py3 behaviour is default, except
    # for list comprehensions where the behaviour differs in Py2 and
    # Py3 (set in Parsing.py based on parser context)
    has_local_scope = True

    def init_scope(self, outer_scope, expr_scope=None):
        if expr_scope is not None:
            self.expr_scope = expr_scope
        elif self.has_local_scope:
            self.expr_scope = Symtab.GeneratorExpressionScope(outer_scope)
        else:
            self.expr_scope = None

    def analyse_declarations(self, env):
        self.init_scope(env)

    def analyse_scoped_declarations(self, env):
        # this is called with the expr_scope as env
4465 4466
        pass

4467 4468
    def analyse_types(self, env):
        # no recursion here, the children will be analysed separately below
4469 4470 4471 4472 4473 4474
        pass

    def analyse_scoped_expressions(self, env):
        # this is called with the expr_scope as env
        pass

4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521
    def generate_evaluation_code(self, code):
        # set up local variables and free their references on exit
        generate_inner_evaluation_code = super(ScopedExprNode, self).generate_evaluation_code
        if not self.has_local_scope or not self.expr_scope.var_entries:
            # no local variables => delegate, done
            generate_inner_evaluation_code(code)
            return

        code.putln('{ /* enter inner scope */')
        py_entries = []
        for entry in self.expr_scope.var_entries:
            if not entry.in_closure:
                code.put_var_declaration(entry)
                if entry.type.is_pyobject and entry.used:
                    py_entries.append(entry)
        if not py_entries:
            # no local Python references => no cleanup required
            generate_inner_evaluation_code(code)
            code.putln('} /* exit inner scope */')
            return

        # must free all local Python references at each exit point
        old_loop_labels = tuple(code.new_loop_labels())
        old_error_label = code.new_error_label()

        generate_inner_evaluation_code(code)

        # normal (non-error) exit
        for entry in py_entries:
            code.put_var_decref(entry)

        # error/loop body exit points
        exit_scope = code.new_label('exit_scope')
        code.put_goto(exit_scope)
        for label, old_label in ([(code.error_label, old_error_label)] +
                                 list(zip(code.get_loop_labels(), old_loop_labels))):
            if code.label_used(label):
                code.put_label(label)
                for entry in py_entries:
                    code.put_var_decref(entry)
                code.put_goto(old_label)
        code.put_label(exit_scope)
        code.putln('} /* exit inner scope */')

        code.set_loop_labels(old_loop_labels)
        code.error_label = old_error_label

4522 4523

class ComprehensionNode(ScopedExprNode):
4524
    subexprs = ["target"]
4525 4526
    child_attrs = ["loop", "append"]

4527 4528
    def infer_type(self, env):
        return self.target.infer_type(env)
4529 4530 4531

    def analyse_declarations(self, env):
        self.append.target = self # this is used in the PyList_Append of the inner loop
4532 4533
        self.init_scope(env)

4534 4535
    def analyse_scoped_declarations(self, env):
        self.loop.analyse_declarations(env)
4536

4537 4538 4539
    def analyse_types(self, env):
        self.target.analyse_expressions(env)
        self.type = self.target.type
4540 4541
        if not self.has_local_scope:
            self.loop.analyse_expressions(env)
4542

4543 4544 4545
    def analyse_scoped_expressions(self, env):
        if self.has_local_scope:
            self.loop.analyse_expressions(env)
4546

4547 4548 4549
    def may_be_none(self):
        return False

4550 4551
    def calculate_result_code(self):
        return self.target.result()
4552

4553 4554
    def generate_result_code(self, code):
        self.generate_operation_code(code)
Robert Bradshaw's avatar
Robert Bradshaw committed
4555

4556 4557 4558
    def generate_operation_code(self, code):
        self.loop.generate_execution_code(code)

4559 4560
    def annotate(self, code):
        self.loop.annotate(code)
4561 4562


4563
class ComprehensionAppendNode(Node):
4564 4565
    # Need to be careful to avoid infinite recursion:
    # target must not be in child_attrs/subexprs
4566 4567

    child_attrs = ['expr']
4568 4569

    type = PyrexTypes.c_int_type
4570

4571 4572
    def analyse_expressions(self, env):
        self.expr.analyse_expressions(env)
4573
        if not self.expr.type.is_pyobject:
Robert Bradshaw's avatar
Robert Bradshaw committed
4574
            self.expr = self.expr.coerce_to_pyobject(env)
4575

4576
    def generate_execution_code(self, code):
4577 4578 4579 4580 4581 4582 4583
        if self.target.type is list_type:
            function = "PyList_Append"
        elif self.target.type is set_type:
            function = "PySet_Add"
        else:
            raise InternalError(
                "Invalid type for comprehension node: %s" % self.target.type)
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598

        self.expr.generate_evaluation_code(code)
        code.putln(code.error_goto_if("%s(%s, (PyObject*)%s)" % (
            function,
            self.target.result(),
            self.expr.result()
            ), self.pos))
        self.expr.generate_disposal_code(code)
        self.expr.free_temps(code)

    def generate_function_definitions(self, env, code):
        self.expr.generate_function_definitions(env, code)

    def annotate(self, code):
        self.expr.annotate(code)
4599 4600

class DictComprehensionAppendNode(ComprehensionAppendNode):
4601
    child_attrs = ['key_expr', 'value_expr']
4602

4603 4604
    def analyse_expressions(self, env):
        self.key_expr.analyse_expressions(env)
4605 4606
        if not self.key_expr.type.is_pyobject:
            self.key_expr = self.key_expr.coerce_to_pyobject(env)
4607
        self.value_expr.analyse_expressions(env)
4608 4609 4610
        if not self.value_expr.type.is_pyobject:
            self.value_expr = self.value_expr.coerce_to_pyobject(env)

4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
    def generate_execution_code(self, code):
        self.key_expr.generate_evaluation_code(code)
        self.value_expr.generate_evaluation_code(code)
        code.putln(code.error_goto_if("PyDict_SetItem(%s, (PyObject*)%s, (PyObject*)%s)" % (
            self.target.result(),
            self.key_expr.result(),
            self.value_expr.result()
            ), self.pos))
        self.key_expr.generate_disposal_code(code)
        self.key_expr.free_temps(code)
        self.value_expr.generate_disposal_code(code)
        self.value_expr.free_temps(code)

    def generate_function_definitions(self, env, code):
        self.key_expr.generate_function_definitions(env, code)
        self.value_expr.generate_function_definitions(env, code)

    def annotate(self, code):
        self.key_expr.annotate(code)
        self.value_expr.annotate(code)
4631 4632


4633 4634 4635 4636 4637
class InlinedGeneratorExpressionNode(ScopedExprNode):
    # An inlined generator expression for which the result is
    # calculated inside of the loop.  This will only be created by
    # transforms when replacing builtin calls on generator
    # expressions.
4638
    #
4639 4640 4641
    # loop           ForStatNode      the for-loop, not containing any YieldExprNodes
    # result_node    ResultRefNode    the reference to the result value temp
    # orig_func      String           the name of the builtin function this node replaces
4642

4643
    child_attrs = ["loop"]
4644
    loop_analysed = False
4645 4646
    type = py_object_type

4647 4648
    def analyse_scoped_declarations(self, env):
        self.loop.analyse_declarations(env)
4649

4650 4651 4652 4653 4654 4655
    def may_be_none(self):
        return False

    def annotate(self, code):
        self.loop.annotate(code)

4656 4657
    def infer_type(self, env):
        return self.result_node.infer_type(env)
4658 4659

    def analyse_types(self, env):
4660 4661 4662
        if not self.has_local_scope:
            self.loop_analysed = True
            self.loop.analyse_expressions(env)
4663 4664 4665
        self.type = self.result_node.type
        self.is_temp = True

4666 4667
    def analyse_scoped_expressions(self, env):
        self.loop_analysed = True
4668 4669
        if self.has_local_scope:
            self.loop.analyse_expressions(env)
4670

4671
    def coerce_to(self, dst_type, env):
4672 4673 4674 4675 4676 4677
        if self.orig_func == 'sum' and dst_type.is_numeric and not self.loop_analysed:
            # We can optimise by dropping the aggregation variable and
            # the add operations into C.  This can only be done safely
            # before analysing the loop body, after that, the result
            # reference type will have infected expressions and
            # assignments.
4678 4679
            self.result_node.type = self.type = dst_type
            return self
4680
        return super(InlinedGeneratorExpressionNode, self).coerce_to(dst_type, env)
4681

4682 4683 4684 4685 4686
    def generate_result_code(self, code):
        self.result_node.result_code = self.result()
        self.loop.generate_execution_code(code)


4687
class SetNode(ExprNode):
4688 4689
    #  Set constructor.

4690 4691
    type = set_type

4692 4693 4694
    subexprs = ['args']

    gil_message = "Constructing Python set"
4695

4696 4697 4698 4699 4700 4701 4702 4703
    def analyse_types(self, env):
        for i in range(len(self.args)):
            arg = self.args[i]
            arg.analyse_types(env)
            self.args[i] = arg.coerce_to_pyobject(env)
        self.type = set_type
        self.is_temp = 1

4704 4705 4706
    def may_be_none(self):
        return False

4707 4708 4709 4710
    def calculate_constant_result(self):
        self.constant_result = set([
                arg.constant_result for arg in self.args])

4711 4712 4713 4714 4715 4716 4717 4718
    def compile_time_value(self, denv):
        values = [arg.compile_time_value(denv) for arg in self.args]
        try:
            return set(values)
        except Exception, e:
            self.compile_time_value_error(e)

    def generate_evaluation_code(self, code):
4719
        code.globalstate.use_utility_code(Builtin.py23_set_utility_code)
4720 4721 4722 4723 4724
        self.allocate_temp_result(code)
        code.putln(
            "%s = PySet_New(0); %s" % (
                self.result(),
                code.error_goto_if_null(self.result(), self.pos)))
4725
        code.put_gotref(self.py_result())
4726 4727 4728 4729 4730 4731 4732 4733
        for arg in self.args:
            arg.generate_evaluation_code(code)
            code.putln(
                code.error_goto_if_neg(
                    "PySet_Add(%s, %s)" % (self.result(), arg.py_result()),
                    self.pos))
            arg.generate_disposal_code(code)
            arg.free_temps(code)
Robert Bradshaw's avatar
Robert Bradshaw committed
4734

William Stein's avatar
William Stein committed
4735

4736
class DictNode(ExprNode):
William Stein's avatar
William Stein committed
4737 4738
    #  Dictionary constructor.
    #
Vitja Makarov's avatar
Vitja Makarov committed
4739 4740
    #  key_value_pairs     [DictItemNode]
    #  exclude_null_values [boolean]          Do not add NULL values to dict
4741 4742
    #
    # obj_conversion_errors    [PyrexError]   used internally
4743

4744
    subexprs = ['key_value_pairs']
4745
    is_temp = 1
Vitja Makarov's avatar
Vitja Makarov committed
4746
    exclude_null_values = False
4747
    type = dict_type
4748

4749
    obj_conversion_errors = []
4750 4751 4752 4753

    def calculate_constant_result(self):
        self.constant_result = dict([
                item.constant_result for item in self.key_value_pairs])
4754

4755
    def compile_time_value(self, denv):
Robert Bradshaw's avatar
Robert Bradshaw committed
4756 4757
        pairs = [(item.key.compile_time_value(denv), item.value.compile_time_value(denv))
            for item in self.key_value_pairs]
4758 4759 4760 4761
        try:
            return dict(pairs)
        except Exception, e:
            self.compile_time_value_error(e)
4762

Robert Bradshaw's avatar
Robert Bradshaw committed
4763
    def type_dependencies(self, env):
4764
        return ()
4765

4766 4767 4768 4769
    def infer_type(self, env):
        # TOOD: Infer struct constructors.
        return dict_type

William Stein's avatar
William Stein committed
4770
    def analyse_types(self, env):
4771
        hold_errors()
Robert Bradshaw's avatar
Robert Bradshaw committed
4772 4773
        for item in self.key_value_pairs:
            item.analyse_types(env)
4774 4775
        self.obj_conversion_errors = held_errors()
        release_errors(ignore=True)
4776 4777 4778

    def may_be_none(self):
        return False
4779

4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793
    def coerce_to(self, dst_type, env):
        if dst_type.is_pyobject:
            self.release_errors()
            if not self.type.subtype_of(dst_type):
                error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
        elif dst_type.is_struct_or_union:
            self.type = dst_type
            if not dst_type.is_struct and len(self.key_value_pairs) != 1:
                error(self.pos, "Exactly one field must be specified to convert to union '%s'" % dst_type)
            elif dst_type.is_struct and len(self.key_value_pairs) < len(dst_type.scope.var_entries):
                warning(self.pos, "Not all members given for struct '%s'" % dst_type, 1)
            for item in self.key_value_pairs:
                if isinstance(item.key, CoerceToPyTypeNode):
                    item.key = item.key.arg
4794
                if not isinstance(item.key, (UnicodeNode, StringNode, BytesNode)):
4795
                    error(item.key.pos, "Invalid struct field identifier")
4796
                    item.key = StringNode(item.key.pos, value="<error>")
4797
                else:
Stefan Behnel's avatar
Stefan Behnel committed
4798 4799
                    key = str(item.key.value) # converts string literals to unicode in Py3
                    member = dst_type.scope.lookup_here(key)
4800
                    if not member:
Stefan Behnel's avatar
Stefan Behnel committed
4801
                        error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, key))
4802 4803 4804 4805 4806 4807 4808 4809 4810
                    else:
                        value = item.value
                        if isinstance(value, CoerceToPyTypeNode):
                            value = value.arg
                        item.value = value.coerce_to(member.type, env)
        else:
            self.type = error_type
            error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
        return self
4811

4812 4813 4814 4815
    def release_errors(self):
        for err in self.obj_conversion_errors:
            report_error(err)
        self.obj_conversion_errors = []
4816 4817 4818

    gil_message = "Constructing Python dict"

William Stein's avatar
William Stein committed
4819 4820 4821
    def generate_evaluation_code(self, code):
        #  Custom method used here because key-value
        #  pairs are evaluated and used one at a time.
4822 4823
        code.mark_pos(self.pos)
        self.allocate_temp_result(code)
4824 4825 4826 4827 4828 4829
        if self.type.is_pyobject:
            self.release_errors()
            code.putln(
                "%s = PyDict_New(); %s" % (
                    self.result(),
                    code.error_goto_if_null(self.result(), self.pos)))
4830
            code.put_gotref(self.py_result())
Robert Bradshaw's avatar
Robert Bradshaw committed
4831 4832
        for item in self.key_value_pairs:
            item.generate_evaluation_code(code)
4833
            if self.type.is_pyobject:
Vitja Makarov's avatar
Vitja Makarov committed
4834 4835
                if self.exclude_null_values:
                    code.putln('if (%s) {' % item.value.py_result())
4836
                code.put_error_if_neg(self.pos,
4837 4838 4839 4840
                    "PyDict_SetItem(%s, %s, %s)" % (
                        self.result(),
                        item.key.py_result(),
                        item.value.py_result()))
Vitja Makarov's avatar
Vitja Makarov committed
4841 4842
                if self.exclude_null_values:
                    code.putln('}')
4843 4844 4845
            else:
                code.putln("%s.%s = %s;" % (
                        self.result(),
4846
                        item.key.value,
4847
                        item.value.result()))
Robert Bradshaw's avatar
Robert Bradshaw committed
4848
            item.generate_disposal_code(code)
4849
            item.free_temps(code)
4850

4851
    def annotate(self, code):
Robert Bradshaw's avatar
Robert Bradshaw committed
4852 4853
        for item in self.key_value_pairs:
            item.annotate(code)
4854

4855
class DictItemNode(ExprNode):
Robert Bradshaw's avatar
Robert Bradshaw committed
4856 4857 4858 4859 4860
    # Represents a single item in a DictNode
    #
    # key          ExprNode
    # value        ExprNode
    subexprs = ['key', 'value']
4861

4862
    nogil_check = None # Parent DictNode takes care of it
4863

4864 4865 4866
    def calculate_constant_result(self):
        self.constant_result = (
            self.key.constant_result, self.value.constant_result)
4867

Robert Bradshaw's avatar
Robert Bradshaw committed
4868 4869 4870 4871 4872
    def analyse_types(self, env):
        self.key.analyse_types(env)
        self.value.analyse_types(env)
        self.key = self.key.coerce_to_pyobject(env)
        self.value = self.value.coerce_to_pyobject(env)
4873

Robert Bradshaw's avatar
Robert Bradshaw committed
4874 4875 4876
    def generate_evaluation_code(self, code):
        self.key.generate_evaluation_code(code)
        self.value.generate_evaluation_code(code)
Stefan Behnel's avatar
Stefan Behnel committed
4877

4878 4879 4880
    def generate_disposal_code(self, code):
        self.key.generate_disposal_code(code)
        self.value.generate_disposal_code(code)
4881 4882 4883 4884

    def free_temps(self, code):
        self.key.free_temps(code)
        self.value.free_temps(code)
4885

4886 4887
    def __iter__(self):
        return iter([self.key, self.value])
William Stein's avatar
William Stein committed
4888

4889 4890 4891 4892 4893 4894 4895
class ModuleNameMixin(object):
    def set_mod_name(self, env):
        self.module_name = env.global_scope().qualified_name

    def get_py_mod_name(self, code):
        return code.get_py_string_const(
                 self.module_name, identifier=True)
Stefan Behnel's avatar
Stefan Behnel committed
4896

4897
class ClassNode(ExprNode, ModuleNameMixin):
William Stein's avatar
William Stein committed
4898 4899 4900 4901
    #  Helper class used in the implementation of Python
    #  class definitions. Constructs a class object given
    #  a name, tuple of bases and class dictionary.
    #
Stefan Behnel's avatar
Stefan Behnel committed
4902
    #  name         EncodedString      Name of the class
William Stein's avatar
William Stein committed
4903 4904 4905
    #  bases        ExprNode           Base class tuple
    #  dict         ExprNode           Class dict (not owned by this node)
    #  doc          ExprNode or None   Doc string
4906
    #  module_name  EncodedString      Name of defining module
4907

4908
    subexprs = ['bases', 'doc']
4909

William Stein's avatar
William Stein committed
4910 4911 4912 4913 4914 4915 4916 4917
    def analyse_types(self, env):
        self.bases.analyse_types(env)
        if self.doc:
            self.doc.analyse_types(env)
            self.doc = self.doc.coerce_to_pyobject(env)
        self.type = py_object_type
        self.is_temp = 1
        env.use_utility_code(create_class_utility_code);
4918 4919
        #TODO(craig,haoyu) This should be moved to a better place
        self.set_mod_name(env)
4920

4921
    def may_be_none(self):
Stefan Behnel's avatar
Stefan Behnel committed
4922
        return True
4923

4924 4925
    gil_message = "Constructing Python class"

William Stein's avatar
William Stein committed
4926
    def generate_result_code(self, code):
4927
        cname = code.intern_identifier(self.name)
4928

William Stein's avatar
William Stein committed
4929
        if self.doc:
4930
            code.put_error_if_neg(self.pos,
Robert Bradshaw's avatar
Robert Bradshaw committed
4931
                'PyDict_SetItemString(%s, "__doc__", %s)' % (
William Stein's avatar
William Stein committed
4932
                    self.dict.py_result(),
Robert Bradshaw's avatar
Robert Bradshaw committed
4933
                    self.doc.py_result()))
4934
        py_mod_name = self.get_py_mod_name(code)
William Stein's avatar
William Stein committed
4935
        code.putln(
4936
            '%s = __Pyx_CreateClass(%s, %s, %s, %s); %s' % (
4937
                self.result(),
William Stein's avatar
William Stein committed
4938 4939
                self.bases.py_result(),
                self.dict.py_result(),
4940
                cname,
4941
                py_mod_name,
4942
                code.error_goto_if_null(self.result(), self.pos)))
4943
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
4944

Stefan Behnel's avatar
Stefan Behnel committed
4945

4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963
class Py3ClassNode(ExprNode):
    #  Helper class used in the implementation of Python3+
    #  class definitions. Constructs a class object given
    #  a name, tuple of bases and class dictionary.
    #
    #  name         EncodedString      Name of the class
    #  dict         ExprNode           Class dict (not owned by this node)
    #  module_name  EncodedString      Name of defining module

    subexprs = []

    def analyse_types(self, env):
        self.type = py_object_type
        self.is_temp = 1

    def may_be_none(self):
        return True

4964
    gil_message = "Constructing Python class"
4965 4966

    def generate_result_code(self, code):
4967
        code.globalstate.use_utility_code(create_py3class_utility_code)
4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015
        cname = code.intern_identifier(self.name)
        code.putln(
            '%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s); %s' % (
                self.result(),
                self.metaclass.result(),
                cname,
                self.bases.py_result(),
                self.dict.py_result(),
                self.mkw.py_result(),
                code.error_goto_if_null(self.result(), self.pos)))
        code.put_gotref(self.py_result())

class KeywordArgsNode(ExprNode):
    # Helper class for keyword arguments
    #
    # keyword_args ExprNode or None     Keyword arguments
    # starstar_arg ExprNode or None     Extra arguments

    subexprs = ['keyword_args', 'starstar_arg']

    def analyse_types(self, env):
        if self.keyword_args:
            self.keyword_args.analyse_types(env)
        if self.starstar_arg:
            self.starstar_arg.analyse_types(env)
            # make sure we have a Python object as **kwargs mapping
            self.starstar_arg = \
                self.starstar_arg.coerce_to_pyobject(env)
        self.type = py_object_type
        self.is_temp = 1

    gil_message = "Constructing Keyword Args"

    def generate_result_code(self, code):
        if self.keyword_args and self.starstar_arg:
            code.put_error_if_neg(self.pos,
                "PyDict_Update(%s, %s)" % (
                    self.keyword_args.py_result(),
                    self.starstar_arg.py_result()))
        if self.keyword_args:
            code.putln("%s = %s;" % (self.result(), self.keyword_args.result()))
            code.put_incref(self.keyword_args.result(), self.keyword_args.ctype())
        elif self.starstar_arg:
            code.putln(
                "%s = PyDict_Copy(%s); %s" % (
                    self.result(),
                    self.starstar_arg.py_result(),
                    code.error_goto_if_null(self.result(), self.pos)))
5016
            code.put_gotref(self.py_result())
5017 5018 5019 5020 5021
        else:
            code.putln(
                "%s = PyDict_New(); %s" % (
                    self.result(),
                    code.error_goto_if_null(self.result(), self.pos)))
5022
            code.put_gotref(self.py_result())
5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090

class PyClassMetaclassNode(ExprNode):
    # Helper class holds Python3 metaclass object
    #
    #  bases        ExprNode           Base class tuple (not owned by this node)
    #  mkw          ExprNode           Class keyword arguments (not owned by this node)

    subexprs = []

    def analyse_types(self, env):
        self.type = py_object_type
        self.is_temp = True

    def may_be_none(self):
        return True

    def generate_result_code(self, code):
        code.putln(
            "%s = __Pyx_Py3MetaclassGet(%s, %s); %s" % (
                self.result(),
                self.bases.result(),
                self.mkw.result(),
                code.error_goto_if_null(self.result(), self.pos)))
        code.put_gotref(self.py_result())

class PyClassNamespaceNode(ExprNode, ModuleNameMixin):
    # Helper class holds Python3 namespace object
    #
    # All this are not owned by this node
    #  metaclass    ExprNode           Metaclass object
    #  bases        ExprNode           Base class tuple
    #  mkw          ExprNode           Class keyword arguments
    #  doc          ExprNode or None   Doc string (owned)

    subexprs = ['doc']

    def analyse_types(self, env):
        self.bases.analyse_types(env)
        if self.doc:
            self.doc.analyse_types(env)
            self.doc = self.doc.coerce_to_pyobject(env)
        self.type = py_object_type
        self.is_temp = 1
        #TODO(craig,haoyu) This should be moved to a better place
        self.set_mod_name(env)

    def may_be_none(self):
        return True

    def generate_result_code(self, code):
        cname = code.intern_identifier(self.name)
        py_mod_name = self.get_py_mod_name(code)
        if self.doc:
            doc_code = self.doc.result()
        else:
            doc_code = '(PyObject *) NULL'
        code.putln(
            "%s = __Pyx_Py3MetaclassPrepare(%s, %s, %s, %s, %s, %s); %s" % (
                self.result(),
                self.metaclass.result(),
                self.bases.result(),
                cname,
                self.mkw.result(),
                py_mod_name,
                doc_code,
                code.error_goto_if_null(self.result(), self.pos)))
        code.put_gotref(self.py_result())

Robert Bradshaw's avatar
Robert Bradshaw committed
5091 5092 5093 5094 5095 5096 5097
class BoundMethodNode(ExprNode):
    #  Helper class used in the implementation of Python
    #  class definitions. Constructs an bound method
    #  object from a class and a function.
    #
    #  function      ExprNode   Function object
    #  self_object   ExprNode   self object
5098

Robert Bradshaw's avatar
Robert Bradshaw committed
5099
    subexprs = ['function']
5100

Robert Bradshaw's avatar
Robert Bradshaw committed
5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116
    def analyse_types(self, env):
        self.function.analyse_types(env)
        self.type = py_object_type
        self.is_temp = 1

    gil_message = "Constructing an bound method"

    def generate_result_code(self, code):
        code.putln(
            "%s = PyMethod_New(%s, %s, (PyObject*)%s->ob_type); %s" % (
                self.result(),
                self.function.py_result(),
                self.self_object.py_result(),
                self.self_object.py_result(),
                code.error_goto_if_null(self.result(), self.pos)))
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
5117

5118
class UnboundMethodNode(ExprNode):
William Stein's avatar
William Stein committed
5119 5120 5121 5122 5123
    #  Helper class used in the implementation of Python
    #  class definitions. Constructs an unbound method
    #  object from a class and a function.
    #
    #  function      ExprNode   Function object
5124

5125 5126
    type = py_object_type
    is_temp = 1
5127

William Stein's avatar
William Stein committed
5128
    subexprs = ['function']
5129

William Stein's avatar
William Stein committed
5130 5131
    def analyse_types(self, env):
        self.function.analyse_types(env)
5132

5133 5134 5135
    def may_be_none(self):
        return False

5136 5137
    gil_message = "Constructing an unbound method"

William Stein's avatar
William Stein committed
5138
    def generate_result_code(self, code):
5139
        class_cname = code.pyclass_stack[-1].classobj.result()
William Stein's avatar
William Stein committed
5140
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
5141
            "%s = PyMethod_New(%s, 0, %s); %s" % (
5142
                self.result(),
William Stein's avatar
William Stein committed
5143
                self.function.py_result(),
5144
                class_cname,
5145
                code.error_goto_if_null(self.result(), self.pos)))
5146
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
5147

Robert Bradshaw's avatar
Robert Bradshaw committed
5148

5149
class PyCFunctionNode(ExprNode, ModuleNameMixin):
William Stein's avatar
William Stein committed
5150 5151 5152 5153
    #  Helper class used in the implementation of Python
    #  class definitions. Constructs a PyCFunction object
    #  from a PyMethodDef struct.
    #
5154
    #  pymethdef_cname   string             PyMethodDef structure
Robert Bradshaw's avatar
Robert Bradshaw committed
5155
    #  self_object       ExprNode or None
Robert Bradshaw's avatar
Robert Bradshaw committed
5156
    #  binding           bool
5157
    #  module_name       EncodedString      Name of defining module
Stefan Behnel's avatar
Stefan Behnel committed
5158 5159

    subexprs = []
Robert Bradshaw's avatar
Robert Bradshaw committed
5160
    self_object = None
Robert Bradshaw's avatar
Robert Bradshaw committed
5161
    binding = False
5162

5163 5164
    type = py_object_type
    is_temp = 1
5165

William Stein's avatar
William Stein committed
5166
    def analyse_types(self, env):
Robert Bradshaw's avatar
Robert Bradshaw committed
5167 5168
        if self.binding:
            env.use_utility_code(binding_cfunc_utility_code)
5169

5170 5171 5172
        #TODO(craig,haoyu) This should be moved to a better place
        self.set_mod_name(env)

5173 5174
    def may_be_none(self):
        return False
5175

5176 5177
    gil_message = "Constructing Python function"

Stefan Behnel's avatar
Stefan Behnel committed
5178
    def self_result_code(self):
Robert Bradshaw's avatar
Robert Bradshaw committed
5179 5180 5181 5182
        if self.self_object is None:
            self_result = "NULL"
        else:
            self_result = self.self_object.py_result()
Stefan Behnel's avatar
Stefan Behnel committed
5183 5184 5185
        return self_result

    def generate_result_code(self, code):
Robert Bradshaw's avatar
Robert Bradshaw committed
5186
        if self.binding:
5187
            constructor = "%s_NewEx" % Naming.binding_cfunc
Robert Bradshaw's avatar
Robert Bradshaw committed
5188
        else:
5189 5190
            constructor = "PyCFunction_NewEx"
        py_mod_name = self.get_py_mod_name(code)
William Stein's avatar
William Stein committed
5191
        code.putln(
5192
            '%s = %s(&%s, %s, %s); %s' % (
5193
                self.result(),
Robert Bradshaw's avatar
Robert Bradshaw committed
5194
                constructor,
William Stein's avatar
William Stein committed
5195
                self.pymethdef_cname,
Stefan Behnel's avatar
Stefan Behnel committed
5196
                self.self_result_code(),
5197
                py_mod_name,
5198
                code.error_goto_if_null(self.result(), self.pos)))
5199
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
5200

Stefan Behnel's avatar
Stefan Behnel committed
5201 5202 5203
class InnerFunctionNode(PyCFunctionNode):
    # Special PyCFunctionNode that depends on a closure class
    #
Vitja Makarov's avatar
Vitja Makarov committed
5204

Robert Bradshaw's avatar
Robert Bradshaw committed
5205
    binding = True
Vitja Makarov's avatar
Vitja Makarov committed
5206 5207
    needs_self_code = True

Stefan Behnel's avatar
Stefan Behnel committed
5208
    def self_result_code(self):
Vitja Makarov's avatar
Vitja Makarov committed
5209 5210 5211
        if self.needs_self_code:
            return "((PyObject*)%s)" % (Naming.cur_scope_cname)
        return "NULL"
Stefan Behnel's avatar
Stefan Behnel committed
5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232

class LambdaNode(InnerFunctionNode):
    # Lambda expression node (only used as a function reference)
    #
    # args          [CArgDeclNode]         formal arguments
    # star_arg      PyArgDeclNode or None  * argument
    # starstar_arg  PyArgDeclNode or None  ** argument
    # lambda_name   string                 a module-globally unique lambda name
    # result_expr   ExprNode
    # def_node      DefNode                the underlying function 'def' node

    child_attrs = ['def_node']

    def_node = None
    name = StringEncoding.EncodedString('<lambda>')

    def analyse_declarations(self, env):
        self.def_node.analyse_declarations(env)
        self.pymethdef_cname = self.def_node.entry.pymethdef_cname
        env.add_lambda_def(self.def_node)

5233

5234 5235 5236 5237 5238 5239 5240 5241
class GeneratorExpressionNode(LambdaNode):
    # A generator expression, e.g.  (i for i in range(10))
    #
    # Result is a generator.
    #
    # loop      ForStatNode   the for-loop, containing a YieldExprNode
    # def_node  DefNode       the underlying generator 'def' node

5242
    name = StringEncoding.EncodedString('genexpr')
5243 5244 5245
    binding = False

    def analyse_declarations(self, env):
5246
        self.def_node.no_assignment_synthesis = True
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259
        self.def_node.analyse_declarations(env)
        env.add_lambda_def(self.def_node)

    def generate_result_code(self, code):
        code.putln(
            '%s = %s(%s, NULL); %s' % (
                self.result(),
                self.def_node.entry.func_cname,
                self.self_result_code(),
                code.error_goto_if_null(self.result(), self.pos)))
        code.put_gotref(self.py_result())


5260 5261 5262 5263 5264
class YieldExprNode(ExprNode):
    # Yield expression node
    #
    # arg         ExprNode   the value to return from the generator
    # label_name  string     name of the C label used for this yield
5265
    # label_num   integer    yield label number
5266 5267 5268

    subexprs = ['arg']
    type = py_object_type
5269
    label_num = 0
5270 5271

    def analyse_types(self, env):
5272 5273
        if not self.label_num:
            error(self.pos, "'yield' not supported here")
5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294
        self.is_temp = 1
        if self.arg is not None:
            self.arg.analyse_types(env)
            if not self.arg.type.is_pyobject:
                self.arg = self.arg.coerce_to_pyobject(env)

    def generate_evaluation_code(self, code):
        self.label_name = code.new_label('resume_from_yield')
        code.use_label(self.label_name)
        if self.arg:
            self.arg.generate_evaluation_code(code)
            self.arg.make_owned_reference(code)
            code.putln(
                "%s = %s;" % (
                    Naming.retval_cname,
                    self.arg.result_as(py_object_type)))
            self.arg.generate_post_assignment_code(code)
            #self.arg.generate_disposal_code(code)
            self.arg.free_temps(code)
        else:
            code.put_init_to_py_none(Naming.retval_cname, py_object_type)
5295
        saved = []
5296
        code.funcstate.closure_temps.reset()
5297
        for cname, type, manage_ref in code.funcstate.temps_in_use():
5298
            save_cname = code.funcstate.closure_temps.allocate_temp(type)
5299 5300 5301 5302
            saved.append((cname, save_cname, type))
            if type.is_pyobject:
                code.put_xgiveref(cname)
            code.putln('%s->%s = %s;' % (Naming.cur_scope_cname, save_cname, cname))
5303

5304
        code.put_xgiveref(Naming.retval_cname)
5305
        code.put_finish_refcount_context()
Stefan Behnel's avatar
Stefan Behnel committed
5306
        code.putln("/* return from generator, yielding value */")
5307 5308 5309
        code.putln("%s->%s.resume_label = %d;" % (Naming.cur_scope_cname, Naming.obj_base_cname, self.label_num))
        code.putln("return %s;" % Naming.retval_cname);
        code.put_label(self.label_name)
5310 5311 5312 5313 5314 5315
        for cname, save_cname, type in saved:
            code.putln('%s = %s->%s;' % (cname, Naming.cur_scope_cname, save_cname))
            if type.is_pyobject:
                code.putln('%s->%s = 0;' % (Naming.cur_scope_cname, save_cname))
            if type.is_pyobject:
                code.put_xgotref(cname)
5316 5317 5318 5319 5320 5321 5322 5323
        if self.result_is_used:
            self.allocate_temp_result(code)
            code.putln('%s = %s; %s' %
                       (self.result(), Naming.sent_value_cname,
                        code.error_goto_if_null(self.result(), self.pos)))
            code.put_incref(self.result(), py_object_type)
        else:
            code.putln(code.error_goto_if_null(Naming.sent_value_cname, self.pos))
5324

William Stein's avatar
William Stein committed
5325 5326 5327 5328 5329 5330
#-------------------------------------------------------------------
#
#  Unary operator nodes
#
#-------------------------------------------------------------------

5331 5332 5333 5334 5335 5336 5337
compile_time_unary_operators = {
    'not': operator.not_,
    '~': operator.inv,
    '-': operator.neg,
    '+': operator.pos,
}

5338
class UnopNode(ExprNode):
William Stein's avatar
William Stein committed
5339 5340 5341 5342 5343 5344 5345 5346 5347 5348
    #  operator     string
    #  operand      ExprNode
    #
    #  Processing during analyse_expressions phase:
    #
    #    analyse_c_operation
    #      Called when the operand is not a pyobject.
    #      - Check operand type and coerce if needed.
    #      - Determine result type and result code fragment.
    #      - Allocate temporary for result if needed.
5349

William Stein's avatar
William Stein committed
5350
    subexprs = ['operand']
Robert Bradshaw's avatar
Robert Bradshaw committed
5351
    infix = True
5352 5353 5354 5355

    def calculate_constant_result(self):
        func = compile_time_unary_operators[self.operator]
        self.constant_result = func(self.operand.constant_result)
5356

5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367
    def compile_time_value(self, denv):
        func = compile_time_unary_operators.get(self.operator)
        if not func:
            error(self.pos,
                "Unary '%s' not supported in compile-time expression"
                    % self.operator)
        operand = self.operand.compile_time_value(denv)
        try:
            return func(operand)
        except Exception, e:
            self.compile_time_value_error(e)
5368

5369
    def infer_type(self, env):
5370 5371 5372 5373 5374
        operand_type = self.operand.infer_type(env)
        if operand_type.is_pyobject:
            return py_object_type
        else:
            return operand_type
5375

William Stein's avatar
William Stein committed
5376 5377 5378 5379 5380 5381
    def analyse_types(self, env):
        self.operand.analyse_types(env)
        if self.is_py_operation():
            self.coerce_operand_to_pyobject(env)
            self.type = py_object_type
            self.is_temp = 1
5382 5383
        elif self.is_cpp_operation():
            self.analyse_cpp_operation(env)
William Stein's avatar
William Stein committed
5384 5385
        else:
            self.analyse_c_operation(env)
5386

William Stein's avatar
William Stein committed
5387
    def check_const(self):
5388
        return self.operand.check_const()
5389

William Stein's avatar
William Stein committed
5390 5391
    def is_py_operation(self):
        return self.operand.type.is_pyobject
5392

5393
    def nogil_check(self, env):
5394
        if self.is_py_operation():
5395
            self.gil_error()
5396

Danilo Freitas's avatar
Danilo Freitas committed
5397
    def is_cpp_operation(self):
Robert Bradshaw's avatar
Robert Bradshaw committed
5398
        type = self.operand.type
Robert Bradshaw's avatar
Robert Bradshaw committed
5399
        return type.is_cpp_class
5400

William Stein's avatar
William Stein committed
5401 5402
    def coerce_operand_to_pyobject(self, env):
        self.operand = self.operand.coerce_to_pyobject(env)
5403

William Stein's avatar
William Stein committed
5404 5405 5406
    def generate_result_code(self, code):
        if self.operand.type.is_pyobject:
            self.generate_py_operation_code(code)
5407

William Stein's avatar
William Stein committed
5408 5409 5410
    def generate_py_operation_code(self, code):
        function = self.py_operation_function()
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
5411
            "%s = %s(%s); %s" % (
5412 5413
                self.result(),
                function,
William Stein's avatar
William Stein committed
5414
                self.operand.py_result(),
5415
                code.error_goto_if_null(self.result(), self.pos)))
5416
        code.put_gotref(self.py_result())
5417

William Stein's avatar
William Stein committed
5418 5419 5420 5421 5422 5423
    def type_error(self):
        if not self.operand.type.is_error:
            error(self.pos, "Invalid operand type for '%s' (%s)" %
                (self.operator, self.operand.type))
        self.type = PyrexTypes.error_type

Danilo Freitas's avatar
Danilo Freitas committed
5424
    def analyse_cpp_operation(self, env):
5425
        type = self.operand.type
Robert Bradshaw's avatar
Robert Bradshaw committed
5426
        if type.is_ptr:
Danilo Freitas's avatar
Danilo Freitas committed
5427
            type = type.base_type
Robert Bradshaw's avatar
Robert Bradshaw committed
5428
        function = type.scope.lookup("operator%s" % self.operator)
Danilo Freitas's avatar
Danilo Freitas committed
5429 5430
        if not function:
            error(self.pos, "'%s' operator not defined for %s"
5431
                % (self.operator, type))
Danilo Freitas's avatar
Danilo Freitas committed
5432 5433
            self.type_error()
            return
5434 5435 5436 5437
        func_type = function.type
        if func_type.is_ptr:
            func_type = func_type.base_type
        self.type = func_type.return_type
Danilo Freitas's avatar
Danilo Freitas committed
5438

William Stein's avatar
William Stein committed
5439

5440
class NotNode(ExprNode):
William Stein's avatar
William Stein committed
5441 5442 5443
    #  'not' operator
    #
    #  operand   ExprNode
5444

5445
    type = PyrexTypes.c_bint_type
5446

5447
    subexprs = ['operand']
5448

5449 5450 5451
    def calculate_constant_result(self):
        self.constant_result = not self.operand.constant_result

5452 5453 5454 5455 5456 5457 5458
    def compile_time_value(self, denv):
        operand = self.operand.compile_time_value(denv)
        try:
            return not operand
        except Exception, e:
            self.compile_time_value_error(e)

5459 5460
    def infer_type(self, env):
        return PyrexTypes.c_bint_type
5461

William Stein's avatar
William Stein committed
5462 5463 5464
    def analyse_types(self, env):
        self.operand.analyse_types(env)
        self.operand = self.operand.coerce_to_boolean(env)
5465

William Stein's avatar
William Stein committed
5466
    def calculate_result_code(self):
5467
        return "(!%s)" % self.operand.result()
5468

William Stein's avatar
William Stein committed
5469 5470 5471 5472 5473 5474
    def generate_result_code(self, code):
        pass


class UnaryPlusNode(UnopNode):
    #  unary '+' operator
5475

William Stein's avatar
William Stein committed
5476
    operator = '+'
5477

William Stein's avatar
William Stein committed
5478
    def analyse_c_operation(self, env):
Lisandro Dalcin's avatar
Lisandro Dalcin committed
5479
        self.type = PyrexTypes.widest_numeric_type(
Robert Bradshaw's avatar
Robert Bradshaw committed
5480
            self.operand.type, PyrexTypes.c_int_type)
5481

William Stein's avatar
William Stein committed
5482 5483
    def py_operation_function(self):
        return "PyNumber_Positive"
5484

William Stein's avatar
William Stein committed
5485
    def calculate_result_code(self):
5486 5487 5488 5489
        if self.is_cpp_operation():
            return "(+%s)" % self.operand.result()
        else:
            return self.operand.result()
William Stein's avatar
William Stein committed
5490 5491 5492 5493


class UnaryMinusNode(UnopNode):
    #  unary '-' operator
5494

William Stein's avatar
William Stein committed
5495
    operator = '-'
5496

William Stein's avatar
William Stein committed
5497 5498
    def analyse_c_operation(self, env):
        if self.operand.type.is_numeric:
5499 5500
            self.type = PyrexTypes.widest_numeric_type(
                self.operand.type, PyrexTypes.c_int_type)
William Stein's avatar
William Stein committed
5501 5502
        else:
            self.type_error()
Robert Bradshaw's avatar
Robert Bradshaw committed
5503
        if self.type.is_complex:
5504
            self.infix = False
5505

William Stein's avatar
William Stein committed
5506 5507
    def py_operation_function(self):
        return "PyNumber_Negative"
5508

William Stein's avatar
William Stein committed
5509
    def calculate_result_code(self):
Robert Bradshaw's avatar
Robert Bradshaw committed
5510 5511 5512 5513
        if self.infix:
            return "(-%s)" % self.operand.result()
        else:
            return "%s(%s)" % (self.operand.type.unary_op('-'), self.operand.result())
William Stein's avatar
William Stein committed
5514

5515 5516 5517 5518 5519
    def get_constant_c_result_code(self):
        value = self.operand.get_constant_c_result_code()
        if value:
            return "(-%s)" % (value)

William Stein's avatar
William Stein committed
5520 5521 5522 5523 5524
class TildeNode(UnopNode):
    #  unary '~' operator

    def analyse_c_operation(self, env):
        if self.operand.type.is_int:
5525 5526
            self.type = PyrexTypes.widest_numeric_type(
                self.operand.type, PyrexTypes.c_int_type)
William Stein's avatar
William Stein committed
5527 5528 5529 5530 5531
        else:
            self.type_error()

    def py_operation_function(self):
        return "PyNumber_Invert"
5532

William Stein's avatar
William Stein committed
5533
    def calculate_result_code(self):
5534
        return "(~%s)" % self.operand.result()
William Stein's avatar
William Stein committed
5535 5536


5537 5538
class CUnopNode(UnopNode):

Robert Bradshaw's avatar
Robert Bradshaw committed
5539 5540 5541
    def is_py_operation(self):
        return False

5542 5543
class DereferenceNode(CUnopNode):
    #  unary * operator
5544 5545

    operator = '*'
5546

Robert Bradshaw's avatar
Robert Bradshaw committed
5547 5548 5549 5550 5551 5552 5553 5554
    def analyse_c_operation(self, env):
        if self.operand.type.is_ptr:
            self.type = self.operand.type.base_type
        else:
            self.type_error()

    def calculate_result_code(self):
        return "(*%s)" % self.operand.result()
William Stein's avatar
William Stein committed
5555 5556


5557 5558
class DecrementIncrementNode(CUnopNode):
    #  unary ++/-- operator
5559

5560
    def analyse_c_operation(self, env):
5561 5562 5563 5564
        if self.operand.type.is_numeric:
            self.type = PyrexTypes.widest_numeric_type(
                self.operand.type, PyrexTypes.c_int_type)
        elif self.operand.type.is_ptr:
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578
            self.type = self.operand.type
        else:
            self.type_error()

    def calculate_result_code(self):
        if self.is_prefix:
            return "(%s%s)" % (self.operator, self.operand.result())
        else:
            return "(%s%s)" % (self.operand.result(), self.operator)

def inc_dec_constructor(is_prefix, operator):
    return lambda pos, **kwds: DecrementIncrementNode(pos, is_prefix=is_prefix, operator=operator, **kwds)


5579
class AmpersandNode(ExprNode):
William Stein's avatar
William Stein committed
5580 5581 5582
    #  The C address-of operator.
    #
    #  operand  ExprNode
5583

William Stein's avatar
William Stein committed
5584
    subexprs = ['operand']
5585

5586 5587
    def infer_type(self, env):
        return PyrexTypes.c_ptr_type(self.operand.infer_type(env))
William Stein's avatar
William Stein committed
5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598

    def analyse_types(self, env):
        self.operand.analyse_types(env)
        argtype = self.operand.type
        if not (argtype.is_cfunction or self.operand.is_lvalue()):
            self.error("Taking address of non-lvalue")
            return
        if argtype.is_pyobject:
            self.error("Cannot take address of Python variable")
            return
        self.type = PyrexTypes.c_ptr_type(argtype)
5599

William Stein's avatar
William Stein committed
5600
    def check_const(self):
5601
        return self.operand.check_const_addr()
5602

William Stein's avatar
William Stein committed
5603 5604 5605 5606
    def error(self, mess):
        error(self.pos, mess)
        self.type = PyrexTypes.error_type
        self.result_code = "<error>"
5607

William Stein's avatar
William Stein committed
5608
    def calculate_result_code(self):
5609
        return "(&%s)" % self.operand.result()
William Stein's avatar
William Stein committed
5610 5611 5612

    def generate_result_code(self, code):
        pass
5613

William Stein's avatar
William Stein committed
5614 5615 5616 5617 5618 5619 5620 5621

unop_node_classes = {
    "+":  UnaryPlusNode,
    "-":  UnaryMinusNode,
    "~":  TildeNode,
}

def unop_node(pos, operator, operand):
5622
    # Construct unnop node of appropriate class for
William Stein's avatar
William Stein committed
5623
    # given operator.
5624
    if isinstance(operand, IntNode) and operator == '-':
5625
        return IntNode(pos = operand.pos, value = str(-Utils.str_to_number(operand.value)))
Robert Bradshaw's avatar
Robert Bradshaw committed
5626 5627
    elif isinstance(operand, UnopNode) and operand.operator == operator:
        warning(pos, "Python has no increment/decrement operator: %s%sx = %s(%sx) = x" % ((operator,)*4), 5)
5628 5629
    return unop_node_classes[operator](pos,
        operator = operator,
William Stein's avatar
William Stein committed
5630 5631 5632
        operand = operand)


5633
class TypecastNode(ExprNode):
William Stein's avatar
William Stein committed
5634 5635
    #  C type cast
    #
5636
    #  operand      ExprNode
William Stein's avatar
William Stein committed
5637 5638
    #  base_type    CBaseTypeNode
    #  declarator   CDeclaratorNode
5639 5640 5641
    #
    #  If used from a transform, one can if wanted specify the attribute
    #  "type" directly and leave base_type and declarator to None
5642

William Stein's avatar
William Stein committed
5643
    subexprs = ['operand']
5644
    base_type = declarator = type = None
5645

Robert Bradshaw's avatar
Robert Bradshaw committed
5646
    def type_dependencies(self, env):
5647
        return ()
5648

Robert Bradshaw's avatar
Robert Bradshaw committed
5649
    def infer_type(self, env):
5650 5651 5652 5653
        if self.type is None:
            base_type = self.base_type.analyse(env)
            _, self.type = self.declarator.analyse(base_type, env)
        return self.type
5654

William Stein's avatar
William Stein committed
5655
    def analyse_types(self, env):
5656 5657 5658
        if self.type is None:
            base_type = self.base_type.analyse(env)
            _, self.type = self.declarator.analyse(base_type, env)
5659 5660 5661 5662
        if self.type.is_cfunction:
            error(self.pos,
                "Cannot cast to a function type")
            self.type = PyrexTypes.error_type
William Stein's avatar
William Stein committed
5663 5664 5665
        self.operand.analyse_types(env)
        to_py = self.type.is_pyobject
        from_py = self.operand.type.is_pyobject
5666 5667
        if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
            error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
William Stein's avatar
William Stein committed
5668
        if to_py and not from_py:
5669 5670 5671 5672 5673 5674
            if self.type is bytes_type and self.operand.type.is_int:
                # FIXME: the type cast node isn't needed in this case
                # and can be dropped once analyse_types() can return a
                # different node
                self.operand = CoerceIntToBytesNode(self.operand, env)
            elif self.operand.type.can_coerce_to_pyobject(env):
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
5675
                self.result_ctype = py_object_type
5676
                self.operand = self.operand.coerce_to_pyobject(env)
5677
            else:
5678 5679 5680 5681
                if self.operand.type.is_ptr:
                    if not (self.operand.type.base_type.is_void or self.operand.type.base_type.is_struct):
                        error(self.pos, "Python objects cannot be cast from pointers of primitive types")
                else:
5682
                    # Should this be an error?
5683
                    warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.operand.type, self.type))
5684
                self.operand = self.operand.coerce_to_simple(env)
5685
        elif from_py and not to_py:
5686
            if self.type.create_from_py_utility_code(env):
5687
                self.operand = self.operand.coerce_to(self.type, env)
5688 5689 5690
            elif self.type.is_ptr:
                if not (self.type.base_type.is_void or self.type.base_type.is_struct):
                    error(self.pos, "Python objects cannot be cast to pointers of primitive types")
5691 5692
            else:
                warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
5693 5694
        elif from_py and to_py:
            if self.typecheck and self.type.is_extension_type:
5695
                self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True)
5696 5697
        elif self.type.is_complex and self.operand.type.is_complex:
            self.operand = self.operand.coerce_to_simple(env)
5698

Stefan Behnel's avatar
Stefan Behnel committed
5699 5700 5701 5702
    def is_simple(self):
        # either temp or a C cast => no side effects
        return True

5703 5704 5705
    def nonlocally_immutable(self):
        return self.operand.nonlocally_immutable()

5706 5707 5708
    def nogil_check(self, env):
        if self.type and self.type.is_pyobject and self.is_temp:
            self.gil_error()
5709

William Stein's avatar
William Stein committed
5710
    def check_const(self):
5711
        return self.operand.check_const()
Stefan Behnel's avatar
Stefan Behnel committed
5712 5713

    def calculate_constant_result(self):
5714 5715 5716
        # we usually do not know the result of a type cast at code
        # generation time
        pass
5717

William Stein's avatar
William Stein committed
5718
    def calculate_result_code(self):
5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729
        if self.type.is_complex:
            operand_result = self.operand.result()
            if self.operand.type.is_complex:
                real_part = self.type.real_type.cast_code("__Pyx_CREAL(%s)" % operand_result)
                imag_part = self.type.real_type.cast_code("__Pyx_CIMAG(%s)" % operand_result)
            else:
                real_part = self.type.real_type.cast_code(operand_result)
                imag_part = "0"
            return "%s(%s, %s)" % (
                    self.type.from_parts,
                    real_part,
5730
                    imag_part)
5731 5732
        else:
            return self.type.cast_code(self.operand.result())
5733

5734 5735 5736 5737
    def get_constant_c_result_code(self):
        operand_result = self.operand.get_constant_c_result_code()
        if operand_result:
            return self.type.cast_code(operand_result)
5738

William Stein's avatar
William Stein committed
5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749
    def result_as(self, type):
        if self.type.is_pyobject and not self.is_temp:
            #  Optimise away some unnecessary casting
            return self.operand.result_as(type)
        else:
            return ExprNode.result_as(self, type)

    def generate_result_code(self, code):
        if self.is_temp:
            code.putln(
                "%s = (PyObject *)%s;" % (
5750 5751 5752
                    self.result(),
                    self.operand.result()))
            code.put_incref(self.result(), self.ctype())
William Stein's avatar
William Stein committed
5753 5754


5755
class SizeofNode(ExprNode):
William Stein's avatar
William Stein committed
5756
    #  Abstract base class for sizeof(x) expression nodes.
5757

5758
    type = PyrexTypes.c_size_t_type
William Stein's avatar
William Stein committed
5759 5760

    def check_const(self):
5761
        return True
William Stein's avatar
William Stein committed
5762 5763 5764 5765 5766 5767 5768 5769 5770 5771

    def generate_result_code(self, code):
        pass


class SizeofTypeNode(SizeofNode):
    #  C sizeof function applied to a type
    #
    #  base_type   CBaseTypeNode
    #  declarator  CDeclaratorNode
5772

William Stein's avatar
William Stein committed
5773
    subexprs = []
5774
    arg_type = None
5775

William Stein's avatar
William Stein committed
5776
    def analyse_types(self, env):
5777 5778
        # we may have incorrectly interpreted a dotted name as a type rather than an attribute
        # this could be better handled by more uniformly treating types as runtime-available objects
5779
        if 0 and self.base_type.module_path:
5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790
            path = self.base_type.module_path
            obj = env.lookup(path[0])
            if obj.as_module is None:
                operand = NameNode(pos=self.pos, name=path[0])
                for attr in path[1:]:
                    operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr)
                operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name)
                self.operand = operand
                self.__class__ = SizeofVarNode
                self.analyse_types(env)
                return
5791 5792 5793 5794
        if self.arg_type is None:
            base_type = self.base_type.analyse(env)
            _, arg_type = self.declarator.analyse(base_type, env)
            self.arg_type = arg_type
5795
        self.check_type()
5796

5797 5798
    def check_type(self):
        arg_type = self.arg_type
5799
        if arg_type.is_pyobject and not arg_type.is_extension_type:
William Stein's avatar
William Stein committed
5800 5801 5802 5803 5804
            error(self.pos, "Cannot take sizeof Python object")
        elif arg_type.is_void:
            error(self.pos, "Cannot take sizeof void")
        elif not arg_type.is_complete():
            error(self.pos, "Cannot take sizeof incomplete type '%s'" % arg_type)
5805

William Stein's avatar
William Stein committed
5806
    def calculate_result_code(self):
5807 5808 5809 5810 5811 5812
        if self.arg_type.is_extension_type:
            # the size of the pointer is boring
            # we want the size of the actual struct
            arg_code = self.arg_type.declaration_code("", deref=1)
        else:
            arg_code = self.arg_type.declaration_code("")
William Stein's avatar
William Stein committed
5813
        return "(sizeof(%s))" % arg_code
5814

William Stein's avatar
William Stein committed
5815 5816 5817 5818 5819

class SizeofVarNode(SizeofNode):
    #  C sizeof function applied to a variable
    #
    #  operand   ExprNode
5820

William Stein's avatar
William Stein committed
5821
    subexprs = ['operand']
5822

William Stein's avatar
William Stein committed
5823
    def analyse_types(self, env):
5824 5825 5826 5827 5828 5829 5830 5831 5832
        # We may actually be looking at a type rather than a variable...
        # If we are, traditional analysis would fail...
        operand_as_type = self.operand.analyse_as_type(env)
        if operand_as_type:
            self.arg_type = operand_as_type
            self.__class__ = SizeofTypeNode
            self.check_type()
        else:
            self.operand.analyse_types(env)
5833

William Stein's avatar
William Stein committed
5834
    def calculate_result_code(self):
5835
        return "(sizeof(%s))" % self.operand.result()
5836

William Stein's avatar
William Stein committed
5837 5838 5839
    def generate_result_code(self, code):
        pass

Robert Bradshaw's avatar
Robert Bradshaw committed
5840
class TypeofNode(ExprNode):
5841 5842 5843
    #  Compile-time type of an expression, as a string.
    #
    #  operand   ExprNode
Robert Bradshaw's avatar
Robert Bradshaw committed
5844
    #  literal   StringNode # internal
5845

Robert Bradshaw's avatar
Robert Bradshaw committed
5846 5847
    literal = None
    type = py_object_type
5848

Stefan Behnel's avatar
Stefan Behnel committed
5849
    subexprs = ['literal'] # 'operand' will be ignored after type analysis!
5850

5851 5852
    def analyse_types(self, env):
        self.operand.analyse_types(env)
5853 5854
        self.literal = StringNode(
            self.pos, value=StringEncoding.EncodedString(str(self.operand.type)))
Robert Bradshaw's avatar
Robert Bradshaw committed
5855 5856
        self.literal.analyse_types(env)
        self.literal = self.literal.coerce_to_pyobject(env)
5857 5858 5859 5860

    def may_be_none(self):
        return False

5861
    def generate_evaluation_code(self, code):
Robert Bradshaw's avatar
Robert Bradshaw committed
5862
        self.literal.generate_evaluation_code(code)
5863

Robert Bradshaw's avatar
Robert Bradshaw committed
5864 5865
    def calculate_result_code(self):
        return self.literal.calculate_result_code()
William Stein's avatar
William Stein committed
5866 5867 5868 5869 5870 5871 5872

#-------------------------------------------------------------------
#
#  Binary operator nodes
#
#-------------------------------------------------------------------

Stefan Behnel's avatar
Stefan Behnel committed
5873 5874 5875
def _not_in(x, seq):
    return x not in seq

5876 5877 5878
compile_time_binary_operators = {
    '<': operator.lt,
    '<=': operator.le,
5879
    '==': operator.eq,
5880 5881 5882 5883 5884 5885 5886
    '!=': operator.ne,
    '>=': operator.ge,
    '>': operator.gt,
    'is': operator.is_,
    'is_not': operator.is_not,
    '+': operator.add,
    '&': operator.and_,
5887
    '/': operator.truediv,
5888 5889 5890 5891 5892 5893 5894 5895 5896
    '//': operator.floordiv,
    '<<': operator.lshift,
    '%': operator.mod,
    '*': operator.mul,
    '|': operator.or_,
    '**': operator.pow,
    '>>': operator.rshift,
    '-': operator.sub,
    '^': operator.xor,
Stefan Behnel's avatar
Stefan Behnel committed
5897 5898
    'in': operator.contains,
    'not_in': _not_in,
5899 5900 5901 5902 5903 5904 5905
}

def get_compile_time_binop(node):
    func = compile_time_binary_operators.get(node.operator)
    if not func:
        error(node.pos,
            "Binary '%s' not supported in compile-time expression"
5906
                % node.operator)
5907 5908
    return func

5909
class BinopNode(ExprNode):
William Stein's avatar
William Stein committed
5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920
    #  operator     string
    #  operand1     ExprNode
    #  operand2     ExprNode
    #
    #  Processing during analyse_expressions phase:
    #
    #    analyse_c_operation
    #      Called when neither operand is a pyobject.
    #      - Check operand types and coerce if needed.
    #      - Determine result type and result code fragment.
    #      - Allocate temporary for result if needed.
5921

William Stein's avatar
William Stein committed
5922
    subexprs = ['operand1', 'operand2']
5923
    inplace = False
5924 5925 5926 5927 5928 5929 5930

    def calculate_constant_result(self):
        func = compile_time_binary_operators[self.operator]
        self.constant_result = func(
            self.operand1.constant_result,
            self.operand2.constant_result)

5931 5932 5933 5934 5935 5936 5937 5938
    def compile_time_value(self, denv):
        func = get_compile_time_binop(self)
        operand1 = self.operand1.compile_time_value(denv)
        operand2 = self.operand2.compile_time_value(denv)
        try:
            return func(operand1, operand2)
        except Exception, e:
            self.compile_time_value_error(e)
5939

5940 5941
    def infer_type(self, env):
        return self.result_type(self.operand1.infer_type(env),
Robert Bradshaw's avatar
Robert Bradshaw committed
5942
                                self.operand2.infer_type(env))
5943

William Stein's avatar
William Stein committed
5944 5945 5946
    def analyse_types(self, env):
        self.operand1.analyse_types(env)
        self.operand2.analyse_types(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
5947
        self.analyse_operation(env)
5948

Robert Bradshaw's avatar
Robert Bradshaw committed
5949
    def analyse_operation(self, env):
William Stein's avatar
William Stein committed
5950 5951
        if self.is_py_operation():
            self.coerce_operands_to_pyobjects(env)
5952 5953 5954
            self.type = self.result_type(self.operand1.type,
                                         self.operand2.type)
            assert self.type.is_pyobject
William Stein's avatar
William Stein committed
5955
            self.is_temp = 1
DaniloFreitas's avatar
DaniloFreitas committed
5956 5957
        elif self.is_cpp_operation():
            self.analyse_cpp_operation(env)
William Stein's avatar
William Stein committed
5958 5959
        else:
            self.analyse_c_operation(env)
5960

William Stein's avatar
William Stein committed
5961
    def is_py_operation(self):
5962
        return self.is_py_operation_types(self.operand1.type, self.operand2.type)
5963

5964 5965 5966
    def is_py_operation_types(self, type1, type2):
        return type1.is_pyobject or type2.is_pyobject

DaniloFreitas's avatar
DaniloFreitas committed
5967
    def is_cpp_operation(self):
Robert Bradshaw's avatar
Robert Bradshaw committed
5968 5969
        return (self.operand1.type.is_cpp_class
            or self.operand2.type.is_cpp_class)
5970

5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986
    def analyse_cpp_operation(self, env):
        type1 = self.operand1.type
        type2 = self.operand2.type
        entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
        if not entry:
            self.type_error()
            return
        func_type = entry.type
        if func_type.is_ptr:
            func_type = func_type.base_type
        if len(func_type.args) == 1:
            self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
        else:
            self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
            self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
        self.type = func_type.return_type
5987

5988 5989
    def result_type(self, type1, type2):
        if self.is_py_operation_types(type1, type2):
5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007
            if type2.is_string:
                type2 = Builtin.bytes_type
            if type1.is_string:
                type1 = Builtin.bytes_type
            elif self.operator == '%' \
                     and type1 in (Builtin.str_type, Builtin.unicode_type):
                # note that  b'%s' % b'abc'  doesn't work in Py3
                return type1
            if type1.is_builtin_type:
                if type1 is type2:
                    if self.operator in '**%+|&^':
                        # FIXME: at least these operators should be safe - others?
                        return type1
                elif self.operator == '*':
                    if type1 in (Builtin.bytes_type, Builtin.str_type, Builtin.unicode_type):
                        return type1
                    # multiplication of containers/numbers with an
                    # integer value always (?) returns the same type
6008
                    if type2.is_int:
6009
                        return type1
6010 6011 6012 6013
            elif type2.is_builtin_type and type1.is_int and self.operator == '*':
                # multiplication of containers/numbers with an
                # integer value always (?) returns the same type
                return type2
6014 6015 6016
            return py_object_type
        else:
            return self.compute_c_result_type(type1, type2)
6017

6018
    def nogil_check(self, env):
6019
        if self.is_py_operation():
6020
            self.gil_error()
6021

William Stein's avatar
William Stein committed
6022 6023 6024
    def coerce_operands_to_pyobjects(self, env):
        self.operand1 = self.operand1.coerce_to_pyobject(env)
        self.operand2 = self.operand2.coerce_to_pyobject(env)
6025

William Stein's avatar
William Stein committed
6026
    def check_const(self):
6027
        return self.operand1.check_const() and self.operand2.check_const()
6028

William Stein's avatar
William Stein committed
6029 6030 6031 6032
    def generate_result_code(self, code):
        #print "BinopNode.generate_result_code:", self.operand1, self.operand2 ###
        if self.operand1.type.is_pyobject:
            function = self.py_operation_function()
6033
            if self.operator == '**':
William Stein's avatar
William Stein committed
6034 6035 6036 6037
                extra_args = ", Py_None"
            else:
                extra_args = ""
            code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
6038
                "%s = %s(%s, %s%s); %s" % (
6039 6040
                    self.result(),
                    function,
William Stein's avatar
William Stein committed
6041 6042 6043
                    self.operand1.py_result(),
                    self.operand2.py_result(),
                    extra_args,
6044
                    code.error_goto_if_null(self.result(), self.pos)))
6045
            code.put_gotref(self.py_result())
6046

William Stein's avatar
William Stein committed
6047 6048 6049 6050
    def type_error(self):
        if not (self.operand1.type.is_error
                or self.operand2.type.is_error):
            error(self.pos, "Invalid operand types for '%s' (%s; %s)" %
6051
                (self.operator, self.operand1.type,
William Stein's avatar
William Stein committed
6052 6053 6054 6055
                    self.operand2.type))
        self.type = PyrexTypes.error_type


Robert Bradshaw's avatar
Robert Bradshaw committed
6056
class CBinopNode(BinopNode):
6057

Robert Bradshaw's avatar
Robert Bradshaw committed
6058 6059 6060 6061
    def analyse_types(self, env):
        BinopNode.analyse_types(self, env)
        if self.is_py_operation():
            self.type = PyrexTypes.error_type
6062

Robert Bradshaw's avatar
Robert Bradshaw committed
6063 6064
    def py_operation_function():
        return ""
6065

Robert Bradshaw's avatar
Robert Bradshaw committed
6066 6067
    def calculate_result_code(self):
        return "(%s %s %s)" % (
6068 6069
            self.operand1.result(),
            self.operator,
Robert Bradshaw's avatar
Robert Bradshaw committed
6070 6071 6072 6073 6074 6075 6076 6077
            self.operand2.result())


def c_binop_constructor(operator):
    def make_binop_node(pos, **operands):
        return CBinopNode(pos, operator=operator, **operands)
    return make_binop_node

William Stein's avatar
William Stein committed
6078 6079
class NumBinopNode(BinopNode):
    #  Binary operation taking numeric arguments.
6080

Robert Bradshaw's avatar
Robert Bradshaw committed
6081
    infix = True
6082

William Stein's avatar
William Stein committed
6083 6084 6085 6086 6087 6088
    def analyse_c_operation(self, env):
        type1 = self.operand1.type
        type2 = self.operand2.type
        self.type = self.compute_c_result_type(type1, type2)
        if not self.type:
            self.type_error()
6089
            return
6090
        if self.type.is_complex:
Robert Bradshaw's avatar
Robert Bradshaw committed
6091
            self.infix = False
6092
        if not self.infix or (type1.is_numeric and type2.is_numeric):
6093 6094
            self.operand1 = self.operand1.coerce_to(self.type, env)
            self.operand2 = self.operand2.coerce_to(self.type, env)
6095

William Stein's avatar
William Stein committed
6096 6097
    def compute_c_result_type(self, type1, type2):
        if self.c_types_okay(type1, type2):
6098 6099 6100 6101 6102
            widest_type = PyrexTypes.widest_numeric_type(type1, type2)
            if widest_type is PyrexTypes.c_bint_type:
                if self.operator not in '|^&':
                    # False + False == 0 # not False!
                    widest_type = PyrexTypes.c_int_type
6103 6104 6105
            else:
                widest_type = PyrexTypes.widest_numeric_type(
                    widest_type, PyrexTypes.c_int_type)
6106
            return widest_type
William Stein's avatar
William Stein committed
6107 6108
        else:
            return None
6109 6110 6111 6112 6113 6114 6115 6116

    def get_constant_c_result_code(self):
        value1 = self.operand1.get_constant_c_result_code()
        value2 = self.operand2.get_constant_c_result_code()
        if value1 and value2:
            return "(%s %s %s)" % (value1, self.operator, value2)
        else:
            return None
6117

William Stein's avatar
William Stein committed
6118
    def c_types_okay(self, type1, type2):
6119 6120 6121
        #print "NumBinopNode.c_types_okay:", type1, type2 ###
        return (type1.is_numeric  or type1.is_enum) \
            and (type2.is_numeric  or type2.is_enum)
William Stein's avatar
William Stein committed
6122 6123

    def calculate_result_code(self):
6124 6125
        if self.infix:
            return "(%s %s %s)" % (
6126 6127
                self.operand1.result(),
                self.operator,
6128 6129
                self.operand2.result())
        else:
6130 6131 6132
            func = self.type.binary_op(self.operator)
            if func is None:
                error(self.pos, "binary operator %s not supported for %s" % (self.operator, self.type))
6133
            return "%s(%s, %s)" % (
6134
                func,
6135 6136
                self.operand1.result(),
                self.operand2.result())
6137

6138
    def is_py_operation_types(self, type1, type2):
Stefan Behnel's avatar
Stefan Behnel committed
6139 6140
        return (type1.is_unicode_char or
                type2.is_unicode_char or
6141
                BinopNode.is_py_operation_types(self, type1, type2))
6142

William Stein's avatar
William Stein committed
6143
    def py_operation_function(self):
6144 6145 6146 6147
        fuction = self.py_functions[self.operator]
        if self.inplace:
            fuction = fuction.replace('PyNumber_', 'PyNumber_InPlace')
        return fuction
William Stein's avatar
William Stein committed
6148 6149

    py_functions = {
Robert Bradshaw's avatar
Robert Bradshaw committed
6150 6151 6152
        "|":        "PyNumber_Or",
        "^":        "PyNumber_Xor",
        "&":        "PyNumber_And",
6153 6154
        "<<":       "PyNumber_Lshift",
        ">>":       "PyNumber_Rshift",
Robert Bradshaw's avatar
Robert Bradshaw committed
6155 6156 6157 6158
        "+":        "PyNumber_Add",
        "-":        "PyNumber_Subtract",
        "*":        "PyNumber_Multiply",
        "/":        "__Pyx_PyNumber_Divide",
6159
        "//":       "PyNumber_FloorDivide",
Robert Bradshaw's avatar
Robert Bradshaw committed
6160
        "%":        "PyNumber_Remainder",
6161
        "**":       "PyNumber_Power"
William Stein's avatar
William Stein committed
6162 6163 6164 6165
    }

class IntBinopNode(NumBinopNode):
    #  Binary operation taking integer arguments.
6166

William Stein's avatar
William Stein committed
6167
    def c_types_okay(self, type1, type2):
6168 6169 6170
        #print "IntBinopNode.c_types_okay:", type1, type2 ###
        return (type1.is_int or type1.is_enum) \
            and (type2.is_int or type2.is_enum)
William Stein's avatar
William Stein committed
6171

6172

William Stein's avatar
William Stein committed
6173 6174
class AddNode(NumBinopNode):
    #  '+' operator.
6175

6176 6177 6178
    def is_py_operation_types(self, type1, type2):
        if type1.is_string and type2.is_string:
            return 1
William Stein's avatar
William Stein committed
6179
        else:
6180
            return NumBinopNode.is_py_operation_types(self, type1, type2)
William Stein's avatar
William Stein committed
6181 6182

    def compute_c_result_type(self, type1, type2):
6183 6184
        #print "AddNode.compute_c_result_type:", type1, self.operator, type2 ###
        if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
William Stein's avatar
William Stein committed
6185
            return type1
6186
        elif (type2.is_ptr or type2.is_array) and (type1.is_int or type1.is_enum):
William Stein's avatar
William Stein committed
6187 6188 6189 6190 6191 6192 6193 6194
            return type2
        else:
            return NumBinopNode.compute_c_result_type(
                self, type1, type2)


class SubNode(NumBinopNode):
    #  '-' operator.
6195

William Stein's avatar
William Stein committed
6196
    def compute_c_result_type(self, type1, type2):
6197
        if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
William Stein's avatar
William Stein committed
6198
            return type1
6199
        elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
William Stein's avatar
William Stein committed
6200 6201 6202 6203 6204 6205 6206 6207
            return PyrexTypes.c_int_type
        else:
            return NumBinopNode.compute_c_result_type(
                self, type1, type2)


class MulNode(NumBinopNode):
    #  '*' operator.
6208

6209
    def is_py_operation_types(self, type1, type2):
William Stein's avatar
William Stein committed
6210 6211 6212 6213
        if (type1.is_string and type2.is_int) \
            or (type2.is_string and type1.is_int):
                return 1
        else:
6214
            return NumBinopNode.is_py_operation_types(self, type1, type2)
William Stein's avatar
William Stein committed
6215 6216


6217 6218
class DivNode(NumBinopNode):
    #  '/' or '//' operator.
6219

6220
    cdivision = None
6221 6222
    truedivision = None   # == "unknown" if operator == '/'
    ctruedivision = False
Robert Bradshaw's avatar
Robert Bradshaw committed
6223
    cdivision_warnings = False
6224
    zerodivision_check = None
6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246

    def find_compile_time_binary_operator(self, op1, op2):
        func = compile_time_binary_operators[self.operator]
        if self.operator == '/' and self.truedivision is None:
            # => true div for floats, floor div for integers
            if isinstance(op1, (int,long)) and isinstance(op2, (int,long)):
                func = compile_time_binary_operators['//']
        return func

    def calculate_constant_result(self):
        op1 = self.operand1.constant_result
        op2 = self.operand2.constant_result
        func = self.find_compile_time_binary_operator(op1, op2)
        self.constant_result = func(
            self.operand1.constant_result,
            self.operand2.constant_result)

    def compile_time_value(self, denv):
        operand1 = self.operand1.compile_time_value(denv)
        operand2 = self.operand2.compile_time_value(denv)
        try:
            func = self.find_compile_time_binary_operator(
Robert Bradshaw's avatar
Robert Bradshaw committed
6247
                operand1, operand2)
6248 6249 6250 6251
            return func(operand1, operand2)
        except Exception, e:
            self.compile_time_value_error(e)

Robert Bradshaw's avatar
Robert Bradshaw committed
6252
    def analyse_operation(self, env):
6253 6254 6255 6256
        if self.cdivision or env.directives['cdivision']:
            self.ctruedivision = False
        else:
            self.ctruedivision = self.truedivision
Robert Bradshaw's avatar
Robert Bradshaw committed
6257
        NumBinopNode.analyse_operation(self, env)
6258 6259
        if self.is_cpp_operation():
            self.cdivision = True
6260
        if not self.type.is_pyobject:
6261 6262
            self.zerodivision_check = (
                self.cdivision is None and not env.directives['cdivision']
6263
                and (not self.operand2.has_constant_result() or
6264
                     self.operand2.constant_result == 0))
6265 6266 6267 6268
            if self.zerodivision_check or env.directives['cdivision_warnings']:
                # Need to check ahead of time to warn or raise zero division error
                self.operand1 = self.operand1.coerce_to_simple(env)
                self.operand2 = self.operand2.coerce_to_simple(env)
6269 6270
                if env.nogil:
                    error(self.pos, "Pythonic division not allowed without gil, consider using cython.cdivision(True)")
6271 6272 6273 6274 6275 6276 6277 6278 6279

    def compute_c_result_type(self, type1, type2):
        if self.operator == '/' and self.ctruedivision:
            if not type1.is_float and not type2.is_float:
                widest_type = PyrexTypes.widest_numeric_type(type1, PyrexTypes.c_double_type)
                widest_type = PyrexTypes.widest_numeric_type(type2, widest_type)
                return widest_type
        return NumBinopNode.compute_c_result_type(self, type1, type2)

6280 6281 6282 6283 6284
    def zero_division_message(self):
        if self.type.is_int:
            return "integer division or modulo by zero"
        else:
            return "float division"
Robert Bradshaw's avatar
Robert Bradshaw committed
6285

6286
    def generate_evaluation_code(self, code):
6287
        if not self.type.is_pyobject and not self.type.is_complex:
6288
            if self.cdivision is None:
6289
                self.cdivision = (code.globalstate.directives['cdivision']
6290 6291 6292
                                    or not self.type.signed
                                    or self.type.is_float)
            if not self.cdivision:
6293
                code.globalstate.use_utility_code(div_int_utility_code.specialize(self.type))
6294
        NumBinopNode.generate_evaluation_code(self, code)
6295
        self.generate_div_warning_code(code)
6296

6297
    def generate_div_warning_code(self, code):
6298 6299
        if not self.type.is_pyobject:
            if self.zerodivision_check:
6300 6301 6302 6303 6304
                if not self.infix:
                    zero_test = "%s(%s)" % (self.type.unary_op('zero'), self.operand2.result())
                else:
                    zero_test = "%s == 0" % self.operand2.result()
                code.putln("if (unlikely(%s)) {" % zero_test)
6305 6306 6307
                code.putln('PyErr_Format(PyExc_ZeroDivisionError, "%s");' % self.zero_division_message())
                code.putln(code.error_goto(self.pos))
                code.putln("}")
6308 6309 6310
                if self.type.is_int and self.type.signed and self.operator != '%':
                    code.globalstate.use_utility_code(division_overflow_test_code)
                    code.putln("else if (sizeof(%s) == sizeof(long) && unlikely(%s == -1) && unlikely(UNARY_NEG_WOULD_OVERFLOW(%s))) {" % (
6311
                                    self.type.declaration_code(''),
6312 6313 6314 6315 6316
                                    self.operand2.result(),
                                    self.operand1.result()))
                    code.putln('PyErr_Format(PyExc_OverflowError, "value too large to perform division");')
                    code.putln(code.error_goto(self.pos))
                    code.putln("}")
Robert Bradshaw's avatar
Robert Bradshaw committed
6317
            if code.globalstate.directives['cdivision_warnings'] and self.operator != '/':
6318 6319 6320 6321 6322
                code.globalstate.use_utility_code(cdivision_warning_utility_code)
                code.putln("if ((%s < 0) ^ (%s < 0)) {" % (
                                self.operand1.result(),
                                self.operand2.result()))
                code.putln(code.set_error_info(self.pos));
6323 6324 6325 6326 6327 6328
                code.put("if (__Pyx_cdivision_warning(%(FILENAME)s, "
                                                     "%(LINENO)s)) " % {
                    'FILENAME': Naming.filename_cname,
                    'LINENO':  Naming.lineno_cname,
                    })

6329 6330
                code.put_goto(code.error_label)
                code.putln("}")
6331

Robert Bradshaw's avatar
Robert Bradshaw committed
6332
    def calculate_result_code(self):
6333 6334 6335
        if self.type.is_complex:
            return NumBinopNode.calculate_result_code(self)
        elif self.type.is_float and self.operator == '//':
6336
            return "floor(%s / %s)" % (
6337
                self.operand1.result(),
6338
                self.operand2.result())
6339 6340 6341 6342 6343 6344 6345 6346 6347
        elif self.truedivision or self.cdivision:
            op1 = self.operand1.result()
            op2 = self.operand2.result()
            if self.truedivision:
                if self.type != self.operand1.type:
                    op1 = self.type.cast_code(op1)
                if self.type != self.operand2.type:
                    op2 = self.type.cast_code(op2)
            return "(%s / %s)" % (op1, op2)
6348 6349
        else:
            return "__Pyx_div_%s(%s, %s)" % (
Craig Citro's avatar
Craig Citro committed
6350
                    self.type.specialization_name(),
6351
                    self.operand1.result(),
6352
                    self.operand2.result())
Robert Bradshaw's avatar
Robert Bradshaw committed
6353 6354


Robert Bradshaw's avatar
Robert Bradshaw committed
6355
class ModNode(DivNode):
William Stein's avatar
William Stein committed
6356
    #  '%' operator.
6357

6358 6359 6360 6361
    def is_py_operation_types(self, type1, type2):
        return (type1.is_string
            or type2.is_string
            or NumBinopNode.is_py_operation_types(self, type1, type2))
William Stein's avatar
William Stein committed
6362

6363 6364 6365 6366 6367
    def zero_division_message(self):
        if self.type.is_int:
            return "integer division or modulo by zero"
        else:
            return "float divmod()"
6368

6369
    def generate_evaluation_code(self, code):
6370 6371 6372 6373 6374
        if not self.type.is_pyobject:
            if self.cdivision is None:
                self.cdivision = code.globalstate.directives['cdivision'] or not self.type.signed
            if not self.cdivision:
                if self.type.is_int:
6375
                    code.globalstate.use_utility_code(mod_int_utility_code.specialize(self.type))
6376
                else:
6377 6378
                    code.globalstate.use_utility_code(
                        mod_float_utility_code.specialize(self.type, math_h_modifier=self.type.math_h_modifier))
6379
        NumBinopNode.generate_evaluation_code(self, code)
6380
        self.generate_div_warning_code(code)
6381

Robert Bradshaw's avatar
Robert Bradshaw committed
6382
    def calculate_result_code(self):
6383 6384 6385 6386
        if self.cdivision:
            if self.type.is_float:
                return "fmod%s(%s, %s)" % (
                    self.type.math_h_modifier,
6387
                    self.operand1.result(),
6388 6389 6390
                    self.operand2.result())
            else:
                return "(%s %% %s)" % (
6391
                    self.operand1.result(),
6392
                    self.operand2.result())
Robert Bradshaw's avatar
Robert Bradshaw committed
6393
        else:
6394
            return "__Pyx_mod_%s(%s, %s)" % (
Craig Citro's avatar
Craig Citro committed
6395
                    self.type.specialization_name(),
6396
                    self.operand1.result(),
6397
                    self.operand2.result())
William Stein's avatar
William Stein committed
6398 6399 6400

class PowNode(NumBinopNode):
    #  '**' operator.
6401

Robert Bradshaw's avatar
Robert Bradshaw committed
6402 6403
    def analyse_c_operation(self, env):
        NumBinopNode.analyse_c_operation(self, env)
6404
        if self.type.is_complex:
Robert Bradshaw's avatar
Robert Bradshaw committed
6405 6406 6407 6408 6409 6410 6411
            if self.type.real_type.is_float:
                self.operand1 = self.operand1.coerce_to(self.type, env)
                self.operand2 = self.operand2.coerce_to(self.type, env)
                self.pow_func = "__Pyx_c_pow" + self.type.real_type.math_h_modifier
            else:
                error(self.pos, "complex int powers not supported")
                self.pow_func = "<error>"
6412
        elif self.type.is_float:
6413
            self.pow_func = "pow" + self.type.math_h_modifier
William Stein's avatar
William Stein committed
6414
        else:
Robert Bradshaw's avatar
Robert Bradshaw committed
6415 6416
            self.pow_func = "__Pyx_pow_%s" % self.type.declaration_code('').replace(' ', '_')
            env.use_utility_code(
6417
                    int_pow_utility_code.specialize(func_name=self.pow_func,
Robert Bradshaw's avatar
Robert Bradshaw committed
6418
                                                type=self.type.declaration_code('')))
6419

William Stein's avatar
William Stein committed
6420
    def calculate_result_code(self):
6421 6422 6423 6424 6425 6426
        # Work around MSVC overloading ambiguity.
        def typecast(operand):
            if self.type == operand.type:
                return operand.result()
            else:
                return self.type.cast_code(operand.result())
Robert Bradshaw's avatar
Robert Bradshaw committed
6427
        return "%s(%s, %s)" % (
6428 6429
            self.pow_func,
            typecast(self.operand1),
6430
            typecast(self.operand2))
6431

William Stein's avatar
William Stein committed
6432

Craig Citro's avatar
Craig Citro committed
6433
# Note: This class is temporarily "shut down" into an ineffective temp
6434 6435
# allocation mode.
#
Craig Citro's avatar
Craig Citro committed
6436 6437 6438
# More sophisticated temp reuse was going on before, one could have a
# look at adding this again after /all/ classes are converted to the
# new temp scheme. (The temp juggling cannot work otherwise).
6439
class BoolBinopNode(ExprNode):
William Stein's avatar
William Stein committed
6440 6441 6442 6443 6444
    #  Short-circuiting boolean operation.
    #
    #  operator     string
    #  operand1     ExprNode
    #  operand2     ExprNode
6445

6446
    subexprs = ['operand1', 'operand2']
6447

6448
    def infer_type(self, env):
6449 6450
        type1 = self.operand1.infer_type(env)
        type2 = self.operand2.infer_type(env)
6451
        return PyrexTypes.independent_spanning_type(type1, type2)
6452

Stefan Behnel's avatar
Stefan Behnel committed
6453 6454 6455 6456 6457 6458
    def may_be_none(self):
        if self.operator == 'or':
            return self.operand2.may_be_none()
        else:
            return self.operand1.may_be_none() or self.operand2.may_be_none()

6459 6460 6461 6462 6463 6464 6465 6466 6467
    def calculate_constant_result(self):
        if self.operator == 'and':
            self.constant_result = \
                self.operand1.constant_result and \
                self.operand2.constant_result
        else:
            self.constant_result = \
                self.operand1.constant_result or \
                self.operand2.constant_result
6468

6469 6470 6471 6472 6473 6474 6475
    def compile_time_value(self, denv):
        if self.operator == 'and':
            return self.operand1.compile_time_value(denv) \
                and self.operand2.compile_time_value(denv)
        else:
            return self.operand1.compile_time_value(denv) \
                or self.operand2.compile_time_value(denv)
6476

6477
    def coerce_to_boolean(self, env):
6478 6479 6480 6481 6482 6483 6484
        return BoolBinopNode(
            self.pos,
            operator = self.operator,
            operand1 = self.operand1.coerce_to_boolean(env),
            operand2 = self.operand2.coerce_to_boolean(env),
            type = PyrexTypes.c_bint_type,
            is_temp = self.is_temp)
6485

William Stein's avatar
William Stein committed
6486 6487 6488
    def analyse_types(self, env):
        self.operand1.analyse_types(env)
        self.operand2.analyse_types(env)
6489
        self.type = PyrexTypes.independent_spanning_type(self.operand1.type, self.operand2.type)
6490 6491
        self.operand1 = self.operand1.coerce_to(self.type, env)
        self.operand2 = self.operand2.coerce_to(self.type, env)
6492

William Stein's avatar
William Stein committed
6493 6494
        # For what we're about to do, it's vital that
        # both operands be temp nodes.
6495 6496
        self.operand1 = self.operand1.coerce_to_simple(env)
        self.operand2 = self.operand2.coerce_to_simple(env)
William Stein's avatar
William Stein committed
6497
        self.is_temp = 1
6498 6499 6500

    gil_message = "Truth-testing Python object"

William Stein's avatar
William Stein committed
6501
    def check_const(self):
6502
        return self.operand1.check_const() and self.operand2.check_const()
6503

William Stein's avatar
William Stein committed
6504
    def generate_evaluation_code(self, code):
6505
        code.mark_pos(self.pos)
William Stein's avatar
William Stein committed
6506
        self.operand1.generate_evaluation_code(code)
6507
        test_result, uses_temp = self.generate_operand1_test(code)
William Stein's avatar
William Stein committed
6508 6509 6510 6511 6512 6513 6514 6515
        if self.operator == 'and':
            sense = ""
        else:
            sense = "!"
        code.putln(
            "if (%s%s) {" % (
                sense,
                test_result))
6516 6517
        if uses_temp:
            code.funcstate.release_temp(test_result)
6518
        self.operand1.generate_disposal_code(code)
William Stein's avatar
William Stein committed
6519
        self.operand2.generate_evaluation_code(code)
6520
        self.allocate_temp_result(code)
6521
        self.operand2.make_owned_reference(code)
6522
        code.putln("%s = %s;" % (self.result(), self.operand2.result()))
6523 6524
        self.operand2.generate_post_assignment_code(code)
        self.operand2.free_temps(code)
6525
        code.putln("} else {")
6526
        self.operand1.make_owned_reference(code)
6527
        code.putln("%s = %s;" % (self.result(), self.operand1.result()))
6528 6529
        self.operand1.generate_post_assignment_code(code)
        self.operand1.free_temps(code)
6530
        code.putln("}")
6531

William Stein's avatar
William Stein committed
6532 6533 6534
    def generate_operand1_test(self, code):
        #  Generate code to test the truth of the first operand.
        if self.type.is_pyobject:
6535 6536
            test_result = code.funcstate.allocate_temp(PyrexTypes.c_bint_type,
                                                       manage_ref=False)
William Stein's avatar
William Stein committed
6537
            code.putln(
6538
                "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
William Stein's avatar
William Stein committed
6539 6540
                    test_result,
                    self.operand1.py_result(),
Robert Bradshaw's avatar
Robert Bradshaw committed
6541
                    code.error_goto_if_neg(test_result, self.pos)))
William Stein's avatar
William Stein committed
6542
        else:
6543
            test_result = self.operand1.result()
6544
        return (test_result, self.type.is_pyobject)
William Stein's avatar
William Stein committed
6545 6546


6547
class CondExprNode(ExprNode):
Robert Bradshaw's avatar
Robert Bradshaw committed
6548 6549 6550 6551 6552
    #  Short-circuiting conditional expression.
    #
    #  test        ExprNode
    #  true_val    ExprNode
    #  false_val   ExprNode
6553

6554 6555
    true_val = None
    false_val = None
6556

Robert Bradshaw's avatar
Robert Bradshaw committed
6557
    subexprs = ['test', 'true_val', 'false_val']
6558

Robert Bradshaw's avatar
Robert Bradshaw committed
6559 6560
    def type_dependencies(self, env):
        return self.true_val.type_dependencies(env) + self.false_val.type_dependencies(env)
6561

Robert Bradshaw's avatar
Robert Bradshaw committed
6562
    def infer_type(self, env):
6563 6564
        return PyrexTypes.independent_spanning_type(self.true_val.infer_type(env),
                                                    self.false_val.infer_type(env))
6565 6566 6567 6568 6569 6570 6571

    def calculate_constant_result(self):
        if self.test.constant_result:
            self.constant_result = self.true_val.constant_result
        else:
            self.constant_result = self.false_val.constant_result

Robert Bradshaw's avatar
Robert Bradshaw committed
6572 6573 6574 6575 6576
    def analyse_types(self, env):
        self.test.analyse_types(env)
        self.test = self.test.coerce_to_boolean(env)
        self.true_val.analyse_types(env)
        self.false_val.analyse_types(env)
6577
        self.type = PyrexTypes.independent_spanning_type(self.true_val.type, self.false_val.type)
6578 6579 6580 6581 6582
        if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject:
            self.true_val = self.true_val.coerce_to(self.type, env)
            self.false_val = self.false_val.coerce_to(self.type, env)
        self.is_temp = 1
        if self.type == PyrexTypes.error_type:
Robert Bradshaw's avatar
Robert Bradshaw committed
6583
            self.type_error()
6584

Robert Bradshaw's avatar
Robert Bradshaw committed
6585 6586 6587 6588 6589
    def type_error(self):
        if not (self.true_val.type.is_error or self.false_val.type.is_error):
            error(self.pos, "Incompatable types in conditional expression (%s; %s)" %
                (self.true_val.type, self.false_val.type))
        self.type = PyrexTypes.error_type
6590

Robert Bradshaw's avatar
Robert Bradshaw committed
6591
    def check_const(self):
6592
        return (self.test.check_const()
6593 6594
            and self.true_val.check_const()
            and self.false_val.check_const())
6595

Robert Bradshaw's avatar
Robert Bradshaw committed
6596
    def generate_evaluation_code(self, code):
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
6597 6598
        # Because subexprs may not be evaluated we can use a more optimal
        # subexpr allocation strategy than the default, so override evaluation_code.
6599

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
6600
        code.mark_pos(self.pos)
6601
        self.allocate_temp_result(code)
Robert Bradshaw's avatar
Robert Bradshaw committed
6602
        self.test.generate_evaluation_code(code)
6603
        code.putln("if (%s) {" % self.test.result() )
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
6604
        self.eval_and_get(code, self.true_val)
Robert Bradshaw's avatar
Robert Bradshaw committed
6605
        code.putln("} else {")
Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
6606
        self.eval_and_get(code, self.false_val)
Robert Bradshaw's avatar
Robert Bradshaw committed
6607 6608
        code.putln("}")
        self.test.generate_disposal_code(code)
6609
        self.test.free_temps(code)
Robert Bradshaw's avatar
Robert Bradshaw committed
6610

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
6611 6612 6613 6614 6615 6616 6617
    def eval_and_get(self, code, expr):
        expr.generate_evaluation_code(code)
        expr.make_owned_reference(code)
        code.putln("%s = %s;" % (self.result(), expr.result()))
        expr.generate_post_assignment_code(code)
        expr.free_temps(code)

6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
richcmp_constants = {
    "<" : "Py_LT",
    "<=": "Py_LE",
    "==": "Py_EQ",
    "!=": "Py_NE",
    "<>": "Py_NE",
    ">" : "Py_GT",
    ">=": "Py_GE",
}

6628
class CmpNode(object):
William Stein's avatar
William Stein committed
6629 6630
    #  Mixin class containing code common to PrimaryCmpNodes
    #  and CascadedCmpNodes.
6631 6632 6633

    special_bool_cmp_function = None

Stefan Behnel's avatar
typo  
Stefan Behnel committed
6634
    def infer_type(self, env):
6635 6636
        # TODO: Actually implement this (after merging with -unstable).
        return py_object_type
6637 6638 6639 6640 6641

    def calculate_cascaded_constant_result(self, operand1_result):
        func = compile_time_binary_operators[self.operator]
        operand2_result = self.operand2.constant_result
        result = func(operand1_result, operand2_result)
6642 6643 6644 6645 6646 6647 6648
        if self.cascade:
            self.cascade.calculate_cascaded_constant_result(operand2_result)
            if self.cascade.constant_result:
                self.constant_result = result and self.cascade.constant_result
        else:
            self.constant_result = result

6649 6650
    def cascaded_compile_time_value(self, operand1, denv):
        func = get_compile_time_binop(self)
6651
        operand2 = self.operand2.compile_time_value(denv)
6652 6653 6654 6655
        try:
            result = func(operand1, operand2)
        except Exception, e:
            self.compile_time_value_error(e)
6656
            result = None
6657 6658 6659
        if result:
            cascade = self.cascade
            if cascade:
6660
                # FIXME: I bet this must call cascaded_compile_time_value()
6661
                result = result and cascade.cascaded_compile_time_value(operand2, denv)
6662 6663
        return result

6664
    def is_cpp_comparison(self):
Robert Bradshaw's avatar
Robert Bradshaw committed
6665
        return self.operand1.type.is_cpp_class or self.operand2.type.is_cpp_class
6666

6667
    def find_common_int_type(self, env, op, operand1, operand2):
6668 6669 6670 6671 6672 6673
        # type1 != type2 and at least one of the types is not a C int
        type1 = operand1.type
        type2 = operand2.type
        type1_can_be_int = False
        type2_can_be_int = False

6674
        if isinstance(operand1, (StringNode, BytesNode, UnicodeNode)) \
6675 6676
               and operand1.can_coerce_to_char_literal():
            type1_can_be_int = True
6677
        if isinstance(operand2, (StringNode, BytesNode, UnicodeNode)) \
6678 6679 6680 6681 6682
                 and operand2.can_coerce_to_char_literal():
            type2_can_be_int = True

        if type1.is_int:
            if type2_can_be_int:
6683
                return type1
6684 6685
        elif type2.is_int:
            if type1_can_be_int:
6686
                return type2
6687 6688
        elif type1_can_be_int:
            if type2_can_be_int:
6689
                return PyrexTypes.c_uchar_type
William Stein's avatar
William Stein committed
6690

6691
        return None
6692

6693
    def find_common_type(self, env, op, operand1, common_type=None):
6694
        operand2 = self.operand2
William Stein's avatar
William Stein committed
6695 6696
        type1 = operand1.type
        type2 = operand2.type
6697

6698 6699
        new_common_type = None

Stefan Behnel's avatar
Stefan Behnel committed
6700
        # catch general errors
6701 6702 6703
        if type1 == str_type and (type2.is_string or type2 in (bytes_type, unicode_type)) or \
               type2 == str_type and (type1.is_string or type1 in (bytes_type, unicode_type)):
            error(self.pos, "Comparisons between bytes/unicode and str are not portable to Python 3")
6704
            new_common_type = error_type
Stefan Behnel's avatar
Stefan Behnel committed
6705 6706

        # try to use numeric comparisons where possible
6707
        elif type1.is_complex or type2.is_complex:
6708
            if op not in ('==', '!='):
6709 6710 6711 6712 6713 6714
                error(self.pos, "complex types are unordered")
                new_common_type = error_type
            if type1.is_pyobject:
                new_common_type = type1
            elif type2.is_pyobject:
                new_common_type = type2
6715
            else:
6716
                new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
6717 6718
        elif type1.is_numeric and type2.is_numeric:
            new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
6719
        elif common_type is None or not common_type.is_pyobject:
6720
            new_common_type = self.find_common_int_type(env, op, operand1, operand2)
6721 6722

        if new_common_type is None:
Stefan Behnel's avatar
Stefan Behnel committed
6723
            # fall back to generic type compatibility tests
6724
            if type1 == type2:
6725 6726 6727 6728 6729 6730
                new_common_type = type1
            elif type1.is_pyobject or type2.is_pyobject:
                if type2.is_numeric or type2.is_string:
                    if operand2.check_for_coercion_error(type1):
                        new_common_type = error_type
                    else:
Robert Bradshaw's avatar
Robert Bradshaw committed
6731
                        new_common_type = py_object_type
6732 6733 6734 6735
                elif type1.is_numeric or type1.is_string:
                    if operand1.check_for_coercion_error(type2):
                        new_common_type = error_type
                    else:
Robert Bradshaw's avatar
Robert Bradshaw committed
6736 6737 6738
                        new_common_type = py_object_type
                elif py_object_type.assignable_from(type1) and py_object_type.assignable_from(type2):
                    new_common_type = py_object_type
6739 6740 6741 6742
                else:
                    # one Python type and one non-Python type, not assignable
                    self.invalid_types_error(operand1, op, operand2)
                    new_common_type = error_type
6743 6744 6745 6746
            elif type1.assignable_from(type2):
                new_common_type = type1
            elif type2.assignable_from(type1):
                new_common_type = type2
6747 6748 6749 6750
            else:
                # C types that we couldn't handle up to here are an error
                self.invalid_types_error(operand1, op, operand2)
                new_common_type = error_type
6751

6752 6753 6754 6755 6756 6757
        if new_common_type.is_string and (isinstance(operand1, BytesNode) or
                                          isinstance(operand2, BytesNode)):
            # special case when comparing char* to bytes literal: must
            # compare string values!
            new_common_type = bytes_type

Stefan Behnel's avatar
Stefan Behnel committed
6758
        # recursively merge types
6759
        if common_type is None or new_common_type.is_error:
6760
            common_type = new_common_type
William Stein's avatar
William Stein committed
6761
        else:
6762 6763 6764
            # we could do a lot better by splitting the comparison
            # into a non-Python part and a Python part, but this is
            # safer for now
6765
            common_type = PyrexTypes.spanning_type(common_type, new_common_type)
6766 6767

        if self.cascade:
6768
            common_type = self.cascade.find_common_type(env, self.operator, operand2, common_type)
6769

6770 6771
        return common_type

6772 6773 6774 6775
    def invalid_types_error(self, operand1, op, operand2):
        error(self.pos, "Invalid types for '%s' (%s, %s)" %
              (op, operand1.type, operand2.type))

Stefan Behnel's avatar
Stefan Behnel committed
6776
    def is_python_comparison(self):
6777 6778 6779 6780 6781
        return (not self.is_ptr_contains()
            and not self.is_c_string_contains()
            and (self.has_python_operands()
                 or (self.cascade and self.cascade.is_python_comparison())
                 or self.operator in ('in', 'not_in')))
Stefan Behnel's avatar
Stefan Behnel committed
6782

6783 6784 6785 6786 6787 6788
    def coerce_operands_to(self, dst_type, env):
        operand2 = self.operand2
        if operand2.type != dst_type:
            self.operand2 = operand2.coerce_to(dst_type, env)
        if self.cascade:
            self.cascade.coerce_operands_to(dst_type, env)
6789

6790
    def is_python_result(self):
6791
        return ((self.has_python_operands() and
6792
                 self.special_bool_cmp_function is None and
6793
                 self.operator not in ('is', 'is_not', 'in', 'not_in') and
6794 6795
                 not self.is_c_string_contains() and
                 not self.is_ptr_contains())
6796
            or (self.cascade and self.cascade.is_python_result()))
William Stein's avatar
William Stein committed
6797

6798 6799
    def is_c_string_contains(self):
        return self.operator in ('in', 'not_in') and \
6800 6801
               ((self.operand1.type.is_int
                 and (self.operand2.type.is_string or self.operand2.type is bytes_type)) or
Stefan Behnel's avatar
Stefan Behnel committed
6802
                (self.operand1.type.is_unicode_char
6803
                 and self.operand2.type is unicode_type))
6804

6805 6806
    def is_ptr_contains(self):
        if self.operator in ('in', 'not_in'):
6807 6808 6809
            container_type = self.operand2.type
            return (container_type.is_ptr or container_type.is_array) \
                and not container_type.is_string
6810

6811 6812 6813 6814 6815 6816 6817 6818
    def find_special_bool_compare_function(self, env):
        if self.operator in ('==', '!='):
            type1, type2 = self.operand1.type, self.operand2.type
            if type1.is_pyobject and type2.is_pyobject:
                if type1 is Builtin.unicode_type or type2 is Builtin.unicode_type:
                    env.use_utility_code(pyunicode_equals_utility_code)
                    self.special_bool_cmp_function = "__Pyx_PyUnicode_Equals"
                    return True
6819 6820 6821 6822 6823 6824 6825 6826
                elif type1 is Builtin.bytes_type or type2 is Builtin.bytes_type:
                    env.use_utility_code(pybytes_equals_utility_code)
                    self.special_bool_cmp_function = "__Pyx_PyBytes_Equals"
                    return True
                elif type1 is Builtin.str_type or type2 is Builtin.str_type:
                    env.use_utility_code(pystr_equals_utility_code)
                    self.special_bool_cmp_function = "__Pyx_PyString_Equals"
                    return True
6827 6828
        return False

6829
    def generate_operation_code(self, code, result_code,
William Stein's avatar
William Stein committed
6830
            operand1, op , operand2):
6831
        if self.type.is_pyobject:
6832 6833 6834
            coerce_result = "__Pyx_PyBool_FromLong"
        else:
            coerce_result = ""
6835
        if 'not' in op:
6836
            negation = "!"
6837
        else:
6838
            negation = ""
6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855
        if self.special_bool_cmp_function:
            if operand1.type.is_pyobject:
                result1 = operand1.py_result()
            else:
                result1 = operand1.result()
            if operand2.type.is_pyobject:
                result2 = operand2.py_result()
            else:
                result2 = operand2.result()
            code.putln("%s = %s(%s, %s, %s); %s" % (
                result_code,
                self.special_bool_cmp_function,
                result1,
                result2,
                richcmp_constants[op],
                code.error_goto_if_neg(result_code, self.pos)))
        elif op == 'in' or op == 'not_in':
Stefan Behnel's avatar
typo  
Stefan Behnel committed
6856
            code.globalstate.use_utility_code(contains_utility_code)
6857
            if self.type.is_pyobject:
6858
                coerce_result = "__Pyx_PyBoolOrNull_FromLong"
6859
            if op == 'not_in':
6860
                negation = "__Pyx_NegateNonNeg"
6861
            if operand2.type is dict_type:
6862
                method = "PyDict_Contains"
6863
            else:
6864
                method = "PySequence_Contains"
6865
            if self.type.is_pyobject:
6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876
                error_clause = code.error_goto_if_null
                got_ref = "__Pyx_XGOTREF(%s); " % result_code
            else:
                error_clause = code.error_goto_if_neg
                got_ref = ""
            code.putln(
                "%s = %s(%s(%s(%s, %s))); %s%s" % (
                    result_code,
                    coerce_result,
                    negation,
                    method,
6877 6878
                    operand2.py_result(),
                    operand1.py_result(),
6879 6880
                    got_ref,
                    error_clause(result_code, self.pos)))
William Stein's avatar
William Stein committed
6881 6882
        elif (operand1.type.is_pyobject
            and op not in ('is', 'is_not')):
6883
                code.putln("%s = PyObject_RichCompare(%s, %s, %s); %s" % (
6884 6885 6886
                        result_code,
                        operand1.py_result(),
                        operand2.py_result(),
6887 6888
                        richcmp_constants[op],
                        code.error_goto_if_null(result_code, self.pos)))
6889
                code.put_gotref(result_code)
6890
        elif operand1.type.is_complex:
6891
            if op == "!=":
6892
                negation = "!"
6893
            else:
6894
                negation = ""
6895
            code.putln("%s = %s(%s%s(%s, %s));" % (
6896
                result_code,
6897 6898
                coerce_result,
                negation,
6899 6900
                operand1.type.unary_op('eq'),
                operand1.result(),
6901
                operand2.result()))
William Stein's avatar
William Stein committed
6902
        else:
6903 6904 6905 6906 6907
            type1 = operand1.type
            type2 = operand2.type
            if (type1.is_extension_type or type2.is_extension_type) \
                    and not type1.same_as(type2):
                common_type = py_object_type
6908 6909
            elif type1.is_numeric:
                common_type = PyrexTypes.widest_numeric_type(type1, type2)
6910
            else:
6911 6912 6913
                common_type = type1
            code1 = operand1.result_as(common_type)
            code2 = operand2.result_as(common_type)
6914
            code.putln("%s = %s(%s %s %s);" % (
6915 6916 6917 6918
                result_code,
                coerce_result,
                code1,
                self.c_operator(op),
6919 6920
                code2))

William Stein's avatar
William Stein committed
6921 6922 6923 6924 6925 6926 6927
    def c_operator(self, op):
        if op == 'is':
            return "=="
        elif op == 'is_not':
            return "!="
        else:
            return op
6928

Stefan Behnel's avatar
typo  
Stefan Behnel committed
6929
contains_utility_code = UtilityCode(
6930
proto="""
6931 6932
static CYTHON_INLINE long __Pyx_NegateNonNeg(long b) { return unlikely(b < 0) ? b : !b; }
static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) {
6933 6934 6935 6936
    return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b);
}
""")

6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958
char_in_bytes_utility_code = UtilityCode(
proto="""
static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character); /*proto*/
""",
impl="""
static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character) {
    const Py_ssize_t length = PyBytes_GET_SIZE(bytes);
    char* char_start = PyBytes_AS_STRING(bytes);
    char* pos;
    for (pos=char_start; pos < char_start+length; pos++) {
        if (character == pos[0]) return 1;
    }
    return 0;
}
""")

pyunicode_in_unicode_utility_code = UtilityCode(
proto="""
static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character); /*proto*/
""",
impl="""
static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character) {
6959
    Py_UNICODE* pos;
6960 6961
    const Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
    Py_UNICODE* char_start = PyUnicode_AS_UNICODE(unicode);
6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976

    for (pos=char_start; pos < char_start+length; pos++) {
        if (unlikely(character == pos[0])) return 1;
    }
    return 0;
}
""")

py_ucs4_in_unicode_utility_code = UtilityCode(
proto="""
static CYTHON_INLINE int __Pyx_UnicodeContainsUCS4(PyObject* unicode, Py_UCS4 character); /*proto*/
""",
# additionally handles surrogate pairs in 16bit Unicode builds
impl="""
static CYTHON_INLINE int __Pyx_UnicodeContainsUCS4(PyObject* unicode, Py_UCS4 character) {
6977
    Py_UNICODE* pos;
6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993
    Py_UNICODE uchar;
    const Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
    Py_UNICODE* char_start = PyUnicode_AS_UNICODE(unicode);

    #if Py_UNICODE_SIZE == 2
    if (unlikely(character > 65535)) {
        Py_UNICODE high_val, low_val;
        high_val = (Py_UNICODE) (0xD800 | (((character - 0x10000) >> 10) & ((1<<10)-1)));
        low_val  = (Py_UNICODE) (0xDC00 | ( (character - 0x10000)        & ((1<<10)-1)));
        for (pos=char_start; pos < char_start+length-1; pos++) {
            if (unlikely(high_val == pos[0]) & unlikely(low_val == pos[1])) return 1;
        }
        return 0;
    }
    #endif
    uchar = (Py_UNICODE) character;
6994
    for (pos=char_start; pos < char_start+length; pos++) {
6995
        if (unlikely(uchar == pos[0])) return 1;
6996 6997 6998 6999 7000
    }
    return 0;
}
""")

7001 7002 7003 7004 7005 7006
pyunicode_equals_utility_code = UtilityCode(
proto="""
static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
""",
impl="""
static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals) {
Stefan Behnel's avatar
Stefan Behnel committed
7007
    if (s1 == s2) {   /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038
        return (equals == Py_EQ);
    } else if (PyUnicode_CheckExact(s1) & PyUnicode_CheckExact(s2)) {
        if (PyUnicode_GET_SIZE(s1) != PyUnicode_GET_SIZE(s2)) {
            return (equals == Py_NE);
        } else if (PyUnicode_GET_SIZE(s1) == 1) {
            if (equals == Py_EQ)
                return (PyUnicode_AS_UNICODE(s1)[0] == PyUnicode_AS_UNICODE(s2)[0]);
            else
                return (PyUnicode_AS_UNICODE(s1)[0] != PyUnicode_AS_UNICODE(s2)[0]);
        } else {
            int result = PyUnicode_Compare(s1, s2);
            if ((result == -1) && unlikely(PyErr_Occurred()))
                return -1;
            return (equals == Py_EQ) ? (result == 0) : (result != 0);
        }
    } else if ((s1 == Py_None) & PyUnicode_CheckExact(s2)) {
        return (equals == Py_NE);
    } else if ((s2 == Py_None) & PyUnicode_CheckExact(s1)) {
        return (equals == Py_NE);
    } else {
        int result;
        PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
        if (!py_result)
            return -1;
        result = __Pyx_PyObject_IsTrue(py_result);
        Py_DECREF(py_result);
        return result;
    }
}
""")

7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054
pybytes_equals_utility_code = UtilityCode(
proto="""
static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
""",
impl="""
static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals) {
    if (s1 == s2) {   /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
        return (equals == Py_EQ);
    } else if (PyBytes_CheckExact(s1) & PyBytes_CheckExact(s2)) {
        if (PyBytes_GET_SIZE(s1) != PyBytes_GET_SIZE(s2)) {
            return (equals == Py_NE);
        } else if (PyBytes_GET_SIZE(s1) == 1) {
            if (equals == Py_EQ)
                return (PyBytes_AS_STRING(s1)[0] == PyBytes_AS_STRING(s2)[0]);
            else
                return (PyBytes_AS_STRING(s1)[0] != PyBytes_AS_STRING(s2)[0]);
7055 7056 7057 7058
        } else {
            int result = memcmp(PyBytes_AS_STRING(s1), PyBytes_AS_STRING(s2), PyBytes_GET_SIZE(s1));
            return (equals == Py_EQ) ? (result == 0) : (result != 0);
        }
7059 7060 7061 7062
    } else if ((s1 == Py_None) & PyBytes_CheckExact(s2)) {
        return (equals == Py_NE);
    } else if ((s2 == Py_None) & PyBytes_CheckExact(s1)) {
        return (equals == Py_NE);
7063
    } else {
7064 7065 7066 7067 7068 7069 7070 7071 7072
        int result;
        PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
        if (!py_result)
            return -1;
        result = __Pyx_PyObject_IsTrue(py_result);
        Py_DECREF(py_result);
        return result;
    }
}
7073 7074
""",
requires=[Builtin.include_string_h_utility_code])
7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085

pystr_equals_utility_code = UtilityCode(
proto="""
#if PY_MAJOR_VERSION >= 3
#define __Pyx_PyString_Equals __Pyx_PyUnicode_Equals
#else
#define __Pyx_PyString_Equals __Pyx_PyBytes_Equals
#endif
""",
requires=[pybytes_equals_utility_code, pyunicode_equals_utility_code])

William Stein's avatar
William Stein committed
7086

7087
class PrimaryCmpNode(ExprNode, CmpNode):
William Stein's avatar
William Stein committed
7088 7089 7090 7091 7092 7093 7094
    #  Non-cascaded comparison or first comparison of
    #  a cascaded sequence.
    #
    #  operator      string
    #  operand1      ExprNode
    #  operand2      ExprNode
    #  cascade       CascadedCmpNode
7095

William Stein's avatar
William Stein committed
7096 7097 7098 7099
    #  We don't use the subexprs mechanism, because
    #  things here are too complicated for it to handle.
    #  Instead, we override all the framework methods
    #  which use it.
7100

Robert Bradshaw's avatar
Robert Bradshaw committed
7101
    child_attrs = ['operand1', 'operand2', 'cascade']
7102

William Stein's avatar
William Stein committed
7103
    cascade = None
7104

Robert Bradshaw's avatar
Robert Bradshaw committed
7105 7106 7107 7108 7109 7110 7111
    def infer_type(self, env):
        # TODO: Actually implement this (after merging with -unstable).
        return py_object_type

    def type_dependencies(self, env):
        return ()

7112
    def calculate_constant_result(self):
7113
        self.calculate_cascaded_constant_result(self.operand1.constant_result)
7114

7115
    def compile_time_value(self, denv):
7116
        operand1 = self.operand1.compile_time_value(denv)
7117 7118
        return self.cascaded_compile_time_value(operand1, denv)

William Stein's avatar
William Stein committed
7119 7120 7121
    def analyse_types(self, env):
        self.operand1.analyse_types(env)
        self.operand2.analyse_types(env)
7122 7123
        if self.is_cpp_comparison():
            self.analyse_cpp_comparison(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
7124 7125 7126
            if self.cascade:
                error(self.pos, "Cascading comparison not yet supported for cpp types.")
            return
William Stein's avatar
William Stein committed
7127
        if self.cascade:
7128 7129
            self.cascade.analyse_types(env)

7130
        if self.operator in ('in', 'not_in'):
7131 7132 7133 7134 7135 7136 7137
            if self.is_c_string_contains():
                self.is_pycmp = False
                common_type = None
                if self.cascade:
                    error(self.pos, "Cascading comparison not yet supported for 'int_val in string'.")
                    return
                if self.operand2.type is unicode_type:
7138 7139 7140 7141 7142 7143
                    self.uchar_test_type = PyrexTypes.widest_numeric_type(
                        self.operand1.type, PyrexTypes.c_py_unicode_type)
                    if self.uchar_test_type is PyrexTypes.c_py_unicode_type:
                        env.use_utility_code(pyunicode_in_unicode_utility_code)
                    else:
                        env.use_utility_code(py_ucs4_in_unicode_utility_code)
7144 7145 7146 7147 7148 7149
                else:
                    if self.operand1.type is PyrexTypes.c_uchar_type:
                        self.operand1 = self.operand1.coerce_to(PyrexTypes.c_char_type, env)
                    if self.operand2.type is not bytes_type:
                        self.operand2 = self.operand2.coerce_to(bytes_type, env)
                    env.use_utility_code(char_in_bytes_utility_code)
Stefan Behnel's avatar
Stefan Behnel committed
7150 7151
                self.operand2 = self.operand2.as_none_safe_node(
                    "argument of type 'NoneType' is not iterable")
7152 7153 7154 7155 7156 7157
            elif self.is_ptr_contains():
                if self.cascade:
                    error(self.pos, "Cascading comparison not yet supported for 'val in sliced pointer'.")
                self.type = PyrexTypes.c_bint_type
                # Will be transformed by IterationTransform
                return
7158
            else:
7159 7160
                if self.operand2.type is dict_type:
                    self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
7161 7162
                common_type = py_object_type
                self.is_pycmp = True
7163 7164 7165 7166
        elif self.find_special_bool_compare_function(env):
            common_type = None # if coercion needed, the method call above has already done it
            self.is_pycmp = False # result is bint
            self.is_temp = True # must check for error return
7167 7168 7169 7170
        else:
            common_type = self.find_common_type(env, self.operator, self.operand1)
            self.is_pycmp = common_type.is_pyobject

7171
        if common_type is not None and not common_type.is_error:
7172 7173 7174
            if self.operand1.type != common_type:
                self.operand1 = self.operand1.coerce_to(common_type, env)
            self.coerce_operands_to(common_type, env)
7175

William Stein's avatar
William Stein committed
7176 7177 7178
        if self.cascade:
            self.operand2 = self.operand2.coerce_to_simple(env)
            self.cascade.coerce_cascaded_operands_to_temp(env)
7179 7180 7181 7182 7183 7184 7185 7186
        if self.is_python_result():
            self.type = PyrexTypes.py_object_type
        else:
            self.type = PyrexTypes.c_bint_type
        cdr = self.cascade
        while cdr:
            cdr.type = self.type
            cdr = cdr.cascade
William Stein's avatar
William Stein committed
7187 7188
        if self.is_pycmp or self.cascade:
            self.is_temp = 1
7189

7190 7191 7192
    def analyse_cpp_comparison(self, env):
        type1 = self.operand1.type
        type2 = self.operand2.type
7193 7194
        entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
        if entry is None:
7195 7196
            error(self.pos, "Invalid types for '%s' (%s, %s)" %
                (self.operator, type1, type2))
7197 7198 7199
            self.type = PyrexTypes.error_type
            self.result_code = "<error>"
            return
7200 7201 7202 7203 7204
        func_type = entry.type
        if func_type.is_ptr:
            func_type = func_type.base_type
        if len(func_type.args) == 1:
            self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
7205
        else:
7206 7207 7208
            self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
            self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
        self.type = func_type.return_type
7209

William Stein's avatar
William Stein committed
7210 7211 7212
    def has_python_operands(self):
        return (self.operand1.type.is_pyobject
            or self.operand2.type.is_pyobject)
7213

William Stein's avatar
William Stein committed
7214 7215 7216
    def check_const(self):
        if self.cascade:
            self.not_const()
7217 7218 7219
            return False
        else:
            return self.operand1.check_const() and self.operand2.check_const()
William Stein's avatar
William Stein committed
7220 7221

    def calculate_result_code(self):
7222 7223 7224 7225 7226 7227 7228
        if self.operand1.type.is_complex:
            if self.operator == "!=":
                negation = "!"
            else:
                negation = ""
            return "(%s%s(%s, %s))" % (
                negation,
7229 7230
                self.operand1.type.binary_op('=='),
                self.operand1.result(),
7231
                self.operand2.result())
7232
        elif self.is_c_string_contains():
7233 7234 7235 7236 7237
            if self.operand2.type is unicode_type:
                if self.uchar_test_type is PyrexTypes.c_py_unicode_type:
                    method = "__Pyx_UnicodeContains"
                else:
                    method = "__Pyx_UnicodeContainsUCS4"
7238
            else:
7239
                method = "__Pyx_BytesContains"
7240 7241 7242 7243 7244 7245 7246
            if self.operator == "not_in":
                negation = "!"
            else:
                negation = ""
            return "(%s%s(%s, %s))" % (
                negation,
                method,
7247
                self.operand2.result(),
7248
                self.operand1.result())
7249 7250 7251 7252 7253
        else:
            return "(%s %s %s)" % (
                self.operand1.result(),
                self.c_operator(self.operator),
                self.operand2.result())
7254

William Stein's avatar
William Stein committed
7255 7256 7257 7258
    def generate_evaluation_code(self, code):
        self.operand1.generate_evaluation_code(code)
        self.operand2.generate_evaluation_code(code)
        if self.is_temp:
7259
            self.allocate_temp_result(code)
7260
            self.generate_operation_code(code, self.result(),
William Stein's avatar
William Stein committed
7261 7262 7263
                self.operand1, self.operator, self.operand2)
            if self.cascade:
                self.cascade.generate_evaluation_code(code,
7264
                    self.result(), self.operand2)
William Stein's avatar
William Stein committed
7265
            self.operand1.generate_disposal_code(code)
7266
            self.operand1.free_temps(code)
William Stein's avatar
William Stein committed
7267
            self.operand2.generate_disposal_code(code)
7268
            self.operand2.free_temps(code)
7269

William Stein's avatar
William Stein committed
7270 7271 7272 7273 7274
    def generate_subexpr_disposal_code(self, code):
        #  If this is called, it is a non-cascaded cmp,
        #  so only need to dispose of the two main operands.
        self.operand1.generate_disposal_code(code)
        self.operand2.generate_disposal_code(code)
7275

7276 7277 7278 7279 7280
    def free_subexpr_temps(self, code):
        #  If this is called, it is a non-cascaded cmp,
        #  so only need to dispose of the two main operands.
        self.operand1.free_temps(code)
        self.operand2.free_temps(code)
7281

7282 7283 7284 7285 7286
    def annotate(self, code):
        self.operand1.annotate(code)
        self.operand2.annotate(code)
        if self.cascade:
            self.cascade.annotate(code)
William Stein's avatar
William Stein committed
7287 7288 7289


class CascadedCmpNode(Node, CmpNode):
7290 7291 7292
    #  A CascadedCmpNode is not a complete expression node. It
    #  hangs off the side of another comparison node, shares
    #  its left operand with that node, and shares its result
William Stein's avatar
William Stein committed
7293 7294 7295 7296 7297 7298
    #  with the PrimaryCmpNode at the head of the chain.
    #
    #  operator      string
    #  operand2      ExprNode
    #  cascade       CascadedCmpNode

Robert Bradshaw's avatar
Robert Bradshaw committed
7299 7300
    child_attrs = ['operand2', 'cascade']

William Stein's avatar
William Stein committed
7301
    cascade = None
7302 7303
    constant_result = constant_value_not_set # FIXME: where to calculate this?

Robert Bradshaw's avatar
Robert Bradshaw committed
7304 7305 7306 7307 7308 7309 7310
    def infer_type(self, env):
        # TODO: Actually implement this (after merging with -unstable).
        return py_object_type

    def type_dependencies(self, env):
        return ()

7311 7312 7313 7314
    def has_constant_result(self):
        return self.constant_result is not constant_value_not_set and \
               self.constant_result is not not_a_constant

7315
    def analyse_types(self, env):
William Stein's avatar
William Stein committed
7316 7317
        self.operand2.analyse_types(env)
        if self.cascade:
7318
            self.cascade.analyse_types(env)
7319

William Stein's avatar
William Stein committed
7320 7321
    def has_python_operands(self):
        return self.operand2.type.is_pyobject
7322

William Stein's avatar
William Stein committed
7323 7324
    def coerce_operands_to_pyobjects(self, env):
        self.operand2 = self.operand2.coerce_to_pyobject(env)
7325 7326
        if self.operand2.type is dict_type and self.operator in ('in', 'not_in'):
            self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
William Stein's avatar
William Stein committed
7327 7328 7329 7330 7331 7332 7333 7334
        if self.cascade:
            self.cascade.coerce_operands_to_pyobjects(env)

    def coerce_cascaded_operands_to_temp(self, env):
        if self.cascade:
            #self.operand2 = self.operand2.coerce_to_temp(env) #CTT
            self.operand2 = self.operand2.coerce_to_simple(env)
            self.cascade.coerce_cascaded_operands_to_temp(env)
7335

William Stein's avatar
William Stein committed
7336
    def generate_evaluation_code(self, code, result, operand1):
7337 7338
        if self.type.is_pyobject:
            code.putln("if (__Pyx_PyObject_IsTrue(%s)) {" % result)
7339
            code.put_decref(result, self.type)
7340 7341
        else:
            code.putln("if (%s) {" % result)
William Stein's avatar
William Stein committed
7342
        self.operand2.generate_evaluation_code(code)
7343
        self.generate_operation_code(code, result,
William Stein's avatar
William Stein committed
7344 7345 7346 7347 7348 7349
            operand1, self.operator, self.operand2)
        if self.cascade:
            self.cascade.generate_evaluation_code(
                code, result, self.operand2)
        # Cascaded cmp result is always temp
        self.operand2.generate_disposal_code(code)
7350
        self.operand2.free_temps(code)
William Stein's avatar
William Stein committed
7351 7352
        code.putln("}")

7353 7354 7355 7356 7357
    def annotate(self, code):
        self.operand2.annotate(code)
        if self.cascade:
            self.cascade.annotate(code)

William Stein's avatar
William Stein committed
7358 7359

binop_node_classes = {
7360 7361
    "or":       BoolBinopNode,
    "and":      BoolBinopNode,
Robert Bradshaw's avatar
Robert Bradshaw committed
7362 7363 7364
    "|":        IntBinopNode,
    "^":        IntBinopNode,
    "&":        IntBinopNode,
7365 7366
    "<<":       IntBinopNode,
    ">>":       IntBinopNode,
Robert Bradshaw's avatar
Robert Bradshaw committed
7367 7368 7369
    "+":        AddNode,
    "-":        SubNode,
    "*":        MulNode,
7370 7371
    "/":        DivNode,
    "//":       DivNode,
Robert Bradshaw's avatar
Robert Bradshaw committed
7372
    "%":        ModNode,
7373
    "**":       PowNode
William Stein's avatar
William Stein committed
7374 7375
}

7376
def binop_node(pos, operator, operand1, operand2, inplace=False):
7377
    # Construct binop node of appropriate class for
William Stein's avatar
William Stein committed
7378
    # given operator.
7379 7380 7381
    return binop_node_classes[operator](pos,
        operator = operator,
        operand1 = operand1,
7382 7383
        operand2 = operand2,
        inplace = inplace)
William Stein's avatar
William Stein committed
7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395

#-------------------------------------------------------------------
#
#  Coercion nodes
#
#  Coercion nodes are special in that they are created during
#  the analyse_types phase of parse tree processing.
#  Their __init__ methods consequently incorporate some aspects
#  of that phase.
#
#-------------------------------------------------------------------

7396
class CoercionNode(ExprNode):
William Stein's avatar
William Stein committed
7397 7398 7399
    #  Abstract base class for coercion nodes.
    #
    #  arg       ExprNode       node being coerced
7400

William Stein's avatar
William Stein committed
7401
    subexprs = ['arg']
7402
    constant_result = not_a_constant
7403

William Stein's avatar
William Stein committed
7404 7405 7406 7407
    def __init__(self, arg):
        self.pos = arg.pos
        self.arg = arg
        if debug_coercion:
Stefan Behnel's avatar
Stefan Behnel committed
7408
            print("%s Coercing %s" % (self, self.arg))
7409 7410

    def calculate_constant_result(self):
7411 7412
        # constant folding can break type coercion, so this is disabled
        pass
7413

7414 7415 7416 7417 7418
    def annotate(self, code):
        self.arg.annotate(code)
        if self.arg.type != self.type:
            file, line, col = self.pos
            code.annotate((file, line, col-1), AnnotationItem(style='coerce', tag='coerce', text='[%s] to [%s]' % (self.arg.type, self.type)))
William Stein's avatar
William Stein committed
7419 7420 7421 7422


class CastNode(CoercionNode):
    #  Wrap a node in a C type cast.
7423

William Stein's avatar
William Stein committed
7424 7425 7426
    def __init__(self, arg, new_type):
        CoercionNode.__init__(self, arg)
        self.type = new_type
Stefan Behnel's avatar
Stefan Behnel committed
7427 7428 7429

    def may_be_none(self):
        return self.arg.may_be_none()
7430

William Stein's avatar
William Stein committed
7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442
    def calculate_result_code(self):
        return self.arg.result_as(self.type)

    def generate_result_code(self, code):
        self.arg.generate_result_code(code)


class PyTypeTestNode(CoercionNode):
    #  This node is used to check that a generic Python
    #  object is an instance of a particular extension type.
    #  This node borrows the result of its argument node.

7443
    def __init__(self, arg, dst_type, env, notnone=False):
William Stein's avatar
William Stein committed
7444 7445
        #  The arg is know to be a Python object, and
        #  the dst_type is known to be an extension type.
Robert Bradshaw's avatar
Robert Bradshaw committed
7446
        assert dst_type.is_extension_type or dst_type.is_builtin_type, "PyTypeTest on non extension type"
William Stein's avatar
William Stein committed
7447 7448 7449
        CoercionNode.__init__(self, arg)
        self.type = dst_type
        self.result_ctype = arg.ctype()
7450
        self.notnone = notnone
7451

7452
    nogil_check = Node.gil_error
7453
    gil_message = "Python type test"
7454

7455 7456
    def analyse_types(self, env):
        pass
Stefan Behnel's avatar
Stefan Behnel committed
7457 7458 7459 7460 7461

    def may_be_none(self):
        if self.notnone:
            return False
        return self.arg.may_be_none()
7462

7463 7464 7465
    def is_simple(self):
        return self.arg.is_simple()

William Stein's avatar
William Stein committed
7466 7467
    def result_in_temp(self):
        return self.arg.result_in_temp()
7468

William Stein's avatar
William Stein committed
7469 7470
    def is_ephemeral(self):
        return self.arg.is_ephemeral()
7471 7472 7473 7474 7475

    def calculate_constant_result(self):
        # FIXME
        pass

William Stein's avatar
William Stein committed
7476
    def calculate_result_code(self):
7477
        return self.arg.result()
7478

William Stein's avatar
William Stein committed
7479 7480
    def generate_result_code(self, code):
        if self.type.typeobj_is_available():
7481
            if not self.type.is_builtin_type:
7482
                code.globalstate.use_utility_code(type_test_utility_code)
William Stein's avatar
William Stein committed
7483
            code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
7484
                "if (!(%s)) %s" % (
7485
                    self.type.type_test_code(self.arg.py_result(), self.notnone),
William Stein's avatar
William Stein committed
7486 7487 7488 7489
                    code.error_goto(self.pos)))
        else:
            error(self.pos, "Cannot test type of extern C class "
                "without type object name specification")
7490

William Stein's avatar
William Stein committed
7491 7492
    def generate_post_assignment_code(self, code):
        self.arg.generate_post_assignment_code(code)
7493 7494 7495

    def free_temps(self, code):
        self.arg.free_temps(code)
7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510


class NoneCheckNode(CoercionNode):
    # This node is used to check that a Python object is not None and
    # raises an appropriate exception (as specified by the creating
    # transform).

    def __init__(self, arg, exception_type_cname, exception_message):
        CoercionNode.__init__(self, arg)
        self.type = arg.type
        self.result_ctype = arg.ctype()
        self.exception_type_cname = exception_type_cname
        self.exception_message = exception_message

    def analyse_types(self, env):
7511
        pass
7512

7513 7514 7515
    def may_be_none(self):
        return False

7516 7517 7518
    def is_simple(self):
        return self.arg.is_simple()

7519 7520 7521 7522 7523
    def result_in_temp(self):
        return self.arg.result_in_temp()

    def calculate_result_code(self):
        return self.arg.result()
7524

7525 7526
    def generate_result_code(self, code):
        code.putln(
7527
            "if (unlikely(%s == Py_None)) {" % self.arg.py_result())
7528 7529
        code.putln('PyErr_SetString(%s, "%s"); %s ' % (
            self.exception_type_cname,
Stefan Behnel's avatar
Stefan Behnel committed
7530 7531
            StringEncoding.escape_byte_string(
                self.exception_message.encode('UTF-8')),
7532 7533 7534 7535 7536 7537 7538 7539 7540
            code.error_goto(self.pos)))
        code.putln("}")

    def generate_post_assignment_code(self, code):
        self.arg.generate_post_assignment_code(code)

    def free_temps(self, code):
        self.arg.free_temps(code)

7541

William Stein's avatar
William Stein committed
7542 7543 7544
class CoerceToPyTypeNode(CoercionNode):
    #  This node is used to convert a C data type
    #  to a Python object.
7545

7546
    type = py_object_type
Robert Bradshaw's avatar
Robert Bradshaw committed
7547
    is_temp = 1
William Stein's avatar
William Stein committed
7548

7549
    def __init__(self, arg, env, type=py_object_type):
William Stein's avatar
William Stein committed
7550
        CoercionNode.__init__(self, arg)
7551
        if not arg.type.create_to_py_utility_code(env):
William Stein's avatar
William Stein committed
7552
            error(arg.pos,
7553
                  "Cannot convert '%s' to Python object" % arg.type)
7554 7555 7556 7557
        if type is py_object_type:
            # be specific about some known types
            if arg.type.is_string:
                self.type = bytes_type
Stefan Behnel's avatar
Stefan Behnel committed
7558
            elif arg.type.is_unicode_char:
7559 7560 7561 7562 7563 7564
                self.type = unicode_type
            elif arg.type.is_complex:
                self.type = Builtin.complex_type
        else:
            # FIXME: check that the target type and the resulting type are compatible
            pass
7565

7566
    gil_message = "Converting to Python object"
7567

7568 7569 7570 7571
    def may_be_none(self):
        # FIXME: is this always safe?
        return False

7572
    def coerce_to_boolean(self, env):
7573 7574 7575 7576 7577 7578
        arg_type = self.arg.type
        if (arg_type == PyrexTypes.c_bint_type or
            (arg_type.is_pyobject and arg_type.name == 'bool')):
            return self.arg.coerce_to_temp(env)
        else:
            return CoerceToBooleanNode(self, env)
7579

7580 7581 7582 7583 7584 7585
    def coerce_to_integer(self, env):
        # If not already some C integer type, coerce to longint.
        if self.arg.type.is_int:
            return self.arg
        else:
            return self.arg.coerce_to(PyrexTypes.c_long_type, env)
7586

7587 7588 7589 7590
    def analyse_types(self, env):
        # The arg is always already analysed
        pass

William Stein's avatar
William Stein committed
7591 7592
    def generate_result_code(self, code):
        function = self.arg.type.to_py_function
Robert Bradshaw's avatar
Robert Bradshaw committed
7593
        code.putln('%s = %s(%s); %s' % (
7594 7595 7596
            self.result(),
            function,
            self.arg.result(),
7597
            code.error_goto_if_null(self.result(), self.pos)))
7598
        code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
7599 7600


7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640
class CoerceIntToBytesNode(CoerceToPyTypeNode):
    #  This node is used to convert a C int type to a Python bytes
    #  object.

    is_temp = 1

    def __init__(self, arg, env):
        arg = arg.coerce_to_simple(env)
        CoercionNode.__init__(self, arg)
        self.type = Builtin.bytes_type

    def generate_result_code(self, code):
        arg = self.arg
        arg_result = arg.result()
        if arg.type not in (PyrexTypes.c_char_type,
                            PyrexTypes.c_uchar_type,
                            PyrexTypes.c_schar_type):
            if arg.type.signed:
                code.putln("if ((%s < 0) || (%s > 255)) {" % (
                    arg_result, arg_result))
            else:
                code.putln("if (%s > 255) {" % arg_result)
            code.putln('PyErr_Format(PyExc_OverflowError, '
                       '"value too large to pack into a byte"); %s' % (
                           code.error_goto(self.pos)))
            code.putln('}')
        temp = None
        if arg.type is not PyrexTypes.c_char_type:
            temp = code.funcstate.allocate_temp(PyrexTypes.c_char_type, manage_ref=False)
            code.putln("%s = (char)%s;" % (temp, arg_result))
            arg_result = temp
        code.putln('%s = PyBytes_FromStringAndSize(&%s, 1); %s' % (
            self.result(),
            arg_result,
            code.error_goto_if_null(self.result(), self.pos)))
        if temp is not None:
            code.funcstate.release_temp(temp)
        code.put_gotref(self.py_result())


William Stein's avatar
William Stein committed
7641 7642 7643 7644 7645 7646 7647 7648
class CoerceFromPyTypeNode(CoercionNode):
    #  This node is used to convert a Python object
    #  to a C data type.

    def __init__(self, result_type, arg, env):
        CoercionNode.__init__(self, arg)
        self.type = result_type
        self.is_temp = 1
7649
        if not result_type.create_from_py_utility_code(env):
William Stein's avatar
William Stein committed
7650
            error(arg.pos,
Craig Citro's avatar
Craig Citro committed
7651
                  "Cannot convert Python object to '%s'" % result_type)
William Stein's avatar
William Stein committed
7652 7653
        if self.type.is_string and self.arg.is_ephemeral():
            error(arg.pos,
Craig Citro's avatar
Craig Citro committed
7654
                  "Obtaining char * from temporary Python value")
7655

7656 7657 7658 7659
    def analyse_types(self, env):
        # The arg is always already analysed
        pass

William Stein's avatar
William Stein committed
7660 7661
    def generate_result_code(self, code):
        function = self.type.from_py_function
7662 7663 7664 7665
        operand = self.arg.py_result()
        rhs = "%s(%s)" % (function, operand)
        if self.type.is_enum:
            rhs = typecast(self.type, c_long_type, rhs)
Robert Bradshaw's avatar
Robert Bradshaw committed
7666
        code.putln('%s = %s; %s' % (
7667
            self.result(),
7668
            rhs,
7669
            code.error_goto_if(self.type.error_condition(self.result()), self.pos)))
7670
        if self.type.is_pyobject:
7671
            code.put_gotref(self.py_result())
William Stein's avatar
William Stein committed
7672 7673 7674 7675 7676


class CoerceToBooleanNode(CoercionNode):
    #  This node is used when a result needs to be used
    #  in a boolean context.
7677

7678
    type = PyrexTypes.c_bint_type
7679 7680 7681 7682

    _special_builtins = {
        Builtin.list_type    : 'PyList_GET_SIZE',
        Builtin.tuple_type   : 'PyTuple_GET_SIZE',
7683
        Builtin.bytes_type   : 'PyBytes_GET_SIZE',
7684 7685 7686
        Builtin.unicode_type : 'PyUnicode_GET_SIZE',
        }

William Stein's avatar
William Stein committed
7687 7688 7689 7690
    def __init__(self, arg, env):
        CoercionNode.__init__(self, arg)
        if arg.type.is_pyobject:
            self.is_temp = 1
7691

7692
    def nogil_check(self, env):
7693
        if self.arg.type.is_pyobject and self._special_builtins.get(self.arg.type) is None:
7694
            self.gil_error()
7695

7696
    gil_message = "Truth-testing Python object"
7697

William Stein's avatar
William Stein committed
7698 7699 7700
    def check_const(self):
        if self.is_temp:
            self.not_const()
7701 7702
            return False
        return self.arg.check_const()
7703

William Stein's avatar
William Stein committed
7704
    def calculate_result_code(self):
7705
        return "(%s != 0)" % self.arg.result()
William Stein's avatar
William Stein committed
7706 7707

    def generate_result_code(self, code):
7708 7709 7710 7711
        if not self.is_temp:
            return
        test_func = self._special_builtins.get(self.arg.type)
        if test_func is not None:
Stefan Behnel's avatar
Stefan Behnel committed
7712
            code.putln("%s = (%s != Py_None) && (%s(%s) != 0);" % (
7713 7714 7715 7716 7717
                       self.result(),
                       self.arg.py_result(),
                       test_func,
                       self.arg.py_result()))
        else:
William Stein's avatar
William Stein committed
7718
            code.putln(
7719
                "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
7720 7721
                    self.result(),
                    self.arg.py_result(),
7722
                    code.error_goto_if_neg(self.result(), self.pos)))
William Stein's avatar
William Stein committed
7723

7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734
class CoerceToComplexNode(CoercionNode):

    def __init__(self, arg, dst_type, env):
        if arg.type.is_complex:
            arg = arg.coerce_to_simple(env)
        self.type = dst_type
        CoercionNode.__init__(self, arg)
        dst_type.create_declaration_utility_code(env)

    def calculate_result_code(self):
        if self.arg.type.is_complex:
7735 7736
            real_part = "__Pyx_CREAL(%s)" % self.arg.result()
            imag_part = "__Pyx_CIMAG(%s)" % self.arg.result()
7737 7738 7739 7740 7741 7742 7743
        else:
            real_part = self.arg.result()
            imag_part = "0"
        return "%s(%s, %s)" % (
                self.type.from_parts,
                real_part,
                imag_part)
7744

7745 7746
    def generate_result_code(self, code):
        pass
William Stein's avatar
William Stein committed
7747 7748 7749 7750 7751 7752 7753 7754 7755

class CoerceToTempNode(CoercionNode):
    #  This node is used to force the result of another node
    #  to be stored in a temporary. It is only used if the
    #  argument node's result is not already in a temporary.

    def __init__(self, arg, env):
        CoercionNode.__init__(self, arg)
        self.type = self.arg.type
7756
        self.constant_result = self.arg.constant_result
William Stein's avatar
William Stein committed
7757 7758 7759
        self.is_temp = 1
        if self.type.is_pyobject:
            self.result_ctype = py_object_type
7760 7761 7762

    gil_message = "Creating temporary Python reference"

7763 7764 7765
    def analyse_types(self, env):
        # The arg is always already analysed
        pass
7766

7767 7768
    def coerce_to_boolean(self, env):
        self.arg = self.arg.coerce_to_boolean(env)
7769 7770
        if self.arg.is_simple():
            return self.arg
7771 7772 7773
        self.type = self.arg.type
        self.result_ctype = self.type
        return self
7774

William Stein's avatar
William Stein committed
7775 7776 7777 7778
    def generate_result_code(self, code):
        #self.arg.generate_evaluation_code(code) # Already done
        # by generic generate_subexpr_evaluation_code!
        code.putln("%s = %s;" % (
7779
            self.result(), self.arg.result_as(self.ctype())))
7780
        if self.type.is_pyobject and self.use_managed_ref:
7781
            code.put_incref(self.result(), self.ctype())
William Stein's avatar
William Stein committed
7782 7783 7784 7785 7786 7787 7788


class CloneNode(CoercionNode):
    #  This node is employed when the result of another node needs
    #  to be used multiple times. The argument node's result must
    #  be in a temporary. This node "borrows" the result from the
    #  argument node, and does not generate any evaluation or
7789
    #  disposal code for it. The original owner of the argument
William Stein's avatar
William Stein committed
7790
    #  node is responsible for doing those things.
7791

William Stein's avatar
William Stein committed
7792
    subexprs = [] # Arg is not considered a subexpr
7793
    nogil_check = None
7794

William Stein's avatar
William Stein committed
7795 7796
    def __init__(self, arg):
        CoercionNode.__init__(self, arg)
7797 7798 7799 7800 7801
        if hasattr(arg, 'type'):
            self.type = arg.type
            self.result_ctype = arg.result_ctype
        if hasattr(arg, 'entry'):
            self.entry = arg.entry
7802

7803
    def result(self):
7804
        return self.arg.result()
7805

Robert Bradshaw's avatar
Robert Bradshaw committed
7806 7807
    def type_dependencies(self, env):
        return self.arg.type_dependencies(env)
7808

7809 7810
    def infer_type(self, env):
        return self.arg.infer_type(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
7811

Robert Bradshaw's avatar
Robert Bradshaw committed
7812 7813 7814 7815
    def analyse_types(self, env):
        self.type = self.arg.type
        self.result_ctype = self.arg.result_ctype
        self.is_temp = 1
7816 7817
        if hasattr(self.arg, 'entry'):
            self.entry = self.arg.entry
7818

7819 7820 7821
    def is_simple(self):
        return True # result is always in a temp (or a name)

William Stein's avatar
William Stein committed
7822 7823 7824 7825 7826
    def generate_evaluation_code(self, code):
        pass

    def generate_result_code(self, code):
        pass
7827

7828
    def generate_disposal_code(self, code):
7829
        pass
7830

7831 7832
    def free_temps(self, code):
        pass
7833

7834

7835 7836
class ModuleRefNode(ExprNode):
    # Simple returns the module object
7837

7838 7839 7840
    type = py_object_type
    is_temp = False
    subexprs = []
7841

7842 7843 7844
    def analyse_types(self, env):
        pass

7845 7846 7847
    def may_be_none(self):
        return False

7848 7849 7850 7851 7852 7853 7854 7855
    def calculate_result_code(self):
        return Naming.module_cname

    def generate_result_code(self, code):
        pass

class DocstringRefNode(ExprNode):
    # Extracts the docstring of the body element
7856

7857 7858 7859
    subexprs = ['body']
    type = py_object_type
    is_temp = True
7860

7861 7862 7863 7864 7865 7866 7867 7868 7869
    def __init__(self, pos, body):
        ExprNode.__init__(self, pos)
        assert body.type.is_pyobject
        self.body = body

    def analyse_types(self, env):
        pass

    def generate_result_code(self, code):
7870 7871 7872
        code.putln('%s = __Pyx_GetAttrString(%s, "__doc__"); %s' % (
            self.result(), self.body.result(),
            code.error_goto_if_null(self.result(), self.pos)))
7873 7874 7875 7876
        code.put_gotref(self.result())



William Stein's avatar
William Stein committed
7877 7878 7879 7880 7881 7882
#------------------------------------------------------------------------------------
#
#  Runtime support code
#
#------------------------------------------------------------------------------------

7883 7884
get_name_interned_utility_code = UtilityCode(
proto = """
7885
static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
7886 7887
""",
impl = """
William Stein's avatar
William Stein committed
7888 7889 7890
static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
    PyObject *result;
    result = PyObject_GetAttr(dict, name);
7891 7892 7893 7894 7895 7896 7897 7898 7899
    if (!result) {
        if (dict != %(BUILTINS)s) {
            PyErr_Clear();
            result = PyObject_GetAttr(%(BUILTINS)s, name);
        }
        if (!result) {
            PyErr_SetObject(PyExc_NameError, name);
        }
    }
William Stein's avatar
William Stein committed
7900 7901
    return result;
}
7902
""" % {'BUILTINS' : Naming.builtins_cname})
William Stein's avatar
William Stein committed
7903 7904 7905

#------------------------------------------------------------------------------------

7906 7907
import_utility_code = UtilityCode(
proto = """
Haoyu Bai's avatar
Haoyu Bai committed
7908
static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level); /*proto*/
7909 7910
""",
impl = """
Haoyu Bai's avatar
Haoyu Bai committed
7911
static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, long level) {
7912
    PyObject *py_import = 0;
William Stein's avatar
William Stein committed
7913 7914 7915 7916 7917
    PyObject *empty_list = 0;
    PyObject *module = 0;
    PyObject *global_dict = 0;
    PyObject *empty_dict = 0;
    PyObject *list;
7918 7919
    py_import = __Pyx_GetAttrString(%(BUILTINS)s, "__import__");
    if (!py_import)
William Stein's avatar
William Stein committed
7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934
        goto bad;
    if (from_list)
        list = from_list;
    else {
        empty_list = PyList_New(0);
        if (!empty_list)
            goto bad;
        list = empty_list;
    }
    global_dict = PyModule_GetDict(%(GLOBALS)s);
    if (!global_dict)
        goto bad;
    empty_dict = PyDict_New();
    if (!empty_dict)
        goto bad;
Haoyu Bai's avatar
Haoyu Bai committed
7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948
    #if PY_VERSION_HEX >= 0x02050000
    {
        PyObject *py_level = PyInt_FromLong(level);
        if (!py_level)
            goto bad;
        module = PyObject_CallFunctionObjArgs(py_import,
            name, global_dict, empty_dict, list, py_level, NULL);
        Py_DECREF(py_level);
    }
    #else
    if (level>0) {
        PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4.");
        goto bad;
    }
7949
    module = PyObject_CallFunctionObjArgs(py_import,
7950
        name, global_dict, empty_dict, list, NULL);
Haoyu Bai's avatar
Haoyu Bai committed
7951
    #endif
William Stein's avatar
William Stein committed
7952 7953
bad:
    Py_XDECREF(empty_list);
7954
    Py_XDECREF(py_import);
William Stein's avatar
William Stein committed
7955 7956 7957 7958 7959 7960
    Py_XDECREF(empty_dict);
    return module;
}
""" % {
    "BUILTINS": Naming.builtins_cname,
    "GLOBALS":  Naming.module_cname,
7961
})
William Stein's avatar
William Stein committed
7962 7963 7964

#------------------------------------------------------------------------------------

7965 7966
get_exception_utility_code = UtilityCode(
proto = """
7967
static PyObject *__Pyx_GetExcValue(void); /*proto*/
7968 7969
""",
impl = """
William Stein's avatar
William Stein committed
7970 7971
static PyObject *__Pyx_GetExcValue(void) {
    PyObject *type = 0, *value = 0, *tb = 0;
7972
    PyObject *tmp_type, *tmp_value, *tmp_tb;
William Stein's avatar
William Stein committed
7973 7974 7975 7976 7977 7978 7979 7980 7981 7982
    PyObject *result = 0;
    PyThreadState *tstate = PyThreadState_Get();
    PyErr_Fetch(&type, &value, &tb);
    PyErr_NormalizeException(&type, &value, &tb);
    if (PyErr_Occurred())
        goto bad;
    if (!value) {
        value = Py_None;
        Py_INCREF(value);
    }
7983 7984 7985
    tmp_type = tstate->exc_type;
    tmp_value = tstate->exc_value;
    tmp_tb = tstate->exc_traceback;
William Stein's avatar
William Stein committed
7986 7987 7988
    tstate->exc_type = type;
    tstate->exc_value = value;
    tstate->exc_traceback = tb;
7989 7990 7991 7992 7993
    /* Make sure tstate is in a consistent state when we XDECREF
    these objects (XDECREF may run arbitrary code). */
    Py_XDECREF(tmp_type);
    Py_XDECREF(tmp_value);
    Py_XDECREF(tmp_tb);
William Stein's avatar
William Stein committed
7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004
    result = value;
    Py_XINCREF(result);
    type = 0;
    value = 0;
    tb = 0;
bad:
    Py_XDECREF(type);
    Py_XDECREF(value);
    Py_XDECREF(tb);
    return result;
}
8005
""")
William Stein's avatar
William Stein committed
8006 8007 8008

#------------------------------------------------------------------------------------

8009 8010
type_test_utility_code = UtilityCode(
proto = """
8011
static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
8012 8013
""",
impl = """
8014
static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
8015
    if (unlikely(!type)) {
William Stein's avatar
William Stein committed
8016 8017 8018
        PyErr_Format(PyExc_SystemError, "Missing type object");
        return 0;
    }
8019
    if (likely(PyObject_TypeCheck(obj, type)))
William Stein's avatar
William Stein committed
8020
        return 1;
8021 8022
    PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
                 Py_TYPE(obj)->tp_name, type->tp_name);
William Stein's avatar
William Stein committed
8023 8024
    return 0;
}
8025
""")
William Stein's avatar
William Stein committed
8026 8027 8028

#------------------------------------------------------------------------------------

8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039
find_py2_metaclass_utility_code = UtilityCode(
proto = '''
static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases); /*proto*/
''',
impl = '''
static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases) {
    PyObject *metaclass;
    /* Default metaclass */
#if PY_MAJOR_VERSION < 3
    if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
        PyObject *base = PyTuple_GET_ITEM(bases, 0);
8040
        metaclass = PyObject_GetAttrString(base, (char *)"__class__");
8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060
        if (!metaclass) {
            PyErr_Clear();
            metaclass = (PyObject*) Py_TYPE(base);
        }
    } else {
        metaclass = (PyObject *) &PyClass_Type;
    }
#else
    if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
        PyObject *base = PyTuple_GET_ITEM(bases, 0);
        metaclass = (PyObject*) Py_TYPE(base);
    } else {
        metaclass = (PyObject *) &PyType_Type;
    }
#endif
    Py_INCREF(metaclass);
    return metaclass;
}
''')

8061 8062
create_class_utility_code = UtilityCode(
proto = """
8063
static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
8064 8065 8066 8067 8068
                                   PyObject *modname); /*proto*/
""",
impl = """
static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
                                   PyObject *modname) {
8069 8070
    PyObject *result;
    PyObject *metaclass;
8071 8072 8073 8074 8075 8076

    if (PyDict_SetItemString(dict, "__module__", modname) < 0)
        return NULL;

    /* Python2 __metaclass__ */
    metaclass = PyDict_GetItemString(dict, "__metaclass__");
8077 8078 8079 8080
    if (metaclass) {
        Py_INCREF(metaclass);
    } else {
        metaclass = __Pyx_FindPy2Metaclass(bases);
8081 8082 8083 8084 8085
    }
    result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL);
    Py_DECREF(metaclass);
    return result;
}
8086 8087
""",
requires = [find_py2_metaclass_utility_code])
8088 8089 8090 8091 8092

#------------------------------------------------------------------------------------

create_py3class_utility_code = UtilityCode(
proto = """
Stefan Behnel's avatar
Stefan Behnel committed
8093 8094 8095
static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw); /*proto*/
static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *mkw, PyObject *modname, PyObject *doc); /*proto*/
static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, PyObject *mkw); /*proto*/
8096 8097
""",
impl = """
Stefan Behnel's avatar
Stefan Behnel committed
8098
PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) {
8099
    PyObject *metaclass = PyDict_GetItemString(mkw, "metaclass");
8100 8101 8102 8103 8104 8105 8106 8107
    if (metaclass) {
        Py_INCREF(metaclass);
        if (PyDict_DelItemString(mkw, "metaclass") < 0) {
            Py_DECREF(metaclass);
            return NULL;
        }
        return metaclass;
    }
8108
    return __Pyx_FindPy2Metaclass(bases);
8109 8110
}

Stefan Behnel's avatar
Stefan Behnel committed
8111 8112
PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *mkw,
                                    PyObject *modname, PyObject *doc) {
8113 8114 8115
    PyObject *prep;
    PyObject *pargs;
    PyObject *ns;
8116
    PyObject *str;
8117

8118
    prep = PyObject_GetAttrString(metaclass, (char *)"__prepare__");
Stefan Behnel's avatar
Stefan Behnel committed
8119
    if (!prep) {
8120
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
8121
            return NULL;
8122
        PyErr_Clear();
8123
        return PyDict_New();
8124
    }
Stefan Behnel's avatar
Stefan Behnel committed
8125 8126
    pargs = PyTuple_New(2);
    if (!pargs) {
8127
        Py_DECREF(prep);
8128
        return NULL;
8129
    }
8130

Stefan Behnel's avatar
Stefan Behnel committed
8131 8132 8133 8134
    Py_INCREF(name);
    Py_INCREF(bases);
    PyTuple_SET_ITEM(pargs, 0, name);
    PyTuple_SET_ITEM(pargs, 1, bases);
8135

Stefan Behnel's avatar
Stefan Behnel committed
8136
    ns = PyObject_Call(prep, pargs, mkw);
8137

8138
    Py_DECREF(prep);
8139 8140
    Py_DECREF(pargs);

8141
    if (ns == NULL)
8142 8143 8144 8145
        return NULL;

    /* Required here to emulate assignment order */
    /* XXX: use consts here */
Stefan Behnel's avatar
Stefan Behnel committed
8146 8147 8148
    #if PY_MAJOR_VERSION >= 3
    str = PyUnicode_FromString("__module__");
    #else
8149
    str = PyString_FromString("__module__");
Stefan Behnel's avatar
Stefan Behnel committed
8150
    #endif
8151
    if (!str) {
8152
        Py_DECREF(ns);
8153
        return NULL;
8154
    }
Vitja Makarov's avatar
Vitja Makarov committed
8155

8156 8157 8158
    if (PyObject_SetItem(ns, str, modname) < 0) {
        Py_DECREF(ns);
        Py_DECREF(str);
8159
        return NULL;
8160 8161 8162
    }
    Py_DECREF(str);
    if (doc) {
Stefan Behnel's avatar
Stefan Behnel committed
8163 8164 8165
        #if PY_MAJOR_VERSION >= 3
        str = PyUnicode_FromString("__doc__");
        #else
8166
        str = PyString_FromString("__doc__");
Stefan Behnel's avatar
Stefan Behnel committed
8167
        #endif
8168 8169
        if (!str) {
            Py_DECREF(ns);
8170
            return NULL;
Vitja Makarov's avatar
Vitja Makarov committed
8171
        }
8172 8173 8174 8175
        if (PyObject_SetItem(ns, str, doc) < 0) {
            Py_DECREF(ns);
            Py_DECREF(str);
            return NULL;
Stefan Behnel's avatar
Stefan Behnel committed
8176
        }
8177
        Py_DECREF(str);
Vitja Makarov's avatar
Vitja Makarov committed
8178
    }
8179 8180 8181
    return ns;
}

Stefan Behnel's avatar
Stefan Behnel committed
8182
PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, PyObject *mkw) {
8183
    PyObject *result;
8184
    PyObject *margs = PyTuple_New(3);
8185 8186
    if (!margs)
        return NULL;
8187 8188 8189 8190 8191 8192
    Py_INCREF(name);
    Py_INCREF(bases);
    Py_INCREF(dict);
    PyTuple_SET_ITEM(margs, 0, name);
    PyTuple_SET_ITEM(margs, 1, bases);
    PyTuple_SET_ITEM(margs, 2, dict);
Stefan Behnel's avatar
Stefan Behnel committed
8193
    result = PyObject_Call(metaclass, margs, mkw);
8194
    Py_DECREF(margs);
William Stein's avatar
William Stein committed
8195 8196
    return result;
}
8197 8198
""",
requires = [find_py2_metaclass_utility_code])
William Stein's avatar
William Stein committed
8199 8200

#------------------------------------------------------------------------------------
Robert Bradshaw's avatar
Robert Bradshaw committed
8201

8202 8203
cpp_exception_utility_code = UtilityCode(
proto = """
8204 8205
#ifndef __Pyx_CppExn2PyErr
static void __Pyx_CppExn2PyErr() {
Robert Bradshaw's avatar
Robert Bradshaw committed
8206 8207 8208 8209 8210
  try {
    if (PyErr_Occurred())
      ; // let the latest Python exn pass through and ignore the current one
    else
      throw;
8211 8212 8213 8214 8215
  } catch (const std::invalid_argument& exn) {
    // Catch a handful of different errors here and turn them into the
    // equivalent Python errors.
    // Change invalid_argument to ValueError
    PyErr_SetString(PyExc_ValueError, exn.what());
Robert Bradshaw's avatar
Robert Bradshaw committed
8216
  } catch (const std::out_of_range& exn) {
8217
    // Change out_of_range to IndexError
Robert Bradshaw's avatar
Robert Bradshaw committed
8218 8219 8220 8221 8222 8223 8224 8225 8226
    PyErr_SetString(PyExc_IndexError, exn.what());
  } catch (const std::exception& exn) {
    PyErr_SetString(PyExc_RuntimeError, exn.what());
  }
  catch (...)
  {
    PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
  }
}
8227
#endif
8228 8229 8230
""",
impl = ""
)
Robert Bradshaw's avatar
Robert Bradshaw committed
8231

8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250
pyerr_occurred_withgil_utility_code= UtilityCode(
proto = """
static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */
""",
impl = """
static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void) {
  int err;
  #ifdef WITH_THREAD
  PyGILState_STATE _save = PyGILState_Ensure();
  #endif
  err = !!PyErr_Occurred();
  #ifdef WITH_THREAD
  PyGILState_Release(_save);
  #endif
  return err;
}
"""
)

Robert Bradshaw's avatar
Robert Bradshaw committed
8251
#------------------------------------------------------------------------------------
Robert Bradshaw's avatar
Robert Bradshaw committed
8252

8253 8254 8255 8256 8257 8258 8259 8260 8261 8262
raise_noneattr_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname);
""",
impl = '''
static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname) {
    PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", attrname);
}
''')

8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282
raise_noneindex_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void);
""",
impl = '''
static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
}
''')

raise_none_iter_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);
""",
impl = '''
static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
}
''')

8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302
raise_unbound_local_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname);
""",
impl = """
static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
    PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
}
""")

raise_closure_name_error_utility_code = UtilityCode(
proto = """
static CYTHON_INLINE void __Pyx_RaiseClosureNameError(const char *varname);
""",
impl = """
static CYTHON_INLINE void __Pyx_RaiseClosureNameError(const char *varname) {
    PyErr_Format(PyExc_NameError, "free variable '%s' referenced before assignment in enclosing scope", varname);
}
""")

8303 8304 8305 8306
#------------------------------------------------------------------------------------

getitem_dict_utility_code = UtilityCode(
proto = """
8307
#if PY_MAJOR_VERSION >= 3
8308
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
8309
    PyObject *value;
8310
    if (unlikely(d == Py_None)) {
8311 8312 8313
        __Pyx_RaiseNoneIndexingError();
        return NULL;
    }
8314 8315 8316
    value = PyDict_GetItemWithError(d, key);
    if (unlikely(!value)) {
        if (!PyErr_Occurred())
8317
            PyErr_SetObject(PyExc_KeyError, key);
8318
        return NULL;
8319
    }
8320 8321
    Py_INCREF(value);
    return value;
8322
}
8323 8324 8325
#else
    #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
#endif
8326
""",
8327 8328 8329
requires = [raise_noneindex_error_utility_code])

#------------------------------------------------------------------------------------
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8330

8331 8332 8333 8334
getitem_int_pyunicode_utility_code = UtilityCode(
proto = '''
#define __Pyx_GetItemInt_Unicode(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
                                               __Pyx_GetItemInt_Unicode_Fast(o, i) : \\
Stefan Behnel's avatar
Stefan Behnel committed
8335
                                               __Pyx_GetItemInt_Unicode_Generic(o, to_py_func(i)))
8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350

static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Fast(PyObject* ustring, Py_ssize_t i) {
    if (likely((0 <= i) & (i < PyUnicode_GET_SIZE(ustring)))) {
        return PyUnicode_AS_UNICODE(ustring)[i];
    } else if ((-PyUnicode_GET_SIZE(ustring) <= i) & (i < 0)) {
        i += PyUnicode_GET_SIZE(ustring);
        return PyUnicode_AS_UNICODE(ustring)[i];
    } else {
        PyErr_SetString(PyExc_IndexError, "string index out of range");
        return (Py_UNICODE)-1;
    }
}

static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Generic(PyObject* ustring, PyObject* j) {
    Py_UNICODE uchar;
8351
    PyObject *uchar_string;
8352
    if (!j) return (Py_UNICODE)-1;
8353
    uchar_string = PyObject_GetItem(ustring, j);
8354
    Py_DECREF(j);
8355 8356 8357
    if (!uchar_string) return (Py_UNICODE)-1;
    uchar = PyUnicode_AS_UNICODE(uchar_string)[0];
    Py_DECREF(uchar_string);
8358 8359
    return uchar;
}
8360
''')
8361

8362 8363
getitem_int_utility_code = UtilityCode(
proto = """
8364

8365
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
8366
    PyObject *r;
8367
    if (!j) return NULL;
8368 8369 8370 8371
    r = PyObject_GetItem(o, j);
    Py_DECREF(j);
    return r;
}
8372

8373 8374
""" + ''.join([
"""
8375 8376
#define __Pyx_GetItemInt_%(type)s(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
                                                    __Pyx_GetItemInt_%(type)s_Fast(o, i) : \\
8377 8378
                                                    __Pyx_GetItemInt_Generic(o, to_py_func(i)))

8379
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_%(type)s_Fast(PyObject *o, Py_ssize_t i) {
8380 8381 8382 8383 8384 8385
    if (likely(o != Py_None)) {
        if (likely((0 <= i) & (i < Py%(type)s_GET_SIZE(o)))) {
            PyObject *r = Py%(type)s_GET_ITEM(o, i);
            Py_INCREF(r);
            return r;
        }
8386 8387
        else if ((-Py%(type)s_GET_SIZE(o) <= i) & (i < 0)) {
            PyObject *r = Py%(type)s_GET_ITEM(o, Py%(type)s_GET_SIZE(o) + i);
8388 8389 8390
            Py_INCREF(r);
            return r;
        }
8391
    }
8392
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
8393
}
8394 8395
""" % {'type' : type_name} for type_name in ('List', 'Tuple')
]) + """
8396

8397 8398
#define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
                                                    __Pyx_GetItemInt_Fast(o, i) : \\
8399 8400
                                                    __Pyx_GetItemInt_Generic(o, to_py_func(i)))

8401
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) {
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8402
    PyObject *r;
Robert Bradshaw's avatar
Robert Bradshaw committed
8403
    if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8404 8405 8406
        r = PyList_GET_ITEM(o, i);
        Py_INCREF(r);
    }
Robert Bradshaw's avatar
Robert Bradshaw committed
8407
    else if (PyTuple_CheckExact(o) && ((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8408 8409 8410
        r = PyTuple_GET_ITEM(o, i);
        Py_INCREF(r);
    }
8411
    else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0))) {
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8412
        r = PySequence_GetItem(o, i);
8413
    }
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8414
    else {
8415
        r = __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8416 8417 8418 8419
    }
    return r;
}
""",
8420 8421
impl = """
""")
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8422

8423 8424


Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8425 8426
#------------------------------------------------------------------------------------

8427 8428
setitem_int_utility_code = UtilityCode(
proto = """
8429 8430
#define __Pyx_SetItemInt(o, i, v, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
                                                    __Pyx_SetItemInt_Fast(o, i, v) : \\
8431 8432
                                                    __Pyx_SetItemInt_Generic(o, to_py_func(i), v))

8433
static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8434
    int r;
8435 8436 8437 8438 8439 8440
    if (!j) return -1;
    r = PyObject_SetItem(o, j, v);
    Py_DECREF(j);
    return r;
}

8441
static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v) {
Stefan Behnel's avatar
Stefan Behnel committed
8442
    if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8443
        Py_INCREF(v);
8444
        Py_DECREF(PyList_GET_ITEM(o, i));
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8445 8446 8447
        PyList_SET_ITEM(o, i, v);
        return 1;
    }
8448 8449
    else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0)))
        return PySequence_SetItem(o, i, v);
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8450
    else {
8451
        PyObject *j = PyInt_FromSsize_t(i);
8452
        return __Pyx_SetItemInt_Generic(o, j, v);
Dag Sverre Seljebotn's avatar
Merge  
Dag Sverre Seljebotn committed
8453 8454 8455
    }
}
""",
8456 8457 8458
impl = """
""")

8459 8460
#------------------------------------------------------------------------------------

8461 8462
delitem_int_utility_code = UtilityCode(
proto = """
8463 8464
#define __Pyx_DelItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
                                                    __Pyx_DelItemInt_Fast(o, i) : \\
8465 8466
                                                    __Pyx_DelItem_Generic(o, to_py_func(i)))

8467
static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
8468
    int r;
8469 8470 8471 8472 8473 8474
    if (!j) return -1;
    r = PyObject_DelItem(o, j);
    Py_DECREF(j);
    return r;
}

8475
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i) {
8476 8477
    if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && likely(i >= 0))
        return PySequence_DelItem(o, i);
8478
    else {
8479
        PyObject *j = PyInt_FromSsize_t(i);
8480
        return __Pyx_DelItem_Generic(o, j);
8481 8482 8483 8484 8485 8486 8487 8488
    }
}
""",
impl = """
""")

#------------------------------------------------------------------------------------

8489 8490
raise_too_many_values_to_unpack = UtilityCode(
proto = """
8491
static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
8492 8493
""",
impl = '''
8494 8495 8496 8497 8498 8499 8500
static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
    PyErr_Format(PyExc_ValueError,
        #if PY_VERSION_HEX < 0x02050000
            "too many values to unpack (expected %d)", (int)expected);
        #else
            "too many values to unpack (expected %zd)", expected);
        #endif
8501 8502 8503 8504 8505
}
''')

raise_need_more_values_to_unpack = UtilityCode(
proto = """
8506
static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
8507 8508
""",
impl = '''
8509
static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521
    PyErr_Format(PyExc_ValueError,
        #if PY_VERSION_HEX < 0x02050000
                 "need more than %d value%s to unpack", (int)index,
        #else
                 "need more than %zd value%s to unpack", index,
        #endif
                 (index == 1) ? "" : "s");
}
''')

#------------------------------------------------------------------------------------

8522 8523 8524
tuple_unpacking_error_code = UtilityCode(
proto = """
static void __Pyx_UnpackTupleError(PyObject *, Py_ssize_t index); /*proto*/
8525
""",
8526 8527 8528 8529 8530 8531 8532
impl = """
static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
    if (t == Py_None) {
      __Pyx_RaiseNoneNotIterableError();
    } else if (PyTuple_GET_SIZE(t) < index) {
      __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
    } else {
8533
      __Pyx_RaiseTooManyValuesError(index);
8534 8535
    }
}
8536
""",
8537 8538 8539 8540 8541
requires = [raise_none_iter_error_utility_code,
            raise_need_more_values_to_unpack,
            raise_too_many_values_to_unpack]
)

8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555
unpacking_utility_code = UtilityCode(
proto = """
static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
""",
impl = """
static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
    PyObject *item;
    if (!(item = PyIter_Next(iter))) {
        if (!PyErr_Occurred()) {
            __Pyx_RaiseNeedMoreValuesError(index);
        }
    }
    return item;
}
8556 8557 8558
""",
requires = [raise_need_more_values_to_unpack]
)
8559

8560 8561 8562 8563 8564 8565 8566 8567
iternext_unpacking_end_utility_code = UtilityCode(
proto = """
static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected); /*proto*/
""",
impl = """
static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
    if (unlikely(retval)) {
        Py_DECREF(retval);
8568
        __Pyx_RaiseTooManyValuesError(expected);
8569
        return -1;
8570 8571 8572 8573 8574 8575 8576
    } else if (PyErr_Occurred()) {
        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
            PyErr_Clear();
            return 0;
        } else {
            return -1;
        }
8577
    }
8578
    return 0;
8579 8580
}
""",
8581
requires = [raise_too_many_values_to_unpack]
8582
)
Robert Bradshaw's avatar
Robert Bradshaw committed
8583

8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595
#------------------------------------------------------------------------------------

# CPython supports calling functions with non-dict kwargs by
# converting them to a dict first

kwargs_call_utility_code = UtilityCode(
proto = """
static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject*, PyObject*, PyObject*); /*proto*/
""",
impl = """
static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject *callable, PyObject *args, PyObject *kwargs) {
    PyObject* result;
8596
    if (likely(PyDict_Check(kwargs))) {
8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607
        return PyEval_CallObjectWithKeywords(callable, args, kwargs);
    } else {
        PyObject* real_dict;
        real_dict = PyObject_CallFunctionObjArgs((PyObject*)&PyDict_Type, kwargs, NULL);
        if (unlikely(!real_dict))
            return NULL;
        result = PyEval_CallObjectWithKeywords(callable, args, real_dict);
        Py_DECREF(real_dict);
        return result; /* may be NULL */
    }
}
8608
""",
8609 8610
)

Robert Bradshaw's avatar
Robert Bradshaw committed
8611 8612 8613 8614 8615

#------------------------------------------------------------------------------------

int_pow_utility_code = UtilityCode(
proto="""
8616
static CYTHON_INLINE %(type)s %(func_name)s(%(type)s, %(type)s); /* proto */
Robert Bradshaw's avatar
Robert Bradshaw committed
8617 8618
""",
impl="""
8619
static CYTHON_INLINE %(type)s %(func_name)s(%(type)s b, %(type)s e) {
Robert Bradshaw's avatar
Robert Bradshaw committed
8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640
    %(type)s t = b;
    switch (e) {
        case 3:
            t *= b;
        case 2:
            t *= b;
        case 1:
            return t;
        case 0:
            return 1;
    }
    if (unlikely(e<0)) return 0;
    t = 1;
    while (likely(e)) {
        t *= (b * (e&1)) | ((~e)&1);    /* 1 or b */
        b *= b;
        e >>= 1;
    }
    return t;
}
""")
8641 8642 8643

# ------------------------------ Division ------------------------------------

8644 8645
div_int_utility_code = UtilityCode(
proto="""
8646
static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s, %(type)s); /* proto */
8647 8648
""",
impl="""
8649
static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s a, %(type)s b) {
8650 8651 8652 8653 8654
    %(type)s q = a / b;
    %(type)s r = a - q*b;
    q -= ((r != 0) & ((r ^ b) < 0));
    return q;
}
8655 8656
""")

8657
mod_int_utility_code = UtilityCode(
8658
proto="""
8659
static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
8660 8661
""",
impl="""
8662
static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
8663 8664 8665
    %(type)s r = a %% b;
    r += ((r != 0) & ((r ^ b) < 0)) * b;
    return r;
8666 8667 8668
}
""")

8669
mod_float_utility_code = UtilityCode(
8670
proto="""
8671
static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
8672 8673
""",
impl="""
8674
static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
8675 8676 8677
    %(type)s r = fmod%(math_h_modifier)s(a, b);
    r += ((r != 0) & ((r < 0) ^ (b < 0))) * b;
    return r;
8678 8679
}
""")
Robert Bradshaw's avatar
Robert Bradshaw committed
8680

8681
cdivision_warning_utility_code = UtilityCode(
Robert Bradshaw's avatar
Robert Bradshaw committed
8682
proto="""
8683
static int __Pyx_cdivision_warning(const char *, int); /* proto */
Robert Bradshaw's avatar
Robert Bradshaw committed
8684 8685
""",
impl="""
8686
static int __Pyx_cdivision_warning(const char *filename, int lineno) {
8687
    return PyErr_WarnExplicit(PyExc_RuntimeWarning,
8688
                              "division with oppositely signed operands, C and Python semantics differ",
8689 8690
                              filename,
                              lineno,
8691
                              __Pyx_MODULE_NAME,
8692
                              NULL);
Robert Bradshaw's avatar
Robert Bradshaw committed
8693
}
8694
""")
8695 8696 8697 8698

# from intobject.c
division_overflow_test_code = UtilityCode(
proto="""
Vitja Makarov's avatar
Vitja Makarov committed
8699 8700
#define UNARY_NEG_WOULD_OVERFLOW(x)    \
        (((x) < 0) & ((unsigned long)(x) == 0-(unsigned long)(x)))
8701
""")
Robert Bradshaw's avatar
Robert Bradshaw committed
8702 8703 8704 8705 8706 8707 8708 8709 8710 8711


binding_cfunc_utility_code = UtilityCode(
proto="""
#define %(binding_cfunc)s_USED 1

typedef struct {
    PyCFunctionObject func;
} %(binding_cfunc)s_object;

8712 8713
static PyTypeObject %(binding_cfunc)s_type;
static PyTypeObject *%(binding_cfunc)s = NULL;
Robert Bradshaw's avatar
Robert Bradshaw committed
8714

8715
static PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module); /* proto */
Robert Bradshaw's avatar
Robert Bradshaw committed
8716 8717
#define %(binding_cfunc)s_New(ml, self) %(binding_cfunc)s_NewEx(ml, self, NULL)

8718
static int %(binding_cfunc)s_init(void); /* proto */
Robert Bradshaw's avatar
Robert Bradshaw committed
8719 8720 8721
""" % Naming.__dict__,
impl="""

8722
static PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
Vitja Makarov's avatar
Vitja Makarov committed
8723
    %(binding_cfunc)s_object *op = PyObject_GC_New(%(binding_cfunc)s_object, %(binding_cfunc)s);
Robert Bradshaw's avatar
Robert Bradshaw committed
8724 8725
    if (op == NULL)
        return NULL;
Vitja Makarov's avatar
Vitja Makarov committed
8726 8727 8728 8729 8730 8731 8732
    op->func.m_ml = ml;
    Py_XINCREF(self);
    op->func.m_self = self;
    Py_XINCREF(module);
    op->func.m_module = module;
    PyObject_GC_Track(op);
    return (PyObject *)op;
Robert Bradshaw's avatar
Robert Bradshaw committed
8733 8734 8735
}

static void %(binding_cfunc)s_dealloc(%(binding_cfunc)s_object *m) {
Vitja Makarov's avatar
Vitja Makarov committed
8736 8737 8738
    PyObject_GC_UnTrack(m);
    Py_XDECREF(m->func.m_self);
    Py_XDECREF(m->func.m_module);
Robert Bradshaw's avatar
Robert Bradshaw committed
8739 8740 8741 8742
    PyObject_GC_Del(m);
}

static PyObject *%(binding_cfunc)s_descr_get(PyObject *func, PyObject *obj, PyObject *type) {
Vitja Makarov's avatar
Vitja Makarov committed
8743 8744 8745
    if (obj == Py_None)
            obj = NULL;
    return PyMethod_New(func, obj, type);
Robert Bradshaw's avatar
Robert Bradshaw committed
8746 8747
}

8748
static int %(binding_cfunc)s_init(void) {
Robert Bradshaw's avatar
Robert Bradshaw committed
8749
    %(binding_cfunc)s_type = PyCFunction_Type;
8750
    %(binding_cfunc)s_type.tp_name = __Pyx_NAMESTR("cython_binding_builtin_function_or_method");
Robert Bradshaw's avatar
Robert Bradshaw committed
8751 8752 8753 8754 8755 8756 8757 8758 8759 8760
    %(binding_cfunc)s_type.tp_dealloc = (destructor)%(binding_cfunc)s_dealloc;
    %(binding_cfunc)s_type.tp_descr_get = %(binding_cfunc)s_descr_get;
    if (PyType_Ready(&%(binding_cfunc)s_type) < 0) {
        return -1;
    }
    %(binding_cfunc)s = &%(binding_cfunc)s_type;
    return 0;

}
""" % Naming.__dict__)
8761 8762 8763

generator_utility_code = UtilityCode(
proto="""
8764 8765 8766 8767
static PyObject *__Pyx_Generator_Next(PyObject *self);
static PyObject *__Pyx_Generator_Send(PyObject *self, PyObject *value);
static PyObject *__Pyx_Generator_Close(PyObject *self);
static PyObject *__Pyx_Generator_Throw(PyObject *gen, PyObject *args, CYTHON_UNUSED PyObject *kwds);
8768

8769
typedef PyObject *(*__pyx_generator_body_t)(PyObject *, PyObject *);
8770 8771
""",
impl="""
8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782
static CYTHON_INLINE void __Pyx_Generator_ExceptionClear(struct __pyx_Generator_object *self)
{
    Py_XDECREF(self->exc_type);
    Py_XDECREF(self->exc_value);
    Py_XDECREF(self->exc_traceback);

    self->exc_type = NULL;
    self->exc_value = NULL;
    self->exc_traceback = NULL;
}

8783
static CYTHON_INLINE PyObject *__Pyx_Generator_SendEx(struct __pyx_Generator_object *self, PyObject *value)
8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801
{
    PyObject *retval;

    if (self->is_running) {
        PyErr_SetString(PyExc_ValueError,
                        "generator already executing");
        return NULL;
    }

    if (self->resume_label == 0) {
        if (value && value != Py_None) {
            PyErr_SetString(PyExc_TypeError,
                            "can't send non-None value to a "
                            "just-started generator");
            return NULL;
        }
    }

8802 8803 8804 8805 8806
    if (self->resume_label == -1) {
        PyErr_SetNone(PyExc_StopIteration);
        return NULL;
    }

8807 8808 8809 8810 8811 8812

    if (value)
        __Pyx_ExceptionSwap(&self->exc_type, &self->exc_value, &self->exc_traceback);
    else
        __Pyx_Generator_ExceptionClear(self);

8813
    self->is_running = 1;
8814
    retval = self->body((PyObject *) self, value);
8815 8816
    self->is_running = 0;

8817 8818 8819 8820 8821
    if (retval)
        __Pyx_ExceptionSwap(&self->exc_type, &self->exc_value, &self->exc_traceback);
    else
        __Pyx_Generator_ExceptionClear(self);

8822 8823 8824
    return retval;
}

8825
static PyObject *__Pyx_Generator_Next(PyObject *self)
8826
{
8827
    return __Pyx_Generator_SendEx((struct __pyx_Generator_object *) self, Py_None);
8828 8829
}

8830
static PyObject *__Pyx_Generator_Send(PyObject *self, PyObject *value)
8831
{
8832
    return __Pyx_Generator_SendEx((struct __pyx_Generator_object *) self, value);
8833
}
Vitja Makarov's avatar
Vitja Makarov committed
8834

8835
static PyObject *__Pyx_Generator_Close(PyObject *self)
Vitja Makarov's avatar
Vitja Makarov committed
8836
{
8837
    struct __pyx_Generator_object *generator = (struct __pyx_Generator_object *) self;
Vitja Makarov's avatar
Vitja Makarov committed
8838
    PyObject *retval;
8839 8840 8841
#if PY_VERSION_HEX < 0x02050000
    PyErr_SetNone(PyExc_StopIteration);
#else
Vitja Makarov's avatar
Vitja Makarov committed
8842
    PyErr_SetNone(PyExc_GeneratorExit);
8843
#endif
8844
    retval = __Pyx_Generator_SendEx(generator, NULL);
Vitja Makarov's avatar
Vitja Makarov committed
8845 8846 8847 8848 8849 8850
    if (retval) {
        Py_DECREF(retval);
        PyErr_SetString(PyExc_RuntimeError,
                        "generator ignored GeneratorExit");
        return NULL;
    }
8851 8852 8853
#if PY_VERSION_HEX < 0x02050000
    if (PyErr_ExceptionMatches(PyExc_StopIteration))
#else
Vitja Makarov's avatar
Vitja Makarov committed
8854 8855
    if (PyErr_ExceptionMatches(PyExc_StopIteration)
        || PyErr_ExceptionMatches(PyExc_GeneratorExit))
8856
#endif
Vitja Makarov's avatar
Vitja Makarov committed
8857 8858 8859 8860 8861 8862 8863
    {
        PyErr_Clear();          /* ignore these errors */
        Py_INCREF(Py_None);
        return Py_None;
    }
    return NULL;
}
8864

8865
static PyObject *__Pyx_Generator_Throw(PyObject *self, PyObject *args, CYTHON_UNUSED PyObject *kwds)
8866
{
8867
    struct __pyx_Generator_object *generator = (struct __pyx_Generator_object *) self;
8868 8869 8870 8871
    PyObject *typ;
    PyObject *tb = NULL;
    PyObject *val = NULL;

8872
    if (!PyArg_UnpackTuple(args, (char *)"throw", 1, 3, &typ, &val, &tb))
8873
        return NULL;
8874
    __Pyx_Raise(typ, val, tb, NULL);
8875
    return __Pyx_Generator_SendEx(generator, NULL);
8876
}
Stefan Behnel's avatar
Stefan Behnel committed
8877 8878
""",
proto_block='utility_code_proto_before_types',
8879
requires=[Nodes.raise_utility_code, Nodes.swap_exception_utility_code],
Stefan Behnel's avatar
Stefan Behnel committed
8880
)