cairo_lang_lowering/lower/
context.rs

1use std::ops::{Deref, DerefMut, Index};
2use std::sync::Arc;
3
4use cairo_lang_defs::ids::{LanguageElementId, ModuleFileId};
5use cairo_lang_diagnostics::{DiagnosticAdded, Maybe};
6use cairo_lang_semantic::ConcreteVariant;
7use cairo_lang_semantic::expr::fmt::ExprFormatter;
8use cairo_lang_semantic::items::enm::SemanticEnumEx;
9use cairo_lang_semantic::items::imp::ImplLookupContext;
10use cairo_lang_semantic::usage::Usages;
11use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
12use cairo_lang_utils::Intern;
13use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
14use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
15use defs::diagnostic_utils::StableLocation;
16use id_arena::Arena;
17use itertools::{Itertools, zip_eq};
18use semantic::corelib::{core_module, get_ty_by_name};
19use semantic::types::wrap_in_snapshots;
20use semantic::{ExprVarMemberPath, MatchArmSelector, TypeLongId};
21use {cairo_lang_defs as defs, cairo_lang_semantic as semantic};
22
23use super::block_builder::{BlockBuilder, SealedBlockBuilder};
24use super::generators;
25use crate::blocks::FlatBlocksBuilder;
26use crate::db::LoweringGroup;
27use crate::diagnostic::LoweringDiagnostics;
28use crate::ids::{
29    ConcreteFunctionWithBodyId, FunctionWithBodyId, GeneratedFunctionKey, LocationId,
30    SemanticFunctionIdEx, Signature,
31};
32use crate::lower::external::{extern_facade_expr, extern_facade_return_tys};
33use crate::objects::Variable;
34use crate::{FlatLowered, MatchArm, MatchExternInfo, MatchInfo, VarUsage, VariableId};
35
36pub struct VariableAllocator<'db> {
37    pub db: &'db dyn LoweringGroup,
38    /// Arena of allocated lowered variables.
39    pub variables: Arena<Variable>,
40    /// Module and file of the declared function.
41    pub module_file_id: ModuleFileId,
42    // Lookup context for impls.
43    pub lookup_context: ImplLookupContext,
44}
45impl<'db> VariableAllocator<'db> {
46    pub fn new(
47        db: &'db dyn LoweringGroup,
48        function_id: defs::ids::FunctionWithBodyId,
49        variables: Arena<Variable>,
50    ) -> Maybe<Self> {
51        let generic_params = db.function_with_body_generic_params(function_id)?;
52        let generic_param_ids = generic_params.iter().map(|p| p.id()).collect_vec();
53        Ok(Self {
54            db,
55            variables,
56            module_file_id: function_id.module_file_id(db.upcast()),
57            lookup_context: ImplLookupContext::new(
58                function_id.parent_module(db.upcast()),
59                generic_param_ids,
60            ),
61        })
62    }
63
64    /// Allocates a new variable in the context's variable arena according to the context.
65    pub fn new_var(&mut self, req: VarRequest) -> VariableId {
66        self.variables.alloc(Variable::new(
67            self.db,
68            self.lookup_context.clone(),
69            req.ty,
70            req.location,
71        ))
72    }
73
74    /// Retrieves the LocationId of a stable syntax pointer in the current function file.
75    pub fn get_location(&self, stable_ptr: SyntaxStablePtrId) -> LocationId {
76        LocationId::from_stable_location(self.db, StableLocation::new(stable_ptr))
77    }
78}
79impl Index<VariableId> for VariableAllocator<'_> {
80    type Output = Variable;
81
82    fn index(&self, index: VariableId) -> &Self::Output {
83        &self.variables[index]
84    }
85}
86
87/// Lowering context for the encapsulating semantic function.
88///
89/// Each semantic function may generate multiple lowered functions. This context is common to all
90/// the generated lowered functions of an encapsulating semantic function.
91pub struct EncapsulatingLoweringContext<'db> {
92    pub db: &'db dyn LoweringGroup,
93    /// Id for the current function being lowered.
94    pub semantic_function_id: defs::ids::FunctionWithBodyId,
95    /// Semantic model for current function body.
96    pub function_body: Arc<semantic::FunctionBody>,
97    /// Definitions encountered for semantic bindings. Since Constants are not lowered, this is
98    /// only used for variables.
99    // TODO(spapini): consider moving to semantic model.
100    pub semantic_defs: UnorderedHashMap<semantic::VarId, semantic::Binding>,
101    /// Expression formatter of the free function.
102    pub expr_formatter: ExprFormatter<'db>,
103    /// Block usages for the entire encapsulating function.
104    pub usages: Usages,
105    /// Lowerings of generated functions.
106    pub lowerings: OrderedHashMap<GeneratedFunctionKey, FlatLowered>,
107}
108impl<'db> EncapsulatingLoweringContext<'db> {
109    pub fn new(
110        db: &'db dyn LoweringGroup,
111        semantic_function_id: defs::ids::FunctionWithBodyId,
112    ) -> Maybe<Self> {
113        let function_body = db.function_body(semantic_function_id)?;
114        let usages = Usages::from_function_body(&function_body);
115        Ok(Self {
116            db,
117            semantic_function_id,
118            function_body,
119            semantic_defs: Default::default(),
120            expr_formatter: ExprFormatter { db: db.upcast(), function_id: semantic_function_id },
121            usages,
122            lowerings: Default::default(),
123        })
124    }
125}
126
127/// The loop result variants for a loop with an early return.
128#[derive(Clone)]
129pub struct LoopEarlyReturnInfo {
130    pub normal_return_variant: ConcreteVariant,
131    pub early_return_variant: ConcreteVariant,
132}
133
134/// Context for lowering a loop.
135pub struct LoopContext {
136    /// The loop expression
137    pub loop_expr_id: semantic::ExprId,
138    /// Optional info related to early return from the loop.
139    pub early_return_info: Option<LoopEarlyReturnInfo>,
140}
141
142pub struct LoweringContext<'a, 'db> {
143    pub encapsulating_ctx: Option<&'a mut EncapsulatingLoweringContext<'db>>,
144    /// Variable allocator.
145    pub variables: VariableAllocator<'db>,
146    /// Current function signature.
147    pub signature: Signature,
148    /// Id for the current function being lowered.
149    pub function_id: FunctionWithBodyId,
150    /// Id for the current concrete function to be used when generating recursive calls.
151    /// This is the generic function specialized with its own generic parameters.
152    pub concrete_function_id: ConcreteFunctionWithBodyId,
153    /// Current loop context.
154    pub current_loop_ctx: Option<LoopContext>,
155    /// Current emitted diagnostics.
156    pub diagnostics: LoweringDiagnostics,
157    /// Lowered blocks of the function.
158    pub blocks: FlatBlocksBuilder,
159    // The return type in the current context, for loops this differs from signature.return_type.
160    pub return_type: semantic::TypeId,
161}
162impl<'a, 'db> LoweringContext<'a, 'db> {
163    pub fn new(
164        global_ctx: &'a mut EncapsulatingLoweringContext<'db>,
165        function_id: FunctionWithBodyId,
166        signature: Signature,
167        return_type: semantic::TypeId,
168    ) -> Maybe<Self>
169    where
170        'db: 'a,
171    {
172        let db = global_ctx.db;
173        let concrete_function_id = function_id.to_concrete(db)?;
174        let semantic_function = function_id.base_semantic_function(db);
175        Ok(Self {
176            encapsulating_ctx: Some(global_ctx),
177            variables: VariableAllocator::new(db, semantic_function, Default::default())?,
178            signature,
179            function_id,
180            concrete_function_id,
181            current_loop_ctx: None,
182            diagnostics: LoweringDiagnostics::default(),
183            blocks: Default::default(),
184            return_type,
185        })
186    }
187}
188impl<'db> Deref for LoweringContext<'_, 'db> {
189    type Target = EncapsulatingLoweringContext<'db>;
190
191    fn deref(&self) -> &Self::Target {
192        self.encapsulating_ctx.as_ref().unwrap()
193    }
194}
195impl DerefMut for LoweringContext<'_, '_> {
196    fn deref_mut(&mut self) -> &mut Self::Target {
197        self.encapsulating_ctx.as_mut().unwrap()
198    }
199}
200impl LoweringContext<'_, '_> {
201    /// Allocates a new variable in the context's variable arena according to the context.
202    pub fn new_var(&mut self, req: VarRequest) -> VariableId {
203        self.variables.new_var(req)
204    }
205
206    /// Same as `new_var` but returns it as a `VarUsage`.
207    /// This is useful when the variable definition and usage locations are the same.
208    pub fn new_var_usage(&mut self, req: VarRequest) -> VarUsage {
209        let location = req.location;
210
211        VarUsage { var_id: self.variables.new_var(req), location }
212    }
213
214    /// Retrieves the LocationId of a stable syntax pointer in the current function file.
215    pub fn get_location(&self, stable_ptr: SyntaxStablePtrId) -> LocationId {
216        self.variables.get_location(stable_ptr)
217    }
218}
219
220/// Request for a lowered variable allocation.
221pub struct VarRequest {
222    pub ty: semantic::TypeId,
223    pub location: LocationId,
224}
225
226/// Representation of the value of a computed expression.
227#[derive(Clone, Debug)]
228pub enum LoweredExpr {
229    /// The expression value lies in a variable.
230    AtVariable(VarUsage),
231    /// The expression value is a tuple.
232    Tuple {
233        exprs: Vec<LoweredExpr>,
234        location: LocationId,
235    },
236    /// The expression value is a fixed size array.
237    FixedSizeArray {
238        ty: semantic::TypeId,
239        exprs: Vec<LoweredExpr>,
240        location: LocationId,
241    },
242    /// The expression value is an enum result from an extern call.
243    ExternEnum(LoweredExprExternEnum),
244    Member(ExprVarMemberPath, LocationId),
245    Snapshot {
246        expr: Box<LoweredExpr>,
247        location: LocationId,
248    },
249}
250impl LoweredExpr {
251    // Returns a VarUsage corresponding to the lowered expression.
252    pub fn as_var_usage(
253        self,
254        ctx: &mut LoweringContext<'_, '_>,
255        builder: &mut BlockBuilder,
256    ) -> Result<VarUsage, LoweringFlowError> {
257        match self {
258            LoweredExpr::AtVariable(var_usage) => Ok(var_usage),
259            LoweredExpr::Tuple { exprs, location } => {
260                let inputs: Vec<_> = exprs
261                    .into_iter()
262                    .map(|expr| expr.as_var_usage(ctx, builder))
263                    .collect::<Result<Vec<_>, _>>()?;
264                let tys =
265                    inputs.iter().map(|var_usage| ctx.variables[var_usage.var_id].ty).collect();
266                let ty = semantic::TypeLongId::Tuple(tys).intern(ctx.db);
267                Ok(generators::StructConstruct { inputs, ty, location }
268                    .add(ctx, &mut builder.statements))
269            }
270            LoweredExpr::ExternEnum(extern_enum) => extern_enum.as_var_usage(ctx, builder),
271            LoweredExpr::Member(member_path, _location) => {
272                Ok(builder.get_ref(ctx, &member_path).unwrap())
273            }
274            LoweredExpr::Snapshot { expr, location } => {
275                let input = expr.clone().as_var_usage(ctx, builder)?;
276                let (original, snapshot) =
277                    generators::Snapshot { input, location }.add(ctx, &mut builder.statements);
278                if let LoweredExpr::Member(member_path, _location) = &*expr {
279                    builder.update_ref(ctx, member_path, original);
280                }
281
282                Ok(VarUsage { var_id: snapshot, location })
283            }
284            LoweredExpr::FixedSizeArray { exprs, location, ty } => {
285                let inputs = exprs
286                    .into_iter()
287                    .map(|expr| expr.as_var_usage(ctx, builder))
288                    .collect::<Result<Vec<_>, _>>()?;
289                Ok(generators::StructConstruct { inputs, ty, location }
290                    .add(ctx, &mut builder.statements))
291            }
292        }
293    }
294
295    pub fn ty(&self, ctx: &mut LoweringContext<'_, '_>) -> semantic::TypeId {
296        match self {
297            LoweredExpr::AtVariable(var_usage) => ctx.variables[var_usage.var_id].ty,
298            LoweredExpr::Tuple { exprs, .. } => {
299                semantic::TypeLongId::Tuple(exprs.iter().map(|expr| expr.ty(ctx)).collect())
300                    .intern(ctx.db)
301            }
302            LoweredExpr::ExternEnum(extern_enum) => semantic::TypeLongId::Concrete(
303                semantic::ConcreteTypeId::Enum(extern_enum.concrete_enum_id),
304            )
305            .intern(ctx.db),
306            LoweredExpr::Member(member_path, _) => member_path.ty(),
307            LoweredExpr::Snapshot { expr, .. } => {
308                wrap_in_snapshots(ctx.db.upcast(), expr.ty(ctx), 1)
309            }
310            LoweredExpr::FixedSizeArray { ty, .. } => *ty,
311        }
312    }
313    pub fn location(&self) -> LocationId {
314        match &self {
315            LoweredExpr::AtVariable(VarUsage { location, .. })
316            | LoweredExpr::Tuple { location, .. }
317            | LoweredExpr::ExternEnum(LoweredExprExternEnum { location, .. })
318            | LoweredExpr::Member(_, location)
319            | LoweredExpr::Snapshot { location, .. } => *location,
320            LoweredExpr::FixedSizeArray { location, .. } => *location,
321        }
322    }
323}
324
325/// Lazy expression value of an extern call returning an enum.
326#[derive(Clone, Debug)]
327pub struct LoweredExprExternEnum {
328    pub function: semantic::FunctionId,
329    pub concrete_enum_id: semantic::ConcreteEnumId,
330    pub inputs: Vec<VarUsage>,
331    pub member_paths: Vec<semantic::ExprVarMemberPath>,
332    pub location: LocationId,
333}
334impl LoweredExprExternEnum {
335    pub fn as_var_usage(
336        self,
337        ctx: &mut LoweringContext<'_, '_>,
338        builder: &mut BlockBuilder,
339    ) -> LoweringResult<VarUsage> {
340        let concrete_variants = ctx
341            .db
342            .concrete_enum_variants(self.concrete_enum_id)
343            .map_err(LoweringFlowError::Failed)?;
344
345        let mut arm_var_ids = vec![];
346        let (sealed_blocks, block_ids): (Vec<_>, Vec<_>) = concrete_variants
347            .clone()
348            .into_iter()
349            .map(|concrete_variant| {
350                let mut subscope = builder.child_block_builder(ctx.blocks.alloc_empty());
351                let block_id = subscope.block_id;
352
353                let mut var_ids = vec![];
354                // Bind the ref parameters.
355                for member_path in &self.member_paths {
356                    let var =
357                        ctx.new_var(VarRequest { ty: member_path.ty(), location: self.location });
358                    var_ids.push(var);
359
360                    subscope.update_ref(ctx, member_path, var);
361                }
362
363                let variant_vars = extern_facade_return_tys(ctx, concrete_variant.ty)
364                    .into_iter()
365                    .map(|ty| ctx.new_var(VarRequest { ty, location: self.location }))
366                    .collect_vec();
367                var_ids.extend(variant_vars.iter());
368
369                arm_var_ids.push(var_ids);
370                let maybe_input =
371                    extern_facade_expr(ctx, concrete_variant.ty, variant_vars, self.location)
372                        .as_var_usage(ctx, &mut subscope);
373                let input = match maybe_input {
374                    Ok(var_usage) => var_usage,
375                    Err(err) => {
376                        return lowering_flow_error_to_sealed_block(ctx, subscope, err)
377                            .map(|sb| (sb, block_id));
378                    }
379                };
380                let result = generators::EnumConstruct {
381                    input,
382                    variant: concrete_variant,
383                    location: self.location,
384                }
385                .add(ctx, &mut subscope.statements);
386                Ok((subscope.goto_callsite(Some(result)), block_id))
387            })
388            .collect::<Result<Vec<_>, _>>()
389            .map_err(LoweringFlowError::Failed)?
390            .into_iter()
391            .unzip();
392
393        let match_info = MatchInfo::Extern(MatchExternInfo {
394            function: self.function.lowered(ctx.db),
395            inputs: self.inputs,
396            arms: zip_eq(zip_eq(concrete_variants, block_ids), arm_var_ids)
397                .map(|((variant_id, block_id), var_ids)| MatchArm {
398                    arm_selector: MatchArmSelector::VariantId(variant_id),
399                    block_id,
400                    var_ids,
401                })
402                .collect(),
403            location: self.location,
404        });
405        builder
406            .merge_and_end_with_match(ctx, match_info, sealed_blocks, self.location)?
407            .as_var_usage(ctx, builder)
408    }
409}
410
411pub type LoweringResult<T> = Result<T, LoweringFlowError>;
412
413/// Cases where the flow of lowering an expression should halt.
414#[derive(Debug)]
415pub enum LoweringFlowError {
416    /// Computation failure. A corresponding diagnostic should be emitted.
417    Failed(DiagnosticAdded),
418    Panic(VarUsage, LocationId),
419    Return(VarUsage, LocationId),
420    /// Every match arm is terminating - does not flow to parent builder
421    /// e.g. returns or panics.
422    Match(MatchInfo),
423}
424impl LoweringFlowError {
425    pub fn is_unreachable(&self) -> bool {
426        match self {
427            LoweringFlowError::Failed(_) => false,
428            LoweringFlowError::Panic(_, _)
429            | LoweringFlowError::Return(_, _)
430            | LoweringFlowError::Match(_) => true,
431        }
432    }
433}
434
435/// Converts a lowering flow error to the appropriate block builder end, if possible.
436pub fn lowering_flow_error_to_sealed_block(
437    ctx: &mut LoweringContext<'_, '_>,
438    mut builder: BlockBuilder,
439    err: LoweringFlowError,
440) -> Maybe<SealedBlockBuilder> {
441    let block_id = builder.block_id;
442    match err {
443        LoweringFlowError::Failed(diag_added) => return Err(diag_added),
444        LoweringFlowError::Return(return_var, location) => {
445            builder.ret(ctx, return_var, location)?;
446        }
447        LoweringFlowError::Panic(data_var, location) => {
448            let panic_instance = generators::StructConstruct {
449                inputs: vec![],
450                ty: get_ty_by_name(
451                    ctx.db.upcast(),
452                    core_module(ctx.db.upcast()),
453                    "Panic".into(),
454                    vec![],
455                ),
456                location,
457            }
458            .add(ctx, &mut builder.statements);
459            let err_instance = generators::StructConstruct {
460                inputs: vec![panic_instance, data_var],
461                ty: TypeLongId::Tuple(vec![
462                    ctx.variables[panic_instance.var_id].ty,
463                    ctx.variables[data_var.var_id].ty,
464                ])
465                .intern(ctx.db),
466                location,
467            }
468            .add(ctx, &mut builder.statements);
469            builder.panic(ctx, err_instance)?;
470        }
471        LoweringFlowError::Match(info) => {
472            builder.unreachable_match(ctx, info);
473        }
474    }
475    Ok(SealedBlockBuilder::Ends(block_id))
476}