| [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 | // | 
|---|
|  | 7 | // Resolver.cc -- | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 12:17:01 2015 | 
|---|
| [7f5566b] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [3cfe27f] | 12 | // Last Modified On : Thu Mar 24 16:43:11 2016 | 
|---|
|  | 13 | // Update Count     : 181 | 
|---|
| [a32b204] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [51b73452] | 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" | 
|---|
| [d3b7937] | 26 | #include "Common/utility.h" | 
|---|
| [51b73452] | 27 |  | 
|---|
| [d9a0e76] | 28 | #include <iostream> | 
|---|
|  | 29 | using namespace std; | 
|---|
| [51b73452] | 30 |  | 
|---|
| [d9a0e76] | 31 | namespace ResolvExpr { | 
|---|
| [a32b204] | 32 | class Resolver : public SymTab::Indexer { | 
|---|
|  | 33 | public: | 
|---|
|  | 34 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} | 
|---|
| [51b73452] | 35 |  | 
|---|
| [a32b204] | 36 | virtual void visit( FunctionDecl *functionDecl ); | 
|---|
|  | 37 | virtual void visit( ObjectDecl *functionDecl ); | 
|---|
|  | 38 | virtual void visit( TypeDecl *typeDecl ); | 
|---|
| [94b4364] | 39 |  | 
|---|
| [bfbf97f] | 40 | virtual void visit( ArrayType * at ); | 
|---|
| [d9a0e76] | 41 |  | 
|---|
| [a32b204] | 42 | virtual void visit( ExprStmt *exprStmt ); | 
|---|
| [7f5566b] | 43 | virtual void visit( AsmExpr *asmExpr ); | 
|---|
|  | 44 | virtual void visit( AsmStmt *asmStmt ); | 
|---|
| [a32b204] | 45 | virtual void visit( IfStmt *ifStmt ); | 
|---|
|  | 46 | virtual void visit( WhileStmt *whileStmt ); | 
|---|
|  | 47 | virtual void visit( ForStmt *forStmt ); | 
|---|
|  | 48 | virtual void visit( SwitchStmt *switchStmt ); | 
|---|
|  | 49 | virtual void visit( ChooseStmt *switchStmt ); | 
|---|
|  | 50 | virtual void visit( CaseStmt *caseStmt ); | 
|---|
| [de62360d] | 51 | virtual void visit( BranchStmt *branchStmt ); | 
|---|
| [a32b204] | 52 | virtual void visit( ReturnStmt *returnStmt ); | 
|---|
| [d9a0e76] | 53 |  | 
|---|
| [a32b204] | 54 | virtual void visit( SingleInit *singleInit ); | 
|---|
|  | 55 | virtual void visit( ListInit *listInit ); | 
|---|
|  | 56 | private: | 
|---|
| [94b4364] | 57 | typedef std::list< Initializer * >::iterator InitIterator; | 
|---|
|  | 58 |  | 
|---|
|  | 59 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); | 
|---|
|  | 60 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); | 
|---|
|  | 61 |  | 
|---|
| [a32b204] | 62 | std::list< Type * > functionReturn; | 
|---|
|  | 63 | Type *initContext; | 
|---|
|  | 64 | Type *switchType; | 
|---|
|  | 65 | }; | 
|---|
| [d9a0e76] | 66 |  | 
|---|
| [a32b204] | 67 | void resolve( std::list< Declaration * > translationUnit ) { | 
|---|
|  | 68 | Resolver resolver; | 
|---|
|  | 69 | acceptAll( translationUnit, resolver ); | 
|---|
| [d9a0e76] | 70 | #if 0 | 
|---|
| [a32b204] | 71 | resolver.print( cerr ); | 
|---|
|  | 72 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { | 
|---|
|  | 73 | (*i)->print( std::cerr ); | 
|---|
|  | 74 | (*i)->accept( resolver ); | 
|---|
|  | 75 | } // for | 
|---|
| [d9a0e76] | 76 | #endif | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [a32b204] | 79 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { | 
|---|
|  | 80 | TypeEnvironment env; | 
|---|
|  | 81 | return resolveInVoidContext( expr, indexer, env ); | 
|---|
| [d9a0e76] | 82 | } | 
|---|
| [a32b204] | 83 |  | 
|---|
|  | 84 | namespace { | 
|---|
|  | 85 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { | 
|---|
|  | 86 | expr->set_env( new TypeSubstitution ); | 
|---|
|  | 87 | env.makeSubstitution( *expr->get_env() ); | 
|---|
|  | 88 | } | 
|---|
|  | 89 |  | 
|---|
|  | 90 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 91 | global_renamer.reset(); | 
|---|
|  | 92 | TypeEnvironment env; | 
|---|
|  | 93 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); | 
|---|
|  | 94 | finishExpr( newExpr, env ); | 
|---|
|  | 95 | return newExpr; | 
|---|
|  | 96 | } | 
|---|
| [51b73452] | 97 |  | 
|---|
| [a32b204] | 98 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 99 | TypeEnvironment env; | 
|---|
|  | 100 | AlternativeFinder finder( indexer, env ); | 
|---|
|  | 101 | finder.find( untyped ); | 
|---|
| [d9a0e76] | 102 | #if 0 | 
|---|
| [a32b204] | 103 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 104 | std::cout << "untyped expr is "; | 
|---|
|  | 105 | untyped->print( std::cout ); | 
|---|
|  | 106 | std::cout << std::endl << "alternatives are:"; | 
|---|
|  | 107 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 108 | i->print( std::cout ); | 
|---|
|  | 109 | } // for | 
|---|
|  | 110 | } // if | 
|---|
| [d9a0e76] | 111 | #endif | 
|---|
| [a32b204] | 112 | assert( finder.get_alternatives().size() == 1 ); | 
|---|
|  | 113 | Alternative &choice = finder.get_alternatives().front(); | 
|---|
|  | 114 | Expression *newExpr = choice.expr->clone(); | 
|---|
|  | 115 | finishExpr( newExpr, choice.env ); | 
|---|
|  | 116 | return newExpr; | 
|---|
|  | 117 | } | 
|---|
| [d9a0e76] | 118 |  | 
|---|
| [a32b204] | 119 | bool isIntegralType( Type *type ) { | 
|---|
|  | 120 | if ( dynamic_cast< EnumInstType * >( type ) ) { | 
|---|
|  | 121 | return true; | 
|---|
|  | 122 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { | 
|---|
|  | 123 | return bt->isInteger(); | 
|---|
|  | 124 | } else { | 
|---|
|  | 125 | return false; | 
|---|
|  | 126 | } // if | 
|---|
|  | 127 | } | 
|---|
| [51b73452] | 128 |  | 
|---|
| [a32b204] | 129 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 130 | TypeEnvironment env; | 
|---|
|  | 131 | AlternativeFinder finder( indexer, env ); | 
|---|
|  | 132 | finder.find( untyped ); | 
|---|
| [d9a0e76] | 133 | #if 0 | 
|---|
| [a32b204] | 134 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 135 | std::cout << "untyped expr is "; | 
|---|
|  | 136 | untyped->print( std::cout ); | 
|---|
|  | 137 | std::cout << std::endl << "alternatives are:"; | 
|---|
|  | 138 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 139 | i->print( std::cout ); | 
|---|
|  | 140 | } // for | 
|---|
|  | 141 | } // if | 
|---|
| [d9a0e76] | 142 | #endif | 
|---|
| [a32b204] | 143 | Expression *newExpr = 0; | 
|---|
|  | 144 | const TypeEnvironment *newEnv = 0; | 
|---|
|  | 145 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 146 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { | 
|---|
|  | 147 | if ( newExpr ) { | 
|---|
|  | 148 | throw SemanticError( "Too many interpretations for case control expression", untyped ); | 
|---|
|  | 149 | } else { | 
|---|
|  | 150 | newExpr = i->expr->clone(); | 
|---|
|  | 151 | newEnv = &i->env; | 
|---|
|  | 152 | } // if | 
|---|
|  | 153 | } // if | 
|---|
|  | 154 | } // for | 
|---|
|  | 155 | if ( ! newExpr ) { | 
|---|
|  | 156 | throw SemanticError( "No interpretations for case control expression", untyped ); | 
|---|
|  | 157 | } // if | 
|---|
|  | 158 | finishExpr( newExpr, *newEnv ); | 
|---|
|  | 159 | return newExpr; | 
|---|
|  | 160 | } | 
|---|
| [51b73452] | 161 |  | 
|---|
| [a32b204] | 162 | } | 
|---|
| [51b73452] | 163 |  | 
|---|
| [a32b204] | 164 | void Resolver::visit( ObjectDecl *objectDecl ) { | 
|---|
|  | 165 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); | 
|---|
|  | 166 | objectDecl->set_type( new_type ); | 
|---|
| [3cfe27f] | 167 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable | 
|---|
|  | 168 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes | 
|---|
|  | 169 | // initContext because of a function type can contain object declarations in the return and parameter types. So | 
|---|
|  | 170 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting | 
|---|
|  | 171 | // the RHS. | 
|---|
|  | 172 | Type *temp = initContext; | 
|---|
| [a32b204] | 173 | initContext = new_type; | 
|---|
|  | 174 | SymTab::Indexer::visit( objectDecl ); | 
|---|
| [3cfe27f] | 175 | initContext = temp; | 
|---|
| [bfbf97f] | 176 | } | 
|---|
|  | 177 |  | 
|---|
|  | 178 | void Resolver::visit( ArrayType * at ) { | 
|---|
|  | 179 | if ( at->get_dimension() ) { | 
|---|
|  | 180 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); | 
|---|
|  | 181 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); | 
|---|
|  | 182 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 183 | delete at->get_dimension(); | 
|---|
|  | 184 | at->set_dimension( newExpr ); | 
|---|
| [d1d17f5] | 185 | } | 
|---|
| [bfbf97f] | 186 | Visitor::visit( at ); | 
|---|
| [a32b204] | 187 | } | 
|---|
| [94b4364] | 188 |  | 
|---|
| [a32b204] | 189 | void Resolver::visit( TypeDecl *typeDecl ) { | 
|---|
|  | 190 | if ( typeDecl->get_base() ) { | 
|---|
|  | 191 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); | 
|---|
|  | 192 | typeDecl->set_base( new_type ); | 
|---|
|  | 193 | } // if | 
|---|
|  | 194 | SymTab::Indexer::visit( typeDecl ); | 
|---|
|  | 195 | } | 
|---|
| [94b4364] | 196 |  | 
|---|
| [a32b204] | 197 | void Resolver::visit( FunctionDecl *functionDecl ) { | 
|---|
| [d9a0e76] | 198 | #if 0 | 
|---|
| [a32b204] | 199 | std::cout << "resolver visiting functiondecl "; | 
|---|
|  | 200 | functionDecl->print( std::cout ); | 
|---|
|  | 201 | std::cout << std::endl; | 
|---|
| [d9a0e76] | 202 | #endif | 
|---|
| [a32b204] | 203 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); | 
|---|
|  | 204 | functionDecl->set_type( new_type ); | 
|---|
|  | 205 | std::list< Type * > oldFunctionReturn = functionReturn; | 
|---|
|  | 206 | functionReturn.clear(); | 
|---|
|  | 207 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { | 
|---|
|  | 208 | functionReturn.push_back( (*i)->get_type() ); | 
|---|
|  | 209 | } // for | 
|---|
|  | 210 | SymTab::Indexer::visit( functionDecl ); | 
|---|
|  | 211 | functionReturn = oldFunctionReturn; | 
|---|
|  | 212 | } | 
|---|
| [51b73452] | 213 |  | 
|---|
| [a32b204] | 214 | void Resolver::visit( ExprStmt *exprStmt ) { | 
|---|
|  | 215 | if ( exprStmt->get_expr() ) { | 
|---|
|  | 216 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); | 
|---|
|  | 217 | delete exprStmt->get_expr(); | 
|---|
|  | 218 | exprStmt->set_expr( newExpr ); | 
|---|
|  | 219 | } // if | 
|---|
|  | 220 | } | 
|---|
| [51b73452] | 221 |  | 
|---|
| [7f5566b] | 222 | void Resolver::visit( AsmExpr *asmExpr ) { | 
|---|
|  | 223 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); | 
|---|
|  | 224 | delete asmExpr->get_operand(); | 
|---|
|  | 225 | asmExpr->set_operand( newExpr ); | 
|---|
|  | 226 | if ( asmExpr->get_inout() ) { | 
|---|
|  | 227 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); | 
|---|
|  | 228 | delete asmExpr->get_inout(); | 
|---|
|  | 229 | asmExpr->set_inout( newExpr ); | 
|---|
|  | 230 | } // if | 
|---|
|  | 231 | } | 
|---|
|  | 232 |  | 
|---|
|  | 233 | void Resolver::visit( AsmStmt *asmStmt ) { | 
|---|
|  | 234 | acceptAll( asmStmt->get_input(), *this); | 
|---|
|  | 235 | acceptAll( asmStmt->get_output(), *this); | 
|---|
|  | 236 | } | 
|---|
|  | 237 |  | 
|---|
| [a32b204] | 238 | void Resolver::visit( IfStmt *ifStmt ) { | 
|---|
|  | 239 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); | 
|---|
|  | 240 | delete ifStmt->get_condition(); | 
|---|
|  | 241 | ifStmt->set_condition( newExpr ); | 
|---|
|  | 242 | Visitor::visit( ifStmt ); | 
|---|
|  | 243 | } | 
|---|
| [51b73452] | 244 |  | 
|---|
| [a32b204] | 245 | void Resolver::visit( WhileStmt *whileStmt ) { | 
|---|
|  | 246 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); | 
|---|
|  | 247 | delete whileStmt->get_condition(); | 
|---|
|  | 248 | whileStmt->set_condition( newExpr ); | 
|---|
|  | 249 | Visitor::visit( whileStmt ); | 
|---|
|  | 250 | } | 
|---|
| [51b73452] | 251 |  | 
|---|
| [a32b204] | 252 | void Resolver::visit( ForStmt *forStmt ) { | 
|---|
| [145f1fc] | 253 | SymTab::Indexer::visit( forStmt ); | 
|---|
|  | 254 |  | 
|---|
| [a32b204] | 255 | if ( forStmt->get_condition() ) { | 
|---|
| [145f1fc] | 256 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); | 
|---|
| [a32b204] | 257 | delete forStmt->get_condition(); | 
|---|
|  | 258 | forStmt->set_condition( newExpr ); | 
|---|
|  | 259 | } // if | 
|---|
| [145f1fc] | 260 |  | 
|---|
| [a32b204] | 261 | if ( forStmt->get_increment() ) { | 
|---|
| [145f1fc] | 262 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); | 
|---|
| [a32b204] | 263 | delete forStmt->get_increment(); | 
|---|
|  | 264 | forStmt->set_increment( newExpr ); | 
|---|
|  | 265 | } // if | 
|---|
|  | 266 | } | 
|---|
| [51b73452] | 267 |  | 
|---|
| [a32b204] | 268 | template< typename SwitchClass > | 
|---|
|  | 269 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { | 
|---|
|  | 270 | Expression *newExpr; | 
|---|
|  | 271 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); | 
|---|
|  | 272 | delete switchStmt->get_condition(); | 
|---|
|  | 273 | switchStmt->set_condition( newExpr ); | 
|---|
| [51b73452] | 274 |  | 
|---|
| [a32b204] | 275 | visitor.Visitor::visit( switchStmt ); | 
|---|
|  | 276 | } | 
|---|
| [51b73452] | 277 |  | 
|---|
| [a32b204] | 278 | void Resolver::visit( SwitchStmt *switchStmt ) { | 
|---|
|  | 279 | handleSwitchStmt( switchStmt, *this ); | 
|---|
|  | 280 | } | 
|---|
| [51b73452] | 281 |  | 
|---|
| [a32b204] | 282 | void Resolver::visit( ChooseStmt *switchStmt ) { | 
|---|
|  | 283 | handleSwitchStmt( switchStmt, *this ); | 
|---|
|  | 284 | } | 
|---|
| [51b73452] | 285 |  | 
|---|
| [a32b204] | 286 | void Resolver::visit( CaseStmt *caseStmt ) { | 
|---|
|  | 287 | Visitor::visit( caseStmt ); | 
|---|
|  | 288 | } | 
|---|
| [51b73452] | 289 |  | 
|---|
| [de62360d] | 290 | void Resolver::visit( BranchStmt *branchStmt ) { | 
|---|
|  | 291 | // must resolve the argument for a computed goto | 
|---|
|  | 292 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement | 
|---|
| [2871210] | 293 | if ( Expression * arg = branchStmt->get_computedTarget() ) { | 
|---|
| [de62360d] | 294 | VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder | 
|---|
|  | 295 | PointerType pt( Type::Qualifiers(), v.clone() ); | 
|---|
|  | 296 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); | 
|---|
|  | 297 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression | 
|---|
|  | 298 | branchStmt->set_target( newExpr ); | 
|---|
|  | 299 | } // if | 
|---|
|  | 300 | } // if | 
|---|
|  | 301 | } | 
|---|
|  | 302 |  | 
|---|
| [a32b204] | 303 | void Resolver::visit( ReturnStmt *returnStmt ) { | 
|---|
|  | 304 | if ( returnStmt->get_expr() ) { | 
|---|
|  | 305 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); | 
|---|
|  | 306 | cloneAll( functionReturn, castExpr->get_results() ); | 
|---|
|  | 307 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 308 | delete castExpr; | 
|---|
|  | 309 | returnStmt->set_expr( newExpr ); | 
|---|
|  | 310 | } // if | 
|---|
|  | 311 | } | 
|---|
| [51b73452] | 312 |  | 
|---|
| [b5c5684] | 313 | template< typename T > | 
|---|
|  | 314 | bool isCharType( T t ) { | 
|---|
|  | 315 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { | 
|---|
|  | 316 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || | 
|---|
|  | 317 | bt->get_kind() == BasicType::UnsignedChar; | 
|---|
|  | 318 | } | 
|---|
|  | 319 | return false; | 
|---|
|  | 320 | } | 
|---|
|  | 321 |  | 
|---|
| [a32b204] | 322 | void Resolver::visit( SingleInit *singleInit ) { | 
|---|
|  | 323 | if ( singleInit->get_value() ) { | 
|---|
| [bdd516a] | 324 | #if 0 | 
|---|
| [a32b204] | 325 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { | 
|---|
|  | 326 | string n = ne->get_name(); | 
|---|
|  | 327 | if (n == "0") { | 
|---|
|  | 328 | initContext = new BasicType(Type::Qualifiers(), | 
|---|
|  | 329 | BasicType::SignedInt); | 
|---|
|  | 330 | } else { | 
|---|
| [52f85e0] | 331 | DeclarationWithType * decl = lookupId( n ); | 
|---|
| [a32b204] | 332 | initContext = decl->get_type(); | 
|---|
|  | 333 | } | 
|---|
|  | 334 | } else if (ConstantExpr * e = | 
|---|
|  | 335 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { | 
|---|
|  | 336 | Constant *c = e->get_constant(); | 
|---|
|  | 337 | initContext = c->get_type(); | 
|---|
|  | 338 | } else { | 
|---|
|  | 339 | assert(0); | 
|---|
|  | 340 | } | 
|---|
| [bdd516a] | 341 | #endif | 
|---|
| [a32b204] | 342 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); | 
|---|
|  | 343 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 344 | delete castExpr; | 
|---|
|  | 345 | singleInit->set_value( newExpr ); | 
|---|
| [b5c5684] | 346 |  | 
|---|
|  | 347 | // check if initializing type is char[] | 
|---|
|  | 348 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
|  | 349 | if ( isCharType( at->get_base() ) ) { | 
|---|
|  | 350 | // check if the resolved type is char * | 
|---|
|  | 351 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { | 
|---|
|  | 352 | if ( isCharType( pt->get_base() ) ) { | 
|---|
| [52f85e0] | 353 | // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello"; | 
|---|
| [b5c5684] | 354 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); | 
|---|
|  | 355 | singleInit->set_value( ce->get_arg() ); | 
|---|
|  | 356 | ce->set_arg( NULL ); | 
|---|
|  | 357 | delete ce; | 
|---|
|  | 358 | } | 
|---|
|  | 359 | } | 
|---|
|  | 360 | } | 
|---|
|  | 361 | } | 
|---|
| [a32b204] | 362 | } // if | 
|---|
| [6c3744e] | 363 | //      singleInit->get_value()->accept( *this ); | 
|---|
| [a32b204] | 364 | } | 
|---|
| [51b73452] | 365 |  | 
|---|
| [94b4364] | 366 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { | 
|---|
|  | 367 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); | 
|---|
|  | 368 | assert( dt ); | 
|---|
|  | 369 | initContext = dt->get_type(); | 
|---|
|  | 370 | try { | 
|---|
|  | 371 | if ( init == initEnd ) return; // stop when there are no more initializers | 
|---|
|  | 372 | (*init)->accept( *this ); | 
|---|
|  | 373 | ++init; // made it past an initializer | 
|---|
|  | 374 | } catch( SemanticError & ) { | 
|---|
|  | 375 | // need to delve deeper, if you can | 
|---|
|  | 376 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { | 
|---|
|  | 377 | resolveAggrInit( sit->get_baseStruct(), init, initEnd ); | 
|---|
|  | 378 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { | 
|---|
|  | 379 | resolveAggrInit( uit->get_baseUnion(), init, initEnd ); | 
|---|
|  | 380 | } else { | 
|---|
| [1869adf] | 381 | // member is not an aggregate type, so can't go any deeper | 
|---|
|  | 382 |  | 
|---|
| [94b4364] | 383 | // might need to rethink what is being thrown | 
|---|
|  | 384 | throw; | 
|---|
|  | 385 | } // if | 
|---|
|  | 386 | } | 
|---|
|  | 387 | } | 
|---|
|  | 388 |  | 
|---|
|  | 389 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { | 
|---|
|  | 390 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { | 
|---|
|  | 391 | // want to resolve each initializer to the members of the struct, | 
|---|
|  | 392 | // but if there are more initializers than members we should stop | 
|---|
|  | 393 | list< Declaration * >::iterator it = st->get_members().begin(); | 
|---|
|  | 394 | for ( ; it != st->get_members().end(); ++it) { | 
|---|
|  | 395 | resolveSingleAggrInit( *it, init, initEnd ); | 
|---|
|  | 396 | } | 
|---|
|  | 397 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { | 
|---|
|  | 398 | // only resolve to the first member of a union | 
|---|
|  | 399 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); | 
|---|
|  | 400 | } // if | 
|---|
|  | 401 | } | 
|---|
|  | 402 |  | 
|---|
|  | 403 | void Resolver::visit( ListInit * listInit ) { | 
|---|
|  | 404 | InitIterator iter = listInit->begin_initializers(); | 
|---|
|  | 405 | InitIterator end = listInit->end_initializers(); | 
|---|
|  | 406 |  | 
|---|
| [b5c5684] | 407 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
| [94b4364] | 408 | // resolve each member to the base type of the array | 
|---|
|  | 409 | for ( ; iter != end; ++iter ) { | 
|---|
| [b5c5684] | 410 | initContext = at->get_base(); | 
|---|
|  | 411 | (*iter)->accept( *this ); | 
|---|
|  | 412 | } // for | 
|---|
| [94b4364] | 413 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { | 
|---|
|  | 414 | resolveAggrInit( st->get_baseStruct(), iter, end ); | 
|---|
| [b5c5684] | 415 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { | 
|---|
| [94b4364] | 416 | resolveAggrInit( st->get_baseUnion(), iter, end ); | 
|---|
| [b5c5684] | 417 | } else { | 
|---|
|  | 418 | // basic types are handled here | 
|---|
|  | 419 | Visitor::visit( listInit ); | 
|---|
|  | 420 | } | 
|---|
|  | 421 |  | 
|---|
| [bdd516a] | 422 | #if 0 | 
|---|
| [a32b204] | 423 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { | 
|---|
|  | 424 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); | 
|---|
|  | 425 | for ( ; iter != listInit->end_initializers(); ++iter ) { | 
|---|
|  | 426 | initContext = at->get_base(); | 
|---|
|  | 427 | (*iter)->accept( *this ); | 
|---|
|  | 428 | } // for | 
|---|
|  | 429 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { | 
|---|
|  | 430 | StructDecl *baseStruct = st->get_baseStruct(); | 
|---|
|  | 431 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); | 
|---|
|  | 432 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); | 
|---|
|  | 433 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { | 
|---|
|  | 434 | if ( (*iter2)->get_designators().empty() ) { | 
|---|
|  | 435 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); | 
|---|
|  | 436 | initContext = dt->get_type(); | 
|---|
|  | 437 | (*iter2)->accept( *this ); | 
|---|
|  | 438 | ++iter1; | 
|---|
|  | 439 | } else { | 
|---|
|  | 440 | StructDecl *st = baseStruct; | 
|---|
|  | 441 | iter1 = st->get_members().begin(); | 
|---|
|  | 442 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); | 
|---|
|  | 443 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { | 
|---|
|  | 444 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); | 
|---|
|  | 445 | assert( key ); | 
|---|
|  | 446 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { | 
|---|
|  | 447 | if ( key->get_name() == (*iter1)->get_name() ) { | 
|---|
|  | 448 | (*iter1)->print( cout ); | 
|---|
|  | 449 | cout << key->get_name() << endl; | 
|---|
|  | 450 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
|  | 451 | assert( fred ); | 
|---|
|  | 452 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); | 
|---|
|  | 453 | assert( mary ); | 
|---|
|  | 454 | st = mary->get_baseStruct(); | 
|---|
|  | 455 | iter1 = st->get_members().begin(); | 
|---|
|  | 456 | break; | 
|---|
|  | 457 | } // if | 
|---|
|  | 458 | }  // for | 
|---|
|  | 459 | } // for | 
|---|
|  | 460 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
|  | 461 | assert( fred ); | 
|---|
|  | 462 | initContext = fred->get_type(); | 
|---|
|  | 463 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
|  | 464 | } // if | 
|---|
|  | 465 | } // for | 
|---|
|  | 466 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { | 
|---|
|  | 467 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); | 
|---|
|  | 468 | initContext = dt->get_type(); | 
|---|
|  | 469 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
| [2c2242c] | 470 | } // if | 
|---|
| [bdd516a] | 471 | #endif | 
|---|
| [a32b204] | 472 | } | 
|---|
| [51b73452] | 473 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 474 |  | 
|---|
|  | 475 | // Local Variables: // | 
|---|
|  | 476 | // tab-width: 4 // | 
|---|
|  | 477 | // mode: c++ // | 
|---|
|  | 478 | // compile-command: "make install" // | 
|---|
|  | 479 | // End: // | 
|---|