Fix a preprocessor memory leak by cleaning up some preprocessor memory management.

git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@24125 e7fa87d3-cd2b-0410-9028-fcbf551c1848
This commit is contained in:
John Kessenich 2013-11-19 20:52:56 +00:00
parent 43e43ce77a
commit e1dba1b59f
9 changed files with 105 additions and 183 deletions

View File

@ -1,7 +1,8 @@
ERROR: 0:46: 'xyxwx' : illegal vector field selection ERROR: 0:46: 'xyxwx' : illegal vector field selection
ERROR: 0:46: 'xyxwx' : illegal vector field selection ERROR: 0:46: 'xyxwx' : illegal vector field selection
ERROR: 0:51: '' : missing #endif ERROR: 0:66: '#define' : Macro redefined; different substitutions: BIG
ERROR: 3 compilation errors. No code generated. ERROR: 0:79: '' : missing #endif
ERROR: 4 compilation errors. No code generated.
ERROR: node is still EOpNull! ERROR: node is still EOpNull!
0:4 Sequence 0:4 Sequence
@ -65,4 +66,6 @@ ERROR: node is still EOpNull!
0:47 3.000000 0:47 3.000000
0:? Linker Objects 0:? Linker Objects
0:? 'sum' (highp float) 0:? 'sum' (highp float)
0:? 'gl_VertexID' (gl_VertexId highp int)
0:? 'gl_InstanceID' (gl_InstanceId highp int)

View File

@ -1,6 +1,6 @@
#version 300 es
#define ON1 #define ON1
#define ON2 #define ON2
float sum = 0.0; float sum = 0.0;
void main() void main()
@ -47,5 +47,33 @@ float foo()
return ADD(gl_Position.y, 3.0) return ADD(gl_Position.y, 3.0)
} }
#define BIG aonetuhanoethuanoenaoethu snaoetuhs onethausoentuas hnoethaueohnatuoeh santuoehsantouhe snathoensuta hsnoethuasntoe hsnuathoesnuathoenstuh nsoethantseuh toae ua \
antoeh uantheount oentahoent uahnsoethasnutoehansteuo santhu sneoathu snoethasnut oesanthoesna thusenotha nsthasunoeth ausntehsunathoensuathoesnta uhnsoetha usntoeh uanhs unosethu \
antoehunatoehu natoehua oentha neotuhan toehu natoehu ntahoe nutah eu natoheunathoen uasoenuasoent asntoehsan tuosnthnu aohenuath eontha untoh eunth unth anth unth nth nth nt \
a ntoehanu tunth nsont uhansoethausn oehsanthnt heauo eanthuo sh nahnoethansu tohe sanuthoe snathuoesntha snuothe anthusonehtasuntoeh asnuthonsa teauhntoeha onetuha nth \
anoethuan toentauh noethauntohe anuthoe nathu noethaun oethanuthoe nathuoe ntahu enotha unetha ntuhenaothu enotahun eotha ntoehu aoehuntha enotuh aonethau noethu anoethuna toheua \
ontehanutoe hnuathoena aoteha aonetuha
// identical
#define BIG aonetuhanoethuanoenaoethu snaoetuhs onethausoentuas hnoethaueohnatuoeh santuoehsantouhe snathoensuta hsnoethuasntoe hsnuathoesnuathoenstuh nsoethantseuh toae ua \
antoeh uantheount oentahoent uahnsoethasnutoehansteuo santhu sneoathu snoethasnut oesanthoesna thusenotha nsthasunoeth ausntehsunathoensuathoesnta uhnsoetha usntoeh uanhs unosethu \
antoehunatoehu natoehua oentha neotuhan toehu natoehu ntahoe nutah eu natoheunathoen uasoenuasoent asntoehsan tuosnthnu aohenuath eontha untoh eunth unth anth unth nth nth nt \
a ntoehanu tunth nsont uhansoethausn oehsanthnt heauo eanthuo sh nahnoethansu tohe sanuthoe snathuoesntha snuothe anthusonehtasuntoeh asnuthonsa teauhntoeha onetuha nth \
anoethuan toentauh noethauntohe anuthoe nathu noethaun oethanuthoe nathuoe ntahu enotha unetha ntuhenaothu enotahun eotha ntoehu aoehuntha enotuh aonethau noethu anoethuna toheua \
ontehanutoe hnuathoena aoteha aonetuha
// ERROR, one character different
#define BIG aonetuhanoethuanoenaoethu snaoetuhs onethausoentuas hnoethaueohnatuoeh santuoehsantouhe snathoensuta hsnoethuasntoe hsnuathoesnuathoenstuh nsoethantseuh toae ua \
antoeh uantheount oentahoent uahnsoethasnutoehansteuo santhu sneoathu snoethasnut oesanthoesna thusenotha nsthasunoeth ausntehsunathoensuathoesnta uhnsoetha usntoeh uanhs unosethu \
antoehunatoehu natoehua oentha neotuhan toehu natoehu ntahoe nutah eu natoheunathoen uasoenuasoent asntoehsan tuosnthnu aohenuath eontha untoh eunth unth anth unth nth nth nt \
a ntoehanu tunth nsont uhansoethasn oehsanthnt heauo eanthuo sh nahnoethansu tohe sanuthoe snathuoesntha snuothe anthusonehtasuntoeh asnuthonsa teauhntoeha onetuha nth \
anoethuan toentauh noethauntohe anuthoe nathu noethaun oethanuthoe nathuoe ntahu enotha unetha ntuhenaothu enotahun eotha ntoehu aoehuntha enotuh aonethau noethu anoethuna toheua \
ontehanutoe hnuathoena aoteha aonetuha
#define BIGARGS1(aonthanotehu, bonthanotehu, conthanotehu, donthanotehu, eonthanotehu, fonthanotehu, gonthanotehu, honthanotehu, ionthanotehu, jonthanotehu, konthanotehu) jonthanotehu
#define BIGARGS2(aonthanotehu, bonthanotehu, conthanotehu, donthanotehu, eonthanotehu, fonthanotehu, gonthanotehu, honthanotehu, ionthanotehu, jonthanotehu, konthanotehu) jonthanotehu
#define BIGARGS3(aonthanotehu, bonthanotehu, conthanotehu, donthanotehu, eonthanotehu, fonthanotehu, gonthanotehu, honthanotehu, ionthanotehu, jonthanotehu, konthanotehu) jonthanotehu
#define BIGARGS4(aonthanotehu, bonthanotehu, conthanotehu, donthanotehu, eonthanotehu, fonthanotehu, gonthanotehu, honthanotehu, ionthanotehu, jonthanotehu, konthanotehu) jonthanotehu
#if 0 #if 0
// ERROR, EOF // ERROR, EOF

