source: src/AST/Print.cpp@ eb1be63

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since eb1be63 was a8ed717, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Small fix in printing, fixing mismatched casing for casts.

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