Ignore:
Timestamp:
Nov 1, 2022, 10:06:40 PM (18 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
7cf8006
Parents:
0bdfcc3 (diff), 03c56f6 (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r0bdfcc3 re50d9cb8  
    6868                /// Adds layout-generation functions to polymorphic types.
    6969                class LayoutFunctionBuilder final : public WithDeclsToAdd, public WithVisitorRef<LayoutFunctionBuilder>, public WithShortCircuiting {
    70                         // Current level of nested functions:
    71                         unsigned int functionNesting = 0;
    7270                public:
    73                         void previsit( FunctionDecl *functionDecl );
    7471                        void previsit( StructDecl *structDecl );
    7572                        void previsit( UnionDecl *unionDecl );
     
    237234        ////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////
    238235
    239         void LayoutFunctionBuilder::previsit( FunctionDecl *functionDecl ) {
    240                 visit_children = false;
    241                 maybeAccept( functionDecl->get_functionType(), *visitor );
    242                 ++functionNesting;
    243                 maybeAccept( functionDecl->get_statements(), *visitor );
    244                 --functionNesting;
    245         }
    246 
    247236        /// Get a list of type declarations that will affect a layout function
    248237        std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) {
     
    271260
    272261        /// Builds a layout function declaration
    273         FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {
     262        FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, bool isInFunction, FunctionType *layoutFnType ) {
    274263                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
    275264                // because each unit generates copies of the default routines for each aggregate.
    276265                FunctionDecl *layoutDecl = new FunctionDecl( layoutofName( typeDecl ),
    277                                                                                                          functionNesting > 0 ? Type::StorageClasses() : Type::StorageClasses( Type::Static ),
     266                                                                                                         isInFunction ? Type::StorageClasses() : Type::StorageClasses( Type::Static ),
    278267                                                                                                         LinkageSpec::AutoGen, layoutFnType, new CompoundStmt(),
    279268                                                                                                         std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) );
     
    347336
    348337                // build function decl
    349                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType );
     338                FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, isInFunction(), layoutFnType );
    350339
    351340                // calculate struct layout in function body
     
    354343                addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant::from_ulong( 0 ) ) ) );
    355344                addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant::from_ulong( 1 ) ) ) );
    356                 unsigned long n_members = 0;
    357                 bool firstMember = true;
    358                 for ( Declaration* member : structDecl->get_members() ) {
    359                         DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( member );
     345                for ( auto index_member : enumerate( structDecl->members ) ) {
     346                        DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( index_member.val );
    360347                        assert( dwt );
    361348                        Type *memberType = dwt->get_type();
    362349
    363                         if ( firstMember ) {
    364                                 firstMember = false;
    365                         } else {
     350                        if ( 0 < index_member.idx ) {
    366351                                // make sure all members after the first (automatically aligned at 0) are properly padded for alignment
    367352                                addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) );
     
    369354
    370355                        // place current size in the current offset index
    371                         addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from_ulong( n_members ) ) ),
     356                        addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from_ulong( index_member.idx ) ) ),
    372357                                                                              derefVar( sizeParam ) ) );
    373                         ++n_members;
    374358
    375359                        // add member size to current size
     
    406390
    407391                // build function decl
    408                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType );
     392                FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, isInFunction(), layoutFnType );
    409393
    410394                // calculate union layout in function body
     
    566550                                if ( tyParam.second.isComplete ) {
    567551                                        Type *concrete = env->lookup( tyParam.first );
    568                                         if ( concrete ) {
    569                                                 arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
    570                                                 arg++;
    571                                                 arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
    572                                                 arg++;
    573                                         } else {
    574                                                 // xxx - should this be an assertion?
    575                                                 SemanticError( appExpr, toString( *env, "\nunbound type variable: ", tyParam.first, " in application " ) );
    576                                         } // if
     552                                        // If there is an unbound type variable, it should have detected already.
     553                                        assertf( concrete, "Unbound type variable: %s in: %s",
     554                                                toCString( tyParam.first ), toCString( *env ) );
     555
     556                                        arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
     557                                        arg++;
     558                                        arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
     559                                        arg++;
    577560                                } // if
    578561                        } // for
     
    638621
    639622                void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) {
    640                         for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
    641                                 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
     623                        for ( Expression * const param : params ) {
     624                                TypeExpr *paramType = dynamic_cast< TypeExpr* >( param );
    642625                                assertf(paramType, "Aggregate parameters should be type expressions");
    643626                                paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) );
     
    692675                }
    693676
     677                // find instances of polymorphic type parameters
     678                struct PolyFinder {
     679                        const TyVarMap * tyVars = nullptr;
     680                        bool found = false;
     681
     682                        void previsit( TypeInstType * t ) {
     683                                if ( isPolyType( t, *tyVars ) ) {
     684                                        found = true;
     685                                }
     686                        }
     687                };
     688
     689                // true if there is an instance of a polymorphic type parameter in t
     690                bool hasPolymorphism( Type * t, const TyVarMap &tyVars ) {
     691                        PassVisitor<PolyFinder> finder;
     692                        finder.pass.tyVars = &tyVars;
     693                        maybeAccept( t, finder );
     694                        return finder.pass.found;
     695                }
     696
     697                /// cast parameters to polymorphic functions so that types are replaced with
     698                /// void * if they are type parameters in the formal type.
     699                /// this gets rid of warnings from gcc.
     700                void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
     701                        // type contains polymorphism, but isn't exactly a polytype, in which case it
     702                        // has some real actual type (e.g. unsigned int) and casting to void * is wrong
     703                        if ( hasPolymorphism( formal, tyVars ) && ! isPolyType( formal, tyVars ) ) {
     704                                Type * newType = formal->clone();
     705                                newType = ScrubTyVars::scrub( newType, tyVars );
     706                                actual = new CastExpr( actual, newType );
     707                        } // if
     708                }
     709
    694710                void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
    695711                        assertf( arg->result, "arg does not have result: %s", toString( arg ).c_str() );
     712                        addCast( arg, param, exprTyVars );
    696713                        if ( ! needsBoxing( param, arg->result, exprTyVars, env ) ) return;
    697714
     
    724741                }
    725742
    726                 // find instances of polymorphic type parameters
    727                 struct PolyFinder {
    728                         const TyVarMap * tyVars = nullptr;
    729                         bool found = false;
    730 
    731                         void previsit( TypeInstType * t ) {
    732                                 if ( isPolyType( t, *tyVars ) ) {
    733                                         found = true;
    734                                 }
    735                         }
    736                 };
    737 
    738                 // true if there is an instance of a polymorphic type parameter in t
    739                 bool hasPolymorphism( Type * t, const TyVarMap &tyVars ) {
    740                         PassVisitor<PolyFinder> finder;
    741                         finder.pass.tyVars = &tyVars;
    742                         maybeAccept( t, finder );
    743                         return finder.pass.found;
    744                 }
    745 
    746                 /// cast parameters to polymorphic functions so that types are replaced with
    747                 /// void * if they are type parameters in the formal type.
    748                 /// this gets rid of warnings from gcc.
    749                 void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
    750                         // type contains polymorphism, but isn't exactly a polytype, in which case it
    751                         // has some real actual type (e.g. unsigned int) and casting to void * is wrong
    752                         if ( hasPolymorphism( formal, tyVars ) && ! isPolyType( formal, tyVars ) ) {
    753                                 Type * newType = formal->clone();
    754                                 newType = ScrubTyVars::scrub( newType, tyVars );
    755                                 actual = new CastExpr( actual, newType );
    756                         } // if
    757                 }
    758 
    759743                void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
    760                         for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->parameters.end(); ++param, ++arg ) {
    761                                 assertf( arg != appExpr->args.end(), "boxParams: missing argument for param %s to %s in %s", toString( *param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
    762                                 addCast( *arg, (*param)->get_type(), exprTyVars );
    763                                 boxParam( (*param)->get_type(), *arg, exprTyVars );
     744                        for ( DeclarationWithType * param : function->parameters ) {
     745                                assertf( arg != appExpr->args.end(), "boxParams: missing argument for param %s to %s in %s", toString( param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
     746                                boxParam( param->get_type(), *arg, exprTyVars );
     747                                ++arg;
    764748                        } // for
    765749                }
     
    767751                void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
    768752                        std::list< Expression *>::iterator cur = arg;
    769                         for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
    770                                 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->assertions.begin(); assert != (*tyVar)->assertions.end(); ++assert ) {
    771                                         InferredParams::const_iterator inferParam = appExpr->inferParams.find( (*assert)->get_uniqueId() );
    772                                         assertf( inferParam != appExpr->inferParams.end(), "addInferredParams missing inferred parameter: %s in: %s", toString( *assert ).c_str(), toString( appExpr ).c_str() );
     753                        for ( TypeDecl * const tyVar : functionType->forall ) {
     754                                for ( DeclarationWithType * const assert : tyVar->assertions ) {
     755                                        InferredParams::const_iterator inferParam = appExpr->inferParams.find( assert->get_uniqueId() );
     756                                        assertf( inferParam != appExpr->inferParams.end(), "addInferredParams missing inferred parameter: %s in: %s", toString( assert ).c_str(), toString( appExpr ).c_str() );
    773757                                        Expression *newExpr = inferParam->second.expr->clone();
    774                                         addCast( newExpr, (*assert)->get_type(), tyVars );
    775                                         boxParam( (*assert)->get_type(), newExpr, tyVars );
     758                                        boxParam( assert->get_type(), newExpr, tyVars );
    776759                                        appExpr->get_args().insert( cur, newExpr );
    777760                                } // for
     
    803786                        assert( param );
    804787                        assert( arg );
    805                         if ( isPolyType( realParam->get_type(), tyVars ) ) {
    806                                 if ( ! isPolyType( arg->get_type() ) ) {
    807                                         UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    808                                         deref->args.push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
    809                                         deref->result = arg->get_type()->clone();
    810                                         return deref;
    811                                 } // if
     788                        if ( isPolyType( realParam->get_type(), tyVars )
     789                                        && ! isPolyType( arg->get_type() ) ) {
     790                                UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
     791                                deref->args.push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
     792                                deref->result = arg->get_type()->clone();
     793                                return deref;
    812794                        } // if
    813795                        return new VariableExpr( param );
     
    11451127                }
    11461128
    1147                 Expression * Pass1::postmutate( UntypedExpr *expr ) {
     1129                bool isPolyDeref( UntypedExpr * expr, TyVarMap const & scopeTyVars, TypeSubstitution const * env ) {
    11481130                        if ( expr->result && isPolyType( expr->result, scopeTyVars, env ) ) {
    11491131                                if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->function ) ) {
    11501132                                        if ( name->name == "*?" ) {
    1151                                                 Expression *ret = expr->args.front();
    1152                                                 expr->args.clear();
    1153                                                 delete expr;
    1154                                                 return ret;
     1133                                                return true;
    11551134                                        } // if
    11561135                                } // if
    11571136                        } // if
     1137                        return false;
     1138                }
     1139
     1140                Expression * Pass1::postmutate( UntypedExpr *expr ) {
     1141                        if ( isPolyDeref( expr, scopeTyVars, env ) ) {
     1142                                Expression *ret = expr->args.front();
     1143                                expr->args.clear();
     1144                                delete expr;
     1145                                return ret;
     1146                        }
    11581147                        return expr;
    11591148                }
     
    11651154                        bool needs = false;
    11661155                        if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->arg ) ) {
    1167                                 if ( expr->result && isPolyType( expr->result, scopeTyVars, env ) ) {
    1168                                         if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->function ) ) {
    1169                                                 if ( name->name == "*?" ) {
    1170                                                         if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->args.front() ) ) {
    1171                                                                 assert( appExpr->function->result );
    1172                                                                 FunctionType *function = getFunctionType( appExpr->function->result );
    1173                                                                 assert( function );
    1174                                                                 needs = needsAdapter( function, scopeTyVars );
    1175                                                         } // if
    1176                                                 } // if
     1156                                if ( isPolyDeref( expr, scopeTyVars, env ) ) {
     1157                                        if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->args.front() ) ) {
     1158                                                assert( appExpr->function->result );
     1159                                                FunctionType *function = getFunctionType( appExpr->function->result );
     1160                                                assert( function );
     1161                                                needs = needsAdapter( function, scopeTyVars );
    11771162                                        } // if
    11781163                                } // if
     
    12261211                        std::list< DeclarationWithType *> &paramList = functionType->parameters;
    12271212                        std::list< FunctionType *> functions;
    1228                         for (  DeclarationWithType * const arg : functionType->parameters ) {
     1213                        for ( DeclarationWithType * const arg : functionType->parameters ) {
    12291214                                Type *orig = arg->get_type();
    12301215                                findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter );
     
    14471432
    14481433                        if(!expect_func_type) {
    1449                                 GuardAction( [this]() {
    1450                                         knownLayouts.endScope();
    1451                                         knownOffsets.endScope();
    1452                                 });
    14531434                                // If this is the first function type we see
    14541435                                // Then it's the type of the declaration and we care about it
    1455                                 knownLayouts.beginScope();
    1456                                 knownOffsets.beginScope();
     1436                                GuardScope( *this );
    14571437                        }
    14581438
Note: See TracChangeset for help on using the changeset viewer.