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 | #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 | GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false ); |
---|
30 | std::string get_typeString() const { return typeString; } |
---|
31 | void set_typeString( const std::string &newValue ) { typeString = newValue; } |
---|
32 | |
---|
33 | void previsit( BaseSyntaxNode * ); |
---|
34 | void postvisit( BaseSyntaxNode * ); |
---|
35 | |
---|
36 | void postvisit( FunctionType * funcType ); |
---|
37 | void postvisit( VoidType * voidType ); |
---|
38 | void postvisit( BasicType * basicType ); |
---|
39 | void postvisit( PointerType * pointerType ); |
---|
40 | void postvisit( ArrayType * arrayType ); |
---|
41 | void postvisit( ReferenceType * refType ); |
---|
42 | void postvisit( StructInstType * structInst ); |
---|
43 | void postvisit( UnionInstType * unionInst ); |
---|
44 | void postvisit( EnumInstType * enumInst ); |
---|
45 | void postvisit( TypeInstType * typeInst ); |
---|
46 | void postvisit( TupleType * tupleType ); |
---|
47 | void postvisit( VarArgsType * varArgsType ); |
---|
48 | void postvisit( ZeroType * zeroType ); |
---|
49 | void postvisit( OneType * oneType ); |
---|
50 | |
---|
51 | private: |
---|
52 | void handleQualifiers( Type *type ); |
---|
53 | std::string handleGeneric( ReferenceToType * refType ); |
---|
54 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
55 | |
---|
56 | std::string typeString; |
---|
57 | bool pretty = false; // pretty print |
---|
58 | bool genC = false; // generating C code? |
---|
59 | bool lineMarks = false; |
---|
60 | }; |
---|
61 | |
---|
62 | std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) { |
---|
63 | PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks ); |
---|
64 | std::ostringstream os; |
---|
65 | |
---|
66 | if ( ! type->get_attributes().empty() ) { |
---|
67 | PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); |
---|
68 | cg.pass.genAttributes( type->get_attributes() ); |
---|
69 | } // if |
---|
70 | |
---|
71 | type->accept( gt ); |
---|
72 | return os.str() + gt.pass.get_typeString(); |
---|
73 | } |
---|
74 | |
---|
75 | std::string genPrettyType( Type * type, const std::string & baseString ) { |
---|
76 | return genType( type, baseString, true, false ); |
---|
77 | } |
---|
78 | |
---|
79 | GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} |
---|
80 | |
---|
81 | // *** BaseSyntaxNode |
---|
82 | void GenType::previsit( BaseSyntaxNode * ) { |
---|
83 | // turn off automatic recursion for all nodes, to allow each visitor to |
---|
84 | // precisely control the order in which its children are visited. |
---|
85 | visit_children = false; |
---|
86 | } |
---|
87 | |
---|
88 | void GenType::postvisit( BaseSyntaxNode * node ) { |
---|
89 | std::stringstream ss; |
---|
90 | node->print( ss ); |
---|
91 | assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() ); |
---|
92 | } |
---|
93 | |
---|
94 | void GenType::postvisit( VoidType * voidType ) { |
---|
95 | typeString = "void " + typeString; |
---|
96 | handleQualifiers( voidType ); |
---|
97 | } |
---|
98 | |
---|
99 | void GenType::postvisit( BasicType * basicType ) { |
---|
100 | BasicType::Kind kind = basicType->kind; |
---|
101 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
102 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
103 | handleQualifiers( basicType ); |
---|
104 | } |
---|
105 | |
---|
106 | void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
107 | std::ostringstream os; |
---|
108 | if ( typeString != "" ) { |
---|
109 | if ( typeString[ 0 ] == '*' ) { |
---|
110 | os << "(" << typeString << ")"; |
---|
111 | } else { |
---|
112 | os << typeString; |
---|
113 | } // if |
---|
114 | } // if |
---|
115 | os << "["; |
---|
116 | |
---|
117 | if ( isStatic ) { |
---|
118 | os << "static "; |
---|
119 | } // if |
---|
120 | if ( qualifiers.is_const ) { |
---|
121 | os << "const "; |
---|
122 | } // if |
---|
123 | if ( qualifiers.is_volatile ) { |
---|
124 | os << "volatile "; |
---|
125 | } // if |
---|
126 | if ( qualifiers.is_restrict ) { |
---|
127 | os << "__restrict "; |
---|
128 | } // if |
---|
129 | if ( qualifiers.is_atomic ) { |
---|
130 | os << "_Atomic "; |
---|
131 | } // if |
---|
132 | if ( dimension != 0 ) { |
---|
133 | PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); |
---|
134 | dimension->accept( cg ); |
---|
135 | } else if ( isVarLen ) { |
---|
136 | // no dimension expression on a VLA means it came in with the * token |
---|
137 | os << "*"; |
---|
138 | } // if |
---|
139 | os << "]"; |
---|
140 | |
---|
141 | typeString = os.str(); |
---|
142 | |
---|
143 | base->accept( *visitor ); |
---|
144 | } |
---|
145 | |
---|
146 | void GenType::postvisit( PointerType * pointerType ) { |
---|
147 | assert( pointerType->base != 0); |
---|
148 | if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) { |
---|
149 | genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() ); |
---|
150 | } else { |
---|
151 | handleQualifiers( pointerType ); |
---|
152 | if ( typeString[ 0 ] == '?' ) { |
---|
153 | typeString = "* " + typeString; |
---|
154 | } else { |
---|
155 | typeString = "*" + typeString; |
---|
156 | } // if |
---|
157 | pointerType->base->accept( *visitor ); |
---|
158 | } // if |
---|
159 | } |
---|
160 | |
---|
161 | void GenType::postvisit( ArrayType * arrayType ) { |
---|
162 | genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
163 | } |
---|
164 | |
---|
165 | void GenType::postvisit( ReferenceType * refType ) { |
---|
166 | assert( refType->base != 0); |
---|
167 | assertf( ! genC, "Reference types should not reach code generation." ); |
---|
168 | handleQualifiers( refType ); |
---|
169 | typeString = "&" + typeString; |
---|
170 | refType->base->accept( *visitor ); |
---|
171 | } |
---|
172 | |
---|
173 | void GenType::postvisit( FunctionType * funcType ) { |
---|
174 | std::ostringstream os; |
---|
175 | |
---|
176 | if ( typeString != "" ) { |
---|
177 | if ( typeString[ 0 ] == '*' ) { |
---|
178 | os << "(" << typeString << ")"; |
---|
179 | } else { |
---|
180 | os << typeString; |
---|
181 | } // if |
---|
182 | } // if |
---|
183 | |
---|
184 | /************* parameters ***************/ |
---|
185 | |
---|
186 | const std::list<DeclarationWithType *> &pars = funcType->parameters; |
---|
187 | |
---|
188 | if ( pars.empty() ) { |
---|
189 | if ( funcType->get_isVarArgs() ) { |
---|
190 | os << "()"; |
---|
191 | } else { |
---|
192 | os << "(void)"; |
---|
193 | } // if |
---|
194 | } else { |
---|
195 | PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); |
---|
196 | os << "(" ; |
---|
197 | |
---|
198 | cg.pass.genCommaList( pars.begin(), pars.end() ); |
---|
199 | |
---|
200 | if ( funcType->get_isVarArgs() ) { |
---|
201 | os << ", ..."; |
---|
202 | } // if |
---|
203 | os << ")"; |
---|
204 | } // if |
---|
205 | |
---|
206 | typeString = os.str(); |
---|
207 | |
---|
208 | if ( funcType->returnVals.size() == 0 ) { |
---|
209 | typeString = "void " + typeString; |
---|
210 | } else { |
---|
211 | funcType->returnVals.front()->get_type()->accept( *visitor ); |
---|
212 | } // if |
---|
213 | |
---|
214 | // add forall |
---|
215 | if( ! funcType->forall.empty() && ! genC ) { |
---|
216 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
217 | std::ostringstream os; |
---|
218 | PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); |
---|
219 | os << "forall("; |
---|
220 | cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() ); |
---|
221 | os << ")" << std::endl; |
---|
222 | typeString = os.str() + typeString; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | std::string GenType::handleGeneric( ReferenceToType * refType ) { |
---|
227 | if ( ! refType->parameters.empty() ) { |
---|
228 | std::ostringstream os; |
---|
229 | PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); |
---|
230 | os << "("; |
---|
231 | cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() ); |
---|
232 | os << ") "; |
---|
233 | return os.str(); |
---|
234 | } |
---|
235 | return ""; |
---|
236 | } |
---|
237 | |
---|
238 | void GenType::postvisit( StructInstType * structInst ) { |
---|
239 | typeString = structInst->name + handleGeneric( structInst ) + " " + typeString; |
---|
240 | if ( genC ) typeString = "struct " + typeString; |
---|
241 | handleQualifiers( structInst ); |
---|
242 | } |
---|
243 | |
---|
244 | void GenType::postvisit( UnionInstType * unionInst ) { |
---|
245 | typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString; |
---|
246 | if ( genC ) typeString = "union " + typeString; |
---|
247 | handleQualifiers( unionInst ); |
---|
248 | } |
---|
249 | |
---|
250 | void GenType::postvisit( EnumInstType * enumInst ) { |
---|
251 | typeString = enumInst->name + " " + typeString; |
---|
252 | if ( genC ) typeString = "enum " + typeString; |
---|
253 | handleQualifiers( enumInst ); |
---|
254 | } |
---|
255 | |
---|
256 | void GenType::postvisit( TypeInstType * typeInst ) { |
---|
257 | typeString = typeInst->name + " " + typeString; |
---|
258 | handleQualifiers( typeInst ); |
---|
259 | } |
---|
260 | |
---|
261 | void GenType::postvisit( TupleType * tupleType ) { |
---|
262 | assertf( ! genC, "Tuple types should not reach code generation." ); |
---|
263 | unsigned int i = 0; |
---|
264 | std::ostringstream os; |
---|
265 | os << "["; |
---|
266 | for ( Type * t : *tupleType ) { |
---|
267 | i++; |
---|
268 | os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", "); |
---|
269 | } |
---|
270 | os << "] "; |
---|
271 | typeString = os.str() + typeString; |
---|
272 | } |
---|
273 | |
---|
274 | void GenType::postvisit( VarArgsType * varArgsType ) { |
---|
275 | typeString = "__builtin_va_list " + typeString; |
---|
276 | handleQualifiers( varArgsType ); |
---|
277 | } |
---|
278 | |
---|
279 | void GenType::postvisit( ZeroType * zeroType ) { |
---|
280 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
281 | typeString = (pretty ? "zero_t " : "long int ") + typeString; |
---|
282 | handleQualifiers( zeroType ); |
---|
283 | } |
---|
284 | |
---|
285 | void GenType::postvisit( OneType * oneType ) { |
---|
286 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
287 | typeString = (pretty ? "one_t " : "long int ") + typeString; |
---|
288 | handleQualifiers( oneType ); |
---|
289 | } |
---|
290 | |
---|
291 | void GenType::handleQualifiers( Type * type ) { |
---|
292 | if ( type->get_const() ) { |
---|
293 | typeString = "const " + typeString; |
---|
294 | } // if |
---|
295 | if ( type->get_volatile() ) { |
---|
296 | typeString = "volatile " + typeString; |
---|
297 | } // if |
---|
298 | if ( type->get_restrict() ) { |
---|
299 | typeString = "__restrict " + typeString; |
---|
300 | } // if |
---|
301 | if ( type->get_atomic() ) { |
---|
302 | typeString = "_Atomic " + typeString; |
---|
303 | } // if |
---|
304 | if ( type->get_lvalue() && ! genC ) { |
---|
305 | // when not generating C code, print lvalue for debugging. |
---|
306 | typeString = "lvalue " + typeString; |
---|
307 | } |
---|
308 | } |
---|
309 | } // namespace CodeGen |
---|
310 | |
---|
311 | // Local Variables: // |
---|
312 | // tab-width: 4 // |
---|
313 | // mode: c++ // |
---|
314 | // compile-command: "make install" // |
---|
315 | // End: // |
---|