Skip to content

Commit 922cdae

Browse files
committed
stash AttrInputExpr
1 parent 052b937 commit 922cdae

17 files changed

+74
-234
lines changed

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,9 @@ TokenCollector::visit (Attribute &attrib)
155155
{
156156
switch (attrib.get_attr_input ().get_attr_input_type ())
157157
{
158-
case AST::AttrInput::AttrInputType::LITERAL:
158+
case AST::AttrInput::AttrInputType::EXPR:
159159
{
160-
visit (static_cast<AttrInputLiteral &> (attrib.get_attr_input ()));
161-
break;
162-
}
163-
case AST::AttrInput::AttrInputType::MACRO:
164-
{
165-
visit (static_cast<AttrInputMacro &> (attrib.get_attr_input ()));
160+
visit (static_cast<AttrInputExpr &> (attrib.get_attr_input ()));
166161
break;
167162
}
168163
case AST::AttrInput::AttrInputType::META_ITEM:
@@ -819,17 +814,10 @@ TokenCollector::visit (LiteralExpr &expr)
819814
}
820815

821816
void
822-
TokenCollector::visit (AttrInputLiteral &literal)
823-
{
824-
push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
825-
visit (literal.get_literal ());
826-
}
827-
828-
void
829-
TokenCollector::visit (AttrInputMacro &macro)
817+
TokenCollector::visit (AttrInputExpr &attr_input)
830818
{
831819
push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
832-
visit (macro.get_macro ());
820+
visit (attr_input.get_expr ());
833821
}
834822

835823
void

gcc/rust/ast/rust-ast-collector.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ class TokenCollector : public ASTVisitor
243243

244244
// rust-expr.h
245245
void visit (LiteralExpr &expr);
246-
void visit (AttrInputLiteral &attr_input);
247-
void visit (AttrInputMacro &attr_input);
246+
void visit (AttrInputExpr &attr_input);
248247
void visit (MetaItemLitExpr &meta_item);
249248
void visit (MetaItemPathExpr &meta_item);
250249
void visit (BorrowExpr &expr);

gcc/rust/ast/rust-ast-full-decls.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ class QualifiedPathInType;
7575
// rust-expr.h
7676
class ExprWithBlock;
7777
class LiteralExpr;
78-
class AttrInputLiteral;
79-
class AttrInputMacro;
78+
class AttrInputExpr;
8079
class MetaItemLitExpr;
8180
class MetaItemPathExpr;
8281
class OperatorExpr;

gcc/rust/ast/rust-ast-visitor.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,9 @@ DefaultASTVisitor::visit (AST::LiteralExpr &expr)
195195
}
196196

197197
void
198-
DefaultASTVisitor::visit (AST::AttrInputLiteral &attr_input)
198+
DefaultASTVisitor::visit (AST::AttrInputExpr &attr_input)
199199
{
200-
visit (attr_input.get_literal ());
201-
}
202-
203-
void
204-
DefaultASTVisitor::visit (AST::AttrInputMacro &attr_input)
205-
{
206-
visit (attr_input.get_macro ());
200+
visit (attr_input.get_expr ());
207201
}
208202

209203
void

gcc/rust/ast/rust-ast-visitor.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ class ASTVisitor
7070

7171
// rust-expr.h
7272
virtual void visit (LiteralExpr &expr) = 0;
73-
virtual void visit (AttrInputLiteral &attr_input) = 0;
74-
virtual void visit (AttrInputMacro &attr_input) = 0;
73+
virtual void visit (AttrInputExpr &attr_input) = 0;
7574
virtual void visit (MetaItemLitExpr &meta_item) = 0;
7675
virtual void visit (MetaItemPathExpr &meta_item) = 0;
7776
virtual void visit (BorrowExpr &expr) = 0;
@@ -268,8 +267,7 @@ class DefaultASTVisitor : public ASTVisitor
268267
virtual void visit (AST::QualifiedPathInExpression &path) override;
269268
virtual void visit (AST::QualifiedPathInType &path) override;
270269
virtual void visit (AST::LiteralExpr &expr) override;
271-
virtual void visit (AST::AttrInputLiteral &attr_input) override;
272-
virtual void visit (AST::AttrInputMacro &attr_input) override;
270+
virtual void visit (AST::AttrInputExpr &attr_input) override;
273271
virtual void visit (AST::MetaItemLitExpr &meta_item) override;
274272
virtual void visit (AST::MetaItemPathExpr &meta_item) override;
275273
virtual void visit (AST::BorrowExpr &expr) override;

