(root)/
gcc-13.2.0/
gcc/
rust/
expand/
rust-macro-substitute-ctx.h
       1  // Copyright (C) 2020-2023 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  #include "rust-ast.h"
      20  #include "rust-macro-expand.h"
      21  
      22  namespace Rust {
      23  class SubstituteCtx
      24  {
      25    std::vector<std::unique_ptr<AST::Token>> &input;
      26    std::vector<std::unique_ptr<AST::Token>> &macro;
      27    std::map<std::string, MatchedFragmentContainer> &fragments;
      28  
      29    /**
      30     * Find the repetition amount to use when expanding a repetition, and
      31     * check that all fragments used respect that repetition amount
      32     *
      33     * @param pattern_start Start of the repetition pattern
      34     * @param pattern_end End of the repetition pattern
      35     * @param repeat_amount Reference to fill with the matched repetition amount
      36     */
      37    bool check_repetition_amount (size_t pattern_start, size_t pattern_end,
      38  				size_t &repeat_amount);
      39  
      40  public:
      41    SubstituteCtx (std::vector<std::unique_ptr<AST::Token>> &input,
      42  		 std::vector<std::unique_ptr<AST::Token>> &macro,
      43  		 std::map<std::string, MatchedFragmentContainer> &fragments)
      44      : input (input), macro (macro), fragments (fragments)
      45    {}
      46  
      47    /**
      48     * Substitute a metavariable by its given fragment in a transcribing context,
      49     * i.e. replacing $var with the associated fragment.
      50     *
      51     * @param metavar Metavariable to try and replace
      52     *
      53     * @return A token containing the associated fragment expanded into tokens if
      54     * any, or the cloned token if no fragment was associated
      55     */
      56    std::vector<std::unique_ptr<AST::Token>>
      57    substitute_metavar (std::unique_ptr<AST::Token> &metavar);
      58  
      59    /**
      60     * Substitute a macro repetition by its given fragments
      61     *
      62     * @param pattern_start Start index of the pattern tokens
      63     * @param pattern_end End index of the patterns tokens
      64     * @param separator Optional separator to include when expanding tokens
      65     *
      66     * @return A vector containing the repeated pattern
      67     */
      68    std::vector<std::unique_ptr<AST::Token>>
      69    substitute_repetition (size_t pattern_start, size_t pattern_end,
      70  			 std::unique_ptr<AST::Token> separator);
      71  
      72    /**
      73     * Substitute a given token by its appropriate representation
      74     *
      75     * @param token_idx Current token to try and substitute
      76     *
      77     * @return A token containing the associated fragment expanded into tokens if
      78     * any, or the cloned token if no fragment was associated, as well as the
      79     * amount of tokens that should be skipped before the next invocation. Since
      80     * this function may consume more than just one token, it is important to skip
      81     * ahead of the input to avoid mis-substitutions
      82     */
      83    std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t>
      84    substitute_token (size_t token_idx);
      85  
      86    /**
      87     * Substitute all tokens by their appropriate representation
      88     *
      89     * @return A vector containing the substituted tokens
      90     */
      91    std::vector<std::unique_ptr<AST::Token>> substitute_tokens ();
      92  };
      93  } // namespace Rust