summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast/ast.hpp2
-rw-r--r--src/hir/expr_ptr.hpp15
-rw-r--r--src/hir/from_ast.cpp24
-rw-r--r--src/hir/from_ast_expr.cpp0
-rw-r--r--src/hir/hir.hpp11
-rw-r--r--src/hir/path.cpp46
-rw-r--r--src/hir/path.hpp21
-rw-r--r--src/hir/type.cpp12
-rw-r--r--src/hir/type.hpp47
-rw-r--r--src/hir/type_ptr.cpp13
-rw-r--r--src/hir/type_ptr.hpp15
-rw-r--r--src/macros.hpp4
12 files changed, 196 insertions, 14 deletions
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp
index 0076678c..a5d4ec8c 100644
--- a/src/ast/ast.hpp
+++ b/src/ast/ast.hpp
@@ -632,7 +632,7 @@ public:
::std::vector<ImplDef>& neg_impls() { return m_neg_impls; }
const ::std::vector<ImplDef>& neg_impls() const { return m_neg_impls; }
- ::std::vector<Module*>& anon_mods() { return m_anon_modules; }
+ ::std::vector<Module*>& anon_mods() { return m_anon_modules; }
const ::std::vector<Module*>& anon_mods() const { return m_anon_modules; }
diff --git a/src/hir/expr_ptr.hpp b/src/hir/expr_ptr.hpp
new file mode 100644
index 00000000..e6b1bec4
--- /dev/null
+++ b/src/hir/expr_ptr.hpp
@@ -0,0 +1,15 @@
+/*
+ */
+#pragma once
+
+
+namespace HIR {
+
+class ExprNode;
+
+class ExprPtr
+{
+ ::HIR::ExprNode* node;
+};
+
+} // namespace HIR
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index b0d56b39..edb5cf7e 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -14,16 +14,23 @@
::HIR::CratePtr LowerHIR_FromAST(::AST::Crate crate)
{
::std::unordered_map< ::std::string, MacroRules > macros;
+ // TODO: Extract macros from root module
+ for( const auto& mac : crate.m_root_module.macros() ) {
+ //if( mac.data.export ) {
+ macros.insert( ::std::make_pair( mac.name, mac.data ) );
+ //}
+ }
auto rootmod = LowerHIR_Module( crate.m_root_module, ::HIR::SimplePath("") );
return ::HIR::CratePtr( ::HIR::Crate { mv$(rootmod), mv$(macros) } );
}
+// --------------------------------------------------------------------
::HIR::GenericParams LowerHIR_GenericParams(const ::AST::GenericParams& gp)
{
throw ::std::runtime_error("TODO: LowerHIR_GenericParams");
}
-::HIR::Expr LowerHIR_Expr(const ::AST::Expr& e)
+::HIR::ExprPtr LowerHIR_Expr(const ::AST::Expr& e)
{
throw ::std::runtime_error("TODO: LowerHIR_Expr");
}
@@ -69,8 +76,9 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H
mod.m_value_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::ValueItem> { is_pub, mv$(ti) }) ) );
}
-::HIR::Module LowerHIR_Module(::AST::Module& module, ::HIR::SimplePath path)
+::HIR::Module LowerHIR_Module(const ::AST::Module& module, ::HIR::SimplePath path)
{
+ TRACE_FUNCTION_F("path = " << path);
::HIR::Module mod { };
for( const auto& item : module.items() )
@@ -84,11 +92,13 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H
),
(Crate,
// TODO: All 'extern crate' items should be normalised into a list in the crate root
+ // - If public, add a namespace import here referring to the root of the imported crate
),
(Type,
_add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_TypeAlias( LowerHIR_TypeAlias(e) ) );
),
(Struct,
+ /// Add value reference
TU_IFLET( ::AST::StructData, e.m_data, Struct, e2,
::HIR::TypeRef ty = ::HIR::TypeRef( ::HIR::Path(mv$(item_path)) );
if( e2.ents.size() == 0 )
@@ -125,7 +135,15 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H
)
}
- throw ::std::runtime_error("TODO: LowerHIR_Module");
+ for( unsigned int i = 0; i < module.anon_mods().size(); i ++ )
+ {
+ auto& submod = *module.anon_mods()[i];
+ ::std::string name = FMT("#" << i);
+ auto item_path = path + name;
+ _add_mod_ns_item( mod, name, false, LowerHIR_Module(submod, mv$(item_path)) );
+ }
+
+ return mod;
}
diff --git a/src/hir/from_ast_expr.cpp b/src/hir/from_ast_expr.cpp
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/src/hir/from_ast_expr.cpp
diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp
index 60702d82..dba408bc 100644
--- a/src/hir/hir.hpp
+++ b/src/hir/hir.hpp
@@ -16,11 +16,10 @@
#include <hir/type.hpp>
#include <hir/path.hpp>
+#include <hir/expr_ptr.hpp>
namespace HIR {
-class Expr {
-};
class Pattern {
};
@@ -54,14 +53,14 @@ struct Static
bool m_is_mut;
TypeRef m_type;
- Expr m_value;
+ ExprPtr m_value;
};
struct Constant
{
GenericParams m_params;
TypeRef m_type;
- Expr m_value;
+ ExprPtr m_value;
};
struct Function
{
@@ -70,7 +69,7 @@ struct Function
::std::vector< ::std::pair< Pattern, TypeRef > > m_args;
TypeRef m_return;
- Expr m_code;
+ ExprPtr m_code;
};
// --------------------------------------------------------------------
@@ -85,7 +84,7 @@ struct Enum
{
TAGGED_UNION(Variant, Unit,
(Unit, struct{}),
- (Value, ::HIR::Expr),
+ (Value, ::HIR::ExprPtr),
(Tuple, ::std::vector<::HIR::TypeRef>),
(Struct, ::std::pair< ::std::string, ::HIR::TypeRef>)
);
diff --git a/src/hir/path.cpp b/src/hir/path.cpp
new file mode 100644
index 00000000..23b9c448
--- /dev/null
+++ b/src/hir/path.cpp
@@ -0,0 +1,46 @@
+/*
+ */
+#include <hir/path.hpp>
+#include <hir/type.hpp>
+
+::HIR::SimplePath HIR::SimplePath::operator+(const ::std::string& s) const
+{
+ ::HIR::SimplePath ret(m_crate_name);
+ ret.m_components = m_components;
+
+ ret.m_components.push_back( s );
+
+ return ret;
+}
+namespace HIR {
+ ::std::ostream& operator<<(::std::ostream& os, const ::HIR::SimplePath& x)
+ {
+ if( x.m_crate_name != "" ) {
+ os << "::\"" << x.m_crate_name << "\"";
+ }
+ else if( x.m_components.size() == 0 ) {
+ os << "::";
+ }
+ else {
+ }
+ for(const auto& n : x.m_components)
+ {
+ os << "::" << n;
+ }
+ return os;
+ }
+}
+
+::HIR::GenericPath::GenericPath()
+{
+}
+::HIR::GenericPath::GenericPath(::HIR::SimplePath sp):
+ m_path( mv$(sp) )
+{
+}
+
+::HIR::Path::Path(::HIR::SimplePath sp):
+ m_data( ::HIR::Path::Data::make_Generic(::HIR::GenericPath(mv$(sp))) )
+{
+}
+
diff --git a/src/hir/path.hpp b/src/hir/path.hpp
index 62c70ce2..5c9ca101 100644
--- a/src/hir/path.hpp
+++ b/src/hir/path.hpp
@@ -4,11 +4,11 @@
#pragma once
#include <common.hpp>
+#include <tagged_union.hpp>
+#include <hir/type_ptr.hpp>
namespace HIR {
-class TypeRef;
-
/// Simple path - Absolute with no generic parameters
struct SimplePath
{
@@ -26,6 +26,7 @@ struct SimplePath
SimplePath operator+(const ::std::string& s) const;
+ friend ::std::ostream& operator<<(::std::ostream& os, const SimplePath& x);
};
/// Generic path - Simple path with one lot of generic params
class GenericPath
@@ -33,13 +34,29 @@ class GenericPath
public:
SimplePath m_path;
::std::vector<TypeRef> m_params;
+
+ GenericPath();
+ GenericPath(::HIR::SimplePath sp);
};
class Path
{
+public:
// Two possibilities
// - UFCS
// - Generic path
+ TAGGED_UNION(Data, Generic,
+ (Generic, GenericPath),
+ (UFCS, struct {
+ TypeRefPtr type;
+ GenericPath trait;
+ ::std::string item;
+ })
+ );
+
+private:
+ Data m_data;
+
public:
Path(GenericPath _);
Path(SimplePath _);
diff --git a/src/hir/type.cpp b/src/hir/type.cpp
new file mode 100644
index 00000000..45e50e6e
--- /dev/null
+++ b/src/hir/type.cpp
@@ -0,0 +1,12 @@
+/*
+ */
+#include "type.hpp"
+
+namespace HIR {
+
+TypeRef::TypeRef(::HIR::Path path):
+ type( TypeRef::Data::make_Path(mv$(path)) )
+{
+}
+
+}
diff --git a/src/hir/type.hpp b/src/hir/type.hpp
index e0023188..7e09a687 100644
--- a/src/hir/type.hpp
+++ b/src/hir/type.hpp
@@ -3,11 +3,32 @@
#define _HIR_TYPE_HPP_
#pragma once
+#include <tagged_union.hpp>
#include <hir/path.hpp>
+#include <hir/expr_ptr.hpp>
namespace HIR {
-class TypeRef
+enum class CoreType
+{
+ Usize, Isize,
+ U8, I8,
+ U16, I16,
+ U32, I32,
+ U64, I64,
+
+ F32, F64,
+
+ Char, Str,
+};
+enum class BorrowType
+{
+ Shared,
+ Unique,
+ Owned,
+};
+
+struct TypeRef
{
// Options:
// - Primitive
@@ -18,7 +39,29 @@ class TypeRef
// - Tuple
// - Borrow
// - Pointer
-public:
+
+ TAGGED_UNION(Data, Infer,
+ (Infer, struct {}),
+ (Primitive, ::HIR::CoreType),
+ (Path, ::HIR::Path),
+ (Array, struct {
+ ::std::unique_ptr<TypeRef> inner;
+ ::HIR::ExprPtr size;
+ }),
+ (Tuple, ::std::vector<TypeRef>),
+ (Borrow, struct {
+ ::HIR::BorrowType type;
+ ::std::unique_ptr<TypeRef> inner;
+ }),
+ (Pointer, struct {
+ bool is_mut;
+ ::std::unique_ptr<TypeRef> inner;
+ })
+ );
+
+ Data type;
+
+
TypeRef(::HIR::Path _);
};
diff --git a/src/hir/type_ptr.cpp b/src/hir/type_ptr.cpp
new file mode 100644
index 00000000..b9faf847
--- /dev/null
+++ b/src/hir/type_ptr.cpp
@@ -0,0 +1,13 @@
+
+#include <hir/type_ptr.hpp>
+#include <hir/type.hpp>
+
+::HIR::TypeRefPtr::TypeRefPtr(TypeRef tr):
+ m_ptr( new TypeRef(mv$(tr)) )
+{
+}
+::HIR::TypeRefPtr::~TypeRefPtr()
+{
+ delete m_ptr, m_ptr = nullptr;
+}
+
diff --git a/src/hir/type_ptr.hpp b/src/hir/type_ptr.hpp
new file mode 100644
index 00000000..05987da1
--- /dev/null
+++ b/src/hir/type_ptr.hpp
@@ -0,0 +1,15 @@
+/*
+ */
+#pragma once
+
+namespace HIR {
+
+class TypeRef;
+class TypeRefPtr {
+ TypeRef* m_ptr;
+public:
+ TypeRefPtr(TypeRef _);
+ ~TypeRefPtr();
+};
+
+} // namespace HIR
diff --git a/src/macros.hpp b/src/macros.hpp
index a001ee94..5646e203 100644
--- a/src/macros.hpp
+++ b/src/macros.hpp
@@ -111,6 +111,10 @@ public:
};
/// A sigle 'macro_rules!' block
+//struct MacroRules {
+// bool export;
+// ::std::vector<MacroRule> m_rules;
+//};
typedef ::std::vector<MacroRule> MacroRules;
extern ::std::unique_ptr<TokenStream> Macro_InvokeRules(const char *name, const MacroRules& rules, TokenTree input);