gcc/rust/ast/rust-ast.cc

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ Attribute::get_traits_to_derive ()
322322
}
323323
break;
324324
case AST::AttrInput::TOKEN_TREE:
325-
case AST::AttrInput::LITERAL:
326-
case AST::AttrInput::MACRO:
325+
case AST::AttrInput::EXPR:
327326
rust_unreachable ();
328327
break;
329328
}
@@ -3344,9 +3343,9 @@ AttrInputMetaItemContainer::as_string () const
33443343
}
33453344

33463345
std::string
3347-
AttrInputMacro::as_string () const
3346+
AttrInputExpr::as_string () const
33483347
{
3349-
return " = " + macro->as_string ();
3348+
return " = " + expr->as_string ();
33503349
}
33513350

33523351
/* Override that calls the function recursively on all items contained within
@@ -4056,14 +4055,14 @@ Token::to_token_stream () const
40564055
Attribute
40574056
MetaNameValueStr::to_attribute () const
40584057
{
4059-
LiteralExpr lit_expr (str, Literal::LitType::STRING,
4060-
PrimitiveCoreType::CORETYPE_UNKNOWN, {}, str_locus);
4058+
auto lit_expr = std::make_unique<AST::LiteralExpr> (str, Literal::LitType::STRING,
4059+
PrimitiveCoreType::CORETYPE_UNKNOWN, std::vector<Attribute> (), str_locus);
40614060
// FIXME: What location do we put here? Is the literal above supposed to have
40624061
// an empty location as well?
40634062
// Should MetaNameValueStr keep a location?
40644063
return Attribute (SimplePath::from_str (ident.as_string (), ident_locus),
4065-
std::unique_ptr<AttrInputLiteral> (
4066-
new AttrInputLiteral (std::move (lit_expr))));
4064+
std::unique_ptr<AttrInputExpr> (
4065+
new AttrInputExpr (std::move (lit_expr))));
40674066
}
40684067

40694068
Attribute
@@ -4129,10 +4128,9 @@ MetaListNameValueStr::to_attribute () const
41294128
Attribute
41304129
MetaItemPathExpr::to_attribute () const
41314130
{
4132-
rust_assert (expr->is_literal ());
4133-
auto &lit = static_cast<LiteralExpr &> (*expr);
4134-
return Attribute (path, std::unique_ptr<AttrInputLiteral> (
4135-
new AttrInputLiteral (lit)));
4131+
// TODO: should we move instead of clone?
4132+
return Attribute (path, std::unique_ptr<AttrInputExpr> (
4133+
new AttrInputExpr (expr->clone_expr ())));
41364134
}
41374135

41384136
std::vector<Attribute>
@@ -4236,19 +4234,6 @@ BlockExpr::normalize_tail_expr ()
42364234
}
42374235
}
42384236

4239-
// needed here because "rust-expr.h" doesn't include "rust-macro.h"
4240-
AttrInputMacro::AttrInputMacro (const AttrInputMacro &oth)
4241-
: macro (oth.macro->clone_macro_invocation_impl ())
4242-
{}
4243-
4244-
AttrInputMacro &
4245-
AttrInputMacro::operator= (const AttrInputMacro &oth)
4246-
{
4247-
macro = std::unique_ptr<MacroInvocation> (
4248-
oth.macro->clone_macro_invocation_impl ());
4249-
return *this;
4250-
}
4251-
42524237
/* Visitor implementations - these are short but inlining can't happen anyway
42534238
* due to virtual functions and I didn't want to make the ast header includes
42544239
* any longer than they already are. */
@@ -4290,13 +4275,7 @@ LiteralExpr::accept_vis (ASTVisitor &vis)
42904275
}
42914276

