source: src/CodeGen/GenType.cc@ 58b6d1b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 58b6d1b was 450805a, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add codegen for variable with qualified type

  • Property mode set to 100644
File size: 10.9 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#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
27namespace 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 void postvisit( GlobalScopeType * globalType );
51 void postvisit( TraitInstType * inst );
52 void postvisit( TypeofType * typeof );
53 void postvisit( QualifiedType * qualType );
54
55 private:
56 void handleQualifiers( Type *type );
57 std::string handleGeneric( ReferenceToType * refType );
58 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
59
60 std::string typeString;
61 bool pretty = false; // pretty print
62 bool genC = false; // generating C code?
63 bool lineMarks = false;
64 };
65
66 std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
67 PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks );
68 std::ostringstream os;
69
70 if ( ! type->get_attributes().empty() ) {
71 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
72 cg.pass.genAttributes( type->get_attributes() );
73 } // if
74
75 type->accept( gt );
76 return os.str() + gt.pass.get_typeString();
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, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
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, pretty, genC, lineMarks );
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( ! 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, pretty, genC, lineMarks );
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() && ! genC ) {
220 // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
221 std::ostringstream os;
222 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
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, pretty, genC, lineMarks );
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 ( genC ) typeString = "struct " + typeString;
245 handleQualifiers( structInst );
246 }
247
248 void GenType::postvisit( UnionInstType * unionInst ) {
249 typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
250 if ( genC ) typeString = "union " + typeString;
251 handleQualifiers( unionInst );
252 }
253
254 void GenType::postvisit( EnumInstType * enumInst ) {
255 typeString = enumInst->name + " " + typeString;
256 if ( genC ) typeString = "enum " + typeString;
257 handleQualifiers( enumInst );
258 }
259
260 void GenType::postvisit( TypeInstType * typeInst ) {
261 typeString = typeInst->name + " " + typeString;
262 handleQualifiers( typeInst );
263 }
264
265 void GenType::postvisit( TupleType * tupleType ) {
266 assertf( ! genC, "Tuple types should not reach code generation." );
267 unsigned int i = 0;
268 std::ostringstream os;
269 os << "[";
270 for ( Type * t : *tupleType ) {
271 i++;
272 os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", ");
273 }
274 os << "] ";
275 typeString = os.str() + typeString;
276 }
277
278 void GenType::postvisit( VarArgsType * varArgsType ) {
279 typeString = "__builtin_va_list " + typeString;
280 handleQualifiers( varArgsType );
281 }
282
283 void GenType::postvisit( ZeroType * zeroType ) {
284 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
285 typeString = (pretty ? "zero_t " : "long int ") + typeString;
286 handleQualifiers( zeroType );
287 }
288
289 void GenType::postvisit( OneType * oneType ) {
290 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
291 typeString = (pretty ? "one_t " : "long int ") + typeString;
292 handleQualifiers( oneType );
293 }
294
295 void GenType::postvisit( GlobalScopeType * globalType ) {
296 assertf( ! genC, "Global scope type should not reach code generation." );
297 handleQualifiers( globalType );
298 }
299
300 void GenType::postvisit( TraitInstType * inst ) {
301 assertf( ! genC, "Trait types should not reach code generation." );
302 typeString = inst->name + " " + typeString;
303 handleQualifiers( inst );
304 }
305
306 void GenType::postvisit( TypeofType * typeof ) {
307 std::ostringstream os;
308 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
309 os << "typeof(";
310 typeof->expr->accept( cg );
311 os << ") " << typeString;
312 typeString = os.str();
313 handleQualifiers( typeof );
314 }
315
316 void GenType::postvisit( QualifiedType * qualType ) {
317 assertf( ! genC, "Qualified types should not reach code generation." );
318 std::ostringstream os;
319 os << genType( qualType->parent, "", pretty, genC, lineMarks ) << "." << genType( qualType->child, "", pretty, genC, lineMarks ) << typeString;
320 typeString = os.str();
321 handleQualifiers( qualType );
322 }
323
324 void GenType::handleQualifiers( Type * type ) {
325 if ( type->get_const() ) {
326 typeString = "const " + typeString;
327 } // if
328 if ( type->get_volatile() ) {
329 typeString = "volatile " + typeString;
330 } // if
331 if ( type->get_restrict() ) {
332 typeString = "__restrict " + typeString;
333 } // if
334 if ( type->get_atomic() ) {
335 typeString = "_Atomic " + typeString;
336 } // if
337 if ( type->get_lvalue() && ! genC ) {
338 // when not generating C code, print lvalue for debugging.
339 typeString = "lvalue " + typeString;
340 }
341 }
342} // namespace CodeGen
343
344// Local Variables: //
345// tab-width: 4 //
346// mode: c++ //
347// compile-command: "make install" //
348// End: //
Note: See TracBrowser for help on using the repository browser.