1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;
const aro = @import("aro");
const CToken = aro.Tokenizer.Token;
const helpers = @import("helpers.zig");
const Translator = @import("Translator.zig");
const Error = Translator.Error;
pub const MacroProcessingError = Error || error{UnexpectedMacroToken};
const Impl = std.meta.DeclEnum(std.zig.c_translation.helpers);
const Template = struct { []const u8, Impl };
/// Templates must be function-like macros
/// first element is macro source, second element is the name of the function
/// in __helpers which implements it
const templates = [_]Template{
.{ "f_SUFFIX(X) (X ## f)", .F_SUFFIX },
.{ "F_SUFFIX(X) (X ## F)", .F_SUFFIX },
.{ "u_SUFFIX(X) (X ## u)", .U_SUFFIX },
.{ "U_SUFFIX(X) (X ## U)", .U_SUFFIX },
.{ "l_SUFFIX(X) (X ## l)", .L_SUFFIX },
.{ "L_SUFFIX(X) (X ## L)", .L_SUFFIX },
.{ "ul_SUFFIX(X) (X ## ul)", .UL_SUFFIX },
.{ "uL_SUFFIX(X) (X ## uL)", .UL_SUFFIX },
.{ "Ul_SUFFIX(X) (X ## Ul)", .UL_SUFFIX },
.{ "UL_SUFFIX(X) (X ## UL)", .UL_SUFFIX },
.{ "ll_SUFFIX(X) (X ## ll)", .LL_SUFFIX },
.{ "LL_SUFFIX(X) (X ## LL)", .LL_SUFFIX },
.{ "ull_SUFFIX(X) (X ## ull)", .ULL_SUFFIX },
.{ "uLL_SUFFIX(X) (X ## uLL)", .ULL_SUFFIX },
.{ "Ull_SUFFIX(X) (X ## Ull)", .ULL_SUFFIX },
.{ "ULL_SUFFIX(X) (X ## ULL)", .ULL_SUFFIX },
.{ "f_SUFFIX(X) X ## f", .F_SUFFIX },
.{ "F_SUFFIX(X) X ## F", .F_SUFFIX },
.{ "u_SUFFIX(X) X ## u", .U_SUFFIX },
.{ "U_SUFFIX(X) X ## U", .U_SUFFIX },
.{ "l_SUFFIX(X) X ## l", .L_SUFFIX },
.{ "L_SUFFIX(X) X ## L", .L_SUFFIX },
.{ "ul_SUFFIX(X) X ## ul", .UL_SUFFIX },
.{ "uL_SUFFIX(X) X ## uL", .UL_SUFFIX },
.{ "Ul_SUFFIX(X) X ## Ul", .UL_SUFFIX },
.{ "UL_SUFFIX(X) X ## UL", .UL_SUFFIX },
.{ "ll_SUFFIX(X) X ## ll", .LL_SUFFIX },
.{ "LL_SUFFIX(X) X ## LL", .LL_SUFFIX },
.{ "ull_SUFFIX(X) X ## ull", .ULL_SUFFIX },
.{ "uLL_SUFFIX(X) X ## uLL", .ULL_SUFFIX },
.{ "Ull_SUFFIX(X) X ## Ull", .ULL_SUFFIX },
.{ "ULL_SUFFIX(X) X ## ULL", .ULL_SUFFIX },
.{ "CAST_OR_CALL(X, Y) (X)(Y)", .CAST_OR_CALL },
.{ "CAST_OR_CALL(X, Y) ((X)(Y))", .CAST_OR_CALL },
.{
\\wl_container_of(ptr, sample, member) \
\\(__typeof__(sample))((char *)(ptr) - \
\\ offsetof(__typeof__(*sample), member))
,
.WL_CONTAINER_OF,
},
.{ "IGNORE_ME(X) ((void)(X))", .DISCARD },
.{ "IGNORE_ME(X) (void)(X)", .DISCARD },
.{ "IGNORE_ME(X) ((const void)(X))", .DISCARD },
.{ "IGNORE_ME(X) (const void)(X)", .DISCARD },
.{ "IGNORE_ME(X) ((volatile void)(X))", .DISCARD },
.{ "IGNORE_ME(X) (volatile void)(X)", .DISCARD },
.{ "IGNORE_ME(X) ((const volatile void)(X))", .DISCARD },
.{ "IGNORE_ME(X) (const volatile void)(X)", .DISCARD },
.{ "IGNORE_ME(X) ((volatile const void)(X))", .DISCARD },
.{ "IGNORE_ME(X) (volatile const void)(X)", .DISCARD },
};
const Pattern = struct {
slicer: MacroSlicer,
impl: Impl,
fn init(pl: *Pattern, allocator: mem.Allocator, template: Template) Error!void {
const source = template[0];
const impl = template[1];
var tok_list: std.ArrayList(CToken) = .empty;
defer tok_list.deinit(allocator);
pl.* = .{
.slicer = try tokenizeMacro(allocator, source, &tok_list),
.impl = impl,
};
}
fn deinit(pl: *Pattern, allocator: mem.Allocator) void {
allocator.free(pl.slicer.tokens);
pl.* = undefined;
}
/// This function assumes that `ms` has already been validated to contain a function-like
/// macro, and that the parsed template macro in `pl` also contains a function-like
/// macro. Please review this logic carefully if changing that assumption. Two
/// function-like macros are considered equivalent if and only if they contain the same
/// list of tokens, modulo parameter names.
fn matches(pat: Pattern, ms: MacroSlicer) bool {
if (ms.params != pat.slicer.params) return false;
if (ms.tokens.len != pat.slicer.tokens.len) return false;
for (ms.tokens, pat.slicer.tokens) |macro_tok, pat_tok| {
if (macro_tok.id != pat_tok.id) return false;
switch (macro_tok.id) {
.macro_param, .macro_param_no_expand => {
// `.end` is the parameter index.
if (macro_tok.end != pat_tok.end) return false;
},
.identifier, .extended_identifier, .string_literal, .char_literal, .pp_num => {
const macro_bytes = ms.slice(macro_tok);
const pattern_bytes = pat.slicer.slice(pat_tok);
if (!mem.eql(u8, pattern_bytes, macro_bytes)) return false;
},
else => {
// other tags correspond to keywords and operators that do not contain a "payload"
// that can vary
},
}
}
return true;
}
};
const PatternList = @This();
patterns: []Pattern,
pub const MacroSlicer = struct {
source: []const u8,
tokens: []const CToken,
params: u32,
fn slice(pl: MacroSlicer, token: CToken) []const u8 {
return pl.source[token.start..token.end];
}
};
pub fn init(allocator: mem.Allocator) Error!PatternList {
const patterns = try allocator.alloc(Pattern, templates.len);
for (patterns, templates) |*pattern, template| {
try pattern.init(allocator, template);
}
return .{ .patterns = patterns };
}
pub fn deinit(pl: *PatternList, allocator: mem.Allocator) void {
for (pl.patterns) |*pattern| pattern.deinit(allocator);
allocator.free(pl.patterns);
pl.* = undefined;
}
pub fn match(pl: PatternList, ms: MacroSlicer) Error!?Impl {
for (pl.patterns) |pattern| if (pattern.matches(ms)) return pattern.impl;
return null;
}
fn tokenizeMacro(allocator: mem.Allocator, source: []const u8, tok_list: *std.ArrayList(CToken)) Error!MacroSlicer {
var param_count: u32 = 0;
var param_buf: [8][]const u8 = undefined;
var tokenizer: aro.Tokenizer = .{
.buf = source,
.source = .unused,
.langopts = .{},
};
{
const name_tok = tokenizer.nextNoWS();
assert(name_tok.id == .identifier);
const l_paren = tokenizer.nextNoWS();
assert(l_paren.id == .l_paren);
}
while (true) {
const param = tokenizer.nextNoWS();
if (param.id == .r_paren) break;
assert(param.id == .identifier);
const slice = source[param.start..param.end];
param_buf[param_count] = slice;
param_count += 1;
const comma = tokenizer.nextNoWS();
if (comma.id == .r_paren) break;
assert(comma.id == .comma);
}
outer: while (true) {
const tok = tokenizer.next();
switch (tok.id) {
.whitespace, .comment => continue,
.identifier => {
const slice = source[tok.start..tok.end];
for (param_buf[0..param_count], 0..) |param, i| {
if (std.mem.eql(u8, param, slice)) {
try tok_list.append(allocator, .{
.id = .macro_param,
.source = .unused,
.end = @intCast(i),
});
continue :outer;
}
}
},
.hash_hash => {
if (tok_list.items[tok_list.items.len - 1].id == .macro_param) {
tok_list.items[tok_list.items.len - 1].id = .macro_param_no_expand;
}
},
.nl, .eof => break,
else => {},
}
try tok_list.append(allocator, tok);
}
return .{
.source = source,
.tokens = try tok_list.toOwnedSlice(allocator),
.params = param_count,
};
}
test "Macro matching" {
const testing = std.testing;
const helper = struct {
fn checkMacro(
allocator: mem.Allocator,
pattern_list: PatternList,
source: []const u8,
comptime expected_match: ?Impl,
) !void {
var tok_list: std.ArrayList(CToken) = .empty;
defer tok_list.deinit(allocator);
const ms = try tokenizeMacro(allocator, source, &tok_list);
defer allocator.free(ms.tokens);
const matched = try pattern_list.match(ms);
if (expected_match) |expected| {
try testing.expectEqual(expected, matched);
} else {
try testing.expectEqual(@as(@TypeOf(matched), null), matched);
}
}
};
const allocator = std.testing.allocator;
var pattern_list = try PatternList.init(allocator);
defer pattern_list.deinit(allocator);
try helper.checkMacro(allocator, pattern_list, "BAR(Z) (Z ## F)", .F_SUFFIX);
try helper.checkMacro(allocator, pattern_list, "BAR(Z) (Z ## U)", .U_SUFFIX);
try helper.checkMacro(allocator, pattern_list, "BAR(Z) (Z ## L)", .L_SUFFIX);
try helper.checkMacro(allocator, pattern_list, "BAR(Z) (Z ## LL)", .LL_SUFFIX);
try helper.checkMacro(allocator, pattern_list, "BAR(Z) (Z ## UL)", .UL_SUFFIX);
try helper.checkMacro(allocator, pattern_list, "BAR(Z) (Z ## ULL)", .ULL_SUFFIX);
try helper.checkMacro(allocator, pattern_list,
\\container_of(a, b, c) \
\\(__typeof__(b))((char *)(a) - \
\\ offsetof(__typeof__(*b), c))
, .WL_CONTAINER_OF);
try helper.checkMacro(allocator, pattern_list, "NO_MATCH(X, Y) (X + Y)", null);
try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) (X)(Y)", .CAST_OR_CALL);
try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) ((X)(Y))", .CAST_OR_CALL);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (void)(X)", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((void)(X))", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const void)(X)", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((const void)(X))", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (volatile void)(X)", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((volatile void)(X))", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const volatile void)(X)", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((const volatile void)(X))", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (volatile const void)(X)", .DISCARD);
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((volatile const void)(X))", .DISCARD);
}
|