Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Print.cpp

    rb0ec971 r20a5977  
    2222#include "TypeSubstitution.hpp"
    2323
    24 #include "Common/utility.h"      // for group_iterate
     24#include "Common/utility.h" // for group_iterate
     25
     26using namespace std;
    2527
    2628namespace ast {
     
    2830template <typename C, typename... T>
    2931constexpr auto make_array(T&&... values) ->
    30         std::array<C,sizeof...(T)>
     32        array<C,sizeof...(T)>
    3133{
    32         return std::array<C,sizeof...(T)>{
    33                 std::forward<T>(values)...
     34        return array<C,sizeof...(T)>{
     35                forward<T>(values)...
    3436        };
    3537}
     
    3739class Printer : public Visitor {
    3840public:
    39         std::ostream & os;
     41        ostream & os;
    4042        Indenter indent;
    41 
    42         Printer(std::ostream & os, Indenter indent) : os( os ), indent( indent ) {}
     43        bool short_mode;
     44
     45        Printer(ostream & os, Indenter indent, bool short_mode) : os( os ), indent( indent ), short_mode(short_mode) {}
    4346
    4447private:
     
    5154                                // need an endl after each element because it's not
    5255                                // easy to know when each individual item should end
    53                                 os << std::endl;
     56                                os << endl;
    5457                        } // if
    5558                } // 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::Node * n ) {
     74                if ( ! n ) { undefined(); return; }
     75                bool old_short = short_mode; short_mode = true;
     76                n->accept( *this );
     77                short_mode = old_short;
    5678        }
    5779
     
    7496
    7597        template<typename storage_t, size_t N>
    76         void print(const storage_t & storage, const std::array<const char *, N> & Names ) {
     98        void print(const storage_t & storage, const array<const char *, N> & Names ) {
    7799                if ( storage.any() ) {
    78100                        for ( size_t i = 0; i < Names.size(); i += 1 ) {
     
    96118        }
    97119
     120        void print( const ast::Expr::InferUnion & inferred, unsigned level = 0 ) {
     121                switch ( inferred.mode ) {
     122                case ast::Expr::InferUnion::Empty: return;
     123                case ast::Expr::InferUnion::Slots: {
     124                        os << indent << "with " << inferred.data.resnSlots.size() << " pending inference slots"
     125                           << std::endl;
     126                        return;
     127                }
     128                case ast::Expr::InferUnion::Params: {
     129                        os << indent << "with inferred parameters " << level << ":" << std::endl;
     130                        ++indent;
     131                        for ( const auto & i : inferred.data.inferParams ) {
     132                                os << indent;
     133                                short_print( Decl::fromId( i.second.decl ) );
     134                                os << std::endl;
     135                                print( i.second.expr->inferred, level+1 );
     136                        }
     137                        --indent;
     138                        return;
     139                }
     140                }
     141        }
     142
    98143        void print( const ast::ParameterizedType::ForallList & forall ) {
    99144                if ( forall.empty() ) return;   
     
    119164                printAll( params );
    120165                --indent;
     166        }
     167
     168        void print( const ast::AggregateDecl * node ) {
     169                os << node->typeString() << " " << node->name << ":";
     170                if ( node->linkage != Linkage::Cforall ) {
     171                        os << " " << Linkage::name( node->linkage );
     172                } // if
     173                os << " with body : " << (node->body ? "yes " : "no ");
     174
     175                if ( ! node->params.empty() ) {
     176                        os << endl << indent << "... with parameters" << endl;
     177                        ++indent;
     178                        printAll( node->params );
     179                        --indent;
     180                } // if
     181                if ( ! node->members.empty() ) {
     182                        os << endl << indent << "... with members" << endl;
     183                        ++indent;
     184                        printAll( node->members );
     185                        --indent;
     186                } // if
     187                if ( ! node->attributes.empty() ) {
     188                        os << endl << indent << "... with attributes" << endl;
     189                        ++indent;
     190                        printAll( node->attributes );
     191                        --indent;
     192                } // if
     193                os << endl;
     194        }
     195
     196        void print( const ast::NamedTypeDecl * node ) {
     197                if ( !node->name.empty() ) os << node->name << ": ";
     198
     199                if ( node->linkage != Linkage::Cforall ) {
     200                        os << Linkage::name( node->linkage ) << " ";
     201                } // if
     202                print( node->storage );
     203                os << node->typeString();
     204                if ( node->base ) {
     205                        os << " for ";
     206                        ++indent;
     207                        node->base->accept( *this );
     208                        --indent;
     209                } // if
     210                if ( ! node->params.empty() ) {
     211                        os << endl << indent << "... with parameters" << endl;
     212                        ++indent;
     213                        printAll( node->params );
     214                        --indent;
     215                } // if
     216                if ( ! node->assertions.empty() ) {
     217                        os << endl << indent << "... with assertions" << endl;
     218                        ++indent;
     219                        printAll( node->assertions );
     220                        --indent;
     221                } // if
     222        }
     223
     224        void postprint( const ast::Expr * node ) {
     225                print( node->inferred );
     226
     227                if ( node->env ) {
     228                        os << std::endl << indent << "... with environment:" << std::endl;
     229                        ++indent;
     230                        node->env->accept( *this );
     231                        --indent;
     232                }
     233               
     234                if ( node->extension ) {
     235                        os << std::endl << indent << "... with extension";
     236                }
    121237        }
    122238
     
    137253
    138254public:
    139         virtual const ast::DeclWithType *     visit( const ast::ObjectDecl          * node ) {
    140                 if ( node->name != "" ) os << node->name << ": ";
     255        virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) {
     256                if ( !node->name.empty() ) os << node->name << ": ";
    141257
    142258                if ( node->linkage != Linkage::Cforall ) {
     
    149265                        node->type->accept( *this );
    150266                } else {
    151                         os << " untyped entity ";
     267                        os << "untyped entity";
    152268                } // if
    153269
     
    157273                                        ? "maybe constructed"
    158274                                        : "not constructed"
    159                                 ) << ")" << std::endl << indent+1;
     275                                ) << ")" << endl << indent+1;
    160276
    161277                        ++indent;
    162278                        node->init->accept( *this );
    163279                        --indent;
    164                         os << std::endl;
     280                        os << endl;
    165281                } // if
    166282
    167283                if ( ! node->attributes.empty() ) {
    168                         os << std::endl << indent << "... with attributes:" << std::endl;
     284                        os << endl << indent << "... with attributes:" << endl;
    169285                        ++indent;
    170286                        printAll( node->attributes );
     
    179295        }
    180296
    181         virtual const ast::DeclWithType *     visit( const ast::FunctionDecl         * node ) {
    182                 return node;
    183         }
    184 
    185         virtual const ast::Decl *             visit( const ast::StructDecl           * node ) {
    186                 return node;
    187         }
    188 
    189         virtual const ast::Decl *             visit( const ast::UnionDecl            * node ) {
    190                 return node;
    191         }
    192 
    193         virtual const ast::Decl *             visit( const ast::EnumDecl             * node ) {
    194                 return node;
    195         }
    196 
    197         virtual const ast::Decl *             visit( const ast::TraitDecl            * node ) {
    198                 return node;
    199         }
    200 
    201         virtual const ast::Decl *             visit( const ast::TypeDecl             * node ) {
    202                 return node;
    203         }
    204 
    205         virtual const ast::Decl *             visit( const ast::TypedefDecl          * node ) {
    206                 return node;
    207         }
    208 
    209         virtual const ast::AsmDecl *          visit( const ast::AsmDecl              * node ) {
    210                 return node;
    211         }
    212 
    213         virtual const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl     * node ) {
    214                 return node;
    215         }
    216 
    217         virtual const ast::CompoundStmt *     visit( const ast::CompoundStmt         * node ) {
    218                 return node;
    219         }
    220 
    221         virtual const ast::Stmt *             visit( const ast::ExprStmt             * node ) {
    222                 return node;
    223         }
    224 
    225         virtual const ast::Stmt *             visit( const ast::AsmStmt              * node ) {
    226                 return node;
    227         }
    228 
    229         virtual const ast::Stmt *             visit( const ast::DirectiveStmt        * node ) {
    230                 return node;
    231         }
    232 
    233         virtual const ast::Stmt *             visit( const ast::IfStmt               * node ) {
    234                 return node;
    235         }
    236 
    237         virtual const ast::Stmt *             visit( const ast::WhileStmt            * node ) {
    238                 return node;
    239         }
    240 
    241         virtual const ast::Stmt *             visit( const ast::ForStmt              * node ) {
    242                 return node;
    243         }
    244 
    245         virtual const ast::Stmt *             visit( const ast::SwitchStmt           * node ) {
    246                 return node;
    247         }
    248 
    249         virtual const ast::Stmt *             visit( const ast::CaseStmt             * node ) {
    250                 return node;
    251         }
    252 
    253         virtual const ast::Stmt *             visit( const ast::BranchStmt           * node ) {
    254                 return node;
    255         }
    256 
    257         virtual const ast::Stmt *             visit( const ast::ReturnStmt           * node ) {
    258                 return node;
    259         }
    260 
    261         virtual const ast::Stmt *             visit( const ast::ThrowStmt            * node ) {
    262                 return node;
    263         }
    264 
    265         virtual const ast::Stmt *             visit( const ast::TryStmt              * node ) {
    266                 return node;
    267         }
    268 
    269         virtual const ast::Stmt *             visit( const ast::CatchStmt            * node ) {
    270                 return node;
    271         }
    272 
    273         virtual const ast::Stmt *             visit( const ast::FinallyStmt          * node ) {
    274                 return node;
    275         }
    276 
    277         virtual const ast::Stmt *             visit( const ast::WaitForStmt          * node ) {
    278                 return node;
    279         }
    280 
    281         virtual const ast::Stmt *             visit( const ast::WithStmt             * node ) {
    282                 return node;
    283         }
    284 
    285         virtual const ast::NullStmt *         visit( const ast::NullStmt             * node ) {
    286                 return node;
    287         }
    288 
    289         virtual const ast::Stmt *             visit( const ast::DeclStmt             * node ) {
    290                 return node;
    291         }
    292 
    293         virtual const ast::Stmt *             visit( const ast::ImplicitCtorDtorStmt * node ) {
    294                 return node;
    295         }
    296 
    297         virtual const ast::Expr *             visit( const ast::ApplicationExpr      * node ) {
    298                 return node;
    299         }
    300 
    301         virtual const ast::Expr *             visit( const ast::UntypedExpr          * node ) {
    302                 return node;
    303         }
    304 
    305         virtual const ast::Expr *             visit( const ast::NameExpr             * node ) {
    306                 return node;
    307         }
    308 
    309         virtual const ast::Expr *             visit( const ast::AddressExpr          * node ) {
    310                 return node;
    311         }
    312 
    313         virtual const ast::Expr *             visit( const ast::LabelAddressExpr     * node ) {
    314                 return node;
    315         }
    316 
    317         virtual const ast::Expr *             visit( const ast::CastExpr             * node ) {
    318                 return node;
    319         }
    320 
    321         virtual const ast::Expr *             visit( const ast::KeywordCastExpr      * node ) {
    322                 return node;
    323         }
    324 
    325         virtual const ast::Expr *             visit( const ast::VirtualCastExpr      * node ) {
    326                 return node;
    327         }
    328 
    329         virtual const ast::Expr *             visit( const ast::UntypedMemberExpr    * node ) {
    330                 return node;
    331         }
    332 
    333         virtual const ast::Expr *             visit( const ast::MemberExpr           * node ) {
    334                 return node;
    335         }
    336 
    337         virtual const ast::Expr *             visit( const ast::VariableExpr         * node ) {
    338                 return node;
    339         }
    340 
    341         virtual const ast::Expr *             visit( const ast::ConstantExpr         * node ) {
    342                 return node;
    343         }
    344 
    345         virtual const ast::Expr *             visit( const ast::SizeofExpr           * node ) {
    346                 return node;
    347         }
    348 
    349         virtual const ast::Expr *             visit( const ast::AlignofExpr          * node ) {
    350                 return node;
    351         }
    352 
    353         virtual const ast::Expr *             visit( const ast::UntypedOffsetofExpr  * node ) {
    354                 return node;
    355         }
    356 
    357         virtual const ast::Expr *             visit( const ast::OffsetofExpr         * node ) {
    358                 return node;
    359         }
    360 
    361         virtual const ast::Expr *             visit( const ast::OffsetPackExpr       * node ) {
    362                 return node;
    363         }
    364 
    365         virtual const ast::Expr *             visit( const ast::LogicalExpr          * node ) {
    366                 return node;
    367         }
    368 
    369         virtual const ast::Expr *             visit( const ast::ConditionalExpr      * node ) {
    370                 return node;
    371         }
    372 
    373         virtual const ast::Expr *             visit( const ast::CommaExpr            * node ) {
    374                 return node;
    375         }
    376 
    377         virtual const ast::Expr *             visit( const ast::TypeExpr             * node ) {
    378                 return node;
    379         }
    380 
    381         virtual const ast::Expr *             visit( const ast::AsmExpr              * node ) {
    382                 return node;
    383         }
    384 
    385         virtual const ast::Expr *             visit( const ast::ImplicitCopyCtorExpr * node ) {
    386                 return node;
    387         }
    388 
    389         virtual const ast::Expr *             visit( const ast::ConstructorExpr      * node ) {
    390                 return node;
    391         }
    392 
    393         virtual const ast::Expr *             visit( const ast::CompoundLiteralExpr  * node ) {
    394                 return node;
    395         }
    396 
    397         virtual const ast::Expr *             visit( const ast::RangeExpr            * node ) {
    398                 return node;
    399         }
    400 
    401         virtual const ast::Expr *             visit( const ast::UntypedTupleExpr     * node ) {
    402                 return node;
    403         }
    404 
    405         virtual const ast::Expr *             visit( const ast::TupleExpr            * node ) {
    406                 return node;
    407         }
    408 
    409         virtual const ast::Expr *             visit( const ast::TupleIndexExpr       * node ) {
    410                 return node;
    411         }
    412 
    413         virtual const ast::Expr *             visit( const ast::TupleAssignExpr      * node ) {
    414                 return node;
    415         }
    416 
    417         virtual const ast::Expr *             visit( const ast::StmtExpr             * node ) {
    418                 return node;
    419         }
    420 
    421         virtual const ast::Expr *             visit( const ast::UniqueExpr           * node ) {
    422                 return node;
    423         }
    424 
    425         virtual const ast::Expr *             visit( const ast::UntypedInitExpr      * node ) {
    426                 return node;
    427         }
    428 
    429         virtual const ast::Expr *             visit( const ast::InitExpr             * node ) {
    430                 return node;
    431         }
    432 
    433         virtual const ast::Expr *             visit( const ast::DeletedExpr          * node ) {
    434                 return node;
    435         }
    436 
    437         virtual const ast::Expr *             visit( const ast::DefaultArgExpr       * node ) {
    438                 return node;
    439         }
    440 
    441         virtual const ast::Expr *             visit( const ast::GenericExpr          * node ) {
    442                 return node;
    443         }
    444 
    445         virtual const ast::Type *             visit( const ast::VoidType             * node ) {
     297        virtual const ast::DeclWithType * visit( const ast::FunctionDecl * node ) {
     298                if ( !node->name.empty() ) {
     299                        os << node->name << ": ";
     300                } // if
     301                if ( node->linkage != Linkage::Cforall ) {
     302                        os << Linkage::name( node->linkage ) << " ";
     303                } // if
     304
     305                printAll( node->attributes );
     306
     307                print( node->storage );
     308                print( node->funcSpec );
     309
     310                if ( node->type ) {
     311                        node->type->accept( *this );
     312                } else {
     313                        os << "untyped entity";
     314                } // if
     315
     316                if ( node->stmts ) {
     317                        os << indent << "... with body" << endl << indent+1;
     318                        ++indent;
     319                        node->stmts->accept( *this );
     320                        --indent;
     321                } // if
     322                return node;
     323        }
     324
     325        virtual const ast::Decl * visit( const ast::StructDecl * node ) {
     326                print(node);
     327                return node;
     328        }
     329
     330        virtual const ast::Decl * visit( const ast::UnionDecl * node ) {
     331                print(node);
     332                return node;
     333        }
     334
     335        virtual const ast::Decl * visit( const ast::EnumDecl * node ) {
     336                print(node);
     337                return node;
     338        }
     339
     340        virtual const ast::Decl * visit( const ast::TraitDecl * node ) {
     341                print(node);
     342                return node;
     343        }
     344
     345        virtual const ast::Decl * visit( const ast::TypeDecl * node ) {
     346                print( node );
     347                if ( node->init ) {
     348                        os << endl << indent << "with type initializer: ";
     349                        ++indent;
     350                        node->init->accept( *this );
     351                        --indent;
     352                }
     353                return node;
     354        }
     355
     356        virtual const ast::Decl * visit( const ast::TypedefDecl * node ) {
     357                print( node );
     358                return node;
     359        }
     360
     361        virtual const ast::AsmDecl * visit( const ast::AsmDecl * node ) {
     362                node->stmt->accept( *this );
     363                return node;
     364        }
     365
     366        virtual const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) {
     367                os << "Static Assert with condition: ";
     368                ++indent;
     369                node->cond->accept( *this );
     370                --indent;
     371                os << endl << indent << "and message: ";
     372                ++indent;
     373                node->msg->accept( *this );
     374                --indent;
     375                os << endl;
     376                return node;
     377        }
     378
     379        virtual const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) {
     380                os << "CompoundStmt" << endl;
     381                ++indent;
     382                printAll( node->kids );
     383                --indent;
     384                return node;
     385        }
     386
     387        virtual const ast::Stmt * visit( const ast::ExprStmt * node ) {
     388                ++indent;
     389                os << "Expression Statement:" << endl << indent;
     390                safe_print( node->expr );
     391                --indent;
     392                return node;
     393        }
     394
     395        virtual const ast::Stmt * visit( const ast::AsmStmt * node ) {
     396                os << "Assembler Statement:" << endl;
     397                ++indent;
     398                os << indent << "instruction: " << endl << indent;
     399                node->instruction->accept( *this );
     400                if ( ! node->output.empty() ) {
     401                        os << endl << indent+1 << "output: " << endl;
     402                        printAll( node->output );
     403                } // if
     404                if ( ! node->input.empty() ) {
     405                        os << indent+1 << "input: " << endl;
     406                        printAll( node->input );
     407                } // if
     408                if ( ! node->clobber.empty() ) {
     409                        os << indent+1 << "clobber: " << endl;
     410                        printAll( node->clobber );
     411                } // if
     412                --indent;
     413                return node;
     414        }
     415
     416        virtual const ast::Stmt * visit( const ast::DirectiveStmt * node ) {
     417                os << "GCC Directive:" << node->directive << endl;
     418                return node;
     419        }
     420
     421        virtual const ast::Stmt * visit( const ast::IfStmt * node ) {
     422                os << "If on condition: " << endl;
     423                os << indent+1;
     424                ++indent;
     425                safe_print( node->cond );
     426                --indent;
     427
     428                if ( !node->inits.empty() ) {
     429                        os << indent << "... with initialization: \n";
     430                        ++indent;
     431                        for ( const Stmt * stmt : node->inits ) {
     432                                os << indent;
     433                                stmt->accept( *this );
     434                        }
     435                        --indent;
     436                        os << endl;
     437                }
     438
     439                os << indent << "... then: " << endl;
     440
     441                ++indent;
     442                os << indent;
     443                safe_print( node->thenPart );
     444                --indent;
     445
     446                if ( node->elsePart != 0 ) {
     447                        os << indent << "... else: " << endl;
     448                        ++indent;
     449                        os << indent;
     450                        node->elsePart->accept( *this );
     451                        --indent;
     452                } // if
     453                return node;
     454        }
     455
     456        virtual const ast::Stmt * visit( const ast::WhileStmt * node ) {
     457                return node;
     458        }
     459
     460        virtual const ast::Stmt * visit( const ast::ForStmt * node ) {
     461                return node;
     462        }
     463
     464        virtual const ast::Stmt * visit( const ast::SwitchStmt * node ) {
     465                return node;
     466        }
     467
     468        virtual const ast::Stmt * visit( const ast::CaseStmt * node ) {
     469                return node;
     470        }
     471
     472        virtual const ast::Stmt * visit( const ast::BranchStmt * node ) {
     473                return node;
     474        }
     475
     476        virtual const ast::Stmt * visit( const ast::ReturnStmt * node ) {
     477                return node;
     478        }
     479
     480        virtual const ast::Stmt * visit( const ast::ThrowStmt * node ) {
     481                return node;
     482        }
     483
     484        virtual const ast::Stmt * visit( const ast::TryStmt * node ) {
     485                return node;
     486        }
     487
     488        virtual const ast::Stmt * visit( const ast::CatchStmt * node ) {
     489                return node;
     490        }
     491
     492        virtual const ast::Stmt * visit( const ast::FinallyStmt * node ) {
     493                return node;
     494        }
     495
     496        virtual const ast::Stmt * visit( const ast::WaitForStmt * node ) {
     497                return node;
     498        }
     499
     500        virtual const ast::Stmt * visit( const ast::WithStmt * node ) {
     501                return node;
     502        }
     503
     504        virtual const ast::NullStmt * visit( const ast::NullStmt * node ) {
     505                return node;
     506        }
     507
     508        virtual const ast::Stmt * visit( const ast::DeclStmt * node ) {
     509                return node;
     510        }
     511
     512        virtual const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) {
     513                return node;
     514        }
     515
     516        virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) {
     517                ++indent;
     518                os << "Application of" << std::endl << indent;
     519                safe_print( node->func );
     520                os << std::endl;
     521                if ( ! node->args.empty() ) {
     522                        os << indent << "... to arguments" << std::endl;
     523                        printAll( node->args );
     524                }
     525                --indent;
     526                postprint( node );
     527
     528                return node;
     529        }
     530
     531        virtual const ast::Expr * visit( const ast::UntypedExpr * node ) {
     532                ++indent;
     533                os << "Applying untyped:" << std::endl;
     534                os << indent;
     535                safe_print( node->func );
     536                os << std::endl << indent-1 << "...to:" << std::endl;
     537                printAll( node->args );
     538                --indent;
     539                postprint( node );
     540
     541                return node;
     542        }
     543
     544        virtual const ast::Expr * visit( const ast::NameExpr * node ) {
     545                os << "Name: " << node->name;
     546                postprint( node );
     547               
     548                return node;
     549        }
     550
     551        virtual const ast::Expr * visit( const ast::AddressExpr * node ) {
     552                os << "Address of:" << std::endl;
     553                ++indent;
     554                os << indent;
     555                safe_print( node->arg );
     556
     557                --indent;
     558
     559                return node;
     560        }
     561
     562        virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) {
     563                os << "Address of label:" << node->arg;
     564
     565                return node;
     566        }
     567
     568        virtual const ast::Expr * visit( const ast::CastExpr * node ) {
     569                ++indent;
     570                os << (node->isGenerated ? "Generated" : "Explicit") << " cast of:" << std::endl << indent;
     571                safe_print( node->arg );
     572                os << std::endl << indent-1 << "... to:";
     573                if ( ! node->result ) {
     574                        os << " ";
     575                        undefined();
     576                } else if ( node->result->isVoid() ) {
     577                        os << " nothing";
     578                } else {
     579                        os << std::endl << indent;
     580                        node->result->accept( *this );
     581                } // if
     582                --indent;
     583                postprint( node );
     584
     585                return node;
     586        }
     587
     588        virtual const ast::Expr * visit( const ast::KeywordCastExpr * node ) {
     589                ++indent;
     590                os << "Keyword Cast of:" << std::endl << indent;
     591                safe_print( node->arg );
     592                --indent;
     593                os << std::endl << indent << "... to: " << node->targetString();
     594                postprint( node );
     595
     596                return node;
     597        }
     598
     599        virtual const ast::Expr * visit( const ast::VirtualCastExpr * node ) {
     600                ++indent;
     601                os << "Virtual Cast of:" << std::endl << indent;
     602                safe_print( node->arg );
     603                os << std::endl << indent-1 << "... to:";
     604                if ( ! node->result ) {
     605                        os << " unknown";
     606                } else {
     607                        os << std::endl << indent;
     608                        node->result->accept( *this );
     609                }
     610                --indent;
     611                postprint( node );
     612
     613                return node;
     614        }
     615
     616        virtual const ast::Expr * visit( const ast::UntypedMemberExpr * node ) {
     617                ++indent;
     618                os << "Untyped Member Expression, with field: " << std::endl << indent;
     619                safe_print( node->member );
     620                os << indent-1 << "... from aggregate:" << std::endl << indent;
     621                safe_print( node->aggregate );
     622                --indent;
     623                postprint( node );
     624
     625                return node;
     626        }
     627
     628        virtual const ast::Expr * visit( const ast::MemberExpr * node ) {
     629                ++indent;
     630                os << "Member Expression, with field:" << std::endl << indent;
     631                safe_print( node->member );
     632                os << std::endl << indent-1 << "... from aggregate:" << std::endl << indent;
     633                safe_print( node->aggregate );
     634                --indent;
     635                postprint( node );
     636
     637                return node;
     638        }
     639
     640        virtual const ast::Expr * visit( const ast::VariableExpr * node ) {
     641                os << "Variable Expression: ";
     642                short_print( node->var );
     643                postprint( node );
     644
     645                return node;
     646        }
     647
     648        virtual const ast::Expr * visit( const ast::ConstantExpr * node ) {
     649                os << "Constant Expression (" << node->rep;
     650                if ( node->result ) {
     651                        os << ": ";
     652                        node->result->accept( *this );
     653                }
     654                os << ")";
     655                postprint( node );
     656
     657                return node;
     658        }
     659
     660        virtual const ast::Expr * visit( const ast::SizeofExpr * node ) {
     661                return node;
     662        }
     663
     664        virtual const ast::Expr * visit( const ast::AlignofExpr * node ) {
     665                return node;
     666        }
     667
     668        virtual const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) {
     669                return node;
     670        }
     671
     672        virtual const ast::Expr * visit( const ast::OffsetofExpr * node ) {
     673                return node;
     674        }
     675
     676        virtual const ast::Expr * visit( const ast::OffsetPackExpr * node ) {
     677                return node;
     678        }
     679
     680        virtual const ast::Expr * visit( const ast::LogicalExpr * node ) {
     681                return node;
     682        }
     683
     684        virtual const ast::Expr * visit( const ast::ConditionalExpr * node ) {
     685                return node;
     686        }
     687
     688        virtual const ast::Expr * visit( const ast::CommaExpr * node ) {
     689                return node;
     690        }
     691
     692        virtual const ast::Expr * visit( const ast::TypeExpr * node ) {
     693                return node;
     694        }
     695
     696        virtual const ast::Expr * visit( const ast::AsmExpr * node ) {
     697                return node;
     698        }
     699
     700        virtual const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) {
     701                return node;
     702        }
     703
     704        virtual const ast::Expr * visit( const ast::ConstructorExpr * node ) {
     705                return node;
     706        }
     707
     708        virtual const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) {
     709                return node;
     710        }
     711
     712        virtual const ast::Expr * visit( const ast::RangeExpr * node ) {
     713                return node;
     714        }
     715
     716        virtual const ast::Expr * visit( const ast::UntypedTupleExpr * node ) {
     717                return node;
     718        }
     719
     720        virtual const ast::Expr * visit( const ast::TupleExpr * node ) {
     721                return node;
     722        }
     723
     724        virtual const ast::Expr * visit( const ast::TupleIndexExpr * node ) {
     725                return node;
     726        }
     727
     728        virtual const ast::Expr * visit( const ast::TupleAssignExpr * node ) {
     729                return node;
     730        }
     731
     732        virtual const ast::Expr * visit( const ast::StmtExpr * node ) {
     733                return node;
     734        }
     735
     736        virtual const ast::Expr * visit( const ast::UniqueExpr * node ) {
     737                return node;
     738        }
     739
     740        virtual const ast::Expr * visit( const ast::UntypedInitExpr * node ) {
     741                return node;
     742        }
     743
     744        virtual const ast::Expr * visit( const ast::InitExpr * node ) {
     745                return node;
     746        }
     747
     748        virtual const ast::Expr * visit( const ast::DeletedExpr * node ) {
     749                return node;
     750        }
     751
     752        virtual const ast::Expr * visit( const ast::DefaultArgExpr * node ) {
     753                return node;
     754        }
     755
     756        virtual const ast::Expr * visit( const ast::GenericExpr * node ) {
     757                return node;
     758        }
     759
     760        virtual const ast::Type * visit( const ast::VoidType * node ) {
    446761                preprint( node );
    447762                os << "void";
     
    449764        }
    450765
    451         virtual const ast::Type *             visit( const ast::BasicType            * node ) {
     766        virtual const ast::Type * visit( const ast::BasicType * node ) {
    452767                preprint( node );
    453768                os << ast::BasicType::typeNames[ node->kind ];
     
    455770        }
    456771
    457         virtual const ast::Type *             visit( const ast::PointerType          * node ) {
     772        virtual const ast::Type * visit( const ast::PointerType * node ) {
    458773                preprint( node );
    459774                if ( ! node->isArray() ) {
     
    473788                        }
    474789                }
    475 
    476                 if ( node->base ) {
    477                         node->base->accept( *this );
    478                 } else {
    479                         os << "UNDEFINED";
    480                 }
    481                 return node;
    482         }
    483 
    484         virtual const ast::Type *             visit( const ast::ArrayType            * node ) {
     790                safe_print( node->base );
     791
     792                return node;
     793        }
     794
     795        virtual const ast::Type * visit( const ast::ArrayType * node ) {
    485796                preprint( node );
    486797                if ( node->isStatic ) {
     
    496807                }
    497808
    498                 if ( node->base ) {
    499                         node->base->accept( *this );
    500                 } else {
    501                         os << "UNDEFINED";
    502                 }
     809                safe_print( node->base );
    503810
    504811                if ( node->dimension ) {
     
    510817        }
    511818
    512         virtual const ast::Type *             visit( const ast::ReferenceType        * node ) {
    513                 preprint( node );
    514 
     819        virtual const ast::Type * visit( const ast::ReferenceType * node ) {
     820                preprint( node );
    515821                os << "reference to ";
    516                 if ( node->base ) {
    517                         node->base->accept( *this );
    518                 } else {
    519                         os << "UNDEFINED";
    520                 }
    521 
    522                 return node;
    523         }
    524 
    525         virtual const ast::Type *             visit( const ast::QualifiedType        * node ) {
    526                 preprint( node );
    527 
     822                safe_print( node->base );
     823
     824                return node;
     825        }
     826
     827        virtual const ast::Type * visit( const ast::QualifiedType * node ) {
     828                preprint( node );
    528829                ++indent;
    529830                os << "Qualified Type:" << std::endl << indent;
    530                 node->parent->accept( *this );
     831                safe_print( node->parent );
    531832                os << std::endl << indent;
    532                 node->child->accept( *this );
     833                safe_print( node->child );
    533834                os << std::endl;
    534835                --indent;
     
    537838        }
    538839
    539         virtual const ast::Type *             visit( const ast::FunctionType        * node ) {
    540                 preprint( node );
    541 
     840        virtual const ast::Type * visit( const ast::FunctionType * node ) {
     841                preprint( node );
     842               
    542843                os << "function" << std::endl;
    543844                if ( ! node->params.empty() ) {
     
    566867        }
    567868
    568         virtual const ast::Type *             visit( const ast::StructInstType       * node ) {
    569                 preprint( node );
    570 
     869        virtual const ast::Type * visit( const ast::StructInstType * node ) {
     870                preprint( node );
    571871                os << "instance of struct " << node->name;
    572872                if ( node->base ) {
     
    578878        }
    579879
    580         virtual const ast::Type *             visit( const ast::UnionInstType        * node ) {
    581                 preprint( node );
    582 
     880        virtual const ast::Type * visit( const ast::UnionInstType * node ) {
     881                preprint( node );
    583882                os << "instance of union " << node->name;
    584883                if ( node->base ) {
     
    590889        }
    591890
    592         virtual const ast::Type *             visit( const ast::EnumInstType         * node ) {
    593                 preprint( node );
    594 
     891        virtual const ast::Type * visit( const ast::EnumInstType * node ) {
     892                preprint( node );
    595893                os << "instance of enum " << node->name;
    596894                if ( node->base ) {
     
    602900        }
    603901
    604         virtual const ast::Type *             visit( const ast::TraitInstType        * node ) {
    605                 preprint( node );
    606 
     902        virtual const ast::Type * visit( const ast::TraitInstType * node ) {
     903                preprint( node );
    607904                os << "instance of trait " << node->name;
    608905                print( node->params );
     
    611908        }
    612909
    613         virtual const ast::Type *             visit( const ast::TypeInstType         * node ) {
    614                 preprint( node );
    615 
     910        virtual const ast::Type * visit( const ast::TypeInstType * node ) {
     911                preprint( node );
    616912                os << "instance of type " << node->name
    617913                   << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)";
     
    621917        }
    622918
    623         virtual const ast::Type *             visit( const ast::TupleType            * node ) {
    624                 preprint( node );
    625 
     919        virtual const ast::Type * visit( const ast::TupleType * node ) {
     920                preprint( node );
    626921                os << "tuple of types" << std::endl;
    627922                ++indent;
     
    632927        }
    633928
    634         virtual const ast::Type *             visit( const ast::TypeofType           * node ) {
    635                 preprint( node );
    636 
     929        virtual const ast::Type * visit( const ast::TypeofType * node ) {
     930                preprint( node );
    637931                if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
    638932                os << "type-of expression ";
    639                 if ( node->expr ) {
    640                         node->expr->accept( *this );
    641                 } else {
    642                         os << "UNDEFINED";
    643                 }
    644 
    645                 return node;
    646         }
    647 
    648         virtual const ast::Type *             visit( const ast::VarArgsType          * node ) {
     933                safe_print( node->expr );
     934
     935                return node;
     936        }
     937
     938        virtual const ast::Type * visit( const ast::VarArgsType * node ) {
    649939                preprint( node );
    650940                os << "builtin var args pack";
     
    652942        }
    653943
    654         virtual const ast::Type *             visit( const ast::ZeroType            * node ) {
     944        virtual const ast::Type * visit( const ast::ZeroType * node ) {
    655945                preprint( node );
    656946                os << "zero_t";
     
    658948        }
    659949
    660         virtual const ast::Type *             visit( const ast::OneType              * node ) {
     950        virtual const ast::Type * visit( const ast::OneType * node ) {
    661951                preprint( node );
    662952                os << "one_t";
     
    664954        }
    665955
    666         virtual const ast::Type *             visit( const ast::GlobalScopeType      * node ) {
     956        virtual const ast::Type * visit( const ast::GlobalScopeType * node ) {
    667957                preprint( node );
    668958                os << "Global Scope Type";
     
    670960        }
    671961
    672         virtual const ast::Designation *      visit( const ast::Designation          * node ) {
     962        virtual const ast::Designation * visit( const ast::Designation * node ) {
    673963                if ( node->designators.empty() ) return node;
    674964                os << "... designated by: " << std::endl;
     
    683973        }
    684974
    685         virtual const ast::Init *             visit( const ast::SingleInit          * node ) {
     975        virtual const ast::Init * visit( const ast::SingleInit * node ) {
    686976                os << "Simple Initializer: ";
    687                 node->value->accept( *this );
    688                 return node;
    689         }
    690 
    691         virtual const ast::Init *             visit( const ast::ListInit            * node ) {
     977                safe_print( node->value );
     978                return node;
     979        }
     980
     981        virtual const ast::Init * visit( const ast::ListInit * node ) {
    692982                os << "Compound initializer: " << std::endl;
    693983                ++indent;
     
    707997        }
    708998
    709         virtual const ast::Init *             visit( const ast::ConstructorInit      * node ) {
     999        virtual const ast::Init * visit( const ast::ConstructorInit * node ) {
    7101000                os << "Constructor initializer: " << std::endl;
    7111001                if ( node->ctor ) {
     
    7321022        }
    7331023
    734         virtual const ast::Attribute *        visit( const ast::Attribute            * node ) {
     1024        virtual const ast::Attribute * visit( const ast::Attribute * node ) {
    7351025                if ( node->empty() ) return node;
    7361026                os << "Attribute with name: " << node->name;
     
    7431033        }
    7441034
    745         virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution     * node ) {
     1035        virtual const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) {
    7461036                os << indent << "Types:" << std::endl;
    7471037                for ( const auto& i : *node ) {
    7481038                        os << indent+1 << i.first << " -> ";
    7491039                        indent += 2;
    750                         i.second->accept( *this );
     1040                        safe_print( i.second );
    7511041                        indent -= 2;
    7521042                        os << std::endl;
     
    7561046                        os << indent+1 << i->first << " -> ";
    7571047                        indent += 2;
    758                         i->second->accept( *this );
     1048                        safe_print( i->second );
    7591049                        indent -= 2;
    7601050                        os << std::endl;
     
    7651055};
    7661056
    767 void print( std::ostream & os, const ast::Node * node, Indenter indent ) {
    768         Printer printer { os, indent };
     1057void print( ostream & os, const ast::Node * node, Indenter indent ) {
     1058        Printer printer { os, indent, false };
     1059        node->accept(printer);
     1060}
     1061
     1062void printShort( ostream & os, const ast::Node * node, Indenter indent ) {
     1063        Printer printer { os, indent, true };
    7691064        node->accept(printer);
    7701065}
     
    7731068// The size here needs to be explicit but at least the compiler will produce an error
    7741069// if the wrong size is specified
    775 constexpr std::array<const char*, 3> Printer::Names::FuncSpecifiers;
    776 constexpr std::array<const char*, 5> Printer::Names::StorageClasses;
    777 constexpr std::array<const char*, 6> Printer::Names::Qualifiers;
     1070constexpr array<const char*, 3> Printer::Names::FuncSpecifiers;
     1071constexpr array<const char*, 5> Printer::Names::StorageClasses;
     1072constexpr array<const char*, 6> Printer::Names::Qualifiers;
    7781073}
Note: See TracChangeset for help on using the changeset viewer.