summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-08-25 10:35:53 +0800
committerJohn Hodge <tpg@mutabah.net>2016-08-25 10:35:53 +0800
commitc8d44a761735c600dfbadc2b71cef58153f052ec (patch)
treeb934973d275128ca0f7bad24b399e87a41a50af7
parentb92a5147b177d019df2927d703e076cfde776b03 (diff)
downloadmrust-c8d44a761735c600dfbadc2b71cef58153f052ec.tar.gz
AST - Support for crate_name and crate_type (draft)
-rw-r--r--Makefile17
-rw-r--r--src/ast/crate.hpp12
-rw-r--r--src/expand/crate_tags.cpp54
-rw-r--r--src/hir/from_ast.cpp3
-rw-r--r--src/main.cpp27
5 files changed, 83 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index 50e97620..93777397 100644
--- a/Makefile
+++ b/Makefile
@@ -49,7 +49,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 += expand/std_prelude.o expand/crate_tags.o
OBJ += macro_rules/mod.o macro_rules/eval.o macro_rules/parse.o
OBJ += resolve/use.o resolve/index.o resolve/absolute.o
OBJ += hir/from_ast.o hir/from_ast_expr.o
@@ -85,12 +85,15 @@ PIPECMD ?= 2>&1 | tee $@_dbg.txt | tail -n 45 ; test $${PIPESTATUS[0]} -eq 0
output/%.ast: samples/%.rs $(BIN)
@mkdir -p output/
- $(DBG) $(BIN) $< --emit ast -o $@ $(PIPECMD)
+ $(DBG) $(BIN) $< -o $@ $(PIPECMD)
RUSTCSRC := ./rustc-nightly/
-output/core.ast: $(RUSTCSRC)src/libcore/lib.rs $(BIN)
+output/libcore.hir: $(RUSTCSRC)src/libcore/lib.rs $(BIN)
@mkdir -p output/
- $(DBG) $(BIN) $< --emit ast -o $@ $(PIPECMD)
+ $(DBG) $(BIN) $< -o $@ $(PIPECMD)
+output/libcollections.hir: $(RUSTCSRC)src/libcollections.rs output/libcore.hir $(BIN)
+ @mkdir -p output/
+ $(DBG) $(BIN) $< -o $@ $(PIPECMD)
.PHONY: UPDATE
UPDATE:
@@ -111,10 +114,8 @@ output/rust/%.o: $(RUST_TESTS_DIR)%.rs $(BIN)
$(BIN) $< -o $@ --stop-after parse > $@.txt 2>&1
touch $@
-test: output/core.ast $(BIN)
-# output/std.ast output/log.ast output/env_logger.ast output/getopts.ast
- @mkdir -p output/
-# $(DBG) $(BIN) samples/1.rs --crate-path output/std.ast -o output/test.c 2>&1 | tee output/1_dbg.txt
+.PHONY: test test_rustos
+test: output/libcore.hir $(BIN)
test_rustos: $(addprefix output/rust_os/,libkernel.rlib)
diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp
index 2ffd5582..9407d8ad 100644
--- a/src/ast/crate.hpp
+++ b/src/ast/crate.hpp
@@ -25,13 +25,22 @@ public:
// Mapping filled by searching for (?visible) macros with is_pub=true
::std::map< ::std::string, const MacroRules*> m_exported_macros;
+ enum class Type {
+ Unknown,
+ RustLib,
+ RustDylib,
+ CDylib,
+ Executable,
+ } m_crate_type = Type::Unknown;
enum LoadStd {
LOAD_STD,
LOAD_CORE,
LOAD_NONE,
- } m_load_std;
+ } m_load_std = LOAD_STD;
+ ::std::string m_crate_name;
AST::Path m_prelude_path;
+
Crate();
Module& root_module() { return m_root_module; }
@@ -49,7 +58,6 @@ public:
};
/// Representation of an imported crate
-/// - Functions are stored as resolved+typechecked ASTs
class ExternCrate
{
::std::map< ::std::string, MacroRulesPtr > m_mr_macros;
diff --git a/src/expand/crate_tags.cpp b/src/expand/crate_tags.cpp
new file mode 100644
index 00000000..66ad01d4
--- /dev/null
+++ b/src/expand/crate_tags.cpp
@@ -0,0 +1,54 @@
+/*
+ * MRustC - Rust Compiler
+ * - By John Hodge (Mutabah/thePowersGang)
+ *
+ * expand/crate_type.cpp
+ * - #![crate_type] handling
+ */
+#include <synext.hpp>
+#include <ast/crate.hpp>
+
+class Decorator_CrateType:
+ public ExpandDecorator
+{
+public:
+ AttrStage stage() const override { return AttrStage::EarlyPre; }
+
+ void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override {
+ if( crate.m_crate_type != AST::Crate::Type::Unknown ) {
+ ERROR(sp, E0000, "Multiple #![crate_type] attributes");
+ }
+ if( !mi.has_string() ) {
+ ERROR(sp, E0000, "#![crate_type] requires a string argument");
+ }
+ const auto& name = mi.string();
+ if( name == "rlib" ) {
+ crate.m_crate_type = AST::Crate::Type::RustLib;
+ }
+ else {
+ ERROR(sp, E0000, "Unknown crate type '" << name << "'");
+ }
+ }
+};
+
+class Decorator_CrateName:
+ public ExpandDecorator
+{
+public:
+ AttrStage stage() const override { return AttrStage::EarlyPre; }
+
+ void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override {
+ if( crate.m_crate_name != "" ) {
+ ERROR(sp, E0000, "Multiple #![crate_name] attributes");
+ }
+ if( !mi.has_string() || mi.string() == "" ) {
+ ERROR(sp, E0000, "#![crate_name] requires a non-empty string argument");
+ }
+ crate.m_crate_name = mi.string();
+ }
+};
+
+STATIC_DECORATOR("crate_type", Decorator_CrateType)
+STATIC_DECORATOR("crate_name", Decorator_CrateName)
+
+
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index 67ee8e4b..554d5699 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -1,4 +1,5 @@
-
+/*
+ */
#include "common.hpp"
#include "hir.hpp"
#include "main_bindings.hpp"
diff --git a/src/main.cpp b/src/main.cpp
index d8c47ff2..cc410e02 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -67,21 +67,19 @@ bool debug_enabled()
struct ProgramParams
{
- static const unsigned int EMIT_C = 0x1;
- static const unsigned int EMIT_AST = 0x2;
enum eLastStage {
STAGE_PARSE,
STAGE_EXPAND,
STAGE_RESOLVE,
STAGE_TYPECK,
STAGE_BORROWCK,
+ STAGE_MIR,
STAGE_ALL,
} last_stage = STAGE_ALL;
const char *infile = NULL;
::std::string outfile;
const char *crate_path = ".";
- unsigned emit_flags = EMIT_C;
ProgramParams(int argc, char *argv[]);
};
@@ -177,6 +175,8 @@ int main(int argc, char *argv[])
return 0;
}
+ // TODO: Extract the crate type from the crate attributes
+
// --------------------------------------
// HIR Section
// --------------------------------------
@@ -258,6 +258,9 @@ int main(int argc, char *argv[])
::std::ofstream os (FMT(params.outfile << "_3_mir.rs"));
MIR_Dump( os, *hir_crate );
});
+ if( params.last_stage == ProgramParams::STAGE_MIR ) {
+ return 0;
+ }
// Optimise the MIR
@@ -267,6 +270,7 @@ int main(int argc, char *argv[])
// });
// Generate code for non-generic public items (if requested)
+ //
// Save HIR tree (if requested)
}
@@ -329,22 +333,6 @@ ProgramParams::ProgramParams(int argc, char *argv[])
}
this->crate_path = argv[++i];
}
- else if( strcmp(arg, "--emit") == 0 ) {
- if( i == argc - 1 ) {
- // TODO: BAIL!
- exit(1);
- }
-
- arg = argv[++i];
- if( strcmp(arg, "ast") == 0 )
- this->emit_flags = EMIT_AST;
- else if( strcmp(arg, "c") == 0 )
- this->emit_flags = EMIT_C;
- else {
- ::std::cerr << "Unknown argument to --emit : '" << arg << "'" << ::std::endl;
- exit(1);
- }
- }
else if( strcmp(arg, "--stop-after") == 0 ) {
if( i == argc - 1 ) {
// TODO: BAIL!
@@ -360,6 +348,7 @@ ProgramParams::ProgramParams(int argc, char *argv[])
}
}
else {
+ ::std::cerr << "Unknown option '" << arg << "'" << ::std::endl;
exit(1);
}
}