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 : Andrew Beach |
---|
12 | // Last Modified On : Fri Aug 18 15:40:00 2017 |
---|
13 | // Update Count : 56 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <list> // for list |
---|
19 | #include <ostream> // for ostream, operator<< |
---|
20 | #include <string> // for string |
---|
21 | |
---|
22 | #include "Common/Indenter.h" // for Indenter |
---|
23 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
24 | #include "SynTree/Declaration.h" // for DeclarationWithType (ptr only), Fun... |
---|
25 | #include "SynTree/Visitor.h" // for Visitor |
---|
26 | #include "SynTree/SynTree.h" // for Visitor Nodes |
---|
27 | |
---|
28 | namespace CodeGen { |
---|
29 | struct CodeGenerator : public WithShortCircuiting, public WithVisitorRef<CodeGenerator> { |
---|
30 | static int tabsize; |
---|
31 | |
---|
32 | CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false ); |
---|
33 | |
---|
34 | //*** Turn off visit_children for all nodes |
---|
35 | void previsit( BaseSyntaxNode * ); |
---|
36 | |
---|
37 | //*** Error for unhandled node types |
---|
38 | void postvisit( BaseSyntaxNode * ); |
---|
39 | |
---|
40 | //*** Declaration |
---|
41 | void postvisit( StructDecl * ); |
---|
42 | void postvisit( FunctionDecl * ); |
---|
43 | void postvisit( ObjectDecl * ); |
---|
44 | void postvisit( UnionDecl *aggregateDecl ); |
---|
45 | void postvisit( EnumDecl *aggregateDecl ); |
---|
46 | void postvisit( TraitDecl *aggregateDecl ); |
---|
47 | void postvisit( TypedefDecl *typeDecl ); |
---|
48 | void postvisit( TypeDecl *typeDecl ); |
---|
49 | |
---|
50 | //*** Initializer |
---|
51 | void postvisit( Designation * ); |
---|
52 | void postvisit( SingleInit * ); |
---|
53 | void postvisit( ListInit * ); |
---|
54 | void postvisit( ConstructorInit * ); |
---|
55 | |
---|
56 | //*** Constant |
---|
57 | void postvisit( Constant * ); |
---|
58 | |
---|
59 | //*** Expression |
---|
60 | void postvisit( ApplicationExpr *applicationExpr ); |
---|
61 | void postvisit( UntypedExpr *untypedExpr ); |
---|
62 | void postvisit( RangeExpr * rangeExpr ); |
---|
63 | void postvisit( NameExpr *nameExpr ); |
---|
64 | void postvisit( AddressExpr *addressExpr ); |
---|
65 | void postvisit( LabelAddressExpr *addressExpr ); |
---|
66 | void postvisit( CastExpr *castExpr ); |
---|
67 | void postvisit( VirtualCastExpr *castExpr ); |
---|
68 | void postvisit( UntypedMemberExpr *memberExpr ); |
---|
69 | void postvisit( MemberExpr *memberExpr ); |
---|
70 | void postvisit( VariableExpr *variableExpr ); |
---|
71 | void postvisit( ConstantExpr *constantExpr ); |
---|
72 | void postvisit( SizeofExpr *sizeofExpr ); |
---|
73 | void postvisit( AlignofExpr *alignofExpr ); |
---|
74 | void postvisit( UntypedOffsetofExpr *offsetofExpr ); |
---|
75 | void postvisit( OffsetofExpr *offsetofExpr ); |
---|
76 | void postvisit( OffsetPackExpr *offsetPackExpr ); |
---|
77 | void postvisit( LogicalExpr *logicalExpr ); |
---|
78 | void postvisit( ConditionalExpr *conditionalExpr ); |
---|
79 | void postvisit( CommaExpr *commaExpr ); |
---|
80 | void postvisit( CompoundLiteralExpr *compLitExpr ); |
---|
81 | void postvisit( UniqueExpr * ); |
---|
82 | void postvisit( TupleAssignExpr * tupleExpr ); |
---|
83 | void postvisit( UntypedTupleExpr *tupleExpr ); |
---|
84 | void postvisit( TupleExpr *tupleExpr ); |
---|
85 | void postvisit( TupleIndexExpr * tupleExpr ); |
---|
86 | void postvisit( TypeExpr *typeExpr ); |
---|
87 | void postvisit( AsmExpr * ); |
---|
88 | void postvisit( StmtExpr * ); |
---|
89 | void postvisit( ConstructorExpr * ); |
---|
90 | |
---|
91 | //*** Statements |
---|
92 | void postvisit( CompoundStmt * ); |
---|
93 | void postvisit( ExprStmt * ); |
---|
94 | void postvisit( AsmStmt * ); |
---|
95 | void postvisit( AsmDecl * ); // special: statement in declaration context |
---|
96 | void postvisit( IfStmt * ); |
---|
97 | void postvisit( SwitchStmt * ); |
---|
98 | void postvisit( CaseStmt * ); |
---|
99 | void postvisit( BranchStmt * ); |
---|
100 | void postvisit( ReturnStmt * ); |
---|
101 | void postvisit( ThrowStmt * ); |
---|
102 | void postvisit( WhileStmt * ); |
---|
103 | void postvisit( ForStmt * ); |
---|
104 | void postvisit( NullStmt * ); |
---|
105 | void postvisit( DeclStmt * ); |
---|
106 | void postvisit( ImplicitCtorDtorStmt * ); |
---|
107 | |
---|
108 | void genAttributes( std::list< Attribute * > & attributes ); |
---|
109 | |
---|
110 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); |
---|
111 | |
---|
112 | struct LabelPrinter { |
---|
113 | LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {} |
---|
114 | LabelPrinter & operator()( std::list< Label > & l ); |
---|
115 | CodeGenerator & cg; |
---|
116 | std::list< Label > * labels; |
---|
117 | }; |
---|
118 | |
---|
119 | void asmName( DeclarationWithType *decl ); |
---|
120 | |
---|
121 | void extension( Expression *expr ); |
---|
122 | void extension( Declaration *decl ); |
---|
123 | |
---|
124 | void updateLocation( BaseSyntaxNode const * to ); |
---|
125 | struct LineEnder { |
---|
126 | CodeGenerator & cg; |
---|
127 | LineEnder( CodeGenerator & cg ) : cg( cg ) {} |
---|
128 | std::ostream & operator()(std::ostream &) const; |
---|
129 | }; |
---|
130 | private: |
---|
131 | Indenter indent; |
---|
132 | std::ostream & output; |
---|
133 | LabelPrinter printLabels; |
---|
134 | bool pretty = false; // pretty print |
---|
135 | bool genC = false; // true if output has to be C code |
---|
136 | bool lineMarks = false; |
---|
137 | public: |
---|
138 | LineEnder endl; |
---|
139 | private: |
---|
140 | |
---|
141 | CodeLocation currentLocation; |
---|
142 | void updateLocation( CodeLocation const & to ); |
---|
143 | |
---|
144 | void handleStorageClass( DeclarationWithType *decl ); |
---|
145 | void handleAggregate( AggregateDecl *aggDecl, const std::string & kind ); |
---|
146 | void handleTypedef( NamedTypeDecl *namedType ); |
---|
147 | std::string mangleName( DeclarationWithType * decl ); |
---|
148 | }; // CodeGenerator |
---|
149 | |
---|
150 | template< class Iterator > |
---|
151 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { |
---|
152 | if ( begin == end ) return; |
---|
153 | for ( ;; ) { |
---|
154 | (*begin++)->accept( *visitor ); |
---|
155 | if ( begin == end ) break; |
---|
156 | output << ", "; // separator |
---|
157 | } // for |
---|
158 | } // genCommaList |
---|
159 | |
---|
160 | inline bool doSemicolon( Declaration* decl ) { |
---|
161 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { |
---|
162 | return ! func->get_statements(); |
---|
163 | } // if |
---|
164 | return true; |
---|
165 | } // doSemicolon |
---|
166 | |
---|
167 | /// returns C-compatible name of declaration |
---|
168 | std::string genName( DeclarationWithType * decl ); |
---|
169 | |
---|
170 | inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) { |
---|
171 | return endl( os ); |
---|
172 | } |
---|
173 | } // namespace CodeGen |
---|
174 | |
---|
175 | // Local Variables: // |
---|
176 | // tab-width: 4 // |
---|
177 | // mode: c++ // |
---|
178 | // compile-command: "make install" // |
---|
179 | // End: // |
---|