|
1 | 1 | use super::{PreferredType, ResolveExprCtx};
|
2 | 2 | use crate::{
|
| 3 | + ast::HelperExpr, |
| 4 | + ir::GlobalVarRef, |
3 | 5 | name::{Name, ResolvedName},
|
4 | 6 | resolve::{
|
5 | 7 | error::{ResolveError, ResolveErrorKind},
|
6 | 8 | expr::resolve_expr,
|
7 | 9 | Initialized,
|
8 | 10 | },
|
9 |
| - resolved::{self, TypedExpr}, |
| 11 | + resolved::{self, Type, TypedExpr}, |
10 | 12 | source_files::Source,
|
11 | 13 | };
|
12 | 14 |
|
@@ -49,41 +51,105 @@ pub fn resolve_variable_expr(
|
49 | 51 | let resolved_name = ResolvedName::new(name);
|
50 | 52 |
|
51 | 53 | if let Some((resolved_type, reference)) = ctx.global_search_ctx.find_global(&resolved_name) {
|
52 |
| - return Ok(TypedExpr::new( |
53 |
| - resolved_type.clone(), |
54 |
| - resolved::Expr::new( |
55 |
| - resolved::ExprKind::GlobalVariable(Box::new(resolved::GlobalVariable { |
56 |
| - reference: *reference, |
57 |
| - resolved_type: resolved_type.clone(), |
58 |
| - })), |
59 |
| - source, |
60 |
| - ), |
61 |
| - )); |
| 54 | + return Ok(resolve_global_variable(resolved_type, *reference, source)); |
62 | 55 | }
|
63 | 56 |
|
64 |
| - if let Some(define) = ctx.helper_exprs.get(&resolved_name) { |
65 |
| - let TypedExpr { |
66 |
| - resolved_type, |
67 |
| - expr, |
68 |
| - is_initialized, |
69 |
| - } = resolve_expr(ctx, &define.value, preferred_type, initialized)?; |
70 |
| - |
71 |
| - return Ok(TypedExpr::new_maybe_initialized( |
72 |
| - resolved_type, |
73 |
| - resolved::Expr::new( |
74 |
| - resolved::ExprKind::ResolvedNamedExpression(name.to_string(), Box::new(expr)), |
75 |
| - source, |
76 |
| - ), |
77 |
| - is_initialized, |
78 |
| - )); |
| 57 | + if let Some(helper_expr) = ctx.helper_exprs.get(&resolved_name) { |
| 58 | + return resolve_helper_expr(ctx, helper_expr, preferred_type, initialized, source); |
79 | 59 | }
|
80 | 60 |
|
81 |
| - // TODO: Check if any global variables from imported namespaces match |
82 |
| - // TODO: Check if any helper exprs from imported namespaces match |
83 |
| - // TODO: They should probably be checked at the same time |
| 61 | + if name.namespace.is_empty() { |
| 62 | + let mut matches = ctx |
| 63 | + .settings |
| 64 | + .imported_namespaces |
| 65 | + .iter() |
| 66 | + .flat_map(|namespace| { |
| 67 | + let resolved_name = ResolvedName::new(&Name::new( |
| 68 | + Some(namespace.to_string()), |
| 69 | + name.basename.to_string(), |
| 70 | + )); |
| 71 | + |
| 72 | + let global = ctx |
| 73 | + .global_search_ctx |
| 74 | + .find_global(&resolved_name) |
| 75 | + .map(GlobalOrHelper::Global); |
| 76 | + |
| 77 | + let helper_expr = ctx |
| 78 | + .helper_exprs |
| 79 | + .get(&resolved_name) |
| 80 | + .copied() |
| 81 | + .map(GlobalOrHelper::HelperExpr); |
| 82 | + |
| 83 | + [global, helper_expr] |
| 84 | + }) |
| 85 | + .flatten(); |
| 86 | + |
| 87 | + if let Some(found) = matches.next() { |
| 88 | + if matches.next().is_some() { |
| 89 | + return Err(ResolveErrorKind::AmbiguousSymbol { |
| 90 | + name: name.to_string(), |
| 91 | + } |
| 92 | + .at(source)); |
| 93 | + } |
| 94 | + |
| 95 | + return match found { |
| 96 | + GlobalOrHelper::Global((ty, reference)) => { |
| 97 | + Ok(resolve_global_variable(ty, *reference, source)) |
| 98 | + } |
| 99 | + GlobalOrHelper::HelperExpr(helper_expr) => { |
| 100 | + resolve_helper_expr(ctx, helper_expr, preferred_type, initialized, source) |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + } |
84 | 105 |
|
85 | 106 | Err(ResolveErrorKind::UndeclaredVariable {
|
86 | 107 | name: name.to_string(),
|
87 | 108 | }
|
88 | 109 | .at(source))
|
89 | 110 | }
|
| 111 | + |
| 112 | +enum GlobalOrHelper<'a> { |
| 113 | + Global((&'a Type, &'a GlobalVarRef)), |
| 114 | + HelperExpr(&'a HelperExpr), |
| 115 | +} |
| 116 | + |
| 117 | +fn resolve_global_variable( |
| 118 | + resolved_type: &Type, |
| 119 | + reference: GlobalVarRef, |
| 120 | + source: Source, |
| 121 | +) -> TypedExpr { |
| 122 | + TypedExpr::new( |
| 123 | + resolved_type.clone(), |
| 124 | + resolved::Expr::new( |
| 125 | + resolved::ExprKind::GlobalVariable(Box::new(resolved::GlobalVariable { |
| 126 | + reference, |
| 127 | + resolved_type: resolved_type.clone(), |
| 128 | + })), |
| 129 | + source, |
| 130 | + ), |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +fn resolve_helper_expr( |
| 135 | + ctx: &mut ResolveExprCtx, |
| 136 | + helper_expr: &HelperExpr, |
| 137 | + preferred_type: Option<PreferredType>, |
| 138 | + initialized: Initialized, |
| 139 | + source: Source, |
| 140 | +) -> Result<TypedExpr, ResolveError> { |
| 141 | + let TypedExpr { |
| 142 | + resolved_type, |
| 143 | + expr, |
| 144 | + is_initialized, |
| 145 | + } = resolve_expr(ctx, &helper_expr.value, preferred_type, initialized)?; |
| 146 | + |
| 147 | + return Ok(TypedExpr::new_maybe_initialized( |
| 148 | + resolved_type, |
| 149 | + resolved::Expr::new( |
| 150 | + resolved::ExprKind::ResolvedNamedExpression(Box::new(expr)), |
| 151 | + source, |
| 152 | + ), |
| 153 | + is_initialized, |
| 154 | + )); |
| 155 | +} |
0 commit comments