source: src/AST/Print.cpp @ a6e0e4c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a6e0e4c was 6a45bd78, checked in by Fangren Yu <f37yu@…>, 3 years ago

cleanup: remove params in TypeDecl? (never used)

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