source: src/CodeGen/GenType.cc @ 4b1fd2c

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 4b1fd2c was 89e6ffc, checked in by Aaron Moss <a3moss@…>, 8 years ago

Added support for ZeroType? and OneType? to all relevant visitors

  • Property mode set to 100644
File size: 6.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
[1db21619]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jul  9 16:43:52 2015
13// Update Count     : 13
[51587aa]14//
15
[5f2f2d7]16#include <sstream>
[51b7345]17#include <cassert>
18
19#include "GenType.h"
[a61fea9a]20#include "CodeGenerator.h"
[e8032b0]21
22#include "SynTree/Declaration.h"
[51b7345]23#include "SynTree/Expression.h"
[e8032b0]24#include "SynTree/Type.h"
25#include "SynTree/Visitor.h"
[51b7345]26
27namespace CodeGen {
[51587aa]28        class GenType : public Visitor {
29          public:
[486341f]30                GenType( const std::string &typeString, bool mangle = true );
[51587aa]31                std::string get_typeString() const { return typeString; }
32                void set_typeString( const std::string &newValue ) { typeString = newValue; }
[486341f]33
[51587aa]34                virtual void visit( FunctionType *funcType );
35                virtual void visit( VoidType *voidType );
36                virtual void visit( BasicType *basicType );
37                virtual void visit( PointerType *pointerType );
38                virtual void visit( ArrayType *arrayType );
39                virtual void visit( StructInstType *structInst );
40                virtual void visit( UnionInstType *unionInst );
41                virtual void visit( EnumInstType *enumInst );
42                virtual void visit( TypeInstType *typeInst );
[44b7088]43                virtual void visit( VarArgsType *varArgsType );
[89e6ffc]44                virtual void visit( ZeroType *zeroType );
45                virtual void visit( OneType *oneType );
[486341f]46
[51587aa]47          private:
48                void handleQualifiers( Type *type );
49                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
[486341f]50
[51587aa]51                std::string typeString;
[486341f]52                bool mangle = true;
[51587aa]53        };
54
[486341f]55        std::string genType( Type *type, const std::string &baseString, bool mangle ) {
56                GenType gt( baseString, mangle );
[51587aa]57                type->accept( gt );
58                return gt.get_typeString();
59        }
60
[486341f]61        GenType::GenType( const std::string &typeString, bool mangle ) : typeString( typeString ), mangle( mangle ) {}
[51587aa]62
63        void GenType::visit( VoidType *voidType ) {
64                typeString = "void " + typeString;
65                handleQualifiers( voidType );
66        }
67
68        void GenType::visit( BasicType *basicType ) {
69                BasicType::Kind kind = basicType->get_kind();
70                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
71                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
72                handleQualifiers( basicType );
73        }
74
75        void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
[5f2f2d7]76                std::ostringstream os;
[51587aa]77                if ( typeString != "" ) {
78                        if ( typeString[ 0 ] == '*' ) {
79                                os << "(" << typeString << ")";
80                        } else {
81                                os << typeString;
82                        } // if
83                } // if
84                os << "[";
85
86                if ( isStatic ) {
87                        os << "static ";
88                } // if
89                if ( qualifiers.isConst ) {
90                        os << "const ";
91                } // if
92                if ( qualifiers.isVolatile ) {
93                        os << "volatile ";
94                } // if
95                if ( qualifiers.isRestrict ) {
96                        os << "__restrict ";
97                } // if
98                if ( qualifiers.isAtomic ) {
99                        os << "_Atomic ";
100                } // if
[1db21619]101                if ( qualifiers.isAttribute ) {
102                        os << "__attribute(( )) ";
103                } // if
[51587aa]104                if ( dimension != 0 ) {
[486341f]105                        CodeGenerator cg( os, mangle );
[51587aa]106                        dimension->accept( cg );
[71bd8c6]107                } else if ( isVarLen ) {
[1db21619]108                        // no dimension expression on a VLA means it came in with the * token
[71bd8c6]109                        os << "*";
[51587aa]110                } // if
111                os << "]";
112
[5f2f2d7]113                typeString = os.str();
[486341f]114
[51587aa]115                base->accept( *this );
116        }
117
118        void GenType::visit( PointerType *pointerType ) {
119                assert( pointerType->get_base() != 0);
120                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
121                        genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
122                } else {
123                        handleQualifiers( pointerType );
124                        if ( typeString[ 0 ] == '?' ) {
125                                typeString = "* " + typeString;
126                        } else {
127                                typeString = "*" + typeString;
128                        } // if
129                        pointerType->get_base()->accept( *this );
130                } // if
131        }
132
[a08ba92]133        void GenType::visit( ArrayType *arrayType ) {
[51587aa]134                genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
135        }
136
137        void GenType::visit( FunctionType *funcType ) {
[5f2f2d7]138                std::ostringstream os;
[51587aa]139
140                if ( typeString != "" ) {
141                        if ( typeString[ 0 ] == '*' ) {
142                                os << "(" << typeString << ")";
143                        } else {
144                                os << typeString;
145                        } // if
146                } // if
[486341f]147
[51587aa]148                /************* parameters ***************/
149
150                const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
151
152                if ( pars.empty() ) {
153                        if ( funcType->get_isVarArgs() ) {
154                                os << "()";
155                        } else {
156                                os << "(void)";
157                        } // if
158                } else {
[486341f]159                        CodeGenerator cg( os, mangle );
[51587aa]160                        os << "(" ;
161
162                        cg.genCommaList( pars.begin(), pars.end() );
163
[a08ba92]164                        if ( funcType->get_isVarArgs() ) {
[51587aa]165                                os << ", ...";
166                        } // if
167                        os << ")";
168                } // if
[486341f]169
[5f2f2d7]170                typeString = os.str();
[51587aa]171
172                if ( funcType->get_returnVals().size() == 0 ) {
173                        typeString = "void " + typeString;
174                } else {
175                        funcType->get_returnVals().front()->get_type()->accept( *this );
176                } // if
177        }
178
179        void GenType::visit( StructInstType *structInst )  {
180                typeString = "struct " + structInst->get_name() + " " + typeString;
181                handleQualifiers( structInst );
182        }
183
184        void GenType::visit( UnionInstType *unionInst ) {
185                typeString = "union " + unionInst->get_name() + " " + typeString;
186                handleQualifiers( unionInst );
187        }
188
189        void GenType::visit( EnumInstType *enumInst ) {
190                typeString = "enum " + enumInst->get_name() + " " + typeString;
191                handleQualifiers( enumInst );
192        }
193
194        void GenType::visit( TypeInstType *typeInst ) {
195                typeString = typeInst->get_name() + " " + typeString;
196                handleQualifiers( typeInst );
197        }
198
[44b7088]199        void GenType::visit( VarArgsType *varArgsType ) {
200                typeString = "__builtin_va_list " + typeString;
[540ddb7d]201                handleQualifiers( varArgsType );
[44b7088]202        }
203
[89e6ffc]204        void GenType::visit( ZeroType *zeroType ) {
205                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
206                typeString = "int " + typeString;
207                handleQualifiers( zeroType );
208        }
209
210        void GenType::visit( OneType *oneType ) {
211                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
212                typeString = "int " + typeString;
213                handleQualifiers( oneType );
214        }
215
[51587aa]216        void GenType::handleQualifiers( Type *type ) {
217                if ( type->get_isConst() ) {
218                        typeString = "const " + typeString;
219                } // if
220                if ( type->get_isVolatile() ) {
221                        typeString = "volatile " + typeString;
222                } // if
223                if ( type->get_isRestrict() ) {
224                        typeString = "__restrict " + typeString;
225                } // if
226                if ( type->get_isAtomic() ) {
227                        typeString = "_Atomic " + typeString;
228                } // if
[1db21619]229                if ( type->get_isAttribute() ) {
230                        typeString = "__attribute(( )) " + typeString;
231                } // if
[51587aa]232        }
[51b7345]233} // namespace CodeGen
[51587aa]234
235// Local Variables: //
236// tab-width: 4 //
237// mode: c++ //
238// compile-command: "make install" //
239// End: //
Note: See TracBrowser for help on using the repository browser.