Skip to content

Commit b77dfdb

Browse files
committed
Parse expression instead of literal in attributes
gcc/rust/ChangeLog: * ast/rust-ast.cc (AttributeParser::parse_path_meta_item): Parse expression instead of literal. Update variant name. (MetaItemPathLit::to_attribute): Remove function. (AttributeParser::parse_path_meta_item): Update name. (MetaItemPathLit::check_cfg_predicate): Likewise. (MetaItemPathExpr::check_cfg_predicate): Likewise. (MetaItemPathLit::accept_vis): Likewise. (MetaItemPathExpr::accept_vis): Likewise. * ast/rust-ast-collector.h: Update prototype and adapt code to new expression. * ast/rust-ast-collector.cc: Update code to expr. * ast/rust-ast-full-decls.h (class MetaItemPathLit): Likewise. (class MetaItemPathExpr): Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.h: Likewise. * ast/rust-ast.h (class MetaItemPathLit): Rename class from here... (class MetaItemPathExpr): ... to here. * ast/rust-expr.h (class MetaItemPathLit): Rename class from here... (class MetaItemPathExpr): ...to here. * expand/rust-derive.h: Update class name. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise. * expand/rust-expand-visitor.h: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h: Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-base.h: Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise. * resolve/rust-early-name-resolver.h: Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. * util/rust-attributes.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 165768e commit b77dfdb

19 files changed

+63
-53
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,13 +840,13 @@ TokenCollector::visit (MetaItemLitExpr &item)
840840
}
841841

842842
void
843-
TokenCollector::visit (MetaItemPathLit &item)
843+
TokenCollector::visit (MetaItemPathExpr &item)
844844
{
845-
auto path = item.get_path ();
846-
auto lit = item.get_literal ();
845+
auto &path = item.get_path ();
846+
auto &expr = item.get_expr ();
847847
visit (path);
848-
push (Rust::Token::make (COLON, item.get_locus ()));
849-
visit (lit);
848+
push (Rust::Token::make (EQUAL, item.get_locus ()));
849+
visit (expr);
850850
}
851851

852852
void

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class TokenCollector : public ASTVisitor
246246
void visit (AttrInputLiteral &attr_input);
247247
void visit (AttrInputMacro &attr_input);
248248
void visit (MetaItemLitExpr &meta_item);
249-
void visit (MetaItemPathLit &meta_item);
249+
void visit (MetaItemPathExpr &meta_item);
250250
void visit (BorrowExpr &expr);
251251
void visit (DereferenceExpr &expr);
252252
void visit (ErrorPropagationExpr &expr);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class LiteralExpr;
7878
class AttrInputLiteral;
7979
class AttrInputMacro;
8080
class MetaItemLitExpr;
81-
class MetaItemPathLit;
81+
class MetaItemPathExpr;
8282
class OperatorExpr;
8383
class BorrowExpr;
8484
class DereferenceExpr;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ DefaultASTVisitor::visit (AST::SimplePath &path)
224224
}
225225

226226
void
227-
DefaultASTVisitor::visit (AST::MetaItemPathLit &meta_item)
227+
DefaultASTVisitor::visit (AST::MetaItemPathExpr &meta_item)
228228
{
229229
visit (meta_item.get_path ());
230-
visit (meta_item.get_literal ());
230+
visit (meta_item.get_expr ());
231231
}
232232

233233
void

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ASTVisitor
7373
virtual void visit (AttrInputLiteral &attr_input) = 0;
7474
virtual void visit (AttrInputMacro &attr_input) = 0;
7575
virtual void visit (MetaItemLitExpr &meta_item) = 0;
76-
virtual void visit (MetaItemPathLit &meta_item) = 0;
76+
virtual void visit (MetaItemPathExpr &meta_item) = 0;
7777
virtual void visit (BorrowExpr &expr) = 0;
7878
virtual void visit (DereferenceExpr &expr) = 0;
7979
virtual void visit (ErrorPropagationExpr &expr) = 0;
@@ -270,7 +270,7 @@ class DefaultASTVisitor : public ASTVisitor
270270
virtual void visit (AST::AttrInputLiteral &attr_input) override;
271271
virtual void visit (AST::AttrInputMacro &attr_input) override;
272272
virtual void visit (AST::MetaItemLitExpr &meta_item) override;
273-
virtual void visit (AST::MetaItemPathLit &meta_item) override;
273+
virtual void visit (AST::MetaItemPathExpr &meta_item) override;
274274
virtual void visit (AST::BorrowExpr &expr) override;
275275
virtual void visit (AST::DereferenceExpr &expr) override;
276276
virtual void visit (AST::ErrorPropagationExpr &expr) override;

