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