1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // FixInit.h -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Wed Jan 13 16:29:30 2016 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Wed Jan 13 16:45:37 2016 |
---|
13 | // Update Count : 17 |
---|
14 | // |
---|
15 | |
---|
16 | #include <stack> |
---|
17 | #include <list> |
---|
18 | #include "RemoveInit.h" |
---|
19 | #include "SynTree/Declaration.h" |
---|
20 | #include "SynTree/Type.h" |
---|
21 | #include "SynTree/Expression.h" |
---|
22 | #include "SynTree/Statement.h" |
---|
23 | #include "SynTree/Initializer.h" |
---|
24 | #include "SynTree/Mutator.h" |
---|
25 | #include "GenPoly/PolyMutator.h" |
---|
26 | |
---|
27 | namespace InitTweak { |
---|
28 | namespace { |
---|
29 | const std::list<Label> noLabels; |
---|
30 | } |
---|
31 | |
---|
32 | class FixInit : public GenPoly::PolyMutator { |
---|
33 | public: |
---|
34 | static void fixInitializers( std::list< Declaration * > &translationUnit ); |
---|
35 | |
---|
36 | virtual ObjectDecl * mutate( ObjectDecl *objDecl ); |
---|
37 | }; |
---|
38 | |
---|
39 | void fix( std::list< Declaration * > & translationUnit ) { |
---|
40 | FixInit::fixInitializers( translationUnit ); |
---|
41 | } |
---|
42 | |
---|
43 | void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) { |
---|
44 | FixInit fixer; |
---|
45 | mutateAll( translationUnit, fixer ); |
---|
46 | } |
---|
47 | |
---|
48 | // in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the |
---|
49 | // declaration. This will (seemingly) cause the later phases to do the right thing with the assignment |
---|
50 | ObjectDecl *FixInit::mutate( ObjectDecl *objDecl ) { |
---|
51 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
52 | // a decision should have been made by the resolver, so either ctor is NULL or init is NULL |
---|
53 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); |
---|
54 | if ( Expression * ctor = ctorInit->get_ctor() ) { |
---|
55 | ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor ); |
---|
56 | stmtsToAddAfter.push_back( ctorStmt ); |
---|
57 | objDecl->set_init( NULL ); |
---|
58 | ctorInit->set_ctor( NULL ); |
---|
59 | } else if ( Initializer * init = ctorInit->get_init() ) { |
---|
60 | objDecl->set_init( init ); |
---|
61 | ctorInit->set_init( NULL ); |
---|
62 | } else { |
---|
63 | // one of them should be non-NULL |
---|
64 | assert( ctorInit->get_ctor() || ctorInit->get_init() ); |
---|
65 | } |
---|
66 | delete ctorInit; |
---|
67 | } |
---|
68 | return objDecl; |
---|
69 | } |
---|
70 | } // namespace InitTweak |
---|
71 | |
---|
72 | // Local Variables: // |
---|
73 | // tab-width: 4 // |
---|
74 | // mode: c++ // |
---|
75 | // compile-command: "make install" // |
---|
76 | // End: // |
---|