-
Notifications
You must be signed in to change notification settings - Fork 0
/
gccxmlparser.py
executable file
·479 lines (391 loc) · 15.3 KB
/
gccxmlparser.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env python
debug = 0
import pprint
from copy import deepcopy
try:
from xml.etree.ElementTree import ElementTree
except ImportError:
from cElementTree import ElementTree
from declaration import *
#------------------------------------------------------------------------------
class InvalidXmlError(Exception): pass
class ParserError(Exception): pass
class InvalidContextError(ParserError): pass
class GccXmlParser(object):
def parse(self, filename):
self.elements = self.parse_xml_file(filename)
self.declarations = []
self.names = {}
if debug > 1:
pprint.pprint(self.elements)
for id in self.elements:
element, declaration = self.elements[id]
if declaration is None:
try:
self.parseElement(id, element)
except InvalidContextError:
pass # ignore nodes with invalid context (gccxml bug)
# parse()
def parse_xml_file(self, filename):
tree = ElementTree(file=filename)
root = tree.getroot()
if root.tag != 'GCC_XML':
raise RuntimeError, 'Not a vailid GCC_XML file'
elements = {}
type2typedef = {}
for element in root:
id = element.get('id')
if id:
elements[id] = element, None
if element.tag == 'Typedef':
type2typedef[element.get('type')] = id
for element in root:
if element.tag != 'PointerType':
continue
type_t = element.get('type')
if type2typedef.has_key(type_t):
type_element, _ = elements[type_t]
if type_element.tag == 'FunctionType':
element.set('type', type2typedef[type_t])
return elements
# parse_xml_file()
def addDeclaration(self, declaration):
if declaration.fullName() in self.names:
declaration.is_unique = False
for d in self.declarations:
if d.fullName() == declaration.fullName():
d.is_unique = False
self.names[declaration.fullName()] = 0
self.declarations.append(declaration)
# addDeclaration()
def getArguments(self, element):
arguments = []
for child in element:
if child.tag == 'Argument':
type_t = self.getType(child.get('type'))
type_t.default = child.get('default')
arguments.append(type_t)
return arguments
# getArguments()
def getDeclaration(self, id):
if id not in self.elements:
if id == '_0':
raise InvalidContextError, 'Invalid context in the xml file.'
else:
raise ParserError, 'Failed to find %s in elements.' % id
element, declaration = self.elements[id]
if declaration is None:
self.parseElement(id, element)
element, declaration = self.elements[id]
if declaration is None:
raise ParserError, 'Failed to parse element: %s' % element.tag
return declaration
# getDeclaration()
def getExceptions(self, exception_list):
if exception_list is None:
return None
exceptions = []
for t in exception_list.split():
exceptions.append(self.getType(t))
return exceptions
# getExceptions()
def getHierarchy(self, bases):
if bases is None:
return []
base_names = bases.split()
this_level = []
next_levels = []
for base in base_names:
split = base.split(':')
if len(split) == 2:
visibility = split[0]
base = split[1]
else:
visibility = Scope.public
declaration = self.getDeclaration(base)
if not isinstance(declaration, Class):
continue
base = Base(declaration.fullName(), visibility)
this_level.append(base)
# normalize with the other levels
for index, level in enumerate(declaration.hierarchy):
if index < len(next_levels):
next_levels[index] = next_levels[index] + level
else:
next_levels.append(level)
hierarchy = []
if this_level:
hierarchy.append(tuple(this_level))
if next_levels:
hierarchy.extend(next_levels)
return hierarchy
# getHierarchy()
def getLocation(self, location):
file, line = location.split(':')
file = self.getDeclaration(file)
return file, int(line)
# getLocation()
def getMembers(self, member_list):
if member_list is None:
return []
members = []
for member in member_list.split():
declaration = self.getDeclaration(member)
if type(declaration) in Class.validMemberTypes():
members.append(declaration)
return members
# getMembers()
def getType(self, id):
def check(id, feature):
pos = id.find(feature)
if pos != -1:
id = id[:pos] + id[pos+1:]
return True, id
else:
return False, id
const, id = check(id, 'c')
volatile, id = check(id, 'v')
restricted, id = check(id, 'r')
declaration = self.getDeclaration(id)
if isinstance(declaration, Type):
result = deepcopy(declaration)
if const:
result.const = const
if volatile:
result.volatile = volatile
if restricted:
result.restricted = restricted
else:
result = Type(declaration.fullName(), const)
result.volatile = volatile
result.restricted = restricted
return result
# getType()
def update(self, id, declaration):
element, _ = self.elements[id]
#pprint.pprint((id, element, declaration))
self.elements[id] = element, declaration
# update()
def parseElement(self, id, element):
method = 'parse' + element.tag
if hasattr(self, method):
func = getattr(self, method)
func(id, element)
else:
#print method
self.parseUnknown(id, element)
# parseElement()
def parseArrayType(self, id, element):
type = self.getType(element.get('type'))
min = element.get('min')
max = element.get('max')
array = ArrayType(type.name, type.const, min, max)
self.update(id, array)
# parseArrayType()
def parseFunctionType(self, id, element):
result = self.getType(element.get('returns'))
args = self.getArguments(element)
func = FunctionType(result, args)
self.update(id, func)
# parseFunctionType()
def parseClass(self, id, element):
#pprint.pprint((self.parseClass, element))
name = element.get('name')
abstract = bool(int(element.get('abstract', '0')))
location = self.getLocation(element.get('location'))
context = self.getDeclaration(element.get('context'))
incomplete = bool(int(element.get('incomplete', 0)))
if isinstance(context, str):
klass = Class(name, context, [], abstract)
else:
visibility = element.get('access', Scope.public)
klass = NestedClass(
name, context.fullName(), visibility, [], abstract)
klass.incomplete = incomplete
self.addDeclaration(klass)
klass.location = location
self.update(id, klass)
klass.hierarchy = self.getHierarchy(element.get('bases'))
if klass.hierarchy:
klass.bases = klass.hierarchy[0]
members = self.getMembers(element.get('members'))
for member in members:
klass.addMember(member)
# parseClass()
def parseConstructor(self, id, element):
name = element.get('name')
visibility = element.get('access', Scope.public)
classname = self.getDeclaration(element.get('context')).fullName()
location = self.getLocation(element.get('location'))
parameters = self.getArguments(element)
artificial = element.get('artificial', False)
if not artificial:
constructor = Constructor(name, classname, parameters, visibility)
else:
# we don't want artificial constructors
constructor = Unknown('__Unknown_Element_%s' % id)
constructor.location = location
self.update(id, constructor)
# parseConstructor()
def parseConverter(self, id, element):
self.parseMethod(id, element, ConverterOperator)
# parseConverter()
def parseDestructor(self, id, element):
name = element.get('name')
visibility = element.get('access', Scope.public)
classname = self.getDeclaration(element.get('context')).fullName()
virtual = bool(int(element.get('virtual', '0')))
location = self.getLocation(element.get('location'))
artificial = element.get('artificial', False)
if not artificial:
destructor = Destructor(name, classname, visibility, virtual)
else:
destructor = Unknown('__Unknown_Element_%s' % id)
destructor.location = location
self.update(id, destructor)
# parseDestructor()
def parseEnumeration(self, id, element):
name = element.get('name')
location = self.getLocation(element.get('location'))
context = self.getDeclaration(element.get('context'))
incomplete = bool(int(element.get('incomplete', 0)))
if isinstance(context, str):
enum = Enumeration(name, context)
else:
visibility = element.get('access', Scope.public)
enum = ClassEnumeration(name, context.fullName(), visibility)
self.addDeclaration(enum)
enum.location = location
for child in element:
if child.tag == 'EnumValue':
name = child.get('name')
value = int(child.get('init'))
enum.values[name] = value
enum.incomplete = incomplete
self.update(id, enum)
# parseEnumeration()
def parseField(self, id, element):
name = element.get('name')
visibility = element.get('access', Scope.public)
classname = self.getDeclaration(element.get('context')).fullName()
type = self.getType(element.get('type'))
static = bool(int(element.get('extern', '0')))
location = self.getLocation(element.get('location'))
variable = ClassVariable(type, name, classname, visibility, static)
variable.location = location
self.update(id, variable)
# parseField()
def parseFile(self, id, element):
filename = element.get('name')
self.update(id, filename)
# parseFile()
def parseFundamentalType(self, id, element):
name = element.get('name')
replacements = {
'long int': 'long',
'long unsigned int': 'unsigned long',
'short int': 'short',
'short unsigned int': 'unsigned short',
'unsigned int': 'unsigned',
}
name = replacements.get(name, name)
type = FundamentalType(name)
self.update(id, type)
# parseFundamentalType()
def parseMethod(self, id, element, methodType=Method):
name = element.get('name')
result = self.getType(element.get('returns'))
classname = self.getDeclaration(element.get('context')).fullName()
visibility = element.get('access', Scope.public)
static = bool(int(element.get('static', '0')))
virtual = bool(int(element.get('virtual', '0')))
abstract = bool(int(element.get('pure_virtual', '0')))
const = bool(int(element.get('const', '0')))
location = self.getLocation(element.get('location'))
throws = self.getExceptions(element.get('throw', None))
parameters = self.getArguments(element)
artificial = element.get('artificial', False)
if not artificial:
method = methodType(name, classname, result, parameters, visibility,
virtual, abstract, static, const, throws)
else:
# we don't want artificial methods
method = Unknown('__Unknown_Element_%s' % id)
method.location = location
self.update(id, method)
# parseMethod()
def parseNamespace(self, id, element):
namespace = element.get('name')
context = element.get('context')
if context:
outer = self.getDeclaration(context)
if not outer.endswith('::'):
outer += '::'
namespace = outer + namespace
if namespace.startswith('::'):
namespace = namespace[2:]
self.update(id, namespace)
# parseNameSpace()
def parseOperatorMethod(self, id, element):
self.parseMethod(id, element, ClassOperator)
# parseOperatorMethod()
def parsePointerType(self, id, element):
type = self.getType(element.get('type'))
expand = not isinstance(type, FunctionType)
ref = PointerType(type.name, type.const, None, expand, type.suffix)
self.update(id, ref)
# parsePointerType()
def parseStruct(self, id, element):
self.parseClass(id, element)
# parseStruct()
def parseReferenceType(self, id, element):
type = self.getType(element.get('type'))
expand = not isinstance(type, FunctionType)
ref = ReferenceType(type.name, type.const, None, expand, type.suffix)
self.update(id, ref)
# parseReference
def parseTypedef(self, id, element):
name = element.get('name')
type = self.getType(element.get('type'))
context = self.getDeclaration(element.get('context'))
if isinstance(context, Class):
context = context.fullName()
typedef = Typedef(type, name, context)
self.update(id, typedef)
self.addDeclaration(typedef)
# parseTypedef()
def parseUnknown(self, id, element):
# catches for instance Union, CvQualifiedType, ??
if debug > 1:
pprint.pprint(element.tag)
name = '__Unknown_Element__%s' % id
declaration = Unknown(name)
self.update(id, declaration)
# parseUnknown()
def parseVariable(self, id, element):
# in gcc_xml, a static Field is declared as a Variable,
# so we check this and call the Field parser.
context = self.getDeclaration(element.get('context'))
if isinstance(context, Class):
self.parseField(id, element)
_, declaration = self.elements[id]
declaration.static = True
else:
namespace = context
name = element.get('name')
type = self.getType(element.get('type'))
location = self.getLocation(element.get('location'))
variable = Variable(type, name, namespace)
variable.location = location
self.addDeclaration(variable)
self.update(id, variable)
# parseVariable()
# class GccXmlParser
def main():
pass
# main()
if __name__ == '__main__':
main()
# Local Variables: ***
# mode: python ***
# End: ***