Nodes.py 153 KB
Newer Older
William Stein's avatar
William Stein committed
1 2 3 4
#
#   Pyrex - Parse tree nodes
#

5
import string, sys, os, time
William Stein's avatar
William Stein committed
6 7

import Code
8
from Errors import error, warning, InternalError
William Stein's avatar
William Stein committed
9 10
import Naming
import PyrexTypes
11
import TypeSlots
12
from PyrexTypes import py_object_type, error_type, CTypedefType, CFuncType
William Stein's avatar
William Stein committed
13 14
from Symtab import ModuleScope, LocalScope, \
    StructOrUnionScope, PyClassScope, CClassScope
William Stein's avatar
William Stein committed
15
from Cython.Utils import open_new_file, replace_suffix
William Stein's avatar
William Stein committed
16
import Options
17
import ControlFlow
William Stein's avatar
William Stein committed
18 19 20

from DebugFlags import debug_disposal_code

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
absolute_path_length = len(os.path.abspath('.')) 

def relative_position(pos):
    """
    We embed the relative filename in the generated C file, since we
    don't want to have to regnerate and compile all the source code
    whenever the Python install directory moves (which could happen,
    e.g,. when distributing binaries.)
    
    INPUT:
        a position tuple -- (absolute filename, line number column position)

    OUTPUT:
        relative filename
        line number

    AUTHOR: William Stein
    """
    return (pos[0][absolute_path_length+1:], pos[1])
        

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
class AttributeAccessor:
    """Used as the result of the Node.get_children_accessors() generator"""
    def __init__(self, obj, attrname):
        self.obj = obj
        self.attrname = attrname
    def get(self):
        try:
            return getattr(self.obj, self.attrname)
        except AttributeError:
            return None
    def set(self, value):
        setattr(self.obj, self.attrname, value)
    def name(self):
        return self.attrname

William Stein's avatar
William Stein committed
57 58 59 60 61 62 63
class Node:
    #  pos         (string, int, int)   Source file position
    #  is_name     boolean              Is a NameNode
    #  is_literal  boolean              Is a ConstNode
    
    is_name = 0
    is_literal = 0
64 65 66

    # All descandants should set child_attrs (see get_child_accessors)    
    child_attrs = None
William Stein's avatar
William Stein committed
67 68 69 70 71
    
    def __init__(self, pos, **kw):
        self.pos = pos
        self.__dict__.update(kw)
    
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 116 117 118 119 120 121
    def get_child_accessors(self):
        """Returns an iterator over the children of the Node. Each member in the
        iterated list is an object with get(), set(value), and name() methods,
        which can be used to fetch and replace the child and query the name
        the relation this node has with the child. For instance, for an
        assignment node, this code:
        
        for child in assignment_node.get_child_accessors():
            print(child.name())
            child.set(i_node)
        
        will print "lhs", "rhs", and change the assignment statement to "i = i"
        (assuming that i_node is a node able to represent the variable i in the
        tree).
        
        Any kind of objects can in principle be returned, but the typical
        candidates are either Node instances or lists of node instances.
        
        The object returned in each iteration stage can only be used until the
        iterator is advanced to the next child attribute. (However, the objects
        returned by the get() function can be kept).
        
        Typically, a Node instance will have other interesting and potentially
        hierarchical attributes as well. These must be explicitly accessed -- this
        method only provides access to attributes that are deemed to naturally
        belong in the parse tree.
        
        Descandant classes can either specify child_attrs, override get_child_attrs,
        or override this method directly in order to provide access to their
        children. All descendants of Node *must* declare their children -- leaf nodes
        should simply declare "child_attrs = []".
        """
        attrnames = self.get_child_attrs()
        if attrnames is None:
            raise InternalError("Children access not implemented for %s" % \
                self.__class__.__name__)
        for name in attrnames:
            a = AttributeAccessor(self, name)
            yield a
            # Not really needed, but one wants to enforce clients not to
            # hold on to iterated objects, in case more advanced iteration
            # is needed later
            a.attrname = None
    
    def get_child_attrs(self):
        """Utility method for more easily implementing get_child_accessors.
        If you override get_child_accessors then this method is not used."""
        return self.child_attrs
    
    
William Stein's avatar
William Stein committed
122
    #
123
    #  There are 4 phases of parse tree processing, applied in order to
William Stein's avatar
William Stein committed
124 125
    #  all the statements in a given scope-block:
    #
126 127 128 129
    #  (0) analyse_control_flow
    #        Create the control flow tree into which state can be asserted and
    #        queried.
    #
William Stein's avatar
William Stein committed
130 131 132 133 134
    #  (1) analyse_declarations
    #        Make symbol table entries for all declarations at the current
    #        level, both explicit (def, cdef, etc.) and implicit (assignment
    #        to an otherwise undeclared name).
    #
135
    #  (2) analyse_expressions
William Stein's avatar
William Stein committed
136 137 138 139 140 141 142
    #         Determine the result types of expressions and fill in the
    #         'type' attribute of each ExprNode. Insert coercion nodes into the
    #         tree where needed to convert to and from Python objects. 
    #         Allocate temporary locals for intermediate results. Fill
    #         in the 'result_code' attribute of each ExprNode with a C code
    #         fragment.
    #
143
    #  (3) generate_code
William Stein's avatar
William Stein committed
144 145 146 147 148
    #         Emit C code for all declarations, statements and expressions.
    #         Recursively applies the 3 processing phases to the bodies of
    #         functions.
    #
    
149 150 151
    def analyse_control_flow(self, env):
        pass
    
William Stein's avatar
William Stein committed
152 153 154 155 156 157 158 159 160 161
    def analyse_declarations(self, env):
        pass
    
    def analyse_expressions(self, env):
        raise InternalError("analyse_expressions not implemented for %s" % \
            self.__class__.__name__)
    
    def generate_code(self, code):
        raise InternalError("generate_code not implemented for %s" % \
            self.__class__.__name__)
162 163 164 165 166
            
    def annotate(self, code):
        # mro does the wrong thing
        if isinstance(self, BlockNode):
            self.body.annotate(code)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            
    def end_pos(self):
        try:
            return self._end_pos
        except AttributeError:
            children = [acc.get() for acc in self.get_child_accessors()]
            if len(children) == 0:
                self._end_pos = self.pos
            else:
                # Sometimes lists, sometimes nodes
                flat = []
                for child in children:
                    if child is None:
                        pass
                    elif isinstance(child, list):
                        flat += child
                    else:
                        flat.append(child)
                self._end_pos = max([child.end_pos() for child in flat])
            return self._end_pos
William Stein's avatar
William Stein committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218


class BlockNode:
    #  Mixin class for nodes representing a declaration block.

    def generate_const_definitions(self, env, code):
        if env.const_entries:
            code.putln("")
            for entry in env.const_entries:
                if not entry.is_interned:
                    code.put_var_declaration(entry, static = 1)
    
    def generate_interned_name_decls(self, env, code):
        #  Flush accumulated interned names from the global scope
        #  and generate declarations for them.
        genv = env.global_scope()
        intern_map = genv.intern_map
        names = genv.interned_names
        if names:
            code.putln("")
            for name in names:
                code.putln(
                    "static PyObject *%s;" % intern_map[name])
            del names[:]
    
    def generate_py_string_decls(self, env, code):
        entries = env.pystring_entries
        if entries:
            code.putln("")
            for entry in entries:
                code.putln(
                    "static PyObject *%s;" % entry.pystring_cname)
219 220 221 222 223 224 225 226 227 228 229 230
    
    def generate_interned_num_decls(self, env, code):
        #  Flush accumulated interned nums from the global scope
        #  and generate declarations for them.
        genv = env.global_scope()
        entries = genv.interned_nums
        if entries:
            code.putln("")
            for entry in entries:
                code.putln(
                    "static PyObject *%s;" % entry.cname)
            del entries[:]
William Stein's avatar
William Stein committed
231

232
    def generate_cached_builtins_decls(self, env, code):
233
        entries = env.global_scope().undeclared_cached_builtins
234 235 236 237 238
        if len(entries) > 0:
            code.putln("")
        for entry in entries:
            code.putln("static PyObject *%s;" % entry.cname)
        del entries[:]
239
        
William Stein's avatar
William Stein committed
240 241 242 243

class StatListNode(Node):
    # stats     a list of StatNode
    
244 245
    child_attrs = ["stats"]
    
246 247 248 249
    def analyse_control_flow(self, env):
        for stat in self.stats:
            stat.analyse_control_flow(env)

William Stein's avatar
William Stein committed
250 251 252 253 254 255 256 257 258 259
    def analyse_declarations(self, env):
        #print "StatListNode.analyse_declarations" ###
        for stat in self.stats:
            stat.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        #print "StatListNode.analyse_expressions" ###
        for stat in self.stats:
            stat.analyse_expressions(env)
    
260
    def generate_function_definitions(self, env, code, transforms):
William Stein's avatar
William Stein committed
261 262
        #print "StatListNode.generate_function_definitions" ###
        for stat in self.stats:
263
            stat.generate_function_definitions(env, code, transforms)
William Stein's avatar
William Stein committed
264 265 266 267 268 269
            
    def generate_execution_code(self, code):
        #print "StatListNode.generate_execution_code" ###
        for stat in self.stats:
            code.mark_pos(stat.pos)
            stat.generate_execution_code(code)
270 271 272 273
            
    def annotate(self, code):
        for stat in self.stats:
            stat.annotate(code)
William Stein's avatar
William Stein committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    

class StatNode(Node):
    #
    #  Code generation for statements is split into the following subphases:
    #
    #  (1) generate_function_definitions
    #        Emit C code for the definitions of any structs,
    #        unions, enums and functions defined in the current
    #        scope-block.
    #
    #  (2) generate_execution_code
    #        Emit C code for executable statements.
    #
    
289
    def generate_function_definitions(self, env, code, transforms):
William Stein's avatar
William Stein committed
290 291 292 293 294 295 296 297 298 299 300
        pass
    
    def generate_execution_code(self, code):
        raise InternalError("generate_execution_code not implemented for %s" % \
            self.__class__.__name__)


class CDefExternNode(StatNode):
    #  include_file   string or None
    #  body           StatNode
    
301 302
    child_attrs = ["body"]
    
William Stein's avatar
William Stein committed
303 304 305 306 307 308 309 310 311 312 313 314 315
    def analyse_declarations(self, env):
        if self.include_file:
            env.add_include_file(self.include_file)
        old_cinclude_flag = env.in_cinclude
        env.in_cinclude = 1
        self.body.analyse_declarations(env)
        env.in_cinclude = old_cinclude_flag
    
    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass
316 317 318

    def annotate(self, code):
        self.body.annotate(code)
William Stein's avatar
William Stein committed
319 320 321 322 323 324 325 326 327 328 329 330
        

class CDeclaratorNode(Node):
    # Part of a C declaration.
    #
    # Processing during analyse_declarations phase:
    #
    #   analyse
    #      Returns (name, type) pair where name is the
    #      CNameDeclaratorNode of the name being declared 
    #      and type is the type it is being declared as.
    #
331 332 333
    #  calling_convention  string   Calling convention of CFuncDeclaratorNode
    #                               for which this is a base 

334 335
    child_attrs = []

336 337
    calling_convention = ""

338 339 340 341 342
    def analyse_expressions(self, env):
        pass

    def generate_execution_code(self, env):
        pass
William Stein's avatar
William Stein committed
343 344 345


class CNameDeclaratorNode(CDeclaratorNode):
346 347 348
    #  name   string             The Pyrex name being declared
    #  cname  string or None     C name, if specified
    #  rhs    ExprNode or None   the value assigned on declaration
William Stein's avatar
William Stein committed
349
    
350
    child_attrs = []
351
    
William Stein's avatar
William Stein committed
352
    def analyse(self, base_type, env):
353
        self.type = base_type
William Stein's avatar
William Stein committed
354
        return self, base_type
355 356 357 358
        
    def analyse_expressions(self, env):
        self.entry = env.lookup(self.name)
        if self.rhs is not None:
359
            env.control_flow.set_state(self.rhs.end_pos(), (self.entry.name, 'initalized'), True)
Robert Bradshaw's avatar
Robert Bradshaw committed
360
            env.control_flow.set_state(self.rhs.end_pos(), (self.entry.name, 'source'), 'assignment')
361
            self.entry.used = 1
362 363 364 365 366 367 368
            if self.type.is_pyobject:
                self.entry.init_to_none = False
                self.entry.init = 0
            self.rhs.analyse_types(env)
            self.rhs = self.rhs.coerce_to(self.type, env)
            self.rhs.allocate_temps(env)
            self.rhs.release_temp(env)
William Stein's avatar
William Stein committed
369

370 371 372 373 374 375 376 377
    def generate_execution_code(self, code):
        if self.rhs is not None:
            self.rhs.generate_evaluation_code(code)
            if self.type.is_pyobject:
                self.rhs.make_owned_reference(code)
            code.putln('%s = %s;' % (self.entry.cname, self.rhs.result_as(self.entry.type)))
            self.rhs.generate_post_assignment_code(code)
            code.putln()
William Stein's avatar
William Stein committed
378 379 380 381

class CPtrDeclaratorNode(CDeclaratorNode):
    # base     CDeclaratorNode
    
382 383
    child_attrs = ["base"]

William Stein's avatar
William Stein committed
384 385 386 387 388 389 390
    def analyse(self, base_type, env):
        if base_type.is_pyobject:
            error(self.pos,
                "Pointer base type cannot be a Python object")
        ptr_type = PyrexTypes.c_ptr_type(base_type)
        return self.base.analyse(ptr_type, env)
        
391 392 393 394 395
    def analyse_expressions(self, env):
        self.base.analyse_expressions(env)

    def generate_execution_code(self, env):
        self.base.generate_execution_code(env)
William Stein's avatar
William Stein committed
396 397 398 399

class CArrayDeclaratorNode(CDeclaratorNode):
    # base        CDeclaratorNode
    # dimension   ExprNode
400 401

    child_attrs = ["base", "dimension"]
William Stein's avatar
William Stein committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    
    def analyse(self, base_type, env):
        if self.dimension:
            self.dimension.analyse_const_expression(env)
            if not self.dimension.type.is_int:
                error(self.dimension.pos, "Array dimension not integer")
            size = self.dimension.result_code
        else:
            size = None
        if not base_type.is_complete():
            error(self.pos,
                "Array element type '%s' is incomplete" % base_type)
        if base_type.is_pyobject:
            error(self.pos,
                "Array element cannot be a Python object")
417 418 419
        if base_type.is_cfunction:
            error(self.pos,
                "Array element cannot be a function")
William Stein's avatar
William Stein committed
420 421 422 423 424 425 426 427 428 429
        array_type = PyrexTypes.c_array_type(base_type, size)
        return self.base.analyse(array_type, env)


class CFuncDeclaratorNode(CDeclaratorNode):
    # base             CDeclaratorNode
    # args             [CArgDeclNode]
    # has_varargs      boolean
    # exception_value  ConstNode
    # exception_check  boolean    True if PyErr_Occurred check needed
430 431
    # nogil            boolean    Can be called without gil
    # with_gil         boolean    Acquire gil around function body
432
    
433 434
    child_attrs = ["base", "args", "exception_value"]

435
    overridable = 0
436
    optional_arg_count = 0
William Stein's avatar
William Stein committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

    def analyse(self, return_type, env):
        func_type_args = []
        for arg_node in self.args:
            name_declarator, type = arg_node.analyse(env)
            name = name_declarator.name
            if name_declarator.cname:
                error(self.pos, 
                    "Function argument cannot have C name specification")
            # Turn *[] argument into **
            if type.is_array:
                type = PyrexTypes.c_ptr_type(type.base_type)
            # Catch attempted C-style func(void) decl
            if type.is_void:
                error(arg_node.pos, "Function argument cannot be void")
452 453 454
            if type.is_pyobject and self.nogil:
                error(self.pos,
                    "Function with Python argument cannot be declared nogil")
William Stein's avatar
William Stein committed
455 456 457
            func_type_args.append(
                PyrexTypes.CFuncTypeArg(name, type, arg_node.pos))
            if arg_node.default:
458
                self.optional_arg_count += 1
459 460
            elif self.optional_arg_count:
                error(self.pos, "Non-default argument follows default argument")
461 462 463
        
        if self.optional_arg_count:
            scope = StructOrUnionScope()
464
            scope.declare_var('%sn' % Naming.pyrex_prefix, PyrexTypes.c_int_type, self.pos)
465 466
            for arg in func_type_args[len(func_type_args)-self.optional_arg_count:]:
                scope.declare_var(arg.name, arg.type, arg.pos, allow_pyobject = 1)
467
            struct_cname = env.mangle(Naming.opt_arg_prefix, self.base.name)
468 469 470 471 472 473
            self.op_args_struct = env.global_scope().declare_struct_or_union(name = struct_cname,
                                        kind = 'struct',
                                        scope = scope,
                                        typedef_flag = 0,
                                        pos = self.pos,
                                        cname = struct_cname)
474
            self.op_args_struct.defined_in_pxd = 1
475 476
            self.op_args_struct.used = 1
        
William Stein's avatar
William Stein committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490
        exc_val = None
        exc_check = 0
        if return_type.is_pyobject \
            and (self.exception_value or self.exception_check):
                error(self.pos,
                    "Exception clause not allowed for function returning Python object")
        else:
            if self.exception_value:
                self.exception_value.analyse_const_expression(env)
                exc_val = self.exception_value.result_code
                if not return_type.assignable_from(self.exception_value.type):
                    error(self.exception_value.pos,
                        "Exception value incompatible with function return type")
            exc_check = self.exception_check
491 492 493 494 495 496 497 498 499
        if return_type.is_pyobject and self.nogil:
            error(self.pos,
                "Function with Python return type cannot be declared nogil")
        if return_type.is_array:
            error(self.pos,
                "Function cannot return an array")
        if return_type.is_cfunction:
            error(self.pos,
                "Function cannot return a function")
William Stein's avatar
William Stein committed
500 501
        func_type = PyrexTypes.CFuncType(
            return_type, func_type_args, self.has_varargs, 
502
            optional_arg_count = self.optional_arg_count,
503
            exception_value = exc_val, exception_check = exc_check,
504
            calling_convention = self.base.calling_convention,
505
            nogil = self.nogil, with_gil = self.with_gil, is_overridable = self.overridable)
506
        if self.optional_arg_count:
507
            func_type.op_arg_struct = PyrexTypes.c_ptr_type(self.op_args_struct.type)
William Stein's avatar
William Stein committed
508 509 510 511 512 513 514 515 516 517 518
        return self.base.analyse(func_type, env)


class CArgDeclNode(Node):
    # Item in a function declaration argument list.
    #
    # base_type      CBaseTypeNode
    # declarator     CDeclaratorNode
    # not_none       boolean            Tagged with 'not None'
    # default        ExprNode or None
    # default_entry  Symtab.Entry       Entry for the variable holding the default value
519
    # default_result_code string        cname or code fragment for default value
William Stein's avatar
William Stein committed
520
    # is_self_arg    boolean            Is the "self" arg of an extension type method
521 522
    # is_kw_only     boolean            Is a keyword-only argument

523 524
    child_attrs = ["base_type", "declarator", "default"]

William Stein's avatar
William Stein committed
525
    is_self_arg = 0
526 527
    is_generic = 1

William Stein's avatar
William Stein committed
528
    def analyse(self, env):
529
        #print "CArgDeclNode.analyse: is_self_arg =", self.is_self_arg ###
William Stein's avatar
William Stein committed
530 531 532
        base_type = self.base_type.analyse(env)
        return self.declarator.analyse(base_type, env)

533 534 535 536
    def annotate(self, code):
        if self.default:
            self.default.annotate(code)

William Stein's avatar
William Stein committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

class CBaseTypeNode(Node):
    # Abstract base class for C base type nodes.
    #
    # Processing during analyse_declarations phase:
    #
    #   analyse
    #     Returns the type.
    
    pass


class CSimpleBaseTypeNode(CBaseTypeNode):
    # name             string
    # module_path      [string]     Qualifying name components
    # is_basic_c_type  boolean
    # signed           boolean
    # longness         integer
    # is_self_arg      boolean      Is self argument of C method

557 558
    child_attrs = []
    
William Stein's avatar
William Stein committed
559 560
    def analyse(self, env):
        # Return type descriptor.
561
        #print "CSimpleBaseTypeNode.analyse: is_self_arg =", self.is_self_arg ###
William Stein's avatar
William Stein committed
562 563 564 565 566 567 568 569 570
        type = None
        if self.is_basic_c_type:
            type = PyrexTypes.simple_c_type(self.signed, self.longness, self.name)
            if not type:
                error(self.pos, "Unrecognised type modifier combination")
        elif self.name == "object" and not self.module_path:
            type = py_object_type
        elif self.name is None:
            if self.is_self_arg and env.is_c_class_scope:
571
                #print "CSimpleBaseTypeNode.analyse: defaulting to parent type" ###
William Stein's avatar
William Stein committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
                type = env.parent_type
            else:
                type = py_object_type
        else:
            scope = env
            for name in self.module_path:
                entry = scope.find(name, self.pos)
                if entry and entry.as_module:
                    scope = entry.as_module
                else:
                    if entry:
                        error(self.pos, "'%s' is not a cimported module" % name)
                    scope = None
                    break
            if scope:
587 588
                if scope.is_c_class_scope:
                    scope = scope.global_scope()