gcc/rust/ast/rust-ast.cc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Attribute::get_traits_to_derive ()
288288
break;
289289
case AST::MetaItem::ItemKind::ListPaths:
290290
case AST::MetaItem::ItemKind::NameValueStr:
291-
case AST::MetaItem::ItemKind::PathLit:
291+
case AST::MetaItem::ItemKind::PathExpr:
292292
case AST::MetaItem::ItemKind::Seq:
293293
case AST::MetaItem::ItemKind::ListNameValueStr:
294294
default:
@@ -3712,20 +3712,12 @@ AttributeParser::parse_path_meta_item ()
37123712
{
37133713
skip_token ();
37143714

3715-
location_t locus = peek_token ()->get_locus ();
3716-
Literal lit = parse_literal ();
3717-
if (lit.is_error ())
3718-
{
3719-
rust_error_at (peek_token ()->get_locus (),
3720-
"failed to parse literal in attribute");
3721-
return nullptr;
3722-
}
3723-
LiteralExpr expr (std::move (lit), {}, locus);
3715+
std::unique_ptr<Expr> expr = parser->parse_expr ();
37243716
// stream_pos++;
37253717
/* shouldn't be required anymore due to parsing literal actually
37263718
* skipping the token */
3727-
return std::unique_ptr<MetaItemPathLit> (
3728-
new MetaItemPathLit (std::move (path), std::move (expr)));
3719+
return std::unique_ptr<MetaItemPathExpr> (
3720+
new MetaItemPathExpr (std::move (path), std::move (expr)));
37293721
}
37303722
case COMMA:
37313723
// just simple path
@@ -4144,10 +4136,12 @@ MetaNameValueStr::check_cfg_predicate (const Session &session) const
41444136
}
41454137

41464138
bool
4147-
MetaItemPathLit::check_cfg_predicate (const Session &session) const
4139+
MetaItemPathExpr::check_cfg_predicate (const Session &session) const
41484140
{
4141+
// FIXME: Accept path expressions
4142+
rust_assert (expr->is_literal ());
41494143
return session.options.target_data.has_key_value_pair (path.as_string (),
4150-
lit.as_string ());
4144+
expr->as_string ());
41514145
}
41524146

41534147
std::vector<std::unique_ptr<Token>>
@@ -4235,8 +4229,10 @@ MetaListNameValueStr::to_attribute () const
42354229
}
42364230

42374231
Attribute
4238-
MetaItemPathLit::to_attribute () const
4232+
MetaItemPathExpr::to_attribute () const
42394233
{
4234+
rust_assert (expr->is_literal ());
4235+
auto &lit = static_cast<LiteralExpr &> (*expr);
42404236
return Attribute (path, std::unique_ptr<AttrInputLiteral> (
42414237
new AttrInputLiteral (lit)));
42424238
}
@@ -4406,7 +4402,7 @@ MetaItemLitExpr::accept_vis (ASTVisitor &vis)
44064402
}
44074403

44084404
void
4409-
MetaItemPathLit::accept_vis (ASTVisitor &vis)
4405+
MetaItemPathExpr::accept_vis (ASTVisitor &vis)
44104406
{
44114407
vis.visit (*this);
44124408
}

gcc/rust/ast/rust-ast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ class MetaItem : public MetaItemInner
10731073
Path,
10741074
Word,
10751075
NameValueStr,
1076-
PathLit,
1076+
PathExpr,
10771077
Seq,
10781078
ListPaths,
10791079
ListNameValueStr,
@@ -1091,7 +1091,7 @@ class MetaItem : public MetaItemInner
10911091
class MetaItemLitExpr;
10921092

