diff options
author | John Hodge <tpg@mutabah.net> | 2016-11-08 16:11:13 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-11-08 16:11:13 +0800 |
commit | 1aa8f457259bbda479a65cb10e1f17a2d3549e11 (patch) | |
tree | 7442480f18a432591b7efac44b6777900ad810c9 /src | |
parent | 5be815dca4df26239a338cc127c9181a4cdf0de7 (diff) | |
download | mrust-1aa8f457259bbda479a65cb10e1f17a2d3549e11.tar.gz |
HIR Const Eval - Fields on structs
Diffstat (limited to 'src')
-rw-r--r-- | src/hir_conv/constant_evaluation.cpp | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index 72c7eb34..06489f37 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -9,6 +9,7 @@ #include <hir/visitor.hpp> #include <algorithm> #include <mir/mir.hpp> +#include <hir_typeck/common.hpp> // Monomorph namespace { typedef ::std::vector< ::std::pair< ::std::string, ::HIR::Static> > t_new_values; @@ -620,7 +621,39 @@ namespace { ERROR(sp, E0000, "Field access on invalid type - " << m_rv_type); ), (Path, - TODO(sp, "Field access on path type - " << m_rv_type); + TU_MATCHA( (e.binding), (pbe), + (Unbound, + ERROR(sp, E0000, "Field access on invalid type - " << m_rv_type); + ), + (Opaque, + ERROR(sp, E0000, "Field access on invalid type - " << m_rv_type); + ), + (Struct, + auto monomorph_cb = monomorphise_type_get_cb(sp, nullptr, &e.path.m_data.as_Generic().m_params, nullptr); + const auto& str = *pbe; + unsigned int idx=0; + TU_MATCHA( (str.m_data), (se), + (Unit, + ERROR(sp, E0000, "Field access on invalid type - " << m_rv_type << " - Unit-like"); + ), + (Tuple, + idx = ::std::atoi( node.m_field.c_str() ); + ASSERT_BUG(sp, idx < se.size(), "Index out of range in tuple struct"); + m_rv_type = monomorphise_type_with(sp, se[idx].ent, monomorph_cb); + ), + (Named, + idx = ::std::find_if(se.begin(), se.end(), [&](const auto&x){return x.first==node.m_field;}) - se.begin(); + ASSERT_BUG(sp, idx < se.size(), "Field no found in struct"); + m_rv_type = monomorphise_type_with(sp, se[idx].second.ent, monomorph_cb); + ) + ) + ASSERT_BUG(sp, idx < vals.size(), "Index out of range in literal"); + m_rv = mv$( vals[idx] ); + ), + (Enum, + TODO(sp, "Field access on enum variant - " << m_rv_type); + ) + ) ), (Tuple, unsigned int idx = ::std::atoi( node.m_field.c_str() ); |