| 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 | // Visitor.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Mon May 18 11:15:55 2015 | 
|---|
| 13 | // Update Count     : 2 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef VISITOR_H | 
|---|
| 17 | #define VISITOR_H | 
|---|
| 18 |  | 
|---|
| 19 | #include "SynTree.h" | 
|---|
| 20 | #include "SemanticError.h" | 
|---|
| 21 | #include "CompilerError.h" | 
|---|
| 22 |  | 
|---|
| 23 | class Visitor { | 
|---|
| 24 | protected: | 
|---|
| 25 | Visitor(); | 
|---|
| 26 | virtual ~Visitor(); | 
|---|
| 27 | public: | 
|---|
| 28 | virtual void visit( ObjectDecl *objectDecl ); | 
|---|
| 29 | virtual void visit( FunctionDecl *functionDecl ); | 
|---|
| 30 | virtual void visit( StructDecl *aggregateDecl ); | 
|---|
| 31 | virtual void visit( UnionDecl *aggregateDecl ); | 
|---|
| 32 | virtual void visit( EnumDecl *aggregateDecl ); | 
|---|
| 33 | virtual void visit( ContextDecl *aggregateDecl ); | 
|---|
| 34 | virtual void visit( TypeDecl *typeDecl ); | 
|---|
| 35 | virtual void visit( TypedefDecl *typeDecl ); | 
|---|
| 36 |  | 
|---|
| 37 | virtual void visit( CompoundStmt *compoundStmt ); | 
|---|
| 38 | virtual void visit( ExprStmt *exprStmt ); | 
|---|
| 39 | virtual void visit( IfStmt *ifStmt ); | 
|---|
| 40 | virtual void visit( WhileStmt *whileStmt ); | 
|---|
| 41 | virtual void visit( ForStmt *forStmt ); | 
|---|
| 42 | virtual void visit( SwitchStmt *switchStmt ); | 
|---|
| 43 | virtual void visit( ChooseStmt *switchStmt ); | 
|---|
| 44 | virtual void visit( FallthruStmt *switchStmt ); | 
|---|
| 45 | virtual void visit( CaseStmt *caseStmt ); | 
|---|
| 46 | virtual void visit( BranchStmt *branchStmt ); | 
|---|
| 47 | virtual void visit( ReturnStmt *returnStmt ); | 
|---|
| 48 | virtual void visit( TryStmt *tryStmt ); | 
|---|
| 49 | virtual void visit( CatchStmt *catchStmt ); | 
|---|
| 50 | virtual void visit( FinallyStmt *finallyStmt ); | 
|---|
| 51 | virtual void visit( NullStmt *nullStmt ); | 
|---|
| 52 | virtual void visit( DeclStmt *declStmt ); | 
|---|
| 53 |  | 
|---|
| 54 | virtual void visit( ApplicationExpr *applicationExpr ); | 
|---|
| 55 | virtual void visit( UntypedExpr *untypedExpr ); | 
|---|
| 56 | virtual void visit( NameExpr *nameExpr ); | 
|---|
| 57 | virtual void visit( CastExpr *castExpr ); | 
|---|
| 58 | virtual void visit( AddressExpr *addressExpr ); | 
|---|
| 59 | virtual void visit( LabelAddressExpr *labAddressExpr ); | 
|---|
| 60 | virtual void visit( UntypedMemberExpr *memberExpr ); | 
|---|
| 61 | virtual void visit( MemberExpr *memberExpr ); | 
|---|
| 62 | virtual void visit( VariableExpr *variableExpr ); | 
|---|
| 63 | virtual void visit( ConstantExpr *constantExpr ); | 
|---|
| 64 | virtual void visit( SizeofExpr *sizeofExpr ); | 
|---|
| 65 | virtual void visit( AttrExpr *attrExpr ); | 
|---|
| 66 | virtual void visit( LogicalExpr *logicalExpr ); | 
|---|
| 67 | virtual void visit( ConditionalExpr *conditionalExpr ); | 
|---|
| 68 | virtual void visit( CommaExpr *commaExpr ); | 
|---|
| 69 | virtual void visit( TupleExpr *tupleExpr ); | 
|---|
| 70 | virtual void visit( SolvedTupleExpr *tupleExpr ); | 
|---|
| 71 | virtual void visit( TypeExpr *typeExpr ); | 
|---|
| 72 | virtual void visit( UntypedValofExpr *valofExpr ); | 
|---|
| 73 |  | 
|---|
| 74 | virtual void visit( VoidType *basicType ); | 
|---|
| 75 | virtual void visit( BasicType *basicType ); | 
|---|
| 76 | virtual void visit( PointerType *pointerType ); | 
|---|
| 77 | virtual void visit( ArrayType *arrayType ); | 
|---|
| 78 | virtual void visit( FunctionType *functionType ); | 
|---|
| 79 | virtual void visit( StructInstType *aggregateUseType ); | 
|---|
| 80 | virtual void visit( UnionInstType *aggregateUseType ); | 
|---|
| 81 | virtual void visit( EnumInstType *aggregateUseType ); | 
|---|
| 82 | virtual void visit( ContextInstType *aggregateUseType ); | 
|---|
| 83 | virtual void visit( TypeInstType *aggregateUseType ); | 
|---|
| 84 | virtual void visit( TupleType *tupleType ); | 
|---|
| 85 | virtual void visit( TypeofType *typeofType ); | 
|---|
| 86 | virtual void visit( AttrType *attrType ); | 
|---|
| 87 |  | 
|---|
| 88 | virtual void visit( SingleInit *singleInit ); | 
|---|
| 89 | virtual void visit( ListInit *listInit ); | 
|---|
| 90 |  | 
|---|
| 91 | virtual void visit( Subrange *subrange ); | 
|---|
| 92 |  | 
|---|
| 93 | virtual void visit( Constant *constant ); | 
|---|
| 94 | private: | 
|---|
| 95 | virtual void visit( AggregateDecl *aggregateDecl ); | 
|---|
| 96 | virtual void visit( NamedTypeDecl *typeDecl ); | 
|---|
| 97 | virtual void visit( ReferenceToType *aggregateUseType ); | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | template< typename TreeType, typename VisitorType > | 
|---|
| 101 | inline void maybeAccept( TreeType *tree, VisitorType &visitor ) { | 
|---|
| 102 | if ( tree ) { | 
|---|
| 103 | tree->accept( visitor ); | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | template< typename Container, typename VisitorType > | 
|---|
| 108 | inline void acceptAll( Container &container, VisitorType &visitor ) { | 
|---|
| 109 | SemanticError errors; | 
|---|
| 110 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
| 111 | try { | 
|---|
| 112 | if ( *i ) { | 
|---|
| 113 | (*i)->accept( visitor ); | 
|---|
| 114 | } | 
|---|
| 115 | } catch( SemanticError &e ) { | 
|---|
| 116 | errors.append( e ); | 
|---|
| 117 | } | 
|---|
| 118 | } | 
|---|
| 119 | if ( ! errors.isEmpty() ) { | 
|---|
| 120 | throw errors; | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | template< typename Container, typename VisitorType > | 
|---|
| 125 | void acceptAllFold( Container &container, VisitorType &visitor, VisitorType &around ) { | 
|---|
| 126 | SemanticError errors; | 
|---|
| 127 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
| 128 | try { | 
|---|
| 129 | if ( *i ) { | 
|---|
| 130 | VisitorType *v = new VisitorType; | 
|---|
| 131 | (*i)->accept( *v ); | 
|---|
| 132 |  | 
|---|
| 133 | typename Container::iterator nxt = i; nxt++; // forward_iterator | 
|---|
| 134 | if ( nxt == container.end() ) | 
|---|
| 135 | visitor += *v; | 
|---|
| 136 | else | 
|---|
| 137 | visitor += *v + around; | 
|---|
| 138 |  | 
|---|
| 139 | delete v; | 
|---|
| 140 | } // if | 
|---|
| 141 | } catch( SemanticError &e ) { | 
|---|
| 142 | errors.append( e ); | 
|---|
| 143 | } // try | 
|---|
| 144 | } // for | 
|---|
| 145 | if ( ! errors.isEmpty() ) { | 
|---|
| 146 | throw errors; | 
|---|
| 147 | } // if | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | #endif // VISITOR_H | 
|---|
| 151 |  | 
|---|
| 152 | // Local Variables: // | 
|---|
| 153 | // tab-width: 4 // | 
|---|
| 154 | // mode: c++ // | 
|---|
| 155 | // compile-command: "make install" // | 
|---|
| 156 | // End: // | 
|---|