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 : Andrew Beach |
---|
12 | // Last Modified On : Wed May 1 15:24:00 2019 |
---|
13 | // Update Count : 23 |
---|
14 | // |
---|
15 | #include "GenType.h" |
---|
16 | |
---|
17 | #include <cassert> // for assert, assertf |
---|
18 | #include <list> // for _List_iterator, _List_const_iterator |
---|
19 | #include <sstream> // for operator<<, ostringstream, basic_os... |
---|
20 | |
---|
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 |
---|
26 | |
---|
27 | namespace CodeGen { |
---|
28 | struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting { |
---|
29 | std::string typeString; |
---|
30 | GenType( const std::string &typeString, const Options &options ); |
---|
31 | |
---|
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 ); |
---|
49 | void postvisit( GlobalScopeType * globalType ); |
---|
50 | void postvisit( TraitInstType * inst ); |
---|
51 | void postvisit( TypeofType * typeof ); |
---|
52 | void postvisit( QualifiedType * qualType ); |
---|
53 | |
---|
54 | private: |
---|
55 | void handleQualifiers( Type *type ); |
---|
56 | std::string handleGeneric( ReferenceToType * refType ); |
---|
57 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
58 | |
---|
59 | Options options; |
---|
60 | }; |
---|
61 | |
---|
62 | std::string genType( Type *type, const std::string &baseString, const Options &options ) { |
---|
63 | PassVisitor<GenType> gt( baseString, options ); |
---|
64 | std::ostringstream os; |
---|
65 | |
---|
66 | if ( ! type->get_attributes().empty() ) { |
---|
67 | PassVisitor<CodeGenerator> cg( os, options ); |
---|
68 | cg.pass.genAttributes( type->get_attributes() ); |
---|
69 | } // if |
---|
70 | |
---|
71 | type->accept( gt ); |
---|
72 | return os.str() + gt.pass.typeString; |
---|
73 | } |
---|
74 | |
---|
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 | } |
---|
82 | |
---|
83 | GenType::GenType( const std::string &typeString, const Options &options ) : typeString( typeString ), options( options ) {} |
---|
84 | |
---|
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 ) { |
---|
99 | typeString = "void " + typeString; |
---|
100 | handleQualifiers( voidType ); |
---|
101 | } |
---|
102 | |
---|
103 | void GenType::postvisit( BasicType * basicType ) { |
---|
104 | BasicType::Kind kind = basicType->kind; |
---|
105 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
106 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
107 | handleQualifiers( basicType ); |
---|
108 | } |
---|
109 | |
---|
110 | void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
111 | std::ostringstream os; |
---|
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 |
---|
124 | if ( qualifiers.is_const ) { |
---|
125 | os << "const "; |
---|
126 | } // if |
---|
127 | if ( qualifiers.is_volatile ) { |
---|
128 | os << "volatile "; |
---|
129 | } // if |
---|
130 | if ( qualifiers.is_restrict ) { |
---|
131 | os << "__restrict "; |
---|
132 | } // if |
---|
133 | if ( qualifiers.is_atomic ) { |
---|
134 | os << "_Atomic "; |
---|
135 | } // if |
---|
136 | if ( dimension != 0 ) { |
---|
137 | PassVisitor<CodeGenerator> cg( os, options ); |
---|
138 | dimension->accept( cg ); |
---|
139 | } else if ( isVarLen ) { |
---|
140 | // no dimension expression on a VLA means it came in with the * token |
---|
141 | os << "*"; |
---|
142 | } // if |
---|
143 | os << "]"; |
---|
144 | |
---|
145 | typeString = os.str(); |
---|
146 | |
---|
147 | base->accept( *visitor ); |
---|
148 | } |
---|
149 | |
---|
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() ); |
---|
154 | } else { |
---|
155 | handleQualifiers( pointerType ); |
---|
156 | if ( typeString[ 0 ] == '?' ) { |
---|
157 | typeString = "* " + typeString; |
---|
158 | } else { |
---|
159 | typeString = "*" + typeString; |
---|
160 | } // if |
---|
161 | pointerType->base->accept( *visitor ); |
---|
162 | } // if |
---|
163 | } |
---|
164 | |
---|
165 | void GenType::postvisit( ArrayType * arrayType ) { |
---|
166 | genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
167 | } |
---|
168 | |
---|
169 | void GenType::postvisit( ReferenceType * refType ) { |
---|
170 | assert( refType->base != 0); |
---|
171 | assertf( ! options.genC, "Reference types should not reach code generation." ); |
---|
172 | handleQualifiers( refType ); |
---|
173 | typeString = "&" + typeString; |
---|
174 | refType->base->accept( *visitor ); |
---|
175 | } |
---|
176 | |
---|
177 | void GenType::postvisit( FunctionType * funcType ) { |
---|
178 | std::ostringstream os; |
---|
179 | |
---|
180 | if ( typeString != "" ) { |
---|
181 | if ( typeString[ 0 ] == '*' ) { |
---|
182 | os << "(" << typeString << ")"; |
---|
183 | } else { |
---|
184 | os << typeString; |
---|
185 | } // if |
---|
186 | } // if |
---|
187 | |
---|
188 | /************* parameters ***************/ |
---|
189 | |
---|
190 | const std::list<DeclarationWithType *> &pars = funcType->parameters; |
---|
191 | |
---|
192 | if ( pars.empty() ) { |
---|
193 | if ( funcType->get_isVarArgs() ) { |
---|
194 | os << "()"; |
---|
195 | } else { |
---|
196 | os << "(void)"; |
---|
197 | } // if |
---|
198 | } else { |
---|
199 | PassVisitor<CodeGenerator> cg( os, options ); |
---|
200 | os << "(" ; |
---|
201 | |
---|
202 | cg.pass.genCommaList( pars.begin(), pars.end() ); |
---|
203 | |
---|
204 | if ( funcType->get_isVarArgs() ) { |
---|
205 | os << ", ..."; |
---|
206 | } // if |
---|
207 | os << ")"; |
---|
208 | } // if |
---|
209 | |
---|
210 | typeString = os.str(); |
---|
211 | |
---|
212 | if ( funcType->returnVals.size() == 0 ) { |
---|
213 | typeString = "void " + typeString; |
---|
214 | } else { |
---|
215 | funcType->returnVals.front()->get_type()->accept( *visitor ); |
---|
216 | } // if |
---|
217 | |
---|
218 | // add forall |
---|
219 | if( ! funcType->forall.empty() && ! options.genC ) { |
---|
220 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
221 | std::ostringstream os; |
---|
222 | PassVisitor<CodeGenerator> cg( os, options ); |
---|
223 | os << "forall("; |
---|
224 | cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() ); |
---|
225 | os << ")" << std::endl; |
---|
226 | typeString = os.str() + typeString; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | std::string GenType::handleGeneric( ReferenceToType * refType ) { |
---|
231 | if ( ! refType->parameters.empty() ) { |
---|
232 | std::ostringstream os; |
---|
233 | PassVisitor<CodeGenerator> cg( os, options ); |
---|
234 | os << "("; |
---|
235 | cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() ); |
---|
236 | os << ") "; |
---|
237 | return os.str(); |
---|
238 | } |
---|
239 | return ""; |
---|
240 | } |
---|
241 | |
---|
242 | void GenType::postvisit( StructInstType * structInst ) { |
---|
243 | typeString = structInst->name + handleGeneric( structInst ) + " " + typeString; |
---|
244 | if ( options.genC ) typeString = "struct " + typeString; |
---|
245 | handleQualifiers( structInst ); |
---|
246 | } |
---|
247 | |
---|
248 | void GenType::postvisit( UnionInstType * unionInst ) { |
---|
249 | typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString; |
---|
250 | if ( options.genC ) typeString = "union " + typeString; |
---|
251 | handleQualifiers( unionInst ); |
---|
252 | } |
---|
253 | |
---|
254 | void GenType::postvisit( EnumInstType * enumInst ) { |
---|
255 | if ( enumInst->baseEnum->base ) { |
---|
256 | typeString = genType(enumInst->baseEnum->base, "", options) + typeString; |
---|
257 | } else { |
---|
258 | typeString = enumInst->name + " " + typeString; |
---|
259 | if ( options.genC ) { |
---|
260 | typeString = "enum " + typeString; |
---|
261 | } |
---|
262 | } |
---|
263 | handleQualifiers( enumInst ); |
---|
264 | } |
---|
265 | |
---|
266 | void GenType::postvisit( TypeInstType * typeInst ) { |
---|
267 | typeString = typeInst->name + " " + typeString; |
---|
268 | handleQualifiers( typeInst ); |
---|
269 | } |
---|
270 | |
---|
271 | void GenType::postvisit( TupleType * tupleType ) { |
---|
272 | assertf( ! options.genC, "Tuple types should not reach code generation." ); |
---|
273 | unsigned int i = 0; |
---|
274 | std::ostringstream os; |
---|
275 | os << "["; |
---|
276 | for ( Type * t : *tupleType ) { |
---|
277 | i++; |
---|
278 | os << genType( t, "", options ) << (i == tupleType->size() ? "" : ", "); |
---|
279 | } |
---|
280 | os << "] "; |
---|
281 | typeString = os.str() + typeString; |
---|
282 | } |
---|
283 | |
---|
284 | void GenType::postvisit( VarArgsType * varArgsType ) { |
---|
285 | typeString = "__builtin_va_list " + typeString; |
---|
286 | handleQualifiers( varArgsType ); |
---|
287 | } |
---|
288 | |
---|
289 | void GenType::postvisit( ZeroType * zeroType ) { |
---|
290 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
291 | typeString = (options.pretty ? "zero_t " : "long int ") + typeString; |
---|
292 | handleQualifiers( zeroType ); |
---|
293 | } |
---|
294 | |
---|
295 | void GenType::postvisit( OneType * oneType ) { |
---|
296 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
297 | typeString = (options.pretty ? "one_t " : "long int ") + typeString; |
---|
298 | handleQualifiers( oneType ); |
---|
299 | } |
---|
300 | |
---|
301 | void GenType::postvisit( GlobalScopeType * globalType ) { |
---|
302 | assertf( ! options.genC, "Global scope type should not reach code generation." ); |
---|
303 | handleQualifiers( globalType ); |
---|
304 | } |
---|
305 | |
---|
306 | void GenType::postvisit( TraitInstType * inst ) { |
---|
307 | assertf( ! options.genC, "Trait types should not reach code generation." ); |
---|
308 | typeString = inst->name + " " + typeString; |
---|
309 | handleQualifiers( inst ); |
---|
310 | } |
---|
311 | |
---|
312 | void GenType::postvisit( TypeofType * typeof ) { |
---|
313 | std::ostringstream os; |
---|
314 | PassVisitor<CodeGenerator> cg( os, options ); |
---|
315 | os << "typeof("; |
---|
316 | typeof->expr->accept( cg ); |
---|
317 | os << ") " << typeString; |
---|
318 | typeString = os.str(); |
---|
319 | handleQualifiers( typeof ); |
---|
320 | } |
---|
321 | |
---|
322 | void GenType::postvisit( QualifiedType * qualType ) { |
---|
323 | assertf( ! options.genC, "Qualified types should not reach code generation." ); |
---|
324 | std::ostringstream os; |
---|
325 | os << genType( qualType->parent, "", options ) << "." << genType( qualType->child, "", options ) << typeString; |
---|
326 | typeString = os.str(); |
---|
327 | handleQualifiers( qualType ); |
---|
328 | } |
---|
329 | |
---|
330 | void GenType::handleQualifiers( Type * type ) { |
---|
331 | if ( type->get_const() ) { |
---|
332 | typeString = "const " + typeString; |
---|
333 | } // if |
---|
334 | if ( type->get_volatile() ) { |
---|
335 | typeString = "volatile " + typeString; |
---|
336 | } // if |
---|
337 | if ( type->get_restrict() ) { |
---|
338 | typeString = "__restrict " + typeString; |
---|
339 | } // if |
---|
340 | if ( type->get_atomic() ) { |
---|
341 | typeString = "_Atomic " + typeString; |
---|
342 | } // if |
---|
343 | } |
---|
344 | } // namespace CodeGen |
---|
345 | |
---|
346 | // Local Variables: // |
---|
347 | // tab-width: 4 // |
---|
348 | // mode: c++ // |
---|
349 | // compile-command: "make install" // |
---|
350 | // End: // |
---|