diff options
author | Philip Herron <herron.philip@googlemail.com> | 2020-05-20 18:21:35 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2020-11-28 21:13:14 +0000 |
commit | e83e46cd082ad4dfdced2eba3369f7e3ae3802ec (patch) | |
tree | b70a6bd90de3e082d76b65a203fc28bab2876c7d /gcc | |
parent | 3601bdbe25be75a05b98f356610963b3f5557b4b (diff) | |
download | gcc-e83e46cd082ad4dfdced2eba3369f7e3ae3802ec.zip gcc-e83e46cd082ad4dfdced2eba3369f7e3ae3802ec.tar.gz gcc-e83e46cd082ad4dfdced2eba3369f7e3ae3802ec.tar.bz2 |
add type resolution to parameters and return types of functions
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/analysis/rust-type-resolution.cc | 42 | ||||
-rw-r--r-- | gcc/rust/analysis/rust-type-resolution.h | 1 |
2 files changed, 43 insertions, 0 deletions
diff --git a/gcc/rust/analysis/rust-type-resolution.cc b/gcc/rust/analysis/rust-type-resolution.cc index 48ea699..64296d0 100644 --- a/gcc/rust/analysis/rust-type-resolution.cc +++ b/gcc/rust/analysis/rust-type-resolution.cc @@ -100,6 +100,38 @@ TypeResolution::typesAreCompatible (AST::Type *lhs, AST::Type *rhs, return false; } + AST::Type *val = NULL; + if (!typeScope.Lookup (lhsTypeStr, &val)) + { + rust_error_at (locus, "unknown type"); + return false; + } + + return true; +} + +bool +TypeResolution::isTypeInScope (AST::Type *type, Location locus) +{ + auto before = typeComparisonBuffer.size (); + type->accept_vis (*this); + if (typeComparisonBuffer.size () <= before) + { + rust_error_at (locus, "unable to decipher type: %s", + type->as_string ().c_str ()); + return false; + } + + auto t = typeComparisonBuffer.back (); + typeComparisonBuffer.pop_back (); + + AST::Type *val = NULL; + if (!typeScope.Lookup (t, &val)) + { + rust_error_at (locus, "unknown type"); + return false; + } + return true; } @@ -619,6 +651,9 @@ TypeResolution::visit (AST::Function &function) scope.Push (); for (auto ¶m : function.function_params) { + if (!isTypeInScope (param.type.get (), param.locus)) + return; + auto before = letPatternBuffer.size (); param.param_name->accept_vis (*this); if (letPatternBuffer.size () <= before) @@ -632,6 +667,13 @@ TypeResolution::visit (AST::Function &function) scope.Insert (paramName.variable_ident, param.type.get ()); } + // ensure the return type is resolved + if (function.has_function_return_type ()) + { + if (!isTypeInScope (function.return_type.get (), function.locus)) + return; + } + // walk the expression body for (auto &stmt : function.function_body->statements) { diff --git a/gcc/rust/analysis/rust-type-resolution.h b/gcc/rust/analysis/rust-type-resolution.h index 58d36db..1155267 100644 --- a/gcc/rust/analysis/rust-type-resolution.h +++ b/gcc/rust/analysis/rust-type-resolution.h @@ -221,6 +221,7 @@ private: bool go () override; bool typesAreCompatible (AST::Type *lhs, AST::Type *rhs, Location locus); AST::Function *lookupFndecl (AST::Expr *expr); + bool isTypeInScope (AST::Type *type, Location locus); Scope<AST::Function *> functionScope; }; |