source: src/CodeGen/GenType.cc @ 60a8062

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 60a8062 was b4f8808, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Removed lvalue from types in the old ast.

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