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