Changes in / [c23f807:faf8857]
- Location:
- src
- Files:
-
- 7 edited
-
GenPoly/Box.cc (modified) (5 diffs)
-
GenPoly/GenPoly.cc (modified) (6 diffs)
-
GenPoly/GenPoly.h (modified) (1 diff)
-
GenPoly/ScrubTyVars.cc (modified) (2 diffs)
-
GenPoly/Specialize.cc (modified) (2 diffs)
-
SymTab/Mangler.cc (modified) (5 diffs)
-
SymTab/Mangler.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
rc23f807 rfaf8857 306 306 307 307 void Pass1::passTypeVars( ApplicationExpr *appExpr, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) { 308 // pass size/align for type variables309 308 for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) { 310 309 ResolvExpr::EqvClass eqvClass; … … 322 321 } // if 323 322 } // for 324 325 // add size/align for generic types to parameter list326 //assert( ! appExpr->get_function()->get_results().empty() );327 if ( appExpr->get_function()->get_results().empty() ) return;328 FunctionType *funcType = getFunctionType( appExpr->get_function()->get_results().front() );329 assert( funcType );330 331 std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();332 std::list< Expression* >::const_iterator fnArg = arg;333 std::set< std::string > seenTypes; //< names for generic types we've seen334 for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) {335 Type *parmType = (*fnParm)->get_type();336 if ( ! dynamic_cast< TypeInstType* >( parmType ) && isPolyType( parmType, exprTyVars ) ) {337 std::string sizeName = sizeofName( parmType );338 if ( seenTypes.count( sizeName ) ) continue;339 340 assert( ! (*fnArg)->get_results().empty() );341 Type *argType = (*fnArg)->get_results().front();342 arg = appExpr->get_args().insert( arg, new SizeofExpr( argType->clone() ) );343 arg++;344 arg = appExpr->get_args().insert( arg, new AlignofExpr( argType->clone() ) );345 arg++;346 347 seenTypes.insert( sizeName );348 }349 }350 323 } 351 324 … … 998 971 TyVarMap oldtyVars = scopeTyVars; 999 972 makeTyVarMap( funcType, scopeTyVars ); 1000 1001 // move polymorphic return type to parameter list 973 1002 974 std::string typeName; 1003 975 if ( isPolyRet( funcType, typeName ) ) { … … 1007 979 funcType->get_returnVals().pop_front(); 1008 980 } 1009 1010 // add size/align and assertions for type parameters to parameter list 981 1011 982 std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); 1012 983 std::list< DeclarationWithType *> inferredParams; … … 1036 1007 (*tyParm)->get_assertions().clear(); 1037 1008 } 1038 1039 // add size/align for generic types to parameter list1040 std::set< std::string > seenTypes; //< sizeofName for generic types we've seen1041 for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) {1042 Type *parmType = (*fnParm)->get_type();1043 if ( ! dynamic_cast< TypeInstType* >( parmType ) && isPolyType( parmType, scopeTyVars ) ) {1044 std::string sizeName = sizeofName( parmType );1045 if ( seenTypes.count( sizeName ) ) continue;1046 1047 ObjectDecl *sizeParm, *alignParm;1048 sizeParm = newObj.clone();1049 sizeParm->set_name( sizeName );1050 last = funcType->get_parameters().insert( last, sizeParm );1051 ++last;1052 1053 alignParm = newObj.clone();1054 alignParm->set_name( alignofName( parmType ) );1055 last = funcType->get_parameters().insert( last, alignParm );1056 ++last;1057 1058 seenTypes.insert( sizeName );1059 }1060 }1061 1062 // splice assertion parameters into parameter list1063 1009 funcType->get_parameters().splice( last, inferredParams ); 1064 1010 addAdapters( funcType ); -
src/GenPoly/GenPoly.cc
rc23f807 rfaf8857 68 68 69 69 namespace { 70 /// Checks a parameter list for polymorphic parameters; will substitute according to env if present 71 bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { 72 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { 73 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 74 assert(paramType && "Aggregate parameters should be type expressions"); 75 if ( isPolyType( paramType->get_type(), env ) ) return true; 76 } 77 return false; 78 } 79 80 /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present 70 /// Checks a parameter list for polymorphic parameters 81 71 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { 82 72 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { … … 88 78 } 89 79 } 90 91 Type *isPolyType( Type *type, const TypeSubstitution *env ) {92 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {93 if ( env ) {94 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {95 return isPolyType( newType, env );96 } // if97 } // if98 return type;99 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {100 if ( hasPolyParams( structType->get_parameters(), env ) ) return type;101 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {102 if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;103 }104 return 0;105 }106 80 107 81 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { … … 110 84 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 111 85 return isPolyType( newType, tyVars, env ); 112 } // if113 86 } // if 87 } // if 114 88 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { 115 89 return type; 116 }90 } 117 91 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { 118 92 if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; … … 123 97 } 124 98 125 Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {126 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {127 return isPolyType( ptr->get_base(), env );128 } else if ( env ) {129 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {130 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {131 return isPolyPtr( newType, env );132 } // if133 } // if134 } // if135 return 0;136 }137 138 99 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { 139 100 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { … … 149 110 } 150 111 151 FunctionType * getFunctionType( Type *ty ) {152 PointerType *ptrType;153 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {154 return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise155 } else {156 return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise157 }158 }159 160 112 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { 161 113 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { … … 166 118 167 119 std::string sizeofName( Type *ty ) { 168 return std::string( "_sizeof_" ) + SymTab::Mangler::mangle Type( ty);120 return std::string( "_sizeof_" ) + SymTab::Mangler::mangle( ty, false, false ); 169 121 } 170 122 171 123 std::string alignofName( Type *ty ) { 172 return std::string( "_alignof_" ) + SymTab::Mangler::mangle Type( ty);124 return std::string( "_alignof_" ) + SymTab::Mangler::mangle( ty, false, false ); 173 125 } 174 126 } // namespace GenPoly -
src/GenPoly/GenPoly.h
rc23f807 rfaf8857 37 37 38 38 /// returns polymorphic type if is polymorphic type, NULL otherwise; will look up substitution in env if provided 39 Type *isPolyType( Type *type, const TypeSubstitution *env = 0 );40 41 /// returns polymorphic type if is polymorphic type in tyVars, NULL otherwise; will look up substitution in env if provided42 39 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 ); 43 40 44 41 /// returns polymorphic type if is pointer to polymorphic type, NULL otherwise; will look up substitution in env if provided 45 Type *isPolyPtr( Type *type, const TypeSubstitution *env = 0 );46 47 /// returns polymorphic type if is pointer to polymorphic type in tyVars, NULL otherwise; will look up substitution in env if provided48 42 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 ); 49 50 /// Returns a pointer to the base FunctionType if ty is the type of a function (or pointer to one), NULL otherwise51 FunctionType * getFunctionType( Type *ty );52 43 53 44 /// Prints type variable map -
src/GenPoly/ScrubTyVars.cc
rc23f807 rfaf8857 46 46 Expression * ScrubTyVars::mutate( SizeofExpr *szeof ) { 47 47 // sizeof( T ) => _sizeof_T parameter, which is the size of T 48 if ( Type *polyType = isPolyType( szeof->get_type() ) ) {49 Expression *expr = new NameExpr( sizeofName( polyType) );48 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( szeof->get_type() ) ) { 49 Expression *expr = new NameExpr( sizeofName( typeInst ) ); 50 50 return expr; 51 51 } else { … … 56 56 Expression * ScrubTyVars::mutate( AlignofExpr *algnof ) { 57 57 // alignof( T ) => _alignof_T parameter, which is the alignment of T 58 if ( Type *polyType = isPolyType( algnof->get_type() ) ) {59 Expression *expr = new NameExpr( alignofName( polyType) );58 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( algnof->get_type() ) ) { 59 Expression *expr = new NameExpr( alignofName( typeInst ) ); 60 60 return expr; 61 61 } else { -
src/GenPoly/Specialize.cc
rc23f807 rfaf8857 17 17 18 18 #include "Specialize.h" 19 #include "GenPoly.h"20 19 #include "PolyMutator.h" 21 20 … … 88 87 } 89 88 89 /// Returns a pointer to the base FunctionType if ty is the type of a function (or pointer to one), NULL otherwise 90 FunctionType * getFunctionType( Type *ty ) { 91 PointerType *ptrType; 92 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { 93 return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise 94 } else { 95 return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise 96 } 97 } 98 90 99 /// Generates a thunk that calls `actual` with type `funType` and returns its address 91 100 Expression * Specialize::createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams ) { -
src/SymTab/Mangler.cc
rc23f807 rfaf8857 30 30 31 31 namespace SymTab { 32 std::string Mangler::mangleType( Type *ty ) { 33 Mangler mangler( false, true ); 34 maybeAccept( ty, mangler ); 35 return mangler.get_mangleName(); 36 } 37 38 Mangler::Mangler( bool mangleOverridable, bool typeMode ) 39 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ) {} 32 Mangler::Mangler( bool mangleOverridable, bool includeQualifiers ) 33 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), includeQualifiers(includeQualifiers) {} 40 34 41 35 Mangler::Mangler( const Mangler &rhs ) : mangleName() { … … 44 38 isTopLevel = rhs.isTopLevel; 45 39 mangleOverridable = rhs.mangleOverridable; 46 typeMode = rhs.typeMode;40 includeQualifiers = rhs.includeQualifiers; 47 41 } 48 42 … … 155 149 void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) { 156 150 printQualifiers( refType ); 157 158 151 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); 159 152 } 160 153 161 void Mangler::mangleGenericRef( ReferenceToType *refType, std::string prefix ) {162 printQualifiers( refType );163 164 std::ostringstream oldName( mangleName.str() );165 mangleName.clear();166 167 mangleName << prefix << refType->get_name();168 169 std::list< Expression* >& params = refType->get_parameters();170 if ( ! params.empty() ) {171 mangleName << "_";172 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) {173 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );174 assert(paramType && "Aggregate parameters should be type expressions");175 maybeAccept( paramType->get_type(), *this );176 }177 mangleName << "_";178 }179 180 oldName << mangleName.str().length() << mangleName.str();181 mangleName.str( oldName.str() );182 }183 184 154 void Mangler::visit( StructInstType *aggregateUseType ) { 185 if ( typeMode ) mangleGenericRef( aggregateUseType, "s" ); 186 else mangleRef( aggregateUseType, "s" ); 155 mangleRef( aggregateUseType, "s" ); 187 156 } 188 157 189 158 void Mangler::visit( UnionInstType *aggregateUseType ) { 190 if ( typeMode ) mangleGenericRef( aggregateUseType, "u" ); 191 else mangleRef( aggregateUseType, "u" ); 159 mangleRef( aggregateUseType, "u" ); 192 160 } 193 161 … … 239 207 void Mangler::printQualifiers( Type *type ) { 240 208 // skip if not including qualifiers 241 if ( typeMode) return;209 if ( ! includeQualifiers ) return; 242 210 243 211 if ( ! type->get_forall().empty() ) { … … 259 227 varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() ); 260 228 for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { 261 Mangler sub_mangler( mangleOverridable, typeMode);229 Mangler sub_mangler( mangleOverridable, includeQualifiers ); 262 230 sub_mangler.nextVarNum = nextVarNum; 263 231 sub_mangler.isTopLevel = false; -
src/SymTab/Mangler.h
rc23f807 rfaf8857 25 25 class Mangler : public Visitor { 26 26 public: 27 /// Mangle syntax tree object; primary interface to clients28 27 template< typename SynTreeClass > 29 static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true ); 30 /// Mangle a type name; secondary interface 31 static std::string mangleType( Type* ty ); 28 static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true, bool includeQualifiers = true ); // interface to clients 32 29 33 30 virtual void visit( ObjectDecl *declaration ); … … 54 51 bool isTopLevel; ///< Is the Mangler at the top level 55 52 bool mangleOverridable; ///< Specially mangle overridable built-in methods 56 bool typeMode; ///< Produce a unique mangled name for a type53 bool includeQualifiers; ///< Include type qualifiers in mangled name 57 54 58 Mangler( bool mangleOverridable, bool typeMode);55 Mangler( bool mangleOverridable, bool includeQualifiers ); 59 56 Mangler( const Mangler & ); 60 57 61 58 void mangleDecl( DeclarationWithType *declaration ); 62 59 void mangleRef( ReferenceToType *refType, std::string prefix ); 63 void mangleGenericRef( ReferenceToType *refType, std::string prefix );64 60 65 61 void printQualifiers( Type *type ); … … 67 63 68 64 template< typename SynTreeClass > 69 std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable ) {70 Mangler mangler( mangleOverridable, false);65 std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable, bool includeQualifiers ) { 66 Mangler mangler( mangleOverridable, includeQualifiers ); 71 67 maybeAccept( decl, mangler ); 72 68 return mangler.get_mangleName();
Note:
See TracChangeset
for help on using the changeset viewer.