Beispiel #1
0
def test_cpp_value_as_str():
    'Make sure we can generate a str from a value - this will be important for errors'
    v1 = crep.cpp_value('dude', top_level_scope(), ctyp.terminal('int'))
    assert 'dude' in str(v1)

    v2 = crep.cpp_value('dude', top_level_scope(), None)
    assert 'dude' in str(v2)
Beispiel #2
0
def test_sequence_type():
    tc = gc_scope_top_level()
    s_value = crep.cpp_value('0.0', tc, ctyp.terminal('int', False))
    i_value = crep.cpp_value('1.0', tc, ctyp.terminal('object', False))

    seq = crep.cpp_sequence(s_value, i_value, tc)

    assert seq.sequence_value().cpp_type().type == 'int'
Beispiel #3
0
def test_variable_pointer_2():
    'Make sure p_depth can deal with a non-type correctly'
    v1 = crep.cpp_value('dude', top_level_scope(), ctyp.terminal('int'))
    v2 = crep.cpp_value('dude', top_level_scope(), None)

    assert v1.cpp_type().type == 'int'
    with pytest.raises(RuntimeError):
        v2.cpp_type()
Beispiel #4
0
def test_as_root_as_single_column():
    q = atlas_xaod_query_ast_visitor()
    node = ast.parse('1/1')
    value_obj = crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int'))
    sequence = crep.cpp_sequence(
        value_obj,
        crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int')),
        gc_scope_top_level())
    node.rep = sequence  # type: ignore
    as_root = q.get_as_ROOT(node)

    assert isinstance(as_root, rh.cpp_ttree_rep)
def test_deepest_scope_equal():
    g = generated_code()
    s1 = statement.iftest("true")
    statement.set_var("v1", "true")
    g.add_statement(s1)
    scope_1 = g.current_scope()

    v1 = crep.cpp_value("v1", scope_1, ctyp.terminal('int'))
    v2 = crep.cpp_value("v2", scope_1, ctyp.terminal('int'))

    assert v1 == deepest_scope(v1, v2)
    assert v2 == deepest_scope(v2, v1)
def test_deepest_scope_one_greater():
    g = generated_code()
    s1 = statement.iftest("true")
    s2 = statement.iftest("true")
    g.add_statement(s1)
    scope_1 = g.current_scope()
    g.add_statement(s2)
    scope_2 = g.current_scope()

    v1 = crep.cpp_value("v1", scope_1, ctyp.terminal('int'))
    v2 = crep.cpp_value("v2", scope_2, ctyp.terminal('int'))

    assert v2 == deepest_scope(v1, v2)
    assert v2 == deepest_scope(v2, v1)
Beispiel #7
0
def test_as_root_as_dict():
    q = atlas_xaod_query_ast_visitor()
    node = ast.parse('1/1')
    dict_obj = crep.cpp_dict(
        {
            ast.Constant(value='hi'):
            crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int'))
        }, gc_scope_top_level())
    sequence = crep.cpp_sequence(
        dict_obj,  # type: ignore
        crep.cpp_value('i', gc_scope_top_level(), ctyp.terminal('int')),
        gc_scope_top_level())
    node.rep = sequence  # type: ignore
    as_root = q.get_as_ROOT(node)

    assert isinstance(as_root, rh.cpp_ttree_rep)
Beispiel #8
0
def process_ast_node(visitor, gc, call_node: ast.Call):
    r'''Inject the proper code into the output stream to deal with this C++ code.

    We expect this to be run on the back-end of the system.

    visitor - The node visitor that is converting the code into C++
    gc - the generated code object that we fill with actual code
    call_node - a Call ast node, with func being a CPPCodeValue.

    Result:
    representation - A value that represents the output
    '''

    # We write everything into a new scope to prevent conflicts. So we have to declare the result ahead of time.
    cpp_ast_node = cast(CPPCodeValue, call_node.func)
    result_rep = cpp_ast_node.result_rep(gc.current_scope())  # type: ignore

    gc.declare_variable(result_rep)

    # Include files and link libraries
    for i in cpp_ast_node.include_files:
        gc.add_include(i)
    for i in cpp_ast_node.link_libraries:
        gc.add_link_library(i)

    # Build the dictionary for replacement for the object we are calling
    # against, if any.
    repl_list = []
    if cpp_ast_node.replacement_instance_obj is not None:
        repl_list += [
            (cpp_ast_node.replacement_instance_obj[0],
             visitor.resolve_id(
                 cpp_ast_node.replacement_instance_obj[1]).rep.as_cpp())
        ]

    # Process the arguments that are getting passed to the function
    for arg, dest in zip(cpp_ast_node.args, call_node.args):
        rep = visitor.get_rep(dest)
        repl_list += [(arg, rep.as_cpp())]

    # Emit the statements.
    blk = statements.block()
    visitor._gc.add_statement(blk)

    for s in cpp_ast_node.running_code:
        l_s = s
        for src, dest in repl_list:
            l_s = l_s.replace(src, str(dest))
        blk.add_statement(statements.arbitrary_statement(l_s))

    # Set the result and close the scope
    assert cpp_ast_node.result is not None
    blk.add_statement(
        statements.set_var(
            result_rep,
            cpp_value(cpp_ast_node.result, gc.current_scope(),
                      result_rep.cpp_type())))
    gc.pop_scope()

    return result_rep
Beispiel #9
0
def test_compare_string_var():
    q = atlas_xaod_query_ast_visitor()
    node = ast.parse('e == "hi"').body[0].value  # type: ignore

    node.left.rep = crep.cpp_value('e', gc_scope_top_level(),
                                   ctyp.terminal('string'))  # type: ignore

    r = q.get_rep(node)

    assert isinstance(r, crep.cpp_value)
    assert r.cpp_type().type == 'bool'
    assert r.as_cpp() == '(e=="hi")'
Beispiel #10
0
def test_variable_type__with_initial_update():
    tc = gc_scope_top_level()
    expr = "a"
    c_type = ctyp.terminal('int', False)
    c_init = crep.cpp_value('0.0', tc, ctyp.terminal('int', False))

    v = crep.cpp_variable(expr, tc, c_type, c_init)
    v.update_type(ctyp.terminal('float', False))

    assert v.cpp_type().type == 'float'
    iv = v.initial_value()
    assert iv is not None
    assert iv.cpp_type().type == 'float'
Beispiel #11
0
def test_expression_pointer_decl():
    e2 = crep.cpp_value("dude", top_level_scope(), ctyp.terminal("int"))
    assert e2.p_depth == 0

    e3 = crep.cpp_value("dude", top_level_scope(), ctyp.terminal("int", p_depth=1))
    assert e3.p_depth == 1
Beispiel #12
0
def test_member_access_obj_depth_1():
    cv = crep.cpp_value('f', gc_scope_top_level(), ctyp.terminal(ctyp.parse_type('obj')))
    assert crep.base_type_member_access(cv, 2) == '(*f)->'
Beispiel #13
0
def test_member_access_obj_ptr():
    cv = crep.cpp_value('f', gc_scope_top_level(), ctyp.terminal(ctyp.parse_type('obj*')))
    assert crep.base_type_member_access(cv) == 'f->'