From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 3CB4A383EC7A; Thu, 30 Jun 2022 18:51:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3CB4A383EC7A Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] ast: Keep ConstGenericArg as default value for ConstGenericParam X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 81abc8623cb75fa18315c65e94c5965ec36fdb54 X-Git-Newrev: 49276efb07ef18098c0f72970acf523a1fc31eb8 Message-Id: <20220630185103.3CB4A383EC7A@sourceware.org> Date: Thu, 30 Jun 2022 18:51:03 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jun 2022 18:51:03 -0000 https://gcc.gnu.org/g:49276efb07ef18098c0f72970acf523a1fc31eb8 commit 49276efb07ef18098c0f72970acf523a1fc31eb8 Author: Arthur Cohen 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 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 default_value; - - Attribute outer_attr; - Location locus; - -public: - ConstGenericParam (Identifier name, std::unique_ptr type, - std::unique_ptr 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 &get_type () - { - rust_assert (has_type ()); - - return type; - } - - std::unique_ptr &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 &get_expression () { return expression; } + const std::unique_ptr &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 type; + + /** + * Default value for the const generic parameter + */ + ConstGenericArg default_value; + + Attribute outer_attr; + Location locus; + +public: + ConstGenericParam (Identifier name, std::unique_ptr 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 &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::parse_generic_param (EndTokenPred is_end_token) param = std::unique_ptr ( 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;