public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Keep ConstGenericArg as default value for ConstGenericParam
@ 2022-06-30 18:51 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-30 18:51 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:49276efb07ef18098c0f72970acf523a1fc31eb8
commit 49276efb07ef18098c0f72970acf523a1fc31eb8
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date: Fri Jun 24 11:54:08 2022 +0200
ast: Keep ConstGenericArg as default value for ConstGenericParam
Diff:
---
gcc/rust/ast/rust-ast-full-test.cc | 4 +-
gcc/rust/ast/rust-ast.h | 69 ----------------------------------
gcc/rust/ast/rust-path.h | 77 +++++++++++++++++++++++++++++++++++++-
gcc/rust/hir/rust-ast-lower-type.h | 12 ++++--
gcc/rust/parse/rust-parse-impl.h | 2 +-
5 files changed, 87 insertions(+), 77 deletions(-)
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index 81791135bc4..d98a7cfeedb 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 51fe3c49c59..461a2460f8f 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 d2d925a01ce..36f1c0478cb 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 dc20be7cd11..2bcf0ee23c4 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 a36107b2319..6a1a3a58693 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;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-30 18:51 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-30 18:51 [gcc/devel/rust/master] ast: Keep ConstGenericArg as default value for ConstGenericParam Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).