source: src/AST/Print.cpp @ 0d070ca

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

correctly print assertions

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