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