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 | // Print.cpp -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue May 21 16:20:15 2019 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #include "Print.hpp" |
---|
17 | |
---|
18 | #include "Decl.hpp" |
---|
19 | #include "Expr.hpp" |
---|
20 | #include "Stmt.hpp" |
---|
21 | #include "Type.hpp" |
---|
22 | #include "TypeSubstitution.hpp" |
---|
23 | |
---|
24 | |
---|
25 | namespace ast { |
---|
26 | |
---|
27 | template <typename C, typename... T> |
---|
28 | constexpr auto make_array(T&&... values) -> |
---|
29 | std::array<C,sizeof...(T)> |
---|
30 | { |
---|
31 | return std::array<C,sizeof...(T)>{ |
---|
32 | std::forward<T>(values)... |
---|
33 | }; |
---|
34 | } |
---|
35 | |
---|
36 | class Printer : public Visitor { |
---|
37 | public: |
---|
38 | std::ostream & os; |
---|
39 | Indenter indent; |
---|
40 | |
---|
41 | Printer(std::ostream & os, Indenter indent) : os( os ), indent( indent ) {} |
---|
42 | |
---|
43 | private: |
---|
44 | template< typename C > |
---|
45 | void printAll( const C & c ) { |
---|
46 | for ( const auto & i : c ) { |
---|
47 | if ( i ) { |
---|
48 | os << indent; |
---|
49 | i->accept( *this ); |
---|
50 | // need an endl after each element because it's not |
---|
51 | // easy to know when each individual item should end |
---|
52 | os << std::endl; |
---|
53 | } // if |
---|
54 | } // for |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | static const char* Names[]; |
---|
59 | |
---|
60 | struct Names { |
---|
61 | static constexpr auto FuncSpecifiers = make_array<const char*>( |
---|
62 | "inline", "_Noreturn", "fortran" |
---|
63 | ); |
---|
64 | |
---|
65 | static constexpr auto StorageClasses = make_array<const char*>( |
---|
66 | "extern", "static", "auto", "register", "_Thread_local" |
---|
67 | ); |
---|
68 | |
---|
69 | static constexpr auto Qualifiers = make_array<const char*>( |
---|
70 | "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic" |
---|
71 | ); |
---|
72 | }; |
---|
73 | |
---|
74 | template<typename storage_t, size_t N> |
---|
75 | void print(const storage_t & storage, const std::array<const char *, N> & Names ) { |
---|
76 | if ( storage.any() ) { |
---|
77 | for ( size_t i = 0; i < Names.size(); i += 1 ) { |
---|
78 | if ( storage[i] ) { |
---|
79 | os << Names[i] << ' '; |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void print( const ast::Function::Specs & specs ) { |
---|
86 | print(specs, Names::FuncSpecifiers); |
---|
87 | } |
---|
88 | |
---|
89 | void print( const ast::Storage::Classes & storage ) { |
---|
90 | print(storage, Names::StorageClasses); |
---|
91 | } |
---|
92 | |
---|
93 | void print( const ast::CV::Qualifiers & qualifiers ) { |
---|
94 | print(qualifiers, Names::Qualifiers); |
---|
95 | } |
---|
96 | |
---|
97 | public: |
---|
98 | virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) { |
---|
99 | if ( node->name != "" ) os << node->name << ": "; |
---|
100 | |
---|
101 | if ( node->linkage != Linkage::Cforall ) { |
---|
102 | os << Linkage::name( node->linkage ) << " "; |
---|
103 | } // if |
---|
104 | |
---|
105 | print( node->storage ); |
---|
106 | |
---|
107 | if ( node->type ) { |
---|
108 | node->type->accept( *this ); |
---|
109 | } else { |
---|
110 | os << " untyped entity "; |
---|
111 | } // if |
---|
112 | |
---|
113 | if ( node->init ) { |
---|
114 | os << " with initializer (" << ( |
---|
115 | node->init->maybeConstructed |
---|
116 | ? "maybe constructed" |
---|
117 | : "not constructed" |
---|
118 | ) << ")" << std::endl << indent+1; |
---|
119 | |
---|
120 | ++indent; |
---|
121 | node->init->accept( *this ); |
---|
122 | --indent; |
---|
123 | os << std::endl; |
---|
124 | } // if |
---|
125 | |
---|
126 | if ( ! node->attributes.empty() ) { |
---|
127 | os << std::endl << indent << "... with attributes:" << std::endl; |
---|
128 | ++indent; |
---|
129 | printAll( node->attributes ); |
---|
130 | --indent; |
---|
131 | } |
---|
132 | |
---|
133 | if ( node->bitfieldWidth ) { |
---|
134 | os << indent << " with bitfield width "; |
---|
135 | node->bitfieldWidth->accept( *this ); |
---|
136 | } // if |
---|
137 | return node; |
---|
138 | } |
---|
139 | |
---|
140 | virtual const ast::DeclWithType * visit( const ast::FunctionDecl * node ) { |
---|
141 | return node; |
---|
142 | } |
---|
143 | |
---|
144 | virtual const ast::Decl * visit( const ast::StructDecl * node ) { |
---|
145 | return node; |
---|
146 | } |
---|
147 | |
---|
148 | virtual const ast::Decl * visit( const ast::UnionDecl * node ) { |
---|
149 | return node; |
---|
150 | } |
---|
151 | |
---|
152 | virtual const ast::Decl * visit( const ast::EnumDecl * node ) { |
---|
153 | return node; |
---|
154 | } |
---|
155 | |
---|
156 | virtual const ast::Decl * visit( const ast::TraitDecl * node ) { |
---|
157 | return node; |
---|
158 | } |
---|
159 | |
---|
160 | virtual const ast::Decl * visit( const ast::TypeDecl * node ) { |
---|
161 | return node; |
---|
162 | } |
---|
163 | |
---|
164 | virtual const ast::Decl * visit( const ast::TypedefDecl * node ) { |
---|
165 | return node; |
---|
166 | } |
---|
167 | |
---|
168 | virtual const ast::AsmDecl * visit( const ast::AsmDecl * node ) { |
---|
169 | return node; |
---|
170 | } |
---|
171 | |
---|
172 | virtual const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) { |
---|
173 | return node; |
---|
174 | } |
---|
175 | |
---|
176 | virtual const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) { |
---|
177 | return node; |
---|
178 | } |
---|
179 | |
---|
180 | virtual const ast::Stmt * visit( const ast::ExprStmt * node ) { |
---|
181 | return node; |
---|
182 | } |
---|
183 | |
---|
184 | virtual const ast::Stmt * visit( const ast::AsmStmt * node ) { |
---|
185 | return node; |
---|
186 | } |
---|
187 | |
---|
188 | virtual const ast::Stmt * visit( const ast::DirectiveStmt * node ) { |
---|
189 | return node; |
---|
190 | } |
---|
191 | |
---|
192 | virtual const ast::Stmt * visit( const ast::IfStmt * node ) { |
---|
193 | return node; |
---|
194 | } |
---|
195 | |
---|
196 | virtual const ast::Stmt * visit( const ast::WhileStmt * node ) { |
---|
197 | return node; |
---|
198 | } |
---|
199 | |
---|
200 | virtual const ast::Stmt * visit( const ast::ForStmt * node ) { |
---|
201 | return node; |
---|
202 | } |
---|
203 | |
---|
204 | virtual const ast::Stmt * visit( const ast::SwitchStmt * node ) { |
---|
205 | return node; |
---|
206 | } |
---|
207 | |
---|
208 | virtual const ast::Stmt * visit( const ast::CaseStmt * node ) { |
---|
209 | return node; |
---|
210 | } |
---|
211 | |
---|
212 | virtual const ast::Stmt * visit( const ast::BranchStmt * node ) { |
---|
213 | return node; |
---|
214 | } |
---|
215 | |
---|
216 | virtual const ast::Stmt * visit( const ast::ReturnStmt * node ) { |
---|
217 | return node; |
---|
218 | } |
---|
219 | |
---|
220 | virtual const ast::Stmt * visit( const ast::ThrowStmt * node ) { |
---|
221 | return node; |
---|
222 | } |
---|
223 | |
---|
224 | virtual const ast::Stmt * visit( const ast::TryStmt * node ) { |
---|
225 | return node; |
---|
226 | } |
---|
227 | |
---|
228 | virtual const ast::Stmt * visit( const ast::CatchStmt * node ) { |
---|
229 | return node; |
---|
230 | } |
---|
231 | |
---|
232 | virtual const ast::Stmt * visit( const ast::FinallyStmt * node ) { |
---|
233 | return node; |
---|
234 | } |
---|
235 | |
---|
236 | virtual const ast::Stmt * visit( const ast::WaitForStmt * node ) { |
---|
237 | return node; |
---|
238 | } |
---|
239 | |
---|
240 | virtual const ast::Stmt * visit( const ast::WithStmt * node ) { |
---|
241 | return node; |
---|
242 | } |
---|
243 | |
---|
244 | virtual const ast::NullStmt * visit( const ast::NullStmt * node ) { |
---|
245 | return node; |
---|
246 | } |
---|
247 | |
---|
248 | virtual const ast::Stmt * visit( const ast::DeclStmt * node ) { |
---|
249 | return node; |
---|
250 | } |
---|
251 | |
---|
252 | virtual const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) { |
---|
253 | return node; |
---|
254 | } |
---|
255 | |
---|
256 | virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) { |
---|
257 | return node; |
---|
258 | } |
---|
259 | |
---|
260 | virtual const ast::Expr * visit( const ast::UntypedExpr * node ) { |
---|
261 | return node; |
---|
262 | } |
---|
263 | |
---|
264 | virtual const ast::Expr * visit( const ast::NameExpr * node ) { |
---|
265 | return node; |
---|
266 | } |
---|
267 | |
---|
268 | virtual const ast::Expr * visit( const ast::AddressExpr * node ) { |
---|
269 | return node; |
---|
270 | } |
---|
271 | |
---|
272 | virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) { |
---|
273 | return node; |
---|
274 | } |
---|
275 | |
---|
276 | virtual const ast::Expr * visit( const ast::CastExpr * node ) { |
---|
277 | return node; |
---|
278 | } |
---|
279 | |
---|
280 | virtual const ast::Expr * visit( const ast::KeywordCastExpr * node ) { |
---|
281 | return node; |
---|
282 | } |
---|
283 | |
---|
284 | virtual const ast::Expr * visit( const ast::VirtualCastExpr * node ) { |
---|
285 | return node; |
---|
286 | } |
---|
287 | |
---|
288 | virtual const ast::Expr * visit( const ast::UntypedMemberExpr * node ) { |
---|
289 | return node; |
---|
290 | } |
---|
291 | |
---|
292 | virtual const ast::Expr * visit( const ast::MemberExpr * node ) { |
---|
293 | return node; |
---|
294 | } |
---|
295 | |
---|
296 | virtual const ast::Expr * visit( const ast::VariableExpr * node ) { |
---|
297 | return node; |
---|
298 | } |
---|
299 | |
---|
300 | virtual const ast::Expr * visit( const ast::ConstantExpr * node ) { |
---|
301 | return node; |
---|
302 | } |
---|
303 | |
---|
304 | virtual const ast::Expr * visit( const ast::SizeofExpr * node ) { |
---|
305 | return node; |
---|
306 | } |
---|
307 | |
---|
308 | virtual const ast::Expr * visit( const ast::AlignofExpr * node ) { |
---|
309 | return node; |
---|
310 | } |
---|
311 | |
---|
312 | virtual const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) { |
---|
313 | return node; |
---|
314 | } |
---|
315 | |
---|
316 | virtual const ast::Expr * visit( const ast::OffsetofExpr * node ) { |
---|
317 | return node; |
---|
318 | } |
---|
319 | |
---|
320 | virtual const ast::Expr * visit( const ast::OffsetPackExpr * node ) { |
---|
321 | return node; |
---|
322 | } |
---|
323 | |
---|
324 | virtual const ast::Expr * visit( const ast::LogicalExpr * node ) { |
---|
325 | return node; |
---|
326 | } |
---|
327 | |
---|
328 | virtual const ast::Expr * visit( const ast::ConditionalExpr * node ) { |
---|
329 | return node; |
---|
330 | } |
---|
331 | |
---|
332 | virtual const ast::Expr * visit( const ast::CommaExpr * node ) { |
---|
333 | return node; |
---|
334 | } |
---|
335 | |
---|
336 | virtual const ast::Expr * visit( const ast::TypeExpr * node ) { |
---|
337 | return node; |
---|
338 | } |
---|
339 | |
---|
340 | virtual const ast::Expr * visit( const ast::AsmExpr * node ) { |
---|
341 | return node; |
---|
342 | } |
---|
343 | |
---|
344 | virtual const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) { |
---|
345 | return node; |
---|
346 | } |
---|
347 | |
---|
348 | virtual const ast::Expr * visit( const ast::ConstructorExpr * node ) { |
---|
349 | return node; |
---|
350 | } |
---|
351 | |
---|
352 | virtual const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) { |
---|
353 | return node; |
---|
354 | } |
---|
355 | |
---|
356 | virtual const ast::Expr * visit( const ast::RangeExpr * node ) { |
---|
357 | return node; |
---|
358 | } |
---|
359 | |
---|
360 | virtual const ast::Expr * visit( const ast::UntypedTupleExpr * node ) { |
---|
361 | return node; |
---|
362 | } |
---|
363 | |
---|
364 | virtual const ast::Expr * visit( const ast::TupleExpr * node ) { |
---|
365 | return node; |
---|
366 | } |
---|
367 | |
---|
368 | virtual const ast::Expr * visit( const ast::TupleIndexExpr * node ) { |
---|
369 | return node; |
---|
370 | } |
---|
371 | |
---|
372 | virtual const ast::Expr * visit( const ast::TupleAssignExpr * node ) { |
---|
373 | return node; |
---|
374 | } |
---|
375 | |
---|
376 | virtual const ast::Expr * visit( const ast::StmtExpr * node ) { |
---|
377 | return node; |
---|
378 | } |
---|
379 | |
---|
380 | virtual const ast::Expr * visit( const ast::UniqueExpr * node ) { |
---|
381 | return node; |
---|
382 | } |
---|
383 | |
---|
384 | virtual const ast::Expr * visit( const ast::UntypedInitExpr * node ) { |
---|
385 | return node; |
---|
386 | } |
---|
387 | |
---|
388 | virtual const ast::Expr * visit( const ast::InitExpr * node ) { |
---|
389 | return node; |
---|
390 | } |
---|
391 | |
---|
392 | virtual const ast::Expr * visit( const ast::DeletedExpr * node ) { |
---|
393 | return node; |
---|
394 | } |
---|
395 | |
---|
396 | virtual const ast::Expr * visit( const ast::DefaultArgExpr * node ) { |
---|
397 | return node; |
---|
398 | } |
---|
399 | |
---|
400 | virtual const ast::Expr * visit( const ast::GenericExpr * node ) { |
---|
401 | return node; |
---|
402 | } |
---|
403 | |
---|
404 | virtual const ast::Type * visit( const ast::VoidType * node ) { |
---|
405 | return node; |
---|
406 | } |
---|
407 | |
---|
408 | virtual const ast::Type * visit( const ast::BasicType * node ) { |
---|
409 | return node; |
---|
410 | } |
---|
411 | |
---|
412 | virtual const ast::Type * visit( const ast::PointerType * node ) { |
---|
413 | return node; |
---|
414 | } |
---|
415 | |
---|
416 | virtual const ast::Type * visit( const ast::ArrayType * node ) { |
---|
417 | return node; |
---|
418 | } |
---|
419 | |
---|
420 | virtual const ast::Type * visit( const ast::ReferenceType * node ) { |
---|
421 | return node; |
---|
422 | } |
---|
423 | |
---|
424 | virtual const ast::Type * visit( const ast::QualifiedType * node ) { |
---|
425 | return node; |
---|
426 | } |
---|
427 | |
---|
428 | virtual const ast::Type * visit( const ast::FunctionType * node ) { |
---|
429 | return node; |
---|
430 | } |
---|
431 | |
---|
432 | virtual const ast::Type * visit( const ast::StructInstType * node ) { |
---|
433 | return node; |
---|
434 | } |
---|
435 | |
---|
436 | virtual const ast::Type * visit( const ast::UnionInstType * node ) { |
---|
437 | return node; |
---|
438 | } |
---|
439 | |
---|
440 | virtual const ast::Type * visit( const ast::EnumInstType * node ) { |
---|
441 | return node; |
---|
442 | } |
---|
443 | |
---|
444 | virtual const ast::Type * visit( const ast::TraitInstType * node ) { |
---|
445 | return node; |
---|
446 | } |
---|
447 | |
---|
448 | virtual const ast::Type * visit( const ast::TypeInstType * node ) { |
---|
449 | return node; |
---|
450 | } |
---|
451 | |
---|
452 | virtual const ast::Type * visit( const ast::TupleType * node ) { |
---|
453 | return node; |
---|
454 | } |
---|
455 | |
---|
456 | virtual const ast::Type * visit( const ast::TypeofType * node ) { |
---|
457 | return node; |
---|
458 | } |
---|
459 | |
---|
460 | virtual const ast::Type * visit( const ast::VarArgsType * node ) { |
---|
461 | return node; |
---|
462 | } |
---|
463 | |
---|
464 | virtual const ast::Type * visit( const ast::ZeroType * node ) { |
---|
465 | return node; |
---|
466 | } |
---|
467 | |
---|
468 | virtual const ast::Type * visit( const ast::OneType * node ) { |
---|
469 | return node; |
---|
470 | } |
---|
471 | |
---|
472 | virtual const ast::Type * visit( const ast::GlobalScopeType * node ) { |
---|
473 | return node; |
---|
474 | } |
---|
475 | |
---|
476 | virtual const ast::Designation * visit( const ast::Designation * node ) { |
---|
477 | return node; |
---|
478 | } |
---|
479 | |
---|
480 | virtual const ast::Init * visit( const ast::SingleInit * node ) { |
---|
481 | return node; |
---|
482 | } |
---|
483 | |
---|
484 | virtual const ast::Init * visit( const ast::ListInit * node ) { |
---|
485 | return node; |
---|
486 | } |
---|
487 | |
---|
488 | virtual const ast::Init * visit( const ast::ConstructorInit * node ) { |
---|
489 | return node; |
---|
490 | } |
---|
491 | |
---|
492 | virtual const ast::Attribute * visit( const ast::Attribute * node ) { |
---|
493 | return node; |
---|
494 | } |
---|
495 | |
---|
496 | virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) { |
---|
497 | return node; |
---|
498 | } |
---|
499 | |
---|
500 | }; |
---|
501 | |
---|
502 | void print( std::ostream & os, const ast::Node * node, Indenter indent ) { |
---|
503 | Printer printer { os, indent }; |
---|
504 | node->accept(printer); |
---|
505 | } |
---|
506 | |
---|
507 | // Annoyingly these needed to be defined out of line to avoid undefined references. |
---|
508 | // The size here needs to be explicit but at least the compiler will produce an error |
---|
509 | // if the wrong size is specified |
---|
510 | constexpr std::array<const char*, 3> Printer::Names::FuncSpecifiers; |
---|
511 | constexpr std::array<const char*, 5> Printer::Names::StorageClasses; |
---|
512 | constexpr std::array<const char*, 6> Printer::Names::Qualifiers; |
---|
513 | } |
---|