42924277
void
4293-
AttrInputLiteral::accept_vis (ASTVisitor &vis)
4294-
{
4295-
vis.visit (*this);
4296-
}
4297-
4298-
void
4299-
AttrInputMacro::accept_vis (ASTVisitor &vis)
4278+
AttrInputExpr::accept_vis (ASTVisitor &vis)
43004279
{
43014280
vis.visit (*this);
43024281
}

gcc/rust/ast/rust-ast.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,6 @@ struct Literal
363363
bool is_error () const { return type == ERROR; }
364364
};
365365

366-
/* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr
367-
* to be defined */
368-
class AttrInputLiteral;
369-
370366
/* TODO: move applicable stuff into here or just don't include it because
371367
* nothing uses it A segment of a path (maybe) */
372368
class PathSegment
@@ -793,8 +789,7 @@ class AttrInput : public Visitable
793789
public:
794790
enum AttrInputType
795791
{
796-
LITERAL,
797-
MACRO,
792+
EXPR,
798793
META_ITEM,
799794
TOKEN_TREE,
800795
};
@@ -1060,10 +1055,6 @@ class DelimTokenTree : public TokenTree, public AttrInput
10601055
location_t get_locus () const { return locus; }
10611056
};
10621057

1063-
/* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr
1064-
* to be defined */
1065-
class AttrInputLiteral;
1066-
10671058
// abstract base meta item class
10681059
class MetaItem : public MetaItemInner
10691060
{

gcc/rust/ast/rust-expr.h

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -131,66 +131,27 @@ class LiteralExpr : public ExprWithoutBlock
131131
}
132132
};
133133

134-
// Literal expression attribute body (non-macro attribute)
135-
class AttrInputLiteral : public AttrInput
134+
class AttrInputExpr : public AttrInput
136135
{
137-
LiteralExpr literal_expr;
138-
139-
public:
140-
AttrInputLiteral (LiteralExpr lit_expr) : literal_expr (std::move (lit_expr))
141-
{}
142-
143-
std::string as_string () const override
144-
{
145-
return " = " + literal_expr.as_string ();
146-
}
147-
148-
void accept_vis (ASTVisitor &vis) override;
149-
150-
/* this can never be a cfg predicate - cfg and cfg_attr require a token-tree
151-
* cfg */
152-
bool check_cfg_predicate (const Session &) const override { return false; }
153-
154-
bool is_meta_item () const override { return false; }
155-
156-
LiteralExpr &get_literal () { return literal_expr; }
157-
158-
AttrInputType get_attr_input_type () const final override
159-
{
160-
return AttrInput::AttrInputType::LITERAL;
161-
}
162-
163-
protected:
164-
/* Use covariance to implement clone function as returning this object rather
165-
* than base */
166-
AttrInputLiteral *clone_attr_input_impl () const override
167-
{
168-
return new AttrInputLiteral (*this);
169-
}
170-
};
171-
172-
// Like an AttrInputLiteral, but stores a MacroInvocation
173-
class AttrInputMacro : public AttrInput
174-
{
175-
std::unique_ptr<MacroInvocation> macro;
136+
std::unique_ptr<Expr> expr;
176137

177138
public:
178-
AttrInputMacro (std::unique_ptr<MacroInvocation> macro)
179-
: macro (std::move (macro))
139+
AttrInputExpr (std::unique_ptr<Expr> expr)
140+
: expr (std::move (expr))
180141
{}
181142

182-
AttrInputMacro (const AttrInputMacro &oth);
183-
184-
AttrInputMacro (AttrInputMacro &&oth) : macro (std::move (oth.macro)) {}
143+
AttrInputExpr (const AttrInputExpr &oth) : AttrInput (oth), expr (oth.expr->clone_expr ()) {}
185144

186-
AttrInputMacro &operator= (const AttrInputMacro &oth);
187-
188-
AttrInputMacro &operator= (AttrInputMacro &&oth)
145+
AttrInputExpr &operator= (const AttrInputExpr &oth)
189146
{
190-
macro = std::move (oth.macro);
147+
AttrInput::operator= (oth);
148+
expr = oth.expr->clone_expr ();
191149
return *this;
192150
}
193151

152+
AttrInputExpr (AttrInputExpr &&oth) = default;
153+
AttrInputExpr &operator= (AttrInputExpr &&oth) = default;
154+
194155
std::string as_string () const override;
195156

196157
void accept_vis (ASTVisitor &vis) override;
@@ -201,17 +162,19 @@ class AttrInputMacro : public AttrInput
201162
// assuming this is like AttrInputLiteral
202163
bool is_meta_item () const override { return false; }
203164

204-
std::unique_ptr<MacroInvocation> &get_macro () { return macro; }
165+
std::unique_ptr<Expr> &get_expr_ptr () { return expr; }
166+
167+
Expr &get_expr () { return *expr; }
205168

206169
AttrInputType get_attr_input_type () const final override
207170
{
208-
return AttrInput::AttrInputType::MACRO;
171+
return AttrInput::AttrInputType::EXPR;
209172
}
210173

211174
protected:
212-
AttrInputMacro *clone_attr_input_impl () const override
175+
AttrInputExpr *clone_attr_input_impl () const override
213176
{
214-
return new AttrInputMacro (*this);
177+
return new AttrInputExpr (*this);
215178
}
216179
};
217180

