Skip to content

Commit 7605fc7

Browse files
committed
Revert "stash AttrInputExpr"
This reverts commit 4a0121eb0c4e85b2bfa5b8e95f525c8e0f23fd64.
1 parent 922cdae commit 7605fc7

17 files changed

+234
-74
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ TokenCollector::visit (Attribute &attrib)
155155
{
156156
switch (attrib.get_attr_input ().get_attr_input_type ())
157157
{
158-
case AST::AttrInput::AttrInputType::EXPR:
158+
case AST::AttrInput::AttrInputType::LITERAL:
159159
{
160-
visit (static_cast<AttrInputExpr &> (attrib.get_attr_input ()));
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 ()));
161166
break;
162167
}
163168
case AST::AttrInput::AttrInputType::META_ITEM:
@@ -814,10 +819,17 @@ TokenCollector::visit (LiteralExpr &expr)
814819
}
815820

816821
void
817-
TokenCollector::visit (AttrInputExpr &attr_input)
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)
818830
{
819831
push (Rust::Token::make (EQUAL, UNDEF_LOCATION));
820-
visit (attr_input.get_expr ());
832+
visit (macro.get_macro ());
821833
}
822834

823835
void

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

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class QualifiedPathInType;
7575
// rust-expr.h
7676
class ExprWithBlock;
7777
class LiteralExpr;
78-
class AttrInputExpr;
78+
class AttrInputLiteral;
79+
class AttrInputMacro;
7980
class MetaItemLitExpr;
8081
class MetaItemPathExpr;
8182
class OperatorExpr;

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

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

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

203209
void

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

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

7171
// rust-expr.h
7272
virtual void visit (LiteralExpr &expr) = 0;
73-
virtual void visit (AttrInputExpr &attr_input) = 0;
73+
virtual void visit (AttrInputLiteral &attr_input) = 0;
74+
virtual void visit (AttrInputMacro &attr_input) = 0;
7475
virtual void visit (MetaItemLitExpr &meta_item) = 0;
7576
virtual void visit (MetaItemPathExpr &meta_item) = 0;
7677
virtual void visit (BorrowExpr &expr) = 0;
@@ -267,7 +268,8 @@ class DefaultASTVisitor : public ASTVisitor
267268
virtual void visit (AST::QualifiedPathInExpression &path) override;
268269
virtual void visit (AST::QualifiedPathInType &path) override;
269270
virtual void visit (AST::LiteralExpr &expr) override;
270-
virtual void visit (AST::AttrInputExpr &attr_input) override;
271+
virtual void visit (AST::AttrInputLiteral &attr_input) override;
272+
virtual void visit (AST::AttrInputMacro &attr_input) override;
271273
virtual void visit (AST::MetaItemLitExpr &meta_item) override;
272274
virtual void visit (AST::MetaItemPathExpr &meta_item) override;
273275
virtual void visit (AST::BorrowExpr &expr) override;

gcc/rust/ast/rust-ast.cc

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

33453346
std::string
3346-
AttrInputExpr::as_string () const
3347+
AttrInputMacro::as_string () const
33473348
{
3348-
return " = " + expr->as_string ();
3349+
return " = " + macro->as_string ();
33493350
}
33503351

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

40684069
Attribute
@@ -4128,9 +4129,10 @@ MetaListNameValueStr::to_attribute () const
41284129
Attribute
41294130
MetaItemPathExpr::to_attribute () const
41304131
{
4131-
// TODO: should we move instead of clone?
4132-
return Attribute (path, std::unique_ptr<AttrInputExpr> (
4133-
new AttrInputExpr (expr->clone_expr ())));
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)));
41344136
}
41354137

41364138
std::vector<Attribute>
@@ -4234,6 +4236,19 @@ BlockExpr::normalize_tail_expr ()
42344236
}
42354237
}
42364238

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+
42374252
/* Visitor implementations - these are short but inlining can't happen anyway
42384253
* due to virtual functions and I didn't want to make the ast header includes
42394254
* any longer than they already are. */
@@ -4275,7 +4290,13 @@ LiteralExpr::accept_vis (ASTVisitor &vis)
42754290
}
42764291

