summaryrefslogtreecommitdiff
path: root/src
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 /src
parentb92a5147b177d019df2927d703e076cfde776b03 (diff)
downloadmrust-c8d44a761735c600dfbadc2b71cef58153f052ec.tar.gz
AST - Support for crate_name and crate_type (draft)
Diffstat (limited to 'src')
-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
4 files changed, 74 insertions, 22 deletions
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);
}
}