source: src/AST/Print.cpp@ 361bf01

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 361bf01 was 361bf01, checked in by Fangren Yu <f37yu@…>, 5 years ago

remove ParameterizedType and put content into FunctionType

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