aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <herron.philip@googlemail.com>2020-05-15 18:19:57 +0100
committerPhilip Herron <philip.herron@embecosm.com>2020-11-28 19:11:30 +0000
commitae88a9f49e8e20bad0c36ab04143ca051c2db4d2 (patch)
tree9911b546b0fcc120abc5c6a77059993b05f5dfa5
parent0a75153ea0d821b98da7205c5de3eb088e059587 (diff)
downloadgcc-ae88a9f49e8e20bad0c36ab04143ca051c2db4d2.zip
gcc-ae88a9f49e8e20bad0c36ab04143ca051c2db4d2.tar.gz
gcc-ae88a9f49e8e20bad0c36ab04143ca051c2db4d2.tar.bz2
Setup error handling for infering types in declarations
-rw-r--r--gcc/rust/analysis/rust-resolution.cc69
-rw-r--r--gcc/rust/analysis/rust-resolution.h1
2 files changed, 56 insertions, 14 deletions
diff --git a/gcc/rust/analysis/rust-resolution.cc b/gcc/rust/analysis/rust-resolution.cc
index 65fed90..b4444e4 100644
--- a/gcc/rust/analysis/rust-resolution.cc
+++ b/gcc/rust/analysis/rust-resolution.cc
@@ -1,9 +1,18 @@
#include "rust-resolution.h"
+#include "rust-diagnostics.h"
namespace Rust {
namespace Analysis {
-TypeResolution::TypeResolution (AST::Crate &crate) : scope (), crate (crate) {}
+TypeResolution::TypeResolution (AST::Crate &crate) : scope (), crate (crate)
+{
+ // push all builtin types
+ // base is parse_path_ident_segment based up on segments
+ /* scope.Insert ("u8",
+ new AST::MaybeNamedParam (Identifier ("u8"),
+ AST::MaybeNamedParam::IDENTIFIER,
+ NULL, Location ()));*/
+}
TypeResolution::~TypeResolution () {}
@@ -40,7 +49,9 @@ TypeResolution::visit (AST::AttrInputMetaItemContainer &input)
void
TypeResolution::visit (AST::IdentifierExpr &ident_expr)
-{}
+{
+ printf ("IdentifierExpr %s\n", ident_expr.as_string ().c_str ());
+}
void
TypeResolution::visit (AST::Lifetime &lifetime)
@@ -80,7 +91,11 @@ TypeResolution::visit (AST::QualifiedPathInType &path)
// rust-expr.h
void
TypeResolution::visit (AST::LiteralExpr &expr)
-{}
+{
+ printf ("LiteralExpr: %s\n", expr.as_string ().c_str ());
+ // figure out what this type is and push it onto the
+}
+
void
TypeResolution::visit (AST::AttrInputLiteral &attr_input)
{}
@@ -526,44 +541,70 @@ TypeResolution::visit (AST::SlicePattern &pattern)
void
TypeResolution::visit (AST::EmptyStmt &stmt)
{}
-void
+void
TypeResolution::visit (AST::LetStmt &stmt)
{
- printf ("Within LetStmt: %s\n", stmt.as_string ().c_str ());
-
- if (!stmt.has_type ())
+ AST::Type *inferedType = NULL;
+ if (stmt.has_type ())
+ {
+ inferedType = stmt.type.get ();
+ }
+ else if (stmt.has_init_expr ())
{
- // infer the type
- printf ("XXX UNKNOWN TYPE PLEASE INFER ME\n");
+ stmt.init_expr->accept_vis (*this);
+
+ if (typeBuffer.empty ())
+ {
+ rust_error_at (
+ stmt.init_expr->get_locus_slow (),
+ "unable to determine type for declaration from init expr");
+ return;
+ }
+
+ inferedType = typeBuffer.back ();
+ typeBuffer.pop_back ();
+ }
+ else
+ {
+ rust_error_at (stmt.locus, "unable to determine type for declaration");
+ return;
}
// TODO check we know what the type is
+ // get all the names part of this declaration and add the types to the scope
stmt.variables_pattern->accept_vis (*this);
-
for (auto it = letPatternBuffer.begin (); it != letPatternBuffer.end (); it++)
{
- scope.Insert (it->variable_ident, stmt.type.get ());
+ scope.Insert (it->variable_ident, inferedType);
}
-
letPatternBuffer.clear ();
}
void
TypeResolution::visit (AST::ExprStmtWithoutBlock &stmt)
-{}
+{
+ printf ("ExprStmtWithoutBlock: %s\n", stmt.as_string ().c_str ());
+ stmt.expr->accept_vis (*this);
+}
+
void
TypeResolution::visit (AST::ExprStmtWithBlock &stmt)
-{}
+{
+ printf ("ExprStmtWithBlock: %s\n", stmt.as_string ().c_str ());
+ stmt.expr->accept_vis (*this);
+}
// rust-type.h
void
TypeResolution::visit (AST::TraitBound &bound)
{}
+
void
TypeResolution::visit (AST::ImplTraitType &type)
{}
+
void
TypeResolution::visit (AST::TraitObjectType &type)
{}
diff --git a/gcc/rust/analysis/rust-resolution.h b/gcc/rust/analysis/rust-resolution.h
index 940557e..9d0927a 100644
--- a/gcc/rust/analysis/rust-resolution.h
+++ b/gcc/rust/analysis/rust-resolution.h
@@ -229,6 +229,7 @@ private:
AST::Crate &crate;
std::vector<AST::IdentifierPattern> letPatternBuffer;
+ std::vector<AST::Type *> typeBuffer;
};
} // namespace Analysis