diff options
author | John Hodge <tpg@mutabah.net> | 2016-11-20 09:41:38 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-11-20 09:41:38 +0800 |
commit | 4dd28709d6dde4230b6c83be66137c0e194ba449 (patch) | |
tree | 4adb2bb81889352033b5b6398b06be96eb29ad22 | |
parent | 3365774a335dd562fc555b23edb5f29d34b3023b (diff) | |
download | mrust-4dd28709d6dde4230b6c83be66137c0e194ba449.tar.gz |
HIR From AST - Shortcut arrays with integer literal sizes
-rw-r--r-- | src/hir/from_ast.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index d45c7ba3..44b7b21c 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -9,6 +9,7 @@ #include "hir.hpp" #include "main_bindings.hpp" #include <ast/ast.hpp> +#include <ast/expr.hpp> // For shortcut in array size handling #include <ast/crate.hpp> #include "from_ast.hpp" #include "visitor.hpp" @@ -684,13 +685,22 @@ return ::HIR::TypeRef::new_pointer( cl, LowerHIR_Type(*e.inner) ); ), (Array, + auto inner = LowerHIR_Type(*e.inner); if( e.size ) { - return ::HIR::TypeRef::new_array( LowerHIR_Type(*e.inner), LowerHIR_Expr( e.size ) ); + // If the size expression is an unannotated or usize integer literal, don't bother converting the expression + if( const auto* ptr = dynamic_cast<const ::AST::ExprNode_Integer*>(&*e.size) ) + { + if( ptr->m_datatype == CORETYPE_UINT || ptr->m_datatype == CORETYPE_ANY ) + { + unsigned int size_val = ptr->m_value; + return ::HIR::TypeRef::new_array( mv$(inner), size_val ); + } + } + + return ::HIR::TypeRef::new_array( mv$(inner), LowerHIR_Expr(e.size) ); } else { - return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_Slice({ - box$( LowerHIR_Type(*e.inner) ) - }) ); + return ::HIR::TypeRef::new_slice( mv$(inner) ); } ), |