• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 namespace angle
18 {
19 
20 namespace pp
21 {
22 
23 class Diagnostics;
24 struct SourceLocation;
25 
26 class MacroExpander : public Lexer
27 {
28   public:
29     MacroExpander(Lexer *lexer,
30                   MacroSet *macroSet,
31                   Diagnostics *diagnostics,
32                   const PreprocessorSettings &settings,
33                   bool parseDefined);
34     ~MacroExpander() override;
35 
36     void lex(Token *token) override;
37 
38   private:
39     void getToken(Token *token);
40     void ungetToken(const Token &token);
41     bool isNextTokenLeftParen();
42 
43     bool pushMacro(std::shared_ptr<Macro> macro, const Token &identifier);
44     void popMacro();
45 
46     bool expandMacro(const Macro &macro, const Token &identifier, std::vector<Token> *replacements);
47 
48     typedef std::vector<Token> MacroArg;
49     bool collectMacroArgs(const Macro &macro,
50                           const Token &identifier,
51                           std::vector<MacroArg> *args,
52                           SourceLocation *closingParenthesisLocation);
53     void replaceMacroParams(const Macro &macro,
54                             const std::vector<MacroArg> &args,
55                             std::vector<Token> *replacements);
56 
57     struct MacroContext
58     {
59         MacroContext();
60         ~MacroContext();
61         bool empty() const;
62         const Token &get();
63         void unget();
64 
65         std::shared_ptr<Macro> macro;
66         std::size_t index;
67         std::vector<Token> replacements;
68     };
69 
70     Lexer *mLexer;
71     MacroSet *mMacroSet;
72     Diagnostics *mDiagnostics;
73     bool mParseDefined;
74 
75     std::unique_ptr<Token> mReserveToken;
76     std::vector<MacroContext *> mContextStack;
77     size_t mTotalTokensInContexts;
78 
79     PreprocessorSettings mSettings;
80 
81     bool mDeferReenablingMacros;
82     std::vector<std::shared_ptr<Macro>> mMacrosToReenable;
83 
84     class ScopedMacroReenabler;
85 };
86 
87 }  // namespace pp
88 
89 }  // namespace angle
90 
91 #endif  // COMPILER_PREPROCESSOR_MACROEXPANDER_H_
92