| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // Resolver.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Richard C. Bilson
|
|---|
| 10 | // Created On : Sun May 17 12:17:01 2015
|
|---|
| 11 | // Last Modified By : Rob Schluntz
|
|---|
| 12 | // Last Modified On : Fri May 13 11:36:40 2016
|
|---|
| 13 | // Update Count : 203
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Resolver.h"
|
|---|
| 17 | #include "AlternativeFinder.h"
|
|---|
| 18 | #include "Alternative.h"
|
|---|
| 19 | #include "RenameVars.h"
|
|---|
| 20 | #include "ResolveTypeof.h"
|
|---|
| 21 | #include "SynTree/Statement.h"
|
|---|
| 22 | #include "SynTree/Type.h"
|
|---|
| 23 | #include "SynTree/Expression.h"
|
|---|
| 24 | #include "SynTree/Initializer.h"
|
|---|
| 25 | #include "SymTab/Indexer.h"
|
|---|
| 26 | #include "Common/utility.h"
|
|---|
| 27 | #include "InitTweak/InitTweak.h"
|
|---|
| 28 |
|
|---|
| 29 | #include <iostream>
|
|---|
| 30 | using namespace std;
|
|---|
| 31 |
|
|---|
| 32 | namespace ResolvExpr {
|
|---|
| 33 | class Resolver : public SymTab::Indexer {
|
|---|
| 34 | public:
|
|---|
| 35 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
|
|---|
| 36 |
|
|---|
| 37 | virtual void visit( FunctionDecl *functionDecl );
|
|---|
| 38 | virtual void visit( ObjectDecl *functionDecl );
|
|---|
| 39 | virtual void visit( TypeDecl *typeDecl );
|
|---|
| 40 | virtual void visit( EnumDecl * enumDecl );
|
|---|
| 41 |
|
|---|
| 42 | virtual void visit( ArrayType * at );
|
|---|
| 43 |
|
|---|
| 44 | virtual void visit( ExprStmt *exprStmt );
|
|---|
| 45 | virtual void visit( AsmExpr *asmExpr );
|
|---|
| 46 | virtual void visit( AsmStmt *asmStmt );
|
|---|
| 47 | virtual void visit( IfStmt *ifStmt );
|
|---|
| 48 | virtual void visit( WhileStmt *whileStmt );
|
|---|
| 49 | virtual void visit( ForStmt *forStmt );
|
|---|
| 50 | virtual void visit( SwitchStmt *switchStmt );
|
|---|
| 51 | virtual void visit( ChooseStmt *switchStmt );
|
|---|
| 52 | virtual void visit( CaseStmt *caseStmt );
|
|---|
| 53 | virtual void visit( BranchStmt *branchStmt );
|
|---|
| 54 | virtual void visit( ReturnStmt *returnStmt );
|
|---|
| 55 | virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt );
|
|---|
| 56 |
|
|---|
| 57 | virtual void visit( SingleInit *singleInit );
|
|---|
| 58 | virtual void visit( ListInit *listInit );
|
|---|
| 59 | virtual void visit( ConstructorInit *ctorInit );
|
|---|
| 60 | private:
|
|---|
| 61 | typedef std::list< Initializer * >::iterator InitIterator;
|
|---|
| 62 |
|
|---|
| 63 | void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
|
|---|
| 64 | void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
|
|---|
| 65 | void fallbackInit( ConstructorInit * ctorInit );
|
|---|
| 66 | std::list< Type * > functionReturn;
|
|---|
| 67 | Type *initContext;
|
|---|
| 68 | Type *switchType;
|
|---|
| 69 | bool inEnumDecl = false;
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | void resolve( std::list< Declaration * > translationUnit ) {
|
|---|
| 73 | Resolver resolver;
|
|---|
| 74 | acceptAll( translationUnit, resolver );
|
|---|
| 75 | #if 0
|
|---|
| 76 | resolver.print( cerr );
|
|---|
| 77 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
|
|---|
| 78 | (*i)->print( std::cerr );
|
|---|
| 79 | (*i)->accept( resolver );
|
|---|
| 80 | } // for
|
|---|
| 81 | #endif
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
|
|---|
| 85 | TypeEnvironment env;
|
|---|
| 86 | return resolveInVoidContext( expr, indexer, env );
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | namespace {
|
|---|
| 91 | void finishExpr( Expression *expr, const TypeEnvironment &env ) {
|
|---|
| 92 | expr->set_env( new TypeSubstitution );
|
|---|
| 93 | env.makeSubstitution( *expr->get_env() );
|
|---|
| 94 | }
|
|---|
| 95 | } // namespace
|
|---|
| 96 |
|
|---|
| 97 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
|
|---|
| 98 | global_renamer.reset();
|
|---|
| 99 | TypeEnvironment env;
|
|---|
| 100 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
|
|---|
| 101 | finishExpr( newExpr, env );
|
|---|
| 102 | return newExpr;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | namespace {
|
|---|
| 106 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
|
|---|
| 107 | TypeEnvironment env;
|
|---|
| 108 | AlternativeFinder finder( indexer, env );
|
|---|
| 109 | finder.find( untyped );
|
|---|
| 110 | #if 0
|
|---|
| 111 | if ( finder.get_alternatives().size() != 1 ) {
|
|---|
| 112 | std::cout << "untyped expr is ";
|
|---|
| 113 | untyped->print( std::cout );
|
|---|
| 114 | std::cout << std::endl << "alternatives are:";
|
|---|
| 115 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
|
|---|
| 116 | i->print( std::cout );
|
|---|
| 117 | } // for
|
|---|
| 118 | } // if
|
|---|
| 119 | #endif
|
|---|
| 120 | assert( finder.get_alternatives().size() == 1 );
|
|---|
| 121 | Alternative &choice = finder.get_alternatives().front();
|
|---|
| 122 | Expression *newExpr = choice.expr->clone();
|
|---|
| 123 | finishExpr( newExpr, choice.env );
|
|---|
| 124 | return newExpr;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | bool isIntegralType( Type *type ) {
|
|---|
| 128 | if ( dynamic_cast< EnumInstType * >( type ) ) {
|
|---|
| 129 | return true;
|
|---|
| 130 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
|
|---|
| 131 | return bt->isInteger();
|
|---|
| 132 | } else {
|
|---|
| 133 | return false;
|
|---|
| 134 | } // if
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
|
|---|
| 138 | TypeEnvironment env;
|
|---|
| 139 | AlternativeFinder finder( indexer, env );
|
|---|
| 140 | finder.find( untyped );
|
|---|
| 141 | #if 0
|
|---|
| 142 | if ( finder.get_alternatives().size() != 1 ) {
|
|---|
| 143 | std::cout << "untyped expr is ";
|
|---|
| 144 | untyped->print( std::cout );
|
|---|
| 145 | std::cout << std::endl << "alternatives are:";
|
|---|
| 146 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
|
|---|
| 147 | i->print( std::cout );
|
|---|
| 148 | } // for
|
|---|
| 149 | } // if
|
|---|
| 150 | #endif
|
|---|
| 151 | Expression *newExpr = 0;
|
|---|
| 152 | const TypeEnvironment *newEnv = 0;
|
|---|
| 153 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
|
|---|
| 154 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
|
|---|
| 155 | if ( newExpr ) {
|
|---|
| 156 | throw SemanticError( "Too many interpretations for case control expression", untyped );
|
|---|
| 157 | } else {
|
|---|
| 158 | newExpr = i->expr->clone();
|
|---|
| 159 | newEnv = &i->env;
|
|---|
| 160 | } // if
|
|---|
| 161 | } // if
|
|---|
| 162 | } // for
|
|---|
| 163 | if ( ! newExpr ) {
|
|---|
| 164 | throw SemanticError( "No interpretations for case control expression", untyped );
|
|---|
| 165 | } // if
|
|---|
| 166 | finishExpr( newExpr, *newEnv );
|
|---|
| 167 | return newExpr;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | void Resolver::visit( ObjectDecl *objectDecl ) {
|
|---|
| 173 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
|
|---|
| 174 | objectDecl->set_type( new_type );
|
|---|
| 175 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
|
|---|
| 176 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes
|
|---|
| 177 | // initContext because of a function type can contain object declarations in the return and parameter types. So
|
|---|
| 178 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
|
|---|
| 179 | // the RHS.
|
|---|
| 180 | Type *temp = initContext;
|
|---|
| 181 | initContext = new_type;
|
|---|
| 182 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
|
|---|
| 183 | // enumerator initializers should not use the enum type to initialize, since
|
|---|
| 184 | // the enum type is still incomplete at this point. Use signed int instead.
|
|---|
| 185 | initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
|
|---|
| 186 | }
|
|---|
| 187 | SymTab::Indexer::visit( objectDecl );
|
|---|
| 188 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
|
|---|
| 189 | // delete newly created signed int type
|
|---|
| 190 | delete initContext;
|
|---|
| 191 | }
|
|---|
| 192 | initContext = temp;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | void Resolver::visit( ArrayType * at ) {
|
|---|
| 196 | if ( at->get_dimension() ) {
|
|---|
| 197 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
|---|
| 198 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
|
|---|
| 199 | Expression *newExpr = findSingleExpression( castExpr, *this );
|
|---|
| 200 | delete at->get_dimension();
|
|---|
| 201 | at->set_dimension( newExpr );
|
|---|
| 202 | }
|
|---|
| 203 | Visitor::visit( at );
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | void Resolver::visit( TypeDecl *typeDecl ) {
|
|---|
| 207 | if ( typeDecl->get_base() ) {
|
|---|
| 208 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
|
|---|
| 209 | typeDecl->set_base( new_type );
|
|---|
| 210 | } // if
|
|---|
| 211 | SymTab::Indexer::visit( typeDecl );
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | void Resolver::visit( FunctionDecl *functionDecl ) {
|
|---|
| 215 | #if 0
|
|---|
| 216 | std::cout << "resolver visiting functiondecl ";
|
|---|
| 217 | functionDecl->print( std::cout );
|
|---|
| 218 | std::cout << std::endl;
|
|---|
| 219 | #endif
|
|---|
| 220 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
|
|---|
| 221 | functionDecl->set_type( new_type );
|
|---|
| 222 | std::list< Type * > oldFunctionReturn = functionReturn;
|
|---|
| 223 | functionReturn.clear();
|
|---|
| 224 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
|
|---|
| 225 | functionReturn.push_back( (*i)->get_type() );
|
|---|
| 226 | } // for
|
|---|
| 227 | SymTab::Indexer::visit( functionDecl );
|
|---|
| 228 | functionReturn = oldFunctionReturn;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | void Resolver::visit( EnumDecl * enumDecl ) {
|
|---|
| 232 | // in case we decide to allow nested enums
|
|---|
| 233 | bool oldInEnumDecl = inEnumDecl;
|
|---|
| 234 | inEnumDecl = true;
|
|---|
| 235 | SymTab::Indexer::visit( enumDecl );
|
|---|
| 236 | inEnumDecl = oldInEnumDecl;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | void Resolver::visit( ExprStmt *exprStmt ) {
|
|---|
| 240 | if ( exprStmt->get_expr() ) {
|
|---|
| 241 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
|
|---|
| 242 | delete exprStmt->get_expr();
|
|---|
| 243 | exprStmt->set_expr( newExpr );
|
|---|
| 244 | } // if
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | void Resolver::visit( AsmExpr *asmExpr ) {
|
|---|
| 248 | Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
|
|---|
| 249 | delete asmExpr->get_operand();
|
|---|
| 250 | asmExpr->set_operand( newExpr );
|
|---|
| 251 | if ( asmExpr->get_inout() ) {
|
|---|
| 252 | newExpr = findVoidExpression( asmExpr->get_inout(), *this );
|
|---|
| 253 | delete asmExpr->get_inout();
|
|---|
| 254 | asmExpr->set_inout( newExpr );
|
|---|
| 255 | } // if
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | void Resolver::visit( AsmStmt *asmStmt ) {
|
|---|
| 259 | acceptAll( asmStmt->get_input(), *this);
|
|---|
| 260 | acceptAll( asmStmt->get_output(), *this);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | void Resolver::visit( IfStmt *ifStmt ) {
|
|---|
| 264 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
|
|---|
| 265 | delete ifStmt->get_condition();
|
|---|
| 266 | ifStmt->set_condition( newExpr );
|
|---|
| 267 | Visitor::visit( ifStmt );
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | void Resolver::visit( WhileStmt *whileStmt ) {
|
|---|
| 271 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
|
|---|
| 272 | delete whileStmt->get_condition();
|
|---|
| 273 | whileStmt->set_condition( newExpr );
|
|---|
| 274 | Visitor::visit( whileStmt );
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | void Resolver::visit( ForStmt *forStmt ) {
|
|---|
| 278 | SymTab::Indexer::visit( forStmt );
|
|---|
| 279 |
|
|---|
| 280 | if ( forStmt->get_condition() ) {
|
|---|
| 281 | Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
|
|---|
| 282 | delete forStmt->get_condition();
|
|---|
| 283 | forStmt->set_condition( newExpr );
|
|---|
| 284 | } // if
|
|---|
| 285 |
|
|---|
| 286 | if ( forStmt->get_increment() ) {
|
|---|
| 287 | Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
|
|---|
| 288 | delete forStmt->get_increment();
|
|---|
| 289 | forStmt->set_increment( newExpr );
|
|---|
| 290 | } // if
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | template< typename SwitchClass >
|
|---|
| 294 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
|
|---|
| 295 | Expression *newExpr;
|
|---|
| 296 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
|
|---|
| 297 | delete switchStmt->get_condition();
|
|---|
| 298 | switchStmt->set_condition( newExpr );
|
|---|
| 299 |
|
|---|
| 300 | visitor.Visitor::visit( switchStmt );
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | void Resolver::visit( SwitchStmt *switchStmt ) {
|
|---|
| 304 | handleSwitchStmt( switchStmt, *this );
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | void Resolver::visit( ChooseStmt *switchStmt ) {
|
|---|
| 308 | handleSwitchStmt( switchStmt, *this );
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | void Resolver::visit( CaseStmt *caseStmt ) {
|
|---|
| 312 | Visitor::visit( caseStmt );
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | void Resolver::visit( BranchStmt *branchStmt ) {
|
|---|
| 316 | // must resolve the argument for a computed goto
|
|---|
| 317 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
|
|---|
| 318 | if ( Expression * arg = branchStmt->get_computedTarget() ) {
|
|---|
| 319 | VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
|
|---|
| 320 | PointerType pt( Type::Qualifiers(), v.clone() );
|
|---|
| 321 | CastExpr * castExpr = new CastExpr( arg, pt.clone() );
|
|---|
| 322 | Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
|
|---|
| 323 | branchStmt->set_target( newExpr );
|
|---|
| 324 | } // if
|
|---|
| 325 | } // if
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | void Resolver::visit( ReturnStmt *returnStmt ) {
|
|---|
| 329 | if ( returnStmt->get_expr() ) {
|
|---|
| 330 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
|
|---|
| 331 | cloneAll( functionReturn, castExpr->get_results() );
|
|---|
| 332 | Expression *newExpr = findSingleExpression( castExpr, *this );
|
|---|
| 333 | delete castExpr;
|
|---|
| 334 | returnStmt->set_expr( newExpr );
|
|---|
| 335 | } // if
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | template< typename T >
|
|---|
| 339 | bool isCharType( T t ) {
|
|---|
| 340 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
|
|---|
| 341 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
|
|---|
| 342 | bt->get_kind() == BasicType::UnsignedChar;
|
|---|
| 343 | }
|
|---|
| 344 | return false;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | void Resolver::visit( SingleInit *singleInit ) {
|
|---|
| 348 | if ( singleInit->get_value() ) {
|
|---|
| 349 | #if 0
|
|---|
| 350 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
|
|---|
| 351 | string n = ne->get_name();
|
|---|
| 352 | if (n == "0") {
|
|---|
| 353 | initContext = new BasicType(Type::Qualifiers(),
|
|---|
| 354 | BasicType::SignedInt);
|
|---|
| 355 | } else {
|
|---|
| 356 | DeclarationWithType * decl = lookupId( n );
|
|---|
| 357 | initContext = decl->get_type();
|
|---|
| 358 | }
|
|---|
| 359 | } else if (ConstantExpr * e =
|
|---|
| 360 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
|
|---|
| 361 | Constant *c = e->get_constant();
|
|---|
| 362 | initContext = c->get_type();
|
|---|
| 363 | } else {
|
|---|
| 364 | assert(0);
|
|---|
| 365 | }
|
|---|
| 366 | #endif
|
|---|
| 367 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
|
|---|
| 368 | Expression *newExpr = findSingleExpression( castExpr, *this );
|
|---|
| 369 | delete castExpr;
|
|---|
| 370 | singleInit->set_value( newExpr );
|
|---|
| 371 |
|
|---|
| 372 | // check if initializing type is char[]
|
|---|
| 373 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
|
|---|
| 374 | if ( isCharType( at->get_base() ) ) {
|
|---|
| 375 | // check if the resolved type is char *
|
|---|
| 376 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
|
|---|
| 377 | if ( isCharType( pt->get_base() ) ) {
|
|---|
| 378 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
|
|---|
| 379 | CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
|
|---|
| 380 | singleInit->set_value( ce->get_arg() );
|
|---|
| 381 | ce->set_arg( NULL );
|
|---|
| 382 | delete ce;
|
|---|
| 383 | }
|
|---|
| 384 | }
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| 387 | } // if
|
|---|
| 388 | // singleInit->get_value()->accept( *this );
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
|
|---|
| 392 | DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
|
|---|
| 393 | assert( dt );
|
|---|
| 394 | initContext = dt->get_type();
|
|---|
| 395 | try {
|
|---|
| 396 | if ( init == initEnd ) return; // stop when there are no more initializers
|
|---|
| 397 | (*init)->accept( *this );
|
|---|
| 398 | ++init; // made it past an initializer
|
|---|
| 399 | } catch( SemanticError & ) {
|
|---|
| 400 | // need to delve deeper, if you can
|
|---|
| 401 | if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
|
|---|
| 402 | resolveAggrInit( sit->get_baseStruct(), init, initEnd );
|
|---|
| 403 | } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
|
|---|
| 404 | resolveAggrInit( uit->get_baseUnion(), init, initEnd );
|
|---|
| 405 | } else {
|
|---|
| 406 | // member is not an aggregate type, so can't go any deeper
|
|---|
| 407 |
|
|---|
| 408 | // might need to rethink what is being thrown
|
|---|
| 409 | throw;
|
|---|
| 410 | } // if
|
|---|
| 411 | }
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
|
|---|
| 415 | if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
|
|---|
| 416 | // want to resolve each initializer to the members of the struct,
|
|---|
| 417 | // but if there are more initializers than members we should stop
|
|---|
| 418 | list< Declaration * >::iterator it = st->get_members().begin();
|
|---|
| 419 | for ( ; it != st->get_members().end(); ++it) {
|
|---|
| 420 | resolveSingleAggrInit( *it, init, initEnd );
|
|---|
| 421 | }
|
|---|
| 422 | } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
|
|---|
| 423 | // only resolve to the first member of a union
|
|---|
| 424 | resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
|
|---|
| 425 | } // if
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | void Resolver::visit( ListInit * listInit ) {
|
|---|
| 429 | InitIterator iter = listInit->begin_initializers();
|
|---|
| 430 | InitIterator end = listInit->end_initializers();
|
|---|
| 431 |
|
|---|
| 432 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
|
|---|
| 433 | // resolve each member to the base type of the array
|
|---|
| 434 | for ( ; iter != end; ++iter ) {
|
|---|
| 435 | initContext = at->get_base();
|
|---|
| 436 | (*iter)->accept( *this );
|
|---|
| 437 | } // for
|
|---|
| 438 | } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
|
|---|
| 439 | resolveAggrInit( st->get_baseStruct(), iter, end );
|
|---|
| 440 | } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
|
|---|
| 441 | resolveAggrInit( st->get_baseUnion(), iter, end );
|
|---|
| 442 | } else {
|
|---|
| 443 | // basic types are handled here
|
|---|
| 444 | Visitor::visit( listInit );
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | #if 0
|
|---|
| 448 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
|
|---|
| 449 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
|
|---|
| 450 | for ( ; iter != listInit->end_initializers(); ++iter ) {
|
|---|
| 451 | initContext = at->get_base();
|
|---|
| 452 | (*iter)->accept( *this );
|
|---|
| 453 | } // for
|
|---|
| 454 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
|
|---|
| 455 | StructDecl *baseStruct = st->get_baseStruct();
|
|---|
| 456 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
|
|---|
| 457 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
|
|---|
| 458 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
|
|---|
| 459 | if ( (*iter2)->get_designators().empty() ) {
|
|---|
| 460 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
|
|---|
| 461 | initContext = dt->get_type();
|
|---|
| 462 | (*iter2)->accept( *this );
|
|---|
| 463 | ++iter1;
|
|---|
| 464 | } else {
|
|---|
| 465 | StructDecl *st = baseStruct;
|
|---|
| 466 | iter1 = st->get_members().begin();
|
|---|
| 467 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
|
|---|
| 468 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
|
|---|
| 469 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
|
|---|
| 470 | assert( key );
|
|---|
| 471 | for ( ; iter1 != st->get_members().end(); ++iter1 ) {
|
|---|
| 472 | if ( key->get_name() == (*iter1)->get_name() ) {
|
|---|
| 473 | (*iter1)->print( cout );
|
|---|
| 474 | cout << key->get_name() << endl;
|
|---|
| 475 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
|
|---|
| 476 | assert( fred );
|
|---|
| 477 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
|
|---|
| 478 | assert( mary );
|
|---|
| 479 | st = mary->get_baseStruct();
|
|---|
| 480 | iter1 = st->get_members().begin();
|
|---|
| 481 | break;
|
|---|
| 482 | } // if
|
|---|
| 483 | } // for
|
|---|
| 484 | } // for
|
|---|
| 485 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
|
|---|
| 486 | assert( fred );
|
|---|
| 487 | initContext = fred->get_type();
|
|---|
| 488 | (*listInit->begin_initializers())->accept( *this );
|
|---|
| 489 | } // if
|
|---|
| 490 | } // for
|
|---|
| 491 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
|
|---|
| 492 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
|
|---|
| 493 | initContext = dt->get_type();
|
|---|
| 494 | (*listInit->begin_initializers())->accept( *this );
|
|---|
| 495 | } // if
|
|---|
| 496 | #endif
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | // ConstructorInit - fall back on C-style initializer
|
|---|
| 500 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
|
|---|
| 501 | // could not find valid constructor, or found an intrinsic constructor
|
|---|
| 502 | // fall back on C-style initializer
|
|---|
| 503 | delete ctorInit->get_ctor();
|
|---|
| 504 | ctorInit->set_ctor( NULL );
|
|---|
| 505 | maybeAccept( ctorInit->get_init(), *this );
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | void Resolver::visit( ConstructorInit *ctorInit ) {
|
|---|
| 509 | try {
|
|---|
| 510 | maybeAccept( ctorInit->get_ctor(), *this );
|
|---|
| 511 | maybeAccept( ctorInit->get_dtor(), *this );
|
|---|
| 512 | } catch ( SemanticError ) {
|
|---|
| 513 | // no alternatives for the constructor initializer - fallback on C-style initializer
|
|---|
| 514 | // xxx - not sure if this makes a ton of sense - should maybe never be able to have this situation?
|
|---|
| 515 | fallbackInit( ctorInit );
|
|---|
| 516 | return;
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | // found a constructor - can get rid of C-style initializer
|
|---|
| 520 | delete ctorInit->get_init();
|
|---|
| 521 | ctorInit->set_init( NULL );
|
|---|
| 522 |
|
|---|
| 523 | // intrinsic single parameter constructors and destructors do nothing. Since this was
|
|---|
| 524 | // implicitly generated, there's no way for it to have side effects, so get rid of it
|
|---|
| 525 | // to clean up generated code.
|
|---|
| 526 | if ( InitTweak::isInstrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
|
|---|
| 527 | delete ctorInit->get_ctor();
|
|---|
| 528 | ctorInit->set_ctor( NULL );
|
|---|
| 529 | }
|
|---|
| 530 | if ( InitTweak::isInstrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
|
|---|
| 531 | delete ctorInit->get_dtor();
|
|---|
| 532 | ctorInit->set_dtor( NULL );
|
|---|
| 533 | }
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | void Resolver::visit( ImplicitCtorDtorStmt * impCtorDtorStmt ) {
|
|---|
| 537 | // before resolving ctor/dtor, need to remove type qualifiers from the first argument (the object being constructed).
|
|---|
| 538 | // Do this through a cast expression to greatly simplify the code.
|
|---|
| 539 | Expression * callExpr = InitTweak::getCtorDtorCall( impCtorDtorStmt );
|
|---|
| 540 | assert( callExpr );
|
|---|
| 541 | Expression *& constructee = InitTweak::getCallArg( callExpr, 0 );
|
|---|
| 542 | Type * type = 0;
|
|---|
| 543 |
|
|---|
| 544 | // need to find the type of the first argument, which is unfortunately not uniform since array construction
|
|---|
| 545 | // includes an untyped '+' expression.
|
|---|
| 546 | if ( UntypedExpr * plusExpr = dynamic_cast< UntypedExpr * >( constructee ) ) {
|
|---|
| 547 | // constructee is <array>+<index>
|
|---|
| 548 | // get Variable <array>, then get the base type of the VariableExpr - this is the type that needs to be fixed
|
|---|
| 549 | Expression * arr = InitTweak::getCallArg( plusExpr, 0 );
|
|---|
| 550 | assert( dynamic_cast< VariableExpr * >( arr ) );
|
|---|
| 551 | assert( arr && arr->get_results().size() == 1 );
|
|---|
| 552 | type = arr->get_results().front()->clone();
|
|---|
| 553 | } else {
|
|---|
| 554 | // otherwise, constructing a plain object, which means the object's address is being taken.
|
|---|
| 555 | // Need to get the type of the VariableExpr object, because the AddressExpr is rebuilt and uses the
|
|---|
| 556 | // type of the VariableExpr to do so.
|
|---|
| 557 | assert( constructee->get_results().size() == 1 );
|
|---|
| 558 | AddressExpr * addrExpr = dynamic_cast< AddressExpr * > ( constructee );
|
|---|
| 559 | assert( addrExpr && addrExpr->get_results().size() == 1);
|
|---|
| 560 | type = addrExpr->get_results().front()->clone();
|
|---|
| 561 | }
|
|---|
| 562 | // cast to T* with qualifiers removed.
|
|---|
| 563 | // unfortunately, lvalue is considered a qualifier. For AddressExpr to resolve, its argument
|
|---|
| 564 | // must have an lvalue qualified type, so remove all qualifiers except lvalue. If we ever
|
|---|
| 565 | // remove lvalue as a qualifier, this can change to
|
|---|
| 566 | // type->get_qualifiers() = Type::Qualifiers();
|
|---|
| 567 | Type * base = InitTweak::getPointerBase( type );
|
|---|
| 568 | assert( base );
|
|---|
| 569 | base->get_qualifiers() -= Type::Qualifiers(true, true, true, false, true, true);
|
|---|
| 570 | // if pointer has lvalue qualifier, cast won't appear in output
|
|---|
| 571 | type->set_isLvalue( false );
|
|---|
| 572 | constructee = new CastExpr( constructee, type );
|
|---|
| 573 |
|
|---|
| 574 | // finally, resolve the ctor/dtor
|
|---|
| 575 | impCtorDtorStmt->get_callStmt()->accept( *this );
|
|---|
| 576 | }
|
|---|
| 577 | } // namespace ResolvExpr
|
|---|
| 578 |
|
|---|
| 579 | // Local Variables: //
|
|---|
| 580 | // tab-width: 4 //
|
|---|
| 581 | // mode: c++ //
|
|---|
| 582 | // compile-command: "make install" //
|
|---|
| 583 | // End: //
|
|---|