source: src/AST/Print.cpp@ 148ba7d

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 148ba7d was 6cebfef, checked in by caparsons <caparson@…>, 4 years ago

added mutex stmt monitor

  • Property mode set to 100644
File size: 36.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#include "CompilationState.h"
24
25#include "Common/utility.h" // for group_iterate
26
27using namespace std;
28
29namespace ast {
30
31template <typename C, typename... T>
32constexpr array<C,sizeof...(T)> make_array(T&&... values)
33{
34 return array<C,sizeof...(T)>{
35 forward<T>(values)...
36 };
37}
38
39class Printer final : public Visitor {
40public:
41 ostream & os;
42 Indenter indent;
43 bool short_mode;
44
45 Printer(ostream & os, Indenter indent, bool short_mode) : os( os ), indent( indent ), short_mode(short_mode) {}
46
47private:
48 template< typename C >
49 void printAll( const C & c ) {
50 for ( const auto & i : c ) {
51 if ( i ) {
52 os << indent;
53 i->accept( *this );
54 // need an endl after each element because it's not
55 // easy to know when each individual item should end
56 os << endl;
57 } // if
58 } // for
59 }
60
61 /// call if mandatory field is missing
62 void undefined() {
63 os << "UNDEFINED";
64 }
65
66 /// call for fields that should be mandatory
67 void safe_print( const ast::Node * n ) {
68 if ( n ) n->accept( *this );
69 else undefined();
70 }
71
72 /// call to print short form. Incorporates features of safe_print()
73 void short_print( const ast::Decl * n ) {
74 if ( ! n ) { undefined(); return; }
75 bool old_short = short_mode; short_mode = true;
76 n->accept( *this );
77 short_mode = old_short;
78 }
79
80 static const char* Names[];
81
82 struct Names {
83 static constexpr auto FuncSpecifiers = make_array<const char*>(
84 "inline", "_Noreturn", "fortran"
85 );
86
87 static constexpr auto StorageClasses = make_array<const char*>(
88 "extern", "static", "auto", "register", "_Thread_local"
89 );
90
91 static constexpr auto Qualifiers = make_array<const char*>(
92 "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic"
93 );
94 };
95
96 template<typename storage_t, size_t N>
97 void print(const storage_t & storage, const array<const char *, N> & Names ) {
98 if ( storage.any() ) {
99 for ( size_t i = 0; i < Names.size(); i += 1 ) {
100 if ( storage[i] ) {
101 os << Names[i] << ' ';
102 }
103 }
104 }
105 }
106
107 void print( const ast::Function::Specs & specs ) {
108 print(specs, Names::FuncSpecifiers);
109 }
110
111 void print( const ast::Storage::Classes & storage ) {
112 print(storage, Names::StorageClasses);
113 }
114
115 void print( const ast::CV::Qualifiers & qualifiers ) {
116 print(qualifiers, Names::Qualifiers);
117 }
118
119 void print( const std::vector<ast::Label> & labels ) {
120 if ( labels.empty() ) return;
121 os << indent << "... Labels: {";
122 bool isFirst = true;
123 for ( const Label & l : labels ) {
124 if ( isFirst ) { isFirst = false; } else { os << ","; }
125 os << l;
126 }
127 os << "}" << endl;
128 }
129
130 void print( const ast::Expr::InferUnion & inferred, unsigned level = 0 ) {
131 if (inferred.data.resnSlots && !inferred.data.resnSlots->empty()) {
132 os << indent << "with " << inferred.data.resnSlots->size()
133 << " pending inference slots" << endl;
134 }
135 if (inferred.data.inferParams && !inferred.data.inferParams->empty()) {
136 os << indent << "with inferred parameters " << level << ":" << endl;
137 ++indent;
138 for ( const auto & i : *inferred.data.inferParams ) {
139 os << indent;
140 short_print( i.second.declptr );
141 os << endl;
142 print( i.second.expr->inferred, level+1 );
143 }
144 --indent;
145 }
146 }
147
148 void print( const ast::FunctionType::ForallList & forall ) {
149 if ( forall.empty() ) return;
150 os << "forall" << endl;
151 ++indent;
152 printAll( forall );
153 os << indent;
154 --indent;
155 }
156
157 void print( const ast::FunctionType::AssertionList & assts ) {
158 if (assts.empty()) return;
159 os << "with assertions" << endl;
160 ++indent;
161 printAll(assts);
162 os << indent;
163 --indent;
164 }
165
166 void print( const std::vector<ptr<Attribute>> & attrs ) {
167 if ( attrs.empty() ) return;
168 os << "with attributes" << endl;
169 ++indent;
170 printAll( attrs );
171 --indent;
172 }
173
174 void print( const std::vector<ptr<Expr>> & params ) {
175 if ( params.empty() ) return;
176 os << endl << indent << "... with parameters" << endl;
177 ++indent;
178 printAll( params );
179 --indent;
180 }
181
182 void print( const ast::AggregateDecl * node ) {
183 os << node->typeString() << " " << node->name;
184
185 if ( ! short_mode && node->linkage != Linkage::Cforall ) {
186 os << " " << Linkage::name( node->linkage );
187 }
188
189 os << " " << (node->body ? "with" : "without") << " body";
190
191 if ( ! node->params.empty() ) {
192 os << endl << indent << "... with parameters" << endl;
193 ++indent;
194 printAll( node->params );
195 --indent;
196 }
197
198 if ( ! short_mode && ! node->members.empty() ) {
199 os << endl << indent << "... with members" << endl;
200 ++indent;
201 printAll( node->members );
202 --indent;
203 }
204
205 if ( ! short_mode && ! node->attributes.empty() ) {
206 os << endl << indent << "... with attributes" << endl;
207 ++indent;
208 printAll( node->attributes );
209 --indent;
210 }
211
212 os << endl;
213 }
214
215 void preprint( const ast::NamedTypeDecl * node ) {
216 if ( ! node->name.empty() ) {
217 os << node->name << ": ";
218 }
219
220 if ( ! short_mode && node->linkage != Linkage::Cforall ) {
221 os << Linkage::name( node->linkage ) << " ";
222 }
223
224 print( node->storage );
225 os << node->typeString();
226
227 if ( node->base ) {
228 os << " for ";
229 ++indent;
230 node->base->accept( *this );
231 --indent;
232 }
233
234 if ( ! node->assertions.empty() ) {
235 os << endl << indent << "... with assertions" << endl;
236 ++indent;
237 printAll( node->assertions );
238 --indent;
239 }
240 }
241
242 void postprint( const ast::Expr * node ) {
243 print( node->inferred );
244
245 if ( node->result ) {
246 os << endl << indent << "... with resolved type:" << endl;
247 ++indent;
248 os << indent;
249 node->result->accept( *this );
250 --indent;
251 }
252
253 if ( node->env ) {
254 os << endl << indent << "... with environment:" << endl;
255 ++indent;
256 node->env->accept( *this );
257 --indent;
258 }
259
260 if ( node->extension ) {
261 os << endl << indent << "... with extension";
262 }
263 }
264
265 void preprint( const ast::Type * node ) {
266 print( node->qualifiers );
267 }
268
269 void preprint( const ast::FunctionType * node ) {
270 print( node->forall );
271 print( node->assertions );
272 print( node->qualifiers );
273 }
274
275 void preprint( const ast::BaseInstType * node ) {
276 print( node->attributes );
277 print( node->qualifiers );
278 }
279
280public:
281 virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
282 if ( ! node->name.empty() ) os << node->name << ": ";
283
284 if ( ! short_mode && node->linkage != Linkage::Cforall ) {
285 os << Linkage::name( node->linkage ) << " ";
286 }
287
288 print( node->storage );
289
290 if ( node->type ) {
291 node->type->accept( *this );
292 } else {
293 os << "untyped entity";
294 }
295
296 if ( ! short_mode && node->init ) {
297 ++indent;
298 os << " with initializer (" << (
299 node->init->maybeConstructed
300 ? "maybe constructed"
301 : "not constructed"
302 ) << ")" << endl << indent;
303 node->init->accept( *this );
304 --indent;
305 os << endl;
306 }
307
308 if ( ! short_mode && ! node->attributes.empty() ) {
309 os << endl << indent << "... with attributes:" << endl;
310 ++indent;
311 printAll( node->attributes );
312 --indent;
313 }
314
315 if ( node->bitfieldWidth ) {
316 os << indent << " with bitfield width ";
317 node->bitfieldWidth->accept( *this );
318 }
319
320 return node;
321 }
322
323 virtual const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
324 if ( !node->name.empty() ) os << node->name << ": ";
325
326 if ( ! short_mode && node->linkage != Linkage::Cforall ) {
327 os << Linkage::name( node->linkage ) << " ";
328 }
329
330 if ( ! short_mode ) printAll( node->attributes );
331
332 print( node->storage );
333 print( node->funcSpec );
334
335 if ( node->type ) {
336 node->type->accept( *this );
337 } else {
338 os << "untyped entity";
339 }
340
341 if ( ! short_mode && node->stmts ) {
342 ++indent;
343 os << " with body" << endl << indent;
344 node->stmts->accept( *this );
345 --indent;
346 }
347
348 return node;
349 }
350
351 virtual const ast::Decl * visit( const ast::StructDecl * node ) override final {
352 print(node);
353 return node;
354 }
355
356 virtual const ast::Decl * visit( const ast::UnionDecl * node ) override final {
357 print(node);
358 return node;
359 }
360
361 virtual const ast::Decl * visit( const ast::EnumDecl * node ) override final {
362 print(node);
363 return node;
364 }
365
366 virtual const ast::Decl * visit( const ast::TraitDecl * node ) override final {
367 print(node);
368 return node;
369 }
370
371 virtual const ast::Decl * visit( const ast::TypeDecl * node ) override final {
372 preprint( node );
373 if ( ! short_mode && node->init ) {
374 os << endl << indent << "with type initializer: ";
375 ++indent;
376 node->init->accept( *this );
377 --indent;
378 }
379
380 return node;
381 }
382
383 virtual const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
384 preprint( node );
385 return node;
386 }
387
388 virtual const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
389 safe_print( node->stmt );
390 return node;
391 }
392
393 virtual const ast::DirectiveDecl * visit( const ast::DirectiveDecl * node ) override final {
394 safe_print( node->stmt );
395 return node;
396 }
397
398 virtual const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
399 os << "Static Assert with condition: ";
400 ++indent;
401 safe_print( node->cond );
402 os << endl << indent-1 << "and message: ";
403 safe_print( node->msg );
404 --indent;
405 os << endl;
406
407 return node;
408 }
409
410 virtual const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
411 os << "Compound Statement:" << endl;
412 ++indent;
413 printAll( node->kids );
414 --indent;
415 return node;
416 }
417
418 virtual const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
419 ++indent;
420 os << "Expression Statement:" << endl << indent;
421 safe_print( node->expr );
422 --indent;
423 return node;
424 }
425
426 virtual const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
427 os << "Assembler Statement:" << endl;
428 ++indent;
429 os << indent-1 << "instruction:" << endl << indent;
430 safe_print( node->instruction );
431 if ( ! node->output.empty() ) {
432 os << endl << indent << "output:" << endl;
433 printAll( node->output );
434 } // if
435 if ( ! node->input.empty() ) {
436 os << indent << "input:" << endl;
437 printAll( node->input );
438 } // if
439 if ( ! node->clobber.empty() ) {
440 os << indent << "clobber:" << endl;
441 printAll( node->clobber );
442 } // if
443 --indent;
444 return node;
445 }
446
447 virtual const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
448 os << "GCC Directive: " << node->directive << endl;
449 return node;
450 }
451
452 virtual const ast::Stmt * visit( const ast::IfStmt * node ) override final {
453 os << "If on condition:" << endl;
454 ++indent;
455 os << indent;
456 safe_print( node->cond );
457 --indent;
458
459 if ( ! node->inits.empty() ) {
460 os << indent << "... with initialization:" << endl;
461 ++indent;
462 for ( const ast::Stmt * stmt : node->inits ) {
463 os << indent;
464 safe_print( stmt );
465 }
466 --indent;
467 os << endl;
468 }
469
470 os << indent << "... then:" << endl;
471
472 ++indent;
473 os << indent;
474 safe_print( node->thenPart );
475 --indent;
476
477 if ( node->elsePart != 0 ) {
478 os << indent << "... else:" << endl;
479 ++indent;
480 os << indent;
481 node->elsePart->accept( *this );
482 --indent;
483 } // if
484 return node;
485 }
486
487 virtual const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
488 if ( node->isDoWhile ) { os << "Do-"; }
489 os << "While on condition:" << endl;
490 ++indent;
491 safe_print( node->cond );
492 os << indent-1 << "... with body:" << endl;
493 safe_print( node->body );
494
495 if ( ! node->inits.empty() ) {
496 os << indent-1 << "... with inits:" << endl;
497 printAll( node->inits );
498 }
499 --indent;
500
501 return node;
502 }
503
504 virtual const ast::Stmt * visit( const ast::ForStmt * node ) override final {
505 os << "For Statement" << endl;
506
507 if ( ! node->inits.empty() ) {
508 os << indent << "... initialization:" << endl;
509 ++indent;
510 for ( const ast::Stmt * stmt : node->inits ) {
511 os << indent+1;
512 safe_print( stmt );
513 }
514 --indent;
515 }
516
517 if ( node->cond ) {
518 os << indent << "... condition:" << endl;
519 ++indent;
520 os << indent;
521 node->cond->accept( *this );
522 --indent;
523 }
524
525 if ( node->inc ) {
526 os << indent << "... increment:" << endl;
527 ++indent;
528 os << indent;
529 node->inc->accept( *this );
530 --indent;
531 }
532
533 if ( node->body ) {
534 os << indent << "... with body:" << endl;
535 ++indent;
536 os << indent;
537 node->body->accept( *this );
538 --indent;
539 }
540 os << endl;
541 print( node->labels );
542
543 return node;
544 }
545
546 virtual const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
547 os << "Switch on condition: ";
548 safe_print( node->cond );
549 os << endl;
550
551 ++indent;
552 for ( const ast::Stmt * stmt : node->stmts ) {
553 stmt->accept( *this );
554 }
555 --indent;
556
557 return node;
558 }
559
560 virtual const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
561 if ( node->isDefault() ) {
562 os << indent << "Default ";
563 } else {
564 os << indent << "Case ";
565 safe_print( node->cond );
566 } // if
567 os << endl;
568
569 ++indent;
570 for ( const ast::Stmt * stmt : node->stmts ) {
571 os << indent;
572 stmt->accept( *this );
573 }
574 --indent;
575
576 return node;
577 }
578
579 virtual const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
580 os << "Branch (" << node->kindName() << ")" << endl;
581 ++indent;
582 if ( ! node->target.empty() ) {
583 os << indent << "with target: " << node->target << endl;
584 }
585
586 if ( ! node->originalTarget.empty() ) {
587 os << indent << "with original target: " << node->originalTarget << endl;
588 }
589
590 if ( node->computedTarget ) {
591 os << indent << "with computed target: ";
592 node->computedTarget->accept( *this );
593 os << endl;
594 }
595 --indent;
596
597 return node;
598 }
599
600 virtual const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
601 os << "Return Statement, returning";
602 if ( node->expr ) {
603 ++indent;
604 os << ":" << endl << indent;
605 node->expr->accept( *this );
606 --indent;
607 } else {
608 os << " void";
609 }
610 os << endl;
611
612 return node;
613 }
614
615 virtual const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
616 if ( node->target ) os << "Non-Local ";
617
618 switch( node->kind ) {
619 case ast::ExceptionKind::Terminate: os << "Terminate "; break;
620 case ast::ExceptionKind::Resume: os << "Resume "; break;
621 }
622
623 ++indent;
624 os << "Throw Statement, raising: ";
625 safe_print( node->expr );
626 if ( node->target ) {
627 os << "... at: ";
628 node->target->accept( *this );
629 }
630 --indent;
631
632 return node;
633 }
634
635 virtual const ast::Stmt * visit( const ast::TryStmt * node ) override final {
636 ++indent;
637 os << "Try Statement" << endl << indent-1
638 << "... with block:" << endl << indent;
639 safe_print( node->body );
640
641 os << indent-1 << "... and handlers:" << endl;
642 for ( const ast::CatchStmt * stmt : node->handlers ) {
643 os << indent;
644 stmt->accept( *this );
645 }
646
647 if ( node->finally ) {
648 os << indent-1 << "... and finally:" << endl << indent;
649 node->finally->accept( *this );
650 }
651 --indent;
652
653 return node;
654 }
655
656 virtual const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
657 os << "Catch ";
658 switch ( node->kind ) {
659 case ast::ExceptionKind::Terminate: os << "Terminate "; break;
660 case ast::ExceptionKind::Resume: os << "Resume "; break;
661 }
662 os << "Statement" << endl << indent;
663
664 ++indent;
665 os << "... catching: ";
666 short_print( node->decl );
667 os << endl;
668
669 if ( node->cond ) {
670 os << indent-1 << "... with conditional:" << endl << indent;
671 node->cond->accept( *this );
672 }
673
674 os << indent-1 << "... with block:" << endl << indent;
675 safe_print( node->body );
676 --indent;
677
678 return node;
679 }
680
681 virtual const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
682 os << "Finally Statement" << endl;
683 os << indent << "... with block:" << endl;
684 ++indent;
685 os << indent;
686 safe_print( node->body );
687 --indent;
688
689 return node;
690 }
691
692 virtual const ast::Stmt * visit( const ast::SuspendStmt * node ) override final {
693 os << "Suspend Statement";
694 switch (node->type) {
695 case ast::SuspendStmt::None : os << " with implicit target"; break;
696 case ast::SuspendStmt::Generator: os << " for generator"; break;
697 case ast::SuspendStmt::Coroutine: os << " for coroutine"; break;
698 }
699 os << endl;
700
701 ++indent;
702 if(node->then) {
703 os << indent << " with post statement :" << endl;
704 safe_print( node->then );
705 }
706 ++indent;
707
708 return node;
709 }
710
711 virtual const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
712 os << "Waitfor Statement" << endl;
713 indent += 2;
714 for( const auto & clause : node->clauses ) {
715 os << indent-1 << "target function: ";
716 safe_print( clause.target.func );
717
718 if ( ! clause.target.args.empty() ) {
719 os << endl << indent-1 << "... with arguments:" << endl;
720 for( const ast::Expr * arg : clause.target.args ) {
721 arg->accept( *this );
722 }
723 }
724
725 if ( clause.stmt ) {
726 os << indent-1 << "... with statment:" << endl;
727 clause.stmt->accept( *this );
728 }
729
730 if ( clause.cond ) {
731 os << indent-1 << "... with condition:" << endl;
732 clause.cond->accept( *this );
733 }
734 }
735
736 if ( node->timeout.time ) {
737 os << indent-1 << "timeout of:" << endl;
738 node->timeout.time->accept( *this );
739
740 if ( node->timeout.stmt ) {
741 os << indent-1 << "... with statment:" << endl;
742 node->timeout.stmt->accept( *this );
743 }
744
745 if ( node->timeout.cond ) {
746 os << indent-1 << "... with condition:" << endl;
747 node->timeout.cond->accept( *this );
748 }
749 }
750
751 if ( node->orElse.stmt ) {
752 os << indent-1 << "else:" << endl;
753 node->orElse.stmt->accept( *this );
754
755 if ( node->orElse.cond ) {
756 os << indent-1 << "... with condition:" << endl;
757 node->orElse.cond->accept( *this );
758 }
759 }
760 indent -= 2;
761
762 return node;
763 }
764
765 virtual const ast::Decl * visit( const ast::WithStmt * node ) override final {
766 os << "With statement" << endl;
767 os << indent << "... with expressions:" << endl;
768 ++indent;
769 printAll( node->exprs );
770 os << indent-1 << "... with statement:" << endl << indent;
771 safe_print( node->stmt );
772 --indent;
773
774 return node;
775 }
776
777 virtual const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
778 os << "Null Statement" << endl;
779 print( node->labels );
780
781 return node;
782 }
783
784 virtual const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
785 os << "Declaration of ";
786 safe_print( node->decl );
787
788 return node;
789 }
790
791 virtual const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
792 os << "Implicit Ctor Dtor Statement" << endl;
793 os << indent << "... with Ctor/Dtor: ";
794 ++indent;
795 safe_print( node->callStmt );
796 --indent;
797 os << endl;
798
799 return node;
800 }
801
802 virtual const ast::Stmt * visit( const ast::MutexStmt * node ) override final {
803 os << "Mutex Statement" << endl;
804 os << indent << "... with Mutex Parameters: ";
805 ++indent;
806 printAll( node->mutexObjs );
807 --indent;
808 os << indent << "... with Statement: ";
809 ++indent;
810 safe_print( node->stmt );
811 --indent;
812 os << endl;
813
814 return node;
815 }
816
817 virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
818 ++indent;
819 os << "Application of" << endl << indent;
820 safe_print( node->func );
821 os << endl;
822 if ( ! node->args.empty() ) {
823 os << indent << "... to arguments" << endl;
824 printAll( node->args );
825 }
826 --indent;
827 postprint( node );
828
829 return node;
830 }
831
832 virtual const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
833 ++indent;
834 os << "Applying untyped:" << endl;
835 os << indent;
836 safe_print( node->func );
837 os << endl << indent-1 << "...to:" << endl;
838 printAll( node->args );
839 --indent;
840 postprint( node );
841
842 return node;
843 }
844
845 virtual const ast::Expr * visit( const ast::NameExpr * node ) override final {
846 os << "Name: " << node->name;
847 postprint( node );
848
849 return node;
850 }
851
852 virtual const ast::Expr * visit( const ast::AddressExpr * node ) override final {
853 os << "Address of:" << endl;
854 ++indent;
855 os << indent;
856 safe_print( node->arg );
857
858 --indent;
859
860 return node;
861 }
862
863 virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
864 os << "Address of label:" << node->arg;
865
866 return node;
867 }
868
869 virtual const ast::Expr * visit( const ast::CastExpr * node ) override final {
870 ++indent;
871 os << (node->isGenerated ? "Generated" : "Explicit") << " Cast of:" << endl << indent;
872 safe_print( node->arg );
873 os << endl << indent-1 << "... to:";
874 if ( ! node->result ) {
875 os << " ";
876 undefined();
877 } else if ( node->result->isVoid() ) {
878 os << " nothing";
879 } else {
880 os << endl << indent;
881 node->result->accept( *this );
882 } // if
883 --indent;
884 postprint( node );
885
886 return node;
887 }
888
889 virtual const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
890 ++indent;
891 os << "Keyword Cast of:" << endl << indent;
892 safe_print( node->arg );
893 --indent;
894 os << endl << indent << "... to: " << node->targetString();
895 postprint( node );
896
897 return node;
898 }
899
900 virtual const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
901 ++indent;
902 os << "Virtual Cast of:" << endl << indent;
903 safe_print( node->arg );
904 os << endl << indent-1 << "... to:";
905 if ( ! node->result ) {
906 os << " unknown";
907 } else {
908 os << endl << indent;
909 node->result->accept( *this );
910 }
911 --indent;
912 postprint( node );
913
914 return node;
915 }
916
917 virtual const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
918 ++indent;
919 os << "Untyped Member Expression, with field: " << endl << indent;
920 safe_print( node->member );
921 os << indent-1 << "... from aggregate:" << endl << indent;
922 safe_print( node->aggregate );
923 --indent;
924 postprint( node );
925
926 return node;
927 }
928
929 virtual const ast::Expr * visit( const ast::MemberExpr * node ) override final {
930 ++indent;
931 os << "Member Expression, with field:" << endl << indent;
932 safe_print( node->member );
933 os << endl << indent-1 << "... from aggregate:" << endl << indent;
934 safe_print( node->aggregate );
935 --indent;
936 postprint( node );
937
938 return node;
939 }
940
941 virtual const ast::Expr * visit( const ast::VariableExpr * node ) override final {
942 os << "Variable Expression: ";
943 short_print( node->var );
944 postprint( node );
945
946 return node;
947 }
948
949 virtual const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
950 os << "Constant Expression (" << node->rep;
951 if ( node->result ) {
952 os << ": ";
953 node->result->accept( *this );
954 }
955 os << ")";
956 postprint( node );
957
958 return node;
959 }
960
961 virtual const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
962 os << "Sizeof Expression on: ";
963 ++indent;
964 if ( node->type ) node->type->accept( *this );
965 else safe_print( node->expr );
966 --indent;
967 postprint( node );
968
969 return node;
970 }
971
972 virtual const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
973 os << "Alignof Expression on: ";
974 ++indent;
975 if ( node->type ) node->type->accept( *this );
976 else safe_print( node->expr );
977 --indent;
978 postprint( node );
979
980 return node;
981 }
982
983 virtual const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
984 os << "Untyped Offsetof Expression on member " << node->member << " of ";
985 ++indent;
986 safe_print( node->type );
987 --indent;
988 postprint( node );
989
990 return node;
991 }
992
993 virtual const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
994 os << "Offsetof Expression on member " << node->member->name << " of ";
995 ++indent;
996 safe_print( node->type );
997 --indent;
998 postprint( node );
999
1000 return node;
1001 }
1002
1003 virtual const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
1004 os << "Offset Pack Expression on: ";
1005 ++indent;
1006 safe_print( node->type );
1007 --indent;
1008 postprint( node );
1009
1010 return node;
1011 }
1012
1013 virtual const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
1014 os << "Short-circuited operation (" << (node->isAnd ? "and" : "or") << ") on: ";
1015 safe_print( node->arg1 );
1016 os << " and ";
1017 safe_print( node->arg2 );
1018 postprint( node );
1019
1020 return node;
1021 }
1022
1023 virtual const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
1024 ++indent;
1025 os << "Conditional expression on:" << endl << indent;
1026 safe_print( node->arg1 );
1027 os << indent-1 << "First alternative:" << endl << indent;
1028 safe_print( node->arg2 );
1029 os << indent-1 << "Second alternative:" << endl << indent;
1030 safe_print( node->arg3 );
1031 --indent;
1032 postprint( node );
1033
1034 return node;
1035 }
1036
1037 virtual const ast::Expr * visit( const ast::CommaExpr * node ) override final {
1038 ++indent;
1039 os << "Comma Expression:" << endl << indent;
1040 safe_print( node->arg1 );
1041 os << endl << indent;
1042 safe_print( node->arg2 );
1043 --indent;
1044 postprint( node );
1045
1046 return node;
1047 }
1048
1049 virtual const ast::Expr * visit( const ast::TypeExpr * node ) override final {
1050 safe_print( node->type );
1051 postprint( node );
1052
1053 return node;
1054 }
1055
1056 virtual const ast::Expr * visit( const ast::AsmExpr * node ) override final {
1057 os << "Asm Expression:" << endl;
1058 ++indent;
1059 if ( !node->inout.empty() ) os << "[" << node->inout << "] ";
1060 if ( node->constraint ) node->constraint->accept( *this );
1061 if ( node->operand ) node->operand->accept( *this );
1062 --indent;
1063
1064 return node;
1065 }
1066
1067 virtual const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
1068 ++indent;
1069 os << "Implicit Copy Constructor Expression:" << endl << indent;
1070 safe_print( node->callExpr );
1071 --indent;
1072 postprint( node );
1073
1074 return node;
1075 }
1076
1077 virtual const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
1078 os << "Constructor Expression:" << endl << indent+1;
1079 indent += 2;
1080 safe_print( node->callExpr );
1081 indent -= 2;
1082 postprint( node );
1083
1084 return node;
1085 }
1086
1087 virtual const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
1088 ++indent;
1089 os << "Compound Literal Expression: " << endl << indent;
1090 safe_print( node->result );
1091 os << indent;
1092 safe_print( node->init );
1093 --indent;
1094 postprint( node );
1095
1096 return node;
1097 }
1098
1099 virtual const ast::Expr * visit( const ast::RangeExpr * node ) override final {
1100 os << "Range Expression: ";
1101 safe_print( node->low );
1102 os << " ... ";
1103 safe_print( node->high );
1104 postprint( node );
1105
1106 return node;
1107 }
1108
1109 virtual const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
1110 os << "Untyped Tuple:" << endl;
1111 ++indent;
1112 printAll( node->exprs );
1113 --indent;
1114 postprint( node );
1115
1116 return node;
1117 }
1118
1119 virtual const ast::Expr * visit( const ast::TupleExpr * node ) override final {
1120 os << "Tuple:" << endl;
1121 ++indent;
1122 printAll( node->exprs );
1123 --indent;
1124 postprint( node );
1125
1126 return node;
1127 }
1128
1129 virtual const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
1130 os << "Tuple Index Expression, with tuple:" << endl;
1131 ++indent;
1132 os << indent;
1133 safe_print( node->tuple );
1134 os << indent << "with index: " << node->index << endl;
1135 --indent;
1136 postprint( node );
1137
1138 return node;
1139 }
1140
1141 virtual const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
1142 os << "Tuple Assignment Expression, with stmt expr:" << endl;
1143 ++indent;
1144 os << indent;
1145 safe_print( node->stmtExpr );
1146 --indent;
1147 postprint( node );
1148
1149 return node;
1150 }
1151
1152 virtual const ast::Expr * visit( const ast::StmtExpr * node ) override final {
1153 ++indent;
1154 os << "Statement Expression:" << endl << indent;
1155 safe_print( node->stmts );
1156 if ( ! node->returnDecls.empty() ) {
1157 os << indent << "... with returnDecls: ";
1158 printAll( node->returnDecls );
1159 }
1160 if ( ! node->dtors.empty() ) {
1161 os << indent << "... with dtors: ";
1162 printAll( node->dtors );
1163 }
1164 --indent;
1165 postprint( node );
1166
1167 return node;
1168 }
1169
1170 virtual const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
1171 ++indent;
1172 os << "Unique Expression with id: " << node->id << endl << indent;
1173 safe_print( node->expr );
1174 if ( node->object ) {
1175 os << indent-1 << "... with decl: ";
1176 short_print( node->object );
1177 }
1178 --indent;
1179 postprint( node );
1180
1181 return node;
1182 }
1183
1184 virtual const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
1185 ++indent;
1186 os << "Untyped Init Expression" << endl << indent;
1187 safe_print( node->expr );
1188 if ( ! node->initAlts.empty() ) {
1189 for ( const InitAlternative & alt : node->initAlts ) {
1190 os << indent << "InitAlternative: ";
1191 safe_print( alt.type );
1192 safe_print( alt.designation );
1193 }
1194 }
1195 --indent;
1196
1197 return node;
1198 }
1199
1200 virtual const ast::Expr * visit( const ast::InitExpr * node ) override final {
1201 ++indent;
1202 os << "Init Expression" << endl << indent;
1203 safe_print( node->expr );
1204 os << indent << "... with designation: ";
1205 safe_print( node->designation );
1206 --indent;
1207
1208 return node;
1209 }
1210
1211 virtual const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
1212 ++indent;
1213 os << "Deleted Expression" << endl << indent;
1214 safe_print( node->expr );
1215 os << endl << indent << "... deleted by: ";
1216 safe_print( node->deleteStmt );
1217 --indent;
1218
1219 return node;
1220 }
1221
1222 virtual const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
1223 ++indent;
1224 os << "Default Argument Expression" << endl << indent;
1225 safe_print( node->expr );
1226 --indent;
1227
1228 return node;
1229 }
1230
1231 virtual const ast::Expr * visit( const ast::GenericExpr * node ) override final {
1232 ++indent;
1233 os << "C11 _Generic Expression" << endl << indent;
1234 safe_print( node->control );
1235 os << endl << indent << "... with associations:" << endl;
1236 for ( const auto & assoc : node->associations ) {
1237 os << indent;
1238 if ( assoc.type ) {
1239 os << "... type: ";
1240 assoc.type->accept( *this );
1241 os << endl << indent << "... expression: ";
1242 safe_print( assoc.expr );
1243 } else {
1244 os << "... default: ";
1245 safe_print( assoc.expr );
1246 }
1247 os << endl;
1248 }
1249 --indent;
1250
1251 return node;
1252 }
1253
1254 virtual const ast::Type * visit( const ast::VoidType * node ) override final {
1255 preprint( node );
1256 os << "void";
1257 return node;
1258 }
1259
1260 virtual const ast::Type * visit( const ast::BasicType * node ) override final {
1261 preprint( node );
1262 os << ast::BasicType::typeNames[ node->kind ];
1263 return node;
1264 }
1265
1266 virtual const ast::Type * visit( const ast::PointerType * node ) override final {
1267 preprint( node );
1268 if ( ! node->isArray() ) {
1269 os << "pointer to ";
1270 } else {
1271 os << "decayed ";
1272 if ( node->isStatic ) {
1273 os << "static ";
1274 }
1275
1276 if ( node->isVarLen ) {
1277 os << "variable length array of ";
1278 } else if ( node->dimension ) {
1279 os << "array of ";
1280 node->dimension->accept( *this );
1281 os << " ";
1282 }
1283 }
1284 safe_print( node->base );
1285
1286 return node;
1287 }
1288
1289 virtual const ast::Type * visit( const ast::ArrayType * node ) override final {
1290 preprint( node );
1291 if ( node->isStatic ) {
1292 os << "static ";
1293 }
1294
1295 if ( node->isVarLen ) {
1296 os << "variable length array of ";
1297 } else if ( node->dimension ) {
1298 os << "array of ";
1299 } else {
1300 os << "open array of ";
1301 }
1302
1303 safe_print( node->base );
1304
1305 if ( node->dimension ) {
1306 os << " with dimension of ";
1307 node->dimension->accept( *this );
1308 }
1309
1310 return node;
1311 }
1312
1313 virtual const ast::Type * visit( const ast::ReferenceType * node ) override final {
1314 preprint( node );
1315 os << "reference to ";
1316 safe_print( node->base );
1317
1318 return node;
1319 }
1320
1321 virtual const ast::Type * visit( const ast::QualifiedType * node ) override final {
1322 preprint( node );
1323 ++indent;
1324 os << "Qualified Type:" << endl << indent;
1325 safe_print( node->parent );
1326 os << endl << indent;
1327 safe_print( node->child );
1328 os << endl;
1329 --indent;
1330
1331 return node;
1332 }
1333
1334 virtual const ast::Type * visit( const ast::FunctionType * node ) override final {
1335 preprint( node );
1336
1337 os << "function" << endl;
1338 if ( ! node->params.empty() ) {
1339 os << indent << "... with parameters" << endl;
1340 ++indent;
1341 printAll( node->params );
1342 if ( node->isVarArgs ) {
1343 os << indent << "and a variable number of other arguments" << endl;
1344 }
1345 --indent;
1346 } else if ( node->isVarArgs ) {
1347 os << indent+1 << "accepting unspecified arguments" << endl;
1348 }
1349
1350 os << indent << "... returning";
1351 if ( node->returns.empty() ) {
1352 os << " nothing" << endl;
1353 } else {
1354 os << endl;
1355 ++indent;
1356 printAll( node->returns );
1357 --indent;
1358 }
1359
1360 return node;
1361 }
1362
1363 virtual const ast::Type * visit( const ast::StructInstType * node ) override final {
1364 preprint( node );
1365 os << "instance of struct " << node->name;
1366 if ( node->base ) {
1367 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1368 }
1369 print( node->params );
1370
1371 return node;
1372 }
1373
1374 virtual const ast::Type * visit( const ast::UnionInstType * node ) override final {
1375 preprint( node );
1376 os << "instance of union " << node->name;
1377 if ( node->base ) {
1378 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1379 }
1380 print( node->params );
1381
1382 return node;
1383 }
1384
1385 virtual const ast::Type * visit( const ast::EnumInstType * node ) override final {
1386 preprint( node );
1387 os << "instance of enum " << node->name;
1388 if ( node->base ) {
1389 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1390 }
1391 print( node->params );
1392
1393 return node;
1394 }
1395
1396 virtual const ast::Type * visit( const ast::TraitInstType * node ) override final {
1397 preprint( node );
1398 os << "instance of trait " << node->name;
1399 print( node->params );
1400
1401 return node;
1402 }
1403
1404 virtual const ast::Type * visit( const ast::TypeInstType * node ) override final {
1405 preprint( node );
1406 const auto & _name = deterministic_output && isUnboundType(node) ? "[unbound]" : node->typeString();
1407 os << "instance of type " << _name
1408 << " (" << (node->kind == ast::TypeDecl::Ftype ? "" : "not ") << "function type)";
1409 print( node->params );
1410
1411 return node;
1412 }
1413
1414 virtual const ast::Type * visit( const ast::TupleType * node ) override final {
1415 preprint( node );
1416 os << "tuple of types" << endl;
1417 ++indent;
1418 printAll( node->types );
1419 --indent;
1420
1421 return node;
1422 }
1423
1424 virtual const ast::Type * visit( const ast::TypeofType * node ) override final {
1425 preprint( node );
1426 if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
1427 os << "type-of expression ";
1428 safe_print( node->expr );
1429
1430 return node;
1431 }
1432
1433 virtual const ast::Type * visit( const ast::VTableType * node ) override final {
1434 preprint( node );
1435 os << "vtable for ";
1436 safe_print( node->base );
1437
1438 return node;
1439 }
1440
1441 virtual const ast::Type * visit( const ast::VarArgsType * node ) override final {
1442 preprint( node );
1443 os << "builtin var args pack";
1444 return node;
1445 }
1446
1447 virtual const ast::Type * visit( const ast::ZeroType * node ) override final {
1448 preprint( node );
1449 os << "zero_t";
1450 return node;
1451 }
1452
1453 virtual const ast::Type * visit( const ast::OneType * node ) override final {
1454 preprint( node );
1455 os << "one_t";
1456 return node;
1457 }
1458
1459 virtual const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
1460 preprint( node );
1461 os << "Global Scope Type";
1462 return node;
1463 }
1464
1465 virtual const ast::Designation * visit( const ast::Designation * node ) override final {
1466 if ( node->designators.empty() ) return node;
1467 os << "... designated by: " << endl;
1468 ++indent;
1469 for ( const ast::Expr * d : node->designators ) {
1470 os << indent;
1471 d->accept( *this );
1472 os << endl;
1473 }
1474 --indent;
1475 return node;
1476 }
1477
1478 virtual const ast::Init * visit( const ast::SingleInit * node ) override final {
1479 os << "Simple Initializer: ";
1480 safe_print( node->value );
1481 return node;
1482 }
1483
1484 virtual const ast::Init * visit( const ast::ListInit * node ) override final {
1485 os << "Compound initializer: " << endl;
1486 ++indent;
1487 for ( auto p : group_iterate( node->designations, node->initializers ) ) {
1488 const ast::Designation * d = std::get<0>(p);
1489 const ast::Init * init = std::get<1>(p);
1490 os << indent;
1491 init->accept( *this );
1492 os << endl;
1493 if ( ! d->designators.empty() ) {
1494 os << indent;
1495 d->accept( *this );
1496 }
1497 }
1498 --indent;
1499 return node;
1500 }
1501
1502 virtual const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1503 os << "Constructor initializer: " << endl;
1504 if ( node->ctor ) {
1505 os << indent << "... initially constructed with ";
1506 ++indent;
1507 node->ctor->accept( *this );
1508 --indent;
1509 }
1510
1511 if ( node->dtor ) {
1512 os << indent << "... destructed with ";
1513 ++indent;
1514 node->dtor->accept( *this );
1515 --indent;
1516 }
1517
1518 if ( node->init ) {
1519 os << indent << "... with fallback C-style initializer: ";
1520 ++indent;
1521 node->init->accept( *this );
1522 --indent;
1523 }
1524 return node;
1525 }
1526
1527 virtual const ast::Attribute * visit( const ast::Attribute * node ) override final {
1528 if ( node->empty() ) return node;
1529 os << "Attribute with name: " << node->name;
1530 if ( node->params.empty() ) return node;
1531 os << " with parameters: " << endl;
1532 ++indent;
1533 printAll( node->params );
1534 --indent;
1535 return node;
1536 }
1537
1538 virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1539 os << indent << "Types:" << endl;
1540 for ( const auto& i : *node ) {
1541 os << indent+1 << i.first.typeString() << " -> ";
1542 indent += 2;
1543 safe_print( i.second );
1544 indent -= 2;
1545 os << endl;
1546 }
1547 return node;
1548 }
1549
1550};
1551
1552void print( ostream & os, const ast::Node * node, Indenter indent ) {
1553 Printer printer { os, indent, false };
1554 node->accept(printer);
1555}
1556
1557void printShort( ostream & os, const ast::Decl * node, Indenter indent ) {
1558 Printer printer { os, indent, true };
1559 node->accept(printer);
1560}
1561
1562// Annoyingly these needed to be defined out of line to avoid undefined references.
1563// The size here needs to be explicit but at least the compiler will produce an error
1564// if the wrong size is specified
1565constexpr array<const char*, 3> Printer::Names::FuncSpecifiers;
1566constexpr array<const char*, 5> Printer::Names::StorageClasses;
1567constexpr array<const char*, 6> Printer::Names::Qualifiers;
1568}
Note: See TracBrowser for help on using the repository browser.