source: src/CodeGen/GenType.cc @ ad1770be

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 ad1770be was c850687, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Add -L flag to turn of line marks. Updated the keyword list.

  • Property mode set to 100644
File size: 8.5 KB
Line 
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//
7// GenType.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 17 09:02:28 2017
13// Update Count     : 22
14//
15
16#include <sstream>
17#include <cassert>
18
19#include "GenType.h"
20#include "CodeGenerator.h"
21
22#include "SynTree/Declaration.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Type.h"
25#include "SynTree/Visitor.h"
26
27namespace CodeGen {
28        class GenType : public Visitor {
29          public:
30                GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false );
31                std::string get_typeString() const { return typeString; }
32                void set_typeString( const std::string &newValue ) { typeString = newValue; }
33
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 );
43                virtual void visit( TupleType * tupleType );
44                virtual void visit( VarArgsType *varArgsType );
45                virtual void visit( ZeroType *zeroType );
46                virtual void visit( OneType *oneType );
47
48          private:
49                void handleQualifiers( Type *type );
50                std::string handleGeneric( ReferenceToType * refType );
51                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
52
53                std::string typeString;
54                bool pretty = false; // pretty print
55                bool genC = false;   // generating C code?
56                bool lineMarks = false;
57        };
58
59        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
60                GenType gt( baseString, pretty, genC, lineMarks );
61                std::ostringstream os;
62
63                if ( ! type->get_attributes().empty() ) {
64                        CodeGenerator cg( os, pretty, genC, lineMarks );
65                        cg.genAttributes( type->get_attributes() );
66                } // if
67
68                type->accept( gt );
69                return os.str() + gt.get_typeString();
70        }
71
72  std::string genPrettyType( Type * type, const std::string & baseString ) {
73        return genType( type, baseString, true, false );
74  }
75
76        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
77
78        void GenType::visit( VoidType *voidType ) {
79                typeString = "void " + typeString;
80                handleQualifiers( voidType );
81        }
82
83        void GenType::visit( BasicType *basicType ) {
84                BasicType::Kind kind = basicType->get_kind();
85                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
86                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
87                handleQualifiers( basicType );
88        }
89
90        void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
91                std::ostringstream os;
92                if ( typeString != "" ) {
93                        if ( typeString[ 0 ] == '*' ) {
94                                os << "(" << typeString << ")";
95                        } else {
96                                os << typeString;
97                        } // if
98                } // if
99                os << "[";
100
101                if ( isStatic ) {
102                        os << "static ";
103                } // if
104                if ( qualifiers.is_const ) {
105                        os << "const ";
106                } // if
107                if ( qualifiers.is_volatile ) {
108                        os << "volatile ";
109                } // if
110                if ( qualifiers.is_restrict ) {
111                        os << "__restrict ";
112                } // if
113                if ( qualifiers.is_atomic ) {
114                        os << "_Atomic ";
115                } // if
116                if ( dimension != 0 ) {
117                        CodeGenerator cg( os, pretty, genC, lineMarks );
118                        dimension->accept( cg );
119                } else if ( isVarLen ) {
120                        // no dimension expression on a VLA means it came in with the * token
121                        os << "*";
122                } // if
123                os << "]";
124
125                typeString = os.str();
126
127                base->accept( *this );
128        }
129
130        void GenType::visit( PointerType *pointerType ) {
131                assert( pointerType->get_base() != 0);
132                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
133                        genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
134                } else {
135                        handleQualifiers( pointerType );
136                        if ( typeString[ 0 ] == '?' ) {
137                                typeString = "* " + typeString;
138                        } else {
139                                typeString = "*" + typeString;
140                        } // if
141                        pointerType->get_base()->accept( *this );
142                } // if
143        }
144
145        void GenType::visit( ArrayType *arrayType ) {
146                genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
147        }
148
149        void GenType::visit( FunctionType *funcType ) {
150                std::ostringstream os;
151
152                if ( typeString != "" ) {
153                        if ( typeString[ 0 ] == '*' ) {
154                                os << "(" << typeString << ")";
155                        } else {
156                                os << typeString;
157                        } // if
158                } // if
159
160                /************* parameters ***************/
161
162                const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
163
164                if ( pars.empty() ) {
165                        if ( funcType->get_isVarArgs() ) {
166                                os << "()";
167                        } else {
168                                os << "(void)";
169                        } // if
170                } else {
171                        CodeGenerator cg( os, pretty, genC, lineMarks );
172                        os << "(" ;
173
174                        cg.genCommaList( pars.begin(), pars.end() );
175
176                        if ( funcType->get_isVarArgs() ) {
177                                os << ", ...";
178                        } // if
179                        os << ")";
180                } // if
181
182                typeString = os.str();
183
184                if ( funcType->get_returnVals().size() == 0 ) {
185                        typeString = "void " + typeString;
186                } else {
187                        funcType->get_returnVals().front()->get_type()->accept( *this );
188                } // if
189
190                // add forall
191                if( ! funcType->get_forall().empty() && ! genC ) {
192                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
193                        std::ostringstream os;
194                        CodeGenerator cg( os, pretty, genC, lineMarks );
195                        os << "forall(";
196                        cg.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
197                        os << ")" << std::endl;
198                        typeString = os.str() + typeString;
199                }
200        }
201
202        std::string GenType::handleGeneric( ReferenceToType * refType ) {
203                if ( ! refType->get_parameters().empty() ) {
204                        std::ostringstream os;
205                        CodeGenerator cg( os, pretty, genC, lineMarks );
206                        os << "(";
207                        cg.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() );
208                        os << ") ";
209                        return os.str();
210                }
211                return "";
212        }
213
214        void GenType::visit( StructInstType *structInst )  {
215                typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
216                if ( genC ) typeString = "struct " + typeString;
217                handleQualifiers( structInst );
218        }
219
220        void GenType::visit( UnionInstType *unionInst ) {
221                typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
222                if ( genC ) typeString = "union " + typeString;
223                handleQualifiers( unionInst );
224        }
225
226        void GenType::visit( EnumInstType *enumInst ) {
227                typeString = enumInst->get_name() + " " + typeString;
228                if ( genC ) typeString = "enum " + typeString;
229                handleQualifiers( enumInst );
230        }
231
232        void GenType::visit( TypeInstType *typeInst ) {
233                typeString = typeInst->get_name() + " " + typeString;
234                handleQualifiers( typeInst );
235        }
236
237        void GenType::visit( TupleType * tupleType ) {
238                assertf( ! genC, "Tuple types should not reach code generation." );
239                Visitor::visit( tupleType );
240                unsigned int i = 0;
241                std::ostringstream os;
242                os << "[";
243                for ( Type * t : *tupleType ) {
244                        i++;
245                        os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", ");
246                }
247                os << "]";
248                typeString = os.str() + typeString;
249        }
250
251        void GenType::visit( VarArgsType *varArgsType ) {
252                typeString = "__builtin_va_list " + typeString;
253                handleQualifiers( varArgsType );
254        }
255
256        void GenType::visit( ZeroType *zeroType ) {
257                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
258                typeString = (pretty ? "zero_t " : "long int ") + typeString;
259                handleQualifiers( zeroType );
260        }
261
262        void GenType::visit( OneType *oneType ) {
263                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
264                typeString = (pretty ? "one_t " : "long int ") + typeString;
265                handleQualifiers( oneType );
266        }
267
268        void GenType::handleQualifiers( Type *type ) {
269                if ( type->get_const() ) {
270                        typeString = "const " + typeString;
271                } // if
272                if ( type->get_volatile() ) {
273                        typeString = "volatile " + typeString;
274                } // if
275                if ( type->get_restrict() ) {
276                        typeString = "__restrict " + typeString;
277                } // if
278                if ( type->get_atomic() ) {
279                        typeString = "_Atomic " + typeString;
280                } // if
281        }
282} // namespace CodeGen
283
284// Local Variables: //
285// tab-width: 4 //
286// mode: c++ //
287// compile-command: "make install" //
288// End: //
Note: See TracBrowser for help on using the repository browser.