diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-06-24 11:54:08 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-06-30 16:48:18 +0200 |
commit | 49276efb07ef18098c0f72970acf523a1fc31eb8 (patch) | |
tree | 280ad30fe8ef571ebd5be9b5001a96af3f5338cf /gcc | |
parent | 81abc8623cb75fa18315c65e94c5965ec36fdb54 (diff) | |
download | gcc-49276efb07ef18098c0f72970acf523a1fc31eb8.zip gcc-49276efb07ef18098c0f72970acf523a1fc31eb8.tar.gz gcc-49276efb07ef18098c0f72970acf523a1fc31eb8.tar.bz2 |
ast: Keep ConstGenericArg as default value for ConstGenericParam
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-full-test.cc | 4 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 69 | ||||
-rw-r--r-- | gcc/rust/ast/rust-path.h | 77 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-type.h | 12 | ||||
-rw-r--r-- | 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 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; |