LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-macro-builtins.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 2 2
Test Date: 2024-12-21 13:15:12 Functions: - 0 0
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_MACRO_BUILTINS_H
      20                 :             : #define RUST_MACRO_BUILTINS_H
      21                 :             : 
      22                 :             : #include "rust-ast.h"
      23                 :             : #include "rust-builtin-ast-nodes.h"
      24                 :             : #include "rust-ast-fragment.h"
      25                 :             : #include "rust-location.h"
      26                 :             : #include "bi-map.h"
      27                 :             : 
      28                 :             : namespace Rust {
      29                 :             : 
      30                 :             : // FIXME: Add a BuiltinMacro class which contains a name (or should it?), a
      31                 :             : // transcriber and extra info if necessary
      32                 :             : // then make a global map<string, BuiltinMacro>
      33                 :             : 
      34                 :             : //
      35                 :             : // All builtin macros possible
      36                 :             : //
      37                 :             : enum class BuiltinMacro
      38                 :             : {
      39                 :             :   Assert,
      40                 :             :   File,
      41                 :             :   Line,
      42                 :             :   Column,
      43                 :             :   IncludeBytes,
      44                 :             :   IncludeStr,
      45                 :             :   Stringify,
      46                 :             :   CompileError,
      47                 :             :   Concat,
      48                 :             :   Env,
      49                 :             :   OptionEnv,
      50                 :             :   Cfg,
      51                 :             :   Include,
      52                 :             :   FormatArgs,
      53                 :             :   FormatArgsNl,
      54                 :             :   ConcatIdents,
      55                 :             :   ModulePath,
      56                 :             :   Asm,
      57                 :             :   LlvmAsm,
      58                 :             :   GlobalAsm,
      59                 :             :   LogSyntax,
      60                 :             :   TraceMacros,
      61                 :             :   Test,
      62                 :             :   Bench,
      63                 :             :   TestCase,
      64                 :             :   GlobalAllocator,
      65                 :             :   CfgAccessible,
      66                 :             :   RustcDecodable,
      67                 :             :   RustcEncodable,
      68                 :             :   Clone,
      69                 :             :   Copy,
      70                 :             :   Debug,
      71                 :             :   Default,
      72                 :             :   Eq,
      73                 :             :   PartialEq,
      74                 :             :   Ord,
      75                 :             :   PartialOrd,
      76                 :             :   Hash,
      77                 :             : };
      78                 :             : 
      79                 :             : tl::optional<BuiltinMacro>
      80                 :             : builtin_macro_from_string (const std::string &identifier);
      81                 :             : 
      82                 :             : //
      83                 :             : //    This class provides a list of builtin macros implemented by the compiler.
      84                 :             : //    The functions defined are called "builtin transcribers" in that they
      85                 :             : //    replace the transcribing part of a macro definition.
      86                 :             : //
      87                 :             : //    Like regular macro transcribers, they are responsible for building and
      88                 :             : //    returning an AST fragment: basically a vector of AST nodes put together.
      89                 :             : //
      90                 :             : //    Unlike regular declarative macros where each match arm has its own
      91                 :             : //    associated transcriber, builtin transcribers are responsible for handling
      92                 :             : //    all match arms of the macro. This means that you should take extra care
      93                 :             : //    when implementing a builtin containing multiple match arms: You will
      94                 :             : //    probably need to do some lookahead in order to determine which match arm
      95                 :             : //    the user intended to use.
      96                 :             : //
      97                 :             : //    An example of this is the `assert!()` macro:
      98                 :             : //
      99                 :             : //    ```
     100                 :             : //    macro_rules! assert {
     101                 :             : //    ($cond:expr $(,)?) => {{ ... }};
     102                 :             : //    ($cond : expr, $ ($arg : tt) +) = > {{ ... }};
     103                 :             : //    }
     104                 :             : //    ```
     105                 :             : //
     106                 :             : //    If more tokens exist beyond the optional comma, they need to be handled as
     107                 :             : //    a token-tree for a custom panic message.
     108                 :             : //
     109                 :             : //    These builtin macros with empty transcribers are defined in the standard
     110                 :             : //    library. They are marked with a special attribute,
     111                 :             : //    `#[rustc_builtin_macro]`. When this attribute is present on a macro
     112                 :             : //    definition, the compiler should look for an associated transcriber in the
     113                 :             : //    mappings. Meaning that you must remember to insert your transcriber in the
     114                 :             : //    `builtin_macros` map of the `Mappings`.
     115                 :             : //
     116                 :             : //    This map is built as a static variable in the `insert_macro_def()` method
     117                 :             : //    of the `Mappings` class.
     118                 :             : 
     119                 :             : class MacroBuiltin
     120                 :             : {
     121                 :             : public:
     122                 :             :   static const BiMap<std::string, BuiltinMacro> builtins;
     123                 :             :   static std::unordered_map<std::string, AST::MacroTranscriberFunc>
     124                 :             :     builtin_transcribers;
     125                 :             : 
     126                 :             :   static tl::optional<AST::Fragment>
     127                 :             :   assert_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     128                 :             : 
     129                 :             :   static tl::optional<AST::Fragment> file_handler (location_t invoc_locus,
     130                 :             :                                                    AST::MacroInvocData &invoc);
     131                 :             : 
     132                 :             :   static tl::optional<AST::Fragment>
     133                 :             :   column_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     134                 :             : 
     135                 :             :   static tl::optional<AST::Fragment>
     136                 :             :   include_bytes_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     137                 :             : 
     138                 :             :   static tl::optional<AST::Fragment>
     139                 :             :   include_str_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     140                 :             : 
     141                 :             :   static tl::optional<AST::Fragment>
     142                 :             :   stringify_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     143                 :             : 
     144                 :             :   static tl::optional<AST::Fragment>
     145                 :             :   compile_error_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     146                 :             : 
     147                 :             :   static tl::optional<AST::Fragment>
     148                 :             :   concat_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     149                 :             : 
     150                 :             :   static tl::optional<AST::Fragment> env_handler (location_t invoc_locus,
     151                 :             :                                                   AST::MacroInvocData &invoc);
     152                 :             : 
     153                 :             :   static tl::optional<AST::Fragment> cfg_handler (location_t invoc_locus,
     154                 :             :                                                   AST::MacroInvocData &invoc);
     155                 :             : 
     156                 :             :   static tl::optional<AST::Fragment>
     157                 :             :   include_handler (location_t invoc_locus, AST::MacroInvocData &invoc);
     158                 :             : 
     159                 :             :   static tl::optional<AST::Fragment> line_handler (location_t invoc_locus,
     160                 :             :                                                    AST::MacroInvocData &invoc);
     161                 :             : 
     162                 :             :   static tl::optional<AST::Fragment>
     163                 :             :   format_args_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
     164                 :             :                        AST::FormatArgs::Newline nl);
     165                 :             : 
     166                 :             :   static tl::optional<AST::Fragment> sorry (location_t invoc_locus,
     167                 :             :                                             AST::MacroInvocData &invoc);
     168                 :             : 
     169                 :             :   /* Builtin procedural macros do not work directly on tokens, but still need a
     170                 :             :    * builtin transcriber to be considered proper builtin macros */
     171                 :             :   static tl::optional<AST::Fragment> proc_macro_builtin (location_t,
     172                 :             :                                                          AST::MacroInvocData &);
     173                 :             : };
     174                 :             : } // namespace Rust
     175                 :             : 
     176                 :             : namespace std {
     177                 :             : template <> struct hash<Rust::BuiltinMacro>
     178                 :             : {
     179                 :      435013 :   size_t operator() (const Rust::BuiltinMacro &macro) const noexcept
     180                 :             :   {
     181                 :      435013 :     return hash<std::underlying_type<Rust::BuiltinMacro>::type> () (
     182                 :             :       static_cast<std::underlying_type<Rust::BuiltinMacro>::type> (macro));
     183                 :             :   }
     184                 :             : };
     185                 :             : } // namespace std
     186                 :             : 
     187                 :             : #endif // RUST_MACRO_BUILTINS_H
        

Generated by: LCOV version 2.1-beta

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.