Skip to content

Commit 0580e33

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 9dc4151 commit 0580e33

19 files changed

+52
-60
lines changed

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

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

841841
void
842-
TokenCollector::visit (MetaItemPathLit &item)
842+
TokenCollector::visit (MetaItemPathExpr &item)
843843
{
844-
auto path = item.get_path ();
845-
auto lit = item.get_literal ();
844+
auto &path = item.get_path ();
845+
auto &expr = item.get_expr ();
846846
visit (path);
847847
push (Rust::Token::make (COLON, item.get_locus ()));
848-
visit (lit);
848+
visit (expr);
849849
}
850850

851851
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
@@ -223,10 +223,10 @@ DefaultASTVisitor::visit (AST::SimplePath &path)
223223
}
224224

225225
void
226-
DefaultASTVisitor::visit (AST::MetaItemPathLit &meta_item)
226+
DefaultASTVisitor::visit (AST::MetaItemPathExpr &meta_item)
227227
{
228228
visit (meta_item.get_path ());
229-
visit (meta_item.get_literal ());
229+
visit (meta_item.get_expr ());
230230
}
231231

232232
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;
@@ -267,7 +267,7 @@ class DefaultASTVisitor : public ASTVisitor
267267
virtual void visit (AST::AttrInputLiteral &attr_input) override;
268268
virtual void visit (AST::AttrInputMacro &attr_input) override;
269269
virtual void visit (AST::MetaItemLitExpr &meta_item) override;
270-
virtual void visit (AST::MetaItemPathLit &meta_item) override;
270+
virtual void visit (AST::MetaItemPathExpr &meta_item) override;
271271
virtual void visit (AST::BorrowExpr &expr) override;
272272
virtual void visit (AST::DereferenceExpr &expr) override;
273273
virtual void visit (AST::ErrorPropagationExpr &expr) override;

gcc/rust/ast/rust-ast.cc

Lines changed: 7 additions & 22 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,10 @@ 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
{
41494141
return session.options.target_data.has_key_value_pair (path.as_string (),
4150-
lit.as_string ());
4142+
expr->as_string ());
41514143
}
41524144

41534145
std::vector<std::unique_ptr<Token>>
@@ -4234,13 +4226,6 @@ MetaListNameValueStr::to_attribute () const
42344226
std::move (new_seq_container));
42354227
}
42364228

4237-
Attribute
4238-
MetaItemPathLit::to_attribute () const
4239-
{
4240-
return Attribute (path, std::unique_ptr<AttrInputLiteral> (
4241-
new AttrInputLiteral (lit)));
4242-
}
4243-
42444229
std::vector<Attribute>
42454230
AttrInputMetaItemContainer::separate_cfg_attrs () const
42464231
{
@@ -4405,7 +4390,7 @@ MetaItemLitExpr::accept_vis (ASTVisitor &vis)
44054390
}
44064391

44074392
void
4408-
MetaItemPathLit::accept_vis (ASTVisitor &vis)
4393+
MetaItemPathExpr::accept_vis (ASTVisitor &vis)
44094394
{
44104395
vis.visit (*this);
44114396
}

gcc/rust/ast/rust-ast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ class MetaItem : public MetaItemInner
10721072
Path,
10731073
Word,
10741074
NameValueStr,
1075-
PathLit,
1075+
PathExpr,
10761076
Seq,
10771077
ListPaths,
10781078
ListNameValueStr,
@@ -1090,7 +1090,7 @@ class MetaItem : public MetaItemInner
10901090
class MetaItemLitExpr;
10911091

10921092
// Forward decl - defined in rust-expr.h
1093-
class MetaItemPathLit;
1093+
class MetaItemPathExpr;
10941094

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

gcc/rust/ast/rust-expr.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,36 +245,45 @@ class MetaItemLitExpr : public MetaItemInner
245245
}
246246
};
247247

248-
// more generic meta item "path = lit" form
249-
class MetaItemPathLit : public MetaItem
248+
// more generic meta item "path = expr" form
249+
class MetaItemPathExpr : public MetaItem
250250
{
251251
SimplePath path;
252-
LiteralExpr lit;
252+
std::unique_ptr<Expr> expr;
253253

254254
public:
255-
MetaItemPathLit (SimplePath path, LiteralExpr lit_expr)
256-
: path (std::move (path)), lit (std::move (lit_expr))
255+
MetaItemPathExpr (SimplePath path, std::unique_ptr<Expr> expr)
256+
: path (std::move (path)), expr (std::move (expr))
257+
{}
258+
259+
MetaItemPathExpr (const MetaItemPathExpr &other)
260+
: path (other.path), expr (other.expr->clone_expr ())
257261
{}
258262

263+
MetaItemPathExpr operator= (const MetaItemPathExpr &other)
264+
{
265+
path = other.path;
266+
expr = other.expr->clone_expr ();
267+
return *this;
268+
}
269+
259270
SimplePath get_path () const { return path; }
260271

261272
SimplePath &get_path () { return path; }
262273

263-
LiteralExpr get_literal () const { return lit; }
264-
265-
LiteralExpr &get_literal () { return lit; }
274+
Expr &get_expr () { return *expr; }
266275

267276
std::string as_string () const override
268277
{
269-
return path.as_string () + " = " + lit.as_string ();
278+
return path.as_string () + " = " + expr->as_string ();
270279
}
271280

272281
MetaItem::ItemKind get_item_kind () const override
273282
{
274-
return MetaItem::ItemKind::PathLit;
283+
return MetaItem::ItemKind::PathExpr;
275284
}
276285

277-
// There are two Locations in MetaItemPathLit (path and lit_expr),
286+
// There are two Locations in MetaItemPathExpr (path and lit_expr),
278287
// we have no idea use which of them, just simply return UNKNOWN_LOCATION
279288
// now.
280289
// Maybe we will figure out when we really need the location in the future.
@@ -286,13 +295,11 @@ class MetaItemPathLit : public MetaItem
286295
/* TODO: return true if "ident" is defined and value of it is "lit", return
287296
* false otherwise */
288297

289-
Attribute to_attribute () const override;
290-
291298
protected:
292299
// Use covariance to implement clone function as returning this type
293-
MetaItemPathLit *clone_meta_item_inner_impl () const override
300+
MetaItemPathExpr *clone_meta_item_inner_impl () const override
294301
{
295-
return new MetaItemPathLit (*this);
302+
return new MetaItemPathExpr (*this);
296303
}
297304
};
298305

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)