prmacros.c revision 199767f8919635c4928607450d9e0abb932109ce
/******************************************************************************
*
* Module Name: prmacros - Preprocessor #define macro support
*
*****************************************************************************/
/*
* Copyright (C) 2000 - 2015, Intel Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
* 3. Neither the names of the above-listed copyright holders nor the names
* of any contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*/
#define _COMPONENT ASL_PREPROCESSOR
ACPI_MODULE_NAME ("prmacros")
/*******************************************************************************
*
* FUNCTION: PrDumpPredefinedNames
*
* PARAMETERS: None
*
* RETURN: None
*
* DESCRIPTION: Dump the list of #defines. Used as the preprocessor starts, to
* display the names that were defined on the command line.
* Debug information only.
*
******************************************************************************/
void
void)
{
while (DefineInfo)
{
"Predefined #define: %s->%s\n",
}
}
/*******************************************************************************
*
* FUNCTION: PrAddDefine
*
* PARAMETERS: Identifier - Name to be replaced
* Replacement - Replacement for Identifier
* Persist - Keep define across multiple compiles?
*
* RETURN: A new define_info struct. NULL on error.
*
* DESCRIPTION: Add a new #define to the global list
*
******************************************************************************/
char *Identifier,
char *Replacement,
{
char *IdentifierString;
char *ReplacementString;
if (!Replacement)
{
Replacement = "";
}
/* Check for already-defined first */
if (DefineInfo)
{
"#define: name already exists: %s\n",
/*
* Name already exists. This is only an error if the target name
* is different.
*/
{
return (NULL);
}
return (DefineInfo);
}
/* Copy input strings */
/* Init and link new define info struct */
if (Gbl_DefineList)
{
}
return (DefineInfo);
}
/*******************************************************************************
*
* FUNCTION: PrRemoveDefine
*
* PARAMETERS: DefineName - Name of define to be removed
*
* RETURN: None
*
* DESCRIPTION: Implements #undef. Remove a #define if found in the global
* list. No error if the target of the #undef does not exist,
* as per the C #undef definition.
*
******************************************************************************/
void
char *DefineName)
{
/* Match name and delete the node */
while (DefineInfo)
{
{
/* Remove from linked list */
if (DefineInfo->Previous)
{
}
else
{
}
if (DefineInfo->Next)
{
}
free (DefineInfo);
return;
}
}
/*
* Name was not found. By definition of #undef, this is not
* an error, however.
*/
"#undef: could not find %s\n",
}
/*******************************************************************************
*
* FUNCTION: PrMatchDefine
*
* PARAMETERS: MatchString - Name associated with the #define
*
* RETURN: Matched string if found. NULL otherwise.
*
* DESCRIPTION: Find a name in global #define list
*
******************************************************************************/
char *MatchString)
{
while (DefineInfo)
{
{
return (DefineInfo);
}
}
return (NULL);
}
/*******************************************************************************
*
* FUNCTION: PrAddMacro
*
* PARAMETERS: Name - Start of the macro definition
* Next - "Next" buffer from GetNextToken
*
* RETURN: None
*
* DESCRIPTION: Add a new macro to the list of #defines. Handles argument
* processing.
*
******************************************************************************/
void
char *Name,
char **Next)
{
char *Body;
char *BodyInSource;
UINT32 i;
char BufferChar;
/* Find the end of the arguments list */
while (1)
{
if (BufferChar == '(')
{
Depth++;
}
else if (BufferChar == ')')
{
Depth--;
}
else if (BufferChar == 0)
{
return;
}
if (Depth == 0)
{
/* Found arg list end */
break;
}
TokenOffset++;
}
/* At this point, we know that we have a reasonable argument list */
/* Get the macro argument names */
for (i = 0; i < PR_MAX_MACRO_ARGS; i++)
{
if (!Token)
{
/* This is the case for a NULL macro body */
BodyInSource = "";
goto AddMacroToList;
}
/* Don't go beyond the argument list */
if (TokenOffset > EndOfArgList)
{
break;
}
"Macro arg: %s \n",
ArgCount++;
if (ArgCount >= PR_MAX_MACRO_ARGS)
{
goto ErrorExit;
}
}
/* Get the macro body. Token now points to start of body */
/* Match each method arg in the macro body for later use */
while (Token)
{
/* Search the macro arg list for matching arg */
{
/*
* Save argument offset within macro body. This is the mechanism
* used to expand the macro upon invocation.
*
* Handles multiple instances of the same argument
*/
{
"Macro Arg #%u: %s UseCount %u Offset %u \n",
{
goto ErrorExit;
}
break;
}
}
}
/* Check if name is already defined first */
if (DefineInfo)
{
"#define: macro name already exists: %s\n",
/* Error only if not exactly the same macro */
{
}
goto ErrorExit;
}
"Macro body: %s \n",
/* Add macro to the #define list */
if (DefineInfo)
{
}
return;
return;
}
/*******************************************************************************
*
* FUNCTION: PrDoMacroInvocation
*
* PARAMETERS: TokenBuffer - Current line buffer
* MacroStart - Start of the macro invocation within
* the token buffer
* DefineInfo - Info for this macro
* Next - "Next" buffer from GetNextToken
*
* RETURN: None
*
* DESCRIPTION: Expand a macro invocation
*
******************************************************************************/
void
char *TokenBuffer,
char *MacroStart,
char **Next)
{
UINT32 i;
/* Take a copy of the macro body for expansion */
/* Replace each argument within the prototype body */
{
/* This macro has no arguments */
if (!Token)
{
goto BadInvocation;
}
return;
}
{
/* Get the next argument from macro invocation */
if (!Token)
{
goto BadInvocation;
}
/* Replace all instances of this argument */
{
/* Offset zero indicates "arg not used" */
/* TBD: Not really needed now, with UseCount available */
{
break;
}
"ExpandArg: %s \n",
}
Args++;
}
/* TBD: need to make sure macro was not invoked with too many arguments */
if (!Token)
{
return;
}
/* Replace the entire macro invocation with the expanded macro */
return;
"Bad macro invocation: %s \n",
return;
}