source: src/CodeGen/CodeGenerator.hpp @ 61e362f

Last change on this file since 61e362f was f988834, checked in by JiadaL <j82liang@…>, 5 months ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 6.4 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#include "Common/Indenter.h"     // 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::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::EnumPosExpr 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::CatchClause const * );
110        void postvisit( ast::WaitForStmt const * );
111        void postvisit( ast::WithStmt const * );
112        void postvisit( ast::WhileDoStmt const * );
113        void postvisit( ast::ForStmt const * );
114        void postvisit( ast::NullStmt const * );
115        void postvisit( ast::DeclStmt const * );
116        void postvisit( ast::ImplicitCtorDtorStmt const * );
117        void postvisit( ast::MutexStmt const * stmt );
118
119private:
120        /// Custom local implementation of endl that updates print location.
121        struct LineEnder {
122                CodeGenerator & cg;
123                LineEnder( CodeGenerator & cg ) : cg( cg ) {}
124                std::ostream & operator()( std::ostream & ) const;
125        };
126        friend std::ostream & operator<<( std::ostream & os, const LineEnder & e ) {
127                return e( os );
128        }
129
130        /// Wrapper class to help print vectors of Labels.
131        struct LabelPrinter {
132                LabelPrinter( CodeGenerator & cg ) : cg( cg ), labels( nullptr ) {}
133                LabelPrinter & operator()( std::vector<ast::Label> const & l );
134                std::ostream & operator()( std::ostream & ) const;
135                CodeGenerator & cg;
136                std::vector<ast::Label> const * labels;
137        };
138        friend std::ostream & operator<<( std::ostream & os, const LabelPrinter & p ) {
139                return p( os );
140        }
141
142        static int tabsize;
143
144        Indenter indent;
145        std::ostream & output;
146        Options options;
147        LabelPrinter printLabels;
148
149        CodeLocation currentLocation;
150        void updateLocation( CodeLocation const & to );
151
152        template<typename Iterator>
153        void genCommaList( Iterator begin, Iterator end ) {
154                if ( begin == end ) return;
155                while (true) {
156                        (*begin++)->accept( *visitor );
157                        if ( begin == end ) break;
158                        output << ", ";
159                }
160        }
161
162public:
163        LineEnder endl;
164        void updateLocation( ast::ParseNode const * to );
165
166        template<typename T>
167        void genCommaList( std::vector<ast::ptr<T>> const & range ) {
168                genCommaList( range.begin(), range.end() );
169        }
170
171        void genAttributes( std::vector<ast::ptr<ast::Attribute>> const & );
172
173private:
174        void asmName( ast::DeclWithType const * decl );
175        void extension( ast::Decl const * );
176        void extension( ast::Expr const * );
177
178        void handleStorageClass( ast::DeclWithType const * decl );
179        void handleAggregate( ast::AggregateDecl const *, const std::string & );
180        void handleTypedef( ast::NamedTypeDecl const * type );
181        std::string mangleName( ast::DeclWithType const * decl );
182};
183
184inline bool doSemicolon( ast::Decl const * decl ) {
185        if ( auto func = dynamic_cast<ast::FunctionDecl const *>( decl ) ) {
186                return !func->stmts;
187        }
188        return true;
189}
190
191/// Returns the C-compatible name of the declaration.
192std::string genName( ast::DeclWithType const * decl );
193
194} // namespace CodeGen
Note: See TracBrowser for help on using the repository browser.