source: src/AST/Print.cpp @ ef9988b

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

fix lost typeinst in resolved assertions

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