source: src/CodeGen/CodeGenerator.h@ 0690350

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0690350 was 55d6e8de, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add CodeGen for WithStmt

  • Property mode set to 100644
File size: 5.8 KB
Line 
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
28namespace 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( CatchStmt * );
103 void postvisit( WaitForStmt * );
104 void postvisit( WithStmt * );
105 void postvisit( WhileStmt * );
106 void postvisit( ForStmt * );
107 void postvisit( NullStmt * );
108 void postvisit( DeclStmt * );
109 void postvisit( ImplicitCtorDtorStmt * );
110
111 void genAttributes( std::list< Attribute * > & attributes );
112
113 template< class Iterator > void genCommaList( Iterator begin, Iterator end );
114
115 struct LabelPrinter {
116 LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {}
117 LabelPrinter & operator()( std::list< Label > & l );
118 CodeGenerator & cg;
119 std::list< Label > * labels;
120 };
121
122 void asmName( DeclarationWithType *decl );
123
124 void extension( Expression *expr );
125 void extension( Declaration *decl );
126
127 void updateLocation( BaseSyntaxNode const * to );
128 struct LineEnder {
129 CodeGenerator & cg;
130 LineEnder( CodeGenerator & cg ) : cg( cg ) {}
131 std::ostream & operator()(std::ostream &) const;
132 };
133 private:
134 Indenter indent;
135 std::ostream & output;
136 LabelPrinter printLabels;
137 bool pretty = false; // pretty print
138 bool genC = false; // true if output has to be C code
139 bool lineMarks = false;
140 public:
141 LineEnder endl;
142 private:
143
144 CodeLocation currentLocation;
145 void updateLocation( CodeLocation const & to );
146
147 void handleStorageClass( DeclarationWithType *decl );
148 void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
149 void handleTypedef( NamedTypeDecl *namedType );
150 std::string mangleName( DeclarationWithType * decl );
151 }; // CodeGenerator
152
153 template< class Iterator >
154 void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
155 if ( begin == end ) return;
156 for ( ;; ) {
157 (*begin++)->accept( *visitor );
158 if ( begin == end ) break;
159 output << ", "; // separator
160 } // for
161 } // genCommaList
162
163 inline bool doSemicolon( Declaration* decl ) {
164 if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
165 return ! func->get_statements();
166 } // if
167 return true;
168 } // doSemicolon
169
170 /// returns C-compatible name of declaration
171 std::string genName( DeclarationWithType * decl );
172
173 inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
174 return endl( os );
175 }
176} // namespace CodeGen
177
178// Local Variables: //
179// tab-width: 4 //
180// mode: c++ //
181// compile-command: "make install" //
182// End: //
Note: See TracBrowser for help on using the repository browser.