source: src/CodeGen/GenType.cc @ ac3362c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since ac3362c 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
RevLine 
[51587aa]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//
[486341f]7// GenType.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[1db21619]11// Last Modified By : Peter A. Buhr
[615a096]12// Last Modified On : Fri Mar 17 09:02:28 2017
13// Update Count     : 22
[51587aa]14//
[3268a58]15#include "GenType.h"
[51587aa]16
[bf2438c]17#include <cassert>                // for assert, assertf
18#include <list>                   // for _List_iterator, _List_const_iterator
19#include <sstream>                // for operator<<, ostringstream, basic_os...
[51b7345]20
[bf2438c]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
[51b7345]26
27namespace CodeGen {
[7dc0246d]28        struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
[c198b69]29                std::string typeString;
30                GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks );
[486341f]31
[7dc0246d]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 );
[c194661]49                void postvisit( GlobalScopeType * globalType );
[da9d79b]50                void postvisit( TraitInstType * inst );
51                void postvisit( TypeofType * typeof );
[c194661]52                void postvisit( QualifiedType * qualType );
[486341f]53
[51587aa]54          private:
55                void handleQualifiers( Type *type );
[e39241b]56                std::string handleGeneric( ReferenceToType * refType );
[51587aa]57                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
[486341f]58
[c198b69]59                bool pretty = false;    // pretty print
60                bool genC = false;      // generating C code?
61                bool lineMarks = false; // lineMarks on for CodeGenerator?
[51587aa]62        };
63
[c850687]64        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
[7dc0246d]65                PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks );
[c0aa336]66                std::ostringstream os;
[35b1bf4]67
[c0aa336]68                if ( ! type->get_attributes().empty() ) {
[9857e8d]69                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
70                        cg.pass.genAttributes( type->get_attributes() );
[c0aa336]71                } // if
72
[51587aa]73                type->accept( gt );
[c198b69]74                return os.str() + gt.pass.typeString;
[51587aa]75        }
76
[35b1bf4]77  std::string genPrettyType( Type * type, const std::string & baseString ) {
[e39241b]78        return genType( type, baseString, true, false );
[35b1bf4]79  }
80
[c850687]81        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
[51587aa]82
[7dc0246d]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 ) {
[51587aa]97                typeString = "void " + typeString;
98                handleQualifiers( voidType );
99        }
100
[7dc0246d]101        void GenType::postvisit( BasicType * basicType ) {
102                BasicType::Kind kind = basicType->kind;
[51587aa]103                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
104                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
105                handleQualifiers( basicType );
106        }
107
[7dc0246d]108        void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) {
[5f2f2d7]109                std::ostringstream os;
[51587aa]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
[615a096]122                if ( qualifiers.is_const ) {
[51587aa]123                        os << "const ";
124                } // if
[615a096]125                if ( qualifiers.is_volatile ) {
[51587aa]126                        os << "volatile ";
127                } // if
[615a096]128                if ( qualifiers.is_restrict ) {
[51587aa]129                        os << "__restrict ";
130                } // if
[615a096]131                if ( qualifiers.is_atomic ) {
[51587aa]132                        os << "_Atomic ";
133                } // if
134                if ( dimension != 0 ) {
[9857e8d]135                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
[51587aa]136                        dimension->accept( cg );
[71bd8c6]137                } else if ( isVarLen ) {
[1db21619]138                        // no dimension expression on a VLA means it came in with the * token
[71bd8c6]139                        os << "*";
[51587aa]140                } // if
141                os << "]";
142
[5f2f2d7]143                typeString = os.str();
[486341f]144
[7dc0246d]145                base->accept( *visitor );
[51587aa]146        }
147
[7dc0246d]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() );
[51587aa]152                } else {
153                        handleQualifiers( pointerType );
154                        if ( typeString[ 0 ] == '?' ) {
155                                typeString = "* " + typeString;
156                        } else {
157                                typeString = "*" + typeString;
158                        } // if
[7dc0246d]159                        pointerType->base->accept( *visitor );
[51587aa]160                } // if
161        }
162
[7dc0246d]163        void GenType::postvisit( ArrayType * arrayType ) {
164                genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
[51587aa]165        }
166
[7dc0246d]167        void GenType::postvisit( ReferenceType * refType ) {
168                assert( refType->base != 0);
[ce8c12f]169                assertf( ! genC, "Reference types should not reach code generation." );
170                handleQualifiers( refType );
171                typeString = "&" + typeString;
[7dc0246d]172                refType->base->accept( *visitor );
[ce8c12f]173        }
174
[7dc0246d]175        void GenType::postvisit( FunctionType * funcType ) {
[5f2f2d7]176                std::ostringstream os;
[51587aa]177
178                if ( typeString != "" ) {
179                        if ( typeString[ 0 ] == '*' ) {
180                                os << "(" << typeString << ")";
181                        } else {
182                                os << typeString;
183                        } // if
184                } // if
[486341f]185
[51587aa]186                /************* parameters ***************/
187
[7dc0246d]188                const std::list<DeclarationWithType *> &pars = funcType->parameters;
[51587aa]189
190                if ( pars.empty() ) {
191                        if ( funcType->get_isVarArgs() ) {
192                                os << "()";
193                        } else {
194                                os << "(void)";
195                        } // if
196                } else {
[9857e8d]197                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
[51587aa]198                        os << "(" ;
199
[9857e8d]200                        cg.pass.genCommaList( pars.begin(), pars.end() );
[51587aa]201
[a08ba92]202                        if ( funcType->get_isVarArgs() ) {
[51587aa]203                                os << ", ...";
204                        } // if
205                        os << ")";
206                } // if
[486341f]207
[5f2f2d7]208                typeString = os.str();
[51587aa]209
[7dc0246d]210                if ( funcType->returnVals.size() == 0 ) {
[51587aa]211                        typeString = "void " + typeString;
212                } else {
[7dc0246d]213                        funcType->returnVals.front()->get_type()->accept( *visitor );
[51587aa]214                } // if
[5f642e38]215
216                // add forall
[7dc0246d]217                if( ! funcType->forall.empty() && ! genC ) {
[5f642e38]218                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
219                        std::ostringstream os;
[9857e8d]220                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
[5f642e38]221                        os << "forall(";
[7dc0246d]222                        cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
[5f642e38]223                        os << ")" << std::endl;
224                        typeString = os.str() + typeString;
225                }
[51587aa]226        }
227
[e39241b]228        std::string GenType::handleGeneric( ReferenceToType * refType ) {
[86f852b]229                if ( ! refType->parameters.empty() ) {
[e39241b]230                        std::ostringstream os;
[9857e8d]231                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
[e39241b]232                        os << "(";
[86f852b]233                        cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() );
[e39241b]234                        os << ") ";
235                        return os.str();
236                }
237                return "";
238        }
239
[7dc0246d]240        void GenType::postvisit( StructInstType * structInst )  {
241                typeString = structInst->name + handleGeneric( structInst ) + " " + typeString;
[e39241b]242                if ( genC ) typeString = "struct " + typeString;
[51587aa]243                handleQualifiers( structInst );
244        }
245
[7dc0246d]246        void GenType::postvisit( UnionInstType * unionInst ) {
247                typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
[e39241b]248                if ( genC ) typeString = "union " + typeString;
[51587aa]249                handleQualifiers( unionInst );
250        }
251
[7dc0246d]252        void GenType::postvisit( EnumInstType * enumInst ) {
253                typeString = enumInst->name + " " + typeString;
[e39241b]254                if ( genC ) typeString = "enum " + typeString;
[51587aa]255                handleQualifiers( enumInst );
256        }
257
[7dc0246d]258        void GenType::postvisit( TypeInstType * typeInst ) {
259                typeString = typeInst->name + " " + typeString;
[51587aa]260                handleQualifiers( typeInst );
261        }
262
[7dc0246d]263        void GenType::postvisit( TupleType * tupleType ) {
[e39241b]264                assertf( ! genC, "Tuple types should not reach code generation." );
[35b1bf4]265                unsigned int i = 0;
266                std::ostringstream os;
267                os << "[";
268                for ( Type * t : *tupleType ) {
269                        i++;
[c850687]270                        os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", ");
[35b1bf4]271                }
[65aca88]272                os << "] ";
[35b1bf4]273                typeString = os.str() + typeString;
[6d4d1a6]274        }
275
[7dc0246d]276        void GenType::postvisit( VarArgsType * varArgsType ) {
[44b7088]277                typeString = "__builtin_va_list " + typeString;
[540ddb7d]278                handleQualifiers( varArgsType );
[44b7088]279        }
280
[7dc0246d]281        void GenType::postvisit( ZeroType * zeroType ) {
[89e6ffc]282                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
[35b1bf4]283                typeString = (pretty ? "zero_t " : "long int ") + typeString;
[89e6ffc]284                handleQualifiers( zeroType );
285        }
286
[7dc0246d]287        void GenType::postvisit( OneType * oneType ) {
[89e6ffc]288                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
[35b1bf4]289                typeString = (pretty ? "one_t " : "long int ") + typeString;
[89e6ffc]290                handleQualifiers( oneType );
291        }
292
[c194661]293        void GenType::postvisit( GlobalScopeType * globalType ) {
294                assertf( ! genC, "Global scope type should not reach code generation." );
295                handleQualifiers( globalType );
296        }
297
[da9d79b]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
[c194661]314        void GenType::postvisit( QualifiedType * qualType ) {
315                assertf( ! genC, "Qualified types should not reach code generation." );
316                std::ostringstream os;
[450805a]317                os << genType( qualType->parent, "", pretty, genC, lineMarks ) << "." << genType( qualType->child, "", pretty, genC, lineMarks ) << typeString;
[c194661]318                typeString = os.str();
319                handleQualifiers( qualType );
320        }
321
[7dc0246d]322        void GenType::handleQualifiers( Type * type ) {
[615a096]323                if ( type->get_const() ) {
[51587aa]324                        typeString = "const " + typeString;
325                } // if
[615a096]326                if ( type->get_volatile() ) {
[51587aa]327                        typeString = "volatile " + typeString;
328                } // if
[615a096]329                if ( type->get_restrict() ) {
[51587aa]330                        typeString = "__restrict " + typeString;
331                } // if
[615a096]332                if ( type->get_atomic() ) {
[51587aa]333                        typeString = "_Atomic " + typeString;
334                } // if
[d104b02]335                if ( type->get_lvalue() && ! genC ) {
336                        // when not generating C code, print lvalue for debugging.
337                        typeString = "lvalue " + typeString;
338                }
[51587aa]339        }
[51b7345]340} // namespace CodeGen
[51587aa]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.