source: src/CodeGen/CodeGenerator.h @ 4e8949f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4e8949f was 4e8949f, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Handle ConstructorExpr? in CodeGenerator?

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