[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
|
---|
[e3e16bc] | 17 | #include <cassert> // for strict_dynamic_cast, assert
|
---|
[ea6332d] | 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...
|
---|
[a4ca48c] | 23 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
[ea6332d] | 24 | #include "Common/SemanticError.h" // for SemanticError
|
---|
| 25 | #include "Common/utility.h" // for ValueGuard, group_iterate
|
---|
| 26 | #include "CurrentObject.h" // for CurrentObject
|
---|
| 27 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt
|
---|
| 28 | #include "RenameVars.h" // for RenameVars, global_renamer
|
---|
| 29 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment
|
---|
| 30 | #include "ResolveTypeof.h" // for resolveTypeof
|
---|
[e4d829b] | 31 | #include "Resolver.h"
|
---|
[ea6332d] | 32 | #include "SymTab/Autogen.h" // for SizeType
|
---|
| 33 | #include "SymTab/Indexer.h" // for Indexer
|
---|
| 34 | #include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar...
|
---|
| 35 | #include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr
|
---|
| 36 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit
|
---|
| 37 | #include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt
|
---|
| 38 | #include "SynTree/Type.h" // for Type, BasicType, PointerType
|
---|
| 39 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
---|
| 40 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept
|
---|
| 41 | #include "typeops.h" // for extractResultType
|
---|
[51b73452] | 42 |
|
---|
[d9a0e76] | 43 | using namespace std;
|
---|
[51b73452] | 44 |
|
---|
[d9a0e76] | 45 | namespace ResolvExpr {
|
---|
[a4ca48c] | 46 | struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting {
|
---|
| 47 | Resolver() {}
|
---|
| 48 | Resolver( const SymTab::Indexer & other ) {
|
---|
| 49 | indexer = other;
|
---|
[1d2b64f] | 50 | }
|
---|
[71f4e4f] | 51 |
|
---|
[a4ca48c] | 52 | void previsit( FunctionDecl *functionDecl );
|
---|
| 53 | void postvisit( FunctionDecl *functionDecl );
|
---|
| 54 | void previsit( ObjectDecl *functionDecl );
|
---|
| 55 | void previsit( TypeDecl *typeDecl );
|
---|
| 56 | void previsit( EnumDecl * enumDecl );
|
---|
| 57 |
|
---|
| 58 | void previsit( ArrayType * at );
|
---|
| 59 | void previsit( PointerType * at );
|
---|
| 60 |
|
---|
| 61 | void previsit( ExprStmt *exprStmt );
|
---|
| 62 | void previsit( AsmExpr *asmExpr );
|
---|
| 63 | void previsit( AsmStmt *asmStmt );
|
---|
| 64 | void previsit( IfStmt *ifStmt );
|
---|
| 65 | void previsit( WhileStmt *whileStmt );
|
---|
| 66 | void previsit( ForStmt *forStmt );
|
---|
| 67 | void previsit( SwitchStmt *switchStmt );
|
---|
| 68 | void previsit( CaseStmt *caseStmt );
|
---|
| 69 | void previsit( BranchStmt *branchStmt );
|
---|
| 70 | void previsit( ReturnStmt *returnStmt );
|
---|
| 71 | void previsit( ThrowStmt *throwStmt );
|
---|
| 72 | void previsit( CatchStmt *catchStmt );
|
---|
| 73 |
|
---|
| 74 | void previsit( SingleInit *singleInit );
|
---|
| 75 | void previsit( ListInit *listInit );
|
---|
| 76 | void previsit( ConstructorInit *ctorInit );
|
---|
[a32b204] | 77 | private:
|
---|
[94b4364] | 78 | typedef std::list< Initializer * >::iterator InitIterator;
|
---|
| 79 |
|
---|
[40e636a] | 80 | template< typename PtrType >
|
---|
| 81 | void handlePtrType( PtrType * type );
|
---|
| 82 |
|
---|
[30b65d8] | 83 | void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & );
|
---|
| 84 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub );
|
---|
[f1e012b] | 85 | void fallbackInit( ConstructorInit * ctorInit );
|
---|
[b726084] | 86 |
|
---|
[77971f6] | 87 | Type * functionReturn = nullptr;
|
---|
[e4d829b] | 88 | CurrentObject currentObject = nullptr;
|
---|
[a436947] | 89 | bool inEnumDecl = false;
|
---|
[a32b204] | 90 | };
|
---|
[d9a0e76] | 91 |
|
---|
[a32b204] | 92 | void resolve( std::list< Declaration * > translationUnit ) {
|
---|
[a4ca48c] | 93 | PassVisitor<Resolver> resolver;
|
---|
[a32b204] | 94 | acceptAll( translationUnit, resolver );
|
---|
[d9a0e76] | 95 | }
|
---|
| 96 |
|
---|
[8b11840] | 97 | void resolveDecl( Declaration * decl, const SymTab::Indexer &indexer ) {
|
---|
| 98 | PassVisitor<Resolver> resolver( indexer );
|
---|
| 99 | maybeAccept( decl, resolver );
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[a4ca48c] | 102 | // used in resolveTypeof
|
---|
[a32b204] | 103 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
|
---|
| 104 | TypeEnvironment env;
|
---|
| 105 | return resolveInVoidContext( expr, indexer, env );
|
---|
[d9a0e76] | 106 | }
|
---|
[a32b204] | 107 |
|
---|
| 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 |
|
---|
[a4ca48c] | 192 | void Resolver::previsit( ObjectDecl *objectDecl ) {
|
---|
| 193 | Type *new_type = resolveTypeof( objectDecl->get_type(), indexer );
|
---|
[a32b204] | 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.
|
---|
[a4ca48c] | 200 | GuardValue( currentObject );
|
---|
[e4d829b] | 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 | }
|
---|
[bfbf97f] | 207 | }
|
---|
| 208 |
|
---|
[40e636a] | 209 | template< typename PtrType >
|
---|
| 210 | void Resolver::handlePtrType( PtrType * type ) {
|
---|
| 211 | if ( type->get_dimension() ) {
|
---|
| 212 | CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
|
---|
[a4ca48c] | 213 | Expression *newExpr = findSingleExpression( castExpr, indexer );
|
---|
[40e636a] | 214 | delete type->get_dimension();
|
---|
| 215 | type->set_dimension( newExpr );
|
---|
[d1d17f5] | 216 | }
|
---|
[40e636a] | 217 | }
|
---|
| 218 |
|
---|
[a4ca48c] | 219 | void Resolver::previsit( ArrayType * at ) {
|
---|
[40e636a] | 220 | handlePtrType( at );
|
---|
[a32b204] | 221 | }
|
---|
[94b4364] | 222 |
|
---|
[a4ca48c] | 223 | void Resolver::previsit( PointerType * pt ) {
|
---|
[40e636a] | 224 | handlePtrType( pt );
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[a4ca48c] | 227 | void Resolver::previsit( TypeDecl *typeDecl ) {
|
---|
[a32b204] | 228 | if ( typeDecl->get_base() ) {
|
---|
[a4ca48c] | 229 | Type *new_type = resolveTypeof( typeDecl->get_base(), indexer );
|
---|
[a32b204] | 230 | typeDecl->set_base( new_type );
|
---|
| 231 | } // if
|
---|
| 232 | }
|
---|
[94b4364] | 233 |
|
---|
[a4ca48c] | 234 | void Resolver::previsit( FunctionDecl *functionDecl ) {
|
---|
[d9a0e76] | 235 | #if 0
|
---|
[a4ca48c] | 236 | std::cerr << "resolver visiting functiondecl ";
|
---|
| 237 | functionDecl->print( std::cerr );
|
---|
| 238 | std::cerr << std::endl;
|
---|
[d9a0e76] | 239 | #endif
|
---|
[a4ca48c] | 240 | Type *new_type = resolveTypeof( functionDecl->get_type(), indexer );
|
---|
[a32b204] | 241 | functionDecl->set_type( new_type );
|
---|
[a4ca48c] | 242 | GuardValue( functionReturn );
|
---|
[906e24d] | 243 | functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
|
---|
[a4ca48c] | 244 | }
|
---|
[88d1066] | 245 |
|
---|
[a4ca48c] | 246 |
|
---|
| 247 | void Resolver::postvisit( FunctionDecl *functionDecl ) {
|
---|
[88d1066] | 248 | // default value expressions have an environment which shouldn't be there and trips up later passes.
|
---|
| 249 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently
|
---|
| 250 | // see how it's useful.
|
---|
| 251 | for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) {
|
---|
| 252 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) {
|
---|
| 253 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) {
|
---|
| 254 | delete init->get_value()->get_env();
|
---|
| 255 | init->get_value()->set_env( nullptr );
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
[a32b204] | 259 | }
|
---|
[51b73452] | 260 |
|
---|
[a4ca48c] | 261 | void Resolver::previsit( EnumDecl * ) {
|
---|
[a436947] | 262 | // in case we decide to allow nested enums
|
---|
[a4ca48c] | 263 | GuardValue( inEnumDecl );
|
---|
[a436947] | 264 | inEnumDecl = true;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[a4ca48c] | 267 | void Resolver::previsit( ExprStmt *exprStmt ) {
|
---|
| 268 | visit_children = false;
|
---|
[1d2b64f] | 269 | assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
|
---|
[a4ca48c] | 270 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer );
|
---|
[1d2b64f] | 271 | delete exprStmt->get_expr();
|
---|
| 272 | exprStmt->set_expr( newExpr );
|
---|
[a32b204] | 273 | }
|
---|
[51b73452] | 274 |
|
---|
[a4ca48c] | 275 | void Resolver::previsit( AsmExpr *asmExpr ) {
|
---|
| 276 | visit_children = false;
|
---|
| 277 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer );
|
---|
[7f5566b] | 278 | delete asmExpr->get_operand();
|
---|
| 279 | asmExpr->set_operand( newExpr );
|
---|
| 280 | if ( asmExpr->get_inout() ) {
|
---|
[a4ca48c] | 281 | newExpr = findVoidExpression( asmExpr->get_inout(), indexer );
|
---|
[7f5566b] | 282 | delete asmExpr->get_inout();
|
---|
| 283 | asmExpr->set_inout( newExpr );
|
---|
| 284 | } // if
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[a4ca48c] | 287 | void Resolver::previsit( AsmStmt *asmStmt ) {
|
---|
| 288 | visit_children = false;
|
---|
| 289 | acceptAll( asmStmt->get_input(), *visitor );
|
---|
| 290 | acceptAll( asmStmt->get_output(), *visitor );
|
---|
[7f5566b] | 291 | }
|
---|
| 292 |
|
---|
[a4ca48c] | 293 | void Resolver::previsit( IfStmt *ifStmt ) {
|
---|
| 294 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer );
|
---|
[a32b204] | 295 | delete ifStmt->get_condition();
|
---|
| 296 | ifStmt->set_condition( newExpr );
|
---|
| 297 | }
|
---|
[51b73452] | 298 |
|
---|
[a4ca48c] | 299 | void Resolver::previsit( WhileStmt *whileStmt ) {
|
---|
| 300 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer );
|
---|
[a32b204] | 301 | delete whileStmt->get_condition();
|
---|
| 302 | whileStmt->set_condition( newExpr );
|
---|
| 303 | }
|
---|
[51b73452] | 304 |
|
---|
[a4ca48c] | 305 | void Resolver::previsit( ForStmt *forStmt ) {
|
---|
[a32b204] | 306 | if ( forStmt->get_condition() ) {
|
---|
[a4ca48c] | 307 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer );
|
---|
[a32b204] | 308 | delete forStmt->get_condition();
|
---|
| 309 | forStmt->set_condition( newExpr );
|
---|
| 310 | } // if
|
---|
[71f4e4f] | 311 |
|
---|
[a32b204] | 312 | if ( forStmt->get_increment() ) {
|
---|
[a4ca48c] | 313 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer );
|
---|
[a32b204] | 314 | delete forStmt->get_increment();
|
---|
| 315 | forStmt->set_increment( newExpr );
|
---|
| 316 | } // if
|
---|
| 317 | }
|
---|
[51b73452] | 318 |
|
---|
[a4ca48c] | 319 | void Resolver::previsit( SwitchStmt *switchStmt ) {
|
---|
| 320 | GuardValue( currentObject );
|
---|
[a32b204] | 321 | Expression *newExpr;
|
---|
[a4ca48c] | 322 | newExpr = findIntegralExpression( switchStmt->get_condition(), indexer );
|
---|
[a32b204] | 323 | delete switchStmt->get_condition();
|
---|
| 324 | switchStmt->set_condition( newExpr );
|
---|
[71f4e4f] | 325 |
|
---|
[e4d829b] | 326 | currentObject = CurrentObject( newExpr->get_result() );
|
---|
[a32b204] | 327 | }
|
---|
[51b73452] | 328 |
|
---|
[a4ca48c] | 329 | void Resolver::previsit( CaseStmt *caseStmt ) {
|
---|
[32b8144] | 330 | if ( caseStmt->get_condition() ) {
|
---|
[e4d829b] | 331 | std::list< InitAlternative > initAlts = currentObject.getOptions();
|
---|
| 332 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." );
|
---|
| 333 | CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() );
|
---|
[a4ca48c] | 334 | Expression * newExpr = findSingleExpression( castExpr, indexer );
|
---|
[e3e16bc] | 335 | castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
|
---|
[32b8144] | 336 | caseStmt->set_condition( castExpr->get_arg() );
|
---|
| 337 | castExpr->set_arg( nullptr );
|
---|
| 338 | delete castExpr;
|
---|
| 339 | }
|
---|
[a32b204] | 340 | }
|
---|
[51b73452] | 341 |
|
---|
[a4ca48c] | 342 | void Resolver::previsit( BranchStmt *branchStmt ) {
|
---|
| 343 | visit_children = false;
|
---|
[de62360d] | 344 | // must resolve the argument for a computed goto
|
---|
| 345 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
|
---|
[2871210] | 346 | if ( Expression * arg = branchStmt->get_computedTarget() ) {
|
---|
[de62360d] | 347 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
|
---|
| 348 | PointerType pt( Type::Qualifiers(), v.clone() );
|
---|
| 349 | CastExpr * castExpr = new CastExpr( arg, pt.clone() );
|
---|
[a4ca48c] | 350 | Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression
|
---|
[de62360d] | 351 | branchStmt->set_target( newExpr );
|
---|
| 352 | } // if
|
---|
| 353 | } // if
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[a4ca48c] | 356 | void Resolver::previsit( ReturnStmt *returnStmt ) {
|
---|
| 357 | visit_children = false;
|
---|
[a32b204] | 358 | if ( returnStmt->get_expr() ) {
|
---|
[906e24d] | 359 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
|
---|
[a4ca48c] | 360 | Expression *newExpr = findSingleExpression( castExpr, indexer );
|
---|
[a32b204] | 361 | delete castExpr;
|
---|
| 362 | returnStmt->set_expr( newExpr );
|
---|
| 363 | } // if
|
---|
| 364 | }
|
---|
[51b73452] | 365 |
|
---|
[a4ca48c] | 366 | void Resolver::previsit( ThrowStmt *throwStmt ) {
|
---|
| 367 | visit_children = false;
|
---|
[cbce272] | 368 | // TODO: Replace *exception type with &exception type.
|
---|
[307a732] | 369 | if ( throwStmt->get_expr() ) {
|
---|
[cbce272] | 370 | StructDecl * exception_decl =
|
---|
[a4ca48c] | 371 | indexer.lookupStruct( "__cfaehm__base_exception_t" );
|
---|
[cbce272] | 372 | assert( exception_decl );
|
---|
| 373 | Expression * wrapped = new CastExpr(
|
---|
| 374 | throwStmt->get_expr(),
|
---|
| 375 | new PointerType(
|
---|
| 376 | noQualifiers,
|
---|
| 377 | new StructInstType(
|
---|
| 378 | noQualifiers,
|
---|
| 379 | exception_decl
|
---|
| 380 | )
|
---|
| 381 | )
|
---|
| 382 | );
|
---|
[a4ca48c] | 383 | Expression * newExpr = findSingleExpression( wrapped, indexer );
|
---|
[307a732] | 384 | throwStmt->set_expr( newExpr );
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[a4ca48c] | 388 | void Resolver::previsit( CatchStmt *catchStmt ) {
|
---|
[cbce272] | 389 | if ( catchStmt->get_cond() ) {
|
---|
| 390 | Expression * wrapped = new CastExpr(
|
---|
| 391 | catchStmt->get_cond(),
|
---|
| 392 | new BasicType( noQualifiers, BasicType::Bool )
|
---|
| 393 | );
|
---|
[a4ca48c] | 394 | catchStmt->set_cond( findSingleExpression( wrapped, indexer ) );
|
---|
[cbce272] | 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[b5c5684] | 398 | template< typename T >
|
---|
| 399 | bool isCharType( T t ) {
|
---|
| 400 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
|
---|
[71f4e4f] | 401 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
|
---|
[b5c5684] | 402 | bt->get_kind() == BasicType::UnsignedChar;
|
---|
| 403 | }
|
---|
| 404 | return false;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[a4ca48c] | 407 | void Resolver::previsit( SingleInit *singleInit ) {
|
---|
| 408 | visit_children = false;
|
---|
[62423350] | 409 | // resolve initialization using the possibilities as determined by the currentObject cursor
|
---|
[e4d829b] | 410 | UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
|
---|
[a4ca48c] | 411 | Expression * newExpr = findSingleExpression( untyped, indexer );
|
---|
[e3e16bc] | 412 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
|
---|
[62423350] | 413 |
|
---|
| 414 | // move cursor to the object that is actually initialized
|
---|
[e4d829b] | 415 | currentObject.setNext( initExpr->get_designation() );
|
---|
[62423350] | 416 |
|
---|
| 417 | // discard InitExpr wrapper and retain relevant pieces
|
---|
| 418 | newExpr = initExpr->get_expr();
|
---|
[435e75f] | 419 | newExpr->set_env( initExpr->get_env() );
|
---|
[62423350] | 420 | initExpr->set_expr( nullptr );
|
---|
| 421 | initExpr->set_env( nullptr );
|
---|
[e4d829b] | 422 | delete initExpr;
|
---|
| 423 |
|
---|
[62423350] | 424 | // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
|
---|
| 425 | Type * initContext = currentObject.getCurrentType();
|
---|
| 426 |
|
---|
| 427 | // check if actual object's type is char[]
|
---|
| 428 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
|
---|
| 429 | if ( isCharType( at->get_base() ) ) {
|
---|
| 430 | // check if the resolved type is char *
|
---|
| 431 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
|
---|
| 432 | if ( isCharType( pt->get_base() ) ) {
|
---|
| 433 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
|
---|
[e3e16bc] | 434 | CastExpr *ce = strict_dynamic_cast< CastExpr * >( newExpr );
|
---|
[62423350] | 435 | newExpr = ce->get_arg();
|
---|
| 436 | ce->set_arg( nullptr );
|
---|
| 437 | delete ce;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | }
|
---|
[94b4364] | 442 |
|
---|
[62423350] | 443 | // set initializer expr to resolved express
|
---|
| 444 | singleInit->set_value( newExpr );
|
---|
| 445 |
|
---|
| 446 | // move cursor to next object in preparation for next initializer
|
---|
| 447 | currentObject.increment();
|
---|
| 448 | }
|
---|
[94b4364] | 449 |
|
---|
[a4ca48c] | 450 | void Resolver::previsit( ListInit * listInit ) {
|
---|
| 451 | visit_children = false;
|
---|
[62423350] | 452 | // move cursor into brace-enclosed initializer-list
|
---|
[e4d829b] | 453 | currentObject.enterListInit();
|
---|
| 454 | // xxx - fix this so that the list isn't copied, iterator should be used to change current element
|
---|
| 455 | std::list<Designation *> newDesignations;
|
---|
| 456 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) {
|
---|
[62423350] | 457 | // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving
|
---|
| 458 | // the initializer against that object.
|
---|
[e4d829b] | 459 | Designation * des = std::get<0>(p);
|
---|
| 460 | Initializer * init = std::get<1>(p);
|
---|
| 461 | newDesignations.push_back( currentObject.findNext( des ) );
|
---|
[a4ca48c] | 462 | init->accept( *visitor );
|
---|
[b5c5684] | 463 | }
|
---|
[62423350] | 464 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
|
---|
[e4d829b] | 465 | listInit->get_designations() = newDesignations; // xxx - memory management
|
---|
| 466 | currentObject.exitListInit();
|
---|
| 467 |
|
---|
[62423350] | 468 | // xxx - this part has not be folded into CurrentObject yet
|
---|
[e4d829b] | 469 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
|
---|
| 470 | // Type * base = tt->get_baseType()->get_base();
|
---|
| 471 | // if ( base ) {
|
---|
| 472 | // // know the implementation type, so try using that as the initContext
|
---|
| 473 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr );
|
---|
| 474 | // currentObject = &tmpObj;
|
---|
| 475 | // visit( listInit );
|
---|
| 476 | // } else {
|
---|
| 477 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
|
---|
| 478 | // Parent::visit( listInit );
|
---|
| 479 | // }
|
---|
| 480 | // } else {
|
---|
[a32b204] | 481 | }
|
---|
[71f4e4f] | 482 |
|
---|
[f1e012b] | 483 | // ConstructorInit - fall back on C-style initializer
|
---|
| 484 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
|
---|
| 485 | // could not find valid constructor, or found an intrinsic constructor
|
---|
| 486 | // fall back on C-style initializer
|
---|
| 487 | delete ctorInit->get_ctor();
|
---|
| 488 | ctorInit->set_ctor( NULL );
|
---|
[71a145de] | 489 | delete ctorInit->get_dtor();
|
---|
| 490 | ctorInit->set_dtor( NULL );
|
---|
[a4ca48c] | 491 | maybeAccept( ctorInit->get_init(), *visitor );
|
---|
[f1e012b] | 492 | }
|
---|
| 493 |
|
---|
[1d2b64f] | 494 | // needs to be callable from outside the resolver, so this is a standalone function
|
---|
| 495 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
|
---|
| 496 | assert( ctorInit );
|
---|
[a4ca48c] | 497 | PassVisitor<Resolver> resolver( indexer );
|
---|
[1d2b64f] | 498 | ctorInit->accept( resolver );
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
|
---|
| 502 | assert( stmtExpr );
|
---|
[a4ca48c] | 503 | PassVisitor<Resolver> resolver( indexer );
|
---|
[1d2b64f] | 504 | stmtExpr->accept( resolver );
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[a4ca48c] | 507 | void Resolver::previsit( ConstructorInit *ctorInit ) {
|
---|
| 508 | visit_children = false;
|
---|
[1ba88a0] | 509 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
|
---|
[a4ca48c] | 510 | maybeAccept( ctorInit->get_ctor(), *visitor );
|
---|
| 511 | maybeAccept( ctorInit->get_dtor(), *visitor );
|
---|
[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: //
|
---|