| [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 | // | 
|---|
| [71f4e4f] | 7 | // Resolver.cc -- | 
|---|
| [a32b204] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 12:17:01 2015 | 
|---|
| [4e06c1e] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Tue Jul 12 17:45:42 2016 | 
|---|
|  | 13 | // Update Count     : 204 | 
|---|
| [a32b204] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [51b73452] | 16 | #include "Resolver.h" | 
|---|
|  | 17 | #include "AlternativeFinder.h" | 
|---|
|  | 18 | #include "Alternative.h" | 
|---|
|  | 19 | #include "RenameVars.h" | 
|---|
|  | 20 | #include "ResolveTypeof.h" | 
|---|
| [906e24d] | 21 | #include "typeops.h" | 
|---|
| [51b73452] | 22 | #include "SynTree/Statement.h" | 
|---|
|  | 23 | #include "SynTree/Type.h" | 
|---|
|  | 24 | #include "SynTree/Expression.h" | 
|---|
|  | 25 | #include "SynTree/Initializer.h" | 
|---|
|  | 26 | #include "SymTab/Indexer.h" | 
|---|
| [5f98ce5] | 27 | #include "SymTab/Autogen.h" | 
|---|
| [d3b7937] | 28 | #include "Common/utility.h" | 
|---|
| [7b3f66b] | 29 | #include "InitTweak/InitTweak.h" | 
|---|
| [51b73452] | 30 |  | 
|---|
| [d9a0e76] | 31 | #include <iostream> | 
|---|
|  | 32 | using namespace std; | 
|---|
| [51b73452] | 33 |  | 
|---|
| [d9a0e76] | 34 | namespace ResolvExpr { | 
|---|
| [62e5546] | 35 | class Resolver final : public SymTab::Indexer { | 
|---|
| [a32b204] | 36 | public: | 
|---|
| [77971f6] | 37 | Resolver() : SymTab::Indexer( false ) {} | 
|---|
| [1d2b64f] | 38 | Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) { | 
|---|
|  | 39 | if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) { | 
|---|
|  | 40 | functionReturn = res->functionReturn; | 
|---|
|  | 41 | initContext = res->initContext; | 
|---|
|  | 42 | inEnumDecl = res->inEnumDecl; | 
|---|
|  | 43 | } | 
|---|
|  | 44 | } | 
|---|
| [71f4e4f] | 45 |  | 
|---|
| [1d2b64f] | 46 | typedef SymTab::Indexer Parent; | 
|---|
|  | 47 | using Parent::visit; | 
|---|
| [62e5546] | 48 | virtual void visit( FunctionDecl *functionDecl ) override; | 
|---|
|  | 49 | virtual void visit( ObjectDecl *functionDecl ) override; | 
|---|
|  | 50 | virtual void visit( TypeDecl *typeDecl ) override; | 
|---|
|  | 51 | virtual void visit( EnumDecl * enumDecl ) override; | 
|---|
| [94b4364] | 52 |  | 
|---|
| [62e5546] | 53 | virtual void visit( ArrayType * at ) override; | 
|---|
|  | 54 | virtual void visit( PointerType * at ) override; | 
|---|
| [d9a0e76] | 55 |  | 
|---|
| [62e5546] | 56 | virtual void visit( ExprStmt *exprStmt ) override; | 
|---|
|  | 57 | virtual void visit( AsmExpr *asmExpr ) override; | 
|---|
|  | 58 | virtual void visit( AsmStmt *asmStmt ) override; | 
|---|
|  | 59 | virtual void visit( IfStmt *ifStmt ) override; | 
|---|
|  | 60 | virtual void visit( WhileStmt *whileStmt ) override; | 
|---|
|  | 61 | virtual void visit( ForStmt *forStmt ) override; | 
|---|
|  | 62 | virtual void visit( SwitchStmt *switchStmt ) override; | 
|---|
|  | 63 | virtual void visit( CaseStmt *caseStmt ) override; | 
|---|
|  | 64 | virtual void visit( BranchStmt *branchStmt ) override; | 
|---|
|  | 65 | virtual void visit( ReturnStmt *returnStmt ) override; | 
|---|
| [d9a0e76] | 66 |  | 
|---|
| [62e5546] | 67 | virtual void visit( SingleInit *singleInit ) override; | 
|---|
|  | 68 | virtual void visit( ListInit *listInit ) override; | 
|---|
|  | 69 | virtual void visit( ConstructorInit *ctorInit ) override; | 
|---|
| [a32b204] | 70 | private: | 
|---|
| [94b4364] | 71 | typedef std::list< Initializer * >::iterator InitIterator; | 
|---|
|  | 72 |  | 
|---|
| [40e636a] | 73 | template< typename PtrType > | 
|---|
|  | 74 | void handlePtrType( PtrType * type ); | 
|---|
|  | 75 |  | 
|---|
| [30b65d8] | 76 | void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & ); | 
|---|
|  | 77 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub ); | 
|---|
| [f1e012b] | 78 | void fallbackInit( ConstructorInit * ctorInit ); | 
|---|
| [b726084] | 79 |  | 
|---|
| [77971f6] | 80 | Type * functionReturn = nullptr; | 
|---|
|  | 81 | Type *initContext = nullptr; | 
|---|
| [a436947] | 82 | bool inEnumDecl = false; | 
|---|
| [a32b204] | 83 | }; | 
|---|
| [d9a0e76] | 84 |  | 
|---|
| [a32b204] | 85 | void resolve( std::list< Declaration * > translationUnit ) { | 
|---|
|  | 86 | Resolver resolver; | 
|---|
|  | 87 | acceptAll( translationUnit, resolver ); | 
|---|
| [d9a0e76] | 88 | } | 
|---|
|  | 89 |  | 
|---|
| [a32b204] | 90 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { | 
|---|
|  | 91 | TypeEnvironment env; | 
|---|
|  | 92 | return resolveInVoidContext( expr, indexer, env ); | 
|---|
| [d9a0e76] | 93 | } | 
|---|
| [a32b204] | 94 |  | 
|---|
| [db4ecc5] | 95 |  | 
|---|
| [a32b204] | 96 | namespace { | 
|---|
|  | 97 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { | 
|---|
|  | 98 | expr->set_env( new TypeSubstitution ); | 
|---|
|  | 99 | env.makeSubstitution( *expr->get_env() ); | 
|---|
|  | 100 | } | 
|---|
| [db4ecc5] | 101 | } // namespace | 
|---|
| [a32b204] | 102 |  | 
|---|
| [db4ecc5] | 103 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 104 | global_renamer.reset(); | 
|---|
|  | 105 | TypeEnvironment env; | 
|---|
|  | 106 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); | 
|---|
|  | 107 | finishExpr( newExpr, env ); | 
|---|
|  | 108 | return newExpr; | 
|---|
|  | 109 | } | 
|---|
| [71f4e4f] | 110 |  | 
|---|
| [db4ecc5] | 111 | namespace { | 
|---|
| [a32b204] | 112 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 113 | TypeEnvironment env; | 
|---|
|  | 114 | AlternativeFinder finder( indexer, env ); | 
|---|
|  | 115 | finder.find( untyped ); | 
|---|
| [d9a0e76] | 116 | #if 0 | 
|---|
| [a32b204] | 117 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 118 | std::cout << "untyped expr is "; | 
|---|
|  | 119 | untyped->print( std::cout ); | 
|---|
|  | 120 | std::cout << std::endl << "alternatives are:"; | 
|---|
|  | 121 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 122 | i->print( std::cout ); | 
|---|
|  | 123 | } // for | 
|---|
|  | 124 | } // if | 
|---|
| [d9a0e76] | 125 | #endif | 
|---|
| [a32b204] | 126 | assert( finder.get_alternatives().size() == 1 ); | 
|---|
|  | 127 | Alternative &choice = finder.get_alternatives().front(); | 
|---|
|  | 128 | Expression *newExpr = choice.expr->clone(); | 
|---|
|  | 129 | finishExpr( newExpr, choice.env ); | 
|---|
|  | 130 | return newExpr; | 
|---|
|  | 131 | } | 
|---|
| [d9a0e76] | 132 |  | 
|---|
| [a32b204] | 133 | bool isIntegralType( Type *type ) { | 
|---|
|  | 134 | if ( dynamic_cast< EnumInstType * >( type ) ) { | 
|---|
|  | 135 | return true; | 
|---|
|  | 136 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { | 
|---|
|  | 137 | return bt->isInteger(); | 
|---|
| [89e6ffc] | 138 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { | 
|---|
|  | 139 | return true; | 
|---|
| [a32b204] | 140 | } else { | 
|---|
|  | 141 | return false; | 
|---|
|  | 142 | } // if | 
|---|
|  | 143 | } | 
|---|
| [71f4e4f] | 144 |  | 
|---|
| [a32b204] | 145 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 146 | TypeEnvironment env; | 
|---|
|  | 147 | AlternativeFinder finder( indexer, env ); | 
|---|
|  | 148 | finder.find( untyped ); | 
|---|
| [d9a0e76] | 149 | #if 0 | 
|---|
| [a32b204] | 150 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 151 | std::cout << "untyped expr is "; | 
|---|
|  | 152 | untyped->print( std::cout ); | 
|---|
|  | 153 | std::cout << std::endl << "alternatives are:"; | 
|---|
|  | 154 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 155 | i->print( std::cout ); | 
|---|
|  | 156 | } // for | 
|---|
|  | 157 | } // if | 
|---|
| [d9a0e76] | 158 | #endif | 
|---|
| [a32b204] | 159 | Expression *newExpr = 0; | 
|---|
|  | 160 | const TypeEnvironment *newEnv = 0; | 
|---|
|  | 161 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
| [906e24d] | 162 | if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) { | 
|---|
| [a32b204] | 163 | if ( newExpr ) { | 
|---|
|  | 164 | throw SemanticError( "Too many interpretations for case control expression", untyped ); | 
|---|
|  | 165 | } else { | 
|---|
|  | 166 | newExpr = i->expr->clone(); | 
|---|
|  | 167 | newEnv = &i->env; | 
|---|
|  | 168 | } // if | 
|---|
|  | 169 | } // if | 
|---|
|  | 170 | } // for | 
|---|
|  | 171 | if ( ! newExpr ) { | 
|---|
|  | 172 | throw SemanticError( "No interpretations for case control expression", untyped ); | 
|---|
|  | 173 | } // if | 
|---|
|  | 174 | finishExpr( newExpr, *newEnv ); | 
|---|
|  | 175 | return newExpr; | 
|---|
|  | 176 | } | 
|---|
| [71f4e4f] | 177 |  | 
|---|
| [a32b204] | 178 | } | 
|---|
| [71f4e4f] | 179 |  | 
|---|
| [a32b204] | 180 | void Resolver::visit( ObjectDecl *objectDecl ) { | 
|---|
|  | 181 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); | 
|---|
|  | 182 | objectDecl->set_type( new_type ); | 
|---|
| [3cfe27f] | 183 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable | 
|---|
|  | 184 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes | 
|---|
|  | 185 | // initContext because of a function type can contain object declarations in the return and parameter types. So | 
|---|
|  | 186 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting | 
|---|
|  | 187 | // the RHS. | 
|---|
|  | 188 | Type *temp = initContext; | 
|---|
| [a32b204] | 189 | initContext = new_type; | 
|---|
| [a436947] | 190 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { | 
|---|
|  | 191 | // enumerator initializers should not use the enum type to initialize, since | 
|---|
|  | 192 | // the enum type is still incomplete at this point. Use signed int instead. | 
|---|
|  | 193 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); | 
|---|
|  | 194 | } | 
|---|
| [1d2b64f] | 195 | Parent::visit( objectDecl ); | 
|---|
| [a436947] | 196 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) { | 
|---|
|  | 197 | // delete newly created signed int type | 
|---|
|  | 198 | delete initContext; | 
|---|
|  | 199 | } | 
|---|
| [3cfe27f] | 200 | initContext = temp; | 
|---|
| [bfbf97f] | 201 | } | 
|---|
|  | 202 |  | 
|---|
| [40e636a] | 203 | template< typename PtrType > | 
|---|
|  | 204 | void Resolver::handlePtrType( PtrType * type ) { | 
|---|
|  | 205 | if ( type->get_dimension() ) { | 
|---|
|  | 206 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); | 
|---|
| [bfbf97f] | 207 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
| [40e636a] | 208 | delete type->get_dimension(); | 
|---|
|  | 209 | type->set_dimension( newExpr ); | 
|---|
| [d1d17f5] | 210 | } | 
|---|
| [40e636a] | 211 | } | 
|---|
|  | 212 |  | 
|---|
|  | 213 | void Resolver::visit( ArrayType * at ) { | 
|---|
|  | 214 | handlePtrType( at ); | 
|---|
| [1d2b64f] | 215 | Parent::visit( at ); | 
|---|
| [a32b204] | 216 | } | 
|---|
| [94b4364] | 217 |  | 
|---|
| [40e636a] | 218 | void Resolver::visit( PointerType * pt ) { | 
|---|
|  | 219 | handlePtrType( pt ); | 
|---|
| [1d2b64f] | 220 | Parent::visit( pt ); | 
|---|
| [40e636a] | 221 | } | 
|---|
|  | 222 |  | 
|---|
| [a32b204] | 223 | void Resolver::visit( TypeDecl *typeDecl ) { | 
|---|
|  | 224 | if ( typeDecl->get_base() ) { | 
|---|
|  | 225 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); | 
|---|
|  | 226 | typeDecl->set_base( new_type ); | 
|---|
|  | 227 | } // if | 
|---|
| [1d2b64f] | 228 | Parent::visit( typeDecl ); | 
|---|
| [a32b204] | 229 | } | 
|---|
| [94b4364] | 230 |  | 
|---|
| [a32b204] | 231 | void Resolver::visit( FunctionDecl *functionDecl ) { | 
|---|
| [d9a0e76] | 232 | #if 0 | 
|---|
| [a32b204] | 233 | std::cout << "resolver visiting functiondecl "; | 
|---|
|  | 234 | functionDecl->print( std::cout ); | 
|---|
|  | 235 | std::cout << std::endl; | 
|---|
| [d9a0e76] | 236 | #endif | 
|---|
| [a32b204] | 237 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); | 
|---|
|  | 238 | functionDecl->set_type( new_type ); | 
|---|
| [906e24d] | 239 | ValueGuard< Type * > oldFunctionReturn( functionReturn ); | 
|---|
|  | 240 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); | 
|---|
| [1d2b64f] | 241 | Parent::visit( functionDecl ); | 
|---|
| [88d1066] | 242 |  | 
|---|
|  | 243 | // default value expressions have an environment which shouldn't be there and trips up later passes. | 
|---|
|  | 244 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently | 
|---|
|  | 245 | // see how it's useful. | 
|---|
|  | 246 | for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) { | 
|---|
|  | 247 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { | 
|---|
|  | 248 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) { | 
|---|
|  | 249 | delete init->get_value()->get_env(); | 
|---|
|  | 250 | init->get_value()->set_env( nullptr ); | 
|---|
|  | 251 | } | 
|---|
|  | 252 | } | 
|---|
|  | 253 | } | 
|---|
| [a32b204] | 254 | } | 
|---|
| [51b73452] | 255 |  | 
|---|
| [a436947] | 256 | void Resolver::visit( EnumDecl * enumDecl ) { | 
|---|
|  | 257 | // in case we decide to allow nested enums | 
|---|
| [1d2b64f] | 258 | ValueGuard< bool > oldInEnumDecl( inEnumDecl ); | 
|---|
| [a436947] | 259 | inEnumDecl = true; | 
|---|
| [1d2b64f] | 260 | Parent::visit( enumDecl ); | 
|---|
| [a436947] | 261 | } | 
|---|
|  | 262 |  | 
|---|
| [a32b204] | 263 | void Resolver::visit( ExprStmt *exprStmt ) { | 
|---|
| [1d2b64f] | 264 | assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" ); | 
|---|
|  | 265 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); | 
|---|
|  | 266 | delete exprStmt->get_expr(); | 
|---|
|  | 267 | exprStmt->set_expr( newExpr ); | 
|---|
| [a32b204] | 268 | } | 
|---|
| [51b73452] | 269 |  | 
|---|
| [7f5566b] | 270 | void Resolver::visit( AsmExpr *asmExpr ) { | 
|---|
|  | 271 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); | 
|---|
|  | 272 | delete asmExpr->get_operand(); | 
|---|
|  | 273 | asmExpr->set_operand( newExpr ); | 
|---|
|  | 274 | if ( asmExpr->get_inout() ) { | 
|---|
|  | 275 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); | 
|---|
|  | 276 | delete asmExpr->get_inout(); | 
|---|
|  | 277 | asmExpr->set_inout( newExpr ); | 
|---|
|  | 278 | } // if | 
|---|
|  | 279 | } | 
|---|
|  | 280 |  | 
|---|
|  | 281 | void Resolver::visit( AsmStmt *asmStmt ) { | 
|---|
|  | 282 | acceptAll( asmStmt->get_input(), *this); | 
|---|
|  | 283 | acceptAll( asmStmt->get_output(), *this); | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
| [a32b204] | 286 | void Resolver::visit( IfStmt *ifStmt ) { | 
|---|
|  | 287 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); | 
|---|
|  | 288 | delete ifStmt->get_condition(); | 
|---|
|  | 289 | ifStmt->set_condition( newExpr ); | 
|---|
| [1d2b64f] | 290 | Parent::visit( ifStmt ); | 
|---|
| [a32b204] | 291 | } | 
|---|
| [51b73452] | 292 |  | 
|---|
| [a32b204] | 293 | void Resolver::visit( WhileStmt *whileStmt ) { | 
|---|
|  | 294 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); | 
|---|
|  | 295 | delete whileStmt->get_condition(); | 
|---|
|  | 296 | whileStmt->set_condition( newExpr ); | 
|---|
| [1d2b64f] | 297 | Parent::visit( whileStmt ); | 
|---|
| [a32b204] | 298 | } | 
|---|
| [51b73452] | 299 |  | 
|---|
| [a32b204] | 300 | void Resolver::visit( ForStmt *forStmt ) { | 
|---|
| [1d2b64f] | 301 | Parent::visit( forStmt ); | 
|---|
| [145f1fc] | 302 |  | 
|---|
| [a32b204] | 303 | if ( forStmt->get_condition() ) { | 
|---|
| [145f1fc] | 304 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); | 
|---|
| [a32b204] | 305 | delete forStmt->get_condition(); | 
|---|
|  | 306 | forStmt->set_condition( newExpr ); | 
|---|
|  | 307 | } // if | 
|---|
| [71f4e4f] | 308 |  | 
|---|
| [a32b204] | 309 | if ( forStmt->get_increment() ) { | 
|---|
| [145f1fc] | 310 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); | 
|---|
| [a32b204] | 311 | delete forStmt->get_increment(); | 
|---|
|  | 312 | forStmt->set_increment( newExpr ); | 
|---|
|  | 313 | } // if | 
|---|
|  | 314 | } | 
|---|
| [51b73452] | 315 |  | 
|---|
| [32b8144] | 316 | void Resolver::visit( SwitchStmt *switchStmt ) { | 
|---|
|  | 317 | ValueGuard< Type * > oldInitContext( initContext ); | 
|---|
| [a32b204] | 318 | Expression *newExpr; | 
|---|
| [32b8144] | 319 | newExpr = findIntegralExpression( switchStmt->get_condition(), *this ); | 
|---|
| [a32b204] | 320 | delete switchStmt->get_condition(); | 
|---|
|  | 321 | switchStmt->set_condition( newExpr ); | 
|---|
| [71f4e4f] | 322 |  | 
|---|
| [32b8144] | 323 | initContext = newExpr->get_result(); | 
|---|
|  | 324 | Parent::visit( switchStmt ); | 
|---|
| [a32b204] | 325 | } | 
|---|
| [51b73452] | 326 |  | 
|---|
| [a32b204] | 327 | void Resolver::visit( CaseStmt *caseStmt ) { | 
|---|
| [32b8144] | 328 | if ( caseStmt->get_condition() ) { | 
|---|
|  | 329 | assert( initContext ); | 
|---|
|  | 330 | CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initContext->clone() ); | 
|---|
|  | 331 | Expression * newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 332 | castExpr = safe_dynamic_cast< CastExpr * >( newExpr ); | 
|---|
|  | 333 | caseStmt->set_condition( castExpr->get_arg() ); | 
|---|
|  | 334 | castExpr->set_arg( nullptr ); | 
|---|
|  | 335 | delete castExpr; | 
|---|
|  | 336 | } | 
|---|
| [1d2b64f] | 337 | Parent::visit( caseStmt ); | 
|---|
| [a32b204] | 338 | } | 
|---|
| [51b73452] | 339 |  | 
|---|
| [de62360d] | 340 | void Resolver::visit( BranchStmt *branchStmt ) { | 
|---|
|  | 341 | // must resolve the argument for a computed goto | 
|---|
|  | 342 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement | 
|---|
| [2871210] | 343 | if ( Expression * arg = branchStmt->get_computedTarget() ) { | 
|---|
| [de62360d] | 344 | VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder | 
|---|
|  | 345 | PointerType pt( Type::Qualifiers(), v.clone() ); | 
|---|
|  | 346 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); | 
|---|
|  | 347 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression | 
|---|
|  | 348 | branchStmt->set_target( newExpr ); | 
|---|
|  | 349 | } // if | 
|---|
|  | 350 | } // if | 
|---|
|  | 351 | } | 
|---|
|  | 352 |  | 
|---|
| [a32b204] | 353 | void Resolver::visit( ReturnStmt *returnStmt ) { | 
|---|
|  | 354 | if ( returnStmt->get_expr() ) { | 
|---|
| [906e24d] | 355 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); | 
|---|
| [a32b204] | 356 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 357 | delete castExpr; | 
|---|
|  | 358 | returnStmt->set_expr( newExpr ); | 
|---|
|  | 359 | } // if | 
|---|
|  | 360 | } | 
|---|
| [51b73452] | 361 |  | 
|---|
| [b5c5684] | 362 | template< typename T > | 
|---|
|  | 363 | bool isCharType( T t ) { | 
|---|
|  | 364 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { | 
|---|
| [71f4e4f] | 365 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || | 
|---|
| [b5c5684] | 366 | bt->get_kind() == BasicType::UnsignedChar; | 
|---|
|  | 367 | } | 
|---|
|  | 368 | return false; | 
|---|
|  | 369 | } | 
|---|
|  | 370 |  | 
|---|
| [a32b204] | 371 | void Resolver::visit( SingleInit *singleInit ) { | 
|---|
|  | 372 | if ( singleInit->get_value() ) { | 
|---|
| [bdd516a] | 373 | #if 0 | 
|---|
| [a32b204] | 374 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { | 
|---|
|  | 375 | string n = ne->get_name(); | 
|---|
|  | 376 | if (n == "0") { | 
|---|
| [71f4e4f] | 377 | initContext = new BasicType(Type::Qualifiers(), | 
|---|
| [a32b204] | 378 | BasicType::SignedInt); | 
|---|
|  | 379 | } else { | 
|---|
| [52f85e0] | 380 | DeclarationWithType * decl = lookupId( n ); | 
|---|
| [a32b204] | 381 | initContext = decl->get_type(); | 
|---|
|  | 382 | } | 
|---|
| [71f4e4f] | 383 | } else if (ConstantExpr * e = | 
|---|
| [a32b204] | 384 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { | 
|---|
|  | 385 | Constant *c = e->get_constant(); | 
|---|
|  | 386 | initContext = c->get_type(); | 
|---|
|  | 387 | } else { | 
|---|
|  | 388 | assert(0); | 
|---|
|  | 389 | } | 
|---|
| [bdd516a] | 390 | #endif | 
|---|
| [a32b204] | 391 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); | 
|---|
|  | 392 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 393 | delete castExpr; | 
|---|
|  | 394 | singleInit->set_value( newExpr ); | 
|---|
| [b5c5684] | 395 |  | 
|---|
|  | 396 | // check if initializing type is char[] | 
|---|
|  | 397 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
|  | 398 | if ( isCharType( at->get_base() ) ) { | 
|---|
|  | 399 | // check if the resolved type is char * | 
|---|
| [906e24d] | 400 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { | 
|---|
| [b5c5684] | 401 | if ( isCharType( pt->get_base() ) ) { | 
|---|
| [52f85e0] | 402 | // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello"; | 
|---|
| [b5c5684] | 403 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); | 
|---|
|  | 404 | singleInit->set_value( ce->get_arg() ); | 
|---|
|  | 405 | ce->set_arg( NULL ); | 
|---|
| [71f4e4f] | 406 | delete ce; | 
|---|
| [b5c5684] | 407 | } | 
|---|
|  | 408 | } | 
|---|
|  | 409 | } | 
|---|
|  | 410 | } | 
|---|
| [a32b204] | 411 | } // if | 
|---|
| [6c3744e] | 412 | //      singleInit->get_value()->accept( *this ); | 
|---|
| [a32b204] | 413 | } | 
|---|
| [51b73452] | 414 |  | 
|---|
| [30b65d8] | 415 | template< typename AggrInst > | 
|---|
|  | 416 | TypeSubstitution makeGenericSubstitutuion( AggrInst * inst ) { | 
|---|
| [66f8528] | 417 | assert( inst ); | 
|---|
|  | 418 | assert( inst->get_baseParameters() ); | 
|---|
| [30b65d8] | 419 | std::list< TypeDecl * > baseParams = *inst->get_baseParameters(); | 
|---|
|  | 420 | std::list< Expression * > typeSubs = inst->get_parameters(); | 
|---|
|  | 421 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); | 
|---|
|  | 422 | return subs; | 
|---|
|  | 423 | } | 
|---|
|  | 424 |  | 
|---|
|  | 425 | ReferenceToType * isStructOrUnion( Type * type ) { | 
|---|
|  | 426 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( type ) ) { | 
|---|
|  | 427 | return sit; | 
|---|
|  | 428 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( type ) ) { | 
|---|
|  | 429 | return uit; | 
|---|
|  | 430 | } | 
|---|
|  | 431 | return nullptr; | 
|---|
|  | 432 | } | 
|---|
|  | 433 |  | 
|---|
|  | 434 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd, TypeSubstitution sub ) { | 
|---|
| [94b4364] | 435 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); | 
|---|
|  | 436 | assert( dt ); | 
|---|
| [30b65d8] | 437 | // need to substitute for generic types, so that casts are to concrete types | 
|---|
|  | 438 | initContext = dt->get_type()->clone(); | 
|---|
|  | 439 | sub.apply( initContext ); | 
|---|
|  | 440 |  | 
|---|
| [94b4364] | 441 | try { | 
|---|
|  | 442 | if ( init == initEnd ) return; // stop when there are no more initializers | 
|---|
|  | 443 | (*init)->accept( *this ); | 
|---|
|  | 444 | ++init; // made it past an initializer | 
|---|
|  | 445 | } catch( SemanticError & ) { | 
|---|
|  | 446 | // need to delve deeper, if you can | 
|---|
| [30b65d8] | 447 | if ( ReferenceToType * type = isStructOrUnion( initContext ) ) { | 
|---|
|  | 448 | resolveAggrInit( type, init, initEnd ); | 
|---|
| [94b4364] | 449 | } else { | 
|---|
| [1869adf] | 450 | // member is not an aggregate type, so can't go any deeper | 
|---|
|  | 451 |  | 
|---|
| [94b4364] | 452 | // might need to rethink what is being thrown | 
|---|
|  | 453 | throw; | 
|---|
|  | 454 | } // if | 
|---|
|  | 455 | } | 
|---|
|  | 456 | } | 
|---|
|  | 457 |  | 
|---|
| [30b65d8] | 458 | void Resolver::resolveAggrInit( ReferenceToType * inst, InitIterator & init, InitIterator & initEnd ) { | 
|---|
|  | 459 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( inst ) ) { | 
|---|
|  | 460 | TypeSubstitution sub = makeGenericSubstitutuion( sit ); | 
|---|
|  | 461 | StructDecl * st = sit->get_baseStruct(); | 
|---|
| [eeaea53] | 462 | if(st->get_members().empty()) return; | 
|---|
| [94b4364] | 463 | // want to resolve each initializer to the members of the struct, | 
|---|
|  | 464 | // but if there are more initializers than members we should stop | 
|---|
|  | 465 | list< Declaration * >::iterator it = st->get_members().begin(); | 
|---|
|  | 466 | for ( ; it != st->get_members().end(); ++it) { | 
|---|
| [30b65d8] | 467 | resolveSingleAggrInit( *it, init, initEnd, sub ); | 
|---|
| [94b4364] | 468 | } | 
|---|
| [30b65d8] | 469 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( inst ) ) { | 
|---|
| [66f8528] | 470 | TypeSubstitution sub = makeGenericSubstitutuion( uit ); | 
|---|
| [30b65d8] | 471 | UnionDecl * un = uit->get_baseUnion(); | 
|---|
| [eeaea53] | 472 | if(un->get_members().empty()) return; | 
|---|
| [94b4364] | 473 | // only resolve to the first member of a union | 
|---|
| [30b65d8] | 474 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd, sub ); | 
|---|
| [94b4364] | 475 | } // if | 
|---|
|  | 476 | } | 
|---|
|  | 477 |  | 
|---|
|  | 478 | void Resolver::visit( ListInit * listInit ) { | 
|---|
| [4d2434a] | 479 | InitIterator iter = listInit->begin(); | 
|---|
|  | 480 | InitIterator end = listInit->end(); | 
|---|
| [94b4364] | 481 |  | 
|---|
| [b5c5684] | 482 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
| [94b4364] | 483 | // resolve each member to the base type of the array | 
|---|
|  | 484 | for ( ; iter != end; ++iter ) { | 
|---|
| [b5c5684] | 485 | initContext = at->get_base(); | 
|---|
|  | 486 | (*iter)->accept( *this ); | 
|---|
|  | 487 | } // for | 
|---|
| [65660bd] | 488 | } else if ( TupleType * tt = dynamic_cast< TupleType * > ( initContext ) ) { | 
|---|
|  | 489 | for ( Type * t : *tt ) { | 
|---|
|  | 490 | if ( iter == end ) break; | 
|---|
|  | 491 | initContext = t; | 
|---|
|  | 492 | (*iter++)->accept( *this ); | 
|---|
|  | 493 | } | 
|---|
| [30b65d8] | 494 | } else if ( ReferenceToType * type = isStructOrUnion( initContext ) ) { | 
|---|
|  | 495 | resolveAggrInit( type, iter, end ); | 
|---|
| [679864e1] | 496 | } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { | 
|---|
| [242d458] | 497 | Type * base = tt->get_baseType()->get_base(); | 
|---|
|  | 498 | if ( base ) { | 
|---|
|  | 499 | // know the implementation type, so try using that as the initContext | 
|---|
|  | 500 | initContext = base; | 
|---|
|  | 501 | visit( listInit ); | 
|---|
|  | 502 | } else { | 
|---|
|  | 503 | // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context | 
|---|
| [1d2b64f] | 504 | Parent::visit( listInit ); | 
|---|
| [242d458] | 505 | } | 
|---|
| [b5c5684] | 506 | } else { | 
|---|
| [89e6ffc] | 507 | assert( dynamic_cast< BasicType * >( initContext ) || dynamic_cast< PointerType * >( initContext ) | 
|---|
| [1d2b64f] | 508 | || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) || dynamic_cast < EnumInstType * > ( initContext ) ); | 
|---|
| [b5c5684] | 509 | // basic types are handled here | 
|---|
| [1d2b64f] | 510 | Parent::visit( listInit ); | 
|---|
| [b5c5684] | 511 | } | 
|---|
|  | 512 |  | 
|---|
| [bdd516a] | 513 | #if 0 | 
|---|
| [a32b204] | 514 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { | 
|---|
|  | 515 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); | 
|---|
|  | 516 | for ( ; iter != listInit->end_initializers(); ++iter ) { | 
|---|
|  | 517 | initContext = at->get_base(); | 
|---|
|  | 518 | (*iter)->accept( *this ); | 
|---|
|  | 519 | } // for | 
|---|
|  | 520 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { | 
|---|
|  | 521 | StructDecl *baseStruct = st->get_baseStruct(); | 
|---|
|  | 522 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); | 
|---|
|  | 523 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); | 
|---|
|  | 524 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { | 
|---|
|  | 525 | if ( (*iter2)->get_designators().empty() ) { | 
|---|
|  | 526 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); | 
|---|
|  | 527 | initContext = dt->get_type(); | 
|---|
|  | 528 | (*iter2)->accept( *this ); | 
|---|
|  | 529 | ++iter1; | 
|---|
|  | 530 | } else { | 
|---|
|  | 531 | StructDecl *st = baseStruct; | 
|---|
|  | 532 | iter1 = st->get_members().begin(); | 
|---|
|  | 533 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); | 
|---|
|  | 534 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { | 
|---|
|  | 535 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); | 
|---|
|  | 536 | assert( key ); | 
|---|
|  | 537 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { | 
|---|
|  | 538 | if ( key->get_name() == (*iter1)->get_name() ) { | 
|---|
|  | 539 | (*iter1)->print( cout ); | 
|---|
|  | 540 | cout << key->get_name() << endl; | 
|---|
|  | 541 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
|  | 542 | assert( fred ); | 
|---|
|  | 543 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); | 
|---|
|  | 544 | assert( mary ); | 
|---|
|  | 545 | st = mary->get_baseStruct(); | 
|---|
|  | 546 | iter1 = st->get_members().begin(); | 
|---|
|  | 547 | break; | 
|---|
|  | 548 | } // if | 
|---|
|  | 549 | }  // for | 
|---|
|  | 550 | } // for | 
|---|
|  | 551 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); | 
|---|
|  | 552 | assert( fred ); | 
|---|
|  | 553 | initContext = fred->get_type(); | 
|---|
|  | 554 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
|  | 555 | } // if | 
|---|
|  | 556 | } // for | 
|---|
|  | 557 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { | 
|---|
|  | 558 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); | 
|---|
|  | 559 | initContext = dt->get_type(); | 
|---|
|  | 560 | (*listInit->begin_initializers())->accept( *this ); | 
|---|
| [2c2242c] | 561 | } // if | 
|---|
| [bdd516a] | 562 | #endif | 
|---|
| [a32b204] | 563 | } | 
|---|
| [71f4e4f] | 564 |  | 
|---|
| [f1e012b] | 565 | // ConstructorInit - fall back on C-style initializer | 
|---|
|  | 566 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { | 
|---|
|  | 567 | // could not find valid constructor, or found an intrinsic constructor | 
|---|
|  | 568 | // fall back on C-style initializer | 
|---|
|  | 569 | delete ctorInit->get_ctor(); | 
|---|
|  | 570 | ctorInit->set_ctor( NULL ); | 
|---|
| [71a145de] | 571 | delete ctorInit->get_dtor(); | 
|---|
|  | 572 | ctorInit->set_dtor( NULL ); | 
|---|
| [f1e012b] | 573 | maybeAccept( ctorInit->get_init(), *this ); | 
|---|
|  | 574 | } | 
|---|
|  | 575 |  | 
|---|
| [1d2b64f] | 576 | // needs to be callable from outside the resolver, so this is a standalone function | 
|---|
|  | 577 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { | 
|---|
|  | 578 | assert( ctorInit ); | 
|---|
|  | 579 | Resolver resolver( indexer ); | 
|---|
|  | 580 | ctorInit->accept( resolver ); | 
|---|
|  | 581 | } | 
|---|
|  | 582 |  | 
|---|
|  | 583 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { | 
|---|
|  | 584 | assert( stmtExpr ); | 
|---|
|  | 585 | Resolver resolver( indexer ); | 
|---|
|  | 586 | stmtExpr->accept( resolver ); | 
|---|
|  | 587 | } | 
|---|
|  | 588 |  | 
|---|
| [71f4e4f] | 589 | void Resolver::visit( ConstructorInit *ctorInit ) { | 
|---|
| [1ba88a0] | 590 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit | 
|---|
|  | 591 | maybeAccept( ctorInit->get_ctor(), *this ); | 
|---|
|  | 592 | maybeAccept( ctorInit->get_dtor(), *this ); | 
|---|
| [071a31a] | 593 |  | 
|---|
| [5b2f5bb] | 594 | // found a constructor - can get rid of C-style initializer | 
|---|
|  | 595 | delete ctorInit->get_init(); | 
|---|
|  | 596 | ctorInit->set_init( NULL ); | 
|---|
| [ec79847] | 597 |  | 
|---|
|  | 598 | // intrinsic single parameter constructors and destructors do nothing. Since this was | 
|---|
|  | 599 | // implicitly generated, there's no way for it to have side effects, so get rid of it | 
|---|
|  | 600 | // to clean up generated code. | 
|---|
| [f9cebb5] | 601 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
| [ec79847] | 602 | delete ctorInit->get_ctor(); | 
|---|
|  | 603 | ctorInit->set_ctor( NULL ); | 
|---|
|  | 604 | } | 
|---|
| [f9cebb5] | 605 |  | 
|---|
|  | 606 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { | 
|---|
| [ec79847] | 607 | delete ctorInit->get_dtor(); | 
|---|
|  | 608 | ctorInit->set_dtor( NULL ); | 
|---|
|  | 609 | } | 
|---|
| [a465caff] | 610 |  | 
|---|
|  | 611 | // xxx - todo -- what about arrays? | 
|---|
|  | 612 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
|  | 613 | //      // can reduce the constructor down to a SingleInit using the | 
|---|
|  | 614 | //      // second argument from the ctor call, since | 
|---|
|  | 615 | //      delete ctorInit->get_ctor(); | 
|---|
|  | 616 | //      ctorInit->set_ctor( NULL ); | 
|---|
|  | 617 |  | 
|---|
|  | 618 | //      Expression * arg = | 
|---|
|  | 619 | //      ctorInit->set_init( new SingleInit( arg ) ); | 
|---|
|  | 620 | // } | 
|---|
| [71f4e4f] | 621 | } | 
|---|
| [51b73452] | 622 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 623 |  | 
|---|
|  | 624 | // Local Variables: // | 
|---|
|  | 625 | // tab-width: 4 // | 
|---|
|  | 626 | // mode: c++ // | 
|---|
|  | 627 | // compile-command: "make install" // | 
|---|
|  | 628 | // End: // | 
|---|