ahci: QOM'ify some more
[qemu/ar7.git] / scripts / qapi.py
blobbaf13213a9afc4e83a1e7968d10f56ffb284e4e6
2 # QAPI helper library
4 # Copyright IBM, Corp. 2011
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPLv2.
10 # See the COPYING.LIB file in the top-level directory.
12 from ordereddict import OrderedDict
14 builtin_types = [
15 'str', 'int', 'number', 'bool',
16 'int8', 'int16', 'int32', 'int64',
17 'uint8', 'uint16', 'uint32', 'uint64'
20 def tokenize(data):
21 while len(data):
22 ch = data[0]
23 data = data[1:]
24 if ch in ['{', '}', ':', ',', '[', ']']:
25 yield ch
26 elif ch in ' \n':
27 None
28 elif ch == "'":
29 string = ''
30 esc = False
31 while True:
32 if (data == ''):
33 raise Exception("Mismatched quotes")
34 ch = data[0]
35 data = data[1:]
36 if esc:
37 string += ch
38 esc = False
39 elif ch == "\\":
40 esc = True
41 elif ch == "'":
42 break
43 else:
44 string += ch
45 yield string
47 def parse(tokens):
48 if tokens[0] == '{':
49 ret = OrderedDict()
50 tokens = tokens[1:]
51 while tokens[0] != '}':
52 key = tokens[0]
53 tokens = tokens[1:]
55 tokens = tokens[1:] # :
57 value, tokens = parse(tokens)
59 if tokens[0] == ',':
60 tokens = tokens[1:]
62 ret[key] = value
63 tokens = tokens[1:]
64 return ret, tokens
65 elif tokens[0] == '[':
66 ret = []
67 tokens = tokens[1:]
68 while tokens[0] != ']':
69 value, tokens = parse(tokens)
70 if tokens[0] == ',':
71 tokens = tokens[1:]
72 ret.append(value)
73 tokens = tokens[1:]
74 return ret, tokens
75 else:
76 return tokens[0], tokens[1:]
78 def evaluate(string):
79 return parse(map(lambda x: x, tokenize(string)))[0]
81 def get_expr(fp):
82 expr = ''
84 for line in fp:
85 if line.startswith('#') or line == '\n':
86 continue
88 if line.startswith(' '):
89 expr += line
90 elif expr:
91 yield expr
92 expr = line
93 else:
94 expr += line
96 if expr:
97 yield expr
99 def parse_schema(fp):
100 exprs = []
102 for expr in get_expr(fp):
103 expr_eval = evaluate(expr)
105 if expr_eval.has_key('enum'):
106 add_enum(expr_eval['enum'])
107 elif expr_eval.has_key('union'):
108 add_enum('%sKind' % expr_eval['union'])
109 elif expr_eval.has_key('type'):
110 add_struct(expr_eval)
111 exprs.append(expr_eval)
113 return exprs
115 def parse_args(typeinfo):
116 if isinstance(typeinfo, basestring):
117 struct = find_struct(typeinfo)
118 assert struct != None
119 typeinfo = struct['data']
121 for member in typeinfo:
122 argname = member
123 argentry = typeinfo[member]
124 optional = False
125 structured = False
126 if member.startswith('*'):
127 argname = member[1:]
128 optional = True
129 if isinstance(argentry, OrderedDict):
130 structured = True
131 yield (argname, argentry, optional, structured)
133 def de_camel_case(name):
134 new_name = ''
135 for ch in name:
136 if ch.isupper() and new_name:
137 new_name += '_'
138 if ch == '-':
139 new_name += '_'
140 else:
141 new_name += ch.lower()
142 return new_name
144 def camel_case(name):
145 new_name = ''
146 first = True
147 for ch in name:
148 if ch in ['_', '-']:
149 first = True
150 elif first:
151 new_name += ch.upper()
152 first = False
153 else:
154 new_name += ch.lower()
155 return new_name
157 def c_var(name, protect=True):
158 # ANSI X3J11/88-090, 3.1.1
159 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
160 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
161 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
162 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
163 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
164 # ISO/IEC 9899:1999, 6.4.1
165 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
166 # ISO/IEC 9899:2011, 6.4.1
167 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
168 '_Static_assert', '_Thread_local'])
169 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
170 # excluding _.*
171 gcc_words = set(['asm', 'typeof'])
172 # namespace pollution:
173 polluted_words = set(['unix'])
174 if protect and (name in c89_words | c99_words | c11_words | gcc_words | polluted_words):
175 return "q_" + name
176 return name.replace('-', '_').lstrip("*")
178 def c_fun(name, protect=True):
179 return c_var(name, protect).replace('.', '_')
181 def c_list_type(name):
182 return '%sList' % name
184 def type_name(name):
185 if type(name) == list:
186 return c_list_type(name[0])
187 return name
189 enum_types = []
190 struct_types = []
192 def add_struct(definition):
193 global struct_types
194 struct_types.append(definition)
196 def find_struct(name):
197 global struct_types
198 for struct in struct_types:
199 if struct['type'] == name:
200 return struct
201 return None
203 def add_enum(name):
204 global enum_types
205 enum_types.append(name)
207 def is_enum(name):
208 global enum_types
209 return (name in enum_types)
211 def c_type(name):
212 if name == 'str':
213 return 'char *'
214 elif name == 'int':
215 return 'int64_t'
216 elif (name == 'int8' or name == 'int16' or name == 'int32' or
217 name == 'int64' or name == 'uint8' or name == 'uint16' or
218 name == 'uint32' or name == 'uint64'):
219 return name + '_t'
220 elif name == 'size':
221 return 'uint64_t'
222 elif name == 'bool':
223 return 'bool'
224 elif name == 'number':
225 return 'double'
226 elif type(name) == list:
227 return '%s *' % c_list_type(name[0])
228 elif is_enum(name):
229 return name
230 elif name == None or len(name) == 0:
231 return 'void'
232 elif name == name.upper():
233 return '%sEvent *' % camel_case(name)
234 else:
235 return '%s *' % name
237 def genindent(count):
238 ret = ""
239 for i in range(count):
240 ret += " "
241 return ret
243 indent_level = 0
245 def push_indent(indent_amount=4):
246 global indent_level
247 indent_level += indent_amount
249 def pop_indent(indent_amount=4):
250 global indent_level
251 indent_level -= indent_amount
253 def cgen(code, **kwds):
254 indent = genindent(indent_level)
255 lines = code.split('\n')
256 lines = map(lambda x: indent + x, lines)
257 return '\n'.join(lines) % kwds + '\n'
259 def mcgen(code, **kwds):
260 return cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
262 def basename(filename):
263 return filename.split("/")[-1]
265 def guardname(filename):
266 guard = basename(filename).rsplit(".", 1)[0]
267 for substr in [".", " ", "-"]:
268 guard = guard.replace(substr, "_")
269 return guard.upper() + '_H'
271 def guardstart(name):
272 return mcgen('''
274 #ifndef %(name)s
275 #define %(name)s
277 ''',
278 name=guardname(name))
280 def guardend(name):
281 return mcgen('''
283 #endif /* %(name)s */
285 ''',
286 name=guardname(name))