Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Print.cpp

    r335f2d8 r20a5977  
    5959        }
    6060
     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;
     78        }
     79
    6180
    6281        static const char* Names[];
     
    99118        }
    100119
     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
     143        void print( const ast::ParameterizedType::ForallList & forall ) {
     144                if ( forall.empty() ) return;   
     145                os << "forall" << std::endl;
     146                ++indent;
     147                printAll( forall );
     148                os << indent;
     149                --indent;
     150        }
     151
     152        void print( const std::vector<ptr<Attribute>> & attrs ) {
     153                if ( attrs.empty() ) return;
     154                os << "with attributes" << std::endl;
     155                ++indent;
     156                printAll( attrs );
     157                --indent;
     158        }
     159
     160        void print( const std::vector<ptr<Expr>> & params ) {
     161                if ( params.empty() ) return;
     162                os << std::endl << indent << "... with parameters" << std::endl;
     163                ++indent;
     164                printAll( params );
     165                --indent;
     166        }
     167
    101168        void print( const ast::AggregateDecl * node ) {
    102169                os << node->typeString() << " " << node->name << ":";
     
    155222        }
    156223
     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                }
     237        }
     238
     239        void preprint( const ast::Type * node ) {
     240                print( node->qualifiers );
     241        }
     242
     243        void preprint( const ast::ParameterizedType * node ) {
     244                print( node->forall );
     245                print( node->qualifiers );
     246        }
     247
     248        void preprint( const ast::ReferenceToType * node ) {
     249                print( node->forall );
     250                print( node->attributes );
     251                print( node->qualifiers );
     252        }
     253
    157254public:
    158255        virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) {
     
    168265                        node->type->accept( *this );
    169266                } else {
    170                         os << " untyped entity ";
     267                        os << "untyped entity";
    171268                } // if
    172269
     
    214311                        node->type->accept( *this );
    215312                } else {
    216                         os << "untyped entity ";
     313                        os << "untyped entity";
    217314                } // if
    218315
     
    291388                ++indent;
    292389                os << "Expression Statement:" << endl << indent;
    293                 node->expr->accept( *this );
     390                safe_print( node->expr );
    294391                --indent;
    295392                return node;
     
    326423                os << indent+1;
    327424                ++indent;
    328                 node->cond->accept( *this );
     425                safe_print( node->cond );
    329426                --indent;
    330427
     
    344441                ++indent;
    345442                os << indent;
    346                 node->thenPart->accept( *this );
     443                safe_print( node->thenPart );
    347444                --indent;
    348445
     
    418515
    419516        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
    420528                return node;
    421529        }
    422530
    423531        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
    424541                return node;
    425542        }
    426543
    427544        virtual const ast::Expr * visit( const ast::NameExpr * node ) {
     545                os << "Name: " << node->name;
     546                postprint( node );
     547               
    428548                return node;
    429549        }
    430550
    431551        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
    432559                return node;
    433560        }
    434561
    435562        virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) {
     563                os << "Address of label:" << node->arg;
     564
    436565                return node;
    437566        }
    438567
    439568        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
    440585                return node;
    441586        }
    442587
    443588        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
    444596                return node;
    445597        }
    446598
    447599        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
    448613                return node;
    449614        }
    450615
    451616        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
    452625                return node;
    453626        }
    454627
    455628        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
    456637                return node;
    457638        }
    458639
    459640        virtual const ast::Expr * visit( const ast::VariableExpr * node ) {
     641                os << "Variable Expression: ";
     642                short_print( node->var );
     643                postprint( node );
     644
    460645                return node;
    461646        }
    462647
    463648        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
    464657                return node;
    465658        }
     
    566759
    567760        virtual const ast::Type * visit( const ast::VoidType * node ) {
     761                preprint( node );
     762                os << "void";
    568763                return node;
    569764        }
    570765
    571766        virtual const ast::Type * visit( const ast::BasicType * node ) {
     767                preprint( node );
     768                os << ast::BasicType::typeNames[ node->kind ];
    572769                return node;
    573770        }
    574771
    575772        virtual const ast::Type * visit( const ast::PointerType * node ) {
     773                preprint( node );
     774                if ( ! node->isArray() ) {
     775                        os << "pointer to ";
     776                } else {
     777                        os << "decayed ";
     778                        if ( node->isStatic ) {
     779                                os << "static ";
     780                        }
     781
     782                        if ( node->isVarLen ) {
     783                                os << "variable length array of ";
     784                        } else if ( node->dimension ) {
     785                                os << "array of ";
     786                                node->dimension->accept( *this );
     787                                os << " ";
     788                        }
     789                }
     790                safe_print( node->base );
     791
    576792                return node;
    577793        }
    578794
    579795        virtual const ast::Type * visit( const ast::ArrayType * node ) {
     796                preprint( node );
     797                if ( node->isStatic ) {
     798                        os << "static ";
     799                }
     800
     801                if ( node->isVarLen ) {
     802                        os << "variable length array of ";
     803                } else if ( node->dimension ) {
     804                        os << "array of ";
     805                } else {
     806                        os << "open array of ";
     807                }
     808
     809                safe_print( node->base );
     810
     811                if ( node->dimension ) {
     812                        os << " with dimension of ";
     813                        node->dimension->accept( *this );
     814                }
     815
    580816                return node;
    581817        }
    582818
    583819        virtual const ast::Type * visit( const ast::ReferenceType * node ) {
     820                preprint( node );
     821                os << "reference to ";
     822                safe_print( node->base );
     823
    584824                return node;
    585825        }
    586826
    587827        virtual const ast::Type * visit( const ast::QualifiedType * node ) {
     828                preprint( node );
     829                ++indent;
     830                os << "Qualified Type:" << std::endl << indent;
     831                safe_print( node->parent );
     832                os << std::endl << indent;
     833                safe_print( node->child );
     834                os << std::endl;
     835                --indent;
     836
    588837                return node;
    589838        }
    590839
    591840        virtual const ast::Type * visit( const ast::FunctionType * node ) {
     841                preprint( node );
     842               
     843                os << "function" << std::endl;
     844                if ( ! node->params.empty() ) {
     845                        os << indent << "... with parameters" << std::endl;
     846                        ++indent;
     847                        printAll( node->params );
     848                        if ( node->isVarArgs ) {
     849                                os << indent << "and a variable number of other arguments" << std::endl;
     850                        }
     851                        --indent;
     852                } else if ( node->isVarArgs ) {
     853                        os << indent+1 << "accepting unspecified arguments" << std::endl;
     854                }
     855
     856                os << indent << "... returning";
     857                if ( node->returns.empty() ) {
     858                        os << " nothing" << std::endl;
     859                } else {
     860                        os << std::endl;
     861                        ++indent;
     862                        printAll( node->returns );
     863                        --indent;
     864                }
     865
    592866                return node;
    593867        }
    594868
    595869        virtual const ast::Type * visit( const ast::StructInstType * node ) {
     870                preprint( node );
     871                os << "instance of struct " << node->name;
     872                if ( node->base ) {
     873                        os << " " << ( node->base->body ? "with" : "without" ) << " body";
     874                }
     875                print( node->params );
     876
    596877                return node;
    597878        }
    598879
    599880        virtual const ast::Type * visit( const ast::UnionInstType * node ) {
     881                preprint( node );
     882                os << "instance of union " << node->name;
     883                if ( node->base ) {
     884                        os << " " << ( node->base->body ? "with" : "without" ) << " body";
     885                }
     886                print( node->params );
     887
    600888                return node;
    601889        }
    602890
    603891        virtual const ast::Type * visit( const ast::EnumInstType * node ) {
     892                preprint( node );
     893                os << "instance of enum " << node->name;
     894                if ( node->base ) {
     895                        os << " " << ( node->base->body ? "with" : "without" ) << " body";
     896                }
     897                print( node->params );
     898
    604899                return node;
    605900        }
    606901
    607902        virtual const ast::Type * visit( const ast::TraitInstType * node ) {
     903                preprint( node );
     904                os << "instance of trait " << node->name;
     905                print( node->params );
     906
    608907                return node;
    609908        }
    610909
    611910        virtual const ast::Type * visit( const ast::TypeInstType * node ) {
     911                preprint( node );
     912                os << "instance of type " << node->name
     913                   << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)";
     914                print( node->params );
     915
    612916                return node;
    613917        }
    614918
    615919        virtual const ast::Type * visit( const ast::TupleType * node ) {
     920                preprint( node );
     921                os << "tuple of types" << std::endl;
     922                ++indent;
     923                printAll( node->types );
     924                --indent;
     925
    616926                return node;
    617927        }
    618928
    619929        virtual const ast::Type * visit( const ast::TypeofType * node ) {
     930                preprint( node );
     931                if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
     932                os << "type-of expression ";
     933                safe_print( node->expr );
     934
    620935                return node;
    621936        }
    622937
    623938        virtual const ast::Type * visit( const ast::VarArgsType * node ) {
     939                preprint( node );
     940                os << "builtin var args pack";
    624941                return node;
    625942        }
    626943
    627944        virtual const ast::Type * visit( const ast::ZeroType * node ) {
     945                preprint( node );
     946                os << "zero_t";
    628947                return node;
    629948        }
    630949
    631950        virtual const ast::Type * visit( const ast::OneType * node ) {
     951                preprint( node );
     952                os << "one_t";
    632953                return node;
    633954        }
    634955
    635956        virtual const ast::Type * visit( const ast::GlobalScopeType * node ) {
     957                preprint( node );
     958                os << "Global Scope Type";
    636959                return node;
    637960        }
     
    652975        virtual const ast::Init * visit( const ast::SingleInit * node ) {
    653976                os << "Simple Initializer: ";
    654                 node->value->accept( *this );
     977                safe_print( node->value );
    655978                return node;
    656979        }
     
    7151038                        os << indent+1 << i.first << " -> ";
    7161039                        indent += 2;
    717                         i.second->accept( *this );
     1040                        safe_print( i.second );
    7181041                        indent -= 2;
    7191042                        os << std::endl;
     
    7231046                        os << indent+1 << i->first << " -> ";
    7241047                        indent += 2;
    725                         i->second->accept( *this );
     1048                        safe_print( i->second );
    7261049                        indent -= 2;
    7271050                        os << std::endl;
Note: See TracChangeset for help on using the changeset viewer.