source: src/AST/Print.cpp@ 82f791f

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 82f791f was 98e8b3b, checked in by Andrew Beach <ajbeach@…>, 5 years ago

ast::ReferenceToType is now ast::BaseInstType.

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