diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2024-05-21 10:23:55 +0200 |
---|---|---|
committer | P-E-P <32375388+P-E-P@users.noreply.github.com> | 2024-06-12 09:56:01 +0000 |
commit | a8e63c472f8169ef8e9ca8e7216dbdb0ddabb7ef (patch) | |
tree | 2a3c44d7bb2c988d7d525f5d42f740076481b0f3 /gcc/rust/parse/rust-parse-impl.h | |
parent | 53a857ac945f0acc8c8db93f534656acca4cbd71 (diff) | |
download | gcc-a8e63c472f8169ef8e9ca8e7216dbdb0ddabb7ef.zip gcc-a8e63c472f8169ef8e9ca8e7216dbdb0ddabb7ef.tar.gz gcc-a8e63c472f8169ef8e9ca8e7216dbdb0ddabb7ef.tar.bz2 |
Parse raw ref operator
The raw ref operator is an unstable feature required to obtain a pointer
to unaligned adresses (mainly unaligned struct fields) without UB.
gcc/rust/ChangeLog:
* ast/rust-ast-builder.cc (Builder::ref): Adapt constructor to the new
API.
* ast/rust-ast-collector.cc (TokenCollector::visit): Emit a raw weak
keyword when required.
* ast/rust-ast.cc (BorrowExpr::as_string): Change as_string
representation to handle raw ref operator.
* ast/rust-expr.h (class BorrowExpr): Add raw discriminant.
* expand/rust-macro-builtins-include.cc: Adapt constructor to the new
API.
* parse/rust-parse-impl.h: Handle the raw weak keyword.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc/rust/parse/rust-parse-impl.h')
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 8dc8b1a..e278a63 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -12376,33 +12376,64 @@ Parser<ManagedTokenSource>::null_denotation_not_path ( case AMP: { // (single) "borrow" expression - shared (mutable) or immutable std::unique_ptr<AST::Expr> expr = nullptr; - bool is_mut_borrow = false; + Mutability mutability = Mutability::Imm; + bool raw_borrow = false; ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; if (!restrictions.can_be_struct_expr) entered_from_unary.can_be_struct_expr = false; - if (lexer.peek_token ()->get_id () == MUT) + auto is_mutability = [] (const_TokenPtr token) { + return token->get_id () == CONST || token->get_id () == MUT; + }; + + auto t = lexer.peek_token (); + // Weak raw keyword, we look (1) ahead and treat it as an identifier if + // there is no mut nor const. + if (t->get_id () == IDENTIFIER + && t->get_str () == Values::WeakKeywords::RAW + && is_mutability (lexer.peek_token (1))) + { + lexer.skip_token (); + switch (lexer.peek_token ()->get_id ()) + { + case MUT: + mutability = Mutability::Mut; + break; + case CONST: + mutability = Mutability::Imm; + break; + default: + rust_error_at (lexer.peek_token ()->get_locus (), + "raw borrow should be either const or mut"); + } + lexer.skip_token (); + expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); + raw_borrow = true; + } + else if (t->get_id () == MUT) { lexer.skip_token (); expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); - is_mut_borrow = true; + mutability = Mutability::Mut; + raw_borrow = false; } else { expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary); + raw_borrow = false; } // FIXME: allow outer attributes on expression return std::unique_ptr<AST::BorrowExpr> ( - new AST::BorrowExpr (std::move (expr), is_mut_borrow, false, + new AST::BorrowExpr (std::move (expr), mutability, raw_borrow, false, std::move (outer_attrs), tok->get_locus ())); } case LOGICAL_AND: { // (double) "borrow" expression - shared (mutable) or immutable std::unique_ptr<AST::Expr> expr = nullptr; - bool is_mut_borrow = false; + Mutability mutability = Mutability::Imm; ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; @@ -12411,16 +12442,17 @@ Parser<ManagedTokenSource>::null_denotation_not_path ( { lexer.skip_token (); expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary); - is_mut_borrow = true; + mutability = Mutability::Mut; } else { expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary); + mutability = Mutability::Imm; } // FIXME: allow outer attributes on expression return std::unique_ptr<AST::BorrowExpr> ( - new AST::BorrowExpr (std::move (expr), is_mut_borrow, true, + new AST::BorrowExpr (std::move (expr), mutability, false, true, std::move (outer_attrs), tok->get_locus ())); } case OR: |