source: src/CodeGen/GenType.cc @ 474a170

ADTast-experimental
Last change on this file since 474a170 was d8c96a9, checked in by Andrew Beach <ajbeach@…>, 21 months ago

Small white-space fix.

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