source: src/AST/Print.cpp@ f9128a5

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 f9128a5 was d908563, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Merge branch 'master' into cleanup-dtors

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