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