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 | // CodeGenerator.h -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Tue Jun 02 13:43:29 2015 |
---|
13 | // Update Count : 14 |
---|
14 | // |
---|
15 | |
---|
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 { |
---|
27 | class CodeGenerator : public Visitor { |
---|
28 | public: |
---|
29 | static int tabsize; |
---|
30 | |
---|
31 | CodeGenerator( std::ostream &os ); |
---|
32 | CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false ); |
---|
33 | CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false ); |
---|
34 | |
---|
35 | //*** Declaration |
---|
36 | virtual void visit( StructDecl * ); |
---|
37 | virtual void visit( FunctionDecl * ); |
---|
38 | virtual void visit( ObjectDecl * ); |
---|
39 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
40 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
41 | virtual void visit( ContextDecl *aggregateDecl ); |
---|
42 | virtual void visit( TypedefDecl *typeDecl ); |
---|
43 | virtual void visit( TypeDecl *typeDecl ); |
---|
44 | |
---|
45 | //*** Initializer |
---|
46 | virtual void visit( SingleInit * ); |
---|
47 | virtual void visit( ListInit * ); |
---|
48 | |
---|
49 | //*** Constant |
---|
50 | virtual void visit( Constant * ); |
---|
51 | |
---|
52 | //*** Expression |
---|
53 | virtual void visit( ApplicationExpr *applicationExpr ); |
---|
54 | virtual void visit( UntypedExpr *untypedExpr ); |
---|
55 | virtual void visit( NameExpr *nameExpr ); |
---|
56 | virtual void visit( AddressExpr *addressExpr ); |
---|
57 | virtual void visit( CastExpr *castExpr ); |
---|
58 | virtual void visit( UntypedMemberExpr *memberExpr ); |
---|
59 | virtual void visit( MemberExpr *memberExpr ); |
---|
60 | virtual void visit( VariableExpr *variableExpr ); |
---|
61 | virtual void visit( ConstantExpr *constantExpr ); |
---|
62 | virtual void visit( SizeofExpr *sizeofExpr ); |
---|
63 | virtual void visit( LogicalExpr *logicalExpr ); |
---|
64 | virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
65 | virtual void visit( CommaExpr *commaExpr ); |
---|
66 | virtual void visit( TupleExpr *tupleExpr ); |
---|
67 | virtual void visit( TypeExpr *typeExpr ); |
---|
68 | |
---|
69 | //*** Statements |
---|
70 | virtual void visit( CompoundStmt * ); |
---|
71 | virtual void visit( ExprStmt * ); |
---|
72 | virtual void visit( IfStmt * ); |
---|
73 | virtual void visit( SwitchStmt * ); |
---|
74 | virtual void visit( CaseStmt * ); |
---|
75 | virtual void visit( BranchStmt * ); |
---|
76 | virtual void visit( ReturnStmt * ); |
---|
77 | virtual void visit( WhileStmt * ); |
---|
78 | virtual void visit( ForStmt * ); |
---|
79 | virtual void visit( NullStmt * ); |
---|
80 | virtual void visit( DeclStmt * ); |
---|
81 | |
---|
82 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); |
---|
83 | private: |
---|
84 | int cur_indent; |
---|
85 | bool insideFunction; |
---|
86 | std::ostream &output; |
---|
87 | |
---|
88 | static std::string printLabels ( std::list < Label > & ); |
---|
89 | void handleStorageClass( Declaration *decl ); |
---|
90 | void handleAggregate( AggregateDecl *aggDecl ); |
---|
91 | void handleTypedef( NamedTypeDecl *namedType ); |
---|
92 | |
---|
93 | }; |
---|
94 | |
---|
95 | template< class Iterator > |
---|
96 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { |
---|
97 | if ( begin == end ) return; |
---|
98 | |
---|
99 | for ( ;; ) { |
---|
100 | (*begin++)->accept( *this ); |
---|
101 | if ( begin == end ) return; |
---|
102 | output << ", "; |
---|
103 | } // for |
---|
104 | } |
---|
105 | |
---|
106 | inline bool doSemicolon( Declaration* decl ) { |
---|
107 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { |
---|
108 | return ! func->get_statements(); |
---|
109 | } // if |
---|
110 | return true; |
---|
111 | } |
---|
112 | } // namespace CodeGen |
---|
113 | |
---|
114 | #endif // CODEGENV_H |
---|
115 | |
---|
116 | // Local Variables: // |
---|
117 | // tab-width: 4 // |
---|
118 | // mode: c++ // |
---|
119 | // compile-command: "make install" // |
---|
120 | // End: // |
---|