Changes in / [effe5b0:af1e8f56]


Ignore:
Location:
src/AST
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    reffe5b0 raf1e8f56  
    700700        }
    701701
    702         bool isIntlikeConstantType(const ast::Type *t) {
    703                 if ( const ast::BasicType * basicType = dynamic_cast< const ast::BasicType * >( t ) ) {
    704                         if ( basicType->isInteger() ) {
    705                                 return true;
    706                         }
    707                 } else if ( dynamic_cast< const ast::OneType * >( t ) ) {
    708                         return true;
    709                 } else if ( dynamic_cast< const ast::ZeroType * >( t ) ) {
    710                         return true;
    711                 } else if ( dynamic_cast< const ast::PointerType * >( t ) ) {
    712                         // null pointer constants, with zero int-values
    713                         return true;
    714                 }
    715                 return false;
    716         }
    717 
    718         bool isFloatlikeConstantType(const ast::Type *t) {
    719                 if ( const ast::BasicType * bty = dynamic_cast< const ast::BasicType * >( t ) ) {
    720                         if ( ! bty->isInteger() ) {
    721                                 return true;
    722                         }
    723                 }
    724                 return false;
    725         }
    726 
    727         bool isStringlikeConstantType(const ast::Type *t) {
    728                 if ( const ast::ArrayType * aty = dynamic_cast< const ast::ArrayType * >( t ) ) {
    729                         if ( const ast::BasicType * bty = aty->base.as<ast::BasicType>() ) {
    730                            if ( bty->kind == ast::BasicType::Kind::Char ) {
    731                                    return true;
    732                            }
    733                         }
    734                 }
    735                 return false;
    736         }
    737 
    738702        const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
    739703                ConstantExpr *rslt = nullptr;
    740                 if (isIntlikeConstantType(node->result)) {
    741                         rslt = new ConstantExpr(Constant(
    742                                 get<Type>().accept1(node->result),
     704                switch ( node->kind ) {
     705                case ast::ConstantExpr::Integer:
     706                        rslt = new ConstantExpr{Constant{
     707                                get<Type>().accept1( node->result ),
    743708                                node->rep,
    744709                                (unsigned long long) node->intValue()
    745                         ));
    746                 } else if (isFloatlikeConstantType(node->result)) {
    747                         rslt = new ConstantExpr(Constant(
     710                        }};
     711                        break;
     712                case ast::ConstantExpr::FloatingPoint:
     713                        rslt = new ConstantExpr{Constant{
    748714                                get<Type>().accept1(node->result),
    749715                                node->rep,
    750716                                (double) node->floatValue()
    751                         ));
    752                 } else if (isStringlikeConstantType(node->result)) {
    753                         rslt = new ConstantExpr(Constant::from_string(
    754                                 node->rep
    755                         ));
     717                        }};
     718                        break;
     719                case ast::ConstantExpr::String:
     720                        rslt = new ConstantExpr{Constant::from_string( node->rep )};
     721                        break;
    756722                }
    757723                assert(rslt);
  • src/AST/Expr.cpp

    reffe5b0 raf1e8f56  
    241241                        FixedLen, DynamicDim },
    242242                std::string{"\""} + s + "\"",
    243                 (unsigned long long)0 };
     243                (unsigned long long)0,
     244                ConstantExpr::String };
    244245}
    245246
  • src/AST/Expr.hpp

    reffe5b0 raf1e8f56  
    337337public:
    338338        std::string rep;
     339        enum Kind { Integer, FloatingPoint, String } kind;
    339340
    340341        ConstantExpr(
    341                 const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v )
    342         : Expr( loc, ty ), val( v ), rep( r ) {}
     342                const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v,
     343                Kind k = Integer )
     344        : Expr( loc, ty ), val( v ), rep( r ), kind( k ) {}
    343345        ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v )
    344         : Expr( loc, ty ), val( v ), rep( r ) {}
     346        : Expr( loc, ty ), val( v ), rep( r ), kind( FloatingPoint ) {}
    345347
    346348        /// Gets the value of this constant as an integer
  • src/AST/Print.cpp

    reffe5b0 raf1e8f56  
    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
    101143        void print( const ast::ParameterizedType::ForallList & forall ) {
    102144                if ( forall.empty() ) return;   
     
    122164                printAll( params );
    123165                --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                }
    124237        }
    125238
     
    138251                print( node->qualifiers );
    139252        }
    140        
    141         void print( const ast::AggregateDecl * node ) {
    142                 os << node->typeString() << " " << node->name << ":";
    143                 if ( node->linkage != Linkage::Cforall ) {
    144                         os << " " << Linkage::name( node->linkage );
    145                 } // if
    146                 os << " with body : " << (node->body ? "yes " : "no ");
    147 
    148                 if ( ! node->params.empty() ) {
    149                         os << endl << indent << "... with parameters" << endl;
    150                         ++indent;
    151                         printAll( node->params );
    152                         --indent;
    153                 } // if
    154                 if ( ! node->members.empty() ) {
    155                         os << endl << indent << "... with members" << endl;
    156                         ++indent;
    157                         printAll( node->members );
    158                         --indent;
    159                 } // if
    160                 if ( ! node->attributes.empty() ) {
    161                         os << endl << indent << "... with attributes" << endl;
    162                         ++indent;
    163                         printAll( node->attributes );
    164                         --indent;
    165                 } // if
    166                 os << endl;
    167         }
    168 
    169         void print( const ast::NamedTypeDecl * node ) {
    170                 if ( !node->name.empty() ) os << node->name << ": ";
    171 
    172                 if ( node->linkage != Linkage::Cforall ) {
    173                         os << Linkage::name( node->linkage ) << " ";
    174                 } // if
    175                 print( node->storage );
    176                 os << node->typeString();
    177                 if ( node->base ) {
    178                         os << " for ";
    179                         ++indent;
    180                         node->base->accept( *this );
    181                         --indent;
    182                 } // if
    183                 if ( ! node->params.empty() ) {
    184                         os << endl << indent << "... with parameters" << endl;
    185                         ++indent;
    186                         printAll( node->params );
    187                         --indent;
    188                 } // if
    189                 if ( ! node->assertions.empty() ) {
    190                         os << endl << indent << "... with assertions" << endl;
    191                         ++indent;
    192                         printAll( node->assertions );
    193                         --indent;
    194                 } // if
    195         }
    196253
    197254public:
     
    208265                        node->type->accept( *this );
    209266                } else {
    210                         os << " untyped entity ";
     267                        os << "untyped entity";
    211268                } // if
    212269
     
    254311                        node->type->accept( *this );
    255312                } else {
    256                         os << "untyped entity ";
     313                        os << "untyped entity";
    257314                } // if
    258315
     
    331388                ++indent;
    332389                os << "Expression Statement:" << endl << indent;
    333                 node->expr->accept( *this );
     390                safe_print( node->expr );
    334391                --indent;
    335392                return node;
     
    366423                os << indent+1;
    367424                ++indent;
    368                 node->cond->accept( *this );
     425                safe_print( node->cond );
    369426                --indent;
    370427
     
    384441                ++indent;
    385442                os << indent;
    386                 node->thenPart->accept( *this );
     443                safe_print( node->thenPart );
    387444                --indent;
    388445
     
    458515
    459516        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
    460528                return node;
    461529        }
    462530
    463531        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
    464541                return node;
    465542        }
    466543
    467544        virtual const ast::Expr * visit( const ast::NameExpr * node ) {
     545                os << "Name: " << node->name;
     546                postprint( node );
     547               
    468548                return node;
    469549        }
    470550
    471551        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
    472559                return node;
    473560        }
    474561
    475562        virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) {
     563                os << "Address of label:" << node->arg;
     564
    476565                return node;
    477566        }
    478567
    479568        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
    480585                return node;
    481586        }
    482587
    483588        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
    484596                return node;
    485597        }
    486598
    487599        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
    488613                return node;
    489614        }
    490615
    491616        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
    492625                return node;
    493626        }
    494627
    495628        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
    496637                return node;
    497638        }
    498639
    499640        virtual const ast::Expr * visit( const ast::VariableExpr * node ) {
     641                os << "Variable Expression: ";
     642                short_print( node->var );
     643                postprint( node );
     644
    500645                return node;
    501646        }
    502647
    503648        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
    504657                return node;
    505658        }
     
    635788                        }
    636789                }
    637 
    638                 if ( node->base ) {
    639                         node->base->accept( *this );
    640                 } else {
    641                         os << "UNDEFINED";
    642                 }
     790                safe_print( node->base );
     791
    643792                return node;
    644793        }
     
    658807                }
    659808
    660                 if ( node->base ) {
    661                         node->base->accept( *this );
    662                 } else {
    663                         os << "UNDEFINED";
    664                 }
     809                safe_print( node->base );
    665810
    666811                if ( node->dimension ) {
     
    674819        virtual const ast::Type * visit( const ast::ReferenceType * node ) {
    675820                preprint( node );
    676 
    677821                os << "reference to ";
    678                 if ( node->base ) {
    679                         node->base->accept( *this );
    680                 } else {
    681                         os << "UNDEFINED";
    682                 }
     822                safe_print( node->base );
    683823
    684824                return node;
     
    687827        virtual const ast::Type * visit( const ast::QualifiedType * node ) {
    688828                preprint( node );
    689 
    690829                ++indent;
    691830                os << "Qualified Type:" << std::endl << indent;
    692                 node->parent->accept( *this );
     831                safe_print( node->parent );
    693832                os << std::endl << indent;
    694                 node->child->accept( *this );
     833                safe_print( node->child );
    695834                os << std::endl;
    696835                --indent;
     
    701840        virtual const ast::Type * visit( const ast::FunctionType * node ) {
    702841                preprint( node );
    703 
     842               
    704843                os << "function" << std::endl;
    705844                if ( ! node->params.empty() ) {
     
    730869        virtual const ast::Type * visit( const ast::StructInstType * node ) {
    731870                preprint( node );
    732 
    733871                os << "instance of struct " << node->name;
    734872                if ( node->base ) {
     
    742880        virtual const ast::Type * visit( const ast::UnionInstType * node ) {
    743881                preprint( node );
    744 
    745882                os << "instance of union " << node->name;
    746883                if ( node->base ) {
     
    754891        virtual const ast::Type * visit( const ast::EnumInstType * node ) {
    755892                preprint( node );
    756 
    757893                os << "instance of enum " << node->name;
    758894                if ( node->base ) {
     
    766902        virtual const ast::Type * visit( const ast::TraitInstType * node ) {
    767903                preprint( node );
    768 
    769904                os << "instance of trait " << node->name;
    770905                print( node->params );
     
    775910        virtual const ast::Type * visit( const ast::TypeInstType * node ) {
    776911                preprint( node );
    777 
    778912                os << "instance of type " << node->name
    779913                   << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)";
     
    785919        virtual const ast::Type * visit( const ast::TupleType * node ) {
    786920                preprint( node );
    787 
    788921                os << "tuple of types" << std::endl;
    789922                ++indent;
     
    796929        virtual const ast::Type * visit( const ast::TypeofType * node ) {
    797930                preprint( node );
    798 
    799931                if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
    800932                os << "type-of expression ";
    801                 if ( node->expr ) {
    802                         node->expr->accept( *this );
    803                 } else {
    804                         os << "UNDEFINED";
    805                 }
     933                safe_print( node->expr );
    806934
    807935                return node;
     
    847975        virtual const ast::Init * visit( const ast::SingleInit * node ) {
    848976                os << "Simple Initializer: ";
    849                 node->value->accept( *this );
     977                safe_print( node->value );
    850978                return node;
    851979        }
     
    9101038                        os << indent+1 << i.first << " -> ";
    9111039                        indent += 2;
    912                         i.second->accept( *this );
     1040                        safe_print( i.second );
    9131041                        indent -= 2;
    9141042                        os << std::endl;
     
    9181046                        os << indent+1 << i->first << " -> ";
    9191047                        indent += 2;
    920                         i->second->accept( *this );
     1048                        safe_print( i->second );
    9211049                        indent -= 2;
    9221050                        os << std::endl;
Note: See TracChangeset for help on using the changeset viewer.