Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    raa19ccf r8a34677  
    205205                        ObjectDecl *makeTemporary( Type *type );
    206206
    207                         ScopedMap< std::string, DeclarationWithType *> assignOps;    ///< Currently known type variable assignment operators
     207                        std::map< 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
     
    292292                        /// adds type parameters to the layout call; will generate the appropriate parameters if needed
    293293                        void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams );
    294 
    295                         /// Enters a new scope for type-variables, adding the type variables from ty
    296                         void beginTypeScope( Type *ty );
    297                         /// Exits the type-variable scope
    298                         void endTypeScope();
    299294                       
    300295                        ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName
     
    390385                for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
    391386                        TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
    392                         std::string paramName = mangleType( &paramType );
    393                         layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
    394                         layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
     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 ) );
    395389                }
    396390        }
    397391
    398392        /// Builds a layout function declaration
    399         FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {
     393        FunctionDecl *buildLayoutFunctionDecl( const std::string &typeName, unsigned int functionNesting, FunctionType *layoutFnType ) {
    400394                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
    401395                // because each unit generates copies of the default routines for each aggregate.
    402396                FunctionDecl *layoutDecl = new FunctionDecl(
    403                         layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
     397                        "__layoutof_" + typeName, functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
    404398                layoutDecl->fixUniqueId();
    405399                return layoutDecl;
     
    468462                PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
    469463               
    470                 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
     464                ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
    471465                layoutFnType->get_parameters().push_back( sizeParam );
    472                 ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
     466                ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
    473467                layoutFnType->get_parameters().push_back( alignParam );
    474                 ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
     468                ObjectDecl *offsetParam = new ObjectDecl( "__offsetof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
    475469                layoutFnType->get_parameters().push_back( offsetParam );
    476470                addOtypeParams( layoutFnType, otypeParams );
    477471
    478472                // build function decl
    479                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType );
     473                FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl->get_name(), functionNesting, layoutFnType );
    480474
    481475                // calculate struct layout in function body
     
    529523                PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
    530524               
    531                 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
     525                ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
    532526                layoutFnType->get_parameters().push_back( sizeParam );
    533                 ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
     527                ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
    534528                layoutFnType->get_parameters().push_back( alignParam );
    535529                addOtypeParams( layoutFnType, otypeParams );
    536530
    537531                // build function decl
    538                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType );
     532                FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl->get_name(), functionNesting, layoutFnType );
    539533
    540534                // calculate union layout in function body
     
    669663                        if ( functionDecl->get_statements() ) {         // empty routine body ?
    670664                                doBeginScope();
    671                                 scopeTyVars.beginScope();
    672                                 assignOps.beginScope();
     665                                TyVarMap oldtyVars = scopeTyVars;
     666                                std::map< std::string, DeclarationWithType *> oldassignOps = assignOps;
    673667                                DeclarationWithType *oldRetval = retval;
    674668                                bool oldUseRetval = useRetval;
     
    711705                                functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
    712706
    713                                 scopeTyVars.endScope();
    714                                 assignOps.endScope();
     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";
    715714                                retval = oldRetval;
    716715                                useRetval = oldUseRetval;
     
    746745
    747746                void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
    748                         Type *polyType = isPolyType( parmType, exprTyVars );
    749                         if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
    750                                 std::string typeName = mangleType( polyType );
    751                                 if ( seenTypes.count( typeName ) ) return;
     747                        Type *polyBase = hasPolyBase( parmType, exprTyVars );
     748                        if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
     749                                std::string sizeName = sizeofName( polyBase );
     750                                if ( seenTypes.count( sizeName ) ) return;
    752751
    753752                                arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
     
    755754                                arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) );
    756755                                arg++;
    757                                 if ( dynamic_cast< StructInstType* >( polyType ) ) {
     756                                if ( dynamic_cast< StructInstType* >( polyBase ) ) {
    758757                                        if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) {
    759758                                                // zero-length arrays are forbidden by C, so don't pass offset for empty struct
     
    767766                                }
    768767
    769                                 seenTypes.insert( typeName );
     768                                seenTypes.insert( sizeName );
    770769                        }
    771770                }
     
    11251124                                addAssign->get_args().push_back( appExpr->get_args().front() );
    11261125                        } // if
    1127                         addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) );
     1126                        addAssign->get_args().push_back( new NameExpr( sizeofName( polyType ) ) );
    11281127                        addAssign->get_results().front() = appExpr->get_results().front()->clone();
    11291128                        if ( appExpr->get_env() ) {
     
    11521151                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    11531152                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    1154                                                         multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
     1153                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
    11551154                                                        ret->get_args().push_back( appExpr->get_args().front() );
    11561155                                                        ret->get_args().push_back( multiply );
     
    11581157                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    11591158                                                        multiply->get_args().push_back( appExpr->get_args().front() );
    1160                                                         multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
     1159                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
    11611160                                                        ret->get_args().push_back( multiply );
    11621161                                                        ret->get_args().push_back( appExpr->get_args().back() );
     
    12211220                                                        UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
    12221221                                                        divide->get_args().push_back( appExpr );
    1223                                                         divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
     1222                                                        divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
    12241223                                                        divide->get_results().push_front( appExpr->get_results().front()->clone() );
    12251224                                                        if ( appExpr->get_env() ) {
     
    12311230                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    12321231                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    1233                                                         multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
     1232                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
    12341233                                                        appExpr->get_args().back() = multiply;
    12351234                                                } else if ( baseType2 ) {
    12361235                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    12371236                                                        multiply->get_args().push_back( appExpr->get_args().front() );
    1238                                                         multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
     1237                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
    12391238                                                        appExpr->get_args().front() = multiply;
    12401239                                                } // if
     
    12461245                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    12471246                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    1248                                                         multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) );
     1247                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) );
    12491248                                                        appExpr->get_args().back() = multiply;
    12501249                                                } // if
     
    12831282                        std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
    12841283
    1285                         TyVarMap exprTyVars( (TypeDecl::Kind)-1 );
     1284                        TyVarMap exprTyVars;
    12861285                        makeTyVarMap( function, exprTyVars );
    12871286                        ReferenceToType *polyRetType = isPolyRet( function );
     
    13061305
    13071306                        boxParams( appExpr, function, arg, exprTyVars );
     1307
    13081308                        passAdapters( appExpr, function, exprTyVars );
    13091309
     
    13851385                                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) {
    13861386                                        // find assignment operator for type variable
    1387                                         ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
     1387                                        std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
    13881388                                        if ( assignIter == assignOps.end() ) {
    13891389                                                throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
     
    14191419                                                DeclarationWithType *assertAssign = 0;
    14201420                                                if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) {
    1421                                                         ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );
     1421                                                        std::map< std::string, DeclarationWithType *>::const_iterator assertAssignIt = assignOps.find( formalTypeInstType->get_name() );
    14221422                                                        if ( assertAssignIt == assignOps.end() ) {
    14231423                                                                throw SemanticError( "No assignment operation found for ", formalTypeInstType );
     
    14601460
    14611461                Type * Pass1::mutate( PointerType *pointerType ) {
    1462                         scopeTyVars.beginScope();
     1462                        TyVarMap oldtyVars = scopeTyVars;
    14631463                        makeTyVarMap( pointerType, scopeTyVars );
    14641464
    14651465                        Type *ret = Mutator::mutate( pointerType );
    14661466
    1467                         scopeTyVars.endScope();
     1467                        scopeTyVars = oldtyVars;
    14681468                        return ret;
    14691469                }
    14701470
    14711471                Type * Pass1::mutate( FunctionType *functionType ) {
    1472                         scopeTyVars.beginScope();
     1472                        TyVarMap oldtyVars = scopeTyVars;
    14731473                        makeTyVarMap( functionType, scopeTyVars );
    14741474
    14751475                        Type *ret = Mutator::mutate( functionType );
    14761476
    1477                         scopeTyVars.endScope();
     1477                        scopeTyVars = oldtyVars;
    14781478                        return ret;
    14791479                }
     
    15401540
    15411541                Type * Pass2::mutate( PointerType *pointerType ) {
    1542                         scopeTyVars.beginScope();
     1542                        TyVarMap oldtyVars = scopeTyVars;
    15431543                        makeTyVarMap( pointerType, scopeTyVars );
    15441544
    15451545                        Type *ret = Mutator::mutate( pointerType );
    15461546
    1547                         scopeTyVars.endScope();
     1547                        scopeTyVars = oldtyVars;
    15481548                        return ret;
    15491549                }
    15501550
    15511551                Type *Pass2::mutate( FunctionType *funcType ) {
    1552                         scopeTyVars.beginScope();
     1552                        TyVarMap oldtyVars = scopeTyVars;
    15531553                        makeTyVarMap( funcType, scopeTyVars );
    15541554
     
    15721572                                if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
    15731573                                        TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
    1574                                         std::string parmName = mangleType( &parmType );
    15751574
    15761575                                        sizeParm = newObj.clone();
    1577                                         sizeParm->set_name( sizeofName( parmName ) );
     1576                                        sizeParm->set_name( sizeofName( &parmType ) );
    15781577                                        last = funcType->get_parameters().insert( last, sizeParm );
    15791578                                        ++last;
    15801579
    15811580                                        alignParm = newObj.clone();
    1582                                         alignParm->set_name( alignofName( parmName ) );
     1581                                        alignParm->set_name( alignofName( &parmType ) );
    15831582                                        last = funcType->get_parameters().insert( last, alignParm );
    15841583                                        ++last;
     
    15951594                        std::set< std::string > seenTypes; // sizeofName for generic types we've seen
    15961595                        for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) {
    1597                                 Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars );
    1598                                 if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
    1599                                         std::string typeName = mangleType( polyType );
    1600                                         if ( seenTypes.count( typeName ) ) continue;
     1596                                Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars );
     1597                                if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
     1598                                        std::string sizeName = sizeofName( polyBase );
     1599                                        if ( seenTypes.count( sizeName ) ) continue;
    16011600
    16021601                                        ObjectDecl *sizeParm, *alignParm, *offsetParm;
    16031602                                        sizeParm = newObj.clone();
    1604                                         sizeParm->set_name( sizeofName( typeName ) );
     1603                                        sizeParm->set_name( sizeName );
    16051604                                        last = funcType->get_parameters().insert( last, sizeParm );
    16061605                                        ++last;
    16071606
    16081607                                        alignParm = newObj.clone();
    1609                                         alignParm->set_name( alignofName( typeName ) );
     1608                                        alignParm->set_name( alignofName( polyBase ) );
    16101609                                        last = funcType->get_parameters().insert( last, alignParm );
    16111610                                        ++last;
    16121611
    1613                                         if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyType ) ) {
     1612                                        if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyBase ) ) {
    16141613                                                // NOTE zero-length arrays are illegal in C, so empty structs have no offset array
    16151614                                                if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) {
    16161615                                                        offsetParm = newPtr.clone();
    1617                                                         offsetParm->set_name( offsetofName( typeName ) );
     1616                                                        offsetParm->set_name( offsetofName( polyBase ) );
    16181617                                                        last = funcType->get_parameters().insert( last, offsetParm );
    16191618                                                        ++last;
     
    16211620                                        }
    16221621
    1623                                         seenTypes.insert( typeName );
     1622                                        seenTypes.insert( sizeName );
    16241623                                }
    16251624                        }
     
    16311630                        mutateAll( funcType->get_parameters(), *this );
    16321631
    1633                         scopeTyVars.endScope();
     1632                        scopeTyVars = oldtyVars;
    16341633                        return funcType;
    16351634                }
     
    18241823                }
    18251824
    1826 ////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
    1827 
    1828                 void PolyGenericCalculator::beginTypeScope( Type *ty ) {
    1829                         scopeTyVars.beginScope();
    1830                         makeTyVarMap( ty, scopeTyVars );
    1831                 }
    1832 
    1833                 void PolyGenericCalculator::endTypeScope() {
    1834                         scopeTyVars.endScope();
    1835                 }
     1825////////////////////////////////////////// MemberExprFixer ////////////////////////////////////////////////////
    18361826
    18371827                template< typename DeclClass >
    18381828                DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) {
    1839                         beginTypeScope( type );
    1840                         knownLayouts.beginScope();
    1841                         knownOffsets.beginScope();
     1829                        TyVarMap oldtyVars = scopeTyVars;
     1830                        makeTyVarMap( type, scopeTyVars );
    18421831
    18431832                        DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
    18441833
    1845                         knownOffsets.endScope();
    1846                         knownLayouts.endScope();
    1847                         endTypeScope();
     1834                        scopeTyVars = oldtyVars;
    18481835                        return ret;
    18491836                }
     
    18671854
    18681855                Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
    1869                         beginTypeScope( pointerType );
     1856                        TyVarMap oldtyVars = scopeTyVars;
     1857                        makeTyVarMap( pointerType, scopeTyVars );
    18701858
    18711859                        Type *ret = Mutator::mutate( pointerType );
    18721860
    1873                         endTypeScope();
     1861                        scopeTyVars = oldtyVars;
    18741862                        return ret;
    18751863                }
    18761864
    18771865                Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
    1878                         beginTypeScope( funcType );
     1866                        TyVarMap oldtyVars = scopeTyVars;
     1867                        makeTyVarMap( funcType, scopeTyVars );
    18791868
    18801869                        // make sure that any type information passed into the function is accounted for
    18811870                        for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) {
    18821871                                // condition here duplicates that in Pass2::mutate( FunctionType* )
    1883                                 Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars );
    1884                                 if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
    1885                                         knownLayouts.insert( mangleType( polyType ) );
     1872                                Type *polyBase = hasPolyBase( (*fnParm)->get_type(), scopeTyVars );
     1873                                if ( polyBase && ! dynamic_cast< TypeInstType* >( polyBase ) ) {
     1874                                        knownLayouts.insert( sizeofName( polyBase ) );
    18861875                                }
    18871876                        }
     
    18891878                        Type *ret = Mutator::mutate( funcType );
    18901879
    1891                         endTypeScope();
     1880                        scopeTyVars = oldtyVars;
    18921881                        return ret;
    18931882                }
     
    19001889                                        Type *declType = objectDecl->get_type();
    19011890                                        UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
    1902                                         alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) );
     1891                                        alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) );
    19031892
    19041893                                        delete objectDecl->get_init();
     
    19321921                        ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) );
    19331922                        UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
    1934                         fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) );
     1923                        fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) );
    19351924                        fieldOffset->get_args().push_back( fieldIndex );
    19361925                        return fieldOffset;
     
    20061995                                if ( findGeneric( *param ) ) {
    20071996                                        // push size/align vars for a generic parameter back
    2008                                         std::string paramName = mangleType( *param );
    2009                                         layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) );
    2010                                         layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) );
     1997                                        layoutCall->get_args().push_back( new NameExpr( sizeofName( *param ) ) );
     1998                                        layoutCall->get_args().push_back( new NameExpr( alignofName( *param ) ) );
    20111999                                } else {
    20122000                                        layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) );
     
    20522040                        } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) {
    20532041                                // check if this type already has a layout generated for it
    2054                                 std::string typeName = mangleType( ty );
    2055                                 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
     2042                                std::string sizeName = sizeofName( ty );
     2043                                if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true;
    20562044
    20572045                                // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
     
    20602048
    20612049                                // insert local variables for layout and generate call to layout function
    2062                                 knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call
     2050                                knownLayouts.insert( sizeName );  // done early so as not to interfere with the later addition of parameters to the layout call
    20632051                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    20642052
     
    20662054                                if ( n_members == 0 ) {
    20672055                                        // all empty structs have the same layout - size 1, align 1
    2068                                         makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from( (unsigned long)1 ) ) ) );
    2069                                         makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from( (unsigned long)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 ) ) ) );
    20702058                                        // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array
    20712059                                } else {
    2072                                         ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
    2073                                         ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
    2074                                         ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from( n_members ) ), false, false ) );
     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 ) );
    20752063
    20762064                                        // generate call to layout function
    2077                                         UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) );
     2065                                        UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + structTy->get_baseStruct()->get_name() ) );
    20782066                                        layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) );
    20792067                                        layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) );
     
    20872075                        } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) {
    20882076                                // check if this type already has a layout generated for it
    2089                                 std::string typeName = mangleType( ty );
    2090                                 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
     2077                                std::string sizeName = sizeofName( ty );
     2078                                if ( knownLayouts.find( sizeName ) != knownLayouts.end() ) return true;
    20912079
    20922080                                // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
     
    20952083
    20962084                                // insert local variables for layout and generate call to layout function
    2097                                 knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call
     2085                                knownLayouts.insert( sizeName );  // done early so as not to interfere with the later addition of parameters to the layout call
    20982086                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    20992087
    2100                                 ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
    2101                                 ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
     2088                                ObjectDecl *sizeVar = makeVar( sizeName, layoutType );
     2089                                ObjectDecl *alignVar = makeVar( alignofName( ty ), layoutType->clone() );
    21022090
    21032091                                // generate call to layout function
    2104                                 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) );
     2092                                UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( "__layoutof_" + unionTy->get_baseUnion()->get_name() ) );
    21052093                                layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) );
    21062094                                layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) );
     
    21182106                        Type *ty = sizeofExpr->get_type();
    21192107                        if ( findGeneric( ty ) ) {
    2120                                 Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) );
     2108                                Expression *ret = new NameExpr( sizeofName( ty ) );
    21212109                                delete sizeofExpr;
    21222110                                return ret;
     
    21282116                        Type *ty = alignofExpr->get_type();
    21292117                        if ( findGeneric( ty ) ) {
    2130                                 Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) );
     2118                                Expression *ret = new NameExpr( alignofName( ty ) );
    21312119                                delete alignofExpr;
    21322120                                return ret;
     
    21662154                        if ( findGeneric( ty ) ) {
    21672155                                // pull offset back from generated type information
    2168                                 ret = new NameExpr( offsetofName( mangleType( ty ) ) );
     2156                                ret = new NameExpr( offsetofName( ty ) );
    21692157                        } else {
    2170                                 std::string offsetName = offsetofName( mangleType( ty ) );
     2158                                std::string offsetName = offsetofName( ty );
    21712159                                if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) {
    21722160                                        // use the already-generated offsets for this type
     
    22082196                void PolyGenericCalculator::doEndScope() {
    22092197                        knownLayouts.endScope();
    2210                         knownOffsets.endScope();
     2198                        knownOffsets.beginScope();
    22112199                }
    22122200
     
    22152203                template< typename DeclClass >
    22162204                DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
    2217                         scopeTyVars.beginScope();
     2205                        TyVarMap oldtyVars = scopeTyVars;
    22182206                        makeTyVarMap( type, scopeTyVars );
    22192207
     
    22212209                        ScrubTyVars::scrub( decl, scopeTyVars );
    22222210
    2223                         scopeTyVars.endScope();
     2211                        scopeTyVars = oldtyVars;
    22242212                        return ret;
    22252213                }
     
    22512239
    22522240                Type * Pass3::mutate( PointerType *pointerType ) {
    2253                         scopeTyVars.beginScope();
     2241                        TyVarMap oldtyVars = scopeTyVars;
    22542242                        makeTyVarMap( pointerType, scopeTyVars );
    22552243
    22562244                        Type *ret = Mutator::mutate( pointerType );
    22572245
    2258                         scopeTyVars.endScope();
     2246                        scopeTyVars = oldtyVars;
    22592247                        return ret;
    22602248                }
    22612249
    22622250                Type * Pass3::mutate( FunctionType *functionType ) {
    2263                         scopeTyVars.beginScope();
     2251                        TyVarMap oldtyVars = scopeTyVars;
    22642252                        makeTyVarMap( functionType, scopeTyVars );
    22652253
    22662254                        Type *ret = Mutator::mutate( functionType );
    22672255
    2268                         scopeTyVars.endScope();
     2256                        scopeTyVars = oldtyVars;
    22692257                        return ret;
    22702258                }
Note: See TracChangeset for help on using the changeset viewer.