xref: /aosp_15_r20/external/angle/src/compiler/preprocessor/MacroExpander.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #ifndef COMPILER_PREPROCESSOR_MACROEXPANDER_H_
8 #define COMPILER_PREPROCESSOR_MACROEXPANDER_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "compiler/preprocessor/Lexer.h"
14 #include "compiler/preprocessor/Macro.h"
15 #include "compiler/preprocessor/Preprocessor.h"
16 #include "compiler/preprocessor/Token.h"
17 
18 namespace angle
19 {
20 
21 namespace pp
22 {
23 
24 class Diagnostics;
25 struct SourceLocation;
26 
27 class MacroExpander : public Lexer
28 {
29   public:
30     MacroExpander(Lexer *lexer,
31                   MacroSet *macroSet,
32                   Diagnostics *diagnostics,
33                   const PreprocessorSettings &settings,
34                   bool parseDefined);
35     ~MacroExpander() override;
36 
37     void lex(Token *token) override;
38 
39   private:
40     void getToken(Token *token);
41     void ungetToken(const Token &token);
42     bool isNextTokenLeftParen();
43 
44     bool pushMacro(std::shared_ptr<Macro> macro, const Token &identifier);
45     void popMacro();
46 
47     bool expandMacro(const Macro &macro, const Token &identifier, std::vector<Token> *replacements);
48 
49     typedef std::vector<Token> MacroArg;
50     bool collectMacroArgs(const Macro &macro,
51                           const Token &identifier,
52                           std::vector<MacroArg> *args,
53                           SourceLocation *closingParenthesisLocation);
54     void replaceMacroParams(const Macro &macro,
55                             const std::vector<MacroArg> &args,
56                             std::vector<Token> *replacements);
57 
58     struct MacroContext
59     {
MacroContextMacroContext60         MacroContext(std::shared_ptr<Macro> macro, std::vector<Token> &&replacements)
61             : macro(std::move(macro)), replacements(std::move(replacements))
62         {}
63         bool empty() const;
64         const Token &get();
65         void unget();
66 
67         std::shared_ptr<Macro> macro;
68         std::vector<Token> replacements;
69         std::size_t index = 0;
70     };
71 
72     Lexer *mLexer;
73     MacroSet *mMacroSet;
74     Diagnostics *mDiagnostics;
75     bool mParseDefined;
76 
77     std::unique_ptr<Token> mReserveToken;
78     std::vector<MacroContext> mContextStack;
79     size_t mTotalTokensInContexts;
80 
81     PreprocessorSettings mSettings;
82 
83     bool mDeferReenablingMacros;
84     std::vector<std::shared_ptr<Macro>> mMacrosToReenable;
85 
86     class ScopedMacroReenabler;
87 };
88 
89 }  // namespace pp
90 
91 }  // namespace angle
92 
93 #endif  // COMPILER_PREPROCESSOR_MACROEXPANDER_H_
94