| [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 | 
|---|
| [2871210] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Fri Jul  3 16:18:20 2015 | 
|---|
|  | 13 | // Update Count     : 159 | 
|---|
| [a32b204] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [51b73452] | 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; | 
|---|
| [51b73452] | 30 |  | 
|---|
| [d9a0e76] | 31 | namespace ResolvExpr { | 
|---|
| [a32b204] | 32 | class Resolver : public SymTab::Indexer { | 
|---|
|  | 33 | public: | 
|---|
|  | 34 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 159 |  | 
|---|
| [a32b204] | 160 | } | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 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 | } | 
|---|
| [51b73452] | 226 |  | 
|---|
| [a32b204] | 227 | void Resolver::visit( ForStmt *forStmt ) { | 
|---|
|  | 228 | // SymTab::Indexer::visit( forStmt ); | 
|---|
|  | 229 | Expression *newExpr; | 
|---|
|  | 230 | // for statements introduce a level of scope | 
|---|
|  | 231 | enterScope(); | 
|---|
|  | 232 | maybeAccept( forStmt->get_initialization(), *this ); | 
|---|
|  | 233 | if ( forStmt->get_condition() ) { | 
|---|
|  | 234 | newExpr = findSingleExpression( forStmt->get_condition(), *this ); | 
|---|
|  | 235 | delete forStmt->get_condition(); | 
|---|
|  | 236 | forStmt->set_condition( newExpr ); | 
|---|
|  | 237 | } // if | 
|---|
| [51b73452] | 238 |  | 
|---|
| [a32b204] | 239 | if ( forStmt->get_increment() ) { | 
|---|
|  | 240 | newExpr = findVoidExpression( forStmt->get_increment(), *this ); | 
|---|
|  | 241 | delete forStmt->get_increment(); | 
|---|
|  | 242 | forStmt->set_increment( newExpr ); | 
|---|
|  | 243 | } // if | 
|---|
| [b1a6d6b] | 244 |  | 
|---|
| [a32b204] | 245 | maybeAccept( forStmt->get_condition(), *this ); | 
|---|
|  | 246 | maybeAccept( forStmt->get_increment(), *this ); | 
|---|
|  | 247 | maybeAccept( forStmt->get_body(), *this ); | 
|---|
|  | 248 | leaveScope(); | 
|---|
|  | 249 | } | 
|---|
| [51b73452] | 250 |  | 
|---|
| [a32b204] | 251 | template< typename SwitchClass > | 
|---|
|  | 252 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { | 
|---|
|  | 253 | Expression *newExpr; | 
|---|
|  | 254 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); | 
|---|
|  | 255 | delete switchStmt->get_condition(); | 
|---|
|  | 256 | switchStmt->set_condition( newExpr ); | 
|---|
| [51b73452] | 257 |  | 
|---|
| [a32b204] | 258 | visitor.Visitor::visit( switchStmt ); | 
|---|
|  | 259 | } | 
|---|
| [51b73452] | 260 |  | 
|---|
| [a32b204] | 261 | void Resolver::visit( SwitchStmt *switchStmt ) { | 
|---|
|  | 262 | handleSwitchStmt( switchStmt, *this ); | 
|---|
|  | 263 | } | 
|---|
| [51b73452] | 264 |  | 
|---|
| [a32b204] | 265 | void Resolver::visit( ChooseStmt *switchStmt ) { | 
|---|
|  | 266 | handleSwitchStmt( switchStmt, *this ); | 
|---|
|  | 267 | } | 
|---|
| [51b73452] | 268 |  | 
|---|
| [a32b204] | 269 | void Resolver::visit( CaseStmt *caseStmt ) { | 
|---|
|  | 270 | Visitor::visit( caseStmt ); | 
|---|
|  | 271 | } | 
|---|
| [51b73452] | 272 |  | 
|---|
| [de62360d] | 273 | void Resolver::visit( BranchStmt *branchStmt ) { | 
|---|
|  | 274 | // must resolve the argument for a computed goto | 
|---|
|  | 275 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement | 
|---|
| [2871210] | 276 | if ( Expression * arg = branchStmt->get_computedTarget() ) { | 
|---|
| [de62360d] | 277 | VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder | 
|---|
|  | 278 | PointerType pt( Type::Qualifiers(), v.clone() ); | 
|---|
|  | 279 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); | 
|---|
|  | 280 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression | 
|---|
|  | 281 | branchStmt->set_target( newExpr ); | 
|---|
|  | 282 | } // if | 
|---|
|  | 283 | } // if | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
| [a32b204] | 286 | void Resolver::visit( ReturnStmt *returnStmt ) { | 
|---|
|  | 287 | if ( returnStmt->get_expr() ) { | 
|---|
|  | 288 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); | 
|---|
|  | 289 | cloneAll( functionReturn, castExpr->get_results() ); | 
|---|
|  | 290 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 291 | delete castExpr; | 
|---|
|  | 292 | returnStmt->set_expr( newExpr ); | 
|---|
|  | 293 | } // if | 
|---|
|  | 294 | } | 
|---|
| [51b73452] | 295 |  | 
|---|
| [b5c5684] | 296 | template< typename T > | 
|---|
|  | 297 | bool isCharType( T t ) { | 
|---|
|  | 298 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { | 
|---|
|  | 299 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || | 
|---|
|  | 300 | bt->get_kind() == BasicType::UnsignedChar; | 
|---|
|  | 301 | } | 
|---|
|  | 302 | return false; | 
|---|
|  | 303 | } | 
|---|
|  | 304 |  | 
|---|
| [a32b204] | 305 | void Resolver::visit( SingleInit *singleInit ) { | 
|---|
|  | 306 | if ( singleInit->get_value() ) { | 
|---|
| [bdd516a] | 307 | #if 0 | 
|---|
| [a32b204] | 308 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { | 
|---|
|  | 309 | string n = ne->get_name(); | 
|---|
|  | 310 | if (n == "0") { | 
|---|
|  | 311 | initContext = new BasicType(Type::Qualifiers(), | 
|---|
|  | 312 | BasicType::SignedInt); | 
|---|
|  | 313 | } else { | 
|---|
|  | 314 | DeclarationWithType * decl = lookupId(n); | 
|---|
|  | 315 | initContext = decl->get_type(); | 
|---|
|  | 316 | } | 
|---|
|  | 317 | } else if (ConstantExpr * e = | 
|---|
|  | 318 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { | 
|---|
|  | 319 | Constant *c = e->get_constant(); | 
|---|
|  | 320 | initContext = c->get_type(); | 
|---|
|  | 321 | } else { | 
|---|
|  | 322 | assert(0); | 
|---|
|  | 323 | } | 
|---|
| [bdd516a] | 324 | #endif | 
|---|
| [a32b204] | 325 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); | 
|---|
|  | 326 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 327 | delete castExpr; | 
|---|
|  | 328 | singleInit->set_value( newExpr ); | 
|---|
| [b5c5684] | 329 |  | 
|---|
|  | 330 | // check if initializing type is char[] | 
|---|
|  | 331 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
|  | 332 | if ( isCharType( at->get_base() ) ) { | 
|---|
|  | 333 | // check if the resolved type is char * | 
|---|
|  | 334 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { | 
|---|
|  | 335 | if ( isCharType( pt->get_base() ) ) { | 
|---|
|  | 336 | // strip cast if we're initializing a char[] with a char *, e.g. | 
|---|
|  | 337 | // char x[] = "hello"; | 
|---|
|  | 338 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); | 
|---|
|  | 339 | singleInit->set_value( ce->get_arg() ); | 
|---|
|  | 340 | ce->set_arg( NULL ); | 
|---|
|  | 341 | delete ce; | 
|---|
|  | 342 | } | 
|---|
|  | 343 | } | 
|---|
|  | 344 | } | 
|---|
|  | 345 | } | 
|---|
| [a32b204] | 346 | } // if | 
|---|
| [6c3744e] | 347 | //      singleInit->get_value()->accept( *this ); | 
|---|
| [a32b204] | 348 | } | 
|---|
| [51b73452] | 349 |  | 
|---|
| [94b4364] | 350 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { | 
|---|
|  | 351 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); | 
|---|
|  | 352 | assert( dt ); | 
|---|
|  | 353 | initContext = dt->get_type(); | 
|---|
|  | 354 | try { | 
|---|
|  | 355 | if ( init == initEnd ) return; // stop when there are no more initializers | 
|---|
|  | 356 | (*init)->accept( *this ); | 
|---|
|  | 357 | ++init; // made it past an initializer | 
|---|
|  | 358 | } catch( SemanticError & ) { | 
|---|
|  | 359 | // need to delve deeper, if you can | 
|---|
|  | 360 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { | 
|---|
|  | 361 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); | 
|---|
|  | 362 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { | 
|---|
|  | 363 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); | 
|---|
|  | 364 | } else { | 
|---|
| [1869adf] | 365 | // member is not an aggregate type, so can't go any deeper | 
|---|
|  | 366 |  | 
|---|
| [94b4364] | 367 | // might need to rethink what is being thrown | 
|---|
|  | 368 | throw; | 
|---|
|  | 369 | } // if | 
|---|
|  | 370 | } | 
|---|
|  | 371 | } | 
|---|
|  | 372 |  | 
|---|
|  | 373 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { | 
|---|
|  | 374 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { | 
|---|
|  | 375 | // want to resolve each initializer to the members of the struct, | 
|---|
|  | 376 | // but if there are more initializers than members we should stop | 
|---|
|  | 377 | list< Declaration * >::iterator it = st->get_members().begin(); | 
|---|
|  | 378 | for ( ; it != st->get_members().end(); ++it) { | 
|---|
|  | 379 | resolveSingleAggrInit( *it, init, initEnd ); | 
|---|
|  | 380 | } | 
|---|
|  | 381 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { | 
|---|
|  | 382 | // only resolve to the first member of a union | 
|---|
|  | 383 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); | 
|---|
|  | 384 | } // if | 
|---|
|  | 385 | } | 
|---|
|  | 386 |  | 
|---|
|  | 387 | void Resolver::visit( ListInit * listInit ) { | 
|---|
|  | 388 | InitIterator iter = listInit->begin_initializers(); | 
|---|
|  | 389 | InitIterator end = listInit->end_initializers(); | 
|---|
|  | 390 |  | 
|---|
| [b5c5684] | 391 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
| [94b4364] | 392 | // resolve each member to the base type of the array | 
|---|
|  | 393 | for ( ; iter != end; ++iter ) { | 
|---|
| [b5c5684] | 394 | initContext = at->get_base(); | 
|---|
|  | 395 | (*iter)->accept( *this ); | 
|---|
|  | 396 | } // for | 
|---|
| [94b4364] | 397 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { | 
|---|
|  | 398 | resolveAggrInit( st->get_baseStruct(), iter, end ); | 
|---|
| [b5c5684] | 399 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { | 
|---|
| [94b4364] | 400 | resolveAggrInit( st->get_baseUnion(), iter, end ); | 
|---|
| [b5c5684] | 401 | } else { | 
|---|
|  | 402 | // basic types are handled here | 
|---|
|  | 403 | Visitor::visit( listInit ); | 
|---|
|  | 404 | } | 
|---|
|  | 405 |  | 
|---|
| [bdd516a] | 406 | #if 0 | 
|---|
| [a32b204] | 407 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { | 
|---|
|  | 408 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); | 
|---|
|  | 409 | for ( ; iter != listInit->end_initializers(); ++iter ) { | 
|---|
|  | 410 | initContext = at->get_base(); | 
|---|
|  | 411 | (*iter)->accept( *this ); | 
|---|
|  | 412 | } // for | 
|---|
|  | 413 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { | 
|---|
|  | 414 | StructDecl *baseStruct = st->get_baseStruct(); | 
|---|
|  | 415 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); | 
|---|
|  | 416 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); | 
|---|
|  | 417 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { | 
|---|
|  | 418 | if ( (*iter2)->get_designators().empty() ) { | 
|---|
|  | 419 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); | 
|---|
|  | 420 | initContext = dt->get_type(); | 
|---|
|  | 421 | (*iter2)->accept( *this ); | 
|---|
|  | 422 | ++iter1; | 
|---|
|  | 423 | } else { | 
|---|
|  | 424 | StructDecl *st = baseStruct; | 
|---|
|  | 425 | iter1 = st->get_members().begin(); | 
|---|
|  | 426 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); | 
|---|
|  | 427 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { | 
|---|
|  | 428 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); | 
|---|
|  | 429 | assert( key ); | 
|---|
|  | 430 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { | 
|---|
|  | 431 | if ( key->get_name() == (*iter1)->get_name() ) { | 
|---|
|  | 432 | (*iter1)->print( cout ); | 
|---|
|  | 433 | cout << key->get_name() << endl; | 
|---|
|  | 434 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
|  | 435 | assert( fred ); | 
|---|
|  | 436 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); | 
|---|
|  | 437 | assert( mary ); | 
|---|
|  | 438 | st = mary->get_baseStruct(); | 
|---|
|  | 439 | iter1 = st->get_members().begin(); | 
|---|
|  | 440 | break; | 
|---|
|  | 441 | } // if | 
|---|
|  | 442 | }  // for | 
|---|
|  | 443 | } // for | 
|---|
|  | 444 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
|  | 445 | assert( fred ); | 
|---|
|  | 446 | initContext = fred->get_type(); | 
|---|
|  | 447 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
|  | 448 | } // if | 
|---|
|  | 449 | } // for | 
|---|
|  | 450 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { | 
|---|
|  | 451 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); | 
|---|
|  | 452 | initContext = dt->get_type(); | 
|---|
|  | 453 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
| [2c2242c] | 454 | } // if | 
|---|
| [bdd516a] | 455 | #endif | 
|---|
| [a32b204] | 456 | } | 
|---|
| [51b73452] | 457 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 458 |  | 
|---|
|  | 459 | // Local Variables: // | 
|---|
|  | 460 | // tab-width: 4 // | 
|---|
|  | 461 | // mode: c++ // | 
|---|
|  | 462 | // compile-command: "make install" // | 
|---|
|  | 463 | // End: // | 
|---|