source: src/CodeGen/CodeGenerator.hpp@ 42cd67b8

Last change on this file since 42cd67b8 was e48aca8, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Added support for code generation of the CountofExpr. This doesn't do anything to final output but is useful for debugging.

  • Property mode set to 100644
File size: 6.8 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.hpp --
8//
9// Author : Andrew Beach
10// Created On : Tue Oct 17 15:54:00 2023
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Oct 25 17:56:00 2023
13// Update Count : 0
14//
15
16#pragma once
17
18#include <ostream> // for ostream, operator<<
19
20#include "AST/Fwd.hpp"
21#include "AST/Pass.hpp" // for WithGuards, WithShortCircuiting, ...
22#include "CodeGen/Options.hpp" // for Options
23#include "Common/Indenter.hpp" // for Indenter
24
25
26namespace CodeGen {
27
28struct CodeGenerator final :
29 public ast::WithGuards,
30 public ast::WithShortCircuiting,
31 public ast::WithVisitorRef<CodeGenerator> {
32 CodeGenerator( std::ostream & out, Options const & options );
33
34 // Turn off visit_children for all nodes.
35 void previsit( ast::Node const * );
36 void previsit( ast::ParseNode const * );
37
38 // Error for unhandled node types.
39 void postvisit( ast::Node const * );
40
41 // Print type for all expressions.
42 void previsit( ast::Expr const * );
43
44 void postvisit( ast::FunctionDecl const * );
45 // Yes, there is one visit that does modify the ast.
46 ast::ObjectDecl const * postvisit( ast::ObjectDecl const * );
47 void postvisit( ast::StructDecl const * );
48 void postvisit( ast::UnionDecl const * );
49 void postvisit( ast::EnumDecl const * );
50 void postvisit( ast::TraitDecl const * );
51 void postvisit( ast::TypedefDecl const * );
52 void postvisit( ast::TypeDecl const * );
53 void postvisit( ast::StaticAssertDecl const * );
54
55 void postvisit( ast::Designation const * );
56 void postvisit( ast::SingleInit const * );
57 void postvisit( ast::ListInit const * );
58 void postvisit( ast::ConstructorInit const * );
59
60 void postvisit( ast::ApplicationExpr const * );
61 void postvisit( ast::UntypedExpr const * );
62 void postvisit( ast::RangeExpr const * );
63 void postvisit( ast::NameExpr const * );
64 void postvisit( ast::AddressExpr const * );
65 void postvisit( ast::LabelAddressExpr const * );
66 void postvisit( ast::CastExpr const * );
67 void postvisit( ast::KeywordCastExpr const * );
68 void postvisit( ast::VirtualCastExpr const * );
69 void postvisit( ast::UntypedMemberExpr const * );
70 void postvisit( ast::MemberExpr const * );
71 void postvisit( ast::VariableExpr const * );
72 void postvisit( ast::ConstantExpr const * );
73 void postvisit( ast::SizeofExpr const * );
74 void postvisit( ast::AlignofExpr const * );
75 void postvisit( ast::CountofExpr const * );
76 void postvisit( ast::UntypedOffsetofExpr const * );
77 void postvisit( ast::OffsetofExpr const * );
78 void postvisit( ast::OffsetPackExpr const * );
79 void postvisit( ast::LogicalExpr const * );
80 void postvisit( ast::ConditionalExpr const * );
81 void postvisit( ast::CommaExpr const * );
82 void postvisit( ast::CompoundLiteralExpr const * );
83 void postvisit( ast::UniqueExpr const * );
84 void postvisit( ast::TupleAssignExpr const * );
85 void postvisit( ast::UntypedTupleExpr const * );
86 void postvisit( ast::TupleExpr const * );
87 void postvisit( ast::TupleIndexExpr const * );
88 void postvisit( ast::TypeExpr const * );
89 void postvisit( ast::DimensionExpr const * );
90 void postvisit( ast::AsmExpr const * );
91 void postvisit( ast::StmtExpr const * );
92 void postvisit( ast::ConstructorExpr const * );
93 void postvisit( ast::DeletedExpr const * );
94 void postvisit( ast::DefaultArgExpr const * );
95 void postvisit( ast::GenericExpr const * );
96
97 void postvisit( ast::CompoundStmt const * );
98 void postvisit( ast::ExprStmt const * );
99 void postvisit( ast::AsmStmt const * );
100 void postvisit( ast::DirectiveStmt const* );
101 void postvisit( ast::AsmDecl const * );
102 void postvisit( ast::DirectiveDecl const * );
103 void postvisit( ast::IfStmt const * );
104 void postvisit( ast::SwitchStmt const * );
105 void postvisit( ast::CaseClause const * );
106 void postvisit( ast::BranchStmt const * );
107 void postvisit( ast::ReturnStmt const * );
108 void postvisit( ast::ThrowStmt const * );
109 void postvisit( ast::TryStmt const * );
110 void postvisit( ast::CatchClause const * );
111 void postvisit( ast::FinallyClause const * );
112 void postvisit( ast::WaitForStmt const * );
113 void postvisit( ast::WithStmt const * );
114 void postvisit( ast::WhileDoStmt const * );
115 void postvisit( ast::ForStmt const * );
116 void postvisit( ast::NullStmt const * );
117 void postvisit( ast::DeclStmt const * );
118 void postvisit( ast::ImplicitCtorDtorStmt const * );
119 void postvisit( ast::MutexStmt const * stmt );
120
121private:
122 /// Custom local implementation of endl that updates print location.
123 struct LineEnder {
124 CodeGenerator & cg;
125 LineEnder( CodeGenerator & cg ) : cg( cg ) {}
126 std::ostream & operator()( std::ostream & ) const;
127 };
128 friend std::ostream & operator<<( std::ostream & os, const LineEnder & e ) {
129 return e( os );
130 }
131
132 /// Wrapper class to help print vectors of Labels.
133 struct LabelPrinter {
134 LabelPrinter( CodeGenerator & cg ) : cg( cg ), labels( nullptr ) {}
135 LabelPrinter & operator()( std::vector<ast::Label> const & l );
136 std::ostream & operator()( std::ostream & ) const;
137 CodeGenerator & cg;
138 std::vector<ast::Label> const * labels;
139 };
140 friend std::ostream & operator<<( std::ostream & os, const LabelPrinter & p ) {
141 return p( os );
142 }
143
144 static int tabsize;
145
146 Indenter indent;
147 std::ostream & output;
148 Options options;
149 LabelPrinter printLabels;
150
151 CodeLocation currentLocation;
152 void updateLocation( CodeLocation const & to );
153
154 template<typename Iterator>
155 void genCommaList( Iterator begin, Iterator end ) {
156 if ( begin == end ) return;
157 while (true) {
158 (*begin++)->accept( *visitor );
159 if ( begin == end ) break;
160 output << ", ";
161 }
162 }
163
164public:
165 LineEnder endl;
166 void updateLocation( ast::ParseNode const * to );
167
168 template<typename T>
169 void genCommaList( std::vector<ast::ptr<T>> const & range ) {
170 genCommaList( range.begin(), range.end() );
171 }
172
173 void genAttributes( std::vector<ast::ptr<ast::Attribute>> const & );
174
175private:
176 void asmName( ast::DeclWithType const * decl );
177 void extension( ast::Decl const * );
178 void extension( ast::Expr const * );
179
180 void handleStorageClass( ast::DeclWithType const * decl );
181 void handleAggregate( ast::AggregateDecl const *, const std::string & );
182 void handleTypedef( ast::NamedTypeDecl const * type );
183 std::string mangleName( ast::DeclWithType const * decl );
184
185 bool nextVisitedNodeIsArgToIntrinsic = false;
186 bool visitingArgToIntrinsic = false;
187 void changeState_ArgToIntrinsic( bool newValue ) {
188 GuardValue( visitingArgToIntrinsic ) = nextVisitedNodeIsArgToIntrinsic;
189 GuardValue( nextVisitedNodeIsArgToIntrinsic ) = newValue;
190 }
191};
192
193inline bool doSemicolon( ast::Decl const * decl ) {
194 if ( auto func = dynamic_cast<ast::FunctionDecl const *>( decl ) ) {
195 return !func->stmts;
196 }
197 return true;
198}
199
200/// Returns the C-compatible name of the declaration.
201std::string genName( ast::DeclWithType const * decl );
202
203} // namespace CodeGen
Note: See TracBrowser for help on using the repository browser.