William Stein's avatar
William Stein committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
                entry = scope.find(self.name, self.pos)
                if entry and entry.is_type:
                    type = entry.type
                else:
                    error(self.pos, "'%s' is not a type identifier" % self.name)
        if type:
            return type
        else:
            return PyrexTypes.error_type


class CComplexBaseTypeNode(CBaseTypeNode):
    # base_type   CBaseTypeNode
    # declarator  CDeclaratorNode
    
604 605
    child_attrs = ["base_type", "declarator"]

William Stein's avatar
William Stein committed
606 607 608 609 610 611 612 613 614 615 616 617
    def analyse(self, env):
        base = self.base_type.analyse(env)
        _, type = self.declarator.analyse(base, env)
        return type


class CVarDefNode(StatNode):
    #  C variable definition or forward/extern function declaration.
    #
    #  visibility    'private' or 'public' or 'extern'
    #  base_type     CBaseTypeNode
    #  declarators   [CDeclaratorNode]
618
    #  in_pxd        boolean
Stefan Behnel's avatar
Stefan Behnel committed
619
    #  api           boolean
620 621

    child_attrs = ["base_type", "declarators"]
William Stein's avatar
William Stein committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    
    def analyse_declarations(self, env, dest_scope = None):
        if not dest_scope:
            dest_scope = env
        base_type = self.base_type.analyse(env)
        for declarator in self.declarators:
            name_declarator, type = declarator.analyse(base_type, env)
            if not type.is_complete():
                if not (self.visibility == 'extern' and type.is_array):
                    error(declarator.pos,
                        "Variable type '%s' is incomplete" % type)
            if self.visibility == 'extern' and type.is_pyobject:
                error(declarator.pos,
                    "Python object cannot be declared extern")
            name = name_declarator.name
            cname = name_declarator.cname
638 639 640
            if name == '':
                error(declarator.pos, "Missing name in declaration.")
                return
William Stein's avatar
William Stein committed
641
            if type.is_cfunction:
642
                entry = dest_scope.declare_cfunction(name, type, declarator.pos,
Stefan Behnel's avatar
Stefan Behnel committed
643 644
                    cname = cname, visibility = self.visibility, in_pxd = self.in_pxd,
                    api = self.api)
William Stein's avatar
William Stein committed
645
            else:
646 647 648
                if self.in_pxd and self.visibility != 'extern':
                    error(self.pos, 
                        "Only 'extern' C variable declaration allowed in .pxd file")
William Stein's avatar
William Stein committed
649 650 651 652
                dest_scope.declare_var(name, type, declarator.pos,
                    cname = cname, visibility = self.visibility, is_cdef = 1)
    
    def analyse_expressions(self, env):
653 654
        for declarator in self.declarators:
            declarator.analyse_expressions(env)
William Stein's avatar
William Stein committed
655 656
    
    def generate_execution_code(self, code):
657 658
        for declarator in self.declarators:
            declarator.generate_execution_code(code)
William Stein's avatar
William Stein committed
659 660 661 662 663 664 665


class CStructOrUnionDefNode(StatNode):
    #  name          string
    #  cname         string or None
    #  kind          "struct" or "union"
    #  typedef_flag  boolean
666
    #  visibility    "public" or "private"
Stefan Behnel's avatar
Stefan Behnel committed
667
    #  in_pxd        boolean
William Stein's avatar
William Stein committed
668 669 670
    #  attributes    [CVarDefNode] or None
    #  entry         Entry
    
671 672
    child_attrs = ["attributes"]

William Stein's avatar
William Stein committed
673 674 675 676 677 678
    def analyse_declarations(self, env):
        scope = None
        if self.attributes is not None:
            scope = StructOrUnionScope()
        self.entry = env.declare_struct_or_union(
            self.name, self.kind, scope, self.typedef_flag, self.pos,
679
            self.cname, visibility = self.visibility)
William Stein's avatar
William Stein committed
680
        if self.attributes is not None:
Stefan Behnel's avatar
Stefan Behnel committed
681 682
            if self.in_pxd and not env.in_cinclude:
                self.entry.defined_in_pxd = 1
William Stein's avatar
William Stein committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
            for attr in self.attributes:
                attr.analyse_declarations(env, scope)
    
    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass


class CEnumDefNode(StatNode):
    #  name           string or None
    #  cname          string or None
    #  items          [CEnumDefItemNode]
    #  typedef_flag   boolean
Stefan Behnel's avatar
Stefan Behnel committed
698 699
    #  visibility     "public" or "private"
    #  in_pxd         boolean
William Stein's avatar
William Stein committed
700 701
    #  entry          Entry
    
702 703
    child_attrs = ["items"]
    
William Stein's avatar
William Stein committed
704 705
    def analyse_declarations(self, env):
        self.entry = env.declare_enum(self.name, self.pos,
706 707
            cname = self.cname, typedef_flag = self.typedef_flag,
            visibility = self.visibility)
Stefan Behnel's avatar
Stefan Behnel committed
708 709 710 711 712
        if self.items is not None:
            if self.in_pxd and not env.in_cinclude:
                self.entry.defined_in_pxd = 1
            for item in self.items:
                item.analyse_declarations(env, self.entry)
William Stein's avatar
William Stein committed
713 714 715 716 717 718 719 720 721 722 723 724 725

    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass


class CEnumDefItemNode(StatNode):
    #  name     string
    #  cname    string or None
    #  value    ExprNode or None
    
726 727
    child_attrs = ["value"]

William Stein's avatar
William Stein committed
728 729 730 731 732 733 734 735 736 737 738 739
    def analyse_declarations(self, env, enum_entry):
        if self.value:
            self.value.analyse_const_expression(env)
            value = self.value.result_code
        else:
            value = self.name
        entry = env.declare_const(self.name, enum_entry.type, 
            value, self.pos, cname = self.cname)
        enum_entry.enum_values.append(entry)


class CTypeDefNode(StatNode):
Stefan Behnel's avatar
Stefan Behnel committed
740 741 742 743
    #  base_type    CBaseTypeNode
    #  declarator   CDeclaratorNode
    #  visibility   "public" or "private"
    #  in_pxd       boolean
744 745

    child_attrs = ["base_type", "declarator"]
William Stein's avatar
William Stein committed
746 747 748 749 750 751
    
    def analyse_declarations(self, env):
        base = self.base_type.analyse(env)
        name_declarator, type = self.declarator.analyse(base, env)
        name = name_declarator.name
        cname = name_declarator.cname
Stefan Behnel's avatar
Stefan Behnel committed
752
        entry = env.declare_typedef(name, type, self.pos,
753
            cname = cname, visibility = self.visibility)
Stefan Behnel's avatar
Stefan Behnel committed
754 755
        if self.in_pxd and not env.in_cinclude:
            entry.defined_in_pxd = 1
William Stein's avatar
William Stein committed
756 757 758 759 760 761 762 763 764 765 766 767 768 769
    
    def analyse_expressions(self, env):
        pass
    def generate_execution_code(self, code):
        pass


class FuncDefNode(StatNode, BlockNode):
    #  Base class for function definition nodes.
    #
    #  return_type     PyrexType
    #  #filename        string        C name of filename string const
    #  entry           Symtab.Entry
    
770
    py_func = None
771 772 773 774 775 776 777 778 779 780
    assmt = None
    
    def analyse_default_values(self, env):
        genv = env.global_scope()
        for arg in self.args:
            if arg.default:
                if arg.is_generic:
                    if not hasattr(arg, 'default_entry'):
                        arg.default.analyse_types(genv)
                        arg.default = arg.default.coerce_to(arg.type, genv)
781 782 783
                        if arg.default.is_literal:
                            arg.default_entry = arg.default
                            arg.default_result_code = arg.default.calculate_result_code()
784 785
                            if arg.default.type != arg.type and not arg.type.is_int:
                                arg.default_result_code = arg.type.cast_code(arg.default_result_code)
786 787 788 789 790
                        else:
                            arg.default.allocate_temps(genv)
                            arg.default_entry = genv.add_default_value(arg.type)
                            arg.default_entry.used = 1
                            arg.default_result_code = arg.default_entry.cname
791 792 793 794
                else:
                    error(arg.pos,
                        "This argument cannot have a default value")
                    arg.default = None
795
    
796 797
    def need_gil_acquisition(self, lenv):
        return 0
William Stein's avatar
William Stein committed
798
                
799
    def generate_function_definitions(self, env, code, transforms):
800
        code.mark_pos(self.pos)
William Stein's avatar
William Stein committed
801 802 803 804 805 806
        # Generate C code for header and body of function
        genv = env.global_scope()
        lenv = LocalScope(name = self.entry.name, outer_scope = genv)
        lenv.return_type = self.return_type
        code.init_labels()
        self.declare_arguments(lenv)
807
        transforms.run('before_analyse_function', self, env=env, lenv=lenv, genv=genv)
808
        self.body.analyse_control_flow(lenv)
William Stein's avatar
William Stein committed
809 810
        self.body.analyse_declarations(lenv)
        self.body.analyse_expressions(lenv)
811
        transforms.run('after_analyse_function', self, env=env, lenv=lenv, genv=genv)
William Stein's avatar
William Stein committed
812 813 814
        # Code for nested function definitions would go here
        # if we supported them, which we probably won't.
        # ----- Top-level constants used by this function
815
        self.generate_interned_num_decls(lenv, code)
William Stein's avatar
William Stein committed
816 817
        self.generate_interned_name_decls(lenv, code)
        self.generate_py_string_decls(lenv, code)
818
        self.generate_cached_builtins_decls(lenv, code)
William Stein's avatar
William Stein committed
819 820 821 822 823
        #code.putln("")
        #code.put_var_declarations(lenv.const_entries, static = 1)
        self.generate_const_definitions(lenv, code)
        # ----- Function header
        code.putln("")
824 825 826 827
        if self.py_func:
            self.py_func.generate_function_header(code, 
                with_pymethdef = env.is_py_class_scope,
                proto_only=True)
William Stein's avatar
William Stein committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
        self.generate_function_header(code,
            with_pymethdef = env.is_py_class_scope)
        # ----- Local variable declarations
        self.generate_argument_declarations(lenv, code)
        code.put_var_declarations(lenv.var_entries)
        init = ""
        if not self.return_type.is_void:
            code.putln(
                "%s%s;" % 
                    (self.return_type.declaration_code(
                        Naming.retval_cname),
                    init))
        code.put_var_declarations(lenv.temp_entries)
        self.generate_keyword_list(code)
        # ----- Extern library function declarations
        lenv.generate_library_function_declarations(code)
844 845 846 847
        # ----- GIL acquisition
        acquire_gil = self.need_gil_acquisition(lenv)
        if acquire_gil:
            code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
William Stein's avatar
William Stein committed
848
        # ----- Fetch arguments
849
        self.generate_argument_parsing_code(env, code)
Robert Bradshaw's avatar
Robert Bradshaw committed
850 851 852 853 854
        # If an argument is assigned to in the body, we must 
        # incref it to properly keep track of refcounts.
        for entry in lenv.arg_entries:
            if entry.type.is_pyobject and lenv.control_flow.get_state((entry.name, 'source')) != 'arg':
                code.put_var_incref(entry)
William Stein's avatar
William Stein committed
855 856
        # ----- Initialise local variables
        for entry in lenv.var_entries:
857
            if entry.type.is_pyobject and entry.init_to_none and entry.used:
William Stein's avatar
William Stein committed
858
                code.put_init_var_to_py_none(entry)
859
        # ----- Check and convert arguments
William Stein's avatar
William Stein committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
        self.generate_argument_type_tests(code)
        # ----- Function body
        self.body.generate_execution_code(code)
        # ----- Default return value
        code.putln("")
        if self.return_type.is_pyobject:
            #if self.return_type.is_extension_type:
            #	lhs = "(PyObject *)%s" % Naming.retval_cname
            #else:
            lhs = Naming.retval_cname
            code.put_init_to_py_none(lhs, self.return_type)
        else:
            val = self.return_type.default_value
            if val:
                code.putln("%s = %s;" % (Naming.retval_cname, val))
875
        #code.putln("goto %s;" % code.return_label)
William Stein's avatar
William Stein committed
876
        # ----- Error cleanup
877 878 879 880 881 882 883
        if code.error_label in code.labels_used:
            code.put_goto(code.return_label)
            code.put_label(code.error_label)
            code.put_var_xdecrefs(lenv.temp_entries)
            err_val = self.error_value()
            exc_check = self.caller_will_check_exceptions()
            if err_val is not None or exc_check:
William Stein's avatar
William Stein committed
884
                code.putln(
885 886 887 888 889 890 891 892 893 894 895 896
                    '__Pyx_AddTraceback("%s");' % 
                        self.entry.qualified_name)
                if err_val is not None:
                    code.putln(
                        "%s = %s;" % (
                            Naming.retval_cname, 
                            err_val))
            else:
                code.putln(
                    '__Pyx_WriteUnraisable("%s");' % 
                        self.entry.qualified_name)
                env.use_utility_code(unraisable_exception_utility_code)
897 898 899 900 901 902 903 904
                #if not self.return_type.is_void:
                default_retval = self.return_type.default_value
                if default_retval:
                    code.putln(
                        "%s = %s;" % (
                            Naming.retval_cname,
                            default_retval))
                            #self.return_type.default_value))
William Stein's avatar
William Stein committed
905 906
        # ----- Return cleanup
        code.put_label(code.return_label)
Robert Bradshaw's avatar
Robert Bradshaw committed
907 908 909 910 911
        if not Options.init_local_none:
            for entry in lenv.var_entries:
                print entry.name, lenv.control_flow.get_state((entry.name, 'initalized'))
                if lenv.control_flow.get_state((entry.name, 'initalized')) is not True:
                    entry.xdecref_cleanup = 1
912
        code.put_var_decrefs(lenv.var_entries, used_only = 1)
Robert Bradshaw's avatar
Robert Bradshaw committed
913
        # Decref any increfed args
914
        for entry in lenv.arg_entries:
Robert Bradshaw's avatar
Robert Bradshaw committed
915
            if entry.type.is_pyobject and lenv.control_flow.get_state((entry.name, 'source')) != 'arg':
916
                code.put_var_decref(entry)
William Stein's avatar
William Stein committed
917
        self.put_stararg_decrefs(code)
918 919
        if acquire_gil:
            code.putln("PyGILState_Release(_save);")
920
        # ----- Return
William Stein's avatar
William Stein committed
921
        if not self.return_type.is_void:
922
            code.putln("return %s;" % Naming.retval_cname)
William Stein's avatar
William Stein committed
923
        code.putln("}")
924 925
        # ----- Python version
        if self.py_func:
926
            self.py_func.generate_function_definitions(env, code, transforms)
927
        self.generate_optarg_wrapper_function(env, code)
Robert Bradshaw's avatar
Robert Bradshaw committed
928
        
William Stein's avatar
William Stein committed
929 930 931 932 933 934 935 936 937 938
    def put_stararg_decrefs(self, code):
        pass

    def declare_argument(self, env, arg):
        if arg.type.is_void:
            error(arg.pos, "Invalid use of 'void'")
        elif not arg.type.is_complete() and not arg.type.is_array:
            error(arg.pos,
                "Argument type '%s' is incomplete" % arg.type)
        return env.declare_arg(arg.name, arg.type, arg.pos)
939
    def generate_optarg_wrapper_function(self, env, code):
William Stein's avatar
William Stein committed
940 941 942
        pass

    def generate_execution_code(self, code):
943 944 945 946
        # Evaluate and store argument default values
        for arg in self.args:
            default = arg.default
            if default:
947 948 949
                if not default.is_literal:
                    default.generate_evaluation_code(code)
                    default.make_owned_reference(code)
950
                    code.putln(
951 952 953 954 955 956 957
                        "%s = %s;" % (
                            arg.default_entry.cname,
                            default.result_as(arg.default_entry.type)))
                    if default.is_temp and default.type.is_pyobject:
                        code.putln(
                            "%s = 0;" %
                                default.result_code)
958 959 960 961
        # For Python class methods, create and store function object
        if self.assmt:
            self.assmt.generate_execution_code(code)
    
William Stein's avatar
William Stein committed
962 963 964 965 966


class CFuncDefNode(FuncDefNode):
    #  C function definition.
    #
Robert Bradshaw's avatar
Robert Bradshaw committed
967
    #  modifiers     ['inline']
William Stein's avatar
William Stein committed
968 969 970 971
    #  visibility    'private' or 'public' or 'extern'
    #  base_type     CBaseTypeNode
    #  declarator    CDeclaratorNode
    #  body          StatListNode
972
    #  api           boolean
William Stein's avatar
William Stein committed
973
    #
974
    #  with_gil      boolean    Acquire GIL around body
William Stein's avatar
William Stein committed
975 976
    #  type          CFuncType
    
977 978
    child_attrs = ["base_type", "declarator", "body"]

William Stein's avatar
William Stein committed
979 980 981 982 983 984
    def unqualified_name(self):
        return self.entry.name
        
    def analyse_declarations(self, env):
        base_type = self.base_type.analyse(env)
        name_declarator, type = self.declarator.analyse(base_type, env)
985 986 987
        if not type.is_cfunction:
            error(self.pos, 
                "Suite attached to non-function declaration")
William Stein's avatar
William Stein committed
988 989 990 991 992
        # Remember the actual type according to the function header
        # written here, because the type in the symbol table entry
        # may be different if we're overriding a C method inherited
        # from the base type of an extension type.
        self.type = type
993
        type.is_overridable = self.overridable
994 995 996 997 998
        declarator = self.declarator
        while not hasattr(declarator, 'args'):
            declarator = declarator.base
        self.args = declarator.args
        for formal_arg, type_arg in zip(self.args, type.args):
999 1000
            formal_arg.type = type_arg.type
            formal_arg.cname = type_arg.cname
William Stein's avatar
William Stein committed
1001 1002 1003 1004 1005
        name = name_declarator.name
        cname = name_declarator.cname
        self.entry = env.declare_cfunction(
            name, type, self.pos, 
            cname = cname, visibility = self.visibility,
1006 1007
            defining = self.body is not None,
            api = self.api)
William Stein's avatar
William Stein committed
1008
        self.return_type = type.return_type
1009
        
1010
        if self.overridable:
1011
            import ExprNodes
1012
            py_func_body = self.call_self_node(is_module_scope = env.is_module_scope)
1013 1014 1015 1016 1017
            self.py_func = DefNode(pos = self.pos, 
                                   name = self.declarator.base.name,
                                   args = self.declarator.args,
                                   star_arg = None,
                                   starstar_arg = None,
1018
                                   doc = self.doc,
1019 1020
                                   body = py_func_body,
                                   is_wrapper = 1)
1021
            self.py_func.is_module_scope = env.is_module_scope
1022
            self.py_func.analyse_declarations(env)
1023
            self.entry.as_variable = self.py_func.entry
1024 1025 1026 1027
            # Reset scope entry the above cfunction
            env.entries[name] = self.entry
            if Options.intern_names:
                self.py_func.interned_attr_cname = env.intern(self.py_func.entry.name)
1028 1029 1030
            if not env.is_module_scope or Options.lookup_module_cpdef:
                self.override = OverrideCheckNode(self.pos, py_func = self.py_func)
                self.body = StatListNode(self.pos, stats=[self.override, self.body])
1031
    
1032
    def call_self_node(self, omit_optional_args=0, is_module_scope=0):
1033 1034 1035 1036 1037
        import ExprNodes
        args = self.type.args
        if omit_optional_args:
            args = args[:len(args) - self.type.optional_arg_count]
        arg_names = [arg.name for arg in args]
1038 1039 1040 1041 1042 1043 1044
        if is_module_scope:
            cfunc = ExprNodes.NameNode(self.pos, name=self.declarator.base.name)
        else:
            self_arg = ExprNodes.NameNode(self.pos, name=arg_names[0])
            cfunc = ExprNodes.AttributeNode(self.pos, obj=self_arg, attribute=self.declarator.base.name)
        skip_dispatch = not is_module_scope or Options.lookup_module_cpdef
        c_call = ExprNodes.SimpleCallNode(self.pos, function=cfunc, args=[ExprNodes.NameNode(self.pos, name=n) for n in arg_names[1-is_module_scope:]], wrapper_call=skip_dispatch)
1045
        return ReturnStatNode(pos=self.pos, return_type=PyrexTypes.py_object_type, value=c_call)
1046
                    
William Stein's avatar
William Stein committed
1047 1048 1049 1050 1051 1052
    def declare_arguments(self, env):
        for arg in self.type.args:
            if not arg.name:
                error(arg.pos, "Missing argument name")
            self.declare_argument(env, arg)
            
1053 1054 1055 1056 1057 1058 1059 1060
    def need_gil_acquisition(self, lenv):
        with_gil = self.type.with_gil
        if self.type.nogil and not with_gil:
            for entry in lenv.var_entries + lenv.temp_entries:
                if entry.type.is_pyobject:
                    error(self.pos, "Function declared nogil has Python locals or temporaries")
        return with_gil

1061 1062 1063 1064 1065
    def analyse_expressions(self, env):
        self.analyse_default_values(env)
        if self.overridable:
            self.py_func.analyse_expressions(env)

1066
    def generate_function_header(self, code, with_pymethdef, with_opt_args = 1):
William Stein's avatar
William Stein committed
1067 1068
        arg_decls = []
        type = self.type
Stefan Behnel's avatar
Stefan Behnel committed
1069
        visibility = self.entry.visibility
1070
        for arg in type.args[:len(type.args)-type.optional_arg_count]:
