Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r2efe4b8 re16294d  
    184184                        /// change the type of generic aggregate members to char[]
    185185                        void mutateMembers( AggregateDecl * aggrDecl );
     186                        /// returns the calculated sizeof expression for ty, or nullptr for use C sizeof()
     187                        Expression* genSizeof( Type* ty );
    186188
    187189                        /// Enters a new scope for type-variables, adding the type variables from ty
     
    279281        /// Adds parameters for otype layout to a function type
    280282        void addOtypeParams( FunctionType *layoutFnType, std::list< TypeDecl* > &otypeParams ) {
    281                 auto sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    282 
    283                 for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin();
    284                                 param != otypeParams.end(); ++param ) {
    285                         auto paramType = new TypeInstType( Type::Qualifiers(), (*param)->get_name(), *param );
    286                         std::string paramName = mangleType( paramType );
    287                         layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), Type::StorageClasses(), LinkageSpec::Cforall, 0, sizeAlignType->clone(), 0 ) );
    288                         layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), Type::StorageClasses(), LinkageSpec::Cforall, 0, sizeAlignType->clone(), 0 ) );
     283                BasicType sizeAlignType( Type::Qualifiers(), BasicType::LongUnsignedInt );
     284
     285                for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
     286                        TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
     287                        std::string paramName = mangleType( &paramType );
     288                        layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), Type::StorageClasses(), LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
     289                        layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), Type::StorageClasses(), LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
    289290                }
    290291        }
     
    383384                unsigned long n_members = 0;
    384385                bool firstMember = true;
    385                 for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) {
    386                         DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member );
     386                for ( Declaration* member : structDecl->get_members() ) {
     387                        DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( member );
    387388                        assert( dwt );
    388389                        Type *memberType = dwt->get_type();
     
    741742                                Type * newType = param->clone();
    742743                                if ( env ) env->apply( newType );
    743                                 ObjectDecl *newObj = ObjectDecl::newObject(
    744                                         tempNamer.newName(), newType, nullptr );
     744                                ObjectDecl *newObj = ObjectDecl::newObject( tempNamer.newName(), newType, nullptr );
    745745                                newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
    746746                                stmtsToAddBefore.push_back( new DeclStmt( newObj ) );
     
    862862                        // do not carry over attributes to real type parameters/return values
    863863                        for ( DeclarationWithType * dwt : realType->parameters ) {
     864                                deleteAll( dwt->get_type()->attributes );
    864865                                dwt->get_type()->attributes.clear();
    865866                        }
    866867                        for ( DeclarationWithType * dwt : realType->returnVals ) {
     868                                deleteAll( dwt->get_type()->attributes );
    867869                                dwt->get_type()->attributes.clear();
    868870                        }
     
    985987                        } // if
    986988                        appExpr->get_args().clear();
     989                        delete appExpr;
    987990                        return addAssign;
    988991                }
     
    10151018                                                } // if
    10161019                                                if ( baseType1 || baseType2 ) {
     1020                                                        delete ret->get_result();
    10171021                                                        ret->set_result( appExpr->get_result()->clone() );
    10181022                                                        if ( appExpr->get_env() ) {
     
    10211025                                                        } // if
    10221026                                                        appExpr->get_args().clear();
     1027                                                        delete appExpr;
    10231028                                                        return ret;
    10241029                                                } // if
     
    10301035                                                        Expression *ret = appExpr->get_args().front();
    10311036                                                        // fix expr type to remove pointer
     1037                                                        delete ret->get_result();
    10321038                                                        ret->set_result( appExpr->get_result()->clone() );
    10331039                                                        if ( appExpr->get_env() ) {
     
    10361042                                                        } // if
    10371043                                                        appExpr->get_args().clear();
     1044                                                        delete appExpr;
    10381045                                                        return ret;
    10391046                                                } // if
     
    11751182                                                Expression *ret = expr->args.front();
    11761183                                                expr->args.clear();
     1184                                                delete expr;
    11771185                                                return ret;
    11781186                                        } // if
     
    12081216                        if ( polytype || needs ) {
    12091217                                Expression *ret = addrExpr->arg;
     1218                                delete ret->result;
    12101219                                ret->result = addrExpr->result->clone();
    12111220                                addrExpr->arg = nullptr;
     1221                                delete addrExpr;
    12121222                                return ret;
    12131223                        } else {
     
    12191229                        if ( retval && returnStmt->expr ) {
    12201230                                assert( returnStmt->expr->result && ! returnStmt->expr->result->isVoid() );
     1231                                delete returnStmt->expr;
    12211232                                returnStmt->expr = nullptr;
    12221233                        } // if
     
    12611272                                }
    12621273                        }
     1274//  deleteAll( functions );
    12631275                }
    12641276
     
    12801292                        for ( Declaration * param : functionDecl->type->parameters ) {
    12811293                                if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( param ) ) {
     1294                                        delete obj->init;
    12821295                                        obj->init = nullptr;
    12831296                                }
     
    13271340                        std::list< DeclarationWithType *> inferredParams;
    13281341                        // size/align/offset parameters may not be used in body, pass along with unused attribute.
    1329                         auto newObj = new ObjectDecl(
    1330                                 "", Type::StorageClasses(), LinkageSpec::C, 0,
    1331                                 new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0,
    1332                             { new Attribute( "unused" ) } );
    1333                         auto newPtr = new ObjectDecl(
    1334                                 "", Type::StorageClasses(), LinkageSpec::C, 0,
    1335                             new PointerType( Type::Qualifiers(),
    1336                                         new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 );
    1337                         for ( Type::ForallList::const_iterator tyParm = funcType->get_forall().begin();
    1338                                         tyParm != funcType->get_forall().end(); ++tyParm ) {
     1342                        ObjectDecl newObj( "", Type::StorageClasses(), LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0,
     1343                                           { new Attribute( "unused" ) } );
     1344                        ObjectDecl newPtr( "", Type::StorageClasses(), LinkageSpec::C, 0,
     1345                                           new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 );
     1346                        for ( Type::ForallList::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
    13391347                                ObjectDecl *sizeParm, *alignParm;
    13401348                                // add all size and alignment parameters to parameter list
    13411349                                if ( (*tyParm)->isComplete() ) {
    1342                                         auto parmType = new TypeInstType(
    1343                                                 Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
    1344                                         std::string parmName = mangleType( parmType );
    1345 
    1346                                         sizeParm = newObj->clone();
     1350                                        TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
     1351                                        std::string parmName = mangleType( &parmType );
     1352
     1353                                        sizeParm = newObj.clone();
    13471354                                        sizeParm->set_name( sizeofName( parmName ) );
    13481355                                        last = funcType->get_parameters().insert( last, sizeParm );
    13491356                                        ++last;
    13501357
    1351                                         alignParm = newObj->clone();
     1358                                        alignParm = newObj.clone();
    13521359                                        alignParm->set_name( alignofName( parmName ) );
    13531360                                        last = funcType->get_parameters().insert( last, alignParm );
     
    13721379
    13731380                                        ObjectDecl *sizeParm, *alignParm, *offsetParm;
    1374                                         sizeParm = newObj->clone();
     1381                                        sizeParm = newObj.clone();
    13751382                                        sizeParm->set_name( sizeofName( typeName ) );
    13761383                                        last = funcType->get_parameters().insert( last, sizeParm );
    13771384                                        ++last;
    13781385
    1379                                         alignParm = newObj->clone();
     1386                                        alignParm = newObj.clone();
    13801387                                        alignParm->set_name( alignofName( typeName ) );
    13811388                                        last = funcType->get_parameters().insert( last, alignParm );
     
    13851392                                                // NOTE zero-length arrays are illegal in C, so empty structs have no offset array
    13861393                                                if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) {
    1387                                                         offsetParm = newPtr->clone();
     1394                                                        offsetParm = newPtr.clone();
    13881395                                                        offsetParm->set_name( offsetofName( typeName ) );
    13891396                                                        last = funcType->get_parameters().insert( last, offsetParm );
     
    14361443                        if ( Type * base = typeDecl->base ) {
    14371444                                // add size/align variables for opaque type declarations
    1438                                 auto inst = new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl );
    1439                                 std::string typeName = mangleType( inst );
     1445                                TypeInstType inst( Type::Qualifiers(), typeDecl->name, typeDecl );
     1446                                std::string typeName = mangleType( &inst );
    14401447                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    14411448
     
    14941501                                                        // polymorphic aggregate members should be converted into monomorphic members.
    14951502                                                        // Using char[size_T] here respects the expected sizing rules of an aggregate type.
    1496                                                         field->type = polyToMonoType( field->type );
     1503                                                        Type * newType = polyToMonoType( field->type );
     1504                                                        delete field->type;
     1505                                                        field->type = newType;
    14971506                                                }
    14981507                                        }
     
    15171526                                        stmtsToAddBefore.push_back( new DeclStmt( newBuf ) );
    15181527
     1528                                        delete objectDecl->get_init();
    15191529                                        objectDecl->set_init( new SingleInit( new VariableExpr( newBuf ) ) );
    15201530                                }
     
    15951605                        }
    15961606
     1607                        delete memberType;
     1608                        delete memberExpr;
    15971609                        return newMemberExpr;
    15981610                }
     
    16141626                                                addrExpr->arg = nullptr;
    16151627                                                std::swap( addrExpr->env, ret->env );
     1628                                                delete addrExpr;
    16161629                                                return ret;
    16171630                                        }
     
    16221635
    16231636                ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) {
    1624                         ObjectDecl *newObj = new ObjectDecl(
    1625                                 name, Type::StorageClasses(), LinkageSpec::C, nullptr, type, init );
     1637                        ObjectDecl *newObj = new ObjectDecl( name, Type::StorageClasses(), LinkageSpec::C, nullptr, type, init );
    16261638                        stmtsToAddBefore.push_back( new DeclStmt( newObj ) );
    16271639                        return newObj;
     
    17371749                }
    17381750
     1751                Expression * PolyGenericCalculator::genSizeof( Type* ty ) {
     1752                        if ( ArrayType * aty = dynamic_cast<ArrayType *>(ty) ) {
     1753                                // generate calculated size for possibly generic array
     1754                                Expression * sizeofBase = genSizeof( aty->get_base() );
     1755                                if ( ! sizeofBase ) return nullptr;
     1756                                Expression * dim = aty->get_dimension();
     1757                                aty->set_dimension( nullptr );
     1758                                return makeOp( "?*?", sizeofBase, dim );
     1759                        } else if ( findGeneric( ty ) ) {
     1760                                // generate calculated size for generic type
     1761                                return new NameExpr( sizeofName( mangleType( ty ) ) );
     1762                        } else return nullptr;
     1763                }
     1764
    17391765                Expression *PolyGenericCalculator::postmutate( SizeofExpr *sizeofExpr ) {
    1740                         Type *ty = sizeofExpr->get_isType() ? sizeofExpr->get_type() : sizeofExpr->get_expr()->get_result();
    1741                         if ( findGeneric( ty ) ) {
    1742                                 return new NameExpr( sizeofName( mangleType( ty ) ) );
    1743                         }
    1744                         return sizeofExpr;
     1766                        Type *ty = sizeofExpr->get_isType() ?
     1767                                sizeofExpr->get_type() : sizeofExpr->get_expr()->get_result();
     1768                       
     1769                        Expression * gen = genSizeof( ty );
     1770                        if ( gen ) {
     1771                                delete sizeofExpr;
     1772                                return gen;
     1773                        } else return sizeofExpr;
    17451774                }
    17461775
     
    17481777                        Type *ty = alignofExpr->get_isType() ? alignofExpr->get_type() : alignofExpr->get_expr()->get_result();
    17491778                        if ( findGeneric( ty ) ) {
    1750                                 return new NameExpr( alignofName( mangleType( ty ) ) );
     1779                                Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) );
     1780                                delete alignofExpr;
     1781                                return ret;
    17511782                        }
    17521783                        return alignofExpr;
     
    17631794                                if ( i == -1 ) return offsetofExpr;
    17641795
    1765                                 return makeOffsetIndex( ty, i );
     1796                                Expression *offsetInd = makeOffsetIndex( ty, i );
     1797                                delete offsetofExpr;
     1798                                return offsetInd;
    17661799                        } else if ( dynamic_cast< UnionInstType* >( ty ) ) {
    17671800                                // all union members are at offset zero
     1801                                delete offsetofExpr;
    17681802                                return new ConstantExpr( Constant::from_ulong( 0 ) );
    17691803                        } else return offsetofExpr;
     
    18051839                        }
    18061840
     1841                        delete offsetPackExpr;
    18071842                        return ret;
    18081843                }
Note: See TracChangeset for help on using the changeset viewer.