mirror of
https://github.com/cc65/cc65.git
synced 2025-01-13 09:31:53 +00:00
Better preprocess macro expansion.
This commit is contained in:
parent
6fc2cd9f61
commit
95e5471cd6
@ -103,8 +103,8 @@ static Collection IFiles = STATIC_COLLECTION_INITIALIZER;
|
||||
/* List of all active files */
|
||||
static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
|
||||
|
||||
/* Input stack used when preprocessing. */
|
||||
static Collection InputStack = STATIC_COLLECTION_INITIALIZER;
|
||||
/* Input stack used when preprocessing */
|
||||
static Collection* CurrentInputStack;
|
||||
|
||||
/* Counter for the __COUNTER__ macro */
|
||||
static unsigned MainFileCounter;
|
||||
@ -394,34 +394,37 @@ static void GetInputChar (void)
|
||||
** are read by this function.
|
||||
*/
|
||||
{
|
||||
/* Drop all pushed fragments that don't have data left */
|
||||
while (SB_GetIndex (Line) >= SB_GetLen (Line)) {
|
||||
/* Cannot read more from this line, check next line on stack if any */
|
||||
if (CollCount (&InputStack) == 0) {
|
||||
/* This is THE line */
|
||||
break;
|
||||
}
|
||||
FreeStrBuf (Line);
|
||||
Line = CollPop (&InputStack);
|
||||
}
|
||||
|
||||
/* Now get the next characters from the line */
|
||||
if (SB_GetIndex (Line) >= SB_GetLen (Line)) {
|
||||
CurC = NextC = '\0';
|
||||
/* Get the next-next character from the line */
|
||||
if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) {
|
||||
/* CurC and NextC come from this fragment */
|
||||
CurC = SB_AtUnchecked (Line, SB_GetIndex (Line));
|
||||
NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1);
|
||||
} else {
|
||||
CurC = SB_AtUnchecked (Line, SB_GetIndex (Line));
|
||||
if (SB_GetIndex (Line) + 1 < SB_GetLen (Line)) {
|
||||
/* NextC comes from this fragment */
|
||||
NextC = SB_AtUnchecked (Line, SB_GetIndex (Line) + 1);
|
||||
} else {
|
||||
/* NextC is '\0' by default */
|
||||
NextC = '\0';
|
||||
|
||||
/* Drop all pushed fragments that don't have data left */
|
||||
if (CurrentInputStack != 0) {
|
||||
while (SB_GetIndex (Line) >= SB_GetLen (Line)) {
|
||||
/* Cannot read more from this line, check next line on stack if any */
|
||||
if (CollCount (CurrentInputStack) == 0) {
|
||||
/* This is THE line */
|
||||
break;
|
||||
}
|
||||
FreeStrBuf (Line);
|
||||
Line = CollPop (CurrentInputStack);
|
||||
}
|
||||
|
||||
/* NextC comes from next fragment */
|
||||
if (CollCount (&InputStack) > 0) {
|
||||
if (CollCount (CurrentInputStack) > 0) {
|
||||
NextC = ' ';
|
||||
} else {
|
||||
NextC = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/* Get CurC from the line */
|
||||
CurC = SB_LookAt (Line, SB_GetIndex (Line));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -441,16 +444,46 @@ void NextChar (void)
|
||||
|
||||
|
||||
|
||||
Collection* UseInputStack (Collection* InputStack)
|
||||
/* Use the provided input stack for incoming input. Return the previously used
|
||||
** InputStack.
|
||||
*/
|
||||
{
|
||||
Collection* OldInputStack = CurrentInputStack;
|
||||
|
||||
CurrentInputStack = InputStack;
|
||||
return OldInputStack;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PushLine (StrBuf* L)
|
||||
/* Save the current input line and use a new one */
|
||||
{
|
||||
PRECONDITION (CurrentInputStack != 0);
|
||||
if (SB_GetIndex (L) < SB_GetLen (L)) {
|
||||
CollAppend (CurrentInputStack, Line);
|
||||
Line = L;
|
||||
GetInputChar ();
|
||||
} else {
|
||||
FreeStrBuf (L);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ClearLine (void)
|
||||
/* Clear the current input line */
|
||||
{
|
||||
unsigned I;
|
||||
if (CurrentInputStack != 0) {
|
||||
unsigned I;
|
||||
|
||||
/* Remove all pushed fragments from the input stack */
|
||||
for (I = 0; I < CollCount (&InputStack); ++I) {
|
||||
FreeStrBuf (CollAtUnchecked (&InputStack, I));
|
||||
/* Remove all pushed fragments from the input stack */
|
||||
for (I = 0; I < CollCount (CurrentInputStack); ++I) {
|
||||
FreeStrBuf (Line);
|
||||
Line = CollPop (CurrentInputStack);
|
||||
}
|
||||
}
|
||||
CollDeleteAll (&InputStack);
|
||||
|
||||
/* Clear the contents of Line */
|
||||
SB_Clear (Line);
|
||||
|
@ -70,6 +70,8 @@ extern StrBuf* Line;
|
||||
extern char CurC;
|
||||
extern char NextC;
|
||||
|
||||
typedef struct Collection Collection;
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
@ -95,6 +97,14 @@ void NextChar (void);
|
||||
** are read by this function.
|
||||
*/
|
||||
|
||||
Collection* UseInputStack (Collection* InputStack);
|
||||
/* Use the provided input stack for incoming input. Return the previously used
|
||||
** InputStack.
|
||||
*/
|
||||
|
||||
void PushLine (StrBuf* L);
|
||||
/* Save the current input line and use a new one */
|
||||
|
||||
void ClearLine (void);
|
||||
/* Clear the current input line */
|
||||
|
||||
|
@ -74,19 +74,17 @@ Macro* NewMacro (const char* Name)
|
||||
*/
|
||||
{
|
||||
/* Get the length of the macro name */
|
||||
unsigned Len = strlen(Name);
|
||||
unsigned Len = strlen (Name);
|
||||
|
||||
/* Allocate the structure */
|
||||
Macro* M = (Macro*) xmalloc (sizeof(Macro) + Len);
|
||||
|
||||
/* Initialize the data */
|
||||
M->Next = 0;
|
||||
M->Expanding = 0;
|
||||
M->ArgCount = -1; /* Flag: Not a function like macro */
|
||||
M->MaxArgs = 0;
|
||||
InitCollection (&M->FormalArgs);
|
||||
M->Next = 0;
|
||||
M->ParamCount = -1; /* Flag: Not a function-like macro */
|
||||
InitCollection (&M->Params);
|
||||
SB_Init (&M->Replacement);
|
||||
M->Variadic = 0;
|
||||
M->Variadic = 0;
|
||||
memcpy (M->Name, Name, Len+1);
|
||||
|
||||
/* Return the new macro */
|
||||
@ -102,10 +100,10 @@ void FreeMacro (Macro* M)
|
||||
{
|
||||
unsigned I;
|
||||
|
||||
for (I = 0; I < CollCount (&M->FormalArgs); ++I) {
|
||||
xfree (CollAtUnchecked (&M->FormalArgs, I));
|
||||
for (I = 0; I < CollCount (&M->Params); ++I) {
|
||||
xfree (CollAtUnchecked (&M->Params, I));
|
||||
}
|
||||
DoneCollection (&M->FormalArgs);
|
||||
DoneCollection (&M->Params);
|
||||
SB_Done (&M->Replacement);
|
||||
xfree (M);
|
||||
}
|
||||
@ -121,12 +119,12 @@ Macro* CloneMacro (const Macro* M)
|
||||
Macro* New = NewMacro (M->Name);
|
||||
unsigned I;
|
||||
|
||||
for (I = 0; I < CollCount (&M->FormalArgs); ++I) {
|
||||
/* Copy the argument */
|
||||
const char* Arg = CollAtUnchecked (&M->FormalArgs, I);
|
||||
CollAppend (&New->FormalArgs, xstrdup (Arg));
|
||||
for (I = 0; I < CollCount (&M->Params); ++I) {
|
||||
/* Copy the parameter */
|
||||
const char* Param = CollAtUnchecked (&M->Params, I);
|
||||
CollAppend (&New->Params, xstrdup (Param));
|
||||
}
|
||||
New->ArgCount = M->ArgCount;
|
||||
New->ParamCount = M->ParamCount;
|
||||
New->Variadic = M->Variadic;
|
||||
SB_Copy (&New->Replacement, &M->Replacement);
|
||||
|
||||
@ -265,14 +263,14 @@ Macro* FindMacro (const char* Name)
|
||||
|
||||
|
||||
|
||||
int FindMacroArg (Macro* M, const char* Arg)
|
||||
/* Search for a formal macro argument. If found, return the index of the
|
||||
** argument. If the argument was not found, return -1.
|
||||
int FindMacroParam (const Macro* M, const char* Param)
|
||||
/* Search for a macro parameter. If found, return the index of the parameter.
|
||||
** If the parameter was not found, return -1.
|
||||
*/
|
||||
{
|
||||
unsigned I;
|
||||
for (I = 0; I < CollCount (&M->FormalArgs); ++I) {
|
||||
if (strcmp (CollAtUnchecked (&M->FormalArgs, I), Arg) == 0) {
|
||||
for (I = 0; I < CollCount (&M->Params); ++I) {
|
||||
if (strcmp (CollAtUnchecked (&M->Params, I), Param) == 0) {
|
||||
/* Found */
|
||||
return I;
|
||||
}
|
||||
@ -284,25 +282,25 @@ int FindMacroArg (Macro* M, const char* Arg)
|
||||
|
||||
|
||||
|
||||
void AddMacroArg (Macro* M, const char* Arg)
|
||||
/* Add a formal macro argument. */
|
||||
void AddMacroParam (Macro* M, const char* Param)
|
||||
/* Add a macro parameter. */
|
||||
{
|
||||
/* Check if we have a duplicate macro argument, but add it anyway.
|
||||
** Beware: Don't use FindMacroArg here, since the actual argument array
|
||||
/* Check if we have a duplicate macro parameter, but add it anyway.
|
||||
** Beware: Don't use FindMacroParam here, since the actual argument array
|
||||
** may not be initialized.
|
||||
*/
|
||||
unsigned I;
|
||||
for (I = 0; I < CollCount (&M->FormalArgs); ++I) {
|
||||
if (strcmp (CollAtUnchecked (&M->FormalArgs, I), Arg) == 0) {
|
||||
for (I = 0; I < CollCount (&M->Params); ++I) {
|
||||
if (strcmp (CollAtUnchecked (&M->Params, I), Param) == 0) {
|
||||
/* Found */
|
||||
PPError ("Duplicate macro parameter: '%s'", Arg);
|
||||
PPError ("Duplicate macro parameter: '%s'", Param);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add the new argument */
|
||||
CollAppend (&M->FormalArgs, xstrdup (Arg));
|
||||
++M->ArgCount;
|
||||
/* Add the new parameter */
|
||||
CollAppend (&M->Params, xstrdup (Param));
|
||||
++M->ParamCount;
|
||||
}
|
||||
|
||||
|
||||
@ -313,14 +311,14 @@ int MacroCmp (const Macro* M1, const Macro* M2)
|
||||
int I;
|
||||
|
||||
/* Argument count must be identical */
|
||||
if (M1->ArgCount != M2->ArgCount) {
|
||||
if (M1->ParamCount != M2->ParamCount) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Compare the arguments */
|
||||
for (I = 0; I < M1->ArgCount; ++I) {
|
||||
if (strcmp (CollConstAt (&M1->FormalArgs, I),
|
||||
CollConstAt (&M2->FormalArgs, I)) != 0) {
|
||||
/* Compare the parameters */
|
||||
for (I = 0; I < M1->ParamCount; ++I) {
|
||||
if (strcmp (CollConstAt (&M1->Params, I),
|
||||
CollConstAt (&M2->Params, I)) != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -55,10 +55,8 @@
|
||||
typedef struct Macro Macro;
|
||||
struct Macro {
|
||||
Macro* Next; /* Next macro with same hash value */
|
||||
int Expanding; /* Are we currently expanding this macro? */
|
||||
int ArgCount; /* Number of parameters, -1 = no parens */
|
||||
unsigned MaxArgs; /* Size of formal argument list */
|
||||
Collection FormalArgs; /* Formal argument list (char*) */
|
||||
int ParamCount; /* Number of parameters, -1 = no parens */
|
||||
Collection Params; /* Parameter list (char*) */
|
||||
StrBuf Replacement; /* Replacement text */
|
||||
unsigned char Variadic; /* C99 variadic macro */
|
||||
char Name[1]; /* Name, dynamically allocated */
|
||||
@ -120,13 +118,13 @@ INLINE int IsMacro (const char* Name)
|
||||
# define IsMacro(Name) (FindMacro (Name) != 0)
|
||||
#endif
|
||||
|
||||
int FindMacroArg (Macro* M, const char* Arg);
|
||||
/* Search for a formal macro argument. If found, return the index of the
|
||||
** argument. If the argument was not found, return -1.
|
||||
int FindMacroParam (const Macro* M, const char* Param);
|
||||
/* Search for a macro parameter. If found, return the index of the parameter.
|
||||
** If the parameter was not found, return -1.
|
||||
*/
|
||||
|
||||
void AddMacroArg (Macro* M, const char* Arg);
|
||||
/* Add a formal macro argument. */
|
||||
void AddMacroParam (Macro* M, const char* Param);
|
||||
/* Add a macro parameter. */
|
||||
|
||||
int MacroCmp (const Macro* M1, const Macro* M2);
|
||||
/* Compare two macros and return zero if both are identical. */
|
||||
|
1561
src/cc65/preproc.c
1561
src/cc65/preproc.c
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user