source: src/AST/Print.cpp@ 5bf3976

ADT ast-experimental
Last change on this file since 5bf3976 was 71806e0, checked in by JiadaL <j82liang@…>, 3 years ago

Rename InlineValueDecl to InlineMemberDecl

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