source: src/AST/Print.cpp@ 0d0931d

ADT ast-experimental
Last change on this file since 0d0931d was 257a8f5, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Made some of the AST/Print helpers public.

  • Property mode set to 100644
File size: 38.6 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 ( ! short_mode && node->stmts ) {
374 ++indent;
375 os << " with body" << endl << indent;
376 node->stmts->accept( *this );
377 --indent;
378 }
379
380 return node;
381 }
382
383 virtual const ast::Decl * visit( const ast::StructDecl * node ) override final {
384 print(node);
385 return node;
386 }
387
388 virtual const ast::DeclWithType * visit( const ast::InlineMemberDecl * node ) override final {
389 os << "inline ";
390 if ( ! node->name.empty() ) os << node->name;
391
392 return node;
393 }
394
395 virtual const ast::Decl * visit( const ast::UnionDecl * node ) override final {
396 print(node);
397 return node;
398 }
399
400 virtual const ast::Decl * visit( const ast::EnumDecl * node ) override final {
401 print(node);
402 return node;
403 }
404
405 virtual const ast::Decl * visit( const ast::TraitDecl * node ) override final {
406 print(node);
407 return node;
408 }
409
410 virtual const ast::Decl * visit( const ast::TypeDecl * node ) override final {
411 preprint( node );
412 if ( ! short_mode && node->init ) {
413 os << endl << indent << "with type initializer: ";
414 ++indent;
415 node->init->accept( *this );
416 --indent;
417 }
418
419 return node;
420 }
421
422 virtual const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
423 preprint( node );
424 return node;
425 }
426
427 virtual const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
428 safe_print( node->stmt );
429 return node;
430 }
431
432 virtual const ast::DirectiveDecl * visit( const ast::DirectiveDecl * node ) override final {
433 safe_print( node->stmt );
434 return node;
435 }
436
437 virtual const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
438 os << "Static Assert with condition: ";
439 ++indent;
440 safe_print( node->cond );
441 os << endl << indent-1 << "and message: ";
442 safe_print( node->msg );
443 --indent;
444 os << endl;
445
446 return node;
447 }
448
449 virtual const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
450 os << "Compound Statement:" << endl;
451 ++indent;
452 printAll( node->kids );
453 --indent;
454 return node;
455 }
456
457 virtual const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
458 ++indent;
459 os << "Expression Statement:" << endl << indent;
460 safe_print( node->expr );
461 --indent;
462 return node;
463 }
464
465 virtual const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
466 os << "Assembler Statement:" << endl;
467 ++indent;
468 os << indent-1 << "instruction:" << endl << indent;
469 safe_print( node->instruction );
470 if ( ! node->output.empty() ) {
471 os << endl << indent << "output:" << endl;
472 printAll( node->output );
473 } // if
474 if ( ! node->input.empty() ) {
475 os << indent << "input:" << endl;
476 printAll( node->input );
477 } // if
478 if ( ! node->clobber.empty() ) {
479 os << indent << "clobber:" << endl;
480 printAll( node->clobber );
481 } // if
482 --indent;
483 return node;
484 }
485
486 virtual const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
487 os << "GCC Directive: " << node->directive << endl;
488 return node;
489 }
490
491 virtual const ast::Stmt * visit( const ast::IfStmt * node ) override final {
492 os << "If on condition:" << endl;
493 ++indent;
494 os << indent;
495 safe_print( node->cond );
496 --indent;
497
498 if ( ! node->inits.empty() ) {
499 os << indent << "... with initialization:" << endl;
500 ++indent;
501 for ( const ast::Stmt * stmt : node->inits ) {
502 os << indent;
503 safe_print( stmt );
504 }
505 --indent;
506 os << endl;
507 }
508
509 os << indent << "... then:" << endl;
510
511 ++indent;
512 os << indent;
513 safe_print( node->then );
514 --indent;
515
516 if ( node->else_ != 0 ) {
517 os << indent << "... else:" << endl;
518 ++indent;
519 os << indent;
520 node->else_->accept( *this );
521 --indent;
522 } // if
523 return node;
524 }
525
526 virtual const ast::Stmt * visit( const ast::WhileDoStmt * node ) override final {
527 if ( node->isDoWhile ) { os << "Do-"; }
528 os << "While on condition:" << endl;
529 ++indent;
530 safe_print( node->cond );
531 os << indent-1 << "... with body:" << endl;
532 safe_print( node->body );
533
534 if ( ! node->inits.empty() ) {
535 os << indent-1 << "... with inits:" << endl;
536 printAll( node->inits );
537 }
538 --indent;
539
540 return node;
541 }
542
543 virtual const ast::Stmt * visit( const ast::ForStmt * node ) override final {
544 os << "For Statement" << endl;
545
546 if ( ! node->inits.empty() ) {
547 os << indent << "... initialization:" << endl;
548 ++indent;
549 for ( const ast::Stmt * stmt : node->inits ) {
550 os << indent+1;
551 safe_print( stmt );
552 }
553 --indent;
554 }
555
556 if ( node->cond ) {
557 os << indent << "... condition:" << endl;
558 ++indent;
559 os << indent;
560 node->cond->accept( *this );
561 --indent;
562 }
563
564 if ( node->inc ) {
565 os << indent << "... increment:" << endl;
566 ++indent;
567 os << indent;
568 node->inc->accept( *this );
569 --indent;
570 }
571
572 if ( node->body ) {
573 os << indent << "... with body:" << endl;
574 ++indent;
575 os << indent;
576 node->body->accept( *this );
577 --indent;
578 }
579 os << endl;
580 print( node->labels );
581
582 return node;
583 }
584
585 virtual const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
586 os << "Switch on condition: ";
587 safe_print( node->cond );
588 os << endl;
589
590 ++indent;
591 for ( const ast::CaseClause * stmt : node->cases ) {
592 stmt->accept( *this );
593 }
594 --indent;
595
596 return node;
597 }
598
599 virtual const ast::CaseClause * visit( const ast::CaseClause * node ) override final {
600 if ( node->isDefault() ) {
601 os << indent << "Default ";
602 } else {
603 os << indent << "Case ";
604 safe_print( node->cond );
605 } // if
606 os << endl;
607
608 ++indent;
609 for ( const ast::Stmt * stmt : node->stmts ) {
610 os << indent;
611 stmt->accept( *this );
612 }
613 --indent;
614
615 return node;
616 }
617
618 virtual const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
619 os << "Branch (" << node->kindName() << ")" << endl;
620 ++indent;
621 if ( ! node->target.empty() ) {
622 os << indent << "with target: " << node->target << endl;
623 }
624
625 if ( ! node->originalTarget.empty() ) {
626 os << indent << "with original target: " << node->originalTarget << endl;
627 }
628
629 if ( node->computedTarget ) {
630 os << indent << "with computed target: ";
631 node->computedTarget->accept( *this );
632 os << endl;
633 }
634 --indent;
635
636 return node;
637 }
638
639 virtual const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
640 os << "Return Statement, returning";
641 if ( node->expr ) {
642 ++indent;
643 os << ":" << endl << indent;
644 node->expr->accept( *this );
645 --indent;
646 } else {
647 os << " void";
648 }
649 os << endl;
650
651 return node;
652 }
653
654 virtual const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
655 if ( node->target ) os << "Non-Local ";
656
657 switch( node->kind ) {
658 case ast::ExceptionKind::Terminate: os << "Terminate "; break;
659 case ast::ExceptionKind::Resume: os << "Resume "; break;
660 }
661
662 ++indent;
663 os << "Throw Statement, raising: ";
664 safe_print( node->expr );
665 if ( node->target ) {
666 os << "... at: ";
667 node->target->accept( *this );
668 }
669 --indent;
670
671 return node;
672 }
673
674 virtual const ast::Stmt * visit( const ast::TryStmt * node ) override final {
675 ++indent;
676 os << "Try Statement" << endl << indent-1
677 << "... with block:" << endl << indent;
678 safe_print( node->body );
679
680 os << indent-1 << "... and handlers:" << endl;
681 for ( const ast::CatchClause * stmt : node->handlers ) {
682 os << indent;
683 stmt->accept( *this );
684 }
685
686 if ( node->finally ) {
687 os << indent-1 << "... and finally:" << endl << indent;
688 node->finally->accept( *this );
689 }
690 --indent;
691
692 return node;
693 }
694
695 virtual const ast::CatchClause * visit( const ast::CatchClause * node ) override final {
696 os << "Catch ";
697 switch ( node->kind ) {
698 case ast::ExceptionKind::Terminate: os << "Terminate "; break;
699 case ast::ExceptionKind::Resume: os << "Resume "; break;
700 }
701 os << "Statement" << endl << indent;
702
703 ++indent;
704 os << "... catching: ";
705 short_print( node->decl );
706 os << endl;
707
708 if ( node->cond ) {
709 os << indent-1 << "... with conditional:" << endl << indent;
710 node->cond->accept( *this );
711 }
712
713 os << indent-1 << "... with block:" << endl << indent;
714 safe_print( node->body );
715 --indent;
716
717 return node;
718 }
719
720 virtual const ast::FinallyClause * visit( const ast::FinallyClause * node ) override final {
721 os << "Finally Statement" << endl;
722 os << indent << "... with block:" << endl;
723 ++indent;
724 os << indent;
725 safe_print( node->body );
726 --indent;
727
728 return node;
729 }
730
731 virtual const ast::Stmt * visit( const ast::SuspendStmt * node ) override final {
732 os << "Suspend Statement";
733 switch (node->type) {
734 case ast::SuspendStmt::None : os << " with implicit target"; break;
735 case ast::SuspendStmt::Generator: os << " for generator"; break;
736 case ast::SuspendStmt::Coroutine: os << " for coroutine"; break;
737 }
738 os << endl;
739
740 ++indent;
741 if(node->then) {
742 os << indent << " with post statement :" << endl;
743 safe_print( node->then );
744 }
745 ++indent;
746
747 return node;
748 }
749
750 virtual const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
751 os << "Waitfor Statement" << endl;
752 indent += 2;
753 for( const auto & clause : node->clauses ) {
754 clause->accept( *this );
755 }
756
757 if ( node->timeout_time ) {
758 os << indent-1 << "timeout of:" << endl;
759 node->timeout_time->accept( *this );
760
761 if ( node->timeout_stmt ) {
762 os << indent-1 << "... with statment:" << endl;
763 node->timeout_stmt->accept( *this );
764 }
765
766 if ( node->timeout_cond ) {
767 os << indent-1 << "... with condition:" << endl;
768 node->timeout_cond->accept( *this );
769 }
770 }
771
772 if ( node->else_stmt ) {
773 os << indent-1 << "else:" << endl;
774 node->else_stmt->accept( *this );
775
776 if ( node->else_cond ) {
777 os << indent-1 << "... with condition:" << endl;
778 node->else_cond->accept( *this );
779 }
780 }
781
782 return node;
783 }
784
785 virtual const ast::WaitForClause * visit( const ast::WaitForClause * node ) override final {
786 os << indent-1 << "target function: ";
787 safe_print( node->target_func );
788
789 if ( !node->target_args.empty() ) {
790 os << endl << indent-1 << "... with arguments:" << endl;
791 for( const ast::Expr * arg : node->target_args ) {
792 arg->accept( *this );
793 }
794 }
795
796 if ( node->stmt ) {
797 os << indent-1 << "... with statment:" << endl;
798 node->stmt->accept( *this );
799 }
800
801 if ( node->cond ) {
802 os << indent-1 << "... with condition:" << endl;
803 node->cond->accept( *this );
804 }
805
806 return node;
807 }
808
809 virtual const ast::Decl * visit( const ast::WithStmt * node ) override final {
810 os << "With statement" << endl;
811 os << indent << "... with expressions:" << endl;
812 ++indent;
813 printAll( node->exprs );
814 os << indent-1 << "... with statement:" << endl << indent;
815 safe_print( node->stmt );
816 --indent;
817
818 return node;
819 }
820
821 virtual const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
822 os << "Null Statement" << endl;
823 print( node->labels );
824
825 return node;
826 }
827
828 virtual const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
829 os << "Declaration of ";
830 safe_print( node->decl );
831
832 return node;
833 }
834
835 virtual const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
836 os << "Implicit Ctor Dtor Statement" << endl;
837 os << indent << "... with Ctor/Dtor: ";
838 ++indent;
839 safe_print( node->callStmt );
840 --indent;
841 os << endl;
842
843 return node;
844 }
845
846 virtual const ast::Stmt * visit( const ast::MutexStmt * node ) override final {
847 os << "Mutex Statement" << endl;
848 os << indent << "... with Mutex Parameters: ";
849 ++indent;
850 printAll( node->mutexObjs );
851 --indent;
852 os << indent << "... with Statement: ";
853 ++indent;
854 safe_print( node->stmt );
855 --indent;
856 os << endl;
857
858 return node;
859 }
860
861 virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
862 ++indent;
863 os << "Application of" << endl << indent;
864 safe_print( node->func );
865 os << endl;
866 if ( ! node->args.empty() ) {
867 os << indent << "... to arguments" << endl;
868 printAll( node->args );
869 }
870 --indent;
871 postprint( node );
872
873 return node;
874 }
875
876 virtual const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
877 ++indent;
878 os << "Applying untyped:" << endl;
879 os << indent;
880 safe_print( node->func );
881 os << endl << indent-1 << "...to:" << endl;
882 printAll( node->args );
883 --indent;
884 postprint( node );
885
886 return node;
887 }
888
889 virtual const ast::Expr * visit( const ast::NameExpr * node ) override final {
890 os << "Name: " << node->name;
891 postprint( node );
892
893 return node;
894 }
895
896 virtual const ast::Expr * visit( const ast::QualifiedNameExpr * node ) override final {
897 os << "QualifiedNameExpr: " << std::endl;
898 os << ++indent << "Type: ";
899 safe_print( node->type_decl );
900 os << std::endl;
901 os << indent << "Name: " << node->name << std::endl;
902 --indent;
903 postprint( node );
904 return node;
905 }
906
907 virtual const ast::Expr * visit( const ast::AddressExpr * node ) override final {
908 os << "Address of:" << endl;
909 ++indent;
910 os << indent;
911 safe_print( node->arg );
912
913 --indent;
914
915 return node;
916 }
917
918 virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
919 os << "Address of label:" << node->arg;
920
921 return node;
922 }
923
924 virtual const ast::Expr * visit( const ast::CastExpr * node ) override final {
925 ++indent;
926 os << (node->isGenerated ? "Generated" : "Explicit") << " Cast of:" << endl << indent;
927 safe_print( node->arg );
928 os << endl << indent-1 << "... to:";
929 if ( ! node->result ) {
930 os << " ";
931 undefined();
932 } else if ( node->result->isVoid() ) {
933 os << " nothing";
934 } else {
935 os << endl << indent;
936 node->result->accept( *this );
937 } // if
938 --indent;
939 postprint( node );
940
941 return node;
942 }
943
944 virtual const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
945 ++indent;
946 os << "Keyword Cast of:" << endl << indent;
947 safe_print( node->arg );
948 --indent;
949 os << endl << indent << "... to: " << node->targetString();
950 postprint( node );
951
952 return node;
953 }
954
955 virtual const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
956 ++indent;
957 os << "Virtual Cast of:" << endl << indent;
958 safe_print( node->arg );
959 os << endl << indent-1 << "... to:";
960 if ( ! node->result ) {
961 os << " unknown";
962 } else {
963 os << endl << indent;
964 node->result->accept( *this );
965 }
966 --indent;
967 postprint( node );
968
969 return node;
970 }
971
972 virtual const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
973 ++indent;
974 os << "Untyped Member Expression, with field: " << endl << indent;
975 safe_print( node->member );
976 os << indent-1 << "... from aggregate:" << endl << indent;
977 safe_print( node->aggregate );
978 --indent;
979 postprint( node );
980
981 return node;
982 }
983
984 virtual const ast::Expr * visit( const ast::MemberExpr * node ) override final {
985 ++indent;
986 os << "Member Expression, with field:" << endl << indent;
987 safe_print( node->member );
988 os << endl << indent-1 << "... from aggregate:" << endl << indent;
989 safe_print( node->aggregate );
990 --indent;
991 postprint( node );
992
993 return node;
994 }
995
996 virtual const ast::Expr * visit( const ast::VariableExpr * node ) override final {
997 os << "Variable Expression: ";
998 short_print( node->var );
999 postprint( node );
1000
1001 return node;
1002 }
1003
1004 virtual const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
1005 os << "Constant Expression (" << node->rep;
1006 if ( node->result ) {
1007 os << ": ";
1008 node->result->accept( *this );
1009 }
1010 os << ")";
1011 postprint( node );
1012
1013 return node;
1014 }
1015
1016 virtual const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
1017 os << "Sizeof Expression on: ";
1018 ++indent;
1019 if ( node->type ) node->type->accept( *this );
1020 else safe_print( node->expr );
1021 --indent;
1022 postprint( node );
1023
1024 return node;
1025 }
1026
1027 virtual const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
1028 os << "Alignof Expression on: ";
1029 ++indent;
1030 if ( node->type ) node->type->accept( *this );
1031 else safe_print( node->expr );
1032 --indent;
1033 postprint( node );
1034
1035 return node;
1036 }
1037
1038 virtual const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
1039 os << "Untyped Offsetof Expression on member " << node->member << " of ";
1040 ++indent;
1041 safe_print( node->type );
1042 --indent;
1043 postprint( node );
1044
1045 return node;
1046 }
1047
1048 virtual const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
1049 os << "Offsetof Expression on member " << node->member->name << " of ";
1050 ++indent;
1051 safe_print( node->type );
1052 --indent;
1053 postprint( node );
1054
1055 return node;
1056 }
1057
1058 virtual const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
1059 os << "Offset Pack Expression on: ";
1060 ++indent;
1061 safe_print( node->type );
1062 --indent;
1063 postprint( node );
1064
1065 return node;
1066 }
1067
1068 virtual const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
1069 os << "Short-circuited operation (" << (node->isAnd ? "and" : "or") << ") on: ";
1070 safe_print( node->arg1 );
1071 os << " and ";
1072 safe_print( node->arg2 );
1073 postprint( node );
1074
1075 return node;
1076 }
1077
1078 virtual const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
1079 ++indent;
1080 os << "Conditional expression on:" << endl << indent;
1081 safe_print( node->arg1 );
1082 os << indent-1 << "First alternative:" << endl << indent;
1083 safe_print( node->arg2 );
1084 os << indent-1 << "Second alternative:" << endl << indent;
1085 safe_print( node->arg3 );
1086 --indent;
1087 postprint( node );
1088
1089 return node;
1090 }
1091
1092 virtual const ast::Expr * visit( const ast::CommaExpr * node ) override final {
1093 ++indent;
1094 os << "Comma Expression:" << endl << indent;
1095 safe_print( node->arg1 );
1096 os << endl << indent;
1097 safe_print( node->arg2 );
1098 --indent;
1099 postprint( node );
1100
1101 return node;
1102 }
1103
1104 virtual const ast::Expr * visit( const ast::TypeExpr * node ) override final {
1105 safe_print( node->type );
1106 postprint( node );
1107
1108 return node;
1109 }
1110
1111 virtual const ast::Expr * visit( const ast::DimensionExpr * node ) override final {
1112 os << "Type-Sys Value: " << node->name;
1113 postprint( node );
1114
1115 return node;
1116 }
1117
1118 virtual const ast::Expr * visit( const ast::AsmExpr * node ) override final {
1119 os << "Asm Expression:" << endl;
1120 ++indent;
1121 if ( !node->inout.empty() ) os << "[" << node->inout << "] ";
1122 if ( node->constraint ) node->constraint->accept( *this );
1123 if ( node->operand ) node->operand->accept( *this );
1124 --indent;
1125
1126 return node;
1127 }
1128
1129 virtual const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
1130 ++indent;
1131 os << "Implicit Copy Constructor Expression:" << endl << indent;
1132 safe_print( node->callExpr );
1133 --indent;
1134 postprint( node );
1135
1136 return node;
1137 }
1138
1139 virtual const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
1140 os << "Constructor Expression:" << endl << indent+1;
1141 indent += 2;
1142 safe_print( node->callExpr );
1143 indent -= 2;
1144 postprint( node );
1145
1146 return node;
1147 }
1148
1149 virtual const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
1150 ++indent;
1151 os << "Compound Literal Expression: " << endl << indent;
1152 safe_print( node->result );
1153 os << indent;
1154 safe_print( node->init );
1155 --indent;
1156 postprint( node );
1157
1158 return node;
1159 }
1160
1161 virtual const ast::Expr * visit( const ast::RangeExpr * node ) override final {
1162 os << "Range Expression: ";
1163 safe_print( node->low );
1164 os << " ... ";
1165 safe_print( node->high );
1166 postprint( node );
1167
1168 return node;
1169 }
1170
1171 virtual const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
1172 os << "Untyped Tuple:" << endl;
1173 ++indent;
1174 printAll( node->exprs );
1175 --indent;
1176 postprint( node );
1177
1178 return node;
1179 }
1180
1181 virtual const ast::Expr * visit( const ast::TupleExpr * node ) override final {
1182 os << "Tuple:" << endl;
1183 ++indent;
1184 printAll( node->exprs );
1185 --indent;
1186 postprint( node );
1187
1188 return node;
1189 }
1190
1191 virtual const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
1192 os << "Tuple Index Expression, with tuple:" << endl;
1193 ++indent;
1194 os << indent;
1195 safe_print( node->tuple );
1196 os << indent << "with index: " << node->index << endl;
1197 --indent;
1198 postprint( node );
1199
1200 return node;
1201 }
1202
1203 virtual const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
1204 os << "Tuple Assignment Expression, with stmt expr:" << endl;
1205 ++indent;
1206 os << indent;
1207 safe_print( node->stmtExpr );
1208 --indent;
1209 postprint( node );
1210
1211 return node;
1212 }
1213
1214 virtual const ast::Expr * visit( const ast::StmtExpr * node ) override final {
1215 ++indent;
1216 os << "Statement Expression:" << endl << indent;
1217 safe_print( node->stmts );
1218 if ( ! node->returnDecls.empty() ) {
1219 os << indent << "... with returnDecls: ";
1220 printAll( node->returnDecls );
1221 }
1222 if ( ! node->dtors.empty() ) {
1223 os << indent << "... with dtors: ";
1224 printAll( node->dtors );
1225 }
1226 --indent;
1227 postprint( node );
1228
1229 return node;
1230 }
1231
1232 virtual const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
1233 ++indent;
1234 os << "Unique Expression with id: " << node->id << endl << indent;
1235 safe_print( node->expr );
1236 if ( node->object ) {
1237 os << indent-1 << "... with decl: ";
1238 short_print( node->object );
1239 }
1240 --indent;
1241 postprint( node );
1242
1243 return node;
1244 }
1245
1246 virtual const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
1247 ++indent;
1248 os << "Untyped Init Expression" << endl << indent;
1249 safe_print( node->expr );
1250 if ( ! node->initAlts.empty() ) {
1251 for ( const InitAlternative & alt : node->initAlts ) {
1252 os << indent << "InitAlternative: ";
1253 safe_print( alt.type );
1254 safe_print( alt.designation );
1255 }
1256 }
1257 --indent;
1258
1259 return node;
1260 }
1261
1262 virtual const ast::Expr * visit( const ast::InitExpr * node ) override final {
1263 ++indent;
1264 os << "Init Expression" << endl << indent;
1265 safe_print( node->expr );
1266 os << indent << "... with designation: ";
1267 safe_print( node->designation );
1268 --indent;
1269
1270 return node;
1271 }
1272
1273 virtual const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
1274 ++indent;
1275 os << "Deleted Expression" << endl << indent;
1276 safe_print( node->expr );
1277 os << endl << indent << "... deleted by: ";
1278 safe_print( node->deleteStmt );
1279 --indent;
1280
1281 return node;
1282 }
1283
1284 virtual const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
1285 ++indent;
1286 os << "Default Argument Expression" << endl << indent;
1287 safe_print( node->expr );
1288 --indent;
1289
1290 return node;
1291 }
1292
1293 virtual const ast::Expr * visit( const ast::GenericExpr * node ) override final {
1294 ++indent;
1295 os << "C11 _Generic Expression" << endl << indent;
1296 safe_print( node->control );
1297 os << endl << indent << "... with associations:" << endl;
1298 for ( const auto & assoc : node->associations ) {
1299 os << indent;
1300 if ( assoc.type ) {
1301 os << "... type: ";
1302 assoc.type->accept( *this );
1303 os << endl << indent << "... expression: ";
1304 safe_print( assoc.expr );
1305 } else {
1306 os << "... default: ";
1307 safe_print( assoc.expr );
1308 }
1309 os << endl;
1310 }
1311 --indent;
1312
1313 return node;
1314 }
1315
1316 virtual const ast::Type * visit( const ast::VoidType * node ) override final {
1317 preprint( node );
1318 os << "void";
1319 return node;
1320 }
1321
1322 virtual const ast::Type * visit( const ast::BasicType * node ) override final {
1323 preprint( node );
1324 os << ast::BasicType::typeNames[ node->kind ];
1325 return node;
1326 }
1327
1328 virtual const ast::Type * visit( const ast::PointerType * node ) override final {
1329 preprint( node );
1330 if ( ! node->isArray() ) {
1331 os << "pointer to ";
1332 } else {
1333 os << "decayed ";
1334 if ( node->isStatic ) {
1335 os << "static ";
1336 }
1337
1338 if ( node->isVarLen ) {
1339 os << "variable length array of ";
1340 } else if ( node->dimension ) {
1341 os << "array of ";
1342 node->dimension->accept( *this );
1343 os << " ";
1344 }
1345 }
1346 safe_print( node->base );
1347
1348 return node;
1349 }
1350
1351 virtual const ast::Type * visit( const ast::ArrayType * node ) override final {
1352 preprint( node );
1353 if ( node->isStatic ) {
1354 os << "static ";
1355 }
1356
1357 if ( node->isVarLen ) {
1358 os << "variable length array of ";
1359 } else if ( node->dimension ) {
1360 os << "array of ";
1361 } else {
1362 os << "open array of ";
1363 }
1364
1365 safe_print( node->base );
1366
1367 if ( node->dimension ) {
1368 os << " with dimension of ";
1369 node->dimension->accept( *this );
1370 }
1371
1372 return node;
1373 }
1374
1375 virtual const ast::Type * visit( const ast::ReferenceType * node ) override final {
1376 preprint( node );
1377 os << "reference to ";
1378 safe_print( node->base );
1379
1380 return node;
1381 }
1382
1383 virtual const ast::Type * visit( const ast::QualifiedType * node ) override final {
1384 preprint( node );
1385 ++indent;
1386 os << "Qualified Type:" << endl << indent;
1387 safe_print( node->parent );
1388 os << endl << indent;
1389 safe_print( node->child );
1390 os << endl;
1391 --indent;
1392
1393 return node;
1394 }
1395
1396 virtual const ast::Type * visit( const ast::FunctionType * node ) override final {
1397 preprint( node );
1398
1399 os << "function" << endl;
1400 if ( ! node->params.empty() ) {
1401 os << indent << "... with parameters" << endl;
1402 ++indent;
1403 printAll( node->params );
1404 if ( node->isVarArgs ) {
1405 os << indent << "and a variable number of other arguments" << endl;
1406 }
1407 --indent;
1408 } else if ( node->isVarArgs ) {
1409 os << indent+1 << "accepting unspecified arguments" << endl;
1410 }
1411
1412 os << indent << "... returning";
1413 if ( node->returns.empty() ) {
1414 os << " nothing" << endl;
1415 } else {
1416 os << endl;
1417 ++indent;
1418 printAll( node->returns );
1419 --indent;
1420 }
1421
1422 return node;
1423 }
1424
1425 virtual const ast::Type * visit( const ast::StructInstType * node ) override final {
1426 preprint( node );
1427 os << "instance of struct " << node->name;
1428 if ( node->base ) {
1429 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1430 }
1431 print( node->params );
1432
1433 return node;
1434 }
1435
1436 virtual const ast::Type * visit( const ast::UnionInstType * node ) override final {
1437 preprint( node );
1438 os << "instance of union " << node->name;
1439 if ( node->base ) {
1440 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1441 }
1442 print( node->params );
1443
1444 return node;
1445 }
1446
1447 virtual const ast::Type * visit( const ast::EnumInstType * node ) override final {
1448 preprint( node );
1449 os << "instance of enum " << node->name;
1450 if ( node->base ) {
1451 os << " " << ( node->base->body ? "with" : "without" ) << " body";
1452 }
1453 print( node->params );
1454
1455 return node;
1456 }
1457
1458 virtual const ast::Type * visit( const ast::TraitInstType * node ) override final {
1459 preprint( node );
1460 os << "instance of trait " << node->name;
1461 print( node->params );
1462
1463 return node;
1464 }
1465
1466 virtual const ast::Type * visit( const ast::TypeInstType * node ) override final {
1467 preprint( node );
1468 const auto & _name = deterministic_output && isUnboundType(node) ? "[unbound]" : node->typeString();
1469 os << "instance of type " << _name
1470 << " (" << (node->kind == ast::TypeDecl::Ftype ? "" : "not ") << "function type)";
1471 print( node->params );
1472
1473 return node;
1474 }
1475
1476 virtual const ast::Type * visit( const ast::TupleType * node ) override final {
1477 preprint( node );
1478 os << "tuple of types" << endl;
1479 ++indent;
1480 printAll( node->types );
1481 --indent;
1482
1483 return node;
1484 }
1485
1486 virtual const ast::Type * visit( const ast::TypeofType * node ) override final {
1487 preprint( node );
1488 if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
1489 os << "type-of expression ";
1490 safe_print( node->expr );
1491
1492 return node;
1493 }
1494
1495 virtual const ast::Type * visit( const ast::VTableType * node ) override final {
1496 preprint( node );
1497 os << "vtable for ";
1498 safe_print( node->base );
1499
1500 return node;
1501 }
1502
1503 virtual const ast::Type * visit( const ast::VarArgsType * node ) override final {
1504 preprint( node );
1505 os << "builtin var args pack";
1506 return node;
1507 }
1508
1509 virtual const ast::Type * visit( const ast::ZeroType * node ) override final {
1510 preprint( node );
1511 os << "zero_t";
1512 return node;
1513 }
1514
1515 virtual const ast::Type * visit( const ast::OneType * node ) override final {
1516 preprint( node );
1517 os << "one_t";
1518 return node;
1519 }
1520
1521 virtual const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
1522 preprint( node );
1523 os << "Global Scope Type";
1524 return node;
1525 }
1526
1527 virtual const ast::Designation * visit( const ast::Designation * node ) override final {
1528 if ( node->designators.empty() ) return node;
1529 os << "... designated by: " << endl;
1530 ++indent;
1531 for ( const ast::Expr * d : node->designators ) {
1532 os << indent;
1533 d->accept( *this );
1534 os << endl;
1535 }
1536 --indent;
1537 return node;
1538 }
1539
1540 virtual const ast::Init * visit( const ast::SingleInit * node ) override final {
1541 os << "Simple Initializer: ";
1542 safe_print( node->value );
1543 return node;
1544 }
1545
1546 virtual const ast::Init * visit( const ast::ListInit * node ) override final {
1547 os << "Compound initializer: " << endl;
1548 ++indent;
1549 for ( auto p : group_iterate( node->designations, node->initializers ) ) {
1550 const ast::Designation * d = std::get<0>(p);
1551 const ast::Init * init = std::get<1>(p);
1552 os << indent;
1553 init->accept( *this );
1554 os << endl;
1555 if ( ! d->designators.empty() ) {
1556 os << indent;
1557 d->accept( *this );
1558 }
1559 }
1560 --indent;
1561 return node;
1562 }
1563
1564 virtual const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1565 os << "Constructor initializer: " << endl;
1566 if ( node->ctor ) {
1567 os << indent << "... initially constructed with ";
1568 ++indent;
1569 node->ctor->accept( *this );
1570 --indent;
1571 }
1572
1573 if ( node->dtor ) {
1574 os << indent << "... destructed with ";
1575 ++indent;
1576 node->dtor->accept( *this );
1577 --indent;
1578 }
1579
1580 if ( node->init ) {
1581 os << indent << "... with fallback C-style initializer: ";
1582 ++indent;
1583 node->init->accept( *this );
1584 --indent;
1585 }
1586 return node;
1587 }
1588
1589 virtual const ast::Attribute * visit( const ast::Attribute * node ) override final {
1590 if ( node->empty() ) return node;
1591 os << "Attribute with name: " << node->name;
1592 if ( node->params.empty() ) return node;
1593 os << " with parameters: " << endl;
1594 ++indent;
1595 printAll( node->params );
1596 --indent;
1597 return node;
1598 }
1599
1600 virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1601 os << indent << "Types:" << endl;
1602 for ( const auto& i : *node ) {
1603 os << indent+1 << i.first.typeString() << " -> ";
1604 indent += 2;
1605 safe_print( i.second );
1606 indent -= 2;
1607 os << endl;
1608 }
1609 return node;
1610 }
1611
1612};
1613
1614} // namespace
1615
1616void print( ostream & os, const ast::Node * node, Indenter indent ) {
1617 Printer printer { os, indent, false };
1618 node->accept(printer);
1619}
1620
1621void printShort( ostream & os, const ast::Decl * node, Indenter indent ) {
1622 Printer printer { os, indent, true };
1623 node->accept(printer);
1624}
1625
1626void print( ostream & os, Function::Specs specs ) {
1627 print( os, specs, Names::FuncSpecifiers );
1628}
1629
1630void print( ostream & os, Storage::Classes storage ) {
1631 print( os, storage, Names::StorageClasses );
1632}
1633
1634void print( ostream & os, CV::Qualifiers qualifiers ) {
1635 print( os, qualifiers, Names::Qualifiers );
1636}
1637
1638} // namespace ast
Note: See TracBrowser for help on using the repository browser.