File: Synopsis/Processors/TypedefFolder.py
 1#
 2# Copyright (C) 2008 Stefan Seefeld
 3# All rights reserved.
 4# Licensed to the public under the terms of the GNU LGPL (>= 2),
 5# see the file COPYING for details.
 6#
 7
 8from Synopsis.Processor import *
 9from Synopsis import ASG
10
11class TypedefFolder(Processor, ASG.Visitor):
12    """Fold (anonymous) types into enclosing typedefs."""
13
14    anonymous_only = Parameter(True, "Whether or not folding should be restricted to unnamed types.")
15
16    def process(self, ir, **kwds):
17
18        self.set_parameters(kwds)
19        self.ir = self.merge_input(ir)
20
21        self.scopes = []
22        # Iterate over a copy so we can safely modify
23        # the original in the process.
24        decls = self.ir.asg.declarations[:]
25        for d in decls:
26            d.accept(self)
27        return self.output_and_return_ir()
28
29    def visit_scope(self, s):
30
31        self.scopes.append(s)
32        decls = s.declarations[:]
33        for d in decls:
34            d.accept(self)
35        self.scopes.pop()
36
37    visit_group = visit_scope
38
39    def visit_typedef(self, t):
40
41        if t.constr:
42            if self.verbose: print 'replace', t.alias.name, 'by', t.name
43            alias = self.ir.asg.types.pop(t.alias.name)
44            alias.declaration.name = alias.name = t.name
45            self.ir.asg.types[alias.name] = alias
46            if len(self.scopes):
47                decls = self.scopes[-1].declarations
48            else:
49                decls = self.ir.asg.declarations
50            del decls[decls.index(t)]
51
52            if type(alias.declaration) == ASG.Class:
53                i = len(alias.declaration.name)
54                for d in alias.declaration.declarations:
55                    d.name = d.name[:i-1] + (alias.name[-1],) + d.name[i:]
56