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