[51b7345] | 1 | #include "Resolver.h" |
---|
| 2 | #include "AlternativeFinder.h" |
---|
| 3 | #include "Alternative.h" |
---|
| 4 | #include "RenameVars.h" |
---|
| 5 | #include "ResolveTypeof.h" |
---|
| 6 | #include "SynTree/Statement.h" |
---|
| 7 | #include "SynTree/Type.h" |
---|
| 8 | #include "SynTree/Expression.h" |
---|
| 9 | #include "SynTree/Initializer.h" |
---|
| 10 | #include "SymTab/Indexer.h" |
---|
| 11 | #include "utility.h" |
---|
| 12 | |
---|
[d9a0e76] | 13 | #include <iostream> |
---|
| 14 | using namespace std; |
---|
[51b7345] | 15 | |
---|
[d9a0e76] | 16 | namespace ResolvExpr { |
---|
| 17 | class Resolver : public SymTab::Indexer { |
---|
| 18 | public: |
---|
| 19 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} |
---|
[51b7345] | 20 | |
---|
[d9a0e76] | 21 | virtual void visit( FunctionDecl *functionDecl ); |
---|
| 22 | virtual void visit( ObjectDecl *functionDecl ); |
---|
| 23 | virtual void visit( TypeDecl *typeDecl ); |
---|
| 24 | |
---|
| 25 | virtual void visit( ExprStmt *exprStmt ); |
---|
| 26 | virtual void visit( IfStmt *ifStmt ); |
---|
| 27 | virtual void visit( WhileStmt *whileStmt ); |
---|
| 28 | virtual void visit( ForStmt *forStmt ); |
---|
| 29 | virtual void visit( SwitchStmt *switchStmt ); |
---|
| 30 | virtual void visit( ChooseStmt *switchStmt ); |
---|
| 31 | virtual void visit( CaseStmt *caseStmt ); |
---|
| 32 | virtual void visit( ReturnStmt *returnStmt ); |
---|
| 33 | |
---|
| 34 | virtual void visit( SingleInit *singleInit ); |
---|
| 35 | virtual void visit( ListInit *listInit ); |
---|
| 36 | private: |
---|
| 37 | std::list< Type * > functionReturn; |
---|
| 38 | Type *initContext; |
---|
| 39 | Type *switchType; |
---|
| 40 | }; |
---|
| 41 | |
---|
| 42 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
| 43 | Resolver resolver; |
---|
| 44 | acceptAll( translationUnit, resolver ); |
---|
| 45 | #if 0 |
---|
[bdd516a] | 46 | resolver.print( cerr ); |
---|
[d9a0e76] | 47 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 48 | (*i)->print( std::cerr ); |
---|
| 49 | (*i)->accept( resolver ); |
---|
[17cd4eb] | 50 | } // for |
---|
[d9a0e76] | 51 | #endif |
---|
| 52 | } |
---|
[51b7345] | 53 | |
---|
[d9a0e76] | 54 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
| 55 | TypeEnvironment env; |
---|
| 56 | return resolveInVoidContext( expr, indexer, env ); |
---|
| 57 | } |
---|
[51b7345] | 58 | |
---|
[d9a0e76] | 59 | namespace { |
---|
| 60 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
| 61 | expr->set_env( new TypeSubstitution ); |
---|
| 62 | env.makeSubstitution( *expr->get_env() ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 66 | global_renamer.reset(); |
---|
| 67 | TypeEnvironment env; |
---|
| 68 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
| 69 | finishExpr( newExpr, env ); |
---|
| 70 | return newExpr; |
---|
| 71 | } |
---|
[51b7345] | 72 | |
---|
[d9a0e76] | 73 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 74 | TypeEnvironment env; |
---|
| 75 | AlternativeFinder finder( indexer, env ); |
---|
| 76 | finder.find( untyped ); |
---|
| 77 | #if 0 |
---|
| 78 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 79 | std::cout << "untyped expr is "; |
---|
| 80 | untyped->print( std::cout ); |
---|
| 81 | std::cout << std::endl << "alternatives are:"; |
---|
| 82 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 83 | i->print( std::cout ); |
---|
[17cd4eb] | 84 | } // for |
---|
| 85 | } // if |
---|
[d9a0e76] | 86 | #endif |
---|
| 87 | assert( finder.get_alternatives().size() == 1 ); |
---|
| 88 | Alternative &choice = finder.get_alternatives().front(); |
---|
| 89 | Expression *newExpr = choice.expr->clone(); |
---|
| 90 | finishExpr( newExpr, choice.env ); |
---|
| 91 | return newExpr; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | bool isIntegralType( Type *type ) { |
---|
| 95 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 96 | return true; |
---|
| 97 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
| 98 | return bt->isInteger(); |
---|
| 99 | } else { |
---|
[c11e31c] | 100 | return false; |
---|
[17cd4eb] | 101 | } // if |
---|
[d9a0e76] | 102 | } |
---|
[51b7345] | 103 | |
---|
[d9a0e76] | 104 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
| 105 | TypeEnvironment env; |
---|
| 106 | AlternativeFinder finder( indexer, env ); |
---|
| 107 | finder.find( untyped ); |
---|
| 108 | #if 0 |
---|
| 109 | if ( finder.get_alternatives().size() != 1 ) { |
---|
| 110 | std::cout << "untyped expr is "; |
---|
| 111 | untyped->print( std::cout ); |
---|
| 112 | std::cout << std::endl << "alternatives are:"; |
---|
| 113 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 114 | i->print( std::cout ); |
---|
[17cd4eb] | 115 | } // for |
---|
| 116 | } // if |
---|
[d9a0e76] | 117 | #endif |
---|
| 118 | Expression *newExpr = 0; |
---|
| 119 | const TypeEnvironment *newEnv = 0; |
---|
| 120 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
| 121 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { |
---|
| 122 | if ( newExpr ) { |
---|
[c11e31c] | 123 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
[d9a0e76] | 124 | } else { |
---|
| 125 | newExpr = i->expr->clone(); |
---|
| 126 | newEnv = &i->env; |
---|
[17cd4eb] | 127 | } // if |
---|
| 128 | } // if |
---|
| 129 | } // for |
---|
[c11e31c] | 130 | if ( ! newExpr ) { |
---|
| 131 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
[17cd4eb] | 132 | } // if |
---|
[d9a0e76] | 133 | finishExpr( newExpr, *newEnv ); |
---|
| 134 | return newExpr; |
---|
| 135 | } |
---|
[51b7345] | 136 | |
---|
[d9a0e76] | 137 | } |
---|
[51b7345] | 138 | |
---|
[d9a0e76] | 139 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
| 140 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
| 141 | objectDecl->set_type( new_type ); |
---|
| 142 | initContext = new_type; |
---|
| 143 | SymTab::Indexer::visit( objectDecl ); |
---|
| 144 | } |
---|
[51b7345] | 145 | |
---|
[d9a0e76] | 146 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 147 | if ( typeDecl->get_base() ) { |
---|
| 148 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 149 | typeDecl->set_base( new_type ); |
---|
[17cd4eb] | 150 | } // if |
---|
[d9a0e76] | 151 | SymTab::Indexer::visit( typeDecl ); |
---|
| 152 | } |
---|
[51b7345] | 153 | |
---|
[d9a0e76] | 154 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
| 155 | #if 0 |
---|
| 156 | std::cout << "resolver visiting functiondecl "; |
---|
| 157 | functionDecl->print( std::cout ); |
---|
| 158 | std::cout << std::endl; |
---|
| 159 | #endif |
---|
| 160 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 161 | functionDecl->set_type( new_type ); |
---|
| 162 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
| 163 | functionReturn.clear(); |
---|
| 164 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
| 165 | functionReturn.push_back( (*i)->get_type() ); |
---|
[17cd4eb] | 166 | } // for |
---|
[d9a0e76] | 167 | SymTab::Indexer::visit( functionDecl ); |
---|
| 168 | functionReturn = oldFunctionReturn; |
---|
| 169 | } |
---|
[51b7345] | 170 | |
---|
[d9a0e76] | 171 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
| 172 | if ( exprStmt->get_expr() ) { |
---|
| 173 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
| 174 | delete exprStmt->get_expr(); |
---|
| 175 | exprStmt->set_expr( newExpr ); |
---|
[17cd4eb] | 176 | } // if |
---|
[d9a0e76] | 177 | } |
---|
[51b7345] | 178 | |
---|
[d9a0e76] | 179 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
| 180 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
| 181 | delete ifStmt->get_condition(); |
---|
| 182 | ifStmt->set_condition( newExpr ); |
---|
| 183 | Visitor::visit( ifStmt ); |
---|
| 184 | } |
---|
[51b7345] | 185 | |
---|
[d9a0e76] | 186 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
| 187 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
| 188 | delete whileStmt->get_condition(); |
---|
| 189 | whileStmt->set_condition( newExpr ); |
---|
| 190 | Visitor::visit( whileStmt ); |
---|
| 191 | } |
---|
[51b7345] | 192 | |
---|
[d9a0e76] | 193 | void Resolver::visit( ForStmt *forStmt ) { |
---|
[b1a6d6b] | 194 | // SymTab::Indexer::visit( forStmt ); |
---|
[d9a0e76] | 195 | Expression *newExpr; |
---|
[b1a6d6b] | 196 | // for statements introduce a level of scope |
---|
| 197 | enterScope(); |
---|
| 198 | maybeAccept( forStmt->get_initialization(), *this ); |
---|
[d9a0e76] | 199 | if ( forStmt->get_condition() ) { |
---|
| 200 | newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
| 201 | delete forStmt->get_condition(); |
---|
| 202 | forStmt->set_condition( newExpr ); |
---|
[17cd4eb] | 203 | } // if |
---|
[51b7345] | 204 | |
---|
[d9a0e76] | 205 | if ( forStmt->get_increment() ) { |
---|
| 206 | newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
| 207 | delete forStmt->get_increment(); |
---|
| 208 | forStmt->set_increment( newExpr ); |
---|
[17cd4eb] | 209 | } // if |
---|
[b1a6d6b] | 210 | |
---|
| 211 | maybeAccept( forStmt->get_condition(), *this ); |
---|
| 212 | maybeAccept( forStmt->get_increment(), *this ); |
---|
| 213 | maybeAccept( forStmt->get_body(), *this ); |
---|
| 214 | leaveScope(); |
---|
[d9a0e76] | 215 | } |
---|
[51b7345] | 216 | |
---|
[d9a0e76] | 217 | template< typename SwitchClass > |
---|
| 218 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
| 219 | Expression *newExpr; |
---|
| 220 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
| 221 | delete switchStmt->get_condition(); |
---|
| 222 | switchStmt->set_condition( newExpr ); |
---|
[51b7345] | 223 | |
---|
[d9a0e76] | 224 | visitor.Visitor::visit( switchStmt ); |
---|
| 225 | } |
---|
[51b7345] | 226 | |
---|
[d9a0e76] | 227 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
| 228 | handleSwitchStmt( switchStmt, *this ); |
---|
| 229 | } |
---|
[51b7345] | 230 | |
---|
[d9a0e76] | 231 | void Resolver::visit( ChooseStmt *switchStmt ) { |
---|
| 232 | handleSwitchStmt( switchStmt, *this ); |
---|
| 233 | } |
---|
[51b7345] | 234 | |
---|
[d9a0e76] | 235 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
| 236 | Visitor::visit( caseStmt ); |
---|
| 237 | } |
---|
[51b7345] | 238 | |
---|
[d9a0e76] | 239 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
| 240 | if ( returnStmt->get_expr() ) { |
---|
| 241 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
| 242 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
| 243 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 244 | delete castExpr; |
---|
| 245 | returnStmt->set_expr( newExpr ); |
---|
[17cd4eb] | 246 | } // if |
---|
[d9a0e76] | 247 | } |
---|
[51b7345] | 248 | |
---|
[d9a0e76] | 249 | void Resolver::visit( SingleInit *singleInit ) { |
---|
[17cd4eb] | 250 | if ( singleInit->get_value() ) { |
---|
[bdd516a] | 251 | #if 0 |
---|
| 252 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
| 253 | string n = ne->get_name(); |
---|
| 254 | if (n == "0") { |
---|
| 255 | initContext = new BasicType(Type::Qualifiers(), |
---|
| 256 | BasicType::SignedInt); |
---|
| 257 | } else { |
---|
| 258 | DeclarationWithType * decl = lookupId(n); |
---|
| 259 | initContext = decl->get_type(); |
---|
| 260 | } |
---|
| 261 | } else if (ConstantExpr * e = |
---|
| 262 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
| 263 | Constant *c = e->get_constant(); |
---|
| 264 | initContext = c->get_type(); |
---|
| 265 | } else { |
---|
| 266 | assert(0); |
---|
| 267 | } |
---|
| 268 | #endif |
---|
[17cd4eb] | 269 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
| 270 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 271 | delete castExpr; |
---|
| 272 | singleInit->set_value( newExpr ); |
---|
| 273 | } // if |
---|
[6c3744e] | 274 | // singleInit->get_value()->accept( *this ); |
---|
[d9a0e76] | 275 | } |
---|
[51b7345] | 276 | |
---|
[d9a0e76] | 277 | void Resolver::visit( ListInit *listInit ) { |
---|
[bdd516a] | 278 | Visitor::visit(listInit); |
---|
| 279 | #if 0 |
---|
[6c3744e] | 280 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
[bdd516a] | 281 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
| 282 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
| 283 | initContext = at->get_base(); |
---|
| 284 | (*iter)->accept( *this ); |
---|
| 285 | } // for |
---|
[6c3744e] | 286 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
| 287 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
| 288 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
| 289 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
[2c2242c] | 290 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
| 291 | if ( (*iter2)->get_designators().empty() ) { |
---|
| 292 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
| 293 | initContext = dt->get_type(); |
---|
| 294 | (*iter2)->accept( *this ); |
---|
| 295 | ++iter1; |
---|
| 296 | } else { |
---|
| 297 | StructDecl *st = baseStruct; |
---|
| 298 | iter1 = st->get_members().begin(); |
---|
| 299 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
| 300 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
| 301 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
| 302 | assert( key ); |
---|
| 303 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
| 304 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
| 305 | (*iter1)->print( cout ); |
---|
| 306 | cout << key->get_name() << endl; |
---|
| 307 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 308 | assert( fred ); |
---|
| 309 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
| 310 | assert( mary ); |
---|
| 311 | st = mary->get_baseStruct(); |
---|
| 312 | iter1 = st->get_members().begin(); |
---|
| 313 | break; |
---|
| 314 | } // if |
---|
| 315 | } // for |
---|
| 316 | } // for |
---|
| 317 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 318 | assert( fred ); |
---|
| 319 | initContext = fred->get_type(); |
---|
| 320 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 321 | } // if |
---|
| 322 | } // for |
---|
[6c3744e] | 323 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
| 324 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
| 325 | initContext = dt->get_type(); |
---|
| 326 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 327 | } // if |
---|
[bdd516a] | 328 | #endif |
---|
[d9a0e76] | 329 | } |
---|
[51b7345] | 330 | } // namespace ResolvExpr |
---|