[71f4e4f] | 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 | // FixInit.h -- |
---|
| 8 | // |
---|
| 9 | // Author : Rob Schluntz |
---|
| 10 | // Created On : Wed Jan 13 16:29:30 2016 |
---|
[adcc065] | 11 | // Last Modified By : Peter A. Buhr |
---|
[4e06c1e] | 12 | // Last Modified On : Tue Jul 12 17:41:15 2016 |
---|
| 13 | // Update Count : 34 |
---|
[71f4e4f] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <stack> |
---|
| 17 | #include <list> |
---|
[c2931ea] | 18 | #include <iterator> |
---|
| 19 | #include <algorithm> |
---|
[7b3f66b] | 20 | #include "InitTweak.h" |
---|
[6cf27a07] | 21 | #include "FixInit.h" |
---|
| 22 | #include "FixGlobalInit.h" |
---|
[db4ecc5] | 23 | #include "ResolvExpr/Resolver.h" |
---|
[845cedc] | 24 | #include "ResolvExpr/typeops.h" |
---|
[71f4e4f] | 25 | #include "SynTree/Declaration.h" |
---|
| 26 | #include "SynTree/Type.h" |
---|
| 27 | #include "SynTree/Expression.h" |
---|
[f9cebb5] | 28 | #include "SynTree/Attribute.h" |
---|
[71f4e4f] | 29 | #include "SynTree/Statement.h" |
---|
| 30 | #include "SynTree/Initializer.h" |
---|
| 31 | #include "SynTree/Mutator.h" |
---|
[db4ecc5] | 32 | #include "SymTab/Indexer.h" |
---|
[c8dfcd3] | 33 | #include "SymTab/Autogen.h" |
---|
[71f4e4f] | 34 | #include "GenPoly/PolyMutator.h" |
---|
[b6fe7e6] | 35 | #include "GenPoly/DeclMutator.h" |
---|
[c2931ea] | 36 | #include "SynTree/AddStmtVisitor.h" |
---|
[f0121d7] | 37 | #include "CodeGen/GenType.h" // for warning/error messages |
---|
[71f4e4f] | 38 | |
---|
[f0121d7] | 39 | bool ctordtorp = false; // print all debug |
---|
| 40 | bool ctorp = false; // print ctor debug |
---|
| 41 | bool cpctorp = false; // print copy ctor debug |
---|
| 42 | bool dtorp = false; // print dtor debug |
---|
[845cedc] | 43 | #define PRINT( text ) if ( ctordtorp ) { text } |
---|
[c2931ea] | 44 | #define CP_CTOR_PRINT( text ) if ( ctordtorp || cpctorp ) { text } |
---|
| 45 | #define DTOR_PRINT( text ) if ( ctordtorp || dtorp ) { text } |
---|
[845cedc] | 46 | |
---|
[71f4e4f] | 47 | namespace InitTweak { |
---|
| 48 | namespace { |
---|
[c2931ea] | 49 | class InsertImplicitCalls : public GenPoly::PolyMutator { |
---|
| 50 | public: |
---|
[adcc065] | 51 | /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which |
---|
| 52 | /// function calls need their parameters to be copy constructed |
---|
[c2931ea] | 53 | static void insert( std::list< Declaration * > & translationUnit ); |
---|
| 54 | |
---|
| 55 | virtual Expression * mutate( ApplicationExpr * appExpr ); |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | class ResolveCopyCtors : public SymTab::Indexer { |
---|
| 59 | public: |
---|
[adcc065] | 60 | /// generate temporary ObjectDecls for each argument and return value of each ImplicitCopyCtorExpr, |
---|
| 61 | /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both |
---|
| 62 | /// arguments and return value temporaries |
---|
[c2931ea] | 63 | static void resolveImplicitCalls( std::list< Declaration * > & translationUnit ); |
---|
| 64 | |
---|
| 65 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
| 66 | |
---|
| 67 | /// create and resolve ctor/dtor expression: fname(var, [cpArg]) |
---|
[65660bd] | 68 | Expression * makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg = NULL ); |
---|
| 69 | Expression * makeCtorDtor( const std::string & fname, Expression * thisArg, Expression * cpArg = NULL ); |
---|
[c2931ea] | 70 | /// true if type does not need to be copy constructed to ensure correctness |
---|
[1132b62] | 71 | bool skipCopyConstruct( Type * type ); |
---|
| 72 | void copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
| 73 | void destructRet( Expression * ret, ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
[c2931ea] | 74 | private: |
---|
| 75 | TypeSubstitution * env; |
---|
| 76 | }; |
---|
| 77 | |
---|
| 78 | /// collects constructed object decls - used as a base class |
---|
| 79 | class ObjDeclCollector : public AddStmtVisitor { |
---|
| 80 | public: |
---|
| 81 | typedef AddStmtVisitor Parent; |
---|
| 82 | using Parent::visit; |
---|
| 83 | typedef std::set< ObjectDecl * > ObjectSet; |
---|
| 84 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 85 | virtual void visit( DeclStmt *stmt ); |
---|
| 86 | protected: |
---|
| 87 | ObjectSet curVars; |
---|
| 88 | }; |
---|
| 89 | |
---|
[6cf27a07] | 90 | // debug |
---|
[c2931ea] | 91 | struct printSet { |
---|
| 92 | typedef ObjDeclCollector::ObjectSet ObjectSet; |
---|
| 93 | printSet( const ObjectSet & objs ) : objs( objs ) {} |
---|
| 94 | const ObjectSet & objs; |
---|
| 95 | }; |
---|
| 96 | std::ostream & operator<<( std::ostream & out, const printSet & set) { |
---|
| 97 | out << "{ "; |
---|
| 98 | for ( ObjectDecl * obj : set.objs ) { |
---|
| 99 | out << obj->get_name() << ", " ; |
---|
[adcc065] | 100 | } // for |
---|
[c2931ea] | 101 | out << " }"; |
---|
| 102 | return out; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | class LabelFinder : public ObjDeclCollector { |
---|
| 106 | public: |
---|
| 107 | typedef ObjDeclCollector Parent; |
---|
| 108 | typedef std::map< Label, ObjectSet > LabelMap; |
---|
| 109 | // map of Label -> live variables at that label |
---|
| 110 | LabelMap vars; |
---|
| 111 | |
---|
| 112 | void handleStmt( Statement * stmt ); |
---|
| 113 | |
---|
| 114 | // xxx - This needs to be done better. |
---|
[adcc065] | 115 | // allow some generalization among different kinds of nodes with with similar parentage (e.g. all |
---|
| 116 | // expressions, all statements, etc.) important to have this to provide a single entry point so that as new |
---|
| 117 | // subclasses are added, there is only one place that the code has to be updated, rather than ensure that |
---|
| 118 | // every specialized class knows about every new kind of statement that might be added. |
---|
[c2931ea] | 119 | virtual void visit( CompoundStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 120 | virtual void visit( ExprStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 121 | virtual void visit( AsmStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 122 | virtual void visit( IfStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 123 | virtual void visit( WhileStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 124 | virtual void visit( ForStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 125 | virtual void visit( SwitchStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 126 | virtual void visit( CaseStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 127 | virtual void visit( BranchStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 128 | virtual void visit( ReturnStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 129 | virtual void visit( TryStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 130 | virtual void visit( CatchStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 131 | virtual void visit( FinallyStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 132 | virtual void visit( NullStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 133 | virtual void visit( DeclStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 134 | virtual void visit( ImplicitCtorDtorStmt *stmt ) { handleStmt( stmt ); return Parent::visit( stmt ); } |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | class InsertDtors : public ObjDeclCollector { |
---|
| 138 | public: |
---|
[adcc065] | 139 | /// insert destructor calls at the appropriate places. must happen before CtorInit nodes are removed |
---|
| 140 | /// (currently by FixInit) |
---|
[c2931ea] | 141 | static void insert( std::list< Declaration * > & translationUnit ); |
---|
| 142 | |
---|
| 143 | typedef ObjDeclCollector Parent; |
---|
| 144 | typedef std::list< ObjectDecl * > OrderedDecls; |
---|
| 145 | typedef std::list< OrderedDecls > OrderedDeclsStack; |
---|
| 146 | |
---|
| 147 | InsertDtors( LabelFinder & finder ) : labelVars( finder.vars ) {} |
---|
| 148 | |
---|
| 149 | virtual void visit( ObjectDecl * objDecl ); |
---|
| 150 | |
---|
| 151 | virtual void visit( CompoundStmt * compoundStmt ); |
---|
| 152 | virtual void visit( ReturnStmt * returnStmt ); |
---|
| 153 | virtual void visit( BranchStmt * stmt ); |
---|
| 154 | private: |
---|
| 155 | void handleGoto( BranchStmt * stmt ); |
---|
| 156 | |
---|
| 157 | LabelFinder::LabelMap & labelVars; |
---|
| 158 | OrderedDeclsStack reverseDeclOrder; |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | class FixInit : public GenPoly::PolyMutator { |
---|
| 162 | public: |
---|
| 163 | /// expand each object declaration to use its constructor after it is declared. |
---|
| 164 | static void fixInitializers( std::list< Declaration * > &translationUnit ); |
---|
| 165 | |
---|
| 166 | virtual DeclarationWithType * mutate( ObjectDecl *objDecl ); |
---|
[72e9222] | 167 | |
---|
| 168 | std::list< Declaration * > staticDtorDecls; |
---|
[c2931ea] | 169 | }; |
---|
| 170 | |
---|
| 171 | class FixCopyCtors : public GenPoly::PolyMutator { |
---|
| 172 | public: |
---|
[adcc065] | 173 | /// expand ImplicitCopyCtorExpr nodes into the temporary declarations, copy constructors, call expression, |
---|
| 174 | /// and destructors |
---|
[c2931ea] | 175 | static void fixCopyCtors( std::list< Declaration * > &translationUnit ); |
---|
| 176 | |
---|
| 177 | virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
| 178 | }; |
---|
[79970ed] | 179 | |
---|
[c8dfcd3] | 180 | class GenStructMemberCalls : public SymTab::Indexer { |
---|
[79970ed] | 181 | public: |
---|
[c8dfcd3] | 182 | typedef Indexer Parent; |
---|
| 183 | /// generate default/copy ctor and dtor calls for user-defined struct ctor/dtors |
---|
| 184 | /// for any member that is missing a corresponding ctor/dtor call. |
---|
| 185 | /// error if a member is used before constructed |
---|
| 186 | static void generate( std::list< Declaration * > & translationUnit ); |
---|
[79970ed] | 187 | |
---|
| 188 | virtual void visit( FunctionDecl * funcDecl ); |
---|
| 189 | |
---|
| 190 | virtual void visit( MemberExpr * memberExpr ); |
---|
| 191 | virtual void visit( ApplicationExpr * appExpr ); |
---|
| 192 | |
---|
[3906301] | 193 | SemanticError errors; |
---|
[79970ed] | 194 | private: |
---|
| 195 | void handleFirstParam( Expression * firstParam ); |
---|
[3906301] | 196 | template< typename... Params > |
---|
| 197 | void emit( const Params &... params ); |
---|
[79970ed] | 198 | |
---|
| 199 | FunctionDecl * function = 0; |
---|
[c8dfcd3] | 200 | std::set< DeclarationWithType * > unhandled, usedUninit; |
---|
[79970ed] | 201 | ObjectDecl * thisParam = 0; |
---|
[c8dfcd3] | 202 | bool isCtor = false; // true if current function is a constructor |
---|
[44f6341] | 203 | StructDecl * structDecl = 0; |
---|
[c8dfcd3] | 204 | }; |
---|
| 205 | |
---|
| 206 | // very simple resolver-like mutator class - used to |
---|
| 207 | // resolve UntypedExprs that are found within newly |
---|
| 208 | // generated constructor/destructor calls |
---|
| 209 | class MutatingResolver : public Mutator { |
---|
| 210 | public: |
---|
| 211 | MutatingResolver( SymTab::Indexer & indexer ) : indexer( indexer ) {} |
---|
| 212 | |
---|
| 213 | virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ); |
---|
| 214 | |
---|
| 215 | virtual Expression* mutate( UntypedExpr *untypedExpr ); |
---|
| 216 | private: |
---|
| 217 | SymTab::Indexer & indexer; |
---|
[79970ed] | 218 | }; |
---|
[b6fe7e6] | 219 | |
---|
| 220 | class FixCtorExprs : public GenPoly::DeclMutator { |
---|
| 221 | public: |
---|
| 222 | /// expands ConstructorExpr nodes into comma expressions, using a temporary for the first argument |
---|
| 223 | static void fix( std::list< Declaration * > & translationUnit ); |
---|
| 224 | |
---|
| 225 | virtual Expression * mutate( ConstructorExpr * ctorExpr ); |
---|
| 226 | }; |
---|
[c2931ea] | 227 | } // namespace |
---|
[db4ecc5] | 228 | |
---|
[6cf27a07] | 229 | void fix( std::list< Declaration * > & translationUnit, const std::string & filename, bool inLibrary ) { |
---|
| 230 | // fixes ConstructorInit for global variables. should happen before fixInitializers. |
---|
| 231 | InitTweak::fixGlobalInit( translationUnit, filename, inLibrary ); |
---|
| 232 | |
---|
[b6fe7e6] | 233 | |
---|
[db4ecc5] | 234 | InsertImplicitCalls::insert( translationUnit ); |
---|
| 235 | ResolveCopyCtors::resolveImplicitCalls( translationUnit ); |
---|
[c2931ea] | 236 | InsertDtors::insert( translationUnit ); |
---|
[71f4e4f] | 237 | FixInit::fixInitializers( translationUnit ); |
---|
[c2931ea] | 238 | |
---|
[db4ecc5] | 239 | // FixCopyCtors must happen after FixInit, so that destructors are placed correctly |
---|
| 240 | FixCopyCtors::fixCopyCtors( translationUnit ); |
---|
[79970ed] | 241 | |
---|
[c8dfcd3] | 242 | GenStructMemberCalls::generate( translationUnit ); |
---|
[b6fe7e6] | 243 | // xxx - ctor expansion currently has to be after FixCopyCtors, because there is currently a |
---|
| 244 | // hack in the way untyped assignments are generated, where the first argument cannot have |
---|
| 245 | // its address taken because of the way codegeneration handles UntypedExpr vs. ApplicationExpr. |
---|
| 246 | // Thus such assignment exprs must never pushed through expression resolution (and thus should |
---|
| 247 | // not go through the FixCopyCtors pass), otherwise they will fail -- guaranteed. |
---|
| 248 | // Also needs to happen after GenStructMemberCalls, since otherwise member constructors exprs |
---|
| 249 | // don't look right, and a member can be constructed more than once. |
---|
| 250 | FixCtorExprs::fix( translationUnit ); |
---|
[db4ecc5] | 251 | } |
---|
| 252 | |
---|
[c2931ea] | 253 | namespace { |
---|
| 254 | void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit ) { |
---|
| 255 | InsertImplicitCalls inserter; |
---|
| 256 | mutateAll( translationUnit, inserter ); |
---|
| 257 | } |
---|
[db4ecc5] | 258 | |
---|
[c2931ea] | 259 | void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit ) { |
---|
| 260 | ResolveCopyCtors resolver; |
---|
| 261 | acceptAll( translationUnit, resolver ); |
---|
| 262 | } |
---|
[71f4e4f] | 263 | |
---|
[c2931ea] | 264 | void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) { |
---|
| 265 | FixInit fixer; |
---|
[72e9222] | 266 | |
---|
| 267 | // can't use mutateAll, because need to insert declarations at top-level |
---|
| 268 | // can't use DeclMutator, because sometimes need to insert IfStmt, etc. |
---|
| 269 | SemanticError errors; |
---|
| 270 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
| 271 | try { |
---|
| 272 | *i = maybeMutate( *i, fixer ); |
---|
[f9cebb5] | 273 | translationUnit.splice( i, fixer.staticDtorDecls ); |
---|
[72e9222] | 274 | } catch( SemanticError &e ) { |
---|
| 275 | errors.append( e ); |
---|
| 276 | } // try |
---|
| 277 | } // for |
---|
| 278 | if ( ! errors.isEmpty() ) { |
---|
| 279 | throw errors; |
---|
| 280 | } // if |
---|
[c2931ea] | 281 | } |
---|
[71f4e4f] | 282 | |
---|
[c2931ea] | 283 | void InsertDtors::insert( std::list< Declaration * > & translationUnit ) { |
---|
| 284 | LabelFinder finder; |
---|
| 285 | InsertDtors inserter( finder ); |
---|
| 286 | acceptAll( translationUnit, finder ); |
---|
| 287 | acceptAll( translationUnit, inserter ); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | void FixCopyCtors::fixCopyCtors( std::list< Declaration * > & translationUnit ) { |
---|
| 291 | FixCopyCtors fixer; |
---|
| 292 | mutateAll( translationUnit, fixer ); |
---|
| 293 | } |
---|
[db4ecc5] | 294 | |
---|
[c8dfcd3] | 295 | void GenStructMemberCalls::generate( std::list< Declaration * > & translationUnit ) { |
---|
| 296 | GenStructMemberCalls warner; |
---|
| 297 | acceptAll( translationUnit, warner ); |
---|
[3906301] | 298 | |
---|
[c8dfcd3] | 299 | // visitor doesn't throw so that it can collect all errors |
---|
| 300 | if ( ! warner.errors.isEmpty() ) { |
---|
| 301 | throw warner.errors; |
---|
[79970ed] | 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
[b6fe7e6] | 305 | void FixCtorExprs::fix( std::list< Declaration * > & translationUnit ) { |
---|
| 306 | FixCtorExprs fixer; |
---|
| 307 | fixer.mutateDeclarationList( translationUnit ); |
---|
| 308 | } |
---|
| 309 | |
---|
[c2931ea] | 310 | Expression * InsertImplicitCalls::mutate( ApplicationExpr * appExpr ) { |
---|
| 311 | appExpr = dynamic_cast< ApplicationExpr * >( Mutator::mutate( appExpr ) ); |
---|
| 312 | assert( appExpr ); |
---|
| 313 | |
---|
| 314 | if ( VariableExpr * function = dynamic_cast< VariableExpr * > ( appExpr->get_function() ) ) { |
---|
| 315 | if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
| 316 | // optimization: don't need to copy construct in order to call intrinsic functions |
---|
| 317 | return appExpr; |
---|
| 318 | } else if ( DeclarationWithType * funcDecl = dynamic_cast< DeclarationWithType * > ( function->get_var() ) ) { |
---|
| 319 | FunctionType * ftype = dynamic_cast< FunctionType * >( GenPoly::getFunctionType( funcDecl->get_type() ) ); |
---|
| 320 | assert( ftype ); |
---|
[79970ed] | 321 | if ( (isConstructor( funcDecl->get_name() ) || funcDecl->get_name() == "?=?") && ftype->get_parameters().size() == 2 ) { |
---|
[c2931ea] | 322 | Type * t1 = ftype->get_parameters().front()->get_type(); |
---|
| 323 | Type * t2 = ftype->get_parameters().back()->get_type(); |
---|
| 324 | PointerType * ptrType = dynamic_cast< PointerType * > ( t1 ); |
---|
| 325 | assert( ptrType ); |
---|
| 326 | |
---|
| 327 | if ( ResolvExpr::typesCompatible( ptrType->get_base(), t2, SymTab::Indexer() ) ) { |
---|
| 328 | // optimization: don't need to copy construct in order to call a copy constructor or |
---|
| 329 | // assignment operator |
---|
| 330 | return appExpr; |
---|
[adcc065] | 331 | } // if |
---|
[79970ed] | 332 | } else if ( isDestructor( funcDecl->get_name() ) ) { |
---|
[c2931ea] | 333 | // correctness: never copy construct arguments to a destructor |
---|
[845cedc] | 334 | return appExpr; |
---|
[adcc065] | 335 | } // if |
---|
| 336 | } // if |
---|
| 337 | } // if |
---|
[c2931ea] | 338 | CP_CTOR_PRINT( std::cerr << "InsertImplicitCalls: adding a wrapper " << appExpr << std::endl; ) |
---|
| 339 | |
---|
| 340 | // wrap each function call so that it is easy to identify nodes that have to be copy constructed |
---|
| 341 | ImplicitCopyCtorExpr * expr = new ImplicitCopyCtorExpr( appExpr ); |
---|
| 342 | // save the type substitution onto the new node so that it is easy to find. |
---|
| 343 | // Ensure it is not deleted with the ImplicitCopyCtorExpr by removing it before deletion. |
---|
| 344 | // The substitution is needed to obtain the type of temporary variables so that copy constructor |
---|
| 345 | // calls can be resolved. Normally this is what PolyMutator is for, but the pass that resolves |
---|
| 346 | // copy constructor calls must be an Indexer. We could alternatively make a PolyIndexer which |
---|
| 347 | // saves the environment, or compute the types of temporaries here, but it's much simpler to |
---|
| 348 | // save the environment here, and more cohesive to compute temporary variables and resolve copy |
---|
| 349 | // constructor calls together. |
---|
| 350 | assert( env ); |
---|
| 351 | expr->set_env( env ); |
---|
| 352 | return expr; |
---|
[db4ecc5] | 353 | } |
---|
| 354 | |
---|
[c2931ea] | 355 | bool ResolveCopyCtors::skipCopyConstruct( Type * type ) { |
---|
| 356 | return dynamic_cast< VarArgsType * >( type ) || GenPoly::getFunctionType( type ); |
---|
| 357 | } |
---|
| 358 | |
---|
[65660bd] | 359 | Expression * ResolveCopyCtors::makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg ) { |
---|
[c2931ea] | 360 | assert( var ); |
---|
[1132b62] | 361 | return makeCtorDtor( fname, new AddressExpr( new VariableExpr( var ) ), cpArg ); |
---|
| 362 | } |
---|
| 363 | |
---|
[65660bd] | 364 | Expression * ResolveCopyCtors::makeCtorDtor( const std::string & fname, Expression * thisArg, Expression * cpArg ) { |
---|
[1132b62] | 365 | assert( thisArg ); |
---|
[c2931ea] | 366 | UntypedExpr * untyped = new UntypedExpr( new NameExpr( fname ) ); |
---|
[1132b62] | 367 | untyped->get_args().push_back( thisArg ); |
---|
[03b812d2] | 368 | if (cpArg) untyped->get_args().push_back( cpArg->clone() ); |
---|
[c2931ea] | 369 | |
---|
| 370 | // resolve copy constructor |
---|
[adcc065] | 371 | // should only be one alternative for copy ctor and dtor expressions, since all arguments are fixed |
---|
| 372 | // (VariableExpr and already resolved expression) |
---|
[c2931ea] | 373 | CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; ) |
---|
[65660bd] | 374 | Expression * resolved = ResolvExpr::findVoidExpression( untyped, *this ); |
---|
| 375 | assert( resolved ); |
---|
[c2931ea] | 376 | if ( resolved->get_env() ) { |
---|
| 377 | env->add( *resolved->get_env() ); |
---|
[adcc065] | 378 | } // if |
---|
[cf18eea] | 379 | |
---|
[c2931ea] | 380 | delete untyped; |
---|
| 381 | return resolved; |
---|
[540de412] | 382 | } |
---|
[db4ecc5] | 383 | |
---|
[1132b62] | 384 | void ResolveCopyCtors::copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
[c2931ea] | 385 | static UniqueName tempNamer("_tmp_cp"); |
---|
[1132b62] | 386 | CP_CTOR_PRINT( std::cerr << "Type Substitution: " << *impCpCtorExpr->get_env() << std::endl; ) |
---|
| 387 | assert( arg->has_result() ); |
---|
| 388 | Type * result = arg->get_result(); |
---|
| 389 | if ( skipCopyConstruct( result ) ) return; // skip certain non-copyable types |
---|
| 390 | |
---|
| 391 | // type may involve type variables, so apply type substitution to get temporary variable's actual type |
---|
| 392 | result = result->clone(); |
---|
| 393 | impCpCtorExpr->get_env()->apply( result ); |
---|
| 394 | ObjectDecl * tmp = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result, 0 ); |
---|
| 395 | tmp->get_type()->set_isConst( false ); |
---|
| 396 | |
---|
| 397 | // create and resolve copy constructor |
---|
| 398 | CP_CTOR_PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; ) |
---|
[65660bd] | 399 | Expression * cpCtor = makeCtorDtor( "?{}", tmp, arg ); |
---|
| 400 | |
---|
| 401 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( cpCtor ) ) { |
---|
| 402 | // if the chosen constructor is intrinsic, the copy is unnecessary, so |
---|
| 403 | // don't create the temporary and don't call the copy constructor |
---|
| 404 | VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() ); |
---|
| 405 | assert( function ); |
---|
| 406 | if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) return; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | // replace argument to function call with temporary |
---|
| 410 | arg = new CommaExpr( cpCtor, new VariableExpr( tmp ) ); |
---|
| 411 | impCpCtorExpr->get_tempDecls().push_back( tmp ); |
---|
| 412 | impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", tmp ) ); |
---|
[1132b62] | 413 | } |
---|
[c2931ea] | 414 | |
---|
[1132b62] | 415 | void ResolveCopyCtors::destructRet( Expression * ret, ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
| 416 | impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", new AddressExpr( ret ) ) ); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | void ResolveCopyCtors::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) { |
---|
[c2931ea] | 420 | CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; ) |
---|
| 421 | Visitor::visit( impCpCtorExpr ); |
---|
| 422 | env = impCpCtorExpr->get_env(); // xxx - maybe we really should just have a PolyIndexer... |
---|
| 423 | |
---|
| 424 | ApplicationExpr * appExpr = impCpCtorExpr->get_callExpr(); |
---|
| 425 | |
---|
| 426 | // take each argument and attempt to copy construct it. |
---|
| 427 | for ( Expression * & arg : appExpr->get_args() ) { |
---|
[1132b62] | 428 | copyConstructArg( arg, impCpCtorExpr ); |
---|
[adcc065] | 429 | } // for |
---|
[db4ecc5] | 430 | |
---|
[adcc065] | 431 | // each return value from the call needs to be connected with an ObjectDecl at the call site, which is |
---|
| 432 | // initialized with the return value and is destructed later |
---|
[c2931ea] | 433 | // xxx - handle multiple return values |
---|
| 434 | ApplicationExpr * callExpr = impCpCtorExpr->get_callExpr(); |
---|
[adcc065] | 435 | // xxx - is this right? callExpr may not have the right environment, because it was attached at a higher |
---|
| 436 | // level. Trying to pass that environment along. |
---|
[c2931ea] | 437 | callExpr->set_env( impCpCtorExpr->get_env()->clone() ); |
---|
[906e24d] | 438 | Type * result = appExpr->get_result(); |
---|
| 439 | if ( ! result->isVoid() ) { |
---|
[1132b62] | 440 | static UniqueName retNamer("_tmp_cp_ret"); |
---|
[c2931ea] | 441 | result = result->clone(); |
---|
| 442 | impCpCtorExpr->get_env()->apply( result ); |
---|
| 443 | ObjectDecl * ret = new ObjectDecl( retNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result, 0 ); |
---|
| 444 | ret->get_type()->set_isConst( false ); |
---|
| 445 | impCpCtorExpr->get_returnDecls().push_back( ret ); |
---|
| 446 | CP_CTOR_PRINT( std::cerr << "makeCtorDtor for a return" << std::endl; ) |
---|
[1132b62] | 447 | destructRet( new VariableExpr( ret ) , impCpCtorExpr ); |
---|
[adcc065] | 448 | } // for |
---|
[c2931ea] | 449 | CP_CTOR_PRINT( std::cerr << "after Resolving: " << impCpCtorExpr << std::endl; ) |
---|
[db4ecc5] | 450 | } |
---|
| 451 | |
---|
| 452 | |
---|
[c2931ea] | 453 | Expression * FixCopyCtors::mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
| 454 | CP_CTOR_PRINT( std::cerr << "FixCopyCtors: " << impCpCtorExpr << std::endl; ) |
---|
| 455 | |
---|
| 456 | impCpCtorExpr = dynamic_cast< ImplicitCopyCtorExpr * >( Mutator::mutate( impCpCtorExpr ) ); |
---|
| 457 | assert( impCpCtorExpr ); |
---|
[db4ecc5] | 458 | |
---|
[c2931ea] | 459 | std::list< ObjectDecl * > & tempDecls = impCpCtorExpr->get_tempDecls(); |
---|
| 460 | std::list< ObjectDecl * > & returnDecls = impCpCtorExpr->get_returnDecls(); |
---|
| 461 | std::list< Expression * > & dtors = impCpCtorExpr->get_dtors(); |
---|
[845cedc] | 462 | |
---|
[c2931ea] | 463 | // add all temporary declarations and their constructors |
---|
| 464 | for ( ObjectDecl * obj : tempDecls ) { |
---|
| 465 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) ); |
---|
[adcc065] | 466 | } // for |
---|
[c2931ea] | 467 | for ( ObjectDecl * obj : returnDecls ) { |
---|
| 468 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) ); |
---|
[adcc065] | 469 | } // for |
---|
[db4ecc5] | 470 | |
---|
[c2931ea] | 471 | // add destructors after current statement |
---|
| 472 | for ( Expression * dtor : dtors ) { |
---|
| 473 | stmtsToAddAfter.push_back( new ExprStmt( noLabels, dtor ) ); |
---|
[adcc065] | 474 | } // for |
---|
[db4ecc5] | 475 | |
---|
[c2931ea] | 476 | // xxx - update to work with multiple return values |
---|
| 477 | ObjectDecl * returnDecl = returnDecls.empty() ? NULL : returnDecls.front(); |
---|
| 478 | Expression * callExpr = impCpCtorExpr->get_callExpr(); |
---|
| 479 | |
---|
| 480 | CP_CTOR_PRINT( std::cerr << "Coming out the back..." << impCpCtorExpr << std::endl; ) |
---|
| 481 | |
---|
| 482 | // detach fields from wrapper node so that it can be deleted without deleting too much |
---|
| 483 | dtors.clear(); |
---|
| 484 | tempDecls.clear(); |
---|
| 485 | returnDecls.clear(); |
---|
| 486 | impCpCtorExpr->set_callExpr( NULL ); |
---|
| 487 | impCpCtorExpr->set_env( NULL ); |
---|
| 488 | delete impCpCtorExpr; |
---|
| 489 | |
---|
| 490 | if ( returnDecl ) { |
---|
| 491 | UntypedExpr * assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 492 | assign->get_args().push_back( new VariableExpr( returnDecl ) ); |
---|
| 493 | assign->get_args().push_back( callExpr ); |
---|
| 494 | // know the result type of the assignment is the type of the LHS (minus the pointer), so |
---|
| 495 | // add that onto the assignment expression so that later steps have the necessary information |
---|
[906e24d] | 496 | assign->set_result( returnDecl->get_type()->clone() ); |
---|
[c2931ea] | 497 | |
---|
| 498 | Expression * retExpr = new CommaExpr( assign, new VariableExpr( returnDecl ) ); |
---|
[906e24d] | 499 | if ( callExpr->get_result()->get_isLvalue() ) { |
---|
[adcc065] | 500 | // lvalue returning functions are funny. Lvalue.cc inserts a *? in front of any lvalue returning |
---|
| 501 | // non-intrinsic function. Add an AddressExpr to the call to negate the derefence and change the |
---|
| 502 | // type of the return temporary from T to T* to properly capture the return value. Then dereference |
---|
| 503 | // the result of the comma expression, since the lvalue returning call was originally wrapped with |
---|
| 504 | // an AddressExpr. Effectively, this turns |
---|
[c2931ea] | 505 | // lvalue T f(); |
---|
| 506 | // &*f() |
---|
| 507 | // into |
---|
| 508 | // T * tmp_cp_retN; |
---|
| 509 | // tmp_cp_ret_N = &*(tmp_cp_ret_N = &*f(), tmp_cp_ret); |
---|
| 510 | // which work out in terms of types, but is pretty messy. It would be nice to find a better way. |
---|
| 511 | assign->get_args().back() = new AddressExpr( assign->get_args().back() ); |
---|
| 512 | |
---|
| 513 | Type * resultType = returnDecl->get_type()->clone(); |
---|
| 514 | returnDecl->set_type( new PointerType( Type::Qualifiers(), returnDecl->get_type() ) ); |
---|
| 515 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 516 | deref->get_args().push_back( retExpr ); |
---|
[906e24d] | 517 | deref->set_result( resultType ); |
---|
[c2931ea] | 518 | retExpr = deref; |
---|
[adcc065] | 519 | } // if |
---|
[b6fe7e6] | 520 | retExpr->set_env( env->clone() ); |
---|
[c2931ea] | 521 | return retExpr; |
---|
| 522 | } else { |
---|
| 523 | return callExpr; |
---|
[adcc065] | 524 | } // if |
---|
[4ffdd63] | 525 | } |
---|
[c2931ea] | 526 | |
---|
| 527 | DeclarationWithType *FixInit::mutate( ObjectDecl *objDecl ) { |
---|
[adcc065] | 528 | // first recursively handle pieces of ObjectDecl so that they aren't missed by other visitors when the init |
---|
| 529 | // is removed from the ObjectDecl |
---|
[c2931ea] | 530 | objDecl = dynamic_cast< ObjectDecl * >( Mutator::mutate( objDecl ) ); |
---|
| 531 | |
---|
| 532 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
| 533 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL |
---|
| 534 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); |
---|
| 535 | if ( Statement * ctor = ctorInit->get_ctor() ) { |
---|
| 536 | if ( objDecl->get_storageClass() == DeclarationNode::Static ) { |
---|
[72e9222] | 537 | // originally wanted to take advantage of gcc nested functions, but |
---|
[f9cebb5] | 538 | // we get memory errors with this approach. To remedy this, the static |
---|
| 539 | // variable is hoisted when the destructor needs to be called. |
---|
[72e9222] | 540 | // |
---|
[c2931ea] | 541 | // generate: |
---|
[f9cebb5] | 542 | // static T __objName_static_varN; |
---|
[72e9222] | 543 | // void __objName_dtor_atexitN() { |
---|
[f9cebb5] | 544 | // __dtor__...; |
---|
[72e9222] | 545 | // } |
---|
| 546 | // int f(...) { |
---|
| 547 | // ... |
---|
| 548 | // static bool __objName_uninitialized = true; |
---|
| 549 | // if (__objName_uninitialized) { |
---|
| 550 | // __ctor(__objName); |
---|
| 551 | // __objName_uninitialized = false; |
---|
[f9cebb5] | 552 | // atexit(__objName_dtor_atexitN); |
---|
[c2931ea] | 553 | // } |
---|
[72e9222] | 554 | // ... |
---|
[c2931ea] | 555 | // } |
---|
| 556 | |
---|
[72e9222] | 557 | static UniqueName dtorCallerNamer( "_dtor_atexit" ); |
---|
| 558 | |
---|
| 559 | // static bool __objName_uninitialized = true |
---|
[c2931ea] | 560 | BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool ); |
---|
| 561 | SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant( boolType->clone(), "1" ) ), noDesignators ); |
---|
| 562 | ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", DeclarationNode::Static, LinkageSpec::Cforall, 0, boolType, boolInitExpr ); |
---|
| 563 | isUninitializedVar->fixUniqueId(); |
---|
| 564 | |
---|
| 565 | // __objName_uninitialized = false; |
---|
| 566 | UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 567 | setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) ); |
---|
| 568 | setTrue->get_args().push_back( new ConstantExpr( Constant( boolType->clone(), "0" ) ) ); |
---|
| 569 | |
---|
| 570 | // generate body of if |
---|
| 571 | CompoundStmt * initStmts = new CompoundStmt( noLabels ); |
---|
| 572 | std::list< Statement * > & body = initStmts->get_kids(); |
---|
| 573 | body.push_back( ctor ); |
---|
| 574 | body.push_back( new ExprStmt( noLabels, setTrue ) ); |
---|
| 575 | |
---|
| 576 | // put it all together |
---|
| 577 | IfStmt * ifStmt = new IfStmt( noLabels, new VariableExpr( isUninitializedVar ), initStmts, 0 ); |
---|
| 578 | stmtsToAddAfter.push_back( new DeclStmt( noLabels, isUninitializedVar ) ); |
---|
| 579 | stmtsToAddAfter.push_back( ifStmt ); |
---|
[72e9222] | 580 | |
---|
[f9cebb5] | 581 | if ( ctorInit->get_dtor() ) { |
---|
| 582 | // if the object has a non-trivial destructor, have to |
---|
| 583 | // hoist it and the object into the global space and |
---|
| 584 | // call the destructor function with atexit. |
---|
| 585 | |
---|
| 586 | Statement * dtorStmt = ctorInit->get_dtor()->clone(); |
---|
| 587 | |
---|
| 588 | // void __objName_dtor_atexitN(...) {...} |
---|
| 589 | FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + dtorCallerNamer.newName(), DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false ); |
---|
| 590 | dtorCaller->fixUniqueId(); |
---|
[c8dfcd3] | 591 | dtorCaller->get_statements()->push_back( dtorStmt ); |
---|
[f9cebb5] | 592 | |
---|
| 593 | // atexit(dtor_atexit); |
---|
| 594 | UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) ); |
---|
| 595 | callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) ); |
---|
| 596 | |
---|
| 597 | body.push_back( new ExprStmt( noLabels, callAtexit ) ); |
---|
| 598 | |
---|
| 599 | // hoist variable and dtor caller decls to list of decls that will be added into global scope |
---|
| 600 | staticDtorDecls.push_back( objDecl ); |
---|
| 601 | staticDtorDecls.push_back( dtorCaller ); |
---|
| 602 | |
---|
| 603 | // need to rename object uniquely since it now appears |
---|
| 604 | // at global scope and there could be multiple function-scoped |
---|
| 605 | // static variables with the same name in different functions. |
---|
[c8dfcd3] | 606 | // Note: it isn't sufficient to modify only the mangleName, because |
---|
| 607 | // then subsequent Indexer passes can choke on seeing the object's name |
---|
| 608 | // if another object has the same name and type. An unfortunate side-effect |
---|
| 609 | // of renaming the object is that subsequent NameExprs may fail to resolve, |
---|
| 610 | // but there shouldn't be any remaining past this point. |
---|
[f9cebb5] | 611 | static UniqueName staticNamer( "_static_var" ); |
---|
[c8dfcd3] | 612 | objDecl->set_name( objDecl->get_name() + staticNamer.newName() ); |
---|
| 613 | objDecl->set_mangleName( SymTab::Mangler::mangle( objDecl ) ); |
---|
[f9cebb5] | 614 | |
---|
| 615 | objDecl->set_init( NULL ); |
---|
| 616 | ctorInit->set_ctor( NULL ); |
---|
| 617 | delete ctorInit; |
---|
| 618 | |
---|
| 619 | // xxx - temporary hack: need to return a declaration, but want to hoist the current object out of this scope |
---|
| 620 | // create a new object which is never used |
---|
| 621 | static UniqueName dummyNamer( "_dummy" ); |
---|
| 622 | ObjectDecl * dummy = new ObjectDecl( dummyNamer.newName(), DeclarationNode::Static, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
| 623 | return dummy; |
---|
| 624 | } |
---|
[c2931ea] | 625 | } else { |
---|
| 626 | stmtsToAddAfter.push_back( ctor ); |
---|
[adcc065] | 627 | } // if |
---|
[c2931ea] | 628 | objDecl->set_init( NULL ); |
---|
| 629 | ctorInit->set_ctor( NULL ); |
---|
| 630 | } else if ( Initializer * init = ctorInit->get_init() ) { |
---|
| 631 | objDecl->set_init( init ); |
---|
| 632 | ctorInit->set_init( NULL ); |
---|
| 633 | } else { |
---|
| 634 | // no constructor and no initializer, which is okay |
---|
| 635 | objDecl->set_init( NULL ); |
---|
[adcc065] | 636 | } // if |
---|
[c2931ea] | 637 | delete ctorInit; |
---|
[adcc065] | 638 | } // if |
---|
[c2931ea] | 639 | return objDecl; |
---|
[db4ecc5] | 640 | } |
---|
| 641 | |
---|
[c2931ea] | 642 | void ObjDeclCollector::visit( CompoundStmt *compoundStmt ) { |
---|
| 643 | std::set< ObjectDecl * > prevVars = curVars; |
---|
| 644 | Parent::visit( compoundStmt ); |
---|
| 645 | curVars = prevVars; |
---|
[db4ecc5] | 646 | } |
---|
| 647 | |
---|
[c2931ea] | 648 | void ObjDeclCollector::visit( DeclStmt *stmt ) { |
---|
[4b2589a] | 649 | // keep track of all variables currently in scope |
---|
[c2931ea] | 650 | if ( ObjectDecl * objDecl = dynamic_cast< ObjectDecl * > ( stmt->get_decl() ) ) { |
---|
| 651 | curVars.insert( objDecl ); |
---|
[adcc065] | 652 | } // if |
---|
[e39aa0f] | 653 | Parent::visit( stmt ); |
---|
[db4ecc5] | 654 | } |
---|
| 655 | |
---|
[c2931ea] | 656 | void LabelFinder::handleStmt( Statement * stmt ) { |
---|
[4b2589a] | 657 | // for each label, remember the variables in scope at that label. |
---|
[c2931ea] | 658 | for ( Label l : stmt->get_labels() ) { |
---|
| 659 | vars[l] = curVars; |
---|
[adcc065] | 660 | } // for |
---|
[71f4e4f] | 661 | } |
---|
[f1e012b] | 662 | |
---|
[ec79847] | 663 | template<typename Iterator, typename OutputIterator> |
---|
| 664 | void insertDtors( Iterator begin, Iterator end, OutputIterator out ) { |
---|
| 665 | for ( Iterator it = begin ; it != end ; ++it ) { |
---|
[adcc065] | 666 | // extract destructor statement from the object decl and insert it into the output. Note that this is |
---|
| 667 | // only called on lists of non-static objects with implicit non-intrinsic dtors, so if the user manually |
---|
| 668 | // calls an intrinsic dtor then the call must (and will) still be generated since the argument may |
---|
| 669 | // contain side effects. |
---|
[c2931ea] | 670 | ObjectDecl * objDecl = *it; |
---|
| 671 | ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ); |
---|
| 672 | assert( ctorInit && ctorInit->get_dtor() ); |
---|
| 673 | *out++ = ctorInit->get_dtor()->clone(); |
---|
[adcc065] | 674 | } // for |
---|
[f1e012b] | 675 | } |
---|
[39786813] | 676 | |
---|
[c2931ea] | 677 | void InsertDtors::visit( ObjectDecl * objDecl ) { |
---|
| 678 | // remember non-static destructed objects so that their destructors can be inserted later |
---|
| 679 | if ( objDecl->get_storageClass() != DeclarationNode::Static ) { |
---|
| 680 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
| 681 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL |
---|
| 682 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); |
---|
| 683 | Statement * dtor = ctorInit->get_dtor(); |
---|
[f9cebb5] | 684 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { |
---|
[c2931ea] | 685 | // don't need to call intrinsic dtor, because it does nothing, but |
---|
| 686 | // non-intrinsic dtors must be called |
---|
| 687 | reverseDeclOrder.front().push_front( objDecl ); |
---|
[adcc065] | 688 | } // if |
---|
| 689 | } // if |
---|
| 690 | } // if |
---|
[c2931ea] | 691 | Parent::visit( objDecl ); |
---|
| 692 | } |
---|
[39786813] | 693 | |
---|
[c2931ea] | 694 | void InsertDtors::visit( CompoundStmt * compoundStmt ) { |
---|
[adcc065] | 695 | // visit statements - this will also populate reverseDeclOrder list. don't want to dump all destructors |
---|
| 696 | // when block is left, just the destructors associated with variables defined in this block, so push a new |
---|
| 697 | // list to the top of the stack so that we can differentiate scopes |
---|
[c2931ea] | 698 | reverseDeclOrder.push_front( OrderedDecls() ); |
---|
| 699 | Parent::visit( compoundStmt ); |
---|
[39786813] | 700 | |
---|
[4b2589a] | 701 | // add destructors for the current scope that we're exiting |
---|
[c2931ea] | 702 | std::list< Statement * > & statements = compoundStmt->get_kids(); |
---|
| 703 | insertDtors( reverseDeclOrder.front().begin(), reverseDeclOrder.front().end(), back_inserter( statements ) ); |
---|
| 704 | reverseDeclOrder.pop_front(); |
---|
[39786813] | 705 | } |
---|
| 706 | |
---|
[c2931ea] | 707 | void InsertDtors::visit( ReturnStmt * returnStmt ) { |
---|
[4b2589a] | 708 | // return exits all scopes, so dump destructors for all scopes |
---|
[c2931ea] | 709 | for ( OrderedDecls & od : reverseDeclOrder ) { |
---|
| 710 | insertDtors( od.begin(), od.end(), back_inserter( stmtsToAdd ) ); |
---|
[adcc065] | 711 | } // for |
---|
[39786813] | 712 | } |
---|
[f1e012b] | 713 | |
---|
[adcc065] | 714 | // Handle break/continue/goto in the same manner as C++. Basic idea: any objects that are in scope at the |
---|
| 715 | // BranchStmt but not at the labelled (target) statement must be destructed. If there are any objects in scope |
---|
| 716 | // at the target location but not at the BranchStmt then those objects would be uninitialized so notify the user |
---|
| 717 | // of the error. See C++ Reference 6.6 Jump Statements for details. |
---|
[c2931ea] | 718 | void InsertDtors::handleGoto( BranchStmt * stmt ) { |
---|
[e39aa0f] | 719 | assert( stmt->get_target() != "" && "BranchStmt missing a label" ); |
---|
[c2931ea] | 720 | // S_L = lvars = set of objects in scope at label definition |
---|
| 721 | // S_G = curVars = set of objects in scope at goto statement |
---|
| 722 | ObjectSet & lvars = labelVars[ stmt->get_target() ]; |
---|
| 723 | |
---|
| 724 | DTOR_PRINT( |
---|
| 725 | std::cerr << "at goto label: " << stmt->get_target().get_name() << std::endl; |
---|
| 726 | std::cerr << "S_G = " << printSet( curVars ) << std::endl; |
---|
| 727 | std::cerr << "S_L = " << printSet( lvars ) << std::endl; |
---|
| 728 | ) |
---|
| 729 | |
---|
| 730 | ObjectSet diff; |
---|
| 731 | // S_L-S_G results in set of objects whose construction is skipped - it's an error if this set is non-empty |
---|
| 732 | std::set_difference( lvars.begin(), lvars.end(), curVars.begin(), curVars.end(), std::inserter( diff, diff.begin() ) ); |
---|
| 733 | DTOR_PRINT( |
---|
| 734 | std::cerr << "S_L-S_G = " << printSet( diff ) << std::endl; |
---|
| 735 | ) |
---|
| 736 | if ( ! diff.empty() ) { |
---|
| 737 | throw SemanticError( std::string("jump to label '") + stmt->get_target().get_name() + "' crosses initialization of " + (*diff.begin())->get_name() + " ", stmt ); |
---|
[adcc065] | 738 | } // if |
---|
[c2931ea] | 739 | // S_G-S_L results in set of objects that must be destructed |
---|
| 740 | diff.clear(); |
---|
| 741 | std::set_difference( curVars.begin(), curVars.end(), lvars.begin(), lvars.end(), std::inserter( diff, diff.end() ) ); |
---|
| 742 | DTOR_PRINT( |
---|
| 743 | std::cerr << "S_G-S_L = " << printSet( diff ) << std::endl; |
---|
| 744 | ) |
---|
| 745 | if ( ! diff.empty() ) { |
---|
| 746 | // go through decl ordered list of objectdecl. for each element that occurs in diff, output destructor |
---|
| 747 | OrderedDecls ordered; |
---|
| 748 | for ( OrderedDecls & rdo : reverseDeclOrder ) { |
---|
| 749 | // add elements from reverseDeclOrder into ordered if they occur in diff - it is key that this happens in reverse declaration order. |
---|
| 750 | copy_if( rdo.begin(), rdo.end(), back_inserter( ordered ), [&]( ObjectDecl * objDecl ) { return diff.count( objDecl ); } ); |
---|
[adcc065] | 751 | } // for |
---|
[c2931ea] | 752 | insertDtors( ordered.begin(), ordered.end(), back_inserter( stmtsToAdd ) ); |
---|
[adcc065] | 753 | } // if |
---|
[c2931ea] | 754 | } |
---|
[39786813] | 755 | |
---|
[c2931ea] | 756 | void InsertDtors::visit( BranchStmt * stmt ) { |
---|
| 757 | switch( stmt->get_type() ) { |
---|
[adcc065] | 758 | case BranchStmt::Continue: |
---|
| 759 | case BranchStmt::Break: |
---|
| 760 | // could optimize the break/continue case, because the S_L-S_G check is unnecessary (this set should |
---|
| 761 | // always be empty), but it serves as a small sanity check. |
---|
| 762 | case BranchStmt::Goto: |
---|
| 763 | handleGoto( stmt ); |
---|
| 764 | break; |
---|
| 765 | default: |
---|
| 766 | assert( false ); |
---|
| 767 | } // switch |
---|
[c2931ea] | 768 | } |
---|
[79970ed] | 769 | |
---|
| 770 | bool checkWarnings( FunctionDecl * funcDecl ) { |
---|
| 771 | // only check for warnings if the current function is a user-defined |
---|
| 772 | // constructor or destructor |
---|
| 773 | if ( ! funcDecl ) return false; |
---|
| 774 | if ( ! funcDecl->get_statements() ) return false; |
---|
| 775 | return isCtorDtor( funcDecl->get_name() ) && ! LinkageSpec::isOverridable( funcDecl->get_linkage() ); |
---|
| 776 | } |
---|
| 777 | |
---|
[c8dfcd3] | 778 | void GenStructMemberCalls::visit( FunctionDecl * funcDecl ) { |
---|
| 779 | ValueGuard< FunctionDecl * > oldFunction( funcDecl ); |
---|
| 780 | ValueGuard< std::set< DeclarationWithType * > > oldUnhandled( unhandled ); |
---|
| 781 | ValueGuard< std::set< DeclarationWithType * > > oldUsedUninit( usedUninit ); |
---|
| 782 | ValueGuard< ObjectDecl * > oldThisParam( thisParam ); |
---|
| 783 | ValueGuard< bool > oldIsCtor( isCtor ); |
---|
[44f6341] | 784 | ValueGuard< StructDecl * > oldStructDecl( structDecl ); |
---|
[c8dfcd3] | 785 | |
---|
| 786 | // need to start with fresh sets |
---|
| 787 | unhandled.clear(); |
---|
| 788 | usedUninit.clear(); |
---|
[79970ed] | 789 | |
---|
| 790 | function = funcDecl; |
---|
[c8dfcd3] | 791 | isCtor = isConstructor( function->get_name() ); |
---|
| 792 | if ( checkWarnings( function ) ) { |
---|
| 793 | FunctionType * type = function->get_functionType(); |
---|
[79970ed] | 794 | assert( ! type->get_parameters().empty() ); |
---|
| 795 | thisParam = safe_dynamic_cast< ObjectDecl * >( type->get_parameters().front() ); |
---|
| 796 | PointerType * ptrType = safe_dynamic_cast< PointerType * > ( thisParam->get_type() ); |
---|
| 797 | StructInstType * structType = dynamic_cast< StructInstType * >( ptrType->get_base() ); |
---|
| 798 | if ( structType ) { |
---|
[44f6341] | 799 | structDecl = structType->get_baseStruct(); |
---|
[79970ed] | 800 | for ( Declaration * member : structDecl->get_members() ) { |
---|
| 801 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) { |
---|
| 802 | // record all of the struct type's members that need to be constructed or |
---|
| 803 | // destructed by the end of the function |
---|
| 804 | unhandled.insert( field ); |
---|
| 805 | } |
---|
| 806 | } |
---|
| 807 | } |
---|
| 808 | } |
---|
[c8dfcd3] | 809 | Parent::visit( function ); |
---|
| 810 | |
---|
| 811 | // remove the unhandled objects from usedUninit, because a call is inserted |
---|
| 812 | // to handle them - only objects that are later constructed are used uninitialized. |
---|
| 813 | std::set< DeclarationWithType * > diff; |
---|
| 814 | std::set_difference( usedUninit.begin(), usedUninit.end(), unhandled.begin(), unhandled.end(), std::inserter( diff, diff.begin() ) ); |
---|
| 815 | for ( DeclarationWithType * member : diff ) { |
---|
[44f6341] | 816 | emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", field ", member->get_name(), " used before being constructed" ); |
---|
[79970ed] | 817 | } |
---|
| 818 | |
---|
[c8dfcd3] | 819 | if ( ! unhandled.empty() ) { |
---|
| 820 | // need to explicitly re-add function parameters in order to resolve copy constructors |
---|
| 821 | enterScope(); |
---|
[b16898e] | 822 | maybeAccept( function->get_functionType(), *this ); |
---|
[44f6341] | 823 | |
---|
| 824 | // need to iterate through members in reverse in order for |
---|
| 825 | // ctor/dtor statements to come out in the right order |
---|
[1ba88a0] | 826 | for ( Declaration * member : reverseIterate( structDecl->get_members() ) ) { |
---|
[44f6341] | 827 | DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ); |
---|
| 828 | // skip non-DWT members |
---|
| 829 | if ( ! field ) continue; |
---|
| 830 | // skip handled members |
---|
| 831 | if ( ! unhandled.count( field ) ) continue; |
---|
| 832 | |
---|
| 833 | // insert and resolve default/copy constructor call for each field that's unhandled |
---|
[c8dfcd3] | 834 | std::list< Statement * > stmt; |
---|
| 835 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 836 | deref->get_args().push_back( new VariableExpr( thisParam ) ); |
---|
[4d4882a] | 837 | |
---|
| 838 | Expression * arg2 = 0; |
---|
| 839 | if ( isCopyConstructor( function ) ) { |
---|
[44f6341] | 840 | // if copy ctor, need to pass second-param-of-this-function.field |
---|
[4d4882a] | 841 | std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); |
---|
| 842 | assert( params.size() == 2 ); |
---|
[44f6341] | 843 | arg2 = new MemberExpr( field, new VariableExpr( params.back() ) ); |
---|
[4d4882a] | 844 | } |
---|
| 845 | InitExpander srcParam( arg2 ); |
---|
[44f6341] | 846 | SymTab::genImplicitCall( srcParam, new MemberExpr( field, deref ), function->get_name(), back_inserter( stmt ), field, isCtor ); |
---|
[c8dfcd3] | 847 | |
---|
| 848 | assert( stmt.size() <= 1 ); |
---|
| 849 | if ( stmt.size() == 1 ) { |
---|
| 850 | Statement * callStmt = stmt.front(); |
---|
| 851 | |
---|
| 852 | MutatingResolver resolver( *this ); |
---|
| 853 | try { |
---|
| 854 | callStmt->acceptMutator( resolver ); |
---|
| 855 | if ( isCtor ) { |
---|
| 856 | function->get_statements()->push_front( callStmt ); |
---|
| 857 | } else { |
---|
| 858 | // destructor statements should be added at the end |
---|
| 859 | function->get_statements()->push_back( callStmt ); |
---|
| 860 | } |
---|
| 861 | } catch ( SemanticError & error ) { |
---|
[44f6341] | 862 | emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", field ", field->get_name(), " not explicitly ", isCtor ? "constructed" : "destructed", " and no ", isCtor ? "default constructor" : "destructor", " found" ); |
---|
[c8dfcd3] | 863 | } |
---|
| 864 | } |
---|
| 865 | } |
---|
| 866 | leaveScope(); |
---|
| 867 | } |
---|
[79970ed] | 868 | } |
---|
| 869 | |
---|
[c8dfcd3] | 870 | void GenStructMemberCalls::visit( ApplicationExpr * appExpr ) { |
---|
[79970ed] | 871 | if ( ! checkWarnings( function ) ) return; |
---|
| 872 | |
---|
| 873 | std::string fname = getFunctionName( appExpr ); |
---|
| 874 | if ( fname == function->get_name() ) { |
---|
| 875 | // call to same kind of function |
---|
| 876 | Expression * firstParam = appExpr->get_args().front(); |
---|
| 877 | |
---|
| 878 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( firstParam ) ) { |
---|
| 879 | // if calling another constructor on thisParam, assume that function handles |
---|
| 880 | // all members - if it doesn't a warning will appear in that function. |
---|
| 881 | if ( varExpr->get_var() == thisParam ) { |
---|
| 882 | unhandled.clear(); |
---|
| 883 | } |
---|
| 884 | } else { |
---|
| 885 | // if first parameter is a member expression then |
---|
| 886 | // remove the member from unhandled set. |
---|
| 887 | handleFirstParam( firstParam ); |
---|
| 888 | } |
---|
| 889 | } |
---|
| 890 | |
---|
| 891 | Parent::visit( appExpr ); |
---|
| 892 | } |
---|
| 893 | |
---|
[c8dfcd3] | 894 | void GenStructMemberCalls::handleFirstParam( Expression * firstParam ) { |
---|
[79970ed] | 895 | using namespace std; |
---|
| 896 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( firstParam ) ) { |
---|
| 897 | if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( addrExpr->get_arg() ) ) { |
---|
| 898 | if ( ApplicationExpr * deref = dynamic_cast< ApplicationExpr * >( memberExpr->get_aggregate() ) ) { |
---|
| 899 | if ( getFunctionName( deref ) == "*?" && deref->get_args().size() == 1 ) { |
---|
| 900 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( deref->get_args().front() ) ) { |
---|
| 901 | if ( varExpr->get_var() == thisParam ) { |
---|
| 902 | unhandled.erase( memberExpr->get_member() ); |
---|
| 903 | } |
---|
| 904 | } |
---|
| 905 | } |
---|
| 906 | } |
---|
| 907 | } |
---|
| 908 | } |
---|
| 909 | } |
---|
| 910 | |
---|
[c8dfcd3] | 911 | void GenStructMemberCalls::visit( MemberExpr * memberExpr ) { |
---|
[79970ed] | 912 | if ( ! checkWarnings( function ) ) return; |
---|
[c8dfcd3] | 913 | if ( ! isCtor ) return; |
---|
[79970ed] | 914 | |
---|
| 915 | if ( ApplicationExpr * deref = dynamic_cast< ApplicationExpr * >( memberExpr->get_aggregate() ) ) { |
---|
| 916 | if ( getFunctionName( deref ) == "*?" && deref->get_args().size() == 1 ) { |
---|
| 917 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( deref->get_args().front() ) ) { |
---|
| 918 | if ( varExpr->get_var() == thisParam ) { |
---|
| 919 | if ( unhandled.count( memberExpr->get_member() ) ) { |
---|
| 920 | // emit a warning because a member was used before it was constructed |
---|
[c8dfcd3] | 921 | usedUninit.insert( memberExpr->get_member() ); |
---|
[79970ed] | 922 | } |
---|
| 923 | } |
---|
| 924 | } |
---|
| 925 | } |
---|
| 926 | } |
---|
| 927 | Parent::visit( memberExpr ); |
---|
| 928 | } |
---|
[3906301] | 929 | |
---|
| 930 | template< typename Visitor, typename... Params > |
---|
| 931 | void error( Visitor & v, const Params &... params ) { |
---|
| 932 | v.errors.append( toString( params... ) ); |
---|
| 933 | } |
---|
| 934 | |
---|
| 935 | template< typename... Params > |
---|
[c8dfcd3] | 936 | void GenStructMemberCalls::emit( const Params &... params ) { |
---|
[3906301] | 937 | // toggle warnings vs. errors here. |
---|
| 938 | // warn( params... ); |
---|
| 939 | error( *this, params... ); |
---|
| 940 | } |
---|
[c8dfcd3] | 941 | |
---|
| 942 | DeclarationWithType * MutatingResolver::mutate( ObjectDecl *objectDecl ) { |
---|
| 943 | // add object to the indexer assumes that there will be no name collisions |
---|
| 944 | // in generated code. If this changes, add mutate methods for entities with |
---|
| 945 | // scope and call {enter,leave}Scope explicitly. |
---|
| 946 | objectDecl->accept( indexer ); |
---|
| 947 | return objectDecl; |
---|
| 948 | } |
---|
| 949 | |
---|
| 950 | Expression* MutatingResolver::mutate( UntypedExpr *untypedExpr ) { |
---|
| 951 | return safe_dynamic_cast< ApplicationExpr * >( ResolvExpr::findVoidExpression( untypedExpr, indexer ) ); |
---|
| 952 | } |
---|
[b6fe7e6] | 953 | |
---|
| 954 | Expression * FixCtorExprs::mutate( ConstructorExpr * ctorExpr ) { |
---|
| 955 | static UniqueName tempNamer( "_tmp_ctor_expr" ); |
---|
[f0121d7] | 956 | // xxx - is the size check necessary? |
---|
[906e24d] | 957 | assert( ctorExpr->has_result() && ctorExpr->get_result()->size() == 1 ); |
---|
| 958 | ObjectDecl * tmp = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, ctorExpr->get_result()->clone(), nullptr ); |
---|
[b6fe7e6] | 959 | addDeclaration( tmp ); |
---|
| 960 | |
---|
| 961 | ApplicationExpr * callExpr = safe_dynamic_cast< ApplicationExpr * > ( ctorExpr->get_callExpr() ); |
---|
| 962 | TypeSubstitution * env = ctorExpr->get_env(); |
---|
| 963 | ctorExpr->set_callExpr( nullptr ); |
---|
| 964 | ctorExpr->set_env( nullptr ); |
---|
| 965 | |
---|
| 966 | Expression *& firstArg = callExpr->get_args().front(); |
---|
| 967 | UntypedExpr * assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 968 | assign->get_args().push_back( new VariableExpr( tmp ) ); |
---|
| 969 | assign->get_args().push_back( firstArg ); |
---|
[906e24d] | 970 | assign->set_result( ctorExpr->get_result()->clone() ); |
---|
[b6fe7e6] | 971 | firstArg = assign; |
---|
| 972 | |
---|
| 973 | CommaExpr * commaExpr = new CommaExpr( callExpr, new VariableExpr( tmp ) ); |
---|
| 974 | commaExpr->set_env( env ); |
---|
| 975 | delete ctorExpr; |
---|
| 976 | return commaExpr; |
---|
| 977 | } |
---|
[c2931ea] | 978 | } // namespace |
---|
[71f4e4f] | 979 | } // namespace InitTweak |
---|
| 980 | |
---|
| 981 | // Local Variables: // |
---|
| 982 | // tab-width: 4 // |
---|
| 983 | // mode: c++ // |
---|
| 984 | // compile-command: "make install" // |
---|
| 985 | // End: // |
---|