aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-06-30 15:52:01 +0000
committerGitHub <noreply@github.com>2022-06-30 15:52:01 +0000
commit33912c1a08d3892d3c72506aed38771754b9cf54 (patch)
treede1e4342203a3567c8a719c8d689a3a097abc71a /gcc/rust
parent81abc8623cb75fa18315c65e94c5965ec36fdb54 (diff)
parent93f63a94d4389f31d5b225ad220ebea5f7288fb7 (diff)
downloadgcc-33912c1a08d3892d3c72506aed38771754b9cf54.zip
gcc-33912c1a08d3892d3c72506aed38771754b9cf54.tar.gz
gcc-33912c1a08d3892d3c72506aed38771754b9cf54.tar.bz2
Merge #1336
1336: Resolve const generic parameters r=CohenArthur a=CohenArthur Fixes #1319. This can only resolve the simpler test cases: Anything that is ambiguous is ignored and does not get resolved. This will be fixed very soon :) Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/ast/rust-ast-full-test.cc4
-rw-r--r--gcc/rust/ast/rust-ast.h69
-rw-r--r--gcc/rust/ast/rust-path.h77
-rw-r--r--gcc/rust/hir/rust-ast-lower-type.h12
-rw-r--r--gcc/rust/parse/rust-parse-impl.h2
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-item.cc95
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-item.h11
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-stmt.cc2
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-stmt.h26
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-type.h27
10 files changed, 154 insertions, 171 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index 8179113..d98a7cf 100644
--- a/gcc/rust/ast/rust-ast-full-test.cc
+++ b/gcc/rust/ast/rust-ast-full-test.cc
@@ -2365,8 +2365,8 @@ ConstGenericParam::as_string () const
std::string str ("ConstGenericParam: ");
str += "const " + name + ": " + type->as_string ();
- if (default_value)
- str += " = " + default_value->as_string ();
+ if (has_default_value ())
+ str += " = " + get_default_value ().as_string ();
return str;
}
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 51fe3c4..461a246 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1344,75 +1344,6 @@ protected:
}
};
-/**
- * Representation of const generic parameters
- */
-class ConstGenericParam : public GenericParam
-{
- /* Name of the parameter */
- Identifier name;
-
- /* Mandatory type of the const parameter - a null pointer is an error */
- std::unique_ptr<AST::Type> type;
-
- /**
- * Default value for the const generic parameter - nullptr indicates a lack
- * of default value, not an error, as these errors are reported during
- * parsing.
- */
- std::unique_ptr<AST::Expr> default_value;
-
- Attribute outer_attr;
- Location locus;
-
-public:
- ConstGenericParam (Identifier name, std::unique_ptr<AST::Type> type,
- std::unique_ptr<AST::Expr> default_value,
- Attribute outer_attr, Location locus)
- : name (name), type (std::move (type)),
- default_value (std::move (default_value)), outer_attr (outer_attr),
- locus (locus)
- {}
-
- ConstGenericParam (const ConstGenericParam &other)
- : GenericParam (), name (other.name), type (other.type->clone_type ()),
- outer_attr (other.outer_attr), locus (other.locus)
- {
- if (other.default_value)
- default_value = other.default_value->clone_expr ();
- }
-
- bool has_type () { return type != nullptr; }
- bool has_default_value () { return default_value != nullptr; }
-
- const Identifier &get_name () const { return name; }
-
- std::unique_ptr<AST::Type> &get_type ()
- {
- rust_assert (has_type ());
-
- return type;
- }
-
- std::unique_ptr<AST::Expr> &get_default_value () { return default_value; }
-
- std::string as_string () const override;
-
- void accept_vis (ASTVisitor &vis) override;
-
- Location get_locus () const override final { return locus; }
-
- Kind get_kind () const override final { return Kind::Const; }
-
-protected:
- /* Use covariance to implement clone function as returning this object rather
- * than base */
- ConstGenericParam *clone_generic_param_impl () const override
- {
- return new ConstGenericParam (*this);
- }
-};
-
// A macro item AST node - abstract base class
class MacroItem : public Item
{
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index d2d925a..36f1c04 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -191,7 +191,12 @@ public:
Kind get_kind () const { return kind; }
- std::unique_ptr<AST::Expr> &get_expression () { return expression; }
+ const std::unique_ptr<AST::Expr> &get_expression () const
+ {
+ rust_assert (kind == Kind::Clear);
+
+ return expression;
+ }
std::string as_string () const
{
@@ -241,6 +246,76 @@ private:
Location locus;
};
+/**
+ * Representation of const generic parameters
+ */
+class ConstGenericParam : public GenericParam
+{
+ /* Name of the parameter */
+ Identifier name;
+
+ /* Mandatory type of the const parameter - a null pointer is an error */
+ std::unique_ptr<AST::Type> type;
+
+ /**
+ * Default value for the const generic parameter
+ */
+ ConstGenericArg default_value;
+
+ Attribute outer_attr;
+ Location locus;
+
+public:
+ ConstGenericParam (Identifier name, std::unique_ptr<AST::Type> type,
+ ConstGenericArg default_value, Attribute outer_attr,
+ Location locus)
+ : name (name), type (std::move (type)),
+ default_value (std::move (default_value)), outer_attr (outer_attr),
+ locus (locus)
+ {}
+
+ ConstGenericParam (const ConstGenericParam &other)
+ : GenericParam (), name (other.name), type (other.type->clone_type ()),
+ default_value (other.default_value), outer_attr (other.outer_attr),
+ locus (other.locus)
+ {}
+
+ bool has_type () const { return type != nullptr; }
+ bool has_default_value () const { return !default_value.is_error (); }
+
+ const Identifier &get_name () const { return name; }
+
+ std::unique_ptr<AST::Type> &get_type ()
+ {
+ rust_assert (has_type ());
+
+ return type;
+ }
+
+ const ConstGenericArg &get_default_value () const
+ {
+ rust_assert (has_default_value ());
+
+ return default_value;
+ }
+
+ std::string as_string () const override;
+
+ void accept_vis (ASTVisitor &vis) override;
+
+ Location get_locus () const override final { return locus; }
+
+ Kind get_kind () const override final { return Kind::Const; }
+
+protected:
+ /* Use covariance to implement clone function as returning this object rather
+ * than base */
+ ConstGenericParam *clone_generic_param_impl () const override
+ {
+ return new ConstGenericParam (*this);
+ }
+};
+
// Generic arguments allowed in each path expression segment - inline?
struct GenericArgs
{
diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h
index dc20be7..2bcf0ee 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -376,10 +376,14 @@ public:
mappings->get_next_localdef_id (crate_num));
auto type = ASTLoweringType::translate (param.get_type ().get ());
- auto default_expr
- = param.has_default_value ()
- ? ASTLoweringExpr::translate (param.get_default_value ().get ())
- : nullptr;
+ // FIXME: Arthur: Remove the second guard once we disambiguate in the
+ // resolveer
+ HIR::Expr *default_expr = nullptr;
+ if (param.has_default_value ()
+ && param.get_default_value ().get_kind ()
+ == AST::ConstGenericArg::Kind::Clear)
+ default_expr = ASTLoweringExpr::translate (
+ param.get_default_value ().get_expression ().get ());
translated
= new HIR::ConstGenericParam (param.get_name (),
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index a36107b..6a1a3a5 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -2901,7 +2901,7 @@ Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
param = std::unique_ptr<AST::ConstGenericParam> (
new AST::ConstGenericParam (name_token->get_str (), std::move (type),
- nullptr, std::move (outer_attrs),
+ default_expr, std::move (outer_attrs),
token->get_locus ()));
break;
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc
index 1ad91af..62518b6 100644
--- a/gcc/rust/resolve/rust-ast-resolve-item.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-item.cc
@@ -73,10 +73,8 @@ ResolveTraitItems::visit (AST::TraitItemFunc &func)
AST::TraitFunctionDecl &function = func.get_trait_function_decl ();
if (function.has_generics ())
- {
- for (auto &generic : function.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : function.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (function.has_return_type ())
ResolveType::go (function.get_return_type ().get ());
@@ -122,10 +120,8 @@ ResolveTraitItems::visit (AST::TraitItemMethod &func)
AST::TraitMethodDecl &function = func.get_trait_method_decl ();
if (function.has_generics ())
- {
- for (auto &generic : function.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : function.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (function.has_return_type ())
ResolveType::go (function.get_return_type ().get ());
@@ -210,10 +206,8 @@ ResolveItem::visit (AST::TypeAlias &alias)
resolver->get_type_scope ().push (scope_node_id);
if (alias.has_generics ())
- {
- for (auto &generic : alias.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : alias.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (alias.has_where_clause ())
ResolveWhereClause::Resolve (alias.get_where_clause ());
@@ -274,12 +268,8 @@ ResolveItem::visit (AST::TupleStruct &struct_decl)
resolver->get_type_scope ().push (scope_node_id);
if (struct_decl.has_generics ())
- {
- for (auto &generic : struct_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : struct_decl.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (struct_decl.has_where_clause ())
ResolveWhereClause::Resolve (struct_decl.get_where_clause ());
@@ -313,12 +303,8 @@ ResolveItem::visit (AST::Enum &enum_decl)
resolver->get_type_scope ().push (scope_node_id);
if (enum_decl.has_generics ())
- {
- for (auto &generic : enum_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : enum_decl.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, cpath);
if (enum_decl.has_where_clause ())
ResolveWhereClause::Resolve (enum_decl.get_where_clause ());
@@ -411,12 +397,8 @@ ResolveItem::visit (AST::StructStruct &struct_decl)
resolver->get_type_scope ().push (scope_node_id);
if (struct_decl.has_generics ())
- {
- for (auto &generic : struct_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : struct_decl.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (struct_decl.has_where_clause ())
ResolveWhereClause::Resolve (struct_decl.get_where_clause ());
@@ -450,12 +432,8 @@ ResolveItem::visit (AST::Union &union_decl)
resolver->get_type_scope ().push (scope_node_id);
if (union_decl.has_generics ())
- {
- for (auto &generic : union_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : union_decl.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (union_decl.has_where_clause ())
ResolveWhereClause::Resolve (union_decl.get_where_clause ());
@@ -523,10 +501,8 @@ ResolveItem::visit (AST::Function &function)
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
if (function.has_generics ())
- {
- for (auto &generic : function.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : function.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
// resolve any where clause items
if (function.has_where_clause ())
@@ -566,12 +542,8 @@ ResolveItem::visit (AST::InherentImpl &impl_block)
resolve_visibility (impl_block.get_visibility ());
if (impl_block.has_generics ())
- {
- for (auto &generic : impl_block.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : impl_block.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
// resolve any where clause items
if (impl_block.has_where_clause ())
@@ -657,10 +629,8 @@ ResolveItem::visit (AST::Method &method)
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
if (method.has_generics ())
- {
- for (auto &generic : method.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : method.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
// resolve any where clause items
if (method.has_where_clause ())
@@ -719,12 +689,8 @@ ResolveItem::visit (AST::TraitImpl &impl_block)
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
if (impl_block.has_generics ())
- {
- for (auto &generic : impl_block.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : impl_block.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
// resolve any where clause items
if (impl_block.has_where_clause ())
@@ -836,9 +802,7 @@ ResolveItem::visit (AST::Trait &trait)
CanonicalPath Self = CanonicalPath::get_big_self (trait.get_node_id ());
for (auto &generic : trait.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
// Self is an implicit TypeParam so lets mark it as such
resolver->get_type_scope ().append_reference_for_def (
@@ -900,7 +864,7 @@ ResolveItem::resolve_impl_item (AST::InherentImplItem *item,
void
ResolveItem::resolve_extern_item (AST::ExternalItem *item)
{
- ResolveExternItem::go (item);
+ ResolveExternItem::go (item, prefix, canonical_prefix);
}
static void
@@ -1070,9 +1034,10 @@ ResolveImplItems::visit (AST::TypeAlias &alias)
}
void
-ResolveExternItem::go (AST::ExternalItem *item)
+ResolveExternItem::go (AST::ExternalItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix)
{
- ResolveExternItem resolver;
+ ResolveExternItem resolver (prefix, canonical_prefix);
item->accept_vis (resolver);
}
@@ -1092,10 +1057,8 @@ ResolveExternItem::visit (AST::ExternalFunctionItem &function)
// resolve the generics
if (function.has_generics ())
- {
- for (auto &generic : function.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : function.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (function.has_return_type ())
ResolveType::go (function.get_return_type ().get ());
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h
index 2824d09..ce521f0 100644
--- a/gcc/rust/resolve/rust-ast-resolve-item.h
+++ b/gcc/rust/resolve/rust-ast-resolve-item.h
@@ -118,13 +118,20 @@ class ResolveExternItem : public ResolverBase
using Rust::Resolver::ResolverBase::visit;
public:
- static void go (AST::ExternalItem *item);
+ static void go (AST::ExternalItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
void visit (AST::ExternalFunctionItem &function) override;
void visit (AST::ExternalStaticItem &item) override;
private:
- ResolveExternItem () : ResolverBase () {}
+ ResolveExternItem (const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix)
+ : ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix)
+ {}
+
+ const CanonicalPath &prefix;
+ const CanonicalPath &canonical_prefix;
};
} // namespace Resolver
diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.cc b/gcc/rust/resolve/rust-ast-resolve-stmt.cc
index 6fc929f..1ce3df0 100644
--- a/gcc/rust/resolve/rust-ast-resolve-stmt.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-stmt.cc
@@ -30,7 +30,7 @@ ResolveStmt::visit (AST::ExternBlock &extern_block)
{
ResolveToplevelExternItem::go (item.get (),
CanonicalPath::create_empty ());
- ResolveExternItem::go (item.get ());
+ ResolveExternItem::go (item.get (), prefix, canonical_prefix);
}
}
diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h b/gcc/rust/resolve/rust-ast-resolve-stmt.h
index 202a280..8867845 100644
--- a/gcc/rust/resolve/rust-ast-resolve-stmt.h
+++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h
@@ -111,9 +111,7 @@ public:
if (struct_decl.has_generics ())
{
for (auto &generic : struct_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
}
for (AST::TupleField &field : struct_decl.get_fields ())
@@ -145,9 +143,7 @@ public:
if (enum_decl.has_generics ())
{
for (auto &generic : enum_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
}
for (auto &variant : enum_decl.get_variants ())
@@ -271,9 +267,7 @@ public:
if (struct_decl.has_generics ())
{
for (auto &generic : struct_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
}
for (AST::StructField &field : struct_decl.get_fields ())
@@ -308,12 +302,8 @@ public:
resolver->get_type_scope ().push (scope_node_id);
if (union_decl.has_generics ())
- {
- for (auto &generic : union_decl.get_generic_params ())
- {
- ResolveGenericParam::go (generic.get ());
- }
- }
+ for (auto &generic : union_decl.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
for (AST::StructField &field : union_decl.get_variants ())
{
@@ -352,10 +342,8 @@ public:
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
if (function.has_generics ())
- {
- for (auto &generic : function.get_generic_params ())
- ResolveGenericParam::go (generic.get ());
- }
+ for (auto &generic : function.get_generic_params ())
+ ResolveGenericParam::go (generic.get (), prefix, canonical_prefix);
if (function.has_return_type ())
ResolveType::go (function.get_return_type ().get ());
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h b/gcc/rust/resolve/rust-ast-resolve-type.h
index 965df42..946cdd4 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.h
+++ b/gcc/rust/resolve/rust-ast-resolve-type.h
@@ -20,6 +20,7 @@
#define RUST_AST_RESOLVE_TYPE_H
#include "rust-ast-resolve-base.h"
+#include "rust-ast-resolve-expr.h"
#include "rust-ast-full.h"
namespace Rust {
@@ -146,17 +147,23 @@ class ResolveGenericParam : public ResolverBase
using Rust::Resolver::ResolverBase::visit;
public:
- static NodeId go (AST::GenericParam *param)
+ static NodeId go (AST::GenericParam *param, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix)
{
- ResolveGenericParam resolver;
+ ResolveGenericParam resolver (prefix, canonical_prefix);
param->accept_vis (resolver);
return resolver.resolved_node;
}
- void visit (AST::ConstGenericParam &) override
+ void visit (AST::ConstGenericParam &param) override
{
- // For now do not do anything and accept everything.
- // FIXME: This needs to change soon!
+ ResolveType::go (param.get_type ().get ());
+
+ if (param.has_default_value ())
+ ResolveExpr::go (param.get_default_value ().get_expression ().get (),
+ prefix, canonical_prefix);
+
+ ok = true;
}
void visit (AST::TypeParam &param) override
@@ -188,7 +195,15 @@ public:
}
private:
- ResolveGenericParam () : ResolverBase () {}
+ ResolveGenericParam (const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix)
+ : ResolverBase (), ok (false), prefix (prefix),
+ canonical_prefix (canonical_prefix)
+ {}
+
+ bool ok;
+ const CanonicalPath &prefix;
+ const CanonicalPath &canonical_prefix;
};
class ResolveWhereClause : public ResolverBase