Changes in src/Parser/ExpressionNode.cc [2f22cc4:a839867]
- File:
-
- 1 edited
-
src/Parser/ExpressionNode.cc (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r2f22cc4 ra839867 10 10 // Created On : Sat May 16 13:17:07 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Aug 10 11:07:38201613 // Update Count : 48612 // Last Modified On : Thu Aug 25 21:39:40 2016 13 // Update Count : 503 14 14 // 15 15 … … 32 32 using namespace std; 33 33 34 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other. name), extension( other.extension ) {}34 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.get_name() ), extension( other.extension ) {} 35 35 36 36 //############################################################################## … … 57 57 static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } 58 58 59 Expression *build_constantInteger( std::string & str ) {59 Expression *build_constantInteger( const std::string & str ) { 60 60 static const BasicType::Kind kind[2][3] = { 61 61 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, … … 120 120 } // if 121 121 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; 123 125 } // build_constantInteger 124 126 125 Expression *build_constantFloat( std::string & str ) {127 Expression *build_constantFloat( const std::string & str ) { 126 128 static const BasicType::Kind kind[2][3] = { 127 129 { BasicType::Float, BasicType::Double, BasicType::LongDouble }, … … 150 152 } // if 151 153 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; 153 157 } // build_constantFloat 154 158 155 Expression *build_constantChar( std::string & str ) { 156 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) ); 159 Expression *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; 157 163 } // build_constantChar 158 164 159 ConstantExpr *build_constantStr( std::string & str ) {165 ConstantExpr *build_constantStr( const std::string & str ) { 160 166 // string should probably be a primitive type 161 167 ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ), … … 163 169 toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' 164 170 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; 166 174 } // build_constantStr 167 175 168 //##############################################################################169 170 176 NameExpr * build_varref( const string *name, bool labelp ) { 171 returnnew NameExpr( *name, nullptr );172 } 173 174 //############################################################################## 177 NameExpr *expr = new NameExpr( *name, nullptr ); 178 delete name; 179 return expr; 180 } 175 181 176 182 static const char *OperName[] = { … … 178 184 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&", 179 185 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", 180 "?=?", "? *=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",186 "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", 181 187 "?[?]", "...", 182 188 // monadic … … 184 190 }; 185 191 186 //##############################################################################187 188 192 Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { 189 Type *targetType = decl_node->buildType();193 Type *targetType = maybeMoveBuildType( decl_node ); 190 194 if ( dynamic_cast< VoidType * >( targetType ) ) { 191 195 delete targetType; 192 return new CastExpr( maybe Build<Expression>(expr_node) );196 return new CastExpr( maybeMoveBuild< Expression >(expr_node) ); 193 197 } else { 194 return new CastExpr( maybe Build<Expression>(expr_node), targetType );198 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); 195 199 } // if 196 200 } 197 201 198 202 Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ) { 199 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybe Build<Expression>(expr_node) );203 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeMoveBuild< Expression >(expr_node) ); 200 204 delete member; 201 205 return ret; … … 204 208 Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ) { 205 209 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 206 deref->get_args().push_back( maybe Build<Expression>(expr_node) );210 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); 207 211 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); 208 212 delete member; … … 211 215 212 216 Expression *build_addressOf( ExpressionNode *expr_node ) { 213 return new AddressExpr( maybe Build<Expression>(expr_node) );217 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) ); 214 218 } 215 219 Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { 216 return new SizeofExpr( maybe Build<Expression>(expr_node) );220 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) ); 217 221 } 218 222 Expression *build_sizeOftype( DeclarationNode *decl_node ) { 219 return new SizeofExpr( decl_node->buildType() );223 return new SizeofExpr( maybeMoveBuildType( decl_node ) ); 220 224 } 221 225 Expression *build_alignOfexpr( ExpressionNode *expr_node ) { 222 return new AlignofExpr( maybe Build<Expression>(expr_node) );226 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) ); 223 227 } 224 228 Expression *build_alignOftype( DeclarationNode *decl_node ) { 225 return new AlignofExpr( decl_node->buildType() );229 return new AlignofExpr( maybeMoveBuildType( decl_node) ); 226 230 } 227 231 Expression *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; 229 235 } 230 236 231 237 Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) { 232 return new LogicalExpr( notZeroExpr( maybe Build<Expression>(expr_node1) ), notZeroExpr( maybeBuild<Expression>(expr_node2) ), kind );238 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); 233 239 } 234 240 235 241 Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) { 236 std::list< Expression *> args;237 args.push_back( maybe Build<Expression>(expr_node) );242 std::list< Expression * > args; 243 args.push_back( maybeMoveBuild< Expression >(expr_node) ); 238 244 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 239 245 } 240 246 Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { 241 std::list< Expression *> args;242 args.push_back( new AddressExpr( maybe Build<Expression>(expr_node) ) );247 std::list< Expression * > args; 248 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) ); 243 249 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 244 250 } 245 251 Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 246 std::list< Expression *> args;247 args.push_back( maybe Build<Expression>(expr_node1) );248 args.push_back( maybe Build<Expression>(expr_node2) );252 std::list< Expression * > args; 253 args.push_back( maybeMoveBuild< Expression >(expr_node1) ); 254 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 249 255 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 250 256 } 251 257 Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 252 std::list< Expression *> args;253 args.push_back( new AddressExpr( maybe Build<Expression>(expr_node1) ) );254 args.push_back( maybe Build<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) ); 255 261 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 256 262 } 257 263 258 264 Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) { 259 return new ConditionalExpr( notZeroExpr( maybe Build<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) ); 260 266 } 261 267 262 268 Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 263 return new CommaExpr( maybe Build<Expression>(expr_node1), maybeBuild<Expression>(expr_node2) );269 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) ); 264 270 } 265 271 266 272 Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) { 267 return new AttrExpr( var, maybe Build<Expression>(expr_node) );273 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) ); 268 274 } 269 275 Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { 270 return new AttrExpr( var, decl_node->buildType() );276 return new AttrExpr( var, maybeMoveBuildType( decl_node ) ); 271 277 } 272 278 273 279 Expression *build_tuple( ExpressionNode * expr_node ) { 274 280 TupleExpr *ret = new TupleExpr(); 275 build List( expr_node, ret->get_exprs() );281 buildMoveList( expr_node, ret->get_exprs() ); 276 282 return ret; 277 283 } 278 284 279 285 Expression *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 ); 284 289 } 285 290 286 291 Expression *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 295 Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) { 296 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) ); 297 } 305 298 306 299 Expression *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 } 365 302 Expression *build_typevalue( DeclarationNode *decl ) { 366 return new TypeExpr( decl->buildType() ); 367 } 368 369 //############################################################################## 303 return new TypeExpr( maybeMoveBuildType( decl ) ); 304 } 370 305 371 306 Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) { 372 Declaration * newDecl = maybeBuild< Declaration>(decl_node); // compound literal type307 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type 373 308 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type 374 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybe Build<Initializer>(kids) );309 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) ); 375 310 // these types do not have associated type information 376 311 } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { 377 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybe Build<Initializer>(kids) );312 return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 378 313 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { 379 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybe Build<Initializer>(kids) );314 return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 380 315 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { 381 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybe Build<Initializer>(kids) );316 return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); 382 317 } else { 383 318 assert( false );
Note:
See TracChangeset
for help on using the changeset viewer.