summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2019-11-03 16:54:12 +0800
committerJohn Hodge <tpg@mutabah.net>2019-11-03 16:54:12 +0800
commitce620c00008eb6cb8d9eb23a13775ec3b8127175 (patch)
tree3cc89fc004bd412e0833999273196fc21dd5e1a4
parent66353070eba9236219a003023c428cb6f1d1de3e (diff)
downloadmrust-ce620c00008eb6cb8d9eb23a13775ec3b8127175.tar.gz
HIR - Correct enum repr types
-rw-r--r--src/hir/hir.cpp16
-rw-r--r--src/hir/hir.hpp3
-rw-r--r--src/hir_conv/constant_evaluation.cpp2
-rw-r--r--src/hir_typeck/expr_check.cpp3
-rw-r--r--src/hir_typeck/expr_visit.cpp3
-rw-r--r--src/mir/visit_crate_mir.cpp3
-rw-r--r--vsproject/mrustc.vcxproj.filters6
7 files changed, 26 insertions, 10 deletions
diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp
index 88b1cb8c..2e9b2830 100644
--- a/src/hir/hir.cpp
+++ b/src/hir/hir.cpp
@@ -188,6 +188,22 @@ uint32_t HIR::Enum::get_value(size_t idx) const
throw "";
}
}
+/*static*/ ::HIR::TypeRef HIR::Enum::get_repr_type(Repr r)
+{
+ switch(r)
+ {
+ case ::HIR::Enum::Repr::Rust:
+ case ::HIR::Enum::Repr::C:
+ return ::HIR::CoreType::Isize;
+ break;
+ case ::HIR::Enum::Repr::Usize: return ::HIR::CoreType::Usize; break;
+ case ::HIR::Enum::Repr::U8 : return ::HIR::CoreType::U8 ; break;
+ case ::HIR::Enum::Repr::U16: return ::HIR::CoreType::U16; break;
+ case ::HIR::Enum::Repr::U32: return ::HIR::CoreType::U32; break;
+ case ::HIR::Enum::Repr::U64: return ::HIR::CoreType::U64; break;
+ }
+ throw "";
+}
const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path(const Span& sp, const char* name) const
diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp
index 8b718b4f..0188ba58 100644
--- a/src/hir/hir.hpp
+++ b/src/hir/hir.hpp
@@ -304,6 +304,9 @@ public:
bool is_value() const;
/// Returns the value for the given variant (onlu for value enums)
uint32_t get_value(size_t variant) const;
+
+ /// Get a type for the given repr value
+ static ::HIR::TypeRef get_repr_type(Repr r);
};
class Struct
{
diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp
index 35669dd9..47c6aaee 100644
--- a/src/hir_conv/constant_evaluation.cpp
+++ b/src/hir_conv/constant_evaluation.cpp
@@ -1172,7 +1172,7 @@ namespace {
static Span sp;
if( auto* e = item.m_data.opt_Value() )
{
- ::HIR::TypeRef ty = ::HIR::CoreType::Isize;
+ auto ty = ::HIR::Enum::get_repr_type(e->repr);
uint64_t i = 0;
for(auto& var : e->variants)
{
diff --git a/src/hir_typeck/expr_check.cpp b/src/hir_typeck/expr_check.cpp
index 7abe5532..7c9367e4 100644
--- a/src/hir_typeck/expr_check.cpp
+++ b/src/hir_typeck/expr_check.cpp
@@ -1325,8 +1325,7 @@ namespace {
if( auto* e = item.m_data.opt_Value() )
{
- // TODO: Use a different type depding on repr()
- auto enum_type = ::HIR::TypeRef(::HIR::CoreType::Isize);
+ auto enum_type = ::HIR::Enum::get_repr_type(e->repr);
for(auto& var : e->variants)
{
DEBUG("Enum value " << p << " - " << var.name);
diff --git a/src/hir_typeck/expr_visit.cpp b/src/hir_typeck/expr_visit.cpp
index df27b7d7..ceb4110a 100644
--- a/src/hir_typeck/expr_visit.cpp
+++ b/src/hir_typeck/expr_visit.cpp
@@ -221,8 +221,7 @@ namespace {
if( auto* e = item.m_data.opt_Value() )
{
- // TODO: Use a different type depding on repr()
- auto enum_type = ::HIR::TypeRef(::HIR::CoreType::Isize);
+ auto enum_type = ::HIR::Enum::get_repr_type(e->repr);
for(auto& var : e->variants)
{
diff --git a/src/mir/visit_crate_mir.cpp b/src/mir/visit_crate_mir.cpp
index 79d2e32a..0bbf20ae 100644
--- a/src/mir/visit_crate_mir.cpp
+++ b/src/mir/visit_crate_mir.cpp
@@ -74,8 +74,7 @@ void MIR::OuterVisitor::visit_enum(::HIR::ItemPath p, ::HIR::Enum& item)
if( auto* e = item.m_data.opt_Value() )
{
- // TODO: Use a different type depding on repr()
- auto enum_type = ::HIR::TypeRef(::HIR::CoreType::Isize);
+ auto enum_type = ::HIR::Enum::get_repr_type(e->repr);
for(auto& var : e->variants)
{
diff --git a/vsproject/mrustc.vcxproj.filters b/vsproject/mrustc.vcxproj.filters
index d94cdca8..9eb137a5 100644
--- a/vsproject/mrustc.vcxproj.filters
+++ b/vsproject/mrustc.vcxproj.filters
@@ -164,9 +164,6 @@
<ClCompile Include="..\src\trans\enumerate.cpp">
<Filter>Source Files\trans</Filter>
</ClCompile>
- <ClCompile Include="..\src\hir_typeck\expr_visit.cpp">
- <Filter>Source Files\hir</Filter>
- </ClCompile>
<ClCompile Include="..\src\hir\expr_ptr.cpp">
<Filter>Source Files\hir</Filter>
</ClCompile>
@@ -401,6 +398,9 @@
<ClCompile Include="..\src\expand\assert.cpp">
<Filter>Source Files\expand</Filter>
</ClCompile>
+ <ClCompile Include="..\src\hir_typeck\expr_visit.cpp">
+ <Filter>Source Files\hir_typeck</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\common.hpp">