4 # Copyright IBM, Corp. 2011
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
15 'str', 'int', 'number', 'bool',
16 'int8', 'int16', 'int32', 'int64',
17 'uint8', 'uint16', 'uint32', 'uint64'
24 if ch
in ['{', '}', ':', ',', '[', ']']:
33 raise Exception("Mismatched quotes")
51 while tokens
[0] != '}':
55 tokens
= tokens
[1:] # :
57 value
, tokens
= parse(tokens
)
65 elif tokens
[0] == '[':
68 while tokens
[0] != ']':
69 value
, tokens
= parse(tokens
)
76 return tokens
[0], tokens
[1:]
79 return parse(map(lambda x
: x
, tokenize(string
)))[0]
85 if line
.startswith('#') or line
== '\n':
88 if line
.startswith(' '):
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
)
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
:
123 argentry
= typeinfo
[member
]
126 if member
.startswith('*'):
129 if isinstance(argentry
, OrderedDict
):
131 yield (argname
, argentry
, optional
, structured
)
133 def de_camel_case(name
):
136 if ch
.isupper() and new_name
:
141 new_name
+= ch
.lower()
144 def camel_case(name
):
151 new_name
+= ch
.upper()
154 new_name
+= ch
.lower()
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
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
):
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
185 if type(name
) == list:
186 return c_list_type(name
[0])
192 def add_struct(definition
):
194 struct_types
.append(definition
)
196 def find_struct(name
):
198 for struct
in struct_types
:
199 if struct
['type'] == name
:
205 enum_types
.append(name
)
209 return (name
in enum_types
)
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'):
224 elif name
== 'number':
226 elif type(name
) == list:
227 return '%s *' % c_list_type(name
[0])
230 elif name
== None or len(name
) == 0:
232 elif name
== name
.upper():
233 return '%sEvent *' % camel_case(name
)
237 def genindent(count
):
239 for i
in range(count
):
245 def push_indent(indent_amount
=4):
247 indent_level
+= indent_amount
249 def pop_indent(indent_amount
=4):
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
):
278 name
=guardname(name
))
283 #endif /* %(name)s */
286 name
=guardname(name
))