William Stein's avatar
William Stein committed
1071
            arg_decls.append(arg.declaration_code())
1072 1073
        if type.optional_arg_count and with_opt_args:
            arg_decls.append(type.op_arg_struct.declaration_code(Naming.optional_args_cname))
William Stein's avatar
William Stein committed
1074 1075 1076 1077
        if type.has_varargs:
            arg_decls.append("...")
        if not arg_decls:
            arg_decls = ["void"]
1078 1079 1080 1081
        cname = self.entry.func_cname
        if not with_opt_args:
            cname += Naming.no_opt_args
        entity = type.function_header_code(cname, string.join(arg_decls, ", "))
Stefan Behnel's avatar
Stefan Behnel committed
1082
        if visibility == 'public':
William Stein's avatar
William Stein committed
1083 1084 1085 1086 1087
            dll_linkage = "DL_EXPORT"
        else:
            dll_linkage = None
        header = self.return_type.declaration_code(entity,
            dll_linkage = dll_linkage)
Stefan Behnel's avatar
Stefan Behnel committed
1088
        if visibility != 'private':
William Stein's avatar
William Stein committed
1089 1090
            storage_class = "%s " % Naming.extern_c_macro
        else:
Stefan Behnel's avatar
Stefan Behnel committed
1091
            storage_class = "static "
Robert Bradshaw's avatar
Robert Bradshaw committed
1092
        code.putln("%s%s %s {" % (
William Stein's avatar
William Stein committed
1093
            storage_class,
1094
            ' '.join(self.modifiers).upper(), # macro forms 
William Stein's avatar
William Stein committed
1095 1096 1097
            header))

    def generate_argument_declarations(self, env, code):
1098
        for arg in self.args:
1099
            if arg.default:
1100
                    code.putln('%s = %s;' % (arg.type.declaration_code(arg.cname), arg.default_result_code))
1101

William Stein's avatar
William Stein committed
1102 1103 1104
    def generate_keyword_list(self, code):
        pass
        
1105
    def generate_argument_parsing_code(self, env, code):
1106
        i = 0
1107 1108
        if self.type.optional_arg_count:
            code.putln('if (%s) {' % Naming.optional_args_cname)
1109
            for arg in self.args:
1110
                if arg.default:
1111
                    code.putln('if (%s->%sn > %s) {' % (Naming.optional_args_cname, Naming.pyrex_prefix, i))
1112 1113 1114 1115
                    declarator = arg.declarator
                    while not hasattr(declarator, 'name'):
                        declarator = declarator.base
                    code.putln('%s = %s->%s;' % (arg.cname, Naming.optional_args_cname, declarator.name))
1116 1117 1118
                    i += 1
            for _ in range(self.type.optional_arg_count):
                code.putln('}')
1119
            code.putln('}')
William Stein's avatar
William Stein committed
1120 1121 1122 1123 1124
    
    def generate_argument_conversion_code(self, code):
        pass
    
    def generate_argument_type_tests(self, code):
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
        # Generate type tests for args whose type in a parent
        # class is a supertype of the declared type.
        for arg in self.type.args:
            if arg.needs_type_test:
                self.generate_arg_type_test(arg, code)
    
    def generate_arg_type_test(self, arg, code):
        # Generate type test for one argument.
        if arg.type.typeobj_is_available():
            typeptr_cname = arg.type.typeptr_cname
1135
            arg_code = "((PyObject *)%s)" % arg.cname
1136
            code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
1137
                'if (unlikely(!__Pyx_ArgTypeTest(%s, %s, %d, "%s"))) %s' % (
1138 1139 1140 1141 1142 1143 1144 1145
                    arg_code, 
                    typeptr_cname,
                    not arg.not_none,
                    arg.name,
                    code.error_goto(arg.pos)))
        else:
            error(arg.pos, "Cannot test type of extern C class "
                "without type object name specification")
1146

William Stein's avatar
William Stein committed
1147 1148 1149 1150 1151 1152 1153 1154 1155
    def error_value(self):
        if self.return_type.is_pyobject:
            return "0"
        else:
            #return None
            return self.entry.type.exception_value
            
    def caller_will_check_exceptions(self):
        return self.entry.type.exception_check
1156
                    
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    def generate_optarg_wrapper_function(self, env, code):
        if self.type.optional_arg_count and \
                self.type.original_sig and not self.type.original_sig.optional_arg_count:
            code.putln()
            self.generate_function_header(code, 0, with_opt_args = 0)
            if not self.return_type.is_void:
                code.put('return ')
            args = self.type.args
            arglist = [arg.cname for arg in args[:len(args)-self.type.optional_arg_count]]
            arglist.append('NULL')
            code.putln('%s(%s);' % (self.entry.func_cname, ', '.join(arglist)))
            code.putln('}')

William Stein's avatar
William Stein committed
1170 1171 1172 1173 1174 1175 1176

class PyArgDeclNode(Node):
    # Argument which must be a Python object (used
    # for * and ** arguments).
    #
    # name   string
    # entry  Symtab.Entry
1177
    child_attrs = []
William Stein's avatar
William Stein committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    
    pass
    

class DefNode(FuncDefNode):
    # A Python function definition.
    #
    # name          string                 the Python name of the function
    # args          [CArgDeclNode]         formal arguments
    # star_arg      PyArgDeclNode or None  * argument
    # starstar_arg  PyArgDeclNode or None  ** argument
    # doc           string or None
    # body          StatListNode
    #
    #  The following subnode is constructed internally
    #  when the def statement is inside a Python class definition.
    #
    #  assmt   AssignmentNode   Function construction/assignment
    
1197 1198
    child_attrs = ["args", "star_arg", "starstar_arg", "body"]

William Stein's avatar
William Stein committed
1199
    assmt = None
1200
    num_kwonly_args = 0
1201
    num_required_kw_args = 0
1202
    reqd_kw_flags_cname = "0"
1203
    is_wrapper = 0
1204 1205 1206
    
    def __init__(self, pos, **kwds):
        FuncDefNode.__init__(self, pos, **kwds)
1207
        k = rk = r = 0
1208 1209
        for arg in self.args:
            if arg.kw_only:
1210
                k += 1
1211
                if not arg.default:
1212 1213 1214 1215 1216 1217
                    rk += 1
            if not arg.default:
                r += 1
        self.num_kwonly_args = k
        self.num_required_kw_args = rk
        self.num_required_args = r
William Stein's avatar
William Stein committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    
    def analyse_declarations(self, env):
        for arg in self.args:
            base_type = arg.base_type.analyse(env)
            name_declarator, type = \
                arg.declarator.analyse(base_type, env)
            arg.name = name_declarator.name
            if name_declarator.cname:
                error(self.pos,
                    "Python function argument cannot have C name specification")
            arg.type = type.as_argument_type()
            arg.hdr_type = None
            arg.needs_conversion = 0
            arg.needs_type_test = 0
            arg.is_generic = 1
            if arg.not_none and not arg.type.is_extension_type:
                error(self.pos,
                    "Only extension type arguments can have 'not None'")
        self.declare_pyfunction(env)
        self.analyse_signature(env)
        self.return_type = self.entry.signature.return_type()
1239 1240 1241
        if self.signature_has_generic_args():
            if self.star_arg:
                env.use_utility_code(get_stararg_utility_code)
1242 1243
            elif self.signature_has_generic_args():
                env.use_utility_code(raise_argtuple_too_long_utility_code)
1244 1245 1246 1247
            if not self.signature_has_nongeneric_args():
                env.use_utility_code(get_keyword_string_check_utility_code)
            elif self.starstar_arg:
                env.use_utility_code(get_splitkeywords_utility_code)
1248
        if self.num_required_kw_args:
1249 1250
            env.use_utility_code(get_checkkeywords_utility_code)

William Stein's avatar
William Stein committed
1251 1252
    def analyse_signature(self, env):
        any_type_tests_needed = 0
1253
        # Use the simpler calling signature for zero- and one-argument functions.
1254
        if not self.entry.is_special and not self.star_arg and not self.starstar_arg:
1255
            if self.entry.signature is TypeSlots.pyfunction_signature and Options.optimize_simple_methods:
1256 1257
                if len(self.args) == 0:
                    self.entry.signature = TypeSlots.pyfunction_noargs
1258 1259 1260
                elif len(self.args) == 1:
                    if self.args[0].default is None and not self.args[0].kw_only:
                        self.entry.signature = TypeSlots.pyfunction_onearg
1261 1262 1263
            elif self.entry.signature is TypeSlots.pymethod_signature:
                if len(self.args) == 1:
                    self.entry.signature = TypeSlots.unaryfunc
1264 1265 1266
                elif len(self.args) == 2:
                    if self.args[1].default is None and not self.args[1].kw_only:
                        self.entry.signature = TypeSlots.ibinaryfunc
1267 1268
        elif self.entry.is_special:
            self.entry.trivial_signature = len(self.args) == 1 and not (self.star_arg or self.starstar_arg)
William Stein's avatar
William Stein committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
        sig = self.entry.signature
        nfixed = sig.num_fixed_args()
        for i in range(nfixed):
            if i < len(self.args):
                arg = self.args[i]
                arg.is_generic = 0
                if sig.is_self_arg(i):
                    arg.is_self_arg = 1
                    arg.hdr_type = arg.type = env.parent_type
                    arg.needs_conversion = 0
                else:
                    arg.hdr_type = sig.fixed_arg_type(i)
                    if not arg.type.same_as(arg.hdr_type):
                        if arg.hdr_type.is_pyobject and arg.type.is_pyobject:
                            arg.needs_type_test = 1
                            any_type_tests_needed = 1
                        else:
                            arg.needs_conversion = 1
                if arg.needs_conversion:
                    arg.hdr_cname = Naming.arg_prefix + arg.name
                else:
                    arg.hdr_cname = Naming.var_prefix + arg.name
            else:
                self.bad_signature()
                return
        if nfixed < len(self.args):
            if not sig.has_generic_args:
                self.bad_signature()
            for arg in self.args:
                if arg.is_generic and arg.type.is_extension_type:
                    arg.needs_type_test = 1
                    any_type_tests_needed = 1
1301 1302 1303 1304 1305 1306
                elif arg.type is PyrexTypes.c_py_ssize_t_type:
                    # Want to use __index__ rather than __int__ method
                    # that PyArg_ParseTupleAndKeywords calls
                    arg.needs_conversion = 1
                    arg.hdr_type = PyrexTypes.py_object_type
                    arg.hdr_cname = Naming.arg_prefix + arg.name
William Stein's avatar
William Stein committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
        if any_type_tests_needed:
            env.use_utility_code(arg_type_test_utility_code)
    
    def bad_signature(self):
        sig = self.entry.signature
        expected_str = "%d" % sig.num_fixed_args()
        if sig.has_generic_args:
            expected_str = expected_str + " or more"
        name = self.name
        if name.startswith("__") and name.endswith("__"):
            desc = "Special method"
        else:
            desc = "Method"
        error(self.pos,
            "%s %s has wrong number of arguments "
            "(%d declared, %s expected)" % (
                desc, self.name, len(self.args), expected_str))
1324 1325 1326

    def signature_has_nongeneric_args(self):
        argcount = len(self.args)
Stefan Behnel's avatar
Stefan Behnel committed
1327
        if argcount == 0 or (argcount == 1 and self.args[0].is_self_arg):
1328 1329 1330 1331 1332
            return 0
        return 1

    def signature_has_generic_args(self):
        return self.entry.signature.has_generic_args
William Stein's avatar
William Stein committed
1333 1334
    
    def declare_pyfunction(self, env):
1335 1336
        #print "DefNode.declare_pyfunction:", self.name, "in", env ###
        name = self.name
1337 1338 1339
        entry = env.lookup_here(self.name)
        if entry and entry.type.is_cfunction and not self.is_wrapper:
            warning(self.pos, "Overriding cdef method with def method.", 5)
1340 1341 1342 1343
        entry = env.declare_pyfunction(self.name, self.pos)
        self.entry = entry
        prefix = env.scope_prefix
        entry.func_cname = \
1344
            Naming.pyfunc_prefix + prefix + name
1345 1346
        entry.pymethdef_cname = \
            Naming.pymethdef_prefix + prefix + name
1347 1348
        if not Options.docstrings:
            self.entry.doc = None
1349
        else:
1350 1351 1352 1353 1354 1355 1356 1357 1358
            if Options.embed_pos_in_docstring:
                entry.doc = 'File: %s (starting at line %s)'%relative_position(self.pos)
                if not self.doc is None:
                    entry.doc = entry.doc + '\\n' + self.doc
            else:
                entry.doc = self.doc
            entry.doc_cname = \
                Naming.funcdoc_prefix + prefix + name

William Stein's avatar
William Stein committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
    def declare_arguments(self, env):
        for arg in self.args:
            if not arg.name:
                error(arg.pos, "Missing argument name")
            if arg.needs_conversion:
                arg.entry = env.declare_var(arg.name, arg.type, arg.pos)
                if arg.type.is_pyobject:
                    arg.entry.init = "0"
                arg.entry.init_to_none = 0
            else:
                arg.entry = self.declare_argument(env, arg)
1370
            arg.entry.used = 1
William Stein's avatar
William Stein committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
            arg.entry.is_self_arg = arg.is_self_arg
            if arg.hdr_type:
                if arg.is_self_arg or \
                    (arg.type.is_extension_type and not arg.hdr_type.is_extension_type):
                        arg.entry.is_declared_generic = 1
        self.declare_python_arg(env, self.star_arg)
        self.declare_python_arg(env, self.starstar_arg)

    def declare_python_arg(self, env, arg):
        if arg:
1381
            entry = env.declare_var(arg.name, 
William Stein's avatar
William Stein committed
1382
                PyrexTypes.py_object_type, arg.pos)
1383 1384 1385 1386 1387
            entry.used = 1
            entry.init = "0"
            entry.init_to_none = 0
            entry.xdecref_cleanup = 1
            arg.entry = entry
William Stein's avatar
William Stein committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
            
    def analyse_expressions(self, env):
        self.analyse_default_values(env)
        if env.is_py_class_scope:
            self.synthesize_assignment_node(env)
    
    def synthesize_assignment_node(self, env):
        import ExprNodes
        self.assmt = SingleAssignmentNode(self.pos,
            lhs = ExprNodes.NameNode(self.pos, name = self.name),
            rhs = ExprNodes.UnboundMethodNode(self.pos, 
                class_cname = env.class_obj_cname,
                function = ExprNodes.PyCFunctionNode(self.pos,
                    pymethdef_cname = self.entry.pymethdef_cname)))
        self.assmt.analyse_declarations(env)
        self.assmt.analyse_expressions(env)
            
1405
    def generate_function_header(self, code, with_pymethdef, proto_only=0):
William Stein's avatar
William Stein committed
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        arg_code_list = []
        sig = self.entry.signature
        if sig.has_dummy_arg:
            arg_code_list.append(
                "PyObject *%s" % Naming.self_cname)
        for arg in self.args:
            if not arg.is_generic:
                if arg.is_self_arg:
                    arg_code_list.append("PyObject *%s" % arg.hdr_cname)
                else:
                    arg_code_list.append(
                        arg.hdr_type.declaration_code(arg.hdr_cname))
1418
        if not self.entry.is_special and sig.method_flags() == [TypeSlots.method_noargs]:
1419
            arg_code_list.append("PyObject *unused")
William Stein's avatar
William Stein committed
1420 1421 1422 1423 1424 1425 1426 1427
        if sig.has_generic_args:
            arg_code_list.append(
                "PyObject *%s, PyObject *%s"
                    % (Naming.args_cname, Naming.kwds_cname))
        arg_code = ", ".join(arg_code_list)
        dc = self.return_type.declaration_code(self.entry.func_cname)
        header = "static %s(%s)" % (dc, arg_code)
        code.putln("%s; /*proto*/" % header)
1428 1429
        if proto_only:
            return
1430
        if self.entry.doc and Options.docstrings:
William Stein's avatar
William Stein committed
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
            code.putln(
                'static char %s[] = "%s";' % (
                    self.entry.doc_cname,
                    self.entry.doc))
        if with_pymethdef:
            code.put(
                "static PyMethodDef %s = " % 
                    self.entry.pymethdef_cname)
            code.put_pymethoddef(self.entry, ";")
        code.putln("%s {" % header)

    def generate_argument_declarations(self, env, code):
        for arg in self.args:
            if arg.is_generic: # or arg.needs_conversion:
1445 1446 1447 1448
                if arg.needs_conversion:
                    code.putln("PyObject *%s = 0;" % arg.hdr_cname)
                else:
                    code.put_var_declaration(arg.entry)
William Stein's avatar
William Stein committed
1449 1450
    
    def generate_keyword_list(self, code):
1451 1452
        if self.signature_has_generic_args() and \
                self.signature_has_nongeneric_args():
1453 1454
            reqd_kw_flags = []
            has_reqd_kwds = False
William Stein's avatar
William Stein committed
1455 1456 1457 1458 1459 1460 1461 1462
            code.put(
                "static char *%s[] = {" %
                    Naming.kwdlist_cname)
            for arg in self.args:
                if arg.is_generic:
                    code.put(
                        '"%s",' % 
                            arg.name)
1463 1464 1465 1466 1467 1468
                    if arg.kw_only and not arg.default:
                        has_reqd_kwds = 1
                        flag = "1"
                    else:
                        flag = "0"
                    reqd_kw_flags.append(flag)
William Stein's avatar
William Stein committed
1469 1470
            code.putln(
                "0};")
1471 1472 1473 1474 1475 1476 1477 1478
            if has_reqd_kwds:
                flags_name = Naming.reqd_kwds_cname
                self.reqd_kw_flags_cname = flags_name
                code.putln(
                    "static char %s[] = {%s};" % (
                        flags_name,
                        ",".join(reqd_kw_flags)))

1479
    def generate_argument_parsing_code(self, env, code):
William Stein's avatar
William Stein committed
1480 1481
        # Generate PyArg_ParseTuple call for generic
        # arguments, if any.
1482 1483 1484
        has_kwonly_args = self.num_kwonly_args > 0
        has_star_or_kw_args = self.star_arg is not None \
            or self.starstar_arg is not None or has_kwonly_args
1485
        if not self.signature_has_generic_args():
1486 1487
            if has_star_or_kw_args:
                error(self.pos, "This method cannot have * or keyword arguments")
1488
            self.generate_argument_conversion_code(code)
1489 1490
        elif not self.signature_has_nongeneric_args():
            # func(*args) or func(**kw) or func(*args, **kw)
1491
            self.generate_stararg_copy_code(code)
1492
        else:
William Stein's avatar
William Stein committed
1493 1494
            arg_addrs = []
            arg_formats = []
1495
            positional_args = []
William Stein's avatar
William Stein committed
1496 1497 1498 1499 1500 1501 1502 1503
            default_seen = 0
            for arg in self.args:
                arg_entry = arg.entry
                if arg.is_generic:
                    if arg.default:
                        code.putln(
                            "%s = %s;" % (
                                arg_entry.cname,
1504
                                arg.default_result_code))
William Stein's avatar
William Stein committed
1505 1506 1507
                        if not default_seen:
                            arg_formats.append("|")
                        default_seen = 1
1508 1509
                        if not arg.is_self_arg and not arg.kw_only:
                            positional_args.append(arg)
1510 1511 1512 1513
                    elif arg.kw_only:
                        if not default_seen:
                            arg_formats.append("|")
                        default_seen = 1
1514
                    elif default_seen:
William Stein's avatar
William Stein committed
1515
                        error(arg.pos, "Non-default argument following default argument")
1516 1517
                    elif not arg.is_self_arg:
                        positional_args.append(arg)
1518 1519 1520 1521 1522 1523
                    if arg.needs_conversion:
                        arg_addrs.append("&" + arg.hdr_cname)
                        format = arg.hdr_type.parsetuple_format
                    else:
                        arg_addrs.append("&" + arg_entry.cname)
                        format = arg_entry.type.parsetuple_format
William Stein's avatar
William Stein committed
1524 1525 1526 1527
                    if format:
                        arg_formats.append(format)
                    else:
                        error(arg.pos, 
1528
                            "Cannot convert Python object argument to type '%s' (when parsing input arguments)" 
William Stein's avatar
William Stein committed
1529
                                % arg.type)
1530

1531
            if has_star_or_kw_args:
William Stein's avatar
William Stein committed
1532
                self.generate_stararg_getting_code(code)
1533 1534 1535
            old_error_label = code.new_error_label()
            our_error_label = code.error_label
            end_label = code.new_label()
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

            self.generate_argument_tuple_parsing_code(
                positional_args, arg_formats, arg_addrs, code)

            code.error_label = old_error_label
            code.put_goto(end_label)
            code.put_label(our_error_label)
            if has_star_or_kw_args:
                self.put_stararg_decrefs(code)
                self.generate_arg_decref(self.star_arg, code)
                if self.starstar_arg:
                    if self.starstar_arg.entry.xdecref_cleanup:
                        code.put_var_xdecref(self.starstar_arg.entry)
                    else:
                        code.put_var_decref(self.starstar_arg.entry)
            code.putln("return %s;" % self.error_value())
            code.put_label(end_label)

    def generate_argument_tuple_parsing_code(self, positional_args,
                                             arg_formats, arg_addrs, code):
        # Unpack inplace if it's simple
        if not self.num_required_kw_args:
1558 1559 1560 1561 1562
            min_positional_args = self.num_required_args - self.num_required_kw_args
            max_positional_args = len(positional_args)
            if len(self.args) > 0 and self.args[0].is_self_arg:
                min_positional_args -= 1
            if max_positional_args == min_positional_args:
