source: src/CodeGen/CodeGenerator.h@ a55472cc

ADT ast-experimental
Last change on this file since a55472cc 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
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 );
55 void postvisit( TraitDecl * aggregateDecl );
56 void postvisit( TypedefDecl * typeDecl );
57 void postvisit( TypeDecl * typeDecl );
58 void postvisit( StaticAssertDecl * assertDecl );
[51587aa]59
60 //*** Initializer
[9857e8d]61 void postvisit( Designation * );
62 void postvisit( SingleInit * );
63 void postvisit( ListInit * );
64 void postvisit( ConstructorInit * );
[51587aa]65
66 //*** Constant
[9857e8d]67 void postvisit( Constant * );
[51587aa]68
69 //*** Expression
[9857e8d]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 );
[da9d79b]77 void postvisit( KeywordCastExpr * castExpr );
[9857e8d]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 );
[6e50a6b]98 void postvisit( DimensionExpr *dimensionExpr );
[9857e8d]99 void postvisit( AsmExpr * );
100 void postvisit( StmtExpr * );
[4e8949f]101 void postvisit( ConstructorExpr * );
[44b4114]102 void postvisit( DeletedExpr * );
[0f79853]103 void postvisit( DefaultArgExpr * );
[d807ca28]104 void postvisit( GenericExpr * );
[b0d9ff7]105 void postvisit( QualifiedNameExpr *);
[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 );
[66d12f7]168 }; // CodeGenerator
[7baed7d]169
[51587aa]170 template< class Iterator >
[6c4ff37]171 void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
[60a8062]172 if ( begin == end ) return;
[51587aa]173 for ( ;; ) {
[9857e8d]174 (*begin++)->accept( *visitor );
[60a8062]175 if ( begin == end ) break;
[44a81853]176 output << ", "; // separator
[51587aa]177 } // for
[44a81853]178 } // genCommaList
[7baed7d]179
[51587aa]180 inline bool doSemicolon( Declaration* decl ) {
181 if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
182 return ! func->get_statements();
183 } // if
184 return true;
[44a81853]185 } // doSemicolon
[9facf3b]186
187 /// returns C-compatible name of declaration
188 std::string genName( DeclarationWithType * decl );
[1931bb01]189 std::string genName( ast::DeclWithType const * decl );
[d22e90f]190
191 inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
192 return endl( os );
193 }
[51b73452]194} // namespace CodeGen
195
[51587aa]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.