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