MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
ast.h
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4#include <memory>
5
6#include <fe/arena.h>
7#include <fe/assert.h>
8#include <fe/cast.h>
9
10#include "mim/driver.h"
11
12#include "mim/ast/tok.h"
13
14namespace mim::ast {
15
16class LamDecl;
17class Module;
18class Scopes;
19class Emitter;
20
21template<class T> using Ptr = fe::Arena::Ptr<const T>;
22template<class T> using Ptrs = std::deque<Ptr<T>>;
23/* */ using Dbgs = std::deque<Dbg>;
24
25struct AnnexInfo {
26 AnnexInfo(Sym sym_plugin, Sym sym_tag, plugin_t id_plugin, tag_t id_tag)
27 : sym{sym_plugin, sym_tag}
28 , id{id_plugin, id_tag, 0, 0} {
29 assert(Annex::mangle(sym_plugin) == id_plugin);
30 }
31
32 bool is_pi() const { return pi && *pi && bool((*pi)->isa<Pi>()); }
33
34 struct {
35 Sym plugin, tag;
36 } sym;
37 struct {
38 plugin_t plugin;
39 tag_t tag;
40 uint8_t curry, trip;
41 } id;
42 std::deque<std::deque<Sym>> subs; ///< List of subs which is a list of aliases.
44 std::optional<const Pi*> pi;
45 bool fresh = true;
46};
47
48class AST {
49public:
50 AST() = default;
52 : world_(&world) {}
53 AST(AST&& other)
54 : AST() {
55 swap(*this, other);
56 }
57 ~AST();
58
59 /// @name Getters
60 ///@{
61 World& world() { return *world_; }
62 Driver& driver() { return world().driver(); }
63 Error& error() { return err_; }
64 const Error& error() const { return err_; }
65 ///@}
66
67 /// @name Sym
68 ///@{
69 Sym sym(const char* s) { return driver().sym(s); }
70 Sym sym(std::string_view s) { return driver().sym(s); }
71 Sym sym(const std::string& s) { return driver().sym(s); }
72 Sym sym_anon() { return sym("_"); } ///< `"_"`.
73 Sym sym_return() { return sym("return"); } ///< `"return"`.
74 Sym sym_error() { return sym("_error_"); } ///< `"_error_"`.
75 ///@}
76
77 template<class T, class... Args> auto ptr(Args&&... args) {
78 return arena_.mk<const T>(std::forward<Args&&>(args)...);
79 }
80
81 /// @name Formatted Output
82 ///@{
83 // clang-format off
84 template<class... Args> Error& error(Loc loc, const char* fmt, Args&&... args) const { return err_.error(loc, fmt, std::forward<Args&&>(args)...); }
85 template<class... Args> Error& warn (Loc loc, const char* fmt, Args&&... args) const { return err_.warn (loc, fmt, std::forward<Args&&>(args)...); }
86 template<class... Args> Error& note (Loc loc, const char* fmt, Args&&... args) const { return err_.note (loc, fmt, std::forward<Args&&>(args)...); }
87 // clang-format on
88 ///@}
89
90 /// @name Manage Annex
91 ///@{
93 const auto& plugin2annexes(Sym plugin) { return plugin2sym2annex_[plugin]; }
94 ///@}
95
96 void bootstrap(Sym plugin, std::ostream& h);
97
98 friend void swap(AST& a1, AST& a2) noexcept {
99 using std::swap;
100 // clang-format off
101 swap(a1.world_, a2.world_);
102 swap(a1.arena_, a2.arena_);
103 swap(a1.err_, a2.err_);
104 // clang-format on
105 }
106
107private:
108 World* world_ = nullptr;
109 fe::Arena arena_;
110 mutable Error err_;
111 absl::node_hash_map<fe::Sym, absl::node_hash_map<fe::Sym, AnnexInfo>> plugin2sym2annex_;
112};
113
114class Node : public fe::RuntimeCast<Node> {
115protected:
117 : loc_(loc) {}
118 virtual ~Node() {}
119
120public:
121 Loc loc() const { return loc_; }
122
123 virtual std::ostream& stream(Tab&, std::ostream&) const = 0;
124 void dump() const;
125
126private:
127 Loc loc_;
128};
129
130class Expr : public Node {
131protected:
133 : Node(loc) {}
134
135public:
136 /// @name Precedence
137 ///@{
138 enum class Prec {
139 Err,
140 Bot,
141 Where,
142 Arrow,
143 Pi,
144 App,
145 Extract,
146 Lit,
147 };
148
149 static constexpr bool is_rassoc(Prec p) { return p == Prec::Arrow; }
150 ///@}
151
152 Ref emit(Emitter&) const;
153 virtual void bind(Scopes&) const = 0;
154 virtual Ref emit_decl(Emitter&, Ref /*type*/) const { fe::unreachable(); }
155 virtual void emit_body(Emitter&, Ref /*decl*/) const { fe::unreachable(); }
156
157private:
158 virtual Ref emit_(Emitter&) const = 0;
159};
160
161class Decl : public Node {
162protected:
164 : Node(loc) {}
165
166public:
167 Ref def() const { return def_; }
168
169protected:
170 mutable Ref def_ = nullptr;
171};
172
173class ValDecl : public Decl {
174protected:
176 : Decl(loc) {}
177
178public:
179 virtual void bind(Scopes&) const = 0;
180 virtual void emit(Emitter&) const = 0;
181};
182
183/*
184 * Ptrn
185 */
186
187class Ptrn : public Decl {
188public:
190 : Decl(loc) {}
191
192 virtual bool implicit() const { return false; }
193
194 virtual void bind(Scopes&, bool rebind, bool quiet) const = 0;
195 virtual Ref emit_value(Emitter&, Ref) const = 0;
196 virtual Ref emit_type(Emitter&) const = 0;
197
198 [[nodiscard]] static Ptr<Expr> to_expr(AST&, Ptr<Ptrn>&&);
199 [[nodiscard]] static Ptr<Ptrn> to_ptrn(Ptr<Expr>&&);
200};
201
202class ErrorPtrn : public Ptrn {
203public:
205 : Ptrn(loc) {}
206
207 void bind(Scopes&, bool rebind, bool quiet) const override;
208 Ref emit_value(Emitter&, Ref) const override;
209 Ref emit_type(Emitter&) const override;
210 std::ostream& stream(Tab&, std::ostream&) const override;
211};
212
213/// `dbg: type`
214class IdPtrn : public Ptrn {
215public:
217 : Ptrn(loc)
218 , dbg_(dbg)
219 , type_(std::move(type)) {}
220
221 Dbg dbg() const { return dbg_; }
222 const Expr* type() const { return type_.get(); }
223
225 auto loc = type->loc();
226 return ast.ptr<IdPtrn>(loc, Dbg(loc, ast.sym_anon()), std::move(type));
227 }
229 auto loc = (type && dbg) ? dbg.loc() + type->loc() : type ? type->loc() : dbg.loc();
230 return ast.ptr<IdPtrn>(loc, dbg, std::move(type));
231 }
232
233 void bind(Scopes&, bool rebind, bool quiet) const override;
234 Ref emit_value(Emitter&, Ref) const override;
235 Ref emit_type(Emitter&) const override;
236 std::ostream& stream(Tab&, std::ostream&) const override;
237
238private:
239 Dbg dbg_;
240 Ptr<Expr> type_;
241};
242
243/// `dbg_0 ... dbg_n-2 id` where `id` = `dbg_n-1: type`
244class GrpPtrn : public Ptrn {
245public:
246 GrpPtrn(Dbg dbg, const IdPtrn* id)
247 : Ptrn(dbg.loc())
248 , dbg_(dbg)
249 , id_(id) {}
250
251 Dbg dbg() const { return dbg_; }
252 const IdPtrn* id() const { return id_; }
253
254 void bind(Scopes&, bool rebind, bool quiet) const override;
255 Ref emit_value(Emitter&, Ref) const override;
256 Ref emit_type(Emitter&) const override;
257 std::ostream& stream(Tab&, std::ostream&) const override;
258
259private:
260 Dbg dbg_;
261 const IdPtrn* id_;
262};
263
264/// `ptrn as id`
265class AliasPtrn : public Ptrn {
266public:
268 : Ptrn(loc)
269 , ptrn_(std::move(ptrn))
270 , dbg_(dbg) {}
271
272 const Ptrn* ptrn() const { return ptrn_.get(); }
273 Dbg dbg() const { return dbg_; }
274 bool implicit() const override { return ptrn()->implicit(); }
275
276 void bind(Scopes&, bool rebind, bool quiet) const override;
277 Ref emit_value(Emitter&, Ref) const override;
278 Ref emit_type(Emitter&) const override;
279 std::ostream& stream(Tab&, std::ostream&) const override;
280
281private:
282 Ptr<Ptrn> ptrn_;
283 Dbg dbg_;
284};
285
286/// `(ptrn_0, ..., ptrn_n-1)`, `[ptrn_0, ..., ptrn_n-1]`, or `{ptrn_0, ..., ptrn_n-1}`
287class TuplePtrn : public Ptrn {
288public:
290 : Ptrn(loc)
291 , delim_l_(delim_l)
292 , ptrns_(std::move(ptrns)) {}
293
294 Tok::Tag delim_l() const { return delim_l_; }
295 Tok::Tag delim_r() const { return Tok::delim_l2r(delim_l()); }
296 bool is_paren() const { return delim_l() == Tok::Tag::D_paren_l; }
297 bool is_brckt() const { return delim_l() == Tok::Tag::D_brckt_l; }
298 bool implicit() const override { return delim_l_ == Tok::Tag::D_brace_l; }
299
300 const auto& ptrns() const { return ptrns_; }
301 const Ptrn* ptrn(size_t i) const { return ptrns_[i].get(); }
302 size_t num_ptrns() const { return ptrns().size(); }
303
304 void bind(Scopes&, bool rebind, bool quiet) const override;
305 Ref emit_value(Emitter&, Ref) const override;
306 Ref emit_type(Emitter&) const override;
307 Ref emit_decl(Emitter&, Ref type) const;
308 Ref emit_body(Emitter&, Ref decl) const;
309 std::ostream& stream(Tab&, std::ostream&) const override;
310
311private:
312 Tok::Tag delim_l_;
313 Ptrs<Ptrn> ptrns_;
314};
315
316/*
317 * Expr
318 */
319
320class ErrorExpr : public Expr {
321public:
323 : Expr(loc) {}
324
325 void bind(Scopes&) const override;
326 std::ostream& stream(Tab&, std::ostream&) const override;
327
328private:
329 Ref emit_(Emitter&) const override;
330};
331
332class InferExpr : public Expr {
333public:
335 : Expr(loc) {}
336
337 void bind(Scopes&) const override;
338 std::ostream& stream(Tab&, std::ostream&) const override;
339
340private:
341 Ref emit_(Emitter&) const override;
342};
343
344/// `sym`
345class IdExpr : public Expr {
346public:
348 : Expr(dbg.loc())
349 , dbg_(dbg) {}
350
351 Dbg dbg() const { return dbg_; }
352 const Decl* decl() const { return decl_; }
353
354 void bind(Scopes&) const override;
355 std::ostream& stream(Tab&, std::ostream&) const override;
356
357private:
358 Ref emit_(Emitter&) const override;
359
360 Dbg dbg_;
361 mutable const Decl* decl_ = nullptr;
362};
363
364/// `tag`
365class PrimaryExpr : public Expr {
366public:
368 : Expr(loc)
369 , tag_(tag) {}
371 : PrimaryExpr(tok.loc(), tok.tag()) {}
372
373 Tok::Tag tag() const { return tag_; }
374
375 void bind(Scopes&) const override;
376 std::ostream& stream(Tab&, std::ostream&) const override;
377
378private:
379 Ref emit_(Emitter&) const override;
380
381 Tok::Tag tag_;
382};
383
384/// `tok:type`
385class LitExpr : public Expr {
386public:
388 : Expr(loc)
389 , tok_(tok)
390 , type_(std::move(type)) {}
391
392 Tok tok() const { return tok_; }
393 Tok::Tag tag() const { return tok_.tag(); }
394 const Expr* type() const { return type_.get(); }
395
396 void bind(Scopes&) const override;
397 std::ostream& stream(Tab&, std::ostream&) const override;
398
399private:
400 Ref emit_(Emitter&) const override;
401
402 Tok tok_;
403 Ptr<Expr> type_;
404};
405
406/// `decls e` or `e where decls` if @p where is `true`.
407class DeclExpr : public Expr {
408public:
410 : Expr(loc)
411 , decls_(std::move(decls))
412 , expr_(std::move(expr))
413 , is_where_(is_where) {}
414
415 const auto& decls() const { return decls_; }
416 bool is_where() const { return is_where_; }
417 const Expr* expr() const { return expr_.get(); }
418
419 void bind(Scopes&) const override;
420 std::ostream& stream(Tab&, std::ostream&) const override;
421
422private:
423 Ref emit_(Emitter&) const override;
424
425 Ptrs<ValDecl> decls_;
426 Ptr<Expr> expr_;
427 bool is_where_;
428};
429
430/// `Type level`
431class TypeExpr : public Expr {
432public:
434 : Expr(loc)
435 , level_(std::move(level)) {}
436
437 const Expr* level() const { return level_.get(); }
438
439 void bind(Scopes&) const override;
440 std::ostream& stream(Tab&, std::ostream&) const override;
441
442private:
443 Ref emit_(Emitter&) const override;
444
445 Ptr<Expr> level_;
446};
447
448// lam
449
450/// `dom -> codom`
451class ArrowExpr : public Expr {
452public:
453 ArrowExpr(Loc loc, Ptr<Expr>&& dom, Ptr<Expr>&& codom)
454 : Expr(loc)
455 , dom_(std::move(dom))
456 , codom_(std::move(codom)) {}
457
458private:
459 const Expr* dom() const { return dom_.get(); }
460 const Expr* codom() const { return codom_.get(); }
461
462 void bind(Scopes&) const override;
463 Ref emit_decl(Emitter&, Ref type) const override;
464 void emit_body(Emitter&, Ref decl) const override;
465 std::ostream& stream(Tab&, std::ostream&) const override;
466
467private:
468 Ref emit_(Emitter&) const override;
469
470 Ptr<Expr> dom_;
471 Ptr<Expr> codom_;
472 mutable Pi* decl_ = nullptr;
473};
474
475/// One of:
476/// * ` dom_0 ... dom_n-1 -> codom`
477/// * `Cn dom_0 ... dom_n-1`
478/// * `Fn dom_0 ... dom_n-1 -> codom`
479class PiExpr : public Expr {
480public:
481 class Dom : public Node {
482 public:
484 : Node(loc)
485 , ptrn_(std::move(ptrn)) {}
486
487 bool implicit() const { return ptrn_->implicit(); }
488 const Ptrn* ptrn() const { return ptrn_.get(); }
489 const IdPtrn* ret() const { return ret_.get(); }
490
491 void add_ret(AST& ast, Ptr<Expr>&& type) const {
492 auto loc = type->loc();
493 ret_ = ast.ptr<IdPtrn>(loc, Dbg(loc, ast.sym_return()), std::move(type));
494 }
495
496 virtual void bind(Scopes& scopes, bool quiet = false) const;
497 virtual void emit_type(Emitter&) const;
498 std::ostream& stream(Tab&, std::ostream&) const override;
499
500 protected:
501 mutable Pi* pi_ = nullptr;
502 mutable Pi* decl_ = nullptr;
503
504 private:
505 Ptr<Ptrn> ptrn_;
506 mutable Ptr<IdPtrn> ret_;
507
508 friend class PiExpr;
509 };
510
511 PiExpr(Loc loc, Tok::Tag tag, Ptrs<Dom>&& doms, Ptr<Expr>&& codom)
512 : Expr(loc)
513 , tag_(tag)
514 , doms_(std::move(doms))
515 , codom_(std::move(codom)) {
516 assert(num_doms() != 0);
517 }
518
519private:
520 Tok::Tag tag() const { return tag_; }
521 const Ptrs<Dom>& doms() const { return doms_; }
522 const Dom* dom(size_t i) const { return doms_[i].get(); }
523 size_t num_doms() const { return doms_.size(); }
524 const Expr* codom() const { return codom_.get(); }
525
526 void bind(Scopes&) const override;
527 Ref emit_decl(Emitter&, Ref type) const override;
528 void emit_body(Emitter&, Ref decl) const override;
529 std::ostream& stream(Tab&, std::ostream&) const override;
530
531private:
532 Ref emit_(Emitter&) const override;
533
534 Tok::Tag tag_;
535 mutable Ptrs<Dom> doms_;
536 Ptr<Expr> codom_;
537};
538
539/// Wraps a LamDecl as Expr.
540class LamExpr : public Expr {
541public:
543
544 const LamDecl* lam() const { return lam_.get(); }
545
546 void bind(Scopes&) const override;
547 Ref emit_decl(Emitter&, Ref type) const override;
548 void emit_body(Emitter&, Ref decl) const override;
549 std::ostream& stream(Tab&, std::ostream&) const override;
550
551private:
552 Ref emit_(Emitter&) const override;
553
554 Ptr<LamDecl> lam_;
555};
556
557/// `callee arg`
558class AppExpr : public Expr {
559public:
561 : Expr(loc)
562 , is_explicit_(is_explicit)
563 , callee_(std::move(callee))
564 , arg_(std::move(arg)) {}
565
566 bool is_explicit() const { return is_explicit_; }
567 const Expr* callee() const { return callee_.get(); }
568 const Expr* arg() const { return arg_.get(); }
569
570 void bind(Scopes&) const override;
571 std::ostream& stream(Tab&, std::ostream&) const override;
572
573private:
574 Ref emit_(Emitter&) const override;
575
576 bool is_explicit_;
577 Ptr<Expr> callee_;
578 Ptr<Expr> arg_;
579};
580
581/// `ret ptrn = callee $ arg; body`
582class RetExpr : public Expr {
583public:
585 : Expr(loc)
586 , ptrn_(std::move(ptrn))
587 , callee_(std::move(callee))
588 , arg_(std::move(arg))
589 , body_(std::move(body)) {}
590
591 const Ptrn* ptrn() const { return ptrn_.get(); }
592 const Expr* callee() const { return callee_.get(); }
593 const Expr* arg() const { return arg_.get(); }
594 const Expr* body() const { return body_.get(); }
595
596 void bind(Scopes&) const override;
597 std::ostream& stream(Tab&, std::ostream&) const override;
598
599private:
600 Ref emit_(Emitter&) const override;
601
602 Ptr<Ptrn> ptrn_;
603 Ptr<Expr> callee_;
604 Ptr<Expr> arg_;
605 Ptr<Expr> body_;
606};
607// tuple
608
609/// Just wraps TuplePtrn as Expr.
610class SigmaExpr : public Expr {
611public:
613 : Expr(ptrn->loc())
614 , ptrn_(std::move(ptrn)) {}
615
616 const TuplePtrn* ptrn() const { return ptrn_.get(); }
617
618 void bind(Scopes&) const override;
619 Ref emit_decl(Emitter&, Ref type) const override;
620 void emit_body(Emitter&, Ref decl) const override;
621 std::ostream& stream(Tab&, std::ostream&) const override;
622
623private:
624 Ref emit_(Emitter&) const override;
625
626 Ptr<TuplePtrn> ptrn_;
627
629};
630
631/// `(elem_0, ..., elem_n-1)`
632class TupleExpr : public Expr {
633public:
635 : Expr(loc)
636 , elems_(std::move(elems)) {}
637
638 const auto& elems() const { return elems_; }
639 const Expr* elem(size_t i) const { return elems_[i].get(); }
640 size_t num_elems() const { return elems().size(); }
641
642 void bind(Scopes&) const override;
643 std::ostream& stream(Tab&, std::ostream&) const override;
644
645private:
646 Ref emit_(Emitter&) const override;
647
648 Ptrs<Expr> elems_;
649};
650
651/// `«dbg: shape; body»` or `‹dbg: shape; body›`
652template<bool arr> class ArrOrPackExpr : public Expr {
653public:
655 : Expr(loc)
656 , shape_(std::move(shape))
657 , body_(std::move(body)) {}
658
659 const IdPtrn* shape() const { return shape_.get(); }
660 const Expr* body() const { return body_.get(); }
661
662 void bind(Scopes&) const override;
663 std::ostream& stream(Tab&, std::ostream&) const override;
664
665private:
666 Ref emit_(Emitter&) const override;
667
668 Ptr<IdPtrn> shape_;
669 Ptr<Expr> body_;
670};
671
674
675/// `tuple#index`
676class ExtractExpr : public Expr {
677public:
679 : Expr(loc)
680 , tuple_(std::move(tuple))
681 , index_(std::move(index)) {}
683 : Expr(loc)
684 , tuple_(std::move(tuple))
685 , index_(index) {}
686
687 const Expr* tuple() const { return tuple_.get(); }
688 const auto& index() const { return index_; }
689 const Decl* decl() const { return decl_; }
690
691 void bind(Scopes&) const override;
692 std::ostream& stream(Tab&, std::ostream&) const override;
693
694private:
695 Ref emit_(Emitter&) const override;
696
697 Ptr<Expr> tuple_;
698 std::variant<Ptr<Expr>, Dbg> index_;
699 mutable const Decl* decl_ = nullptr;
700};
701
702/// `ins(tuple, index, value)`
703class InsertExpr : public Expr {
704public:
706 : Expr(loc)
707 , tuple_(std::move(tuple))
708 , index_(std::move(index))
709 , value_(std::move(value)) {}
710
711 const Expr* tuple() const { return tuple_.get(); }
712 const Expr* index() const { return index_.get(); }
713 const Expr* value() const { return value_.get(); }
714
715 void bind(Scopes&) const override;
716 std::ostream& stream(Tab&, std::ostream&) const override;
717
718private:
719 Ref emit_(Emitter&) const override;
720
721 Ptr<Expr> tuple_;
722 Ptr<Expr> index_;
723 Ptr<Expr> value_;
724};
725
726/*
727 * Decls
728 */
729
730/// `let ptrn: type = value;`
731class LetDecl : public ValDecl {
732public:
734 : ValDecl(loc)
735 , ptrn_(std::move(ptrn))
736 , value_(std::move(value)) {}
737
738 const Ptrn* ptrn() const { return ptrn_.get(); }
739 const Expr* value() const { return value_.get(); }
740
741 void bind(Scopes&) const override;
742 void emit(Emitter&) const override;
743 std::ostream& stream(Tab&, std::ostream&) const override;
744
745private:
746 Ptr<Ptrn> ptrn_;
747 Ptr<Expr> value_;
748 mutable AnnexInfo* annex_ = nullptr;
749 mutable sub_t sub_;
750};
751
752/// `axm ptrn: type = value;`
753class AxiomDecl : public ValDecl {
754public:
755 class Alias : public Decl {
756 public:
758 : Decl(dbg.loc())
759 , dbg_(dbg) {}
760
761 Dbg dbg() const { return dbg_; }
762
763 void bind(Scopes&, const AxiomDecl*) const;
764 std::ostream& stream(Tab&, std::ostream&) const override;
765
766 private:
767 Dbg dbg_;
768 mutable Dbg full_;
769
770 friend class AxiomDecl;
771 };
772
774 : ValDecl(loc)
775 , dbg_(dbg)
776 , subs_(std::move(subs))
777 , type_(std::move(type))
778 , normalizer_(normalizer)
779 , curry_(curry)
780 , trip_(trip) {}
781
782 Dbg dbg() const { return dbg_; }
783 const auto& subs() const { return subs_; }
784 size_t num_subs() const { return subs_.size(); }
785 const auto& sub(size_t i) const { return subs_[i]; }
786 const Expr* type() const { return type_.get(); }
787 Dbg normalizer() const { return normalizer_; }
788 Tok curry() const { return curry_; }
789 Tok trip() const { return trip_; }
790
791 void bind(Scopes&) const override;
792 void emit(Emitter&) const override;
793 std::ostream& stream(Tab&, std::ostream&) const override;
794
795private:
796 Dbg dbg_;
797 std::deque<Ptrs<Alias>> subs_;
798 Ptr<Expr> type_;
799 Dbg normalizer_;
800 Tok curry_, trip_;
801 mutable AnnexInfo* annex_ = nullptr;
802 mutable Ref mim_type_;
803};
804
805/// `.rec dbg: type = body`
806class RecDecl : public ValDecl {
807public:
809 : ValDecl(loc)
810 , dbg_(dbg)
811 , type_(std::move(type))
812 , body_(std::move(body))
813 , next_(std::move(next)) {}
814
815 Dbg dbg() const { return dbg_; }
816 const Expr* type() const { return type_.get(); }
817 const Expr* body() const { return body_.get(); }
818 const RecDecl* next() const { return next_.get(); }
819
820 void bind(Scopes&) const override;
821 virtual void bind_decl(Scopes&) const;
822 virtual void bind_body(Scopes&) const;
823
824 void emit(Emitter&) const override;
825 virtual void emit_decl(Emitter&) const;
826 virtual void emit_body(Emitter&) const;
827
828 std::ostream& stream(Tab&, std::ostream&) const override;
829
830private:
831 Dbg dbg_;
832 Ptr<Expr> type_;
833 Ptr<Expr> body_;
834 Ptr<RecDecl> next_;
835 mutable AnnexInfo* annex_ = nullptr;
836 mutable sub_t sub_;
837};
838
839/// One of:
840/// * `λ dom_0 ... dom_n-1 -> codom`
841/// * `cn dom_0 ... dom_n-1`
842/// * `fn dom_0 ... dom_n-1 -> codom`
843/// * `lam dbg dom_0 ... dom_n-1 -> codom`
844/// * `con dbg dom_0 ... dom_n-1`
845/// * `fun dbg dom_0 ... dom_n-1 -> codom`
846class LamDecl : public RecDecl {
847public:
848 class Dom : public PiExpr::Dom {
849 public:
851 : PiExpr::Dom(loc, std::move(ptrn))
852 , filter_(std::move(filter)) {}
853
854 bool implicit() const { return ptrn()->implicit(); }
855 const Expr* filter() const { return filter_.get(); }
856
857 void bind(Scopes& scopes, bool quiet = false) const override;
858 Lam* emit_value(Emitter&) const;
859 std::ostream& stream(Tab&, std::ostream&) const override;
860
861 private:
862 Ptr<Expr> filter_;
863 mutable Lam* lam_;
864
865 friend class LamDecl;
866 };
867
870 bool is_external,
871 Dbg dbg,
872 Ptrs<Dom>&& doms,
874 Ptr<Expr>&& body,
876 : RecDecl(loc, dbg, nullptr, std::move(body), std::move(next))
877 , tag_(tag)
878 , is_external_(is_external)
879 , doms_(std::move(doms))
880 , codom_(std::move(codom)) {
881 assert(num_doms() != 0);
882 }
883
884 Tok::Tag tag() const { return tag_; }
885 bool is_external() const { return is_external_; }
886 const Ptrs<Dom>& doms() const { return doms_; }
887 const Dom* dom(size_t i) const { return doms_[i].get(); }
888 size_t num_doms() const { return doms_.size(); }
889 const Expr* codom() const { return codom_.get(); }
890
891 void bind_decl(Scopes&) const override;
892 void bind_body(Scopes&) const override;
893 void emit_decl(Emitter&) const override;
894 void emit_body(Emitter&) const override;
895 std::ostream& stream(Tab&, std::ostream&) const override;
896
897private:
898 Tok::Tag tag_;
899 bool is_external_;
900 Ptrs<Dom> doms_;
901 Ptr<Expr> codom_;
902 mutable AnnexInfo* annex_ = nullptr;
903 mutable sub_t sub_;
904};
905
906/// `cfun dbg dom -> codom`
907class CDecl : public ValDecl {
908public:
910 : ValDecl(loc)
911 , tag_(tag)
912 , dbg_(dbg)
913 , dom_(std::move(dom))
914 , codom_(std::move(codom)) {}
915
916 Dbg dbg() const { return dbg_; }
917 Tok::Tag tag() const { return tag_; }
918 const Ptrn* dom() const { return dom_.get(); }
919 const Expr* codom() const { return codom_.get(); }
920
921 void bind(Scopes&) const override;
922 void emit(Emitter&) const override;
923 std::ostream& stream(Tab&, std::ostream&) const override;
924
925private:
926 Tok::Tag tag_;
927 Dbg dbg_;
928 Ptr<Ptrn> dom_;
929 Ptr<Expr> codom_;
930};
931
932/*
933 * Module
934 */
935
936class Import : public Node {
937public:
939 : Node(loc)
940 , dbg_(dbg)
941 , tag_(tag)
942 , module_(std::move(module)) {}
943
944 Dbg dbg() const { return dbg_; }
945 Tok::Tag tag() const { return tag_; }
946 const Module* module() const { return module_.get(); }
947
948 void bind(Scopes&) const;
949 void emit(Emitter&) const;
950 std::ostream& stream(Tab&, std::ostream&) const;
951
952 friend void swap(Import& i1, Import& i2) noexcept {
953 using std::swap;
954 swap(i1.dbg_, i2.dbg_);
955 swap(i1.tag_, i2.tag_);
956 swap(i1.module_, i2.module_);
957 }
958
959private:
960 Dbg dbg_;
961 Tok::Tag tag_;
962 Ptr<Module> module_;
963};
964
965class Module : public Node {
966public:
968 : Node(loc)
969 , imports_(std::move(imports))
970 , decls_(std::move(decls)) {}
971
972 const auto& implicit_imports() const { return implicit_imports_; }
973 const auto& imports() const { return imports_; }
974 const auto& decls() const { return decls_; }
975
976 void add_implicit_imports(Ptrs<Import>&& imports) const { implicit_imports_ = std::move(imports); }
977
978 void compile(AST&) const;
979 void bind(AST&) const;
980 void bind(Scopes&) const;
981 void emit(AST&) const;
982 void emit(Emitter&) const;
983 std::ostream& stream(Tab&, std::ostream&) const override;
984
985private:
986 mutable Ptrs<Import> implicit_imports_;
987 Ptrs<Import> imports_;
988 Ptrs<ValDecl> decls_;
989};
990
993 return load_plugins(w, Vector<Sym>(plugins.size(), [&](size_t i) { return w.sym(plugins[i]); }));
994}
995inline AST load_plugins(World& w, Sym sym) { return load_plugins(w, View<Sym>({sym})); }
996inline AST load_plugins(World& w, const std::string& plugin) { return load_plugins(w, w.sym(plugin)); }
997
998} // namespace mim::ast
Some "global" variables needed all over the place.
Definition driver.h:17
Error & error(Loc loc, const char *s, Args &&... args)
Definition dbg.h:71
Error & note(Loc loc, const char *s, Args &&... args)
Definition dbg.h:73
Error & warn(Loc loc, const char *s, Args &&... args)
Definition dbg.h:72
A function.
Definition lam.h:103
A dependent function type.
Definition lam.h:11
Helper class to retrieve Infer::arg if present.
Definition def.h:86
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
Keeps track of indentation level.
Definition print.h:195
This is a thin wrapper for absl::InlinedVector<T, N, / A> which in turn is a drop-in replacement for ...
Definition vector.h:16
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
const Driver & driver() const
Definition world.h:81
AST(AST &&other)
Definition ast.h:53
World & world()
Definition ast.h:61
friend void swap(AST &a1, AST &a2) noexcept
Definition ast.h:98
Driver & driver()
Definition ast.h:62
auto ptr(Args &&... args)
Definition ast.h:77
AST()=default
Error & error(Loc loc, const char *fmt, Args &&... args) const
Definition ast.h:84
AnnexInfo * name2annex(Dbg dbg, sub_t *)
Definition ast.cpp:14
Sym sym(std::string_view s)
Definition ast.h:70
const auto & plugin2annexes(Sym plugin)
Definition ast.h:93
void bootstrap(Sym plugin, std::ostream &h)
Definition ast.cpp:52
const Error & error() const
Definition ast.h:64
Sym sym_return()
"return".
Definition ast.h:73
Sym sym_anon()
"_".
Definition ast.h:72
Error & note(Loc loc, const char *fmt, Args &&... args) const
Definition ast.h:86
Error & error()
Definition ast.h:63
Sym sym(const char *s)
Definition ast.h:69
Error & warn(Loc loc, const char *fmt, Args &&... args) const
Definition ast.h:85
Sym sym_error()
"_error_".
Definition ast.h:74
Sym sym(const std::string &s)
Definition ast.h:71
AST(World &world)
Definition ast.h:51
ptrn as id
Definition ast.h:265
Dbg dbg() const
Definition ast.h:273
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:54
Ref emit_type(Emitter &) const override
Definition emit.cpp:82
const Ptrn * ptrn() const
Definition ast.h:272
Ref emit_value(Emitter &, Ref) const override
Definition emit.cpp:62
AliasPtrn(Loc loc, Ptr< Ptrn > &&ptrn, Dbg dbg)
Definition ast.h:267
void bind(Scopes &, bool rebind, bool quiet) const override
Definition bind.cpp:102
bool implicit() const override
Definition ast.h:274
callee arg
Definition ast.h:558
AppExpr(Loc loc, bool is_explicit, Ptr< Expr > &&callee, Ptr< Expr > &&arg)
Definition ast.h:560
void bind(Scopes &) const override
Definition bind.cpp:166
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:126
bool is_explicit() const
Definition ast.h:566
const Expr * arg() const
Definition ast.h:568
const Expr * callee() const
Definition ast.h:567
Ref emit_(Emitter &) const override
Definition emit.cpp:254
«dbg: shape; body» or ‹dbg: shape; body›
Definition ast.h:652
const Expr * body() const
Definition ast.h:660
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:138
ArrOrPackExpr(Loc loc, Ptr< IdPtrn > &&shape, Ptr< Expr > &&body)
Definition ast.h:654
const IdPtrn * shape() const
Definition ast.h:659
Ref emit_(Emitter &) const override
Definition emit.cpp:284
void bind(Scopes &) const override
Definition bind.cpp:188
dom -> codom
Definition ast.h:451
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:108
Ref emit_(Emitter &) const override
Definition emit.cpp:197
void emit_body(Emitter &, Ref decl) const override
Definition emit.cpp:192
void bind(Scopes &) const override
Definition bind.cpp:141
Ref emit_decl(Emitter &, Ref type) const override
Definition emit.cpp:190
ArrowExpr(Loc loc, Ptr< Expr > &&dom, Ptr< Expr > &&codom)
Definition ast.h:453
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:158
void bind(Scopes &, const AxiomDecl *) const
Definition bind.cpp:218
axm ptrn: type = value;
Definition ast.h:753
const auto & subs() const
Definition ast.h:783
Tok curry() const
Definition ast.h:788
size_t num_subs() const
Definition ast.h:784
AxiomDecl(Loc loc, Dbg dbg, std::deque< Ptrs< Alias > > &&subs, Ptr< Expr > &&type, Dbg normalizer, Tok curry, Tok trip)
Definition ast.h:773
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:160
void bind(Scopes &) const override
Definition bind.cpp:224
const Expr * type() const
Definition ast.h:786
void emit(Emitter &) const override
Definition emit.cpp:348
Dbg dbg() const
Definition ast.h:782
Dbg normalizer() const
Definition ast.h:787
Tok trip() const
Definition ast.h:789
const auto & sub(size_t i) const
Definition ast.h:785
cfun dbg dom -> codom
Definition ast.h:907
Tok::Tag tag() const
Definition ast.h:917
CDecl(Loc loc, Tok::Tag tag, Dbg dbg, Ptr< Ptrn > &&dom, Ptr< Expr > &&codom)
Definition ast.h:909
const Ptrn * dom() const
Definition ast.h:918
void emit(Emitter &) const override
Definition emit.cpp:467
const Expr * codom() const
Definition ast.h:919
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:211
Dbg dbg() const
Definition ast.h:916
void bind(Scopes &) const override
Definition bind.cpp:325
decls e or e where decls if where is true.
Definition ast.h:407
const Expr * expr() const
Definition ast.h:417
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:93
const auto & decls() const
Definition ast.h:415
Ref emit_(Emitter &) const override
Definition emit.cpp:182
void bind(Scopes &) const override
Definition bind.cpp:133
bool is_where() const
Definition ast.h:416
DeclExpr(Loc loc, Ptrs< ValDecl > &&decls, Ptr< Expr > &&expr, bool is_where)
Definition ast.h:409
Decl(Loc loc)
Definition ast.h:163
Ref def() const
Definition ast.h:167
Ref emit_(Emitter &) const override
Definition emit.cpp:126
void bind(Scopes &) const override
Definition bind.cpp:118
ErrorExpr(Loc loc)
Definition ast.h:322
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:75
Ref emit_value(Emitter &, Ref) const override
Definition emit.cpp:53
void bind(Scopes &, bool rebind, bool quiet) const override
Definition bind.cpp:94
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:53
ErrorPtrn(Loc loc)
Definition ast.h:204
Ref emit_type(Emitter &) const override
Definition emit.cpp:75
virtual void emit_body(Emitter &, Ref) const
Definition ast.h:155
virtual void bind(Scopes &) const =0
Expr(Loc loc)
Definition ast.h:132
virtual Ref emit_decl(Emitter &, Ref) const
Definition ast.h:154
virtual Ref emit_(Emitter &) const =0
Ref emit(Emitter &) const
Definition emit.cpp:121
static constexpr bool is_rassoc(Prec p)
Definition ast.h:149
tuple#index
Definition ast.h:676
const Decl * decl() const
Definition ast.h:689
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:145
ExtractExpr(Loc loc, Ptr< Expr > &&tuple, Ptr< Expr > &&index)
Definition ast.h:678
const auto & index() const
Definition ast.h:688
const Expr * tuple() const
Definition ast.h:687
void bind(Scopes &) const override
Definition bind.cpp:198
ExtractExpr(Loc loc, Ptr< Expr > &&tuple, Dbg index)
Definition ast.h:682
Ref emit_(Emitter &) const override
Definition emit.cpp:316
dbg_0 ... dbg_n-2 id where id = dbg_n-1: type
Definition ast.h:244
GrpPtrn(Dbg dbg, const IdPtrn *id)
Definition ast.h:246
Ref emit_value(Emitter &, Ref) const override
Definition emit.cpp:60
void bind(Scopes &, bool rebind, bool quiet) const override
Definition bind.cpp:95
Dbg dbg() const
Definition ast.h:251
Ref emit_type(Emitter &) const override
Definition emit.cpp:84
const IdPtrn * id() const
Definition ast.h:252
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:55
Dbg dbg() const
Definition ast.h:351
IdExpr(Dbg dbg)
Definition ast.h:347
void bind(Scopes &) const override
Definition bind.cpp:116
const Decl * decl() const
Definition ast.h:352
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:74
Ref emit_(Emitter &) const override
Definition emit.cpp:129
dbg: type
Definition ast.h:214
static Ptr< IdPtrn > mk_id(AST &ast, Dbg dbg, Ptr< Expr > &&type)
Definition ast.h:228
void bind(Scopes &, bool rebind, bool quiet) const override
Definition bind.cpp:97
static Ptr< IdPtrn > mk_type(AST &ast, Ptr< Expr > &&type)
Definition ast.h:224
IdPtrn(Loc loc, Dbg dbg, Ptr< Expr > &&type)
Definition ast.h:216
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:57
Ref emit_type(Emitter &) const override
Definition emit.cpp:77
Dbg dbg() const
Definition ast.h:221
const Expr * type() const
Definition ast.h:222
Ref emit_value(Emitter &, Ref) const override
Definition emit.cpp:55
friend void swap(Import &i1, Import &i2) noexcept
Definition ast.h:952
Import(Loc loc, Tok::Tag tag, Dbg dbg, Ptr< Module > &&module)
Definition ast.h:938
void bind(Scopes &) const
Definition bind.cpp:88
Dbg dbg() const
Definition ast.h:944
void emit(Emitter &) const
Definition emit.cpp:47
Tok::Tag tag() const
Definition ast.h:945
const Module * module() const
Definition ast.h:946
std::ostream & stream(Tab &, std::ostream &) const
Definition stream.cpp:41
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:76
InferExpr(Loc loc)
Definition ast.h:334
Ref emit_(Emitter &) const override
Definition emit.cpp:127
void bind(Scopes &) const override
Definition bind.cpp:119
ins(tuple, index, value)
Definition ast.h:703
Ref emit_(Emitter &) const override
Definition emit.cpp:337
InsertExpr(Loc loc, Ptr< Expr > &&tuple, Ptr< Expr > &&index, Ptr< Expr > &&value)
Definition ast.h:705
const Expr * index() const
Definition ast.h:712
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:150
void bind(Scopes &) const override
Definition bind.cpp:208
const Expr * tuple() const
Definition ast.h:711
const Expr * value() const
Definition ast.h:713
Lam * emit_value(Emitter &) const
Definition emit.cpp:419
const Expr * filter() const
Definition ast.h:855
bool implicit() const
Definition ast.h:854
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:187
Dom(Loc loc, Ptr< Ptrn > &&ptrn, Ptr< Expr > &&filter)
Definition ast.h:850
void bind(Scopes &scopes, bool quiet=false) const override
Definition bind.cpp:283
One of:
Definition ast.h:846
Tok::Tag tag() const
Definition ast.h:884
void bind_body(Scopes &) const override
Definition bind.cpp:318
bool is_external() const
Definition ast.h:885
const Ptrs< Dom > & doms() const
Definition ast.h:886
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:194
const Expr * codom() const
Definition ast.h:889
void emit_decl(Emitter &) const override
Definition emit.cpp:433
size_t num_doms() const
Definition ast.h:888
void emit_body(Emitter &) const override
Definition emit.cpp:461
void bind_decl(Scopes &) const override
Definition bind.cpp:288
LamDecl(Loc loc, Tok::Tag tag, bool is_external, Dbg dbg, Ptrs< Dom > &&doms, Ptr< Expr > &&codom, Ptr< Expr > &&body, Ptr< RecDecl > &&next)
Definition ast.h:868
const Dom * dom(size_t i) const
Definition ast.h:887
Wraps a LamDecl as Expr.
Definition ast.h:540
Ref emit_decl(Emitter &, Ref type) const override
Definition emit.cpp:245
void emit_body(Emitter &, Ref decl) const override
Definition emit.cpp:246
void bind(Scopes &) const override
Definition bind.cpp:161
Ref emit_(Emitter &) const override
Definition emit.cpp:248
LamExpr(Ptr< LamDecl > &&lam)
Definition ast.cpp:159
const LamDecl * lam() const
Definition ast.h:544
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:124
let ptrn: type = value;
Definition ast.h:731
void bind(Scopes &) const override
Definition bind.cpp:255
const Expr * value() const
Definition ast.h:739
const Ptrn * ptrn() const
Definition ast.h:738
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:177
LetDecl(Loc loc, Ptr< Ptrn > &&ptrn, Ptr< Expr > &&value)
Definition ast.h:733
void emit(Emitter &) const override
Definition emit.cpp:396
tok:type
Definition ast.h:385
Tok tok() const
Definition ast.h:392
const Expr * type() const
Definition ast.h:394
LitExpr(Loc loc, Tok tok, Ptr< Expr > &&type)
Definition ast.h:387
Tok::Tag tag() const
Definition ast.h:393
void bind(Scopes &) const override
Definition bind.cpp:123
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:79
Ref emit_(Emitter &) const override
Definition emit.cpp:165
Module(Loc loc, Ptrs< Import > &&imports, Ptrs< ValDecl > &&decls)
Definition ast.h:967
const auto & decls() const
Definition ast.h:974
void emit(AST &) const
Definition emit.cpp:35
const auto & implicit_imports() const
Definition ast.h:972
void bind(AST &) const
Definition bind.cpp:77
const auto & imports() const
Definition ast.h:973
void compile(AST &) const
Definition ast.cpp:183
void add_implicit_imports(Ptrs< Import > &&imports) const
Definition ast.h:976
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:43
virtual std::ostream & stream(Tab &, std::ostream &) const =0
void dump() const
Definition stream.cpp:32
virtual ~Node()
Definition ast.h:118
Loc loc() const
Definition ast.h:121
Node(Loc loc)
Definition ast.h:116
virtual void emit_type(Emitter &) const
Definition emit.cpp:203
Dom(Loc loc, Ptr< Ptrn > &&ptrn)
Definition ast.h:483
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:112
virtual void bind(Scopes &scopes, bool quiet=false) const
Definition bind.cpp:146
bool implicit() const
Definition ast.h:487
const IdPtrn * ret() const
Definition ast.h:489
const Ptrn * ptrn() const
Definition ast.h:488
void add_ret(AST &ast, Ptr< Expr > &&type) const
Definition ast.h:491
One of:
Definition ast.h:479
void emit_body(Emitter &, Ref decl) const override
Definition emit.cpp:231
Ref emit_(Emitter &) const override
Definition emit.cpp:233
Ref emit_decl(Emitter &, Ref type) const override
Definition emit.cpp:226
void bind(Scopes &) const override
Definition bind.cpp:151
PiExpr(Loc loc, Tok::Tag tag, Ptrs< Dom > &&doms, Ptr< Expr > &&codom)
Definition ast.h:511
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:118
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:77
Tok::Tag tag() const
Definition ast.h:373
void bind(Scopes &) const override
Definition bind.cpp:120
PrimaryExpr(Loc loc, Tok::Tag tag)
Definition ast.h:367
Ref emit_(Emitter &) const override
Definition emit.cpp:139
PrimaryExpr(Tok tok)
Definition ast.h:370
virtual Ref emit_value(Emitter &, Ref) const =0
virtual void bind(Scopes &, bool rebind, bool quiet) const =0
static Ptr< Ptrn > to_ptrn(Ptr< Expr > &&)
Definition ast.cpp:177
virtual Ref emit_type(Emitter &) const =0
static Ptr< Expr > to_expr(AST &, Ptr< Ptrn > &&)
Definition ast.cpp:167
Ptrn(Loc loc)
Definition ast.h:189
virtual bool implicit() const
Definition ast.h:192
.rec dbg: type = body
Definition ast.h:806
void emit(Emitter &) const override
Definition emit.cpp:402
Dbg dbg() const
Definition ast.h:815
virtual void emit_body(Emitter &) const
Definition emit.cpp:413
virtual void bind_body(Scopes &) const
Definition bind.cpp:281
virtual void emit_decl(Emitter &) const
Definition emit.cpp:407
RecDecl(Loc loc, Dbg dbg, Ptr< Expr > &&type, Ptr< Expr > &&body, Ptr< RecDecl > &&next)
Definition ast.h:808
virtual void bind_decl(Scopes &) const
Definition bind.cpp:270
const Expr * body() const
Definition ast.h:817
const Expr * type() const
Definition ast.h:816
const RecDecl * next() const
Definition ast.h:818
void bind(Scopes &) const override
Definition bind.cpp:264
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:181
ret ptrn = callee $ arg; body
Definition ast.h:582
const Ptrn * ptrn() const
Definition ast.h:591
Ref emit_(Emitter &) const override
Definition emit.cpp:260
const Expr * arg() const
Definition ast.h:593
RetExpr(Loc loc, Ptr< Ptrn > &&ptrn, Ptr< Expr > &&callee, Ptr< Expr > &&arg, Ptr< Expr > body)
Definition ast.h:584
void bind(Scopes &) const override
Definition bind.cpp:171
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:130
const Expr * body() const
Definition ast.h:594
const Expr * callee() const
Definition ast.h:592
Just wraps TuplePtrn as Expr.
Definition ast.h:610
const TuplePtrn * ptrn() const
Definition ast.h:616
void emit_body(Emitter &, Ref decl) const override
Definition emit.cpp:276
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:135
Ref emit_(Emitter &) const override
Definition emit.cpp:277
Ref emit_decl(Emitter &, Ref type) const override
Definition emit.cpp:275
SigmaExpr(Ptr< TuplePtrn > &&ptrn)
Definition ast.h:612
void bind(Scopes &) const override
Definition bind.cpp:178
static constexpr Tok::Tag delim_l2r(Tag tag)
Definition tok.h:115
Tag tag() const
Definition tok.h:152
(elem_0, ..., elem_n-1)
Definition ast.h:632
const Expr * elem(size_t i) const
Definition ast.h:639
void bind(Scopes &) const override
Definition bind.cpp:184
TupleExpr(Loc loc, Ptrs< Expr > &&elems)
Definition ast.h:634
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:136
Ref emit_(Emitter &) const override
Definition emit.cpp:279
const auto & elems() const
Definition ast.h:638
size_t num_elems() const
Definition ast.h:640
(ptrn_0, ..., ptrn_n-1), [ptrn_0, ..., ptrn_n-1], or {ptrn_0, ..., ptrn_n-1}
Definition ast.h:287
void bind(Scopes &, bool rebind, bool quiet) const override
Definition bind.cpp:107
bool is_paren() const
Definition ast.h:296
const Ptrn * ptrn(size_t i) const
Definition ast.h:301
Ref emit_value(Emitter &, Ref) const override
Definition emit.cpp:64
Tok::Tag delim_r() const
Definition ast.h:295
Ref emit_type(Emitter &) const override
Definition emit.cpp:86
Ref emit_decl(Emitter &, Ref type) const
Definition emit.cpp:111
const auto & ptrns() const
Definition ast.h:300
size_t num_ptrns() const
Definition ast.h:302
TuplePtrn(Loc loc, Tok::Tag delim_l, Ptrs< Ptrn > &&ptrns)
Definition ast.h:289
Tok::Tag delim_l() const
Definition ast.h:294
Ref emit_body(Emitter &, Ref decl) const
Definition emit.cpp:88
bool implicit() const override
Definition ast.h:298
bool is_brckt() const
Definition ast.h:297
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:66
Type level
Definition ast.h:431
const Expr * level() const
Definition ast.h:437
TypeExpr(Loc loc, Ptr< Expr > &&level)
Definition ast.h:433
Ref emit_(Emitter &) const override
Definition emit.cpp:134
std::ostream & stream(Tab &, std::ostream &) const override
Definition stream.cpp:106
void bind(Scopes &) const override
Definition bind.cpp:117
virtual void bind(Scopes &) const =0
virtual void emit(Emitter &) const =0
ValDecl(Loc loc)
Definition ast.h:175
Definition ast.h:14
std::deque< Dbg > Dbgs
Definition ast.h:23
fe::Arena::Ptr< const T > Ptr
Definition ast.h:21
AST load_plugins(World &, View< Sym >)
Definition ast.cpp:190
std::deque< Ptr< T > > Ptrs
Definition ast.h:22
u8 sub_t
Definition types.h:48
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
Definition print.h:155
u64 plugin_t
Definition types.h:46
u8 tag_t
Definition types.h:47
Definition span.h:104
static std::optional< plugin_t > mangle(Sym plugin)
Mangles s into a dense 48-bit representation.
Definition plugin.cpp:9
Loc loc() const
Definition dbg.h:146
bool is_pi() const
Definition ast.h:32
std::deque< std::deque< Sym > > subs
List of subs which is a list of aliases.
Definition ast.h:42
std::optional< const Pi * > pi
Definition ast.h:44
struct mim::ast::AnnexInfo::@0 sym
AnnexInfo(Sym sym_plugin, Sym sym_tag, plugin_t id_plugin, tag_t id_tag)
Definition ast.h:26
struct mim::ast::AnnexInfo::@1 id