source: src/CodeGen/CodeGenerator.hpp@ 8655363

Last change on this file since 8655363 was 83fd57d, checked in by Andrew Beach <ajbeach@…>, 22 months ago

Removed 'New' suffixes, they are no longer needed for disambiguation.

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