Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Autogen.cc

    rf0ecf9b r4e8949f  
    1616#include "Autogen.h"
    1717
     18#include <cstddef>                 // for NULL
    1819#include <algorithm>               // for count_if
    1920#include <cassert>                 // for strict_dynamic_cast, assert, assertf
     
    2627#include "AddVisit.h"              // for addVisit
    2728#include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign
    28 #include "Common/PassVisitor.h"    // for PassVisitor
    2929#include "Common/ScopedMap.h"      // for ScopedMap<>::const_iterator, Scope...
    3030#include "Common/utility.h"        // for cloneAll, operator+
     31#include "GenPoly/DeclMutator.h"   // for DeclMutator
    3132#include "GenPoly/ScopedSet.h"     // for ScopedSet, ScopedSet<>::iterator
    32 #include "InitTweak/GenInit.h"     // for fixReturnStatements
    33 #include "ResolvExpr/Resolver.h"   // for resolveDecl
    3433#include "SymTab/Mangler.h"        // for Mangler
    3534#include "SynTree/Attribute.h"     // For Attribute
     
    4342namespace SymTab {
    4443        Type * SizeType = 0;
    45 
    46         /// Data used to generate functions generically. Specifically, the name of the generated function and a function which generates the routine protoype
     44        typedef ScopedMap< std::string, bool > TypeMap;
     45
     46        /// Data used to generate functions generically. Specifically, the name of the generated function, a function which generates the routine protoype, and a map which contains data to determine whether a function should be generated.
    4747        struct FuncData {
    4848                typedef FunctionType * (*TypeGen)( Type * );
    49                 FuncData( const std::string & fname, const TypeGen & genType ) : fname( fname ), genType( genType ) {}
     49                FuncData( const std::string & fname, const TypeGen & genType, TypeMap & map ) : fname( fname ), genType( genType ), map( map ) {}
    5050                std::string fname;
    5151                TypeGen genType;
     52                TypeMap & map;
    5253        };
    5354
    54         struct AutogenerateRoutines final : public WithDeclsToAdd, public WithVisitorRef<AutogenerateRoutines>, public WithGuards, public WithShortCircuiting, public WithIndexer {
     55        class AutogenerateRoutines final : public Visitor {
     56            template< typename Visitor >
     57            friend void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor );
     58            template< typename Visitor >
     59            friend void addVisitStatementList( std::list< Statement* > &stmts, Visitor &visitor );
     60          public:
     61                std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
     62
     63                typedef Visitor Parent;
     64                using Parent::visit;
     65
    5566                AutogenerateRoutines();
    5667
    57                 void previsit( EnumDecl * enumDecl );
    58                 void previsit( StructDecl * structDecl );
    59                 void previsit( UnionDecl * structDecl );
    60                 void previsit( TypeDecl * typeDecl );
    61                 void previsit( TraitDecl * traitDecl );
    62                 void previsit( FunctionDecl * functionDecl );
    63 
    64                 void previsit( FunctionType * ftype );
    65                 void previsit( PointerType * ptype );
    66 
    67                 void previsit( CompoundStmt * compoundStmt );
     68                virtual void visit( EnumDecl *enumDecl );
     69                virtual void visit( StructDecl *structDecl );
     70                virtual void visit( UnionDecl *structDecl );
     71                virtual void visit( TypeDecl *typeDecl );
     72                virtual void visit( TraitDecl *ctxDecl );
     73                virtual void visit( FunctionDecl *functionDecl );
     74
     75                virtual void visit( FunctionType *ftype );
     76                virtual void visit( PointerType *ftype );
     77
     78                virtual void visit( CompoundStmt *compoundStmt );
     79                virtual void visit( SwitchStmt *switchStmt );
    6880
    6981          private:
    70 
    71                 GenPoly::ScopedSet< std::string > structsDone;
     82                template< typename StmtClass > void visitStatement( StmtClass *stmt );
     83
     84                std::list< Declaration * > declsToAdd, declsToAddAfter;
     85                std::set< std::string > structsDone;
    7286                unsigned int functionNesting = 0;     // current level of nested functions
    73 
    74                 InitTweak::ManagedTypes managedTypes;
     87                /// Note: the following maps could be ScopedSets, but it should be easier to work
     88                /// deleted functions in if they are maps, since the value false can be inserted
     89                /// at the current scope without affecting outer scopes or requiring copies.
     90                TypeMap copyable, assignable, constructable, destructable;
    7591                std::vector< FuncData > data;
    7692        };
    7793
    7894        /// generates routines for tuple types.
    79         struct AutogenTupleRoutines : public WithDeclsToAdd, public WithVisitorRef<AutogenTupleRoutines>, public WithGuards, public WithShortCircuiting {
    80                 void previsit( FunctionDecl * functionDecl );
    81 
    82                 void postvisit( TupleType * tupleType );
    83 
    84                 void previsit( CompoundStmt * compoundStmt );
     95        /// Doesn't really need to be a mutator, but it's easier to reuse DeclMutator than it is to use AddVisit
     96        /// or anything we currently have that supports adding new declarations for visitors
     97        class AutogenTupleRoutines : public GenPoly::DeclMutator {
     98          public:
     99                typedef GenPoly::DeclMutator Parent;
     100                using Parent::mutate;
     101
     102                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
     103
     104                virtual Type * mutate( TupleType *tupleType );
     105
     106                virtual CompoundStmt * mutate( CompoundStmt *compoundStmt );
    85107
    86108          private:
     
    90112
    91113        void autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
    92                 PassVisitor<AutogenerateRoutines> generator;
    93                 acceptAll( translationUnit, generator );
     114                AutogenerateRoutines generator;
     115                acceptAndAdd( translationUnit, generator );
    94116
    95117                // needs to be done separately because AutogenerateRoutines skips types that appear as function arguments, etc.
    96118                // AutogenTupleRoutines tupleGenerator;
    97                 // acceptAll( translationUnit, tupleGenerator );
    98         }
    99 
    100         //=============================================================================================
    101         // FuncGenerator definitions
    102         //=============================================================================================
    103         class FuncGenerator {
    104         public:
    105                 std::list< Declaration * > definitions, forwards;
    106 
    107                 FuncGenerator( Type * type, const std::vector< FuncData > & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : type( type ), data( data ), functionNesting( functionNesting ), indexer( indexer ) {}
    108 
    109                 virtual bool shouldAutogen() const = 0;
    110                 void genStandardFuncs();
    111                 virtual void genFieldCtors() = 0;
    112         protected:
    113                 Type * type;
    114                 const std::vector< FuncData > & data;
    115                 unsigned int functionNesting;
    116                 SymTab::Indexer & indexer;
    117 
    118                 virtual void genFuncBody( FunctionDecl * dcl ) = 0;
    119                 virtual bool isConcurrentType() const = 0;
    120 
    121                 void resolve( FunctionDecl * dcl );
    122                 void generatePrototypes( std::list< FunctionDecl * > & newFuncs );
    123         };
    124 
    125         class StructFuncGenerator : public FuncGenerator {
    126                 StructDecl * aggregateDecl;
    127         public:
    128                 StructFuncGenerator( StructDecl * aggregateDecl, StructInstType * refType, const std::vector< FuncData > & data,  unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), aggregateDecl( aggregateDecl) {}
    129 
    130                 virtual bool shouldAutogen() const override;
    131                 virtual bool isConcurrentType() const override;
    132 
    133                 virtual void genFuncBody( FunctionDecl * dcl ) override;
    134                 virtual void genFieldCtors() override;
    135 
    136         private:
    137                 /// generates a single struct member operation (constructor call, destructor call, assignment call)
    138                 void makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward = true );
    139 
    140                 /// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies
    141                 template<typename Iterator>
    142                 void makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true );
    143 
    144                 /// generate the body of a constructor which takes parameters that match fields, e.g.
    145                 /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
    146                 template<typename Iterator>
    147                 void makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func );
    148         };
    149 
    150         class UnionFuncGenerator : public FuncGenerator {
    151                 UnionDecl * aggregateDecl;
    152         public:
    153                 UnionFuncGenerator( UnionDecl * aggregateDecl, UnionInstType * refType, const std::vector< FuncData > & data,  unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), aggregateDecl( aggregateDecl) {}
    154 
    155                 virtual bool shouldAutogen() const override;
    156                 virtual bool isConcurrentType() const override;
    157 
    158                 virtual void genFuncBody( FunctionDecl * dcl ) override;
    159                 virtual void genFieldCtors() override;
    160 
    161         private:
    162                 /// generates a single struct member operation (constructor call, destructor call, assignment call)
    163                 template<typename OutputIterator>
    164                 void makeMemberOp( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out );
    165 
    166                 /// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies
    167                 template<typename Iterator>
    168                 void makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true );
    169 
    170                 /// generate the body of a constructor which takes parameters that match fields, e.g.
    171                 /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
    172                 template<typename Iterator>
    173                 void makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func );
    174         };
    175 
    176         class EnumFuncGenerator : public FuncGenerator {
    177         public:
    178                 EnumFuncGenerator( EnumInstType * refType, const std::vector< FuncData > & data,  unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ) {}
    179 
    180                 virtual bool shouldAutogen() const override;
    181                 virtual bool isConcurrentType() const override;
    182 
    183                 virtual void genFuncBody( FunctionDecl * dcl ) override;
    184                 virtual void genFieldCtors() override;
    185 
    186         private:
    187         };
    188 
    189         class TypeFuncGenerator : public FuncGenerator {
    190                 TypeDecl * typeDecl;
    191         public:
    192                 TypeFuncGenerator( TypeDecl * typeDecl, TypeInstType * refType, const std::vector<FuncData> & data, unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, data, functionNesting, indexer ), typeDecl( typeDecl ) {}
    193 
    194                 virtual bool shouldAutogen() const override;
    195                 virtual void genFuncBody( FunctionDecl * dcl ) override;
    196                 virtual bool isConcurrentType() const override;
    197                 virtual void genFieldCtors() override;
    198         };
    199 
    200         //=============================================================================================
    201         // helper functions
    202         //=============================================================================================
    203         void generateFunctions( FuncGenerator & gen, std::list< Declaration * > & declsToAdd ) {
    204                 if ( ! gen.shouldAutogen() ) return;
    205 
    206                 // generate each of the functions based on the supplied FuncData objects
    207                 gen.genStandardFuncs();
    208                 gen.genFieldCtors();
    209 
    210                 declsToAdd.splice( declsToAdd.end(), gen.forwards );
    211                 declsToAdd.splice( declsToAdd.end(), gen.definitions );
     119                // tupleGenerator.mutateDeclarationList( translationUnit );
    212120        }
    213121
    214122        bool isUnnamedBitfield( ObjectDecl * obj ) {
    215                 return obj != nullptr && obj->name == "" && obj->bitfieldWidth != nullptr;
     123                return obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL;
    216124        }
    217125
     
    219127        void addForwardDecl( FunctionDecl * functionDecl, std::list< Declaration * > & declsToAdd ) {
    220128                FunctionDecl * decl = functionDecl->clone();
    221                 delete decl->statements;
    222                 decl->statements = nullptr;
     129                delete decl->get_statements();
     130                decl->set_statements( NULL );
    223131                declsToAdd.push_back( decl );
    224132                decl->fixUniqueId();
    225133        }
    226134
    227         const std::list< TypeDecl * > getGenericParams( Type * t ) {
    228                 std::list< TypeDecl * > * ret = nullptr;
    229                 if ( StructInstType * inst = dynamic_cast< StructInstType * > ( t ) ) {
    230                         ret = inst->get_baseParameters();
    231                 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) {
    232                         ret = inst->get_baseParameters();
    233                 }
    234                 return ret ? *ret : std::list< TypeDecl * >();
    235         }
    236 
    237135        /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *)
    238136        FunctionType * genDefaultType( Type * paramType ) {
    239                 const auto & typeParams = getGenericParams( paramType );
    240137                FunctionType *ftype = new FunctionType( Type::Qualifiers(), false );
    241                 cloneAll( typeParams, ftype->forall );
    242138                ObjectDecl *dstParam = new ObjectDecl( "_dst", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), paramType->clone() ), nullptr );
    243                 ftype->parameters.push_back( dstParam );
     139                ftype->get_parameters().push_back( dstParam );
     140
    244141                return ftype;
    245142        }
     
    249146                FunctionType *ftype = genDefaultType( paramType );
    250147                ObjectDecl *srcParam = new ObjectDecl( "_src", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
    251                 ftype->parameters.push_back( srcParam );
     148                ftype->get_parameters().push_back( srcParam );
    252149                return ftype;
    253150        }
     
    257154                FunctionType *ftype = genCopyType( paramType );
    258155                ObjectDecl *returnVal = new ObjectDecl( "_ret", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
    259                 ftype->returnVals.push_back( returnVal );
     156                ftype->get_returnVals().push_back( returnVal );
    260157                return ftype;
    261158        }
     
    274171        }
    275172
    276         Type * declToType( Declaration * decl ) {
    277                 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    278                         return dwt->get_type();
    279                 }
    280                 return nullptr;
    281         }
    282 
    283         Type * declToTypeDeclBase( Declaration * decl ) {
    284                 if ( TypeDecl * td = dynamic_cast< TypeDecl * >( decl ) ) {
    285                         return td->base;
    286                 }
    287                 return nullptr;
    288         }
    289 
    290         //=============================================================================================
    291         // FuncGenerator member definitions
    292         //=============================================================================================
    293         void FuncGenerator::genStandardFuncs() {
    294                 std::list< FunctionDecl * > newFuncs;
    295                 generatePrototypes( newFuncs );
    296 
    297                 for ( FunctionDecl * dcl : newFuncs ) {
    298                         genFuncBody( dcl );
    299                         if ( CodeGen::isAssignment( dcl->name ) ) {
    300                                 // assignment needs to return a value
    301                                 FunctionType * assignType = dcl->type;
    302                                 assert( assignType->parameters.size() == 2 );
    303                                 assert( assignType->returnVals.size() == 1 );
    304                                 ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.front() );
    305                                 dcl->statements->push_back( new ReturnStmt( noLabels, new VariableExpr( dstParam ) ) );
    306                         }
    307                         resolve( dcl );
    308                 }
    309         }
    310 
    311         void FuncGenerator::generatePrototypes( std::list< FunctionDecl * > & newFuncs ) {
    312                 bool concurrent_type = isConcurrentType();
    313                 for ( const FuncData & d : data ) {
    314                         // generate a function (?{}, ?=?, ^?{}) based on the current FuncData.
    315                         FunctionType * ftype = d.genType( type );
    316 
    317                         // destructor for concurrent type must be mutex
    318                         if ( concurrent_type && CodeGen::isDestructor( d.fname ) ) {
     173        /// inserts base type of first argument into map if pred(funcDecl) is true
     174        void insert( FunctionDecl *funcDecl, TypeMap & map, FunctionDecl * (*pred)(Declaration *) ) {
     175                // insert type into constructable, etc. map if appropriate
     176                if ( pred( funcDecl ) ) {
     177                        FunctionType * ftype = funcDecl->get_functionType();
     178                        assert( ! ftype->get_parameters().empty() );
     179                        Type * t = InitTweak::getPointerBase( ftype->get_parameters().front()->get_type() );
     180                        assert( t );
     181                        map.insert( Mangler::mangleType( t ), true );
     182                }
     183        }
     184
     185        /// using map and t, determines if is constructable, etc.
     186        bool lookup( const TypeMap & map, Type * t ) {
     187                assertf( t, "Autogenerate lookup was given non-type: %s", toString( t ).c_str() );
     188                if ( dynamic_cast< PointerType * >( t ) ) {
     189                        // will need more complicated checking if we want this to work with pointer types, since currently
     190                        return true;
     191                } else if ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) {
     192                        // an array's constructor, etc. is generated on the fly based on the base type's constructor, etc.
     193                        return lookup( map, at->get_base() );
     194                }
     195                TypeMap::const_iterator it = map.find( Mangler::mangleType( t ) );
     196                if ( it != map.end() ) return it->second;
     197                // something that does not appear in the map is by default not constructable, etc.
     198                return false;
     199        }
     200
     201        /// using map and aggr, examines each member to determine if constructor, etc. should be generated
     202        template<typename Container>
     203        bool shouldGenerate( const TypeMap & map, const Container & container ) {
     204                for ( Type * t : container ) {
     205                        if ( ! lookup( map, t ) ) return false;
     206                }
     207                return true;
     208        }
     209
     210        /// data structure for abstracting the generation of special functions
     211        template< typename OutputIterator, typename Container >
     212        struct FuncGenerator {
     213                const Container & container;
     214                Type *refType;
     215                unsigned int functionNesting;
     216                const std::list< TypeDecl* > & typeParams;
     217                OutputIterator out;
     218                FuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, const std::list< TypeDecl* > & typeParams, OutputIterator out ) : container( container ), refType( refType ), functionNesting( functionNesting ), typeParams( typeParams ), out( out ) {}
     219
     220                /// generates a function (?{}, ?=?, ^?{}) based on the data argument and members. If function is generated, inserts the type into the map.
     221                void gen( const FuncData & data, bool concurrent_type ) {
     222                        if ( ! shouldGenerate( data.map, container ) ) return;
     223                        FunctionType * ftype = data.genType( refType );
     224
     225                        if ( concurrent_type && CodeGen::isDestructor( data.fname ) ) {
    319226                                ftype->parameters.front()->get_type()->set_mutex( true );
    320227                        }
    321228
    322                         newFuncs.push_back( genFunc( d.fname, ftype, functionNesting ) );
    323                 }
    324         }
    325 
    326         void FuncGenerator::resolve( FunctionDecl * dcl ) {
    327                 try {
    328                         ResolvExpr::resolveDecl( dcl, indexer );
    329                         if ( functionNesting == 0 ) {
    330                                 // forward declare if top-level struct, so that
    331                                 // type is complete as soon as its body ends
    332                                 // Note: this is necessary if we want structs which contain
    333                                 // generic (otype) structs as members.
    334                                 addForwardDecl( dcl, forwards );
    335                         }
    336                         definitions.push_back( dcl );
    337                         indexer.addId( dcl );
    338                 } catch ( SemanticError err ) {
    339                         // okay if decl does not resolve - that means the function should not be generated
    340                         delete dcl;
    341                 }
    342         }
    343 
    344         bool StructFuncGenerator::shouldAutogen() const {
    345                 // Builtins do not use autogeneration.
    346                 return ! aggregateDecl->linkage.is_builtin;
    347         }
    348         bool StructFuncGenerator::isConcurrentType() const { return aggregateDecl->is_thread() || aggregateDecl->is_monitor(); }
    349 
    350         void StructFuncGenerator::genFuncBody( FunctionDecl * dcl ) {
    351                 // generate appropriate calls to member ctor, assignment
    352                 // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
    353                 if ( ! CodeGen::isDestructor( dcl->name ) ) {
    354                         makeFunctionBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), dcl );
    355                 } else {
    356                         makeFunctionBody( aggregateDecl->members.rbegin(), aggregateDecl->members.rend(), dcl, false );
    357                 }
    358         }
    359 
    360         void StructFuncGenerator::genFieldCtors() {
    361                 // field ctors are only generated if default constructor and copy constructor are both generated
    362                 unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->name ); } );
    363 
    364                 // Field constructors are only generated if default and copy constructor
    365                 // are generated, since they need access to both
    366                 if ( numCtors != 2 ) return;
    367 
    368                 // create constructors which take each member type as a parameter.
    369                 // for example, for struct A { int x, y; }; generate
    370                 //   void ?{}(A *, int) and void ?{}(A *, int, int)
    371                 FunctionType * memCtorType = genDefaultType( type );
    372                 for ( Declaration * member : aggregateDecl->members ) {
    373                         DeclarationWithType * field = strict_dynamic_cast<DeclarationWithType *>( member );
    374                         if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
    375                                 // don't make a function whose parameter is an unnamed bitfield
    376                                 continue;
    377                         }
    378                         memCtorType->parameters.push_back( new ObjectDecl( field->name, Type::StorageClasses(), LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 ) );
    379                         FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
    380                         makeFieldCtorBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), ctor );
    381                         resolve( ctor );
    382                 }
    383                 delete memCtorType;
    384         }
    385 
    386         void StructFuncGenerator::makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward ) {
     229                        cloneAll( typeParams, ftype->forall );
     230                        *out++ = genFunc( data.fname, ftype, functionNesting );
     231                        data.map.insert( Mangler::mangleType( refType ), true );
     232                }
     233        };
     234
     235        template< typename OutputIterator, typename Container >
     236        FuncGenerator<OutputIterator, Container> makeFuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, const std::list< TypeDecl* > & typeParams, OutputIterator out ) {
     237                return FuncGenerator<OutputIterator, Container>( container, refType, functionNesting, typeParams, out );
     238        }
     239
     240        /// generates a single enumeration assignment expression
     241        ApplicationExpr * genEnumAssign( FunctionType * ftype, FunctionDecl * assignDecl ) {
     242                // enum copy construct and assignment is just C-style assignment.
     243                // this looks like a bad recursive call, but code gen will turn it into
     244                // a C-style assignment.
     245                // This happens before function pointer type conversion, so need to do it manually here
     246                // NOTE: ftype is not necessarily the functionType belonging to assignDecl - ftype is the
     247                // type of the function that this expression is being generated for (so that the correct
     248                // parameters) are using in the variable exprs
     249                assert( ftype->get_parameters().size() == 2 );
     250                ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().front() );
     251                ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().back() );
     252
     253                VariableExpr * assignVarExpr = new VariableExpr( assignDecl );
     254                Type * assignVarExprType = assignVarExpr->get_result();
     255                assignVarExprType = new PointerType( Type::Qualifiers(), assignVarExprType );
     256                assignVarExpr->set_result( assignVarExprType );
     257                ApplicationExpr * assignExpr = new ApplicationExpr( assignVarExpr );
     258                assignExpr->get_args().push_back( new VariableExpr( dstParam ) );
     259                assignExpr->get_args().push_back( new VariableExpr( srcParam ) );
     260                return assignExpr;
     261        }
     262
     263        // E ?=?(E volatile*, int),
     264        //   ?=?(E _Atomic volatile*, int);
     265        void makeEnumFunctions( EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
     266
     267                // T ?=?(E *, E);
     268                FunctionType *assignType = genAssignType( refType );
     269
     270                // void ?{}(E *); void ^?{}(E *);
     271                FunctionType * ctorType = genDefaultType( refType->clone() );
     272                FunctionType * dtorType = genDefaultType( refType->clone() );
     273
     274                // void ?{}(E *, E);
     275                FunctionType *copyCtorType = genCopyType( refType->clone() );
     276
     277                // add unused attribute to parameters of default constructor and destructor
     278                ctorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
     279                dtorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
     280
     281                // xxx - should we also generate void ?{}(E *, int) and E ?{}(E *, E)?
     282                // right now these cases work, but that might change.
     283
     284                // xxx - Temporary: make these functions intrinsic so they codegen as C assignment.
     285                // Really they're something of a cross between instrinsic and autogen, so should
     286                // probably make a new linkage type
     287                FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting, true );
     288                FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting, true );
     289                FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting, true );
     290                FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting, true );
     291
     292                // body is either return stmt or expr stmt
     293                assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, genEnumAssign( assignType, assignDecl ) ) );
     294                copyCtorDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, genEnumAssign( copyCtorType, assignDecl ) ) );
     295
     296                declsToAdd.push_back( ctorDecl );
     297                declsToAdd.push_back( copyCtorDecl );
     298                declsToAdd.push_back( dtorDecl );
     299                declsToAdd.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
     300        }
     301
     302        /// generates a single struct member operation (constructor call, destructor call, assignment call)
     303        void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward = true ) {
    387304                InitTweak::InitExpander srcParam( src );
    388305
    389306                // assign to destination
    390                 Expression *dstselect = new MemberExpr( field, new CastExpr( new VariableExpr( dstParam ), strict_dynamic_cast< ReferenceType* >( dstParam->get_type() )->base->clone() ) );
    391                 genImplicitCall( srcParam, dstselect, func->name, back_inserter( func->statements->kids ), field, forward );
    392         }
    393 
     307                Expression *dstselect = new MemberExpr( field, new CastExpr( new VariableExpr( dstParam ), strict_dynamic_cast< ReferenceType* >( dstParam->get_type() )->get_base()->clone() ) );
     308                genImplicitCall( srcParam, dstselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward );
     309        }
     310
     311        /// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies
    394312        template<typename Iterator>
    395         void StructFuncGenerator::makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward ) {
     313        void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true ) {
    396314                for ( ; member != end; ++member ) {
    397315                        if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
     
    403321                                }
    404322
    405                                 if ( type->get_const() && CodeGen::isAssignment( func->name ) ) {
     323                                if ( type->get_const() && func->get_name() == "?=?" ) {
    406324                                        // don't assign const members, but do construct/destruct
    407325                                        continue;
    408326                                }
    409327
     328                                if ( field->get_name() == "" ) {
     329                                        // don't assign to anonymous members
     330                                        // xxx - this is a temporary fix. Anonymous members tie into
     331                                        // our inheritance model. I think the correct way to handle this is to
     332                                        // cast the structure to the type of the member and let the resolver
     333                                        // figure out whether it's valid and have a pass afterwards that fixes
     334                                        // the assignment to use pointer arithmetic with the offset of the
     335                                        // member, much like how generic type members are handled.
     336                                        continue;
     337                                }
     338
    410339                                assert( ! func->get_functionType()->get_parameters().empty() );
    411340                                ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
    412                                 ObjectDecl * srcParam = nullptr;
     341                                ObjectDecl * srcParam = NULL;
    413342                                if ( func->get_functionType()->get_parameters().size() == 2 ) {
    414343                                        srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() );
    415344                                }
    416 
    417345                                // srcParam may be NULL, in which case we have default ctor/dtor
    418346                                assert( dstParam );
    419347
    420                                 Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : nullptr;
    421                                 makeMemberOp( dstParam, srcselect, field, func, forward );
     348                                Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : NULL;
     349                                makeStructMemberOp( dstParam, srcselect, field, func, forward );
    422350                        } // if
    423351                } // for
    424         } // makeFunctionBody
    425 
     352        } // makeStructFunctionBody
     353
     354        /// generate the body of a constructor which takes parameters that match fields, e.g.
     355        /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
    426356        template<typename Iterator>
    427         void StructFuncGenerator::makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) {
    428                 FunctionType * ftype = func->type;
    429                 std::list<DeclarationWithType*> & params = ftype->parameters;
     357        void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) {
     358                FunctionType * ftype = func->get_functionType();
     359                std::list<DeclarationWithType*> & params = ftype->get_parameters();
    430360                assert( params.size() >= 2 );  // should not call this function for default ctor, etc.
    431361
     
    439369                                        // don't make a function whose parameter is an unnamed bitfield
    440370                                        continue;
     371                                } else if ( field->get_name() == "" ) {
     372                                        // don't assign to anonymous members
     373                                        // xxx - this is a temporary fix. Anonymous members tie into
     374                                        // our inheritance model. I think the correct way to handle this is to
     375                                        // cast the structure to the type of the member and let the resolver
     376                                        // figure out whether it's valid and have a pass afterwards that fixes
     377                                        // the assignment to use pointer arithmetic with the offset of the
     378                                        // member, much like how generic type members are handled.
     379                                        continue;
    441380                                } else if ( parameter != params.end() ) {
    442381                                        // matching parameter, initialize field with copy ctor
    443382                                        Expression *srcselect = new VariableExpr(*parameter);
    444                                         makeMemberOp( dstParam, srcselect, field, func );
     383                                        makeStructMemberOp( dstParam, srcselect, field, func );
    445384                                        ++parameter;
    446385                                } else {
    447386                                        // no matching parameter, initialize field with default ctor
    448                                         makeMemberOp( dstParam, nullptr, field, func );
     387                                        makeStructMemberOp( dstParam, NULL, field, func );
    449388                                }
    450389                        }
     
    452391        }
    453392
    454         bool UnionFuncGenerator::shouldAutogen() const {
     393        Type * declToType( Declaration * decl ) {
     394                if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     395                        return dwt->get_type();
     396                }
     397                return nullptr;
     398        }
     399
     400        /// generates struct constructors, destructor, and assignment functions
     401        void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd, const std::vector< FuncData > & data ) {
    455402                // Builtins do not use autogeneration.
    456                 return ! aggregateDecl->linkage.is_builtin;
    457         }
    458 
    459         // xxx - is this right?
    460         bool UnionFuncGenerator::isConcurrentType() const { return false; };
     403                if ( aggregateDecl->get_linkage() == LinkageSpec::BuiltinCFA ||
     404                         aggregateDecl->get_linkage() == LinkageSpec::BuiltinC ) {
     405                        return;
     406                }
     407
     408                // Make function polymorphic in same parameters as generic struct, if applicable
     409                const std::list< TypeDecl* > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
     410
     411                // generate each of the functions based on the supplied FuncData objects
     412                std::list< FunctionDecl * > newFuncs;
     413                // structure that iterates aggregate decl members, returning their types
     414                auto generator = makeFuncGenerator( lazy_map( aggregateDecl->members, declToType ), refType, functionNesting, typeParams, back_inserter( newFuncs ) );
     415                for ( const FuncData & d : data ) {
     416                        generator.gen( d, aggregateDecl->is_thread() || aggregateDecl->is_monitor() );
     417                }
     418
     419                // field ctors are only generated if default constructor and copy constructor are both generated
     420                unsigned numCtors = std::count_if( newFuncs.begin(), newFuncs.end(), [](FunctionDecl * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
     421
     422                if ( functionNesting == 0 ) {
     423                        // forward declare if top-level struct, so that
     424                        // type is complete as soon as its body ends
     425                        // Note: this is necessary if we want structs which contain
     426                        // generic (otype) structs as members.
     427                        for ( FunctionDecl * dcl : newFuncs ) {
     428                                addForwardDecl( dcl, declsToAdd );
     429                        }
     430                }
     431
     432                for ( FunctionDecl * dcl : newFuncs ) {
     433                        // generate appropriate calls to member ctor, assignment
     434                        // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
     435                        if ( ! CodeGen::isDestructor( dcl->get_name() ) ) {
     436                                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), dcl );
     437                        } else {
     438                                makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dcl, false );
     439                        }
     440                        if ( CodeGen::isAssignment( dcl->get_name() ) ) {
     441                                // assignment needs to return a value
     442                                FunctionType * assignType = dcl->get_functionType();
     443                                assert( assignType->get_parameters().size() == 2 );
     444                                ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->get_parameters().back() );
     445                                dcl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
     446                        }
     447                        declsToAdd.push_back( dcl );
     448                }
     449
     450                // create constructors which take each member type as a parameter.
     451                // for example, for struct A { int x, y; }; generate
     452                //   void ?{}(A *, int) and void ?{}(A *, int, int)
     453                // Field constructors are only generated if default and copy constructor
     454                // are generated, since they need access to both
     455                if ( numCtors == 2 ) {
     456                        FunctionType * memCtorType = genDefaultType( refType );
     457                        cloneAll( typeParams, memCtorType->get_forall() );
     458                        for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
     459                                DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
     460                                assert( member );
     461                                if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) {
     462                                        // don't make a function whose parameter is an unnamed bitfield
     463                                        continue;
     464                                } else if ( member->get_name() == "" ) {
     465                                        // don't assign to anonymous members
     466                                        // xxx - this is a temporary fix. Anonymous members tie into
     467                                        // our inheritance model. I think the correct way to handle this is to
     468                                        // cast the structure to the type of the member and let the resolver
     469                                        // figure out whether it's valid/choose the correct unnamed member
     470                                        continue;
     471                                }
     472                                memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), Type::StorageClasses(), LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
     473                                FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
     474                                makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor );
     475                                declsToAdd.push_back( ctor );
     476                        }
     477                        delete memCtorType;
     478                }
     479        }
    461480
    462481        /// generate a single union assignment expression (using memcpy)
    463482        template< typename OutputIterator >
    464         void UnionFuncGenerator::makeMemberOp( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ) {
     483        void makeUnionFieldsAssignment( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ) {
    465484                UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
    466                 copy->args.push_back( new AddressExpr( new VariableExpr( dstParam ) ) );
    467                 copy->args.push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
    468                 copy->args.push_back( new SizeofExpr( srcParam->get_type()->clone() ) );
     485                copy->get_args().push_back( new AddressExpr( new VariableExpr( dstParam ) ) );
     486                copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
     487                copy->get_args().push_back( new SizeofExpr( srcParam->get_type()->clone() ) );
    469488                *out++ = new ExprStmt( noLabels, copy );
    470489        }
    471490
    472491        /// generates the body of a union assignment/copy constructor/field constructor
    473         void UnionFuncGenerator::genFuncBody( FunctionDecl * funcDecl ) {
    474                 FunctionType * ftype = funcDecl->type;
    475                 if ( InitTweak::isCopyConstructor( funcDecl ) || InitTweak::isAssignment( funcDecl ) ) {
    476                         assert( ftype->parameters.size() == 2 );
    477                         ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() );
    478                         ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.back() );
    479                         makeMemberOp( srcParam, dstParam, back_inserter( funcDecl->statements->kids ) );
    480                 } else {
    481                         // default ctor/dtor body is empty - add unused attribute to parameter to silence warnings
    482                         assert( ftype->parameters.size() == 1 );
    483                         ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() );
    484                         dstParam->attributes.push_back( new Attribute( "unused" ) );
    485                 }
    486         }
    487 
    488         /// generate the body of a constructor which takes parameters that match fields, e.g.
    489         /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
    490         void UnionFuncGenerator::genFieldCtors() {
    491                 // field ctors are only generated if default constructor and copy constructor are both generated
    492                 unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
    493 
    494                 // Field constructors are only generated if default and copy constructor
    495                 // are generated, since they need access to both
    496                 if ( numCtors != 2 ) return;
     492        void makeUnionAssignBody( FunctionDecl * funcDecl ) {
     493                FunctionType * ftype = funcDecl->get_functionType();
     494                assert( ftype->get_parameters().size() == 2 );
     495                ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().front() );
     496                ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->get_parameters().back() );
     497
     498                makeUnionFieldsAssignment( srcParam, dstParam, back_inserter( funcDecl->get_statements()->get_kids() ) );
     499                if ( CodeGen::isAssignment( funcDecl->get_name() ) ) {
     500                        // also generate return statement in assignment
     501                        funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
     502                }
     503        }
     504
     505        /// generates union constructors, destructors, and assignment operator
     506        void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
     507                // Make function polymorphic in same parameters as generic union, if applicable
     508                const std::list< TypeDecl* > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
     509
     510                // default ctor/dtor need only first parameter
     511                // void ?{}(T *); void ^?{}(T *);
     512                FunctionType *ctorType = genDefaultType( refType );
     513                FunctionType *dtorType = genDefaultType( refType );
     514
     515                // copy ctor needs both parameters
     516                // void ?{}(T *, T);
     517                FunctionType *copyCtorType = genCopyType( refType );
     518
     519                // assignment needs both and return value
     520                // T ?=?(T *, T);
     521                FunctionType *assignType = genAssignType( refType );
     522
     523                cloneAll( typeParams, ctorType->get_forall() );
     524                cloneAll( typeParams, dtorType->get_forall() );
     525                cloneAll( typeParams, copyCtorType->get_forall() );
     526                cloneAll( typeParams, assignType->get_forall() );
     527
     528                // add unused attribute to parameters of default constructor and destructor
     529                ctorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
     530                dtorType->get_parameters().front()->get_attributes().push_back( new Attribute( "unused" ) );
     531
     532                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
     533                // because each unit generates copies of the default routines for each aggregate.
     534                FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting );
     535                FunctionDecl *ctorDecl = genFunc( "?{}",  ctorType, functionNesting );
     536                FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting );
     537                FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting );
     538
     539                makeUnionAssignBody( assignDecl );
     540
     541                // body of assignment and copy ctor is the same
     542                makeUnionAssignBody( copyCtorDecl );
    497543
    498544                // create a constructor which takes the first member type as a parameter.
     
    500546                // void ?{}(A *, int)
    501547                // This is to mimic C's behaviour which initializes the first member of the union.
    502                 FunctionType * memCtorType = genDefaultType( type );
    503                 for ( Declaration * member : aggregateDecl->members ) {
    504                         DeclarationWithType * field = strict_dynamic_cast<DeclarationWithType *>( member );
    505                         if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
    506                                 // don't make a function whose parameter is an unnamed bitfield
     548                std::list<Declaration *> memCtors;
     549                for ( Declaration * member : aggregateDecl->get_members() ) {
     550                        if ( DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ) ) {
     551                                ObjectDecl * srcParam = new ObjectDecl( "src", Type::StorageClasses(), LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 );
     552
     553                                FunctionType * memCtorType = ctorType->clone();
     554                                memCtorType->get_parameters().push_back( srcParam );
     555                                FunctionDecl * ctor = genFunc( "?{}", memCtorType, functionNesting );
     556
     557                                makeUnionAssignBody( ctor );
     558                                memCtors.push_back( ctor );
     559                                // only generate a ctor for the first field
    507560                                break;
    508561                        }
    509                         memCtorType->parameters.push_back( new ObjectDecl( field->name, Type::StorageClasses(), LinkageSpec::Cforall, nullptr, field->get_type()->clone(), nullptr ) );
    510                         FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
    511                         ObjectDecl * srcParam = strict_dynamic_cast<ObjectDecl *>( ctor->type->parameters.back() );
    512                         srcParam->fixUniqueId();
    513                         ObjectDecl * dstParam = InitTweak::getParamThis( ctor->type );
    514                         makeMemberOp( srcParam, dstParam, back_inserter( ctor->statements->kids ) );
    515                         resolve( ctor );
    516                         // only generate one field ctor for unions
    517                         break;
    518                 }
    519                 delete memCtorType;
    520         }
    521 
    522         void EnumFuncGenerator::genFuncBody( FunctionDecl * funcDecl ) {
    523                 // xxx - Temporary: make these functions intrinsic so they codegen as C assignment.
    524                 // Really they're something of a cross between instrinsic and autogen, so should
    525                 // probably make a new linkage type
    526                 funcDecl->linkage = LinkageSpec::Intrinsic;
    527                 FunctionType * ftype = funcDecl->type;
    528                 if ( InitTweak::isCopyConstructor( funcDecl ) || InitTweak::isAssignment( funcDecl ) ) {
    529                         assert( ftype->parameters.size() == 2 );
    530                         ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() );
    531                         ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.back() );
    532 
    533                         // enum copy construct and assignment is just C-style assignment.
    534                         // this looks like a bad recursive call, but code gen will turn it into
    535                         // a C-style assignment.
    536                         // This happens before function pointer type conversion, so need to do it manually here
    537                         ApplicationExpr * callExpr = new ApplicationExpr( VariableExpr::functionPointer( funcDecl ) );
    538                         callExpr->get_args().push_back( new VariableExpr( dstParam ) );
    539                         callExpr->get_args().push_back( new VariableExpr( srcParam ) );
    540                         funcDecl->statements->push_back( new ExprStmt( noLabels, callExpr ) );
    541                 } else {
    542                         // default ctor/dtor body is empty - add unused attribute to parameter to silence warnings
    543                         assert( ftype->parameters.size() == 1 );
    544                         ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( ftype->parameters.front() );
    545                         dstParam->attributes.push_back( new Attribute( "unused" ) );
    546                 }
    547         }
    548 
    549         bool EnumFuncGenerator::shouldAutogen() const { return true; }
    550         bool EnumFuncGenerator::isConcurrentType() const { return false; }
    551         // enums do not have field constructors
    552         void EnumFuncGenerator::genFieldCtors() {}
    553 
    554         bool TypeFuncGenerator::shouldAutogen() const { return true; };
    555 
    556         void TypeFuncGenerator::genFuncBody( FunctionDecl * dcl ) {
    557                 FunctionType * ftype = dcl->type;
    558                 assertf( ftype->parameters.size() == 1 || ftype->parameters.size() == 2, "Incorrect number of parameters in autogenerated typedecl function: %zd", ftype->parameters.size() );
    559                 DeclarationWithType * dst = ftype->parameters.front();
    560                 DeclarationWithType * src = ftype->parameters.size() == 2 ? ftype->parameters.back() : nullptr;
    561                 // generate appropriate calls to member ctor, assignment
    562                 UntypedExpr * expr = new UntypedExpr( new NameExpr( dcl->name ) );
    563                 expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) );
    564                 if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) );
    565                 dcl->statements->kids.push_back( new ExprStmt( noLabels, expr ) );
    566         };
    567 
    568         // xxx - should reach in and determine if base type is concurrent?
    569         bool TypeFuncGenerator::isConcurrentType() const { return false; };
    570 
    571         // opaque types do not have field constructors
    572         void TypeFuncGenerator::genFieldCtors() {};
    573 
    574         //=============================================================================================
    575         // Visitor definitions
    576         //=============================================================================================
     562                }
     563
     564                declsToAdd.push_back( ctorDecl );
     565                declsToAdd.push_back( copyCtorDecl );
     566                declsToAdd.push_back( dtorDecl );
     567                declsToAdd.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
     568                declsToAdd.splice( declsToAdd.end(), memCtors );
     569        }
     570
    577571        AutogenerateRoutines::AutogenerateRoutines() {
    578572                // the order here determines the order that these functions are generated.
    579573                // assignment should come last since it uses copy constructor in return.
    580                 data.emplace_back( "?{}", genDefaultType );
    581                 data.emplace_back( "?{}", genCopyType );
    582                 data.emplace_back( "^?{}", genDefaultType );
    583                 data.emplace_back( "?=?", genAssignType );
    584         }
    585 
    586         void AutogenerateRoutines::previsit( EnumDecl * enumDecl ) {
    587                 // must visit children (enum constants) to add them to the indexer
    588                 if ( enumDecl->has_body() ) {
    589                         EnumInstType enumInst( Type::Qualifiers(), enumDecl->get_name() );
    590                         enumInst.set_baseEnum( enumDecl );
    591                         EnumFuncGenerator gen( &enumInst, data, functionNesting, indexer );
    592                         generateFunctions( gen, declsToAddAfter );
    593                 }
    594         }
    595 
    596         void AutogenerateRoutines::previsit( StructDecl * structDecl ) {
    597                 visit_children = false;
    598                 if ( structDecl->has_body() ) {
    599                         StructInstType structInst( Type::Qualifiers(), structDecl->name );
     574                data.push_back( FuncData( "?{}", genDefaultType, constructable ) );
     575                data.push_back( FuncData( "?{}", genCopyType, copyable ) );
     576                data.push_back( FuncData( "^?{}", genDefaultType, destructable ) );
     577                data.push_back( FuncData( "?=?", genAssignType, assignable ) );
     578        }
     579
     580        void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
     581                if ( ! enumDecl->get_members().empty() ) {
     582                        EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
     583                        // enumInst->set_baseEnum( enumDecl );
     584                        makeEnumFunctions( enumInst, functionNesting, declsToAddAfter );
     585                }
     586        }
     587
     588        void AutogenerateRoutines::visit( StructDecl *structDecl ) {
     589                if ( structDecl->has_body() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
     590                        StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
     591                        for ( TypeDecl * typeDecl : structDecl->get_parameters() ) {
     592                                // need to visit assertions so that they are added to the appropriate maps
     593                                acceptAll( typeDecl->get_assertions(), *this );
     594                                structInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
     595                        }
    600596                        structInst.set_baseStruct( structDecl );
    601                         for ( TypeDecl * typeDecl : structDecl->parameters ) {
    602                                 structInst.parameters.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl ) ) );
    603                         }
    604                         StructFuncGenerator gen( structDecl, &structInst, data, functionNesting, indexer );
    605                         generateFunctions( gen, declsToAddAfter );
     597                        makeStructFunctions( structDecl, &structInst, functionNesting, declsToAddAfter, data );
     598                        structsDone.insert( structDecl->get_name() );
    606599                } // if
    607600        }
    608601
    609         void AutogenerateRoutines::previsit( UnionDecl * unionDecl ) {
    610                 visit_children = false;
    611                 if ( unionDecl->has_body()  ) {
     602        void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
     603                if ( ! unionDecl->get_members().empty() ) {
    612604                        UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
    613605                        unionInst.set_baseUnion( unionDecl );
     
    615607                                unionInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
    616608                        }
    617                         UnionFuncGenerator gen( unionDecl, &unionInst, data, functionNesting, indexer );
    618                         generateFunctions( gen, declsToAddAfter );
     609                        makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAddAfter );
    619610                } // if
    620611        }
    621612
     613        Type * declToTypeDeclBase( Declaration * decl ) {
     614                if ( TypeDecl * td = dynamic_cast< TypeDecl * >( decl ) ) {
     615                        return td->base;
     616                }
     617                return nullptr;
     618        }
     619
    622620        // generate ctor/dtors/assign for typedecls, e.g., otype T = int *;
    623         void AutogenerateRoutines::previsit( TypeDecl * typeDecl ) {
    624                 visit_children = false;
     621        void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
    625622                if ( ! typeDecl->base ) return;
    626623
     624                // generate each of the functions based on the supplied FuncData objects
     625                std::list< FunctionDecl * > newFuncs;
     626                std::list< Declaration * > tds { typeDecl };
     627                std::list< TypeDecl * > typeParams;
    627628                TypeInstType refType( Type::Qualifiers(), typeDecl->name, typeDecl );
    628                 TypeFuncGenerator gen( typeDecl, &refType, data, functionNesting, indexer );
    629                 generateFunctions( gen, declsToAddAfter );
    630         }
    631 
    632         void AutogenerateRoutines::previsit( FunctionType *) {
     629                auto generator = makeFuncGenerator( lazy_map( tds, declToTypeDeclBase ), &refType, functionNesting, typeParams, back_inserter( newFuncs ) );
     630                for ( const FuncData & d : data ) {
     631                        generator.gen( d, false );
     632                }
     633
     634                if ( functionNesting == 0 ) {
     635                        // forward declare if top-level struct, so that
     636                        // type is complete as soon as its body ends
     637                        // Note: this is necessary if we want structs which contain
     638                        // generic (otype) structs as members.
     639                        for ( FunctionDecl * dcl : newFuncs ) {
     640                                addForwardDecl( dcl, declsToAddAfter );
     641                        }
     642                }
     643
     644                for ( FunctionDecl * dcl : newFuncs ) {
     645                        FunctionType * ftype = dcl->type;
     646                        assertf( ftype->parameters.size() == 1 || ftype->parameters.size() == 2, "Incorrect number of parameters in autogenerated typedecl function: %zd", ftype->parameters.size() );
     647                        DeclarationWithType * dst = ftype->parameters.front();
     648                        DeclarationWithType * src = ftype->parameters.size() == 2 ? ftype->parameters.back() : nullptr;
     649                        // generate appropriate calls to member ctor, assignment
     650                        // destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
     651                        UntypedExpr * expr = new UntypedExpr( new NameExpr( dcl->name ) );
     652                        expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) );
     653                        if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) );
     654                        dcl->statements->kids.push_back( new ExprStmt( noLabels, expr ) );
     655                        if ( CodeGen::isAssignment( dcl->get_name() ) ) {
     656                                // assignment needs to return a value
     657                                FunctionType * assignType = dcl->type;
     658                                assert( assignType->parameters.size() == 2 );
     659                                ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.back() );
     660                                dcl->statements->kids.push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
     661                        }
     662                        declsToAddAfter.push_back( dcl );
     663                }
     664        }
     665
     666        void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
     667                for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
     668                        statements.insert( i, new DeclStmt( noLabels, *decl ) );
     669                } // for
     670                declsToAdd.clear();
     671        }
     672
     673        void AutogenerateRoutines::visit( FunctionType *) {
    633674                // ensure that we don't add assignment ops for types defined as part of the function
    634                 visit_children = false;
    635         }
    636 
    637         void AutogenerateRoutines::previsit( PointerType *) {
     675        }
     676
     677        void AutogenerateRoutines::visit( PointerType *) {
    638678                // ensure that we don't add assignment ops for types defined as part of the pointer
    639                 visit_children = false;
    640         }
    641 
    642         void AutogenerateRoutines::previsit( TraitDecl * ) {
     679        }
     680
     681        void AutogenerateRoutines::visit( TraitDecl *) {
    643682                // ensure that we don't add assignment ops for types defined as part of the trait
    644                 visit_children = false;
    645         }
    646 
    647         void AutogenerateRoutines::previsit( FunctionDecl * functionDecl ) {
    648                 visit_children = false;
     683        }
     684
     685        template< typename StmtClass >
     686        inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
     687                std::set< std::string > oldStructs = structsDone;
     688                addVisit( stmt, *this );
     689                structsDone = oldStructs;
     690        }
     691
     692        void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
    649693                // record the existence of this function as appropriate
    650                 managedTypes.handleDWT( functionDecl );
    651 
    652                 maybeAccept( functionDecl->type, *visitor );
     694                insert( functionDecl, constructable, InitTweak::isDefaultConstructor );
     695                insert( functionDecl, assignable, InitTweak::isAssignment );
     696                insert( functionDecl, copyable, InitTweak::isCopyConstructor );
     697                insert( functionDecl, destructable, InitTweak::isDestructor );
     698
     699                maybeAccept( functionDecl->get_functionType(), *this );
    653700                functionNesting += 1;
    654                 maybeAccept( functionDecl->statements, *visitor );
     701                maybeAccept( functionDecl->get_statements(), *this );
    655702                functionNesting -= 1;
    656703        }
    657704
    658         void AutogenerateRoutines::previsit( CompoundStmt * ) {
    659                 GuardScope( managedTypes );
    660                 GuardScope( structsDone );
     705        void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
     706                constructable.beginScope();
     707                assignable.beginScope();
     708                copyable.beginScope();
     709                destructable.beginScope();
     710                visitStatement( compoundStmt );
     711                constructable.endScope();
     712                assignable.endScope();
     713                copyable.endScope();
     714                destructable.endScope();
     715        }
     716
     717        void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
     718                visitStatement( switchStmt );
    661719        }
    662720
     
    676734        }
    677735
    678         void AutogenTupleRoutines::postvisit( TupleType * tupleType ) {
     736        Type * AutogenTupleRoutines::mutate( TupleType * tupleType ) {
     737                tupleType = strict_dynamic_cast< TupleType * >( Parent::mutate( tupleType ) );
    679738                std::string mangleName = SymTab::Mangler::mangleType( tupleType );
    680                 if ( seenTuples.find( mangleName ) != seenTuples.end() ) return;
     739                if ( seenTuples.find( mangleName ) != seenTuples.end() ) return tupleType;
    681740                seenTuples.insert( mangleName );
    682741
     
    696755                        if ( TypeInstType * ty = dynamic_cast< TypeInstType * >( t ) ) {
    697756                                if ( ! done.count( ty->get_baseType() ) ) {
    698                                         TypeDecl * newDecl = new TypeDecl( ty->get_baseType()->get_name(), Type::StorageClasses(), nullptr, TypeDecl::Dtype, true );
     757                                        TypeDecl * newDecl = new TypeDecl( ty->get_baseType()->get_name(), Type::StorageClasses(), nullptr, TypeDecl::Any );
    699758                                        TypeInstType * inst = new TypeInstType( Type::Qualifiers(), newDecl->get_name(), newDecl );
    700759                                        newDecl->get_assertions().push_back( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, genAssignType( inst ), nullptr,
     
    726785                makeTupleFunctionBody( dtorDecl );
    727786
    728                 declsToAddBefore.push_back( ctorDecl );
    729                 declsToAddBefore.push_back( copyCtorDecl );
    730                 declsToAddBefore.push_back( dtorDecl );
    731                 declsToAddBefore.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
    732         }
    733 
    734         void AutogenTupleRoutines::previsit( FunctionDecl *functionDecl ) {
    735                 visit_children = false;
    736                 maybeAccept( functionDecl->type, *visitor );
     787                addDeclaration( ctorDecl );
     788                addDeclaration( copyCtorDecl );
     789                addDeclaration( dtorDecl );
     790                addDeclaration( assignDecl ); // assignment should come last since it uses copy constructor in return
     791
     792                return tupleType;
     793        }
     794
     795        DeclarationWithType * AutogenTupleRoutines::mutate( FunctionDecl *functionDecl ) {
     796                functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );
    737797                functionNesting += 1;
    738                 maybeAccept( functionDecl->statements, *visitor );
     798                functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
    739799                functionNesting -= 1;
    740         }
    741 
    742         void AutogenTupleRoutines::previsit( CompoundStmt * ) {
    743                 GuardScope( seenTuples );
     800                return functionDecl;
     801        }
     802
     803        CompoundStmt * AutogenTupleRoutines::mutate( CompoundStmt *compoundStmt ) {
     804                seenTuples.beginScope();
     805                compoundStmt = strict_dynamic_cast< CompoundStmt * >( Parent::mutate( compoundStmt ) );
     806                seenTuples.endScope();
     807                return compoundStmt;
    744808        }
    745809} // SymTab
Note: See TracChangeset for help on using the changeset viewer.