Added C99 variadic macros
git-svn-id: svn://svn.cc65.org/cc65/trunk@3547 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
parent
a45649752f
commit
1aeeffdc52
3 changed files with 88 additions and 38 deletions
|
@ -6,7 +6,7 @@
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
/* (C) 2000-2004 Ullrich von Bassewitz */
|
/* (C) 2000-2005, Ullrich von Bassewitz */
|
||||||
/* Römerstraße 52 */
|
/* Römerstraße 52 */
|
||||||
/* D-70794 Filderstadt */
|
/* D-70794 Filderstadt */
|
||||||
/* EMail: uz@cc65.org */
|
/* EMail: uz@cc65.org */
|
||||||
|
@ -82,6 +82,7 @@ Macro* NewMacro (const char* Name)
|
||||||
M->MaxArgs = 0;
|
M->MaxArgs = 0;
|
||||||
InitCollection (&M->FormalArgs);
|
InitCollection (&M->FormalArgs);
|
||||||
InitStrBuf (&M->Replacement);
|
InitStrBuf (&M->Replacement);
|
||||||
|
M->Variadic = 0;
|
||||||
memcpy (M->Name, Name, Len+1);
|
memcpy (M->Name, Name, Len+1);
|
||||||
|
|
||||||
/* Return the new macro */
|
/* Return the new macro */
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
/* */
|
/* */
|
||||||
/* (C) 2000-2004 Ullrich von Bassewitz */
|
/* (C) 2000-2005, Ullrich von Bassewitz */
|
||||||
/* Römerstraße 52 */
|
/* Römerstraße 52 */
|
||||||
/* D-70794 Filderstadt */
|
/* D-70794 Filderstadt */
|
||||||
/* EMail: uz@cc65.org */
|
/* EMail: uz@cc65.org */
|
||||||
|
@ -60,6 +60,7 @@ struct Macro {
|
||||||
unsigned MaxArgs; /* Size of formal argument list */
|
unsigned MaxArgs; /* Size of formal argument list */
|
||||||
Collection FormalArgs; /* Formal argument list (char*) */
|
Collection FormalArgs; /* Formal argument list (char*) */
|
||||||
StrBuf Replacement; /* Replacement text */
|
StrBuf Replacement; /* Replacement text */
|
||||||
|
unsigned char Variadic; /* C99 variadic macro */
|
||||||
char Name[1]; /* Name, dynamically allocated */
|
char Name[1]; /* Name, dynamically allocated */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -205,6 +205,15 @@ static StrBuf* ME_GetActual (MacroExp* E, unsigned Index)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int ME_ArgIsVariadic (const MacroExp* E)
|
||||||
|
/* Return true if the next actual argument we will add is a variadic one */
|
||||||
|
{
|
||||||
|
return (E->M->Variadic &&
|
||||||
|
E->M->ArgCount == (int) CollCount (&E->ActualArgs) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
/* Code */
|
/* Code */
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
@ -362,21 +371,37 @@ static void ReadMacroArgs (MacroExp* E)
|
||||||
unsigned Parens; /* Number of open parenthesis */
|
unsigned Parens; /* Number of open parenthesis */
|
||||||
StrBuf Arg = STATIC_STRBUF_INITIALIZER;
|
StrBuf Arg = STATIC_STRBUF_INITIALIZER;
|
||||||
|
|
||||||
/* Read the actual macro arguments and store pointers to these arguments
|
/* Read the actual macro arguments */
|
||||||
* into the array of actual arguments in the macro definition.
|
|
||||||
*/
|
|
||||||
Parens = 0;
|
Parens = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (CurC == '(') {
|
if (CurC == '(') {
|
||||||
|
|
||||||
/* Nested parenthesis */
|
/* Nested parenthesis */
|
||||||
SB_AppendChar (&Arg, CurC);
|
SB_AppendChar (&Arg, CurC);
|
||||||
NextChar ();
|
NextChar ();
|
||||||
++Parens;
|
++Parens;
|
||||||
} else if (IsQuote (CurC)) {
|
|
||||||
CopyQuotedString (&Arg);
|
|
||||||
} else if (CurC == ',' || CurC == ')') {
|
|
||||||
if (Parens == 0) {
|
|
||||||
|
|
||||||
|
} else if (IsQuote (CurC)) {
|
||||||
|
|
||||||
|
/* Quoted string - just copy */
|
||||||
|
CopyQuotedString (&Arg);
|
||||||
|
|
||||||
|
} else if (CurC == ',' || CurC == ')') {
|
||||||
|
|
||||||
|
if (Parens) {
|
||||||
|
/* Comma or right paren inside nested parenthesis */
|
||||||
|
if (CurC == ')') {
|
||||||
|
--Parens;
|
||||||
|
}
|
||||||
|
SB_AppendChar (&Arg, CurC);
|
||||||
|
NextChar ();
|
||||||
|
} else if (CurC == ',' && ME_ArgIsVariadic (E)) {
|
||||||
|
/* It's a comma, but we're inside a variadic macro argument, so
|
||||||
|
* just copy it and proceed.
|
||||||
|
*/
|
||||||
|
SB_AppendChar (&Arg, CurC);
|
||||||
|
NextChar ();
|
||||||
|
} else {
|
||||||
/* End of actual argument. Remove whitespace from the end. */
|
/* End of actual argument. Remove whitespace from the end. */
|
||||||
while (IsSpace (SB_LookAtLast (&Arg))) {
|
while (IsSpace (SB_LookAtLast (&Arg))) {
|
||||||
SB_Drop (&Arg, 1);
|
SB_Drop (&Arg, 1);
|
||||||
|
@ -398,13 +423,6 @@ static void ReadMacroArgs (MacroExp* E)
|
||||||
/* Start the next param */
|
/* Start the next param */
|
||||||
NextChar ();
|
NextChar ();
|
||||||
SB_Clear (&Arg);
|
SB_Clear (&Arg);
|
||||||
} else {
|
|
||||||
/* Comma or right paren inside nested parenthesis */
|
|
||||||
if (CurC == ')') {
|
|
||||||
--Parens;
|
|
||||||
}
|
|
||||||
SB_AppendChar (&Arg, CurC);
|
|
||||||
NextChar ();
|
|
||||||
}
|
}
|
||||||
} else if (IsSpace (CurC)) {
|
} else if (IsSpace (CurC)) {
|
||||||
/* Squeeze runs of blanks within an arg */
|
/* Squeeze runs of blanks within an arg */
|
||||||
|
@ -549,7 +567,6 @@ static void MacroArgSubst (MacroExp* E)
|
||||||
NextChar ();
|
NextChar ();
|
||||||
SkipWhitespace ();
|
SkipWhitespace ();
|
||||||
if (!IsSym (Ident) || (ArgIdx = FindMacroArg (E->M, Ident)) < 0) {
|
if (!IsSym (Ident) || (ArgIdx = FindMacroArg (E->M, Ident)) < 0) {
|
||||||
printf ("<%.*s>\n", SB_GetLen (Line), SB_GetConstBuf (Line));
|
|
||||||
PPError ("`#' is not followed by a macro parameter");
|
PPError ("`#' is not followed by a macro parameter");
|
||||||
} else {
|
} else {
|
||||||
/* Make a valid string from Replacement */
|
/* Make a valid string from Replacement */
|
||||||
|
@ -675,6 +692,7 @@ static void DefineMacro (void)
|
||||||
ident Ident;
|
ident Ident;
|
||||||
Macro* M;
|
Macro* M;
|
||||||
Macro* Existing;
|
Macro* Existing;
|
||||||
|
int C89;
|
||||||
|
|
||||||
/* Read the macro name */
|
/* Read the macro name */
|
||||||
SkipWhitespace ();
|
SkipWhitespace ();
|
||||||
|
@ -682,6 +700,9 @@ static void DefineMacro (void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Remember if we're in C89 mode */
|
||||||
|
C89 = (IS_Get (&Standard) == STD_C89);
|
||||||
|
|
||||||
/* Get an existing macro definition with this name */
|
/* Get an existing macro definition with this name */
|
||||||
Existing = FindMacro (Ident);
|
Existing = FindMacro (Ident);
|
||||||
|
|
||||||
|
@ -703,12 +724,39 @@ static void DefineMacro (void)
|
||||||
if (CurC == ')') {
|
if (CurC == ')') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The next token must be either an identifier, or - if not in
|
||||||
|
* C89 mode - the ellipsis.
|
||||||
|
*/
|
||||||
|
if (!C89 && CurC == '.') {
|
||||||
|
/* Ellipsis */
|
||||||
|
NextChar ();
|
||||||
|
if (CurC != '.' || NextC != '.') {
|
||||||
|
PPError ("`...' expected");
|
||||||
|
ClearLine ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NextChar ();
|
||||||
|
NextChar ();
|
||||||
|
strcpy (Ident, "__VA_ARGS__");
|
||||||
|
M->Variadic = 1;
|
||||||
|
} else {
|
||||||
|
/* Must be macro argument name */
|
||||||
if (MacName (Ident) == 0) {
|
if (MacName (Ident) == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* __VA_ARGS__ is only allowed in C89 mode */
|
||||||
|
if (!C89 && strcmp (Ident, "__VA_ARGS__") == 0) {
|
||||||
|
PPWarning ("`__VA_ARGS__' can only appear in the expansion "
|
||||||
|
"of a C99 variadic macro");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add the macro argument */
|
||||||
AddMacroArg (M, Ident);
|
AddMacroArg (M, Ident);
|
||||||
SkipWhitespace ();
|
SkipWhitespace ();
|
||||||
if (CurC != ',') {
|
if (M->Variadic || CurC != ',') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
NextChar ();
|
NextChar ();
|
||||||
|
|
Loading…
Add table
Reference in a new issue