source: src/AST/Print.cpp@ 0b57626

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 0b57626 was c957e7f, checked in by Aaron Moss <a3moss@…>, 7 years ago

Added initializer printers

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