10931093
// Forward decl - defined in rust-expr.h
1094-
class MetaItemPathLit;
1094+
class MetaItemPathExpr;
10951095

10961096
// Forward decl - defined in rust-macro.h
10971097
class MetaItemPath;

gcc/rust/ast/rust-expr.h

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,36 +249,50 @@ class MetaItemLitExpr : public MetaItemInner
249249
}
250250
};
251251

252-
// more generic meta item "path = lit" form
253-
class MetaItemPathLit : public MetaItem
252+
// more generic meta item "path = expr" form
253+
class MetaItemPathExpr : public MetaItem
254254
{
255255
SimplePath path;
256-
LiteralExpr lit;
256+
std::unique_ptr<Expr> expr;
257257

258258
public:
259-
MetaItemPathLit (SimplePath path, LiteralExpr lit_expr)
260-
: path (std::move (path)), lit (std::move (lit_expr))
259+
MetaItemPathExpr (SimplePath path, std::unique_ptr<Expr> expr)
260+
: path (std::move (path)), expr (std::move (expr))
261+
{}
262+
263+
MetaItemPathExpr (const MetaItemPathExpr &other)
264+
: MetaItem (other), path (other.path), expr (other.expr->clone_expr ())
261265
{}
262266

267+
MetaItemPathExpr (MetaItemPathExpr &&) = default;
268+
269+
MetaItemPathExpr &operator= (MetaItemPathExpr &&) = default;
270+
271+
MetaItemPathExpr operator= (const MetaItemPathExpr &other)
272+
{
273+
MetaItem::operator= (other);
274+
path = other.path;
275+
expr = other.expr->clone_expr ();
276+
return *this;
277+
}
278+
263279
SimplePath get_path () const { return path; }
264280

265281
SimplePath &get_path () { return path; }
266282

267-
LiteralExpr get_literal () const { return lit; }
268-
269-
LiteralExpr &get_literal () { return lit; }
283+
Expr &get_expr () { return *expr; }
270284

271285
std::string as_string () const override
272286
{
273-
return path.as_string () + " = " + lit.as_string ();
287+
return path.as_string () + " = " + expr->as_string ();
274288
}
275289

276290
MetaItem::ItemKind get_item_kind () const override
277291
{
278-
return MetaItem::ItemKind::PathLit;
292+
return MetaItem::ItemKind::PathExpr;
279293
}
280294

281-
// There are two Locations in MetaItemPathLit (path and lit_expr),
295+
// There are two Locations in MetaItemPathExpr (path and expr),
282296
// we have no idea use which of them, just simply return UNKNOWN_LOCATION
283297
// now.
284298
// Maybe we will figure out when we really need the location in the future.
@@ -294,9 +308,9 @@ class MetaItemPathLit : public MetaItem
294308

295309
protected:
296310
// Use covariance to implement clone function as returning this type
297-
MetaItemPathLit *clone_meta_item_inner_impl () const override
311+
MetaItemPathExpr *clone_meta_item_inner_impl () const override
298312
{
299-
return new MetaItemPathLit (*this);
313+
return new MetaItemPathExpr (*this);
300314
}
301315
};
302316

gcc/rust/expand/rust-derive.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class DeriveVisitor : public AST::ASTVisitor
118118
virtual void visit (LiteralExpr &expr) override final{};
119119
virtual void visit (AttrInputLiteral &attr_input) override final{};
120120
virtual void visit (MetaItemLitExpr &meta_item) override final{};
121-
virtual void visit (MetaItemPathLit &meta_item) override final{};
121+
virtual void visit (MetaItemPathExpr &meta_item) override final{};
122122
virtual void visit (BorrowExpr &expr) override final{};
123123
virtual void visit (DereferenceExpr &expr) override final{};
124124
virtual void visit (ErrorPropagationExpr &expr) override final{};

gcc/rust/expand/rust-expand-visitor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ ExpandVisitor::visit (AST::MetaItemLitExpr &)
538538
{}
539539

540540
void
541-
ExpandVisitor::visit (AST::MetaItemPathLit &)
541+
ExpandVisitor::visit (AST::MetaItemPathExpr &)
542542
{}
543543

544544
void

0 commit comments

Comments
 (0)