View File

@ -90,10 +90,6 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "PpContext.h" #include "PpContext.h"
#include "PpTokens.h" #include "PpTokens.h"
/* Don't use memory.c's replacements, as we clean up properly here */
#undef malloc
#undef free
namespace glslang { namespace glslang {
int TPpContext::InitCPP() int TPpContext::InitCPP()
@ -129,19 +125,11 @@ int TPpContext::InitCPP()
return 1; return 1;
} }
int TPpContext::FinalCPP()
{
mem_FreePool(pool);
return 1;
}
int TPpContext::CPPdefine(TPpToken* ppToken) int TPpContext::CPPdefine(TPpToken* ppToken)
{ {
int token, atom, args[maxMacroArgs], argc; int token, atom, args[maxMacroArgs], argc;
MacroSymbol mac; MacroSymbol mac;
Symbol *symb; Symbol *symb;
memset(&mac, 0, sizeof(mac));
token = currentInput->scan(this, currentInput, ppToken); token = currentInput->scan(this, currentInput, ppToken);
if (token != CPP_IDENTIFIER) { if (token != CPP_IDENTIFIER) {
parseContext.error(ppToken->loc, "must be followed by macro name", "#define", ""); parseContext.error(ppToken->loc, "must be followed by macro name", "#define", "");
@ -194,7 +182,7 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
token = currentInput->scan(this, currentInput, ppToken); token = currentInput->scan(this, currentInput, ppToken);
} }
TSourceLoc defineLoc = ppToken->loc; // because ppToken is going to go to the next line before we report errors TSourceLoc defineLoc = ppToken->loc; // because ppToken is going to go to the next line before we report errors
mac.body = NewTokenStream(pool); mac.body = new TokenStream;
while (token != '\n') { while (token != '\n') {
if (token == '\\') { if (token == '\\') {
parseContext.lineContinuationCheck(ppToken->loc); parseContext.lineContinuationCheck(ppToken->loc);
@ -243,6 +231,7 @@ int TPpContext::CPPdefine(TPpToken* ppToken)
} else { } else {
symb = AddSymbol(atom); symb = AddSymbol(atom);
} }
delete symb->mac.body;
symb->mac = mac; symb->mac = mac;
return '\n'; return '\n';
@ -857,17 +846,12 @@ int TPpContext::readCPPline(TPpToken * ppToken)
return token; return token;
} }
void TPpContext::FreeMacro(MacroSymbol *s) {
DeleteTokenStream(s->body);
}
int eof_scan(TPpContext*, TPpContext::InputSrc* in, TPpToken* ppToken) { return -1; } int eof_scan(TPpContext*, TPpContext::InputSrc* in, TPpToken* ppToken) { return -1; }
void noop(TPpContext*, TPpContext::InputSrc* in, int ch, TPpToken* ppToken) { } void noop(TPpContext*, TPpContext::InputSrc* in, int ch, TPpToken* ppToken) { }
void TPpContext::PushEofSrc() void TPpContext::PushEofSrc()
{ {
InputSrc *in = (InputSrc*)malloc(sizeof(InputSrc)); InputSrc *in = new InputSrc;
memset(in, 0, sizeof(InputSrc));
in->scan = eof_scan; in->scan = eof_scan;
in->getch = eof_scan; in->getch = eof_scan;
in->ungetch = noop; in->ungetch = noop;
@ -880,7 +864,7 @@ void TPpContext::PopEofSrc()
if (currentInput->scan == eof_scan) { if (currentInput->scan == eof_scan) {
InputSrc *in = currentInput; InputSrc *in = currentInput;
currentInput = in->prev; currentInput = in->prev;
free(in); delete in;
} }
} }
@ -896,7 +880,7 @@ TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream *a, TPpToken *
} while (token != EOF); } while (token != EOF);
if (token == EOF) if (token == EOF)
return a; return a;
n = NewTokenStream(0); n = new TokenStream;
PushEofSrc(); PushEofSrc();
ReadFromTokenStream(a, 0, 0); ReadFromTokenStream(a, 0, 0);
while ((token = currentInput->scan(this, currentInput, ppToken)) > 0) { while ((token = currentInput->scan(this, currentInput, ppToken)) > 0) {
@ -905,7 +889,7 @@ TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream *a, TPpToken *
RecordToken(n, token, ppToken); RecordToken(n, token, ppToken);
} }
PopEofSrc(); PopEofSrc();
DeleteTokenStream(a); delete a;
return n; return n;
} }
@ -917,9 +901,9 @@ TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream *a, TPpToken *
/* /*
** return the next token for a macro expansion, handling macro args ** return the next token for a macro expansion, handling macro args
*/ */
int TPpContext::macro_scan(TPpContext* pp, TPpContext::InputSrc* inInput, TPpToken* ppToken) int TPpContext::macro_scan(TPpContext* pp, InputSrc* inInput, TPpToken* ppToken)
{ {
TPpContext::MacroInputSrc* in = (TPpContext::MacroInputSrc*)inInput; MacroInputSrc* in = (TPpContext::MacroInputSrc*)inInput;
int i; int i;
int token; int token;
@ -942,13 +926,8 @@ int TPpContext::macro_scan(TPpContext* pp, TPpContext::InputSrc* inInput, TPpTok
return token; return token;
in->mac->busy = 0; in->mac->busy = 0;
pp->currentInput = in->base.prev; pp->currentInput = in->prev;
if (in->args) { delete in;
for (i=in->mac->argc-1; i>=0; i--)
pp->DeleteTokenStream(in->args[i]);
free(in->args);
}
free(in);
return pp->currentInput->scan(pp, pp->currentInput, ppToken); return pp->currentInput->scan(pp, pp->currentInput, ppToken);
} }
@ -962,8 +941,8 @@ int TPpContext::zero_scan(TPpContext* pp, InputSrc *inInput, TPpToken* ppToken)
ppToken->ival = 0; ppToken->ival = 0;
// pop input // pop input
pp->currentInput = in->base.prev; pp->currentInput = in->prev;
free(in); delete in;
return CPP_INTCONSTANT; return CPP_INTCONSTANT;
} }
@ -1015,19 +994,18 @@ int TPpContext::MacroExpand(int atom, TPpToken* ppToken, int expandUndef)
if ((! sym || sym->mac.undef) && ! expandUndef) if ((! sym || sym->mac.undef) && ! expandUndef)
return 0; return 0;
in = (MacroInputSrc*)malloc(sizeof(*in)); in = new MacroInputSrc;
memset(in, 0, sizeof(*in));
if ((! sym || sym->mac.undef) && expandUndef) { if ((! sym || sym->mac.undef) && expandUndef) {
// push input // push input
in->base.scan = zero_scan; in->scan = zero_scan;
in->base.prev = currentInput; in->prev = currentInput;
currentInput = &in->base; currentInput = in;
return -1; return -1;
} }
in->base.scan = macro_scan; in->scan = macro_scan;
in->mac = &sym->mac; in->mac = &sym->mac;
if (sym->mac.args) { if (sym->mac.args) {
token = currentInput->scan(this, currentInput, ppToken); token = currentInput->scan(this, currentInput, ppToken);
@ -1037,9 +1015,9 @@ int TPpContext::MacroExpand(int atom, TPpToken* ppToken, int expandUndef)
return 0; return 0;
} }
in->args = (TokenStream**)malloc(in->mac->argc * sizeof(TokenStream *)); in->args.resize(in->mac->argc);
for (i = 0; i < in->mac->argc; i++) for (i = 0; i < in->mac->argc; i++)
in->args[i] = NewTokenStream(0); in->args[i] = new TokenStream;
i = 0; i = 0;
j = 0; j = 0;
do { do {
@ -1086,16 +1064,15 @@ int TPpContext::MacroExpand(int atom, TPpToken* ppToken, int expandUndef)
} }
parseContext.error(ppToken->loc, "Too many args in Macro", "preprocessor", GetAtomString(atom)); parseContext.error(ppToken->loc, "Too many args in Macro", "preprocessor", GetAtomString(atom));
} }
for (i = 0; i<in->mac->argc; i++) { for (i = 0; i < in->mac->argc; i++)
in->args[i] = PrescanMacroArg(in->args[i], ppToken); in->args[i] = PrescanMacroArg(in->args[i], ppToken);
} }
}
/*retain the input source*/ /*retain the input source*/
in->base.prev = currentInput; in->prev = currentInput;
sym->mac.busy = 1; sym->mac.busy = 1;
RewindTokenStream(sym->mac.body); RewindTokenStream(sym->mac.body);
currentInput = &in->base; currentInput = in;
return 1; return 1;
} }

