• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMMON_PREPROCESSOR_H_
16 #define COMMON_PREPROCESSOR_H_
17 
18 // DAWN_PP_GET_HEAD: get the first element of a __VA_ARGS__ without triggering empty
19 // __VA_ARGS__ warnings.
20 #define DAWN_INTERNAL_PP_GET_HEAD(firstParam, ...) firstParam
21 #define DAWN_PP_GET_HEAD(...) DAWN_INTERNAL_PP_GET_HEAD(__VA_ARGS__, dummyArg)
22 
23 // DAWN_PP_CONCATENATE: Concatenate tokens, first expanding the arguments passed in.
24 #define DAWN_PP_CONCATENATE(arg1, arg2) DAWN_PP_CONCATENATE_1(arg1, arg2)
25 #define DAWN_PP_CONCATENATE_1(arg1, arg2) DAWN_PP_CONCATENATE_2(arg1, arg2)
26 #define DAWN_PP_CONCATENATE_2(arg1, arg2) arg1##arg2
27 
28 // DAWN_PP_EXPAND: Needed to help expand __VA_ARGS__ out on MSVC
29 #define DAWN_PP_EXPAND(...) __VA_ARGS__
30 
31 // Implementation of DAWN_PP_FOR_EACH, called by concatenating DAWN_PP_FOR_EACH_ with a number.
32 #define DAWN_PP_FOR_EACH_1(func, x) func(x)
33 #define DAWN_PP_FOR_EACH_2(func, x, ...) \
34     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_1)(func, __VA_ARGS__))
35 #define DAWN_PP_FOR_EACH_3(func, x, ...) \
36     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_2)(func, __VA_ARGS__))
37 #define DAWN_PP_FOR_EACH_4(func, x, ...) \
38     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_3)(func, __VA_ARGS__))
39 #define DAWN_PP_FOR_EACH_5(func, x, ...) \
40     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_4)(func, __VA_ARGS__))
41 #define DAWN_PP_FOR_EACH_6(func, x, ...) \
42     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_5)(func, __VA_ARGS__))
43 #define DAWN_PP_FOR_EACH_7(func, x, ...) \
44     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_6)(func, __VA_ARGS__))
45 #define DAWN_PP_FOR_EACH_8(func, x, ...) \
46     func(x) DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_7)(func, __VA_ARGS__))
47 
48 // Implementation for DAWN_PP_FOR_EACH. Get the number of args in __VA_ARGS__ so we can concat
49 // DAWN_PP_FOR_EACH_ and N.
50 // ex.) DAWN_PP_FOR_EACH_NARG(a, b, c) ->
51 //      DAWN_PP_FOR_EACH_NARG(a, b, c, DAWN_PP_FOR_EACH_RSEQ()) ->
52 //      DAWN_PP_FOR_EACH_NARG_(a, b, c, 8, 7, 6, 5, 4, 3, 2, 1, 0) ->
53 //      DAWN_PP_FOR_EACH_ARG_N(a, b, c, 8, 7, 6, 5, 4, 3, 2, 1, 0) ->
54 //      DAWN_PP_FOR_EACH_ARG_N( ,  ,  ,  ,  ,  ,  , ,  N) ->
55 //      3
56 #define DAWN_PP_FOR_EACH_NARG(...) DAWN_PP_FOR_EACH_NARG_(__VA_ARGS__, DAWN_PP_FOR_EACH_RSEQ())
57 #define DAWN_PP_FOR_EACH_NARG_(...) \
58     DAWN_PP_EXPAND(DAWN_PP_EXPAND(DAWN_PP_FOR_EACH_ARG_N)(__VA_ARGS__))
59 #define DAWN_PP_FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
60 #define DAWN_PP_FOR_EACH_RSEQ() 8, 7, 6, 5, 4, 3, 2, 1, 0
61 
62 // Implementation for DAWN_PP_FOR_EACH.
63 // Creates a call to DAWN_PP_FOR_EACH_X where X is 1, 2, ..., etc.
64 #define DAWN_PP_FOR_EACH_(N, func, ...) DAWN_PP_CONCATENATE(DAWN_PP_FOR_EACH_, N)(func, __VA_ARGS__)
65 
66 // DAWN_PP_FOR_EACH: Apply |func| to each argument in |x| and __VA_ARGS__
67 #define DAWN_PP_FOR_EACH(func, ...) \
68     DAWN_PP_FOR_EACH_(DAWN_PP_FOR_EACH_NARG(__VA_ARGS__), func, __VA_ARGS__)
69 
70 #endif  // COMMON_PREPROCESSOR_H_
71