source: translator/InitTweak/BasicInit.h @ 51587aa

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 51587aa was 51587aa, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: fourth groups of files

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