forked from davidmalcolm/gcc-python-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-casts-c.py
120 lines (97 loc) · 3.96 KB
/
generate-casts-c.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
# Copyright 2013 David Malcolm <[email protected]>
# 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/>.
import glob
import sys
sys.path.append('gcc-c-api')
from xmltypes import ApiRegistry, Api
COPYRIGHT_HEADER = '''
Copyright 2013 David Malcolm <[email protected]>
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:
*/
''')
def write_c(registry, c_out):
write_header(c_out)
c_out.write('#include <Python.h>\n')
c_out.write('#include "gcc-python.h"\n')
c_out.write('#include "gcc-python-wrappers.h"\n')
c_out.write('#include "gcc-python-compat.h"\n')
c_out.write('#include "cp/cp-tree.h"\n')
c_out.write('#include "gimple.h"\n')
c_out.write('#include "cp/cp-tree.h" /* for TFF_* for use by PyGccFunctionDecl_get_fullname */\n')
c_out.write('/* op_symbol_code moved to tree-pretty-print.h in gcc 4.9\n')
c_out.write(' but tree-pretty-print.h is only available from 4.7 onwards. */\n')
c_out.write('#if (GCC_VERSION >= 4009)\n')
c_out.write('#include "tree-pretty-print.h"\n')
c_out.write('#endif\n')
c_out.write('#include "gcc-c-api/gcc-tree.h"\n')
c_out.write('#include "gcc-c-api/gcc-type.h"\n')
c_out.write('#include "autogenerated-casts.h"\n\n\n')
tree_type = registry.lookup_type('tree')
for subclass in tree_type.get_subclasses(recursive=True):
c_out.write('%s\n'
'PyGccTree_as_%s(struct PyGccTree * self)\n'
'{\n'
' return gcc_tree_as_%s(self->t);\n'
'}\n\n'
% (subclass.get_c_name(),
subclass.get_c_name(),
subclass.get_c_name()))
write_footer(c_out)
def write_h(registry, h_out):
write_header(h_out)
h_out.write('#include <gcc-c-api/gcc-tree.h>\n')
tree_type = registry.lookup_type('tree')
for subclass in tree_type.get_subclasses(recursive=True):
h_out.write('extern %s\n'
'PyGccTree_as_%s(struct PyGccTree * self);\n\n'
% (subclass.get_c_name(), subclass.get_c_name()))
write_footer(h_out)
def main(c_filename, h_filename, xmldir):
registry = ApiRegistry()
for xmlfile in sorted(glob.glob(xmldir + '*.xml')):
api = Api(registry, xmlfile)
with open(c_filename, 'w') as c_out:
write_c(registry, c_out)
with open(h_filename, 'w') as h_out:
write_h(registry, h_out)
main(c_filename=sys.argv[1],
h_filename=sys.argv[2],
xmldir=sys.argv[3])