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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use cairo_lang_diagnostics::Maybe;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;

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

/// Query implementation of
/// [crate::db::LoweringGroup::function_with_body_direct_callees].
pub fn function_with_body_direct_callees(
    db: &dyn LoweringGroup,
    function_id: FunctionWithBodyId,
) -> Maybe<OrderedHashSet<FunctionId>> {
    let lowered = db.function_with_body_lowering(function_id)?;
    let mut direct_callees = OrderedHashSet::default();
    lowered.blocks.has_root()?;
    for (_, block) in lowered.blocks.iter() {
        for stmt in &block.statements {
            match stmt {
                crate::Statement::Call(stmt) => {
                    direct_callees.insert(stmt.function);
                }
                crate::Statement::Literal(_)
                | crate::Statement::StructConstruct(_)
                | crate::Statement::StructDestructure(_)
                | crate::Statement::EnumConstruct(_)
                | crate::Statement::Snapshot(_)
                | crate::Statement::Desnap(_) => {}
            };
        }
        match &block.end {
            crate::FlatBlockEnd::Match { info: MatchInfo::Extern(s) } => {
                direct_callees.insert(s.function);
            }
            crate::FlatBlockEnd::Match { info: MatchInfo::Enum(_) }
            | crate::FlatBlockEnd::NotSet
            | crate::FlatBlockEnd::Return(_)
            | crate::FlatBlockEnd::Panic(_)
            | crate::FlatBlockEnd::Goto(_, _) => {}
        }
    }
    Ok(direct_callees)
}

/// 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,
) -> Maybe<OrderedHashSet<FunctionWithBodyId>> {
    Ok(db
        .function_with_body_direct_callees(function_id)?
        .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::contains_cycle].
pub fn contains_cycle(
    db: &dyn LoweringGroup,
    function_id: ConcreteFunctionWithBodyId,
) -> Maybe<bool> {
    let direct_callees = db.concrete_function_with_body_direct_callees_with_body(function_id)?;
    for callee in direct_callees {
        if db.contains_cycle(callee)? {
            return Ok(true);
        }
    }

    Ok(false)
}

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

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