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]
87 if line
.startswith('#') or line
== '\n':
90 if line
.startswith(' '):
93 expr_eval
= evaluate(expr
)
94 if expr_eval
.has_key('enum'):
95 add_enum(expr_eval
['enum'])
96 elif expr_eval
.has_key('union'):
97 add_enum('%sKind' % expr_eval
['union'])
98 exprs
.append(expr_eval
)
104 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 exprs
.append(expr_eval
)
113 def parse_args(typeinfo
):
114 for member
in typeinfo
:
116 argentry
= typeinfo
[member
]
119 if member
.startswith('*'):
122 if isinstance(argentry
, OrderedDict
):
124 yield (argname
, argentry
, optional
, structured
)
126 def de_camel_case(name
):
129 if ch
.isupper() and new_name
:
134 new_name
+= ch
.lower()
137 def camel_case(name
):
144 new_name
+= ch
.upper()
147 new_name
+= ch
.lower()
150 def c_var(name
, protect
=True):
151 # ANSI X3J11/88-090, 3.1.1
152 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
153 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
154 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
155 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
156 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
157 # ISO/IEC 9899:1999, 6.4.1
158 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
159 # ISO/IEC 9899:2011, 6.4.1
160 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
161 '_Static_assert', '_Thread_local'])
162 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
164 gcc_words
= set(['asm', 'typeof'])
165 # namespace pollution:
166 polluted_words
= set(['unix'])
167 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words | polluted_words
):
169 return name
.replace('-', '_').lstrip("*")
171 def c_fun(name
, protect
=True):
172 return c_var(name
, protect
).replace('.', '_')
174 def c_list_type(name
):
175 return '%sList' % name
178 if type(name
) == list:
179 return c_list_type(name
[0])
186 enum_types
.append(name
)
190 return (name
in enum_types
)
197 elif (name
== 'int8' or name
== 'int16' or name
== 'int32' or
198 name
== 'int64' or name
== 'uint8' or name
== 'uint16' or
199 name
== 'uint32' or name
== 'uint64'):
205 elif name
== 'number':
207 elif type(name
) == list:
208 return '%s *' % c_list_type(name
[0])
211 elif name
== None or len(name
) == 0:
213 elif name
== name
.upper():
214 return '%sEvent *' % camel_case(name
)
218 def genindent(count
):
220 for i
in range(count
):
226 def push_indent(indent_amount
=4):
228 indent_level
+= indent_amount
230 def pop_indent(indent_amount
=4):
232 indent_level
-= indent_amount
234 def cgen(code
, **kwds
):
235 indent
= genindent(indent_level
)
236 lines
= code
.split('\n')
237 lines
= map(lambda x
: indent
+ x
, lines
)
238 return '\n'.join(lines
) % kwds
+ '\n'
240 def mcgen(code
, **kwds
):
241 return cgen('\n'.join(code
.split('\n')[1:-1]), **kwds
)
243 def basename(filename
):
244 return filename
.split("/")[-1]
246 def guardname(filename
):
247 guard
= basename(filename
).rsplit(".", 1)[0]
248 for substr
in [".", " ", "-"]:
249 guard
= guard
.replace(substr
, "_")
250 return guard
.upper() + '_H'
252 def guardstart(name
):
259 name
=guardname(name
))
264 #endif /* %(name)s */
267 name
=guardname(name
))