source: src/CodeGen/GenType.cc @ c659968

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 c659968 was 7dc0246d, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Convert GenType? to PassVisitor?

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