Changeset d9fa60a


Ignore:
Timestamp:
Nov 16, 2016, 4:37:50 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
186fd86
Parents:
33a7b6d
Message:

moved substituion into MemberExpr? constructor, change generated tuple structs to generic structs, tuples containing type variables almost works

Location:
src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r33a7b6d rd9fa60a  
    9898                        void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes );
    9999                        /// passes extra type parameters into a polymorphic function application
    100                         void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
     100                        void passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
    101101                        /// wraps a function application with a new temporary for the out-parameter return value
    102102                        Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
     
    107107                        Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true );
    108108                        /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value
    109                         Expression *addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg );
     109                        Expression *addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *polyType, std::list< Expression *>::iterator &arg );
    110110                        Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
    111111                        void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars );
     
    769769                }
    770770
    771                 void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
     771                void Pass1::passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
    772772                        // pass size/align for type variables
    773773                        for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) {
     
    818818
    819819                Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
    820                         // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
    821                         // if ( useRetval ) {
    822                         //      assert( retval );
    823                         //      arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) );
    824                         //      arg++;
    825                         // } else {
    826 
    827820                        // Create temporary to hold return value of polymorphic function and produce that temporary as a result
    828821                        // using a comma expression.  Possibly change comma expression into statement expression "{}" for multiple
    829822                        // return values.
     823                        assert( retType );
    830824                        ObjectDecl *newObj = makeTemporary( retType->clone() );
    831825                        Expression *paramExpr = new VariableExpr( newObj );
     
    843837                        appExpr->set_env( 0 );
    844838                        return commaExpr;
    845                         // } // if
    846                         // return appExpr;
    847839                }
    848840
     
    878870                }
    879871
    880                 Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *dynType, std::list< Expression *>::iterator &arg ) {
     872                Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *dynType, std::list< Expression *>::iterator &arg ) {
    881873                        assert( env );
    882874                        Type *concrete = replaceWithConcrete( appExpr, dynType );
     
    12881280                        TyVarMap exprTyVars( (TypeDecl::Kind)-1 );
    12891281                        makeTyVarMap( function, exprTyVars );
    1290                         ReferenceToType *concRetType = dynamic_cast< ReferenceToType* >( appExpr->get_result() ); // xxx - is concRetType a good name?
    12911282                        ReferenceToType *dynRetType = isDynRet( function, exprTyVars );
     1283                        Type *concRetType = appExpr->get_result();// ?: dynRetType; // xxx - is concRetType a good name?
    12921284
    12931285                        if ( dynRetType ) {
     
    15891581                        // move polymorphic return type to parameter list
    15901582                        if ( isDynRet( funcType ) ) {
    1591                                 DeclarationWithType *ret = funcType->get_returnVals().front();
     1583                                ObjectDecl *ret = safe_dynamic_cast< ObjectDecl* >( funcType->get_returnVals().front() );
    15921584                                ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) );
    15931585                                funcType->get_parameters().push_front( ret );
    15941586                                funcType->get_returnVals().pop_front();
     1587                                ret->set_init( nullptr ); // xxx - memory leak?
    15951588                        }
    15961589
  • src/GenPoly/PolyMutator.cc

    r33a7b6d rd9fa60a  
    149149        }
    150150
     151        Expression *PolyMutator::mutate( StmtExpr * stmtExpr ) {
     152                // don't want statements from outer CompoundStmts to be added to this StmtExpr
     153                ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd );
     154                ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter );
     155
     156                // xxx - not sure if these are needed, along with appropriate assignments, but I don't think so...
     157                // ValueGuard< TyVarMap > oldScopeTyVars;
     158                // ValueGuard< TypeSubstitution * > oldEnv;
     159
     160                stmtsToAdd.clear();
     161                stmtsToAddAfter.clear();
     162
     163                stmtExpr->set_result( maybeMutate( stmtExpr->get_result(), *this ) );
     164                stmtExpr->set_statements( maybeMutate( stmtExpr->get_statements(), *this ) );
     165                return stmtExpr;
     166        }
    151167
    152168        Initializer *PolyMutator::mutate( SingleInit *singleInit ) {
     
    154170                return singleInit;
    155171        }
    156 
    157172} // namespace GenPoly
    158173
  • src/GenPoly/PolyMutator.h

    r33a7b6d rd9fa60a  
    4747
    4848                virtual Expression* mutate(UntypedExpr *untypedExpr);
     49                virtual Expression* mutate( StmtExpr *stmtExpr );
    4950
    5051                virtual Initializer* mutate(SingleInit *SingleInit);
  • src/InitTweak/FixInit.cc

    r33a7b6d rd9fa60a  
    904904                                        // insert and resolve default/copy constructor call for each field that's unhandled
    905905                                        std::list< Statement * > stmt;
    906                                         UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
    907                                         deref->get_args().push_back( new VariableExpr( thisParam ) );
     906                                        UntypedExpr * deref = UntypedExpr::createDeref( new VariableExpr( thisParam ) );
    908907
    909908                                        Expression * arg2 = 0;
  • src/ResolvExpr/AlternativeFinder.cc

    r33a7b6d rd9fa60a  
    218218                std::list< Declaration* > members;
    219219                aggInst->lookup( name, members );
    220                 TypeSubstitution sub = TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() );
    221220                for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
    222221                        if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
    223                                 MemberExpr * memExpr = new MemberExpr( dwt, expr->clone() );
    224                                 sub.apply( memExpr );
    225                                 alternatives.push_back( Alternative( memExpr, env, newCost ) );
     222                                alternatives.push_back( Alternative( new MemberExpr( dwt, expr->clone() ), env, newCost ) );
    226223                                renameTypes( alternatives.back().expr );
    227224                        } else {
  • src/SymTab/Autogen.cc

    r33a7b6d rd9fa60a  
    175175
    176176        void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isDynamicLayout, bool forward = true ) {
    177 //              if ( isDynamicLayout && src ) {
    178 //                      genericSubs.apply( src );
    179 //              }
    180 
    181177                ObjectDecl * returnVal = NULL;
    182178                if ( ! func->get_functionType()->get_returnVals().empty() ) {
     
    187183
    188184                // assign to destination (and return value if generic)
    189                 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
    190                 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
     185                UntypedExpr *derefExpr = UntypedExpr::createDeref( new VariableExpr( dstParam ) );
    191186                Expression *dstselect = new MemberExpr( field, derefExpr );
    192187                genImplicitCall( srcParam, dstselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward );
    193188
    194189                if ( isDynamicLayout && returnVal ) {
    195                         UntypedExpr *derefRet = new UntypedExpr( new NameExpr( "*?" ) );
    196                         derefRet->get_args().push_back( new VariableExpr( returnVal ) );
    197                         Expression *retselect = new MemberExpr( field, derefRet );
     190                        // xxx - there used to be a dereference on returnVal, but this seems to have been wrong?
     191                        Expression *retselect = new MemberExpr( field, new VariableExpr( returnVal ) );
    198192                        genImplicitCall( srcParam, retselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward );
    199193                } // if
  • src/SynTree/Expression.cc

    r33a7b6d rd9fa60a  
    332332}
    333333
     334namespace {
     335        TypeSubstitution makeSub( Type * t ) {
     336                if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
     337                        return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() );
     338                } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) {
     339                        return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() );
     340                } else {
     341                        assertf( false, "makeSub expects struct or union type for aggregate" );
     342                }
     343        }
     344}
     345
    334346
    335347MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
    336348                Expression( _aname ), member(_member), aggregate(_aggregate) {
    337         set_result( member->get_type()->clone() );
     349
     350        TypeSubstitution sub( makeSub( aggregate->get_result() ) );
     351        Type * res = member->get_type()->clone();
     352        sub.apply( res );
     353        set_result( res );
    338354        get_result()->set_isLvalue( true );
    339355}
  • src/Tuples/TupleExpansion.cc

    r33a7b6d rd9fa60a  
    9393                        typedef Mutator Parent;
    9494                        using Parent::mutate;
    95                        
     95
    9696                        virtual Expression * mutate( TupleExpr * tupleExpr ) override;
    9797                };
     
    218218        Type * TupleTypeReplacer::mutate( TupleType * tupleType ) {
    219219                std::string mangleName = SymTab::Mangler::mangleType( tupleType );
    220                 TupleType * newType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
     220                tupleType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
    221221                if ( ! typeMap.count( mangleName ) ) {
    222222                        // generate struct type to replace tuple type
    223223                        StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
    224224                        decl->set_body( true );
    225                         int cnt = 0;
    226                         for ( Type * t : *newType ) {
    227                                 decl->get_members().push_back( new ObjectDecl( toString("field_", cnt++), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
     225                        for ( size_t i = 0; i < tupleType->size(); ++i ) {
     226                                TypeDecl * tyParam = new TypeDecl( toString("tuple_param_", i), DeclarationNode::NoStorageClass, nullptr, TypeDecl::Any );
     227                                decl->get_members().push_back( new ObjectDecl( toString("field_", i), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, new TypeInstType( Type::Qualifiers(), tyParam->get_name(), tyParam ), nullptr ) );
     228                                decl->get_parameters().push_back( tyParam );
    228229                        }
    229230                        typeMap[mangleName] = decl;
    230231                        addDeclaration( decl );
    231232                }
    232                 Type::Qualifiers qualifiers = newType->get_qualifiers();
    233                 delete newType;
    234                 return new StructInstType( qualifiers, typeMap[mangleName] );
     233                Type::Qualifiers qualifiers = tupleType->get_qualifiers();
     234
     235                StructDecl * decl = typeMap[mangleName];
     236                StructInstType * newType = new StructInstType( qualifiers, decl );
     237                for ( Type * t : *tupleType ) {
     238                        newType->get_parameters().push_back( new TypeExpr( t->clone() ) );
     239                }
     240                delete tupleType;
     241                return newType;
    235242        }
    236243
  • src/main.cc

    r33a7b6d rd9fa60a  
    266266                OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused
    267267                Tuples::expandUniqueExpr( translationUnit );
     268                OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
     269                Tuples::expandTuples( translationUnit );
    268270
    269271                OPTPRINT("instantiateGenerics")
     
    282284                OPTPRINT( "box" )
    283285                GenPoly::box( translationUnit );
    284                 OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
    285                 Tuples::expandTuples( translationUnit );
    286286
    287287                // print tree right before code generation
  • src/tests/tupleFunction.c

    r33a7b6d rd9fa60a  
    5151}
    5252
    53 // forall(otype T | { T ?+?(T, T); })
    54 // [T, T, T] ?+?([T, T, T] x, [T, T, T] y) {
    55 //      T x1, x2, x3, y1, y2, y3;
    56 //      [x1, x2, x3] = x;
    57 //      [y1, y2, y3] = y;
    58 //      return [x1+y1, x2+y2, x3+y3];
    59 // }
     53int main() {
     54        [int, double, int] x = [777, 2.76, 8675];
     55        int x1 = 123, x3 = 456;
     56        double x2 = 999.123;
    6057
    61 int main() {
    62   [int, double, int] x = [777, 2.76, 8675];
    63   int x1 = 123, x3 = 456;
    64   double x2 = 999.123;
    65 
    66   printf("foo(...)=%d\n", foo(x1, x3, x2, (S){ 321, 654, 'Q', 3.14 }));
     58        printf("foo(...)=%d\n", foo(x1, x3, x2, (S){ 321, 654, 'Q', 3.14 }));
    6759
    6860        // call function with tuple parameter using tuple variable arg
     
    9183        [x1, x2, x3] = quux();
    9284        printf("x1=%d x2=%lg x3=%d\n", x1, x2, x3);
     85
     86        // xxx - tuples of type parameters should come out as generic types?
     87        // [x1, x2, x3] = ([(int)x1, (int)x2, (int)x3]) + ([(int)1, (int)2, (int)3]);
     88        // ([(int)x1, (int)x2, (int)x3]) + ([(int)1, (int)2, (int)3]);
     89        // printf("%d %g %d\n", x1, x2, x3);
     90
     91        // xxx - comes out the back as a cast, but should come out as a tuple expression of the first n fields cast to each of the result types
     92        // ([int, double])x;
    9393}
    9494
Note: See TracChangeset for help on using the changeset viewer.