From d9fb7d06ca79e8c0f784da02696b436bc50e2f49 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Thu, 16 Jun 2022 05:00:29 +0200 Subject: ast: Add ConstGenericParam class This also implements all the necessary boilerplate to allow visitors to work properly. For now, no operation is performed on const generic params - similarly to lifetimes --- gcc/rust/ast/rust-ast-dump.cc | 4 +++ gcc/rust/ast/rust-ast-dump.h | 1 + gcc/rust/ast/rust-ast-full-decls.h | 1 + gcc/rust/ast/rust-ast-full-test.cc | 19 +++++++++++ gcc/rust/ast/rust-ast-visitor.h | 1 + gcc/rust/ast/rust-ast.h | 66 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index d1105c9..6bf2bee 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -83,6 +83,10 @@ void Dump::visit (LifetimeParam &lifetime_param) {} +void +Dump::visit (ConstGenericParam &lifetime_param) +{} + // rust-path.h void Dump::visit (PathInExpression &path) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index e6c6ca4..51c6f84 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -71,6 +71,7 @@ private: void visit (IdentifierExpr &ident_expr); void visit (Lifetime &lifetime); void visit (LifetimeParam &lifetime_param); + void visit (ConstGenericParam &const_param); // rust-path.h void visit (PathInExpression &path); diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index f3af42e..47f3321 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -50,6 +50,7 @@ class TypeParamBound; class Lifetime; class GenericParam; class LifetimeParam; +class ConstGenericParam; class MacroItem; class TraitItem; class InherentImplItem; diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 92325f1..d2940dd 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -18,6 +18,7 @@ along with GCC; see the file COPYING3. If not see . */ // FIXME: This does not work on Windows +#include #include #include "rust-ast-full.h" @@ -2357,6 +2358,18 @@ LifetimeParam::as_string () const } std::string +ConstGenericParam::as_string () const +{ + std::string str ("ConstGenericParam: "); + str += "const " + name + ": " + type->as_string (); + + if (default_value) + str += " = " + default_value->as_string (); + + return str; +} + +std::string MacroMatchFragment::as_string () const { return "$" + ident + ": " + frag_spec.as_string (); @@ -4879,6 +4892,12 @@ LifetimeParam::accept_vis (ASTVisitor &vis) } void +ConstGenericParam::accept_vis (ASTVisitor &vis) +{ + vis.visit (*this); +} + +void PathInExpression::accept_vis (ASTVisitor &vis) { vis.visit (*this); diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index d3383b4..bbb0477 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -49,6 +49,7 @@ public: virtual void visit (Lifetime &lifetime) = 0; // virtual void visit(GenericParam& generic_param) = 0; virtual void visit (LifetimeParam &lifetime_param) = 0; + virtual void visit (ConstGenericParam &const_param) = 0; // virtual void visit(TraitItem& trait_item) = 0; // virtual void visit(InherentImplItem& inherent_impl_item) = 0; // virtual void visit(TraitImplItem& trait_impl_item) = 0; diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 2d7d31a..b4d401c 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1342,6 +1342,72 @@ 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; } + + 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 { -- cgit v1.1