source: src/CodeGen/GenType.cc@ 44adf1b

Last change on this file since 44adf1b was 0522ebe, checked in by JiadaL <j82liang@…>, 19 months ago

Add EnumPosType to type system

  • Property mode set to 100644
File size: 10.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 : Andrew Beach
12// Last Modified On : Fri May 20 11:18:00 2022
13// Update Count : 24
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 "AST/Print.hpp" // for print
22#include "AST/Vector.hpp" // for vector
23#include "CodeGenerator.hpp" // for CodeGenerator
24#include "Common/UniqueName.h" // for UniqueName
25
26namespace CodeGen {
27
28namespace {
29
30struct GenType final :
31 public ast::WithShortCircuiting,
32 public ast::WithVisitorRef<GenType> {
33 std::string result;
34 GenType( const std::string &typeString, const Options &options );
35
36 void previsit( ast::Node const * );
37 void postvisit( ast::Node const * );
38
39 void postvisit( ast::FunctionType const * type );
40 void postvisit( ast::VoidType const * type );
41 void postvisit( ast::BasicType const * type );
42 void postvisit( ast::PointerType const * type );
43 void postvisit( ast::ArrayType const * type );
44 void postvisit( ast::ReferenceType const * type );
45 void postvisit( ast::StructInstType const * type );
46 void postvisit( ast::UnionInstType const * type );
47 void postvisit( ast::EnumInstType const * type );
48 void postvisit( ast::EnumPosType const * type );
49 void postvisit( ast::TypeInstType const * type );
50 void postvisit( ast::TupleType const * type );
51 void postvisit( ast::VarArgsType const * type );
52 void postvisit( ast::ZeroType const * type );
53 void postvisit( ast::OneType const * type );
54 void postvisit( ast::GlobalScopeType const * type );
55 void postvisit( ast::TraitInstType const * type );
56 void postvisit( ast::TypeofType const * type );
57 void postvisit( ast::VTableType const * type );
58 void postvisit( ast::QualifiedType const * type );
59
60private:
61 void handleQualifiers( ast::Type const *type );
62 std::string handleGeneric( ast::BaseInstType const * type );
63 void genArray( const ast::CV::Qualifiers &qualifiers, ast::Type const *base, ast::Expr const *dimension, bool isVarLen, bool isStatic );
64 std::string genParamList( const ast::vector<ast::Type> & );
65
66 Options options;
67};
68
69GenType::GenType( const std::string &typeString, const Options &options ) : result( typeString ), options( options ) {}
70
71void GenType::previsit( ast::Node const * ) {
72 // Turn off automatic recursion for all nodes, to allow each visitor to
73 // precisely control the order in which its children are visited.
74 visit_children = false;
75}
76
77void GenType::postvisit( ast::Node const * node ) {
78 std::stringstream ss;
79 ast::print( ss, node );
80 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
81}
82
83void GenType::postvisit( ast::VoidType const * type ) {
84 result = "void " + result;
85 handleQualifiers( type );
86}
87
88void GenType::postvisit( ast::BasicType const * type ) {
89 ast::BasicType::Kind kind = type->kind;
90 assert( 0 <= kind && kind < ast::BasicType::NUMBER_OF_BASIC_TYPES );
91 result = std::string( ast::BasicType::typeNames[kind] ) + " " + result;
92 handleQualifiers( type );
93}
94
95void GenType::genArray( const ast::CV::Qualifiers & qualifiers, ast::Type const * base, ast::Expr const *dimension, bool isVarLen, bool isStatic ) {
96 std::ostringstream os;
97 if ( result != "" ) {
98 if ( result[ 0 ] == '*' ) {
99 os << "(" << result << ")";
100 } else {
101 os << result;
102 }
103 }
104 os << "[";
105 if ( isStatic ) {
106 os << "static ";
107 }
108 if ( qualifiers.is_const ) {
109 os << "const ";
110 }
111 if ( qualifiers.is_volatile ) {
112 os << "volatile ";
113 }
114 if ( qualifiers.is_restrict ) {
115 os << "__restrict ";
116 }
117 if ( qualifiers.is_atomic ) {
118 os << "_Atomic ";
119 }
120 if ( dimension != 0 ) {
121 ast::Pass<CodeGenerator>::read( dimension, os, options );
122 } else if ( isVarLen ) {
123 // no dimension expression on a VLA means it came in with the * token
124 os << "*";
125 }
126 os << "]";
127
128 result = os.str();
129
130 base->accept( *visitor );
131}
132
133void GenType::postvisit( ast::PointerType const * type ) {
134 if ( type->isStatic || type->isVarLen || type->dimension ) {
135 genArray( type->qualifiers, type->base, type->dimension, type->isVarLen, type->isStatic );
136 } else {
137 handleQualifiers( type );
138 if ( result[ 0 ] == '?' ) {
139 result = "* " + result;
140 } else {
141 result = "*" + result;
142 }
143 type->base->accept( *visitor );
144 }
145}
146
147void GenType::postvisit( ast::ArrayType const * type ) {
148 genArray( type->qualifiers, type->base, type->dimension, type->isVarLen, type->isStatic );
149}
150
151void GenType::postvisit( ast::ReferenceType const * type ) {
152 assertf( !options.genC, "Reference types should not reach code generation." );
153 handleQualifiers( type );
154 result = "&" + result;
155 type->base->accept( *visitor );
156}
157
158void GenType::postvisit( ast::FunctionType const * type ) {
159 std::ostringstream os;
160
161 if ( result != "" ) {
162 if ( result[ 0 ] == '*' ) {
163 os << "(" << result << ")";
164 } else {
165 os << result;
166 }
167 }
168
169 if ( type->params.empty() ) {
170 if ( type->isVarArgs ) {
171 os << "()";
172 } else {
173 os << "(void)";
174 }
175 } else {
176 os << "(" ;
177
178 os << genParamList( type->params );
179
180 if ( type->isVarArgs ) {
181 os << ", ...";
182 }
183 os << ")";
184 }
185
186 result = os.str();
187
188 if ( type->returns.size() == 0 ) {
189 result = "void " + result;
190 } else {
191 type->returns.front()->accept( *visitor );
192 }
193
194 // Add forall clause.
195 if( !type->forall.empty() && !options.genC ) {
196 //assertf( !options.genC, "FunctionDecl type parameters should not reach code generation." );
197 std::ostringstream os;
198 ast::Pass<CodeGenerator> cg( os, options );
199 os << "forall(";
200 cg.core.genCommaList( type->forall );
201 os << ")" << std::endl;
202 result = os.str() + result;
203 }
204}
205
206std::string GenType::handleGeneric( ast::BaseInstType const * type ) {
207 if ( !type->params.empty() ) {
208 std::ostringstream os;
209 ast::Pass<CodeGenerator> cg( os, options );
210 os << "(";
211 cg.core.genCommaList( type->params );
212 os << ") ";
213 return os.str();
214 }
215 return "";
216}
217
218void GenType::postvisit( ast::StructInstType const * type ) {
219 result = type->name + handleGeneric( type ) + " " + result;
220 if ( options.genC ) result = "struct " + result;
221 handleQualifiers( type );
222}
223
224void GenType::postvisit( ast::UnionInstType const * type ) {
225 result = type->name + handleGeneric( type ) + " " + result;
226 if ( options.genC ) result = "union " + result;
227 handleQualifiers( type );
228}
229
230void GenType::postvisit( ast::EnumInstType const * type ) {
231 // if ( type->base && type->base->base ) {
232 // result = genType( type->base->base, result, options );
233 // } else {
234 result = type->name + " " + result;
235 if ( options.genC ) {
236 result = "enum " + result;
237 }
238 // }
239 handleQualifiers( type );
240}
241
242void GenType::postvisit( ast::EnumPosType const * type ) {
243 postvisit( type->instance );
244}
245
246void GenType::postvisit( ast::TypeInstType const * type ) {
247 assertf( !options.genC, "TypeInstType should not reach code generation." );
248 result = type->name + " " + result;
249 handleQualifiers( type );
250}
251
252void GenType::postvisit( ast::TupleType const * type ) {
253 assertf( !options.genC, "TupleType should not reach code generation." );
254 unsigned int i = 0;
255 std::ostringstream os;
256 os << "[";
257 for ( ast::ptr<ast::Type> const & t : type->types ) {
258 i++;
259 os << genType( t, "", options ) << (i == type->size() ? "" : ", ");
260 }
261 os << "] ";
262 result = os.str() + result;
263}
264
265void GenType::postvisit( ast::VarArgsType const * type ) {
266 result = "__builtin_va_list " + result;
267 handleQualifiers( type );
268}
269
270void GenType::postvisit( ast::ZeroType const * type ) {
271 // Ideally these wouldn't hit codegen at all, but should be safe to make them ints.
272 result = (options.pretty ? "zero_t " : "long int ") + result;
273 handleQualifiers( type );
274}
275
276void GenType::postvisit( ast::OneType const * type ) {
277 // Ideally these wouldn't hit codegen at all, but should be safe to make them ints.
278 result = (options.pretty ? "one_t " : "long int ") + result;
279 handleQualifiers( type );
280}
281
282void GenType::postvisit( ast::GlobalScopeType const * type ) {
283 assertf( !options.genC, "GlobalScopeType should not reach code generation." );
284 handleQualifiers( type );
285}
286
287void GenType::postvisit( ast::TraitInstType const * type ) {
288 assertf( !options.genC, "TraitInstType should not reach code generation." );
289 result = type->name + " " + result;
290 handleQualifiers( type );
291}
292
293void GenType::postvisit( ast::TypeofType const * type ) {
294 std::ostringstream os;
295 os << "typeof(";
296 ast::Pass<CodeGenerator>::read( type->expr.get(), os, options );
297 os << ") " << result;
298 result = os.str();
299 handleQualifiers( type );
300}
301
302void GenType::postvisit( ast::VTableType const * type ) {
303 assertf( !options.genC, "Virtual table types should not reach code generation." );
304 std::ostringstream os;
305 os << "vtable(" << genType( type->base, "", options ) << ") " << result;
306 result = os.str();
307 handleQualifiers( type );
308}
309
310void GenType::postvisit( ast::QualifiedType const * type ) {
311 assertf( !options.genC, "QualifiedType should not reach code generation." );
312 std::ostringstream os;
313 os << genType( type->parent, "", options ) << "." << genType( type->child, "", options ) << result;
314 result = os.str();
315 handleQualifiers( type );
316}
317
318void GenType::handleQualifiers( ast::Type const * type ) {
319 if ( type->is_const() ) {
320 result = "const " + result;
321 }
322 if ( type->is_volatile() ) {
323 result = "volatile " + result;
324 }
325 if ( type->is_restrict() ) {
326 result = "__restrict " + result;
327 }
328 if ( type->is_atomic() ) {
329 result = "_Atomic " + result;
330 }
331}
332
333std::string GenType::genParamList( const ast::vector<ast::Type> & range ) {
334 auto cur = range.begin();
335 auto end = range.end();
336 if ( cur == end ) return "";
337 std::ostringstream oss;
338 UniqueName param( "__param_" );
339 while ( true ) {
340 oss << genType( *cur++, options.genC ? param.newName() : "", options );
341 if ( cur == end ) break;
342 oss << ", ";
343 }
344 return oss.str();
345}
346
347} // namespace
348
349std::string genType( ast::Type const * type, const std::string & base, const Options & options ) {
350 std::ostringstream os;
351 if ( !type->attributes.empty() ) {
352 ast::Pass<CodeGenerator> cg( os, options );
353 cg.core.genAttributes( type->attributes );
354 }
355
356 return os.str() + ast::Pass<GenType>::read( type, base, options );
357}
358
359std::string genTypeNoAttr( ast::Type const * type, const std::string & base, const Options & options ) {
360 return ast::Pass<GenType>::read( type, base, options );
361}
362
363} // namespace CodeGen
364
365// Local Variables: //
366// tab-width: 4 //
367// mode: c++ //
368// compile-command: "make install" //
369// End: //
Note: See TracBrowser for help on using the repository browser.