source: src/AST/Print.cpp @ 3c79ea9

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 3c79ea9 was 4ec9513, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Converted validate C, including adding DimensionExpr? to the new ast.

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