source: src/CodeGen/CodeGenerator.h@ 12df6fe

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 12df6fe was b0d9ff7, checked in by JiadaL <j82liang@…>, 3 years ago

Fix up the QualifiedNameExpr. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

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