source: translator/InitTweak/InitModel.h @ 8c17ab0

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 8c17ab0 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#ifndef _INITTWEAK_MODEL_H_
2#define _INITTWEAK_MODEL_H_
3
4#include "Association.h"
5#include "SemanticError.h"
6#include "SynTree/Visitor.h"
7
8#include "SynTree/Initializer.h"
9#include "SynTree/Declaration.h"
10#include "SynTree/Expression.h"
11#include "SynTree/Type.h"
12
13namespace InitTweak {
14
15  class InitModelBuilder : public AssociationBuilder, public Visitor {
16  public:
17    InitModelBuilder( Declaration * );
18    ~InitModelBuilder();
19
20    virtual Association *grab_assoc() { taken = true; return building; }
21    virtual Association *get_assoc() { return building; }
22    void set_assoc( Association *newAssoc ) { building = newAssoc; }
23
24    void init();
25    static int interpretDimension( Expression *exp ) {
26      ConstantFolder folder( exp );
27      try {
28        return folder.get_constant();
29      } catch (...) {
30        throw SemanticError("Invalid array dimension");
31      }
32    }
33
34    // types
35    virtual void visit( ArrayType * );
36    virtual void visit( StructInstType * );
37    virtual void visit( UnionInstType * );
38    virtual void visit( EnumInstType * );
39    virtual void visit( ContextInstType * ) { throw 0; }
40    virtual void visit( TypeInstType * )    { throw 0; }
41    // virtual void visit( TupleType *tupleType );
42    // declarations
43    virtual void visit( StructDecl *);
44    virtual void visit( UnionDecl *);
45    virtual void visit( EnumDecl *);
46
47  private:
48    class ConstantFolder : public Visitor {
49    public:
50      ConstantFolder( Expression *_expr = 0 ): expr(_expr) {}
51      int get_constant() throw() { expr->accept( *this ); return value; }
52      void set_constant( Expression *newExp ) { expr = newExp; }
53      // Visitor interface
54      void visit( Expression * ) { throw 0; }
55      void visit( NameExpr * ) { throw 0; }
56      void visit( CastExpr * ) { throw 0; }
57      void visit( UntypedMemberExpr * ) { throw 0; }
58      void visit( VariableExpr * ) { throw 0; }
59      void visit( ConstantExpr * );
60      void visit( SizeofExpr * ) { throw 0; }
61      void visit( AttrExpr * ) { throw 0; }
62      void visit( LogicalExpr * ) { throw 0; }
63      void visit( ConditionalExpr * ) { throw 0; }
64      void visit( CommaExpr * ) { throw 0; }
65    private:
66      Expression *expr;
67      int value;
68    };
69
70    bool taken;
71    Declaration *decl;  // ?
72    Association *building;
73  };
74
75  class InitModelFiller : public AssociationFiller, public Visitor {
76  public:
77    InitModelFiller( Association *, Initializer *, bool _topLevel = false );
78    ~InitModelFiller() { /* pointers in here are not owned by object (never created by object either) */ }
79    virtual Association *get_assoc() { return model; }
80    virtual void set_assoc( Association *newAssoc ) { model = newAssoc; }
81
82    void init();
83    // Visitor interface
84    virtual void visit( MemberInit *memberInit ) { throw 0; }
85    virtual void visit( ElementInit *elementInit ) { throw 0; }
86    virtual void visit( SingleInit *singleInit );
87    virtual void visit( ListInit *listInit );
88
89  private:
90    Association *model;
91    Initializer *orgInit;
92    bool topLevel;
93    long int next;
94  };
95
96  class InitUnspooler : public AssociationVisitor {
97  public:
98    InitUnspooler() : init(0), taken( false ) {}
99    virtual ~InitUnspooler() { if (!taken && (init != 0)) { delete init; init = 0; } }
100    Initializer *get_initializer() { return init; }
101    Initializer *grab_initializer() { taken = true; return init; }
102
103    virtual void visit( SingleName * );
104    virtual void visit( PointAssociation * );
105    virtual void visit( RangeAssociation * ) { std::cerr << "InitUnspooler - In a range assoc" << std::endl; return; }
106
107  private:
108    Initializer *init;
109    bool taken;
110  };
111
112} // namespace InitTweak
113
114#endif // #define _INITTWEAK_MODEL_H_
115
116/*
117  Local Variables:
118  mode: c++
119  End:
120*/
Note: See TracBrowser for help on using the repository browser.