Changes in / [af1e8f56:effe5b0]


Ignore:
Location:
src/AST
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    raf1e8f56 reffe5b0  
    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
    702738        const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
    703739                ConstantExpr *rslt = nullptr;
    704                 switch ( node->kind ) {
    705                 case ast::ConstantExpr::Integer:
    706                         rslt = new ConstantExpr{Constant{
    707                                 get<Type>().accept1( node->result ),
     740                if (isIntlikeConstantType(node->result)) {
     741                        rslt = new ConstantExpr(Constant(
     742                                get<Type>().accept1(node->result),
    708743                                node->rep,
    709744                                (unsigned long long) node->intValue()
    710                         }};
    711                         break;
    712                 case ast::ConstantExpr::FloatingPoint:
    713                         rslt = new ConstantExpr{Constant{
     745                        ));
     746                } else if (isFloatlikeConstantType(node->result)) {
     747                        rslt = new ConstantExpr(Constant(
    714748                                get<Type>().accept1(node->result),
    715749                                node->rep,
    716750                                (double) node->floatValue()
    717                         }};
    718                         break;
    719                 case ast::ConstantExpr::String:
    720                         rslt = new ConstantExpr{Constant::from_string( node->rep )};
    721                         break;
     751                        ));
     752                } else if (isStringlikeConstantType(node->result)) {
     753                        rslt = new ConstantExpr(Constant::from_string(
     754                                node->rep
     755                        ));
    722756                }
    723757                assert(rslt);
  • src/AST/Expr.cpp

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

    raf1e8f56 reffe5b0  
    337337public:
    338338        std::string rep;
    339         enum Kind { Integer, FloatingPoint, String } kind;
    340339
    341340        ConstantExpr(
    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 ) {}
     341                const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v )
     342        : Expr( loc, ty ), val( v ), rep( r ) {}
    345343        ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v )
    346         : Expr( loc, ty ), val( v ), rep( r ), kind( FloatingPoint ) {}
     344        : Expr( loc, ty ), val( v ), rep( r ) {}
    347345
    348346        /// Gets the value of this constant as an integer
  • src/AST/Print.cpp

    raf1e8f56 reffe5b0  
    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 
    143101        void print( const ast::ParameterizedType::ForallList & forall ) {
    144102                if ( forall.empty() ) return;   
     
    166124        }
    167125
     126        void preprint( const ast::Type * node ) {
     127                print( node->qualifiers );
     128        }
     129
     130        void preprint( const ast::ParameterizedType * node ) {
     131                print( node->forall );
     132                print( node->qualifiers );
     133        }
     134
     135        void preprint( const ast::ReferenceToType * node ) {
     136                print( node->forall );
     137                print( node->attributes );
     138                print( node->qualifiers );
     139        }
     140       
    168141        void print( const ast::AggregateDecl * node ) {
    169142                os << node->typeString() << " " << node->name << ":";
     
    222195        }
    223196
    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 
    254197public:
    255198        virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) {
     
    265208                        node->type->accept( *this );
    266209                } else {
    267                         os << "untyped entity";
     210                        os << " untyped entity ";
    268211                } // if
    269212
     
    311254                        node->type->accept( *this );
    312255                } else {
    313                         os << "untyped entity";
     256                        os << "untyped entity ";
    314257                } // if
    315258
     
    388331                ++indent;
    389332                os << "Expression Statement:" << endl << indent;
    390                 safe_print( node->expr );
     333                node->expr->accept( *this );
    391334                --indent;
    392335                return node;
     
    423366                os << indent+1;
    424367                ++indent;
    425                 safe_print( node->cond );
     368                node->cond->accept( *this );
    426369                --indent;
    427370
     
    441384                ++indent;
    442385                os << indent;
    443                 safe_print( node->thenPart );
     386                node->thenPart->accept( *this );
    444387                --indent;
    445388
     
    515458
    516459        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 
    528460                return node;
    529461        }
    530462
    531463        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 
    541464                return node;
    542465        }
    543466
    544467        virtual const ast::Expr * visit( const ast::NameExpr * node ) {
    545                 os << "Name: " << node->name;
    546                 postprint( node );
    547                
    548468                return node;
    549469        }
    550470
    551471        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 
    559472                return node;
    560473        }
    561474
    562475        virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) {
    563                 os << "Address of label:" << node->arg;
    564 
    565476                return node;
    566477        }
    567478
    568479        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 
    585480                return node;
    586481        }
    587482
    588483        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 
    596484                return node;
    597485        }
    598486
    599487        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 
    613488                return node;
    614489        }
    615490
    616491        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 
    625492                return node;
    626493        }
    627494
    628495        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 
    637496                return node;
    638497        }
    639498
    640499        virtual const ast::Expr * visit( const ast::VariableExpr * node ) {
    641                 os << "Variable Expression: ";
    642                 short_print( node->var );
    643                 postprint( node );
    644 
    645500                return node;
    646501        }
    647502
    648503        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 
    657504                return node;
    658505        }
     
    788635                        }
    789636                }
    790                 safe_print( node->base );
    791 
     637
     638                if ( node->base ) {
     639                        node->base->accept( *this );
     640                } else {
     641                        os << "UNDEFINED";
     642                }
    792643                return node;
    793644        }
     
    807658                }
    808659
    809                 safe_print( node->base );
     660                if ( node->base ) {
     661                        node->base->accept( *this );
     662                } else {
     663                        os << "UNDEFINED";
     664                }
    810665
    811666                if ( node->dimension ) {
     
    819674        virtual const ast::Type * visit( const ast::ReferenceType * node ) {
    820675                preprint( node );
     676
    821677                os << "reference to ";
    822                 safe_print( node->base );
     678                if ( node->base ) {
     679                        node->base->accept( *this );
     680                } else {
     681                        os << "UNDEFINED";
     682                }
    823683
    824684                return node;
     
    827687        virtual const ast::Type * visit( const ast::QualifiedType * node ) {
    828688                preprint( node );
     689
    829690                ++indent;
    830691                os << "Qualified Type:" << std::endl << indent;
    831                 safe_print( node->parent );
     692                node->parent->accept( *this );
    832693                os << std::endl << indent;
    833                 safe_print( node->child );
     694                node->child->accept( *this );
    834695                os << std::endl;
    835696                --indent;
     
    840701        virtual const ast::Type * visit( const ast::FunctionType * node ) {
    841702                preprint( node );
    842                
     703
    843704                os << "function" << std::endl;
    844705                if ( ! node->params.empty() ) {
     
    869730        virtual const ast::Type * visit( const ast::StructInstType * node ) {
    870731                preprint( node );
     732
    871733                os << "instance of struct " << node->name;
    872734                if ( node->base ) {
     
    880742        virtual const ast::Type * visit( const ast::UnionInstType * node ) {
    881743                preprint( node );
     744
    882745                os << "instance of union " << node->name;
    883746                if ( node->base ) {
     
    891754        virtual const ast::Type * visit( const ast::EnumInstType * node ) {
    892755                preprint( node );
     756
    893757                os << "instance of enum " << node->name;
    894758                if ( node->base ) {
     
    902766        virtual const ast::Type * visit( const ast::TraitInstType * node ) {
    903767                preprint( node );
     768
    904769                os << "instance of trait " << node->name;
    905770                print( node->params );
     
    910775        virtual const ast::Type * visit( const ast::TypeInstType * node ) {
    911776                preprint( node );
     777
    912778                os << "instance of type " << node->name
    913779                   << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)";
     
    919785        virtual const ast::Type * visit( const ast::TupleType * node ) {
    920786                preprint( node );
     787
    921788                os << "tuple of types" << std::endl;
    922789                ++indent;
     
    929796        virtual const ast::Type * visit( const ast::TypeofType * node ) {
    930797                preprint( node );
     798
    931799                if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; }
    932800                os << "type-of expression ";
    933                 safe_print( node->expr );
     801                if ( node->expr ) {
     802                        node->expr->accept( *this );
     803                } else {
     804                        os << "UNDEFINED";
     805                }
    934806
    935807                return node;
     
    975847        virtual const ast::Init * visit( const ast::SingleInit * node ) {
    976848                os << "Simple Initializer: ";
    977                 safe_print( node->value );
     849                node->value->accept( *this );
    978850                return node;
    979851        }
     
    1038910                        os << indent+1 << i.first << " -> ";
    1039911                        indent += 2;
    1040                         safe_print( i.second );
     912                        i.second->accept( *this );
    1041913                        indent -= 2;
    1042914                        os << std::endl;
     
    1046918                        os << indent+1 << i->first << " -> ";
    1047919                        indent += 2;
    1048                         safe_print( i->second );
     920                        i->second->accept( *this );
    1049921                        indent -= 2;
    1050922                        os << std::endl;
Note: See TracChangeset for help on using the changeset viewer.