source: src/AST/Print.cpp@ 94b1f718

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
Last change on this file since 94b1f718 was 94b1f718, checked in by Aaron Moss <a3moss@…>, 6 years ago

Add some Stmt printers

  • Property mode set to 100644
File size: 31.4 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 os << endl << indent-1 << "... with temporaries:" << endl;
889 printAll( node->tempDecls );
890 os << endl << indent-1 << "... with return temporaries:" << endl;
891 printAll( node->returnDecls );
892 --indent;
893 postprint( node );
894
895 return node;
896 }
897
898 virtual const ast::Expr * visit( const ast::ConstructorExpr * node ) {
899 os << "Constructor Expression:" << endl << indent+1;
900 indent += 2;
901 safe_print( node->callExpr );
902 indent -= 2;
903 postprint( node );
904
905 return node;
906 }
907
908 virtual const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) {
909 ++indent;
910 os << "Compound Literal Expression: " << endl << indent;
911 safe_print( node->result );
912 os << indent;
913 safe_print( node->init );
914 --indent;
915 postprint( node );
916
917 return node;
918 }
919
920 virtual const ast::Expr * visit( const ast::RangeExpr * node ) {
921 os << "Range Expression: ";
922 safe_print( node->low );
923 os << " ... ";
924 safe_print( node->high );
925 postprint( node );
926
927 return node;
928 }
929
930 virtual const ast::Expr * visit( const ast::UntypedTupleExpr * node ) {
931 os << "Untyped Tuple:" << endl;
932 ++indent;
933 printAll( node->exprs );
934 --indent;
935 postprint( node );
936
937 return node;
938 }
939
940 virtual const ast::Expr * visit( const ast::TupleExpr * node ) {
941 os << "Tuple:" << endl;
942 ++indent;
943 printAll( node->exprs );
944 --indent;
945 postprint( node );
946
947 return node;
948 }
949
950 virtual const ast::Expr * visit( const ast::TupleIndexExpr * node ) {
951 os << "Tuple Index Expression, with tuple:" << endl;
952 ++indent;
953 os << indent;
954 safe_print( node->tuple );
955 os << indent << "with index: " << node->index << endl;
956 --indent;
957 postprint( node );
958
959 return node;
960 }
961
962 virtual const ast::Expr * visit( const ast::TupleAssignExpr * node ) {
963 os << "Tuple Assignment Expression, with stmt expr:" << endl;
964 ++indent;
965 os << indent;
966 safe_print( node->stmtExpr );
967 --indent;
968 postprint( node );
969
970 return node;
971 }
972
973 virtual const ast::Expr * visit( const ast::StmtExpr * node ) {
974 ++indent;
975 os << "Statement Expression:" << endl << indent;
976 safe_print( node->stmts );
977 if ( ! node->returnDecls.empty() ) {
978 os << indent << "... with returnDecls: ";
979 printAll( node->returnDecls );
980 }
981 if ( ! node->dtors.empty() ) {
982 os << indent << "... with dtors: ";
983 printAll( node->dtors );
984 }
985 --indent;
986 postprint( node );
987
988 return node;
989 }
990
991 virtual const ast::Expr * visit( const ast::UniqueExpr * node ) {
992 ++indent;
993 os << "Unique Expression with id: " << node->id << endl << indent;
994 safe_print( node->expr );
995 if ( node->object ) {
996 os << indent-1 << "... with decl: ";
997 short_print( node->object );
998 }
999 --indent;
1000 postprint( node );
1001
1002 return node;
1003 }
1004
1005 virtual const ast::Expr * visit( const ast::UntypedInitExpr * node ) {
1006 ++indent;
1007 os << "Untyped Init Expression" << endl << indent;
1008 safe_print( node->expr );
1009 if ( ! node->initAlts.empty() ) {
1010 for ( const InitAlternative & alt : node->initAlts ) {
1011 os << indent << "InitAlternative: ";
1012 safe_print( alt.type );
1013 safe_print( alt.designation );
1014 }
1015 }
1016 --indent;
1017
1018 return node;
1019 }
1020
1021 virtual const ast::Expr * visit( const ast::InitExpr * node ) {
1022 ++indent;
1023 os << "Init Expression" << endl << indent;
1024 safe_print( node->expr );
1025 os << indent << "... with designation: ";
1026 safe_print( node->designation );
1027 --indent;
1028
1029 return node;
1030 }
1031
1032 virtual const ast::Expr * visit( const ast::DeletedExpr * node ) {
1033 ++indent;
1034 os << "Deleted Expression" << endl << indent;
1035 safe_print( node->expr );
1036 os << endl << indent << "... deleted by: ";
1037 safe_print( node->deleteStmt );
1038 --indent;
1039
1040 return node;
1041 }
1042
1043 virtual const ast::Expr * visit( const ast::DefaultArgExpr * node ) {
1044 ++indent;
1045 os << "Default Argument Expression" << endl << indent;
1046 safe_print( node->expr );
1047 --indent;
1048
1049 return node;
1050 }
1051
1052 virtual const ast::Expr * visit( const ast::GenericExpr * node ) {
1053 ++indent;
1054 os << "C11 _Generic Expression" << endl << indent;
1055 safe_print( node->control );
1056 os << endl << indent << "... with associations:" << endl;
1057 for ( const auto & assoc : node->associations ) {
1058 os << indent;
1059 if ( assoc.type ) {
1060 os << "... type: ";
1061 assoc.type->accept( *this );
1062 os << endl << indent << "... expression: ";
1063 safe_print( assoc.expr );
1064 } else {
1065 os << "... default: ";
1066 safe_print( assoc.expr );
1067 }
1068 os << endl;
1069 }
1070 --indent;
1071
1072 return node;
1073 }
1074
1075 virtual const ast::Type * visit( const ast::VoidType * node ) {
1076 preprint( node );
1077 os << "void";
1078 return node;
1079 }
1080
1081 virtual const ast::Type * visit( const ast::BasicType * node ) {
1082 preprint( node );
1083 os << ast::BasicType::typeNames[ node->kind ];
1084 return node;
1085 }
1086
1087 virtual const ast::Type * visit( const ast::PointerType * node ) {
1088 preprint( node );
1089 if ( ! node->isArray() ) {
1090 os << "pointer to ";
1091 } else {
1092 os << "decayed ";
1093 if ( node->isStatic ) {
1094 os << "static ";
1095 }
1096
1097 if ( node->isVarLen ) {
1098 os << "variable length array of ";
1099 } else if ( node->dimension ) {
1100 os << "array of ";
1101 node->dimension->accept( *this );
1102 os << " ";
1103 }
1104 }
1105 safe_print( node->base );
1106
1107 return node;
1108 }
1109
1110 virtual const ast::Type * visit( const ast::ArrayType * node ) {
1111 preprint( node );
1112 if ( node->isStatic ) {
1113 os << "static ";
1114 }
1115
1116 if ( node->isVarLen ) {
1117 os << "variable length array of ";
1118 } else if ( node->dimension ) {
1119 os << "array of ";
1120 } else {
1121 os << "open array of ";
1122 }
1123
1124 safe_print( node->base );
1125
1126 if ( node->dimension ) {
1127 os << " with dimension of ";
1128 node->dimension->accept( *this );
1129 }
1130
1131 return node;
1132 }
1133
1134 virtual const ast::Type * visit( const ast::ReferenceType * node ) {
1135 preprint( node );
1136 os << "reference to ";
1137 safe_print( node->base );
1138
1139 return node;
1140 }
1141
1142 virtual const ast::Type * visit( const ast::QualifiedType * node ) {
1143 preprint( node );
1144 ++indent;
1145 os << "Qualified Type:" << endl << indent;
1146 safe_print( node->parent );
1147 os << endl << indent;
1148 safe_print( node->child );
1149 os << endl;
1150 --indent;
1151
1152 return node;
1153 }
1154
1155 virtual const ast::Type * visit( const ast::FunctionType * node ) {
1156 preprint( node );
1157
1158 os << "function" << endl;
1159 if ( ! node->params.empty() ) {
1160 os << indent << "... with parameters" << endl;
1161 ++indent;
1162 printAll( node->params );
1163 if ( node->isVarArgs ) {
1164 os << indent << "and a variable number of other arguments" << endl;
1165 }
1166 --indent;
1167 } else if ( node->isVarArgs ) {
1168 os << indent+1 << "accepting unspecified arguments" << endl;
1169 }
1170
1171 os << indent << "... returning";
1172 if ( node->returns.empty() ) {
1173 os << " nothing" << endl;
1174 } else {
1175 os << endl;
1176 ++indent;
1177 printAll( node->returns );
1178 --indent;
1179 }
1180
1181 return node;
1182 }
1183
1184 virtual const ast::Type * visit( const ast::StructInstType * node ) {
1185 preprint( node );
1186 os << "instance of struct " << node->name;
1187 if ( node->base ) {
1188 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1189 }
1190 print( node->params );
1191
1192 return node;
1193 }
1194
1195 virtual const ast::Type * visit( const ast::UnionInstType * node ) {
1196 preprint( node );
1197 os << "instance of union " << node->name;
1198 if ( node->base ) {
1199 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1200 }
1201 print( node->params );
1202
1203 return node;
1204 }
1205
1206 virtual const ast::Type * visit( const ast::EnumInstType * node ) {
1207 preprint( node );
1208 os << "instance of enum " << node->name;
1209 if ( node->base ) {
1210 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1211 }
1212 print( node->params );
1213
1214 return node;
1215 }
1216
1217 virtual const ast::Type * visit( const ast::TraitInstType * node ) {
1218 preprint( node );
1219 os << "instance of trait " << node->name;
1220 print( node->params );
1221
1222 return node;
1223 }
1224
1225 virtual const ast::Type * visit( const ast::TypeInstType * node ) {
1226 preprint( node );
1227 os << "instance of type " << node->name
1228 << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)";
1229 print( node->params );
1230
1231 return node;
1232 }
1233
1234 virtual const ast::Type * visit( const ast::TupleType * node ) {
1235 preprint( node );
1236 os << "tuple of types" << endl;
1237 ++indent;
1238 printAll( node->types );
1239 --indent;
1240
1241 return node;
1242 }
1243
1244 virtual const ast::Type * visit( const ast::TypeofType * node ) {
1245 preprint( node );
1246 if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
1247 os << "type-of expression ";
1248 safe_print( node->expr );
1249
1250 return node;
1251 }
1252
1253 virtual const ast::Type * visit( const ast::VarArgsType * node ) {
1254 preprint( node );
1255 os << "builtin var args pack";
1256 return node;
1257 }
1258
1259 virtual const ast::Type * visit( const ast::ZeroType * node ) {
1260 preprint( node );
1261 os << "zero_t";
1262 return node;
1263 }
1264
1265 virtual const ast::Type * visit( const ast::OneType * node ) {
1266 preprint( node );
1267 os << "one_t";
1268 return node;
1269 }
1270
1271 virtual const ast::Type * visit( const ast::GlobalScopeType * node ) {
1272 preprint( node );
1273 os << "Global Scope Type";
1274 return node;
1275 }
1276
1277 virtual const ast::Designation * visit( const ast::Designation * node ) {
1278 if ( node->designators.empty() ) return node;
1279 os << "... designated by: " << endl;
1280 ++indent;
1281 for ( const ast::Expr * d : node->designators ) {
1282 os << indent;
1283 d->accept( *this );
1284 os << endl;
1285 }
1286 --indent;
1287 return node;
1288 }
1289
1290 virtual const ast::Init * visit( const ast::SingleInit * node ) {
1291 os << "Simple Initializer: ";
1292 safe_print( node->value );
1293 return node;
1294 }
1295
1296 virtual const ast::Init * visit( const ast::ListInit * node ) {
1297 os << "Compound initializer: " << endl;
1298 ++indent;
1299 for ( auto p : group_iterate( node->designations, node->initializers ) ) {
1300 const ast::Designation * d = std::get<0>(p);
1301 const ast::Init * init = std::get<1>(p);
1302 os << indent;
1303 init->accept( *this );
1304 os << endl;
1305 if ( ! d->designators.empty() ) {
1306 os << indent;
1307 d->accept( *this );
1308 }
1309 }
1310 --indent;
1311 return node;
1312 }
1313
1314 virtual const ast::Init * visit( const ast::ConstructorInit * node ) {
1315 os << "Constructor initializer: " << endl;
1316 if ( node->ctor ) {
1317 os << indent << "... initially constructed with ";
1318 ++indent;
1319 node->ctor->accept( *this );
1320 --indent;
1321 }
1322
1323 if ( node->dtor ) {
1324 os << indent << "... destructed with ";
1325 ++indent;
1326 node->dtor->accept( *this );
1327 --indent;
1328 }
1329
1330 if ( node->init ) {
1331 os << indent << "... with fallback C-style initializer: ";
1332 ++indent;
1333 node->init->accept( *this );
1334 --indent;
1335 }
1336 return node;
1337 }
1338
1339 virtual const ast::Attribute * visit( const ast::Attribute * node ) {
1340 if ( node->empty() ) return node;
1341 os << "Attribute with name: " << node->name;
1342 if ( node->params.empty() ) return node;
1343 os << " with parameters: " << endl;
1344 ++indent;
1345 printAll( node->params );
1346 --indent;
1347 return node;
1348 }
1349
1350 virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) {
1351 os << indent << "Types:" << endl;
1352 for ( const auto& i : *node ) {
1353 os << indent+1 << i.first << " -> ";
1354 indent += 2;
1355 safe_print( i.second );
1356 indent -= 2;
1357 os << endl;
1358 }
1359 os << indent << "Non-types:" << endl;
1360 for ( auto i = node->beginVar(); i != node->endVar(); ++i ) {
1361 os << indent+1 << i->first << " -> ";
1362 indent += 2;
1363 safe_print( i->second );
1364 indent -= 2;
1365 os << endl;
1366 }
1367 return node;
1368 }
1369
1370};
1371
1372void print( ostream & os, const ast::Node * node, Indenter indent ) {
1373 Printer printer { os, indent, false };
1374 node->accept(printer);
1375}
1376
1377void printShort( ostream & os, const ast::Node * node, Indenter indent ) {
1378 Printer printer { os, indent, true };
1379 node->accept(printer);
1380}
1381
1382// Annoyingly these needed to be defined out of line to avoid undefined references.
1383// The size here needs to be explicit but at least the compiler will produce an error
1384// if the wrong size is specified
1385constexpr array<const char*, 3> Printer::Names::FuncSpecifiers;
1386constexpr array<const char*, 5> Printer::Names::StorageClasses;
1387constexpr array<const char*, 6> Printer::Names::Qualifiers;
1388}
Note: See TracBrowser for help on using the repository browser.