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