View File

@ -90,10 +90,6 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "PpContext.h" #include "PpContext.h"
#include "PpTokens.h" #include "PpTokens.h"
#undef malloc
#undef realloc
#undef free
namespace { namespace {
using namespace glslang; using namespace glslang;

View File

@ -97,20 +97,21 @@ TPpContext::TPpContext(TParseContext& pc) :
TPpContext::~TPpContext() TPpContext::~TPpContext()
{ {
FinalCPP(); for (TSymbolMap::iterator it = symbols.begin(); it != symbols.end(); ++it)
delete it->second->mac.body;
mem_FreePool(pool);
delete [] preamble; delete [] preamble;
} }
void TPpContext::setInput(TInputScanner& input, bool versionWillBeError) void TPpContext::setInput(TInputScanner& input, bool versionWillBeError)
{ {
StringInputSrc *in = (StringInputSrc *)malloc(sizeof(StringInputSrc)); StringInputSrc* in = new StringInputSrc;
memset(in, 0, sizeof(StringInputSrc));
in->input = &input; in->input = &input;
in->base.scan = sourceScan; in->scan = sourceScan;
in->base.getch = (int (*)(TPpContext*, InputSrc *, TPpToken *))sourceGetCh; in->getch = (int (*)(TPpContext*, InputSrc *, TPpToken *))sourceGetCh;
in->base.ungetch = (void (*)(TPpContext*, InputSrc *, int, TPpToken *))sourceUngetCh; in->ungetch = (void (*)(TPpContext*, InputSrc *, int, TPpToken *))sourceUngetCh;
in->base.prev = currentInput; in->prev = currentInput;
currentInput = &in->base; currentInput = in;
errorOnVersion = versionWillBeError; errorOnVersion = versionWillBeError;
} }

View File

@ -126,24 +126,17 @@ public:
// TODO: preprocessor simplification: this should be a base class, not a set of function pointers // TODO: preprocessor simplification: this should be a base class, not a set of function pointers
struct InputSrc { struct InputSrc {
InputSrc() : prev(0), scan(0), getch(0), ungetch(0) { }
struct InputSrc *prev; struct InputSrc *prev;
int (*scan)(TPpContext*, struct InputSrc *, TPpToken *); int (*scan)(TPpContext*, struct InputSrc *, TPpToken *);
int (*getch)(TPpContext*, struct InputSrc *, TPpToken *); int (*getch)(TPpContext*, struct InputSrc *, TPpToken *);
void (*ungetch)(TPpContext*, struct InputSrc *, int, TPpToken *); void (*ungetch)(TPpContext*, struct InputSrc *, int, TPpToken *);
}; };
struct TokenBlock {
TokenBlock *next;
int current;
int count;
int max;
unsigned char *data;
};
struct TokenStream { struct TokenStream {
TokenStream *next; TokenStream() : current(0) { }
TokenBlock *head; TVector<unsigned char> data;
TokenBlock *current; size_t current;
}; };
struct MemoryPool { struct MemoryPool {
@ -159,6 +152,7 @@ public:
// //
struct MacroSymbol { struct MacroSymbol {
MacroSymbol() : argc(0), args(0), body(0), busy(0), undef(0) { }
int argc; int argc;
int *args; int *args;
TokenStream *body; TokenStream *body;
@ -201,10 +195,15 @@ protected:
int elsetracker; // #if-#else and #endif constructs...Counter. int elsetracker; // #if-#else and #endif constructs...Counter.
const char *ErrMsg; const char *ErrMsg;
struct MacroInputSrc { struct MacroInputSrc : public InputSrc {
InputSrc base; MacroInputSrc() : mac(0) { }
virtual ~MacroInputSrc()
{
for (size_t i = 0; i < args.size(); ++i)
delete args[i];
}
MacroSymbol *mac; MacroSymbol *mac;
TokenStream **args; TVector<TokenStream*> args;
}; };
InputSrc *currentInput; InputSrc *currentInput;
@ -241,7 +240,6 @@ protected:
TSourceLoc ifloc; /* outermost #if */ TSourceLoc ifloc; /* outermost #if */
int InitCPP(); int InitCPP();
int FinalCPP();
int CPPdefine(TPpToken * ppToken); int CPPdefine(TPpToken * ppToken);
int CPPundef(TPpToken * ppToken); int CPPundef(TPpToken * ppToken);
int CPPelse(int matchelse, TPpToken * ppToken); int CPPelse(int matchelse, TPpToken * ppToken);
@ -255,7 +253,6 @@ protected:
int CPPversion(TPpToken * ppToken); int CPPversion(TPpToken * ppToken);
int CPPextension(TPpToken * ppToken); int CPPextension(TPpToken * ppToken);
int readCPPline(TPpToken * ppToken); int readCPPline(TPpToken * ppToken);
void FreeMacro(MacroSymbol *s);
void PushEofSrc(); void PushEofSrc();
void PopEofSrc(); void PopEofSrc();
TokenStream* PrescanMacroArg(TokenStream *a, TPpToken * ppToken); TokenStream* PrescanMacroArg(TokenStream *a, TPpToken * ppToken);
@ -273,24 +270,19 @@ protected:
// //
// From PpTokens.cpp // From PpTokens.cpp
// //
TPpContext::TokenBlock* lNewBlock(TokenStream *fTok, MemoryPool *pool);
void lAddByte(TokenStream *fTok, unsigned char fVal); void lAddByte(TokenStream *fTok, unsigned char fVal);
int lReadByte(TokenStream *pTok); int lReadByte(TokenStream *pTok);
TokenStream *NewTokenStream(MemoryPool *pool);
void DeleteTokenStream(TokenStream *pTok);
void RecordToken(TokenStream* pTok, int token, TPpToken* ppToken); void RecordToken(TokenStream* pTok, int token, TPpToken* ppToken);
void RewindTokenStream(TokenStream *pTok); void RewindTokenStream(TokenStream *pTok);
int ReadToken(TokenStream* pTok, TPpToken* ppToken); int ReadToken(TokenStream* pTok, TPpToken* ppToken);
int ReadFromTokenStream(TokenStream *ts, int name, int (*final)(TPpContext *)); int ReadFromTokenStream(TokenStream *ts, int name, int (*final)(TPpContext *));
void UngetToken(int token, TPpToken* ppToken); void UngetToken(int token, TPpToken* ppToken);
struct TokenInputSrc { struct TokenInputSrc : public InputSrc {
InputSrc base;
TokenStream *tokens; TokenStream *tokens;
int (*final)(TPpContext *); int (*final)(TPpContext *);
}; };
static int scan_token(TPpContext*, TokenInputSrc *in, TPpToken * ppToken); static int scan_token(TPpContext*, TokenInputSrc *in, TPpToken * ppToken);
struct UngotToken { struct UngotToken : public InputSrc {
InputSrc base;
int token; int token;
TPpToken lval; TPpToken lval;
}; };
@ -299,8 +291,7 @@ protected:
// //
// From PpScanner.cpp // From PpScanner.cpp
// //
struct StringInputSrc { struct StringInputSrc : public InputSrc {
InputSrc base;
TInputScanner* input; TInputScanner* input;
}; };
int InitScanner(TPpContext *cpp); int InitScanner(TPpContext *cpp);

View File

@ -87,10 +87,6 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define CHUNKSIZE (64*1024) #define CHUNKSIZE (64*1024)
#define ALIGN 8 #define ALIGN 8
// we need to call the `real' malloc and free, not our replacements
#undef malloc
#undef free
namespace glslang { namespace glslang {
struct chunk { struct chunk {

View File

@ -110,7 +110,7 @@ int TPpContext::sourceGetCh(TPpContext* pp, StringInputSrc *in)
int ch = in->input->get(); int ch = in->input->get();
if (ch == EOF) if (ch == EOF)
free(in); delete in;
return ch; return ch;
} }

View File

@ -96,36 +96,9 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang { namespace glslang {
TPpContext::TokenBlock* TPpContext::lNewBlock(TokenStream *fTok, MemoryPool *pool)
{
TokenBlock *lBlock;
if (!pool)
lBlock = (TokenBlock *) malloc(sizeof(TokenBlock) + 256);
else
lBlock = (TokenBlock *) mem_Alloc(pool, sizeof(TokenBlock) + 256);
lBlock->count = 0;
lBlock->current = 0;
lBlock->data = (unsigned char *) lBlock + sizeof(TokenBlock);
lBlock->max = 256;
lBlock->next = NULL;
if (fTok->head) {
fTok->current->next = lBlock;
} else {
fTok->head = lBlock;
}
fTok->current = lBlock;
return lBlock;
}
void TPpContext::lAddByte(TokenStream *fTok, unsigned char fVal) void TPpContext::lAddByte(TokenStream *fTok, unsigned char fVal)
{ {
TokenBlock *lBlock; fTok->data.push_back(fVal);
lBlock = fTok->current;
if (lBlock->count >= lBlock->max)
lBlock = lNewBlock(fTok, 0);
lBlock->data[lBlock->count++] = fVal;
} }
/* /*
@ -133,54 +106,10 @@ void TPpContext::lAddByte(TokenStream *fTok, unsigned char fVal)
*/ */
int TPpContext::lReadByte(TokenStream *pTok) int TPpContext::lReadByte(TokenStream *pTok)
{ {
TokenBlock *lBlock; if (pTok->current < pTok->data.size())
int lval = -1; return pTok->data[pTok->current++];
lBlock = pTok->current;
if (lBlock) {
if (lBlock->current >= lBlock->count) {
lBlock = lBlock->next;
if (lBlock)
lBlock->current = 0;
pTok->current = lBlock;
}
if (lBlock)
lval = lBlock->data[lBlock->current++];
}
return lval;
}
//
// Make a token stream (used for reprocessing macros).
//
TPpContext::TokenStream* TPpContext::NewTokenStream(MemoryPool *pool)
{
TokenStream *pTok;
if (!pool)
pTok = (TokenStream *) malloc(sizeof(TokenStream));
else else
pTok = (TokenStream*)mem_Alloc(pool, sizeof(TokenStream)); return -1;
pTok->next = NULL;
pTok->head = NULL;
pTok->current = NULL;
lNewBlock(pTok, pool);
return pTok;
}
void TPpContext::DeleteTokenStream(TokenStream *pTok)
{
TokenBlock *pBlock, *nBlock;
if (pTok) {
pBlock = pTok->head;
while (pBlock) {
nBlock = pBlock->next;
free(pBlock);
pBlock = nBlock;
}
free(pTok);
}
} }
/* /*
@ -227,10 +156,12 @@ void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken)
*/ */
void TPpContext::RewindTokenStream(TokenStream *pTok) void TPpContext::RewindTokenStream(TokenStream *pTok)
{ {
if (pTok->head) { pTok->current = 0;
pTok->current = pTok->head;
pTok->current->current = 0; //if (pTok->head) {
} // pTok->current = pTok->head;
// pTok->current->current = 0;
//}
} }
/* /*
@ -306,9 +237,9 @@ int TPpContext::scan_token(TPpContext* pp, TokenInputSrc *in, TPpToken * ppToken
if (token > 0) if (token > 0)
return token; return token;
pp->currentInput = in->base.prev; pp->currentInput = in->prev;
final = in->final; final = in->final;
free(in); delete in;
if (final && !final(pp)) if (final && !final(pp))
return -1; return -1;
@ -317,14 +248,13 @@ int TPpContext::scan_token(TPpContext* pp, TokenInputSrc *in, TPpToken * ppToken
int TPpContext::ReadFromTokenStream(TokenStream *ts, int name, int (*final)(TPpContext *)) int TPpContext::ReadFromTokenStream(TokenStream *ts, int name, int (*final)(TPpContext *))
{ {
TokenInputSrc *in = (TokenInputSrc *) malloc(sizeof(TokenInputSrc)); TokenInputSrc* in = new TokenInputSrc;
memset(in, 0, sizeof(TokenInputSrc)); in->prev = currentInput;
in->base.prev = currentInput; in->scan = (int (*)(TPpContext*, InputSrc*, TPpToken*))scan_token;
in->base.scan = (int (*)(TPpContext*, InputSrc*, TPpToken*))scan_token;
in->tokens = ts; in->tokens = ts;
in->final = final; in->final = final;
RewindTokenStream(ts); RewindTokenStream(ts);
currentInput = &in->base; currentInput = in;
return 1; return 1;
} }
@ -333,20 +263,20 @@ int TPpContext::reget_token(TPpContext* pp, UngotToken *t, TPpToken * ppToken)
{ {
int token = t->token; int token = t->token;
*ppToken = t->lval; *ppToken = t->lval;
pp->currentInput = t->base.prev; pp->currentInput = t->prev;
free(t); delete t;
return token; return token;
} }
void TPpContext::UngetToken(int token, TPpToken* ppToken) void TPpContext::UngetToken(int token, TPpToken* ppToken)
{ {
UngotToken *t = (UngotToken *) malloc(sizeof(UngotToken)); UngotToken *t = new UngotToken;
memset(t, 0, sizeof(UngotToken));
t->token = token; t->token = token;
t->lval = *ppToken; t->lval = *ppToken;
t->base.scan = (int(*)(TPpContext*, struct InputSrc *, TPpToken *))reget_token; t->scan = (int(*)(TPpContext*, struct InputSrc *, TPpToken *))reget_token;
t->base.prev = currentInput; t->prev = currentInput;
currentInput = &t->base; currentInput = t;
} }
} // end namespace glslang } // end namespace glslang