[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 | // |
---|
| 7 | // CodeGenerator2.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 23:35:37 2015 |
---|
| 13 | // Update Count : 2 |
---|
| 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #ifndef CODEGENV_H |
---|
| 17 | #define CODEGENV_H |
---|
| 18 | |
---|
| 19 | #include <strstream> |
---|
| 20 | #include <list> |
---|
| 21 | |
---|
| 22 | #include "SynTree/SynTree.h" |
---|
| 23 | #include "SynTree/Visitor.h" |
---|
| 24 | #include "SymTab/Indexer.h" |
---|
| 25 | |
---|
| 26 | namespace CodeGen { |
---|
[51587aa] | 27 | class CodeGenerator2 : public Visitor { |
---|
| 28 | public: |
---|
| 29 | static int tabsize; |
---|
| 30 | |
---|
| 31 | CodeGenerator2( std::ostream &os ); |
---|
| 32 | CodeGenerator2( std::ostream &os, std::string, int indent = 0, bool infun = false ); |
---|
| 33 | CodeGenerator2( std::ostream &os, char *, int indent = 0, bool infun = false ); |
---|
| 34 | |
---|
| 35 | CodeGenerator2( CodeGenerator2 & ); |
---|
| 36 | |
---|
| 37 | //*** Declaration |
---|
| 38 | virtual void visit( StructDecl * ); |
---|
| 39 | virtual void visit( FunctionDecl * ); |
---|
| 40 | virtual void visit( ObjectDecl * ); |
---|
| 41 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
| 42 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
| 43 | virtual void visit( ContextDecl *aggregateDecl ); |
---|
| 44 | virtual void visit( TypedefDecl *typeDecl ); |
---|
| 45 | virtual void visit( TypeDecl *typeDecl ); |
---|
| 46 | |
---|
| 47 | //*** Initializer |
---|
| 48 | virtual void visit( SingleInit * ); |
---|
| 49 | virtual void visit( ListInit * ); |
---|
| 50 | |
---|
| 51 | //*** Constant |
---|
| 52 | virtual void visit( Constant * ); |
---|
| 53 | |
---|
| 54 | //*** Expression |
---|
| 55 | virtual void visit( ApplicationExpr *applicationExpr ); |
---|
| 56 | virtual void visit( UntypedExpr *untypedExpr ); |
---|
| 57 | virtual void visit( NameExpr *nameExpr ); |
---|
| 58 | virtual void visit( AddressExpr *addressExpr ); |
---|
| 59 | virtual void visit( CastExpr *castExpr ); |
---|
| 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( LogicalExpr *logicalExpr ); |
---|
| 66 | virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
| 67 | virtual void visit( CommaExpr *commaExpr ); |
---|
| 68 | virtual void visit( TupleExpr *tupleExpr ); |
---|
| 69 | virtual void visit( TypeExpr *typeExpr ); |
---|
| 70 | |
---|
| 71 | //*** Statements |
---|
| 72 | virtual void visit( CompoundStmt * ); |
---|
| 73 | virtual void visit( ExprStmt * ); |
---|
| 74 | virtual void visit( IfStmt * ); |
---|
| 75 | virtual void visit( SwitchStmt * ); |
---|
| 76 | virtual void visit( CaseStmt * ); |
---|
| 77 | virtual void visit( BranchStmt * ); |
---|
| 78 | virtual void visit( ReturnStmt * ); |
---|
| 79 | virtual void visit( WhileStmt * ); |
---|
| 80 | virtual void visit( ForStmt * ); |
---|
| 81 | virtual void visit( NullStmt * ); |
---|
| 82 | virtual void visit( DeclStmt * ); |
---|
| 83 | |
---|
| 84 | std::string get_string( void ); |
---|
| 85 | void add_string_left( std::string s ) { before << s; } |
---|
| 86 | void shift_left(); |
---|
| 87 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); |
---|
| 88 | private: |
---|
| 89 | int cur_indent; |
---|
| 90 | bool insideFunction; |
---|
| 91 | std::ostream &before; |
---|
| 92 | std::string after; |
---|
| 93 | |
---|
| 94 | static std::string printLabels ( std::list < Label > & ); |
---|
| 95 | void handleStorageClass( Declaration *decl ); |
---|
| 96 | void handleAggregate( AggregateDecl *aggDecl ); |
---|
| 97 | void handleTypedef( NamedTypeDecl *namedType ); |
---|
| 98 | |
---|
| 99 | }; |
---|
| 100 | |
---|
| 101 | template< class Iterator > |
---|
| 102 | void CodeGenerator2::genCommaList( Iterator begin, Iterator end ) { |
---|
| 103 | if ( begin == end ) return; |
---|
| 104 | |
---|
| 105 | for ( ;; ) { |
---|
| 106 | (*begin++)->accept( *this ); |
---|
| 107 | if ( begin == end ) return; |
---|
| 108 | before << ", "; |
---|
| 109 | } // for |
---|
[17cd4eb] | 110 | } |
---|
[51b7345] | 111 | |
---|
[51587aa] | 112 | inline bool doSemicolon( Declaration* decl ) { |
---|
| 113 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { |
---|
| 114 | return ! func->get_statements(); |
---|
| 115 | } // if |
---|
| 116 | return true; |
---|
[17cd4eb] | 117 | } |
---|
[51b7345] | 118 | } // namespace CodeGen |
---|
| 119 | |
---|
[51587aa] | 120 | #endif // CODEGENV_H |
---|
[51b7345] | 121 | |
---|
[51587aa] | 122 | // Local Variables: // |
---|
| 123 | // tab-width: 4 // |
---|
| 124 | // mode: c++ // |
---|
| 125 | // compile-command: "make install" // |
---|
| 126 | // End: // |
---|