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