#  | The Macro Expansion Process |

The macro expansion process described here was initially developed by Paul Mensonides and is implemented in `Wave`. It is much more understandable as the description of the desired macro expansion algorithm provided in the C++ Standard [1].

Macro replacement proceeds left-to-right.

If, during scanning (or rescanning) an identifier is found, it is looked up in the symbol table. If the identifier is not found in the symbol table, it is not a macro and scanning continues.

If the identifier is found, the value of a flag associated with the identifier is used to determine if the identifier is available for expansion. If it is not, the specific token (i.e. the specific instance of the identifier) is marked as disabled and is not expanded. If the identifier is available for expansion, the value of a different flag associated with the identifier in the symbol table is used to determine if the identifier is an object-like or function-like macro. If it is an object-like macro, it is expanded. If it is a function-like macro, it is only expanded if the next token is an left parenthesis.
An identifier is available for expansion if it is not marked as disabled and if the the value of the flag associated with the identifier is not set, which is used to determine if the identifier is available for expansion.

(If a macro is an object-like macro, skip past the next two paragraphs.)

If a macro to be expanded is a function-like macro, it must have the exact number of actual arguments as the number of formal parameters required by the definition of the macro. Each argument is recursively scanned and expanded. Each parameter name found in the replacement list is replaced by the expanded actual argument after leading and trailing whitespace and all placeholder tokens are removed unless the parameter name immediately follows the stringizing operator (`'#'`) or is adjacent to the token-pasting operator (`'##'`).

If the parameter name immediately follows the stringizing operator (`'#'`), a stringized version of the unexpanded actual argument is inserted. If the parameter name is adjacent to the token-pasting operator (`'##'`), the unexpanded actual argument is inserted after all placeholder tokens are removed.

All concatenation takes place in the replacement list. (If a single concatenation yields multiple tokens, the behavior is undefined. Moreover, `Wave` in normal C++98 and C99 modes issues an error, if more then one token is produced as the result of the concatenation. In C++0x mode `Wave` treats token-pasting of unrelated tokens as well defined and inserts the reparsed string representation of the concatenated tokens into the replacement list.).

The flag in the symbol table entry associated with the name of the macro being expanded is set to indicate the that the macro is not available for expansion.

The replacement list is rescanned for further macro expansion. All leading and trailing whitespace tokens in the replacement list are removed (the placeholder tokens are left intact).

After rescanning completes, the flag in the symbol table entry associated with the name of macro being expanded is cleared to indicate that the macro is again available for expansion, and the sequence of tokens that constitutes the rescanned replacement list is returned to the point of invocation of the macro.

If this sequence of tokens is empty, it is replaced by a placeholder token. If a placeholder is found during scanning (or rescanning) it is ignored. (Also, if the only thing separating a parameter from the stringizing operator or token-pasting operator is placeholder, it is also ignored in that context.)

This sequence of tokens is inserted at the original point that the macro was invoked, and scanning continues starting with the last token of the newly inserted sequence of tokens. I.e. scanning looks back a single token (possibly a placeholder token) and continues.

Copyright © 2003-2005 Hartmut Kaiser
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Last updated: Sunday, May 15, 2005 12:23