-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_inline4_file.py
183 lines (141 loc) · 4.86 KB
/
gen_inline4_file.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
# This script is a codegen module for idltool.py.
#
# gen_inline4_file.py - Generates include/inline4/<libname>.h files
# Copyright (C) 2021,2022 Steven Solie
#
# This program 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 <https://www.gnu.org/licenses/>
class Inline4File:
""" Code generator to create library interface inline4 files in C.
"""
def __init__(self, spec_file):
self.spec_file = spec_file
self.out_file = None
def codegen(self, args, tool_version, out_dir):
""" Code generate the library interface file.
"""
import os
lib_spec = self.spec_file.library_spec()
lib_name = lib_spec.attrib["name"]
path = os.path.join(out_dir, 'include', 'inline4')
try:
os.makedirs(path)
except:
pass
path = os.path.join(path, lib_name + '.h')
self.out_file = open(path, "w+")
guard_label = 'INLINE4_' + lib_name.upper() + '_H'
self.put_header(guard_label, tool_version)
self.putln()
self.put_includes()
self.putln()
for iface_spec in self.spec_file.interfaces_spec():
inline4_status = iface_spec.get('inline4')
if inline4_status == 'no':
# Skip interfaces which should not be inlined.
continue
iface_name = iface_spec.attrib['name']
global_name = iface_spec.attrib['global']
self.putln('/* Inline macros for Interface "' + iface_name + '" */')
for method in iface_spec:
if method.tag != 'method':
# Skip non-method element.
continue
method_name = method.attrib['name']
method_status = method.get('status')
if method_status == 'private':
if args['f']:
# Do not output private method.
continue
if method_status == 'unimplemented':
# Do not output unimplemented method.
continue
excluded_methods = ('Obtain', 'Release', 'Expunge', 'Clone')
if method_name in excluded_methods:
# Skip excluded methods common to all interfaces.
continue
self.put('#define ' + method_name + '(')
self.put_method_macro_params(method)
self.put(') ')
self.put(global_name + '->' + method_name + '(')
self.put_method_macro_args(method)
self.putln(')')
self.putln()
self.put_footer(guard_label)
self.out_file.close()
def put(self, line=''):
self.out_file.write(line)
def putln(self, line=''):
self.out_file.write(line + '\n')
def put_header(self, guard_name, tool_version):
self.putln('#ifndef ' + guard_name)
self.putln('#define ' + guard_name)
self.putln()
self.putln('/*')
self.putln('** This file was machine generated by idltool.py ' + tool_version + '.')
self.putln('** Do not edit.')
copyright = self.spec_file.library_spec().find('copyright')
if copyright != None:
self.putln('**')
self.putln('** ' + copyright.text.strip())
self.putln('**')
self.putln('** It provides compatibility to AmigaOS 3.x style library')
self.putln('** calls by substituting functions.')
self.putln('*/')
def put_includes(self):
self.putln('#include <exec/types.h>')
self.putln('#include <exec/exec.h>')
self.putln('#include <exec/interfaces.h>')
self.putln()
for spec in self.spec_file.includes_spec():
name = spec.text.strip()
self.putln('#include <' + name + '>')
for spec in self.spec_file.inline4_includes_spec():
name = spec.text.strip()
self.putln('#include <' + name + '>')
def put_method_macro_params(self, method_spec):
vararg_spec = method_spec.find('vararg')
# Variadic macros suppress the next to last arg if present
# to work around the trailing comma issue.
if vararg_spec != None:
method_spec = method_spec[:-2]
num_args = 0
for arg_spec in method_spec:
if num_args > 0:
self.put(', ')
param_name = arg_spec.attrib['name']
self.put(param_name)
num_args += 1
if vararg_spec != None:
if num_args > 0:
self.put(', ')
self.put('...')
def put_method_macro_args(self, method_spec):
vararg_spec = method_spec.find('vararg')
# Variadic macros suppress the next to last arg if present
# to work around the trailing comma issue.
if vararg_spec != None:
method_spec = method_spec[:-2]
num_args = 0
for arg_spec in method_spec:
if num_args > 0:
self.put(', ')
arg_name = arg_spec.attrib['name']
self.put('(' + arg_name + ')')
num_args += 1
if vararg_spec != None:
if num_args > 0:
self.put(', ')
self.put('__VA_ARGS__')
def put_footer(self, guard_name):
self.putln('#endif /* ' + guard_name + ' */')