forked from davidmalcolm/gcc-python-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-to-h.py
306 lines (269 loc) · 10.8 KB
/
xml-to-h.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import glob
import sys
from xmltypes import ApiRegistry, Api
COPYRIGHT_HEADER = '''
Copyright 2013 David Malcolm <dmalcolm@redhat.com>
Copyright 2013 Red Hat, Inc.
This is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
'''
def write_header(out):
out.write('/* This file is autogenerated: do not edit */\n')
out.write('/*%s*/\n' % COPYRIGHT_HEADER)
out.write('\n')
def write_footer(out):
out.write('''
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/
''')
class SourceWriter:
def __init__(self, out):
self.out = out
self._indent = 0
def indent(self):
self._indent += 1
def outdent(self):
self._indent -= 1
def writeln(self, line=None):
if line:
self.out.write('%s%s\n' % (' ' * self._indent, line))
else:
self.out.write('\n')
def write_doc_comment(self, doc):
self.write_comment(doc.as_text())
def write_comment(self, doc):
self.writeln('/*')
self.indent()
for line in doc.splitlines():
self.writeln(line)
self.outdent()
self.writeln('*/')
def write_begin_extern_c(self):
self.writeln('#ifdef __cplusplus')
self.writeln('extern "C" {')
self.writeln('#endif')
def write_end_extern_c(self):
self.writeln('#ifdef __cplusplus')
self.writeln('}')
self.writeln('#endif')
def write_api(api, out):
writer = SourceWriter(out)
write_header(out)
if api.get_xml_name() == 'rtl':
out.write('''
/* FIXME: rationalize these headers */
#include "gcc-common.h"
''')
else:
out.write('#include "gcc-common.h"\n')
writer.writeln()
writer.write_begin_extern_c()
doc = api.get_doc()
if doc:
writer.write_doc_comment(doc)
writer.writeln()
for type_ in api.iter_types():
writer.writeln('/* %s */\n' % type_.get_c_name())
doc = type_.get_doc()
if doc:
writer.write_doc_comment(doc)
# mark_in_use:
writer.writeln('GCC_PUBLIC_API(void)')
writer.writeln('%s_mark_in_use(%s %s);'
% (type_.get_c_prefix(),
type_.get_c_name(),
type_.get_varname()))
writer.writeln()
# add getters for attributes:
for attr in type_.iter_attrs():
doc = attr.get_doc()
if doc:
writer.write_doc_comment(doc)
if attr.get_c_name().startswith('is_'):
# "gcc_foo_is_some_boolean", rather than
# "gcc_foo_get_is_some_boolean":
writer.writeln('GCC_PUBLIC_API(%s)' % attr.get_c_type())
writer.writeln('%s_%s(%s %s);'
% (type_.get_c_prefix(),
attr.get_c_name(),
type_.get_c_name(),
type_.get_varname()))
else:
writer.writeln('GCC_PUBLIC_API(%s)' % attr.get_c_type())
writer.writeln('%s_get_%s(%s %s);'
% (type_.get_c_prefix(),
attr.get_c_name(),
type_.get_c_name(),
type_.get_varname()))
writer.writeln()
# add iterators
for iter_ in type_.iter_iters():
itertype = iter_.get_type()
writer.write_comment('Iterator; terminates if the callback returns truth\n'
'(for linear search)')
writer.writeln('GCC_PUBLIC_API(bool)')
writer.writeln('%s_for_each_%s(%s %s,'
% (type_.get_c_prefix(),
iter_.get_c_name(),
type_.get_c_name(),
type_.get_varname()))
writer.writeln(' bool (*cb)(%s %s, void *user_data),'
% (itertype.get_c_name(),
itertype.get_varname()))
writer.writeln(' void *user_data);')
writer.writeln()
# add functions:
for fun in type_.iter_functions():
doc = fun.get_doc()
if doc:
writer.write_doc_comment(doc)
writer.writeln('GCC_PUBLIC_API(%s)' % fun.get_c_return_type())
paramstrs = ['%s %s' % (type_.get_c_name(),
type_.get_varname())]
for param in fun.iter_params():
paramstrs.append('%s %s' % (param.get_c_type(),
param.get_xml_name()))
writer.writeln('%s_%s(%s);'
% (type_.get_c_prefix(),
fun.get_c_name(),
', '.join(paramstrs)))
writer.writeln()
# add upcasts
for base in type_.get_bases():
writer.writeln('GCC_PUBLIC_API(%s)'
% base.get_c_name())
writer.writeln('%s_as_%s(%s %s);'
% (type_.get_c_prefix(),
base.get_c_name(),
type_.get_c_name(),
type_.get_varname()))
writer.writeln()
# add downcasts
for subclass in type_.get_subclasses(recursive=True):
writer.writeln('GCC_PUBLIC_API(%s)'
% subclass.get_c_name())
writer.writeln('%s_as_%s(%s %s);'
% (type_.get_c_prefix(),
subclass.get_c_name(),
type_.get_c_name(),
type_.get_varname()))
writer.writeln()
# add getters for attributes:
for attr in api.iter_attrs():
doc = attr.get_doc()
if doc:
writer.write_doc_comment(doc, out)
if attr.is_readable():
writer.writeln('GCC_PUBLIC_API(%s)' % attr.get_c_type())
writer.writeln('gcc_get_%s(void);' % attr.get_c_name())
if attr.is_writable():
writer.writeln('GCC_PUBLIC_API(void)')
writer.writeln('gcc_set_%s(%s %s);'
% (attr.get_c_name(),
attr.get_c_type(),
attr.get_varname()))
writer.writeln()
# add iterators
for iter_ in api.iter_iters():
itertype = iter_.get_type()
writer.write_comment(' Iterator; terminates if the callback returns truth\n'
' (for linear search).')
writer.writeln('GCC_PUBLIC_API(bool)')
writer.writeln('gcc_for_each_%s(bool (*cb)(%s %s, void *user_data),'
% (iter_.get_c_name(),
itertype.get_c_name(),
itertype.get_varname()))
writer.writeln(' void *user_data);')
writer.writeln()
# add functions
for fun in api.iter_functions():
writer.writeln('GCC_PUBLIC_API(%s)' % fun.get_c_return_type())
paramstrs = []
for param in fun.iter_params():
paramstrs.append('%s %s' % (param.get_c_type(),
param.get_xml_name()))
writer.writeln('gcc_%s(%s);'
% (fun.get_c_name(),
', '.join(paramstrs)))
writer.write_end_extern_c()
write_footer(out)
def write_public_types(registry, out):
writer = SourceWriter(out)
write_header(out)
out.write('#ifndef INCLUDED__GCC_PUBLIC_TYPES_H\n')
out.write('#define INCLUDED__GCC_PUBLIC_TYPES_H\n')
out.write('\n')
out.write('#include "gcc-semiprivate-types.h"\n')
out.write('\n')
writer.write_begin_extern_c()
for api in registry.apis:
out.write('/* Opaque types: %s */\n' % api.get_doc().as_text())
for type_ in api.iter_types():
out.write('typedef struct %s %s;\n'
% (type_.get_c_name(), type_.get_c_name()))
out.write('\n')
writer.write_end_extern_c()
out.write('#endif /* INCLUDED__GCC_PUBLIC_TYPES_H */\n')
write_footer(out)
def write_semiprivate_types(registry, out):
writer = SourceWriter(out)
write_header(out)
out.write('#ifndef INCLUDED__GCC_SEMIPRIVATE_TYPES_H\n')
out.write('#define INCLUDED__GCC_SEMIPRIVATE_TYPES_H\n')
out.write('\n')
out.write('#include "input.h" /* for location_t */\n')
out.write('#include "options.h" /* for enum opt_code */\n')
out.write('\n')
writer.write_begin_extern_c()
out.write('/*\n')
out.write(' These "interface types" should be treated like pointers, only that\n')
out.write(' users are required to collaborate with the garbage-collector.\n')
out.write('\n')
out.write(' The internal details are exposed here so that the debugger is able to\n')
out.write(' identify the real types. Plugin developers should *not* rely on the\n')
out.write(' internal implementation details.\n')
out.write('\n')
out.write(' By being structs, the compiler will be able to complain if plugin code\n')
out.write(' directly pokes at a pointer.\n')
out.write('*/\n')
for api in registry.apis:
out.write('/* Semiprivate types: %s */\n' % api.get_doc().as_text())
for type_ in api.iter_types():
out.write('struct %s {\n' % type_.get_c_name())
out.write(' %s inner;\n' % type_.get_inner_type())
out.write('};\n')
out.write('\n')
out.write('GCC_PRIVATE_API(struct %s)\n' % type_.get_c_name())
out.write('gcc_private_make_%s(%s inner);\n'
% (type_.get_xml_name(),
type_.get_inner_type()))
out.write('\n')
writer.write_end_extern_c()
out.write('#endif /* INCLUDED__GCC_SEMIPRIVATE_TYPES_H */\n')
write_footer(out)
registry = ApiRegistry()
xmldir=sys.argv[1]
for xmlfile in sorted(glob.glob(xmldir + '*.xml')):
api = Api(registry, xmlfile)
for api in registry.apis:
with open(api.get_header_filename(), 'w') as f:
# write(api, sys.stdout)
write_api(api, f)
with open('gcc-public-types.h', 'w') as f:
write_public_types(registry, f)
with open('gcc-semiprivate-types.h', 'w') as f:
write_semiprivate_types(registry, f)