/* * MRustC - Rust Compiler * - By John Hodge (Mutabah/thePowersGang) * * ast/ast.hpp * - Core AST header */ #ifndef AST_HPP_INCLUDED #define AST_HPP_INCLUDED #include #include #include #include "../coretypes.hpp" #include #include #include #include #include "../parse/tokentree.hpp" #include "../types.hpp" #include "../macros.hpp" #include #include "pattern.hpp" #include "attrs.hpp" #include "expr.hpp" #include "macro.hpp" #include "item.hpp" #include "generics.hpp" namespace AST { class Crate; class Module; class Item; using ::std::unique_ptr; using ::std::move; enum eItemType { ITEM_TRAIT, ITEM_STRUCT, ITEM_FN, }; struct StructItem: public Serialisable { ::AST::MetaItems m_attrs; bool m_is_public; ::std::string m_name; TypeRef m_type; StructItem() { } StructItem(::AST::MetaItems attrs, bool is_pub, ::std::string name, TypeRef ty): m_attrs( mv$(attrs) ), m_is_public(is_pub), m_name( mv$(name) ), m_type( mv$(ty) ) { } friend ::std::ostream& operator<<(::std::ostream& os, const StructItem& x) { return os << (x.m_is_public ? "pub " : "") << x.m_name << ": " << x.m_type; } SERIALISABLE_PROTOTYPES(); }; struct TupleItem: public Serialisable { ::AST::MetaItems m_attrs; bool m_is_public; TypeRef m_type; TupleItem() { } TupleItem(::AST::MetaItems attrs, bool is_pub, TypeRef ty): m_attrs( mv$(attrs) ), m_is_public(is_pub), m_type( mv$(ty) ) { } friend ::std::ostream& operator<<(::std::ostream& os, const TupleItem& x) { return os << (x.m_is_public ? "pub " : "") << x.m_type; } SERIALISABLE_PROTOTYPES(); }; class TypeAlias: public Serialisable { GenericParams m_params; TypeRef m_type; public: TypeAlias() {} TypeAlias(GenericParams params, TypeRef type): m_params( move(params) ), m_type( move(type) ) {} const GenericParams& params() const { return m_params; } const TypeRef& type() const { return m_type; } GenericParams& params() { return m_params; } TypeRef& type() { return m_type; } SERIALISABLE_PROTOTYPES(); }; class Static: public Serialisable { public: enum Class { CONST, STATIC, MUT, }; private: Class m_class; TypeRef m_type; Expr m_value; public: Static(): m_class(CONST) {} Static(Class s_class, TypeRef type, Expr value): m_class(s_class), m_type( move(type) ), m_value( move(value) ) {} const Class& s_class() const { return m_class; } const TypeRef& type() const { return m_type; } const Expr& value() const { return m_value; } TypeRef& type() { return m_type; } Expr& value() { return m_value; } SERIALISABLE_PROTOTYPES(); }; class Function: public Serialisable { public: typedef ::std::vector< ::std::pair > Arglist; private: ::std::string m_lifetime; GenericParams m_params; Expr m_code; TypeRef m_rettype; Arglist m_args; public: Function() {} Function(const Function&) = delete; Function(Function&&) noexcept = default; Function(GenericParams params, TypeRef ret_type, Arglist args): m_params( move(params) ), m_rettype( move(ret_type) ), m_args( move(args) ) { } void set_code(Expr code) { m_code = ::std::move(code); } void set_self_lifetime(::std::string s) { m_lifetime = s; } const GenericParams& params() const { return m_params; } const Expr& code() const { return m_code; } const TypeRef& rettype() const { return m_rettype; } const Arglist& args() const { return m_args; } GenericParams& params() { return m_params; } Expr& code() { return m_code; } TypeRef& rettype() { return m_rettype; } Arglist& args() { return m_args; } SERIALISABLE_PROTOTYPES(); }; class Trait: public Serialisable { GenericParams m_params; ::std::vector m_supertraits; NamedList m_items; public: Trait() {} Trait(GenericParams params, ::std::vector supertraits): m_params( mv$(params) ), m_supertraits( mv$(supertraits) ) { } const GenericParams& params() const { return m_params; } GenericParams& params() { return m_params; } const ::std::vector& supertraits() const { return m_supertraits; } ::std::vector& supertraits() { return m_supertraits; } const NamedList& items() const { return m_items; } NamedList& items() { return m_items; } void add_type(::std::string name, TypeRef type); void add_function(::std::string name, Function fcn); void add_static(::std::string name, Static v); bool has_named_item(const ::std::string& name, bool& out_is_fcn) const; SERIALISABLE_PROTOTYPES(); }; TAGGED_UNION_EX(EnumVariantData, (: public Serialisable), Value, ( (Value, struct { ::AST::Expr m_value; }), (Tuple, struct { ::std::vector m_sub_types; }), (Struct, struct { ::std::vector m_fields; }) ), (), (), ( public: SERIALISABLE_PROTOTYPES(); ) ); struct EnumVariant: public Serialisable { MetaItems m_attrs; ::std::string m_name; EnumVariantData m_data; EnumVariant() { } EnumVariant(MetaItems attrs, ::std::string name, Expr&& value): m_attrs( mv$(attrs) ), m_name( mv$(name) ), m_data( EnumVariantData::make_Value({mv$(value)}) ) { } EnumVariant(MetaItems attrs, ::std::string name, ::std::vector sub_types): m_attrs( mv$(attrs) ), m_name( ::std::move(name) ), m_data( EnumVariantData::make_Tuple( {mv$(sub_types)} ) ) { } EnumVariant(MetaItems attrs, ::std::string name, ::std::vector fields): m_attrs( mv$(attrs) ), m_name( ::std::move(name) ), m_data( EnumVariantData::make_Struct( {mv$(fields)} ) ) { } friend ::std::ostream& operator<<(::std::ostream& os, const EnumVariant& x) { os << "EnumVariant(" << x.m_name; TU_MATCH(EnumVariantData, (x.m_data), (e), (Value, os << " = " << e.m_value; ), (Tuple, os << "(" << e.m_sub_types << ")"; ), (Struct, os << " { " << e.m_fields << " }"; ) ) return os << ")"; } SERIALISABLE_PROTOTYPES(); }; class Enum: public Serialisable { GenericParams m_params; ::std::vector m_variants; public: Enum() {} Enum( GenericParams params, ::std::vector variants ): m_params( move(params) ), m_variants( move(variants) ) {} const GenericParams& params() const { return m_params; } const ::std::vector& variants() const { return m_variants; } GenericParams& params() { return m_params; } ::std::vector& variants() { return m_variants; } SERIALISABLE_PROTOTYPES(); }; TAGGED_UNION_EX(StructData, (: public Serialisable), Struct, ( (Tuple, struct { ::std::vector ents; }), (Struct, struct { ::std::vector ents; }) ), (),(), ( public: SERIALISABLE_PROTOTYPES(); ) ); class Struct: public Serialisable { GenericParams m_params; public: StructData m_data; Struct() {} Struct( GenericParams params, ::std::vector fields ): m_params( move(params) ), m_data( StructData::make_Struct({mv$(fields)}) ) {} Struct( GenericParams params, ::std::vector fields ): m_params( move(params) ), m_data( StructData::make_Tuple({mv$(fields)}) ) {} const GenericParams& params() const { return m_params; } GenericParams& params() { return m_params; } TypeRef get_field_type(const char *name, const ::std::vector& args); SERIALISABLE_PROTOTYPES(); }; class ImplDef: public Serialisable { MetaItems m_attrs; GenericParams m_params; Path m_trait; TypeRef m_type; public: ImplDef() {} ImplDef(ImplDef&&) /*noexcept*/ = default; ImplDef(MetaItems attrs, GenericParams params, Path trait_type, TypeRef impl_type): m_attrs( move(attrs) ), m_params( move(params) ), m_trait( move(trait_type) ), m_type( move(impl_type) ) {} // Accessors const MetaItems& attrs() const { return m_attrs; } const GenericParams& params() const { return m_params; } const Path& trait() const { return m_trait; } const TypeRef& type() const { return m_type; } GenericParams& params() { return m_params; } Path& trait() { return m_trait; } TypeRef& type() { return m_type; } /// Compare this impl against a trait,type pair bool matches(::std::vector& types, const Path& trait, const TypeRef& type) const; friend ::std::ostream& operator<<(::std::ostream& os, const ImplDef& impl); SERIALISABLE_PROTOTYPES(); }; class Impl: public Serialisable { ImplDef m_def; NamedList m_items; //NamedList m_types; //NamedList m_functions; //NamedList m_statics; ::std::vector< ::std::pair< ::std::vector, Impl > > m_concrete_impls; public: ::std::vector m_macro_invocations; Impl() {} Impl(Impl&&) /*noexcept*/ = default; Impl(MetaItems attrs, GenericParams params, TypeRef impl_type, Path trait_type): m_def( move(attrs), move(params), move(trait_type), move(impl_type) ) {} void add_function(bool is_public, ::std::string name, Function fcn); void add_type(bool is_public, ::std::string name, TypeRef type); void add_static(bool is_public, ::std::string name, Static v); void add_macro_invocation( MacroInvocation inv ) { m_macro_invocations.push_back( mv$(inv) ); } const ImplDef& def() const { return m_def; } const NamedList& items() const { return m_items; } ImplDef& def() { return m_def; } NamedList& items() { return m_items; } bool has_named_item(const ::std::string& name) const; /// Obtain a concrete implementation based on the provided types (caches) Impl& get_concrete(const ::std::vector& param_types); friend ::std::ostream& operator<<(::std::ostream& os, const Impl& impl); SERIALISABLE_PROTOTYPES(); private: /// Actually create a concrete impl Impl make_concrete(const ::std::vector& types) const; }; struct UseStmt: public Serialisable { Span sp; ::AST::Path path; ::AST::MetaItems attrs; UseStmt(UseStmt&&) = default; UseStmt(){} UseStmt(Span sp, Path p): sp(sp), path(p) { } friend ::std::ostream& operator<<(::std::ostream& os, const UseStmt& x); SERIALISABLE_PROTOTYPES(); }; typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn); /// Representation of a parsed (and being converted) function class Module: public Serialisable { public: class ItemRef { public: enum Type { TAG_None, TAG_Module, TAG_Crate, TAG_TypeAlias, TAG_Function, TAG_Trait, TAG_Struct, TAG_Enum, TAG_Static, TAG_Use, }; private: Type m_type; const void* m_ref; public: ItemRef(): m_type(TAG_None) {} Type tag() const { return m_type; } bool is_None() const { return m_type == TAG_None; } const Type& as_None() const { return m_type; } // HACK: Returns &Type in place of &void #define _(ty,ident) \ ItemRef(const ty& ref): m_type(TAG_##ident), m_ref(&ref) {} \ bool is_##ident() const { return m_type == TAG_##ident; } \ const ty& as_##ident() const { assert(m_type == TAG_##ident); return *(const ty*)m_ref; } _(AST::Module, Module) _(::std::string, Crate) _(AST::TypeAlias, TypeAlias) _(AST::Function, Function) _(AST::Trait, Trait) _(AST::Struct, Struct) _(AST::Enum, Enum) _(AST::Static, Static) _(AST::Named, Use) #undef _ friend ::std::ostream& operator<<(::std::ostream& os, const ItemRef& x) { switch(x.m_type) { #define _(ident) case TAG_##ident: return os << "ItemRef(" #ident ")"; _(None) _(Module) _(Crate) _(TypeAlias) _(Function) _(Trait) _(Struct) _(Enum) _(Static) _(Use) #undef _ } throw ""; } }; private: typedef ::std::vector< Named > itemlist_use_t; ::AST::Path m_my_path; // Module-level items /// General items ::std::vector> m_items; /// `use` imports (public and private) itemlist_use_t m_imports; /// Macro invocations ::std::vector m_macro_invocations; /// Impl blocks ::std::vector m_impls; /// Negative impl blocks ::std::vector m_neg_impls; // --- Runtime caches and state --- ::std::vector m_anon_modules; ::std::vector< NamedNS > m_macro_import_res; // Vec of imported macros (not serialised) ::std::vector< Named > m_macros; public: char m_index_populated = 0; // 0 = no, 1 = partial, 2 = complete // TODO: Add "namespace" list (separate to types) struct IndexEnt { bool is_pub; ::AST::Path path; }; ::std::unordered_map< ::std::string, IndexEnt > m_namespace_items; ::std::unordered_map< ::std::string, IndexEnt > m_type_items; ::std::unordered_map< ::std::string, IndexEnt > m_value_items; public: Module() {} Module(::AST::Path path): m_my_path( mv$(path) ) { } bool is_anon() const { return m_my_path.nodes().back().name()[0] == '#'; } // Called when module is loaded from a serialised format void prescan(); /// Create an anon module (for use inside expressions) ::std::unique_ptr add_anon(); void add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs); void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs); void add_alias(bool is_public, UseStmt path, ::std::string name, MetaItems attrs); void add_typealias(bool is_public, ::std::string name, TypeAlias alias, MetaItems attrs); void add_static(bool is_public, ::std::string name, Static item, MetaItems attrs); void add_trait(bool is_public, ::std::string name, Trait item, MetaItems attrs); void add_struct(bool is_public, ::std::string name, Struct item, MetaItems attrs); void add_enum(bool is_public, ::std::string name, Enum inst, MetaItems attrs); void add_function(bool is_public, ::std::string name, Function item, MetaItems attrs); void add_submod(bool is_public, ::std::string name, Module mod, MetaItems attrs); void add_impl(Impl impl) { m_impls.emplace_back( ::std::move(impl) ); } void add_neg_impl(ImplDef impl) { m_neg_impls.emplace_back( ::std::move(impl) ); } void add_macro(bool is_exported, ::std::string name, MacroRules macro) { m_macros.push_back( Named( move(name), move(macro), is_exported ) ); } void add_macro_import(::std::string name, const MacroRules& mr) { m_macro_import_res.push_back( NamedNS( mv$(name), &mr, false ) ); } void add_macro_invocation(MacroInvocation item) { m_macro_invocations.push_back( mv$(item) ); } unsigned int add_anon_module(Module* mod_ptr) { auto it = ::std::find(m_anon_modules.begin(), m_anon_modules.end(), mod_ptr); if( it != m_anon_modules.end() ) return it - m_anon_modules.begin(); m_anon_modules.push_back(mod_ptr); return m_anon_modules.size()-1; } void iterate_functions(fcn_visitor_t* visitor, const Crate& crate); const ::AST::Path& path() const { return m_my_path; } ItemRef find_item(const ::std::string& needle, bool allow_leaves = true, bool ignore_private_wildcard = true) const; ::std::vector>& items() { return m_items; } const ::std::vector>& items() const { return m_items; } itemlist_use_t& imports() { return m_imports; } const itemlist_use_t& imports() const { return m_imports; } ::std::vector& impls() { return m_impls; } const ::std::vector& impls() const { return m_impls; } ::std::vector& neg_impls() { return m_neg_impls; } const ::std::vector& neg_impls() const { return m_neg_impls; } ::std::vector& anon_mods() { return m_anon_modules; } const ::std::vector& anon_mods() const { return m_anon_modules; } ::std::vector& macro_invs() { return m_macro_invocations; } const NamedList& macros() const { return m_macros; } const ::std::vector > macro_imports_res() const { return m_macro_import_res; } SERIALISABLE_PROTOTYPES(); private: void resolve_macro_import(const Crate& crate, const ::std::string& modname, const ::std::string& macro_name); }; TAGGED_UNION_EX(Item, (: public Serialisable), None, ( (None, struct {} ), (Module, Module), (Crate, struct { ::std::string name; }), (Type, TypeAlias), (Struct, Struct), (Enum, Enum), (Trait, Trait), (Function, Function), (Static, Static) ), (, attrs(mv$(x.attrs))), (attrs = mv$(x.attrs);), ( public: MetaItems attrs; SERIALISABLE_PROTOTYPES(); ) ); struct ImplRef { const Impl& impl; ::std::vector params; ImplRef(const Impl& impl, ::std::vector params): impl(impl), params(params) {} ::rust::option find_named_item(const ::std::string& name) const; }; } // namespace AST class GenericResolveClosure { const ::AST::GenericParams& m_params; const ::std::vector& m_args; public: GenericResolveClosure(const AST::GenericParams& params, const ::std::vector& args): m_params(params), m_args(args) {} const TypeRef& operator()(const char *argname) { for(unsigned int i = 0; i < m_params.ty_params().size(); i ++) { if( m_params.ty_params()[i].name() == argname ) { return m_args.at(i); } } throw ::std::runtime_error("BUGCHECK - Unknown arg in field type"); } }; extern AST::Module g_compiler_module; extern void AST_InitProvidedModule(); #endif // AST_HPP_INCLUDED