| [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 | 
|---|
| [cbce272] | 11 | // Last Modified By : Andrew Beach | 
|---|
|  | 12 | // Last Modified On : Tus Aug  8 16:06:00 2017 | 
|---|
|  | 13 | // Update Count     : 212 | 
|---|
| [a32b204] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [ea6332d] | 16 | #include <stddef.h>                      // for NULL | 
|---|
|  | 17 | #include <cassert>                       // for safe_dynamic_cast, assert | 
|---|
|  | 18 | #include <memory>                        // for allocator, allocator_traits<... | 
|---|
|  | 19 | #include <tuple>                         // for get | 
|---|
|  | 20 |  | 
|---|
|  | 21 | #include "Alternative.h"                 // for Alternative, AltList | 
|---|
|  | 22 | #include "AlternativeFinder.h"           // for AlternativeFinder, resolveIn... | 
|---|
|  | 23 | #include "Common/SemanticError.h"        // for SemanticError | 
|---|
|  | 24 | #include "Common/utility.h"              // for ValueGuard, group_iterate | 
|---|
|  | 25 | #include "CurrentObject.h"               // for CurrentObject | 
|---|
|  | 26 | #include "InitTweak/InitTweak.h"         // for isIntrinsicSingleArgCallStmt | 
|---|
|  | 27 | #include "RenameVars.h"                  // for RenameVars, global_renamer | 
|---|
|  | 28 | #include "ResolvExpr/TypeEnvironment.h"  // for TypeEnvironment | 
|---|
|  | 29 | #include "ResolveTypeof.h"               // for resolveTypeof | 
|---|
| [e4d829b] | 30 | #include "Resolver.h" | 
|---|
| [ea6332d] | 31 | #include "SymTab/Autogen.h"              // for SizeType | 
|---|
|  | 32 | #include "SymTab/Indexer.h"              // for Indexer | 
|---|
|  | 33 | #include "SynTree/Declaration.h"         // for ObjectDecl, TypeDecl, Declar... | 
|---|
|  | 34 | #include "SynTree/Expression.h"          // for Expression, CastExpr, InitExpr | 
|---|
|  | 35 | #include "SynTree/Initializer.h"         // for ConstructorInit, SingleInit | 
|---|
|  | 36 | #include "SynTree/Statement.h"           // for ForStmt, Statement, BranchStmt | 
|---|
|  | 37 | #include "SynTree/Type.h"                // for Type, BasicType, PointerType | 
|---|
|  | 38 | #include "SynTree/TypeSubstitution.h"    // for TypeSubstitution | 
|---|
|  | 39 | #include "SynTree/Visitor.h"             // for acceptAll, maybeAccept | 
|---|
|  | 40 | #include "typeops.h"                     // for extractResultType | 
|---|
| [51b73452] | 41 |  | 
|---|
| [d9a0e76] | 42 | using namespace std; | 
|---|
| [51b73452] | 43 |  | 
|---|
| [d9a0e76] | 44 | namespace ResolvExpr { | 
|---|
| [62e5546] | 45 | class Resolver final : public SymTab::Indexer { | 
|---|
| [a32b204] | 46 | public: | 
|---|
| [77971f6] | 47 | Resolver() : SymTab::Indexer( false ) {} | 
|---|
| [1d2b64f] | 48 | Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) { | 
|---|
|  | 49 | if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) { | 
|---|
|  | 50 | functionReturn = res->functionReturn; | 
|---|
| [e4d829b] | 51 | currentObject = res->currentObject; | 
|---|
| [1d2b64f] | 52 | inEnumDecl = res->inEnumDecl; | 
|---|
|  | 53 | } | 
|---|
|  | 54 | } | 
|---|
| [71f4e4f] | 55 |  | 
|---|
| [1d2b64f] | 56 | typedef SymTab::Indexer Parent; | 
|---|
|  | 57 | using Parent::visit; | 
|---|
| [62e5546] | 58 | virtual void visit( FunctionDecl *functionDecl ) override; | 
|---|
|  | 59 | virtual void visit( ObjectDecl *functionDecl ) override; | 
|---|
|  | 60 | virtual void visit( TypeDecl *typeDecl ) override; | 
|---|
|  | 61 | virtual void visit( EnumDecl * enumDecl ) override; | 
|---|
| [94b4364] | 62 |  | 
|---|
| [62e5546] | 63 | virtual void visit( ArrayType * at ) override; | 
|---|
|  | 64 | virtual void visit( PointerType * at ) override; | 
|---|
| [d9a0e76] | 65 |  | 
|---|
| [62e5546] | 66 | virtual void visit( ExprStmt *exprStmt ) override; | 
|---|
|  | 67 | virtual void visit( AsmExpr *asmExpr ) override; | 
|---|
|  | 68 | virtual void visit( AsmStmt *asmStmt ) override; | 
|---|
|  | 69 | virtual void visit( IfStmt *ifStmt ) override; | 
|---|
|  | 70 | virtual void visit( WhileStmt *whileStmt ) override; | 
|---|
|  | 71 | virtual void visit( ForStmt *forStmt ) override; | 
|---|
|  | 72 | virtual void visit( SwitchStmt *switchStmt ) override; | 
|---|
|  | 73 | virtual void visit( CaseStmt *caseStmt ) override; | 
|---|
|  | 74 | virtual void visit( BranchStmt *branchStmt ) override; | 
|---|
|  | 75 | virtual void visit( ReturnStmt *returnStmt ) override; | 
|---|
| [307a732] | 76 | virtual void visit( ThrowStmt *throwStmt ) override; | 
|---|
| [cbce272] | 77 | virtual void visit( CatchStmt *catchStmt ) override; | 
|---|
| [d9a0e76] | 78 |  | 
|---|
| [62e5546] | 79 | virtual void visit( SingleInit *singleInit ) override; | 
|---|
|  | 80 | virtual void visit( ListInit *listInit ) override; | 
|---|
|  | 81 | virtual void visit( ConstructorInit *ctorInit ) override; | 
|---|
| [a32b204] | 82 | private: | 
|---|
| [94b4364] | 83 | typedef std::list< Initializer * >::iterator InitIterator; | 
|---|
|  | 84 |  | 
|---|
| [40e636a] | 85 | template< typename PtrType > | 
|---|
|  | 86 | void handlePtrType( PtrType * type ); | 
|---|
|  | 87 |  | 
|---|
| [30b65d8] | 88 | void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & ); | 
|---|
|  | 89 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub ); | 
|---|
| [f1e012b] | 90 | void fallbackInit( ConstructorInit * ctorInit ); | 
|---|
| [b726084] | 91 |  | 
|---|
| [77971f6] | 92 | Type * functionReturn = nullptr; | 
|---|
| [e4d829b] | 93 | CurrentObject currentObject = nullptr; | 
|---|
| [a436947] | 94 | bool inEnumDecl = false; | 
|---|
| [a32b204] | 95 | }; | 
|---|
| [d9a0e76] | 96 |  | 
|---|
| [a32b204] | 97 | void resolve( std::list< Declaration * > translationUnit ) { | 
|---|
|  | 98 | Resolver resolver; | 
|---|
|  | 99 | acceptAll( translationUnit, resolver ); | 
|---|
| [d9a0e76] | 100 | } | 
|---|
|  | 101 |  | 
|---|
| [a32b204] | 102 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { | 
|---|
|  | 103 | TypeEnvironment env; | 
|---|
|  | 104 | return resolveInVoidContext( expr, indexer, env ); | 
|---|
| [d9a0e76] | 105 | } | 
|---|
| [a32b204] | 106 |  | 
|---|
| [db4ecc5] | 107 |  | 
|---|
| [a32b204] | 108 | namespace { | 
|---|
|  | 109 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { | 
|---|
|  | 110 | expr->set_env( new TypeSubstitution ); | 
|---|
|  | 111 | env.makeSubstitution( *expr->get_env() ); | 
|---|
|  | 112 | } | 
|---|
| [db4ecc5] | 113 | } // namespace | 
|---|
| [a32b204] | 114 |  | 
|---|
| [db4ecc5] | 115 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 116 | global_renamer.reset(); | 
|---|
|  | 117 | TypeEnvironment env; | 
|---|
|  | 118 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); | 
|---|
|  | 119 | finishExpr( newExpr, env ); | 
|---|
|  | 120 | return newExpr; | 
|---|
|  | 121 | } | 
|---|
| [71f4e4f] | 122 |  | 
|---|
| [db4ecc5] | 123 | namespace { | 
|---|
| [a32b204] | 124 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 125 | TypeEnvironment env; | 
|---|
|  | 126 | AlternativeFinder finder( indexer, env ); | 
|---|
|  | 127 | finder.find( untyped ); | 
|---|
| [d9a0e76] | 128 | #if 0 | 
|---|
| [a32b204] | 129 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 130 | std::cout << "untyped expr is "; | 
|---|
|  | 131 | untyped->print( std::cout ); | 
|---|
|  | 132 | std::cout << std::endl << "alternatives are:"; | 
|---|
|  | 133 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 134 | i->print( std::cout ); | 
|---|
|  | 135 | } // for | 
|---|
|  | 136 | } // if | 
|---|
| [d9a0e76] | 137 | #endif | 
|---|
| [0b150ec] | 138 | assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end." ); | 
|---|
| [a32b204] | 139 | Alternative &choice = finder.get_alternatives().front(); | 
|---|
|  | 140 | Expression *newExpr = choice.expr->clone(); | 
|---|
|  | 141 | finishExpr( newExpr, choice.env ); | 
|---|
|  | 142 | return newExpr; | 
|---|
|  | 143 | } | 
|---|
| [d9a0e76] | 144 |  | 
|---|
| [a32b204] | 145 | bool isIntegralType( Type *type ) { | 
|---|
|  | 146 | if ( dynamic_cast< EnumInstType * >( type ) ) { | 
|---|
|  | 147 | return true; | 
|---|
|  | 148 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { | 
|---|
|  | 149 | return bt->isInteger(); | 
|---|
| [89e6ffc] | 150 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { | 
|---|
|  | 151 | return true; | 
|---|
| [a32b204] | 152 | } else { | 
|---|
|  | 153 | return false; | 
|---|
|  | 154 | } // if | 
|---|
|  | 155 | } | 
|---|
| [71f4e4f] | 156 |  | 
|---|
| [a32b204] | 157 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { | 
|---|
|  | 158 | TypeEnvironment env; | 
|---|
|  | 159 | AlternativeFinder finder( indexer, env ); | 
|---|
|  | 160 | finder.find( untyped ); | 
|---|
| [d9a0e76] | 161 | #if 0 | 
|---|
| [a32b204] | 162 | if ( finder.get_alternatives().size() != 1 ) { | 
|---|
|  | 163 | std::cout << "untyped expr is "; | 
|---|
|  | 164 | untyped->print( std::cout ); | 
|---|
|  | 165 | std::cout << std::endl << "alternatives are:"; | 
|---|
|  | 166 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
|  | 167 | i->print( std::cout ); | 
|---|
|  | 168 | } // for | 
|---|
|  | 169 | } // if | 
|---|
| [d9a0e76] | 170 | #endif | 
|---|
| [a32b204] | 171 | Expression *newExpr = 0; | 
|---|
|  | 172 | const TypeEnvironment *newEnv = 0; | 
|---|
|  | 173 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { | 
|---|
| [906e24d] | 174 | if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) { | 
|---|
| [a32b204] | 175 | if ( newExpr ) { | 
|---|
|  | 176 | throw SemanticError( "Too many interpretations for case control expression", untyped ); | 
|---|
|  | 177 | } else { | 
|---|
|  | 178 | newExpr = i->expr->clone(); | 
|---|
|  | 179 | newEnv = &i->env; | 
|---|
|  | 180 | } // if | 
|---|
|  | 181 | } // if | 
|---|
|  | 182 | } // for | 
|---|
|  | 183 | if ( ! newExpr ) { | 
|---|
|  | 184 | throw SemanticError( "No interpretations for case control expression", untyped ); | 
|---|
|  | 185 | } // if | 
|---|
|  | 186 | finishExpr( newExpr, *newEnv ); | 
|---|
|  | 187 | return newExpr; | 
|---|
|  | 188 | } | 
|---|
| [71f4e4f] | 189 |  | 
|---|
| [a32b204] | 190 | } | 
|---|
| [71f4e4f] | 191 |  | 
|---|
| [a32b204] | 192 | void Resolver::visit( ObjectDecl *objectDecl ) { | 
|---|
|  | 193 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); | 
|---|
|  | 194 | objectDecl->set_type( new_type ); | 
|---|
| [3cfe27f] | 195 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable | 
|---|
|  | 196 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes | 
|---|
|  | 197 | // initContext because of a function type can contain object declarations in the return and parameter types. So | 
|---|
|  | 198 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting | 
|---|
|  | 199 | // the RHS. | 
|---|
| [e4d829b] | 200 | ValueGuard<CurrentObject> temp( currentObject ); | 
|---|
|  | 201 | currentObject = CurrentObject( objectDecl->get_type() ); | 
|---|
|  | 202 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { | 
|---|
| [a436947] | 203 | // enumerator initializers should not use the enum type to initialize, since | 
|---|
|  | 204 | // the enum type is still incomplete at this point. Use signed int instead. | 
|---|
| [e4d829b] | 205 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); | 
|---|
| [a436947] | 206 | } | 
|---|
| [1d2b64f] | 207 | Parent::visit( objectDecl ); | 
|---|
| [e4d829b] | 208 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { | 
|---|
| [a436947] | 209 | // delete newly created signed int type | 
|---|
| [e4d829b] | 210 | // delete currentObject.getType(); | 
|---|
| [a436947] | 211 | } | 
|---|
| [bfbf97f] | 212 | } | 
|---|
|  | 213 |  | 
|---|
| [40e636a] | 214 | template< typename PtrType > | 
|---|
|  | 215 | void Resolver::handlePtrType( PtrType * type ) { | 
|---|
|  | 216 | if ( type->get_dimension() ) { | 
|---|
|  | 217 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); | 
|---|
| [bfbf97f] | 218 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
| [40e636a] | 219 | delete type->get_dimension(); | 
|---|
|  | 220 | type->set_dimension( newExpr ); | 
|---|
| [d1d17f5] | 221 | } | 
|---|
| [40e636a] | 222 | } | 
|---|
|  | 223 |  | 
|---|
|  | 224 | void Resolver::visit( ArrayType * at ) { | 
|---|
|  | 225 | handlePtrType( at ); | 
|---|
| [1d2b64f] | 226 | Parent::visit( at ); | 
|---|
| [a32b204] | 227 | } | 
|---|
| [94b4364] | 228 |  | 
|---|
| [40e636a] | 229 | void Resolver::visit( PointerType * pt ) { | 
|---|
|  | 230 | handlePtrType( pt ); | 
|---|
| [1d2b64f] | 231 | Parent::visit( pt ); | 
|---|
| [40e636a] | 232 | } | 
|---|
|  | 233 |  | 
|---|
| [a32b204] | 234 | void Resolver::visit( TypeDecl *typeDecl ) { | 
|---|
|  | 235 | if ( typeDecl->get_base() ) { | 
|---|
|  | 236 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); | 
|---|
|  | 237 | typeDecl->set_base( new_type ); | 
|---|
|  | 238 | } // if | 
|---|
| [1d2b64f] | 239 | Parent::visit( typeDecl ); | 
|---|
| [a32b204] | 240 | } | 
|---|
| [94b4364] | 241 |  | 
|---|
| [a32b204] | 242 | void Resolver::visit( FunctionDecl *functionDecl ) { | 
|---|
| [d9a0e76] | 243 | #if 0 | 
|---|
| [a32b204] | 244 | std::cout << "resolver visiting functiondecl "; | 
|---|
|  | 245 | functionDecl->print( std::cout ); | 
|---|
|  | 246 | std::cout << std::endl; | 
|---|
| [d9a0e76] | 247 | #endif | 
|---|
| [a32b204] | 248 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); | 
|---|
|  | 249 | functionDecl->set_type( new_type ); | 
|---|
| [906e24d] | 250 | ValueGuard< Type * > oldFunctionReturn( functionReturn ); | 
|---|
|  | 251 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); | 
|---|
| [1d2b64f] | 252 | Parent::visit( functionDecl ); | 
|---|
| [88d1066] | 253 |  | 
|---|
|  | 254 | // default value expressions have an environment which shouldn't be there and trips up later passes. | 
|---|
|  | 255 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently | 
|---|
|  | 256 | // see how it's useful. | 
|---|
|  | 257 | for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) { | 
|---|
|  | 258 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { | 
|---|
|  | 259 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) { | 
|---|
|  | 260 | delete init->get_value()->get_env(); | 
|---|
|  | 261 | init->get_value()->set_env( nullptr ); | 
|---|
|  | 262 | } | 
|---|
|  | 263 | } | 
|---|
|  | 264 | } | 
|---|
| [a32b204] | 265 | } | 
|---|
| [51b73452] | 266 |  | 
|---|
| [a436947] | 267 | void Resolver::visit( EnumDecl * enumDecl ) { | 
|---|
|  | 268 | // in case we decide to allow nested enums | 
|---|
| [1d2b64f] | 269 | ValueGuard< bool > oldInEnumDecl( inEnumDecl ); | 
|---|
| [a436947] | 270 | inEnumDecl = true; | 
|---|
| [1d2b64f] | 271 | Parent::visit( enumDecl ); | 
|---|
| [a436947] | 272 | } | 
|---|
|  | 273 |  | 
|---|
| [a32b204] | 274 | void Resolver::visit( ExprStmt *exprStmt ) { | 
|---|
| [1d2b64f] | 275 | assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" ); | 
|---|
|  | 276 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); | 
|---|
|  | 277 | delete exprStmt->get_expr(); | 
|---|
|  | 278 | exprStmt->set_expr( newExpr ); | 
|---|
| [a32b204] | 279 | } | 
|---|
| [51b73452] | 280 |  | 
|---|
| [7f5566b] | 281 | void Resolver::visit( AsmExpr *asmExpr ) { | 
|---|
|  | 282 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this ); | 
|---|
|  | 283 | delete asmExpr->get_operand(); | 
|---|
|  | 284 | asmExpr->set_operand( newExpr ); | 
|---|
|  | 285 | if ( asmExpr->get_inout() ) { | 
|---|
|  | 286 | newExpr = findVoidExpression( asmExpr->get_inout(), *this ); | 
|---|
|  | 287 | delete asmExpr->get_inout(); | 
|---|
|  | 288 | asmExpr->set_inout( newExpr ); | 
|---|
|  | 289 | } // if | 
|---|
|  | 290 | } | 
|---|
|  | 291 |  | 
|---|
|  | 292 | void Resolver::visit( AsmStmt *asmStmt ) { | 
|---|
|  | 293 | acceptAll( asmStmt->get_input(), *this); | 
|---|
|  | 294 | acceptAll( asmStmt->get_output(), *this); | 
|---|
|  | 295 | } | 
|---|
|  | 296 |  | 
|---|
| [a32b204] | 297 | void Resolver::visit( IfStmt *ifStmt ) { | 
|---|
|  | 298 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); | 
|---|
|  | 299 | delete ifStmt->get_condition(); | 
|---|
|  | 300 | ifStmt->set_condition( newExpr ); | 
|---|
| [1d2b64f] | 301 | Parent::visit( ifStmt ); | 
|---|
| [a32b204] | 302 | } | 
|---|
| [51b73452] | 303 |  | 
|---|
| [a32b204] | 304 | void Resolver::visit( WhileStmt *whileStmt ) { | 
|---|
|  | 305 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); | 
|---|
|  | 306 | delete whileStmt->get_condition(); | 
|---|
|  | 307 | whileStmt->set_condition( newExpr ); | 
|---|
| [1d2b64f] | 308 | Parent::visit( whileStmt ); | 
|---|
| [a32b204] | 309 | } | 
|---|
| [51b73452] | 310 |  | 
|---|
| [a32b204] | 311 | void Resolver::visit( ForStmt *forStmt ) { | 
|---|
| [1d2b64f] | 312 | Parent::visit( forStmt ); | 
|---|
| [145f1fc] | 313 |  | 
|---|
| [a32b204] | 314 | if ( forStmt->get_condition() ) { | 
|---|
| [145f1fc] | 315 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this ); | 
|---|
| [a32b204] | 316 | delete forStmt->get_condition(); | 
|---|
|  | 317 | forStmt->set_condition( newExpr ); | 
|---|
|  | 318 | } // if | 
|---|
| [71f4e4f] | 319 |  | 
|---|
| [a32b204] | 320 | if ( forStmt->get_increment() ) { | 
|---|
| [145f1fc] | 321 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this ); | 
|---|
| [a32b204] | 322 | delete forStmt->get_increment(); | 
|---|
|  | 323 | forStmt->set_increment( newExpr ); | 
|---|
|  | 324 | } // if | 
|---|
|  | 325 | } | 
|---|
| [51b73452] | 326 |  | 
|---|
| [32b8144] | 327 | void Resolver::visit( SwitchStmt *switchStmt ) { | 
|---|
| [e4d829b] | 328 | ValueGuard< CurrentObject > oldCurrentObject( currentObject ); | 
|---|
| [a32b204] | 329 | Expression *newExpr; | 
|---|
| [32b8144] | 330 | newExpr = findIntegralExpression( switchStmt->get_condition(), *this ); | 
|---|
| [a32b204] | 331 | delete switchStmt->get_condition(); | 
|---|
|  | 332 | switchStmt->set_condition( newExpr ); | 
|---|
| [71f4e4f] | 333 |  | 
|---|
| [e4d829b] | 334 | currentObject = CurrentObject( newExpr->get_result() ); | 
|---|
| [32b8144] | 335 | Parent::visit( switchStmt ); | 
|---|
| [a32b204] | 336 | } | 
|---|
| [51b73452] | 337 |  | 
|---|
| [a32b204] | 338 | void Resolver::visit( CaseStmt *caseStmt ) { | 
|---|
| [32b8144] | 339 | if ( caseStmt->get_condition() ) { | 
|---|
| [e4d829b] | 340 | std::list< InitAlternative > initAlts = currentObject.getOptions(); | 
|---|
|  | 341 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); | 
|---|
|  | 342 | CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() ); | 
|---|
| [32b8144] | 343 | Expression * newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 344 | castExpr = safe_dynamic_cast< CastExpr * >( newExpr ); | 
|---|
|  | 345 | caseStmt->set_condition( castExpr->get_arg() ); | 
|---|
|  | 346 | castExpr->set_arg( nullptr ); | 
|---|
|  | 347 | delete castExpr; | 
|---|
|  | 348 | } | 
|---|
| [1d2b64f] | 349 | Parent::visit( caseStmt ); | 
|---|
| [a32b204] | 350 | } | 
|---|
| [51b73452] | 351 |  | 
|---|
| [de62360d] | 352 | void Resolver::visit( BranchStmt *branchStmt ) { | 
|---|
|  | 353 | // must resolve the argument for a computed goto | 
|---|
|  | 354 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement | 
|---|
| [2871210] | 355 | if ( Expression * arg = branchStmt->get_computedTarget() ) { | 
|---|
| [de62360d] | 356 | VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder | 
|---|
|  | 357 | PointerType pt( Type::Qualifiers(), v.clone() ); | 
|---|
|  | 358 | CastExpr * castExpr = new CastExpr( arg, pt.clone() ); | 
|---|
|  | 359 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression | 
|---|
|  | 360 | branchStmt->set_target( newExpr ); | 
|---|
|  | 361 | } // if | 
|---|
|  | 362 | } // if | 
|---|
|  | 363 | } | 
|---|
|  | 364 |  | 
|---|
| [a32b204] | 365 | void Resolver::visit( ReturnStmt *returnStmt ) { | 
|---|
|  | 366 | if ( returnStmt->get_expr() ) { | 
|---|
| [906e24d] | 367 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); | 
|---|
| [a32b204] | 368 | Expression *newExpr = findSingleExpression( castExpr, *this ); | 
|---|
|  | 369 | delete castExpr; | 
|---|
|  | 370 | returnStmt->set_expr( newExpr ); | 
|---|
|  | 371 | } // if | 
|---|
|  | 372 | } | 
|---|
| [51b73452] | 373 |  | 
|---|
| [307a732] | 374 | void Resolver::visit( ThrowStmt *throwStmt ) { | 
|---|
| [cbce272] | 375 | // TODO: Replace *exception type with &exception type. | 
|---|
| [307a732] | 376 | if ( throwStmt->get_expr() ) { | 
|---|
| [cbce272] | 377 | StructDecl * exception_decl = | 
|---|
|  | 378 | lookupStruct( "__cfaehm__base_exception_t" ); | 
|---|
|  | 379 | assert( exception_decl ); | 
|---|
|  | 380 | Expression * wrapped = new CastExpr( | 
|---|
|  | 381 | throwStmt->get_expr(), | 
|---|
|  | 382 | new PointerType( | 
|---|
|  | 383 | noQualifiers, | 
|---|
|  | 384 | new StructInstType( | 
|---|
|  | 385 | noQualifiers, | 
|---|
|  | 386 | exception_decl | 
|---|
|  | 387 | ) | 
|---|
|  | 388 | ) | 
|---|
|  | 389 | ); | 
|---|
| [307a732] | 390 | Expression * newExpr = findSingleExpression( wrapped, *this ); | 
|---|
|  | 391 | throwStmt->set_expr( newExpr ); | 
|---|
|  | 392 | } | 
|---|
|  | 393 | } | 
|---|
|  | 394 |  | 
|---|
| [cbce272] | 395 | void Resolver::visit( CatchStmt *catchStmt ) { | 
|---|
| [3f27b9a] | 396 | // inline Indexer::visit so that the exception variable is still in-scope for | 
|---|
|  | 397 | // findSingleExpression() below | 
|---|
|  | 398 | Parent::enterScope(); | 
|---|
|  | 399 | Visitor::visit( catchStmt ); | 
|---|
| [6a36975] | 400 |  | 
|---|
| [cbce272] | 401 | if ( catchStmt->get_cond() ) { | 
|---|
|  | 402 | Expression * wrapped = new CastExpr( | 
|---|
|  | 403 | catchStmt->get_cond(), | 
|---|
|  | 404 | new BasicType( noQualifiers, BasicType::Bool ) | 
|---|
|  | 405 | ); | 
|---|
|  | 406 | catchStmt->set_cond( findSingleExpression( wrapped, *this ) ); | 
|---|
|  | 407 | } | 
|---|
| [3f27b9a] | 408 |  | 
|---|
|  | 409 | Parent::leaveScope(); | 
|---|
| [cbce272] | 410 | } | 
|---|
|  | 411 |  | 
|---|
| [b5c5684] | 412 | template< typename T > | 
|---|
|  | 413 | bool isCharType( T t ) { | 
|---|
|  | 414 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { | 
|---|
| [71f4e4f] | 415 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || | 
|---|
| [b5c5684] | 416 | bt->get_kind() == BasicType::UnsignedChar; | 
|---|
|  | 417 | } | 
|---|
|  | 418 | return false; | 
|---|
|  | 419 | } | 
|---|
|  | 420 |  | 
|---|
| [e4d829b] | 421 | void Resolver::visit( SingleInit *singleInit ) { | 
|---|
| [62423350] | 422 | // resolve initialization using the possibilities as determined by the currentObject cursor | 
|---|
| [e4d829b] | 423 | UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() ); | 
|---|
|  | 424 | Expression * newExpr = findSingleExpression( untyped, *this ); | 
|---|
|  | 425 | InitExpr * initExpr = safe_dynamic_cast< InitExpr * >( newExpr ); | 
|---|
| [62423350] | 426 |  | 
|---|
|  | 427 | // move cursor to the object that is actually initialized | 
|---|
| [e4d829b] | 428 | currentObject.setNext( initExpr->get_designation() ); | 
|---|
| [62423350] | 429 |  | 
|---|
|  | 430 | // discard InitExpr wrapper and retain relevant pieces | 
|---|
|  | 431 | newExpr = initExpr->get_expr(); | 
|---|
| [435e75f] | 432 | newExpr->set_env( initExpr->get_env() ); | 
|---|
| [62423350] | 433 | initExpr->set_expr( nullptr ); | 
|---|
|  | 434 | initExpr->set_env( nullptr ); | 
|---|
| [e4d829b] | 435 | delete initExpr; | 
|---|
|  | 436 |  | 
|---|
| [62423350] | 437 | // get the actual object's type (may not exactly match what comes back from the resolver due to conversions) | 
|---|
|  | 438 | Type * initContext = currentObject.getCurrentType(); | 
|---|
|  | 439 |  | 
|---|
|  | 440 | // check if actual object's type is char[] | 
|---|
|  | 441 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { | 
|---|
|  | 442 | if ( isCharType( at->get_base() ) ) { | 
|---|
|  | 443 | // check if the resolved type is char * | 
|---|
|  | 444 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { | 
|---|
|  | 445 | if ( isCharType( pt->get_base() ) ) { | 
|---|
|  | 446 | // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello"; | 
|---|
|  | 447 | CastExpr *ce = safe_dynamic_cast< CastExpr * >( newExpr ); | 
|---|
|  | 448 | newExpr = ce->get_arg(); | 
|---|
|  | 449 | ce->set_arg( nullptr ); | 
|---|
|  | 450 | delete ce; | 
|---|
|  | 451 | } | 
|---|
|  | 452 | } | 
|---|
|  | 453 | } | 
|---|
|  | 454 | } | 
|---|
| [94b4364] | 455 |  | 
|---|
| [62423350] | 456 | // set initializer expr to resolved express | 
|---|
|  | 457 | singleInit->set_value( newExpr ); | 
|---|
|  | 458 |  | 
|---|
|  | 459 | // move cursor to next object in preparation for next initializer | 
|---|
|  | 460 | currentObject.increment(); | 
|---|
|  | 461 | } | 
|---|
| [94b4364] | 462 |  | 
|---|
|  | 463 | void Resolver::visit( ListInit * listInit ) { | 
|---|
| [62423350] | 464 | // move cursor into brace-enclosed initializer-list | 
|---|
| [e4d829b] | 465 | currentObject.enterListInit(); | 
|---|
|  | 466 | // xxx - fix this so that the list isn't copied, iterator should be used to change current element | 
|---|
|  | 467 | std::list<Designation *> newDesignations; | 
|---|
|  | 468 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { | 
|---|
| [62423350] | 469 | // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving | 
|---|
|  | 470 | // the initializer against that object. | 
|---|
| [e4d829b] | 471 | Designation * des = std::get<0>(p); | 
|---|
|  | 472 | Initializer * init = std::get<1>(p); | 
|---|
|  | 473 | newDesignations.push_back( currentObject.findNext( des ) ); | 
|---|
|  | 474 | init->accept( *this ); | 
|---|
| [b5c5684] | 475 | } | 
|---|
| [62423350] | 476 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list | 
|---|
| [e4d829b] | 477 | listInit->get_designations() = newDesignations; // xxx - memory management | 
|---|
|  | 478 | currentObject.exitListInit(); | 
|---|
|  | 479 |  | 
|---|
| [62423350] | 480 | // xxx - this part has not be folded into CurrentObject yet | 
|---|
| [e4d829b] | 481 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { | 
|---|
|  | 482 | //      Type * base = tt->get_baseType()->get_base(); | 
|---|
|  | 483 | //      if ( base ) { | 
|---|
|  | 484 | //              // know the implementation type, so try using that as the initContext | 
|---|
|  | 485 | //              ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); | 
|---|
|  | 486 | //              currentObject = &tmpObj; | 
|---|
|  | 487 | //              visit( listInit ); | 
|---|
|  | 488 | //      } else { | 
|---|
|  | 489 | //              // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context | 
|---|
|  | 490 | //              Parent::visit( listInit ); | 
|---|
|  | 491 | //      } | 
|---|
|  | 492 | // } else { | 
|---|
| [a32b204] | 493 | } | 
|---|
| [71f4e4f] | 494 |  | 
|---|
| [f1e012b] | 495 | // ConstructorInit - fall back on C-style initializer | 
|---|
|  | 496 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { | 
|---|
|  | 497 | // could not find valid constructor, or found an intrinsic constructor | 
|---|
|  | 498 | // fall back on C-style initializer | 
|---|
|  | 499 | delete ctorInit->get_ctor(); | 
|---|
|  | 500 | ctorInit->set_ctor( NULL ); | 
|---|
| [71a145de] | 501 | delete ctorInit->get_dtor(); | 
|---|
|  | 502 | ctorInit->set_dtor( NULL ); | 
|---|
| [f1e012b] | 503 | maybeAccept( ctorInit->get_init(), *this ); | 
|---|
|  | 504 | } | 
|---|
|  | 505 |  | 
|---|
| [1d2b64f] | 506 | // needs to be callable from outside the resolver, so this is a standalone function | 
|---|
|  | 507 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { | 
|---|
|  | 508 | assert( ctorInit ); | 
|---|
|  | 509 | Resolver resolver( indexer ); | 
|---|
|  | 510 | ctorInit->accept( resolver ); | 
|---|
|  | 511 | } | 
|---|
|  | 512 |  | 
|---|
|  | 513 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { | 
|---|
|  | 514 | assert( stmtExpr ); | 
|---|
|  | 515 | Resolver resolver( indexer ); | 
|---|
|  | 516 | stmtExpr->accept( resolver ); | 
|---|
|  | 517 | } | 
|---|
|  | 518 |  | 
|---|
| [71f4e4f] | 519 | void Resolver::visit( ConstructorInit *ctorInit ) { | 
|---|
| [1ba88a0] | 520 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit | 
|---|
|  | 521 | maybeAccept( ctorInit->get_ctor(), *this ); | 
|---|
|  | 522 | maybeAccept( ctorInit->get_dtor(), *this ); | 
|---|
| [071a31a] | 523 |  | 
|---|
| [5b2f5bb] | 524 | // found a constructor - can get rid of C-style initializer | 
|---|
|  | 525 | delete ctorInit->get_init(); | 
|---|
|  | 526 | ctorInit->set_init( NULL ); | 
|---|
| [ec79847] | 527 |  | 
|---|
|  | 528 | // intrinsic single parameter constructors and destructors do nothing. Since this was | 
|---|
|  | 529 | // implicitly generated, there's no way for it to have side effects, so get rid of it | 
|---|
|  | 530 | // to clean up generated code. | 
|---|
| [f9cebb5] | 531 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
| [ec79847] | 532 | delete ctorInit->get_ctor(); | 
|---|
|  | 533 | ctorInit->set_ctor( NULL ); | 
|---|
|  | 534 | } | 
|---|
| [f9cebb5] | 535 |  | 
|---|
|  | 536 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { | 
|---|
| [ec79847] | 537 | delete ctorInit->get_dtor(); | 
|---|
|  | 538 | ctorInit->set_dtor( NULL ); | 
|---|
|  | 539 | } | 
|---|
| [a465caff] | 540 |  | 
|---|
|  | 541 | // xxx - todo -- what about arrays? | 
|---|
|  | 542 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { | 
|---|
|  | 543 | //      // can reduce the constructor down to a SingleInit using the | 
|---|
|  | 544 | //      // second argument from the ctor call, since | 
|---|
|  | 545 | //      delete ctorInit->get_ctor(); | 
|---|
|  | 546 | //      ctorInit->set_ctor( NULL ); | 
|---|
|  | 547 |  | 
|---|
|  | 548 | //      Expression * arg = | 
|---|
|  | 549 | //      ctorInit->set_init( new SingleInit( arg ) ); | 
|---|
|  | 550 | // } | 
|---|
| [71f4e4f] | 551 | } | 
|---|
| [51b73452] | 552 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 553 |  | 
|---|
|  | 554 | // Local Variables: // | 
|---|
|  | 555 | // tab-width: 4 // | 
|---|
|  | 556 | // mode: c++ // | 
|---|
|  | 557 | // compile-command: "make install" // | 
|---|
|  | 558 | // End: // | 
|---|