1563
                count_cond = "likely(PyTuple_GET_SIZE(%s) == %s)" % (
1564
                    Naming.args_cname, max_positional_args)
1565
            else:
1566
                count_cond = "likely(%s <= PyTuple_GET_SIZE(%s)) && likely(PyTuple_GET_SIZE(%s) <= %s)" % (
1567
                               min_positional_args,
1568 1569
                               Naming.args_cname,
                               Naming.args_cname,
1570
                               max_positional_args)
1571
            code.putln(
1572
                'if (likely(!%s) && %s) {' % (Naming.kwds_cname, count_cond))
1573
            i = 0
1574 1575
            closing = 0
            for arg in positional_args:
1576 1577
                if arg.default:
                    code.putln('if (PyTuple_GET_SIZE(%s) > %s) {' % (Naming.args_cname, i))
1578
                    closing += 1
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
                item = "PyTuple_GET_ITEM(%s, %s)" % (Naming.args_cname, i)
                if arg.type.is_pyobject:
                    if arg.is_generic:
                        item = PyrexTypes.typecast(arg.type, PyrexTypes.py_object_type, item)
                    code.putln("%s = %s;" % (arg.entry.cname, item))
                else:
                    func = arg.type.from_py_function
                    if func:
                        code.putln("%s = %s(%s); %s" % (
                            arg.entry.cname,
                            func,
                            item,
                            code.error_goto_if(arg.type.error_condition(arg.entry.cname), arg.pos)))
                    else:
                        error(arg.pos, "Cannot convert Python object argument to type '%s'" % arg.type)
                i += 1
1595
            for _ in range(closing):
1596 1597 1598
                code.putln('}')
            code.putln(
                '}')
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
            code.putln('else {')

        argformat = '"%s"' % string.join(arg_formats, "")
        pt_arglist = [Naming.args_cname, Naming.kwds_cname, argformat,
                      Naming.kwdlist_cname] + arg_addrs
        pt_argstring = string.join(pt_arglist, ", ")
        code.putln(
            'if (unlikely(!PyArg_ParseTupleAndKeywords(%s))) %s' % (
                pt_argstring,
                code.error_goto(self.pos)))
        self.generate_argument_conversion_code(code)

        if not self.num_required_kw_args:
            code.putln('}')
1613

William Stein's avatar
William Stein committed
1614
    def put_stararg_decrefs(self, code):
1615 1616 1617
        if self.star_arg:
            code.put_decref(Naming.args_cname, py_object_type)
        if self.starstar_arg:
William Stein's avatar
William Stein committed
1618 1619 1620 1621 1622 1623
            code.put_xdecref(Naming.kwds_cname, py_object_type)
    
    def generate_arg_xdecref(self, arg, code):
        if arg:
            code.put_var_xdecref(arg.entry)
    
1624 1625 1626 1627
    def generate_arg_decref(self, arg, code):
        if arg:
            code.put_var_decref(arg.entry)
    
William Stein's avatar
William Stein committed
1628 1629 1630 1631 1632 1633
    def arg_address(self, arg):
        if arg:
            return "&%s" % arg.entry.cname
        else:
            return 0

1634 1635
    def generate_stararg_copy_code(self, code):
        if not self.star_arg:
1636
            self.generate_positional_args_check(code, 0)
1637 1638
        self.generate_keyword_args_check(code)

1639
        if self.starstar_arg:
1640
            code.putln("%s = (%s) ? PyDict_Copy(%s) : PyDict_New();" % (
1641 1642 1643
                    self.starstar_arg.entry.cname,
                    Naming.kwds_cname,
                    Naming.kwds_cname))
1644 1645
            code.putln("if (unlikely(!%s)) return %s;" % (
                    self.starstar_arg.entry.cname, self.error_value()))
1646 1647 1648
            self.starstar_arg.entry.xdecref_cleanup = 0
            self.starstar_arg = None

1649 1650 1651 1652 1653 1654 1655 1656
        if self.star_arg:
            code.put_incref(Naming.args_cname, py_object_type)
            code.putln("%s = %s;" % (
                    self.star_arg.entry.cname,
                    Naming.args_cname))
            self.star_arg.entry.xdecref_cleanup = 0
            self.star_arg = None

William Stein's avatar
William Stein committed
1657
    def generate_stararg_getting_code(self, code):
1658
        num_kwonly = self.num_kwonly_args
1659 1660
        fixed_args = self.entry.signature.num_fixed_args()
        nargs = len(self.args) - num_kwonly - fixed_args
Stefan Behnel's avatar
Stefan Behnel committed
1661
        error_return = "return %s;" % self.error_value()
1662

1663
        if self.star_arg:
1664
            star_arg_cname = self.star_arg.entry.cname
1665
            code.putln("if (likely(PyTuple_GET_SIZE(%s) <= %d)) {" % (
1666 1667
                    Naming.args_cname, nargs))
            code.put_incref(Naming.args_cname, py_object_type)
1668
            code.put("%s = %s; " % (star_arg_cname, Naming.empty_tuple))
1669 1670 1671
            code.put_incref(Naming.empty_tuple, py_object_type)
            code.putln("}")
            code.putln("else {")
1672
            code.putln(
1673
                "if (unlikely(__Pyx_SplitStarArg(&%s, %d, &%s) < 0)) return %s;" % (
1674 1675
                    Naming.args_cname,
                    nargs,
1676
                    star_arg_cname,
1677
                    self.error_value()))
1678
            code.putln("}")
1679
            self.star_arg.entry.xdecref_cleanup = 0
1680
        elif self.signature_has_generic_args():
Stefan Behnel's avatar
Stefan Behnel committed
1681 1682 1683
            # make sure supernumerous positional arguments do not run
            # into keyword-only arguments and provide a more helpful
            # message than PyArg_ParseTupelAndKeywords()
1684
            self.generate_positional_args_check(code, nargs)
1685 1686 1687 1688 1689

        handle_error = 0
        if self.starstar_arg:
            handle_error = 1
            code.put(
1690
                "if (unlikely(__Pyx_SplitKeywords(&%s, %s, &%s, %s) < 0)) " % (
1691 1692
                    Naming.kwds_cname,
                    Naming.kwdlist_cname,
1693
                    self.starstar_arg.entry.cname,
1694
                    self.reqd_kw_flags_cname))
1695
            self.starstar_arg.entry.xdecref_cleanup = 0
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
        elif self.num_required_kw_args:
            handle_error = 1
            code.put("if (unlikely(__Pyx_CheckRequiredKeywords(%s, %s, %s) < 0)) " % (
                    Naming.kwds_cname,
                    Naming.kwdlist_cname,
                    self.reqd_kw_flags_cname))

        if handle_error:
            if self.star_arg:
                code.putln("{")
                code.put_decref(Naming.args_cname, py_object_type)
                code.put_decref(self.star_arg.entry.cname, py_object_type)
                code.putln(error_return)
                code.putln("}")
            else:
                code.putln(error_return)
1712

1713 1714 1715
    def generate_positional_args_check(self, code, nargs):
        code.putln("if (unlikely(PyTuple_GET_SIZE(%s) > %d)) {" % (
                Naming.args_cname, nargs))
1716 1717
        code.putln("__Pyx_RaiseArgtupleTooLong(%d, PyTuple_GET_SIZE(%s));" % (
                nargs, Naming.args_cname))
1718 1719 1720
        code.putln("return %s;" % self.error_value())
        code.putln("}")

1721 1722 1723 1724 1725 1726 1727
    def generate_keyword_args_check(self, code):
        code.putln("if (unlikely(%s)) {" % Naming.kwds_cname)
        code.putln("if (unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % (
                Naming.kwds_cname, self.name,
                bool(self.starstar_arg), self.error_value()))
        code.putln("}")

William Stein's avatar
William Stein committed
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
    def generate_argument_conversion_code(self, code):
        # Generate code to convert arguments from
        # signature type to declared type, if needed.
        for arg in self.args:
            if arg.needs_conversion:
                self.generate_arg_conversion(arg, code)

    def generate_arg_conversion(self, arg, code):
        # Generate conversion code for one argument.
        old_type = arg.hdr_type
        new_type = arg.type
        if old_type.is_pyobject:
Robert Bradshaw's avatar
Robert Bradshaw committed
1740 1741 1742 1743
            if arg.default:
                code.putln("if (%s) {" % arg.hdr_cname)
            else:
                code.putln("assert(%s); {" % arg.hdr_cname)
William Stein's avatar
William Stein committed
1744
            self.generate_arg_conversion_from_pyobject(arg, code)
Robert Bradshaw's avatar
Robert Bradshaw committed
1745
            code.putln("}")
William Stein's avatar
William Stein committed
1746 1747 1748 1749 1750 1751 1752 1753
        elif new_type.is_pyobject:
            self.generate_arg_conversion_to_pyobject(arg, code)
        else:
            if new_type.assignable_from(old_type):
                code.putln(
                    "%s = %s;" % (arg.entry.cname, arg.hdr_cname))
            else:
                error(arg.pos,
1754
                    "Cannot convert 1 argument from '%s' to '%s'" %
William Stein's avatar
William Stein committed
1755 1756 1757 1758 1759
                        (old_type, new_type))
    
    def generate_arg_conversion_from_pyobject(self, arg, code):
        new_type = arg.type
        func = new_type.from_py_function
1760
        # copied from CoerceFromPyTypeNode
William Stein's avatar
William Stein committed
1761
        if func:
Robert Bradshaw's avatar
Robert Bradshaw committed
1762
            code.putln("%s = %s(%s); %s" % (
William Stein's avatar
William Stein committed
1763 1764 1765
                arg.entry.cname,
                func,
                arg.hdr_cname,
1766
                code.error_goto_if(new_type.error_condition(arg.entry.cname), arg.pos)))
William Stein's avatar
William Stein committed
1767 1768 1769 1770 1771 1772 1773 1774 1775
        else:
            error(arg.pos, 
                "Cannot convert Python object argument to type '%s'" 
                    % new_type)
    
    def generate_arg_conversion_to_pyobject(self, arg, code):
        old_type = arg.hdr_type
        func = old_type.to_py_function
        if func:
Robert Bradshaw's avatar
Robert Bradshaw committed
1776
            code.putln("%s = %s(%s); %s" % (
William Stein's avatar
William Stein committed
1777 1778 1779
                arg.entry.cname,
                func,
                arg.hdr_cname,
Robert Bradshaw's avatar
Robert Bradshaw committed
1780
                code.error_goto_if_null(arg.entry.cname, arg.pos)))
William Stein's avatar
William Stein committed
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
        else:
            error(arg.pos,
                "Cannot convert argument of type '%s' to Python object"
                    % old_type)

    def generate_argument_type_tests(self, code):
        # Generate type tests for args whose signature
        # type is PyObject * and whose declared type is
        # a subtype thereof.
        for arg in self.args:
            if arg.needs_type_test:
                self.generate_arg_type_test(arg, code)
    
    def generate_arg_type_test(self, arg, code):
        # Generate type test for one argument.
        if arg.type.typeobj_is_available():
            typeptr_cname = arg.type.typeptr_cname
            arg_code = "((PyObject *)%s)" % arg.entry.cname
            code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
1800
                'if (unlikely(!__Pyx_ArgTypeTest(%s, %s, %d, "%s"))) %s' % (
William Stein's avatar
William Stein committed
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
                    arg_code, 
                    typeptr_cname,
                    not arg.not_none,
                    arg.name,
                    code.error_goto(arg.pos)))
        else:
            error(arg.pos, "Cannot test type of extern C class "
                "without type object name specification")
    
    def error_value(self):
        return self.entry.signature.error_value
    
    def caller_will_check_exceptions(self):
        return 1
            
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
class OverrideCheckNode(StatNode):
    # A Node for dispatching to the def method if it
    # is overriden. 
    #
    #  py_func
    #
    #  args
    #  func_temp
    #  body
    
    def analyse_expressions(self, env):
        self.args = env.arg_entries
1828 1829 1830 1831
        if self.py_func.is_module_scope:
            first_arg = 0
        else:
            first_arg = 1
1832 1833
        import ExprNodes
        self.func_node = ExprNodes.PyTempNode(self.pos, env)
1834
        call_tuple = ExprNodes.TupleNode(self.pos, args=[ExprNodes.NameNode(self.pos, name=arg.name) for arg in self.args[first_arg:]])
1835 1836
        call_node = ExprNodes.SimpleCallNode(self.pos,
                                             function=self.func_node, 
1837
                                             args=[ExprNodes.NameNode(self.pos, name=arg.name) for arg in self.args[first_arg:]])
1838 1839 1840 1841 1842 1843 1844
        self.body = ReturnStatNode(self.pos, value=call_node)
#        self.func_temp = env.allocate_temp_pyobject()
        self.body.analyse_expressions(env)
#        env.release_temp(self.func_temp)
        
    def generate_execution_code(self, code):
        # Check to see if we are an extension type
1845 1846 1847 1848
        if self.py_func.is_module_scope:
            self_arg = "((PyObject *)%s)" % Naming.module_cname
        else:
            self_arg = "((PyObject *)%s)" % self.args[0].cname
1849 1850
        code.putln("/* Check if called by wrapper */")
        code.putln("if (unlikely(%s)) %s = 0;" % (Naming.skip_dispatch_cname, Naming.skip_dispatch_cname))
1851
        code.putln("/* Check if overriden in Python */")
1852 1853 1854 1855
        if self.py_func.is_module_scope:
            code.putln("else {")
        else:
            code.putln("else if (unlikely(%s->ob_type->tp_dictoffset != 0)) {" % self_arg)
1856 1857 1858 1859 1860 1861 1862 1863
        err = code.error_goto_if_null(self_arg, self.pos)
        # need to get attribute manually--scope would return cdef method
        if Options.intern_names:
            code.putln("%s = PyObject_GetAttr(%s, %s); %s" % (self.func_node.result_code, self_arg, self.py_func.interned_attr_cname, err))
        else:
            code.putln('%s = PyObject_GetAttrString(%s, "%s"); %s' % (self.func_node.result_code, self_arg, self.py_func.entry.name, err))
        # It appears that this type is not anywhere exposed in the Python/C API
        is_builtin_function_or_method = '(strcmp(%s->ob_type->tp_name, "builtin_function_or_method") == 0)' % self.func_node.result_code
Robert Bradshaw's avatar
Robert Bradshaw committed
1864
        is_overridden = '(PyCFunction_GET_FUNCTION(%s) != (void *)&%s)' % (self.func_node.result_code, self.py_func.entry.func_cname)
1865 1866 1867 1868 1869 1870 1871
        code.putln('if (!%s || %s) {' % (is_builtin_function_or_method, is_overridden))
        self.body.generate_execution_code(code)
        code.putln('}')
#        code.put_decref(self.func_temp, PyrexTypes.py_object_type)
        code.putln("}")


William Stein's avatar
William Stein committed
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886

class PyClassDefNode(StatNode, BlockNode):
    #  A Python class definition.
    #
    #  name     string          Name of the class
    #  doc      string or None
    #  body     StatNode        Attribute definition code
    #  entry    Symtab.Entry
    #  scope    PyClassScope
    #
    #  The following subnodes are constructed internally:
    #
    #  dict     DictNode   Class dictionary
    #  classobj ClassNode  Class object
    #  target   NameNode   Variable to assign class object to
1887 1888

    child_attrs = ["body", "dict", "classobj", "target"]
William Stein's avatar
William Stein committed
1889 1890 1891 1892 1893 1894 1895 1896
    
    def __init__(self, pos, name, bases, doc, body):
        StatNode.__init__(self, pos)
        self.name = name
        self.doc = doc
        self.body = body
        import ExprNodes
        self.dict = ExprNodes.DictNode(pos, key_value_pairs = [])
1897
        if self.doc and Options.docstrings:
1898 1899 1900 1901
            if Options.embed_pos_in_docstring:
                doc = 'File: %s (starting at line %s)'%relative_position(self.pos)
            doc = doc + '\\n' + self.doc
            doc_node = ExprNodes.StringNode(pos, value = doc)
William Stein's avatar
William Stein committed
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
        else:
            doc_node = None
        self.classobj = ExprNodes.ClassNode(pos,
            name = ExprNodes.StringNode(pos, value = name), 
            bases = bases, dict = self.dict, doc = doc_node)
        self.target = ExprNodes.NameNode(pos, name = name)
    
    def analyse_declarations(self, env):
        self.target.analyse_target_declaration(env)
    
    def analyse_expressions(self, env):
        self.dict.analyse_expressions(env)
        self.classobj.analyse_expressions(env)
        genv = env.global_scope()
        cenv = PyClassScope(name = self.name, outer_scope = genv)
        cenv.class_dict_cname = self.dict.result_code
        cenv.class_obj_cname = self.classobj.result_code
        self.scope = cenv
        self.body.analyse_declarations(cenv)
        self.body.analyse_expressions(cenv)
1922
        self.target.analyse_target_expression(env, self.classobj)
William Stein's avatar
William Stein committed
1923
        self.dict.release_temp(env)
1924 1925
        #self.classobj.release_temp(env)
        #self.target.release_target_temp(env)
William Stein's avatar
William Stein committed
1926
    
1927
    def generate_function_definitions(self, env, code, transforms):
William Stein's avatar
William Stein committed
1928 1929
        self.generate_py_string_decls(self.scope, code)
        self.body.generate_function_definitions(
1930
            self.scope, code, transforms)
William Stein's avatar
William Stein committed
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
    
    def generate_execution_code(self, code):
        self.dict.generate_evaluation_code(code)
        self.classobj.generate_evaluation_code(code)
        self.body.generate_execution_code(code)
        self.target.generate_assignment_code(self.classobj, code)
        self.dict.generate_disposal_code(code)


class CClassDefNode(StatNode):
    #  An extension type definition.
    #
    #  visibility         'private' or 'public' or 'extern'
    #  typedef_flag       boolean
Stefan Behnel's avatar
Stefan Behnel committed
1945
    #  api                boolean
William Stein's avatar
William Stein committed
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
    #  module_name        string or None    For import of extern type objects
    #  class_name         string            Unqualified name of class
    #  as_name            string or None    Name to declare as in this scope
    #  base_class_module  string or None    Module containing the base class
    #  base_class_name    string or None    Name of the base class
    #  objstruct_name     string or None    Specified C name of object struct
    #  typeobj_name       string or None    Specified C name of type object
    #  in_pxd             boolean           Is in a .pxd file
    #  doc                string or None
    #  body               StatNode or None
    #  entry              Symtab.Entry
    #  base_type          PyExtensionType or None
    
1959 1960
    child_attrs = ["body"]

William Stein's avatar
William Stein committed
1961 1962 1963 1964 1965 1966 1967 1968
    def analyse_declarations(self, env):
        #print "CClassDefNode.analyse_declarations:", self.class_name
        #print "...visibility =", self.visibility
        #print "...module_name =", self.module_name
        if env.in_cinclude and not self.objstruct_name:
            error(self.pos, "Object struct name specification required for "
                "C class defined in 'extern from' block")
        self.base_type = None
1969 1970 1971 1972 1973 1974 1975 1976 1977
        # Now that module imports are cached, we need to 
        # import the modules for extern classes. 
        if self.module_name:
            self.module = None
            for module in env.cimported_modules:
                if module.name == self.module_name:
                    self.module = module
            if self.module is None:
                self.module = ModuleScope(self.module_name, None, env.context)
1978
                self.module.has_extern_class = 1
1979
                env.cimported_modules.append(self.module)
1980

William Stein's avatar
William Stein committed
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
        if self.base_class_name:
            if self.base_class_module:
                base_class_scope = env.find_module(self.base_class_module, self.pos)
            else:
                base_class_scope = env
            if base_class_scope:
                base_class_entry = base_class_scope.find(self.base_class_name, self.pos)
                if base_class_entry:
                    if not base_class_entry.is_type:
                        error(self.pos, "'%s' is not a type name" % self.base_class_name)
                    elif not base_class_entry.type.is_extension_type:
                        error(self.pos, "'%s' is not an extension type" % self.base_class_name)
                    elif not base_class_entry.type.is_complete():
                        error(self.pos, "Base class '%s' is incomplete" % self.base_class_name)
                    else:
                        self.base_type = base_class_entry.type
        has_body = self.body is not None
        self.entry = env.declare_c_class(
            name = self.class_name, 
            pos = self.pos,
            defining = has_body and self.in_pxd,
            implementing = has_body and not self.in_pxd,
            module_name = self.module_name,
            base_type = self.base_type,
            objstruct_cname = self.objstruct_name,
            typeobj_cname = self.typeobj_name,
            visibility = self.visibility,
Stefan Behnel's avatar
Stefan Behnel committed
2008 2009
            typedef_flag = self.typedef_flag,
            api = self.api)
William Stein's avatar
William Stein committed
2010
        scope = self.entry.type.scope
2011
        
2012
        if self.doc and Options.docstrings:
2013 2014 2015 2016 2017
            if Options.embed_pos_in_docstring:
                scope.doc = 'File: %s (starting at line %s)'%relative_position(self.pos)
                scope.doc = scope.doc + '\\n' + self.doc
            else:
                scope.doc = self.doc
2018

William Stein's avatar
William Stein committed
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
        if has_body:
            self.body.analyse_declarations(scope)
            if self.in_pxd:
                scope.defined = 1
            else:
                scope.implemented = 1
        env.allocate_vtable_names(self.entry)
        
    def analyse_expressions(self, env):
        if self.body:
