source: src/AST/Print.cpp @ 943bfad

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 943bfad was 2d019af, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

parser global pragmas, fixes #241

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