source: src/CodeGen/GenType.cc @ 1be845b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 1be845b was c198b69, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Minor cleanup in GenType?

  • Property mode set to 100644
File size: 10.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// 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        struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
29                std::string typeString;
30                GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks );
31
32                void previsit( BaseSyntaxNode * );
33                void postvisit( BaseSyntaxNode * );
34
35                void postvisit( FunctionType * funcType );
36                void postvisit( VoidType * voidType );
37                void postvisit( BasicType * basicType );
38                void postvisit( PointerType * pointerType );
39                void postvisit( ArrayType * arrayType );
40                void postvisit( ReferenceType * refType );
41                void postvisit( StructInstType * structInst );
42                void postvisit( UnionInstType * unionInst );
43                void postvisit( EnumInstType * enumInst );
44                void postvisit( TypeInstType * typeInst );
45                void postvisit( TupleType  * tupleType );
46                void postvisit( VarArgsType * varArgsType );
47                void postvisit( ZeroType * zeroType );
48                void postvisit( OneType * oneType );
49                void postvisit( GlobalScopeType * globalType );
50                void postvisit( TraitInstType * inst );
51                void postvisit( TypeofType * typeof );
52                void postvisit( QualifiedType * qualType );
53
54          private:
55                void handleQualifiers( Type *type );
56                std::string handleGeneric( ReferenceToType * refType );
57                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
58
59                bool pretty = false;    // pretty print
60                bool genC = false;      // generating C code?
61                bool lineMarks = false; // lineMarks on for CodeGenerator?
62        };
63
64        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
65                PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks );
66                std::ostringstream os;
67
68                if ( ! type->get_attributes().empty() ) {
69                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
70                        cg.pass.genAttributes( type->get_attributes() );
71                } // if
72
73                type->accept( gt );
74                return os.str() + gt.pass.typeString;
75        }
76
77  std::string genPrettyType( Type * type, const std::string & baseString ) {
78        return genType( type, baseString, true, false );
79  }
80
81        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
82
83        // *** BaseSyntaxNode
84        void GenType::previsit( BaseSyntaxNode * ) {
85                // turn off automatic recursion for all nodes, to allow each visitor to
86                // precisely control the order in which its children are visited.
87                visit_children = false;
88        }
89
90        void GenType::postvisit( BaseSyntaxNode * node ) {
91                std::stringstream ss;
92                node->print( ss );
93                assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
94        }
95
96        void GenType::postvisit( VoidType * voidType ) {
97                typeString = "void " + typeString;
98                handleQualifiers( voidType );
99        }
100
101        void GenType::postvisit( BasicType * basicType ) {
102                BasicType::Kind kind = basicType->kind;
103                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
104                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
105                handleQualifiers( basicType );
106        }
107
108        void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) {
109                std::ostringstream os;
110                if ( typeString != "" ) {
111                        if ( typeString[ 0 ] == '*' ) {
112                                os << "(" << typeString << ")";
113                        } else {
114                                os << typeString;
115                        } // if
116                } // if
117                os << "[";
118
119                if ( isStatic ) {
120                        os << "static ";
121                } // if
122                if ( qualifiers.is_const ) {
123                        os << "const ";
124                } // if
125                if ( qualifiers.is_volatile ) {
126                        os << "volatile ";
127                } // if
128                if ( qualifiers.is_restrict ) {
129                        os << "__restrict ";
130                } // if
131                if ( qualifiers.is_atomic ) {
132                        os << "_Atomic ";
133                } // if
134                if ( dimension != 0 ) {
135                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
136                        dimension->accept( cg );
137                } else if ( isVarLen ) {
138                        // no dimension expression on a VLA means it came in with the * token
139                        os << "*";
140                } // if
141                os << "]";
142
143                typeString = os.str();
144
145                base->accept( *visitor );
146        }
147
148        void GenType::postvisit( PointerType * pointerType ) {
149                assert( pointerType->base != 0);
150                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
151                        genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
152                } else {
153                        handleQualifiers( pointerType );
154                        if ( typeString[ 0 ] == '?' ) {
155                                typeString = "* " + typeString;
156                        } else {
157                                typeString = "*" + typeString;
158                        } // if
159                        pointerType->base->accept( *visitor );
160                } // if
161        }
162
163        void GenType::postvisit( ArrayType * arrayType ) {
164                genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
165        }
166
167        void GenType::postvisit( ReferenceType * refType ) {
168                assert( refType->base != 0);
169                assertf( ! genC, "Reference types should not reach code generation." );
170                handleQualifiers( refType );
171                typeString = "&" + typeString;
172                refType->base->accept( *visitor );
173        }
174
175        void GenType::postvisit( FunctionType * funcType ) {
176                std::ostringstream os;
177
178                if ( typeString != "" ) {
179                        if ( typeString[ 0 ] == '*' ) {
180                                os << "(" << typeString << ")";
181                        } else {
182                                os << typeString;
183                        } // if
184                } // if
185
186                /************* parameters ***************/
187
188                const std::list<DeclarationWithType *> &pars = funcType->parameters;
189
190                if ( pars.empty() ) {
191                        if ( funcType->get_isVarArgs() ) {
192                                os << "()";
193                        } else {
194                                os << "(void)";
195                        } // if
196                } else {
197                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
198                        os << "(" ;
199
200                        cg.pass.genCommaList( pars.begin(), pars.end() );
201
202                        if ( funcType->get_isVarArgs() ) {
203                                os << ", ...";
204                        } // if
205                        os << ")";
206                } // if
207
208                typeString = os.str();
209
210                if ( funcType->returnVals.size() == 0 ) {
211                        typeString = "void " + typeString;
212                } else {
213                        funcType->returnVals.front()->get_type()->accept( *visitor );
214                } // if
215
216                // add forall
217                if( ! funcType->forall.empty() && ! genC ) {
218                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
219                        std::ostringstream os;
220                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
221                        os << "forall(";
222                        cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
223                        os << ")" << std::endl;
224                        typeString = os.str() + typeString;
225                }
226        }
227
228        std::string GenType::handleGeneric( ReferenceToType * refType ) {
229                if ( ! refType->parameters.empty() ) {
230                        std::ostringstream os;
231                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
232                        os << "(";
233                        cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() );
234                        os << ") ";
235                        return os.str();
236                }
237                return "";
238        }
239
240        void GenType::postvisit( StructInstType * structInst )  {
241                typeString = structInst->name + handleGeneric( structInst ) + " " + typeString;
242                if ( genC ) typeString = "struct " + typeString;
243                handleQualifiers( structInst );
244        }
245
246        void GenType::postvisit( UnionInstType * unionInst ) {
247                typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
248                if ( genC ) typeString = "union " + typeString;
249                handleQualifiers( unionInst );
250        }
251
252        void GenType::postvisit( EnumInstType * enumInst ) {
253                typeString = enumInst->name + " " + typeString;
254                if ( genC ) typeString = "enum " + typeString;
255                handleQualifiers( enumInst );
256        }
257
258        void GenType::postvisit( TypeInstType * typeInst ) {
259                typeString = typeInst->name + " " + typeString;
260                handleQualifiers( typeInst );
261        }
262
263        void GenType::postvisit( TupleType * tupleType ) {
264                assertf( ! genC, "Tuple types should not reach code generation." );
265                unsigned int i = 0;
266                std::ostringstream os;
267                os << "[";
268                for ( Type * t : *tupleType ) {
269                        i++;
270                        os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", ");
271                }
272                os << "] ";
273                typeString = os.str() + typeString;
274        }
275
276        void GenType::postvisit( VarArgsType * varArgsType ) {
277                typeString = "__builtin_va_list " + typeString;
278                handleQualifiers( varArgsType );
279        }
280
281        void GenType::postvisit( ZeroType * zeroType ) {
282                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
283                typeString = (pretty ? "zero_t " : "long int ") + typeString;
284                handleQualifiers( zeroType );
285        }
286
287        void GenType::postvisit( OneType * oneType ) {
288                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
289                typeString = (pretty ? "one_t " : "long int ") + typeString;
290                handleQualifiers( oneType );
291        }
292
293        void GenType::postvisit( GlobalScopeType * globalType ) {
294                assertf( ! genC, "Global scope type should not reach code generation." );
295                handleQualifiers( globalType );
296        }
297
298        void GenType::postvisit( TraitInstType * inst ) {
299                assertf( ! genC, "Trait types should not reach code generation." );
300                typeString = inst->name + " " + typeString;
301                handleQualifiers( inst );
302        }
303
304        void GenType::postvisit( TypeofType * typeof ) {
305                std::ostringstream os;
306                PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
307                os << "typeof(";
308                typeof->expr->accept( cg );
309                os << ") " << typeString;
310                typeString = os.str();
311                handleQualifiers( typeof );
312        }
313
314        void GenType::postvisit( QualifiedType * qualType ) {
315                assertf( ! genC, "Qualified types should not reach code generation." );
316                std::ostringstream os;
317                os << genType( qualType->parent, "", pretty, genC, lineMarks ) << "." << genType( qualType->child, "", pretty, genC, lineMarks ) << typeString;
318                typeString = os.str();
319                handleQualifiers( qualType );
320        }
321
322        void GenType::handleQualifiers( Type * type ) {
323                if ( type->get_const() ) {
324                        typeString = "const " + typeString;
325                } // if
326                if ( type->get_volatile() ) {
327                        typeString = "volatile " + typeString;
328                } // if
329                if ( type->get_restrict() ) {
330                        typeString = "__restrict " + typeString;
331                } // if
332                if ( type->get_atomic() ) {
333                        typeString = "_Atomic " + typeString;
334                } // if
335                if ( type->get_lvalue() && ! genC ) {
336                        // when not generating C code, print lvalue for debugging.
337                        typeString = "lvalue " + typeString;
338                }
339        }
340} // namespace CodeGen
341
342// Local Variables: //
343// tab-width: 4 //
344// mode: c++ //
345// compile-command: "make install" //
346// End: //
Note: See TracBrowser for help on using the repository browser.