source: translator/InitTweak/BasicInit.h @ 51b7345

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 51b7345 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 6.1 KB
Line 
1#ifndef _BASINIT_H_
2#define _BASINIT_H_
3
4#include <list>
5
6#include "SynTree/Visitor.h"
7#include "SymTab/Indexer.h"
8
9#include "SynTree/Type.h"
10#include "SynTree/Initializer.h"
11#include "SynTree/Expression.h"
12#include "NameInCollection.h"
13#include "NameAssociation.h"
14
15namespace InitTweak {
16
17  bool isDeclStmtP(Statement *stmt);
18
19  class BreakInitializer;
20  class BreakDesignator;
21
22  class BasicInit: public Mutator
23    {
24    public:
25      BasicInit() : bindings( 0 ) {}
26      BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {}
27      BasicInit( const BasicInit &other ) {
28        bindings = other.get_bindings();
29        index = other.index;
30      }
31
32      ~BasicInit() { /* delete bindings; bindings = 0; */ }
33
34      NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; }
35      void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) {
36        bindings = newValue;
37      }
38
39      bool has_bindings() {
40        return ( get_bindings() != 0 || ! stmts.empty() );
41      }
42
43      virtual ObjectDecl     *mutate( ObjectDecl *objectDecl )
44      { index.visit( objectDecl ); return objectDecl; }
45      virtual TypeDecl       *mutate( TypeDecl *typeDecl )
46      { index.visit( typeDecl ); return typeDecl; }
47      virtual TypedefDecl    *mutate( TypedefDecl *typeDecl )
48      { index.visit( typeDecl ); return typeDecl; }
49      virtual StructDecl     *mutate( StructDecl *aggregateDecl )
50      { index.visit( aggregateDecl ); return aggregateDecl; }
51      virtual UnionDecl      *mutate( UnionDecl *aggregateDecl )
52      { index.visit( aggregateDecl ); return aggregateDecl; }
53      virtual EnumDecl       *mutate( EnumDecl *aggregateDecl )
54      { index.visit( aggregateDecl ); return aggregateDecl; }
55
56      virtual Type           *mutate( StructInstType *aggrInst )
57      { index.visit( aggrInst ); return aggrInst; }
58      virtual Type           *mutate( UnionInstType *aggrInst )
59      { index.visit( aggrInst ); return aggrInst; }
60
61      virtual CompoundStmt   *mutate(CompoundStmt *compoundStmt);
62      virtual Statement *mutate(DeclStmt *declStmt);
63
64      std::list< Statement *> get_statements() const { return stmts;  }
65
66      static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc,
67                                    std::string aggName, std::list< Statement *> &stmts );
68    private:
69      NameAssociation< Expression *, BreakInitializer > *bindings;
70      Statement *assignFromDecl( DeclStmt *declStmt );
71      SymTab::Indexer index;
72      std::list< Statement *> stmts;
73
74      class Classify {
75      public:
76        enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T };
77        enum InitKind { NULL_I, SINGLE_I, COMPOUND_I };
78
79        static TypeKind type( Type * );
80        static InitKind initializer( Initializer *);
81
82        static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index );
83        static std::list< Statement * >
84           matchInit( NameInCollection *, ObjectDecl *, Initializer * );
85        static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit );
86
87        // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc );
88      };
89  };
90
91  class BreakInitializer {
92    enum InitKind { EMPTY, SINGLE, COMPOUND };
93
94    class BreakDesignator;
95    typedef BreakDesignator NameSplitter;
96
97  public:
98    typedef std::list<Initializer *>::iterator element_iterator;
99    typedef std::list< NameSplitter >::iterator name_iterator;
100
101    BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) {
102      std::list<Expression *> temp;
103
104      if( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) {
105        kind = SINGLE;
106        temp = sinit->get_designators();
107      } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) {
108        kind = COMPOUND;
109        temp = cinit->get_designators();
110      }
111
112      std::transform( temp.begin(), temp.end(), std::back_inserter( designators ),
113                      ctor_noptr<NameSplitter, Expression *> );
114
115    }
116
117    //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; }
118    ~BreakInitializer () {}
119
120    BreakInitializer set_name( NameSplitter &name ) {
121      designators.clear();
122      designators.push_back( name );
123
124      return *this;
125    }
126
127    element_iterator element_begin() {
128      assert( cinit != 0 );
129      return cinit->begin_initializers();
130    }
131    element_iterator element_end() {
132      assert( cinit != 0 );
133      return cinit->end_initializers();
134    }
135
136    name_iterator names_begin() { return designators.begin(); }
137    name_iterator names_end() { return designators.end(); }
138
139    int names_size() const { return designators.size(); }
140
141    bool has_index() const { return ! designators.empty(); }
142    bool is_single() const { return kind == SINGLE; }
143    bool is_composite() const { return kind == COMPOUND;  }
144
145    Expression *get_value() {
146      switch ( kind ) {
147      case EMPTY:
148        return 0;
149        break;
150      case SINGLE:
151        return sinit->get_value();
152        break;
153      case COMPOUND:
154        assert(false);
155        break;
156      default:
157        assert(false);
158      }
159      return 0;
160    }
161
162    // attributes
163  private:
164    InitKind kind;
165    SingleInit *sinit;
166    ListInit *cinit;
167    std::list< BreakDesignator > designators;
168
169    // helper classes
170  public:
171    class BreakDesignator {
172    public:
173      BreakDesignator( Expression *exp ) {
174        Expression *prfx = exp;
175        UntypedMemberExpr *me = 0;
176
177        do {
178          if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break;
179          blown_struct.push_front( me->get_member() );
180          prfx = me->get_aggregate();
181        } while( prfx != 0 );
182
183        NameExpr *ne;
184        if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 ) 
185          blown_struct.push_front( ne->get_name() );
186      }
187
188      BreakDesignator( std::string name ) {
189        blown_struct.push_front( name );
190      }
191
192      bool is_flat() const { return blown_struct.size() == 1; }
193      bool is_nested() const { return blown_struct.size() > 1; }
194
195      std::string get_name() { return blown_struct.front(); }
196
197      BreakDesignator &name_remainder() {
198        blown_struct.pop_front();
199        return *this;
200      }
201
202    private:
203      std::list< std::string > blown_struct;
204    };
205
206  };
207
208} // namespace InitTweak
209
210#endif // #ifndef _BASINIT_H_
211
212/*
213  Local Variables:
214  mode: c++
215  End:
216*/
Note: See TracBrowser for help on using the repository browser.