source: src/AST/Print.cpp @ 42cd451

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 42cd451 was 37cdd97, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added a ast node for suspend statements

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