Home | History | Annotate | Download | only in preprocessor
      1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //    http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #ifndef COMPILER_PREPROCESSOR_MACRO_EXPANDER_H_
     16 #define COMPILER_PREPROCESSOR_MACRO_EXPANDER_H_
     17 
     18 #include <memory>
     19 #include <vector>
     20 
     21 #include "Lexer.h"
     22 #include "Macro.h"
     23 #include "pp_utils.h"
     24 
     25 namespace pp
     26 {
     27 
     28 class Diagnostics;
     29 struct SourceLocation;
     30 
     31 class MacroExpander : public Lexer
     32 {
     33 public:
     34 	MacroExpander(Lexer *lexer, MacroSet *macroSet, Diagnostics *diagnostics, bool parseDefined, int allowedMacroExpansionDepth);
     35 	~MacroExpander() override;
     36 
     37 	void lex(Token *token) override;
     38 
     39 private:
     40 	PP_DISALLOW_COPY_AND_ASSIGN(MacroExpander);
     41 
     42 	void getToken(Token *token);
     43 	void ungetToken(const Token &token);
     44 	bool isNextTokenLeftParen();
     45 
     46 	bool pushMacro(std::shared_ptr<Macro> macro, const Token &identifier);
     47 	void popMacro();
     48 
     49 	bool expandMacro(const Macro &macro, const Token &identifier, std::vector<Token> *replacements);
     50 
     51 	typedef std::vector<Token> MacroArg;
     52 	bool collectMacroArgs(const Macro &macro,
     53 	                      const Token &identifier,
     54 	                      std::vector<MacroArg> *args,
     55 	                      SourceLocation *closingParenthesisLocation);
     56 	void replaceMacroParams(const Macro &macro,
     57 	                        const std::vector<MacroArg> &args,
     58 	                        std::vector<Token> *replacements);
     59 
     60 	struct MacroContext
     61 	{
     62 		MacroContext();
     63 		~MacroContext();
     64 		bool empty() const;
     65 		const Token &get();
     66 		void unget();
     67 
     68 		std::shared_ptr<Macro> macro;
     69 		std::size_t index;
     70 		std::vector<Token> replacements;
     71 	};
     72 
     73 	Lexer *mLexer;
     74 	MacroSet *mMacroSet;
     75 	Diagnostics *mDiagnostics;
     76 	const bool mParseDefined;
     77 
     78 	std::unique_ptr<Token> mReserveToken;
     79 	std::vector<MacroContext *> mContextStack;
     80 	size_t mTotalTokensInContexts;
     81 
     82 	int mAllowedMacroExpansionDepth;
     83 
     84 	bool mDeferReenablingMacros;
     85 	std::vector<std::shared_ptr<Macro>> mMacrosToReenable;
     86 
     87 	class ScopedMacroReenabler;
     88 };
     89 
     90 }  // namespace pp
     91 #endif  // COMPILER_PREPROCESSOR_MACROEXPANDER_H_
     92 
     93