cairo_lang_lowering/graph_algorithms/
cycles.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use cairo_lang_diagnostics::Maybe;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;

use crate::DependencyType;
use crate::db::{LoweringGroup, get_direct_callees};
use crate::ids::{ConcreteFunctionWithBodyId, FunctionId, FunctionWithBodyId};

/// Query implementation of
/// [crate::db::LoweringGroup::function_with_body_direct_callees].
pub fn function_with_body_direct_callees(
    db: &dyn LoweringGroup,
    function_id: FunctionWithBodyId,
    dependency_type: DependencyType,
) -> Maybe<OrderedHashSet<FunctionId>> {
    let (lowered, block_extra_calls) =
        db.function_with_body_lowering_with_borrow_check(function_id)?;
    Ok(get_direct_callees(db, &lowered, dependency_type, &block_extra_calls).into_iter().collect())
}

/// Query implementation of
/// [crate::db::LoweringGroup::function_with_body_direct_function_with_body_callees].
pub fn function_with_body_direct_function_with_body_callees(
    db: &dyn LoweringGroup,
    function_id: FunctionWithBodyId,
    dependency_type: DependencyType,
) -> Maybe<OrderedHashSet<FunctionWithBodyId>> {
    Ok(db
        .function_with_body_direct_callees(function_id, dependency_type)?
        .into_iter()
        .map(|function_id| function_id.body(db))
        .collect::<Maybe<Vec<Option<_>>>>()?
        .into_iter()
        .flatten()
        .map(|x| x.function_with_body_id(db))
        .collect())
}

/// Query implementation of [LoweringGroup::final_contains_call_cycle].
pub fn final_contains_call_cycle(
    db: &dyn LoweringGroup,
    function_id: ConcreteFunctionWithBodyId,
) -> Maybe<bool> {
    let direct_callees = db.final_concrete_function_with_body_lowered_direct_callees(
        function_id,
        DependencyType::Call,
    )?;
    for callee in direct_callees {
        if db.final_contains_call_cycle(callee)? {
            return Ok(true);
        }
    }

    Ok(false)
}

/// Cycle handling for [LoweringGroup::final_contains_call_cycle].
pub fn final_contains_call_cycle_handle_cycle(
    _db: &dyn LoweringGroup,
    _cycle: &salsa::Cycle,
    _function_id: &ConcreteFunctionWithBodyId,
) -> Maybe<bool> {
    Ok(true)
}

/// Query implementation of [LoweringGroup::in_cycle].
pub fn in_cycle(
    db: &dyn LoweringGroup,
    function_id: FunctionWithBodyId,
    dependency_type: DependencyType,
) -> Maybe<bool> {
    if db
        .function_with_body_direct_function_with_body_callees(function_id, dependency_type)?
        .contains(&function_id)
    {
        return Ok(true);
    }
    Ok(db.function_with_body_scc(function_id, dependency_type).len() > 1)
}