gcc/rust/backend/rust-compile-base.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ tl::optional<GGC::Ident> inline get_unmangled_name (const std::string &function_
7575
rust_assert (attr.has_attr_input ());
7676
auto &input = attr.get_attr_input ();
7777

78-
rust_assert (input.get_attr_input_type () == AST::AttrInput::AttrInputType::LITERAL);
79-
auto &literal = static_cast<AST::AttrInputLiteral &> (input).get_literal ();
78+
rust_assert (input.get_attr_input_type () == AST::AttrInput::AttrInputType::EXPR);
79+
auto &input_expr = static_cast<AST::AttrInputExpr &> (input).get_expr ();
80+
81+
rust_assert (input_expr.is_literal ());
82+
auto &literal = static_cast<AST::LiteralExpr &> (input_expr);
8083

8184
rust_assert (literal.get_lit_type () == AST::Literal::LitType::STRING || literal.get_lit_type () == AST::Literal::LitType::RAW_STRING);
8285
// TODO: special handling for STRING/RAW_STRING?

gcc/rust/expand/rust-derive.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class DeriveVisitor : public AST::ASTVisitor
103103
virtual void visit (Token &tok) override final{};
104104
virtual void visit (DelimTokenTree &delim_tok_tree) override final{};
105105
virtual void visit (AttrInputMetaItemContainer &input) override final{};
106-
virtual void visit (AttrInputMacro &expr) override final{};
106+
virtual void visit (AttrInputExpr &attr_input) override final{};
107107
virtual void visit (IdentifierExpr &ident_expr) override final{};
108108
virtual void visit (Lifetime &lifetime) override final{};
109109
virtual void visit (LifetimeParam &lifetime_param) override final{};
@@ -116,7 +116,6 @@ class DeriveVisitor : public AST::ASTVisitor
116116
virtual void visit (QualifiedPathInExpression &path) override final{};
117117
virtual void visit (QualifiedPathInType &path) override final{};
118118
virtual void visit (LiteralExpr &expr) override final{};
119-
virtual void visit (AttrInputLiteral &attr_input) override final{};
120119
virtual void visit (MetaItemLitExpr &meta_item) override final{};
121120
virtual void visit (MetaItemPathExpr &meta_item) override final{};
122121
virtual void visit (BorrowExpr &expr) override final{};

0 commit comments

Comments
 (0)