[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 | // |
---|
[71f4e4f] | 7 | // Resolver.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:17:01 2015 |
---|
[4e06c1e] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue Jul 12 17:45:42 2016 |
---|
| 13 | // Update Count : 204 |
---|
[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" |
---|
[5f98ce5] | 26 | #include "SymTab/Autogen.h" |
---|
[d3b7937] | 27 | #include "Common/utility.h" |
---|
[7b3f66b] | 28 | #include "InitTweak/InitTweak.h" |
---|
[51b7345] | 29 | |
---|
[d9a0e76] | 30 | #include <iostream> |
---|
| 31 | using namespace std; |
---|
[51b7345] | 32 | |
---|
[d9a0e76] | 33 | namespace ResolvExpr { |
---|
[a32b204] | 34 | class Resolver : public SymTab::Indexer { |
---|
| 35 | public: |
---|
| 36 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} |
---|
[71f4e4f] | 37 | |
---|
[a32b204] | 38 | virtual void visit( FunctionDecl *functionDecl ); |
---|
| 39 | virtual void visit( ObjectDecl *functionDecl ); |
---|
| 40 | virtual void visit( TypeDecl *typeDecl ); |
---|
[a436947] | 41 | virtual void visit( EnumDecl * enumDecl ); |
---|
[94b4364] | 42 | |
---|
[bfbf97f] | 43 | virtual void visit( ArrayType * at ); |
---|
[40e636a] | 44 | virtual void visit( PointerType * at ); |
---|
[d9a0e76] | 45 | |
---|
[a32b204] | 46 | virtual void visit( ExprStmt *exprStmt ); |
---|
[7f5566b] | 47 | virtual void visit( AsmExpr *asmExpr ); |
---|
| 48 | virtual void visit( AsmStmt *asmStmt ); |
---|
[a32b204] | 49 | virtual void visit( IfStmt *ifStmt ); |
---|
| 50 | virtual void visit( WhileStmt *whileStmt ); |
---|
| 51 | virtual void visit( ForStmt *forStmt ); |
---|
| 52 | virtual void visit( SwitchStmt *switchStmt ); |
---|
| 53 | virtual void visit( CaseStmt *caseStmt ); |
---|
[de62360d] | 54 | virtual void visit( BranchStmt *branchStmt ); |
---|
[a32b204] | 55 | virtual void visit( ReturnStmt *returnStmt ); |
---|
[d9a0e76] | 56 | |
---|
[a32b204] | 57 | virtual void visit( SingleInit *singleInit ); |
---|
| 58 | virtual void visit( ListInit *listInit ); |
---|
[71f4e4f] | 59 | virtual void visit( ConstructorInit *ctorInit ); |
---|
[a32b204] | 60 | private: |
---|
[94b4364] | 61 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
| 62 | |
---|
[40e636a] | 63 | template< typename PtrType > |
---|
| 64 | void handlePtrType( PtrType * type ); |
---|
| 65 | |
---|
[94b4364] | 66 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); |
---|
| 67 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); |
---|
[f1e012b] | 68 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
[a32b204] | 69 | std::list< Type * > functionReturn; |
---|
| 70 | Type *initContext; |
---|
| 71 | Type *switchType; |
---|
[a436947] | 72 | bool inEnumDecl = false; |
---|
[a32b204] | 73 | }; |
---|
[d9a0e76] | 74 | |
---|
[a32b204] | 75 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
| 76 | Resolver resolver; |
---|
| 77 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 78 | #if 0 |
---|
[a32b204] | 79 | resolver.print( cerr ); |
---|
| 80 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 81 | (*i)->print( std::cerr ); |
---|
| 82 | (*i)->accept( resolver ); |
---|
| 83 | } // for |
---|
[d9a0e76] | 84 | #endif |
---|
| 85 | } |
---|
| 86 | |
---|
[a32b204] | 87 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
| 88 | TypeEnvironment env; |
---|
| 89 | return resolveInVoidContext( expr, indexer, env ); |
---|
[d9a0e76] | 90 | } |
---|
[a32b204] | 91 | |
---|
[db4ecc5] | 92 | |
---|
[a32b204] | 93 | namespace { |
---|
| 94 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
| 95 | expr->set_env( new TypeSubstitution ); |
---|
| 96 | env.makeSubstitution( *expr->get_env() ); |
---|
| 97 | } |
---|
[db4ecc5] | 98 | } // namespace |
---|
[a32b204] | 99 | |
---|
[db4ecc5] | 100 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 101 | global_renamer.reset(); |
---|
| 102 | TypeEnvironment env; |
---|
| 103 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 104 | finishExpr( newExpr, env ); |
---|
| 105 | return newExpr; |
---|
| 106 | } |
---|
[71f4e4f] | 107 | |
---|
[db4ecc5] | 108 | namespace { |
---|
[a32b204] | 109 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 110 | TypeEnvironment env; |
---|
| 111 | AlternativeFinder finder( indexer, env ); |
---|
| 112 | finder.find( untyped ); |
---|
[d9a0e76] | 113 | #if 0 |
---|
[a32b204] | 114 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 115 | std::cout << "untyped expr is "; |
---|
| 116 | untyped->print( std::cout ); |
---|
| 117 | std::cout << std::endl << "alternatives are:"; |
---|
| 118 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 119 | i->print( std::cout ); |
---|
| 120 | } // for |
---|
| 121 | } // if |
---|
[d9a0e76] | 122 | #endif |
---|
[a32b204] | 123 | assert( finder.get_alternatives().size() == 1 ); |
---|
| 124 | Alternative &choice = finder.get_alternatives().front(); |
---|
| 125 | Expression *newExpr = choice.expr->clone(); |
---|
| 126 | finishExpr( newExpr, choice.env ); |
---|
| 127 | return newExpr; |
---|
| 128 | } |
---|
[d9a0e76] | 129 | |
---|
[a32b204] | 130 | bool isIntegralType( Type *type ) { |
---|
| 131 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 132 | return true; |
---|
| 133 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
| 134 | return bt->isInteger(); |
---|
| 135 | } else { |
---|
| 136 | return false; |
---|
| 137 | } // if |
---|
| 138 | } |
---|
[71f4e4f] | 139 | |
---|
[a32b204] | 140 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 141 | TypeEnvironment env; |
---|
| 142 | AlternativeFinder finder( indexer, env ); |
---|
| 143 | finder.find( untyped ); |
---|
[d9a0e76] | 144 | #if 0 |
---|
[a32b204] | 145 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 146 | std::cout << "untyped expr is "; |
---|
| 147 | untyped->print( std::cout ); |
---|
| 148 | std::cout << std::endl << "alternatives are:"; |
---|
| 149 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 150 | i->print( std::cout ); |
---|
| 151 | } // for |
---|
| 152 | } // if |
---|
[d9a0e76] | 153 | #endif |
---|
[a32b204] | 154 | Expression *newExpr = 0; |
---|
| 155 | const TypeEnvironment *newEnv = 0; |
---|
| 156 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 157 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { |
---|
| 158 | if ( newExpr ) { |
---|
| 159 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
| 160 | } else { |
---|
| 161 | newExpr = i->expr->clone(); |
---|
| 162 | newEnv = &i->env; |
---|
| 163 | } // if |
---|
| 164 | } // if |
---|
| 165 | } // for |
---|
| 166 | if ( ! newExpr ) { |
---|
| 167 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
| 168 | } // if |
---|
| 169 | finishExpr( newExpr, *newEnv ); |
---|
| 170 | return newExpr; |
---|
| 171 | } |
---|
[71f4e4f] | 172 | |
---|
[a32b204] | 173 | } |
---|
[71f4e4f] | 174 | |
---|
[a32b204] | 175 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
| 176 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
| 177 | objectDecl->set_type( new_type ); |
---|
[3cfe27f] | 178 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable |
---|
| 179 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes |
---|
| 180 | // initContext because of a function type can contain object declarations in the return and parameter types. So |
---|
| 181 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting |
---|
| 182 | // the RHS. |
---|
| 183 | Type *temp = initContext; |
---|
[a32b204] | 184 | initContext = new_type; |
---|
[a436947] | 185 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
| 186 | // enumerator initializers should not use the enum type to initialize, since |
---|
| 187 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
| 188 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); |
---|
| 189 | } |
---|
[a32b204] | 190 | SymTab::Indexer::visit( objectDecl ); |
---|
[a436947] | 191 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
| 192 | // delete newly created signed int type |
---|
| 193 | delete initContext; |
---|
| 194 | } |
---|
[3cfe27f] | 195 | initContext = temp; |
---|
[bfbf97f] | 196 | } |
---|
| 197 | |
---|
[40e636a] | 198 | template< typename PtrType > |
---|
| 199 | void Resolver::handlePtrType( PtrType * type ) { |
---|
| 200 | if ( type->get_dimension() ) { |
---|
| 201 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); |
---|
[bfbf97f] | 202 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
[40e636a] | 203 | delete type->get_dimension(); |
---|
| 204 | type->set_dimension( newExpr ); |
---|
[d1d17f5] | 205 | } |
---|
[40e636a] | 206 | } |
---|
| 207 | |
---|
| 208 | void Resolver::visit( ArrayType * at ) { |
---|
| 209 | handlePtrType( at ); |
---|
[bfbf97f] | 210 | Visitor::visit( at ); |
---|
[a32b204] | 211 | } |
---|
[94b4364] | 212 | |
---|
[40e636a] | 213 | void Resolver::visit( PointerType * pt ) { |
---|
| 214 | handlePtrType( pt ); |
---|
| 215 | Visitor::visit( pt ); |
---|
| 216 | } |
---|
| 217 | |
---|
[a32b204] | 218 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 219 | if ( typeDecl->get_base() ) { |
---|
| 220 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 221 | typeDecl->set_base( new_type ); |
---|
| 222 | } // if |
---|
| 223 | SymTab::Indexer::visit( typeDecl ); |
---|
| 224 | } |
---|
[94b4364] | 225 | |
---|
[a32b204] | 226 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
[d9a0e76] | 227 | #if 0 |
---|
[a32b204] | 228 | std::cout << "resolver visiting functiondecl "; |
---|
| 229 | functionDecl->print( std::cout ); |
---|
| 230 | std::cout << std::endl; |
---|
[d9a0e76] | 231 | #endif |
---|
[a32b204] | 232 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 233 | functionDecl->set_type( new_type ); |
---|
| 234 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
| 235 | functionReturn.clear(); |
---|
| 236 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
| 237 | functionReturn.push_back( (*i)->get_type() ); |
---|
| 238 | } // for |
---|
| 239 | SymTab::Indexer::visit( functionDecl ); |
---|
| 240 | functionReturn = oldFunctionReturn; |
---|
| 241 | } |
---|
[51b7345] | 242 | |
---|
[a436947] | 243 | void Resolver::visit( EnumDecl * enumDecl ) { |
---|
| 244 | // in case we decide to allow nested enums |
---|
| 245 | bool oldInEnumDecl = inEnumDecl; |
---|
| 246 | inEnumDecl = true; |
---|
| 247 | SymTab::Indexer::visit( enumDecl ); |
---|
| 248 | inEnumDecl = oldInEnumDecl; |
---|
| 249 | } |
---|
| 250 | |
---|
[a32b204] | 251 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
| 252 | if ( exprStmt->get_expr() ) { |
---|
| 253 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
| 254 | delete exprStmt->get_expr(); |
---|
| 255 | exprStmt->set_expr( newExpr ); |
---|
| 256 | } // if |
---|
| 257 | } |
---|
[51b7345] | 258 | |
---|
[7f5566b] | 259 | void Resolver::visit( AsmExpr *asmExpr ) { |
---|
| 260 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); |
---|
| 261 | delete asmExpr->get_operand(); |
---|
| 262 | asmExpr->set_operand( newExpr ); |
---|
| 263 | if ( asmExpr->get_inout() ) { |
---|
| 264 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); |
---|
| 265 | delete asmExpr->get_inout(); |
---|
| 266 | asmExpr->set_inout( newExpr ); |
---|
| 267 | } // if |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void Resolver::visit( AsmStmt *asmStmt ) { |
---|
| 271 | acceptAll( asmStmt->get_input(), *this); |
---|
| 272 | acceptAll( asmStmt->get_output(), *this); |
---|
| 273 | } |
---|
| 274 | |
---|
[a32b204] | 275 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
| 276 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
| 277 | delete ifStmt->get_condition(); |
---|
| 278 | ifStmt->set_condition( newExpr ); |
---|
| 279 | Visitor::visit( ifStmt ); |
---|
| 280 | } |
---|
[51b7345] | 281 | |
---|
[a32b204] | 282 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
| 283 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
| 284 | delete whileStmt->get_condition(); |
---|
| 285 | whileStmt->set_condition( newExpr ); |
---|
| 286 | Visitor::visit( whileStmt ); |
---|
| 287 | } |
---|
[51b7345] | 288 | |
---|
[a32b204] | 289 | void Resolver::visit( ForStmt *forStmt ) { |
---|
[145f1fc] | 290 | SymTab::Indexer::visit( forStmt ); |
---|
| 291 | |
---|
[a32b204] | 292 | if ( forStmt->get_condition() ) { |
---|
[145f1fc] | 293 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
[a32b204] | 294 | delete forStmt->get_condition(); |
---|
| 295 | forStmt->set_condition( newExpr ); |
---|
| 296 | } // if |
---|
[71f4e4f] | 297 | |
---|
[a32b204] | 298 | if ( forStmt->get_increment() ) { |
---|
[145f1fc] | 299 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
[a32b204] | 300 | delete forStmt->get_increment(); |
---|
| 301 | forStmt->set_increment( newExpr ); |
---|
| 302 | } // if |
---|
| 303 | } |
---|
[51b7345] | 304 | |
---|
[a32b204] | 305 | template< typename SwitchClass > |
---|
| 306 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
| 307 | Expression *newExpr; |
---|
| 308 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
| 309 | delete switchStmt->get_condition(); |
---|
| 310 | switchStmt->set_condition( newExpr ); |
---|
[71f4e4f] | 311 | |
---|
[a32b204] | 312 | visitor.Visitor::visit( switchStmt ); |
---|
| 313 | } |
---|
[51b7345] | 314 | |
---|
[a32b204] | 315 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
| 316 | handleSwitchStmt( switchStmt, *this ); |
---|
| 317 | } |
---|
[51b7345] | 318 | |
---|
[a32b204] | 319 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
| 320 | Visitor::visit( caseStmt ); |
---|
| 321 | } |
---|
[51b7345] | 322 | |
---|
[de62360d] | 323 | void Resolver::visit( BranchStmt *branchStmt ) { |
---|
| 324 | // must resolve the argument for a computed goto |
---|
| 325 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
[2871210] | 326 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
[de62360d] | 327 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder |
---|
| 328 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
| 329 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); |
---|
| 330 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression |
---|
| 331 | branchStmt->set_target( newExpr ); |
---|
| 332 | } // if |
---|
| 333 | } // if |
---|
| 334 | } |
---|
| 335 | |
---|
[a32b204] | 336 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
| 337 | if ( returnStmt->get_expr() ) { |
---|
| 338 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
| 339 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
| 340 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 341 | delete castExpr; |
---|
| 342 | returnStmt->set_expr( newExpr ); |
---|
| 343 | } // if |
---|
| 344 | } |
---|
[51b7345] | 345 | |
---|
[b5c5684] | 346 | template< typename T > |
---|
| 347 | bool isCharType( T t ) { |
---|
| 348 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
[71f4e4f] | 349 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
[b5c5684] | 350 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 351 | } |
---|
| 352 | return false; |
---|
| 353 | } |
---|
| 354 | |
---|
[a32b204] | 355 | void Resolver::visit( SingleInit *singleInit ) { |
---|
| 356 | if ( singleInit->get_value() ) { |
---|
[bdd516a] | 357 | #if 0 |
---|
[a32b204] | 358 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
| 359 | string n = ne->get_name(); |
---|
| 360 | if (n == "0") { |
---|
[71f4e4f] | 361 | initContext = new BasicType(Type::Qualifiers(), |
---|
[a32b204] | 362 | BasicType::SignedInt); |
---|
| 363 | } else { |
---|
[52f85e0] | 364 | DeclarationWithType * decl = lookupId( n ); |
---|
[a32b204] | 365 | initContext = decl->get_type(); |
---|
| 366 | } |
---|
[71f4e4f] | 367 | } else if (ConstantExpr * e = |
---|
[a32b204] | 368 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
| 369 | Constant *c = e->get_constant(); |
---|
| 370 | initContext = c->get_type(); |
---|
| 371 | } else { |
---|
| 372 | assert(0); |
---|
| 373 | } |
---|
[bdd516a] | 374 | #endif |
---|
[a32b204] | 375 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
| 376 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 377 | delete castExpr; |
---|
| 378 | singleInit->set_value( newExpr ); |
---|
[b5c5684] | 379 | |
---|
| 380 | // check if initializing type is char[] |
---|
| 381 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 382 | if ( isCharType( at->get_base() ) ) { |
---|
| 383 | // check if the resolved type is char * |
---|
| 384 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { |
---|
| 385 | if ( isCharType( pt->get_base() ) ) { |
---|
[52f85e0] | 386 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
[b5c5684] | 387 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); |
---|
| 388 | singleInit->set_value( ce->get_arg() ); |
---|
| 389 | ce->set_arg( NULL ); |
---|
[71f4e4f] | 390 | delete ce; |
---|
[b5c5684] | 391 | } |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | } |
---|
[a32b204] | 395 | } // if |
---|
[6c3744e] | 396 | // singleInit->get_value()->accept( *this ); |
---|
[a32b204] | 397 | } |
---|
[51b7345] | 398 | |
---|
[94b4364] | 399 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { |
---|
| 400 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); |
---|
| 401 | assert( dt ); |
---|
| 402 | initContext = dt->get_type(); |
---|
| 403 | try { |
---|
| 404 | if ( init == initEnd ) return; // stop when there are no more initializers |
---|
| 405 | (*init)->accept( *this ); |
---|
| 406 | ++init; // made it past an initializer |
---|
| 407 | } catch( SemanticError & ) { |
---|
| 408 | // need to delve deeper, if you can |
---|
| 409 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { |
---|
| 410 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); |
---|
| 411 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { |
---|
| 412 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); |
---|
| 413 | } else { |
---|
[1869adf] | 414 | // member is not an aggregate type, so can't go any deeper |
---|
| 415 | |
---|
[94b4364] | 416 | // might need to rethink what is being thrown |
---|
| 417 | throw; |
---|
| 418 | } // if |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { |
---|
| 423 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { |
---|
| 424 | // want to resolve each initializer to the members of the struct, |
---|
| 425 | // but if there are more initializers than members we should stop |
---|
| 426 | list< Declaration * >::iterator it = st->get_members().begin(); |
---|
| 427 | for ( ; it != st->get_members().end(); ++it) { |
---|
| 428 | resolveSingleAggrInit( *it, init, initEnd ); |
---|
| 429 | } |
---|
| 430 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { |
---|
| 431 | // only resolve to the first member of a union |
---|
| 432 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); |
---|
| 433 | } // if |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | void Resolver::visit( ListInit * listInit ) { |
---|
[4d2434a] | 437 | InitIterator iter = listInit->begin(); |
---|
| 438 | InitIterator end = listInit->end(); |
---|
[94b4364] | 439 | |
---|
[b5c5684] | 440 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
[94b4364] | 441 | // resolve each member to the base type of the array |
---|
| 442 | for ( ; iter != end; ++iter ) { |
---|
[b5c5684] | 443 | initContext = at->get_base(); |
---|
| 444 | (*iter)->accept( *this ); |
---|
| 445 | } // for |
---|
[94b4364] | 446 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { |
---|
| 447 | resolveAggrInit( st->get_baseStruct(), iter, end ); |
---|
[b5c5684] | 448 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { |
---|
[94b4364] | 449 | resolveAggrInit( st->get_baseUnion(), iter, end ); |
---|
[b5c5684] | 450 | } else { |
---|
| 451 | // basic types are handled here |
---|
| 452 | Visitor::visit( listInit ); |
---|
| 453 | } |
---|
| 454 | |
---|
[bdd516a] | 455 | #if 0 |
---|
[a32b204] | 456 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
| 457 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
| 458 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
| 459 | initContext = at->get_base(); |
---|
| 460 | (*iter)->accept( *this ); |
---|
| 461 | } // for |
---|
| 462 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
| 463 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
| 464 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
| 465 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
| 466 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
| 467 | if ( (*iter2)->get_designators().empty() ) { |
---|
| 468 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
| 469 | initContext = dt->get_type(); |
---|
| 470 | (*iter2)->accept( *this ); |
---|
| 471 | ++iter1; |
---|
| 472 | } else { |
---|
| 473 | StructDecl *st = baseStruct; |
---|
| 474 | iter1 = st->get_members().begin(); |
---|
| 475 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
| 476 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
| 477 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
| 478 | assert( key ); |
---|
| 479 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
| 480 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
| 481 | (*iter1)->print( cout ); |
---|
| 482 | cout << key->get_name() << endl; |
---|
| 483 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 484 | assert( fred ); |
---|
| 485 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
| 486 | assert( mary ); |
---|
| 487 | st = mary->get_baseStruct(); |
---|
| 488 | iter1 = st->get_members().begin(); |
---|
| 489 | break; |
---|
| 490 | } // if |
---|
| 491 | } // for |
---|
| 492 | } // for |
---|
| 493 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 494 | assert( fred ); |
---|
| 495 | initContext = fred->get_type(); |
---|
| 496 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 497 | } // if |
---|
| 498 | } // for |
---|
| 499 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
| 500 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
| 501 | initContext = dt->get_type(); |
---|
| 502 | (*listInit->begin_initializers())->accept( *this ); |
---|
[2c2242c] | 503 | } // if |
---|
[bdd516a] | 504 | #endif |
---|
[a32b204] | 505 | } |
---|
[71f4e4f] | 506 | |
---|
[f1e012b] | 507 | // ConstructorInit - fall back on C-style initializer |
---|
| 508 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
| 509 | // could not find valid constructor, or found an intrinsic constructor |
---|
| 510 | // fall back on C-style initializer |
---|
| 511 | delete ctorInit->get_ctor(); |
---|
| 512 | ctorInit->set_ctor( NULL ); |
---|
[71a145de] | 513 | delete ctorInit->get_dtor(); |
---|
| 514 | ctorInit->set_dtor( NULL ); |
---|
[f1e012b] | 515 | maybeAccept( ctorInit->get_init(), *this ); |
---|
| 516 | } |
---|
| 517 | |
---|
[71f4e4f] | 518 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
[071a31a] | 519 | try { |
---|
[5b2f5bb] | 520 | maybeAccept( ctorInit->get_ctor(), *this ); |
---|
| 521 | maybeAccept( ctorInit->get_dtor(), *this ); |
---|
[071a31a] | 522 | } catch ( SemanticError ) { |
---|
| 523 | // no alternatives for the constructor initializer - fallback on C-style initializer |
---|
[70f89d00] | 524 | // xxx - not sure if this makes a ton of sense - should maybe never be able to have this situation? |
---|
[f1e012b] | 525 | fallbackInit( ctorInit ); |
---|
[071a31a] | 526 | return; |
---|
| 527 | } |
---|
| 528 | |
---|
[5b2f5bb] | 529 | // found a constructor - can get rid of C-style initializer |
---|
| 530 | delete ctorInit->get_init(); |
---|
| 531 | ctorInit->set_init( NULL ); |
---|
[ec79847] | 532 | |
---|
| 533 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
| 534 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
| 535 | // to clean up generated code. |
---|
[f9cebb5] | 536 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
[ec79847] | 537 | delete ctorInit->get_ctor(); |
---|
| 538 | ctorInit->set_ctor( NULL ); |
---|
| 539 | } |
---|
[f9cebb5] | 540 | |
---|
| 541 | // xxx - todo |
---|
| 542 | // if ( InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
| 543 | // // can reduce the constructor down to a SingleInit using the |
---|
| 544 | // // second argument from the ctor call |
---|
| 545 | // } |
---|
| 546 | |
---|
| 547 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
[ec79847] | 548 | delete ctorInit->get_dtor(); |
---|
| 549 | ctorInit->set_dtor( NULL ); |
---|
| 550 | } |
---|
[71f4e4f] | 551 | } |
---|
[51b7345] | 552 | } // namespace ResolvExpr |
---|
[a32b204] | 553 | |
---|
| 554 | // Local Variables: // |
---|
| 555 | // tab-width: 4 // |
---|
| 556 | // mode: c++ // |
---|
| 557 | // compile-command: "make install" // |
---|
| 558 | // End: // |
---|