From 3ebfb4d4222d252673ce1afff5a7264e332b3b13 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 21 Jun 2021 17:26:25 -0600 Subject: Use unique_ptr in Token Token currently uses a "std::string *". There's a comment proposing that perhaps this should be changed; but meanwhile, it's slightly cleaner to use unique_ptr rather than have Token manage the string's lifetime manually. --- gcc/rust/lex/rust-token.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/lex/rust-token.h b/gcc/rust/lex/rust-token.h index f76899c..5526a28 100644 --- a/gcc/rust/lex/rust-token.h +++ b/gcc/rust/lex/rust-token.h @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Free Software Foundation, Inc. +// Copyright (C) 2020, 2021 Free Software Foundation, Inc. // This file is part of GCC. @@ -249,7 +249,7 @@ private: // Token location. Location locus; // Associated text (if any) of token. - std::string *str; + std::unique_ptr str; // TODO: maybe remove issues and just store std::string as value? /* Type hint for token based on lexer data (e.g. type suffix). Does not exist * for most tokens. */ @@ -298,7 +298,7 @@ public: Token (Token &&other) = default; Token &operator= (Token &&other) = default; - ~Token () { delete str; } + ~Token () = default; /* TODO: make_shared (which saves a heap allocation) does not work with the * private constructor */ -- cgit v1.1 From 9bedf77e05cfc25f522c42f1332507768abe3af0 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 21 Jun 2021 21:54:49 -0600 Subject: Require moves in Token constructors This avoids some string copies by changing the Token constructors to require any std::string argument to be transferred via std::move. --- gcc/rust/lex/rust-lex.cc | 26 +++++++++++++------------- gcc/rust/lex/rust-token.h | 39 +++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 31 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index dcb57c8..b320401 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Free Software Foundation, Inc. +// Copyright (C) 2020, 2021 Free Software Foundation, Inc. // This file is part of GCC. @@ -1436,7 +1436,7 @@ Lexer::parse_byte_string (Location loc) str.shrink_to_fit (); - return Token::make_byte_string (loc, str); + return Token::make_byte_string (loc, std::move (str)); } // Parses a raw byte string. @@ -1509,7 +1509,7 @@ Lexer::parse_raw_byte_string (Location loc) str.shrink_to_fit (); - return Token::make_byte_string (loc, str); + return Token::make_byte_string (loc, std::move (str)); } // Parses a raw identifier. @@ -1559,7 +1559,7 @@ Lexer::parse_raw_identifier (Location loc) { str.shrink_to_fit (); - return Token::make_identifier (loc, str); + return Token::make_identifier (loc, std::move (str)); } } @@ -1623,7 +1623,7 @@ Lexer::parse_string (Location loc) } str.shrink_to_fit (); - return Token::make_string (loc, str); + return Token::make_string (loc, std::move (str)); } // Parses an identifier or keyword. @@ -1659,7 +1659,7 @@ Lexer::parse_identifier_or_keyword (Location loc) TokenId keyword = classify_keyword (str); if (keyword == IDENTIFIER) - return Token::make_identifier (loc, str); + return Token::make_identifier (loc, std::move (str)); else return Token::make (keyword, loc); } @@ -1736,7 +1736,7 @@ Lexer::parse_raw_string (Location loc, int initial_hash_count) str.shrink_to_fit (); - return Token::make_string (loc, str); + return Token::make_string (loc, std::move (str)); } template @@ -1797,7 +1797,7 @@ Lexer::parse_non_decimal_int_literal (Location loc, IsDigitFunc is_digit_func, : ""))); return nullptr; } - return Token::make_int (loc, existent_str, type_hint); + return Token::make_int (loc, std::move (existent_str), type_hint); } // Parses a hex, binary or octal int literal. @@ -1889,7 +1889,7 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; str.shrink_to_fit (); - return Token::make_float (loc, str, type_hint); + return Token::make_float (loc, std::move (str), type_hint); } else if (current_char == '.' && check_valid_float_dot_end (peek_input (1))) { @@ -1909,7 +1909,7 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; str.shrink_to_fit (); - return Token::make_float (loc, str, CORETYPE_UNKNOWN); + return Token::make_float (loc, std::move (str), CORETYPE_UNKNOWN); } else if (current_char == 'E' || current_char == 'e') { @@ -1938,7 +1938,7 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; str.shrink_to_fit (); - return Token::make_float (loc, str, type_hint); + return Token::make_float (loc, std::move (str), type_hint); } else { @@ -1952,7 +1952,7 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; str.shrink_to_fit (); - return Token::make_int (loc, str, type_hint); + return Token::make_int (loc, std::move (str), type_hint); } } @@ -2026,7 +2026,7 @@ Lexer::parse_char_or_lifetime (Location loc) current_column += length; str.shrink_to_fit (); - return Token::make_lifetime (loc, str); + return Token::make_lifetime (loc, std::move (str)); } else { diff --git a/gcc/rust/lex/rust-token.h b/gcc/rust/lex/rust-token.h index 5526a28..e8d5519 100644 --- a/gcc/rust/lex/rust-token.h +++ b/gcc/rust/lex/rust-token.h @@ -262,9 +262,9 @@ private: {} // Token constructor from token id, location, and a string. - Token (TokenId token_id, Location location, const std::string ¶mStr) - : token_id (token_id), locus (location), str (new std::string (paramStr)), - type_hint (CORETYPE_UNKNOWN) + Token (TokenId token_id, Location location, std::string &¶mStr) + : token_id (token_id), locus (location), + str (new std::string (std::move (paramStr))), type_hint (CORETYPE_UNKNOWN) {} // Token constructor from token id, location, and a char. @@ -281,10 +281,10 @@ private: {} // Token constructor from token id, location, a string, and type hint. - Token (TokenId token_id, Location location, const std::string ¶mStr, + Token (TokenId token_id, Location location, std::string &¶mStr, PrimitiveCoreType parType) - : token_id (token_id), locus (location), str (new std::string (paramStr)), - type_hint (parType) + : token_id (token_id), locus (location), + str (new std::string (std::move (paramStr))), type_hint (parType) {} public: @@ -311,34 +311,37 @@ public: } // Makes and returns a new TokenPtr of type IDENTIFIER. - static TokenPtr make_identifier (Location locus, const std::string &str) + static TokenPtr make_identifier (Location locus, std::string &&str) { // return std::make_shared (IDENTIFIER, locus, str); - return TokenPtr (new Token (IDENTIFIER, locus, str)); + return TokenPtr (new Token (IDENTIFIER, locus, std::move (str))); } // Makes and returns a new TokenPtr of type INT_LITERAL. - static TokenPtr make_int (Location locus, const std::string &str, + static TokenPtr make_int (Location locus, std::string &&str, PrimitiveCoreType type_hint = CORETYPE_UNKNOWN) { // return std::make_shared (INT_LITERAL, locus, str, type_hint); - return TokenPtr (new Token (INT_LITERAL, locus, str, type_hint)); + return TokenPtr ( + new Token (INT_LITERAL, locus, std::move (str), type_hint)); } // Makes and returns a new TokenPtr of type FLOAT_LITERAL. - static TokenPtr make_float (Location locus, const std::string &str, + static TokenPtr make_float (Location locus, std::string &&str, PrimitiveCoreType type_hint = CORETYPE_UNKNOWN) { // return std::make_shared (FLOAT_LITERAL, locus, str, type_hint); - return TokenPtr (new Token (FLOAT_LITERAL, locus, str, type_hint)); + return TokenPtr ( + new Token (FLOAT_LITERAL, locus, std::move (str), type_hint)); } // Makes and returns a new TokenPtr of type STRING_LITERAL. - static TokenPtr make_string (Location locus, const std::string &str) + static TokenPtr make_string (Location locus, std::string &&str) { // return std::make_shared (STRING_LITERAL, locus, str, // CORETYPE_STR); - return TokenPtr (new Token (STRING_LITERAL, locus, str, CORETYPE_STR)); + return TokenPtr ( + new Token (STRING_LITERAL, locus, std::move (str), CORETYPE_STR)); } // Makes and returns a new TokenPtr of type CHAR_LITERAL. @@ -356,17 +359,17 @@ public: } // Makes and returns a new TokenPtr of type BYTE_STRING_LITERAL (fix). - static TokenPtr make_byte_string (Location locus, const std::string &str) + static TokenPtr make_byte_string (Location locus, std::string &&str) { // return std::make_shared (BYTE_STRING_LITERAL, locus, str); - return TokenPtr (new Token (BYTE_STRING_LITERAL, locus, str)); + return TokenPtr (new Token (BYTE_STRING_LITERAL, locus, std::move (str))); } // Makes and returns a new TokenPtr of type LIFETIME. - static TokenPtr make_lifetime (Location locus, const std::string &str) + static TokenPtr make_lifetime (Location locus, std::string &&str) { // return std::make_shared (LIFETIME, locus, str); - return TokenPtr (new Token (LIFETIME, locus, str)); + return TokenPtr (new Token (LIFETIME, locus, std::move (str))); } // Gets id of the token. -- cgit v1.1