[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 |
---|
[6013bd7] | 12 | // Last Modified On : Thu Mar 23 17:23:14 2017 |
---|
| 13 | // Update Count : 211 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[e4d829b] | 16 | #include <iostream> |
---|
| 17 | |
---|
[51b7345] | 18 | #include "Alternative.h" |
---|
[e4d829b] | 19 | #include "AlternativeFinder.h" |
---|
| 20 | #include "CurrentObject.h" |
---|
[51b7345] | 21 | #include "RenameVars.h" |
---|
[e4d829b] | 22 | #include "Resolver.h" |
---|
[51b7345] | 23 | #include "ResolveTypeof.h" |
---|
[906e24d] | 24 | #include "typeops.h" |
---|
[e4d829b] | 25 | |
---|
[51b7345] | 26 | #include "SynTree/Expression.h" |
---|
| 27 | #include "SynTree/Initializer.h" |
---|
[e4d829b] | 28 | #include "SynTree/Statement.h" |
---|
| 29 | #include "SynTree/Type.h" |
---|
| 30 | |
---|
[5f98ce5] | 31 | #include "SymTab/Autogen.h" |
---|
[e4d829b] | 32 | #include "SymTab/Indexer.h" |
---|
| 33 | |
---|
[d3b7937] | 34 | #include "Common/utility.h" |
---|
[e4d829b] | 35 | |
---|
[7b3f66b] | 36 | #include "InitTweak/InitTweak.h" |
---|
[51b7345] | 37 | |
---|
[d9a0e76] | 38 | using namespace std; |
---|
[51b7345] | 39 | |
---|
[d9a0e76] | 40 | namespace ResolvExpr { |
---|
[62e5546] | 41 | class Resolver final : public SymTab::Indexer { |
---|
[a32b204] | 42 | public: |
---|
[77971f6] | 43 | Resolver() : SymTab::Indexer( false ) {} |
---|
[1d2b64f] | 44 | Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) { |
---|
| 45 | if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) { |
---|
| 46 | functionReturn = res->functionReturn; |
---|
[e4d829b] | 47 | currentObject = res->currentObject; |
---|
[1d2b64f] | 48 | inEnumDecl = res->inEnumDecl; |
---|
| 49 | } |
---|
| 50 | } |
---|
[71f4e4f] | 51 | |
---|
[1d2b64f] | 52 | typedef SymTab::Indexer Parent; |
---|
| 53 | using Parent::visit; |
---|
[62e5546] | 54 | virtual void visit( FunctionDecl *functionDecl ) override; |
---|
| 55 | virtual void visit( ObjectDecl *functionDecl ) override; |
---|
| 56 | virtual void visit( TypeDecl *typeDecl ) override; |
---|
| 57 | virtual void visit( EnumDecl * enumDecl ) override; |
---|
[94b4364] | 58 | |
---|
[62e5546] | 59 | virtual void visit( ArrayType * at ) override; |
---|
| 60 | virtual void visit( PointerType * at ) override; |
---|
[d9a0e76] | 61 | |
---|
[62e5546] | 62 | virtual void visit( ExprStmt *exprStmt ) override; |
---|
| 63 | virtual void visit( AsmExpr *asmExpr ) override; |
---|
| 64 | virtual void visit( AsmStmt *asmStmt ) override; |
---|
| 65 | virtual void visit( IfStmt *ifStmt ) override; |
---|
| 66 | virtual void visit( WhileStmt *whileStmt ) override; |
---|
| 67 | virtual void visit( ForStmt *forStmt ) override; |
---|
| 68 | virtual void visit( SwitchStmt *switchStmt ) override; |
---|
| 69 | virtual void visit( CaseStmt *caseStmt ) override; |
---|
| 70 | virtual void visit( BranchStmt *branchStmt ) override; |
---|
| 71 | virtual void visit( ReturnStmt *returnStmt ) override; |
---|
[307a732] | 72 | virtual void visit( ThrowStmt *throwStmt ) override; |
---|
[d9a0e76] | 73 | |
---|
[62e5546] | 74 | virtual void visit( SingleInit *singleInit ) override; |
---|
| 75 | virtual void visit( ListInit *listInit ) override; |
---|
| 76 | virtual void visit( ConstructorInit *ctorInit ) override; |
---|
[a32b204] | 77 | private: |
---|
[94b4364] | 78 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
| 79 | |
---|
[40e636a] | 80 | template< typename PtrType > |
---|
| 81 | void handlePtrType( PtrType * type ); |
---|
| 82 | |
---|
[30b65d8] | 83 | void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & ); |
---|
| 84 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub ); |
---|
[f1e012b] | 85 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
[b726084] | 86 | |
---|
[77971f6] | 87 | Type * functionReturn = nullptr; |
---|
[e4d829b] | 88 | CurrentObject currentObject = nullptr; |
---|
[a436947] | 89 | bool inEnumDecl = false; |
---|
[a32b204] | 90 | }; |
---|
[d9a0e76] | 91 | |
---|
[a32b204] | 92 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
| 93 | Resolver resolver; |
---|
| 94 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 95 | } |
---|
| 96 | |
---|
[a32b204] | 97 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
| 98 | TypeEnvironment env; |
---|
| 99 | return resolveInVoidContext( expr, indexer, env ); |
---|
[d9a0e76] | 100 | } |
---|
[a32b204] | 101 | |
---|
[db4ecc5] | 102 | |
---|
[a32b204] | 103 | namespace { |
---|
| 104 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
| 105 | expr->set_env( new TypeSubstitution ); |
---|
| 106 | env.makeSubstitution( *expr->get_env() ); |
---|
| 107 | } |
---|
[db4ecc5] | 108 | } // namespace |
---|
[a32b204] | 109 | |
---|
[db4ecc5] | 110 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 111 | global_renamer.reset(); |
---|
| 112 | TypeEnvironment env; |
---|
| 113 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 114 | finishExpr( newExpr, env ); |
---|
| 115 | return newExpr; |
---|
| 116 | } |
---|
[71f4e4f] | 117 | |
---|
[db4ecc5] | 118 | namespace { |
---|
[a32b204] | 119 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 120 | TypeEnvironment env; |
---|
| 121 | AlternativeFinder finder( indexer, env ); |
---|
| 122 | finder.find( untyped ); |
---|
[d9a0e76] | 123 | #if 0 |
---|
[a32b204] | 124 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 125 | std::cout << "untyped expr is "; |
---|
| 126 | untyped->print( std::cout ); |
---|
| 127 | std::cout << std::endl << "alternatives are:"; |
---|
| 128 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 129 | i->print( std::cout ); |
---|
| 130 | } // for |
---|
| 131 | } // if |
---|
[d9a0e76] | 132 | #endif |
---|
[0b150ec] | 133 | assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end." ); |
---|
[a32b204] | 134 | Alternative &choice = finder.get_alternatives().front(); |
---|
| 135 | Expression *newExpr = choice.expr->clone(); |
---|
| 136 | finishExpr( newExpr, choice.env ); |
---|
| 137 | return newExpr; |
---|
| 138 | } |
---|
[d9a0e76] | 139 | |
---|
[a32b204] | 140 | bool isIntegralType( Type *type ) { |
---|
| 141 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 142 | return true; |
---|
| 143 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
| 144 | return bt->isInteger(); |
---|
[89e6ffc] | 145 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
| 146 | return true; |
---|
[a32b204] | 147 | } else { |
---|
| 148 | return false; |
---|
| 149 | } // if |
---|
| 150 | } |
---|
[71f4e4f] | 151 | |
---|
[a32b204] | 152 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 153 | TypeEnvironment env; |
---|
| 154 | AlternativeFinder finder( indexer, env ); |
---|
| 155 | finder.find( untyped ); |
---|
[d9a0e76] | 156 | #if 0 |
---|
[a32b204] | 157 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 158 | std::cout << "untyped expr is "; |
---|
| 159 | untyped->print( std::cout ); |
---|
| 160 | std::cout << std::endl << "alternatives are:"; |
---|
| 161 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 162 | i->print( std::cout ); |
---|
| 163 | } // for |
---|
| 164 | } // if |
---|
[d9a0e76] | 165 | #endif |
---|
[a32b204] | 166 | Expression *newExpr = 0; |
---|
| 167 | const TypeEnvironment *newEnv = 0; |
---|
| 168 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
[906e24d] | 169 | if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) { |
---|
[a32b204] | 170 | if ( newExpr ) { |
---|
| 171 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
| 172 | } else { |
---|
| 173 | newExpr = i->expr->clone(); |
---|
| 174 | newEnv = &i->env; |
---|
| 175 | } // if |
---|
| 176 | } // if |
---|
| 177 | } // for |
---|
| 178 | if ( ! newExpr ) { |
---|
| 179 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
| 180 | } // if |
---|
| 181 | finishExpr( newExpr, *newEnv ); |
---|
| 182 | return newExpr; |
---|
| 183 | } |
---|
[71f4e4f] | 184 | |
---|
[a32b204] | 185 | } |
---|
[71f4e4f] | 186 | |
---|
[a32b204] | 187 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
| 188 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
| 189 | objectDecl->set_type( new_type ); |
---|
[3cfe27f] | 190 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable |
---|
| 191 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes |
---|
| 192 | // initContext because of a function type can contain object declarations in the return and parameter types. So |
---|
| 193 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting |
---|
| 194 | // the RHS. |
---|
[e4d829b] | 195 | ValueGuard<CurrentObject> temp( currentObject ); |
---|
| 196 | currentObject = CurrentObject( objectDecl->get_type() ); |
---|
| 197 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
[a436947] | 198 | // enumerator initializers should not use the enum type to initialize, since |
---|
| 199 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
[e4d829b] | 200 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[a436947] | 201 | } |
---|
[1d2b64f] | 202 | Parent::visit( objectDecl ); |
---|
[e4d829b] | 203 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
[a436947] | 204 | // delete newly created signed int type |
---|
[e4d829b] | 205 | // delete currentObject.getType(); |
---|
[a436947] | 206 | } |
---|
[bfbf97f] | 207 | } |
---|
| 208 | |
---|
[40e636a] | 209 | template< typename PtrType > |
---|
| 210 | void Resolver::handlePtrType( PtrType * type ) { |
---|
| 211 | if ( type->get_dimension() ) { |
---|
| 212 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); |
---|
[bfbf97f] | 213 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
[40e636a] | 214 | delete type->get_dimension(); |
---|
| 215 | type->set_dimension( newExpr ); |
---|
[d1d17f5] | 216 | } |
---|
[40e636a] | 217 | } |
---|
| 218 | |
---|
| 219 | void Resolver::visit( ArrayType * at ) { |
---|
| 220 | handlePtrType( at ); |
---|
[1d2b64f] | 221 | Parent::visit( at ); |
---|
[a32b204] | 222 | } |
---|
[94b4364] | 223 | |
---|
[40e636a] | 224 | void Resolver::visit( PointerType * pt ) { |
---|
| 225 | handlePtrType( pt ); |
---|
[1d2b64f] | 226 | Parent::visit( pt ); |
---|
[40e636a] | 227 | } |
---|
| 228 | |
---|
[a32b204] | 229 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 230 | if ( typeDecl->get_base() ) { |
---|
| 231 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 232 | typeDecl->set_base( new_type ); |
---|
| 233 | } // if |
---|
[1d2b64f] | 234 | Parent::visit( typeDecl ); |
---|
[a32b204] | 235 | } |
---|
[94b4364] | 236 | |
---|
[a32b204] | 237 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
[d9a0e76] | 238 | #if 0 |
---|
[a32b204] | 239 | std::cout << "resolver visiting functiondecl "; |
---|
| 240 | functionDecl->print( std::cout ); |
---|
| 241 | std::cout << std::endl; |
---|
[d9a0e76] | 242 | #endif |
---|
[a32b204] | 243 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 244 | functionDecl->set_type( new_type ); |
---|
[906e24d] | 245 | ValueGuard< Type * > oldFunctionReturn( functionReturn ); |
---|
| 246 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); |
---|
[1d2b64f] | 247 | Parent::visit( functionDecl ); |
---|
[88d1066] | 248 | |
---|
| 249 | // default value expressions have an environment which shouldn't be there and trips up later passes. |
---|
| 250 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently |
---|
| 251 | // see how it's useful. |
---|
| 252 | for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) { |
---|
| 253 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { |
---|
| 254 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) { |
---|
| 255 | delete init->get_value()->get_env(); |
---|
| 256 | init->get_value()->set_env( nullptr ); |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | } |
---|
[a32b204] | 260 | } |
---|
[51b7345] | 261 | |
---|
[a436947] | 262 | void Resolver::visit( EnumDecl * enumDecl ) { |
---|
| 263 | // in case we decide to allow nested enums |
---|
[1d2b64f] | 264 | ValueGuard< bool > oldInEnumDecl( inEnumDecl ); |
---|
[a436947] | 265 | inEnumDecl = true; |
---|
[1d2b64f] | 266 | Parent::visit( enumDecl ); |
---|
[a436947] | 267 | } |
---|
| 268 | |
---|
[a32b204] | 269 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
[1d2b64f] | 270 | assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" ); |
---|
| 271 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
| 272 | delete exprStmt->get_expr(); |
---|
| 273 | exprStmt->set_expr( newExpr ); |
---|
[a32b204] | 274 | } |
---|
[51b7345] | 275 | |
---|
[7f5566b] | 276 | void Resolver::visit( AsmExpr *asmExpr ) { |
---|
| 277 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); |
---|
| 278 | delete asmExpr->get_operand(); |
---|
| 279 | asmExpr->set_operand( newExpr ); |
---|
| 280 | if ( asmExpr->get_inout() ) { |
---|
| 281 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); |
---|
| 282 | delete asmExpr->get_inout(); |
---|
| 283 | asmExpr->set_inout( newExpr ); |
---|
| 284 | } // if |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | void Resolver::visit( AsmStmt *asmStmt ) { |
---|
| 288 | acceptAll( asmStmt->get_input(), *this); |
---|
| 289 | acceptAll( asmStmt->get_output(), *this); |
---|
| 290 | } |
---|
| 291 | |
---|
[a32b204] | 292 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
| 293 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
| 294 | delete ifStmt->get_condition(); |
---|
| 295 | ifStmt->set_condition( newExpr ); |
---|
[1d2b64f] | 296 | Parent::visit( ifStmt ); |
---|
[a32b204] | 297 | } |
---|
[51b7345] | 298 | |
---|
[a32b204] | 299 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
| 300 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
| 301 | delete whileStmt->get_condition(); |
---|
| 302 | whileStmt->set_condition( newExpr ); |
---|
[1d2b64f] | 303 | Parent::visit( whileStmt ); |
---|
[a32b204] | 304 | } |
---|
[51b7345] | 305 | |
---|
[a32b204] | 306 | void Resolver::visit( ForStmt *forStmt ) { |
---|
[1d2b64f] | 307 | Parent::visit( forStmt ); |
---|
[145f1fc] | 308 | |
---|
[a32b204] | 309 | if ( forStmt->get_condition() ) { |
---|
[145f1fc] | 310 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
[a32b204] | 311 | delete forStmt->get_condition(); |
---|
| 312 | forStmt->set_condition( newExpr ); |
---|
| 313 | } // if |
---|
[71f4e4f] | 314 | |
---|
[a32b204] | 315 | if ( forStmt->get_increment() ) { |
---|
[145f1fc] | 316 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
[a32b204] | 317 | delete forStmt->get_increment(); |
---|
| 318 | forStmt->set_increment( newExpr ); |
---|
| 319 | } // if |
---|
| 320 | } |
---|
[51b7345] | 321 | |
---|
[32b8144] | 322 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
[e4d829b] | 323 | ValueGuard< CurrentObject > oldCurrentObject( currentObject ); |
---|
[a32b204] | 324 | Expression *newExpr; |
---|
[32b8144] | 325 | newExpr = findIntegralExpression( switchStmt->get_condition(), *this ); |
---|
[a32b204] | 326 | delete switchStmt->get_condition(); |
---|
| 327 | switchStmt->set_condition( newExpr ); |
---|
[71f4e4f] | 328 | |
---|
[e4d829b] | 329 | currentObject = CurrentObject( newExpr->get_result() ); |
---|
[32b8144] | 330 | Parent::visit( switchStmt ); |
---|
[a32b204] | 331 | } |
---|
[51b7345] | 332 | |
---|
[a32b204] | 333 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
[32b8144] | 334 | if ( caseStmt->get_condition() ) { |
---|
[e4d829b] | 335 | std::list< InitAlternative > initAlts = currentObject.getOptions(); |
---|
| 336 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); |
---|
| 337 | CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() ); |
---|
[32b8144] | 338 | Expression * newExpr = findSingleExpression( castExpr, *this ); |
---|
| 339 | castExpr = safe_dynamic_cast< CastExpr * >( newExpr ); |
---|
| 340 | caseStmt->set_condition( castExpr->get_arg() ); |
---|
| 341 | castExpr->set_arg( nullptr ); |
---|
| 342 | delete castExpr; |
---|
| 343 | } |
---|
[1d2b64f] | 344 | Parent::visit( caseStmt ); |
---|
[a32b204] | 345 | } |
---|
[51b7345] | 346 | |
---|
[de62360d] | 347 | void Resolver::visit( BranchStmt *branchStmt ) { |
---|
| 348 | // must resolve the argument for a computed goto |
---|
| 349 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
[2871210] | 350 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
[de62360d] | 351 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder |
---|
| 352 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
| 353 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); |
---|
| 354 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression |
---|
| 355 | branchStmt->set_target( newExpr ); |
---|
| 356 | } // if |
---|
| 357 | } // if |
---|
| 358 | } |
---|
| 359 | |
---|
[a32b204] | 360 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
| 361 | if ( returnStmt->get_expr() ) { |
---|
[906e24d] | 362 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); |
---|
[a32b204] | 363 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 364 | delete castExpr; |
---|
| 365 | returnStmt->set_expr( newExpr ); |
---|
| 366 | } // if |
---|
| 367 | } |
---|
[51b7345] | 368 | |
---|
[307a732] | 369 | void Resolver::visit( ThrowStmt *throwStmt ) { |
---|
| 370 | if ( throwStmt->get_expr() ) { |
---|
| 371 | Expression * wrapped = new CastExpr( throwStmt->get_expr(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
| 372 | Expression * newExpr = findSingleExpression( wrapped, *this ); |
---|
| 373 | throwStmt->set_expr( newExpr ); |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
[b5c5684] | 377 | template< typename T > |
---|
| 378 | bool isCharType( T t ) { |
---|
| 379 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
[71f4e4f] | 380 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
[b5c5684] | 381 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 382 | } |
---|
| 383 | return false; |
---|
| 384 | } |
---|
| 385 | |
---|
[e4d829b] | 386 | void Resolver::visit( SingleInit *singleInit ) { |
---|
[62423350] | 387 | // resolve initialization using the possibilities as determined by the currentObject cursor |
---|
[e4d829b] | 388 | UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() ); |
---|
| 389 | Expression * newExpr = findSingleExpression( untyped, *this ); |
---|
| 390 | InitExpr * initExpr = safe_dynamic_cast< InitExpr * >( newExpr ); |
---|
[62423350] | 391 | |
---|
| 392 | // move cursor to the object that is actually initialized |
---|
[e4d829b] | 393 | currentObject.setNext( initExpr->get_designation() ); |
---|
[62423350] | 394 | |
---|
| 395 | // discard InitExpr wrapper and retain relevant pieces |
---|
| 396 | newExpr = initExpr->get_expr(); |
---|
[435e75f] | 397 | newExpr->set_env( initExpr->get_env() ); |
---|
[62423350] | 398 | initExpr->set_expr( nullptr ); |
---|
| 399 | initExpr->set_env( nullptr ); |
---|
[e4d829b] | 400 | delete initExpr; |
---|
| 401 | |
---|
[62423350] | 402 | // get the actual object's type (may not exactly match what comes back from the resolver due to conversions) |
---|
| 403 | Type * initContext = currentObject.getCurrentType(); |
---|
| 404 | |
---|
| 405 | // check if actual object's type is char[] |
---|
| 406 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 407 | if ( isCharType( at->get_base() ) ) { |
---|
| 408 | // check if the resolved type is char * |
---|
| 409 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
| 410 | if ( isCharType( pt->get_base() ) ) { |
---|
| 411 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
| 412 | CastExpr *ce = safe_dynamic_cast< CastExpr * >( newExpr ); |
---|
| 413 | newExpr = ce->get_arg(); |
---|
| 414 | ce->set_arg( nullptr ); |
---|
| 415 | delete ce; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | } |
---|
[94b4364] | 420 | |
---|
[62423350] | 421 | // set initializer expr to resolved express |
---|
| 422 | singleInit->set_value( newExpr ); |
---|
| 423 | |
---|
| 424 | // move cursor to next object in preparation for next initializer |
---|
| 425 | currentObject.increment(); |
---|
| 426 | } |
---|
[94b4364] | 427 | |
---|
| 428 | void Resolver::visit( ListInit * listInit ) { |
---|
[62423350] | 429 | // move cursor into brace-enclosed initializer-list |
---|
[e4d829b] | 430 | currentObject.enterListInit(); |
---|
| 431 | // xxx - fix this so that the list isn't copied, iterator should be used to change current element |
---|
| 432 | std::list<Designation *> newDesignations; |
---|
| 433 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { |
---|
[62423350] | 434 | // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving |
---|
| 435 | // the initializer against that object. |
---|
[e4d829b] | 436 | Designation * des = std::get<0>(p); |
---|
| 437 | Initializer * init = std::get<1>(p); |
---|
| 438 | newDesignations.push_back( currentObject.findNext( des ) ); |
---|
| 439 | init->accept( *this ); |
---|
[b5c5684] | 440 | } |
---|
[62423350] | 441 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list |
---|
[e4d829b] | 442 | listInit->get_designations() = newDesignations; // xxx - memory management |
---|
| 443 | currentObject.exitListInit(); |
---|
| 444 | |
---|
[62423350] | 445 | // xxx - this part has not be folded into CurrentObject yet |
---|
[e4d829b] | 446 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
| 447 | // Type * base = tt->get_baseType()->get_base(); |
---|
| 448 | // if ( base ) { |
---|
| 449 | // // know the implementation type, so try using that as the initContext |
---|
| 450 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); |
---|
| 451 | // currentObject = &tmpObj; |
---|
| 452 | // visit( listInit ); |
---|
| 453 | // } else { |
---|
| 454 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
| 455 | // Parent::visit( listInit ); |
---|
| 456 | // } |
---|
| 457 | // } else { |
---|
[a32b204] | 458 | } |
---|
[71f4e4f] | 459 | |
---|
[f1e012b] | 460 | // ConstructorInit - fall back on C-style initializer |
---|
| 461 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
| 462 | // could not find valid constructor, or found an intrinsic constructor |
---|
| 463 | // fall back on C-style initializer |
---|
| 464 | delete ctorInit->get_ctor(); |
---|
| 465 | ctorInit->set_ctor( NULL ); |
---|
[71a145de] | 466 | delete ctorInit->get_dtor(); |
---|
| 467 | ctorInit->set_dtor( NULL ); |
---|
[f1e012b] | 468 | maybeAccept( ctorInit->get_init(), *this ); |
---|
| 469 | } |
---|
| 470 | |
---|
[1d2b64f] | 471 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
| 472 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
| 473 | assert( ctorInit ); |
---|
| 474 | Resolver resolver( indexer ); |
---|
| 475 | ctorInit->accept( resolver ); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
| 479 | assert( stmtExpr ); |
---|
| 480 | Resolver resolver( indexer ); |
---|
| 481 | stmtExpr->accept( resolver ); |
---|
| 482 | } |
---|
| 483 | |
---|
[71f4e4f] | 484 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
[1ba88a0] | 485 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
| 486 | maybeAccept( ctorInit->get_ctor(), *this ); |
---|
| 487 | maybeAccept( ctorInit->get_dtor(), *this ); |
---|
[071a31a] | 488 | |
---|
[5b2f5bb] | 489 | // found a constructor - can get rid of C-style initializer |
---|
| 490 | delete ctorInit->get_init(); |
---|
| 491 | ctorInit->set_init( NULL ); |
---|
[ec79847] | 492 | |
---|
| 493 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
| 494 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
| 495 | // to clean up generated code. |
---|
[f9cebb5] | 496 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
[ec79847] | 497 | delete ctorInit->get_ctor(); |
---|
| 498 | ctorInit->set_ctor( NULL ); |
---|
| 499 | } |
---|
[f9cebb5] | 500 | |
---|
| 501 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
[ec79847] | 502 | delete ctorInit->get_dtor(); |
---|
| 503 | ctorInit->set_dtor( NULL ); |
---|
| 504 | } |
---|
[a465caf] | 505 | |
---|
| 506 | // xxx - todo -- what about arrays? |
---|
| 507 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
| 508 | // // can reduce the constructor down to a SingleInit using the |
---|
| 509 | // // second argument from the ctor call, since |
---|
| 510 | // delete ctorInit->get_ctor(); |
---|
| 511 | // ctorInit->set_ctor( NULL ); |
---|
| 512 | |
---|
| 513 | // Expression * arg = |
---|
| 514 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
| 515 | // } |
---|
[71f4e4f] | 516 | } |
---|
[51b7345] | 517 | } // namespace ResolvExpr |
---|
[a32b204] | 518 | |
---|
| 519 | // Local Variables: // |
---|
| 520 | // tab-width: 4 // |
---|
| 521 | // mode: c++ // |
---|
| 522 | // compile-command: "make install" // |
---|
| 523 | // End: // |
---|