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