source: src/AST/Print.cpp @ 0f6a7752

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0f6a7752 was 1259c35, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Merge branch 'master' into cleanup-dtors

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