source: src/AST/Print.cpp @ 6083392

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

reimplement function type and eliminate deep copy

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