source: src/CodeGen/CodeGenerator.h@ 3a513d89

ADT
Last change on this file since 3a513d89 was f4e01f1, checked in by JiadaL <j82liang@…>, 2 years ago

Save progress

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