pycparser 2.22__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycparser/__init__.py +93 -0
- pycparser/_ast_gen.py +336 -0
- pycparser/_build_tables.py +40 -0
- pycparser/_c_ast.cfg +195 -0
- pycparser/ast_transforms.py +164 -0
- pycparser/c_ast.py +1125 -0
- pycparser/c_generator.py +502 -0
- pycparser/c_lexer.py +555 -0
- pycparser/c_parser.py +1950 -0
- pycparser/lextab.py +10 -0
- pycparser/ply/__init__.py +5 -0
- pycparser/ply/cpp.py +905 -0
- pycparser/ply/ctokens.py +133 -0
- pycparser/ply/lex.py +1099 -0
- pycparser/ply/yacc.py +3494 -0
- pycparser/ply/ygen.py +74 -0
- pycparser/plyparser.py +133 -0
- pycparser/yacctab.py +369 -0
- pycparser-2.22.dist-info/LICENSE +27 -0
- pycparser-2.22.dist-info/METADATA +28 -0
- pycparser-2.22.dist-info/RECORD +23 -0
- pycparser-2.22.dist-info/WHEEL +5 -0
- pycparser-2.22.dist-info/top_level.txt +1 -0
pycparser/c_generator.py
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
#------------------------------------------------------------------------------
|
|
2
|
+
# pycparser: c_generator.py
|
|
3
|
+
#
|
|
4
|
+
# C code generator from pycparser AST nodes.
|
|
5
|
+
#
|
|
6
|
+
# Eli Bendersky [https://eli.thegreenplace.net/]
|
|
7
|
+
# License: BSD
|
|
8
|
+
#------------------------------------------------------------------------------
|
|
9
|
+
from . import c_ast
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CGenerator(object):
|
|
13
|
+
""" Uses the same visitor pattern as c_ast.NodeVisitor, but modified to
|
|
14
|
+
return a value from each visit method, using string accumulation in
|
|
15
|
+
generic_visit.
|
|
16
|
+
"""
|
|
17
|
+
def __init__(self, reduce_parentheses=False):
|
|
18
|
+
""" Constructs C-code generator
|
|
19
|
+
|
|
20
|
+
reduce_parentheses:
|
|
21
|
+
if True, eliminates needless parentheses on binary operators
|
|
22
|
+
"""
|
|
23
|
+
# Statements start with indentation of self.indent_level spaces, using
|
|
24
|
+
# the _make_indent method.
|
|
25
|
+
self.indent_level = 0
|
|
26
|
+
self.reduce_parentheses = reduce_parentheses
|
|
27
|
+
|
|
28
|
+
def _make_indent(self):
|
|
29
|
+
return ' ' * self.indent_level
|
|
30
|
+
|
|
31
|
+
def visit(self, node):
|
|
32
|
+
method = 'visit_' + node.__class__.__name__
|
|
33
|
+
return getattr(self, method, self.generic_visit)(node)
|
|
34
|
+
|
|
35
|
+
def generic_visit(self, node):
|
|
36
|
+
if node is None:
|
|
37
|
+
return ''
|
|
38
|
+
else:
|
|
39
|
+
return ''.join(self.visit(c) for c_name, c in node.children())
|
|
40
|
+
|
|
41
|
+
def visit_Constant(self, n):
|
|
42
|
+
return n.value
|
|
43
|
+
|
|
44
|
+
def visit_ID(self, n):
|
|
45
|
+
return n.name
|
|
46
|
+
|
|
47
|
+
def visit_Pragma(self, n):
|
|
48
|
+
ret = '#pragma'
|
|
49
|
+
if n.string:
|
|
50
|
+
ret += ' ' + n.string
|
|
51
|
+
return ret
|
|
52
|
+
|
|
53
|
+
def visit_ArrayRef(self, n):
|
|
54
|
+
arrref = self._parenthesize_unless_simple(n.name)
|
|
55
|
+
return arrref + '[' + self.visit(n.subscript) + ']'
|
|
56
|
+
|
|
57
|
+
def visit_StructRef(self, n):
|
|
58
|
+
sref = self._parenthesize_unless_simple(n.name)
|
|
59
|
+
return sref + n.type + self.visit(n.field)
|
|
60
|
+
|
|
61
|
+
def visit_FuncCall(self, n):
|
|
62
|
+
fref = self._parenthesize_unless_simple(n.name)
|
|
63
|
+
return fref + '(' + self.visit(n.args) + ')'
|
|
64
|
+
|
|
65
|
+
def visit_UnaryOp(self, n):
|
|
66
|
+
if n.op == 'sizeof':
|
|
67
|
+
# Always parenthesize the argument of sizeof since it can be
|
|
68
|
+
# a name.
|
|
69
|
+
return 'sizeof(%s)' % self.visit(n.expr)
|
|
70
|
+
else:
|
|
71
|
+
operand = self._parenthesize_unless_simple(n.expr)
|
|
72
|
+
if n.op == 'p++':
|
|
73
|
+
return '%s++' % operand
|
|
74
|
+
elif n.op == 'p--':
|
|
75
|
+
return '%s--' % operand
|
|
76
|
+
else:
|
|
77
|
+
return '%s%s' % (n.op, operand)
|
|
78
|
+
|
|
79
|
+
# Precedence map of binary operators:
|
|
80
|
+
precedence_map = {
|
|
81
|
+
# Should be in sync with c_parser.CParser.precedence
|
|
82
|
+
# Higher numbers are stronger binding
|
|
83
|
+
'||': 0, # weakest binding
|
|
84
|
+
'&&': 1,
|
|
85
|
+
'|': 2,
|
|
86
|
+
'^': 3,
|
|
87
|
+
'&': 4,
|
|
88
|
+
'==': 5, '!=': 5,
|
|
89
|
+
'>': 6, '>=': 6, '<': 6, '<=': 6,
|
|
90
|
+
'>>': 7, '<<': 7,
|
|
91
|
+
'+': 8, '-': 8,
|
|
92
|
+
'*': 9, '/': 9, '%': 9 # strongest binding
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
def visit_BinaryOp(self, n):
|
|
96
|
+
# Note: all binary operators are left-to-right associative
|
|
97
|
+
#
|
|
98
|
+
# If `n.left.op` has a stronger or equally binding precedence in
|
|
99
|
+
# comparison to `n.op`, no parenthesis are needed for the left:
|
|
100
|
+
# e.g., `(a*b) + c` is equivalent to `a*b + c`, as well as
|
|
101
|
+
# `(a+b) - c` is equivalent to `a+b - c` (same precedence).
|
|
102
|
+
# If the left operator is weaker binding than the current, then
|
|
103
|
+
# parentheses are necessary:
|
|
104
|
+
# e.g., `(a+b) * c` is NOT equivalent to `a+b * c`.
|
|
105
|
+
lval_str = self._parenthesize_if(
|
|
106
|
+
n.left,
|
|
107
|
+
lambda d: not (self._is_simple_node(d) or
|
|
108
|
+
self.reduce_parentheses and isinstance(d, c_ast.BinaryOp) and
|
|
109
|
+
self.precedence_map[d.op] >= self.precedence_map[n.op]))
|
|
110
|
+
# If `n.right.op` has a stronger -but not equal- binding precedence,
|
|
111
|
+
# parenthesis can be omitted on the right:
|
|
112
|
+
# e.g., `a + (b*c)` is equivalent to `a + b*c`.
|
|
113
|
+
# If the right operator is weaker or equally binding, then parentheses
|
|
114
|
+
# are necessary:
|
|
115
|
+
# e.g., `a * (b+c)` is NOT equivalent to `a * b+c` and
|
|
116
|
+
# `a - (b+c)` is NOT equivalent to `a - b+c` (same precedence).
|
|
117
|
+
rval_str = self._parenthesize_if(
|
|
118
|
+
n.right,
|
|
119
|
+
lambda d: not (self._is_simple_node(d) or
|
|
120
|
+
self.reduce_parentheses and isinstance(d, c_ast.BinaryOp) and
|
|
121
|
+
self.precedence_map[d.op] > self.precedence_map[n.op]))
|
|
122
|
+
return '%s %s %s' % (lval_str, n.op, rval_str)
|
|
123
|
+
|
|
124
|
+
def visit_Assignment(self, n):
|
|
125
|
+
rval_str = self._parenthesize_if(
|
|
126
|
+
n.rvalue,
|
|
127
|
+
lambda n: isinstance(n, c_ast.Assignment))
|
|
128
|
+
return '%s %s %s' % (self.visit(n.lvalue), n.op, rval_str)
|
|
129
|
+
|
|
130
|
+
def visit_IdentifierType(self, n):
|
|
131
|
+
return ' '.join(n.names)
|
|
132
|
+
|
|
133
|
+
def _visit_expr(self, n):
|
|
134
|
+
if isinstance(n, c_ast.InitList):
|
|
135
|
+
return '{' + self.visit(n) + '}'
|
|
136
|
+
elif isinstance(n, c_ast.ExprList):
|
|
137
|
+
return '(' + self.visit(n) + ')'
|
|
138
|
+
else:
|
|
139
|
+
return self.visit(n)
|
|
140
|
+
|
|
141
|
+
def visit_Decl(self, n, no_type=False):
|
|
142
|
+
# no_type is used when a Decl is part of a DeclList, where the type is
|
|
143
|
+
# explicitly only for the first declaration in a list.
|
|
144
|
+
#
|
|
145
|
+
s = n.name if no_type else self._generate_decl(n)
|
|
146
|
+
if n.bitsize: s += ' : ' + self.visit(n.bitsize)
|
|
147
|
+
if n.init:
|
|
148
|
+
s += ' = ' + self._visit_expr(n.init)
|
|
149
|
+
return s
|
|
150
|
+
|
|
151
|
+
def visit_DeclList(self, n):
|
|
152
|
+
s = self.visit(n.decls[0])
|
|
153
|
+
if len(n.decls) > 1:
|
|
154
|
+
s += ', ' + ', '.join(self.visit_Decl(decl, no_type=True)
|
|
155
|
+
for decl in n.decls[1:])
|
|
156
|
+
return s
|
|
157
|
+
|
|
158
|
+
def visit_Typedef(self, n):
|
|
159
|
+
s = ''
|
|
160
|
+
if n.storage: s += ' '.join(n.storage) + ' '
|
|
161
|
+
s += self._generate_type(n.type)
|
|
162
|
+
return s
|
|
163
|
+
|
|
164
|
+
def visit_Cast(self, n):
|
|
165
|
+
s = '(' + self._generate_type(n.to_type, emit_declname=False) + ')'
|
|
166
|
+
return s + ' ' + self._parenthesize_unless_simple(n.expr)
|
|
167
|
+
|
|
168
|
+
def visit_ExprList(self, n):
|
|
169
|
+
visited_subexprs = []
|
|
170
|
+
for expr in n.exprs:
|
|
171
|
+
visited_subexprs.append(self._visit_expr(expr))
|
|
172
|
+
return ', '.join(visited_subexprs)
|
|
173
|
+
|
|
174
|
+
def visit_InitList(self, n):
|
|
175
|
+
visited_subexprs = []
|
|
176
|
+
for expr in n.exprs:
|
|
177
|
+
visited_subexprs.append(self._visit_expr(expr))
|
|
178
|
+
return ', '.join(visited_subexprs)
|
|
179
|
+
|
|
180
|
+
def visit_Enum(self, n):
|
|
181
|
+
return self._generate_struct_union_enum(n, name='enum')
|
|
182
|
+
|
|
183
|
+
def visit_Alignas(self, n):
|
|
184
|
+
return '_Alignas({})'.format(self.visit(n.alignment))
|
|
185
|
+
|
|
186
|
+
def visit_Enumerator(self, n):
|
|
187
|
+
if not n.value:
|
|
188
|
+
return '{indent}{name},\n'.format(
|
|
189
|
+
indent=self._make_indent(),
|
|
190
|
+
name=n.name,
|
|
191
|
+
)
|
|
192
|
+
else:
|
|
193
|
+
return '{indent}{name} = {value},\n'.format(
|
|
194
|
+
indent=self._make_indent(),
|
|
195
|
+
name=n.name,
|
|
196
|
+
value=self.visit(n.value),
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
def visit_FuncDef(self, n):
|
|
200
|
+
decl = self.visit(n.decl)
|
|
201
|
+
self.indent_level = 0
|
|
202
|
+
body = self.visit(n.body)
|
|
203
|
+
if n.param_decls:
|
|
204
|
+
knrdecls = ';\n'.join(self.visit(p) for p in n.param_decls)
|
|
205
|
+
return decl + '\n' + knrdecls + ';\n' + body + '\n'
|
|
206
|
+
else:
|
|
207
|
+
return decl + '\n' + body + '\n'
|
|
208
|
+
|
|
209
|
+
def visit_FileAST(self, n):
|
|
210
|
+
s = ''
|
|
211
|
+
for ext in n.ext:
|
|
212
|
+
if isinstance(ext, c_ast.FuncDef):
|
|
213
|
+
s += self.visit(ext)
|
|
214
|
+
elif isinstance(ext, c_ast.Pragma):
|
|
215
|
+
s += self.visit(ext) + '\n'
|
|
216
|
+
else:
|
|
217
|
+
s += self.visit(ext) + ';\n'
|
|
218
|
+
return s
|
|
219
|
+
|
|
220
|
+
def visit_Compound(self, n):
|
|
221
|
+
s = self._make_indent() + '{\n'
|
|
222
|
+
self.indent_level += 2
|
|
223
|
+
if n.block_items:
|
|
224
|
+
s += ''.join(self._generate_stmt(stmt) for stmt in n.block_items)
|
|
225
|
+
self.indent_level -= 2
|
|
226
|
+
s += self._make_indent() + '}\n'
|
|
227
|
+
return s
|
|
228
|
+
|
|
229
|
+
def visit_CompoundLiteral(self, n):
|
|
230
|
+
return '(' + self.visit(n.type) + '){' + self.visit(n.init) + '}'
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def visit_EmptyStatement(self, n):
|
|
234
|
+
return ';'
|
|
235
|
+
|
|
236
|
+
def visit_ParamList(self, n):
|
|
237
|
+
return ', '.join(self.visit(param) for param in n.params)
|
|
238
|
+
|
|
239
|
+
def visit_Return(self, n):
|
|
240
|
+
s = 'return'
|
|
241
|
+
if n.expr: s += ' ' + self.visit(n.expr)
|
|
242
|
+
return s + ';'
|
|
243
|
+
|
|
244
|
+
def visit_Break(self, n):
|
|
245
|
+
return 'break;'
|
|
246
|
+
|
|
247
|
+
def visit_Continue(self, n):
|
|
248
|
+
return 'continue;'
|
|
249
|
+
|
|
250
|
+
def visit_TernaryOp(self, n):
|
|
251
|
+
s = '(' + self._visit_expr(n.cond) + ') ? '
|
|
252
|
+
s += '(' + self._visit_expr(n.iftrue) + ') : '
|
|
253
|
+
s += '(' + self._visit_expr(n.iffalse) + ')'
|
|
254
|
+
return s
|
|
255
|
+
|
|
256
|
+
def visit_If(self, n):
|
|
257
|
+
s = 'if ('
|
|
258
|
+
if n.cond: s += self.visit(n.cond)
|
|
259
|
+
s += ')\n'
|
|
260
|
+
s += self._generate_stmt(n.iftrue, add_indent=True)
|
|
261
|
+
if n.iffalse:
|
|
262
|
+
s += self._make_indent() + 'else\n'
|
|
263
|
+
s += self._generate_stmt(n.iffalse, add_indent=True)
|
|
264
|
+
return s
|
|
265
|
+
|
|
266
|
+
def visit_For(self, n):
|
|
267
|
+
s = 'for ('
|
|
268
|
+
if n.init: s += self.visit(n.init)
|
|
269
|
+
s += ';'
|
|
270
|
+
if n.cond: s += ' ' + self.visit(n.cond)
|
|
271
|
+
s += ';'
|
|
272
|
+
if n.next: s += ' ' + self.visit(n.next)
|
|
273
|
+
s += ')\n'
|
|
274
|
+
s += self._generate_stmt(n.stmt, add_indent=True)
|
|
275
|
+
return s
|
|
276
|
+
|
|
277
|
+
def visit_While(self, n):
|
|
278
|
+
s = 'while ('
|
|
279
|
+
if n.cond: s += self.visit(n.cond)
|
|
280
|
+
s += ')\n'
|
|
281
|
+
s += self._generate_stmt(n.stmt, add_indent=True)
|
|
282
|
+
return s
|
|
283
|
+
|
|
284
|
+
def visit_DoWhile(self, n):
|
|
285
|
+
s = 'do\n'
|
|
286
|
+
s += self._generate_stmt(n.stmt, add_indent=True)
|
|
287
|
+
s += self._make_indent() + 'while ('
|
|
288
|
+
if n.cond: s += self.visit(n.cond)
|
|
289
|
+
s += ');'
|
|
290
|
+
return s
|
|
291
|
+
|
|
292
|
+
def visit_StaticAssert(self, n):
|
|
293
|
+
s = '_Static_assert('
|
|
294
|
+
s += self.visit(n.cond)
|
|
295
|
+
if n.message:
|
|
296
|
+
s += ','
|
|
297
|
+
s += self.visit(n.message)
|
|
298
|
+
s += ')'
|
|
299
|
+
return s
|
|
300
|
+
|
|
301
|
+
def visit_Switch(self, n):
|
|
302
|
+
s = 'switch (' + self.visit(n.cond) + ')\n'
|
|
303
|
+
s += self._generate_stmt(n.stmt, add_indent=True)
|
|
304
|
+
return s
|
|
305
|
+
|
|
306
|
+
def visit_Case(self, n):
|
|
307
|
+
s = 'case ' + self.visit(n.expr) + ':\n'
|
|
308
|
+
for stmt in n.stmts:
|
|
309
|
+
s += self._generate_stmt(stmt, add_indent=True)
|
|
310
|
+
return s
|
|
311
|
+
|
|
312
|
+
def visit_Default(self, n):
|
|
313
|
+
s = 'default:\n'
|
|
314
|
+
for stmt in n.stmts:
|
|
315
|
+
s += self._generate_stmt(stmt, add_indent=True)
|
|
316
|
+
return s
|
|
317
|
+
|
|
318
|
+
def visit_Label(self, n):
|
|
319
|
+
return n.name + ':\n' + self._generate_stmt(n.stmt)
|
|
320
|
+
|
|
321
|
+
def visit_Goto(self, n):
|
|
322
|
+
return 'goto ' + n.name + ';'
|
|
323
|
+
|
|
324
|
+
def visit_EllipsisParam(self, n):
|
|
325
|
+
return '...'
|
|
326
|
+
|
|
327
|
+
def visit_Struct(self, n):
|
|
328
|
+
return self._generate_struct_union_enum(n, 'struct')
|
|
329
|
+
|
|
330
|
+
def visit_Typename(self, n):
|
|
331
|
+
return self._generate_type(n.type)
|
|
332
|
+
|
|
333
|
+
def visit_Union(self, n):
|
|
334
|
+
return self._generate_struct_union_enum(n, 'union')
|
|
335
|
+
|
|
336
|
+
def visit_NamedInitializer(self, n):
|
|
337
|
+
s = ''
|
|
338
|
+
for name in n.name:
|
|
339
|
+
if isinstance(name, c_ast.ID):
|
|
340
|
+
s += '.' + name.name
|
|
341
|
+
else:
|
|
342
|
+
s += '[' + self.visit(name) + ']'
|
|
343
|
+
s += ' = ' + self._visit_expr(n.expr)
|
|
344
|
+
return s
|
|
345
|
+
|
|
346
|
+
def visit_FuncDecl(self, n):
|
|
347
|
+
return self._generate_type(n)
|
|
348
|
+
|
|
349
|
+
def visit_ArrayDecl(self, n):
|
|
350
|
+
return self._generate_type(n, emit_declname=False)
|
|
351
|
+
|
|
352
|
+
def visit_TypeDecl(self, n):
|
|
353
|
+
return self._generate_type(n, emit_declname=False)
|
|
354
|
+
|
|
355
|
+
def visit_PtrDecl(self, n):
|
|
356
|
+
return self._generate_type(n, emit_declname=False)
|
|
357
|
+
|
|
358
|
+
def _generate_struct_union_enum(self, n, name):
|
|
359
|
+
""" Generates code for structs, unions, and enums. name should be
|
|
360
|
+
'struct', 'union', or 'enum'.
|
|
361
|
+
"""
|
|
362
|
+
if name in ('struct', 'union'):
|
|
363
|
+
members = n.decls
|
|
364
|
+
body_function = self._generate_struct_union_body
|
|
365
|
+
else:
|
|
366
|
+
assert name == 'enum'
|
|
367
|
+
members = None if n.values is None else n.values.enumerators
|
|
368
|
+
body_function = self._generate_enum_body
|
|
369
|
+
s = name + ' ' + (n.name or '')
|
|
370
|
+
if members is not None:
|
|
371
|
+
# None means no members
|
|
372
|
+
# Empty sequence means an empty list of members
|
|
373
|
+
s += '\n'
|
|
374
|
+
s += self._make_indent()
|
|
375
|
+
self.indent_level += 2
|
|
376
|
+
s += '{\n'
|
|
377
|
+
s += body_function(members)
|
|
378
|
+
self.indent_level -= 2
|
|
379
|
+
s += self._make_indent() + '}'
|
|
380
|
+
return s
|
|
381
|
+
|
|
382
|
+
def _generate_struct_union_body(self, members):
|
|
383
|
+
return ''.join(self._generate_stmt(decl) for decl in members)
|
|
384
|
+
|
|
385
|
+
def _generate_enum_body(self, members):
|
|
386
|
+
# `[:-2] + '\n'` removes the final `,` from the enumerator list
|
|
387
|
+
return ''.join(self.visit(value) for value in members)[:-2] + '\n'
|
|
388
|
+
|
|
389
|
+
def _generate_stmt(self, n, add_indent=False):
|
|
390
|
+
""" Generation from a statement node. This method exists as a wrapper
|
|
391
|
+
for individual visit_* methods to handle different treatment of
|
|
392
|
+
some statements in this context.
|
|
393
|
+
"""
|
|
394
|
+
typ = type(n)
|
|
395
|
+
if add_indent: self.indent_level += 2
|
|
396
|
+
indent = self._make_indent()
|
|
397
|
+
if add_indent: self.indent_level -= 2
|
|
398
|
+
|
|
399
|
+
if typ in (
|
|
400
|
+
c_ast.Decl, c_ast.Assignment, c_ast.Cast, c_ast.UnaryOp,
|
|
401
|
+
c_ast.BinaryOp, c_ast.TernaryOp, c_ast.FuncCall, c_ast.ArrayRef,
|
|
402
|
+
c_ast.StructRef, c_ast.Constant, c_ast.ID, c_ast.Typedef,
|
|
403
|
+
c_ast.ExprList):
|
|
404
|
+
# These can also appear in an expression context so no semicolon
|
|
405
|
+
# is added to them automatically
|
|
406
|
+
#
|
|
407
|
+
return indent + self.visit(n) + ';\n'
|
|
408
|
+
elif typ in (c_ast.Compound,):
|
|
409
|
+
# No extra indentation required before the opening brace of a
|
|
410
|
+
# compound - because it consists of multiple lines it has to
|
|
411
|
+
# compute its own indentation.
|
|
412
|
+
#
|
|
413
|
+
return self.visit(n)
|
|
414
|
+
elif typ in (c_ast.If,):
|
|
415
|
+
return indent + self.visit(n)
|
|
416
|
+
else:
|
|
417
|
+
return indent + self.visit(n) + '\n'
|
|
418
|
+
|
|
419
|
+
def _generate_decl(self, n):
|
|
420
|
+
""" Generation from a Decl node.
|
|
421
|
+
"""
|
|
422
|
+
s = ''
|
|
423
|
+
if n.funcspec: s = ' '.join(n.funcspec) + ' '
|
|
424
|
+
if n.storage: s += ' '.join(n.storage) + ' '
|
|
425
|
+
if n.align: s += self.visit(n.align[0]) + ' '
|
|
426
|
+
s += self._generate_type(n.type)
|
|
427
|
+
return s
|
|
428
|
+
|
|
429
|
+
def _generate_type(self, n, modifiers=[], emit_declname = True):
|
|
430
|
+
""" Recursive generation from a type node. n is the type node.
|
|
431
|
+
modifiers collects the PtrDecl, ArrayDecl and FuncDecl modifiers
|
|
432
|
+
encountered on the way down to a TypeDecl, to allow proper
|
|
433
|
+
generation from it.
|
|
434
|
+
"""
|
|
435
|
+
typ = type(n)
|
|
436
|
+
#~ print(n, modifiers)
|
|
437
|
+
|
|
438
|
+
if typ == c_ast.TypeDecl:
|
|
439
|
+
s = ''
|
|
440
|
+
if n.quals: s += ' '.join(n.quals) + ' '
|
|
441
|
+
s += self.visit(n.type)
|
|
442
|
+
|
|
443
|
+
nstr = n.declname if n.declname and emit_declname else ''
|
|
444
|
+
# Resolve modifiers.
|
|
445
|
+
# Wrap in parens to distinguish pointer to array and pointer to
|
|
446
|
+
# function syntax.
|
|
447
|
+
#
|
|
448
|
+
for i, modifier in enumerate(modifiers):
|
|
449
|
+
if isinstance(modifier, c_ast.ArrayDecl):
|
|
450
|
+
if (i != 0 and
|
|
451
|
+
isinstance(modifiers[i - 1], c_ast.PtrDecl)):
|
|
452
|
+
nstr = '(' + nstr + ')'
|
|
453
|
+
nstr += '['
|
|
454
|
+
if modifier.dim_quals:
|
|
455
|
+
nstr += ' '.join(modifier.dim_quals) + ' '
|
|
456
|
+
nstr += self.visit(modifier.dim) + ']'
|
|
457
|
+
elif isinstance(modifier, c_ast.FuncDecl):
|
|
458
|
+
if (i != 0 and
|
|
459
|
+
isinstance(modifiers[i - 1], c_ast.PtrDecl)):
|
|
460
|
+
nstr = '(' + nstr + ')'
|
|
461
|
+
nstr += '(' + self.visit(modifier.args) + ')'
|
|
462
|
+
elif isinstance(modifier, c_ast.PtrDecl):
|
|
463
|
+
if modifier.quals:
|
|
464
|
+
nstr = '* %s%s' % (' '.join(modifier.quals),
|
|
465
|
+
' ' + nstr if nstr else '')
|
|
466
|
+
else:
|
|
467
|
+
nstr = '*' + nstr
|
|
468
|
+
if nstr: s += ' ' + nstr
|
|
469
|
+
return s
|
|
470
|
+
elif typ == c_ast.Decl:
|
|
471
|
+
return self._generate_decl(n.type)
|
|
472
|
+
elif typ == c_ast.Typename:
|
|
473
|
+
return self._generate_type(n.type, emit_declname = emit_declname)
|
|
474
|
+
elif typ == c_ast.IdentifierType:
|
|
475
|
+
return ' '.join(n.names) + ' '
|
|
476
|
+
elif typ in (c_ast.ArrayDecl, c_ast.PtrDecl, c_ast.FuncDecl):
|
|
477
|
+
return self._generate_type(n.type, modifiers + [n],
|
|
478
|
+
emit_declname = emit_declname)
|
|
479
|
+
else:
|
|
480
|
+
return self.visit(n)
|
|
481
|
+
|
|
482
|
+
def _parenthesize_if(self, n, condition):
|
|
483
|
+
""" Visits 'n' and returns its string representation, parenthesized
|
|
484
|
+
if the condition function applied to the node returns True.
|
|
485
|
+
"""
|
|
486
|
+
s = self._visit_expr(n)
|
|
487
|
+
if condition(n):
|
|
488
|
+
return '(' + s + ')'
|
|
489
|
+
else:
|
|
490
|
+
return s
|
|
491
|
+
|
|
492
|
+
def _parenthesize_unless_simple(self, n):
|
|
493
|
+
""" Common use case for _parenthesize_if
|
|
494
|
+
"""
|
|
495
|
+
return self._parenthesize_if(n, lambda d: not self._is_simple_node(d))
|
|
496
|
+
|
|
497
|
+
def _is_simple_node(self, n):
|
|
498
|
+
""" Returns True for nodes that are "simple" - i.e. nodes that always
|
|
499
|
+
have higher precedence than operators.
|
|
500
|
+
"""
|
|
501
|
+
return isinstance(n, (c_ast.Constant, c_ast.ID, c_ast.ArrayRef,
|
|
502
|
+
c_ast.StructRef, c_ast.FuncCall))
|