42774292
void
4278-
AttrInputExpr::accept_vis (ASTVisitor &vis)
4293+
AttrInputLiteral::accept_vis (ASTVisitor &vis)
4294+
{
4295+
vis.visit (*this);
4296+
}
4297+
4298+
void
4299+
AttrInputMacro::accept_vis (ASTVisitor &vis)
42794300
{
42804301
vis.visit (*this);
42814302
}

gcc/rust/ast/rust-ast.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ 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+
366370
/* TODO: move applicable stuff into here or just don't include it because
367371
* nothing uses it A segment of a path (maybe) */
368372
class PathSegment
@@ -789,7 +793,8 @@ class AttrInput : public Visitable
789793
public:
790794
enum AttrInputType
791795
{
792-
EXPR,
796+
LITERAL,
797+
MACRO,
793798
META_ITEM,
794799
TOKEN_TREE,
795800
};
@@ -1055,6 +1060,10 @@ class DelimTokenTree : public TokenTree, public AttrInput
10551060
location_t get_locus () const { return locus; }
10561061
};
10571062

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

gcc/rust/ast/rust-expr.h

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

134-
class AttrInputExpr : public AttrInput
134+
// Literal expression attribute body (non-macro attribute)
135+
class AttrInputLiteral : public AttrInput
135136
{
136-
std::unique_ptr<Expr> expr;
137+
LiteralExpr literal_expr;
137138

138139
public:
139-
AttrInputExpr (std::unique_ptr<Expr> expr)
140-
: expr (std::move (expr))
140+
AttrInputLiteral (LiteralExpr lit_expr) : literal_expr (std::move (lit_expr))
141141
{}
142142

143-
AttrInputExpr (const AttrInputExpr &oth) : AttrInput (oth), expr (oth.expr->clone_expr ()) {}
143+
std::string as_string () const override
144+
{
145+
return " = " + literal_expr.as_string ();
146+
}
144147

145-
AttrInputExpr &operator= (const AttrInputExpr &oth)
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
146159
{
147-
AttrInput::operator= (oth);
148-
expr = oth.expr->clone_expr ();
149-
return *this;
160+
return AttrInput::AttrInputType::LITERAL;
150161
}
151162

152-
AttrInputExpr (AttrInputExpr &&oth) = default;
153-
AttrInputExpr &operator= (AttrInputExpr &&oth) = default;
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;
176+
177+
public:
178+
AttrInputMacro (std::unique_ptr<MacroInvocation> macro)
179+
: macro (std::move (macro))
180+
{}
181+
182+
AttrInputMacro (const AttrInputMacro &oth);
183+
184+
AttrInputMacro (AttrInputMacro &&oth) : macro (std::move (oth.macro)) {}
185+
186+
AttrInputMacro &operator= (const AttrInputMacro &oth);
187+
188+
AttrInputMacro &operator= (AttrInputMacro &&oth)
189+
{
190+
macro = std::move (oth.macro);
191+
return *this;
192+
}
154193

155194
std::string as_string () const override;
156195

@@ -162,19 +201,17 @@ class AttrInputExpr : public AttrInput
162201
// assuming this is like AttrInputLiteral
163202
bool is_meta_item () const override { return false; }
164203

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

169206
AttrInputType get_attr_input_type () const final override
170207
{
171-
return AttrInput::AttrInputType::EXPR;
208+
return AttrInput::AttrInputType::MACRO;
172209
}
173210

174211
protected:
175-
AttrInputExpr *clone_attr_input_impl () const override
212+
AttrInputMacro *clone_attr_input_impl () const override
176213
{
177-
return new AttrInputExpr (*this);
214+
return new AttrInputMacro (*this);
178215
}
179216
};
180217

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@ 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::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);
78+
rust_assert (input.get_attr_input_type () == AST::AttrInput::AttrInputType::LITERAL);
79+
auto &literal = static_cast<AST::AttrInputLiteral &> (input).get_literal ();
8380

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

gcc/rust/expand/rust-derive.h

Lines changed: 2 additions & 1 deletion
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 (AttrInputExpr &attr_input) override final{};
106+
virtual void visit (AttrInputMacro &expr) 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,6 +116,7 @@ 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{};
119120
virtual void visit (MetaItemLitExpr &meta_item) override final{};
120121
virtual void visit (MetaItemPathExpr &meta_item) override final{};
121122
virtual void visit (BorrowExpr &expr) override final{};

0 commit comments

Comments
 (0)