diff options
author | John Hodge <tpg@mutabah.net> | 2016-10-29 13:26:52 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-10-29 13:26:52 +0800 |
commit | f5420b8174f7acf062f4c5d682820019d79cbdaf (patch) | |
tree | d6c6cdb63a0be8b754d618395d0ffa7443eb2352 /src | |
parent | d8fed3a1def1ae667c1471f44b19749a1f88cc42 (diff) | |
download | mrust-f5420b8174f7acf062f4c5d682820019d79cbdaf.tar.gz |
HIR Types - Store array size expr as a shared_ptr to allow cloning
Diffstat (limited to 'src')
-rw-r--r-- | src/hir/deserialise.cpp | 2 | ||||
-rw-r--r-- | src/hir/expr.hpp | 6 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 6 | ||||
-rw-r--r-- | src/hir/hir.cpp | 7 | ||||
-rw-r--r-- | src/hir/type.cpp | 19 | ||||
-rw-r--r-- | src/hir/type.hpp | 14 | ||||
-rw-r--r-- | src/hir/visitor.cpp | 3 | ||||
-rw-r--r-- | src/hir_conv/constant_evaluation.cpp | 8 | ||||
-rw-r--r-- | src/hir_expand/reborrow.cpp | 2 | ||||
-rw-r--r-- | src/hir_expand/ufcs_everything.cpp | 2 | ||||
-rw-r--r-- | src/hir_typeck/common.cpp | 6 | ||||
-rw-r--r-- | src/hir_typeck/expr_check.cpp | 2 | ||||
-rw-r--r-- | src/hir_typeck/expr_visit.cpp | 2 | ||||
-rw-r--r-- | src/mir/check.cpp | 2 | ||||
-rw-r--r-- | src/mir/from_hir.cpp | 4 |
15 files changed, 46 insertions, 39 deletions
diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index bb0449cc..4c8b1a43 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -604,7 +604,7 @@ namespace { }) _(Array, { deserialise_ptr< ::HIR::TypeRef>(), - ::HIR::ExprPtr(), + nullptr, m_in.read_u64c() }) _(Slice, { diff --git a/src/hir/expr.hpp b/src/hir/expr.hpp index 62bc4610..e22c1ab8 100644 --- a/src/hir/expr.hpp +++ b/src/hir/expr.hpp @@ -604,11 +604,7 @@ struct ExprNode_Literal: (ByteString, m_res_type = ::HIR::TypeRef::Data::make_Borrow({ ::HIR::BorrowType::Shared, - box$( ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Array({ - box$( ::HIR::TypeRef(::HIR::TypeRef::Data::make_Primitive(::HIR::CoreType::U8)) ), - ::HIR::ExprPtr(), - e.size() - }) ) ) + box$( ::HIR::TypeRef::new_array( ::HIR::CoreType::U8, e.size() ) ) }); ) ) diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 7731f04c..b0185596 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -684,11 +684,7 @@ ), (Array, if( e.size ) { - return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Array({ - box$( LowerHIR_Type(*e.inner) ), - LowerHIR_Expr( e.size ), - ~0u - }) ); + return ::HIR::TypeRef::new_array( LowerHIR_Type(*e.inner), LowerHIR_Expr( e.size ) ); } else { return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Slice({ diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp index f49c2f80..3a8ab95b 100644 --- a/src/hir/hir.cpp +++ b/src/hir/hir.cpp @@ -1,4 +1,11 @@ /* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * hir/hir.cpp + * - Processed module tree (High-level Intermediate Representation) + * + * HIR type helper code */ #include "hir.hpp" diff --git a/src/hir/type.cpp b/src/hir/type.cpp index 2daf1612..bee801e7 100644 --- a/src/hir/type.cpp +++ b/src/hir/type.cpp @@ -1,8 +1,13 @@ /* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * hir/type.cpp + * - HIR Type helper code */ #include "type.hpp" #include <span.hpp> -#include "expr.hpp" +#include "expr.hpp" // Hack for cloning array types namespace HIR { @@ -784,21 +789,19 @@ bool ::HIR::TypeRef::match_test_generics(const Span& sp, const ::HIR::TypeRef& x (Array, unsigned int size_val = e.size_val; if( e.size_val == ~0u ) { + assert( e.size ); + assert( *e.size ); // TODO: Need to invoke const eval here? Or support cloning expressions? Or run consteval earlier. - if( const auto* ptr = dynamic_cast<const ::HIR::ExprNode_Literal*>(&*e.size) ) + if( const auto* ptr = dynamic_cast<const ::HIR::ExprNode_Literal*>(&**e.size) ) { size_val = ptr->m_data.as_Integer().m_value; } else { - BUG(Span(), "Attempting to clone array with unknown size - " << *this); + return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Array({ box$(e.inner->clone()), e.size, ~0u }) ); } } - return ::HIR::TypeRef( Data::make_Array({ - box$( e.inner->clone() ), - ::HIR::ExprPtr(), - size_val - }) ); + return ::HIR::TypeRef::new_array( e.inner->clone(), size_val ); ), (Slice, return ::HIR::TypeRef( Data::make_Slice({ diff --git a/src/hir/type.hpp b/src/hir/type.hpp index 63a87059..60ea04bd 100644 --- a/src/hir/type.hpp +++ b/src/hir/type.hpp @@ -1,4 +1,10 @@ - +/* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * hir/type.hpp + * - HIR Type representation + */ #ifndef _HIR_TYPE_HPP_ #define _HIR_TYPE_HPP_ #pragma once @@ -140,7 +146,7 @@ public: }), (Array, struct { ::std::unique_ptr<TypeRef> inner; - ::HIR::ExprPtr size; + ::std::shared_ptr<::HIR::ExprPtr> size; size_t size_val; }), (Slice, struct { @@ -209,10 +215,10 @@ public: } static TypeRef new_array(TypeRef inner, unsigned int size) { assert(size != ~0u); - return TypeRef(Data::make_Array({box$(mv$(inner)), ::HIR::ExprPtr(), size})); + return TypeRef(Data::make_Array({box$(mv$(inner)), nullptr, size})); } static TypeRef new_array(TypeRef inner, ::HIR::ExprPtr size_expr) { - return TypeRef(Data::make_Array({box$(mv$(inner)), mv$(size_expr), ~0u})); + return TypeRef(Data::make_Array({box$(mv$(inner)), ::std::shared_ptr< ::HIR::ExprPtr>( new ::HIR::ExprPtr(mv$(size_expr)) ), ~0u})); } static TypeRef new_path(::HIR::Path path, TypePathBinding binding) { return TypeRef(Data::make_Path({ mv$(path), mv$(binding) })); diff --git a/src/hir/visitor.cpp b/src/hir/visitor.cpp index 42713c36..64458901 100644 --- a/src/hir/visitor.cpp +++ b/src/hir/visitor.cpp @@ -303,7 +303,8 @@ void ::HIR::Visitor::visit_type(::HIR::TypeRef& ty) ), (Array, this->visit_type( *e.inner ); - this->visit_expr( e.size ); + if( e.size ) + this->visit_expr( *e.size ); ), (Slice, this->visit_type( *e.inner ); diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index b362d5f9..b3e1cc9d 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -1088,10 +1088,12 @@ namespace { ::HIR::Visitor::visit_type(*e.inner); if( e.size_val == ~0u ) { - assert(e.size.get() != nullptr); - auto val = evaluate_constant(e.size->span(), m_crate, NewvalState { m_new_values, *m_mod_path, FMT("ty_" << &ty << "$") }, e.size); + assert(e.size); + assert(*e.size); + const auto& expr_ptr = *e.size; + auto val = evaluate_constant(expr_ptr->span(), m_crate, NewvalState { m_new_values, *m_mod_path, FMT("ty_" << &ty << "$") }, expr_ptr); if( !val.is_Integer() ) - ERROR(e.size->span(), E0000, "Array size isn't an integer"); + ERROR(expr_ptr->span(), E0000, "Array size isn't an integer"); e.size_val = val.as_Integer(); } DEBUG("Array " << ty << " - size = " << e.size_val); diff --git a/src/hir_expand/reborrow.cpp b/src/hir_expand/reborrow.cpp index a23d0cd6..3bc89be9 100644 --- a/src/hir_expand/reborrow.cpp +++ b/src/hir_expand/reborrow.cpp @@ -129,7 +129,7 @@ namespace { DEBUG("Array size " << ty); if( e.size ) { ExprVisitor_Mutate ev(m_crate); - ev.visit_node_ptr( e.size ); + ev.visit_node_ptr( *e.size ); } ) else { diff --git a/src/hir_expand/ufcs_everything.cpp b/src/hir_expand/ufcs_everything.cpp index 9f246f74..daba3e6a 100644 --- a/src/hir_expand/ufcs_everything.cpp +++ b/src/hir_expand/ufcs_everything.cpp @@ -757,7 +757,7 @@ namespace { DEBUG("Array size " << ty); if( e.size ) { ExprVisitor_Mutate ev(m_crate); - ev.visit_node_ptr( e.size ); + ev.visit_node_ptr( *e.size ); } ) else { diff --git a/src/hir_typeck/common.cpp b/src/hir_typeck/common.cpp index e900a066..8c902bd2 100644 --- a/src/hir_typeck/common.cpp +++ b/src/hir_typeck/common.cpp @@ -227,11 +227,7 @@ bool monomorphise_type_needed(const ::HIR::TypeRef& tpl) if( e.size_val == ~0u ) { BUG(sp, "Attempting to clone array with unknown size - " << tpl); } - rv = ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Array({ - box$( monomorphise_type_with_inner(sp, *e.inner, callback, allow_infer) ), - ::HIR::ExprPtr(), - e.size_val - }) ); + rv = ::HIR::TypeRef::new_array( monomorphise_type_with_inner(sp, *e.inner, callback, allow_infer), e.size_val ); ), (Slice, rv = ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Slice({ box$(monomorphise_type_with_inner(sp, *e.inner, callback, allow_infer)) }) ); diff --git a/src/hir_typeck/expr_check.cpp b/src/hir_typeck/expr_check.cpp index fa9a48d4..229bf388 100644 --- a/src/hir_typeck/expr_check.cpp +++ b/src/hir_typeck/expr_check.cpp @@ -1006,7 +1006,7 @@ namespace { t_args tmp; auto ty_usize = ::HIR::TypeRef(::HIR::CoreType::Usize); ExprVisitor_Validate ev(m_resolve, tmp, ty_usize); - ev.visit_root( *e.size ); + ev.visit_root( **e.size ); } ) else { diff --git a/src/hir_typeck/expr_visit.cpp b/src/hir_typeck/expr_visit.cpp index ac3da63d..9713e862 100644 --- a/src/hir_typeck/expr_visit.cpp +++ b/src/hir_typeck/expr_visit.cpp @@ -81,7 +81,7 @@ namespace { DEBUG("Array size " << ty); t_args tmp; if( e.size ) { - Typecheck_Code( m_ms, tmp, ::HIR::TypeRef(::HIR::CoreType::Usize), e.size ); + Typecheck_Code( m_ms, tmp, ::HIR::TypeRef(::HIR::CoreType::Usize), *e.size ); } ) else { diff --git a/src/mir/check.cpp b/src/mir/check.cpp index 3b9f4c64..b033ec51 100644 --- a/src/mir/check.cpp +++ b/src/mir/check.cpp @@ -568,7 +568,7 @@ namespace { this->visit_type( *e.inner ); DEBUG("Array size " << ty); if( e.size ) { - MIR_Validate(m_resolve, ::HIR::ItemPath(), *e.size.m_mir, {}, ::HIR::TypeRef(::HIR::CoreType::Usize)); + MIR_Validate(m_resolve, ::HIR::ItemPath(), *e.size->m_mir, {}, ::HIR::TypeRef(::HIR::CoreType::Usize)); } ) else { diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp index cac498b1..27c7d4cf 100644 --- a/src/mir/from_hir.cpp +++ b/src/mir/from_hir.cpp @@ -1773,8 +1773,8 @@ namespace { this->visit_type( *e.inner ); DEBUG("Array size " << ty); if( e.size ) { - auto fcn = LowerMIR(m_resolve, e.size, {}); - e.size.m_mir = mv$(fcn); + auto fcn = LowerMIR(m_resolve, *e.size, {}); + e.size->m_mir = mv$(fcn); } ) else { |