source: src/AST/Print.cpp@ 2c04369

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 2c04369 was a7d50b6, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Removed trailing whitespace.

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