[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 ) {} |
---|
[1d2b64f] | 38 | Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) { |
---|
| 39 | if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) { |
---|
| 40 | functionReturn = res->functionReturn; |
---|
| 41 | initContext = res->initContext; |
---|
| 42 | inEnumDecl = res->inEnumDecl; |
---|
| 43 | } |
---|
| 44 | } |
---|
[71f4e4f] | 45 | |
---|
[1d2b64f] | 46 | typedef SymTab::Indexer Parent; |
---|
| 47 | using Parent::visit; |
---|
[62e5546] | 48 | virtual void visit( FunctionDecl *functionDecl ) override; |
---|
| 49 | virtual void visit( ObjectDecl *functionDecl ) override; |
---|
| 50 | virtual void visit( TypeDecl *typeDecl ) override; |
---|
| 51 | virtual void visit( EnumDecl * enumDecl ) override; |
---|
[94b4364] | 52 | |
---|
[62e5546] | 53 | virtual void visit( ArrayType * at ) override; |
---|
| 54 | virtual void visit( PointerType * at ) override; |
---|
[d9a0e76] | 55 | |
---|
[62e5546] | 56 | virtual void visit( ExprStmt *exprStmt ) override; |
---|
| 57 | virtual void visit( AsmExpr *asmExpr ) override; |
---|
| 58 | virtual void visit( AsmStmt *asmStmt ) override; |
---|
| 59 | virtual void visit( IfStmt *ifStmt ) override; |
---|
| 60 | virtual void visit( WhileStmt *whileStmt ) override; |
---|
| 61 | virtual void visit( ForStmt *forStmt ) override; |
---|
| 62 | virtual void visit( SwitchStmt *switchStmt ) override; |
---|
| 63 | virtual void visit( CaseStmt *caseStmt ) override; |
---|
| 64 | virtual void visit( BranchStmt *branchStmt ) override; |
---|
| 65 | virtual void visit( ReturnStmt *returnStmt ) override; |
---|
[d9a0e76] | 66 | |
---|
[62e5546] | 67 | virtual void visit( SingleInit *singleInit ) override; |
---|
| 68 | virtual void visit( ListInit *listInit ) override; |
---|
| 69 | virtual void visit( ConstructorInit *ctorInit ) override; |
---|
[a32b204] | 70 | private: |
---|
[94b4364] | 71 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
| 72 | |
---|
[40e636a] | 73 | template< typename PtrType > |
---|
| 74 | void handlePtrType( PtrType * type ); |
---|
| 75 | |
---|
[30b65d8] | 76 | void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & ); |
---|
| 77 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub ); |
---|
[f1e012b] | 78 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
[b726084] | 79 | |
---|
[77971f6] | 80 | Type * functionReturn = nullptr; |
---|
| 81 | Type *initContext = nullptr; |
---|
[a436947] | 82 | bool inEnumDecl = false; |
---|
[a32b204] | 83 | }; |
---|
[d9a0e76] | 84 | |
---|
[a32b204] | 85 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
| 86 | Resolver resolver; |
---|
| 87 | acceptAll( translationUnit, resolver ); |
---|
[d9a0e76] | 88 | #if 0 |
---|
[a32b204] | 89 | resolver.print( cerr ); |
---|
| 90 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 91 | (*i)->print( std::cerr ); |
---|
| 92 | (*i)->accept( resolver ); |
---|
| 93 | } // for |
---|
[d9a0e76] | 94 | #endif |
---|
| 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 |
---|
[a32b204] | 133 | assert( finder.get_alternatives().size() == 1 ); |
---|
| 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. |
---|
| 195 | Type *temp = initContext; |
---|
[a32b204] | 196 | initContext = new_type; |
---|
[a436947] | 197 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
| 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. |
---|
| 200 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); |
---|
| 201 | } |
---|
[1d2b64f] | 202 | Parent::visit( objectDecl ); |
---|
[a436947] | 203 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { |
---|
| 204 | // delete newly created signed int type |
---|
| 205 | delete initContext; |
---|
| 206 | } |
---|
[3cfe27f] | 207 | initContext = temp; |
---|
[bfbf97f] | 208 | } |
---|
| 209 | |
---|
[40e636a] | 210 | template< typename PtrType > |
---|
| 211 | void Resolver::handlePtrType( PtrType * type ) { |
---|
| 212 | if ( type->get_dimension() ) { |
---|
| 213 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); |
---|
[bfbf97f] | 214 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
[40e636a] | 215 | delete type->get_dimension(); |
---|
| 216 | type->set_dimension( newExpr ); |
---|
[d1d17f5] | 217 | } |
---|
[40e636a] | 218 | } |
---|
| 219 | |
---|
| 220 | void Resolver::visit( ArrayType * at ) { |
---|
| 221 | handlePtrType( at ); |
---|
[1d2b64f] | 222 | Parent::visit( at ); |
---|
[a32b204] | 223 | } |
---|
[94b4364] | 224 | |
---|
[40e636a] | 225 | void Resolver::visit( PointerType * pt ) { |
---|
| 226 | handlePtrType( pt ); |
---|
[1d2b64f] | 227 | Parent::visit( pt ); |
---|
[40e636a] | 228 | } |
---|
| 229 | |
---|
[a32b204] | 230 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 231 | if ( typeDecl->get_base() ) { |
---|
| 232 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 233 | typeDecl->set_base( new_type ); |
---|
| 234 | } // if |
---|
[1d2b64f] | 235 | Parent::visit( typeDecl ); |
---|
[a32b204] | 236 | } |
---|
[94b4364] | 237 | |
---|
[a32b204] | 238 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
[d9a0e76] | 239 | #if 0 |
---|
[a32b204] | 240 | std::cout << "resolver visiting functiondecl "; |
---|
| 241 | functionDecl->print( std::cout ); |
---|
| 242 | std::cout << std::endl; |
---|
[d9a0e76] | 243 | #endif |
---|
[a32b204] | 244 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 245 | functionDecl->set_type( new_type ); |
---|
[906e24d] | 246 | ValueGuard< Type * > oldFunctionReturn( functionReturn ); |
---|
| 247 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); |
---|
[1d2b64f] | 248 | Parent::visit( functionDecl ); |
---|
[a32b204] | 249 | } |
---|
[51b7345] | 250 | |
---|
[a436947] | 251 | void Resolver::visit( EnumDecl * enumDecl ) { |
---|
| 252 | // in case we decide to allow nested enums |
---|
[1d2b64f] | 253 | ValueGuard< bool > oldInEnumDecl( inEnumDecl ); |
---|
[a436947] | 254 | inEnumDecl = true; |
---|
[1d2b64f] | 255 | Parent::visit( enumDecl ); |
---|
[a436947] | 256 | } |
---|
| 257 | |
---|
[a32b204] | 258 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
[1d2b64f] | 259 | assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" ); |
---|
| 260 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
| 261 | delete exprStmt->get_expr(); |
---|
| 262 | exprStmt->set_expr( newExpr ); |
---|
[a32b204] | 263 | } |
---|
[51b7345] | 264 | |
---|
[7f5566b] | 265 | void Resolver::visit( AsmExpr *asmExpr ) { |
---|
| 266 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); |
---|
| 267 | delete asmExpr->get_operand(); |
---|
| 268 | asmExpr->set_operand( newExpr ); |
---|
| 269 | if ( asmExpr->get_inout() ) { |
---|
| 270 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); |
---|
| 271 | delete asmExpr->get_inout(); |
---|
| 272 | asmExpr->set_inout( newExpr ); |
---|
| 273 | } // if |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | void Resolver::visit( AsmStmt *asmStmt ) { |
---|
| 277 | acceptAll( asmStmt->get_input(), *this); |
---|
| 278 | acceptAll( asmStmt->get_output(), *this); |
---|
| 279 | } |
---|
| 280 | |
---|
[a32b204] | 281 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
| 282 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
| 283 | delete ifStmt->get_condition(); |
---|
| 284 | ifStmt->set_condition( newExpr ); |
---|
[1d2b64f] | 285 | Parent::visit( ifStmt ); |
---|
[a32b204] | 286 | } |
---|
[51b7345] | 287 | |
---|
[a32b204] | 288 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
| 289 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
| 290 | delete whileStmt->get_condition(); |
---|
| 291 | whileStmt->set_condition( newExpr ); |
---|
[1d2b64f] | 292 | Parent::visit( whileStmt ); |
---|
[a32b204] | 293 | } |
---|
[51b7345] | 294 | |
---|
[a32b204] | 295 | void Resolver::visit( ForStmt *forStmt ) { |
---|
[1d2b64f] | 296 | Parent::visit( forStmt ); |
---|
[145f1fc] | 297 | |
---|
[a32b204] | 298 | if ( forStmt->get_condition() ) { |
---|
[145f1fc] | 299 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
[a32b204] | 300 | delete forStmt->get_condition(); |
---|
| 301 | forStmt->set_condition( newExpr ); |
---|
| 302 | } // if |
---|
[71f4e4f] | 303 | |
---|
[a32b204] | 304 | if ( forStmt->get_increment() ) { |
---|
[145f1fc] | 305 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
[a32b204] | 306 | delete forStmt->get_increment(); |
---|
| 307 | forStmt->set_increment( newExpr ); |
---|
| 308 | } // if |
---|
| 309 | } |
---|
[51b7345] | 310 | |
---|
[a32b204] | 311 | template< typename SwitchClass > |
---|
| 312 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
| 313 | Expression *newExpr; |
---|
| 314 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
| 315 | delete switchStmt->get_condition(); |
---|
| 316 | switchStmt->set_condition( newExpr ); |
---|
[71f4e4f] | 317 | |
---|
[a32b204] | 318 | visitor.Visitor::visit( switchStmt ); |
---|
| 319 | } |
---|
[51b7345] | 320 | |
---|
[a32b204] | 321 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
| 322 | handleSwitchStmt( switchStmt, *this ); |
---|
| 323 | } |
---|
[51b7345] | 324 | |
---|
[a32b204] | 325 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
[1d2b64f] | 326 | Parent::visit( caseStmt ); |
---|
[a32b204] | 327 | } |
---|
[51b7345] | 328 | |
---|
[de62360d] | 329 | void Resolver::visit( BranchStmt *branchStmt ) { |
---|
| 330 | // must resolve the argument for a computed goto |
---|
| 331 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
[2871210] | 332 | if ( Expression * arg = branchStmt->get_computedTarget() ) { |
---|
[de62360d] | 333 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder |
---|
| 334 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
| 335 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); |
---|
| 336 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression |
---|
| 337 | branchStmt->set_target( newExpr ); |
---|
| 338 | } // if |
---|
| 339 | } // if |
---|
| 340 | } |
---|
| 341 | |
---|
[a32b204] | 342 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
| 343 | if ( returnStmt->get_expr() ) { |
---|
[906e24d] | 344 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); |
---|
[a32b204] | 345 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 346 | delete castExpr; |
---|
| 347 | returnStmt->set_expr( newExpr ); |
---|
| 348 | } // if |
---|
| 349 | } |
---|
[51b7345] | 350 | |
---|
[b5c5684] | 351 | template< typename T > |
---|
| 352 | bool isCharType( T t ) { |
---|
| 353 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
[71f4e4f] | 354 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
[b5c5684] | 355 | bt->get_kind() == BasicType::UnsignedChar; |
---|
| 356 | } |
---|
| 357 | return false; |
---|
| 358 | } |
---|
| 359 | |
---|
[a32b204] | 360 | void Resolver::visit( SingleInit *singleInit ) { |
---|
| 361 | if ( singleInit->get_value() ) { |
---|
[bdd516a] | 362 | #if 0 |
---|
[a32b204] | 363 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
| 364 | string n = ne->get_name(); |
---|
| 365 | if (n == "0") { |
---|
[71f4e4f] | 366 | initContext = new BasicType(Type::Qualifiers(), |
---|
[a32b204] | 367 | BasicType::SignedInt); |
---|
| 368 | } else { |
---|
[52f85e0] | 369 | DeclarationWithType * decl = lookupId( n ); |
---|
[a32b204] | 370 | initContext = decl->get_type(); |
---|
| 371 | } |
---|
[71f4e4f] | 372 | } else if (ConstantExpr * e = |
---|
[a32b204] | 373 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
| 374 | Constant *c = e->get_constant(); |
---|
| 375 | initContext = c->get_type(); |
---|
| 376 | } else { |
---|
| 377 | assert(0); |
---|
| 378 | } |
---|
[bdd516a] | 379 | #endif |
---|
[a32b204] | 380 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
| 381 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 382 | delete castExpr; |
---|
| 383 | singleInit->set_value( newExpr ); |
---|
[b5c5684] | 384 | |
---|
| 385 | // check if initializing type is char[] |
---|
| 386 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
| 387 | if ( isCharType( at->get_base() ) ) { |
---|
| 388 | // check if the resolved type is char * |
---|
[906e24d] | 389 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
[b5c5684] | 390 | if ( isCharType( pt->get_base() ) ) { |
---|
[52f85e0] | 391 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
[b5c5684] | 392 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); |
---|
| 393 | singleInit->set_value( ce->get_arg() ); |
---|
| 394 | ce->set_arg( NULL ); |
---|
[71f4e4f] | 395 | delete ce; |
---|
[b5c5684] | 396 | } |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | } |
---|
[a32b204] | 400 | } // if |
---|
[6c3744e] | 401 | // singleInit->get_value()->accept( *this ); |
---|
[a32b204] | 402 | } |
---|
[51b7345] | 403 | |
---|
[30b65d8] | 404 | template< typename AggrInst > |
---|
| 405 | TypeSubstitution makeGenericSubstitutuion( AggrInst * inst ) { |
---|
[66f8528] | 406 | assert( inst ); |
---|
| 407 | assert( inst->get_baseParameters() ); |
---|
[30b65d8] | 408 | std::list< TypeDecl * > baseParams = *inst->get_baseParameters(); |
---|
| 409 | std::list< Expression * > typeSubs = inst->get_parameters(); |
---|
| 410 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); |
---|
| 411 | return subs; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | ReferenceToType * isStructOrUnion( Type * type ) { |
---|
| 415 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( type ) ) { |
---|
| 416 | return sit; |
---|
| 417 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( type ) ) { |
---|
| 418 | return uit; |
---|
| 419 | } |
---|
| 420 | return nullptr; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd, TypeSubstitution sub ) { |
---|
[94b4364] | 424 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); |
---|
| 425 | assert( dt ); |
---|
[30b65d8] | 426 | // need to substitute for generic types, so that casts are to concrete types |
---|
| 427 | initContext = dt->get_type()->clone(); |
---|
| 428 | sub.apply( initContext ); |
---|
| 429 | |
---|
[94b4364] | 430 | try { |
---|
| 431 | if ( init == initEnd ) return; // stop when there are no more initializers |
---|
| 432 | (*init)->accept( *this ); |
---|
| 433 | ++init; // made it past an initializer |
---|
| 434 | } catch( SemanticError & ) { |
---|
| 435 | // need to delve deeper, if you can |
---|
[30b65d8] | 436 | if ( ReferenceToType * type = isStructOrUnion( initContext ) ) { |
---|
| 437 | resolveAggrInit( type, init, initEnd ); |
---|
[94b4364] | 438 | } else { |
---|
[1869adf] | 439 | // member is not an aggregate type, so can't go any deeper |
---|
| 440 | |
---|
[94b4364] | 441 | // might need to rethink what is being thrown |
---|
| 442 | throw; |
---|
| 443 | } // if |
---|
| 444 | } |
---|
| 445 | } |
---|
| 446 | |
---|
[30b65d8] | 447 | void Resolver::resolveAggrInit( ReferenceToType * inst, InitIterator & init, InitIterator & initEnd ) { |
---|
| 448 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( inst ) ) { |
---|
| 449 | TypeSubstitution sub = makeGenericSubstitutuion( sit ); |
---|
| 450 | StructDecl * st = sit->get_baseStruct(); |
---|
[94b4364] | 451 | // want to resolve each initializer to the members of the struct, |
---|
| 452 | // but if there are more initializers than members we should stop |
---|
| 453 | list< Declaration * >::iterator it = st->get_members().begin(); |
---|
| 454 | for ( ; it != st->get_members().end(); ++it) { |
---|
[30b65d8] | 455 | resolveSingleAggrInit( *it, init, initEnd, sub ); |
---|
[94b4364] | 456 | } |
---|
[30b65d8] | 457 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( inst ) ) { |
---|
[66f8528] | 458 | TypeSubstitution sub = makeGenericSubstitutuion( uit ); |
---|
[30b65d8] | 459 | UnionDecl * un = uit->get_baseUnion(); |
---|
[94b4364] | 460 | // only resolve to the first member of a union |
---|
[30b65d8] | 461 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd, sub ); |
---|
[94b4364] | 462 | } // if |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | void Resolver::visit( ListInit * listInit ) { |
---|
[4d2434a] | 466 | InitIterator iter = listInit->begin(); |
---|
| 467 | InitIterator end = listInit->end(); |
---|
[94b4364] | 468 | |
---|
[b5c5684] | 469 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
[94b4364] | 470 | // resolve each member to the base type of the array |
---|
| 471 | for ( ; iter != end; ++iter ) { |
---|
[b5c5684] | 472 | initContext = at->get_base(); |
---|
| 473 | (*iter)->accept( *this ); |
---|
| 474 | } // for |
---|
[65660bd] | 475 | } else if ( TupleType * tt = dynamic_cast< TupleType * > ( initContext ) ) { |
---|
| 476 | for ( Type * t : *tt ) { |
---|
| 477 | if ( iter == end ) break; |
---|
| 478 | initContext = t; |
---|
| 479 | (*iter++)->accept( *this ); |
---|
| 480 | } |
---|
[30b65d8] | 481 | } else if ( ReferenceToType * type = isStructOrUnion( initContext ) ) { |
---|
| 482 | resolveAggrInit( type, iter, end ); |
---|
[679864e1] | 483 | } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
[242d458] | 484 | Type * base = tt->get_baseType()->get_base(); |
---|
| 485 | if ( base ) { |
---|
| 486 | // know the implementation type, so try using that as the initContext |
---|
| 487 | initContext = base; |
---|
| 488 | visit( listInit ); |
---|
| 489 | } else { |
---|
| 490 | // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
[1d2b64f] | 491 | Parent::visit( listInit ); |
---|
[242d458] | 492 | } |
---|
[b5c5684] | 493 | } else { |
---|
[89e6ffc] | 494 | assert( dynamic_cast< BasicType * >( initContext ) || dynamic_cast< PointerType * >( initContext ) |
---|
[1d2b64f] | 495 | || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) || dynamic_cast < EnumInstType * > ( initContext ) ); |
---|
[b5c5684] | 496 | // basic types are handled here |
---|
[1d2b64f] | 497 | Parent::visit( listInit ); |
---|
[b5c5684] | 498 | } |
---|
| 499 | |
---|
[bdd516a] | 500 | #if 0 |
---|
[a32b204] | 501 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
| 502 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
| 503 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
| 504 | initContext = at->get_base(); |
---|
| 505 | (*iter)->accept( *this ); |
---|
| 506 | } // for |
---|
| 507 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
| 508 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
| 509 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
| 510 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
| 511 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
| 512 | if ( (*iter2)->get_designators().empty() ) { |
---|
| 513 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
| 514 | initContext = dt->get_type(); |
---|
| 515 | (*iter2)->accept( *this ); |
---|
| 516 | ++iter1; |
---|
| 517 | } else { |
---|
| 518 | StructDecl *st = baseStruct; |
---|
| 519 | iter1 = st->get_members().begin(); |
---|
| 520 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
| 521 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
| 522 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
| 523 | assert( key ); |
---|
| 524 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
| 525 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
| 526 | (*iter1)->print( cout ); |
---|
| 527 | cout << key->get_name() << endl; |
---|
| 528 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 529 | assert( fred ); |
---|
| 530 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
| 531 | assert( mary ); |
---|
| 532 | st = mary->get_baseStruct(); |
---|
| 533 | iter1 = st->get_members().begin(); |
---|
| 534 | break; |
---|
| 535 | } // if |
---|
| 536 | } // for |
---|
| 537 | } // for |
---|
| 538 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 539 | assert( fred ); |
---|
| 540 | initContext = fred->get_type(); |
---|
| 541 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 542 | } // if |
---|
| 543 | } // for |
---|
| 544 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
| 545 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
| 546 | initContext = dt->get_type(); |
---|
| 547 | (*listInit->begin_initializers())->accept( *this ); |
---|
[2c2242c] | 548 | } // if |
---|
[bdd516a] | 549 | #endif |
---|
[a32b204] | 550 | } |
---|
[71f4e4f] | 551 | |
---|
[f1e012b] | 552 | // ConstructorInit - fall back on C-style initializer |
---|
| 553 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
| 554 | // could not find valid constructor, or found an intrinsic constructor |
---|
| 555 | // fall back on C-style initializer |
---|
| 556 | delete ctorInit->get_ctor(); |
---|
| 557 | ctorInit->set_ctor( NULL ); |
---|
[71a145de] | 558 | delete ctorInit->get_dtor(); |
---|
| 559 | ctorInit->set_dtor( NULL ); |
---|
[f1e012b] | 560 | maybeAccept( ctorInit->get_init(), *this ); |
---|
| 561 | } |
---|
| 562 | |
---|
[1d2b64f] | 563 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
| 564 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
| 565 | assert( ctorInit ); |
---|
| 566 | Resolver resolver( indexer ); |
---|
| 567 | ctorInit->accept( resolver ); |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
| 571 | assert( stmtExpr ); |
---|
| 572 | Resolver resolver( indexer ); |
---|
| 573 | stmtExpr->accept( resolver ); |
---|
| 574 | } |
---|
| 575 | |
---|
[71f4e4f] | 576 | void Resolver::visit( ConstructorInit *ctorInit ) { |
---|
[1ba88a0] | 577 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
| 578 | maybeAccept( ctorInit->get_ctor(), *this ); |
---|
| 579 | maybeAccept( ctorInit->get_dtor(), *this ); |
---|
[071a31a] | 580 | |
---|
[5b2f5bb] | 581 | // found a constructor - can get rid of C-style initializer |
---|
| 582 | delete ctorInit->get_init(); |
---|
| 583 | ctorInit->set_init( NULL ); |
---|
[ec79847] | 584 | |
---|
| 585 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
| 586 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
| 587 | // to clean up generated code. |
---|
[f9cebb5] | 588 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
[ec79847] | 589 | delete ctorInit->get_ctor(); |
---|
| 590 | ctorInit->set_ctor( NULL ); |
---|
| 591 | } |
---|
[f9cebb5] | 592 | |
---|
| 593 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
[ec79847] | 594 | delete ctorInit->get_dtor(); |
---|
| 595 | ctorInit->set_dtor( NULL ); |
---|
| 596 | } |
---|
[a465caf] | 597 | |
---|
| 598 | // xxx - todo -- what about arrays? |
---|
| 599 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
| 600 | // // can reduce the constructor down to a SingleInit using the |
---|
| 601 | // // second argument from the ctor call, since |
---|
| 602 | // delete ctorInit->get_ctor(); |
---|
| 603 | // ctorInit->set_ctor( NULL ); |
---|
| 604 | |
---|
| 605 | // Expression * arg = |
---|
| 606 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
| 607 | // } |
---|
[71f4e4f] | 608 | } |
---|
[51b7345] | 609 | } // namespace ResolvExpr |
---|
[a32b204] | 610 | |
---|
| 611 | // Local Variables: // |
---|
| 612 | // tab-width: 4 // |
---|
| 613 | // mode: c++ // |
---|
| 614 | // compile-command: "make install" // |
---|
| 615 | // End: // |
---|