Robert Bradshaw's avatar
Robert Bradshaw committed
2029 2030
            scope = self.entry.type.scope
            self.body.analyse_expressions(scope)
William Stein's avatar
William Stein committed
2031
    
2032
    def generate_function_definitions(self, env, code, transforms):
William Stein's avatar
William Stein committed
2033 2034
        if self.body:
            self.body.generate_function_definitions(
2035
                self.entry.type.scope, code, transforms)
William Stein's avatar
William Stein committed
2036 2037 2038 2039 2040 2041
    
    def generate_execution_code(self, code):
        # This is needed to generate evaluation code for
        # default values of method arguments.
        if self.body:
            self.body.generate_execution_code(code)
2042 2043 2044 2045
            
    def annotate(self, code):
        if self.body:
            self.body.annotate(code)
William Stein's avatar
William Stein committed
2046 2047 2048 2049 2050 2051 2052 2053 2054


class PropertyNode(StatNode):
    #  Definition of a property in an extension type.
    #
    #  name   string
    #  doc    string or None    Doc string
    #  body   StatListNode
    
2055 2056
    child_attrs = ["body"]

William Stein's avatar
William Stein committed
2057 2058 2059
    def analyse_declarations(self, env):
        entry = env.declare_property(self.name, self.doc, self.pos)
        if entry:
2060
            if self.doc and Options.docstrings:
William Stein's avatar
William Stein committed
2061 2062 2063 2064 2065 2066 2067
                doc_entry = env.get_string_const(self.doc)
                entry.doc_cname = doc_entry.cname
            self.body.analyse_declarations(entry.scope)
        
    def analyse_expressions(self, env):
        self.body.analyse_expressions(env)
    
2068 2069
    def generate_function_definitions(self, env, code, transforms):
        self.body.generate_function_definitions(env, code, transforms)
William Stein's avatar
William Stein committed
2070 2071 2072 2073

    def generate_execution_code(self, code):
        pass

2074 2075 2076
    def annotate(self, code):
        self.body.annotate(code)

William Stein's avatar
William Stein committed
2077 2078 2079 2080 2081 2082

class GlobalNode(StatNode):
    # Global variable declaration.
    #
    # names    [string]
    
2083 2084
    child_attrs = []

William Stein's avatar
William Stein committed
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
    def analyse_declarations(self, env):
        for name in self.names:
            env.declare_global(name, self.pos)

    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass


class ExprStatNode(StatNode):
    #  Expression used as a statement.
    #
    #  expr   ExprNode
2100 2101

    child_attrs = ["expr"]
William Stein's avatar
William Stein committed
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
    
    def analyse_expressions(self, env):
        self.expr.analyse_expressions(env)
        self.expr.release_temp(env)
    
    def generate_execution_code(self, code):
        self.expr.generate_evaluation_code(code)
        if not self.expr.is_temp and self.expr.result_code:
            code.putln("%s;" % self.expr.result_code)
        self.expr.generate_disposal_code(code)

2113 2114 2115
    def annotate(self, code):
        self.expr.annotate(code)

William Stein's avatar
William Stein committed
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126

class AssignmentNode(StatNode):
    #  Abstract base class for assignment nodes.
    #
    #  The analyse_expressions and generate_execution_code
    #  phases of assignments are split into two sub-phases
    #  each, to enable all the right hand sides of a
    #  parallel assignment to be evaluated before assigning
    #  to any of the left hand sides.

    def analyse_expressions(self, env):
2127 2128 2129 2130 2131 2132 2133
        self.analyse_types(env)
        self.allocate_rhs_temps(env)
        self.allocate_lhs_temps(env)

#	def analyse_expressions(self, env):
#		self.analyse_expressions_1(env)
#		self.analyse_expressions_2(env)
William Stein's avatar
William Stein committed
2134 2135 2136 2137

    def generate_execution_code(self, code):
        self.generate_rhs_evaluation_code(code)
        self.generate_assignment_code(code)
2138
        
William Stein's avatar
William Stein committed
2139 2140 2141 2142 2143 2144 2145 2146

class SingleAssignmentNode(AssignmentNode):
    #  The simplest case:
    #
    #    a = b
    #
    #  lhs      ExprNode      Left hand side
    #  rhs      ExprNode      Right hand side
2147 2148
    
    child_attrs = ["lhs", "rhs"]
William Stein's avatar
William Stein committed
2149 2150 2151 2152

    def analyse_declarations(self, env):
        self.lhs.analyse_target_declaration(env)
    
2153
    def analyse_types(self, env, use_temp = 0):
William Stein's avatar
William Stein committed
2154 2155 2156 2157 2158
        self.rhs.analyse_types(env)
        self.lhs.analyse_target_types(env)
        self.rhs = self.rhs.coerce_to(self.lhs.type, env)
        if use_temp:
            self.rhs = self.rhs.coerce_to_temp(env)
2159 2160
    
    def allocate_rhs_temps(self, env):
William Stein's avatar
William Stein committed
2161
        self.rhs.allocate_temps(env)
2162 2163 2164 2165 2166

    def allocate_lhs_temps(self, env):
        self.lhs.allocate_target_temps(env, self.rhs)
        #self.lhs.release_target_temp(env)
        #self.rhs.release_temp(env)		
William Stein's avatar
William Stein committed
2167
    
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
#	def analyse_expressions_1(self, env, use_temp = 0):
#		self.rhs.analyse_types(env)
#		self.lhs.analyse_target_types(env)
#		self.rhs = self.rhs.coerce_to(self.lhs.type, env)
#		if use_temp:
#			self.rhs = self.rhs.coerce_to_temp(env)
#		self.rhs.allocate_temps(env)
#	
#	def analyse_expressions_2(self, env):
#		self.lhs.allocate_target_temps(env)
#		self.lhs.release_target_temp(env)
#		self.rhs.release_temp(env)		
William Stein's avatar
William Stein committed
2180 2181 2182 2183 2184 2185 2186

    def generate_rhs_evaluation_code(self, code):
        self.rhs.generate_evaluation_code(code)
    
    def generate_assignment_code(self, code):
        self.lhs.generate_assignment_code(self.rhs, code)

2187 2188 2189 2190
    def annotate(self, code):
        self.lhs.annotate(code)
        self.rhs.annotate(code)

William Stein's avatar
William Stein committed
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203

class CascadedAssignmentNode(AssignmentNode):
    #  An assignment with multiple left hand sides:
    #
    #    a = b = c
    #
    #  lhs_list   [ExprNode]   Left hand sides
    #  rhs        ExprNode     Right hand sides
    #
    #  Used internally:
    #
    #  coerced_rhs_list   [ExprNode]   RHS coerced to type of each LHS
    
2204 2205
    child_attrs = ["lhs_list", "rhs", "coerced_rhs_list"]

William Stein's avatar
William Stein committed
2206 2207 2208 2209
    def analyse_declarations(self, env):
        for lhs in self.lhs_list:
            lhs.analyse_target_declaration(env)
    
2210
    def analyse_types(self, env, use_temp = 0):
William Stein's avatar
William Stein committed
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
        self.rhs.analyse_types(env)
        if use_temp:
            self.rhs = self.rhs.coerce_to_temp(env)
        else:
            self.rhs = self.rhs.coerce_to_simple(env)
        from ExprNodes import CloneNode
        self.coerced_rhs_list = []
        for lhs in self.lhs_list:
            lhs.analyse_target_types(env)
            rhs = CloneNode(self.rhs)
            rhs = rhs.coerce_to(lhs.type, env)
            self.coerced_rhs_list.append(rhs)
2223 2224 2225 2226 2227 2228

    def allocate_rhs_temps(self, env):
        self.rhs.allocate_temps(env)
    
    def allocate_lhs_temps(self, env):
        for lhs, rhs in zip(self.lhs_list, self.coerced_rhs_list):
William Stein's avatar
William Stein committed
2229
            rhs.allocate_temps(env)
2230 2231 2232
            lhs.allocate_target_temps(env, rhs)
            #lhs.release_target_temp(env)
            #rhs.release_temp(env)
William Stein's avatar
William Stein committed
2233
        self.rhs.release_temp(env)
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
    
#	def analyse_expressions_1(self, env, use_temp = 0):
#		self.rhs.analyse_types(env)
#		if use_temp:
#			self.rhs = self.rhs.coerce_to_temp(env)
#		else:
#			self.rhs = self.rhs.coerce_to_simple(env)
#		self.rhs.allocate_temps(env)
#	
#	def analyse_expressions_2(self, env):
#		from ExprNodes import CloneNode
#		self.coerced_rhs_list = []
#		for lhs in self.lhs_list:
#			lhs.analyse_target_types(env)
#			rhs = CloneNode(self.rhs)
#			rhs = rhs.coerce_to(lhs.type, env)
#			self.coerced_rhs_list.append(rhs)
#			rhs.allocate_temps(env)
#			lhs.allocate_target_temps(env)
#			lhs.release_target_temp(env)
#			rhs.release_temp(env)
#		self.rhs.release_temp(env)
William Stein's avatar
William Stein committed
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
    
    def generate_rhs_evaluation_code(self, code):
        self.rhs.generate_evaluation_code(code)
    
    def generate_assignment_code(self, code):
        for i in range(len(self.lhs_list)):
            lhs = self.lhs_list[i]
            rhs = self.coerced_rhs_list[i]
            rhs.generate_evaluation_code(code)
            lhs.generate_assignment_code(rhs, code)
            # Assignment has disposed of the cloned RHS
        self.rhs.generate_disposal_code(code)

2269 2270 2271 2272 2273 2274 2275
    def annotate(self, code):
        for i in range(len(self.lhs_list)):
            lhs = self.lhs_list[i].annotate(code)
            rhs = self.coerced_rhs_list[i].annotate(code)
        self.rhs.annotate(code)
        

William Stein's avatar
William Stein committed
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
class ParallelAssignmentNode(AssignmentNode):
    #  A combined packing/unpacking assignment:
    #
    #    a, b, c =  d, e, f
    #
    #  This has been rearranged by the parser into
    #
    #    a = d ; b = e ; c = f
    #
    #  but we must evaluate all the right hand sides
    #  before assigning to any of the left hand sides.
    #
    #  stats     [AssignmentNode]   The constituent assignments
    
2290 2291
    child_attrs = ["stats"]

William Stein's avatar
William Stein committed
2292 2293 2294 2295 2296 2297
    def analyse_declarations(self, env):
        for stat in self.stats:
            stat.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        for stat in self.stats:
2298 2299
            stat.analyse_types(env, use_temp = 1)
            stat.allocate_rhs_temps(env)
William Stein's avatar
William Stein committed
2300
        for stat in self.stats:
2301 2302 2303 2304 2305 2306 2307
            stat.allocate_lhs_temps(env)

#	def analyse_expressions(self, env):
#		for stat in self.stats:
#			stat.analyse_expressions_1(env, use_temp = 1)
#		for stat in self.stats:
#			stat.analyse_expressions_2(env)
William Stein's avatar
William Stein committed
2308 2309 2310 2311 2312 2313 2314
    
    def generate_execution_code(self, code):
        for stat in self.stats:
            stat.generate_rhs_evaluation_code(code)
        for stat in self.stats:
            stat.generate_assignment_code(code)

2315 2316 2317 2318 2319
    def annotate(self, code):
        for stat in self.stats:
            stat.annotate(code)


2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
class InPlaceAssignmentNode(AssignmentNode):
    #  An in place arithmatic operand:
    #
    #    a += b
    #    a -= b
    #    ...
    #
    #  lhs      ExprNode      Left hand side
    #  rhs      ExprNode      Right hand side
    #  op       char          one of "+-*/%^&|"
    #  dup     (ExprNode)     copy of lhs used for operation (auto-generated)
    #
    #  This code is a bit tricky because in order to obey Python 
    #  semantics the sub-expressions (e.g. indices) of the lhs must 
    #  not be evaluated twice. So we must re-use the values calculated 
    #  in evaluation phase for the assignment phase as well. 
    #  Fortunately, the type of the lhs node is fairly constrained 
    #  (it must be a NameNode, AttributeNode, or IndexNode).     
    
2339 2340
    child_attrs = ["lhs", "rhs", "dup"]

2341 2342
    def analyse_declarations(self, env):
        self.lhs.analyse_target_declaration(env)
2343 2344 2345
        
    def analyse_types(self, env):
        self.dup = self.create_dup_node(env) # re-assigns lhs to a shallow copy
2346 2347
        self.rhs.analyse_types(env)
        self.lhs.analyse_target_types(env)
2348 2349
        if Options.incref_local_binop and self.dup.type.is_pyobject:
            self.dup = self.dup.coerce_to_temp(env)
2350 2351
        
    def allocate_rhs_temps(self, env):
2352
        import ExprNodes
2353 2354 2355
        if self.lhs.type.is_pyobject:
            self.rhs = self.rhs.coerce_to_pyobject(env)
        elif self.rhs.type.is_pyobject:
2356 2357
            self.rhs = self.rhs.coerce_to(self.lhs.type, env)
        if self.lhs.type.is_pyobject:
2358
             self.result = ExprNodes.PyTempNode(self.pos, env).coerce_to(self.lhs.type, env)
2359
             self.result.allocate_temps(env)
2360 2361
#        if use_temp:
#            self.rhs = self.rhs.coerce_to_temp(env)
2362
        self.rhs.allocate_temps(env)
2363 2364 2365
        self.dup.allocate_subexpr_temps(env)
        self.dup.allocate_temp(env)
    
2366
    def allocate_lhs_temps(self, env):
2367 2368
        self.lhs.allocate_target_temps(env, self.rhs)
#        self.lhs.release_target_temp(env)
2369 2370 2371
        self.dup.release_temp(env)
        if self.dup.is_temp:
            self.dup.release_subexpr_temps(env)
2372
#        self.rhs.release_temp(env)
2373 2374 2375 2376 2377 2378 2379 2380 2381
        if self.lhs.type.is_pyobject:
            self.result.release_temp(env)

    def generate_execution_code(self, code):
        self.rhs.generate_evaluation_code(code)
        self.dup.generate_subexpr_evaluation_code(code)
        self.dup.generate_result_code(code)
        if self.lhs.type.is_pyobject:
            code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
2382
                "%s = %s(%s, %s); %s" % (
2383 2384 2385 2386
                    self.result.result_code, 
                    self.py_operation_function(), 
                    self.dup.py_result(),
                    self.rhs.py_result(),
Robert Bradshaw's avatar
Robert Bradshaw committed
2387
                    code.error_goto_if_null(self.result.py_result(), self.pos)))
2388
            self.result.generate_evaluation_code(code) # May be a type check...
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
            self.rhs.generate_disposal_code(code)
            self.dup.generate_disposal_code(code)
            self.lhs.generate_assignment_code(self.result, code)
        else: 
            # have to do assignment directly to avoid side-effects
            code.putln("%s %s= %s;" % (self.lhs.result_code, self.operator, self.rhs.result_code) )
            self.rhs.generate_disposal_code(code)
        if self.dup.is_temp:
            self.dup.generate_subexpr_disposal_code(code)
            
    def create_dup_node(self, env): 
        import ExprNodes
        self.dup = self.lhs
        self.dup.analyse_types(env)
        if isinstance(self.lhs, ExprNodes.NameNode):
            target_lhs = ExprNodes.NameNode(self.dup.pos, name = self.dup.name, is_temp = self.dup.is_temp, entry = self.dup.entry)
        elif isinstance(self.lhs, ExprNodes.AttributeNode):
            target_lhs = ExprNodes.AttributeNode(self.dup.pos, obj = ExprNodes.CloneNode(self.lhs.obj), attribute = self.dup.attribute, is_temp = self.dup.is_temp)
        elif isinstance(self.lhs, ExprNodes.IndexNode):
            target_lhs = ExprNodes.IndexNode(self.dup.pos, base = ExprNodes.CloneNode(self.dup.base), index = ExprNodes.CloneNode(self.lhs.index), is_temp = self.dup.is_temp)
        self.lhs = target_lhs
2410
        return self.dup
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
    
    def py_operation_function(self):
        return self.py_functions[self.operator]

    py_functions = {
        "|":		"PyNumber_InPlaceOr",
        "^":		"PyNumber_InPlaceXor",
        "&":		"PyNumber_InPlaceAnd",
        "+":		"PyNumber_InPlaceAdd",
        "-":		"PyNumber_InPlaceSubtract",
        "*":		"PyNumber_InPlaceMultiply",
        "/":		"PyNumber_InPlaceDivide",
        "%":		"PyNumber_InPlaceRemainder",
    }

2426 2427 2428 2429 2430
    def annotate(self, code):
        self.lhs.annotate(code)
        self.rhs.annotate(code)
        self.dup.annotate(code)

William Stein's avatar
William Stein committed
2431 2432 2433 2434 2435 2436 2437

class PrintStatNode(StatNode):
    #  print statement
    #
    #  args              [ExprNode]
    #  ends_with_comma   boolean
    
2438 2439
    child_attrs = ["args"]
    
William Stein's avatar
William Stein committed
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
    def analyse_expressions(self, env):
        for i in range(len(self.args)):
            arg = self.args[i]
            arg.analyse_types(env)
            arg = arg.coerce_to_pyobject(env)
            arg.allocate_temps(env)
            arg.release_temp(env)
            self.args[i] = arg
            #env.recycle_pending_temps() # TEMPORARY
        env.use_utility_code(printing_utility_code)
    
    def generate_execution_code(self, code):
        for arg in self.args:
            arg.generate_evaluation_code(code)
            code.putln(
                "if (__Pyx_PrintItem(%s) < 0) %s" % (
                    arg.py_result(),
                    code.error_goto(self.pos)))
            arg.generate_disposal_code(code)
        if not self.ends_with_comma:
            code.putln(
                "if (__Pyx_PrintNewline() < 0) %s" %
                    code.error_goto(self.pos))
2463 2464 2465 2466
                    
    def annotate(self, code):
        for arg in self.args:
            arg.annotate(code)
William Stein's avatar
William Stein committed
2467 2468 2469 2470 2471 2472 2473


class DelStatNode(StatNode):
    #  del statement
    #
    #  args     [ExprNode]
    
2474 2475
    child_attrs = ["args"]

William Stein's avatar
William Stein committed
2476 2477 2478 2479 2480 2481
    def analyse_declarations(self, env):
        for arg in self.args:
            arg.analyse_target_declaration(env)
    
    def analyse_expressions(self, env):
        for arg in self.args:
2482
            arg.analyse_target_expression(env, None)
William Stein's avatar
William Stein committed
2483 2484
            if not arg.type.is_pyobject:
                error(arg.pos, "Deletion of non-Python object")
2485
            #arg.release_target_temp(env)
William Stein's avatar
William Stein committed
2486 2487 2488 2489 2490 2491 2492
    
    def generate_execution_code(self, code):
        for arg in self.args:
            if arg.type.is_pyobject:
                arg.generate_deletion_code(code)
            # else error reported earlier

2493 2494 2495 2496
    def annotate(self, code):
        for arg in self.args:
            arg.annotate(code)

William Stein's avatar
William Stein committed
2497 2498 2499

class PassStatNode(StatNode):
    #  pass statement
2500 2501

    child_attrs = []
William Stein's avatar
William Stein committed
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
    
    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass


class BreakStatNode(StatNode):

2512 2513
    child_attrs = []

William Stein's avatar
William Stein committed
2514 2515 2516 2517 2518 2519 2520
    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        if not code.break_label:
            error(self.pos, "break statement not inside loop")
        else:
2521 2522 2523 2524
            #code.putln(
            #	"goto %s;" %
            #		code.break_label)
            code.put_goto(code.break_label)
William Stein's avatar
William Stein committed
2525 2526 2527 2528


class ContinueStatNode(StatNode):

2529 2530
    child_attrs = []

William Stein's avatar
William Stein committed
2531 2532 2533 2534 2535 2536 2537 2538 2539
    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        if code.in_try_finally:
            error(self.pos, "continue statement inside try of try...finally")
        elif not code.continue_label:
            error(self.pos, "continue statement not inside loop")
        else:
2540
            code.put_goto(code.continue_label)
William Stein's avatar
William Stein committed
2541 2542 2543 2544 2545 2546 2547 2548 2549


class ReturnStatNode(StatNode):
    #  return statement
    #
    #  value         ExprNode or None
    #  return_type   PyrexType
    #  temps_in_use  [Entry]            Temps in use at time of return
    
2550 2551
    child_attrs = ["value"]

William Stein's avatar
William Stein committed
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
    def analyse_expressions(self, env):
        return_type = env.return_type
        self.return_type = return_type
        self.temps_in_use = env.temps_in_use()
        if not return_type:
            error(self.pos, "Return not inside a function body")
            return
        if self.value:
            self.value.analyse_types(env)
            if return_type.is_void or return_type.is_returncode:
                error(self.value.pos, 
                    "Return with value in void function")
            else:
                self.value = self.value.coerce_to(env.return_type, env)
            self.value.allocate_temps(env)
            self.value.release_temp(env)
        else:
            if (not return_type.is_void
                and not return_type.is_pyobject
                and not return_type.is_returncode):
                    error(self.pos, "Return value required")
    
    def generate_execution_code(self, code):
2575
        code.mark_pos(self.pos)
