Ignore:
Timestamp:
Dec 2, 2015, 11:58:59 AM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
9cb8e88d
Parents:
9ed3237 (diff), f2b2029 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into ctor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r9ed3237 r0ddb713  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 21:50:04 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 11 16:59:35 2015
    13 // Update Count     : 196
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Fri Nov 20 16:33:52 2015
     13// Update Count     : 201
    1414//
    1515
     
    126126        };
    127127
    128         class AddStructAssignment : public Visitor {
     128        class AutogenerateRoutines : public Visitor {
    129129          public:
    130130                /// Generates assignment operators for aggregate types as required
    131                 static void addStructAssignment( std::list< Declaration * > &translationUnit );
     131                static void autogenerateRoutines( std::list< Declaration * > &translationUnit );
    132132
    133133                std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
     
    152152                virtual void visit( CatchStmt *catchStmt );
    153153
    154                 AddStructAssignment() : functionNesting( 0 ) {}
     154                AutogenerateRoutines() : functionNesting( 0 ) {}
    155155          private:
    156156                template< typename StmtClass > void visitStatement( StmtClass *stmt );
     
    196196                acceptAll( translationUnit, pass1 );
    197197                acceptAll( translationUnit, pass2 );
    198                 // need to collect all of the assignment operators prior to
    199                 // this point and only generate assignment operators if one doesn't exist
    200                 AddStructAssignment::addStructAssignment( translationUnit );
     198                AutogenerateRoutines::autogenerateRoutines( translationUnit );
    201199                acceptAll( translationUnit, pass3 );
    202200        }
     
    502500        static const std::list< std::string > noLabels;
    503501
    504         void AddStructAssignment::addStructAssignment( std::list< Declaration * > &translationUnit ) {
    505                 AddStructAssignment visitor;
     502        void AutogenerateRoutines::autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
     503                AutogenerateRoutines visitor;
    506504                acceptAndAdd( translationUnit, visitor, false );
    507505        }
     
    627625        }
    628626
     627        /// Clones a reference type, replacing any parameters it may have with a clone of the provided list
     628        template< typename GenericInstType >
     629        GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) {
     630                GenericInstType *clone = refType->clone();
     631                clone->get_parameters().clear();
     632                cloneAll( params, clone->get_parameters() );
     633                return clone;
     634        }
    629635
    630636        Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
    631637                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
    632  
    633                 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
     638
     639                // Make function polymorphic in same parameters as generic struct, if applicable
     640                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
     641                std::list< Expression* > structParams;  // List of matching parameters to put on types
     642                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
     643                        TypeDecl *typeParam = (*param)->clone();
     644                        assignType->get_forall().push_back( typeParam );
     645                        structParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
     646                }
     647 
     648                ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
    634649                assignType->get_returnVals().push_back( returnVal );
    635650 
    636                 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
     651                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );
    637652                assignType->get_parameters().push_back( dstParam );
    638653 
    639                 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
     654                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
    640655                assignType->get_parameters().push_back( srcParam );
    641656
     
    673688        Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {
    674689                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
    675  
    676                 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
     690
     691                // Make function polymorphic in same parameters as generic union, if applicable
     692                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
     693                std::list< Expression* > unionParams;  // List of matching parameters to put on types
     694                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
     695                        TypeDecl *typeParam = (*param)->clone();
     696                        assignType->get_forall().push_back( typeParam );
     697                        unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
     698                }
     699 
     700                ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
    677701                assignType->get_returnVals().push_back( returnVal );
    678702 
    679                 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
     703                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );
    680704                assignType->get_parameters().push_back( dstParam );
    681705 
    682                 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
     706                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
    683707                assignType->get_parameters().push_back( srcParam );
    684708 
     
    691715                copy->get_args().push_back( new VariableExpr( dstParam ) );
    692716                copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
    693                 copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
     717                copy->get_args().push_back( new SizeofExpr( cloneWithParams( refType, unionParams ) ) );
    694718
    695719                assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
     
    699723        }
    700724
    701         void AddStructAssignment::visit( EnumDecl *enumDecl ) {
     725        void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
    702726                if ( ! enumDecl->get_members().empty() ) {
    703727                        EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
     
    708732        }
    709733
    710         void AddStructAssignment::visit( StructDecl *structDecl ) {
     734        void AutogenerateRoutines::visit( StructDecl *structDecl ) {
    711735                if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
    712                         StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
    713                         structInst->set_baseStruct( structDecl );
    714                         declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) );
     736                        StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
     737                        structInst.set_baseStruct( structDecl );
     738                        declsToAdd.push_back( makeStructAssignment( structDecl, &structInst, functionNesting ) );
    715739                        structsDone.insert( structDecl->get_name() );
    716740                } // if
    717741        }
    718742
    719         void AddStructAssignment::visit( UnionDecl *unionDecl ) {
     743        void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
    720744                if ( ! unionDecl->get_members().empty() ) {
    721                         UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
    722                         unionInst->set_baseUnion( unionDecl );
    723                         declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) );
    724                 } // if
    725         }
    726 
    727         void AddStructAssignment::visit( TypeDecl *typeDecl ) {
     745                        UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
     746                        unionInst.set_baseUnion( unionDecl );
     747                        declsToAdd.push_back( makeUnionAssignment( unionDecl, &unionInst, functionNesting ) );
     748                } // if
     749        }
     750
     751        void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
    728752                CompoundStmt *stmts = 0;
    729753                TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
     
    753777        }
    754778
    755         void AddStructAssignment::visit( FunctionType *) {
     779        void AutogenerateRoutines::visit( FunctionType *) {
    756780                // ensure that we don't add assignment ops for types defined as part of the function
    757781        }
    758782
    759         void AddStructAssignment::visit( PointerType *) {
     783        void AutogenerateRoutines::visit( PointerType *) {
    760784                // ensure that we don't add assignment ops for types defined as part of the pointer
    761785        }
    762786
    763         void AddStructAssignment::visit( ContextDecl *) {
     787        void AutogenerateRoutines::visit( ContextDecl *) {
    764788                // ensure that we don't add assignment ops for types defined as part of the context
    765789        }
    766790
    767791        template< typename StmtClass >
    768         inline void AddStructAssignment::visitStatement( StmtClass *stmt ) {
     792        inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
    769793                std::set< std::string > oldStructs = structsDone;
    770794                addVisit( stmt, *this );
     
    772796        }
    773797
    774         void AddStructAssignment::visit( FunctionDecl *functionDecl ) {
     798        void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
    775799                maybeAccept( functionDecl->get_functionType(), *this );
    776800                acceptAll( functionDecl->get_oldDecls(), *this );
     
    780804        }
    781805
    782         void AddStructAssignment::visit( CompoundStmt *compoundStmt ) {
     806        void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
    783807                visitStatement( compoundStmt );
    784808        }
    785809
    786         void AddStructAssignment::visit( IfStmt *ifStmt ) {
     810        void AutogenerateRoutines::visit( IfStmt *ifStmt ) {
    787811                visitStatement( ifStmt );
    788812        }
    789813
    790         void AddStructAssignment::visit( WhileStmt *whileStmt ) {
     814        void AutogenerateRoutines::visit( WhileStmt *whileStmt ) {
    791815                visitStatement( whileStmt );
    792816        }
    793817
    794         void AddStructAssignment::visit( ForStmt *forStmt ) {
     818        void AutogenerateRoutines::visit( ForStmt *forStmt ) {
    795819                visitStatement( forStmt );
    796820        }
    797821
    798         void AddStructAssignment::visit( SwitchStmt *switchStmt ) {
     822        void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
    799823                visitStatement( switchStmt );
    800824        }
    801825
    802         void AddStructAssignment::visit( ChooseStmt *switchStmt ) {
     826        void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) {
    803827                visitStatement( switchStmt );
    804828        }
    805829
    806         void AddStructAssignment::visit( CaseStmt *caseStmt ) {
     830        void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
    807831                visitStatement( caseStmt );
    808832        }
    809833
    810         void AddStructAssignment::visit( CatchStmt *cathStmt ) {
     834        void AutogenerateRoutines::visit( CatchStmt *cathStmt ) {
    811835                visitStatement( cathStmt );
    812836        }
Note: See TracChangeset for help on using the changeset viewer.