cairo_lang_lowering/concretize/
mod.rs

1use cairo_lang_diagnostics::Maybe;
2use cairo_lang_semantic::substitution::{
3    GenericSubstitution, SemanticRewriter, SubstitutionRewriter,
4};
5use cairo_lang_utils::{Intern, LookupIntern};
6
7use crate::db::LoweringGroup;
8use crate::ids::{FunctionId, FunctionLongId, GeneratedFunction};
9use crate::{FlatBlockEnd, FlatLowered, MatchArm, Statement};
10
11/// Rewrites a [FunctionId] with a [SubstitutionRewriter].
12fn concretize_function(
13    db: &dyn LoweringGroup,
14    rewriter: &mut SubstitutionRewriter<'_>,
15    function: FunctionId,
16) -> Maybe<FunctionId> {
17    let long_id = match function.lookup_intern(db) {
18        FunctionLongId::Semantic(id) => FunctionLongId::Semantic(rewriter.rewrite(id)?),
19        FunctionLongId::Generated(GeneratedFunction { parent, key }) => {
20            FunctionLongId::Generated(GeneratedFunction { parent: rewriter.rewrite(parent)?, key })
21        }
22    };
23    Ok(long_id.intern(db))
24}
25
26/// Concretizes a lowered generic function by applying a generic parameter substitution on its
27/// variable types, variants and called functions.
28pub fn concretize_lowered(
29    db: &dyn LoweringGroup,
30    lowered: &mut FlatLowered,
31    substitution: &GenericSubstitution,
32) -> Maybe<()> {
33    let mut rewriter = SubstitutionRewriter { db: db.upcast(), substitution };
34    // Substitute all types.
35    for (_, var) in lowered.variables.iter_mut() {
36        var.ty = rewriter.rewrite(var.ty)?;
37
38        for impl_id in [&mut var.destruct_impl, &mut var.panic_destruct_impl].into_iter().flatten()
39        {
40            rewriter.internal_rewrite(impl_id)?;
41        }
42    }
43    // Substitute all statements.
44    for block in lowered.blocks.iter_mut() {
45        for stmt in block.statements.iter_mut() {
46            match stmt {
47                Statement::Call(stmt) => {
48                    stmt.function = concretize_function(db, &mut rewriter, stmt.function)?;
49                }
50                Statement::EnumConstruct(stmt) => {
51                    stmt.variant = rewriter.rewrite(stmt.variant.clone())?;
52                }
53                Statement::Const(stmt) => {
54                    stmt.value = rewriter.rewrite(stmt.value.clone())?;
55                }
56                Statement::Snapshot(_)
57                | Statement::Desnap(_)
58                | Statement::StructConstruct(_)
59                | Statement::StructDestructure(_) => {}
60            }
61        }
62        if let FlatBlockEnd::Match { info } = &mut block.end {
63            for MatchArm { arm_selector: selector, .. } in match info {
64                crate::MatchInfo::Enum(s) => s.arms.iter_mut(),
65                crate::MatchInfo::Extern(s) => {
66                    s.function = concretize_function(db, &mut rewriter, s.function)?;
67                    s.arms.iter_mut()
68                }
69                crate::MatchInfo::Value(s) => s.arms.iter_mut(),
70            } {
71                *selector = rewriter.rewrite(selector.clone())?;
72            }
73        }
74    }
75    lowered.signature = rewriter.rewrite(lowered.signature.clone())?;
76
77    Ok(())
78}