Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ class FmtEscaped {
s(s.c_str()),
e(s.c_str() + s.size())
{}
FmtEscaped(const char* s, size_t l):
s(s),
e(s + l)
{}
// See main.cpp
friend ::std::ostream& operator<<(::std::ostream& os, const FmtEscaped& x);
};
Expand Down
10 changes: 5 additions & 5 deletions src/expand/macro_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CMacroUseHandler:
continue;
}
ASSERT_BUG(sp, ec.m_hir->m_root_module.m_macro_items.count(name) == 1, "Macro `" << name << "` missing from crate " << ec.m_name);
const auto* e = &*ec.m_hir->m_root_module.m_macro_items.at(name);
const auto* e = &ec.m_hir->m_root_module.m_macro_items.at(name);
if( !e->publicity.is_global() )
{
DEBUG("Not public: " << name);
Expand All @@ -125,7 +125,7 @@ class CMacroUseHandler:
const ::HIR::Module& mod = crate.m_extern_crates.at(imp->path.crate_name()).m_hir->get_mod_by_path(sp, imp->path, /*ignore_last_node*/true, /*ignore_crate_name*/true);

ASSERT_BUG(sp, mod.m_macro_items.count(imp->path.components().back()), "Failed to find final component of " << imp->path);
e = &*mod.m_macro_items.at(imp->path.components().back());
e = &mod.m_macro_items.at(imp->path.components().back());
if( const auto& imp2 = e->ent.opt_Import() ) {
if( imp2->path.crate_name() == CRATE_BUILTINS ) {
DEBUG("Importing builtin (skip): " << name);
Expand All @@ -144,7 +144,7 @@ class CMacroUseHandler:
TU_MATCH_HDRA( (e->ent), { )
TU_ARMA(Import, imp) { throw "Unexpected"; }
TU_ARMA(MacroRules, mac_ptr) { mr = &*mac_ptr; }
TU_ARMA(ProcMacro, p) { mr = &p; }
TU_ARMA(ProcMacro, p) { mr = &*p; }
}
if(!exists(name, mr))
{
Expand Down Expand Up @@ -305,8 +305,8 @@ class CMacroReexportHandler:
if( it == ext_crate.m_exported_macro_names.end() )
ERROR(sp, E0000, "Could not find macro " << name << "! in crate " << crate_name);
// TODO: Do this differently.
ext_crate.m_root_module.m_macro_items.at(name)->ent.as_MacroRules()->m_exported = true;
//ext_crate.m_root_module.m_macro_items.at(name)->publicity = AST::Publicity::new_global();
ext_crate.m_root_module.m_macro_items.at(name).ent.as_MacroRules()->m_exported = true;
//ext_crate.m_root_module.m_macro_items.at(name).publicity = AST::Publicity::new_global();
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/expand/proc_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ ProcMacroInv ProcMacro_Invoke_int(const Span& sp, const ::AST::Crate& crate, con
const ::HIR::ProcMacro* pmp = nullptr;
for(const auto& mi : ext_crate.m_hir->m_root_module.m_macro_items)
{
if( !mi.second->ent.is_ProcMacro() )
if( !mi.second.ent.is_ProcMacro() )
continue ;
const auto& pm = mi.second->ent.as_ProcMacro();
const auto& pm = *mi.second.ent.as_ProcMacro();
bool good = true;
for(size_t i = 0; i < ::std::min( mac_path.size()-1, pm.path.components().size() ); i++)
{
Expand Down
34 changes: 17 additions & 17 deletions src/hir/deserialise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ namespace {
m_in.read( bytes.data(), bytes.size() );
return ::MIR::Constant::make_Bytes( mv$(bytes) );
}
_(StaticString, m_in.read_string() )
_(StaticString, RcString(m_in.read_string()) ) // NOTE: Not using `read_istring` to avoid polluting the interning map
_(Const, { box$(deserialise_path()) } )
_(Generic, deserialise_genericref())
_(Function, { box$(deserialise_path()) } )
Expand Down Expand Up @@ -727,21 +727,21 @@ namespace {
return ::HIR::TypeItem::make_Import({ mv$(spath), is_variant, static_cast<unsigned int>(m_in.read_count()) });
}
case 1:
return ::HIR::TypeItem( deserialise_module() );
return ::HIR::TypeItem( box$(deserialise_module()) );
case 2:
return ::HIR::TypeItem( deserialise_typealias() );
return ::HIR::TypeItem( box$(deserialise_typealias()) );
case 3:
return ::HIR::TypeItem( deserialise_enum() );
return ::HIR::TypeItem( box$(deserialise_enum()) );
case 4:
return ::HIR::TypeItem( deserialise_struct() );
return ::HIR::TypeItem( box$(deserialise_struct()) );
case 5:
return ::HIR::TypeItem( deserialise_trait() );
return ::HIR::TypeItem( box$(deserialise_trait()) );
case 6:
return ::HIR::TypeItem( deserialise_union() );
return ::HIR::TypeItem( box$(deserialise_union()) );
case 7:
return ::HIR::TypeItem( deserialise_externtype() );
return ::HIR::TypeItem( box$(deserialise_externtype()) );
case 8:
return ::HIR::TypeItem( deserialise_traitalias() );
return ::HIR::TypeItem( box$(deserialise_traitalias()) );
default:
BUG(Span(), "Bad tag for HIR::TypeItem - " << tag);
}
Expand All @@ -756,13 +756,13 @@ namespace {
return ::HIR::ValueItem::make_Import({ mv$(spath), is_variant, static_cast<unsigned int>(m_in.read_count()) });
}
case 1:
return ::HIR::ValueItem( deserialise_constant() );
return ::HIR::ValueItem( box$(deserialise_constant()) );
case 2:
return ::HIR::ValueItem( deserialise_static() );
return ::HIR::ValueItem( box$(deserialise_static()) );
case 3:
return ::HIR::ValueItem::make_StructConstant({ deserialise_simplepath() });
case 4:
return ::HIR::ValueItem( deserialise_function() );
return ::HIR::ValueItem( box$(deserialise_function()) );
case 5:
return ::HIR::ValueItem::make_StructConstructor({ deserialise_simplepath() });
default:
Expand All @@ -783,7 +783,7 @@ namespace {
case HIR::MacroItem::TAG_MacroRules:
return deserialise_macrorulesptr();
case HIR::MacroItem::TAG_ProcMacro:
return deserialise_procmacro();
return box$(deserialise_procmacro());
}

TODO(Span(), "Bad tag for MacroItem - " << tag);
Expand Down Expand Up @@ -1691,7 +1691,7 @@ namespace {
#define _(x, ...) case ::MIR::SwitchValues::TAG_##x: return ::MIR::SwitchValues::make_##x( __VA_ARGS__ );
_(Unsigned, deserialise_vec_c<uint64_t>([&](){ return m_in.read_u64c(); }))
_(Signed , deserialise_vec_c< int64_t>([&](){ return m_in.read_i64c(); }))
_(String , deserialise_vec<::std::string>())
_(String , deserialise_vec_c<RcString>([&](){ return RcString(m_in.read_string()); }))
_(ByteString, deserialise_vec<::std::vector<uint8_t>>())
#undef _
default:
Expand Down Expand Up @@ -1724,9 +1724,9 @@ namespace {
::HIR::Module rv;

// m_traits doesn't need to be serialised
rv.m_value_items = deserialise_istrumap< ::std::unique_ptr< ::HIR::VisEnt< ::HIR::ValueItem> > >();
rv.m_mod_items = deserialise_istrumap< ::std::unique_ptr< ::HIR::VisEnt< ::HIR::TypeItem> > >();
rv.m_macro_items = deserialise_istrumap< ::std::unique_ptr< ::HIR::VisEnt< ::HIR::MacroItem> > >();
rv.m_value_items = deserialise_istrumap< ::HIR::VisEnt< ::HIR::ValueItem> >();
rv.m_mod_items = deserialise_istrumap< ::HIR::VisEnt< ::HIR::TypeItem > >();
rv.m_macro_items = deserialise_istrumap< ::HIR::VisEnt< ::HIR::MacroItem> >();

return rv;
}
Expand Down
64 changes: 33 additions & 31 deletions src/hir/from_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,10 @@ ::HIR::t_struct_fields LowerHIR_StructFields(::HIR::ItemPath path, const ::HIR::
// NOTE: I'd love to have this be a `Constant`, but that would require duplicating the type and the params
// meh. Lazy option is to just duplicate
auto name = RcString::new_interned(FMT(path.get_name() << "#default_" << field.m_name));
out_mod.m_value_items.insert(std::make_pair(name, ::std::make_unique<HIR::VisEnt<HIR::ValueItem>>(HIR::VisEnt<HIR::ValueItem>{
out_mod.m_value_items.insert(std::make_pair(name, HIR::VisEnt<HIR::ValueItem>{
HIR::Publicity::new_global(),
HIR::ValueItem(HIR::Constant(params.clone(), type.clone(), LowerHIR_Expr(field.m_default)))
})));
HIR::ValueItem(::std::make_unique<HIR::Constant>(params.clone(), type.clone(), LowerHIR_Expr(field.m_default)))
}));
field_default = std::make_unique<HIR::GenericPath>((*path.parent + name).get_simple_path(), params.make_nop_params(0) );
}
fields.push_back(HIR::StructField {
Expand Down Expand Up @@ -1942,13 +1942,13 @@ ::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::AttributeList&
}

void _add_mod_ns_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pub, ::HIR::TypeItem ti) {
mod.m_mod_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::TypeItem> { is_pub, mv$(ti) }) ) );
mod.m_mod_items.insert( ::std::make_pair( mv$(name), ::HIR::VisEnt< ::HIR::TypeItem> { is_pub, mv$(ti) }) );
}
void _add_mod_val_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pub, ::HIR::ValueItem ti) {
mod.m_value_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::ValueItem> { is_pub, mv$(ti) }) ) );
mod.m_value_items.insert( ::std::make_pair( mv$(name), ::HIR::VisEnt< ::HIR::ValueItem> { is_pub, mv$(ti) }) );
}
void _add_mod_mac_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pub, ::HIR::MacroItem ti) {
mod.m_macro_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::MacroItem> { is_pub, mv$(ti) }) ) );
mod.m_macro_items.insert( ::std::make_pair( mv$(name), ::HIR::VisEnt< ::HIR::MacroItem> { is_pub, mv$(ti) }) );
}

