source: src/CodeGen/CodeGenerator.h @ 5b544a6

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 5b544a6 was 60a8062, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

rewrite most of OperatorTable? and change caller modules to use new interface

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[51587aa]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//
[7baed7d]7// CodeGenerator.h --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[60a8062]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Feb 16 03:58:31 2020
13// Update Count     : 62
[51587aa]14//
15
[6b0b624]16#pragma once
[51b7345]17
[bf2438c]18#include <list>                   // for list
19#include <ostream>                // for ostream, operator<<
20#include <string>                 // for string
[51b7345]21
[42a36d9]22#include "CodeGen/Options.h"      // for Options
[e6cee92]23#include "Common/Indenter.h"      // for Indenter
[9857e8d]24#include "Common/PassVisitor.h"   // for PassVisitor
[bf2438c]25#include "SynTree/Declaration.h"  // for DeclarationWithType (ptr only), Fun...
26#include "SynTree/Visitor.h"      // for Visitor
27#include "SynTree/SynTree.h"      // for Visitor Nodes
[c850687]28
[51b7345]29namespace CodeGen {
[5f08961d]30        struct CodeGenerator : public WithShortCircuiting, public WithGuards, public WithVisitorRef<CodeGenerator> {
[60a8062]31                static int tabsize;
[51587aa]32
[5f08961d]33                CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false, bool printExprTypes = false );
[42a36d9]34                CodeGenerator( std::ostream &os, const Options &options );
[51587aa]35
[9857e8d]36                //*** Turn off visit_children for all nodes
37                void previsit( BaseSyntaxNode * );
38
39                //*** Error for unhandled node types
40                void postvisit( BaseSyntaxNode * );
41
[5f08961d]42                //*** print type for all expressions
43                void previsit( Expression * node );
44
[51587aa]45                //*** Declaration
[9857e8d]46                void postvisit( StructDecl * );
47                void postvisit( FunctionDecl * );
48                void postvisit( ObjectDecl * );
[92fea32]49                void postvisit( UnionDecl * aggregateDecl );
50                void postvisit( EnumDecl * aggregateDecl );
51                void postvisit( TraitDecl * aggregateDecl );
52                void postvisit( TypedefDecl * typeDecl );
53                void postvisit( TypeDecl * typeDecl );
54                void postvisit( StaticAssertDecl * assertDecl );
[51587aa]55
56                //*** Initializer
[9857e8d]57                void postvisit( Designation * );
58                void postvisit( SingleInit * );
59                void postvisit( ListInit * );
60                void postvisit( ConstructorInit * );
[51587aa]61
62                //*** Constant
[9857e8d]63                void postvisit( Constant * );
[51587aa]64
65                //*** Expression
[9857e8d]66                void postvisit( ApplicationExpr *applicationExpr );
67                void postvisit( UntypedExpr *untypedExpr );
68                void postvisit( RangeExpr * rangeExpr );
69                void postvisit( NameExpr *nameExpr );
70                void postvisit( AddressExpr *addressExpr );
71                void postvisit( LabelAddressExpr *addressExpr );
72                void postvisit( CastExpr *castExpr );
[da9d79b]73                void postvisit( KeywordCastExpr * castExpr );
[9857e8d]74                void postvisit( VirtualCastExpr *castExpr );
75                void postvisit( UntypedMemberExpr *memberExpr );
76                void postvisit( MemberExpr *memberExpr );
77                void postvisit( VariableExpr *variableExpr );
78                void postvisit( ConstantExpr *constantExpr );
79                void postvisit( SizeofExpr *sizeofExpr );
80                void postvisit( AlignofExpr *alignofExpr );
81                void postvisit( UntypedOffsetofExpr *offsetofExpr );
82                void postvisit( OffsetofExpr *offsetofExpr );
83                void postvisit( OffsetPackExpr *offsetPackExpr );
84                void postvisit( LogicalExpr *logicalExpr );
85                void postvisit( ConditionalExpr *conditionalExpr );
86                void postvisit( CommaExpr *commaExpr );
87                void postvisit( CompoundLiteralExpr *compLitExpr );
88                void postvisit( UniqueExpr * );
89                void postvisit( TupleAssignExpr * tupleExpr );
90                void postvisit( UntypedTupleExpr *tupleExpr );
91                void postvisit( TupleExpr *tupleExpr );
92                void postvisit( TupleIndexExpr * tupleExpr );
93                void postvisit( TypeExpr *typeExpr );
94                void postvisit( AsmExpr * );
95                void postvisit( StmtExpr * );
[4e8949f]96                void postvisit( ConstructorExpr * );
[44b4114]97                void postvisit( DeletedExpr * );
[0f79853]98                void postvisit( DefaultArgExpr * );
[d807ca28]99                void postvisit( GenericExpr * );
[51587aa]100
101                //*** Statements
[9857e8d]102                void postvisit( CompoundStmt * );
103                void postvisit( ExprStmt * );
104                void postvisit( AsmStmt * );
[cc32d83]105                void postvisit( DirectiveStmt * );
[60a8062]106                void postvisit( AsmDecl * );                                    // special: statement in declaration context
[9857e8d]107                void postvisit( IfStmt * );
108                void postvisit( SwitchStmt * );
109                void postvisit( CaseStmt * );
110                void postvisit( BranchStmt * );
111                void postvisit( ReturnStmt * );
112                void postvisit( ThrowStmt * );
[e4ea10b7]113                void postvisit( CatchStmt * );
114                void postvisit( WaitForStmt * );
[55d6e8de]115                void postvisit( WithStmt * );
[9857e8d]116                void postvisit( WhileStmt * );
117                void postvisit( ForStmt * );
118                void postvisit( NullStmt * );
119                void postvisit( DeclStmt * );
120                void postvisit( ImplicitCtorDtorStmt * );
[7baed7d]121
122                void genAttributes( std::list< Attribute * > & attributes );
[51587aa]123
124                template< class Iterator > void genCommaList( Iterator begin, Iterator end );
[cda48b6]125
[888cbe4]126                struct LabelPrinter {
127                        LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {}
128                        LabelPrinter & operator()( std::list< Label > & l );
129                        CodeGenerator & cg;
130                        std::list< Label > * labels;
[cda48b6]131                };
[e04ef3a]132
[58dd019]133                void asmName( DeclarationWithType *decl );
134
[8e9cbb2]135                void extension( Expression *expr );
136                void extension( Declaration *decl );
[0dd18fd]137
138                void updateLocation( BaseSyntaxNode const * to );
[d22e90f]139                struct LineEnder {
140                        CodeGenerator & cg;
141                        LineEnder( CodeGenerator & cg ) : cg( cg ) {}
142                        std::ostream & operator()(std::ostream &) const;
143                };
[51587aa]144          private:
[cda48b6]145                Indenter indent;
[d22e90f]146                std::ostream & output;
[888cbe4]147                LabelPrinter printLabels;
[42a36d9]148                Options options;
[60a8062]149          public:
[d22e90f]150                LineEnder endl;
[60a8062]151          private:
[51587aa]152
[8bafacc]153                CodeLocation currentLocation;
154                void updateLocation( CodeLocation const & to );
155
[dd020c0]156                void handleStorageClass( DeclarationWithType *decl );
[5f642e38]157                void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
[51587aa]158                void handleTypedef( NamedTypeDecl *namedType );
[486341f]159                std::string mangleName( DeclarationWithType * decl );
[66d12f7]160        }; // CodeGenerator
[7baed7d]161
[51587aa]162        template< class Iterator >
[6c4ff37]163        void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
[60a8062]164                if ( begin == end ) return;
[51587aa]165                for ( ;; ) {
[9857e8d]166                        (*begin++)->accept( *visitor );
[60a8062]167                        if ( begin == end ) break;
[44a81853]168                        output << ", ";                                                         // separator
[51587aa]169                } // for
[44a81853]170        } // genCommaList
[7baed7d]171
[51587aa]172        inline bool doSemicolon( Declaration* decl ) {
173                if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
174                        return ! func->get_statements();
175                } // if
176                return true;
[44a81853]177        } // doSemicolon
[9facf3b]178
179        /// returns C-compatible name of declaration
180        std::string genName( DeclarationWithType * decl );
[d22e90f]181
182        inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
183                return endl( os );
184        }
[51b7345]185} // namespace CodeGen
186
[51587aa]187// Local Variables: //
188// tab-width: 4 //
189// mode: c++ //
190// compile-command: "make install" //
191// End: //
Note: See TracBrowser for help on using the repository browser.