Changes in src/ResolvExpr/Resolver.cc [94b4364:de62360d]
- File:
-
- 1 edited
-
src/ResolvExpr/Resolver.cc (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Resolver.cc
r94b4364 rde62360d 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:17:01 2015 11 // Last Modified By : Rob Schluntz12 // Last Modified On : Tue Jun 16 14:50:11201513 // Update Count : 15411 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 24 15:47:16 2015 13 // Update Count : 50 14 14 // 15 15 … … 38 38 virtual void visit( TypeDecl *typeDecl ); 39 39 40 virtual void visit( ArrayType * at );41 42 40 virtual void visit( ExprStmt *exprStmt ); 43 41 virtual void visit( IfStmt *ifStmt ); … … 47 45 virtual void visit( ChooseStmt *switchStmt ); 48 46 virtual void visit( CaseStmt *caseStmt ); 47 virtual void visit( BranchStmt *branchStmt ); 49 48 virtual void visit( ReturnStmt *returnStmt ); 50 49 … … 52 51 virtual void visit( ListInit *listInit ); 53 52 private: 54 typedef std::list< Initializer * >::iterator InitIterator;55 56 void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );57 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );58 59 53 std::list< Type * > functionReturn; 60 54 Type *initContext; … … 164 158 initContext = new_type; 165 159 SymTab::Indexer::visit( objectDecl ); 166 } 167 168 void Resolver::visit( ArrayType * at ) { 169 if ( at->get_dimension() ) { 170 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 171 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 172 Expression *newExpr = findSingleExpression( castExpr, *this ); 173 delete at->get_dimension(); 174 at->set_dimension( newExpr ); 175 } 176 Visitor::visit( at ); 177 } 178 160 161 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){ 162 if ( at->get_dimension() ) { 163 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 164 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 165 Expression *newExpr = findSingleExpression( castExpr, *this ); 166 delete at->get_dimension(); 167 at->set_dimension( newExpr ); 168 } 169 } 170 } 171 179 172 void Resolver::visit( TypeDecl *typeDecl ) { 180 173 if ( typeDecl->get_base() ) { … … 184 177 SymTab::Indexer::visit( typeDecl ); 185 178 } 186 179 187 180 void Resolver::visit( FunctionDecl *functionDecl ) { 188 181 #if 0 … … 270 263 } 271 264 265 void Resolver::visit( BranchStmt *branchStmt ) { 266 // must resolve the argument for a computed goto 267 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement 268 if ( NameExpr * arg = dynamic_cast< NameExpr * >( branchStmt->get_computedTarget() ) ) { 269 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder 270 PointerType pt( Type::Qualifiers(), v.clone() ); 271 CastExpr * castExpr = new CastExpr( arg, pt.clone() ); 272 Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression 273 branchStmt->set_target( newExpr ); 274 } // if 275 } // if 276 } 277 272 278 void Resolver::visit( ReturnStmt *returnStmt ) { 273 279 if ( returnStmt->get_expr() ) { … … 278 284 returnStmt->set_expr( newExpr ); 279 285 } // if 280 }281 282 template< typename T >283 bool isCharType( T t ) {284 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {285 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||286 bt->get_kind() == BasicType::UnsignedChar;287 }288 return false;289 286 } 290 287 … … 313 310 delete castExpr; 314 311 singleInit->set_value( newExpr ); 315 316 // check if initializing type is char[]317 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {318 if ( isCharType( at->get_base() ) ) {319 // check if the resolved type is char *320 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {321 if ( isCharType( pt->get_base() ) ) {322 // strip cast if we're initializing a char[] with a char *, e.g.323 // char x[] = "hello";324 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );325 singleInit->set_value( ce->get_arg() );326 ce->set_arg( NULL );327 delete ce;328 }329 }330 }331 }332 312 } // if 333 313 // singleInit->get_value()->accept( *this ); 334 314 } 335 315 336 void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { 337 DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); 338 assert( dt ); 339 initContext = dt->get_type(); 340 try { 341 if ( init == initEnd ) return; // stop when there are no more initializers 342 (*init)->accept( *this ); 343 ++init; // made it past an initializer 344 } catch( SemanticError & ) { 345 // need to delve deeper, if you can 346 if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { 347 resolveAggrInit( sit->get_baseStruct(), init, initEnd ); 348 } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { 349 resolveAggrInit( uit->get_baseUnion(), init, initEnd ); 350 } else { 351 // might need to rethink what is being thrown 352 throw; 353 } // if 354 } 355 } 356 357 void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { 358 if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { 359 // want to resolve each initializer to the members of the struct, 360 // but if there are more initializers than members we should stop 361 list< Declaration * >::iterator it = st->get_members().begin(); 362 for ( ; it != st->get_members().end(); ++it) { 363 resolveSingleAggrInit( *it, init, initEnd ); 364 } 365 } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { 366 // only resolve to the first member of a union 367 resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); 368 } // if 369 } 370 371 void Resolver::visit( ListInit * listInit ) { 372 InitIterator iter = listInit->begin_initializers(); 373 InitIterator end = listInit->end_initializers(); 374 375 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 376 // resolve each member to the base type of the array 377 for ( ; iter != end; ++iter ) { 378 initContext = at->get_base(); 379 (*iter)->accept( *this ); 380 } // for 381 } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { 382 resolveAggrInit( st->get_baseStruct(), iter, end ); 383 } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { 384 resolveAggrInit( st->get_baseUnion(), iter, end ); 385 } else { 386 // basic types are handled here 387 Visitor::visit( listInit ); 388 } 389 316 void Resolver::visit( ListInit *listInit ) { 317 Visitor::visit(listInit); 390 318 #if 0 391 319 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
Note:
See TracChangeset
for help on using the changeset viewer.