Commit 73bc298e by alokp@chromium.org

Hooked up the new preprocessor behind a compile-time flag ANGLE_USE_NEW_PREPROCESSOR.

Review URL: https://codereview.appspot.com/6304095 git-svn-id: https://angleproject.googlecode.com/svn/trunk@1158 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent f3cdb460
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
'defines': [ 'defines': [
'ANGLE_DISABLE_TRACE', 'ANGLE_DISABLE_TRACE',
'ANGLE_COMPILE_OPTIMIZATION_LEVEL=D3DCOMPILE_OPTIMIZATION_LEVEL0', 'ANGLE_COMPILE_OPTIMIZATION_LEVEL=D3DCOMPILE_OPTIMIZATION_LEVEL0',
'ANGLE_USE_NEW_PREPROCESSOR=0',
], ],
}, },
'targets': [ 'targets': [
...@@ -32,6 +33,7 @@ ...@@ -32,6 +33,7 @@
'compiler/preprocessor/new/Macro.h', 'compiler/preprocessor/new/Macro.h',
'compiler/preprocessor/new/MacroExpander.cpp', 'compiler/preprocessor/new/MacroExpander.cpp',
'compiler/preprocessor/new/MacroExpander.h', 'compiler/preprocessor/new/MacroExpander.h',
'compiler/preprocessor/new/pp_utils.h',
'compiler/preprocessor/new/Preprocessor.cpp', 'compiler/preprocessor/new/Preprocessor.cpp',
'compiler/preprocessor/new/Preprocessor.h', 'compiler/preprocessor/new/Preprocessor.h',
'compiler/preprocessor/new/SourceLocation.h', 'compiler/preprocessor/new/SourceLocation.h',
......
...@@ -27,7 +27,7 @@ static TBehavior getBehavior(const std::string& str) ...@@ -27,7 +27,7 @@ static TBehavior getBehavior(const std::string& str)
TDirectiveHandler::TDirectiveHandler(TExtensionBehavior& extBehavior, TDirectiveHandler::TDirectiveHandler(TExtensionBehavior& extBehavior,
TDiagnostics& diagnostics) TDiagnostics& diagnostics)
: mExtBehavior(extBehavior), : mExtensionBehavior(extBehavior),
mDiagnostics(diagnostics) mDiagnostics(diagnostics)
{ {
} }
...@@ -113,15 +113,15 @@ void TDirectiveHandler::handleExtension(const pp::SourceLocation& loc, ...@@ -113,15 +113,15 @@ void TDirectiveHandler::handleExtension(const pp::SourceLocation& loc,
} }
else else
{ {
for (TExtensionBehavior::iterator iter = mExtBehavior.begin(); for (TExtensionBehavior::iterator iter = mExtensionBehavior.begin();
iter != mExtBehavior.end(); ++iter) iter != mExtensionBehavior.end(); ++iter)
iter->second = behaviorVal; iter->second = behaviorVal;
} }
return; return;
} }
TExtensionBehavior::iterator iter = mExtBehavior.find(name); TExtensionBehavior::iterator iter = mExtensionBehavior.find(name);
if (iter != mExtBehavior.end()) if (iter != mExtensionBehavior.end())
{ {
iter->second = behaviorVal; iter->second = behaviorVal;
return; return;
......
...@@ -21,7 +21,7 @@ class TDirectiveHandler : public pp::DirectiveHandler ...@@ -21,7 +21,7 @@ class TDirectiveHandler : public pp::DirectiveHandler
virtual ~TDirectiveHandler(); virtual ~TDirectiveHandler();
const TPragma& pragma() const { return mPragma; } const TPragma& pragma() const { return mPragma; }
const TExtensionBehavior& extBehavior() const { return mExtBehavior; } const TExtensionBehavior& extensionBehavior() const { return mExtensionBehavior; }
virtual void handleError(const pp::SourceLocation& loc, virtual void handleError(const pp::SourceLocation& loc,
const std::string& msg); const std::string& msg);
...@@ -39,7 +39,7 @@ class TDirectiveHandler : public pp::DirectiveHandler ...@@ -39,7 +39,7 @@ class TDirectiveHandler : public pp::DirectiveHandler
private: private:
TPragma mPragma; TPragma mPragma;
TExtensionBehavior& mExtBehavior; TExtensionBehavior& mExtensionBehavior;
TDiagnostics& mDiagnostics; TDiagnostics& mDiagnostics;
}; };
......
...@@ -12,40 +12,6 @@ ...@@ -12,40 +12,6 @@
#include "compiler/glslang.h" #include "compiler/glslang.h"
#include "compiler/preprocessor/new/SourceLocation.h" #include "compiler/preprocessor/new/SourceLocation.h"
extern "C" {
extern int InitPreprocessor();
extern int FinalizePreprocessor();
extern void PredefineIntMacro(const char *name, int value);
}
static void DefineExtensionMacros(const TExtensionBehavior& extBehavior)
{
for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
iter != extBehavior.end(); ++iter) {
PredefineIntMacro(iter->first.c_str(), 1);
}
}
///////////////////////////////////////////////////////////////////////
//
// Preprocessor
//
////////////////////////////////////////////////////////////////////////
bool TParseContext::initPreprocessor()
{
if (InitPreprocessor())
return false;
DefineExtensionMacros(directiveHandler.extBehavior());
return true;
}
void TParseContext::destroyPreprocessor()
{
FinalizePreprocessor();
}
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// //
// Sub- vector and matrix fields // Sub- vector and matrix fields
...@@ -942,7 +908,7 @@ bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier p ...@@ -942,7 +908,7 @@ bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier p
bool TParseContext::extensionErrorCheck(int line, const TString& extension) bool TParseContext::extensionErrorCheck(int line, const TString& extension)
{ {
const TExtensionBehavior& extBehavior = directiveHandler.extBehavior(); const TExtensionBehavior& extBehavior = extensionBehavior();
TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str()); TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
if (iter == extBehavior.end()) { if (iter == extBehavior.end()) {
error(line, "extension", extension.c_str(), "is not supported"); error(line, "extension", extension.c_str(), "is not supported");
...@@ -963,9 +929,9 @@ bool TParseContext::extensionErrorCheck(int line, const TString& extension) ...@@ -963,9 +929,9 @@ bool TParseContext::extensionErrorCheck(int line, const TString& extension)
bool TParseContext::supportsExtension(const char* extension) bool TParseContext::supportsExtension(const char* extension)
{ {
const TExtensionBehavior& extBehavior = directiveHandler.extBehavior(); const TExtensionBehavior& extbehavior = extensionBehavior();
TExtensionBehavior::const_iterator iter = extBehavior.find(extension); TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
return (iter != extBehavior.end()); return (iter != extbehavior.end());
} }
void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior) void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
...@@ -1513,9 +1479,6 @@ int PaParseStrings(int count, const char* const string[], const int length[], ...@@ -1513,9 +1479,6 @@ int PaParseStrings(int count, const char* const string[], const int length[],
if ((count == 0) || (string == NULL)) if ((count == 0) || (string == NULL))
return 1; return 1;
if (!context->initPreprocessor())
return 1;
if (glslang_initialize(context)) if (glslang_initialize(context))
return 1; return 1;
...@@ -1523,8 +1486,7 @@ int PaParseStrings(int count, const char* const string[], const int length[], ...@@ -1523,8 +1486,7 @@ int PaParseStrings(int count, const char* const string[], const int length[],
if (!error) if (!error)
error = glslang_parse(context); error = glslang_parse(context);
glslang_finalize(context); glslang_finalize(context);
context->destroyPreprocessor();
return (error == 0) && (context->numErrors == 0) ? 0 : 1; return (error == 0) && (context->numErrors == 0) ? 0 : 1;
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "compiler/Diagnostics.h" #include "compiler/Diagnostics.h"
#include "compiler/DirectiveHandler.h" #include "compiler/DirectiveHandler.h"
#include "compiler/localintermediate.h" #include "compiler/localintermediate.h"
#include "compiler/preprocessor/new/Preprocessor.h"
#include "compiler/ShHandle.h" #include "compiler/ShHandle.h"
#include "compiler/SymbolTable.h" #include "compiler/SymbolTable.h"
...@@ -27,8 +28,6 @@ struct TParseContext { ...@@ -27,8 +28,6 @@ struct TParseContext {
TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, ShShaderType type, ShShaderSpec spec, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) : TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, ShShaderType type, ShShaderSpec spec, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) :
intermediate(interm), intermediate(interm),
symbolTable(symt), symbolTable(symt),
diagnostics(is),
directiveHandler(ext, diagnostics),
shaderType(type), shaderType(type),
shaderSpec(spec), shaderSpec(spec),
compileOptions(options), compileOptions(options),
...@@ -42,11 +41,12 @@ struct TParseContext { ...@@ -42,11 +41,12 @@ struct TParseContext {
currentFunctionType(NULL), currentFunctionType(NULL),
functionReturnsValue(false), functionReturnsValue(false),
checksPrecisionErrors(checksPrecErrors), checksPrecisionErrors(checksPrecErrors),
diagnostics(is),
directiveHandler(ext, diagnostics),
preprocessor(&diagnostics, &directiveHandler),
scanner(NULL) { } scanner(NULL) { }
TIntermediate& intermediate; // to hold and build a parse tree TIntermediate& intermediate; // to hold and build a parse tree
TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed
TDiagnostics diagnostics;
TDirectiveHandler directiveHandler;
ShShaderType shaderType; // vertex or fragment language (future: pack or unpack) ShShaderType shaderType; // vertex or fragment language (future: pack or unpack)
ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL. ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL.
int compileOptions; int compileOptions;
...@@ -62,11 +62,11 @@ struct TParseContext { ...@@ -62,11 +62,11 @@ struct TParseContext {
bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit. bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit.
TString HashErrMsg; TString HashErrMsg;
bool AfterEOF; bool AfterEOF;
TDiagnostics diagnostics;
TDirectiveHandler directiveHandler;
pp::Preprocessor preprocessor;
void* scanner; void* scanner;
bool initPreprocessor();
void destroyPreprocessor();
TInfoSink& infoSink() { return diagnostics.infoSink(); } TInfoSink& infoSink() { return diagnostics.infoSink(); }
void error(TSourceLoc loc, const char *reason, const char* token, void error(TSourceLoc loc, const char *reason, const char* token,
const char* extraInfoFormat, ...); const char* extraInfoFormat, ...);
...@@ -103,6 +103,7 @@ struct TParseContext { ...@@ -103,6 +103,7 @@ struct TParseContext {
bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type); bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
bool extensionErrorCheck(int line, const TString&); bool extensionErrorCheck(int line, const TString&);
const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
bool supportsExtension(const char* extension); bool supportsExtension(const char* extension);
void handleExtensionDirective(int line, const char* extName, const char* behavior); void handleExtensionDirective(int line, const char* extName, const char* behavior);
......
...@@ -38,6 +38,7 @@ WHICH GENERATES THE GLSL ES LEXER (glslang_lex.cpp). ...@@ -38,6 +38,7 @@ WHICH GENERATES THE GLSL ES LEXER (glslang_lex.cpp).
%{ %{
#include "compiler/glslang.h" #include "compiler/glslang.h"
#include "compiler/ParseHelper.h" #include "compiler/ParseHelper.h"
#include "compiler/preprocessor/new/Token.h"
#include "compiler/util.h" #include "compiler/util.h"
#include "glslang_tab.h" #include "glslang_tab.h"
...@@ -271,10 +272,15 @@ O [0-7] ...@@ -271,10 +272,15 @@ O [0-7]
%% %%
#if !ANGLE_USE_NEW_PREPROCESSOR
extern "C" { extern "C" {
// Preprocessor interface. // Preprocessor interface.
#include "compiler/preprocessor/preprocess.h" #include "compiler/preprocessor/preprocess.h"
extern int InitPreprocessor();
extern int FinalizePreprocessor();
extern void PredefineIntMacro(const char *name, int value);
#define SETUP_CONTEXT(pp) \ #define SETUP_CONTEXT(pp) \
TParseContext* context = (TParseContext*) pp->pC; \ TParseContext* context = (TParseContext*) pp->pC; \
struct yyguts_t* yyg = (struct yyguts_t*) context->scanner; struct yyguts_t* yyg = (struct yyguts_t*) context->scanner;
...@@ -393,17 +399,27 @@ void updateExtensionBehavior(const char* extName, const char* behavior) ...@@ -393,17 +399,27 @@ void updateExtensionBehavior(const char* extName, const char* behavior)
context->handleExtensionDirective(yylineno, extName, behavior); context->handleExtensionDirective(yylineno, extName, behavior);
} }
} // extern "C" } // extern "C"
#endif // !ANGLE_USE_NEW_PREPROCESSOR
int string_input(char* buf, int max_size, yyscan_t yyscanner) { int string_input(char* buf, int max_size, yyscan_t yyscanner) {
int len; int len = 0;
if ((len = yylex_CPP(buf, max_size)) == 0) #if ANGLE_USE_NEW_PREPROCESSOR
return 0; pp::Token token;
if (len >= max_size) yyget_extra(yyscanner)->preprocessor.lex(&token);
YY_FATAL_ERROR("input buffer overflow, can't enlarge buffer because scanner uses REJECT"); len = token.type == pp::Token::LAST ? 0 : token.value.size();
if ((len > 0) && (len < max_size))
buf[len] = ' '; memcpy(buf, token.value.c_str(), len);
return len+1; yyset_lineno(EncodeSourceLoc(token.location.file, token.location.line), yyscanner);
#else
len = yylex_CPP(buf, max_size);
#endif // ANGLE_USE_NEW_PREPROCESSOR
if (len >= max_size)
YY_FATAL_ERROR("Input buffer overflow");
else if (len > 0)
buf[len++] = ' ';
return len;
} }
int check_type(yyscan_t yyscanner) { int check_type(yyscan_t yyscanner) {
...@@ -455,7 +471,12 @@ int glslang_finalize(TParseContext* context) { ...@@ -455,7 +471,12 @@ int glslang_finalize(TParseContext* context) {
if (scanner == NULL) return 0; if (scanner == NULL) return 0;
context->scanner = NULL; context->scanner = NULL;
return yylex_destroy(scanner); yylex_destroy(scanner);
#if !ANGLE_USE_NEW_PREPROCESSOR
FinalizePreprocessor();
#endif
return 0;
} }
int glslang_scan(int count, const char* const string[], const int length[], int glslang_scan(int count, const char* const string[], const int length[],
...@@ -463,10 +484,30 @@ int glslang_scan(int count, const char* const string[], const int length[], ...@@ -463,10 +484,30 @@ int glslang_scan(int count, const char* const string[], const int length[],
yyrestart(NULL, context->scanner); yyrestart(NULL, context->scanner);
yyset_lineno(EncodeSourceLoc(0, 1), context->scanner); yyset_lineno(EncodeSourceLoc(0, 1), context->scanner);
context->AfterEOF = false; context->AfterEOF = false;
// Init preprocessor. // Initialize preprocessor.
#if ANGLE_USE_NEW_PREPROCESSOR
if (!context->preprocessor.init(count, string, length))
return 1;
#else
if (InitPreprocessor())
return 1;
cpp->pC = context; cpp->pC = context;
cpp->pastFirstStatement = 0; cpp->pastFirstStatement = 0;
return InitScannerInput(cpp, count, string, length); if (InitScannerInput(cpp, count, string, length))
return 1;
#endif // ANGLE_USE_NEW_PREPROCESSOR
// Define extension macros.
const TExtensionBehavior& extBehavior = context->extensionBehavior();
for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
iter != extBehavior.end(); ++iter) {
#if ANGLE_USE_NEW_PREPROCESSOR
context->preprocessor.predefineMacro(iter->first, 1);
#else
PredefineIntMacro(iter->first.c_str(), 1);
#endif
}
return 0;
} }
...@@ -807,6 +807,7 @@ WHICH GENERATES THE GLSL ES LEXER (glslang_lex.cpp). ...@@ -807,6 +807,7 @@ WHICH GENERATES THE GLSL ES LEXER (glslang_lex.cpp).
#include "compiler/glslang.h" #include "compiler/glslang.h"
#include "compiler/ParseHelper.h" #include "compiler/ParseHelper.h"
#include "compiler/preprocessor/new/Token.h"
#include "compiler/util.h" #include "compiler/util.h"
#include "glslang_tab.h" #include "glslang_tab.h"
...@@ -2947,10 +2948,15 @@ void yyfree (void * ptr , yyscan_t yyscanner) ...@@ -2947,10 +2948,15 @@ void yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables" #define YYTABLES_NAME "yytables"
#if !ANGLE_USE_NEW_PREPROCESSOR
extern "C" { extern "C" {
// Preprocessor interface. // Preprocessor interface.
#include "compiler/preprocessor/preprocess.h" #include "compiler/preprocessor/preprocess.h"
extern int InitPreprocessor();
extern int FinalizePreprocessor();
extern void PredefineIntMacro(const char *name, int value);
#define SETUP_CONTEXT(pp) \ #define SETUP_CONTEXT(pp) \
TParseContext* context = (TParseContext*) pp->pC; \ TParseContext* context = (TParseContext*) pp->pC; \
struct yyguts_t* yyg = (struct yyguts_t*) context->scanner; struct yyguts_t* yyg = (struct yyguts_t*) context->scanner;
...@@ -3069,17 +3075,27 @@ void updateExtensionBehavior(const char* extName, const char* behavior) ...@@ -3069,17 +3075,27 @@ void updateExtensionBehavior(const char* extName, const char* behavior)
context->handleExtensionDirective(yylineno, extName, behavior); context->handleExtensionDirective(yylineno, extName, behavior);
} }
} // extern "C" } // extern "C"
#endif // !ANGLE_USE_NEW_PREPROCESSOR
int string_input(char* buf, int max_size, yyscan_t yyscanner) { int string_input(char* buf, int max_size, yyscan_t yyscanner) {
int len; int len = 0;
if ((len = yylex_CPP(buf, max_size)) == 0) #if ANGLE_USE_NEW_PREPROCESSOR
return 0; pp::Token token;
if (len >= max_size) yyget_extra(yyscanner)->preprocessor.lex(&token);
YY_FATAL_ERROR("input buffer overflow, can't enlarge buffer because scanner uses REJECT"); len = token.type == pp::Token::LAST ? 0 : token.value.size();
if ((len > 0) && (len < max_size))
memcpy(buf, token.value.c_str(), len);
yyset_lineno(EncodeSourceLoc(token.location.file, token.location.line),yyscanner);
#else
len = yylex_CPP(buf, max_size);
#endif // ANGLE_USE_NEW_PREPROCESSOR
buf[len] = ' '; if (len >= max_size)
return len+1; YY_FATAL_ERROR("Input buffer overflow");
else if (len > 0)
buf[len++] = ' ';
return len;
} }
int check_type(yyscan_t yyscanner) { int check_type(yyscan_t yyscanner) {
...@@ -3131,7 +3147,12 @@ int glslang_finalize(TParseContext* context) { ...@@ -3131,7 +3147,12 @@ int glslang_finalize(TParseContext* context) {
if (scanner == NULL) return 0; if (scanner == NULL) return 0;
context->scanner = NULL; context->scanner = NULL;
return yylex_destroy(scanner); yylex_destroy(scanner);
#if !ANGLE_USE_NEW_PREPROCESSOR
FinalizePreprocessor();
#endif
return 0;
} }
int glslang_scan(int count, const char* const string[], const int length[], int glslang_scan(int count, const char* const string[], const int length[],
...@@ -3139,10 +3160,30 @@ int glslang_scan(int count, const char* const string[], const int length[], ...@@ -3139,10 +3160,30 @@ int glslang_scan(int count, const char* const string[], const int length[],
yyrestart(NULL,context->scanner); yyrestart(NULL,context->scanner);
yyset_lineno(EncodeSourceLoc(0, 1),context->scanner); yyset_lineno(EncodeSourceLoc(0, 1),context->scanner);
context->AfterEOF = false; context->AfterEOF = false;
// Init preprocessor. // Initialize preprocessor.
#if ANGLE_USE_NEW_PREPROCESSOR
if (!context->preprocessor.init(count, string, length))
return 1;
#else
if (InitPreprocessor())
return 1;
cpp->pC = context; cpp->pC = context;
cpp->pastFirstStatement = 0; cpp->pastFirstStatement = 0;
return InitScannerInput(cpp, count, string, length); if (InitScannerInput(cpp, count, string, length))
return 1;
#endif // ANGLE_USE_NEW_PREPROCESSOR
// Define extension macros.
const TExtensionBehavior& extBehavior = context->extensionBehavior();
for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
iter != extBehavior.end(); ++iter) {
#if ANGLE_USE_NEW_PREPROCESSOR
context->preprocessor.predefineMacro(iter->first, 1);
#else
PredefineIntMacro(iter->first.c_str(), 1);
#endif
}
return 0;
} }
...@@ -8,17 +8,40 @@ ...@@ -8,17 +8,40 @@
#include <sstream> #include <sstream>
#include "DirectiveParser.h"
#include "Macro.h"
#include "MacroExpander.h"
#include "Token.h" #include "Token.h"
#include "Tokenizer.h"
namespace pp namespace pp
{ {
struct PreprocessorImpl
{
MacroSet macroSet;
Tokenizer tokenizer;
DirectiveParser directiveParser;
MacroExpander macroExpander;
PreprocessorImpl(Diagnostics* diagnostics,
DirectiveHandler* directiveHandler) :
tokenizer(diagnostics),
directiveParser(&tokenizer, &macroSet, diagnostics, directiveHandler),
macroExpander(&directiveParser, &macroSet, diagnostics)
{
}
};
Preprocessor::Preprocessor(Diagnostics* diagnostics, Preprocessor::Preprocessor(Diagnostics* diagnostics,
DirectiveHandler* directiveHandler) : DirectiveHandler* directiveHandler)
mTokenizer(diagnostics), {
mDirectiveParser(&mTokenizer, &mMacroSet, diagnostics, directiveHandler), mImpl = new PreprocessorImpl(diagnostics, directiveHandler);
mMacroExpander(&mDirectiveParser, &mMacroSet, diagnostics) }
Preprocessor::~Preprocessor()
{ {
delete mImpl;
} }
bool Preprocessor::init(int count, bool Preprocessor::init(int count,
...@@ -33,10 +56,10 @@ bool Preprocessor::init(int count, ...@@ -33,10 +56,10 @@ bool Preprocessor::init(int count,
predefineMacro("__VERSION__", kGLSLVersion); predefineMacro("__VERSION__", kGLSLVersion);
predefineMacro("GL_ES", 1); predefineMacro("GL_ES", 1);
return mTokenizer.init(count, string, length); return mImpl->tokenizer.init(count, string, length);
} }
void Preprocessor::predefineMacro(const std::string& name, int value) void Preprocessor::predefineMacro(const char* name, int value)
{ {
std::stringstream stream; std::stringstream stream;
stream << value; stream << value;
...@@ -51,12 +74,12 @@ void Preprocessor::predefineMacro(const std::string& name, int value) ...@@ -51,12 +74,12 @@ void Preprocessor::predefineMacro(const std::string& name, int value)
macro.name = name; macro.name = name;
macro.replacements.push_back(token); macro.replacements.push_back(token);
mMacroSet[name] = macro; mImpl->macroSet[name] = macro;
} }
void Preprocessor::lex(Token* token) void Preprocessor::lex(Token* token)
{ {
mMacroExpander.lex(token); mImpl->macroExpander.lex(token);
} }
} // namespace pp } // namespace pp
......
...@@ -7,21 +7,21 @@ ...@@ -7,21 +7,21 @@
#ifndef COMPILER_PREPROCESSOR_PREPROCESSOR_H_ #ifndef COMPILER_PREPROCESSOR_PREPROCESSOR_H_
#define COMPILER_PREPROCESSOR_PREPROCESSOR_H_ #define COMPILER_PREPROCESSOR_PREPROCESSOR_H_
#include "DirectiveParser.h" #include "pp_utils.h"
#include "Macro.h"
#include "MacroExpander.h"
#include "Tokenizer.h"
namespace pp namespace pp
{ {
class Diagnostics; class Diagnostics;
class DirectiveHandler; class DirectiveHandler;
struct PreprocessorImpl;
struct Token;
class Preprocessor class Preprocessor
{ {
public: public:
Preprocessor(Diagnostics* diagnostics, DirectiveHandler* directiveHandler); Preprocessor(Diagnostics* diagnostics, DirectiveHandler* directiveHandler);
~Preprocessor();
// count: specifies the number of elements in the string and length arrays. // count: specifies the number of elements in the string and length arrays.
// string: specifies an array of pointers to strings. // string: specifies an array of pointers to strings.
...@@ -34,17 +34,14 @@ class Preprocessor ...@@ -34,17 +34,14 @@ class Preprocessor
// is null terminated. // is null terminated.
bool init(int count, const char* const string[], const int length[]); bool init(int count, const char* const string[], const int length[]);
// Adds a pre-defined macro. // Adds a pre-defined macro.
void predefineMacro(const std::string& name, int value); void predefineMacro(const char* name, int value);
void lex(Token* token); void lex(Token* token);
private: private:
PP_DISALLOW_COPY_AND_ASSIGN(Preprocessor); PP_DISALLOW_COPY_AND_ASSIGN(Preprocessor);
MacroSet mMacroSet; PreprocessorImpl* mImpl;
Tokenizer mTokenizer;
DirectiveParser mDirectiveParser;
MacroExpander mMacroExpander;
}; };
} // namespace pp } // namespace pp
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
// //
#include "PreprocessorTest.h" #include "PreprocessorTest.h"
#include "Input.h"
#include "Token.h" #include "Token.h"
class InitTest : public PreprocessorTest class InitTest : public PreprocessorTest
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment