https://github.com/shader-slang/slang
Revision 89051251016be7d3798c0b9586c6db7b4ed5f21d authored by Theresa Foley on 16 June 2021, 21:35:25 UTC, committed by GitHub on 16 June 2021, 21:35:25 UTC
This change adds support for variadic macros in the C-style
preprocessor, e.g.:

    #define DEBUG(MSG, ...) print(__FILE__, __LINE__, MSG, __VA_ARGS__)

Similar to the gcc preprocessor, this feature supports both named
variadic macro parameters and unnamed ones (which then default to
`__VA_ARGS__`.

The implementation work is mostly straightforward, although there are a
few subtle design choices worth mentioning:

* A variadic macro is represented by it having a variadic *parameter*
  that is part of the ordinary parameter list.

* Argument parsing does *not* detect whether the macro being invoked is
  variadic and collect/combine arguments to form a single argument value
  for the variadic parameter. This is motivated by the need for some
  extensions to differentiate a variadic parameter receiving a single
  empty argument vs. zero arguments.

* Because any reference to the variadic parameter needs to expand to the
  comma-separated arguments that match it, the logic for turning a macro
  parameter reference into a list of tokens has been factored out into a
  subroutine that handles the details.

* The choice in the earlier refactor to have a macro invocation collect
  all the argument tokens (including the intervening commas) into a
  single token list seems to pay off here, because it means that the
  tokens in the expansion of a variadic parameter reference were already
  stored contiguously.

* The special-case logic for handling an empty argument list had to be
  tweaked again to ensure that an empty argument list is treated as
  having zero arguments for the variadic parameter. Note that
  historically C did not define the behavior of this case, and always
  required at least one argument for any variadic macro parameter.

* The logic for checking whether the number of arguments to a macro
  invocation is valid needed to handle variadic and non-variadic macros
  as distinct cases. There really isn't much overlap in how the checks
  need to work, even if we tried to change the underling representation.

The main missing feature here is any way to discard a comma in a macro
body that appears before a variadic parameter reference, e.g.:

    #define DEBUG(...) print("debug:", __VA_ARGS__)

In this case, an empty invocation list `DEBUG()` will expand to
`print("debug:",)` - a call with a trailing comma in the argument list.

If users end up needing a way to discard commas in cases like this, we
have many options we can consider. This change does not implement any of
them to keep the initial work as minimal as possible.
1 parent 45f737d
History
Tip revision: 89051251016be7d3798c0b9586c6db7b4ed5f21d authored by Theresa Foley on 16 June 2021, 21:35:25 UTC
Initial support for variadic macros (#1887)
Tip revision: 8905125
File Mode Size
.github
build
docs
examples
external
extras
prelude
source
tests
tools
.editorconfig -rw-r--r-- 937 bytes
.gitattributes -rw-r--r-- 95 bytes
.gitignore -rw-r--r-- 1.1 KB
.gitmodules -rw-r--r-- 951 bytes
CODE_OF_CONDUCT.md -rw-r--r-- 3.1 KB
LICENSE -rw-r--r-- 1.1 KB
README.md -rw-r--r-- 6.1 KB
github_build.sh -rw-r--r-- 495 bytes
github_test.sh -rw-r--r-- 546 bytes
premake.bat -rw-r--r-- 120 bytes
premake5.lua -rw-r--r-- 49.1 KB
slang-com-helper.h -rw-r--r-- 4.9 KB
slang-com-ptr.h -rw-r--r-- 4.9 KB
slang-gfx.h -rw-r--r-- 45.5 KB
slang-tag-version.h -rw-r--r-- 36 bytes
slang.h -rw-r--r-- 174.8 KB
slang.sln -rw-r--r-- 21.2 KB
test.bat -rw-r--r-- 1.4 KB

README.md

back to top