Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r2f22cc4 ra839867  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Aug 10 11:07:38 2016
    13 // Update Count     : 486
     12// Last Modified On : Thu Aug 25 21:39:40 2016
     13// Update Count     : 503
    1414//
    1515
     
    3232using namespace std;
    3333
    34 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ), extension( other.extension ) {}
     34ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.get_name() ), extension( other.extension ) {}
    3535
    3636//##############################################################################
     
    5757static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    5858
    59 Expression *build_constantInteger( std::string & str ) {
     59Expression *build_constantInteger( const std::string & str ) {
    6060        static const BasicType::Kind kind[2][3] = {
    6161                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
     
    120120        } // if
    121121
    122         return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) );
     122        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) );
     123        delete &str;                                                                            // created by lex
     124        return ret;
    123125} // build_constantInteger
    124126
    125 Expression *build_constantFloat( std::string & str ) {
     127Expression *build_constantFloat( const std::string & str ) {
    126128        static const BasicType::Kind kind[2][3] = {
    127129                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
     
    150152        } // if
    151153
    152         return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) );
     154        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) );
     155        delete &str;                                                                            // created by lex
     156        return ret;
    153157} // build_constantFloat
    154158
    155 Expression *build_constantChar( std::string & str ) {
    156         return new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) );
     159Expression *build_constantChar( const std::string & str ) {
     160        Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) );
     161        delete &str;                                                                            // created by lex
     162        return ret;
    157163} // build_constantChar
    158164
    159 ConstantExpr *build_constantStr( std::string & str ) {
     165ConstantExpr *build_constantStr( const std::string & str ) {
    160166        // string should probably be a primitive type
    161167        ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ),
     
    163169                                                                                        toString( str.size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
    164170                                                                   false, false );
    165         return new ConstantExpr( Constant( at, str ) );
     171        ConstantExpr * ret = new ConstantExpr( Constant( at, str ) );
     172        delete &str;                                                                            // created by lex
     173        return ret;
    166174} // build_constantStr
    167175
    168 //##############################################################################
    169 
    170176NameExpr * build_varref( const string *name, bool labelp ) {
    171         return new NameExpr( *name, nullptr );
    172 }
    173 
    174 //##############################################################################
     177        NameExpr *expr = new NameExpr( *name, nullptr );
     178        delete name;
     179        return expr;
     180}
    175181
    176182static const char *OperName[] = {
     
    178184        "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
    179185        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
    180         "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
     186        "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
    181187        "?[?]", "...",
    182188        // monadic
     
    184190};
    185191
    186 //##############################################################################
    187 
    188192Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {
    189         Type *targetType = decl_node->buildType();
     193        Type *targetType = maybeMoveBuildType( decl_node );
    190194        if ( dynamic_cast< VoidType * >( targetType ) ) {
    191195                delete targetType;
    192                 return new CastExpr( maybeBuild<Expression>(expr_node) );
     196                return new CastExpr( maybeMoveBuild< Expression >(expr_node) );
    193197        } else {
    194                 return new CastExpr( maybeBuild<Expression>(expr_node), targetType );
     198                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType );
    195199        } // if
    196200}
    197201
    198202Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ) {
    199         UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeBuild<Expression>(expr_node) );
     203        UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeMoveBuild< Expression >(expr_node) );
    200204        delete member;
    201205        return ret;
     
    204208Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ) {
    205209        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    206         deref->get_args().push_back( maybeBuild<Expression>(expr_node) );
     210        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
    207211        UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref );
    208212        delete member;
     
    211215
    212216Expression *build_addressOf( ExpressionNode *expr_node ) {
    213                 return new AddressExpr( maybeBuild<Expression>(expr_node) );
     217                return new AddressExpr( maybeMoveBuild< Expression >(expr_node) );
    214218}
    215219Expression *build_sizeOfexpr( ExpressionNode *expr_node ) {
    216         return new SizeofExpr( maybeBuild<Expression>(expr_node) );
     220        return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) );
    217221}
    218222Expression *build_sizeOftype( DeclarationNode *decl_node ) {
    219         return new SizeofExpr( decl_node->buildType() );
     223        return new SizeofExpr( maybeMoveBuildType( decl_node ) );
    220224}
    221225Expression *build_alignOfexpr( ExpressionNode *expr_node ) {
    222         return new AlignofExpr( maybeBuild<Expression>(expr_node) );
     226        return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) );
    223227}
    224228Expression *build_alignOftype( DeclarationNode *decl_node ) {
    225         return new AlignofExpr( decl_node->buildType() );
     229        return new AlignofExpr( maybeMoveBuildType( decl_node) );
    226230}
    227231Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) {
    228         return new UntypedOffsetofExpr( decl_node->buildType(), member->get_name() );
     232        Expression* ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
     233        delete member;
     234        return ret;
    229235}
    230236
    231237Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {
    232         return new LogicalExpr( notZeroExpr( maybeBuild<Expression>(expr_node1) ), notZeroExpr( maybeBuild<Expression>(expr_node2) ), kind );
     238        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
    233239}
    234240
    235241Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
    236         std::list<Expression *> args;
    237         args.push_back( maybeBuild<Expression>(expr_node) );
     242        std::list< Expression * > args;
     243        args.push_back( maybeMoveBuild< Expression >(expr_node) );
    238244        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    239245}
    240246Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
    241         std::list<Expression *> args;
    242         args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node) ) );
     247        std::list< Expression * > args;
     248        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
    243249        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    244250}
    245251Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    246         std::list<Expression *> args;
    247         args.push_back( maybeBuild<Expression>(expr_node1) );
    248         args.push_back( maybeBuild<Expression>(expr_node2) );
     252        std::list< Expression * > args;
     253        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
     254        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
    249255        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    250256}
    251257Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    252         std::list<Expression *> args;
    253         args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node1) ) );
    254         args.push_back( maybeBuild<Expression>(expr_node2) );
     258        std::list< Expression * > args;
     259        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
     260        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
    255261        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    256262}
    257263
    258264Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {
    259         return new ConditionalExpr( notZeroExpr( maybeBuild<Expression>(expr_node1) ), maybeBuild<Expression>(expr_node2), maybeBuild<Expression>(expr_node3) );
     265        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
    260266}
    261267
    262268Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    263         return new CommaExpr( maybeBuild<Expression>(expr_node1), maybeBuild<Expression>(expr_node2) );
     269        return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) );
    264270}
    265271
    266272Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {
    267         return new AttrExpr( var, maybeBuild<Expression>(expr_node) );
     273        return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) );
    268274}
    269275Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) {
    270         return new AttrExpr( var, decl_node->buildType() );
     276        return new AttrExpr( var, maybeMoveBuildType( decl_node ) );
    271277}
    272278
    273279Expression *build_tuple( ExpressionNode * expr_node ) {
    274280        TupleExpr *ret = new TupleExpr();
    275         buildList( expr_node, ret->get_exprs() );
     281        buildMoveList( expr_node, ret->get_exprs() );
    276282        return ret;
    277283}
    278284
    279285Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
    280         std::list<Expression *> args;
    281 
    282         buildList( expr_node, args );
    283         return new UntypedExpr( maybeBuild<Expression>(function), args, nullptr );
     286        std::list< Expression * > args;
     287        buildMoveList( expr_node, args );
     288        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
    284289}
    285290
    286291Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
    287         Expression *low_cexpr = maybeBuild<Expression>( low );
    288         Expression *high_cexpr = maybeBuild<Expression>( high );
    289         return new RangeExpr( low_cexpr, high_cexpr );
    290 }
    291 
    292 //##############################################################################
    293 
    294 Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
    295         return new AsmExpr( maybeBuild< Expression >( inout ), constraint, maybeBuild<Expression>(operand) );
    296 }
    297 
    298 //##############################################################################
    299 
    300 void LabelNode::print( std::ostream &os, int indent ) const {}
    301 
    302 void LabelNode::printOneLine( std::ostream &os, int indent ) const {}
    303 
    304 //##############################################################################
     292        return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) );
     293}
     294
     295Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {
     296        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
     297}
    305298
    306299Expression *build_valexpr( StatementNode *s ) {
    307         return new UntypedValofExpr( maybeBuild<Statement>(s), nullptr );
    308 }
    309 
    310 //##############################################################################
    311  
    312 // ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
    313 //      if ( init_ == 0 )
    314 //              init = 0;
    315 //      else {
    316 //              DeclarationNode *decl;
    317 //              ExpressionNode *exp;
    318 
    319 //              if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0)
    320 //                      init = new StatementNode( decl );
    321 //              else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0)
    322 //                      init = new StatementNode( StatementNode::Exp, exp );
    323 //              else
    324 //                      throw SemanticError("Error in for control expression");
    325 //      }
    326 // }
    327 
    328 // ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other )
    329 //      : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) {
    330 // }
    331 
    332 // ForCtlExprNode::~ForCtlExprNode() {
    333 //      delete init;
    334 //      delete condition;
    335 //      delete change;
    336 // }
    337 
    338 // Expression *ForCtlExprNode::build() const {
    339 //      // this shouldn't be used!
    340 //      assert( false );
    341 //      return 0;
    342 // }
    343 
    344 // void ForCtlExprNode::print( std::ostream &os, int indent ) const{
    345 //      os << string( indent,' ' ) << "For Control Expression -- :" << endl;
    346 
    347 //      os << string( indent + 2, ' ' ) << "initialization:" << endl;
    348 //      if ( init != 0 )
    349 //              init->printList( os, indent + 4 );
    350 
    351 //      os << string( indent + 2, ' ' ) << "condition: " << endl;
    352 //      if ( condition != 0 )
    353 //              condition->print( os, indent + 4 );
    354 //      os << string( indent + 2, ' ' ) << "increment: " << endl;
    355 //      if ( change != 0 )
    356 //              change->print( os, indent + 4 );
    357 // }
    358 
    359 // void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const {
    360 //      assert( false );
    361 // }
    362 
    363 //##############################################################################
    364 
     300        return new UntypedValofExpr( maybeMoveBuild< Statement >(s), nullptr );
     301}
    365302Expression *build_typevalue( DeclarationNode *decl ) {
    366         return new TypeExpr( decl->buildType() );
    367 }
    368 
    369 //##############################################################################
     303        return new TypeExpr( maybeMoveBuildType( decl ) );
     304}
    370305
    371306Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {
    372         Declaration * newDecl = maybeBuild<Declaration>(decl_node); // compound literal type
     307        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
    373308        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
    374                 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeBuild<Initializer>(kids) );
     309                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
    375310        // these types do not have associated type information
    376311        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
    377                 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeBuild<Initializer>(kids) );
     312                return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
    378313        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
    379                 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeBuild<Initializer>(kids) );
     314                return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
    380315        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
    381                 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeBuild<Initializer>(kids) );
     316                return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
    382317        } else {
    383318                assert( false );
Note: See TracChangeset for help on using the changeset viewer.