source: src/AST/Print.cpp @ a12cd594

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a12cd594 was cd6a6ff, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Improved coverage of deterministic_output to be much finer grain.

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