예제 #1
0
def convertFunctionCallToOutline(provider, function_ref, values):
    # This has got to have pretty man details, pylint: disable=R0914

    function_body = function_ref.getFunctionBody()

    # TODO: Use the call location
    source_ref = function_body.getSourceReference()

    outline_body = ExpressionOutlineBody(provider=provider,
                                         name="inline",
                                         body=None,
                                         source_ref=source_ref)

    clone = function_body.getBody().makeClone()

    temp_scope = outline_body.getOutlineTempScope()

    translation = {}

    for variable in function_body.getLocalVariables():
        # TODO: Later we should be able to do that too.
        assert not variable.isSharedTechnically()

        new_variable = outline_body.allocateTempVariable(
            temp_scope=temp_scope, name=variable.getName())

        # TODO: Lets update all at once maybe, it would take less visits.
        updateVariableUsage(clone,
                            old_variable=variable,
                            new_variable=new_variable)

        translation[variable.getName()] = new_variable

    statements = []

    argument_names = function_body.getParameters().getAllNames()

    assert len(argument_names) == len(values), (argument_names, values)

    for argument_name, value in zip(argument_names, values):
        statements.append(
            StatementAssignmentVariable(
                variable_ref=makeVariableTargetRefNode(
                    variable=translation[argument_name],
                    source_ref=source_ref),
                source=value,
                source_ref=source_ref,
            ))

    body = makeStatementsSequence(statements=(statements, clone),
                                  allow_none=False,
                                  source_ref=source_ref)
    outline_body.setBody(body)

    return outline_body
예제 #2
0
def convertFunctionCallToOutline(provider, function_ref, values):
    # This has got to have pretty man details, pylint: disable=too-many-locals
    function_body = function_ref.getFunctionBody()

    call_source_ref = function_ref.getSourceReference()
    function_source_ref = function_body.getSourceReference()

    outline_body = ExpressionOutlineBody(
        provider=provider, name="inline", source_ref=function_source_ref
    )

    clone = function_body.getBody().makeClone()

    temp_scope = outline_body.getOutlineTempScope()

    translation = {}

    for variable in function_body.getLocalVariables():
        # TODO: Later we should be able to do that too.
        assert variable.isSharedTechnically() is False

        new_variable = outline_body.allocateTempVariable(
            temp_scope=temp_scope, name=variable.getName()
        )

        # TODO: Lets update all at once maybe, it would take less visits.
        updateVariableUsage(clone, old_variable=variable, new_variable=new_variable)

        translation[variable.getName()] = new_variable

    statements = []

    if function_body.isExpressionClassBody():
        argument_names = ()
    else:
        argument_names = function_body.getParameters().getParameterNames()

    assert len(argument_names) == len(values), (argument_names, values)

    for argument_name, value in zip(argument_names, values):
        statements.append(
            StatementAssignmentVariable(
                variable=translation[argument_name],
                source=value,
                source_ref=call_source_ref,
            )
        )

    body = makeStatementsSequence(
        statements=(statements, clone), allow_none=False, source_ref=function_source_ref
    )
    outline_body.setBody(body)

    return outline_body
예제 #3
0
    def demoteClosureVariable(self, variable):
        assert variable.isLocalVariable()

        self.taken.remove(variable)

        assert variable.getOwner() is not self

        new_variable = Variables.LocalVariable(
            owner=self, variable_name=variable.getName())

        self.providing[variable.getName()] = new_variable

        updateVariableUsage(provider=self,
                            old_variable=variable,
                            new_variable=new_variable)

        VariableRegistry.addVariableUsage(new_variable, self)
예제 #4
0
    def demoteClosureVariable(self, variable):
        assert variable.isLocalVariable()

        self.taken.remove(variable)

        assert variable.getOwner() is not self

        new_variable = Variables.LocalVariable(
            owner=self, variable_name=variable.getName())
        for variable_trace in variable.traces:
            if variable_trace.getOwner() is self:
                new_variable.addTrace(variable_trace)
        new_variable.updateUsageState()

        self.providing[variable.getName()] = new_variable

        updateVariableUsage(provider=self,
                            old_variable=variable,
                            new_variable=new_variable)
예제 #5
0
    def demoteClosureVariable(self, variable):
        assert variable.isLocalVariable()

        self.taken.remove(variable)

        assert variable.getOwner() is not self

        new_variable = Variables.LocalVariable(
            owner=self, variable_name=variable.getName()
        )
        for variable_trace in variable.traces:
            if variable_trace.getOwner() is self:
                new_variable.addTrace(variable_trace)
        new_variable.updateUsageState()

        self.providing[variable.getName()] = new_variable

        updateVariableUsage(
            provider=self, old_variable=variable, new_variable=new_variable
        )
예제 #6
0
    def demoteClosureVariable(self, variable):
        assert variable.isLocalVariable()

        self.taken.remove(variable)

        assert variable.getOwner() is not self

        new_variable = Variables.LocalVariable(
            owner         = self,
            variable_name = variable.getName()
        )

        self.providing[variable.getName()] = new_variable

        updateVariableUsage(
            provider     = self,
            old_variable = variable,
            new_variable = new_variable
        )

        VariableRegistry.removeVariableUsage(variable, self)
        VariableRegistry.addVariableUsage(new_variable, self)
예제 #7
0
def convertFunctionCallToOutline(provider, function_ref, values):
    # This has got to have pretty man details, pylint: disable=too-many-locals
    function_body = function_ref.getFunctionBody()

    call_source_ref = function_ref.getSourceReference()
    function_source_ref = function_body.getSourceReference()

    outline_body = ExpressionOutlineBody(provider=provider,
                                         name="inline",
                                         source_ref=function_source_ref)

    clone = function_body.getBody().makeClone()

    temp_scope = outline_body.getOutlineTempScope()

    translation = {}

    for variable in function_body.getLocalVariables():
        # TODO: Later we should be able to do that too.
        assert variable.isSharedTechnically() is False

        new_variable = outline_body.allocateTempVariable(
            temp_scope=temp_scope, name=variable.getName())

        # TODO: Lets update all at once maybe, it would take less visits.
        updateVariableUsage(clone,
                            old_variable=variable,
                            new_variable=new_variable)

        translation[variable.getName()] = new_variable

    statements = []

    if function_body.isExpressionClassBody():
        argument_names = ()
    else:
        argument_names = function_body.getParameters().getParameterNames()

    assert len(argument_names) == len(values), (argument_names, values)

    for argument_name, value in zip(argument_names, values):
        statements.append(
            StatementAssignmentVariable(
                variable=translation[argument_name],
                source=value,
                source_ref=call_source_ref,
            ))

    body = makeStatementsSequence(statements=(statements, clone),
                                  allow_none=False,
                                  source_ref=function_source_ref)

    auto_releases = function_body.getFunctionVariablesWithAutoReleases()

    if auto_releases:
        releases = [
            StatementReleaseVariable(variable=variable,
                                     source_ref=function_source_ref)
            for variable in auto_releases
        ]

        body = makeTryFinallyStatement(
            provider=outline_body,
            tried=body,
            final=releases,
            source_ref=function_source_ref,
        )

    outline_body.setBody(body)

    return outline_body