William Stein's avatar
William Stein committed
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
        if not self.return_type:
            # error reported earlier
            return
        if self.value:
            self.value.generate_evaluation_code(code)
            self.value.make_owned_reference(code)
            code.putln(
                "%s = %s;" % (
                    Naming.retval_cname,
                    self.value.result_as(self.return_type)))
            self.value.generate_post_assignment_code(code)
        else:
            if self.return_type.is_pyobject:
                code.put_init_to_py_none(Naming.retval_cname, self.return_type)
            elif self.return_type.is_returncode:
                code.putln(
                    "%s = %s;" % (
                        Naming.retval_cname,
                        self.return_type.default_value))
2595 2596
        for entry in self.temps_in_use:
            code.put_var_decref_clear(entry)
2597 2598 2599 2600
        #code.putln(
        #	"goto %s;" %
        #		code.return_label)
        code.put_goto(code.return_label)
2601 2602 2603 2604
        
    def annotate(self, code):
        if self.value:
            self.value.annotate(code)
William Stein's avatar
William Stein committed
2605 2606 2607 2608 2609 2610 2611 2612 2613


class RaiseStatNode(StatNode):
    #  raise statement
    #
    #  exc_type    ExprNode or None
    #  exc_value   ExprNode or None
    #  exc_tb      ExprNode or None
    
2614 2615
    child_attrs = ["exc_type", "exc_value", "exc_tb"]

William Stein's avatar
William Stein committed
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
    def analyse_expressions(self, env):
        if self.exc_type:
            self.exc_type.analyse_types(env)
            self.exc_type = self.exc_type.coerce_to_pyobject(env)
            self.exc_type.allocate_temps(env)
        if self.exc_value:
            self.exc_value.analyse_types(env)
            self.exc_value = self.exc_value.coerce_to_pyobject(env)
            self.exc_value.allocate_temps(env)
        if self.exc_tb:
            self.exc_tb.analyse_types(env)
            self.exc_tb = self.exc_tb.coerce_to_pyobject(env)
            self.exc_tb.allocate_temps(env)
        if self.exc_type:
            self.exc_type.release_temp(env)
        if self.exc_value:
            self.exc_value.release_temp(env)
        if self.exc_tb:
            self.exc_tb.release_temp(env)
2635 2636 2637 2638
#		if not (self.exc_type or self.exc_value or self.exc_tb):
#			env.use_utility_code(reraise_utility_code)
#		else:
        env.use_utility_code(raise_utility_code)
William Stein's avatar
William Stein committed
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
    
    def generate_execution_code(self, code):
        if self.exc_type:
            self.exc_type.generate_evaluation_code(code)
            type_code = self.exc_type.py_result()
        else:
            type_code = 0
        if self.exc_value:
            self.exc_value.generate_evaluation_code(code)
            value_code = self.exc_value.py_result()
        else:
            value_code = "0"
        if self.exc_tb:
            self.exc_tb.generate_evaluation_code(code)
            tb_code = self.exc_tb.py_result()
        else:
            tb_code = "0"
        if self.exc_type or self.exc_value or self.exc_tb:
            code.putln(
                "__Pyx_Raise(%s, %s, %s);" % (
                    type_code,
                    value_code,
                    tb_code))
        else:
            code.putln(
                "__Pyx_ReRaise();")
        if self.exc_type:
            self.exc_type.generate_disposal_code(code)
        if self.exc_value:
            self.exc_value.generate_disposal_code(code)
        if self.exc_tb:
            self.exc_tb.generate_disposal_code(code)
        code.putln(
            code.error_goto(self.pos))

2674 2675 2676 2677 2678 2679 2680 2681
    def annotate(self, code):
        if self.exc_type:
            self.exc_type.annotate(code)
        if self.exc_value:
            self.exc_value.annotate(code)
        if self.exc_tb:
            self.exc_tb.annotate(code)

William Stein's avatar
William Stein committed
2682

2683 2684
class ReraiseStatNode(StatNode):

2685 2686
    child_attrs = []

2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
    def analyse_expressions(self, env):
        env.use_utility_code(raise_utility_code)

    def generate_execution_code(self, code):
        vars = code.exc_vars
        if vars:
            code.putln("__Pyx_Raise(%s, %s, %s);" % tuple(vars))
            code.putln(code.error_goto(self.pos))
        else:
            error(self.pos, "Reraise not inside except clause")
        

William Stein's avatar
William Stein committed
2699 2700 2701 2702 2703 2704
class AssertStatNode(StatNode):
    #  assert statement
    #
    #  cond    ExprNode
    #  value   ExprNode or None
    
2705 2706
    child_attrs = ["cond", "value"]

William Stein's avatar
William Stein committed
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
    def analyse_expressions(self, env):
        self.cond = self.cond.analyse_boolean_expression(env)
        if self.value:
            self.value.analyse_types(env)
            self.value = self.value.coerce_to_pyobject(env)
            self.value.allocate_temps(env)
        self.cond.release_temp(env)
        if self.value:
            self.value.release_temp(env)
        #env.recycle_pending_temps() # TEMPORARY
    
    def generate_execution_code(self, code):
2719
        code.putln("#ifndef PYREX_WITHOUT_ASSERTIONS")
William Stein's avatar
William Stein committed
2720 2721
        self.cond.generate_evaluation_code(code)
        code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
2722
            "if (unlikely(!%s)) {" %
William Stein's avatar
William Stein committed
2723 2724
                self.cond.result_code)
        if self.value:
2725
            self.value.generate_evaluation_code(code)
William Stein's avatar
William Stein committed
2726 2727 2728
            code.putln(
                "PyErr_SetObject(PyExc_AssertionError, %s);" %
                    self.value.py_result())
2729
            self.value.generate_disposal_code(code)
William Stein's avatar
William Stein committed
2730 2731 2732 2733 2734 2735 2736 2737
        else:
            code.putln(
                "PyErr_SetNone(PyExc_AssertionError);")
        code.putln(
                code.error_goto(self.pos))
        code.putln(
            "}")
        self.cond.generate_disposal_code(code)
2738
        code.putln("#endif")
William Stein's avatar
William Stein committed
2739

2740 2741 2742 2743 2744 2745
    def annotate(self, code):
        self.cond.annotate(code)
        if self.value:
            self.value.annotate(code)


William Stein's avatar
William Stein committed
2746 2747 2748 2749 2750
class IfStatNode(StatNode):
    #  if statement
    #
    #  if_clauses   [IfClauseNode]
    #  else_clause  StatNode or None
2751 2752

    child_attrs = ["if_clauses", "else_clause"]
William Stein's avatar
William Stein committed
2753
    
2754 2755 2756 2757 2758 2759 2760 2761 2762
    def analyse_control_flow(self, env):
        env.start_branching(self.pos)
        for if_clause in self.if_clauses:
            if_clause.analyse_control_flow(env)
            env.next_branch(if_clause.end_pos())
        if self.else_clause:
            self.else_clause.analyse_control_flow(env)
        env.finish_branching(self.end_pos())

William Stein's avatar
William Stein committed
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
    def analyse_declarations(self, env):
        for if_clause in self.if_clauses:
            if_clause.analyse_declarations(env)
        if self.else_clause:
            self.else_clause.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        for if_clause in self.if_clauses:
            if_clause.analyse_expressions(env)
        if self.else_clause:
            self.else_clause.analyse_expressions(env)
    
    def generate_execution_code(self, code):
2776
        code.mark_pos(self.pos)
William Stein's avatar
William Stein committed
2777 2778 2779 2780 2781 2782 2783 2784
        end_label = code.new_label()
        for if_clause in self.if_clauses:
            if_clause.generate_execution_code(code, end_label)
        if self.else_clause:
            code.putln("/*else*/ {")
            self.else_clause.generate_execution_code(code)
            code.putln("}")
        code.put_label(end_label)
2785 2786 2787 2788 2789 2790
        
    def annotate(self, code):
        for if_clause in self.if_clauses:
            if_clause.annotate(code)
        if self.else_clause:
            self.else_clause.annotate(code)
William Stein's avatar
William Stein committed
2791 2792 2793 2794 2795 2796 2797 2798


class IfClauseNode(Node):
    #  if or elif clause in an if statement
    #
    #  condition   ExprNode
    #  body        StatNode
    
2799 2800
    child_attrs = ["condition", "body"]

2801 2802 2803
    def analyse_control_flow(self, env):
        self.body.analyse_control_flow(env)
        
William Stein's avatar
William Stein committed
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
    def analyse_declarations(self, env):
        self.condition.analyse_declarations(env)
        self.body.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        self.condition = \
            self.condition.analyse_temp_boolean_expression(env)
        self.condition.release_temp(env)
        #env.recycle_pending_temps() # TEMPORARY
        self.body.analyse_expressions(env)
    
    def generate_execution_code(self, code, end_label):
        self.condition.generate_evaluation_code(code)
        code.putln(
            "if (%s) {" %
                self.condition.result_code)
        self.body.generate_execution_code(code)
2821 2822 2823 2824
        #code.putln(
        #	"goto %s;" %
        #		end_label)
        code.put_goto(end_label)
William Stein's avatar
William Stein committed
2825
        code.putln("}")
2826 2827 2828 2829

    def annotate(self, code):
        self.condition.annotate(code)
        self.body.annotate(code)
William Stein's avatar
William Stein committed
2830
        
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
        
class LoopNode:
    
    def analyse_control_flow(self, env):
        env.start_branching(self.pos)
        self.body.analyse_control_flow(env)
        env.next_branch(self.body.end_pos())
        if self.else_clause:
            self.else_clause.analyse_control_flow(env)
        env.finish_branching(self.end_pos())

William Stein's avatar
William Stein committed
2842
    
2843
class WhileStatNode(LoopNode, StatNode):
William Stein's avatar
William Stein committed
2844 2845 2846 2847 2848
    #  while statement
    #
    #  condition    ExprNode
    #  body         StatNode
    #  else_clause  StatNode
2849 2850

    child_attrs = ["condition", "body", "else_clause"]
2851

William Stein's avatar
William Stein committed
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
    def analyse_declarations(self, env):
        self.body.analyse_declarations(env)
        if self.else_clause:
            self.else_clause.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        self.condition = \
            self.condition.analyse_temp_boolean_expression(env)
        self.condition.release_temp(env)
        #env.recycle_pending_temps() # TEMPORARY
        self.body.analyse_expressions(env)
        if self.else_clause:
            self.else_clause.analyse_expressions(env)
    
    def generate_execution_code(self, code):
        old_loop_labels = code.new_loop_labels()
        code.putln(
            "while (1) {")
        self.condition.generate_evaluation_code(code)
        code.putln(
            "if (!%s) break;" %
                self.condition.result_code)
        self.body.generate_execution_code(code)
2875
        code.put_label(code.continue_label)
William Stein's avatar
William Stein committed
2876 2877 2878 2879 2880 2881 2882 2883 2884
        code.putln("}")
        break_label = code.break_label
        code.set_loop_labels(old_loop_labels)
        if self.else_clause:
            code.putln("/*else*/ {")
            self.else_clause.generate_execution_code(code)
            code.putln("}")
        code.put_label(break_label)

2885 2886 2887 2888 2889 2890
    def annotate(self, code):
        self.condition.annotate(code)
        self.body.annotate(code)
        if self.else_clause:
            self.else_clause.annotate(code)

William Stein's avatar
William Stein committed
2891

Robert Bradshaw's avatar
Robert Bradshaw committed
2892 2893 2894 2895 2896 2897
def ForStatNode(pos, **kw):
    if kw.has_key('iterator'):
        return ForInStatNode(pos, **kw)
    else:
        return ForFromStatNode(pos, **kw)

2898
class ForInStatNode(LoopNode, StatNode):
William Stein's avatar
William Stein committed
2899 2900 2901 2902 2903 2904 2905 2906
    #  for statement
    #
    #  target        ExprNode
    #  iterator      IteratorNode
    #  body          StatNode
    #  else_clause   StatNode
    #  item          NextNode       used internally
    
2907 2908
    child_attrs = ["target", "iterator", "body", "else_clause", "item"]
    
William Stein's avatar
William Stein committed
2909 2910 2911 2912 2913
    def analyse_declarations(self, env):
        self.target.analyse_target_declaration(env)
        self.body.analyse_declarations(env)
        if self.else_clause:
            self.else_clause.analyse_declarations(env)
2914 2915 2916 2917 2918 2919
            
    def analyse_range_step(self, args):
        import ExprNodes
        # The direction must be determined at compile time to set relations. 
        # Otherwise, return False. 
        if len(args) < 3:
2920
            self.step = ExprNodes.IntNode(pos = args[0].pos, value='1')
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
            self.relation1 = '<='
            self.relation2 = '<'
            return True
        else:
            step = args[2]
            if isinstance(step, ExprNodes.UnaryMinusNode) and isinstance(step.operand, ExprNodes.IntNode):
                step = ExprNodes.IntNode(pos = step.pos, value=-int(step.operand.value))
            if isinstance(step, ExprNodes.IntNode):
                if step.value > 0:
                    self.step = step
                    self.relation1 = '<='
                    self.relation2 = '<'
                    return True
                elif step.value < 0:
                    self.step = ExprNodes.IntNode(pos = step.pos, value=-int(step.value))
                    self.relation1 = '>='
                    self.relation2 = '>'
                    return True
        return False
                
William Stein's avatar
William Stein committed
2941 2942 2943 2944
    
    def analyse_expressions(self, env):
        import ExprNodes
        self.target.analyse_target_types(env)
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
        if Options.convert_range and self.target.type.is_int:
            sequence = self.iterator.sequence
            if isinstance(sequence, ExprNodes.SimpleCallNode) \
                  and sequence.self is None \
                  and isinstance(sequence.function, ExprNodes.NameNode) \
                  and sequence.function.name == 'range':
                args = sequence.args
                # Make sure we can determine direction from step
                if self.analyse_range_step(args):
                    # Mutate to ForFrom loop type
                    self.__class__ = ForFromStatNode
                    if len(args) == 1:
                        self.bound1 = ExprNodes.IntNode(pos = sequence.pos, value='0')
                        self.bound2 = args[0]
                    else:
                        self.bound1 = args[0]
                        self.bound2 = args[1]
                    ForFromStatNode.analyse_expressions(self, env)
                    return
                    
        self.iterator.analyse_expressions(env)
William Stein's avatar
William Stein committed
2966 2967 2968
        self.item = ExprNodes.NextNode(self.iterator, env)
        self.item = self.item.coerce_to(self.target.type, env)
        self.item.allocate_temps(env)
2969 2970 2971
        self.target.allocate_target_temps(env, self.item)
        #self.item.release_temp(env)
        #self.target.release_target_temp(env)
William Stein's avatar
William Stein committed
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
        self.body.analyse_expressions(env)
        if self.else_clause:
            self.else_clause.analyse_expressions(env)
        self.iterator.release_temp(env)

    def generate_execution_code(self, code):
        old_loop_labels = code.new_loop_labels()
        self.iterator.generate_evaluation_code(code)
        code.putln(
            "for (;;) {")
        self.item.generate_evaluation_code(code)
        self.target.generate_assignment_code(self.item, code)
        self.body.generate_execution_code(code)
2985
        code.put_label(code.continue_label)
William Stein's avatar
William Stein committed
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
        code.putln(
            "}")
        break_label = code.break_label
        code.set_loop_labels(old_loop_labels)
        if self.else_clause:
            code.putln("/*else*/ {")
            self.else_clause.generate_execution_code(code)
            code.putln("}")
        code.put_label(break_label)
        self.iterator.generate_disposal_code(code)

2997 2998 2999 3000 3001 3002 3003 3004
    def annotate(self, code):
        self.target.annotate(code)
        self.iterator.annotate(code)
        self.body.annotate(code)
        if self.else_clause:
            self.else_clause.annotate(code)
        self.item.annotate(code)

William Stein's avatar
William Stein committed
3005

3006
class ForFromStatNode(LoopNode, StatNode):
William Stein's avatar
William Stein committed
3007 3008 3009 3010 3011 3012 3013
    #  for name from expr rel name rel expr
    #
    #  target        NameNode
    #  bound1        ExprNode
    #  relation1     string
    #  relation2     string
    #  bound2        ExprNode
3014
    #  step          ExprNode or None
William Stein's avatar
William Stein committed
3015 3016 3017 3018 3019
    #  body          StatNode
    #  else_clause   StatNode or None
    #
    #  Used internally:
    #
3020
    #  is_py_target       bool
William Stein's avatar
William Stein committed
3021 3022
    #  loopvar_name       string
    #  py_loopvar_node    PyTempNode or None
3023
    child_attrs = ["target", "bound1", "bound2", "step", "body", "else_clause", "py_loopvar_node"]
William Stein's avatar
William Stein committed
3024 3025 3026 3027 3028 3029
    
    def analyse_expressions(self, env):
        import ExprNodes
        self.target.analyse_target_types(env)
        self.bound1.analyse_types(env)
        self.bound2.analyse_types(env)
3030 3031 3032 3033 3034 3035
        if self.target.type.is_numeric:
            self.bound1 = self.bound1.coerce_to(self.target.type, env)
            self.bound2 = self.bound2.coerce_to(self.target.type, env)
        else:
            self.bound1 = self.bound1.coerce_to_integer(env)
            self.bound2 = self.bound2.coerce_to_integer(env)
3036
        if self.step is not None:
3037 3038
            if isinstance(self.step, ExprNodes.UnaryMinusNode):
                warning(self.step.pos, "Probable infinite loop in for-from-by statment. Consider switching the directions of the relations.", 2)
3039 3040
            self.step.analyse_types(env)
            self.step = self.step.coerce_to_integer(env)
William Stein's avatar
William Stein committed
3041 3042 3043
        if not (self.bound2.is_name or self.bound2.is_literal):
            self.bound2 = self.bound2.coerce_to_temp(env)
        target_type = self.target.type
3044
        if not (target_type.is_pyobject or target_type.is_numeric):
3045 3046 3047 3048 3049 3050
            error(self.target.pos,
                "Integer for-loop variable must be of type int or Python object")
        #if not (target_type.is_pyobject
        #	or target_type.assignable_from(PyrexTypes.c_int_type)):
        #		error(self.target.pos,
        #			"Cannot assign integer to variable of type '%s'" % target_type)
3051
        if target_type.is_numeric:
3052
            self.is_py_target = 0
William Stein's avatar
William Stein committed
3053 3054 3055
            self.loopvar_name = self.target.entry.cname
            self.py_loopvar_node = None
        else:
3056
            self.is_py_target = 1
William Stein's avatar
William Stein committed
3057 3058 3059 3060 3061 3062 3063 3064
            c_loopvar_node = ExprNodes.TempNode(self.pos, 
                PyrexTypes.c_long_type, env)
            c_loopvar_node.allocate_temps(env)
            self.loopvar_name = c_loopvar_node.result_code
            self.py_loopvar_node = \
                ExprNodes.CloneNode(c_loopvar_node).coerce_to_pyobject(env)
        self.bound1.allocate_temps(env)
        self.bound2.allocate_temps(env)
3065 3066
        if self.step is not None:
            self.step.allocate_temps(env)
3067
        if self.is_py_target:
William Stein's avatar
William Stein committed
3068
            self.py_loopvar_node.allocate_temps(env)
3069 3070 3071
            self.target.allocate_target_temps(env, self.py_loopvar_node)
            #self.target.release_target_temp(env)
            #self.py_loopvar_node.release_temp(env)
William Stein's avatar
William Stein committed
3072
        self.body.analyse_expressions(env)
3073
        if self.is_py_target:
William Stein's avatar
William Stein committed
3074 3075 3076 3077 3078
            c_loopvar_node.release_temp(env)
        if self.else_clause:
            self.else_clause.analyse_expressions(env)
        self.bound1.release_temp(env)
        self.bound2.release_temp(env)
3079 3080
        if self.step is not None:
            self.step.release_temp(env)
William Stein's avatar
William Stein committed
3081 3082 3083 3084 3085 3086
            
    def generate_execution_code(self, code):
        old_loop_labels = code.new_loop_labels()
        self.bound1.generate_evaluation_code(code)
        self.bound2.generate_evaluation_code(code)
        offset, incop = self.relation_table[self.relation1]
3087 3088 3089
        if self.step is not None:
            self.step.generate_evaluation_code(code)
            incop = "%s=%s" % (incop[0], self.step.result_code)
William Stein's avatar
William Stein committed
3090 3091 3092 3093 3094
        code.putln(
            "for (%s = %s%s; %s %s %s; %s%s) {" % (
                self.loopvar_name,
                self.bound1.result_code, offset,
                self.loopvar_name, self.relation2, self.bound2.result_code,
3095
                self.loopvar_name, incop))
William Stein's avatar
William Stein committed
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110
        if self.py_loopvar_node:
            self.py_loopvar_node.generate_evaluation_code(code)
            self.target.generate_assignment_code(self.py_loopvar_node, code)
        self.body.generate_execution_code(code)
        code.put_label(code.continue_label)
        code.putln("}")
        break_label = code.break_label
        code.set_loop_labels(old_loop_labels)
        if self.else_clause:
            code.putln("/*else*/ {")
            self.else_clause.generate_execution_code(code)
            code.putln("}")
        code.put_label(break_label)
        self.bound1.generate_disposal_code(code)
        self.bound2.generate_disposal_code(code)
3111 3112
        if self.step is not None:
            self.step.generate_disposal_code(code)
William Stein's avatar
William Stein committed
3113 3114 3115 3116 3117 3118 3119 3120
    
    relation_table = {
        # {relop : (initial offset, increment op)}
        '<=': ("",   "++"),
        '<' : ("+1", "++"),
        '>=': ("",   "--"),
        '>' : ("-1", "--")
    }
