source: src/CodeGen/CodeGenerator.h @ 9dc31c10

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 9dc31c10 was 92fea32, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Add codegen for _Static_assert

  • Property mode set to 100644
File size: 5.9 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                void postvisit( StaticAssertDecl * assertDecl );
50
51                //*** Initializer
52                void postvisit( Designation * );
53                void postvisit( SingleInit * );
54                void postvisit( ListInit * );
55                void postvisit( ConstructorInit * );
56
57                //*** Constant
58                void postvisit( Constant * );
59
60                //*** Expression
61                void postvisit( ApplicationExpr *applicationExpr );
62                void postvisit( UntypedExpr *untypedExpr );
63                void postvisit( RangeExpr * rangeExpr );
64                void postvisit( NameExpr *nameExpr );
65                void postvisit( AddressExpr *addressExpr );
66                void postvisit( LabelAddressExpr *addressExpr );
67                void postvisit( CastExpr *castExpr );
68                void postvisit( VirtualCastExpr *castExpr );
69                void postvisit( UntypedMemberExpr *memberExpr );
70                void postvisit( MemberExpr *memberExpr );
71                void postvisit( VariableExpr *variableExpr );
72                void postvisit( ConstantExpr *constantExpr );
73                void postvisit( SizeofExpr *sizeofExpr );
74                void postvisit( AlignofExpr *alignofExpr );
75                void postvisit( UntypedOffsetofExpr *offsetofExpr );
76                void postvisit( OffsetofExpr *offsetofExpr );
77                void postvisit( OffsetPackExpr *offsetPackExpr );
78                void postvisit( LogicalExpr *logicalExpr );
79                void postvisit( ConditionalExpr *conditionalExpr );
80                void postvisit( CommaExpr *commaExpr );
81                void postvisit( CompoundLiteralExpr *compLitExpr );
82                void postvisit( UniqueExpr * );
83                void postvisit( TupleAssignExpr * tupleExpr );
84                void postvisit( UntypedTupleExpr *tupleExpr );
85                void postvisit( TupleExpr *tupleExpr );
86                void postvisit( TupleIndexExpr * tupleExpr );
87                void postvisit( TypeExpr *typeExpr );
88                void postvisit( AsmExpr * );
89                void postvisit( StmtExpr * );
90                void postvisit( ConstructorExpr * );
91                void postvisit( DeletedExpr * );
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( CatchStmt * );
105                void postvisit( WaitForStmt * );
106                void postvisit( WithStmt * );
107                void postvisit( WhileStmt * );
108                void postvisit( ForStmt * );
109                void postvisit( NullStmt * );
110                void postvisit( DeclStmt * );
111                void postvisit( ImplicitCtorDtorStmt * );
112
113                void genAttributes( std::list< Attribute * > & attributes );
114
115                template< class Iterator > void genCommaList( Iterator begin, Iterator end );
116
117                struct LabelPrinter {
118                        LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {}
119                        LabelPrinter & operator()( std::list< Label > & l );
120                        CodeGenerator & cg;
121                        std::list< Label > * labels;
122                };
123
124                void asmName( DeclarationWithType *decl );
125
126                void extension( Expression *expr );
127                void extension( Declaration *decl );
128
129                void updateLocation( BaseSyntaxNode const * to );
130                struct LineEnder {
131                        CodeGenerator & cg;
132                        LineEnder( CodeGenerator & cg ) : cg( cg ) {}
133                        std::ostream & operator()(std::ostream &) const;
134                };
135          private:
136                Indenter indent;
137                std::ostream & output;
138                LabelPrinter printLabels;
139                bool pretty = false;  // pretty print
140                bool genC = false;    // true if output has to be C code
141                bool lineMarks = false;
142        public:
143                LineEnder endl;
144        private:
145
146                CodeLocation currentLocation;
147                void updateLocation( CodeLocation const & to );
148
149                void handleStorageClass( DeclarationWithType *decl );
150                void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
151                void handleTypedef( NamedTypeDecl *namedType );
152                std::string mangleName( DeclarationWithType * decl );
153        }; // CodeGenerator
154
155        template< class Iterator >
156        void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
157          if ( begin == end ) return;
158                for ( ;; ) {
159                        (*begin++)->accept( *visitor );
160                  if ( begin == end ) break;
161                        output << ", ";                                                         // separator
162                } // for
163        } // genCommaList
164
165        inline bool doSemicolon( Declaration* decl ) {
166                if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
167                        return ! func->get_statements();
168                } // if
169                return true;
170        } // doSemicolon
171
172        /// returns C-compatible name of declaration
173        std::string genName( DeclarationWithType * decl );
174
175        inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
176                return endl( os );
177        }
178} // namespace CodeGen
179
180// Local Variables: //
181// tab-width: 4 //
182// mode: c++ //
183// compile-command: "make install" //
184// End: //
Note: See TracBrowser for help on using the repository browser.