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