3121 3122 3123 3124 3125 3126 3127 3128 3129 3130
    
    def annotate(self, code):
        self.target.annotate(code)
        self.bound1.annotate(code)
        self.bound2.annotate(code)
        if self.step:
            self.bound2.annotate(code)
        self.body.annotate(code)
        if self.else_clause:
            self.else_clause.annotate(code)
William Stein's avatar
William Stein committed
3131 3132 3133 3134 3135 3136 3137 3138 3139


class TryExceptStatNode(StatNode):
    #  try .. except statement
    #
    #  body             StatNode
    #  except_clauses   [ExceptClauseNode]
    #  else_clause      StatNode or None
    #  cleanup_list     [Entry]            temps to clean up on error
3140

3141
    child_attrs = ["body", "except_clauses", "else_clause"]
William Stein's avatar
William Stein committed
3142
    
3143 3144 3145
    def analyse_control_flow(self, env):
        env.start_branching(self.pos)
        self.body.analyse_control_flow(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
3146 3147 3148 3149 3150
        successful_try = env.control_flow # grab this for later
        env.next_branch(self.body.end_pos())
        env.finish_branching(self.body.end_pos())
        
        env.start_branching(self.except_clauses[0].pos)
3151 3152
        for except_clause in self.except_clauses:
            except_clause.analyse_control_flow(env)
Robert Bradshaw's avatar
Robert Bradshaw committed
3153 3154 3155 3156
            env.next_branch(except_clause.end_pos())
            
        # the else cause it executed only when the try clause finishes
        env.control_flow.incoming = successful_try
3157 3158 3159 3160
        if self.else_clause:
            self.else_clause.analyse_control_flow(env)
        env.finish_branching(self.end_pos())

William Stein's avatar
William Stein committed
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
    def analyse_declarations(self, env):
        self.body.analyse_declarations(env)
        for except_clause in self.except_clauses:
            except_clause.analyse_declarations(env)
        if self.else_clause:
            self.else_clause.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        self.body.analyse_expressions(env)
        self.cleanup_list = env.free_temp_entries[:]
        for except_clause in self.except_clauses:
            except_clause.analyse_expressions(env)
        if self.else_clause:
            self.else_clause.analyse_expressions(env)
    
    def generate_execution_code(self, code):
        old_error_label = code.new_error_label()
        our_error_label = code.error_label
        end_label = code.new_label()
        code.putln(
            "/*try:*/ {")
        self.body.generate_execution_code(code)
        code.putln(
            "}")
        code.error_label = old_error_label
        if self.else_clause:
            code.putln(
                "/*else:*/ {")
            self.else_clause.generate_execution_code(code)
            code.putln(
                "}")
3192
        code.put_goto(end_label)
William Stein's avatar
William Stein committed
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
        code.put_label(our_error_label)
        code.put_var_xdecrefs_clear(self.cleanup_list)
        default_clause_seen = 0
        for except_clause in self.except_clauses:
            if not except_clause.pattern:
                default_clause_seen = 1
            else:
                if default_clause_seen:
                    error(except_clause.pos, "Default except clause not last")
            except_clause.generate_handling_code(code, end_label)
        if not default_clause_seen:
3204
            code.put_goto(code.error_label)
William Stein's avatar
William Stein committed
3205 3206
        code.put_label(end_label)

3207 3208 3209 3210 3211 3212 3213
    def annotate(self, code):
        self.body.annotate(code)
        for except_node in self.except_clauses:
            except_node.annotate(code)
        if self.else_clause:
            self.else_clause.annotate(code)

William Stein's avatar
William Stein committed
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223

class ExceptClauseNode(Node):
    #  Part of try ... except statement.
    #
    #  pattern        ExprNode
    #  target         ExprNode or None
    #  body           StatNode
    #  match_flag     string             result of exception match
    #  exc_value      ExcValueNode       used internally
    #  function_name  string             qualified name of enclosing function
3224
    #  exc_vars       (string * 3)       local exception variables
William Stein's avatar
William Stein committed
3225
    
3226 3227
    child_attrs = ["pattern", "target", "body", "exc_value"]

William Stein's avatar
William Stein committed
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242
    def analyse_declarations(self, env):
        if self.target:
            self.target.analyse_target_declaration(env)
        self.body.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        import ExprNodes
        genv = env.global_scope()
        self.function_name = env.qualified_name
        if self.pattern:
            self.pattern.analyse_expressions(env)
            self.pattern = self.pattern.coerce_to_pyobject(env)
            self.match_flag = env.allocate_temp(PyrexTypes.c_int_type)
            self.pattern.release_temp(env)
            env.release_temp(self.match_flag)
3243
        self.exc_vars = [env.allocate_temp(py_object_type) for i in xrange(3)]
William Stein's avatar
William Stein committed
3244
        if self.target:
3245 3246
            self.exc_value = ExprNodes.ExcValueNode(self.pos, env, self.exc_vars[1])
            self.exc_value.allocate_temps(env)
3247
            self.target.analyse_target_expression(env, self.exc_value)
William Stein's avatar
William Stein committed
3248
        self.body.analyse_expressions(env)
3249 3250 3251
        for var in self.exc_vars:
            env.release_temp(var)
        env.use_utility_code(get_exception_utility_code)
William Stein's avatar
William Stein committed
3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
    
    def generate_handling_code(self, code, end_label):
        code.mark_pos(self.pos)
        if self.pattern:
            self.pattern.generate_evaluation_code(code)
            code.putln(
                "%s = PyErr_ExceptionMatches(%s);" % (
                    self.match_flag,
                    self.pattern.py_result()))
            self.pattern.generate_disposal_code(code)
            code.putln(
                "if (%s) {" %
                    self.match_flag)
        else:
            code.putln(
                "/*except:*/ {")
        code.putln(
            '__Pyx_AddTraceback("%s");' % (self.function_name))
        # We always have to fetch the exception value even if
        # there is no target, because this also normalises the 
        # exception and stores it in the thread state.
3273 3274 3275
        exc_args = "&%s, &%s, &%s" % tuple(self.exc_vars)
        code.putln("if (__Pyx_GetException(%s) < 0) %s" % (exc_args,
            code.error_goto(self.pos)))
William Stein's avatar
William Stein committed
3276
        if self.target:
3277
            self.exc_value.generate_evaluation_code(code)
William Stein's avatar
William Stein committed
3278
            self.target.generate_assignment_code(self.exc_value, code)
3279 3280
        old_exc_vars = code.exc_vars
        code.exc_vars = self.exc_vars
William Stein's avatar
William Stein committed
3281
        self.body.generate_execution_code(code)
3282 3283
        code.exc_vars = old_exc_vars
        for var in self.exc_vars:
3284
            code.putln("Py_DECREF(%s); %s = 0;" % (var, var))
3285
        code.put_goto(end_label)
William Stein's avatar
William Stein committed
3286 3287 3288
        code.putln(
            "}")

3289
    def annotate(self, code):
3290 3291
        if self.pattern:
            self.pattern.annotate(code)
3292 3293 3294 3295
        if self.target:
            self.target.annotate(code)
        self.body.annotate(code)

William Stein's avatar
William Stein committed
3296 3297 3298 3299 3300 3301

class TryFinallyStatNode(StatNode):
    #  try ... finally statement
    #
    #  body             StatNode
    #  finally_clause   StatNode
3302
    #
William Stein's avatar
William Stein committed
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
    #  cleanup_list     [Entry]      temps to clean up on error
    #
    #  The plan is that we funnel all continue, break
    #  return and error gotos into the beginning of the
    #  finally block, setting a variable to remember which
    #  one we're doing. At the end of the finally block, we
    #  switch on the variable to figure out where to go.
    #  In addition, if we're doing an error, we save the
    #  exception on entry to the finally block and restore
    #  it on exit.
3313

3314
    child_attrs = ["body", "finally_clause"]
William Stein's avatar
William Stein committed
3315
    
3316 3317
    preserve_exception = 1
    
William Stein's avatar
William Stein committed
3318 3319 3320 3321 3322
    disallow_continue_in_try_finally = 0
    # There doesn't seem to be any point in disallowing
    # continue in the try block, since we have no problem
    # handling it.
    
3323 3324 3325 3326
    def analyse_control_flow(self, env):
        env.start_branching(self.pos)
        self.body.analyse_control_flow(env)
        env.next_branch(self.body.end_pos())
Robert Bradshaw's avatar
Robert Bradshaw committed
3327
        env.finish_branching(self.body.end_pos())
3328 3329
        self.finally_clause.analyse_control_flow(env)

William Stein's avatar
William Stein committed
3330 3331 3332 3333 3334 3335 3336
    def analyse_declarations(self, env):
        self.body.analyse_declarations(env)
        self.finally_clause.analyse_declarations(env)
    
    def analyse_expressions(self, env):
        self.body.analyse_expressions(env)
        self.cleanup_list = env.free_temp_entries[:]
3337 3338 3339 3340 3341 3342
        #self.exc_vars = (
        #	env.allocate_temp(PyrexTypes.py_object_type),
        #	env.allocate_temp(PyrexTypes.py_object_type),
        #	env.allocate_temp(PyrexTypes.py_object_type))
        #self.lineno_var = \
        #	env.allocate_temp(PyrexTypes.c_int_type)
William Stein's avatar
William Stein committed
3343
        self.finally_clause.analyse_expressions(env)
3344 3345
        #for var in self.exc_vars:
        #	env.release_temp(var)
William Stein's avatar
William Stein committed
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
    
    def generate_execution_code(self, code):
        old_error_label = code.error_label
        old_labels = code.all_new_labels()
        new_labels = code.get_all_labels()
        new_error_label = code.error_label
        catch_label = code.new_label()
        code.putln(
            "/*try:*/ {")
        if self.disallow_continue_in_try_finally:
            was_in_try_finally = code.in_try_finally
            code.in_try_finally = 1
        self.body.generate_execution_code(code)
        if self.disallow_continue_in_try_finally:
            code.in_try_finally = was_in_try_finally
        code.putln(
            "}")
        code.putln(
            "/*finally:*/ {")
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
        cases_used = []
        error_label_used = 0
        for i, new_label in enumerate(new_labels):
            if new_label in code.labels_used:
                cases_used.append(i)
                if new_label == new_error_label:
                    error_label_used = 1
                    error_label_case = i
        if cases_used:
            code.putln(
                    "int __pyx_why;")
            if error_label_used and self.preserve_exception:
                code.putln(
                    "PyObject *%s, *%s, *%s;" % Naming.exc_vars)
                code.putln(
                    "int %s;" % Naming.exc_lineno_name)
            code.use_label(catch_label)
            code.putln(
                    "__pyx_why = 0; goto %s;" % catch_label)
            for i in cases_used:
                new_label = new_labels[i]
Stefan Behnel's avatar
Stefan Behnel committed
3386
                #if new_label and new_label != "<try>":
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396
                if new_label == new_error_label and self.preserve_exception:
                    self.put_error_catcher(code, 
                        new_error_label, i+1, catch_label)
                else:
                    code.putln(
                        "%s: __pyx_why = %s; goto %s;" % (
                            new_label,
                            i+1,
                            catch_label))
            code.put_label(catch_label)
William Stein's avatar
William Stein committed
3397
        code.set_all_labels(old_labels)
3398 3399 3400
        if error_label_used:
            code.new_error_label()
            finally_error_label = code.error_label
William Stein's avatar
William Stein committed
3401
        self.finally_clause.generate_execution_code(code)
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
        if error_label_used:
            if finally_error_label in code.labels_used and self.preserve_exception:
                over_label = code.new_label()
                code.put_goto(over_label);
                code.put_label(finally_error_label)
                code.putln("if (__pyx_why == %d) {" % (error_label_case + 1))
                for var in Naming.exc_vars:
                    code.putln("Py_XDECREF(%s);" % var)
                code.putln("}")
                code.put_goto(old_error_label)
                code.put_label(over_label)
            code.error_label = old_error_label
        if cases_used:
            code.putln(
William Stein's avatar
William Stein committed
3416
                "switch (__pyx_why) {")
3417 3418 3419
            for i in cases_used:
                old_label = old_labels[i]
                if old_label == old_error_label and self.preserve_exception:
William Stein's avatar
William Stein committed
3420 3421
                    self.put_error_uncatcher(code, i+1, old_error_label)
                else:
3422
                    code.use_label(old_label)
William Stein's avatar
William Stein committed
3423 3424 3425
                    code.putln(
                        "case %s: goto %s;" % (
                            i+1,
3426 3427
                            old_label))
            code.putln(
William Stein's avatar
William Stein committed
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441
                "}")		
        code.putln(
            "}")

    def put_error_catcher(self, code, error_label, i, catch_label):
        code.putln(
            "%s: {" %
                error_label)
        code.putln(
                "__pyx_why = %s;" %
                    i)
        code.put_var_xdecrefs_clear(self.cleanup_list)
        code.putln(
                "PyErr_Fetch(&%s, &%s, &%s);" %
3442
                    Naming.exc_vars)
William Stein's avatar
William Stein committed
3443 3444
        code.putln(
                "%s = %s;" % (
3445
                    Naming.exc_lineno_name, Naming.lineno_cname))
3446 3447 3448 3449
        #code.putln(
        #		"goto %s;" %
        #			catch_label)
        code.put_goto(catch_label)
William Stein's avatar
William Stein committed
3450 3451 3452 3453 3454 3455 3456 3457 3458
        code.putln(
            "}")
            
    def put_error_uncatcher(self, code, i, error_label):
        code.putln(
            "case %s: {" %
                i)
        code.putln(
                "PyErr_Restore(%s, %s, %s);" %
3459
                    Naming.exc_vars)
William Stein's avatar
William Stein committed
3460 3461
        code.putln(
                "%s = %s;" % (
3462 3463
                    Naming.lineno_cname, Naming.exc_lineno_name))
        for var in Naming.exc_vars:
William Stein's avatar
William Stein committed
3464 3465 3466
            code.putln(
                "%s = 0;" %
                    var)
3467
        code.put_goto(error_label)
William Stein's avatar
William Stein committed
3468 3469 3470
        code.putln(
            "}")

3471 3472 3473 3474
    def annotate(self, code):
        self.body.annotate(code)
        self.finally_clause.annotate(code)

William Stein's avatar
William Stein committed
3475

3476 3477 3478 3479 3480
class GILStatNode(TryFinallyStatNode):
    #  'with gil' or 'with nogil' statement
    #
    #   state   string   'gil' or 'nogil'
        
3481 3482
    child_attrs = []
    
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519
    preserve_exception = 0

    def __init__(self, pos, state, body):
        self.state = state
        TryFinallyStatNode.__init__(self, pos,
            body = body,
            finally_clause = GILExitNode(pos, state = state))

    def generate_execution_code(self, code):
        code.putln("/*with %s:*/ {" % self.state)
        if self.state == 'gil':
            code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
        else:
            code.putln("PyThreadState *_save;")
            code.putln("Py_UNBLOCK_THREADS")
        TryFinallyStatNode.generate_execution_code(self, code)
        code.putln("}")

#class GILEntryNode(StatNode):
#	#  state   string   'gil' or 'nogil'
#
#	def analyse_expressions(self, env):
#		pass
#
#	def generate_execution_code(self, code):
#		if self.state == 'gil':
#			code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
#		else:
#			code.putln("PyThreadState *_save;")
#			code.putln("Py_UNBLOCK_THREADS")


class GILExitNode(StatNode):
    #  Used as the 'finally' block in a GILStatNode
    #
    #  state   string   'gil' or 'nogil'

3520 3521
    child_attrs = []

3522 3523 3524 3525 3526 3527 3528 3529 3530 3531
    def analyse_expressions(self, env):
        pass

    def generate_execution_code(self, code):
        if self.state == 'gil':
            code.putln("PyGILState_Release();")
        else:
            code.putln("Py_BLOCK_THREADS")


William Stein's avatar
William Stein committed
3532 3533 3534 3535 3536
class CImportStatNode(StatNode):
    #  cimport statement
    #
    #  module_name   string           Qualified name of module being imported
    #  as_name       string or None   Name specified in "as" clause, if any
3537 3538

    child_attrs = []
William Stein's avatar
William Stein committed
3539 3540
    
    def analyse_declarations(self, env):
3541 3542 3543
        if not env.is_module_scope:
            error(self.pos, "cimport only allowed at module level")
            return
William Stein's avatar
William Stein committed
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
        module_scope = env.find_module(self.module_name, self.pos)
        if "." in self.module_name:
            names = self.module_name.split(".")
            top_name = names[0]
            top_module_scope = env.context.find_submodule(top_name)
            module_scope = top_module_scope
            for name in names[1:]:
                submodule_scope = module_scope.find_submodule(name)
                module_scope.declare_module(name, submodule_scope, self.pos)
                module_scope = submodule_scope
            if self.as_name:
                env.declare_module(self.as_name, module_scope, self.pos)
            else:
                env.declare_module(top_name, top_module_scope, self.pos)
        else:
            name = self.as_name or self.module_name
            env.declare_module(name, module_scope, self.pos)

    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass
    

class FromCImportStatNode(StatNode):
    #  from ... cimport statement
    #
    #  module_name     string                  Qualified name of module
    #  imported_names  [(pos, name, as_name)]  Names to be imported
    
3575 3576
    child_attrs = []

William Stein's avatar
William Stein committed
3577
    def analyse_declarations(self, env):
3578 3579 3580
        if not env.is_module_scope:
            error(self.pos, "cimport only allowed at module level")
            return
William Stein's avatar
William Stein committed
3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
        module_scope = env.find_module(self.module_name, self.pos)
        env.add_imported_module(module_scope)
        for pos, name, as_name in self.imported_names:
            entry = module_scope.find(name, pos)
            if entry:
                local_name = as_name or name
                env.add_imported_entry(local_name, entry, pos)

    def analyse_expressions(self, env):
        pass
    
    def generate_execution_code(self, code):
        pass


class FromImportStatNode(StatNode):
    #  from ... import statement
    #
    #  module           ImportNode
    #  items            [(string, NameNode)]
    #  interned_items   [(string, NameNode)]
    #  item             PyTempNode            used internally
3603 3604

    child_attrs = ["module"]
William Stein's avatar
William Stein committed
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
    
    def analyse_declarations(self, env):
        for _, target in self.items:
            target.analyse_target_declaration(env)
    
    def analyse_expressions(self, env):
        import ExprNodes
        self.module.analyse_expressions(env)
        self.item = ExprNodes.PyTempNode(self.pos, env)
        self.item.allocate_temp(env)
        self.interned_items = []
        for name, target in self.items:
            if Options.intern_names:
                self.interned_items.append((env.intern(name), target))
3619 3620
            target.analyse_target_expression(env, None)
            #target.release_target_temp(env) # was release_temp ?!?
William Stein's avatar
William Stein committed
3621 3622 3623 3624 3625 3626 3627 3628
        self.module.release_temp(env)
        self.item.release_temp(env)
    
    def generate_execution_code(self, code):
        self.module.generate_evaluation_code(code)
        if Options.intern_names:
            for cname, target in self.interned_items:
                code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
3629
                    '%s = PyObject_GetAttr(%s, %s); %s' % (
William Stein's avatar
William Stein committed
3630 3631 3632
                        self.item.result_code, 
                        self.module.py_result(),
                        cname,
Robert Bradshaw's avatar
Robert Bradshaw committed
3633
                        code.error_goto_if_null(self.item.result_code, self.pos)))
William Stein's avatar
William Stein committed
3634 3635 3636 3637
                target.generate_assignment_code(self.item, code)
        else:
            for name, target in self.items:
                code.putln(
Robert Bradshaw's avatar
Robert Bradshaw committed
3638
                    '%s = PyObject_GetAttrString(%s, "%s"); %s' % (
William Stein's avatar
William Stein committed
3639 3640 3641
                        self.item.result_code, 
                        self.module.py_result(),
                        name,
Robert Bradshaw's avatar
Robert Bradshaw committed
3642
                        code.error_goto_if_null(self.item.result_code, self.pos)))
William Stein's avatar
William Stein committed
3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
                target.generate_assignment_code(self.item, code)
        self.module.generate_disposal_code(code)

#------------------------------------------------------------------------------------
#
#  Runtime support code
#
#------------------------------------------------------------------------------------

utility_function_predeclarations = \
"""
3654 3655 3656
#ifdef __GNUC__
#define INLINE __inline__
#elif _WIN32
3657
#define INLINE __inline
3658 3659 3660 3661
#else
#define INLINE 
#endif

William Stein's avatar
William Stein committed
3662
typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/
3663
typedef struct {PyObject **p; char *s; long n; int is_unicode;} __Pyx_StringTabEntry; /*proto*/
3664

3665 3666 3667 3668 3669
""" + """

static int %(skip_dispatch_cname)s = 0;

""" % { 'skip_dispatch_cname': Naming.skip_dispatch_cname }
Robert Bradshaw's avatar
Robert Bradshaw committed
3670 3671 3672 3673

if Options.gcc_branch_hints:
    branch_prediction_macros = \
    """
3674
#ifdef __GNUC__
3675 3676 3677
/* Test for GCC > 2.95 */
#if __GNUC__ > 2 || \
              (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) 
Robert Bradshaw's avatar
Robert Bradshaw committed
3678 3679
#define likely(x)   __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
3680 3681 3682 3683
#else /* __GNUC__ > 2 ... */
#define likely(x)   (x)
#define unlikely(x) (x)
#endif /* __GNUC__ > 2 ... */
3684 3685 3686 3687
#else /* __GNUC__ */
#define likely(x)   (x)
#define unlikely(x) (x)
#endif /* __GNUC__ */
Robert Bradshaw's avatar
Robert Bradshaw committed
3688 3689 3690 3691 3692 3693 3694
    """
