[a32b204] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // Resolver.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:17:01 2015 |
---|
[7f5566b] | 11 | // Last Modified By : Peter A. Buhr |
---|
[52f85e0] | 12 | // Last Modified On : Tue Feb 9 21:57:52 2016 |
---|
| 13 | // Update Count : 179 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include "Resolver.h" |
---|
| 17 | #include "AlternativeFinder.h" |
---|
| 18 | #include "Alternative.h" |
---|
| 19 | #include "RenameVars.h" |
---|
| 20 | #include "ResolveTypeof.h" |
---|
| 21 | #include "SynTree/Statement.h" |
---|
| 22 | #include "SynTree/Type.h" |
---|
| 23 | #include "SynTree/Expression.h" |
---|
| 24 | #include "SynTree/Initializer.h" |
---|
| 25 | #include "SymTab/Indexer.h" |
---|
[d3b7937] | 26 | #include "Common/utility.h" |
---|
[51b7345] | 27 | |
---|
[d9a0e76] | 28 | #include <iostream> |
---|
| 29 | using namespace std; |
---|
[51b7345] | 30 | |
---|
[d9a0e76] | 31 | namespace ResolvExpr { |
---|
[a32b204] | 32 | class Resolver : public SymTab::Indexer { |
---|
| 33 | public: |
---|
| 34 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} |
---|
[51b7345] | 35 | |
---|
[a32b204] | 36 | virtual void visit( FunctionDecl *functionDecl ); |
---|
| 37 | virtual void visit( ObjectDecl *functionDecl ); |
---|
| 38 | virtual void visit( TypeDecl *typeDecl ); |
---|
[94b4364] | 39 | |
---|
[bfbf97f] | 40 | virtual void visit( ArrayType * at ); |
---|
[d9a0e76] | 41 | |
---|
[a32b204] | 42 | virtual void visit( ExprStmt *exprStmt ); |
---|
[7f5566b] | 43 | virtual void visit( AsmExpr *asmExpr ); |
---|
| 44 | virtual void visit( AsmStmt *asmStmt ); |
---|
[a32b204] | 45 | virtual void visit( IfStmt *ifStmt ); |
---|
| 46 | virtual void visit( WhileStmt *whileStmt ); |
---|
| 47 | virtual void visit( ForStmt *forStmt ); |
---|
| 48 | virtual void visit( SwitchStmt *switchStmt ); |
---|
| 49 | virtual void visit( ChooseStmt *switchStmt ); |
---|
| 50 | virtual void visit( CaseStmt *caseStmt ); |
---|
[de62360d] | 51 | virtual void visit( BranchStmt *branchStmt ); |
---|
[a32b204] | 52 | virtual void visit( ReturnStmt *returnStmt ); |
---|
[d9a0e76] | 53 | |
---|
[a32b204] | 54 | virtual void visit( SingleInit *singleInit ); |
---|
| 55 | virtual void visit( ListInit *listInit ); |
---|
| 56 | private: |
---|
[94b4364] | 57 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
| 58 | |
---|
| 59 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); |
---|
| 60 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); |
---|
| 61 | |
---|
[a32b204] | 62 | std::list< Type * > functionReturn; |
---|
| 63 | Type *initContext; |
---|
| 64 | Type *switchType; |
---|
| 65 | }; |
---|
[d9a0e76] | 66 | |
---|
[a32b204] | 67 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
| 68 | Resolver resolver; |
---|
| 69 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 70 | #if 0 |
---|
[a32b204] | 71 | resolver.print( cerr ); |
---|
| 72 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 73 | (*i)->print( std::cerr ); |
---|
| 74 | (*i)->accept( resolver ); |
---|
| 75 | } // for |
---|
[d9a0e76] | 76 | #endif |
---|
| 77 | } |
---|
| 78 | |
---|
[a32b204] | 79 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
| 80 | TypeEnvironment env; |
---|
| 81 | return resolveInVoidContext( expr, indexer, env ); |
---|
[d9a0e76] | 82 | } |
---|
[a32b204] | 83 | |
---|
| 84 | namespace { |
---|
| 85 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
| 86 | expr->set_env( new TypeSubstitution ); |
---|
| 87 | env.makeSubstitution( *expr->get_env() ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 91 | global_renamer.reset(); |
---|
| 92 | TypeEnvironment env; |
---|
| 93 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 94 | finishExpr( newExpr, env ); |
---|
| 95 | return newExpr; |
---|
| 96 | } |
---|
[51b7345] | 97 | |
---|
[a32b204] | 98 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 99 | TypeEnvironment env; |
---|
| 100 | AlternativeFinder finder( indexer, env ); |
---|
| 101 | finder.find( untyped ); |
---|
[d9a0e76] | 102 | #if 0 |
---|
[a32b204] | 103 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 104 | std::cout << "untyped expr is "; |
---|
| 105 | untyped->print( std::cout ); |
---|
| 106 | std::cout << std::endl << "alternatives are:"; |
---|
| 107 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 108 | i->print( std::cout ); |
---|
| 109 | } // for |
---|
| 110 | } // if |
---|
[d9a0e76] | 111 | #endif |
---|
[a32b204] | 112 | assert( finder.get_alternatives().size() == 1 ); |
---|
| 113 | Alternative &choice = finder.get_alternatives().front(); |
---|
| 114 | Expression *newExpr = choice.expr->clone(); |
---|
| 115 | finishExpr( newExpr, choice.env ); |
---|
| 116 | return newExpr; |
---|
| 117 | } |
---|
[d9a0e76] | 118 | |
---|
[a32b204] | 119 | bool isIntegralType( Type *type ) { |
---|
| 120 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 121 | return true; |
---|
| 122 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
| 123 | return bt->isInteger(); |
---|
| 124 | } else { |
---|
| 125 | return false; |
---|
| 126 | } // if |
---|
| 127 | } |
---|
[51b7345] | 128 | |
---|
[a32b204] | 129 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 130 | TypeEnvironment env; |
---|
| 131 | AlternativeFinder finder( indexer, env ); |
---|
| 132 | finder.find( untyped ); |
---|
[d9a0e76] | 133 | #if 0 |
---|
[a32b204] | 134 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 135 | std::cout << "untyped expr is "; |
---|
| 136 | untyped->print( std::cout ); |
---|
| 137 | std::cout << std::endl << "alternatives are:"; |
---|
| 138 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 139 | i->print( std::cout ); |
---|
| 140 | } // for |
---|
| 141 | } // if |
---|
[d9a0e76] | 142 | #endif |
---|
[a32b204] | 143 | Expression *newExpr = 0; |
---|
| 144 | const TypeEnvironment *newEnv = 0; |
---|
| 145 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 146 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { |
---|
| 147 | if ( newExpr ) { |
---|
| 148 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
| 149 | } else { |
---|
| 150 | newExpr = i->expr->clone(); |
---|
| 151 | newEnv = &i->env; |
---|
| 152 | } // if |
---|
| 153 | } // if |
---|
| 154 | } // for |
---|
| 155 | if ( ! newExpr ) { |
---|
| 156 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
| 157 | } // if |
---|
| 158 | finishExpr( newExpr, *newEnv ); |
---|
| 159 | return newExpr; |
---|
| 160 | } |
---|
[51b7345] | 161 | |
---|
[a32b204] | 162 | } |
---|
[51b7345] | 163 | |
---|
[a32b204] | 164 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
| 165 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
| 166 | objectDecl->set_type( new_type ); |
---|
| 167 | initContext = new_type; |
---|
| 168 | SymTab::Indexer::visit( objectDecl ); |
---|
[bfbf97f] | 169 | } |
---|
| 170 | |
---|
| 171 | void Resolver::visit( ArrayType * at ) { |
---|
| 172 | if ( at->get_dimension() ) { |
---|
| 173 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
| 174 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); |
---|
| 175 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 176 | delete at->get_dimension(); |
---|
| 177 | at->set_dimension( newExpr ); |
---|
[d1d17f5] | 178 | } |
---|
[bfbf97f] | 179 | Visitor::visit( at ); |
---|
[a32b204] | 180 | } |
---|
[94b4364] | 181 | |
---|
[a32b204] | 182 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 183 | if ( typeDecl->get_base() ) { |
---|
| 184 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 185 | typeDecl->set_base( new_type ); |
---|
| 186 | } // if |
---|
| 187 | SymTab::Indexer::visit( typeDecl ); |
---|
| 188 | } |
---|
[94b4364] | 189 | |
---|
[a32b204] | 190 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
[d9a0e76] | 191 | #if 0 |
---|
[a32b204] | 192 | std::cout << "resolver visiting functiondecl "; |
---|
| 193 | functionDecl->print( std::cout ); |
---|
| 194 | std::cout << std::endl; |
---|
[d9a0e76] | 195 | #endif |
---|
[a32b204] | 196 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 197 | functionDecl->set_type( new_type ); |
---|
| 198 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
| 199 | functionReturn.clear(); |
---|
| 200 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
| 201 | functionReturn.push_back( (*i)->get_type() ); |
---|
| 202 | } // for |
---|
| 203 | SymTab::Indexer::visit( functionDecl ); |
---|
| 204 | functionReturn = oldFunctionReturn; |
---|
| 205 | } |
---|
[51b7345] | 206 | |
---|
[a32b204] | 207 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
| 208 | if ( exprStmt->get_expr() ) { |
---|
| 209 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
| 210 | delete exprStmt->get_expr(); |
---|
| 211 | exprStmt->set_expr( newExpr ); |
---|
| 212 | } // if |
---|
| 213 | } |
---|
[51b7345] | 214 | |
---|
[7f5566b] | 215 | void Resolver::visit( AsmExpr *asmExpr ) { |
---|
| 216 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); |
---|
| 217 | delete asmExpr->get_operand(); |
---|
| 218 | asmExpr->set_operand( newExpr ); |
---|
| 219 | if ( asmExpr->get_inout() ) { |
---|
| 220 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); |
---|
| 221 | delete asmExpr->get_inout(); |
---|
| 222 | asmExpr->set_inout( newExpr ); |
---|
| 223 | } // if |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | void Resolver::visit( AsmStmt *asmStmt ) { |
---|
| 227 | acceptAll( asmStmt->get_input(), *this); |
---|
| 228 | acceptAll( asmStmt->get_output(), *this); |
---|
| 229 | } |
---|
| 230 | |
---|
[a32b204] | 231 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
| 232 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
| 233 | delete ifStmt->get_condition(); |
---|
| 234 | ifStmt->set_condition( newExpr ); |
---|
| 235 | Visitor::visit( ifStmt ); |
---|
| 236 | } |
---|
[51b7345] | 237 | |
---|
[a32b204] | 238 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
| 239 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
| 240 | delete whileStmt->get_condition(); |
---|
| 241 | whileStmt->set_condition( newExpr ); |
---|
| 242 | Visitor::visit( whileStmt ); |
---|
| 243 | } |
---|
[51b7345] | 244 | |
---|
[a32b204] | 245 | void Resolver::visit( ForStmt *forStmt ) { |
---|
[145f1fc] | 246 | SymTab::Indexer::visit( forStmt ); |
---|
| 247 | |
---|
[a32b204] | 248 | if ( forStmt->get_condition() ) { |
---|
[145f1fc] | 249 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
[a32b204] | 250 | delete forStmt->get_condition(); |
---|
| 251 | forStmt->set_condition( newExpr ); |
---|
| 252 | } // if |
---|
[145f1fc] | 253 | |
---|
[a32b204] | 254 | if ( forStmt->get_increment() ) { |
---|
[145f1fc] | 255 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
[a32b204] | 256 | delete forStmt->get_increment(); |
---|
| 257 | forStmt->set_increment( newExpr ); |
---|
| 258 | } // if |
---|
| 259 | } |
---|
[51b7345] | 260 | |
---|
[a32b204] | 261 | template< typename SwitchClass > |
---|
| 262 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
| 263 | Expression *newExpr; |
---|
| 264 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
| 265 | delete switchStmt->get_condition(); |
---|
| 266 | switchStmt->set_condition( newExpr ); |
---|
[51b7345] | 267 | |
---|
[a32b204] | 268 | visitor.Visitor::visit( switchStmt ); |
---|
| 269 | } |
---|
[51b7345] | 270 | |
---|
[a32b204] | 271 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
| 272 | handleSwitchStmt( switchStmt, *this ); |
---|
| 273 | } |
---|
[51b7345] | 274 | |
---|
[a32b204] | 275 | void Resolver::visit( ChooseStmt *switchStmt ) { |
---|
| 276 | handleSwitchStmt( switchStmt, *this ); |
---|
| 277 | } |
---|
[51b7345] | 278 | |
---|
[a32b204] | 279 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
| 280 | Visitor::visit( caseStmt ); |
---|
| 281 | } |
---|
[51b7345] | 282 | |
---|
[de62360d] | 283 | void Resolver::visit( BranchStmt *branchStmt ) { |
---|
| 284 | // must resolve the argument for a computed goto |
---|
| 285 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
[2871210] | 286 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
[de62360d] | 287 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder |
---|
| 288 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
| 289 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); |
---|
| 290 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression |
---|
| 291 | branchStmt->set_target( newExpr ); |
---|
| 292 | } // if |
---|
| 293 | } // if |
---|
| 294 | } |
---|
| 295 | |
---|
[a32b204] | 296 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
| 297 | if ( returnStmt->get_expr() ) { |
---|
| 298 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
| 299 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
| 300 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 301 | delete castExpr; |
---|
| 302 | returnStmt->set_expr( newExpr ); |
---|
| 303 | } // if |
---|
| 304 | } |
---|
[51b7345] | 305 | |
---|
[b5c5684] | 306 | template< typename T > |
---|
| 307 | bool isCharType( T t ) { |
---|
| 308 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
| 309 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
| 310 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 311 | } |
---|
| 312 | return false; |
---|
| 313 | } |
---|
| 314 | |
---|
[a32b204] | 315 | void Resolver::visit( SingleInit *singleInit ) { |
---|
| 316 | if ( singleInit->get_value() ) { |
---|
[bdd516a] | 317 | #if 0 |
---|
[a32b204] | 318 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
| 319 | string n = ne->get_name(); |
---|
| 320 | if (n == "0") { |
---|
| 321 | initContext = new BasicType(Type::Qualifiers(), |
---|
| 322 | BasicType::SignedInt); |
---|
| 323 | } else { |
---|
[52f85e0] | 324 | DeclarationWithType * decl = lookupId( n ); |
---|
[a32b204] | 325 | initContext = decl->get_type(); |
---|
| 326 | } |
---|
| 327 | } else if (ConstantExpr * e = |
---|
| 328 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
| 329 | Constant *c = e->get_constant(); |
---|
| 330 | initContext = c->get_type(); |
---|
| 331 | } else { |
---|
| 332 | assert(0); |
---|
| 333 | } |
---|
[bdd516a] | 334 | #endif |
---|
[a32b204] | 335 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
| 336 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 337 | delete castExpr; |
---|
| 338 | singleInit->set_value( newExpr ); |
---|
[b5c5684] | 339 | |
---|
| 340 | // check if initializing type is char[] |
---|
| 341 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 342 | if ( isCharType( at->get_base() ) ) { |
---|
| 343 | // check if the resolved type is char * |
---|
| 344 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { |
---|
| 345 | if ( isCharType( pt->get_base() ) ) { |
---|
[52f85e0] | 346 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
[b5c5684] | 347 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); |
---|
| 348 | singleInit->set_value( ce->get_arg() ); |
---|
| 349 | ce->set_arg( NULL ); |
---|
| 350 | delete ce; |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | } |
---|
[a32b204] | 355 | } // if |
---|
[6c3744e] | 356 | // singleInit->get_value()->accept( *this ); |
---|
[a32b204] | 357 | } |
---|
[51b7345] | 358 | |
---|
[94b4364] | 359 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { |
---|
| 360 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); |
---|
| 361 | assert( dt ); |
---|
| 362 | initContext = dt->get_type(); |
---|
| 363 | try { |
---|
| 364 | if ( init == initEnd ) return; // stop when there are no more initializers |
---|
| 365 | (*init)->accept( *this ); |
---|
| 366 | ++init; // made it past an initializer |
---|
| 367 | } catch( SemanticError & ) { |
---|
| 368 | // need to delve deeper, if you can |
---|
| 369 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { |
---|
| 370 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); |
---|
| 371 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { |
---|
| 372 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); |
---|
| 373 | } else { |
---|
[1869adf] | 374 | // member is not an aggregate type, so can't go any deeper |
---|
| 375 | |
---|
[94b4364] | 376 | // might need to rethink what is being thrown |
---|
| 377 | throw; |
---|
| 378 | } // if |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { |
---|
| 383 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { |
---|
| 384 | // want to resolve each initializer to the members of the struct, |
---|
| 385 | // but if there are more initializers than members we should stop |
---|
| 386 | list< Declaration * >::iterator it = st->get_members().begin(); |
---|
| 387 | for ( ; it != st->get_members().end(); ++it) { |
---|
| 388 | resolveSingleAggrInit( *it, init, initEnd ); |
---|
| 389 | } |
---|
| 390 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { |
---|
| 391 | // only resolve to the first member of a union |
---|
| 392 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); |
---|
| 393 | } // if |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | void Resolver::visit( ListInit * listInit ) { |
---|
| 397 | InitIterator iter = listInit->begin_initializers(); |
---|
| 398 | InitIterator end = listInit->end_initializers(); |
---|
| 399 | |
---|
[b5c5684] | 400 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
[94b4364] | 401 | // resolve each member to the base type of the array |
---|
| 402 | for ( ; iter != end; ++iter ) { |
---|
[b5c5684] | 403 | initContext = at->get_base(); |
---|
| 404 | (*iter)->accept( *this ); |
---|
| 405 | } // for |
---|
[94b4364] | 406 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { |
---|
| 407 | resolveAggrInit( st->get_baseStruct(), iter, end ); |
---|
[b5c5684] | 408 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { |
---|
[94b4364] | 409 | resolveAggrInit( st->get_baseUnion(), iter, end ); |
---|
[b5c5684] | 410 | } else { |
---|
| 411 | // basic types are handled here |
---|
| 412 | Visitor::visit( listInit ); |
---|
| 413 | } |
---|
| 414 | |
---|
[bdd516a] | 415 | #if 0 |
---|
[a32b204] | 416 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
| 417 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
| 418 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
| 419 | initContext = at->get_base(); |
---|
| 420 | (*iter)->accept( *this ); |
---|
| 421 | } // for |
---|
| 422 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
| 423 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
| 424 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
| 425 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
| 426 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
| 427 | if ( (*iter2)->get_designators().empty() ) { |
---|
| 428 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
| 429 | initContext = dt->get_type(); |
---|
| 430 | (*iter2)->accept( *this ); |
---|
| 431 | ++iter1; |
---|
| 432 | } else { |
---|
| 433 | StructDecl *st = baseStruct; |
---|
| 434 | iter1 = st->get_members().begin(); |
---|
| 435 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
| 436 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
| 437 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
| 438 | assert( key ); |
---|
| 439 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
| 440 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
| 441 | (*iter1)->print( cout ); |
---|
| 442 | cout << key->get_name() << endl; |
---|
| 443 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 444 | assert( fred ); |
---|
| 445 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
| 446 | assert( mary ); |
---|
| 447 | st = mary->get_baseStruct(); |
---|
| 448 | iter1 = st->get_members().begin(); |
---|
| 449 | break; |
---|
| 450 | } // if |
---|
| 451 | } // for |
---|
| 452 | } // for |
---|
| 453 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 454 | assert( fred ); |
---|
| 455 | initContext = fred->get_type(); |
---|
| 456 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 457 | } // if |
---|
| 458 | } // for |
---|
| 459 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
| 460 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
| 461 | initContext = dt->get_type(); |
---|
| 462 | (*listInit->begin_initializers())->accept( *this ); |
---|
[2c2242c] | 463 | } // if |
---|
[bdd516a] | 464 | #endif |
---|
[a32b204] | 465 | } |
---|
[51b7345] | 466 | } // namespace ResolvExpr |
---|
[a32b204] | 467 | |
---|
| 468 | // Local Variables: // |
---|
| 469 | // tab-width: 4 // |
---|
| 470 | // mode: c++ // |
---|
| 471 | // compile-command: "make install" // |
---|
| 472 | // End: // |
---|