[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 |
---|
[5f2f2d7] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sun Jun 7 21:50:37 2015 |
---|
| 13 | // Update Count : 23 |
---|
[a32b204] | 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 ); |
---|
[d1d17f5] | 159 | |
---|
| 160 | if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){ |
---|
[30651b0] | 161 | if ( at->get_dimension() ) { |
---|
| 162 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
| 163 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); |
---|
| 164 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 165 | delete at->get_dimension(); |
---|
| 166 | at->set_dimension( newExpr ); |
---|
| 167 | } |
---|
[d1d17f5] | 168 | } |
---|
[a32b204] | 169 | } |
---|
[51b7345] | 170 | |
---|
[a32b204] | 171 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
| 172 | if ( typeDecl->get_base() ) { |
---|
| 173 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
| 174 | typeDecl->set_base( new_type ); |
---|
| 175 | } // if |
---|
| 176 | SymTab::Indexer::visit( typeDecl ); |
---|
| 177 | } |
---|
[51b7345] | 178 | |
---|
[a32b204] | 179 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
[d9a0e76] | 180 | #if 0 |
---|
[a32b204] | 181 | std::cout << "resolver visiting functiondecl "; |
---|
| 182 | functionDecl->print( std::cout ); |
---|
| 183 | std::cout << std::endl; |
---|
[d9a0e76] | 184 | #endif |
---|
[a32b204] | 185 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
| 186 | functionDecl->set_type( new_type ); |
---|
| 187 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
| 188 | functionReturn.clear(); |
---|
| 189 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
| 190 | functionReturn.push_back( (*i)->get_type() ); |
---|
| 191 | } // for |
---|
| 192 | SymTab::Indexer::visit( functionDecl ); |
---|
| 193 | functionReturn = oldFunctionReturn; |
---|
| 194 | } |
---|
[51b7345] | 195 | |
---|
[a32b204] | 196 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
| 197 | if ( exprStmt->get_expr() ) { |
---|
| 198 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
| 199 | delete exprStmt->get_expr(); |
---|
| 200 | exprStmt->set_expr( newExpr ); |
---|
| 201 | } // if |
---|
| 202 | } |
---|
[51b7345] | 203 | |
---|
[a32b204] | 204 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
| 205 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
| 206 | delete ifStmt->get_condition(); |
---|
| 207 | ifStmt->set_condition( newExpr ); |
---|
| 208 | Visitor::visit( ifStmt ); |
---|
| 209 | } |
---|
[51b7345] | 210 | |
---|
[a32b204] | 211 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
| 212 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
| 213 | delete whileStmt->get_condition(); |
---|
| 214 | whileStmt->set_condition( newExpr ); |
---|
| 215 | Visitor::visit( whileStmt ); |
---|
| 216 | } |
---|
[51b7345] | 217 | |
---|
[a32b204] | 218 | void Resolver::visit( ForStmt *forStmt ) { |
---|
| 219 | // SymTab::Indexer::visit( forStmt ); |
---|
| 220 | Expression *newExpr; |
---|
| 221 | // for statements introduce a level of scope |
---|
| 222 | enterScope(); |
---|
| 223 | maybeAccept( forStmt->get_initialization(), *this ); |
---|
| 224 | if ( forStmt->get_condition() ) { |
---|
| 225 | newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
| 226 | delete forStmt->get_condition(); |
---|
| 227 | forStmt->set_condition( newExpr ); |
---|
| 228 | } // if |
---|
[51b7345] | 229 | |
---|
[a32b204] | 230 | if ( forStmt->get_increment() ) { |
---|
| 231 | newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
| 232 | delete forStmt->get_increment(); |
---|
| 233 | forStmt->set_increment( newExpr ); |
---|
| 234 | } // if |
---|
[b1a6d6b] | 235 | |
---|
[a32b204] | 236 | maybeAccept( forStmt->get_condition(), *this ); |
---|
| 237 | maybeAccept( forStmt->get_increment(), *this ); |
---|
| 238 | maybeAccept( forStmt->get_body(), *this ); |
---|
| 239 | leaveScope(); |
---|
| 240 | } |
---|
[51b7345] | 241 | |
---|
[a32b204] | 242 | template< typename SwitchClass > |
---|
| 243 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
| 244 | Expression *newExpr; |
---|
| 245 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
| 246 | delete switchStmt->get_condition(); |
---|
| 247 | switchStmt->set_condition( newExpr ); |
---|
[51b7345] | 248 | |
---|
[a32b204] | 249 | visitor.Visitor::visit( switchStmt ); |
---|
| 250 | } |
---|
[51b7345] | 251 | |
---|
[a32b204] | 252 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
| 253 | handleSwitchStmt( switchStmt, *this ); |
---|
| 254 | } |
---|
[51b7345] | 255 | |
---|
[a32b204] | 256 | void Resolver::visit( ChooseStmt *switchStmt ) { |
---|
| 257 | handleSwitchStmt( switchStmt, *this ); |
---|
| 258 | } |
---|
[51b7345] | 259 | |
---|
[a32b204] | 260 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
| 261 | Visitor::visit( caseStmt ); |
---|
| 262 | } |
---|
[51b7345] | 263 | |
---|
[a32b204] | 264 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
| 265 | if ( returnStmt->get_expr() ) { |
---|
| 266 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
| 267 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
| 268 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 269 | delete castExpr; |
---|
| 270 | returnStmt->set_expr( newExpr ); |
---|
| 271 | } // if |
---|
| 272 | } |
---|
[51b7345] | 273 | |
---|
[a32b204] | 274 | void Resolver::visit( SingleInit *singleInit ) { |
---|
| 275 | if ( singleInit->get_value() ) { |
---|
[bdd516a] | 276 | #if 0 |
---|
[a32b204] | 277 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
| 278 | string n = ne->get_name(); |
---|
| 279 | if (n == "0") { |
---|
| 280 | initContext = new BasicType(Type::Qualifiers(), |
---|
| 281 | BasicType::SignedInt); |
---|
| 282 | } else { |
---|
| 283 | DeclarationWithType * decl = lookupId(n); |
---|
| 284 | initContext = decl->get_type(); |
---|
| 285 | } |
---|
| 286 | } else if (ConstantExpr * e = |
---|
| 287 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
| 288 | Constant *c = e->get_constant(); |
---|
| 289 | initContext = c->get_type(); |
---|
| 290 | } else { |
---|
| 291 | assert(0); |
---|
| 292 | } |
---|
[bdd516a] | 293 | #endif |
---|
[a32b204] | 294 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
| 295 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
| 296 | delete castExpr; |
---|
| 297 | singleInit->set_value( newExpr ); |
---|
| 298 | } // if |
---|
[6c3744e] | 299 | // singleInit->get_value()->accept( *this ); |
---|
[a32b204] | 300 | } |
---|
[51b7345] | 301 | |
---|
[a32b204] | 302 | void Resolver::visit( ListInit *listInit ) { |
---|
| 303 | Visitor::visit(listInit); |
---|
[bdd516a] | 304 | #if 0 |
---|
[a32b204] | 305 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
| 306 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
| 307 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
| 308 | initContext = at->get_base(); |
---|
| 309 | (*iter)->accept( *this ); |
---|
| 310 | } // for |
---|
| 311 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
| 312 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
| 313 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
| 314 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
| 315 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
| 316 | if ( (*iter2)->get_designators().empty() ) { |
---|
| 317 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
| 318 | initContext = dt->get_type(); |
---|
| 319 | (*iter2)->accept( *this ); |
---|
| 320 | ++iter1; |
---|
| 321 | } else { |
---|
| 322 | StructDecl *st = baseStruct; |
---|
| 323 | iter1 = st->get_members().begin(); |
---|
| 324 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
| 325 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
| 326 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
| 327 | assert( key ); |
---|
| 328 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
| 329 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
| 330 | (*iter1)->print( cout ); |
---|
| 331 | cout << key->get_name() << endl; |
---|
| 332 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 333 | assert( fred ); |
---|
| 334 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
| 335 | assert( mary ); |
---|
| 336 | st = mary->get_baseStruct(); |
---|
| 337 | iter1 = st->get_members().begin(); |
---|
| 338 | break; |
---|
| 339 | } // if |
---|
| 340 | } // for |
---|
| 341 | } // for |
---|
| 342 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
| 343 | assert( fred ); |
---|
| 344 | initContext = fred->get_type(); |
---|
| 345 | (*listInit->begin_initializers())->accept( *this ); |
---|
| 346 | } // if |
---|
| 347 | } // for |
---|
| 348 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
| 349 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
| 350 | initContext = dt->get_type(); |
---|
| 351 | (*listInit->begin_initializers())->accept( *this ); |
---|
[2c2242c] | 352 | } // if |
---|
[bdd516a] | 353 | #endif |
---|
[a32b204] | 354 | } |
---|
[51b7345] | 355 | } // namespace ResolvExpr |
---|
[a32b204] | 356 | |
---|
| 357 | // Local Variables: // |
---|
| 358 | // tab-width: 4 // |
---|
| 359 | // mode: c++ // |
---|
| 360 | // compile-command: "make install" // |
---|
| 361 | // End: // |
---|