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