[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" |
---|
[906e24d] | 21 | #include "typeops.h" |
---|
[51b7345] | 22 | #include "SynTree/Statement.h" |
---|
| 23 | #include "SynTree/Type.h" |
---|
| 24 | #include "SynTree/Expression.h" |
---|
| 25 | #include "SynTree/Initializer.h" |
---|
| 26 | #include "SymTab/Indexer.h" |
---|
[5f98ce5] | 27 | #include "SymTab/Autogen.h" |
---|
[d3b7937] | 28 | #include "Common/utility.h" |
---|
[7b3f66b] | 29 | #include "InitTweak/InitTweak.h" |
---|
[51b7345] | 30 | |
---|
[d9a0e76] | 31 | #include <iostream> |
---|
| 32 | using namespace std; |
---|
[51b7345] | 33 | |
---|
[d9a0e76] | 34 | namespace ResolvExpr { |
---|
[62e5546] | 35 | class Resolver final : public SymTab::Indexer { |
---|
[a32b204] | 36 | public: |
---|
[77971f6] | 37 | Resolver() : SymTab::Indexer( false ) {} |
---|
[71f4e4f] | 38 | |
---|
[62e5546] | 39 | using SymTab::Indexer::visit; |
---|
| 40 | virtual void visit( FunctionDecl *functionDecl ) override; |
---|
| 41 | virtual void visit( ObjectDecl *functionDecl ) override; |
---|
| 42 | virtual void visit( TypeDecl *typeDecl ) override; |
---|
| 43 | virtual void visit( EnumDecl * enumDecl ) override; |
---|
[94b4364] | 44 | |
---|
[62e5546] | 45 | virtual void visit( ArrayType * at ) override; |
---|
| 46 | virtual void visit( PointerType * at ) override; |
---|
[d9a0e76] | 47 | |
---|
[62e5546] | 48 | virtual void visit( ExprStmt *exprStmt ) override; |
---|
| 49 | virtual void visit( AsmExpr *asmExpr ) override; |
---|
| 50 | virtual void visit( AsmStmt *asmStmt ) override; |
---|
| 51 | virtual void visit( IfStmt *ifStmt ) override; |
---|
| 52 | virtual void visit( WhileStmt *whileStmt ) override; |
---|
| 53 | virtual void visit( ForStmt *forStmt ) override; |
---|
| 54 | virtual void visit( SwitchStmt *switchStmt ) override; |
---|
| 55 | virtual void visit( CaseStmt *caseStmt ) override; |
---|
| 56 | virtual void visit( BranchStmt *branchStmt ) override; |
---|
| 57 | virtual void visit( ReturnStmt *returnStmt ) override; |
---|
[d9a0e76] | 58 | |
---|
[62e5546] | 59 | virtual void visit( SingleInit *singleInit ) override; |
---|
| 60 | virtual void visit( ListInit *listInit ) override; |
---|
| 61 | virtual void visit( ConstructorInit *ctorInit ) override; |
---|
[a32b204] | 62 | private: |
---|
[94b4364] | 63 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
| 64 | |
---|
[40e636a] | 65 | template< typename PtrType > |
---|
| 66 | void handlePtrType( PtrType * type ); |
---|
| 67 | |
---|
[94b4364] | 68 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); |
---|
| 69 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); |
---|
[f1e012b] | 70 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
[b726084] | 71 | |
---|
[77971f6] | 72 | Type * functionReturn = nullptr; |
---|
| 73 | Type *initContext = nullptr; |
---|
[a436947] | 74 | bool inEnumDecl = false; |
---|
[a32b204] | 75 | }; |
---|
[d9a0e76] | 76 | |
---|
[a32b204] | 77 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
| 78 | Resolver resolver; |
---|
| 79 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 80 | #if 0 |
---|
[a32b204] | 81 | resolver.print( cerr ); |
---|
| 82 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 83 | (*i)->print( std::cerr ); |
---|
| 84 | (*i)->accept( resolver ); |
---|
| 85 | } // for |
---|
[d9a0e76] | 86 | #endif |
---|
| 87 | } |
---|
| 88 | |
---|
[a32b204] | 89 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
| 90 | TypeEnvironment env; |
---|
| 91 | return resolveInVoidContext( expr, indexer, env ); |
---|
[d9a0e76] | 92 | } |
---|
[a32b204] | 93 | |
---|
[db4ecc5] | 94 | |
---|
[a32b204] | 95 | namespace { |
---|
| 96 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
| 97 | expr->set_env( new TypeSubstitution ); |
---|
| 98 | env.makeSubstitution( *expr->get_env() ); |
---|
| 99 | } |
---|
[db4ecc5] | 100 | } // namespace |
---|
[a32b204] | 101 | |
---|
[db4ecc5] | 102 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 103 | global_renamer.reset(); |
---|
| 104 | TypeEnvironment env; |
---|
| 105 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 106 | finishExpr( newExpr, env ); |
---|
| 107 | return newExpr; |
---|
| 108 | } |
---|
[71f4e4f] | 109 | |
---|
[db4ecc5] | 110 | namespace { |
---|
[a32b204] | 111 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 112 | TypeEnvironment env; |
---|
| 113 | AlternativeFinder finder( indexer, env ); |
---|
| 114 | finder.find( untyped ); |
---|
[d9a0e76] | 115 | #if 0 |
---|
[a32b204] | 116 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 117 | std::cout << "untyped expr is "; |
---|
| 118 | untyped->print( std::cout ); |
---|
| 119 | std::cout << std::endl << "alternatives are:"; |
---|
| 120 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 121 | i->print( std::cout ); |
---|
| 122 | } // for |
---|
| 123 | } // if |
---|
[d9a0e76] | 124 | #endif |
---|
[a32b204] | 125 | assert( finder.get_alternatives().size() == 1 ); |
---|
| 126 | Alternative &choice = finder.get_alternatives().front(); |
---|
| 127 | Expression *newExpr = choice.expr->clone(); |
---|
| 128 | finishExpr( newExpr, choice.env ); |
---|
| 129 | return newExpr; |
---|
| 130 | } |
---|
[d9a0e76] | 131 | |
---|
[a32b204] | 132 | bool isIntegralType( Type *type ) { |
---|
| 133 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 134 | return true; |
---|
| 135 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
| 136 | return bt->isInteger(); |
---|
[89e6ffc] | 137 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
| 138 | return true; |
---|
[a32b204] | 139 | } else { |
---|
| 140 | return false; |
---|
| 141 | } // if |
---|
| 142 | } |
---|
[71f4e4f] | 143 | |
---|
[a32b204] | 144 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 145 | TypeEnvironment env; |
---|
| 146 | AlternativeFinder finder( indexer, env ); |
---|
| 147 | finder.find( untyped ); |
---|
[d9a0e76] | 148 | #if 0 |
---|
[a32b204] | 149 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 150 | std::cout << "untyped expr is "; |
---|
| 151 | untyped->print( std::cout ); |
---|
| 152 | std::cout << std::endl << "alternatives are:"; |
---|
| 153 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 154 | i->print( std::cout ); |
---|
| 155 | } // for |
---|
| 156 | } // if |
---|
[d9a0e76] | 157 | #endif |
---|
[a32b204] | 158 | Expression *newExpr = 0; |
---|
| 159 | const TypeEnvironment *newEnv = 0; |
---|
| 160 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
[906e24d] | 161 | if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) { |
---|
[a32b204] | 162 | if ( newExpr ) { |
---|
| 163 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
| 164 | } else { |
---|
| 165 | newExpr = i->expr->clone(); |
---|
| 166 | newEnv = &i->env; |
---|
| 167 | } // if |
---|
| 168 | } // if |
---|
| 169 | } // for |
---|
| 170 | if ( ! newExpr ) { |
---|
| 171 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
| 172 | } // if |
---|
| 173 | finishExpr( newExpr, *newEnv ); |
---|
| 174 | return newExpr; |
---|
| 175 | } |
---|
[71f4e4f] | 176 | |
---|
[a32b204] | 177 | } |
---|
[71f4e4f] | 178 | |
---|
[a32b204] | 179 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
| 180 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
| 181 | objectDecl->set_type( new_type ); |
---|
[3cfe27f] | 182 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable |
---|
| 183 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes |
---|
| 184 | // initContext because of a function type can contain object declarations in the return and parameter types. So |
---|
| 185 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting |
---|
| 186 | // the RHS. |
---|
| 187 | Type *temp = initContext; |
---|
[a32b204] | 188 | initContext = new_type; |
---|
[a436947] | 189 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
| 190 | // enumerator initializers should not use the enum type to initialize, since |
---|
| 191 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
| 192 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); |
---|
| 193 | } |
---|
[a32b204] | 194 | SymTab::Indexer::visit( objectDecl ); |
---|
[a436947] | 195 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
| 196 | // delete newly created signed int type |
---|
| 197 | delete initContext; |
---|
| 198 | } |
---|
[3cfe27f] | 199 | initContext = temp; |
---|
[bfbf97f] | 200 | } |
---|
| 201 | |
---|
[40e636a] | 202 | template< typename PtrType > |
---|
| 203 | void Resolver::handlePtrType( PtrType * type ) { |
---|
| 204 | if ( type->get_dimension() ) { |
---|
| 205 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); |
---|
[bfbf97f] | 206 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
[40e636a] | 207 | delete type->get_dimension(); |
---|
| 208 | type->set_dimension( newExpr ); |
---|
[d1d17f5] | 209 | } |
---|
[40e636a] | 210 | } |
---|
| 211 | |
---|
| 212 | void Resolver::visit( ArrayType * at ) { |
---|
| 213 | handlePtrType( at ); |
---|
[bfbf97f] | 214 | Visitor::visit( at ); |
---|
[a32b204] | 215 | } |
---|
[94b4364] | 216 | |
---|
[40e636a] | 217 | void Resolver::visit( PointerType * pt ) { |
---|
| 218 | handlePtrType( pt ); |
---|
| 219 | Visitor::visit( pt ); |
---|
| 220 | } |
---|
| 221 | |
---|
[a32b204] | 222 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 223 | if ( typeDecl->get_base() ) { |
---|
| 224 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 225 | typeDecl->set_base( new_type ); |
---|
| 226 | } // if |
---|
| 227 | SymTab::Indexer::visit( typeDecl ); |
---|
| 228 | } |
---|
[94b4364] | 229 | |
---|
[a32b204] | 230 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
[d9a0e76] | 231 | #if 0 |
---|
[a32b204] | 232 | std::cout << "resolver visiting functiondecl "; |
---|
| 233 | functionDecl->print( std::cout ); |
---|
| 234 | std::cout << std::endl; |
---|
[d9a0e76] | 235 | #endif |
---|
[a32b204] | 236 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 237 | functionDecl->set_type( new_type ); |
---|
[906e24d] | 238 | ValueGuard< Type * > oldFunctionReturn( functionReturn ); |
---|
| 239 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); |
---|
[a32b204] | 240 | SymTab::Indexer::visit( functionDecl ); |
---|
| 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() ) { |
---|
[906e24d] | 338 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); |
---|
[a32b204] | 339 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 340 | delete castExpr; |
---|
| 341 | returnStmt->set_expr( newExpr ); |
---|
| 342 | } // if |
---|
| 343 | } |
---|
[51b7345] | 344 | |
---|
[b5c5684] | 345 | template< typename T > |
---|
| 346 | bool isCharType( T t ) { |
---|
| 347 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
[71f4e4f] | 348 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
[b5c5684] | 349 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 350 | } |
---|
| 351 | return false; |
---|
| 352 | } |
---|
| 353 | |
---|
[a32b204] | 354 | void Resolver::visit( SingleInit *singleInit ) { |
---|
| 355 | if ( singleInit->get_value() ) { |
---|
[bdd516a] | 356 | #if 0 |
---|
[a32b204] | 357 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
| 358 | string n = ne->get_name(); |
---|
| 359 | if (n == "0") { |
---|
[71f4e4f] | 360 | initContext = new BasicType(Type::Qualifiers(), |
---|
[a32b204] | 361 | BasicType::SignedInt); |
---|
| 362 | } else { |
---|
[52f85e0] | 363 | DeclarationWithType * decl = lookupId( n ); |
---|
[a32b204] | 364 | initContext = decl->get_type(); |
---|
| 365 | } |
---|
[71f4e4f] | 366 | } else if (ConstantExpr * e = |
---|
[a32b204] | 367 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
| 368 | Constant *c = e->get_constant(); |
---|
| 369 | initContext = c->get_type(); |
---|
| 370 | } else { |
---|
| 371 | assert(0); |
---|
| 372 | } |
---|
[bdd516a] | 373 | #endif |
---|
[a32b204] | 374 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
| 375 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 376 | delete castExpr; |
---|
| 377 | singleInit->set_value( newExpr ); |
---|
[b5c5684] | 378 | |
---|
| 379 | // check if initializing type is char[] |
---|
| 380 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 381 | if ( isCharType( at->get_base() ) ) { |
---|
| 382 | // check if the resolved type is char * |
---|
[906e24d] | 383 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
[b5c5684] | 384 | if ( isCharType( pt->get_base() ) ) { |
---|
[52f85e0] | 385 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
[b5c5684] | 386 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); |
---|
| 387 | singleInit->set_value( ce->get_arg() ); |
---|
| 388 | ce->set_arg( NULL ); |
---|
[71f4e4f] | 389 | delete ce; |
---|
[b5c5684] | 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | } |
---|
[a32b204] | 394 | } // if |
---|
[6c3744e] | 395 | // singleInit->get_value()->accept( *this ); |
---|
[a32b204] | 396 | } |
---|
[51b7345] | 397 | |
---|
[94b4364] | 398 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { |
---|
| 399 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); |
---|
| 400 | assert( dt ); |
---|
| 401 | initContext = dt->get_type(); |
---|
| 402 | try { |
---|
| 403 | if ( init == initEnd ) return; // stop when there are no more initializers |
---|
| 404 | (*init)->accept( *this ); |
---|
| 405 | ++init; // made it past an initializer |
---|
| 406 | } catch( SemanticError & ) { |
---|
| 407 | // need to delve deeper, if you can |
---|
| 408 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { |
---|
| 409 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); |
---|
| 410 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { |
---|
| 411 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); |
---|
| 412 | } else { |
---|
[1869adf] | 413 | // member is not an aggregate type, so can't go any deeper |
---|
| 414 | |
---|
[94b4364] | 415 | // might need to rethink what is being thrown |
---|
| 416 | throw; |
---|
| 417 | } // if |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { |
---|
| 422 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { |
---|
| 423 | // want to resolve each initializer to the members of the struct, |
---|
| 424 | // but if there are more initializers than members we should stop |
---|
| 425 | list< Declaration * >::iterator it = st->get_members().begin(); |
---|
| 426 | for ( ; it != st->get_members().end(); ++it) { |
---|
| 427 | resolveSingleAggrInit( *it, init, initEnd ); |
---|
| 428 | } |
---|
| 429 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { |
---|
| 430 | // only resolve to the first member of a union |
---|
| 431 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); |
---|
| 432 | } // if |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | void Resolver::visit( ListInit * listInit ) { |
---|
[4d2434a] | 436 | InitIterator iter = listInit->begin(); |
---|
| 437 | InitIterator end = listInit->end(); |
---|
[94b4364] | 438 | |
---|
[b5c5684] | 439 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
[94b4364] | 440 | // resolve each member to the base type of the array |
---|
| 441 | for ( ; iter != end; ++iter ) { |
---|
[b5c5684] | 442 | initContext = at->get_base(); |
---|
| 443 | (*iter)->accept( *this ); |
---|
| 444 | } // for |
---|
[65660bd] | 445 | } else if ( TupleType * tt = dynamic_cast< TupleType * > ( initContext ) ) { |
---|
| 446 | for ( Type * t : *tt ) { |
---|
| 447 | if ( iter == end ) break; |
---|
| 448 | initContext = t; |
---|
| 449 | (*iter++)->accept( *this ); |
---|
| 450 | } |
---|
[94b4364] | 451 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { |
---|
| 452 | resolveAggrInit( st->get_baseStruct(), iter, end ); |
---|
[679864e1] | 453 | } else if ( UnionInstType * st = dynamic_cast< UnionInstType * >( initContext ) ) { |
---|
[94b4364] | 454 | resolveAggrInit( st->get_baseUnion(), iter, end ); |
---|
[679864e1] | 455 | } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
[242d458] | 456 | Type * base = tt->get_baseType()->get_base(); |
---|
| 457 | if ( base ) { |
---|
| 458 | // know the implementation type, so try using that as the initContext |
---|
| 459 | initContext = base; |
---|
| 460 | visit( listInit ); |
---|
| 461 | } else { |
---|
| 462 | // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
| 463 | Visitor::visit( listInit ); |
---|
| 464 | } |
---|
[b5c5684] | 465 | } else { |
---|
[89e6ffc] | 466 | assert( dynamic_cast< BasicType * >( initContext ) || dynamic_cast< PointerType * >( initContext ) |
---|
| 467 | || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) ); |
---|
[b5c5684] | 468 | // basic types are handled here |
---|
| 469 | Visitor::visit( listInit ); |
---|
| 470 | } |
---|
| 471 | |
---|
[bdd516a] | 472 | #if 0 |
---|
[a32b204] | 473 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
| 474 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
| 475 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
| 476 | initContext = at->get_base(); |
---|
| 477 | (*iter)->accept( *this ); |
---|
| 478 | } // for |
---|
| 479 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
| 480 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
| 481 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
| 482 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
| 483 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
| 484 | if ( (*iter2)->get_designators().empty() ) { |
---|
| 485 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
| 486 | initContext = dt->get_type(); |
---|
| 487 | (*iter2)->accept( *this ); |
---|
| 488 | ++iter1; |
---|
| 489 | } else { |
---|
| 490 | StructDecl *st = baseStruct; |
---|
| 491 | iter1 = st->get_members().begin(); |
---|
| 492 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
| 493 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
| 494 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
| 495 | assert( key ); |
---|
| 496 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
| 497 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
| 498 | (*iter1)->print( cout ); |
---|
| 499 | cout << key->get_name() << endl; |
---|
| 500 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 501 | assert( fred ); |
---|
| 502 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
| 503 | assert( mary ); |
---|
| 504 | st = mary->get_baseStruct(); |
---|
| 505 | iter1 = st->get_members().begin(); |
---|
| 506 | break; |
---|
| 507 | } // if |
---|
| 508 | } // for |
---|
| 509 | } // for |
---|
| 510 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 511 | assert( fred ); |
---|
| 512 | initContext = fred->get_type(); |
---|
| 513 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 514 | } // if |
---|
| 515 | } // for |
---|
| 516 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
| 517 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
| 518 | initContext = dt->get_type(); |
---|
| 519 | (*listInit->begin_initializers())->accept( *this ); |
---|
[2c2242c] | 520 | } // if |
---|
[bdd516a] | 521 | #endif |
---|
[a32b204] | 522 | } |
---|
[71f4e4f] | 523 | |
---|
[f1e012b] | 524 | // ConstructorInit - fall back on C-style initializer |
---|
| 525 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
| 526 | // could not find valid constructor, or found an intrinsic constructor |
---|
| 527 | // fall back on C-style initializer |
---|
| 528 | delete ctorInit->get_ctor(); |
---|
| 529 | ctorInit->set_ctor( NULL ); |
---|
[71a145de] | 530 | delete ctorInit->get_dtor(); |
---|
| 531 | ctorInit->set_dtor( NULL ); |
---|
[f1e012b] | 532 | maybeAccept( ctorInit->get_init(), *this ); |
---|
| 533 | } |
---|
| 534 | |
---|
[71f4e4f] | 535 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
[1ba88a0] | 536 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
| 537 | maybeAccept( ctorInit->get_ctor(), *this ); |
---|
| 538 | maybeAccept( ctorInit->get_dtor(), *this ); |
---|
[071a31a] | 539 | |
---|
[5b2f5bb] | 540 | // found a constructor - can get rid of C-style initializer |
---|
| 541 | delete ctorInit->get_init(); |
---|
| 542 | ctorInit->set_init( NULL ); |
---|
[ec79847] | 543 | |
---|
| 544 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
| 545 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
| 546 | // to clean up generated code. |
---|
[f9cebb5] | 547 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
[ec79847] | 548 | delete ctorInit->get_ctor(); |
---|
| 549 | ctorInit->set_ctor( NULL ); |
---|
| 550 | } |
---|
[f9cebb5] | 551 | |
---|
| 552 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
[ec79847] | 553 | delete ctorInit->get_dtor(); |
---|
| 554 | ctorInit->set_dtor( NULL ); |
---|
| 555 | } |
---|
[a465caf] | 556 | |
---|
| 557 | // xxx - todo -- what about arrays? |
---|
| 558 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
| 559 | // // can reduce the constructor down to a SingleInit using the |
---|
| 560 | // // second argument from the ctor call, since |
---|
| 561 | // delete ctorInit->get_ctor(); |
---|
| 562 | // ctorInit->set_ctor( NULL ); |
---|
| 563 | |
---|
| 564 | // Expression * arg = |
---|
| 565 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
| 566 | // } |
---|
[71f4e4f] | 567 | } |
---|
[51b7345] | 568 | } // namespace ResolvExpr |
---|
[a32b204] | 569 | |
---|
| 570 | // Local Variables: // |
---|
| 571 | // tab-width: 4 // |
---|
| 572 | // mode: c++ // |
---|
| 573 | // compile-command: "make install" // |
---|
| 574 | // End: // |
---|