michael@0: // michael@0: // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: #ifndef COMPILER_PREPROCESSOR_MACRO_EXPANDER_H_ michael@0: #define COMPILER_PREPROCESSOR_MACRO_EXPANDER_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "Lexer.h" michael@0: #include "Macro.h" michael@0: #include "pp_utils.h" michael@0: michael@0: namespace pp michael@0: { michael@0: michael@0: class Diagnostics; michael@0: michael@0: class MacroExpander : public Lexer michael@0: { michael@0: public: michael@0: MacroExpander(Lexer* lexer, MacroSet* macroSet, Diagnostics* diagnostics); michael@0: virtual ~MacroExpander(); michael@0: michael@0: virtual void lex(Token* token); michael@0: michael@0: private: michael@0: PP_DISALLOW_COPY_AND_ASSIGN(MacroExpander); michael@0: michael@0: void getToken(Token* token); michael@0: void ungetToken(const Token& token); michael@0: bool isNextTokenLeftParen(); michael@0: michael@0: bool pushMacro(const Macro& macro, const Token& identifier); michael@0: void popMacro(); michael@0: michael@0: bool expandMacro(const Macro& macro, michael@0: const Token& identifier, michael@0: std::vector* replacements); michael@0: michael@0: typedef std::vector MacroArg; michael@0: bool collectMacroArgs(const Macro& macro, michael@0: const Token& identifier, michael@0: std::vector* args); michael@0: void replaceMacroParams(const Macro& macro, michael@0: const std::vector& args, michael@0: std::vector* replacements); michael@0: michael@0: struct MacroContext michael@0: { michael@0: const Macro* macro; michael@0: std::size_t index; michael@0: std::vector replacements; michael@0: michael@0: MacroContext() : macro(0), index(0) { } michael@0: bool empty() const { return index == replacements.size(); } michael@0: const Token& get() { return replacements[index++]; } michael@0: void unget() { assert(index > 0); --index; } michael@0: }; michael@0: michael@0: Lexer* mLexer; michael@0: MacroSet* mMacroSet; michael@0: Diagnostics* mDiagnostics; michael@0: michael@0: std::auto_ptr mReserveToken; michael@0: std::vector mContextStack; michael@0: }; michael@0: michael@0: } // namespace pp michael@0: #endif // COMPILER_PREPROCESSOR_MACRO_EXPANDER_H_ michael@0: