| [51587aa] | 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 | // | 
|---|
| [a08ba92] | 7 | // InitModel.h -- | 
|---|
| [51587aa] | 8 | // | 
|---|
| [843054c2] | 9 | // Author           : Rodolfo G. Esteves | 
|---|
| [51587aa] | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [a08ba92] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Tue May 19 16:37:52 2015 | 
|---|
|  | 13 | // Update Count     : 2 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [a08ba92] | 15 |  | 
|---|
| [51b73452] | 16 | #ifndef _INITTWEAK_MODEL_H_ | 
|---|
|  | 17 | #define _INITTWEAK_MODEL_H_ | 
|---|
|  | 18 |  | 
|---|
|  | 19 | #include "Association.h" | 
|---|
| [d3b7937] | 20 | #include "Common/SemanticError.h" | 
|---|
| [51b73452] | 21 | #include "SynTree/Visitor.h" | 
|---|
|  | 22 |  | 
|---|
|  | 23 | #include "SynTree/Initializer.h" | 
|---|
|  | 24 | #include "SynTree/Declaration.h" | 
|---|
|  | 25 | #include "SynTree/Expression.h" | 
|---|
|  | 26 | #include "SynTree/Type.h" | 
|---|
|  | 27 |  | 
|---|
|  | 28 | namespace InitTweak { | 
|---|
| [a08ba92] | 29 | class InitModelBuilder : public AssociationBuilder, public Visitor { | 
|---|
|  | 30 | public: | 
|---|
|  | 31 | InitModelBuilder( Declaration * ); | 
|---|
|  | 32 | ~InitModelBuilder(); | 
|---|
| [bdd516a] | 33 |  | 
|---|
| [a08ba92] | 34 | virtual Association *grab_assoc() { taken = true; return building; } | 
|---|
|  | 35 | virtual Association *get_assoc() { return building; } | 
|---|
|  | 36 | void set_assoc( Association *newAssoc ) { building = newAssoc; } | 
|---|
| [bdd516a] | 37 |  | 
|---|
| [a08ba92] | 38 | void init(); | 
|---|
|  | 39 | static int interpretDimension( Expression *exp ) { | 
|---|
|  | 40 | ConstantFolder folder( exp ); | 
|---|
|  | 41 | try { | 
|---|
|  | 42 | return folder.get_constant(); | 
|---|
|  | 43 | } catch (...) { | 
|---|
|  | 44 | throw SemanticError("Invalid array dimension"); | 
|---|
|  | 45 | } | 
|---|
|  | 46 | } | 
|---|
| [bdd516a] | 47 |  | 
|---|
| [a08ba92] | 48 | // types | 
|---|
|  | 49 | virtual void visit( ArrayType * ); | 
|---|
|  | 50 | virtual void visit( StructInstType * ); | 
|---|
|  | 51 | virtual void visit( UnionInstType * ); | 
|---|
|  | 52 | virtual void visit( EnumInstType * ); | 
|---|
|  | 53 | virtual void visit( ContextInstType * ) { throw 0; } | 
|---|
|  | 54 | virtual void visit( TypeInstType * )    { throw 0; } | 
|---|
|  | 55 | // virtual void visit( TupleType *tupleType ); | 
|---|
|  | 56 | // declarations | 
|---|
|  | 57 | virtual void visit( StructDecl *); | 
|---|
|  | 58 | virtual void visit( UnionDecl *); | 
|---|
|  | 59 | virtual void visit( EnumDecl *); | 
|---|
| [bdd516a] | 60 | private: | 
|---|
| [a08ba92] | 61 | class ConstantFolder : public Visitor { | 
|---|
|  | 62 | public: | 
|---|
|  | 63 | ConstantFolder( Expression *_expr = 0 ): expr(_expr) {} | 
|---|
|  | 64 | int get_constant() throw() { expr->accept( *this ); return value; } | 
|---|
|  | 65 | void set_constant( Expression *newExp ) { expr = newExp; } | 
|---|
|  | 66 | // Visitor interface | 
|---|
|  | 67 | void visit( Expression * ) { throw 0; } | 
|---|
|  | 68 | void visit( NameExpr * ) { throw 0; } | 
|---|
|  | 69 | void visit( CastExpr * ) { throw 0; } | 
|---|
|  | 70 | void visit( UntypedMemberExpr * ) { throw 0; } | 
|---|
|  | 71 | void visit( VariableExpr * ) { throw 0; } | 
|---|
|  | 72 | void visit( ConstantExpr * ); | 
|---|
|  | 73 | void visit( SizeofExpr * ) { throw 0; } | 
|---|
| [47534159] | 74 | void visit( AlignofExpr * ) { throw 0; } | 
|---|
| [2a4b088] | 75 | void visit( UntypedOffsetofExpr * ) { throw 0; } | 
|---|
| [25a054f] | 76 | void visit( OffsetofExpr * ) { throw 0; } | 
|---|
| [a08ba92] | 77 | void visit( AttrExpr * ) { throw 0; } | 
|---|
|  | 78 | void visit( LogicalExpr * ) { throw 0; } | 
|---|
|  | 79 | void visit( ConditionalExpr * ) { throw 0; } | 
|---|
|  | 80 | void visit( CommaExpr * ) { throw 0; } | 
|---|
|  | 81 | private: | 
|---|
|  | 82 | Expression *expr; | 
|---|
|  | 83 | int value; | 
|---|
|  | 84 | }; | 
|---|
| [51b73452] | 85 |  | 
|---|
| [a08ba92] | 86 | bool taken; | 
|---|
|  | 87 | Declaration *decl;  // ? | 
|---|
|  | 88 | Association *building; | 
|---|
|  | 89 | }; | 
|---|
| [bdd516a] | 90 |  | 
|---|
| [a08ba92] | 91 | class InitModelFiller : public AssociationFiller, public Visitor { | 
|---|
|  | 92 | public: | 
|---|
|  | 93 | InitModelFiller( Association *, Initializer *, bool _topLevel = false ); | 
|---|
|  | 94 | ~InitModelFiller() { /* pointers in here are not owned by object (never created by object either) */ } | 
|---|
|  | 95 | virtual Association *get_assoc() { return model; } | 
|---|
|  | 96 | virtual void set_assoc( Association *newAssoc ) { model = newAssoc; } | 
|---|
| [51b73452] | 97 |  | 
|---|
| [a08ba92] | 98 | void init(); | 
|---|
|  | 99 | // Visitor interface | 
|---|
|  | 100 | virtual void visit( SingleInit *singleInit ); | 
|---|
|  | 101 | virtual void visit( ListInit *listInit ); | 
|---|
|  | 102 | private: | 
|---|
|  | 103 | Association *model; | 
|---|
|  | 104 | Initializer *orgInit; | 
|---|
|  | 105 | bool topLevel; | 
|---|
|  | 106 | long int next; | 
|---|
|  | 107 | }; | 
|---|
| [bdd516a] | 108 |  | 
|---|
| [a08ba92] | 109 | class InitUnspooler : public AssociationVisitor { | 
|---|
|  | 110 | public: | 
|---|
|  | 111 | InitUnspooler() : init(0), taken( false ) {} | 
|---|
|  | 112 | virtual ~InitUnspooler() { if ( ! taken && (init != 0)) { delete init; init = 0; } } | 
|---|
|  | 113 | Initializer *get_initializer() { return init; } | 
|---|
|  | 114 | Initializer *grab_initializer() { taken = true; return init; } | 
|---|
| [51b73452] | 115 |  | 
|---|
| [a08ba92] | 116 | virtual void visit( SingleName * ); | 
|---|
|  | 117 | virtual void visit( PointAssociation * ); | 
|---|
|  | 118 | virtual void visit( RangeAssociation * ) { std::cerr << "InitUnspooler - In a range assoc" << std::endl; return; } | 
|---|
|  | 119 | private: | 
|---|
|  | 120 | Initializer *init; | 
|---|
|  | 121 | bool taken; | 
|---|
|  | 122 | }; | 
|---|
| [51b73452] | 123 | } // namespace InitTweak | 
|---|
|  | 124 |  | 
|---|
| [bdd516a] | 125 | #endif // _INITTWEAK_MODEL_H_ | 
|---|
| [51b73452] | 126 |  | 
|---|
| [51587aa] | 127 | // Local Variables: // | 
|---|
|  | 128 | // tab-width: 4 // | 
|---|
|  | 129 | // mode: c++ // | 
|---|
|  | 130 | // compile-command: "make install" // | 
|---|
|  | 131 | // End: // | 
|---|