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 | |
---|
26 | namespace CodeGen { |
---|
27 | |
---|
28 | struct 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::StructDecl const * ); |
---|
45 | void postvisit( ast::FunctionDecl const * ); |
---|
46 | // Yes, there is one visit that does modify the ast. |
---|
47 | ast::ObjectDecl const * postvisit( ast::ObjectDecl 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::UntypedOffsetofExpr const * ); |
---|
76 | void postvisit( ast::OffsetofExpr const * ); |
---|
77 | void postvisit( ast::OffsetPackExpr const * ); |
---|
78 | void postvisit( ast::LogicalExpr const * ); |
---|
79 | void postvisit( ast::ConditionalExpr const * ); |
---|
80 | void postvisit( ast::CommaExpr const * ); |
---|
81 | void postvisit( ast::CompoundLiteralExpr const * ); |
---|
82 | void postvisit( ast::UniqueExpr const * ); |
---|
83 | void postvisit( ast::TupleAssignExpr const * ); |
---|
84 | void postvisit( ast::UntypedTupleExpr const * ); |
---|
85 | void postvisit( ast::TupleExpr const * ); |
---|
86 | void postvisit( ast::TupleIndexExpr const * ); |
---|
87 | void postvisit( ast::TypeExpr const * ); |
---|
88 | void postvisit( ast::DimensionExpr const * ); |
---|
89 | void postvisit( ast::AsmExpr const * ); |
---|
90 | void postvisit( ast::StmtExpr const * ); |
---|
91 | void postvisit( ast::ConstructorExpr const * ); |
---|
92 | void postvisit( ast::DeletedExpr const * ); |
---|
93 | void postvisit( ast::DefaultArgExpr const * ); |
---|
94 | void postvisit( ast::GenericExpr const * ); |
---|
95 | |
---|
96 | void postvisit( ast::CompoundStmt const * ); |
---|
97 | void postvisit( ast::ExprStmt const * ); |
---|
98 | void postvisit( ast::AsmStmt const * ); |
---|
99 | void postvisit( ast::DirectiveStmt const* ); |
---|
100 | void postvisit( ast::AsmDecl const * ); |
---|
101 | void postvisit( ast::DirectiveDecl const * ); |
---|
102 | void postvisit( ast::IfStmt const * ); |
---|
103 | void postvisit( ast::SwitchStmt const * ); |
---|
104 | void postvisit( ast::CaseClause const * ); |
---|
105 | void postvisit( ast::BranchStmt const * ); |
---|
106 | void postvisit( ast::ReturnStmt const * ); |
---|
107 | void postvisit( ast::ThrowStmt const * ); |
---|
108 | void postvisit( ast::CatchClause const * ); |
---|
109 | void postvisit( ast::WaitForStmt const * ); |
---|
110 | void postvisit( ast::WithStmt const * ); |
---|
111 | void postvisit( ast::WhileDoStmt const * ); |
---|
112 | void postvisit( ast::ForStmt const * ); |
---|
113 | void postvisit( ast::NullStmt const * ); |
---|
114 | void postvisit( ast::DeclStmt const * ); |
---|
115 | void postvisit( ast::ImplicitCtorDtorStmt const * ); |
---|
116 | void postvisit( ast::MutexStmt const * stmt ); |
---|
117 | |
---|
118 | private: |
---|
119 | /// Custom local implementation of endl that updates print location. |
---|
120 | struct LineEnder { |
---|
121 | CodeGenerator & cg; |
---|
122 | LineEnder( CodeGenerator & cg ) : cg( cg ) {} |
---|
123 | std::ostream & operator()( std::ostream & ) const; |
---|
124 | }; |
---|
125 | friend std::ostream & operator<<( std::ostream & os, const LineEnder & e ) { |
---|
126 | return e( os ); |
---|
127 | } |
---|
128 | |
---|
129 | /// Wrapper class to help print vectors of Labels. |
---|
130 | struct LabelPrinter { |
---|
131 | LabelPrinter( CodeGenerator & cg ) : cg( cg ), labels( nullptr ) {} |
---|
132 | LabelPrinter & operator()( std::vector<ast::Label> const & l ); |
---|
133 | std::ostream & operator()( std::ostream & ) const; |
---|
134 | CodeGenerator & cg; |
---|
135 | std::vector<ast::Label> const * labels; |
---|
136 | }; |
---|
137 | friend std::ostream & operator<<( std::ostream & os, const LabelPrinter & p ) { |
---|
138 | return p( os ); |
---|
139 | } |
---|
140 | |
---|
141 | static int tabsize; |
---|
142 | |
---|
143 | Indenter indent; |
---|
144 | std::ostream & output; |
---|
145 | Options options; |
---|
146 | LabelPrinter printLabels; |
---|
147 | |
---|
148 | CodeLocation currentLocation; |
---|
149 | void updateLocation( CodeLocation const & to ); |
---|
150 | |
---|
151 | template<typename Iterator> |
---|
152 | void genCommaList( Iterator begin, Iterator end ) { |
---|
153 | if ( begin == end ) return; |
---|
154 | while (true) { |
---|
155 | (*begin++)->accept( *visitor ); |
---|
156 | if ( begin == end ) break; |
---|
157 | output << ", "; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | public: |
---|
162 | LineEnder endl; |
---|
163 | void updateLocation( ast::ParseNode const * to ); |
---|
164 | |
---|
165 | template<typename T> |
---|
166 | void genCommaList( std::vector<ast::ptr<T>> const & range ) { |
---|
167 | genCommaList( range.begin(), range.end() ); |
---|
168 | } |
---|
169 | |
---|
170 | void genAttributes( std::vector<ast::ptr<ast::Attribute>> const & ); |
---|
171 | |
---|
172 | private: |
---|
173 | void asmName( ast::DeclWithType const * decl ); |
---|
174 | void extension( ast::Decl const * ); |
---|
175 | void extension( ast::Expr const * ); |
---|
176 | |
---|
177 | void handleStorageClass( ast::DeclWithType const * decl ); |
---|
178 | void handleAggregate( ast::AggregateDecl const *, const std::string & ); |
---|
179 | void handleTypedef( ast::NamedTypeDecl const * type ); |
---|
180 | std::string mangleName( ast::DeclWithType const * decl ); |
---|
181 | }; |
---|
182 | |
---|
183 | inline bool doSemicolon( ast::Decl const * decl ) { |
---|
184 | if ( auto func = dynamic_cast<ast::FunctionDecl const *>( decl ) ) { |
---|
185 | return !func->stmts; |
---|
186 | } |
---|
187 | return true; |
---|
188 | } |
---|
189 | |
---|
190 | /// Returns the C-compatible name of the declaration. |
---|
191 | std::string genName( ast::DeclWithType const * decl ); |
---|
192 | |
---|
193 | } // namespace CodeGen |
---|