::HIR::ValueItem LowerHIR_Static(::HIR::ItemPath p, const ::AST::AttributeList& attrs, const ::AST::Static& e, const Span& sp, const RcString& name)
Expand All @@ -1957,7 +1957,7 @@ ::HIR::ValueItem LowerHIR_Static(::HIR::ItemPath p, const ::AST::AttributeList&

if( e.s_class() == ::AST::Static::CONST )
// Note: Empty names are allowed for `const _: ...`
return ::HIR::ValueItem::make_Constant(::HIR::Constant(
return ::HIR::ValueItem::make_Constant(::std::make_unique<::HIR::Constant>(
::HIR::GenericParams {},
LowerHIR_Type(e.type()),
LowerHIR_Expr(e.value())
Expand Down Expand Up @@ -1986,7 +1986,7 @@ ::HIR::ValueItem LowerHIR_Static(::HIR::ItemPath p, const ::AST::AttributeList&
linkage.name = name.c_str();
}

return ::HIR::ValueItem::make_Static(::HIR::Static(
return ::HIR::ValueItem::make_Static(::std::make_unique<::HIR::Static>(
mv$(linkage),
(e.s_class() == ::AST::Static::MUT),
LowerHIR_Type(e.type()),
Expand Down Expand Up @@ -2038,7 +2038,7 @@ ::HIR::Module LowerHIR_Module(const ::AST::Module& ast_mod, ::HIR::ItemPath path
void push_trait_alias_hir(const HIR::TraitAlias& ta) {
for(const auto& p : ta.m_traits) {
if( const auto* tap = g_crate_ptr->get_typeitem_by_path(Span(), p.m_path.m_path).opt_TraitAlias()) {
push_trait_alias_hir(*tap);
push_trait_alias_hir(**tap);
}
else {
push_trait(p.m_path.m_path);
Expand Down Expand Up @@ -2067,7 +2067,7 @@ ::HIR::Module LowerHIR_Module(const ::AST::Module& ast_mod, ::HIR::ItemPath path
auto& submod = *submod_ptr;
auto name = RcString::new_interned(FMT("#" << i));
auto item_path = ::HIR::ItemPath(path, name.c_str());
auto ti = ::HIR::TypeItem::make_Module( LowerHIR_Module(submod, item_path, mod.m_traits) );
auto ti = ::HIR::TypeItem::make_Module( ::std::make_unique<HIR::Module>(LowerHIR_Module(submod, item_path, mod.m_traits)) );
_add_mod_ns_item( mod, mv$(name), ::HIR::Publicity::new_priv(mod_path), mv$(ti) );
}
}
Expand Down Expand Up @@ -2118,7 +2118,7 @@ ::HIR::Module LowerHIR_Module(const ::AST::Module& ast_mod, ::HIR::ItemPath path
// Ignore - The index is used to add `Import`s
}
TU_ARMA(Module, e) {
_add_mod_ns_item( mod, item.name, get_vis(item.vis), LowerHIR_Module(e, mv$(item_path)) );
_add_mod_ns_item( mod, item.name, get_vis(item.vis), ::std::make_unique<HIR::Module>(LowerHIR_Module(e, mv$(item_path))) );
}
TU_ARMA(Crate, e) {
// All 'extern crate' items should be normalised into a list in the crate root
Expand All @@ -2132,10 +2132,10 @@ ::HIR::Module LowerHIR_Module(const ::AST::Module& ast_mod, ::HIR::ItemPath path
{
ERROR(item.span, E0000, "Generics on extern type");
}
_add_mod_ns_item(mod, item.name, get_vis(item.vis), ::HIR::ExternType {});
_add_mod_ns_item(mod, item.name, get_vis(item.vis), ::std::make_unique<::HIR::ExternType>());
break;
}
_add_mod_ns_item( mod, item.name, get_vis(item.vis), ::HIR::TypeItem::make_TypeAlias( LowerHIR_TypeAlias(item_path, e) ) );
_add_mod_ns_item( mod, item.name, get_vis(item.vis), ::HIR::TypeItem::make_TypeAlias( ::std::make_unique<HIR::TypeAlias>(LowerHIR_TypeAlias(item_path, e)) ) );
}
TU_ARMA(Struct, e) {
/// Add value reference
Expand All @@ -2147,26 +2147,28 @@ ::HIR::Module LowerHIR_Module(const ::AST::Module& ast_mod, ::HIR::ItemPath path
}
else {
}
_add_mod_ns_item( mod, item.name, get_vis(item.vis), LowerHIR_Struct(ip->span, item_path, e, item.attrs, mod) );
_add_mod_ns_item( mod, item.name, get_vis(item.vis), box$(LowerHIR_Struct(ip->span, item_path, e, item.attrs, mod)) );
}
TU_ARMA(Enum, e) {
auto enm = LowerHIR_Enum(item_path, e, item.attrs, [&](auto name, auto str){ _add_mod_ns_item(mod, name, get_vis(item.vis), mv$(str)); }, mod);
_add_mod_ns_item( mod, item.name, get_vis(item.vis), mv$(enm) );
auto enm = LowerHIR_Enum(item_path, e, item.attrs, [&](RcString name, HIR::Struct str){
_add_mod_ns_item(mod, name, get_vis(item.vis), box$(str));
}, mod);
_add_mod_ns_item( mod, item.name, get_vis(item.vis), box$(enm) );
}
TU_ARMA(Union, e) {
_add_mod_ns_item( mod, item.name, get_vis(item.vis), LowerHIR_Union(item_path, e, item.attrs) );
_add_mod_ns_item( mod, item.name, get_vis(item.vis), box$(LowerHIR_Union(item_path, e, item.attrs)) );
}
TU_ARMA(Trait, e) {
_add_mod_ns_item( mod, item.name, get_vis(item.vis), LowerHIR_Trait(item_path.get_simple_path(), e) );
_add_mod_ns_item( mod, item.name, get_vis(item.vis), box$(LowerHIR_Trait(item_path.get_simple_path(), e)) );
}
TU_ARMA(TraitAlias, e) {
_add_mod_ns_item( mod, item.name, get_vis(item.vis), LowerHIR_TraitAlias(sp, item_path, e) );
_add_mod_ns_item( mod, item.name, get_vis(item.vis), box$(LowerHIR_TraitAlias(sp, item_path, e)) );
}
TU_ARMA(Function, e) {
_add_mod_val_item(mod, item.name, get_vis(item.vis), LowerHIR_Function(item_path, item.attrs, e, ::HIR::TypeRef{}));
_add_mod_val_item( mod, item.name, get_vis(item.vis), box$(LowerHIR_Function(item_path, item.attrs, e, ::HIR::TypeRef{})) );
}
TU_ARMA(Static, e) {
_add_mod_val_item(mod, item.name, get_vis(item.vis), LowerHIR_Static(item_path, item.attrs, e, sp, item.name));
_add_mod_val_item( mod, item.name, get_vis(item.vis), LowerHIR_Static(item_path, item.attrs, e, sp, item.name) );
}
}
}
Expand Down Expand Up @@ -2598,7 +2600,7 @@ ::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
};
// Register under an invalid SimplePath
::HIR::ProcMacro::Ty ty = H::cvt_macro_ty(ent.ty);
macros.insert( std::make_pair(ent.name, ::HIR::ProcMacro { ty, ent.name, ::HIR::SimplePath(RcString(""), { ent.name }), ent.attributes }) );
macros.insert( std::make_pair(ent.name, make_unique_ptr(::HIR::ProcMacro { ty, ent.name, ::HIR::SimplePath(RcString(""), { ent.name }), ent.attributes })) );
rv.m_exported_macro_names.push_back(ent.name);
DEBUG("Export proc_macro " << ent.name);
}
Expand Down Expand Up @@ -2662,7 +2664,7 @@ ::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
if(e.second.is_MacroRules() ) {
ASSERT_BUG(Span(), !e.second.as_MacroRules()->m_rules.empty(), "Empty macro? - " << e.first);
}
rv.m_root_module.m_macro_items.insert( ::std::make_pair(e.first, box$(HIR::VisEnt<HIR::MacroItem> { HIR::Publicity::new_global(), mv$(e.second) })) );
rv.m_root_module.m_macro_items.insert( ::std::make_pair(e.first, HIR::VisEnt<HIR::MacroItem> { HIR::Publicity::new_global(), mv$(e.second) }) );
}

LowerHIR_Module_Impls(crate.m_root_module, rv);
Expand All @@ -2687,16 +2689,16 @@ ::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
if( it2 == mod->m_mod_items.end() )
return ::HIR::SimplePath();
const auto& e = it2->second;
if(const auto* ip = e->ent.opt_Import())
if(const auto* ip = e.ent.opt_Import())
{
// TODO: Handle module aliases?
(void)ip;
return ::HIR::SimplePath();
}
else if(const auto* ep = e->ent.opt_Module() )
else if(const auto* ep = e.ent.opt_Module() )
{
cur_path_components.push_back(*it);
mod = ep;
mod = &**ep;
}
else
{
Expand All @@ -2717,7 +2719,7 @@ ::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
return ::HIR::SimplePath();

// Found: Either return the current path, or return this alias.
if(const auto* ip = it2->second->ent.opt_Import())
if(const auto* ip = it2->second.ent.opt_Import())
{
if(ip->is_variant)
return ::HIR::SimplePath();
Expand Down Expand Up @@ -2899,12 +2901,12 @@ ::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
{
TRACE_FUNCTION_F(path);
for(auto& mi : mod.m_mod_items) {
if(auto* submod_p = mi.second->ent.opt_Module()) {
fix_macros_in_mod(path + mi.first, *submod_p);
if(auto* submod_p = mi.second.ent.opt_Module()) {
fix_macros_in_mod(path + mi.first, **submod_p);
}
}
for(auto& mi : mod.m_macro_items) {
if(auto* mrpp = mi.second->ent.opt_MacroRules()) {
if(auto* mrpp = mi.second.ent.opt_MacroRules()) {
auto& mr = **mrpp;
if( mr.m_source_crate.size() == 0 ) {
mr.m_source_crate = g_crate_name;
Expand All @@ -2914,7 +2916,7 @@ ::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
fix_macro_contents(rule.m_contents);
}
}
if( const auto* i = mi.second->ent.opt_Import() ) {
if( const auto* i = mi.second.ent.opt_Import() ) {
DEBUG(path << ": Import " << mi.first << " = " << i->path);
if( i->path.crate_name() == CRATE_BUILTINS ) {
}
Expand Down
Loading
Loading