Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Print.cpp

    r20a5977 r335f2d8  
    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 
    8061
    8162        static const char* Names[];
     
    11899        }
    119100
    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 
    168101        void print( const ast::AggregateDecl * node ) {
    169102                os << node->typeString() << " " << node->name << ":";
     
    222155        }
    223156
    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 
    254157public:
    255158        virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) {
     
    265168                        node->type->accept( *this );
    266169                } else {
    267                         os << "untyped entity";
     170                        os << " untyped entity ";
    268171                } // if
    269172
     
    311214                        node->type->accept( *this );
    312215                } else {
    313                         os << "untyped entity";
     216                        os << "untyped entity ";
    314217                } // if
    315218
     
    388291                ++indent;
    389292                os << "Expression Statement:" << endl << indent;
    390                 safe_print( node->expr );
     293                node->expr->accept( *this );
    391294                --indent;
    392295                return node;
     
    423326                os << indent+1;
    424327                ++indent;
    425                 safe_print( node->cond );
     328                node->cond->accept( *this );
    426329                --indent;
    427330
     
    441344                ++indent;
    442345                os << indent;
    443                 safe_print( node->thenPart );
     346                node->thenPart->accept( *this );
    444347                --indent;
    445348
     
    515418
    516419        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 
    528420                return node;
    529421        }
    530422
    531423        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 
    541424                return node;
    542425        }
    543426
    544427        virtual const ast::Expr * visit( const ast::NameExpr * node ) {
    545                 os << "Name: " << node->name;
    546                 postprint( node );
    547                
    548428                return node;
    549429        }
    550430
    551431        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 
    559432                return node;
    560433        }
    561434
    562435        virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) {
    563                 os << "Address of label:" << node->arg;
    564 
    565436                return node;
    566437        }
    567438
    568439        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 
    585440                return node;
    586441        }
    587442
    588443        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 
    596444                return node;
    597445        }
    598446
    599447        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 
    613448                return node;
    614449        }
    615450
    616451        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 
    625452                return node;
    626453        }
    627454
    628455        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 
    637456                return node;
    638457        }
    639458
    640459        virtual const ast::Expr * visit( const ast::VariableExpr * node ) {
    641                 os << "Variable Expression: ";
    642                 short_print( node->var );
    643                 postprint( node );
    644 
    645460                return node;
    646461        }
    647462
    648463        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 
    657464                return node;
    658465        }
     
    759566
    760567        virtual const ast::Type * visit( const ast::VoidType * node ) {
    761                 preprint( node );
    762                 os << "void";
    763568                return node;
    764569        }
    765570
    766571        virtual const ast::Type * visit( const ast::BasicType * node ) {
    767                 preprint( node );
    768                 os << ast::BasicType::typeNames[ node->kind ];
    769572                return node;
    770573        }
    771574
    772575        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 
    792576                return node;
    793577        }
    794578
    795579        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 
    816580                return node;
    817581        }
    818582
    819583        virtual const ast::Type * visit( const ast::ReferenceType * node ) {
    820                 preprint( node );
    821                 os << "reference to ";
    822                 safe_print( node->base );
    823 
    824584                return node;
    825585        }
    826586
    827587        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 
    837588                return node;
    838589        }
    839590
    840591        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 
    866592                return node;
    867593        }
    868594
    869595        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 
    877596                return node;
    878597        }
    879598
    880599        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 
    888600                return node;
    889601        }
    890602
    891603        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 
    899604                return node;
    900605        }
    901606
    902607        virtual const ast::Type * visit( const ast::TraitInstType * node ) {
    903                 preprint( node );
    904                 os << "instance of trait " << node->name;
    905                 print( node->params );
    906 
    907608                return node;
    908609        }
    909610
    910611        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 
    916612                return node;
    917613        }
    918614
    919615        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 
    926616                return node;
    927617        }
    928618
    929619        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 
    935620                return node;
    936621        }
    937622
    938623        virtual const ast::Type * visit( const ast::VarArgsType * node ) {
    939                 preprint( node );
    940                 os << "builtin var args pack";
    941624                return node;
    942625        }
    943626
    944627        virtual const ast::Type * visit( const ast::ZeroType * node ) {
    945                 preprint( node );
    946                 os << "zero_t";
    947628                return node;
    948629        }
    949630
    950631        virtual const ast::Type * visit( const ast::OneType * node ) {
    951                 preprint( node );
    952                 os << "one_t";
    953632                return node;
    954633        }
    955634
    956635        virtual const ast::Type * visit( const ast::GlobalScopeType * node ) {
    957                 preprint( node );
    958                 os << "Global Scope Type";
    959636                return node;
    960637        }
     
    975652        virtual const ast::Init * visit( const ast::SingleInit * node ) {
    976653                os << "Simple Initializer: ";
    977                 safe_print( node->value );
     654                node->value->accept( *this );
    978655                return node;
    979656        }
     
    1038715                        os << indent+1 << i.first << " -> ";
    1039716                        indent += 2;
    1040                         safe_print( i.second );
     717                        i.second->accept( *this );
    1041718                        indent -= 2;
    1042719                        os << std::endl;
     
    1046723                        os << indent+1 << i->first << " -> ";
    1047724                        indent += 2;
    1048                         safe_print( i->second );
     725                        i->second->accept( *this );
    1049726                        indent -= 2;
    1050727                        os << std::endl;
Note: See TracChangeset for help on using the changeset viewer.