1use crate::semantics::Value;
4use std::cell::RefCell;
5use std::collections::HashMap;
6use std::rc::Rc;
7
8#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Loc {
12 pub sl: usize,
14 pub sc: usize,
16 pub el: usize,
18 pub ec: usize,
20}
21
22thread_local! {
27 static EXPR_LOCS: RefCell<HashMap<usize, Loc>> = RefCell::new(HashMap::new());
28}
29fn expr_key(e: &Rc<Expr>) -> usize {
30 Rc::as_ptr(e) as *const u8 as usize
31}
32pub fn set_expr_loc(e: &Rc<Expr>, loc: Loc) {
34 EXPR_LOCS.with(|t| t.borrow_mut().insert(expr_key(e), loc));
35}
36pub fn expr_loc(e: &Rc<Expr>) -> Option<Loc> {
38 EXPR_LOCS.with(|t| t.borrow().get(&expr_key(e)).copied())
39}
40
41#[derive(Debug, Clone)]
42pub enum TypeAst {
44 Prim {
46 name: String,
48 loc: Option<Loc>,
50 },
51 Lit {
53 v: Value,
55 loc: Option<Loc>,
57 },
58 Range {
60 lo: Value,
62 hi: Value,
64 excl: bool,
66 loc: Option<Loc>,
68 },
69 Pattern {
71 re: String,
73 loc: Option<Loc>,
75 },
76 Record {
78 members: Vec<MemberAst>,
80 open: bool,
82 loc: Option<Loc>,
84 },
85 Map {
87 key: Box<TypeAst>,
89 val: Box<TypeAst>,
91 loc: Option<Loc>,
93 },
94 Array {
96 elem: Box<TypeAst>,
98 lo: Option<Value>,
100 hi: Option<Value>,
102 excl: bool,
104 loc: Option<Loc>,
106 },
107 Union {
109 arms: Vec<TypeAst>,
111 loc: Option<Loc>,
113 },
114 Isect {
116 arms: Vec<TypeAst>,
118 loc: Option<Loc>,
120 },
121 Func {
123 params: Vec<TypeAst>,
125 ret: Box<TypeAst>,
127 loc: Option<Loc>,
129 },
130 Named {
132 name: String,
134 args: Vec<TypeAst>,
136 preds: Option<Vec<Rc<Expr>>>,
138 ext: Option<Box<TypeAst>>,
140 loc: Option<Loc>,
142 },
143}
144impl TypeAst {
145 pub fn loc(&self) -> Option<Loc> {
147 match self {
148 TypeAst::Prim { loc, .. }
149 | TypeAst::Lit { loc, .. }
150 | TypeAst::Range { loc, .. }
151 | TypeAst::Pattern { loc, .. }
152 | TypeAst::Record { loc, .. }
153 | TypeAst::Map { loc, .. }
154 | TypeAst::Array { loc, .. }
155 | TypeAst::Union { loc, .. }
156 | TypeAst::Isect { loc, .. }
157 | TypeAst::Func { loc, .. }
158 | TypeAst::Named { loc, .. } => *loc,
159 }
160 }
161 pub fn set_loc(&mut self, l: Loc) {
163 match self {
164 TypeAst::Prim { loc, .. }
165 | TypeAst::Lit { loc, .. }
166 | TypeAst::Range { loc, .. }
167 | TypeAst::Pattern { loc, .. }
168 | TypeAst::Record { loc, .. }
169 | TypeAst::Map { loc, .. }
170 | TypeAst::Array { loc, .. }
171 | TypeAst::Union { loc, .. }
172 | TypeAst::Isect { loc, .. }
173 | TypeAst::Func { loc, .. }
174 | TypeAst::Named { loc, .. } => *loc = Some(l),
175 }
176 }
177}
178
179#[derive(Debug, Clone)]
180pub enum MemberAst {
182 Value {
184 name: String,
186 opt: bool,
188 ty: TypeAst,
190 dflt: Option<Rc<Expr>>,
192 annotations: Vec<Annotation>,
194 loc: Option<Loc>,
196 },
197 Derived {
199 name: String,
201 ty: Option<TypeAst>,
203 expr: Rc<Expr>,
205 hidden: bool,
207 annotations: Vec<Annotation>,
209 loc: Option<Loc>,
211 },
212 Context {
214 variable: String,
216 ty: TypeAst,
218 annotations: Vec<Annotation>,
220 loc: Option<Loc>,
222 },
223 Assert {
225 name: String,
227 cond: Rc<Expr>,
229 tail: Option<Tail>,
231 annotations: Vec<Annotation>,
233 loc: Option<Loc>,
235 },
236 When {
238 cond: Rc<Expr>,
240 body: Vec<MemberAst>,
242 annotations: Vec<Annotation>,
244 loc: Option<Loc>,
246 },
247}
248impl MemberAst {
249 pub fn loc(&self) -> Option<Loc> {
251 match self {
252 MemberAst::Value { loc, .. }
253 | MemberAst::Derived { loc, .. }
254 | MemberAst::Context { loc, .. }
255 | MemberAst::Assert { loc, .. }
256 | MemberAst::When { loc, .. } => *loc,
257 }
258 }
259 pub fn set_loc(&mut self, l: Loc) {
261 match self {
262 MemberAst::Value { loc, .. }
263 | MemberAst::Derived { loc, .. }
264 | MemberAst::Context { loc, .. }
265 | MemberAst::Assert { loc, .. }
266 | MemberAst::When { loc, .. } => *loc = Some(l),
267 }
268 }
269 pub fn set_annotations(&mut self, a: Vec<Annotation>) {
271 match self {
272 MemberAst::Value { annotations, .. }
273 | MemberAst::Derived { annotations, .. }
274 | MemberAst::Context { annotations, .. }
275 | MemberAst::Assert { annotations, .. }
276 | MemberAst::When { annotations, .. } => *annotations = a,
277 }
278 }
279 pub fn annotations(&self) -> &[Annotation] {
281 match self {
282 MemberAst::Value { annotations, .. }
283 | MemberAst::Derived { annotations, .. }
284 | MemberAst::Context { annotations, .. }
285 | MemberAst::Assert { annotations, .. }
286 | MemberAst::When { annotations, .. } => annotations,
287 }
288 }
289 pub fn name(&self) -> Option<&str> {
291 match self {
292 MemberAst::Value { name, .. }
293 | MemberAst::Derived { name, .. }
294 | MemberAst::Assert { name, .. } => Some(name),
295 _ => None,
296 }
297 }
298}
299
300#[derive(Debug, Clone)]
301pub enum TPart {
303 Text(String),
305 Expr(Rc<Expr>),
307}
308
309#[derive(Debug, Clone)]
310pub enum Tail {
312 Inline {
314 severity: String,
316 template: Vec<TPart>,
318 },
319 Ref {
321 name: String,
323 args: Vec<Rc<Expr>>,
325 },
326}
327
328#[derive(Debug, Clone)]
329pub struct ForClause {
331 pub v: String,
333 pub iter: Rc<Expr>,
335 pub filters: Vec<Rc<Expr>>,
337}
338
339#[derive(Debug, Clone)]
340pub struct MatchArm {
342 pub v: String,
344 pub ty: Option<TypeAst>,
346 pub body: Rc<Expr>,
348}
349
350#[derive(Debug, Clone)]
351pub enum Expr {
353 Lit(Value),
355 UnitLit {
357 num: f64,
359 unit: String,
361 },
362 Template(Vec<TPart>),
364 Name(String),
366 Ctx(String),
368 Referrers {
370 ty: String,
372 member: String,
374 },
375 Obj(Vec<(String, Rc<Expr>)>),
377 Spread(Rc<Expr>),
379 Arr(Vec<(bool, Rc<Expr>)>),
381 Comp {
383 head: Rc<Expr>,
385 clauses: Vec<ForClause>,
387 },
388 MapComp {
390 key: Rc<Expr>,
392 val: Rc<Expr>,
394 clauses: Vec<ForClause>,
396 },
397 Bin {
399 op: String,
401 l: Rc<Expr>,
403 r: Rc<Expr>,
405 },
406 Un {
408 op: String,
410 x: Rc<Expr>,
412 },
413 Paren(Rc<Expr>),
415 If {
417 c: Rc<Expr>,
419 t: Rc<Expr>,
421 f: Rc<Expr>,
423 },
424 Lambda {
426 params: Vec<String>,
428 body: Rc<Expr>,
430 },
431 Call {
433 fun: Rc<Expr>,
435 args: Vec<Rc<Expr>>,
437 },
438 Member {
440 x: Rc<Expr>,
442 name: String,
444 safe: bool,
446 },
447 Index {
449 x: Rc<Expr>,
451 i: Rc<Expr>,
453 },
454 With {
456 base: Rc<Expr>,
458 patch: Rc<Expr>,
460 },
461 Pattern(String),
463 Match {
465 subject: Rc<Expr>,
467 arms: Vec<MatchArm>,
469 },
470}
471
472#[derive(Debug, Clone)]
473pub struct Annotation {
475 pub name: String,
477 pub args: Vec<Rc<Expr>>,
479 pub loc: Option<Loc>,
481}
482
483#[derive(Debug, Clone)]
484pub struct Param {
486 pub name: String,
488 pub ty: Option<TypeAst>,
490}
491
492#[derive(Debug, Clone)]
493pub struct ImportItem {
495 pub name: String,
497 pub alias: Option<String>,
499}
500
501#[allow(dead_code)]
504#[derive(Debug, Clone)]
505pub enum DeclBody {
507 Type {
509 name: String,
511 params: Vec<Param>,
513 ty: TypeAst,
515 tail: Option<Tail>,
517 },
518 Const {
520 name: String,
522 ty: Option<TypeAst>,
524 expr: Rc<Expr>,
526 },
527 Func {
529 name: String,
531 params: Vec<Param>,
533 ret: Option<TypeAst>,
535 body: Rc<Expr>,
537 },
538 Output {
540 name: String,
542 ty: TypeAst,
544 expr: Rc<Expr>,
546 },
547 Input {
549 name: String,
551 ty: TypeAst,
553 fallback: Option<Rc<Expr>>,
555 },
556 Diagnostic {
558 name: String,
560 params: Vec<Param>,
562 severity: String,
564 template: Vec<TPart>,
566 },
567 Dimension {
569 name: String,
571 terms: Option<Vec<(String, i32)>>,
573 },
574 Unit {
576 name: String,
578 dim: Option<String>,
580 factor: Option<Rc<Expr>>,
582 base: Option<String>,
584 },
585 Import {
587 from: String,
589 names: Option<Vec<ImportItem>>,
591 ns: Option<String>,
593 },
594 ReExport {
596 from: String,
598 names: Vec<ImportItem>,
600 },
601}
602
603#[derive(Debug, Clone)]
604pub struct Decl {
606 pub body: DeclBody,
608 pub exported: bool,
610 pub annotations: Vec<Annotation>,
612 pub loc: Option<Loc>,
614}
615
616impl Decl {
617 pub fn name(&self) -> Option<&str> {
619 match &self.body {
620 DeclBody::Type { name, .. }
621 | DeclBody::Const { name, .. }
622 | DeclBody::Func { name, .. }
623 | DeclBody::Output { name, .. }
624 | DeclBody::Input { name, .. }
625 | DeclBody::Diagnostic { name, .. }
626 | DeclBody::Dimension { name, .. }
627 | DeclBody::Unit { name, .. } => Some(name),
628 _ => None,
629 }
630 }
631}
632
633pub fn mentions_referrers(e: &Expr) -> bool {
635 match e {
636 Expr::Referrers { .. } => true,
637 Expr::Lit(_) | Expr::UnitLit { .. } | Expr::Name(_) | Expr::Ctx(_) | Expr::Pattern(_) => {
638 false
639 }
640 Expr::Template(parts) => parts
641 .iter()
642 .any(|p| matches!(p, TPart::Expr(x) if mentions_referrers(x))),
643 Expr::Obj(es) => es.iter().any(|(_, v)| mentions_referrers(v)),
644 Expr::Spread(x) => mentions_referrers(x),
645 Expr::Arr(items) => items.iter().any(|(_, v)| mentions_referrers(v)),
646 Expr::Comp { head, clauses } => {
647 mentions_referrers(head) || clauses.iter().any(clause_mentions)
648 }
649 Expr::MapComp { key, val, clauses } => {
650 mentions_referrers(key)
651 || mentions_referrers(val)
652 || clauses.iter().any(clause_mentions)
653 }
654 Expr::Bin { l, r, .. } => mentions_referrers(l) || mentions_referrers(r),
655 Expr::Un { x, .. } | Expr::Paren(x) => mentions_referrers(x),
656 Expr::If { c, t, f } => {
657 mentions_referrers(c) || mentions_referrers(t) || mentions_referrers(f)
658 }
659 Expr::Lambda { body, .. } => mentions_referrers(body),
660 Expr::Call { fun, args } => {
661 mentions_referrers(fun) || args.iter().any(|a| mentions_referrers(a))
662 }
663 Expr::Member { x, .. } => mentions_referrers(x),
664 Expr::Index { x, i } => mentions_referrers(x) || mentions_referrers(i),
665 Expr::With { base, patch } => mentions_referrers(base) || mentions_referrers(patch),
666 Expr::Match { subject, arms } => {
667 mentions_referrers(subject) || arms.iter().any(|a| mentions_referrers(&a.body))
668 }
669 }
670}
671
672fn clause_mentions(c: &ForClause) -> bool {
673 mentions_referrers(&c.iter) || c.filters.iter().any(|f| mentions_referrers(f))
674}
675
676pub fn expr_name(e: &Expr) -> String {
678 match e {
679 Expr::Name(n) => n.clone(),
680 Expr::Call { fun, .. } => expr_name(fun),
681 _ => "<predicate>".into(),
682 }
683}
684
685pub(crate) fn walk_expr_tree(e: &Rc<Expr>, into_types: bool, f: &mut dyn FnMut(&Rc<Expr>)) {
690 f(e);
691 let mut go = |x: &Rc<Expr>| walk_expr_tree(x, into_types, f);
692 match &**e {
693 Expr::Lit(_)
694 | Expr::UnitLit { .. }
695 | Expr::Name(_)
696 | Expr::Ctx(_)
697 | Expr::Referrers { .. }
698 | Expr::Pattern(_) => {}
699 Expr::Template(parts) => {
700 for p in parts {
701 if let TPart::Expr(x) = p {
702 go(x);
703 }
704 }
705 }
706 Expr::Obj(entries) => entries.iter().for_each(|(_, v)| go(v)),
707 Expr::Arr(items) => items.iter().for_each(|(_, v)| go(v)),
708 Expr::Comp { head, clauses } => {
709 go(head);
710 for c in clauses {
711 go(&c.iter);
712 c.filters.iter().for_each(&mut go);
713 }
714 }
715 Expr::MapComp { key, val, clauses } => {
716 go(key);
717 go(val);
718 for c in clauses {
719 go(&c.iter);
720 c.filters.iter().for_each(&mut go);
721 }
722 }
723 Expr::Bin { l, r, .. } => {
724 go(l);
725 go(r);
726 }
727 Expr::Un { x, .. } | Expr::Paren(x) | Expr::Spread(x) => go(x),
728 Expr::If { c, t, f: ff } => {
729 go(c);
730 go(t);
731 go(ff);
732 }
733 Expr::Lambda { body, .. } => go(body),
734 Expr::Call { fun, args } => {
735 go(fun);
736 args.iter().for_each(&mut go);
737 }
738 Expr::Member { x, .. } => go(x),
739 Expr::Index { x, i } => {
740 go(x);
741 go(i);
742 }
743 Expr::With { base, patch } => {
744 go(base);
745 go(patch);
746 }
747 Expr::Match { subject, arms } => {
748 go(subject);
749 for a in arms {
750 if into_types {
751 if let Some(t) = &a.ty {
752 walk_type_exprs(t, into_types, f);
753 }
754 }
755 walk_expr_tree(&a.body, into_types, f);
756 }
757 }
758 }
759}
760pub(crate) fn walk_type_exprs(t: &TypeAst, into_types: bool, f: &mut dyn FnMut(&Rc<Expr>)) {
762 match t {
763 TypeAst::Prim { .. }
764 | TypeAst::Lit { .. }
765 | TypeAst::Range { .. }
766 | TypeAst::Pattern { .. } => {}
767 TypeAst::Record { members, .. } => members
768 .iter()
769 .for_each(|m| walk_member_exprs(m, into_types, f)),
770 TypeAst::Map { key, val, .. } => {
771 walk_type_exprs(key, into_types, f);
772 walk_type_exprs(val, into_types, f);
773 }
774 TypeAst::Array { elem, .. } => walk_type_exprs(elem, into_types, f),
775 TypeAst::Union { arms, .. } | TypeAst::Isect { arms, .. } => {
776 arms.iter().for_each(|a| walk_type_exprs(a, into_types, f))
777 }
778 TypeAst::Func { params, ret, .. } => {
779 params
780 .iter()
781 .for_each(|p| walk_type_exprs(p, into_types, f));
782 walk_type_exprs(ret, into_types, f);
783 }
784 TypeAst::Named {
785 args, preds, ext, ..
786 } => {
787 args.iter().for_each(|a| walk_type_exprs(a, into_types, f));
788 if let Some(ps) = preds {
789 ps.iter().for_each(|p| walk_expr_tree(p, into_types, f));
790 }
791 if let Some(x) = ext {
792 walk_type_exprs(x, into_types, f);
793 }
794 }
795 }
796}
797fn walk_member_exprs(m: &MemberAst, into_types: bool, f: &mut dyn FnMut(&Rc<Expr>)) {
798 match m {
799 MemberAst::Value { ty, dflt, .. } => {
800 walk_type_exprs(ty, into_types, f);
801 if let Some(d) = dflt {
802 walk_expr_tree(d, into_types, f);
803 }
804 }
805 MemberAst::Derived { ty, expr, .. } => {
806 if let Some(t) = ty {
807 walk_type_exprs(t, into_types, f);
808 }
809 walk_expr_tree(expr, into_types, f);
810 }
811 MemberAst::Context { ty, .. } => walk_type_exprs(ty, into_types, f),
812 MemberAst::Assert { cond, tail, .. } => {
813 walk_expr_tree(cond, into_types, f);
814 match tail {
815 Some(Tail::Inline { template, .. }) => {
816 for p in template {
817 if let TPart::Expr(x) = p {
818 walk_expr_tree(x, into_types, f);
819 }
820 }
821 }
822 Some(Tail::Ref { args, .. }) => {
823 args.iter().for_each(|a| walk_expr_tree(a, into_types, f))
824 }
825 None => {}
826 }
827 }
828 MemberAst::When { cond, body, .. } => {
829 walk_expr_tree(cond, into_types, f);
830 body.iter()
831 .for_each(|b| walk_member_exprs(b, into_types, f));
832 }
833 }
834}