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>;
27 :
sym{sym_plugin, sym_tag}
28 ,
id{id_plugin, id_tag, 0, 0} {
32 bool is_pi()
const {
return pi && *
pi && bool((*pi)->isa<
Pi>()); }
42 std::deque<std::deque<Sym>>
subs;
44 std::optional<const Pi*>
pi;
70 Sym
sym(std::string_view s) {
return driver().sym(s); }
71 Sym
sym(
const std::string& s) {
return driver().sym(s); }
77 template<
class T,
class... Args>
auto ptr(Args&&... args) {
78 return arena_.mk<
const T>(std::forward<Args&&>(args)...);
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)...); }
96 void bootstrap(Sym plugin, std::ostream& h);
101 swap(a1.world_, a2.world_);
102 swap(a1.arena_, a2.arena_);
103 swap(a1.err_, a2.err_);
108 World* world_ =
nullptr;
111 absl::node_hash_map<fe::Sym, absl::node_hash_map<fe::Sym, AnnexInfo>> plugin2sym2annex_;
114class Node :
public fe::RuntimeCast<Node> {
121 Loc
loc()
const {
return loc_; }
123 virtual std::ostream&
stream(
Tab&, std::ostream&)
const = 0;
200 void bind(
Scopes&,
bool quiet =
false)
const override;
202 std::ostream&
stream(
Tab&, std::ostream&)
const override;
223 void bind(
Scopes&,
bool quiet =
false)
const override;
225 std::ostream&
stream(
Tab&, std::ostream&)
const override;
240 void bind(
Scopes&,
bool quiet =
false)
const override;
242 std::ostream&
stream(
Tab&, std::ostream&)
const override;
261 const auto&
ptrns()
const {
return ptrns_; }
262 const Ptrn*
ptrn(
size_t i)
const {
return ptrns_[i].get(); }
265 void bind(
Scopes&,
bool quiet =
false)
const override;
269 std::ostream&
stream(
Tab&, std::ostream&)
const override;
288 std::ostream&
stream(
Tab&, std::ostream&)
const override;
300 std::ostream&
stream(
Tab&, std::ostream&)
const override;
317 std::ostream&
stream(
Tab&, std::ostream&)
const override;
323 mutable const Decl* decl_ =
nullptr;
338 std::ostream&
stream(
Tab&, std::ostream&)
const override;
359 std::ostream&
stream(
Tab&, std::ostream&)
const override;
377 const auto&
decls()
const {
return decls_; }
382 std::ostream&
stream(
Tab&, std::ostream&)
const override;
402 std::ostream&
stream(
Tab&, std::ostream&)
const override;
417 , dom_(
std::move(dom))
418 , codom_(
std::move(codom)) {}
421 const Expr* dom()
const {
return dom_.get(); }
422 const Expr* codom()
const {
return codom_.get(); }
424 void bind(Scopes&)
const override;
425 Ref
emit_decl(Emitter&, Ref type)
const override;
426 void emit_body(Emitter&, Ref decl)
const override;
427 std::ostream&
stream(Tab&, std::ostream&)
const override;
430 Ref
emit_(Emitter&)
const override;
434 mutable Pi* decl_ =
nullptr;
455 auto loc = type->loc();
459 virtual void bind(
Scopes& scopes,
bool quiet =
false)
const;
461 std::ostream&
stream(
Tab&, std::ostream&)
const override;
478 , doms_(
std::move(doms))
479 , codom_(
std::move(codom)) {
480 assert(num_doms() != 0);
484 Tok::Tag tag()
const {
return tag_; }
485 const Ptrs<Dom>& doms()
const {
return doms_; }
486 const Dom* dom(
size_t i)
const {
return doms_[i].get(); }
487 size_t num_doms()
const {
return doms_.size(); }
488 const Expr* codom()
const {
return codom_.get(); }
490 void bind(Scopes&)
const override;
491 Ref
emit_decl(Emitter&, Ref type)
const override;
492 void emit_body(Emitter&, Ref decl)
const override;
493 std::ostream&
stream(Tab&, std::ostream&)
const override;
496 Ref
emit_(Emitter&)
const override;
513 std::ostream&
stream(
Tab&, std::ostream&)
const override;
532 const Expr*
arg()
const {
return arg_.get(); }
535 std::ostream&
stream(
Tab&, std::ostream&)
const override;
557 const Expr*
arg()
const {
return arg_.get(); }
561 std::ostream&
stream(
Tab&, std::ostream&)
const override;
585 std::ostream&
stream(
Tab&, std::ostream&)
const override;
602 const auto&
elems()
const {
return elems_; }
603 const Expr*
elem(
size_t i)
const {
return elems_[i].get(); }
607 std::ostream&
stream(
Tab&, std::ostream&)
const override;
627 std::ostream&
stream(
Tab&, std::ostream&)
const override;
652 const auto&
index()
const {
return index_; }
656 std::ostream&
stream(
Tab&, std::ostream&)
const override;
662 std::variant<Ptr<Expr>,
Dbg> index_;
663 mutable const Decl* decl_ =
nullptr;
680 std::ostream&
stream(
Tab&, std::ostream&)
const override;
707 std::ostream&
stream(
Tab&, std::ostream&)
const override;
728 std::ostream&
stream(
Tab&, std::ostream&)
const override;
747 const auto&
subs()
const {
return subs_; }
749 const auto&
sub(
size_t i)
const {
return subs_[i]; }
757 std::ostream&
stream(
Tab&, std::ostream&)
const override;
761 std::deque<Ptrs<Alias>> subs_;
766 mutable Ref mim_type_;
792 std::ostream&
stream(
Tab&, std::ostream&)
const override;
820 void bind(
Scopes& scopes,
bool quiet =
false)
const override;
822 std::ostream&
stream(
Tab&, std::ostream&)
const override;
850 const Dom*
dom(
size_t i)
const {
return doms_[i].get(); }
858 std::ostream&
stream(
Tab&, std::ostream&)
const override;
881 const Ptrn*
dom()
const {
return dom_.get(); }
886 std::ostream&
stream(
Tab&, std::ostream&)
const override;
913 std::ostream&
stream(
Tab&, std::ostream&)
const;
917 swap(i1.dbg_, i2.dbg_);
918 swap(i1.tag_, i2.tag_);
919 swap(i1.module_, i2.module_);
936 const auto&
imports()
const {
return imports_; }
937 const auto&
decls()
const {
return decls_; }
946 std::ostream&
stream(
Tab&, std::ostream&)
const override;
Some "global" variables needed all over the place.
Error & error(Loc loc, const char *s, Args &&... args)
Error & note(Loc loc, const char *s, Args &&... args)
Error & warn(Loc loc, const char *s, Args &&... args)
A dependent function type.
Helper class to retrieve Infer::arg if present.
This is a thin wrapper for std::span<T, N> with the following additional features:
Keeps track of indentation level.
This is a thin wrapper for absl::InlinedVector<T, N, / A> which in turn is a drop-in replacement for ...
The World represents the whole program and manages creation of MimIR nodes (Defs).
const Driver & driver() const
friend void swap(AST &a1, AST &a2) noexcept
auto ptr(Args &&... args)
Error & error(Loc loc, const char *fmt, Args &&... args) const
AnnexInfo * name2annex(Dbg dbg, sub_t *)
Sym sym(std::string_view s)
const auto & plugin2annexes(Sym plugin)
void bootstrap(Sym plugin, std::ostream &h)
const Error & error() const
Sym sym_return()
"return".
Error & note(Loc loc, const char *fmt, Args &&... args) const
Error & warn(Loc loc, const char *fmt, Args &&... args) const
Sym sym_error()
"_error_".
Sym sym(const std::string &s)
AppExpr(Loc loc, bool is_explicit, Ptr< Expr > &&callee, Ptr< Expr > &&arg)
void bind(Scopes &) const override
std::ostream & stream(Tab &, std::ostream &) const override
const Expr * callee() const
Ref emit_(Emitter &) const override
«dbg: shape; body» or ‹dbg: shape; body›
const Expr * body() const
std::ostream & stream(Tab &, std::ostream &) const override
ArrOrPackExpr(Loc loc, Ptr< IdPtrn > &&shape, Ptr< Expr > &&body)
const IdPtrn * shape() const
Ref emit_(Emitter &) const override
void bind(Scopes &) const override
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_(Emitter &) const override
void emit_body(Emitter &, Ref decl) const override
void bind(Scopes &) const override
Ref emit_decl(Emitter &, Ref type) const override
ArrowExpr(Loc loc, Ptr< Expr > &&dom, Ptr< Expr > &&codom)
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &, const AxiomDecl *) const
const auto & subs() const
AxiomDecl(Loc loc, Dbg dbg, std::deque< Ptrs< Alias > > &&subs, Ptr< Expr > &&type, Dbg normalizer, Tok curry, Tok trip)
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
const Expr * type() const
void emit(Emitter &) const override
const auto & sub(size_t i) const
CDecl(Loc loc, Tok::Tag tag, Dbg dbg, Ptr< Ptrn > &&dom, Ptr< Expr > &&codom)
void emit(Emitter &) const override
const Expr * codom() const
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
decls e or e where decls if where is true.
const Expr * expr() const
std::ostream & stream(Tab &, std::ostream &) const override
const auto & decls() const
Ref emit_(Emitter &) const override
void bind(Scopes &) const override
DeclExpr(Loc loc, Ptrs< ValDecl > &&decls, Ptr< Expr > &&expr, bool is_where)
Ref emit_(Emitter &) const override
void bind(Scopes &) const override
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &, bool quiet=false) const override
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_type(Emitter &) const override
virtual void emit_body(Emitter &, Ref) const
virtual void bind(Scopes &) const =0
virtual Ref emit_decl(Emitter &, Ref) const
virtual Ref emit_(Emitter &) const =0
Ref emit(Emitter &) const
dbg_0 ... dbg_n-2 id where id = dbg_n-1: type
GrpPtrn(Dbg dbg, const IdPtrn *id)
void bind(Scopes &, bool quiet=false) const override
Ref emit_type(Emitter &) const override
const IdPtrn * id() const
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
const Decl * decl() const
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_(Emitter &) const override
static Ptr< IdPtrn > mk_id(AST &ast, Dbg dbg, Ptr< Expr > &&type)
static Ptr< IdPtrn > mk_type(AST &ast, Ptr< Expr > &&type)
IdPtrn(Loc loc, bool rebind, Dbg dbg, Ptr< Expr > &&type)
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &, bool quiet=false) const override
Ref emit_type(Emitter &) const override
const Expr * type() const
friend void swap(Import &i1, Import &i2) noexcept
Import(Loc loc, Tok::Tag tag, Dbg dbg, Ptr< Module > &&module)
void bind(Scopes &) const
void emit(Emitter &) const
const Module * module() const
std::ostream & stream(Tab &, std::ostream &) const
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_(Emitter &) const override
void bind(Scopes &) const override
Ref emit_(Emitter &) const override
InsertExpr(Loc loc, Ptr< Expr > &&tuple, Ptr< Expr > &&index, Ptr< Expr > &&value)
const Expr * index() const
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
const Expr * tuple() const
const Expr * value() const
Lam * emit_value(Emitter &) const
const Expr * filter() const
std::ostream & stream(Tab &, std::ostream &) const override
Dom(Loc loc, bool is_implicit, Ptr< Ptrn > &&ptrn, Ptr< Expr > &&filter)
void bind(Scopes &scopes, bool quiet=false) const override
void bind_body(Scopes &) const override
const Ptrs< Dom > & doms() const
std::ostream & stream(Tab &, std::ostream &) const override
const Expr * codom() const
void emit_decl(Emitter &) const override
void emit_body(Emitter &) const override
void bind_decl(Scopes &) const override
LamDecl(Loc loc, Tok::Tag tag, bool is_external, Dbg dbg, Ptrs< Dom > &&doms, Ptr< Expr > &&codom, Ptr< Expr > &&body, Ptr< RecDecl > &&next)
const Dom * dom(size_t i) const
Ref emit_decl(Emitter &, Ref type) const override
void emit_body(Emitter &, Ref decl) const override
void bind(Scopes &) const override
Ref emit_(Emitter &) const override
LamExpr(Ptr< LamDecl > &&lam)
const LamDecl * lam() const
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
const Expr * value() const
const Ptrn * ptrn() const
std::ostream & stream(Tab &, std::ostream &) const override
LetDecl(Loc loc, Ptr< Ptrn > &&ptrn, Ptr< Expr > &&value)
void emit(Emitter &) const override
const Expr * type() const
LitExpr(Loc loc, Tok tok, Ptr< Expr > &&type)
void bind(Scopes &) const override
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_(Emitter &) const override
Module(Loc loc, Ptrs< Import > &&imports, Ptrs< ValDecl > &&decls)
const auto & decls() const
const auto & implicit_imports() const
const auto & imports() const
void compile(AST &) const
void add_implicit_imports(Ptrs< Import > &&imports) const
std::ostream & stream(Tab &, std::ostream &) const override
virtual std::ostream & stream(Tab &, std::ostream &) const =0
virtual void emit_type(Emitter &) const
std::ostream & stream(Tab &, std::ostream &) const override
virtual void bind(Scopes &scopes, bool quiet=false) const
Dom(Loc loc, bool is_implicit, Ptr< Ptrn > &&ptrn)
const IdPtrn * ret() const
const Ptrn * ptrn() const
void add_ret(AST &ast, Ptr< Expr > &&type) const
void emit_body(Emitter &, Ref decl) const override
Ref emit_(Emitter &) const override
Ref emit_decl(Emitter &, Ref type) const override
void bind(Scopes &) const override
PiExpr(Loc loc, Tok::Tag tag, Ptrs< Dom > &&doms, Ptr< Expr > &&codom)
std::ostream & stream(Tab &, std::ostream &) const override
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
PrimaryExpr(Loc loc, Tok::Tag tag)
Ref emit_(Emitter &) const override
Ref emit_value(Emitter &, Ref) const
static Ptr< Ptrn > to_ptrn(Ptr< Expr > &&)
virtual Ref emit_type(Emitter &) const =0
static Ptr< Expr > to_expr(AST &, Ptr< Ptrn > &&)
Ptrn(Loc loc, bool rebind, Dbg dbg)
virtual void emit_value_(Emitter &, Ref) const
virtual void bind(Scopes &, bool quiet=false) const =0
void emit(Emitter &) const override
virtual void emit_body(Emitter &) const
virtual void bind_body(Scopes &) const
virtual void emit_decl(Emitter &) const
RecDecl(Loc loc, Dbg dbg, Ptr< Expr > &&type, Ptr< Expr > &&body, Ptr< RecDecl > &&next)
virtual void bind_decl(Scopes &) const
const Expr * body() const
const Expr * type() const
const RecDecl * next() const
void bind(Scopes &) const override
std::ostream & stream(Tab &, std::ostream &) const override
ret ptrn = callee $ arg; body
const Ptrn * ptrn() const
Ref emit_(Emitter &) const override
RetExpr(Loc loc, Ptr< Ptrn > &&ptrn, Ptr< Expr > &&callee, Ptr< Expr > &&arg, Ptr< Expr > body)
void bind(Scopes &) const override
std::ostream & stream(Tab &, std::ostream &) const override
const Expr * body() const
const Expr * callee() const
Just wraps TuplePtrn as Expr.
const TuplePtrn * ptrn() const
void emit_body(Emitter &, Ref decl) const override
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_(Emitter &) const override
Ref emit_decl(Emitter &, Ref type) const override
SigmaExpr(Ptr< TuplePtrn > &&ptrn)
void bind(Scopes &) const override
static constexpr Tok::Tag delim_l2r(Tag tag)
const Expr * elem(size_t i) const
void bind(Scopes &) const override
TupleExpr(Loc loc, Ptrs< Expr > &&elems)
std::ostream & stream(Tab &, std::ostream &) const override
Ref emit_(Emitter &) const override
const auto & elems() const
dbg::(ptrn_0, ..., ptrn_n-1) or dbg::[ptrn_0, ..., ptrn_n-1]
void bind(Scopes &, bool quiet=false) const override
const Ptrn * ptrn(size_t i) const
Ref emit_type(Emitter &) const override
TuplePtrn(Loc loc, Tok::Tag delim_l, Ptrs< Ptrn > &&ptrns, bool rebind, Dbg dbg)
Ref emit_decl(Emitter &, Ref type) const
const auto & ptrns() const
void emit_value_(Emitter &, Ref) const override
Ref emit_body(Emitter &, Ref decl) const
std::ostream & stream(Tab &, std::ostream &) const override
const Expr * level() const
TypeExpr(Loc loc, Ptr< Expr > &&level)
Ref emit_(Emitter &) const override
std::ostream & stream(Tab &, std::ostream &) const override
void bind(Scopes &) const override
virtual void bind(Scopes &) const =0
virtual void emit(Emitter &) const =0
fe::Arena::Ptr< const T > Ptr
AST load_plugins(World &, View< Sym >)
std::deque< Ptr< T > > Ptrs
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
static std::optional< plugin_t > mangle(Sym plugin)
Mangles s into a dense 48-bit representation.
std::deque< std::deque< Sym > > subs
List of subs which is a list of aliases.
std::optional< const Pi * > pi
struct mim::ast::AnnexInfo::@0 sym
AnnexInfo(Sym sym_plugin, Sym sym_tag, plugin_t id_plugin, tag_t id_tag)
struct mim::ast::AnnexInfo::@1 id