| [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 | // | 
|---|
| [9fe33947] | 7 | // FixInit.cc -- | 
|---|
| [71f4e4f] | 8 | // | 
|---|
|  | 9 | // Author           : Rob Schluntz | 
|---|
|  | 10 | // Created On       : Wed Jan 13 16:29:30 2016 | 
|---|
| [adcc065] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [60a8062] | 12 | // Last Modified On : Sun Feb 16 04:17:07 2020 | 
|---|
|  | 13 | // Update Count     : 82 | 
|---|
| [71f4e4f] | 14 | // | 
|---|
| [be9288a] | 15 | #include "FixInit.h" | 
|---|
| [71f4e4f] | 16 |  | 
|---|
| [d180746] | 17 | #include <stddef.h>                    // for NULL | 
|---|
|  | 18 | #include <algorithm>                   // for set_difference, copy_if | 
|---|
| [e3e16bc] | 19 | #include <cassert>                     // for assert, strict_dynamic_cast | 
|---|
| [d180746] | 20 | #include <iostream>                    // for operator<<, ostream, basic_ost... | 
|---|
|  | 21 | #include <iterator>                    // for insert_iterator, back_inserter | 
|---|
|  | 22 | #include <list>                        // for _List_iterator, list, list<>::... | 
|---|
|  | 23 | #include <map>                         // for _Rb_tree_iterator, _Rb_tree_co... | 
|---|
|  | 24 | #include <memory>                      // for allocator_traits<>::value_type | 
|---|
|  | 25 | #include <set>                         // for set, set<>::value_type | 
|---|
|  | 26 | #include <unordered_map>               // for unordered_map, unordered_map<>... | 
|---|
|  | 27 | #include <unordered_set>               // for unordered_set | 
|---|
|  | 28 | #include <utility>                     // for pair | 
|---|
| [134322e] | 29 |  | 
|---|
| [d180746] | 30 | #include "CodeGen/GenType.h"           // for genPrettyType | 
|---|
| [bff227f] | 31 | #include "CodeGen/OperatorTable.h" | 
|---|
| [d180746] | 32 | #include "Common/PassVisitor.h"        // for PassVisitor, WithStmtsToAdd | 
|---|
|  | 33 | #include "Common/SemanticError.h"      // for SemanticError | 
|---|
|  | 34 | #include "Common/UniqueName.h"         // for UniqueName | 
|---|
|  | 35 | #include "Common/utility.h"            // for CodeLocation, ValueGuard, toSt... | 
|---|
|  | 36 | #include "FixGlobalInit.h"             // for fixGlobalInit | 
|---|
|  | 37 | #include "GenInit.h"                   // for genCtorDtor | 
|---|
|  | 38 | #include "GenPoly/GenPoly.h"           // for getFunctionType | 
|---|
|  | 39 | #include "InitTweak.h"                 // for getFunctionName, getCallArg | 
|---|
|  | 40 | #include "ResolvExpr/Resolver.h"       // for findVoidExpression | 
|---|
| [e563edf] | 41 | #include "ResolvExpr/Unify.h"          // for typesCompatible | 
|---|
| [d180746] | 42 | #include "SymTab/Autogen.h"            // for genImplicitCall | 
|---|
|  | 43 | #include "SymTab/Indexer.h"            // for Indexer | 
|---|
|  | 44 | #include "SymTab/Mangler.h"            // for Mangler | 
|---|
| [07de76b] | 45 | #include "SynTree/LinkageSpec.h"       // for C, Spec, Cforall, isBuiltin | 
|---|
| [d180746] | 46 | #include "SynTree/Attribute.h"         // for Attribute | 
|---|
|  | 47 | #include "SynTree/Constant.h"          // for Constant | 
|---|
|  | 48 | #include "SynTree/Declaration.h"       // for ObjectDecl, FunctionDecl, Decl... | 
|---|
|  | 49 | #include "SynTree/Expression.h"        // for UniqueExpr, VariableExpr, Unty... | 
|---|
|  | 50 | #include "SynTree/Initializer.h"       // for ConstructorInit, SingleInit | 
|---|
| [ba3706f] | 51 | #include "SynTree/Label.h"             // for Label, operator< | 
|---|
| [d180746] | 52 | #include "SynTree/Mutator.h"           // for mutateAll, Mutator, maybeMutate | 
|---|
|  | 53 | #include "SynTree/Statement.h"         // for ExprStmt, CompoundStmt, Branch... | 
|---|
|  | 54 | #include "SynTree/Type.h"              // for Type, Type::StorageClasses | 
|---|
|  | 55 | #include "SynTree/TypeSubstitution.h"  // for TypeSubstitution, operator<< | 
|---|
| [90152a4] | 56 | #include "SynTree/DeclReplacer.h"      // for DeclReplacer | 
|---|
| [d180746] | 57 | #include "SynTree/Visitor.h"           // for acceptAll, maybeAccept | 
|---|
| [2bfc6b2] | 58 | #include "Validate/FindSpecialDecls.h" // for dtorStmt, dtorStructDestroy | 
|---|
| [71f4e4f] | 59 |  | 
|---|
| [f0121d7] | 60 | bool ctordtorp = false; // print all debug | 
|---|
|  | 61 | bool ctorp = false; // print ctor debug | 
|---|
|  | 62 | bool cpctorp = false; // print copy ctor debug | 
|---|
|  | 63 | bool dtorp = false; // print dtor debug | 
|---|
| [845cedc] | 64 | #define PRINT( text ) if ( ctordtorp ) { text } | 
|---|
| [c2931ea] | 65 | #define CP_CTOR_PRINT( text ) if ( ctordtorp || cpctorp ) { text } | 
|---|
|  | 66 | #define DTOR_PRINT( text ) if ( ctordtorp || dtorp ) { text } | 
|---|
| [845cedc] | 67 |  | 
|---|
| [71f4e4f] | 68 | namespace InitTweak { | 
|---|
|  | 69 | namespace { | 
|---|
| [babeeda] | 70 | struct SelfAssignChecker { | 
|---|
|  | 71 | void previsit( ApplicationExpr * appExpr ); | 
|---|
|  | 72 | }; | 
|---|
|  | 73 |  | 
|---|
| [0e315a5] | 74 | struct StmtExprResult { | 
|---|
|  | 75 | static void link( std::list< Declaration * > & translationUnit ); | 
|---|
|  | 76 |  | 
|---|
|  | 77 | void previsit( StmtExpr * stmtExpr ); | 
|---|
|  | 78 | }; | 
|---|
|  | 79 |  | 
|---|
| [02fdb8e] | 80 | struct InsertImplicitCalls : public WithConstTypeSubstitution { | 
|---|
| [adcc065] | 81 | /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which | 
|---|
|  | 82 | /// function calls need their parameters to be copy constructed | 
|---|
| [c0714bf] | 83 | static void insert( std::list< Declaration * > & translationUnit ); | 
|---|
| [134322e] | 84 |  | 
|---|
|  | 85 | Expression * postmutate( ApplicationExpr * appExpr ); | 
|---|
| [c2931ea] | 86 | }; | 
|---|
|  | 87 |  | 
|---|
| [2f86ddf] | 88 | struct ResolveCopyCtors final : public WithStmtsToAdd, public WithIndexer, public WithShortCircuiting, public WithTypeSubstitution, public WithVisitorRef<ResolveCopyCtors> { | 
|---|
| [adcc065] | 89 | /// generate temporary ObjectDecls for each argument and return value of each ImplicitCopyCtorExpr, | 
|---|
|  | 90 | /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both | 
|---|
|  | 91 | /// arguments and return value temporaries | 
|---|
| [2f86ddf] | 92 | static void resolveImplicitCalls( std::list< Declaration * > & translationUnit ); | 
|---|
| [31f379c] | 93 |  | 
|---|
| [2f86ddf] | 94 | Expression * postmutate( ImplicitCopyCtorExpr * impCpCtorExpr ); | 
|---|
|  | 95 | void premutate( StmtExpr * stmtExpr ); | 
|---|
|  | 96 | void premutate( UniqueExpr * unqExpr ); | 
|---|
| [c2931ea] | 97 |  | 
|---|
|  | 98 | /// create and resolve ctor/dtor expression: fname(var, [cpArg]) | 
|---|
| [65660bd] | 99 | Expression * makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg = NULL ); | 
|---|
| [c2931ea] | 100 | /// true if type does not need to be copy constructed to ensure correctness | 
|---|
| [1132b62] | 101 | bool skipCopyConstruct( Type * type ); | 
|---|
| [ae1b9ea] | 102 | void copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr, Type * formal ); | 
|---|
| [2f86ddf] | 103 | void destructRet( ObjectDecl * ret, ImplicitCopyCtorExpr * impCpCtorExpr, Expression *& arg ); | 
|---|
| [c2931ea] | 104 | }; | 
|---|
|  | 105 |  | 
|---|
|  | 106 | /// collects constructed object decls - used as a base class | 
|---|
| [698ec72] | 107 | struct ObjDeclCollector : public WithGuards, public WithShortCircuiting { | 
|---|
| [62423350] | 108 | // use ordered data structure to maintain ordering for set_difference and for consistent error messages | 
|---|
|  | 109 | typedef std::list< ObjectDecl * > ObjectSet; | 
|---|
| [698ec72] | 110 | void previsit( CompoundStmt *compoundStmt ); | 
|---|
|  | 111 | void previsit( DeclStmt *stmt ); | 
|---|
| [52c14b3] | 112 |  | 
|---|
|  | 113 | // don't go into other functions | 
|---|
| [698ec72] | 114 | void previsit( FunctionDecl * ) { visit_children = false; } | 
|---|
| [52c14b3] | 115 |  | 
|---|
| [c2931ea] | 116 | protected: | 
|---|
|  | 117 | ObjectSet curVars; | 
|---|
|  | 118 | }; | 
|---|
|  | 119 |  | 
|---|
| [6cf27a07] | 120 | // debug | 
|---|
| [62423350] | 121 | template<typename ObjectSet> | 
|---|
|  | 122 | struct PrintSet { | 
|---|
|  | 123 | PrintSet( const ObjectSet & objs ) : objs( objs ) {} | 
|---|
| [c2931ea] | 124 | const ObjectSet & objs; | 
|---|
|  | 125 | }; | 
|---|
| [62423350] | 126 | template<typename ObjectSet> | 
|---|
|  | 127 | PrintSet<ObjectSet> printSet( const ObjectSet & objs ) { return PrintSet<ObjectSet>( objs ); } | 
|---|
|  | 128 | template<typename ObjectSet> | 
|---|
|  | 129 | std::ostream & operator<<( std::ostream & out, const PrintSet<ObjectSet> & set) { | 
|---|
| [c2931ea] | 130 | out << "{ "; | 
|---|
|  | 131 | for ( ObjectDecl * obj : set.objs ) { | 
|---|
|  | 132 | out << obj->get_name() << ", " ; | 
|---|
| [adcc065] | 133 | } // for | 
|---|
| [c2931ea] | 134 | out << " }"; | 
|---|
|  | 135 | return out; | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
| [698ec72] | 138 | struct LabelFinder final : public ObjDeclCollector { | 
|---|
| [c2931ea] | 139 | typedef std::map< Label, ObjectSet > LabelMap; | 
|---|
|  | 140 | // map of Label -> live variables at that label | 
|---|
|  | 141 | LabelMap vars; | 
|---|
|  | 142 |  | 
|---|
| [698ec72] | 143 | typedef ObjDeclCollector Parent; | 
|---|
|  | 144 | using Parent::previsit; | 
|---|
|  | 145 | void previsit( Statement * stmt ); | 
|---|
|  | 146 |  | 
|---|
|  | 147 | void previsit( CompoundStmt *compoundStmt ); | 
|---|
|  | 148 | void previsit( DeclStmt *stmt ); | 
|---|
| [c2931ea] | 149 | }; | 
|---|
|  | 150 |  | 
|---|
| [698ec72] | 151 | struct InsertDtors final : public ObjDeclCollector, public WithStmtsToAdd { | 
|---|
| [adcc065] | 152 | /// insert destructor calls at the appropriate places.  must happen before CtorInit nodes are removed | 
|---|
|  | 153 | /// (currently by FixInit) | 
|---|
| [c2931ea] | 154 | static void insert( std::list< Declaration * > & translationUnit ); | 
|---|
|  | 155 |  | 
|---|
|  | 156 | typedef std::list< ObjectDecl * > OrderedDecls; | 
|---|
|  | 157 | typedef std::list< OrderedDecls > OrderedDeclsStack; | 
|---|
|  | 158 |  | 
|---|
| [698ec72] | 159 | InsertDtors( PassVisitor<LabelFinder> & finder ) : finder( finder ), labelVars( finder.pass.vars ) {} | 
|---|
| [c2931ea] | 160 |  | 
|---|
| [698ec72] | 161 | typedef ObjDeclCollector Parent; | 
|---|
|  | 162 | using Parent::previsit; | 
|---|
| [62e5546] | 163 |  | 
|---|
| [698ec72] | 164 | void previsit( FunctionDecl * funcDecl ); | 
|---|
| [c2931ea] | 165 |  | 
|---|
| [698ec72] | 166 | void previsit( BranchStmt * stmt ); | 
|---|
| [c2931ea] | 167 | private: | 
|---|
|  | 168 | void handleGoto( BranchStmt * stmt ); | 
|---|
|  | 169 |  | 
|---|
| [698ec72] | 170 | PassVisitor<LabelFinder> & finder; | 
|---|
| [c2931ea] | 171 | LabelFinder::LabelMap & labelVars; | 
|---|
|  | 172 | OrderedDeclsStack reverseDeclOrder; | 
|---|
|  | 173 | }; | 
|---|
|  | 174 |  | 
|---|
| [0508ab3] | 175 | class FixInit : public WithStmtsToAdd { | 
|---|
| [c2931ea] | 176 | public: | 
|---|
|  | 177 | /// expand each object declaration to use its constructor after it is declared. | 
|---|
|  | 178 | static void fixInitializers( std::list< Declaration * > &translationUnit ); | 
|---|
|  | 179 |  | 
|---|
| [aff3af4] | 180 | DeclarationWithType * postmutate( ObjectDecl *objDecl ); | 
|---|
| [72e9222] | 181 |  | 
|---|
|  | 182 | std::list< Declaration * > staticDtorDecls; | 
|---|
| [c2931ea] | 183 | }; | 
|---|
|  | 184 |  | 
|---|
| [b3fc977] | 185 | struct GenStructMemberCalls final : public WithGuards, public WithShortCircuiting, public WithIndexer, public WithVisitorRef<GenStructMemberCalls> { | 
|---|
| [c8dfcd3] | 186 | /// generate default/copy ctor and dtor calls for user-defined struct ctor/dtors | 
|---|
|  | 187 | /// for any member that is missing a corresponding ctor/dtor call. | 
|---|
|  | 188 | /// error if a member is used before constructed | 
|---|
|  | 189 | static void generate( std::list< Declaration * > & translationUnit ); | 
|---|
| [79970ed] | 190 |  | 
|---|
| [b3fc977] | 191 | void premutate( FunctionDecl * funcDecl ); | 
|---|
|  | 192 | DeclarationWithType * postmutate( FunctionDecl * funcDecl ); | 
|---|
| [79970ed] | 193 |  | 
|---|
| [b3fc977] | 194 | void premutate( MemberExpr * memberExpr ); | 
|---|
|  | 195 | void premutate( ApplicationExpr * appExpr ); | 
|---|
| [79970ed] | 196 |  | 
|---|
| [b3fc977] | 197 | /// Note: this post mutate used to be in a separate visitor. If this pass breaks, one place to examine is whether it is | 
|---|
|  | 198 | /// okay for this part of the recursion to occur alongside the rest. | 
|---|
|  | 199 | Expression * postmutate( UntypedExpr * expr ); | 
|---|
| [79970ed] | 200 |  | 
|---|
| [a16764a6] | 201 | SemanticErrorException errors; | 
|---|
| [79970ed] | 202 | private: | 
|---|
| [3906301] | 203 | template< typename... Params > | 
|---|
| [64ac636] | 204 | void emit( CodeLocation, const Params &... params ); | 
|---|
| [79970ed] | 205 |  | 
|---|
| [b0f601fa] | 206 | FunctionDecl * function = nullptr; | 
|---|
| [64ac636] | 207 | std::set< DeclarationWithType * > unhandled; | 
|---|
|  | 208 | std::map< DeclarationWithType *, CodeLocation > usedUninit; | 
|---|
| [b0f601fa] | 209 | ObjectDecl * thisParam = nullptr; | 
|---|
| [c8dfcd3] | 210 | bool isCtor = false; // true if current function is a constructor | 
|---|
| [b0f601fa] | 211 | StructDecl * structDecl = nullptr; | 
|---|
| [c8dfcd3] | 212 | }; | 
|---|
|  | 213 |  | 
|---|
| [696bf6e] | 214 | struct FixCtorExprs final : public WithDeclsToAdd, public WithIndexer { | 
|---|
| [b6fe7e6] | 215 | /// expands ConstructorExpr nodes into comma expressions, using a temporary for the first argument | 
|---|
|  | 216 | static void fix( std::list< Declaration * > & translationUnit ); | 
|---|
|  | 217 |  | 
|---|
| [696bf6e] | 218 | Expression * postmutate( ConstructorExpr * ctorExpr ); | 
|---|
| [b6fe7e6] | 219 | }; | 
|---|
| [ec42ff2e] | 220 |  | 
|---|
| [490fb92e] | 221 | struct SplitExpressions : public WithShortCircuiting, /*public WithTypeSubstitution, */public WithStmtsToAdd { | 
|---|
| [ec42ff2e] | 222 | /// add CompoundStmts around top-level expressions so that temporaries are destroyed in the correct places. | 
|---|
|  | 223 | static void split( std::list< Declaration * > &translationUnit ); | 
|---|
|  | 224 |  | 
|---|
|  | 225 | Statement * postmutate( ExprStmt * stmt ); | 
|---|
|  | 226 | void premutate( TupleAssignExpr * expr ); | 
|---|
|  | 227 | }; | 
|---|
| [c2931ea] | 228 | } // namespace | 
|---|
| [db4ecc5] | 229 |  | 
|---|
| [1be845b] | 230 | void fix( std::list< Declaration * > & translationUnit, bool inLibrary ) { | 
|---|
| [babeeda] | 231 | PassVisitor<SelfAssignChecker> checker; | 
|---|
|  | 232 | acceptAll( translationUnit, checker ); | 
|---|
|  | 233 |  | 
|---|
| [0e315a5] | 234 | // fixes StmtExpr to properly link to their resulting expression | 
|---|
|  | 235 | StmtExprResult::link( translationUnit ); | 
|---|
|  | 236 |  | 
|---|
| [6cf27a07] | 237 | // fixes ConstructorInit for global variables. should happen before fixInitializers. | 
|---|
| [1be845b] | 238 | InitTweak::fixGlobalInit( translationUnit, inLibrary ); | 
|---|
| [6cf27a07] | 239 |  | 
|---|
| [ec42ff2e] | 240 | // must happen before ResolveCopyCtors because temporaries have to be inserted into the correct scope | 
|---|
|  | 241 | SplitExpressions::split( translationUnit ); | 
|---|
|  | 242 |  | 
|---|
| [c0714bf] | 243 | InsertImplicitCalls::insert( translationUnit ); | 
|---|
| [c2931ea] | 244 |  | 
|---|
| [2f86ddf] | 245 | // Needs to happen before ResolveCopyCtors, because argument/return temporaries should not be considered in | 
|---|
|  | 246 | // error checking branch statements | 
|---|
|  | 247 | InsertDtors::insert( translationUnit ); | 
|---|
| [79970ed] | 248 |  | 
|---|
| [2f86ddf] | 249 | ResolveCopyCtors::resolveImplicitCalls( translationUnit ); | 
|---|
|  | 250 | FixInit::fixInitializers( translationUnit ); | 
|---|
| [c8dfcd3] | 251 | GenStructMemberCalls::generate( translationUnit ); | 
|---|
| [ae1b9ea] | 252 |  | 
|---|
| [bcc0946] | 253 | // Needs to happen after GenStructMemberCalls, since otherwise member constructors exprs | 
|---|
|  | 254 | // don't have the correct form, and a member can be constructed more than once. | 
|---|
| [b6fe7e6] | 255 | FixCtorExprs::fix( translationUnit ); | 
|---|
| [db4ecc5] | 256 | } | 
|---|
|  | 257 |  | 
|---|
| [c2931ea] | 258 | namespace { | 
|---|
| [bcc0946] | 259 | /// find and return the destructor used in `input`. If `input` is not a simple destructor call, generate a thunk | 
|---|
|  | 260 | /// that wraps the destructor, insert it into `stmtsToAdd` and return the new function declaration | 
|---|
|  | 261 | DeclarationWithType * getDtorFunc( ObjectDecl * objDecl, Statement * input, std::list< Statement * > & stmtsToAdd ) { | 
|---|
|  | 262 | // unwrap implicit statement wrapper | 
|---|
|  | 263 | Statement * dtor = input; | 
|---|
|  | 264 | assert( dtor ); | 
|---|
|  | 265 | std::list< Expression * > matches; | 
|---|
|  | 266 | collectCtorDtorCalls( dtor, matches ); | 
|---|
|  | 267 |  | 
|---|
|  | 268 | if ( dynamic_cast< ExprStmt * >( dtor ) ) { | 
|---|
|  | 269 | // only one destructor call in the expression | 
|---|
|  | 270 | if ( matches.size() == 1 ) { | 
|---|
|  | 271 | DeclarationWithType * func = getFunction( matches.front() ); | 
|---|
|  | 272 | assertf( func, "getFunction failed to find function in %s", toString( matches.front() ).c_str() ); | 
|---|
|  | 273 |  | 
|---|
|  | 274 | // cleanup argument must be a function, not an object (including function pointer) | 
|---|
|  | 275 | if ( FunctionDecl * dtorFunc = dynamic_cast< FunctionDecl * > ( func ) ) { | 
|---|
|  | 276 | if ( dtorFunc->type->forall.empty() ) { | 
|---|
|  | 277 | // simple case where the destructor is a monomorphic function call - can simply | 
|---|
|  | 278 | // use that function as the cleanup function. | 
|---|
|  | 279 | delete dtor; | 
|---|
|  | 280 | return func; | 
|---|
|  | 281 | } | 
|---|
|  | 282 | } | 
|---|
|  | 283 | } | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
|  | 286 | // otherwise the cleanup is more complicated - need to build a single argument cleanup function that | 
|---|
|  | 287 | // wraps the more complicated code. | 
|---|
|  | 288 | static UniqueName dtorNamer( "__cleanup_dtor" ); | 
|---|
| [546e712] | 289 | std::string name = dtorNamer.newName(); | 
|---|
|  | 290 | FunctionDecl * dtorFunc = FunctionDecl::newFunction( name, SymTab::genDefaultType( objDecl->type->stripReferences(), false ), new CompoundStmt() ); | 
|---|
| [bcc0946] | 291 | stmtsToAdd.push_back( new DeclStmt( dtorFunc ) ); | 
|---|
|  | 292 |  | 
|---|
|  | 293 | // the original code contains uses of objDecl - replace them with the newly generated 'this' parameter. | 
|---|
|  | 294 | ObjectDecl * thisParam = getParamThis( dtorFunc->type ); | 
|---|
|  | 295 | Expression * replacement = new VariableExpr( thisParam ); | 
|---|
|  | 296 |  | 
|---|
|  | 297 | Type * base = replacement->result->stripReferences(); | 
|---|
|  | 298 | if ( dynamic_cast< ArrayType * >( base ) || dynamic_cast< TupleType * > ( base ) ) { | 
|---|
|  | 299 | // need to cast away reference for array types, since the destructor is generated without the reference type, | 
|---|
|  | 300 | // and for tuple types since tuple indexing does not work directly on a reference | 
|---|
|  | 301 | replacement = new CastExpr( replacement, base->clone() ); | 
|---|
|  | 302 | } | 
|---|
| [4e5e6cc] | 303 | DeclReplacer::replace( dtor, { std::make_pair( objDecl, replacement ) } ); | 
|---|
| [bcc0946] | 304 | dtorFunc->statements->push_back( strict_dynamic_cast<Statement *>( dtor ) ); | 
|---|
|  | 305 |  | 
|---|
|  | 306 | return dtorFunc; | 
|---|
|  | 307 | } | 
|---|
|  | 308 |  | 
|---|
| [0e315a5] | 309 | void StmtExprResult::link( std::list< Declaration * > & translationUnit ) { | 
|---|
|  | 310 | PassVisitor<StmtExprResult> linker; | 
|---|
|  | 311 | acceptAll( translationUnit, linker ); | 
|---|
|  | 312 | } | 
|---|
|  | 313 |  | 
|---|
| [ec42ff2e] | 314 | void SplitExpressions::split( std::list< Declaration * > & translationUnit ) { | 
|---|
|  | 315 | PassVisitor<SplitExpressions> splitter; | 
|---|
|  | 316 | mutateAll( translationUnit, splitter ); | 
|---|
|  | 317 | } | 
|---|
|  | 318 |  | 
|---|
| [c0714bf] | 319 | void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit ) { | 
|---|
|  | 320 | PassVisitor<InsertImplicitCalls> inserter; | 
|---|
| [c2931ea] | 321 | mutateAll( translationUnit, inserter ); | 
|---|
|  | 322 | } | 
|---|
| [db4ecc5] | 323 |  | 
|---|
| [2f86ddf] | 324 | void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit ) { | 
|---|
|  | 325 | PassVisitor<ResolveCopyCtors> resolver; | 
|---|
|  | 326 | mutateAll( translationUnit, resolver ); | 
|---|
| [c2931ea] | 327 | } | 
|---|
| [71f4e4f] | 328 |  | 
|---|
| [c2931ea] | 329 | void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) { | 
|---|
| [aff3af4] | 330 | PassVisitor<FixInit> fixer; | 
|---|
| [72e9222] | 331 |  | 
|---|
|  | 332 | // can't use mutateAll, because need to insert declarations at top-level | 
|---|
|  | 333 | // can't use DeclMutator, because sometimes need to insert IfStmt, etc. | 
|---|
| [a16764a6] | 334 | SemanticErrorException errors; | 
|---|
| [72e9222] | 335 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { | 
|---|
|  | 336 | try { | 
|---|
| [3c398b6] | 337 | maybeMutate( *i, fixer ); | 
|---|
| [aff3af4] | 338 | translationUnit.splice( i, fixer.pass.staticDtorDecls ); | 
|---|
| [a16764a6] | 339 | } catch( SemanticErrorException &e ) { | 
|---|
| [72e9222] | 340 | errors.append( e ); | 
|---|
|  | 341 | } // try | 
|---|
|  | 342 | } // for | 
|---|
|  | 343 | if ( ! errors.isEmpty() ) { | 
|---|
|  | 344 | throw errors; | 
|---|
|  | 345 | } // if | 
|---|
| [c2931ea] | 346 | } | 
|---|
| [71f4e4f] | 347 |  | 
|---|
| [c2931ea] | 348 | void InsertDtors::insert( std::list< Declaration * > & translationUnit ) { | 
|---|
| [698ec72] | 349 | PassVisitor<LabelFinder> finder; | 
|---|
|  | 350 | PassVisitor<InsertDtors> inserter( finder ); | 
|---|
| [c2931ea] | 351 | acceptAll( translationUnit, inserter ); | 
|---|
|  | 352 | } | 
|---|
|  | 353 |  | 
|---|
| [c8dfcd3] | 354 | void GenStructMemberCalls::generate( std::list< Declaration * > & translationUnit ) { | 
|---|
| [9a707e4e] | 355 | PassVisitor<GenStructMemberCalls> warner; | 
|---|
| [b3fc977] | 356 | mutateAll( translationUnit, warner ); | 
|---|
| [79970ed] | 357 | } | 
|---|
|  | 358 |  | 
|---|
| [b6fe7e6] | 359 | void FixCtorExprs::fix( std::list< Declaration * > & translationUnit ) { | 
|---|
| [696bf6e] | 360 | PassVisitor<FixCtorExprs> fixer; | 
|---|
|  | 361 | mutateAll( translationUnit, fixer ); | 
|---|
| [b6fe7e6] | 362 | } | 
|---|
|  | 363 |  | 
|---|
| [0e315a5] | 364 | void StmtExprResult::previsit( StmtExpr * stmtExpr ) { | 
|---|
|  | 365 | // we might loose the result expression here so add a pointer to trace back | 
|---|
|  | 366 | assert( stmtExpr->result ); | 
|---|
|  | 367 | Type * result = stmtExpr->result; | 
|---|
|  | 368 | if ( ! result->isVoid() ) { | 
|---|
|  | 369 | CompoundStmt * body = stmtExpr->statements; | 
|---|
|  | 370 | assert( ! body->kids.empty() ); | 
|---|
|  | 371 | stmtExpr->resultExpr = strict_dynamic_cast< ExprStmt * >( body->kids.back() ); | 
|---|
|  | 372 | } | 
|---|
|  | 373 | } | 
|---|
|  | 374 |  | 
|---|
| [ec42ff2e] | 375 | Statement * SplitExpressions::postmutate( ExprStmt * stmt ) { | 
|---|
|  | 376 | // wrap each top-level ExprStmt in a block so that destructors for argument and return temporaries are destroyed | 
|---|
|  | 377 | // in the correct places | 
|---|
|  | 378 | CompoundStmt * ret = new CompoundStmt( { stmt } ); | 
|---|
|  | 379 | return ret; | 
|---|
|  | 380 | } | 
|---|
| [babeeda] | 381 |  | 
|---|
| [ec42ff2e] | 382 | void SplitExpressions::premutate( TupleAssignExpr * ) { | 
|---|
|  | 383 | // don't do this within TupleAssignExpr, since it is already broken up into multiple expressions | 
|---|
|  | 384 | visit_children = false; | 
|---|
|  | 385 | } | 
|---|
| [babeeda] | 386 |  | 
|---|
| [ec42ff2e] | 387 | // Relatively simple structural comparison for expressions, needed to determine | 
|---|
|  | 388 | // if two expressions are "the same" (used to determine if self assignment occurs) | 
|---|
|  | 389 | struct StructuralChecker { | 
|---|
|  | 390 | Expression * stripCasts( Expression * expr ) { | 
|---|
|  | 391 | // this might be too permissive. It's possible that only particular casts are relevant. | 
|---|
|  | 392 | while ( CastExpr * cast = dynamic_cast< CastExpr * >( expr ) ) { | 
|---|
|  | 393 | expr = cast->arg; | 
|---|
| [babeeda] | 394 | } | 
|---|
| [ec42ff2e] | 395 | return expr; | 
|---|
|  | 396 | } | 
|---|
| [babeeda] | 397 |  | 
|---|
| [ec42ff2e] | 398 | void previsit( Expression * ) { | 
|---|
|  | 399 | // anything else does not qualify | 
|---|
|  | 400 | isSimilar = false; | 
|---|
|  | 401 | } | 
|---|
| [babeeda] | 402 |  | 
|---|
| [ec42ff2e] | 403 | template<typename T> | 
|---|
|  | 404 | T * cast( Expression * node ) { | 
|---|
|  | 405 | // all expressions need to ignore casts, so this bit has been factored out | 
|---|
|  | 406 | return dynamic_cast< T * >( stripCasts( node ) ); | 
|---|
|  | 407 | } | 
|---|
| [babeeda] | 408 |  | 
|---|
| [ec42ff2e] | 409 | // ignore casts | 
|---|
|  | 410 | void previsit( CastExpr * ) {} | 
|---|
|  | 411 |  | 
|---|
|  | 412 | void previsit( MemberExpr * memExpr ) { | 
|---|
|  | 413 | if ( MemberExpr * otherMember = cast< MemberExpr >( other ) ) { | 
|---|
|  | 414 | if ( otherMember->member == memExpr->member ) { | 
|---|
|  | 415 | other = otherMember->aggregate; | 
|---|
|  | 416 | return; | 
|---|
| [babeeda] | 417 | } | 
|---|
|  | 418 | } | 
|---|
| [ec42ff2e] | 419 | isSimilar = false; | 
|---|
|  | 420 | } | 
|---|
| [babeeda] | 421 |  | 
|---|
| [ec42ff2e] | 422 | void previsit( VariableExpr * varExpr ) { | 
|---|
|  | 423 | if ( VariableExpr * otherVar = cast< VariableExpr >( other ) ) { | 
|---|
|  | 424 | if ( otherVar->var == varExpr->var ) { | 
|---|
| [babeeda] | 425 | return; | 
|---|
|  | 426 | } | 
|---|
|  | 427 | } | 
|---|
| [ec42ff2e] | 428 | isSimilar = false; | 
|---|
|  | 429 | } | 
|---|
| [babeeda] | 430 |  | 
|---|
| [ec42ff2e] | 431 | void previsit( AddressExpr * ) { | 
|---|
|  | 432 | if ( AddressExpr * addrExpr = cast< AddressExpr >( other ) ) { | 
|---|
|  | 433 | other = addrExpr->arg; | 
|---|
|  | 434 | return; | 
|---|
|  | 435 | } | 
|---|
|  | 436 | isSimilar = false; | 
|---|
| [babeeda] | 437 | } | 
|---|
| [ec42ff2e] | 438 |  | 
|---|
|  | 439 | Expression * other = nullptr; | 
|---|
|  | 440 | bool isSimilar = true; | 
|---|
|  | 441 | }; | 
|---|
|  | 442 |  | 
|---|
|  | 443 | bool structurallySimilar( Expression * e1, Expression * e2 ) { | 
|---|
|  | 444 | PassVisitor<StructuralChecker> checker; | 
|---|
|  | 445 | checker.pass.other = e2; | 
|---|
|  | 446 | e1->accept( checker ); | 
|---|
|  | 447 | return checker.pass.isSimilar; | 
|---|
| [babeeda] | 448 | } | 
|---|
|  | 449 |  | 
|---|
|  | 450 | void SelfAssignChecker::previsit( ApplicationExpr * appExpr ) { | 
|---|
|  | 451 | DeclarationWithType * function = getFunction( appExpr ); | 
|---|
| [a0c7d5cc] | 452 | if ( function->name == "?=?" ) { // doesn't use isAssignment, because ?+=?, etc. should not count as self-assignment | 
|---|
| [babeeda] | 453 | if ( appExpr->args.size() == 2 ) { | 
|---|
|  | 454 | // check for structural similarity (same variable use, ignore casts, etc. - but does not look too deeply, anything looking like a function is off limits) | 
|---|
|  | 455 | if ( structurallySimilar( appExpr->args.front(), appExpr->args.back() ) ) { | 
|---|
|  | 456 | SemanticWarning( appExpr->location, Warning::SelfAssignment, toCString( appExpr->args.front() ) ); | 
|---|
|  | 457 | } | 
|---|
|  | 458 | } | 
|---|
|  | 459 | } | 
|---|
|  | 460 | } | 
|---|
|  | 461 |  | 
|---|
| [134322e] | 462 | Expression * InsertImplicitCalls::postmutate( ApplicationExpr * appExpr ) { | 
|---|
| [c2931ea] | 463 | if ( VariableExpr * function = dynamic_cast< VariableExpr * > ( appExpr->get_function() ) ) { | 
|---|
| [be151bf] | 464 | if ( function->var->linkage.is_builtin ) { | 
|---|
| [c2931ea] | 465 | // optimization: don't need to copy construct in order to call intrinsic functions | 
|---|
|  | 466 | return appExpr; | 
|---|
|  | 467 | } else if ( DeclarationWithType * funcDecl = dynamic_cast< DeclarationWithType * > ( function->get_var() ) ) { | 
|---|
|  | 468 | FunctionType * ftype = dynamic_cast< FunctionType * >( GenPoly::getFunctionType( funcDecl->get_type() ) ); | 
|---|
| [05807e9] | 469 | assertf( ftype, "Function call without function type: %s", toString( funcDecl ).c_str() ); | 
|---|
| [be151bf] | 470 | if ( CodeGen::isConstructor( funcDecl->get_name() ) && ftype->parameters.size() == 2 ) { | 
|---|
|  | 471 | Type * t1 = getPointerBase( ftype->parameters.front()->get_type() ); | 
|---|
|  | 472 | Type * t2 = ftype->parameters.back()->get_type(); | 
|---|
| [0a81c3f] | 473 | assert( t1 ); | 
|---|
| [c2931ea] | 474 |  | 
|---|
| [0a81c3f] | 475 | if ( ResolvExpr::typesCompatible( t1, t2, SymTab::Indexer() ) ) { | 
|---|
| [a28bc02] | 476 | // optimization: don't need to copy construct in order to call a copy constructor | 
|---|
| [c2931ea] | 477 | return appExpr; | 
|---|
| [adcc065] | 478 | } // if | 
|---|
| [bff227f] | 479 | } else if ( CodeGen::isDestructor( funcDecl->get_name() ) ) { | 
|---|
| [c2931ea] | 480 | // correctness: never copy construct arguments to a destructor | 
|---|
| [845cedc] | 481 | return appExpr; | 
|---|
| [adcc065] | 482 | } // if | 
|---|
|  | 483 | } // if | 
|---|
|  | 484 | } // if | 
|---|
| [c2931ea] | 485 | CP_CTOR_PRINT( std::cerr << "InsertImplicitCalls: adding a wrapper " << appExpr << std::endl; ) | 
|---|
|  | 486 |  | 
|---|
|  | 487 | // wrap each function call so that it is easy to identify nodes that have to be copy constructed | 
|---|
|  | 488 | ImplicitCopyCtorExpr * expr = new ImplicitCopyCtorExpr( appExpr ); | 
|---|
| [c0714bf] | 489 | // Move the type substitution to the new top-level, if it is attached to the appExpr. | 
|---|
| [c2931ea] | 490 | // Ensure it is not deleted with the ImplicitCopyCtorExpr by removing it before deletion. | 
|---|
|  | 491 | // The substitution is needed to obtain the type of temporary variables so that copy constructor | 
|---|
| [c0714bf] | 492 | // calls can be resolved. | 
|---|
| [c2931ea] | 493 | assert( env ); | 
|---|
| [c0714bf] | 494 | std::swap( expr->env, appExpr->env ); | 
|---|
| [c2931ea] | 495 | return expr; | 
|---|
| [db4ecc5] | 496 | } | 
|---|
|  | 497 |  | 
|---|
| [29bc63e] | 498 | bool ResolveCopyCtors::skipCopyConstruct( Type * type ) { return ! isConstructable( type ); } | 
|---|
| [c2931ea] | 499 |  | 
|---|
| [65660bd] | 500 | Expression * ResolveCopyCtors::makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg ) { | 
|---|
| [c2931ea] | 501 | assert( var ); | 
|---|
| [092528b] | 502 | // arrays are not copy constructed, so this should always be an ExprStmt | 
|---|
|  | 503 | ImplicitCtorDtorStmt * stmt = genCtorDtor( fname, var, cpArg ); | 
|---|
| [9fe33947] | 504 | assertf( stmt, "ResolveCopyCtors: genCtorDtor returned nullptr: %s / %s / %s", fname.c_str(), toString( var ).c_str(), toString( cpArg ).c_str() ); | 
|---|
|  | 505 | ExprStmt * exprStmt = strict_dynamic_cast< ExprStmt * >( stmt->callStmt ); | 
|---|
| [08da53d] | 506 | Expression * resolved = exprStmt->expr; | 
|---|
|  | 507 | exprStmt->expr = nullptr; // take ownership of expr | 
|---|
| [c2931ea] | 508 |  | 
|---|
|  | 509 | // resolve copy constructor | 
|---|
| [adcc065] | 510 | // should only be one alternative for copy ctor and dtor expressions, since all arguments are fixed | 
|---|
|  | 511 | // (VariableExpr and already resolved expression) | 
|---|
| [08da53d] | 512 | CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << resolved << std::endl; ) | 
|---|
|  | 513 | ResolvExpr::findVoidExpression( resolved, indexer ); | 
|---|
| [65660bd] | 514 | assert( resolved ); | 
|---|
| [dd05e12] | 515 | if ( resolved->env ) { | 
|---|
| [31f379c] | 516 | // Extract useful information and discard new environments. Keeping them causes problems in PolyMutator passes. | 
|---|
| [dd05e12] | 517 | env->add( *resolved->env ); | 
|---|
|  | 518 | delete resolved->env; | 
|---|
|  | 519 | resolved->env = nullptr; | 
|---|
| [adcc065] | 520 | } // if | 
|---|
| [092528b] | 521 | delete stmt; | 
|---|
| [7641b6c] | 522 | if ( TupleAssignExpr * assign = dynamic_cast< TupleAssignExpr * >( resolved ) ) { | 
|---|
|  | 523 | // fix newly generated StmtExpr | 
|---|
| [2f86ddf] | 524 | premutate( assign->stmtExpr ); | 
|---|
| [7641b6c] | 525 | } | 
|---|
| [c2931ea] | 526 | return resolved; | 
|---|
| [540de412] | 527 | } | 
|---|
| [db4ecc5] | 528 |  | 
|---|
| [ae1b9ea] | 529 | void ResolveCopyCtors::copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr, Type * formal ) { | 
|---|
| [c2931ea] | 530 | static UniqueName tempNamer("_tmp_cp"); | 
|---|
| [31f379c] | 531 | assert( env ); | 
|---|
|  | 532 | CP_CTOR_PRINT( std::cerr << "Type Substitution: " << *env << std::endl; ) | 
|---|
| [d29fa5f] | 533 | assert( arg->result ); | 
|---|
| [ae1b9ea] | 534 | Type * result = arg->result; | 
|---|
| [1132b62] | 535 | if ( skipCopyConstruct( result ) ) return; // skip certain non-copyable types | 
|---|
|  | 536 |  | 
|---|
| [f5c3b6c] | 537 | // type may involve type variables, so apply type substitution to get temporary variable's actual type, | 
|---|
|  | 538 | // since result type may not be substituted (e.g., if the type does not appear in the parameter list) | 
|---|
| [ae1b9ea] | 539 | // Use applyFree so that types bound in function pointers are not substituted, e.g. in forall(dtype T) void (*)(T). | 
|---|
|  | 540 | env->applyFree( result ); | 
|---|
| [3aeaecd] | 541 | ObjectDecl * tmp = ObjectDecl::newObject( "__tmp", result, nullptr ); | 
|---|
| [615a096] | 542 | tmp->get_type()->set_const( false ); | 
|---|
| [1132b62] | 543 |  | 
|---|
|  | 544 | // create and resolve copy constructor | 
|---|
|  | 545 | CP_CTOR_PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; ) | 
|---|
| [65660bd] | 546 | Expression * cpCtor = makeCtorDtor( "?{}", tmp, arg ); | 
|---|
|  | 547 |  | 
|---|
|  | 548 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( cpCtor ) ) { | 
|---|
|  | 549 | // if the chosen constructor is intrinsic, the copy is unnecessary, so | 
|---|
|  | 550 | // don't create the temporary and don't call the copy constructor | 
|---|
| [ae1b9ea] | 551 | VariableExpr * function = strict_dynamic_cast< VariableExpr * >( appExpr->function ); | 
|---|
|  | 552 | if ( function->var->linkage == LinkageSpec::Intrinsic ) { | 
|---|
|  | 553 | // arguments that need to be boxed need a temporary regardless of whether the copy constructor is intrinsic, | 
|---|
|  | 554 | // so that the object isn't changed inside of the polymorphic function | 
|---|
|  | 555 | if ( ! GenPoly::needsBoxing( formal, result, impCpCtorExpr->callExpr, env ) ) return; | 
|---|
| [ec42ff2e] | 556 | // xxx - leaking tmp | 
|---|
| [ae1b9ea] | 557 | } | 
|---|
| [65660bd] | 558 | } | 
|---|
|  | 559 |  | 
|---|
| [3aeaecd] | 560 | // set a unique name for the temporary once it's certain the call is necessary | 
|---|
|  | 561 | tmp->name = tempNamer.newName(); | 
|---|
|  | 562 |  | 
|---|
| [65660bd] | 563 | // replace argument to function call with temporary | 
|---|
| [2f86ddf] | 564 | stmtsToAddBefore.push_back( new DeclStmt( tmp ) ); | 
|---|
|  | 565 | arg = cpCtor; | 
|---|
|  | 566 | destructRet( tmp, impCpCtorExpr, arg ); | 
|---|
|  | 567 |  | 
|---|
|  | 568 | // impCpCtorExpr->dtors.push_front( makeCtorDtor( "^?{}", tmp ) ); | 
|---|
| [1132b62] | 569 | } | 
|---|
| [c2931ea] | 570 |  | 
|---|
| [6a9d4b4] | 571 | void ResolveCopyCtors::destructRet( ObjectDecl * ret, ImplicitCopyCtorExpr * /*impCpCtorExpr*/, Expression *& arg ) { | 
|---|
| [2f86ddf] | 572 | // TODO: refactor code for generating cleanup attribute, since it's common and reused in ~3-4 places | 
|---|
|  | 573 | // check for existing cleanup attribute before adding another(?) | 
|---|
|  | 574 | // need to add __Destructor for _tmp_cp variables as well | 
|---|
|  | 575 |  | 
|---|
|  | 576 | assertf( Validate::dtorStruct && Validate::dtorStruct->members.size() == 2, "Destructor generation requires __Destructor definition." ); | 
|---|
|  | 577 | assertf( Validate::dtorStructDestroy, "Destructor generation requires __destroy_Destructor." ); | 
|---|
|  | 578 |  | 
|---|
|  | 579 | // generate a __Destructor for ret that calls the destructor | 
|---|
|  | 580 | Expression * dtor = makeCtorDtor( "^?{}", ret ); | 
|---|
|  | 581 |  | 
|---|
| [9b086ca] | 582 | // if the chosen destructor is intrinsic, elide the generated dtor handler | 
|---|
|  | 583 | if ( arg && isIntrinsicCallExpr( dtor ) ) { | 
|---|
|  | 584 | arg = new CommaExpr( arg, new VariableExpr( ret ) ); | 
|---|
|  | 585 | return; | 
|---|
|  | 586 | } | 
|---|
| [2f86ddf] | 587 |  | 
|---|
|  | 588 | if ( ! dtor->env ) dtor->env = maybeClone( env ); | 
|---|
|  | 589 | DeclarationWithType * dtorFunc = getDtorFunc( ret, new ExprStmt( dtor ), stmtsToAddBefore ); | 
|---|
|  | 590 |  | 
|---|
|  | 591 | StructInstType * dtorStructType = new StructInstType( Type::Qualifiers(), Validate::dtorStruct ); | 
|---|
|  | 592 | dtorStructType->parameters.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); | 
|---|
|  | 593 |  | 
|---|
|  | 594 | // cast destructor pointer to void (*)(void *), to silence GCC incompatible pointer warnings | 
|---|
|  | 595 | FunctionType * dtorFtype = new FunctionType( Type::Qualifiers(), false ); | 
|---|
|  | 596 | dtorFtype->parameters.push_back( ObjectDecl::newObject( "", new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), nullptr ) ); | 
|---|
|  | 597 | Type * dtorType = new PointerType( Type::Qualifiers(), dtorFtype ); | 
|---|
|  | 598 |  | 
|---|
|  | 599 | static UniqueName namer( "_ret_dtor" ); | 
|---|
|  | 600 | ObjectDecl * retDtor = ObjectDecl::newObject( namer.newName(), dtorStructType, new ListInit( { new SingleInit( new ConstantExpr( Constant::null() ) ), new SingleInit( new CastExpr( new VariableExpr( dtorFunc ), dtorType ) ) } ) ); | 
|---|
|  | 601 | retDtor->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( Validate::dtorStructDestroy ) } ) ); | 
|---|
|  | 602 | stmtsToAddBefore.push_back( new DeclStmt( retDtor ) ); | 
|---|
|  | 603 |  | 
|---|
|  | 604 | if ( arg ) { | 
|---|
|  | 605 | Expression * member = new MemberExpr( strict_dynamic_cast<DeclarationWithType *>( Validate::dtorStruct->members.front() ), new VariableExpr( retDtor ) ); | 
|---|
|  | 606 | Expression * object = new CastExpr( new AddressExpr( new VariableExpr( ret ) ), new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ) ); | 
|---|
|  | 607 | Expression * assign = createBitwiseAssignment( member, object ); | 
|---|
|  | 608 | arg = new CommaExpr( new CommaExpr( arg, assign ), new VariableExpr( ret ) ); | 
|---|
|  | 609 | } | 
|---|
|  | 610 |  | 
|---|
|  | 611 | // impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) ); | 
|---|
| [1132b62] | 612 | } | 
|---|
|  | 613 |  | 
|---|
| [2f86ddf] | 614 | Expression * ResolveCopyCtors::postmutate( ImplicitCopyCtorExpr *impCpCtorExpr ) { | 
|---|
| [c2931ea] | 615 | CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; ) | 
|---|
|  | 616 |  | 
|---|
| [ae1b9ea] | 617 | ApplicationExpr * appExpr = impCpCtorExpr->callExpr; | 
|---|
| [2f86ddf] | 618 | ObjectDecl * returnDecl = nullptr; | 
|---|
| [c2931ea] | 619 |  | 
|---|
|  | 620 | // take each argument and attempt to copy construct it. | 
|---|
| [ae1b9ea] | 621 | FunctionType * ftype = GenPoly::getFunctionType( appExpr->function->result ); | 
|---|
|  | 622 | assert( ftype ); | 
|---|
|  | 623 | auto & params = ftype->parameters; | 
|---|
|  | 624 | auto iter = params.begin(); | 
|---|
|  | 625 | for ( Expression * & arg : appExpr->args ) { | 
|---|
|  | 626 | Type * formal = nullptr; | 
|---|
| [2f86ddf] | 627 | if ( iter != params.end() ) { // does not copy construct C-style variadic arguments | 
|---|
| [ae1b9ea] | 628 | DeclarationWithType * param = *iter++; | 
|---|
|  | 629 | formal = param->get_type(); | 
|---|
|  | 630 | } | 
|---|
|  | 631 |  | 
|---|
|  | 632 | copyConstructArg( arg, impCpCtorExpr, formal ); | 
|---|
| [adcc065] | 633 | } // for | 
|---|
| [db4ecc5] | 634 |  | 
|---|
| [adcc065] | 635 | // each return value from the call needs to be connected with an ObjectDecl at the call site, which is | 
|---|
|  | 636 | // initialized with the return value and is destructed later | 
|---|
| [31f379c] | 637 | // xxx - handle named return values? | 
|---|
| [ae1b9ea] | 638 | Type * result = appExpr->result; | 
|---|
| [906e24d] | 639 | if ( ! result->isVoid() ) { | 
|---|
| [1132b62] | 640 | static UniqueName retNamer("_tmp_cp_ret"); | 
|---|
| [c2931ea] | 641 | result = result->clone(); | 
|---|
| [31f379c] | 642 | env->apply( result ); | 
|---|
| [3aeaecd] | 643 | ObjectDecl * ret = ObjectDecl::newObject( retNamer.newName(), result, nullptr ); | 
|---|
| [ae1b9ea] | 644 | ret->type->set_const( false ); | 
|---|
| [2f86ddf] | 645 | returnDecl = ret; | 
|---|
|  | 646 | stmtsToAddBefore.push_back( new DeclStmt( ret ) ); | 
|---|
| [c2931ea] | 647 | CP_CTOR_PRINT( std::cerr << "makeCtorDtor for a return" << std::endl; ) | 
|---|
| [2f86ddf] | 648 | } // for | 
|---|
|  | 649 | CP_CTOR_PRINT( std::cerr << "after Resolving: " << impCpCtorExpr << std::endl; ) | 
|---|
|  | 650 | // ------------------------------------------------------ | 
|---|
|  | 651 |  | 
|---|
|  | 652 | CP_CTOR_PRINT( std::cerr << "Coming out the back..." << impCpCtorExpr << std::endl; ) | 
|---|
|  | 653 |  | 
|---|
|  | 654 | // detach fields from wrapper node so that it can be deleted without deleting too much | 
|---|
|  | 655 | impCpCtorExpr->callExpr = nullptr; | 
|---|
|  | 656 | std::swap( impCpCtorExpr->env, appExpr->env ); | 
|---|
|  | 657 | assert( impCpCtorExpr->env == nullptr ); | 
|---|
|  | 658 | delete impCpCtorExpr; | 
|---|
|  | 659 |  | 
|---|
|  | 660 | if ( returnDecl ) { | 
|---|
|  | 661 | Expression * assign = createBitwiseAssignment( new VariableExpr( returnDecl ), appExpr ); | 
|---|
| [0a81c3f] | 662 | if ( ! dynamic_cast< ReferenceType * >( result ) ) { | 
|---|
| [3aeaecd] | 663 | // destructing reference returns is bad because it can cause multiple destructor calls to the same object - the returned object is not a temporary | 
|---|
| [2f86ddf] | 664 | destructRet( returnDecl, impCpCtorExpr, assign ); | 
|---|
|  | 665 | } else { | 
|---|
|  | 666 | assign = new CommaExpr( assign, new VariableExpr( returnDecl ) ); | 
|---|
| [dc86541] | 667 | } | 
|---|
| [2f86ddf] | 668 | // move env from appExpr to retExpr | 
|---|
|  | 669 | std::swap( assign->env, appExpr->env ); | 
|---|
|  | 670 | return assign; | 
|---|
|  | 671 | } else { | 
|---|
|  | 672 | return appExpr; | 
|---|
|  | 673 | } // if | 
|---|
| [db4ecc5] | 674 | } | 
|---|
|  | 675 |  | 
|---|
| [2f86ddf] | 676 | void ResolveCopyCtors::premutate( StmtExpr * stmtExpr ) { | 
|---|
|  | 677 | // function call temporaries should be placed at statement-level, rather than nested inside of a new statement expression, | 
|---|
|  | 678 | // since temporaries can be shared across sub-expressions, e.g. | 
|---|
| [0e315a5] | 679 | //   [A, A] f();       // decl | 
|---|
|  | 680 | //   g([A] x, [A] y);  // decl | 
|---|
|  | 681 | //   g(f());           // call | 
|---|
| [2f86ddf] | 682 | // f is executed once, so the return temporary is shared across the tuple constructors for x and y. | 
|---|
|  | 683 | // Explicitly mutating children instead of mutating the inner compound statement forces the temporaries to be added | 
|---|
|  | 684 | // to the outer context, rather than inside of the statement expression. | 
|---|
|  | 685 | visit_children = false; | 
|---|
|  | 686 |  | 
|---|
| [c0714bf] | 687 | assert( env ); | 
|---|
| [2f86ddf] | 688 |  | 
|---|
| [0e315a5] | 689 | indexer.enterScope(); | 
|---|
| [2f86ddf] | 690 | // visit all statements | 
|---|
|  | 691 | std::list< Statement * > & stmts = stmtExpr->statements->get_kids(); | 
|---|
|  | 692 | for ( Statement *& stmt : stmts ) { | 
|---|
|  | 693 | stmt = stmt->acceptMutator( *visitor ); | 
|---|
|  | 694 | } // for | 
|---|
| [0e315a5] | 695 | indexer.leaveScope(); | 
|---|
| [2f86ddf] | 696 |  | 
|---|
|  | 697 | assert( stmtExpr->result ); | 
|---|
|  | 698 | Type * result = stmtExpr->result; | 
|---|
| [31f379c] | 699 | if ( ! result->isVoid() ) { | 
|---|
|  | 700 | static UniqueName retNamer("_tmp_stmtexpr_ret"); | 
|---|
|  | 701 |  | 
|---|
|  | 702 | result = result->clone(); | 
|---|
|  | 703 | env->apply( result ); | 
|---|
| [6dfa2e1] | 704 | if ( ! InitTweak::isConstructable( result ) ) { | 
|---|
|  | 705 | delete result; | 
|---|
|  | 706 | return; | 
|---|
|  | 707 | } | 
|---|
|  | 708 |  | 
|---|
|  | 709 | // create variable that will hold the result of the stmt expr | 
|---|
| [3aeaecd] | 710 | ObjectDecl * ret = ObjectDecl::newObject( retNamer.newName(), result, nullptr ); | 
|---|
| [7641b6c] | 711 | ret->type->set_const( false ); | 
|---|
| [2f86ddf] | 712 | stmtsToAddBefore.push_back( new DeclStmt( ret ) ); | 
|---|
| [31f379c] | 713 |  | 
|---|
| [4741dfe] | 714 | assertf( | 
|---|
|  | 715 | stmtExpr->resultExpr, | 
|---|
|  | 716 | "Statement-Expression should have a resulting expression at %s:%d", | 
|---|
|  | 717 | stmtExpr->location.filename.c_str(), | 
|---|
|  | 718 | stmtExpr->location.first_line | 
|---|
|  | 719 | ); | 
|---|
|  | 720 |  | 
|---|
| [0e315a5] | 721 | ExprStmt * last = stmtExpr->resultExpr; | 
|---|
|  | 722 | try { | 
|---|
|  | 723 | last->expr = makeCtorDtor( "?{}", ret, last->expr ); | 
|---|
|  | 724 | } catch(...) { | 
|---|
| [4741dfe] | 725 | std::cerr << "*CFA internal error: "; | 
|---|
|  | 726 | std::cerr << "can't resolve implicit constructor"; | 
|---|
|  | 727 | std::cerr << " at " << stmtExpr->location.filename; | 
|---|
|  | 728 | std::cerr << ":" << stmtExpr->location.first_line << std::endl; | 
|---|
| [0e315a5] | 729 |  | 
|---|
|  | 730 | abort(); | 
|---|
|  | 731 | } | 
|---|
| [31f379c] | 732 |  | 
|---|
| [2f86ddf] | 733 | // add destructors after current statement | 
|---|
|  | 734 | stmtsToAddAfter.push_back( new ExprStmt( makeCtorDtor( "^?{}", ret ) ) ); | 
|---|
|  | 735 |  | 
|---|
|  | 736 | // must have a non-empty body, otherwise it wouldn't have a result | 
|---|
|  | 737 | assert( ! stmts.empty() ); | 
|---|
|  | 738 |  | 
|---|
|  | 739 | // if there is a return decl, add a use as the last statement; will not have return decl on non-constructable returns | 
|---|
|  | 740 | stmts.push_back( new ExprStmt( new VariableExpr( ret ) ) ); | 
|---|
| [31f379c] | 741 | } // if | 
|---|
|  | 742 |  | 
|---|
| [2f86ddf] | 743 | assert( stmtExpr->returnDecls.empty() ); | 
|---|
|  | 744 | assert( stmtExpr->dtors.empty() ); | 
|---|
| [c0714bf] | 745 | } | 
|---|
|  | 746 |  | 
|---|
| [60a8062] | 747 | // to prevent warnings ('_unq0' may be used uninitialized in this function), | 
|---|
| [ddae809] | 748 | // insert an appropriate zero initializer for UniqueExpr temporaries. | 
|---|
|  | 749 | Initializer * makeInit( Type * t ) { | 
|---|
|  | 750 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( t ) ) { | 
|---|
|  | 751 | // initizer for empty struct must be empty | 
|---|
|  | 752 | if ( inst->baseStruct->members.empty() ) return new ListInit({}); | 
|---|
|  | 753 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) { | 
|---|
|  | 754 | // initizer for empty union must be empty | 
|---|
|  | 755 | if ( inst->baseUnion->members.empty() ) return new ListInit({}); | 
|---|
|  | 756 | } | 
|---|
|  | 757 |  | 
|---|
|  | 758 | return new ListInit( { new SingleInit( new ConstantExpr( Constant::from_int( 0 ) ) ) } ); | 
|---|
|  | 759 | } | 
|---|
|  | 760 |  | 
|---|
| [2f86ddf] | 761 | void ResolveCopyCtors::premutate( UniqueExpr * unqExpr ) { | 
|---|
| [dc2334c] | 762 | visit_children = false; | 
|---|
| [2f86ddf] | 763 | // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated | 
|---|
|  | 764 | static std::unordered_map< int, UniqueExpr * > unqMap; | 
|---|
|  | 765 | if ( ! unqMap.count( unqExpr->get_id() ) ) { | 
|---|
|  | 766 | // resolve expr and find its | 
|---|
|  | 767 |  | 
|---|
|  | 768 | ImplicitCopyCtorExpr * impCpCtorExpr = dynamic_cast< ImplicitCopyCtorExpr * >( unqExpr->expr ); | 
|---|
|  | 769 | // PassVisitor<ResolveCopyCtors> fixer; | 
|---|
|  | 770 | unqExpr->expr = unqExpr->expr->acceptMutator( *visitor ); | 
|---|
|  | 771 |  | 
|---|
|  | 772 | // it should never be necessary to wrap a void-returning expression in a UniqueExpr - if this assumption changes, this needs to be rethought | 
|---|
|  | 773 | assert( unqExpr->result ); | 
|---|
|  | 774 | if ( impCpCtorExpr ) { | 
|---|
|  | 775 | CommaExpr * comma = strict_dynamic_cast< CommaExpr * >( unqExpr->expr ); | 
|---|
|  | 776 | VariableExpr * var = strict_dynamic_cast<VariableExpr *>( comma->arg2 ); | 
|---|
|  | 777 | // note the variable used as the result from the call | 
|---|
|  | 778 | unqExpr->var = var->clone(); | 
|---|
|  | 779 | } else { | 
|---|
|  | 780 | // expr isn't a call expr, so create a new temporary variable to use to hold the value of the unique expression | 
|---|
|  | 781 | unqExpr->object = ObjectDecl::newObject( toString("_unq", unqExpr->get_id()), unqExpr->result->clone(), makeInit( unqExpr->result ) ); | 
|---|
|  | 782 | unqExpr->var = new VariableExpr( unqExpr->object ); | 
|---|
| [6dfa2e1] | 783 | } | 
|---|
| [31f379c] | 784 |  | 
|---|
| [2f86ddf] | 785 | // stmtsToAddBefore.splice( stmtsToAddBefore.end(), fixer.pass.stmtsToAddBefore ); | 
|---|
|  | 786 | // stmtsToAddAfter.splice( stmtsToAddAfter.end(), fixer.pass.stmtsToAddAfter ); | 
|---|
|  | 787 | unqMap[unqExpr->get_id()] = unqExpr; | 
|---|
|  | 788 | } else { | 
|---|
| [141b786] | 789 | // take data from other UniqueExpr to ensure consistency | 
|---|
|  | 790 | delete unqExpr->get_expr(); | 
|---|
| [2f86ddf] | 791 | unqExpr->expr = unqMap[unqExpr->get_id()]->expr->clone(); | 
|---|
|  | 792 | delete unqExpr->result; | 
|---|
|  | 793 | unqExpr->result = maybeClone( unqExpr->expr->result ); | 
|---|
| [141b786] | 794 | } | 
|---|
|  | 795 | } | 
|---|
|  | 796 |  | 
|---|
| [1bc749f] | 797 | DeclarationWithType * FixInit::postmutate( ObjectDecl *objDecl ) { | 
|---|
| [aff3af4] | 798 | // since this removes the init field from objDecl, it must occur after children are mutated (i.e. postmutate) | 
|---|
| [c2931ea] | 799 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { | 
|---|
|  | 800 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL | 
|---|
|  | 801 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); | 
|---|
|  | 802 | if ( Statement * ctor = ctorInit->get_ctor() ) { | 
|---|
| [08d5507b] | 803 | if ( objDecl->get_storageClasses().is_static ) { | 
|---|
| [f1791a4] | 804 |  | 
|---|
|  | 805 | // The ojbect needs to go in the data section, regardless of dtor complexity below. | 
|---|
|  | 806 | // The attribute works, and is meant to apply, both for leaving the static local alone, | 
|---|
|  | 807 | // and for hoisting it out as a static global. | 
|---|
| [720f2fe2] | 808 | addDataSectionAttribute( objDecl ); | 
|---|
| [f1791a4] | 809 |  | 
|---|
| [72e9222] | 810 | // originally wanted to take advantage of gcc nested functions, but | 
|---|
| [f9cebb5] | 811 | // we get memory errors with this approach. To remedy this, the static | 
|---|
|  | 812 | // variable is hoisted when the destructor needs to be called. | 
|---|
| [72e9222] | 813 | // | 
|---|
| [c2931ea] | 814 | // generate: | 
|---|
| [f9cebb5] | 815 | // static T __objName_static_varN; | 
|---|
| [72e9222] | 816 | // void __objName_dtor_atexitN() { | 
|---|
| [f9cebb5] | 817 | //   __dtor__...; | 
|---|
| [72e9222] | 818 | // } | 
|---|
|  | 819 | // int f(...) { | 
|---|
|  | 820 | //   ... | 
|---|
|  | 821 | //   static bool __objName_uninitialized = true; | 
|---|
|  | 822 | //   if (__objName_uninitialized) { | 
|---|
|  | 823 | //     __ctor(__objName); | 
|---|
|  | 824 | //     __objName_uninitialized = false; | 
|---|
| [f9cebb5] | 825 | //     atexit(__objName_dtor_atexitN); | 
|---|
| [c2931ea] | 826 | //   } | 
|---|
| [72e9222] | 827 | //   ... | 
|---|
| [c2931ea] | 828 | // } | 
|---|
|  | 829 |  | 
|---|
| [72e9222] | 830 | static UniqueName dtorCallerNamer( "_dtor_atexit" ); | 
|---|
|  | 831 |  | 
|---|
|  | 832 | // static bool __objName_uninitialized = true | 
|---|
| [c2931ea] | 833 | BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool ); | 
|---|
| [579263a] | 834 | SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant::from_int( 1 ) ) ); | 
|---|
| [68fe077a] | 835 | ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", Type::StorageClasses( Type::Static ), LinkageSpec::Cforall, 0, boolType, boolInitExpr ); | 
|---|
| [c2931ea] | 836 | isUninitializedVar->fixUniqueId(); | 
|---|
|  | 837 |  | 
|---|
|  | 838 | // __objName_uninitialized = false; | 
|---|
|  | 839 | UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) ); | 
|---|
|  | 840 | setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) ); | 
|---|
| [d56e5bc] | 841 | setTrue->get_args().push_back( new ConstantExpr( Constant::from_int( 0 ) ) ); | 
|---|
| [c2931ea] | 842 |  | 
|---|
|  | 843 | // generate body of if | 
|---|
| [ba3706f] | 844 | CompoundStmt * initStmts = new CompoundStmt(); | 
|---|
| [c2931ea] | 845 | std::list< Statement * > & body = initStmts->get_kids(); | 
|---|
|  | 846 | body.push_back( ctor ); | 
|---|
| [ba3706f] | 847 | body.push_back( new ExprStmt( setTrue ) ); | 
|---|
| [c2931ea] | 848 |  | 
|---|
|  | 849 | // put it all together | 
|---|
| [ba3706f] | 850 | IfStmt * ifStmt = new IfStmt( new VariableExpr( isUninitializedVar ), initStmts, 0 ); | 
|---|
|  | 851 | stmtsToAddAfter.push_back( new DeclStmt( isUninitializedVar ) ); | 
|---|
| [c2931ea] | 852 | stmtsToAddAfter.push_back( ifStmt ); | 
|---|
| [72e9222] | 853 |  | 
|---|
| [a4dd728] | 854 | Statement * dtor = ctorInit->get_dtor(); | 
|---|
| [62423350] | 855 | objDecl->set_init( nullptr ); | 
|---|
|  | 856 | ctorInit->set_ctor( nullptr ); | 
|---|
| [a4dd728] | 857 | ctorInit->set_dtor( nullptr ); | 
|---|
|  | 858 | if ( dtor ) { | 
|---|
| [f9cebb5] | 859 | // if the object has a non-trivial destructor, have to | 
|---|
|  | 860 | // hoist it and the object into the global space and | 
|---|
|  | 861 | // call the destructor function with atexit. | 
|---|
|  | 862 |  | 
|---|
| [a4dd728] | 863 | Statement * dtorStmt = dtor->clone(); | 
|---|
| [f9cebb5] | 864 |  | 
|---|
|  | 865 | // void __objName_dtor_atexitN(...) {...} | 
|---|
| [ba3706f] | 866 | FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + dtorCallerNamer.newName(), Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt() ); | 
|---|
| [f9cebb5] | 867 | dtorCaller->fixUniqueId(); | 
|---|
| [c8dfcd3] | 868 | dtorCaller->get_statements()->push_back( dtorStmt ); | 
|---|
| [f9cebb5] | 869 |  | 
|---|
|  | 870 | // atexit(dtor_atexit); | 
|---|
|  | 871 | UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) ); | 
|---|
|  | 872 | callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) ); | 
|---|
|  | 873 |  | 
|---|
| [ba3706f] | 874 | body.push_back( new ExprStmt( callAtexit ) ); | 
|---|
| [f9cebb5] | 875 |  | 
|---|
|  | 876 | // hoist variable and dtor caller decls to list of decls that will be added into global scope | 
|---|
|  | 877 | staticDtorDecls.push_back( objDecl ); | 
|---|
|  | 878 | staticDtorDecls.push_back( dtorCaller ); | 
|---|
|  | 879 |  | 
|---|
|  | 880 | // need to rename object uniquely since it now appears | 
|---|
|  | 881 | // at global scope and there could be multiple function-scoped | 
|---|
|  | 882 | // static variables with the same name in different functions. | 
|---|
| [c8dfcd3] | 883 | // Note: it isn't sufficient to modify only the mangleName, because | 
|---|
|  | 884 | // then subsequent Indexer passes can choke on seeing the object's name | 
|---|
|  | 885 | // if another object has the same name and type. An unfortunate side-effect | 
|---|
|  | 886 | // of renaming the object is that subsequent NameExprs may fail to resolve, | 
|---|
|  | 887 | // but there shouldn't be any remaining past this point. | 
|---|
| [f9cebb5] | 888 | static UniqueName staticNamer( "_static_var" ); | 
|---|
| [c8dfcd3] | 889 | objDecl->set_name( objDecl->get_name() + staticNamer.newName() ); | 
|---|
|  | 890 | objDecl->set_mangleName( SymTab::Mangler::mangle( objDecl ) ); | 
|---|
| [f9cebb5] | 891 |  | 
|---|
|  | 892 | // xxx - temporary hack: need to return a declaration, but want to hoist the current object out of this scope | 
|---|
|  | 893 | // create a new object which is never used | 
|---|
|  | 894 | static UniqueName dummyNamer( "_dummy" ); | 
|---|
| [68fe077a] | 895 | ObjectDecl * dummy = new ObjectDecl( dummyNamer.newName(), Type::StorageClasses( Type::Static ), LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), 0, std::list< Attribute * >{ new Attribute("unused") } ); | 
|---|
| [a4dd728] | 896 | delete ctorInit; | 
|---|
| [f9cebb5] | 897 | return dummy; | 
|---|
|  | 898 | } | 
|---|
| [c2931ea] | 899 | } else { | 
|---|
| [e3e16bc] | 900 | ImplicitCtorDtorStmt * implicit = strict_dynamic_cast< ImplicitCtorDtorStmt * > ( ctor ); | 
|---|
| [b0f601fa] | 901 | ExprStmt * ctorStmt = dynamic_cast< ExprStmt * >( implicit->callStmt ); | 
|---|
| [233e4d9] | 902 | ApplicationExpr * ctorCall = nullptr; | 
|---|
| [b0f601fa] | 903 | if ( ctorStmt && (ctorCall = isIntrinsicCallExpr( ctorStmt->expr )) && ctorCall->get_args().size() == 2 ) { | 
|---|
| [233e4d9] | 904 | // clean up intrinsic copy constructor calls by making them into SingleInits | 
|---|
| [e41306d] | 905 | Expression * ctorArg = ctorCall->args.back(); | 
|---|
|  | 906 | std::swap( ctorArg->env, ctorCall->env ); | 
|---|
|  | 907 | objDecl->init = new SingleInit( ctorArg ); | 
|---|
|  | 908 |  | 
|---|
| [b0f601fa] | 909 | ctorCall->args.pop_back(); | 
|---|
| [233e4d9] | 910 | } else { | 
|---|
|  | 911 | stmtsToAddAfter.push_back( ctor ); | 
|---|
| [b0f601fa] | 912 | objDecl->init = nullptr; | 
|---|
|  | 913 | ctorInit->ctor = nullptr; | 
|---|
| [233e4d9] | 914 | } | 
|---|
| [1bc749f] | 915 |  | 
|---|
|  | 916 | Statement * dtor = ctorInit->dtor; | 
|---|
|  | 917 | if ( dtor ) { | 
|---|
|  | 918 | ImplicitCtorDtorStmt * implicit = strict_dynamic_cast< ImplicitCtorDtorStmt * >( dtor ); | 
|---|
|  | 919 | Statement * dtorStmt = implicit->callStmt; | 
|---|
| [20eacb7] | 920 |  | 
|---|
| [1bc749f] | 921 | // don't need to call intrinsic dtor, because it does nothing, but | 
|---|
|  | 922 | // non-intrinsic dtors must be called | 
|---|
|  | 923 | if ( ! isIntrinsicSingleArgCallStmt( dtorStmt ) ) { | 
|---|
|  | 924 | // set dtor location to the object's location for error messages | 
|---|
| [88e79ad] | 925 | DeclarationWithType * dtorFunc = getDtorFunc( objDecl, dtorStmt, stmtsToAddBefore ); | 
|---|
| [1bc749f] | 926 | objDecl->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( dtorFunc ) } ) ); | 
|---|
|  | 927 | ctorInit->dtor = nullptr; | 
|---|
|  | 928 | } // if | 
|---|
|  | 929 | } | 
|---|
| [adcc065] | 930 | } // if | 
|---|
| [b0f601fa] | 931 | } else if ( Initializer * init = ctorInit->init ) { | 
|---|
|  | 932 | objDecl->init = init; | 
|---|
|  | 933 | ctorInit->init = nullptr; | 
|---|
| [c2931ea] | 934 | } else { | 
|---|
|  | 935 | // no constructor and no initializer, which is okay | 
|---|
| [b0f601fa] | 936 | objDecl->init = nullptr; | 
|---|
| [adcc065] | 937 | } // if | 
|---|
| [c2931ea] | 938 | delete ctorInit; | 
|---|
| [adcc065] | 939 | } // if | 
|---|
| [c2931ea] | 940 | return objDecl; | 
|---|
| [db4ecc5] | 941 | } | 
|---|
|  | 942 |  | 
|---|
| [698ec72] | 943 | void ObjDeclCollector::previsit( CompoundStmt * ) { | 
|---|
|  | 944 | GuardValue( curVars ); | 
|---|
| [db4ecc5] | 945 | } | 
|---|
|  | 946 |  | 
|---|
| [698ec72] | 947 | void ObjDeclCollector::previsit( DeclStmt * stmt ) { | 
|---|
| [4b2589a] | 948 | // keep track of all variables currently in scope | 
|---|
| [c2931ea] | 949 | if ( ObjectDecl * objDecl = dynamic_cast< ObjectDecl * > ( stmt->get_decl() ) ) { | 
|---|
| [62423350] | 950 | curVars.push_back( objDecl ); | 
|---|
| [adcc065] | 951 | } // if | 
|---|
| [db4ecc5] | 952 | } | 
|---|
|  | 953 |  | 
|---|
| [698ec72] | 954 | void LabelFinder::previsit( Statement * stmt ) { | 
|---|
| [4b2589a] | 955 | // for each label, remember the variables in scope at that label. | 
|---|
| [c2931ea] | 956 | for ( Label l : stmt->get_labels() ) { | 
|---|
|  | 957 | vars[l] = curVars; | 
|---|
| [adcc065] | 958 | } // for | 
|---|
| [71f4e4f] | 959 | } | 
|---|
| [f1e012b] | 960 |  | 
|---|
| [698ec72] | 961 | void LabelFinder::previsit( CompoundStmt * stmt ) { | 
|---|
|  | 962 | previsit( (Statement *)stmt ); | 
|---|
|  | 963 | Parent::previsit( stmt ); | 
|---|
|  | 964 | } | 
|---|
|  | 965 |  | 
|---|
|  | 966 | void LabelFinder::previsit( DeclStmt * stmt ) { | 
|---|
|  | 967 | previsit( (Statement *)stmt ); | 
|---|
|  | 968 | Parent::previsit( stmt ); | 
|---|
|  | 969 | } | 
|---|
|  | 970 |  | 
|---|
|  | 971 |  | 
|---|
|  | 972 | void InsertDtors::previsit( FunctionDecl * funcDecl ) { | 
|---|
| [52c14b3] | 973 | // each function needs to have its own set of labels | 
|---|
| [698ec72] | 974 | GuardValue( labelVars ); | 
|---|
| [52c14b3] | 975 | labelVars.clear(); | 
|---|
| [680620d] | 976 | // LabelFinder does not recurse into FunctionDecl, so need to visit | 
|---|
|  | 977 | // its children manually. | 
|---|
| [698ec72] | 978 | maybeAccept( funcDecl->type, finder ); | 
|---|
|  | 979 | maybeAccept( funcDecl->statements, finder ); | 
|---|
| [52c14b3] | 980 |  | 
|---|
| [698ec72] | 981 | // all labels for this function have been collected, insert destructors as appropriate via implicit recursion. | 
|---|
| [52c14b3] | 982 | } | 
|---|
|  | 983 |  | 
|---|
| [adcc065] | 984 | // Handle break/continue/goto in the same manner as C++.  Basic idea: any objects that are in scope at the | 
|---|
|  | 985 | // BranchStmt but not at the labelled (target) statement must be destructed.  If there are any objects in scope | 
|---|
|  | 986 | // at the target location but not at the BranchStmt then those objects would be uninitialized so notify the user | 
|---|
|  | 987 | // of the error.  See C++ Reference 6.6 Jump Statements for details. | 
|---|
| [c2931ea] | 988 | void InsertDtors::handleGoto( BranchStmt * stmt ) { | 
|---|
| [5809461] | 989 | // can't do anything for computed goto | 
|---|
|  | 990 | if ( stmt->computedTarget ) return; | 
|---|
|  | 991 |  | 
|---|
|  | 992 | assertf( stmt->get_target() != "", "BranchStmt missing a label: %s", toString( stmt ).c_str() ); | 
|---|
| [c2931ea] | 993 | // S_L = lvars = set of objects in scope at label definition | 
|---|
|  | 994 | // S_G = curVars = set of objects in scope at goto statement | 
|---|
|  | 995 | ObjectSet & lvars = labelVars[ stmt->get_target() ]; | 
|---|
|  | 996 |  | 
|---|
|  | 997 | DTOR_PRINT( | 
|---|
|  | 998 | std::cerr << "at goto label: " << stmt->get_target().get_name() << std::endl; | 
|---|
|  | 999 | std::cerr << "S_G = " << printSet( curVars ) << std::endl; | 
|---|
|  | 1000 | std::cerr << "S_L = " << printSet( lvars ) << std::endl; | 
|---|
|  | 1001 | ) | 
|---|
|  | 1002 |  | 
|---|
| [aaeacf4] | 1003 |  | 
|---|
|  | 1004 | // std::set_difference requires that the inputs be sorted. | 
|---|
|  | 1005 | lvars.sort(); | 
|---|
|  | 1006 | curVars.sort(); | 
|---|
|  | 1007 |  | 
|---|
| [c2931ea] | 1008 | ObjectSet diff; | 
|---|
|  | 1009 | // S_L-S_G results in set of objects whose construction is skipped - it's an error if this set is non-empty | 
|---|
|  | 1010 | std::set_difference( lvars.begin(), lvars.end(), curVars.begin(), curVars.end(), std::inserter( diff, diff.begin() ) ); | 
|---|
|  | 1011 | DTOR_PRINT( | 
|---|
|  | 1012 | std::cerr << "S_L-S_G = " << printSet( diff ) << std::endl; | 
|---|
|  | 1013 | ) | 
|---|
|  | 1014 | if ( ! diff.empty() ) { | 
|---|
| [a16764a6] | 1015 | SemanticError( stmt, std::string("jump to label '") + stmt->get_target().get_name() + "' crosses initialization of " + (*diff.begin())->get_name() + " " ); | 
|---|
| [adcc065] | 1016 | } // if | 
|---|
| [c2931ea] | 1017 | } | 
|---|
| [39786813] | 1018 |  | 
|---|
| [698ec72] | 1019 | void InsertDtors::previsit( BranchStmt * stmt ) { | 
|---|
| [c2931ea] | 1020 | switch( stmt->get_type() ) { | 
|---|
| [adcc065] | 1021 | case BranchStmt::Continue: | 
|---|
|  | 1022 | case BranchStmt::Break: | 
|---|
|  | 1023 | // could optimize the break/continue case, because the S_L-S_G check is unnecessary (this set should | 
|---|
|  | 1024 | // always be empty), but it serves as a small sanity check. | 
|---|
|  | 1025 | case BranchStmt::Goto: | 
|---|
|  | 1026 | handleGoto( stmt ); | 
|---|
|  | 1027 | break; | 
|---|
|  | 1028 | default: | 
|---|
|  | 1029 | assert( false ); | 
|---|
|  | 1030 | } // switch | 
|---|
| [c2931ea] | 1031 | } | 
|---|
| [79970ed] | 1032 |  | 
|---|
|  | 1033 | bool checkWarnings( FunctionDecl * funcDecl ) { | 
|---|
|  | 1034 | // only check for warnings if the current function is a user-defined | 
|---|
|  | 1035 | // constructor or destructor | 
|---|
|  | 1036 | if ( ! funcDecl ) return false; | 
|---|
|  | 1037 | if ( ! funcDecl->get_statements() ) return false; | 
|---|
| [bff227f] | 1038 | return CodeGen::isCtorDtor( funcDecl->get_name() ) && ! LinkageSpec::isOverridable( funcDecl->get_linkage() ); | 
|---|
| [79970ed] | 1039 | } | 
|---|
|  | 1040 |  | 
|---|
| [b3fc977] | 1041 | void GenStructMemberCalls::premutate( FunctionDecl * funcDecl ) { | 
|---|
| [b0f601fa] | 1042 | GuardValue( function ); | 
|---|
| [9a707e4e] | 1043 | GuardValue( unhandled ); | 
|---|
|  | 1044 | GuardValue( usedUninit ); | 
|---|
|  | 1045 | GuardValue( thisParam ); | 
|---|
|  | 1046 | GuardValue( isCtor ); | 
|---|
|  | 1047 | GuardValue( structDecl ); | 
|---|
| [a16764a6] | 1048 | errors = SemanticErrorException();  // clear previous errors | 
|---|
| [c8dfcd3] | 1049 |  | 
|---|
|  | 1050 | // need to start with fresh sets | 
|---|
|  | 1051 | unhandled.clear(); | 
|---|
|  | 1052 | usedUninit.clear(); | 
|---|
| [79970ed] | 1053 |  | 
|---|
|  | 1054 | function = funcDecl; | 
|---|
| [bff227f] | 1055 | isCtor = CodeGen::isConstructor( function->get_name() ); | 
|---|
| [c8dfcd3] | 1056 | if ( checkWarnings( function ) ) { | 
|---|
|  | 1057 | FunctionType * type = function->get_functionType(); | 
|---|
| [79970ed] | 1058 | assert( ! type->get_parameters().empty() ); | 
|---|
| [e3e16bc] | 1059 | thisParam = strict_dynamic_cast< ObjectDecl * >( type->get_parameters().front() ); | 
|---|
| [0a81c3f] | 1060 | Type * thisType = getPointerBase( thisParam->get_type() ); | 
|---|
|  | 1061 | StructInstType * structType = dynamic_cast< StructInstType * >( thisType ); | 
|---|
| [79970ed] | 1062 | if ( structType ) { | 
|---|
| [44f6341] | 1063 | structDecl = structType->get_baseStruct(); | 
|---|
| [79970ed] | 1064 | for ( Declaration * member : structDecl->get_members() ) { | 
|---|
|  | 1065 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) { | 
|---|
|  | 1066 | // record all of the struct type's members that need to be constructed or | 
|---|
|  | 1067 | // destructed by the end of the function | 
|---|
|  | 1068 | unhandled.insert( field ); | 
|---|
|  | 1069 | } | 
|---|
|  | 1070 | } | 
|---|
|  | 1071 | } | 
|---|
|  | 1072 | } | 
|---|
| [9a707e4e] | 1073 | } | 
|---|
|  | 1074 |  | 
|---|
| [b3fc977] | 1075 | DeclarationWithType * GenStructMemberCalls::postmutate( FunctionDecl * funcDecl ) { | 
|---|
| [c8dfcd3] | 1076 | // remove the unhandled objects from usedUninit, because a call is inserted | 
|---|
|  | 1077 | // to handle them - only objects that are later constructed are used uninitialized. | 
|---|
| [64ac636] | 1078 | std::map< DeclarationWithType *, CodeLocation > diff; | 
|---|
|  | 1079 | // need the comparator since usedUninit and unhandled have different types | 
|---|
|  | 1080 | struct comp_t { | 
|---|
|  | 1081 | typedef decltype(usedUninit)::value_type usedUninit_t; | 
|---|
|  | 1082 | typedef decltype(unhandled)::value_type unhandled_t; | 
|---|
|  | 1083 | bool operator()(usedUninit_t x, unhandled_t y) { return x.first < y; } | 
|---|
|  | 1084 | bool operator()(unhandled_t x, usedUninit_t y) { return x < y.first; } | 
|---|
|  | 1085 | } comp; | 
|---|
|  | 1086 | std::set_difference( usedUninit.begin(), usedUninit.end(), unhandled.begin(), unhandled.end(), std::inserter( diff, diff.begin() ), comp ); | 
|---|
|  | 1087 | for ( auto p : diff ) { | 
|---|
|  | 1088 | DeclarationWithType * member = p.first; | 
|---|
|  | 1089 | CodeLocation loc = p.second; | 
|---|
|  | 1090 | // xxx - make error message better by also tracking the location that the object is constructed at? | 
|---|
|  | 1091 | emit( loc, "in ", CodeGen::genPrettyType( function->get_functionType(), function->get_name() ), ", field ", member->get_name(), " used before being constructed" ); | 
|---|
| [79970ed] | 1092 | } | 
|---|
|  | 1093 |  | 
|---|
| [c8dfcd3] | 1094 | if ( ! unhandled.empty() ) { | 
|---|
| [4618319] | 1095 | // need to explicitly re-add function parameters to the indexer in order to resolve copy constructors | 
|---|
| [9a707e4e] | 1096 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this]() { indexer.leaveScope(); } ); | 
|---|
| [5fe35d6] | 1097 | indexer.addFunctionType( function->type ); | 
|---|
| [44f6341] | 1098 |  | 
|---|
|  | 1099 | // need to iterate through members in reverse in order for | 
|---|
|  | 1100 | // ctor/dtor statements to come out in the right order | 
|---|
| [1ba88a0] | 1101 | for ( Declaration * member : reverseIterate( structDecl->get_members() ) ) { | 
|---|
| [44f6341] | 1102 | DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ); | 
|---|
|  | 1103 | // skip non-DWT members | 
|---|
|  | 1104 | if ( ! field ) continue; | 
|---|
| [05807e9] | 1105 | // skip non-constructable members | 
|---|
|  | 1106 | if ( ! tryConstruct( field ) ) continue; | 
|---|
| [44f6341] | 1107 | // skip handled members | 
|---|
|  | 1108 | if ( ! unhandled.count( field ) ) continue; | 
|---|
|  | 1109 |  | 
|---|
|  | 1110 | // insert and resolve default/copy constructor call for each field that's unhandled | 
|---|
| [c8dfcd3] | 1111 | std::list< Statement * > stmt; | 
|---|
| [5fe35d6] | 1112 | Expression * arg2 = nullptr; | 
|---|
| [4d4882a] | 1113 | if ( isCopyConstructor( function ) ) { | 
|---|
| [44f6341] | 1114 | // if copy ctor, need to pass second-param-of-this-function.field | 
|---|
| [4d4882a] | 1115 | std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); | 
|---|
|  | 1116 | assert( params.size() == 2 ); | 
|---|
| [44f6341] | 1117 | arg2 = new MemberExpr( field, new VariableExpr( params.back() ) ); | 
|---|
| [4d4882a] | 1118 | } | 
|---|
| [b8524ca] | 1119 | InitExpander_old srcParam( arg2 ); | 
|---|
| [4618319] | 1120 | // cast away reference type and construct field. | 
|---|
|  | 1121 | Expression * thisExpr = new CastExpr( new VariableExpr( thisParam ), thisParam->get_type()->stripReferences()->clone() ); | 
|---|
|  | 1122 | Expression * memberDest = new MemberExpr( field, thisExpr ); | 
|---|
|  | 1123 | SymTab::genImplicitCall( srcParam, memberDest, function->get_name(), back_inserter( stmt ), field, isCtor ); | 
|---|
| [c8dfcd3] | 1124 |  | 
|---|
|  | 1125 | assert( stmt.size() <= 1 ); | 
|---|
|  | 1126 | if ( stmt.size() == 1 ) { | 
|---|
|  | 1127 | Statement * callStmt = stmt.front(); | 
|---|
|  | 1128 |  | 
|---|
|  | 1129 | try { | 
|---|
| [b3fc977] | 1130 | callStmt->acceptMutator( *visitor ); | 
|---|
| [c8dfcd3] | 1131 | if ( isCtor ) { | 
|---|
| [88e79ad] | 1132 | function->statements->push_front( callStmt ); | 
|---|
| [b54ad9c] | 1133 | } else { // TODO: don't generate destructor function/object for intrinsic calls | 
|---|
| [c8dfcd3] | 1134 | // destructor statements should be added at the end | 
|---|
| [88e79ad] | 1135 | // function->get_statements()->push_back( callStmt ); | 
|---|
|  | 1136 |  | 
|---|
| [2bfc6b2] | 1137 | // Optimization: do not need to call intrinsic destructors on members | 
|---|
|  | 1138 | if ( isIntrinsicSingleArgCallStmt( callStmt ) ) continue;; | 
|---|
|  | 1139 |  | 
|---|
| [b54ad9c] | 1140 | // __Destructor _dtor0 = { (void *)&b.a1, (void (*)(void *)_destroy_A }; | 
|---|
| [88e79ad] | 1141 | std::list< Statement * > stmtsToAdd; | 
|---|
|  | 1142 |  | 
|---|
|  | 1143 | static UniqueName memberDtorNamer = { "__memberDtor" }; | 
|---|
| [2bfc6b2] | 1144 | assertf( Validate::dtorStruct, "builtin __Destructor not found." ); | 
|---|
|  | 1145 | assertf( Validate::dtorStructDestroy, "builtin __destroy_Destructor not found." ); | 
|---|
| [88e79ad] | 1146 |  | 
|---|
| [b54ad9c] | 1147 | Expression * thisExpr = new CastExpr( new AddressExpr( new VariableExpr( thisParam ) ), new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ) ); | 
|---|
| [88e79ad] | 1148 | Expression * dtorExpr = new VariableExpr( getDtorFunc( thisParam, callStmt, stmtsToAdd ) ); | 
|---|
|  | 1149 |  | 
|---|
| [10dc6908] | 1150 | // cast destructor pointer to void (*)(void *), to silence GCC incompatible pointer warnings | 
|---|
|  | 1151 | FunctionType * dtorFtype = new FunctionType( Type::Qualifiers(), false ); | 
|---|
|  | 1152 | dtorFtype->parameters.push_back( ObjectDecl::newObject( "", new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), nullptr ) ); | 
|---|
|  | 1153 | Type * dtorType = new PointerType( Type::Qualifiers(), dtorFtype ); | 
|---|
|  | 1154 |  | 
|---|
| [2bfc6b2] | 1155 | ObjectDecl * destructor = ObjectDecl::newObject( memberDtorNamer.newName(), new StructInstType( Type::Qualifiers(), Validate::dtorStruct ), new ListInit( { new SingleInit( thisExpr ), new SingleInit( new CastExpr( dtorExpr, dtorType ) ) } ) ); | 
|---|
| [f9feab8] | 1156 | function->statements->push_front( new DeclStmt( destructor ) ); | 
|---|
| [2bfc6b2] | 1157 | destructor->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( Validate::dtorStructDestroy ) } ) ); | 
|---|
| [88e79ad] | 1158 |  | 
|---|
|  | 1159 | function->statements->kids.splice( function->statements->kids.begin(), stmtsToAdd ); | 
|---|
| [c8dfcd3] | 1160 | } | 
|---|
| [a16764a6] | 1161 | } catch ( SemanticErrorException & error ) { | 
|---|
| [64ac636] | 1162 | emit( funcDecl->location, "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] | 1163 | } | 
|---|
|  | 1164 | } | 
|---|
|  | 1165 | } | 
|---|
|  | 1166 | } | 
|---|
| [64ac636] | 1167 | if (! errors.isEmpty()) { | 
|---|
|  | 1168 | throw errors; | 
|---|
|  | 1169 | } | 
|---|
| [b3fc977] | 1170 | return funcDecl; | 
|---|
| [79970ed] | 1171 | } | 
|---|
|  | 1172 |  | 
|---|
| [4618319] | 1173 | /// true if expr is effectively just the 'this' parameter | 
|---|
|  | 1174 | bool isThisExpression( Expression * expr, DeclarationWithType * thisParam ) { | 
|---|
|  | 1175 | // TODO: there are more complicated ways to pass 'this' to a constructor, e.g. &*, *&, etc. | 
|---|
|  | 1176 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { | 
|---|
|  | 1177 | return varExpr->get_var() == thisParam; | 
|---|
|  | 1178 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( expr ) ) { | 
|---|
|  | 1179 | return isThisExpression( castExpr->get_arg(), thisParam ); | 
|---|
|  | 1180 | } | 
|---|
|  | 1181 | return false; | 
|---|
|  | 1182 | } | 
|---|
|  | 1183 |  | 
|---|
|  | 1184 | /// returns a MemberExpr if expr is effectively just member access on the 'this' parameter, else nullptr | 
|---|
|  | 1185 | MemberExpr * isThisMemberExpr( Expression * expr, DeclarationWithType * thisParam ) { | 
|---|
|  | 1186 | if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { | 
|---|
|  | 1187 | if ( isThisExpression( memberExpr->get_aggregate(), thisParam ) ) { | 
|---|
|  | 1188 | return memberExpr; | 
|---|
|  | 1189 | } | 
|---|
|  | 1190 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { | 
|---|
|  | 1191 | return isThisMemberExpr( castExpr->get_arg(), thisParam ); | 
|---|
|  | 1192 | } | 
|---|
|  | 1193 | return nullptr; | 
|---|
|  | 1194 | } | 
|---|
|  | 1195 |  | 
|---|
| [b3fc977] | 1196 | void GenStructMemberCalls::premutate( ApplicationExpr * appExpr ) { | 
|---|
| [9a707e4e] | 1197 | if ( ! checkWarnings( function ) ) { | 
|---|
|  | 1198 | visit_children = false; | 
|---|
|  | 1199 | return; | 
|---|
|  | 1200 | } | 
|---|
| [79970ed] | 1201 |  | 
|---|
|  | 1202 | std::string fname = getFunctionName( appExpr ); | 
|---|
| [589a70b] | 1203 | if ( fname == function->name ) { | 
|---|
| [79970ed] | 1204 | // call to same kind of function | 
|---|
| [589a70b] | 1205 | Expression * firstParam = appExpr->args.front(); | 
|---|
| [79970ed] | 1206 |  | 
|---|
| [4618319] | 1207 | if ( isThisExpression( firstParam, thisParam ) ) { | 
|---|
| [79970ed] | 1208 | // if calling another constructor on thisParam, assume that function handles | 
|---|
|  | 1209 | // all members - if it doesn't a warning will appear in that function. | 
|---|
| [4618319] | 1210 | unhandled.clear(); | 
|---|
|  | 1211 | } else if ( MemberExpr * memberExpr = isThisMemberExpr( firstParam, thisParam ) ) { | 
|---|
|  | 1212 | // if first parameter is a member expression on the this parameter, | 
|---|
|  | 1213 | // then remove the member from unhandled set. | 
|---|
| [589a70b] | 1214 | if ( isThisExpression( memberExpr->aggregate, thisParam ) ) { | 
|---|
|  | 1215 | unhandled.erase( memberExpr->member ); | 
|---|
| [79970ed] | 1216 | } | 
|---|
|  | 1217 | } | 
|---|
|  | 1218 | } | 
|---|
|  | 1219 | } | 
|---|
|  | 1220 |  | 
|---|
| [b3fc977] | 1221 | void GenStructMemberCalls::premutate( MemberExpr * memberExpr ) { | 
|---|
| [9a707e4e] | 1222 | if ( ! checkWarnings( function ) || ! isCtor ) { | 
|---|
|  | 1223 | visit_children = false; | 
|---|
|  | 1224 | return; | 
|---|
|  | 1225 | } | 
|---|
| [79970ed] | 1226 |  | 
|---|
| [4618319] | 1227 | if ( isThisExpression( memberExpr->get_aggregate(), thisParam ) ) { | 
|---|
|  | 1228 | if ( unhandled.count( memberExpr->get_member() ) ) { | 
|---|
|  | 1229 | // emit a warning because a member was used before it was constructed | 
|---|
|  | 1230 | usedUninit.insert( { memberExpr->get_member(), memberExpr->location } ); | 
|---|
| [79970ed] | 1231 | } | 
|---|
|  | 1232 | } | 
|---|
|  | 1233 | } | 
|---|
| [3906301] | 1234 |  | 
|---|
|  | 1235 | template< typename... Params > | 
|---|
| [64ac636] | 1236 | void GenStructMemberCalls::emit( CodeLocation loc, const Params &... params ) { | 
|---|
| [c94b1f0] | 1237 | SemanticErrorException err( loc, toString( params... ) ); | 
|---|
|  | 1238 | errors.append( err ); | 
|---|
| [3906301] | 1239 | } | 
|---|
| [c8dfcd3] | 1240 |  | 
|---|
| [b3fc977] | 1241 | Expression * GenStructMemberCalls::postmutate( UntypedExpr * untypedExpr ) { | 
|---|
| [08da53d] | 1242 | Expression * newExpr = untypedExpr; | 
|---|
|  | 1243 | ResolvExpr::findVoidExpression( newExpr, indexer ); | 
|---|
|  | 1244 | return newExpr; | 
|---|
| [c8dfcd3] | 1245 | } | 
|---|
| [b6fe7e6] | 1246 |  | 
|---|
| [696bf6e] | 1247 | Expression * FixCtorExprs::postmutate( ConstructorExpr * ctorExpr ) { | 
|---|
| [b6fe7e6] | 1248 | static UniqueName tempNamer( "_tmp_ctor_expr" ); | 
|---|
| [f0121d7] | 1249 | // xxx - is the size check necessary? | 
|---|
| [d29fa5f] | 1250 | assert( ctorExpr->result && ctorExpr->get_result()->size() == 1 ); | 
|---|
| [b7b8674] | 1251 |  | 
|---|
| [627f585] | 1252 | // xxx - this can be TupleAssignExpr now. Need to properly handle this case. | 
|---|
| [e3e16bc] | 1253 | ApplicationExpr * callExpr = strict_dynamic_cast< ApplicationExpr * > ( ctorExpr->get_callExpr() ); | 
|---|
| [b6fe7e6] | 1254 | TypeSubstitution * env = ctorExpr->get_env(); | 
|---|
|  | 1255 | ctorExpr->set_callExpr( nullptr ); | 
|---|
|  | 1256 | ctorExpr->set_env( nullptr ); | 
|---|
| [f5c3b6c] | 1257 |  | 
|---|
|  | 1258 | // 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. | 
|---|
|  | 1259 | ObjectDecl * tmp = ObjectDecl::newObject( tempNamer.newName(), callExpr->args.front()->result->clone(), nullptr ); | 
|---|
|  | 1260 | declsToAddBefore.push_back( tmp ); | 
|---|
| [8a6cf7e] | 1261 | delete ctorExpr; | 
|---|
| [b6fe7e6] | 1262 |  | 
|---|
| [696bf6e] | 1263 | // build assignment and replace constructor's first argument with new temporary | 
|---|
| [b6fe7e6] | 1264 | Expression *& firstArg = callExpr->get_args().front(); | 
|---|
| [696bf6e] | 1265 | Expression * assign = new UntypedExpr( new NameExpr( "?=?" ), { new AddressExpr( new VariableExpr( tmp ) ), new AddressExpr( firstArg ) } ); | 
|---|
| [8a6cf7e] | 1266 | firstArg = new VariableExpr( tmp ); | 
|---|
|  | 1267 |  | 
|---|
| [696bf6e] | 1268 | // resolve assignment and dispose of new env | 
|---|
| [08da53d] | 1269 | ResolvExpr::findVoidExpression( assign, indexer ); | 
|---|
|  | 1270 | delete assign->env; | 
|---|
|  | 1271 | assign->env = nullptr; | 
|---|
| [696bf6e] | 1272 |  | 
|---|
| [8a6cf7e] | 1273 | // for constructor expr: | 
|---|
|  | 1274 | //   T x; | 
|---|
|  | 1275 | //   x{}; | 
|---|
|  | 1276 | // results in: | 
|---|
|  | 1277 | //   T x; | 
|---|
|  | 1278 | //   T & tmp; | 
|---|
|  | 1279 | //   &tmp = &x, ?{}(tmp), tmp | 
|---|
| [08da53d] | 1280 | CommaExpr * commaExpr = new CommaExpr( assign, new CommaExpr( callExpr, new VariableExpr( tmp ) ) ); | 
|---|
| [b6fe7e6] | 1281 | commaExpr->set_env( env ); | 
|---|
|  | 1282 | return commaExpr; | 
|---|
|  | 1283 | } | 
|---|
| [c2931ea] | 1284 | } // namespace | 
|---|
| [71f4e4f] | 1285 | } // namespace InitTweak | 
|---|
|  | 1286 |  | 
|---|
|  | 1287 | // Local Variables: // | 
|---|
|  | 1288 | // tab-width: 4 // | 
|---|
|  | 1289 | // mode: c++ // | 
|---|
|  | 1290 | // compile-command: "make install" // | 
|---|
|  | 1291 | // End: // | 
|---|