else:
    branch_prediction_macros = \
    """
#define likely(x)   (x)
#define unlikely(x) (x)
    """
William Stein's avatar
William Stein committed
3695

3696 3697
#get_name_predeclaration = \
#"static PyObject *__Pyx_GetName(PyObject *dict, char *name); /*proto*/"
William Stein's avatar
William Stein committed
3698

3699 3700
#get_name_interned_predeclaration = \
#"static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/"
William Stein's avatar
William Stein committed
3701 3702 3703

#------------------------------------------------------------------------------------

3704 3705 3706 3707 3708
printing_utility_code = [
"""
static int __Pyx_PrintItem(PyObject *); /*proto*/
static int __Pyx_PrintNewline(void); /*proto*/
""",r"""
William Stein's avatar
William Stein committed
3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729
static PyObject *__Pyx_GetStdout(void) {
    PyObject *f = PySys_GetObject("stdout");
    if (!f) {
        PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
    }
    return f;
}

static int __Pyx_PrintItem(PyObject *v) {
    PyObject *f;
    
    if (!(f = __Pyx_GetStdout()))
        return -1;
    if (PyFile_SoftSpace(f, 1)) {
        if (PyFile_WriteString(" ", f) < 0)
            return -1;
    }
    if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
        return -1;
    if (PyString_Check(v)) {
        char *s = PyString_AsString(v);
3730
        Py_ssize_t len = PyString_Size(v);
William Stein's avatar
William Stein committed
3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
        if (len > 0 &&
            isspace(Py_CHARMASK(s[len-1])) &&
            s[len-1] != ' ')
                PyFile_SoftSpace(f, 0);
    }
    return 0;
}

static int __Pyx_PrintNewline(void) {
    PyObject *f;
    
    if (!(f = __Pyx_GetStdout()))
        return -1;
    if (PyFile_WriteString("\n", f) < 0)
        return -1;
    PyFile_SoftSpace(f, 0);
    return 0;
}
3749
"""]
William Stein's avatar
William Stein committed
3750 3751 3752 3753 3754

#------------------------------------------------------------------------------------

# The following function is based on do_raise() from ceval.c.

3755
raise_utility_code = [
William Stein's avatar
William Stein committed
3756
"""
3757 3758
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
""","""
William Stein's avatar
William Stein committed
3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
    Py_XINCREF(type);
    Py_XINCREF(value);
    Py_XINCREF(tb);
    /* First, check the traceback argument, replacing None with NULL. */
    if (tb == Py_None) {
        Py_DECREF(tb);
        tb = 0;
    }
    else if (tb != NULL && !PyTraceBack_Check(tb)) {
        PyErr_SetString(PyExc_TypeError,
            "raise: arg 3 must be a traceback or None");
        goto raise_error;
    }
    /* Next, replace a missing value with None */
    if (value == NULL) {
        value = Py_None;
        Py_INCREF(value);
    }
3778 3779 3780 3781 3782 3783
    #if PY_VERSION_HEX < 0x02050000
    if (!PyClass_Check(type))
    #else
    if (!PyType_Check(type))
    #endif
    {
William Stein's avatar
William Stein committed
3784 3785 3786
        /* Raising an instance.  The value should be a dummy. */
        if (value != Py_None) {
            PyErr_SetString(PyExc_TypeError,
3787
                "instance exception may not have a separate value");
William Stein's avatar
William Stein committed
3788 3789
            goto raise_error;
        }
3790 3791 3792
        /* Normalize to raise <class>, <instance> */
        Py_DECREF(value);
        value = type;
3793 3794 3795 3796 3797 3798
        #if PY_VERSION_HEX < 0x02050000
            if (PyInstance_Check(type)) {
                type = (PyObject*) ((PyInstanceObject*)type)->in_class;
                Py_INCREF(type);
            }
            else {
3799
                type = 0;
3800 3801 3802 3803 3804
                PyErr_SetString(PyExc_TypeError,
                    "raise: exception must be an old-style class or instance");
                goto raise_error;
            }
        #else
3805
            type = (PyObject*) type->ob_type;
3806 3807 3808 3809 3810 3811 3812
            Py_INCREF(type);
            if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
                PyErr_SetString(PyExc_TypeError,
                    "raise: exception class must be a subclass of BaseException");
                goto raise_error;
            }
        #endif
William Stein's avatar
William Stein committed
3813
    }
3814 3815 3816 3817
    PyErr_Restore(type, value, tb);
    return;
raise_error:
    Py_XDECREF(value);
William Stein's avatar
William Stein committed
3818 3819 3820 3821
    Py_XDECREF(type);
    Py_XDECREF(tb);
    return;
}
3822
"""]
William Stein's avatar
William Stein committed
3823 3824 3825

#------------------------------------------------------------------------------------

3826
reraise_utility_code = [
William Stein's avatar
William Stein committed
3827
"""
3828 3829
static void __Pyx_ReRaise(void); /*proto*/
""","""
William Stein's avatar
William Stein committed
3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
static void __Pyx_ReRaise(void) {
    PyThreadState *tstate = PyThreadState_Get();
    PyObject *type = tstate->exc_type;
    PyObject *value = tstate->exc_value;
    PyObject *tb = tstate->exc_traceback;
    Py_XINCREF(type);
    Py_XINCREF(value);
    Py_XINCREF(tb);
    PyErr_Restore(type, value, tb);
}
3840
"""]
William Stein's avatar
William Stein committed
3841 3842 3843

#------------------------------------------------------------------------------------

3844
arg_type_test_utility_code = [
William Stein's avatar
William Stein committed
3845
"""
3846 3847
static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name); /*proto*/
""","""
William Stein's avatar
William Stein committed
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859
static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name) {
    if (!type) {
        PyErr_Format(PyExc_SystemError, "Missing type object");
        return 0;
    }
    if ((none_allowed && obj == Py_None) || PyObject_TypeCheck(obj, type))
        return 1;
    PyErr_Format(PyExc_TypeError,
        "Argument '%s' has incorrect type (expected %s, got %s)",
        name, type->tp_name, obj->ob_type->tp_name);
    return 0;
}
3860
"""]
William Stein's avatar
William Stein committed
3861 3862 3863

#------------------------------------------------------------------------------------
#
3864
#  __Pyx_SplitStarArg splits the args tuple into two parts, one part
3865 3866 3867 3868 3869
#  suitable for passing to PyArg_ParseTupleAndKeywords, and the other
#  containing any extra arguments. On success, replaces the borrowed
#  reference *args with references to a new tuple, and passes back a
#  new reference in *args2.  Does not touch any of its arguments on
#  failure.
William Stein's avatar
William Stein committed
3870

3871
get_stararg_utility_code = [
William Stein's avatar
William Stein committed
3872
"""
3873
static INLINE int __Pyx_SplitStarArg(PyObject **args, Py_ssize_t nargs, PyObject **args2); /*proto*/
3874
""","""
3875
static INLINE int __Pyx_SplitStarArg(
William Stein's avatar
William Stein committed
3876
    PyObject **args, 
3877
    Py_ssize_t nargs,
3878
    PyObject **args2)
William Stein's avatar
William Stein committed
3879
{
3880
    PyObject *args1 = 0;
3881
    args1 = PyTuple_GetSlice(*args, 0, nargs);
3882 3883
    if (!args1) {
        *args2 = 0;
3884
        return -1;
3885
    }
3886
    *args2 = PyTuple_GetSlice(*args, nargs, PyTuple_GET_SIZE(*args));
3887 3888 3889 3890
    if (!*args2) {
        Py_DECREF(args1);
        return -1;
    }
3891 3892 3893 3894 3895
    *args = args1;
    return 0;
}
"""]

3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919
#------------------------------------------------------------------------------------
#
#  __Pyx_RaiseArgtupleTooLong raises the correct exception when too
#  many positional arguments were found.  This handles Py_ssize_t
#  formatting correctly.

raise_argtuple_too_long_utility_code = [
"""
static INLINE void __Pyx_RaiseArgtupleTooLong(Py_ssize_t num_expected, Py_ssize_t num_found); /*proto*/
""","""
static INLINE void __Pyx_RaiseArgtupleTooLong(
    Py_ssize_t num_expected,
    Py_ssize_t num_found)
{
    const char* error_message =
    #if PY_VERSION_HEX < 0x02050000
        "function takes at most %d positional arguments (%d given)";
    #else
        "function takes at most %zd positional arguments (%zd given)";
    #endif
    PyErr_Format(PyExc_TypeError, error_message, num_expected, num_found);
}
"""]

3920 3921
#------------------------------------------------------------------------------------
#
3922 3923 3924
#  __Pyx_CheckKeywordStrings raises an error if non-string keywords
#  were passed to a function, or if any keywords were passed to a
#  function that does not accept them.
3925

3926
get_keyword_string_check_utility_code = [
3927
"""
3928
static int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); /*proto*/
3929
""","""
3930 3931 3932 3933 3934
static int __Pyx_CheckKeywordStrings(
    PyObject *kwdict,
    const char* function_name,
    int kw_allowed)
{
3935 3936
    PyObject* key = 0;
    Py_ssize_t pos = 0;
3937 3938 3939 3940 3941 3942
    while (PyDict_Next(kwdict, &pos, &key, 0)) {
        if (unlikely(!PyString_Check(key))) {
            PyErr_Format(PyExc_TypeError,
                         "%s() keywords must be strings", function_name);
            return 0;
        }
3943
    }
3944
    if (unlikely(!kw_allowed) && unlikely(key)) {
3945 3946 3947
        PyErr_Format(PyExc_TypeError,
                     "'%s' is an invalid keyword argument for this function",
                     PyString_AsString(key));
3948
        return 0;
3949
    }
3950
    return 1;
3951 3952 3953
}
"""]

3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986
#------------------------------------------------------------------------------------
#
#  __Pyx_SplitKeywords splits the kwds dict into two parts one part
#  suitable for passing to PyArg_ParseTupleAndKeywords, and the other
#  containing any extra arguments. On success, replaces the borrowed
#  reference *kwds with references to a new dict, and passes back a
#  new reference in *kwds2.  Does not touch any of its arguments on
#  failure.
#
#  Any of *kwds and kwds2 may be 0 (but not kwds). If *kwds == 0, it
#  is not changed. If kwds2 == 0 and *kwds != 0, a new reference to
#  the same dictionary is passed back in *kwds.
#
#  If rqd_kwds is not 0, it is an array of booleans corresponding to
#  the names in kwd_list, indicating required keyword arguments. If
#  any of these are not present in kwds, an exception is raised.
#

get_splitkeywords_utility_code = [
"""
static int __Pyx_SplitKeywords(PyObject **kwds, char *kwd_list[], \
    PyObject **kwds2, char rqd_kwds[]); /*proto*/
""","""
static int __Pyx_SplitKeywords(
    PyObject **kwds,
    char *kwd_list[], 
    PyObject **kwds2,
    char rqd_kwds[])
{
    PyObject *s = 0, *x = 0, *kwds1 = 0;
    int i;
    char **p;
    
3987
    if (*kwds) {
3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001
        kwds1 = PyDict_New();
        if (!kwds1)
            goto bad;
        *kwds2 = PyDict_Copy(*kwds);
        if (!*kwds2)
            goto bad;
        for (i = 0, p = kwd_list; *p; i++, p++) {
            s = PyString_FromString(*p);
            x = PyDict_GetItem(*kwds, s);
            if (x) {
                if (PyDict_SetItem(kwds1, s, x) < 0)
                    goto bad;
                if (PyDict_DelItem(*kwds2, s) < 0)
                    goto bad;
4002
            }
4003 4004 4005
            else if (rqd_kwds && rqd_kwds[i])
                goto missing_kwarg;
            Py_DECREF(s);
William Stein's avatar
William Stein committed
4006
        }
4007
        s = 0;
William Stein's avatar
William Stein committed
4008 4009
    }
    else {
4010
        if (rqd_kwds) {
4011
            for (i = 0, p = kwd_list; *p; i++, p++)
4012 4013 4014
                if (rqd_kwds[i])
                    goto missing_kwarg;
        }
4015 4016 4017
        *kwds2 = PyDict_New();
        if (!*kwds2)
            goto bad;
William Stein's avatar
William Stein committed
4018
    }
4019

William Stein's avatar
William Stein committed
4020 4021
    *kwds = kwds1;
    return 0;
4022 4023 4024
missing_kwarg:
    PyErr_Format(PyExc_TypeError,
        "required keyword argument '%s' is missing", *p);
William Stein's avatar
William Stein committed
4025
bad:
4026
    Py_XDECREF(s);
William Stein's avatar
William Stein committed
4027
    Py_XDECREF(kwds1);
4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
    Py_XDECREF(*kwds2);
    return -1;
}
"""]

get_checkkeywords_utility_code = [
"""
static INLINE int __Pyx_CheckRequiredKeywords(PyObject *kwds, char *kwd_list[],
    char rqd_kwds[]); /*proto*/
""","""
static INLINE int __Pyx_CheckRequiredKeywords(
    PyObject *kwds,
    char *kwd_list[],
    char rqd_kwds[])
{
    int i;
    char **p;

    if (kwds) {
        for (i = 0, p = kwd_list; *p; i++, p++)
            if (rqd_kwds[i] && !PyDict_GetItemString(kwds, *p))
                goto missing_kwarg;
    }
    else {
        for (i = 0, p = kwd_list; *p; i++, p++)
            if (rqd_kwds[i])
                goto missing_kwarg;
4055
    }
4056 4057 4058 4059 4060

    return 0;
missing_kwarg:
    PyErr_Format(PyExc_TypeError,
        "required keyword argument '%s' is missing", *p);
William Stein's avatar
William Stein committed
4061 4062
    return -1;
}
4063
"""]
William Stein's avatar
William Stein committed
4064 4065 4066

#------------------------------------------------------------------------------------

4067
unraisable_exception_utility_code = [
William Stein's avatar
William Stein committed
4068
"""
4069 4070
static void __Pyx_WriteUnraisable(char *name); /*proto*/
""","""
William Stein's avatar
William Stein committed
4071 4072 4073 4074 4075 4076 4077 4078 4079
static void __Pyx_WriteUnraisable(char *name) {
    PyObject *old_exc, *old_val, *old_tb;
    PyObject *ctx;
    PyErr_Fetch(&old_exc, &old_val, &old_tb);
    ctx = PyString_FromString(name);
    PyErr_Restore(old_exc, old_val, old_tb);
    if (!ctx)
        ctx = Py_None;
    PyErr_WriteUnraisable(ctx);
4080 4081
}
"""]
William Stein's avatar
William Stein committed
4082 4083 4084

#------------------------------------------------------------------------------------

4085
traceback_utility_code = [
William Stein's avatar
William Stein committed
4086
"""
4087 4088
static void __Pyx_AddTraceback(char *funcname); /*proto*/
""","""
William Stein's avatar
William Stein committed
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
#include "compile.h"
#include "frameobject.h"
#include "traceback.h"

static void __Pyx_AddTraceback(char *funcname) {
    PyObject *py_srcfile = 0;
    PyObject *py_funcname = 0;
    PyObject *py_globals = 0;
    PyObject *empty_string = 0;
    PyCodeObject *py_code = 0;
    PyFrameObject *py_frame = 0;
    
    py_srcfile = PyString_FromString(%(FILENAME)s);
    if (!py_srcfile) goto bad;
    py_funcname = PyString_FromString(funcname);
    if (!py_funcname) goto bad;
    py_globals = PyModule_GetDict(%(GLOBALS)s);
    if (!py_globals) goto bad;
    empty_string = PyString_FromString("");
    if (!empty_string) goto bad;
    py_code = PyCode_New(
        0,            /*int argcount,*/
        0,            /*int nlocals,*/
        0,            /*int stacksize,*/
        0,            /*int flags,*/
        empty_string, /*PyObject *code,*/
Stefan Behnel's avatar
Stefan Behnel committed
4115 4116 4117 4118 4119
        %(EMPTY_TUPLE)s,  /*PyObject *consts,*/
        %(EMPTY_TUPLE)s,  /*PyObject *names,*/
        %(EMPTY_TUPLE)s,  /*PyObject *varnames,*/
        %(EMPTY_TUPLE)s,  /*PyObject *freevars,*/
        %(EMPTY_TUPLE)s,  /*PyObject *cellvars,*/
William Stein's avatar
William Stein committed
4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144
        py_srcfile,   /*PyObject *filename,*/
        py_funcname,  /*PyObject *name,*/
        %(LINENO)s,   /*int firstlineno,*/
        empty_string  /*PyObject *lnotab*/
    );
    if (!py_code) goto bad;
    py_frame = PyFrame_New(
        PyThreadState_Get(), /*PyThreadState *tstate,*/
        py_code,             /*PyCodeObject *code,*/
        py_globals,          /*PyObject *globals,*/
        0                    /*PyObject *locals*/
    );
    if (!py_frame) goto bad;
    py_frame->f_lineno = %(LINENO)s;
    PyTraceBack_Here(py_frame);
bad:
    Py_XDECREF(py_srcfile);
    Py_XDECREF(py_funcname);
    Py_XDECREF(empty_string);
    Py_XDECREF(py_code);
    Py_XDECREF(py_frame);
}
""" % {
    'FILENAME': Naming.filename_cname,
    'LINENO':  Naming.lineno_cname,
Stefan Behnel's avatar
Stefan Behnel committed
4145 4146
    'GLOBALS': Naming.module_cname,
    'EMPTY_TUPLE' : Naming.empty_tuple,
4147
}]
William Stein's avatar
William Stein committed
4148 4149 4150

#------------------------------------------------------------------------------------

4151
set_vtable_utility_code = [
William Stein's avatar
William Stein committed
4152
"""
4153 4154
static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
""","""
William Stein's avatar
William Stein committed
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
    PyObject *pycobj = 0;
    int result;
    
    pycobj = PyCObject_FromVoidPtr(vtable, 0);
    if (!pycobj)
        goto bad;
    if (PyDict_SetItemString(dict, "__pyx_vtable__", pycobj) < 0)
        goto bad;
    result = 0;
    goto done;

bad:
    result = -1;
done:
    Py_XDECREF(pycobj);
    return result;
}
4173
"""]
William Stein's avatar
William Stein committed
4174 4175 4176

#------------------------------------------------------------------------------------

4177 4178 4179 4180
get_vtable_utility_code = [
"""
static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/
""",r"""
William Stein's avatar
William Stein committed
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199
static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) {
    int result;
    PyObject *pycobj;
    
    pycobj = PyMapping_GetItemString(dict, "__pyx_vtable__");
    if (!pycobj)
        goto bad;
    *(void **)vtabptr = PyCObject_AsVoidPtr(pycobj);
    if (!*(void **)vtabptr)
        goto bad;
    result = 0;
    goto done;

bad:
    result = -1;
done:
    Py_XDECREF(pycobj);
    return result;
}
4200
"""]
William Stein's avatar
William Stein committed
4201 4202 4203

#------------------------------------------------------------------------------------

4204
init_intern_tab_utility_code = [
William Stein's avatar
William Stein committed
4205
"""
4206 4207
static int __Pyx_InternStrings(__Pyx_InternTabEntry *t); /*proto*/
""","""
William Stein's avatar
William Stein committed
4208 4209 4210 4211 4212 4213 4214 4215 4216
static int __Pyx_InternStrings(__Pyx_InternTabEntry *t) {
    while (t->p) {
        *t->p = PyString_InternFromString(t->s);
        if (!*t->p)
            return -1;
        ++t;
    }
    return 0;
}
4217
"""]
William Stein's avatar
William Stein committed
4218 4219 4220

#------------------------------------------------------------------------------------

4221
init_string_tab_utility_code = [
William Stein's avatar
William Stein committed
4222
"""
4223 4224
static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
""","""
William Stein's avatar
William Stein committed
4225 4226
static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
    while (t->p) {
4227 4228 4229 4230 4231
        if (t->is_unicode) {
            *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);
        } else {
            *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
        }
William Stein's avatar
William Stein committed
4232 4233 4234 4235 4236 4237
        if (!*t->p)
            return -1;
        ++t;
    }
    return 0;
}
4238
"""]
William Stein's avatar
William Stein committed
4239 4240

#------------------------------------------------------------------------------------
4241

4242
get_exception_utility_code = [
4243
"""
4244
static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
4245
""","""
4246
static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) {
4247
    PyObject *tmp_type, *tmp_value, *tmp_tb;
4248 4249 4250 4251 4252 4253 4254 4255
    PyThreadState *tstate = PyThreadState_Get();
    PyErr_Fetch(type, value, tb);
    PyErr_NormalizeException(type, value, tb);
    if (PyErr_Occurred())
        goto bad;
    Py_INCREF(*type);
    Py_INCREF(*value);
    Py_INCREF(*tb);
4256 4257 4258
    tmp_type = tstate->exc_type;
    tmp_value = tstate->exc_value;
    tmp_tb = tstate->exc_traceback;
4259 4260 4261
    tstate->exc_type = *type;
    tstate->exc_value = *value;
    tstate->exc_traceback = *tb;
4262 4263 4264 4265 4266
    /* 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);
4267
    return 0;
4268 4269 4270 4271 4272
bad:
    Py_XDECREF(*type);
    Py_XDECREF(*value);
    Py_XDECREF(*tb);
    return -1;
4273
}
4274
"""]
4275 4276

#------------------------------------------------------------------------------------