Exemplo n.º 1
0
    def visitFunCall(self, node):
        self.visit_children(node)
        funty = node.definition._type

        if not isinstance(funty, FunType):
            raise NodeError(node, 'Error: %s is not a function' % node.name)

        typed_args = node.args
        nargs = len(node.args)
        nparams = len(funty.params)
        defargs = (node.definition.location, 'Note: function is defined here')

        # number of arguments must match number of parameters in definition
        if funty.varargs:
            if nargs < nparams:
                raise BackrefError(
                    node.location, 'Not enough arguments for varargs function',
                    *defargs)

            typed_args = node.args[:len(funty.params)]

        elif nargs != nparams:
            errnode = node.args[nparams] if nargs > nparams else node
            raise BackrefError(
                errnode.location,
                'Expected %d arguments, got %d' % (nparams, nargs), *defargs)

        # argument types must match defined parameter types
        for arg, param in zip(typed_args, funty.params):
            self.check_type(arg, param._type)

        # resulting expression has return type of function
        node.ty = funty.return_type
Exemplo n.º 2
0
    def add_to_scope(self, node):
        curscope = self.scopes[-1]

        if node.name in curscope:
            raise BackrefError(
                    node.location, 'Error: redefinition of %s' % node.name,
                    curscope[node.name].location, 'Note: defined earlier here')

        curscope[node.name] = node
Exemplo n.º 3
0
    def visitVarUse(self, node):
        # variable uses inherit the type of their declaration
        ty = node.definition._type

        # indexed arrays produce the array base type
        if node.index:
            self.visit_children(node)

            if not ty.is_array():
                raise BackrefError(node.location,
                                   'Error: cannot index non-array variable',
                                   node.definition.location,
                                   'Note: variable is defined here')

            ty = ty.base

        node.ty = ty