swamp_analyzer/
access.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use crate::Analyzer;
6use crate::TypeContext;
7use swamp_semantic::intr::IntrinsicFunction;
8use swamp_semantic::{ArgumentExpression, Expression, ExpressionKind, Function};
9use swamp_types::prelude::*;
10
11impl Analyzer<'_> {
12    #[must_use]
13    pub fn lookup_associated_function(
14        &self,
15        ty: &TypeRef,
16        function_name: &str,
17    ) -> Option<Function> {
18        let x = self
19            .shared
20            .state
21            .associated_impls
22            .get_member_function(ty, function_name)
23            .cloned();
24
25        x.map(|found_func_ref| found_func_ref.as_ref().clone())
26    }
27
28    pub(crate) fn analyze_min_max_expr(
29        &mut self,
30        min_expr: &swamp_ast::Expression,
31        max_expr: &swamp_ast::Expression,
32    ) -> (Expression, Expression) {
33        let int_type = self.shared.state.types.int();
34        let context = TypeContext::new_argument(&int_type, false);
35
36        let resolved_min = self.analyze_expression(min_expr, &context);
37        let resolved_max = self.analyze_expression(max_expr, &context);
38
39        (resolved_min, resolved_max)
40    }
41
42    /// # Errors
43    ///
44    /// #  Panics
45    /// If core hasn't added the `Range` type
46    pub fn analyze_range(
47        &mut self,
48        min_expr: &swamp_ast::Expression,
49        max_expr: &swamp_ast::Expression,
50        mode: &swamp_ast::RangeMode,
51        ast_node: &swamp_ast::Node,
52    ) -> Expression {
53        let (min, max) = self.analyze_min_max_expr(min_expr, max_expr);
54
55        let range_type = self.shared.state.types.range_int();
56
57        let is_inclusive = matches!(mode, swamp_ast::RangeMode::Inclusive);
58
59        let bool_expr_kind = ExpressionKind::BoolLiteral(is_inclusive);
60        let bool_type = self.shared.state.types.bool();
61        let bool_expr = self.create_expr(bool_expr_kind, bool_type, ast_node);
62
63        let call_kind = ExpressionKind::IntrinsicCallEx(
64            IntrinsicFunction::RangeInit,
65            Vec::from(&[
66                ArgumentExpression::Expression(min),
67                ArgumentExpression::Expression(max),
68                ArgumentExpression::Expression(bool_expr),
69            ]), //ast_node,
70                //&range_type,
71        );
72
73        self.create_expr(call_kind, range_type, ast_node)
74    }
75}