LCOV - code coverage report
Current view: top level - gcc/rust/lex - rust-token.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.6 % 68 65
Test Date: 2024-03-23 14:05:01 Functions: 100.0 % 15 15
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : // This file is part of GCC.
       4                 :             : 
       5                 :             : // GCC is free software; you can redistribute it and/or modify it under
       6                 :             : // the terms of the GNU General Public License as published by the Free
       7                 :             : // Software Foundation; either version 3, or (at your option) any later
       8                 :             : // version.
       9                 :             : 
      10                 :             : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : // for more details.
      14                 :             : 
      15                 :             : // You should have received a copy of the GNU General Public License
      16                 :             : // along with GCC; see the file COPYING3.  If not see
      17                 :             : // <http://www.gnu.org/licenses/>.
      18                 :             : 
      19                 :             : #ifndef RUST_TOKEN_H
      20                 :             : #define RUST_TOKEN_H
      21                 :             : 
      22                 :             : #include "rust-system.h"
      23                 :             : #include "rust-linemap.h"
      24                 :             : #include "rust-make-unique.h"
      25                 :             : #include "rust-unicode.h"
      26                 :             : 
      27                 :             : namespace Rust {
      28                 :             : // "Primitive core types" in Rust - the different int and float types, as well
      29                 :             : // as some others
      30                 :             : enum PrimitiveCoreType
      31                 :             : {
      32                 :             :   CORETYPE_UNKNOWN,
      33                 :             :   // named primitives
      34                 :             :   CORETYPE_BOOL,
      35                 :             :   CORETYPE_CHAR,
      36                 :             :   CORETYPE_STR,
      37                 :             :   // okay technically int and uint are arch-dependent (pointer size)
      38                 :             :   CORETYPE_INT,
      39                 :             :   CORETYPE_UINT,
      40                 :             :   // numbered number primitives
      41                 :             :   CORETYPE_F32,
      42                 :             :   CORETYPE_F64,
      43                 :             :   CORETYPE_I8,
      44                 :             :   CORETYPE_I16,
      45                 :             :   CORETYPE_I32,
      46                 :             :   CORETYPE_I64,
      47                 :             :   CORETYPE_I128,
      48                 :             :   CORETYPE_U8,
      49                 :             :   CORETYPE_U16,
      50                 :             :   CORETYPE_U32,
      51                 :             :   CORETYPE_U64,
      52                 :             :   CORETYPE_U128,
      53                 :             :   // Pure decimals are used for tuple index.
      54                 :             :   // Also means there is no type hint.
      55                 :             :   CORETYPE_PURE_DECIMAL,
      56                 :             :   // arch-dependent pointer sizes
      57                 :             :   CORETYPE_ISIZE = CORETYPE_INT,
      58                 :             :   CORETYPE_USIZE = CORETYPE_UINT
      59                 :             : };
      60                 :             : 
      61                 :             : // RS_TOKEN(name, description)
      62                 :             : // RS_TOKEN_KEYWORD_{2015,2018}(name, identifier)
      63                 :             : 
      64                 :             : // Keep RS_TOKEN_KEYWORD sorted
      65                 :             : 
      66                 :             : /* note that abstract, async, become, box, do, final, macro, override, priv,
      67                 :             :  * try, typeof, unsized, virtual, and yield are unused */
      68                 :             : #define RS_TOKEN_LIST                                                          \
      69                 :             :   RS_TOKEN (FIRST_TOKEN, "<first-token-marker>")                               \
      70                 :             :   RS_TOKEN (END_OF_FILE, "end of file")                                        \
      71                 :             :   RS_TOKEN (EXCLAM, "!")                                                       \
      72                 :             :   RS_TOKEN (NOT_EQUAL, "!=")                                                   \
      73                 :             :   RS_TOKEN (PERCENT, "%")                                                      \
      74                 :             :   RS_TOKEN (PERCENT_EQ, "%=")                                                  \
      75                 :             :   RS_TOKEN (AMP, "&")                                                          \
      76                 :             :   RS_TOKEN (AMP_EQ, "&=")                                                      \
      77                 :             :   RS_TOKEN (LOGICAL_AND, "&&")                                                 \
      78                 :             :   RS_TOKEN (ASTERISK, "*")                                                     \
      79                 :             :   RS_TOKEN (ASTERISK_EQ, "*=")                                                 \
      80                 :             :   RS_TOKEN (PLUS, "+")                                                         \
      81                 :             :   RS_TOKEN (PLUS_EQ, "+=")                                                     \
      82                 :             :   RS_TOKEN (COMMA, ",")                                                        \
      83                 :             :   RS_TOKEN (MINUS, "-")                                                        \
      84                 :             :   RS_TOKEN (MINUS_EQ, "-=")                                                    \
      85                 :             :   RS_TOKEN (RETURN_TYPE, "->")                                                 \
      86                 :             :   RS_TOKEN (DOT, ".")                                                          \
      87                 :             :   RS_TOKEN (DOT_DOT, "..")                                                     \
      88                 :             :   RS_TOKEN (DOT_DOT_EQ, "..=")                                                 \
      89                 :             :   RS_TOKEN (ELLIPSIS, "...")                                                   \
      90                 :             :   RS_TOKEN (DIV, "/")                                                          \
      91                 :             :   RS_TOKEN (DIV_EQ, "/=")                                                      \
      92                 :             :   RS_TOKEN (COLON, ":")                                                        \
      93                 :             :   RS_TOKEN (SEMICOLON, ";")                                                    \
      94                 :             :   RS_TOKEN (LEFT_SHIFT, "<<")                                                  \
      95                 :             :   RS_TOKEN (LEFT_SHIFT_EQ, "<<=")                                              \
      96                 :             :   RS_TOKEN (LEFT_ANGLE, "<")                                                   \
      97                 :             :   RS_TOKEN (LESS_OR_EQUAL, "<=")                                               \
      98                 :             :   RS_TOKEN (EQUAL, "=")                                                        \
      99                 :             :   RS_TOKEN (EQUAL_EQUAL, "==")                                                 \
     100                 :             :   RS_TOKEN (MATCH_ARROW, "=>")                                                 \
     101                 :             :   RS_TOKEN (RIGHT_ANGLE, ">")                                                  \
     102                 :             :   RS_TOKEN (GREATER_OR_EQUAL, ">=")                                            \
     103                 :             :   RS_TOKEN (RIGHT_SHIFT, ">>")                                                 \
     104                 :             :   RS_TOKEN (RIGHT_SHIFT_EQ, ">>=")                                             \
     105                 :             :   RS_TOKEN (PATTERN_BIND, "@")                                                 \
     106                 :             :   RS_TOKEN (TILDE, "~")                                                        \
     107                 :             :   RS_TOKEN (BACKSLASH, "\\")                                                   \
     108                 :             :   RS_TOKEN (BACKTICK, "`")                                                     \
     109                 :             :   RS_TOKEN (CARET, "^")                                                        \
     110                 :             :   RS_TOKEN (CARET_EQ, "^=")                                                    \
     111                 :             :   RS_TOKEN (PIPE, "|")                                                         \
     112                 :             :   RS_TOKEN (PIPE_EQ, "|=")                                                     \
     113                 :             :   RS_TOKEN (OR, "||")                                                          \
     114                 :             :   RS_TOKEN (QUESTION_MARK, "?")                                                \
     115                 :             :   RS_TOKEN (HASH, "#")                                                         \
     116                 :             :   /* from here on, dodgy and may not be correct. not operators and may be      \
     117                 :             :    * symbols */                                                                \
     118                 :             :   /* RS_TOKEN(SPACE, " ") probably too dodgy */                                \
     119                 :             :   /* RS_TOKEN(NEWLINE, "\n")*/                                                 \
     120                 :             :   RS_TOKEN (SCOPE_RESOLUTION, "::") /* dodgy */                                \
     121                 :             :   RS_TOKEN (SINGLE_QUOTE, "'") /* should i differentiate from lifetime? */     \
     122                 :             :   RS_TOKEN (DOUBLE_QUOTE, "\"")                                                \
     123                 :             :   RS_TOKEN (IDENTIFIER, "identifier")                                          \
     124                 :             :   RS_TOKEN (INT_LITERAL,                                                       \
     125                 :             :             "integer literal") /* do different int and float types need        \
     126                 :             :                                   different literal types? */                  \
     127                 :             :   RS_TOKEN (FLOAT_LITERAL, "float literal")                                    \
     128                 :             :   RS_TOKEN (STRING_LITERAL, "string literal")                                  \
     129                 :             :   RS_TOKEN (CHAR_LITERAL, "character literal")                                 \
     130                 :             :   RS_TOKEN (BYTE_STRING_LITERAL, "byte string literal")                        \
     131                 :             :   RS_TOKEN (BYTE_CHAR_LITERAL, "byte character literal")                       \
     132                 :             :   RS_TOKEN (LIFETIME, "lifetime") /* TODO: improve token type */               \
     133                 :             :   /* Have "interpolated" tokens (whatever that means)? identifer, path, type,  \
     134                 :             :    * pattern, */                                                               \
     135                 :             :   /* expression, statement, block, meta, item in mrustc (but not directly in   \
     136                 :             :    * lexer). */                                                                \
     137                 :             :   RS_TOKEN (LEFT_PAREN, "(")                                                   \
     138                 :             :   RS_TOKEN (RIGHT_PAREN, ")")                                                  \
     139                 :             :   RS_TOKEN (LEFT_CURLY, "{")                                                   \
     140                 :             :   RS_TOKEN (RIGHT_CURLY, "}")                                                  \
     141                 :             :   RS_TOKEN (LEFT_SQUARE, "[")                                                  \
     142                 :             :   RS_TOKEN (RIGHT_SQUARE, "]")                                                 \
     143                 :             :   /* Macros */                                                                 \
     144                 :             :   RS_TOKEN (DOLLAR_SIGN, "$")                                                  \
     145                 :             :   /* Doc Comments */                                                           \
     146                 :             :   RS_TOKEN (INNER_DOC_COMMENT, "#![doc]")                                      \
     147                 :             :   RS_TOKEN (OUTER_DOC_COMMENT, "#[doc]")                                       \
     148                 :             :   RS_TOKEN_KEYWORD_2015 (ABSTRACT, "abstract") /* unused */                    \
     149                 :             :   RS_TOKEN_KEYWORD_2015 (AS, "as")                                             \
     150                 :             :   RS_TOKEN_KEYWORD_2018 (ASYNC, "async") /* unused */                          \
     151                 :             :   RS_TOKEN_KEYWORD_2015 (AUTO, "auto")                                         \
     152                 :             :   RS_TOKEN_KEYWORD_2018 (AWAIT, "await")                                       \
     153                 :             :   RS_TOKEN_KEYWORD_2015 (BECOME, "become") /* unused */                        \
     154                 :             :   RS_TOKEN_KEYWORD_2015 (BOX, "box")     /* unused */                        \
     155                 :             :   RS_TOKEN_KEYWORD_2015 (BREAK, "break")                                       \
     156                 :             :   RS_TOKEN_KEYWORD_2015 (CONST, "const")                                       \
     157                 :             :   RS_TOKEN_KEYWORD_2015 (CONTINUE, "continue")                                 \
     158                 :             :   RS_TOKEN_KEYWORD_2015 (CRATE, "crate")                                       \
     159                 :             :   RS_TOKEN_KEYWORD_2015 (DO, "do") /* unused */                                \
     160                 :             :   RS_TOKEN_KEYWORD_2018 (DYN, "dyn")                                           \
     161                 :             :   RS_TOKEN_KEYWORD_2015 (ELSE, "else")                                         \
     162                 :             :   RS_TOKEN_KEYWORD_2015 (ENUM_KW, "enum")                                      \
     163                 :             :   RS_TOKEN_KEYWORD_2015 (EXTERN_KW, "extern")                                  \
     164                 :             :   RS_TOKEN_KEYWORD_2015 (FALSE_LITERAL, "false")                               \
     165                 :             :   RS_TOKEN_KEYWORD_2015 (FINAL_KW, "final") /* unused */                       \
     166                 :             :   RS_TOKEN_KEYWORD_2015 (FN_KW, "fn")                                          \
     167                 :             :   RS_TOKEN_KEYWORD_2015 (FOR, "for")                                           \
     168                 :             :   RS_TOKEN_KEYWORD_2015 (IF, "if")                                             \
     169                 :             :   RS_TOKEN_KEYWORD_2015 (IMPL, "impl")                                         \
     170                 :             :   RS_TOKEN_KEYWORD_2015 (IN, "in")                                             \
     171                 :             :   RS_TOKEN_KEYWORD_2015 (LET, "let")                                           \
     172                 :             :   RS_TOKEN_KEYWORD_2015 (LOOP, "loop")                                         \
     173                 :             :   RS_TOKEN_KEYWORD_2015 (MACRO, "macro")                                       \
     174                 :             :   RS_TOKEN_KEYWORD_2015 (MATCH_KW, "match")                                    \
     175                 :             :   RS_TOKEN_KEYWORD_2015 (MOD, "mod")                                           \
     176                 :             :   RS_TOKEN_KEYWORD_2015 (MOVE, "move")                                         \
     177                 :             :   RS_TOKEN_KEYWORD_2015 (MUT, "mut")                                           \
     178                 :             :   RS_TOKEN_KEYWORD_2015 (OVERRIDE_KW, "override") /* unused */                 \
     179                 :             :   RS_TOKEN_KEYWORD_2015 (PRIV, "priv")                  /* unused */                 \
     180                 :             :   RS_TOKEN_KEYWORD_2015 (PUB, "pub")                                           \
     181                 :             :   RS_TOKEN_KEYWORD_2015 (REF, "ref")                                           \
     182                 :             :   RS_TOKEN_KEYWORD_2015 (RETURN_KW, "return")                                  \
     183                 :             :   RS_TOKEN_KEYWORD_2015 (                                                      \
     184                 :             :     SELF_ALIAS, "Self") /* mrustc does not treat this as a reserved word*/     \
     185                 :             :   RS_TOKEN_KEYWORD_2015 (SELF, "self")                                         \
     186                 :             :   RS_TOKEN_KEYWORD_2015 (STATIC_KW, "static")                                  \
     187                 :             :   RS_TOKEN_KEYWORD_2015 (STRUCT_KW, "struct")                                  \
     188                 :             :   RS_TOKEN_KEYWORD_2015 (SUPER, "super")                                       \
     189                 :             :   RS_TOKEN_KEYWORD_2015 (TRAIT, "trait")                                       \
     190                 :             :   RS_TOKEN_KEYWORD_2015 (TRUE_LITERAL, "true")                                 \
     191                 :             :   RS_TOKEN_KEYWORD_2015 (TRY, "try") /* unused */                              \
     192                 :             :   RS_TOKEN_KEYWORD_2015 (TYPE, "type")                                         \
     193                 :             :   RS_TOKEN_KEYWORD_2015 (TYPEOF, "typeof") /* unused */                        \
     194                 :             :   RS_TOKEN_KEYWORD_2015 (UNDERSCORE, "_")                                      \
     195                 :             :   RS_TOKEN_KEYWORD_2015 (UNSAFE, "unsafe")                                     \
     196                 :             :   RS_TOKEN_KEYWORD_2015 (UNSIZED, "unsized") /* unused */                      \
     197                 :             :   RS_TOKEN_KEYWORD_2015 (USE, "use")                                           \
     198                 :             :   RS_TOKEN_KEYWORD_2015 (VIRTUAL, "virtual") /* unused */                      \
     199                 :             :   RS_TOKEN_KEYWORD_2015 (WHERE, "where")                                       \
     200                 :             :   RS_TOKEN_KEYWORD_2015 (WHILE, "while")                                       \
     201                 :             :   RS_TOKEN_KEYWORD_2015 (YIELD, "yield") /* unused */                          \
     202                 :             :   RS_TOKEN (LAST_TOKEN, "<last-token-marker>")
     203                 :             : 
     204                 :             : // Contains all token types. Crappy implementation via x-macros.
     205                 :             : enum TokenId
     206                 :             : {
     207                 :             : #define RS_TOKEN(name, _) name,
     208                 :             : #define RS_TOKEN_KEYWORD_2015(x, y) RS_TOKEN (x, y)
     209                 :             : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
     210                 :             :   RS_TOKEN_LIST
     211                 :             : #undef RS_TOKEN_KEYWORD_2015
     212                 :             : #undef RS_TOKEN_KEYWORD_2018
     213                 :             : #undef RS_TOKEN
     214                 :             : };
     215                 :             : 
     216                 :             : // dodgy "TokenPtr" declaration with Token forward declaration
     217                 :             : class Token;
     218                 :             : // A smart pointer (shared_ptr) to Token.
     219                 :             : typedef std::shared_ptr<Token> TokenPtr;
     220                 :             : // A smart pointer (shared_ptr) to a constant Token.
     221                 :             : typedef std::shared_ptr<const Token> const_TokenPtr;
     222                 :             : 
     223                 :             : // Hackily defined way to get token description for enum value using x-macros
     224                 :             : const char *
     225                 :             : get_token_description (TokenId id);
     226                 :             : /* Hackily defined way to get token description as a string for enum value using
     227                 :             :  * x-macros */
     228                 :             : const char *
     229                 :             : token_id_to_str (TokenId id);
     230                 :             : /* checks if a token is a keyword */
     231                 :             : bool
     232                 :             : token_id_is_keyword (TokenId id);
     233                 :             : /* gets the string associated with a keyword */
     234                 :             : const std::string &
     235                 :             : token_id_keyword_string (TokenId id);
     236                 :             : // Get type hint description as a string.
     237                 :             : const char *
     238                 :             : get_type_hint_string (PrimitiveCoreType type);
     239                 :             : 
     240                 :             : /* Normalize string if a token is a identifier */
     241                 :             : std::string
     242                 :             : nfc_normalize_token_string (location_t loc, TokenId id, const std::string &str);
     243                 :             : 
     244                 :             : // Represents a single token. Create using factory static methods.
     245                 :             : class Token
     246                 :             : {
     247                 :             : private:
     248                 :             :   // Token kind.
     249                 :             :   TokenId token_id;
     250                 :             :   // Token location.
     251                 :             :   location_t locus;
     252                 :             :   // Associated text (if any) of token.
     253                 :             :   std::unique_ptr<std::string> str;
     254                 :             :   // TODO: maybe remove issues and just store std::string as value?
     255                 :             :   /* Type hint for token based on lexer data (e.g. type suffix). Does not exist
     256                 :             :    * for most tokens. */
     257                 :             :   PrimitiveCoreType type_hint;
     258                 :             : 
     259                 :             :   // Token constructor from token id and location. Has a null string.
     260                 :      293469 :   Token (TokenId token_id, location_t location)
     261                 :      293469 :     : token_id (token_id), locus (location), str (nullptr),
     262                 :      293469 :       type_hint (CORETYPE_UNKNOWN)
     263                 :             :   {}
     264                 :             : 
     265                 :             :   // Token constructor from token id, location, and a string.
     266                 :      109005 :   Token (TokenId token_id, location_t location, std::string &&paramStr)
     267                 :      109005 :     : token_id (token_id), locus (location), type_hint (CORETYPE_UNKNOWN)
     268                 :             :   {
     269                 :             :     // Normalize identifier tokens
     270                 :      218010 :     str = Rust::make_unique<std::string> (
     271                 :      218010 :       nfc_normalize_token_string (location, token_id, paramStr));
     272                 :      109005 :   }
     273                 :             : 
     274                 :             :   // Token constructor from token id, location, and a char.
     275                 :        1194 :   Token (TokenId token_id, location_t location, char paramChar)
     276                 :        1194 :     : token_id (token_id), locus (location),
     277                 :        1194 :       str (new std::string (1, paramChar)), type_hint (CORETYPE_UNKNOWN)
     278                 :             :   {
     279                 :             :     // Do not need to normalize 1byte char
     280                 :        1194 :   }
     281                 :             : 
     282                 :             :   // Token constructor from token id, location, and a "codepoint".
     283                 :         226 :   Token (TokenId token_id, location_t location, Codepoint paramCodepoint)
     284                 :         226 :     : token_id (token_id), locus (location), type_hint (CORETYPE_UNKNOWN)
     285                 :             :   {
     286                 :             :     // Normalize identifier tokens
     287                 :         452 :     str = Rust::make_unique<std::string> (
     288                 :         452 :       nfc_normalize_token_string (location, token_id,
     289                 :         452 :                                   paramCodepoint.as_string ()));
     290                 :         226 :   }
     291                 :             : 
     292                 :             :   // Token constructor from token id, location, a string, and type hint.
     293                 :       18891 :   Token (TokenId token_id, location_t location, std::string &&paramStr,
     294                 :             :          PrimitiveCoreType parType)
     295                 :       18891 :     : token_id (token_id), locus (location), type_hint (parType)
     296                 :             :   {
     297                 :             :     // Normalize identifier tokens
     298                 :       37782 :     str = Rust::make_unique<std::string> (
     299                 :       37782 :       nfc_normalize_token_string (location, token_id, paramStr));
     300                 :       18891 :   }
     301                 :             : 
     302                 :             : public:
     303                 :             :   // No default constructor.
     304                 :             :   Token () = delete;
     305                 :             :   // Do not copy/assign tokens.
     306                 :             :   Token (const Token &) = delete;
     307                 :             :   Token &operator= (const Token &) = delete;
     308                 :             : 
     309                 :             :   // Allow moving tokens.
     310                 :             :   Token (Token &&other) = default;
     311                 :             :   Token &operator= (Token &&other) = default;
     312                 :             : 
     313                 :      399430 :   ~Token () = default;
     314                 :             : 
     315                 :             :   /* TODO: make_shared (which saves a heap allocation) does not work with the
     316                 :             :    * private constructor */
     317                 :             : 
     318                 :             :   // Makes and returns a new TokenPtr (with null string).
     319                 :      293469 :   static TokenPtr make (TokenId token_id, location_t locus)
     320                 :             :   {
     321                 :             :     // return std::make_shared<Token> (token_id, locus);
     322                 :      293469 :     return TokenPtr (new Token (token_id, locus));
     323                 :             :   }
     324                 :             : 
     325                 :             :   // Makes and returns a new TokenPtr of type IDENTIFIER.
     326                 :      107661 :   static TokenPtr make_identifier (location_t locus, std::string &&str)
     327                 :             :   {
     328                 :             :     // return std::make_shared<Token> (IDENTIFIER, locus, str);
     329                 :      107661 :     return TokenPtr (new Token (IDENTIFIER, locus, std::move (str)));
     330                 :             :   }
     331                 :             : 
     332                 :             :   // Makes and returns a new TokenPtr of type INT_LITERAL.
     333                 :       11612 :   static TokenPtr make_int (location_t locus, std::string &&str,
     334                 :             :                             PrimitiveCoreType type_hint = CORETYPE_UNKNOWN)
     335                 :             :   {
     336                 :             :     // return std::make_shared<Token> (INT_LITERAL, locus, str, type_hint);
     337                 :       11612 :     return TokenPtr (
     338                 :       11612 :       new Token (INT_LITERAL, locus, std::move (str), type_hint));
     339                 :             :   }
     340                 :             : 
     341                 :             :   // Makes and returns a new TokenPtr of type FLOAT_LITERAL.
     342                 :         348 :   static TokenPtr make_float (location_t locus, std::string &&str,
     343                 :             :                               PrimitiveCoreType type_hint = CORETYPE_UNKNOWN)
     344                 :             :   {
     345                 :             :     // return std::make_shared<Token> (FLOAT_LITERAL, locus, str, type_hint);
     346                 :         348 :     return TokenPtr (
     347                 :         348 :       new Token (FLOAT_LITERAL, locus, std::move (str), type_hint));
     348                 :             :   }
     349                 :             : 
     350                 :             :   // Makes and returns a new TokenPtr of type STRING_LITERAL.
     351                 :        6931 :   static TokenPtr make_string (location_t locus, std::string &&str)
     352                 :             :   {
     353                 :             :     // return std::make_shared<Token> (STRING_LITERAL, locus, str,
     354                 :             :     // CORETYPE_STR);
     355                 :        6931 :     return TokenPtr (
     356                 :        6931 :       new Token (STRING_LITERAL, locus, std::move (str), CORETYPE_STR));
     357                 :             :   }
     358                 :             : 
     359                 :             :   // Makes and returns a new TokenPtr of type CHAR_LITERAL.
     360                 :         226 :   static TokenPtr make_char (location_t locus, Codepoint char_lit)
     361                 :             :   {
     362                 :             :     // return std::make_shared<Token> (CHAR_LITERAL, locus, char_lit);
     363                 :         226 :     return TokenPtr (new Token (CHAR_LITERAL, locus, char_lit));
     364                 :             :   }
     365                 :             : 
     366                 :             :   // Makes and returns a new TokenPtr of type BYTE_CHAR_LITERAL.
     367                 :        1194 :   static TokenPtr make_byte_char (location_t locus, char byte_char)
     368                 :             :   {
     369                 :             :     // return std::make_shared<Token> (BYTE_CHAR_LITERAL, locus, byte_char);
     370                 :        1194 :     return TokenPtr (new Token (BYTE_CHAR_LITERAL, locus, byte_char));
     371                 :             :   }
     372                 :             : 
     373                 :             :   // Makes and returns a new TokenPtr of type BYTE_STRING_LITERAL (fix).
     374                 :          73 :   static TokenPtr make_byte_string (location_t locus, std::string &&str)
     375                 :             :   {
     376                 :             :     // return std::make_shared<Token> (BYTE_STRING_LITERAL, locus, str);
     377                 :          73 :     return TokenPtr (new Token (BYTE_STRING_LITERAL, locus, std::move (str)));
     378                 :             :   }
     379                 :             : 
     380                 :             :   // Makes and returns a new TokenPtr of type INNER_DOC_COMMENT.
     381                 :         135 :   static TokenPtr make_inner_doc_comment (location_t locus, std::string &&str)
     382                 :             :   {
     383                 :         135 :     return TokenPtr (new Token (INNER_DOC_COMMENT, locus, std::move (str)));
     384                 :             :   }
     385                 :             : 
     386                 :             :   // Makes and returns a new TokenPtr of type OUTER_DOC_COMMENT.
     387                 :         227 :   static TokenPtr make_outer_doc_comment (location_t locus, std::string &&str)
     388                 :             :   {
     389                 :         227 :     return TokenPtr (new Token (OUTER_DOC_COMMENT, locus, std::move (str)));
     390                 :             :   }
     391                 :             : 
     392                 :             :   // Makes and returns a new TokenPtr of type LIFETIME.
     393                 :         909 :   static TokenPtr make_lifetime (location_t locus, std::string &&str)
     394                 :             :   {
     395                 :             :     // return std::make_shared<Token> (LIFETIME, locus, str);
     396                 :         909 :     return TokenPtr (new Token (LIFETIME, locus, std::move (str)));
     397                 :             :   }
     398                 :             : 
     399                 :             :   // Gets id of the token.
     400                 :     2054352 :   TokenId get_id () const { return token_id; }
     401                 :             : 
     402                 :             :   // Gets location of the token.
     403                 :      422305 :   location_t get_locus () const { return locus; }
     404                 :             : 
     405                 :             :   // Set location of the token.
     406                 :           0 :   void set_locus (location_t locus) { this->locus = locus; }
     407                 :             : 
     408                 :             :   // Gets string description of the token.
     409                 :             :   const std::string &
     410                 :             :   get_str () const; /*{
     411                 :             : // FIXME: put in header again when fix null problem
     412                 :             : //gcc_assert(str != nullptr);
     413                 :             : if (str == nullptr) {
     414                 :             : error_at(get_locus(), "attempted to get string for '%s', which has no string.
     415                 :             : returning empty string instead.", get_token_description()); return "";
     416                 :             : }
     417                 :             : return *str;
     418                 :             : }*/
     419                 :             : 
     420                 :             :   // Gets token's type hint info.
     421                 :       20476 :   PrimitiveCoreType get_type_hint () const
     422                 :             :   {
     423                 :       20476 :     return type_hint == CORETYPE_PURE_DECIMAL ? CORETYPE_UNKNOWN : type_hint;
     424                 :             :   }
     425                 :             : 
     426                 :             :   // diagnostics (error reporting)
     427                 :      134083 :   const char *get_token_description () const
     428                 :             :   {
     429                 :      133794 :     return Rust::get_token_description (token_id);
     430                 :             :   }
     431                 :             : 
     432                 :             :   // debugging
     433                 :           0 :   const char *token_id_to_str () const
     434                 :             :   {
     435                 :           0 :     return Rust::token_id_to_str (token_id);
     436                 :             :   }
     437                 :             : 
     438                 :             :   // debugging
     439                 :             :   const char *get_type_hint_str () const;
     440                 :             : 
     441                 :             :   /* Returns whether the token is a literal of any type (int, float, char,
     442                 :             :    * string, byte char, byte string). */
     443                 :       40531 :   bool is_literal () const
     444                 :             :   {
     445                 :       38416 :     switch (token_id)
     446                 :             :       {
     447                 :             :       case INT_LITERAL:
     448                 :             :       case FLOAT_LITERAL:
     449                 :             :       case CHAR_LITERAL:
     450                 :             :       case STRING_LITERAL:
     451                 :             :       case BYTE_CHAR_LITERAL:
     452                 :             :       case BYTE_STRING_LITERAL:
     453                 :             :         return true;
     454                 :       36665 :       default:
     455                 :       34566 :         return false;
     456                 :             :       }
     457                 :             :   }
     458                 :             : 
     459                 :             :   /* Returns whether the token actually has a string (regardless of whether it
     460                 :             :    * should or not). */
     461                 :       69504 :   bool has_str () const { return str != nullptr; }
     462                 :             : 
     463                 :             :   // Returns whether the token should have a string.
     464                 :       38663 :   bool should_have_str () const
     465                 :             :   {
     466                 :       38784 :     return is_literal () || token_id == IDENTIFIER || token_id == LIFETIME;
     467                 :             :   }
     468                 :             : 
     469                 :             :   // Returns whether the token is a pure decimal int literal
     470                 :         773 :   bool is_pure_decimal () const { return type_hint == CORETYPE_PURE_DECIMAL; }
     471                 :             : 
     472                 :             :   // Return the token representation as someone would find it in the original
     473                 :             :   // source code file.
     474                 :             :   std::string as_string () const;
     475                 :             : };
     476                 :             : } // namespace Rust
     477                 :             : 
     478                 :             : namespace std {
     479                 :             : template <> struct hash<Rust::PrimitiveCoreType>
     480                 :             : {
     481                 :      151577 :   size_t operator() (const Rust::PrimitiveCoreType &coretype) const noexcept
     482                 :             :   {
     483                 :      151577 :     return hash<std::underlying_type<Rust::PrimitiveCoreType>::type> () (
     484                 :             :       static_cast<std::underlying_type<Rust::PrimitiveCoreType>::type> (
     485                 :             :         coretype));
     486                 :             :   }
     487                 :             : };
     488                 :             : } // namespace std
     489                 :             : 
     490                 :             : #endif
        

Generated by: LCOV version 2.0-1

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.