summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-03-19 14:51:35 +0800
committerJohn Hodge <tpg@mutabah.net>2016-03-19 14:51:35 +0800
commit13e938957698086e77b7a2c8bbcfea9bba76c17a (patch)
treef3d1eece3c3dd6977635acf375aaa7268962b6ae
parentd9ddf019fe73c78213cddac02c700b6ebf782de4 (diff)
downloadmrust-13e938957698086e77b7a2c8bbcfea9bba76c17a.tar.gz
Expand - Handle std/core loading (partially)
-rw-r--r--Makefile1
-rw-r--r--src/ast/crate.cpp13
-rw-r--r--src/ast/crate.hpp7
-rw-r--r--src/expand/mod.cpp20
-rw-r--r--src/expand/std_prelude.cpp53
5 files changed, 88 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index d6009aa6..f6ac536f 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,7 @@ OBJ += expand/mod.o expand/macro_rules.o expand/cfg.o
OBJ += expand/format_args.o
OBJ += expand/concat.o expand/stringify.o expand/file_line.o
OBJ += expand/derive.o expand/lang_item.o
+OBJ += expand/std_prelude.o
OBJ += dump_as_rust.o
OBJ += convert/ast_iterate.o
#OBJ += convert/decorators.o
diff --git a/src/ast/crate.cpp b/src/ast/crate.cpp
index 55f78ad1..135c2ae7 100644
--- a/src/ast/crate.cpp
+++ b/src/ast/crate.cpp
@@ -27,7 +27,7 @@ namespace AST {
Crate::Crate():
m_root_module(::AST::Path()),
- m_load_std(true)
+ m_load_std(LOAD_STD)
{
}
@@ -292,8 +292,15 @@ void Crate::load_extern_crate(::std::string name)
m_extern_crates.insert( make_pair(::std::move(name), ::std::move(ret)) );
}
-SERIALISE_TYPE_A(Crate::, "AST_Crate", {
- s.item(m_load_std);
+SERIALISE_TYPE(Crate::, "AST_Crate", {
+ unsigned ls = m_load_std;
+ s.item(ls);
+ s.item(m_extern_crates);
+ s.item(m_root_module);
+},{
+ unsigned ls = m_load_std;
+ s.item(ls);
+ m_load_std = (::AST::Crate::LoadStd)ls;
s.item(m_extern_crates);
s.item(m_root_module);
})
diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp
index cf9e2d10..a25d2480 100644
--- a/src/ast/crate.hpp
+++ b/src/ast/crate.hpp
@@ -26,7 +26,12 @@ public:
// Mapping filled by searching for (?visible) macros with is_pub=true
::std::map< ::std::string, const MacroRules*> m_exported_macros;
- bool m_load_std;
+ enum LoadStd {
+ LOAD_STD,
+ LOAD_CORE,
+ LOAD_NONE,
+ } m_load_std;
+ AST::Path m_prelude_path;
Crate();
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index f1a9891a..3b46736f 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -636,8 +636,9 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> mo
for( auto& i : impl.items() )
{
DEBUG(" - " << i.name << " :: " << i.data.attrs);
+
// TODO: Make a path from the impl definition? Requires having the impl def resolved to be correct
- // - Does it? the namespace is essentially the same
+ // - Does it? the namespace is essentially the same. There may be issues with wherever the path is used though
//::AST::Path path = modpath + i.name;
auto attrs = mv$(i.data.attrs);
@@ -680,7 +681,22 @@ void Expand(::AST::Crate& crate)
// 1. Crate attributes
Expand_Attrs(crate.m_attrs, AttrStage::EarlyPre, [&](const auto& d, const auto& a){ d.handle(a, crate); });
- // TODO: Load std/core
+ // Load libstd/libcore
+ switch( crate.m_load_std )
+ {
+ case ::AST::Crate::LOAD_STD:
+ if( crate.m_prelude_path != AST::Path() )
+ crate.m_prelude_path = AST::Path("std", {AST::PathNode("prelude"), AST::PathNode("v1")});
+ TODO(Span(), "Load libstd");
+ break;
+ case ::AST::Crate::LOAD_CORE:
+ if( crate.m_prelude_path != AST::Path() )
+ crate.m_prelude_path = AST::Path("core", {AST::PathNode("prelude")});
+ TODO(Span(), "Load libcore");
+ break;
+ case ::AST::Crate::LOAD_NONE:
+ break;
+ }
// 2. Module attributes
for( auto& a : crate.m_attrs.m_items )
diff --git a/src/expand/std_prelude.cpp b/src/expand/std_prelude.cpp
new file mode 100644
index 00000000..c8ad6471
--- /dev/null
+++ b/src/expand/std_prelude.cpp
@@ -0,0 +1,53 @@
+/*
+ */
+#include <synext.hpp>
+#include <ast/crate.hpp>
+
+class Decorator_NoStd:
+ public ExpandDecorator
+{
+public:
+ AttrStage stage() const override { return AttrStage::EarlyPre; }
+
+ void handle(const AST::MetaItem& mi, AST::Crate& crate) const override {
+ if( crate.m_load_std != AST::Crate::LOAD_STD ) {
+ ERROR(Span()/*mi.span()*/, E0000, "Invalid use of #![no_std] with itself or #![no_core]");
+ }
+ crate.m_load_std = AST::Crate::LOAD_CORE;
+ }
+};
+class Decorator_NoCore:
+ public ExpandDecorator
+{
+public:
+ AttrStage stage() const override { return AttrStage::EarlyPre; }
+
+ void handle(const AST::MetaItem& mi, AST::Crate& crate) const override {
+ if( crate.m_load_std != AST::Crate::LOAD_STD ) {
+ ERROR(Span()/*mi.span()*/, E0000, "Invalid use of #![no_core] with itself or #![no_std]");
+ }
+ crate.m_load_std = AST::Crate::LOAD_NONE;
+ }
+};
+//class Decorator_Prelude:
+// public ExpandDecorator
+//{
+//public:
+// AttrStage stage() const override { return AttrStage::EarlyPre; }
+//};
+
+class Decorator_NoPrelude:
+ public ExpandDecorator
+{
+public:
+ AttrStage stage() const override { return AttrStage::EarlyPre; }
+};
+
+
+
+STATIC_DECORATOR("no_std", Decorator_NoStd)
+STATIC_DECORATOR("no_core", Decorator_NoCore)
+//STATIC_DECORATOR("prelude", Decorator_Prelude)
+
+STATIC_DECORATOR("no_prelude", Decorator_NoPrelude)
+