source: src/CodeGen/GenType.cc @ b93a3de

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since b93a3de was 9857e8d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Convert CodeGenerator? to PassVisitor?

  • Property mode set to 100644
File size: 9.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// GenType.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 17 09:02:28 2017
13// Update Count     : 22
14//
15#include "GenType.h"
16
17#include <cassert>                // for assert, assertf
18#include <list>                   // for _List_iterator, _List_const_iterator
19#include <sstream>                // for operator<<, ostringstream, basic_os...
20
21#include "CodeGenerator.h"        // for CodeGenerator
22#include "SynTree/Declaration.h"  // for DeclarationWithType
23#include "SynTree/Expression.h"   // for Expression
24#include "SynTree/Type.h"         // for PointerType, Type, FunctionType
25#include "SynTree/Visitor.h"      // for Visitor
26
27namespace CodeGen {
28        class GenType : public Visitor {
29          public:
30                GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false );
31                std::string get_typeString() const { return typeString; }
32                void set_typeString( const std::string &newValue ) { typeString = newValue; }
33
34                virtual void visit( FunctionType *funcType );
35                virtual void visit( VoidType *voidType );
36                virtual void visit( BasicType *basicType );
37                virtual void visit( PointerType *pointerType );
38                virtual void visit( ArrayType *arrayType );
39                virtual void visit( ReferenceType *refType );
40                virtual void visit( StructInstType *structInst );
41                virtual void visit( UnionInstType *unionInst );
42                virtual void visit( EnumInstType *enumInst );
43                virtual void visit( TypeInstType *typeInst );
44                virtual void visit( TupleType * tupleType );
45                virtual void visit( VarArgsType *varArgsType );
46                virtual void visit( ZeroType *zeroType );
47                virtual void visit( OneType *oneType );
48
49          private:
50                void handleQualifiers( Type *type );
51                std::string handleGeneric( ReferenceToType * refType );
52                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
53
54                std::string typeString;
55                bool pretty = false; // pretty print
56                bool genC = false;   // generating C code?
57                bool lineMarks = false;
58        };
59
60        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
61                GenType gt( baseString, pretty, genC, lineMarks );
62                std::ostringstream os;
63
64                if ( ! type->get_attributes().empty() ) {
65                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
66                        cg.pass.genAttributes( type->get_attributes() );
67                } // if
68
69                type->accept( gt );
70                return os.str() + gt.get_typeString();
71        }
72
73  std::string genPrettyType( Type * type, const std::string & baseString ) {
74        return genType( type, baseString, true, false );
75  }
76
77        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
78
79        void GenType::visit( VoidType *voidType ) {
80                typeString = "void " + typeString;
81                handleQualifiers( voidType );
82        }
83
84        void GenType::visit( BasicType *basicType ) {
85                BasicType::Kind kind = basicType->get_kind();
86                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
87                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
88                handleQualifiers( basicType );
89        }
90
91        void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
92                std::ostringstream os;
93                if ( typeString != "" ) {
94                        if ( typeString[ 0 ] == '*' ) {
95                                os << "(" << typeString << ")";
96                        } else {
97                                os << typeString;
98                        } // if
99                } // if
100                os << "[";
101
102                if ( isStatic ) {
103                        os << "static ";
104                } // if
105                if ( qualifiers.is_const ) {
106                        os << "const ";
107                } // if
108                if ( qualifiers.is_volatile ) {
109                        os << "volatile ";
110                } // if
111                if ( qualifiers.is_restrict ) {
112                        os << "__restrict ";
113                } // if
114                if ( qualifiers.is_atomic ) {
115                        os << "_Atomic ";
116                } // if
117                if ( dimension != 0 ) {
118                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
119                        dimension->accept( cg );
120                } else if ( isVarLen ) {
121                        // no dimension expression on a VLA means it came in with the * token
122                        os << "*";
123                } // if
124                os << "]";
125
126                typeString = os.str();
127
128                base->accept( *this );
129        }
130
131        void GenType::visit( PointerType *pointerType ) {
132                assert( pointerType->get_base() != 0);
133                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
134                        genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
135                } else {
136                        handleQualifiers( pointerType );
137                        if ( typeString[ 0 ] == '?' ) {
138                                typeString = "* " + typeString;
139                        } else {
140                                typeString = "*" + typeString;
141                        } // if
142                        pointerType->get_base()->accept( *this );
143                } // if
144        }
145
146        void GenType::visit( ArrayType *arrayType ) {
147                genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
148        }
149
150        void GenType::visit( ReferenceType *refType ) {
151                assert( refType->get_base() != 0);
152                assertf( ! genC, "Reference types should not reach code generation." );
153                handleQualifiers( refType );
154                typeString = "&" + typeString;
155                refType->get_base()->accept( *this );
156        }
157
158        void GenType::visit( FunctionType *funcType ) {
159                std::ostringstream os;
160
161                if ( typeString != "" ) {
162                        if ( typeString[ 0 ] == '*' ) {
163                                os << "(" << typeString << ")";
164                        } else {
165                                os << typeString;
166                        } // if
167                } // if
168
169                /************* parameters ***************/
170
171                const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
172
173                if ( pars.empty() ) {
174                        if ( funcType->get_isVarArgs() ) {
175                                os << "()";
176                        } else {
177                                os << "(void)";
178                        } // if
179                } else {
180                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
181                        os << "(" ;
182
183                        cg.pass.genCommaList( pars.begin(), pars.end() );
184
185                        if ( funcType->get_isVarArgs() ) {
186                                os << ", ...";
187                        } // if
188                        os << ")";
189                } // if
190
191                typeString = os.str();
192
193                if ( funcType->get_returnVals().size() == 0 ) {
194                        typeString = "void " + typeString;
195                } else {
196                        funcType->get_returnVals().front()->get_type()->accept( *this );
197                } // if
198
199                // add forall
200                if( ! funcType->get_forall().empty() && ! genC ) {
201                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
202                        std::ostringstream os;
203                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
204                        os << "forall(";
205                        cg.pass.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
206                        os << ")" << std::endl;
207                        typeString = os.str() + typeString;
208                }
209        }
210
211        std::string GenType::handleGeneric( ReferenceToType * refType ) {
212                if ( ! refType->get_parameters().empty() ) {
213                        std::ostringstream os;
214                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
215                        os << "(";
216                        cg.pass.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() );
217                        os << ") ";
218                        return os.str();
219                }
220                return "";
221        }
222
223        void GenType::visit( StructInstType *structInst )  {
224                typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
225                if ( genC ) typeString = "struct " + typeString;
226                handleQualifiers( structInst );
227        }
228
229        void GenType::visit( UnionInstType *unionInst ) {
230                typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
231                if ( genC ) typeString = "union " + typeString;
232                handleQualifiers( unionInst );
233        }
234
235        void GenType::visit( EnumInstType *enumInst ) {
236                typeString = enumInst->get_name() + " " + typeString;
237                if ( genC ) typeString = "enum " + typeString;
238                handleQualifiers( enumInst );
239        }
240
241        void GenType::visit( TypeInstType *typeInst ) {
242                typeString = typeInst->get_name() + " " + typeString;
243                handleQualifiers( typeInst );
244        }
245
246        void GenType::visit( TupleType * tupleType ) {
247                assertf( ! genC, "Tuple types should not reach code generation." );
248                unsigned int i = 0;
249                std::ostringstream os;
250                os << "[";
251                for ( Type * t : *tupleType ) {
252                        i++;
253                        os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", ");
254                }
255                os << "] ";
256                typeString = os.str() + typeString;
257        }
258
259        void GenType::visit( VarArgsType *varArgsType ) {
260                typeString = "__builtin_va_list " + typeString;
261                handleQualifiers( varArgsType );
262        }
263
264        void GenType::visit( ZeroType *zeroType ) {
265                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
266                typeString = (pretty ? "zero_t " : "long int ") + typeString;
267                handleQualifiers( zeroType );
268        }
269
270        void GenType::visit( OneType *oneType ) {
271                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
272                typeString = (pretty ? "one_t " : "long int ") + typeString;
273                handleQualifiers( oneType );
274        }
275
276        void GenType::handleQualifiers( Type *type ) {
277                if ( type->get_const() ) {
278                        typeString = "const " + typeString;
279                } // if
280                if ( type->get_volatile() ) {
281                        typeString = "volatile " + typeString;
282                } // if
283                if ( type->get_restrict() ) {
284                        typeString = "__restrict " + typeString;
285                } // if
286                if ( type->get_atomic() ) {
287                        typeString = "_Atomic " + typeString;
288                } // if
289                if ( type->get_lvalue() && ! genC ) {
290                        // when not generating C code, print lvalue for debugging.
291                        typeString = "lvalue " + typeString;
292                }
293        }
294} // namespace CodeGen
295
296// Local Variables: //
297// tab-width: 4 //
298// mode: c++ //
299// compile-command: "make install" //
300// End: //
Note: See TracBrowser for help on using the repository browser.