Changes in / [e55ca05:df2be83]


Ignore:
Location:
src/GenPoly
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    re55ca05 rdf2be83  
    205205                        ObjectDecl *makeTemporary( Type *type );
    206206
    207                         std::map< std::string, DeclarationWithType *> assignOps;     ///< Currently known type variable assignment operators
     207                        ScopedMap< std::string, DeclarationWithType *> assignOps;    ///< Currently known type variable assignment operators
    208208                        ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps;  ///< Currently known assignment operators
    209209                        ScopedMap< std::string, DeclarationWithType* > adapters;     ///< Set of adapter functions in the current scope
     
    385385                for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
    386386                        TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
    387                         layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( &paramType ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
    388                         layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( &paramType ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
     387                        std::string paramName = mangleType( &paramType );
     388                        layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
     389                        layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
    389390                }
    390391        }
    391392
    392393        /// Builds a layout function declaration
    393         FunctionDecl *buildLayoutFunctionDecl( const std::string &typeName, unsigned int functionNesting, FunctionType *layoutFnType ) {
     394        FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {
    394395                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
    395396                // because each unit generates copies of the default routines for each aggregate.
    396397                FunctionDecl *layoutDecl = new FunctionDecl(
    397                         "__layoutof_" + typeName, functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
     398                        layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
    398399                layoutDecl->fixUniqueId();
    399400                return layoutDecl;
     
    462463                PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
    463464               
    464                 ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
     465                ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
    465466                layoutFnType->get_parameters().push_back( sizeParam );
    466                 ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
     467                ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
    467468                layoutFnType->get_parameters().push_back( alignParam );
    468                 ObjectDecl *offsetParam = new ObjectDecl( "__offsetof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
     469                ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
    469470                layoutFnType->get_parameters().push_back( offsetParam );
    470471                addOtypeParams( layoutFnType, otypeParams );
    471472
    472473                // build function decl
    473                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl->get_name(), functionNesting, layoutFnType );
     474                FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType );
    474475
    475476                // calculate struct layout in function body
     
    523524                PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
    524525               
    525                 ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
     526                ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
    526527                layoutFnType->get_parameters().push_back( sizeParam );
    527                 ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
     528                ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
    528529                layoutFnType->get_parameters().push_back( alignParam );
    529530                addOtypeParams( layoutFnType, otypeParams );
    530531
    531532                // build function decl
    532                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl->get_name(), functionNesting, layoutFnType );
     533                FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType );
    533534
    534535                // calculate union layout in function body
     
    663664                        if ( functionDecl->get_statements() ) {         // empty routine body ?
    664665                                doBeginScope();
    665                                 TyVarMap oldtyVars = scopeTyVars;
    666                                 std::map< std::string, DeclarationWithType *> oldassignOps = assignOps;
     666                                scopeTyVars.beginScope();;
     667                                assignOps.beginScope();
    667668                                DeclarationWithType *oldRetval = retval;
    668669                                bool oldUseRetval = useRetval;
     
    705706                                functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
    706707
    707                                 scopeTyVars = oldtyVars;
    708                                 assignOps = oldassignOps;
    709                                 // std::cerr << "end FunctionDecl: ";
    710                                 // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
    711                                 //      std::cerr << i->first << " ";
    712                                 // }
    713                                 // std::cerr << "\n";
     708                                scopeTyVars.endScope();
     709                                assignOps.endScope();
    714710                                retval = oldRetval;
    715711                                useRetval = oldUseRetval;
     
    747743                        Type *polyBase = hasPolyBase( parmType, exprTyVars );
    748744                        if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
    749                                 std::string sizeName = sizeofName( polyBase );
    750                                 if ( seenTypes.count( sizeName ) ) return;
     745                                std::string typeName = mangleType( polyBase );
     746                                if ( seenTypes.count( typeName ) ) return;
    751747
    752748                                arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
     
    766762                                }
    767763
    768                                 seenTypes.insert( sizeName );
     764                                seenTypes.insert( typeName );
    769765                        }
    770766                }
     
    11241120                                addAssign->get_args().push_back( appExpr->get_args().front() );
    11251121                        } // if
    1126                         addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) );
     1122                        addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) );
    11271123                        addAssign->get_results().front() = appExpr->get_results().front()->clone();
    11281124                        if ( appExpr->get_env() ) {
     
    11511147                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    11521148                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    1153                                                         multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
     1149                                                        multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
    11541150                                                        ret->get_args().push_back( appExpr->get_args().front() );
    11551151                                                        ret->get_args().push_back( multiply );
     
    11571153                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    11581154                                                        multiply->get_args().push_back( appExpr->get_args().front() );
    1159                                                         multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
     1155                                                        multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
    11601156                                                        ret->get_args().push_back( multiply );
    11611157                                                        ret->get_args().push_back( appExpr->get_args().back() );
     
    12201216                                                        UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
    12211217                                                        divide->get_args().push_back( appExpr );
    1222                                                         divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
     1218                                                        divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
    12231219                                                        divide->get_results().push_front( appExpr->get_results().front()->clone() );
    12241220                                                        if ( appExpr->get_env() ) {
     
    12301226                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    12311227                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    1232                                                         multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
     1228                                                        multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
    12331229                                                        appExpr->get_args().back() = multiply;
    12341230                                                } else if ( baseType2 ) {
    12351231                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    12361232                                                        multiply->get_args().push_back( appExpr->get_args().front() );
    1237                                                         multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
     1233                                                        multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
    12381234                                                        appExpr->get_args().front() = multiply;
    12391235                                                } // if
     
    12451241                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    12461242                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    1247                                                         multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) );
     1243                                                        multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) );
    12481244                                                        appExpr->get_args().back() = multiply;
    12491245                                                } // if
     
    12821278                        std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
    12831279
    1284                         TyVarMap exprTyVars;
     1280                        TyVarMap exprTyVars( (TypeDecl::Kind)-1 );
    12851281                        makeTyVarMap( function, exprTyVars );
    12861282                        ReferenceToType *polyRetType = isPolyRet( function );
     
    13851381                                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) {
    13861382                                        // find assignment operator for type variable
    1387                                         std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
     1383                                        ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
    13881384                                        if ( assignIter == assignOps.end() ) {
    13891385                                                throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
     
    14191415                                                DeclarationWithType *assertAssign = 0;
    14201416                                                if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) {
    1421                                                         std::map< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );
     1417                                                        ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );
    14221418                                                        if ( assertAssignIt == assignOps.end() ) {
    14231419                                                                throw SemanticError( "No assignment operation found for ", formalTypeInstType );
     
    14601456
    14611457                Type * Pass1::mutate( PointerType *pointerType ) {
    1462                         TyVarMap oldtyVars = scopeTyVars;
     1458                        scopeTyVars.beginScope();
    14631459                        makeTyVarMap( pointerType, scopeTyVars );
    14641460
    14651461                        Type *ret = Mutator::mutate( pointerType );
    14661462
    1467                         scopeTyVars = oldtyVars;
     1463                        scopeTyVars.endScope();
    14681464                        return ret;
    14691465                }
    14701466
    14711467                Type * Pass1::mutate( FunctionType *functionType ) {
    1472                         TyVarMap oldtyVars = scopeTyVars;
     1468                        scopeTyVars.beginScope();
    14731469                        makeTyVarMap( functionType, scopeTyVars );
    14741470
    14751471                        Type *ret = Mutator::mutate( functionType );
    14761472
    1477                         scopeTyVars = oldtyVars;
     1473                        scopeTyVars.endScope();
    14781474                        return ret;
    14791475                }
     
    15401536
    15411537                Type * Pass2::mutate( PointerType *pointerType ) {
    1542                         TyVarMap oldtyVars = scopeTyVars;
     1538                        scopeTyVars.beginScope();
    15431539                        makeTyVarMap( pointerType, scopeTyVars );
    15441540
    15451541                        Type *ret = Mutator::mutate( pointerType );
    15461542
    1547                         scopeTyVars = oldtyVars;
     1543                        scopeTyVars.endScope();
    15481544                        return ret;
    15491545                }
    15501546
    15511547                Type *Pass2::mutate( FunctionType *funcType ) {
    1552                         TyVarMap oldtyVars = scopeTyVars;
     1548                        scopeTyVars.beginScope();
    15531549                        makeTyVarMap( funcType, scopeTyVars );
    15541550
     
    15721568                                if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
    15731569                                        TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
     1570                                        std::string parmName = mangleType( &parmType );
    15741571
    15751572                                        sizeParm = newObj.clone();
    1576                                         sizeParm->set_name( sizeofName( &parmType ) );
     1573                                        sizeParm->set_name( sizeofName( parmName ) );
    15771574                                        last = funcType->get_parameters().insert( last, sizeParm );
    15781575                                        ++last;
    15791576
    15801577                                        alignParm = newObj.clone();
    1581                                         alignParm->set_name( alignofName( &parmType ) );
     1578                                        alignParm->set_name( alignofName( parmName ) );
    15821579                                        last = funcType->get_parameters().insert( last, alignParm );
    15831580                                        ++last;
     
    15961593                                Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars );
    15971594                                if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
    1598                                         std::string sizeName = sizeofName( polyBase );
    1599                                         if ( seenTypes.count( sizeName ) ) continue;
     1595                                        std::string typeName = mangleType( polyBase );
     1596                                        if ( seenTypes.count( typeName ) ) continue;
    16001597
    16011598                                        ObjectDecl *sizeParm, *alignParm, *offsetParm;
    16021599                                        sizeParm = newObj.clone();
    1603                                         sizeParm->set_name( sizeName );
     1600                                        sizeParm->set_name( sizeofName( typeName ) );
    16041601                                        last = funcType->get_parameters().insert( last, sizeParm );
    16051602                                        ++last;
    16061603
    16071604                                        alignParm = newObj.clone();
    1608                                         alignParm->set_name( alignofName( polyBase ) );
     1605                                        alignParm->set_name( alignofName( typeName ) );
    16091606                                        last = funcType->get_parameters().insert( last, alignParm );
    16101607                                        ++last;
     
    16141611                                                if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) {
    16151612                                                        offsetParm = newPtr.clone();
    1616                                                         offsetParm->set_name( offsetofName( polyBase ) );
     1613                                                        offsetParm->set_name( offsetofName( typeName ) );
    16171614                                                        last = funcType->get_parameters().insert( last, offsetParm );
    16181615                                                        ++last;
     
    16201617                                        }
    16211618
    1622                                         seenTypes.insert( sizeName );
     1619                                        seenTypes.insert( typeName );
    16231620                                }
    16241621                        }
     
    16301627                        mutateAll( funcType->get_parameters(), *this );
    16311628
    1632                         scopeTyVars = oldtyVars;
     1629                        scopeTyVars.endScope();
    16331630                        return funcType;
    16341631                }
     
    18271824                template< typename DeclClass >
    18281825                DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) {
    1829                         TyVarMap oldtyVars = scopeTyVars;
     1826                        scopeTyVars.beginScope();
    18301827                        makeTyVarMap( type, scopeTyVars );
    18311828
    18321829                        DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
    18331830
    1834                         scopeTyVars = oldtyVars;
     1831                        scopeTyVars.endScope();
    18351832                        return ret;
    18361833                }
     
    18541851
    18551852                Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
    1856                         TyVarMap oldtyVars = scopeTyVars;
     1853                        scopeTyVars.beginScope();
    18571854                        makeTyVarMap( pointerType, scopeTyVars );
    18581855
    18591856                        Type *ret = Mutator::mutate( pointerType );
    18601857
    1861                         scopeTyVars = oldtyVars;
     1858                        scopeTyVars.endScope();
    18621859                        return ret;
    18631860                }
    18641861
    18651862                Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
    1866                         TyVarMap oldtyVars = scopeTyVars;
     1863                        scopeTyVars.beginScope();
    18671864                        makeTyVarMap( funcType, scopeTyVars );
    18681865
     
    18721869                                Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars );
    18731870                                if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
    1874                                         knownLayouts.insert( sizeofName( polyBase ) );
     1871                                        knownLayouts.insert( mangleType( polyBase ) );
    18751872                                }
    18761873                        }
     
    18781875                        Type *ret = Mutator::mutate( funcType );
    18791876
    1880                         scopeTyVars = oldtyVars;
     1877                        scopeTyVars.endScope();
    18811878                        return ret;
    18821879                }
     
    18891886                                        Type *declType = objectDecl->get_type();
    18901887                                        UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
    1891                                         alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) );
     1888                                        alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) );
    18921889
    18931890                                        delete objectDecl->get_init();
     
    19211918                        ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) );
    19221919                        UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
    1923                         fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) );
     1920                        fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) );
    19241921                        fieldOffset->get_args().push_back( fieldIndex );
    19251922                        return fieldOffset;
     
    19951992                                if ( findGeneric( *param ) ) {
    19961993                                        // push size/align vars for a generic parameter back
    1997                                         layoutCall->get_args().push_back( new NameExpr( sizeofName( *param ) ) );
    1998                                         layoutCall->get_args().push_back( new NameExpr( alignofName( *param ) ) );
     1994                                        std::string paramName = mangleType( *param );
     1995                                        layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) );
     1996                                        layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) );
    19991997                                } else {
    20001998                                        layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) );
     
    20402038                        } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) {
    20412039                                // check if this type already has a layout generated for it
    2042                                 std::string sizeName = sizeofName( ty );
    2043                                 if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true;
     2040                                std::string typeName = mangleType( ty );
     2041                                if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
    20442042
    20452043                                // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
     
    20482046
    20492047                                // insert local variables for layout and generate call to layout function
    2050                                 knownLayouts.insert( sizeName );  // done early so as not to interfere with the later addition of parameters to the layout call
     2048                                knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call
    20512049                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    20522050
     
    20542052                                if ( n_members == 0 ) {
    20552053                                        // all empty structs have the same layout - size 1, align 1
    2056                                         makeVar( sizeName, layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) );
    2057                                         makeVar( alignofName( ty ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) );
     2054                                        makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) );
     2055                                        makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) );
    20582056                                        // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array
    20592057                                } else {
    2060                                         ObjectDecl *sizeVar = makeVar( sizeName, layoutType );
    2061                                         ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() );
    2062                                         ObjectDecl *offsetVar = makeVar( offsetofName( ty ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) );
     2058                                        ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
     2059                                        ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
     2060                                        ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) );
    20632061
    20642062                                        // generate call to layout function
    2065                                         UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + structTy->get_baseStruct()->get_name() ) );
     2063                                        UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) );
    20662064                                        layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) );
    20672065                                        layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) );
     
    20752073                        } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) {
    20762074                                // check if this type already has a layout generated for it
    2077                                 std::string sizeName = sizeofName( ty );
    2078                                 if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true;
     2075                                std::string typeName = mangleType( ty );
     2076                                if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
    20792077
    20802078                                // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
     
    20832081
    20842082                                // insert local variables for layout and generate call to layout function
    2085                                 knownLayouts.insert( sizeName );  // done early so as not to interfere with the later addition of parameters to the layout call
     2083                                knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call
    20862084                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    20872085
    2088                                 ObjectDecl *sizeVar = makeVar( sizeName, layoutType );
    2089                                 ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() );
     2086                                ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
     2087                                ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
    20902088
    20912089                                // generate call to layout function
    2092                                 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + unionTy->get_baseUnion()->get_name() ) );
     2090                                UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) );
    20932091                                layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) );
    20942092                                layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) );
     
    21062104                        Type *ty = sizeofExpr->get_type();
    21072105                        if ( findGeneric( ty ) ) {
    2108                                 Expression *ret = new NameExpr( sizeofName( ty ) );
     2106                                Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) );
    21092107                                delete sizeofExpr;
    21102108                                return ret;
     
    21162114                        Type *ty = alignofExpr->get_type();
    21172115                        if ( findGeneric( ty ) ) {
    2118                                 Expression *ret = new NameExpr( alignofName( ty ) );
     2116                                Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) );
    21192117                                delete alignofExpr;
    21202118                                return ret;
     
    21542152                        if ( findGeneric( ty ) ) {
    21552153                                // pull offset back from generated type information
    2156                                 ret = new NameExpr( offsetofName( ty ) );
     2154                                ret = new NameExpr( offsetofName( mangleType( ty ) ) );
    21572155                        } else {
    2158                                 std::string offsetName = offsetofName( ty );
     2156                                std::string offsetName = offsetofName( mangleType( ty ) );
    21592157                                if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) {
    21602158                                        // use the already-generated offsets for this type
     
    21962194                void PolyGenericCalculator::doEndScope() {
    21972195                        knownLayouts.endScope();
    2198                         knownOffsets.beginScope();
     2196                        knownOffsets.endScope();
    21992197                }
    22002198
     
    22032201                template< typename DeclClass >
    22042202                DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
    2205                         TyVarMap oldtyVars = scopeTyVars;
     2203                        scopeTyVars.beginScope();
    22062204                        makeTyVarMap( type, scopeTyVars );
    22072205
     
    22092207                        ScrubTyVars::scrub( decl, scopeTyVars );
    22102208
    2211                         scopeTyVars = oldtyVars;
     2209                        scopeTyVars.endScope();
    22122210                        return ret;
    22132211                }
     
    22392237
    22402238                Type * Pass3::mutate( PointerType *pointerType ) {
    2241                         TyVarMap oldtyVars = scopeTyVars;
     2239                        scopeTyVars.beginScope();
    22422240                        makeTyVarMap( pointerType, scopeTyVars );
    22432241
    22442242                        Type *ret = Mutator::mutate( pointerType );
    22452243
    2246                         scopeTyVars = oldtyVars;
     2244                        scopeTyVars.endScope();
    22472245                        return ret;
    22482246                }
    22492247
    22502248                Type * Pass3::mutate( FunctionType *functionType ) {
    2251                         TyVarMap oldtyVars = scopeTyVars;
     2249                        scopeTyVars.beginScope();
    22522250                        makeTyVarMap( functionType, scopeTyVars );
    22532251
    22542252                        Type *ret = Mutator::mutate( functionType );
    22552253
    2256                         scopeTyVars = oldtyVars;
     2254                        scopeTyVars.endScope();
    22572255                        return ret;
    22582256                }
  • src/GenPoly/FindFunction.cc

    re55ca05 rdf2be83  
    5555                        TyVarMap::iterator var = tyVars.find( (*i)->get_name() );
    5656                        if ( var != tyVars.end() ) {
    57                                 tyVars.erase( var );
     57                                tyVars.erase( var->first );
    5858                        } // if
    5959                } // for
     
    6161
    6262        Type * FindFunction::mutate( FunctionType *functionType ) {
    63                 TyVarMap oldTyVars = tyVars;
     63                tyVars.beginScope();
    6464                handleForall( functionType->get_forall() );
    6565                mutateAll( functionType->get_returnVals(), *this );
     
    7272                        } // if
    7373                } // if
    74                 tyVars = oldTyVars;
     74                tyVars.endScope();
    7575                return ret;
    7676        }
    7777
    7878        Type * FindFunction::mutate( PointerType *pointerType ) {
    79                 TyVarMap oldTyVars = tyVars;
     79                tyVars.beginScope();
    8080                handleForall( pointerType->get_forall() );
    8181                Type *ret = Mutator::mutate( pointerType );
    82                 tyVars = oldTyVars;
     82                tyVars.endScope();
    8383                return ret;
    8484        }
  • src/GenPoly/GenPoly.cc

    re55ca05 rdf2be83  
    1616#include "GenPoly.h"
    1717
    18 #include "SymTab/Mangler.h"
    1918#include "SynTree/Expression.h"
    2019#include "SynTree/Type.h"
     
    3837        ReferenceToType *isPolyRet( FunctionType *function ) {
    3938                if ( ! function->get_returnVals().empty() ) {
    40                         TyVarMap forallTypes;
     39                        TyVarMap forallTypes( (TypeDecl::Kind)-1 );
    4140                        makeTyVarMap( function, forallTypes );
    4241                        return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
     
    218217        }
    219218
    220         std::string sizeofName( Type *ty ) {
    221                 return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );
    222         }
    223 
    224         std::string alignofName( Type *ty ) {
    225                 return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );
    226         }
    227 
    228         std::string offsetofName( Type* ty ) {
    229                 return std::string( "_offsetof_" ) + SymTab::Mangler::mangleType( ty );
    230         }
    231 
    232219} // namespace GenPoly
    233220
  • src/GenPoly/GenPoly.h

    re55ca05 rdf2be83  
    1717#define GENPOLY_H
    1818
    19 #include <map>
    2019#include <string>
    2120#include <iostream>
    2221#include <utility>
     22
     23#include "ErasableScopedMap.h"
     24
     25#include "SymTab/Mangler.h"
    2326
    2427#include "SynTree/Declaration.h"
     
    2730
    2831namespace GenPoly {
    29         typedef std::map< std::string, TypeDecl::Kind > TyVarMap;
     32        typedef ErasableScopedMap< std::string, TypeDecl::Kind > TyVarMap;
    3033
    3134        /// A function needs an adapter if it returns a polymorphic value or if any of its
     
    6972        void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap );
    7073
    71         /// Gets the name of the sizeof parameter for the type
    72         std::string sizeofName( Type *ty );
     74        /// Gets the mangled name of this type; alias for SymTab::Mangler::mangleType().
     75        inline std::string mangleType( Type *ty ) { return SymTab::Mangler::mangleType( ty ); }
     76       
     77        /// Gets the name of the sizeof parameter for the type, given its mangled name
     78        inline std::string sizeofName( const std::string &name ) { return std::string( "_sizeof_" ) + name; }
    7379
    74         /// Gets the name of the alignof parameter for the type
    75         std::string alignofName( Type *ty );
     80        /// Gets the name of the alignof parameter for the type, given its mangled name
     81        inline std::string alignofName( const std::string &name ) { return std::string( "_alignof_" ) + name; }
    7682
    77         /// Gets the name of the offsetof parameter for the type
    78         std::string offsetofName( Type *ty );
     83        /// Gets the name of the offsetof parameter for the type, given its mangled name
     84        inline std::string offsetofName( const std::string &name ) { return std::string( "_offsetof_" ) + name; }
     85
     86        /// Gets the name of the layout function for a given aggregate type, given its declaration
     87        inline std::string layoutofName( AggregateDecl *decl ) { return std::string( "_layoutof_" ) + decl->get_name(); }
     88       
    7989} // namespace GenPoly
    8090
  • src/GenPoly/PolyMutator.cc

    re55ca05 rdf2be83  
    2727        }
    2828
    29         PolyMutator::PolyMutator() : env( 0 ) {
    30         }
     29        PolyMutator::PolyMutator() : scopeTyVars( (TypeDecl::Kind)-1 ), env( 0 ) {}
    3130
    3231        void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) {
  • src/GenPoly/ScopedMap.h

    re55ca05 rdf2be83  
    5151                        typedef typename scope_list::size_type size_type;
    5252
     53                        /// Checks if this iterator points to a valid item
     54                        bool is_valid() const {
     55                                return it != (*scopes)[i].end();
     56                        }
     57
     58                        /// Increments on invalid
     59                        iterator& next_valid() {
     60                                if ( ! is_valid() ) { ++(*this); }
     61                                return *this;
     62                        }
     63
     64                        /// Decrements on invalid
     65                        iterator& prev_valid() {
     66                                if ( ! is_valid() ) { --(*this); }
     67                                return *this;
     68                        }
     69
    5370                        iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
    5471                                : scopes(&_scopes), it(_it), i(_i) {}
     
    6885                                        --i;
    6986                                        it = (*scopes)[i].begin();
    70                                         return *this;
    71                                 }
    72                                 ++it;
    73                                 return *this;
     87                                } else {
     88                                        ++it;
     89                                }
     90                                return next_valid();
    7491                        }
    7592                        iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     
    8299                                }
    83100                                --it;
    84                                 return *this;
     101                                return prev_valid();
    85102                        }
    86103                        iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     
    105122                        typedef typename scope_list::size_type size_type;
    106123
     124                        /// Checks if this iterator points to a valid item
     125                        bool is_valid() const {
     126                                return it != (*scopes)[i].end();
     127                        }
     128
     129                        /// Increments on invalid
     130                        const_iterator& next_valid() {
     131                                if ( ! is_valid() ) { ++(*this); }
     132                                return *this;
     133                        }
     134
     135                        /// Decrements on invalid
     136                        const_iterator& prev_valid() {
     137                                if ( ! is_valid() ) { --(*this); }
     138                                return *this;
     139                        }
     140
    107141                        const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
    108142                                : scopes(&_scopes), it(_it), i(_i) {}
     
    127161                                        --i;
    128162                                        it = (*scopes)[i].begin();
    129                                         return *this;
    130                                 }
    131                                 ++it;
    132                                 return *this;
     163                                } else {
     164                                        ++it;
     165                                }
     166                                return next_valid();
    133167                        }
    134168                        const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     
    141175                                }
    142176                                --it;
    143                                 return *this;
     177                                return prev_valid();
    144178                        }
    145179                        const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     
    171205                ScopedMap() { beginScope(); }
    172206
    173                 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); }
    174                 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
    175                 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
     207                iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     208                const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     209                const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    176210                iterator end() { return iterator(scopes, scopes[0].end(), 0); }
    177211                const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
  • src/GenPoly/ScopedSet.h

    re55ca05 rdf2be83  
    4848                        typedef typename scope_list::size_type size_type;
    4949
     50                        /// Checks if this iterator points to a valid item
     51                        bool is_valid() const {
     52                                return it != (*scopes)[i].end();
     53                        }
     54
     55                        /// Increments on invalid
     56                        iterator& next_valid() {
     57                                if ( ! is_valid() ) { ++(*this); }
     58                                return *this;
     59                        }
     60
     61                        /// Decrements on invalid
     62                        iterator& prev_valid() {
     63                                if ( ! is_valid() ) { --(*this); }
     64                                return *this;
     65                        }
     66
    5067                        iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
    5168                                : scopes(&_scopes), it(_it), i(_i) {}
     
    6582                                        --i;
    6683                                        it = (*scopes)[i].begin();
    67                                         return *this;
    68                                 }
    69                                 ++it;
    70                                 return *this;
     84                                } else {
     85                                        ++it;
     86                                }
     87                                return next_valid();
    7188                        }
    7289                        iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     
    7996                                }
    8097                                --it;
    81                                 return *this;
     98                                return prev_valid();
    8299                        }
    83100                        iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     
    102119                        typedef typename scope_list::size_type size_type;
    103120
     121                        /// Checks if this iterator points to a valid item
     122                        bool is_valid() const {
     123                                return it != (*scopes)[i].end();
     124                        }
     125
     126                        /// Increments on invalid
     127                        const_iterator& next_valid() {
     128                                if ( ! is_valid() ) { ++(*this); }
     129                                return *this;
     130                        }
     131
     132                        /// Decrements on invalid
     133                        const_iterator& prev_valid() {
     134                                if ( ! is_valid() ) { --(*this); }
     135                                return *this;
     136                        }
     137
    104138                        const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
    105139                                : scopes(&_scopes), it(_it), i(_i) {}
     
    124158                                        --i;
    125159                                        it = (*scopes)[i].begin();
    126                                         return *this;
    127                                 }
    128                                 ++it;
    129                                 return *this;
     160                                } else {
     161                                        ++it;
     162                                }
     163                                return next_valid();
    130164                        }
    131165                        const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     
    138172                                }
    139173                                --it;
    140                                 return *this;
     174                                return prev_valid();
    141175                        }
    142176                        const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     
    167201                ScopedSet() { beginScope(); }
    168202
    169                 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); }
    170                 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
    171                 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
     203                iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     204                const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     205                const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    172206                iterator end() { return iterator(scopes, scopes[0].end(), 0); }
    173207                const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
  • src/GenPoly/ScrubTyVars.cc

    re55ca05 rdf2be83  
    6464                // sizeof( T ) => _sizeof_T parameter, which is the size of T
    6565                if ( Type *polyType = isPolyType( szeof->get_type() ) ) {
    66                         Expression *expr = new NameExpr( sizeofName( polyType ) );
     66                        Expression *expr = new NameExpr( sizeofName( mangleType( polyType ) ) );
    6767                        return expr;
    6868                } else {
     
    7474                // alignof( T ) => _alignof_T parameter, which is the alignment of T
    7575                if ( Type *polyType = isPolyType( algnof->get_type() ) ) {
    76                         Expression *expr = new NameExpr( alignofName( polyType ) );
     76                        Expression *expr = new NameExpr( alignofName( mangleType( polyType ) ) );
    7777                        return expr;
    7878                } else {
Note: See TracChangeset for help on using the changeset viewer.