| 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 | // | 
|---|
| 7 | // Resolver.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sun May 17 12:17:01 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue Jul 12 17:45:42 2016 | 
|---|
| 13 | // Update Count     : 204 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "Resolver.h" | 
|---|
| 17 | #include "AlternativeFinder.h" | 
|---|
| 18 | #include "Alternative.h" | 
|---|
| 19 | #include "RenameVars.h" | 
|---|
| 20 | #include "ResolveTypeof.h" | 
|---|
| 21 | #include "SynTree/Statement.h" | 
|---|
| 22 | #include "SynTree/Type.h" | 
|---|
| 23 | #include "SynTree/Expression.h" | 
|---|
| 24 | #include "SynTree/Initializer.h" | 
|---|
| 25 | #include "SymTab/Indexer.h" | 
|---|
| 26 | #include "Common/utility.h" | 
|---|
| 27 | #include "InitTweak/InitTweak.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include <iostream> | 
|---|
| 30 | using namespace std; | 
|---|
| 31 |  | 
|---|
| 32 | namespace ResolvExpr { | 
|---|
| 33 | class Resolver : public SymTab::Indexer { | 
|---|
| 34 | public: | 
|---|
| 35 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} | 
|---|
| 36 |  | 
|---|
| 37 | virtual void visit( FunctionDecl *functionDecl ); | 
|---|
| 38 | virtual void visit( ObjectDecl *functionDecl ); | 
|---|
| 39 | virtual void visit( TypeDecl *typeDecl ); | 
|---|
| 40 | virtual void visit( EnumDecl * enumDecl ); | 
|---|
| 41 |  | 
|---|
| 42 | virtual void visit( ArrayType * at ); | 
|---|
| 43 |  | 
|---|
| 44 | virtual void visit( ExprStmt *exprStmt ); | 
|---|
| 45 | virtual void visit( AsmExpr *asmExpr ); | 
|---|
| 46 | virtual void visit( AsmStmt *asmStmt ); | 
|---|
| 47 | virtual void visit( IfStmt *ifStmt ); | 
|---|
| 48 | virtual void visit( WhileStmt *whileStmt ); | 
|---|
| 49 | virtual void visit( ForStmt *forStmt ); | 
|---|
| 50 | virtual void visit( SwitchStmt *switchStmt ); | 
|---|
| 51 | virtual void visit( CaseStmt *caseStmt ); | 
|---|
| 52 | virtual void visit( BranchStmt *branchStmt ); | 
|---|
| 53 | virtual void visit( ReturnStmt *returnStmt ); | 
|---|
| 54 | virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt ); | 
|---|
| 55 |  | 
|---|
| 56 | virtual void visit( SingleInit *singleInit ); | 
|---|
| 57 | virtual void visit( ListInit *listInit ); | 
|---|
| 58 | virtual void visit( ConstructorInit *ctorInit ); | 
|---|
| 59 | private: | 
|---|
| 60 | typedef std::list< Initializer * >::iterator InitIterator; | 
|---|
| 61 |  | 
|---|
| 62 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); | 
|---|
| 63 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); | 
|---|
| 64 | void fallbackInit( ConstructorInit * ctorInit ); | 
|---|
| 65 | std::list< Type * > functionReturn; | 
|---|
| 66 | Type *initContext; | 
|---|
| 67 | Type *switchType; | 
|---|
| 68 | bool inEnumDecl = false; | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | void resolve( std::list< Declaration * > translationUnit ) { | 
|---|
| 72 | Resolver resolver; | 
|---|
| 73 | acceptAll( translationUnit, resolver ); | 
|---|
| 74 | #if 0 | 
|---|
| 75 | resolver.print( cerr ); | 
|---|
| 76 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { | 
|---|
| 77 | (*i)->print( std::cerr ); | 
|---|
| 78 | (*i)->accept( resolver ); | 
|---|
| 79 | } // for | 
|---|
| 80 | #endif | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { | 
|---|
| 84 | TypeEnvironment env; | 
|---|
| 85 | return resolveInVoidContext( expr, indexer, env ); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | namespace { | 
|---|
| 90 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { | 
|---|
| 91 | expr->set_env( new TypeSubstitution ); | 
|---|
| 92 | env.makeSubstitution( *expr->get_env() ); | 
|---|
| 93 | } | 
|---|
| 94 | } // namespace | 
|---|
| 95 |  | 
|---|
| 96 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
| 97 | global_renamer.reset(); | 
|---|
| 98 | TypeEnvironment env; | 
|---|
| 99 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); | 
|---|
| 100 | finishExpr( newExpr, env ); | 
|---|
| 101 | return newExpr; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | namespace { | 
|---|
| 105 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
| 106 | TypeEnvironment env; | 
|---|
| 107 | AlternativeFinder finder( indexer, env ); | 
|---|
| 108 | finder.find( untyped ); | 
|---|
| 109 | #if 0 | 
|---|
| 110 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
| 111 | std::cout << "untyped expr is "; | 
|---|
| 112 | untyped->print( std::cout ); | 
|---|
| 113 | std::cout << std::endl << "alternatives are:"; | 
|---|
| 114 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
| 115 | i->print( std::cout ); | 
|---|
| 116 | } // for | 
|---|
| 117 | } // if | 
|---|
| 118 | #endif | 
|---|
| 119 | assert( finder.get_alternatives().size() == 1 ); | 
|---|
| 120 | Alternative &choice = finder.get_alternatives().front(); | 
|---|
| 121 | Expression *newExpr = choice.expr->clone(); | 
|---|
| 122 | finishExpr( newExpr, choice.env ); | 
|---|
| 123 | return newExpr; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | bool isIntegralType( Type *type ) { | 
|---|
| 127 | if ( dynamic_cast< EnumInstType * >( type ) ) { | 
|---|
| 128 | return true; | 
|---|
| 129 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { | 
|---|
| 130 | return bt->isInteger(); | 
|---|
| 131 | } else { | 
|---|
| 132 | return false; | 
|---|
| 133 | } // if | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
| 137 | TypeEnvironment env; | 
|---|
| 138 | AlternativeFinder finder( indexer, env ); | 
|---|
| 139 | finder.find( untyped ); | 
|---|
| 140 | #if 0 | 
|---|
| 141 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
| 142 | std::cout << "untyped expr is "; | 
|---|
| 143 | untyped->print( std::cout ); | 
|---|
| 144 | std::cout << std::endl << "alternatives are:"; | 
|---|
| 145 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
| 146 | i->print( std::cout ); | 
|---|
| 147 | } // for | 
|---|
| 148 | } // if | 
|---|
| 149 | #endif | 
|---|
| 150 | Expression *newExpr = 0; | 
|---|
| 151 | const TypeEnvironment *newEnv = 0; | 
|---|
| 152 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
| 153 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { | 
|---|
| 154 | if ( newExpr ) { | 
|---|
| 155 | throw SemanticError( "Too many interpretations for case control expression", untyped ); | 
|---|
| 156 | } else { | 
|---|
| 157 | newExpr = i->expr->clone(); | 
|---|
| 158 | newEnv = &i->env; | 
|---|
| 159 | } // if | 
|---|
| 160 | } // if | 
|---|
| 161 | } // for | 
|---|
| 162 | if ( ! newExpr ) { | 
|---|
| 163 | throw SemanticError( "No interpretations for case control expression", untyped ); | 
|---|
| 164 | } // if | 
|---|
| 165 | finishExpr( newExpr, *newEnv ); | 
|---|
| 166 | return newExpr; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | void Resolver::visit( ObjectDecl *objectDecl ) { | 
|---|
| 172 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); | 
|---|
| 173 | objectDecl->set_type( new_type ); | 
|---|
| 174 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable | 
|---|
| 175 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes | 
|---|
| 176 | // initContext because of a function type can contain object declarations in the return and parameter types. So | 
|---|
| 177 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting | 
|---|
| 178 | // the RHS. | 
|---|
| 179 | Type *temp = initContext; | 
|---|
| 180 | initContext = new_type; | 
|---|
| 181 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { | 
|---|
| 182 | // enumerator initializers should not use the enum type to initialize, since | 
|---|
| 183 | // the enum type is still incomplete at this point. Use signed int instead. | 
|---|
| 184 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); | 
|---|
| 185 | } | 
|---|
| 186 | SymTab::Indexer::visit( objectDecl ); | 
|---|
| 187 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { | 
|---|
| 188 | // delete newly created signed int type | 
|---|
| 189 | delete initContext; | 
|---|
| 190 | } | 
|---|
| 191 | initContext = temp; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | void Resolver::visit( ArrayType * at ) { | 
|---|
| 195 | if ( at->get_dimension() ) { | 
|---|
| 196 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
| 197 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); | 
|---|
| 198 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
| 199 | delete at->get_dimension(); | 
|---|
| 200 | at->set_dimension( newExpr ); | 
|---|
| 201 | } | 
|---|
| 202 | Visitor::visit( at ); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | void Resolver::visit( TypeDecl *typeDecl ) { | 
|---|
| 206 | if ( typeDecl->get_base() ) { | 
|---|
| 207 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); | 
|---|
| 208 | typeDecl->set_base( new_type ); | 
|---|
| 209 | } // if | 
|---|
| 210 | SymTab::Indexer::visit( typeDecl ); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | void Resolver::visit( FunctionDecl *functionDecl ) { | 
|---|
| 214 | #if 0 | 
|---|
| 215 | std::cout << "resolver visiting functiondecl "; | 
|---|
| 216 | functionDecl->print( std::cout ); | 
|---|
| 217 | std::cout << std::endl; | 
|---|
| 218 | #endif | 
|---|
| 219 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); | 
|---|
| 220 | functionDecl->set_type( new_type ); | 
|---|
| 221 | std::list< Type * > oldFunctionReturn = functionReturn; | 
|---|
| 222 | functionReturn.clear(); | 
|---|
| 223 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { | 
|---|
| 224 | functionReturn.push_back( (*i)->get_type() ); | 
|---|
| 225 | } // for | 
|---|
| 226 | SymTab::Indexer::visit( functionDecl ); | 
|---|
| 227 | functionReturn = oldFunctionReturn; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | void Resolver::visit( EnumDecl * enumDecl ) { | 
|---|
| 231 | // in case we decide to allow nested enums | 
|---|
| 232 | bool oldInEnumDecl = inEnumDecl; | 
|---|
| 233 | inEnumDecl = true; | 
|---|
| 234 | SymTab::Indexer::visit( enumDecl ); | 
|---|
| 235 | inEnumDecl = oldInEnumDecl; | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | void Resolver::visit( ExprStmt *exprStmt ) { | 
|---|
| 239 | if ( exprStmt->get_expr() ) { | 
|---|
| 240 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); | 
|---|
| 241 | delete exprStmt->get_expr(); | 
|---|
| 242 | exprStmt->set_expr( newExpr ); | 
|---|
| 243 | } // if | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | void Resolver::visit( AsmExpr *asmExpr ) { | 
|---|
| 247 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); | 
|---|
| 248 | delete asmExpr->get_operand(); | 
|---|
| 249 | asmExpr->set_operand( newExpr ); | 
|---|
| 250 | if ( asmExpr->get_inout() ) { | 
|---|
| 251 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); | 
|---|
| 252 | delete asmExpr->get_inout(); | 
|---|
| 253 | asmExpr->set_inout( newExpr ); | 
|---|
| 254 | } // if | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | void Resolver::visit( AsmStmt *asmStmt ) { | 
|---|
| 258 | acceptAll( asmStmt->get_input(), *this); | 
|---|
| 259 | acceptAll( asmStmt->get_output(), *this); | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | void Resolver::visit( IfStmt *ifStmt ) { | 
|---|
| 263 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); | 
|---|
| 264 | delete ifStmt->get_condition(); | 
|---|
| 265 | ifStmt->set_condition( newExpr ); | 
|---|
| 266 | Visitor::visit( ifStmt ); | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | void Resolver::visit( WhileStmt *whileStmt ) { | 
|---|
| 270 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); | 
|---|
| 271 | delete whileStmt->get_condition(); | 
|---|
| 272 | whileStmt->set_condition( newExpr ); | 
|---|
| 273 | Visitor::visit( whileStmt ); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | void Resolver::visit( ForStmt *forStmt ) { | 
|---|
| 277 | SymTab::Indexer::visit( forStmt ); | 
|---|
| 278 |  | 
|---|
| 279 | if ( forStmt->get_condition() ) { | 
|---|
| 280 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); | 
|---|
| 281 | delete forStmt->get_condition(); | 
|---|
| 282 | forStmt->set_condition( newExpr ); | 
|---|
| 283 | } // if | 
|---|
| 284 |  | 
|---|
| 285 | if ( forStmt->get_increment() ) { | 
|---|
| 286 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); | 
|---|
| 287 | delete forStmt->get_increment(); | 
|---|
| 288 | forStmt->set_increment( newExpr ); | 
|---|
| 289 | } // if | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | template< typename SwitchClass > | 
|---|
| 293 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { | 
|---|
| 294 | Expression *newExpr; | 
|---|
| 295 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); | 
|---|
| 296 | delete switchStmt->get_condition(); | 
|---|
| 297 | switchStmt->set_condition( newExpr ); | 
|---|
| 298 |  | 
|---|
| 299 | visitor.Visitor::visit( switchStmt ); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | void Resolver::visit( SwitchStmt *switchStmt ) { | 
|---|
| 303 | handleSwitchStmt( switchStmt, *this ); | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | void Resolver::visit( CaseStmt *caseStmt ) { | 
|---|
| 307 | Visitor::visit( caseStmt ); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | void Resolver::visit( BranchStmt *branchStmt ) { | 
|---|
| 311 | // must resolve the argument for a computed goto | 
|---|
| 312 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement | 
|---|
| 313 | if ( Expression * arg = branchStmt->get_computedTarget() ) { | 
|---|
| 314 | VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder | 
|---|
| 315 | PointerType pt( Type::Qualifiers(), v.clone() ); | 
|---|
| 316 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); | 
|---|
| 317 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression | 
|---|
| 318 | branchStmt->set_target( newExpr ); | 
|---|
| 319 | } // if | 
|---|
| 320 | } // if | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | void Resolver::visit( ReturnStmt *returnStmt ) { | 
|---|
| 324 | if ( returnStmt->get_expr() ) { | 
|---|
| 325 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); | 
|---|
| 326 | cloneAll( functionReturn, castExpr->get_results() ); | 
|---|
| 327 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
| 328 | delete castExpr; | 
|---|
| 329 | returnStmt->set_expr( newExpr ); | 
|---|
| 330 | } // if | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | template< typename T > | 
|---|
| 334 | bool isCharType( T t ) { | 
|---|
| 335 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { | 
|---|
| 336 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || | 
|---|
| 337 | bt->get_kind() == BasicType::UnsignedChar; | 
|---|
| 338 | } | 
|---|
| 339 | return false; | 
|---|
| 340 | } | 
|---|
| 341 |  | 
|---|
| 342 | void Resolver::visit( SingleInit *singleInit ) { | 
|---|
| 343 | if ( singleInit->get_value() ) { | 
|---|
| 344 | #if 0 | 
|---|
| 345 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { | 
|---|
| 346 | string n = ne->get_name(); | 
|---|
| 347 | if (n == "0") { | 
|---|
| 348 | initContext = new BasicType(Type::Qualifiers(), | 
|---|
| 349 | BasicType::SignedInt); | 
|---|
| 350 | } else { | 
|---|
| 351 | DeclarationWithType * decl = lookupId( n ); | 
|---|
| 352 | initContext = decl->get_type(); | 
|---|
| 353 | } | 
|---|
| 354 | } else if (ConstantExpr * e = | 
|---|
| 355 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { | 
|---|
| 356 | Constant *c = e->get_constant(); | 
|---|
| 357 | initContext = c->get_type(); | 
|---|
| 358 | } else { | 
|---|
| 359 | assert(0); | 
|---|
| 360 | } | 
|---|
| 361 | #endif | 
|---|
| 362 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); | 
|---|
| 363 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
| 364 | delete castExpr; | 
|---|
| 365 | singleInit->set_value( newExpr ); | 
|---|
| 366 |  | 
|---|
| 367 | // check if initializing type is char[] | 
|---|
| 368 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
| 369 | if ( isCharType( at->get_base() ) ) { | 
|---|
| 370 | // check if the resolved type is char * | 
|---|
| 371 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { | 
|---|
| 372 | if ( isCharType( pt->get_base() ) ) { | 
|---|
| 373 | // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello"; | 
|---|
| 374 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); | 
|---|
| 375 | singleInit->set_value( ce->get_arg() ); | 
|---|
| 376 | ce->set_arg( NULL ); | 
|---|
| 377 | delete ce; | 
|---|
| 378 | } | 
|---|
| 379 | } | 
|---|
| 380 | } | 
|---|
| 381 | } | 
|---|
| 382 | } // if | 
|---|
| 383 | //      singleInit->get_value()->accept( *this ); | 
|---|
| 384 | } | 
|---|
| 385 |  | 
|---|
| 386 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { | 
|---|
| 387 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); | 
|---|
| 388 | assert( dt ); | 
|---|
| 389 | initContext = dt->get_type(); | 
|---|
| 390 | try { | 
|---|
| 391 | if ( init == initEnd ) return; // stop when there are no more initializers | 
|---|
| 392 | (*init)->accept( *this ); | 
|---|
| 393 | ++init; // made it past an initializer | 
|---|
| 394 | } catch( SemanticError & ) { | 
|---|
| 395 | // need to delve deeper, if you can | 
|---|
| 396 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { | 
|---|
| 397 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); | 
|---|
| 398 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { | 
|---|
| 399 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); | 
|---|
| 400 | } else { | 
|---|
| 401 | // member is not an aggregate type, so can't go any deeper | 
|---|
| 402 |  | 
|---|
| 403 | // might need to rethink what is being thrown | 
|---|
| 404 | throw; | 
|---|
| 405 | } // if | 
|---|
| 406 | } | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { | 
|---|
| 410 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { | 
|---|
| 411 | // want to resolve each initializer to the members of the struct, | 
|---|
| 412 | // but if there are more initializers than members we should stop | 
|---|
| 413 | list< Declaration * >::iterator it = st->get_members().begin(); | 
|---|
| 414 | for ( ; it != st->get_members().end(); ++it) { | 
|---|
| 415 | resolveSingleAggrInit( *it, init, initEnd ); | 
|---|
| 416 | } | 
|---|
| 417 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { | 
|---|
| 418 | // only resolve to the first member of a union | 
|---|
| 419 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); | 
|---|
| 420 | } // if | 
|---|
| 421 | } | 
|---|
| 422 |  | 
|---|
| 423 | void Resolver::visit( ListInit * listInit ) { | 
|---|
| 424 | InitIterator iter = listInit->begin_initializers(); | 
|---|
| 425 | InitIterator end = listInit->end_initializers(); | 
|---|
| 426 |  | 
|---|
| 427 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
| 428 | // resolve each member to the base type of the array | 
|---|
| 429 | for ( ; iter != end; ++iter ) { | 
|---|
| 430 | initContext = at->get_base(); | 
|---|
| 431 | (*iter)->accept( *this ); | 
|---|
| 432 | } // for | 
|---|
| 433 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { | 
|---|
| 434 | resolveAggrInit( st->get_baseStruct(), iter, end ); | 
|---|
| 435 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { | 
|---|
| 436 | resolveAggrInit( st->get_baseUnion(), iter, end ); | 
|---|
| 437 | } else { | 
|---|
| 438 | // basic types are handled here | 
|---|
| 439 | Visitor::visit( listInit ); | 
|---|
| 440 | } | 
|---|
| 441 |  | 
|---|
| 442 | #if 0 | 
|---|
| 443 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { | 
|---|
| 444 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); | 
|---|
| 445 | for ( ; iter != listInit->end_initializers(); ++iter ) { | 
|---|
| 446 | initContext = at->get_base(); | 
|---|
| 447 | (*iter)->accept( *this ); | 
|---|
| 448 | } // for | 
|---|
| 449 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { | 
|---|
| 450 | StructDecl *baseStruct = st->get_baseStruct(); | 
|---|
| 451 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); | 
|---|
| 452 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); | 
|---|
| 453 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { | 
|---|
| 454 | if ( (*iter2)->get_designators().empty() ) { | 
|---|
| 455 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); | 
|---|
| 456 | initContext = dt->get_type(); | 
|---|
| 457 | (*iter2)->accept( *this ); | 
|---|
| 458 | ++iter1; | 
|---|
| 459 | } else { | 
|---|
| 460 | StructDecl *st = baseStruct; | 
|---|
| 461 | iter1 = st->get_members().begin(); | 
|---|
| 462 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); | 
|---|
| 463 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { | 
|---|
| 464 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); | 
|---|
| 465 | assert( key ); | 
|---|
| 466 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { | 
|---|
| 467 | if ( key->get_name() == (*iter1)->get_name() ) { | 
|---|
| 468 | (*iter1)->print( cout ); | 
|---|
| 469 | cout << key->get_name() << endl; | 
|---|
| 470 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
| 471 | assert( fred ); | 
|---|
| 472 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); | 
|---|
| 473 | assert( mary ); | 
|---|
| 474 | st = mary->get_baseStruct(); | 
|---|
| 475 | iter1 = st->get_members().begin(); | 
|---|
| 476 | break; | 
|---|
| 477 | } // if | 
|---|
| 478 | }  // for | 
|---|
| 479 | } // for | 
|---|
| 480 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
| 481 | assert( fred ); | 
|---|
| 482 | initContext = fred->get_type(); | 
|---|
| 483 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
| 484 | } // if | 
|---|
| 485 | } // for | 
|---|
| 486 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { | 
|---|
| 487 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); | 
|---|
| 488 | initContext = dt->get_type(); | 
|---|
| 489 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
| 490 | } // if | 
|---|
| 491 | #endif | 
|---|
| 492 | } | 
|---|
| 493 |  | 
|---|
| 494 | // ConstructorInit - fall back on C-style initializer | 
|---|
| 495 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { | 
|---|
| 496 | // could not find valid constructor, or found an intrinsic constructor | 
|---|
| 497 | // fall back on C-style initializer | 
|---|
| 498 | delete ctorInit->get_ctor(); | 
|---|
| 499 | ctorInit->set_ctor( NULL ); | 
|---|
| 500 | delete ctorInit->get_dtor(); | 
|---|
| 501 | ctorInit->set_dtor( NULL ); | 
|---|
| 502 | maybeAccept( ctorInit->get_init(), *this ); | 
|---|
| 503 | } | 
|---|
| 504 |  | 
|---|
| 505 | void Resolver::visit( ConstructorInit *ctorInit ) { | 
|---|
| 506 | try { | 
|---|
| 507 | maybeAccept( ctorInit->get_ctor(), *this ); | 
|---|
| 508 | maybeAccept( ctorInit->get_dtor(), *this ); | 
|---|
| 509 | } catch ( SemanticError ) { | 
|---|
| 510 | // no alternatives for the constructor initializer - fallback on C-style initializer | 
|---|
| 511 | // xxx - not sure if this makes a ton of sense - should maybe never be able to have this situation? | 
|---|
| 512 | fallbackInit( ctorInit ); | 
|---|
| 513 | return; | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | // found a constructor - can get rid of C-style initializer | 
|---|
| 517 | delete ctorInit->get_init(); | 
|---|
| 518 | ctorInit->set_init( NULL ); | 
|---|
| 519 |  | 
|---|
| 520 | // intrinsic single parameter constructors and destructors do nothing. Since this was | 
|---|
| 521 | // implicitly generated, there's no way for it to have side effects, so get rid of it | 
|---|
| 522 | // to clean up generated code. | 
|---|
| 523 | if ( InitTweak::isInstrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
| 524 | delete ctorInit->get_ctor(); | 
|---|
| 525 | ctorInit->set_ctor( NULL ); | 
|---|
| 526 | } | 
|---|
| 527 | if ( InitTweak::isInstrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
| 528 | delete ctorInit->get_dtor(); | 
|---|
| 529 | ctorInit->set_dtor( NULL ); | 
|---|
| 530 | } | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | void Resolver::visit( ImplicitCtorDtorStmt * impCtorDtorStmt ) { | 
|---|
| 534 | // before resolving ctor/dtor, need to remove type qualifiers from the first argument (the object being constructed). | 
|---|
| 535 | // Do this through a cast expression to greatly simplify the code. | 
|---|
| 536 | Expression * callExpr = InitTweak::getCtorDtorCall( impCtorDtorStmt ); | 
|---|
| 537 | assert( callExpr ); | 
|---|
| 538 | Expression *& constructee = InitTweak::getCallArg( callExpr, 0 ); | 
|---|
| 539 | Type * type = 0; | 
|---|
| 540 |  | 
|---|
| 541 | // need to find the type of the first argument, which is unfortunately not uniform since array construction | 
|---|
| 542 | // includes an untyped '+' expression. | 
|---|
| 543 | if ( UntypedExpr * plusExpr = dynamic_cast< UntypedExpr * >( constructee ) ) { | 
|---|
| 544 | // constructee is <array>+<index> | 
|---|
| 545 | // get Variable <array>, then get the base type of the VariableExpr - this is the type that needs to be fixed | 
|---|
| 546 | Expression * arr = InitTweak::getCallArg( plusExpr, 0 ); | 
|---|
| 547 | assert( dynamic_cast< VariableExpr * >( arr ) || dynamic_cast< MemberExpr *>( arr ) ); | 
|---|
| 548 | assert( arr && arr->get_results().size() == 1 ); | 
|---|
| 549 | type = arr->get_results().front()->clone(); | 
|---|
| 550 | } else { | 
|---|
| 551 | // otherwise, constructing a plain object, which means the object's address is being taken. | 
|---|
| 552 | // Need to get the type of the VariableExpr object, because the AddressExpr is rebuilt and uses the | 
|---|
| 553 | // type of the VariableExpr to do so. | 
|---|
| 554 | assert( constructee->get_results().size() == 1 ); | 
|---|
| 555 | AddressExpr * addrExpr = dynamic_cast< AddressExpr * > ( constructee ); | 
|---|
| 556 | assert( addrExpr && addrExpr->get_results().size() == 1 ); | 
|---|
| 557 | type = addrExpr->get_results().front()->clone(); | 
|---|
| 558 | } | 
|---|
| 559 | // cast to T* with qualifiers removed. | 
|---|
| 560 | // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument | 
|---|
| 561 | // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever | 
|---|
| 562 | // remove lvalue as a qualifier, this can change to | 
|---|
| 563 | //   type->get_qualifiers() = Type::Qualifiers(); | 
|---|
| 564 | Type * base = InitTweak::getPointerBase( type ); | 
|---|
| 565 | assert( base ); | 
|---|
| 566 | base->get_qualifiers() -= Type::Qualifiers(true, true, true, false, true, true); | 
|---|
| 567 | // if pointer has lvalue qualifier, cast won't appear in output | 
|---|
| 568 | type->set_isLvalue( false ); | 
|---|
| 569 | constructee = new CastExpr( constructee, type ); | 
|---|
| 570 |  | 
|---|
| 571 | // finally, resolve the ctor/dtor | 
|---|
| 572 | impCtorDtorStmt->get_callStmt()->accept( *this ); | 
|---|
| 573 | } | 
|---|
| 574 | } // namespace ResolvExpr | 
|---|
| 575 |  | 
|---|
| 576 | // Local Variables: // | 
|---|
| 577 | // tab-width: 4 // | 
|---|
| 578 | // mode: c++ // | 
|---|
| 579 | // compile-command: "make install" // | 
|---|
| 580 | // End: // | 
|---|