source: src/AST/Print.cpp@ 3a513d89

ADT
Last change on this file since 3a513d89 was 561354f, checked in by JiadaL <j82liang@…>, 2 years ago

Save progress

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