- File:
-
- 1 edited
-
src/GenPoly/InstantiateGeneric.cc (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/InstantiateGeneric.cc
r63db3d76 r5bf4712 77 77 std::list< Type* > params; ///< Instantiation parameters 78 78 }; 79 79 80 80 /// Maps a concrete type to the instantiated struct type, accounting for scope 81 81 class InstantiationMap { … … 143 143 /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately 144 144 class Instantiate : public DeclMutator { 145 /// Map of (generic type, parameter list) pairs to concrete type instantiations146 145 InstantiationMap instantiations; 147 /// Namer for concrete types148 146 UniqueName typeNamer; 149 147 … … 151 149 Instantiate() : DeclMutator(), instantiations(), typeNamer("_conc_") {} 152 150 151 // virtual Declaration* mutate( StructDecl *aggregateDecl ); 152 // virtual Declaration* mutate( UnionDecl *aggregateDecl ); 153 153 154 virtual Type* mutate( StructInstType *inst ); 154 155 virtual Type* mutate( UnionInstType *inst ); 155 156 // virtual Expression* mutate( MemberExpr *memberExpr );157 156 158 157 virtual void doBeginScope(); … … 167 166 /// Makes substitutions of params into baseParams; returns true if all parameters substituted for a concrete type 168 167 bool makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) { 169 bool allConcrete = true; // will finish the substitution list even if they're not all concrete168 bool allConcrete = true; // will finish the substitution list even if they're not all concrete 170 169 171 170 // substitute concrete types for given parameters, and incomplete types for placeholders … … 173 172 std::list< Expression* >::const_iterator param = params.begin(); 174 173 for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) { 175 //switch ( (*baseParam)->get_kind() ) {176 //case TypeDecl::Any: { // any type is a valid substitution here; complete types can be used to instantiate generics174 switch ( (*baseParam)->get_kind() ) { 175 case TypeDecl::Any: { // any type is a valid substitution here; complete types can be used to instantiate generics 177 176 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 178 177 assert(paramType && "Aggregate parameters should be type expressions"); … … 180 179 // check that the substituted type isn't a type variable itself 181 180 if ( dynamic_cast< TypeInstType* >( paramType->get_type() ) ) { 182 allConcrete = false;181 allConcrete = false; 183 182 } 184 // break; 185 // } 186 // case TypeDecl::Dtype: // dtype can be consistently replaced with void [only pointers, which become void*] 187 // out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); 188 // break; 189 // case TypeDecl::Ftype: // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype] 190 // out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); 191 // break; 192 // } 193 } 194 195 // if any parameters left over, not done 196 if ( baseParam != baseParams.end() ) return false; 197 // // if not enough parameters given, substitute remaining incomplete types for placeholders 198 // for ( ; baseParam != baseParams.end(); ++baseParam ) { 199 // switch ( (*baseParam)->get_kind() ) { 200 // case TypeDecl::Any: // no more substitutions here, fail early 201 // return false; 202 // case TypeDecl::Dtype: // dtype can be consistently replaced with void [only pointers, which become void*] 203 // out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); 204 // break; 205 // case TypeDecl::Ftype: // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype] 206 // out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); 207 // break; 208 // } 209 // } 210 211 return allConcrete; 212 } 213 183 break; 184 } 185 case TypeDecl::Dtype: // dtype can be consistently replaced with void [only pointers, which become void*] 186 out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); 187 break; 188 case TypeDecl::Ftype: // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype] 189 out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); 190 break; 191 } 192 } 193 194 // if not enough parameters given, substitute remaining incomplete types for placeholders 195 for ( ; baseParam != baseParams.end(); ++baseParam ) { 196 switch ( (*baseParam)->get_kind() ) { 197 case TypeDecl::Any: // no more substitutions here, fail early 198 return false; 199 case TypeDecl::Dtype: // dtype can be consistently replaced with void [only pointers, which become void*] 200 out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); 201 break; 202 case TypeDecl::Ftype: // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype] 203 out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); 204 break; 205 } 206 } 207 208 return allConcrete; 209 } 210 214 211 /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out 215 212 void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs, … … 291 288 return newInst; 292 289 } 293 294 // /// Gets the base struct or union declaration for a member expression; NULL if not applicable295 // AggregateDecl* getMemberBaseDecl( MemberExpr *memberExpr ) {296 // // get variable for member aggregate297 // VariableExpr *varExpr = dynamic_cast< VariableExpr* >( memberExpr->get_aggregate() );298 // if ( ! varExpr ) return NULL;299 //300 // // get object for variable301 // ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() );302 // if ( ! objectDecl ) return NULL;303 //304 // // get base declaration from object type305 // Type *objectType = objectDecl->get_type();306 // StructInstType *structType = dynamic_cast< StructInstType* >( objectType );307 // if ( structType ) return structType->get_baseStruct();308 // UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType );309 // if ( unionType ) return unionType->get_baseUnion();310 //311 // return NULL;312 // }313 //314 // /// Finds the declaration with the given name, returning decls.end() if none such315 // std::list< Declaration* >::const_iterator findDeclNamed( const std::list< Declaration* > &decls, const std::string &name ) {316 // for( std::list< Declaration* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) {317 // if ( (*decl)->get_name() == name ) return decl;318 // }319 // return decls.end();320 // }321 //322 // Expression* Instantiate::mutate( MemberExpr *memberExpr ) {323 // // mutate, exiting early if no longer MemberExpr324 // Expression *expr = Mutator::mutate( memberExpr );325 // memberExpr = dynamic_cast< MemberExpr* >( expr );326 // if ( ! memberExpr ) return expr;327 //328 // // get declaration of member and base declaration of member, exiting early if not found329 // AggregateDecl *memberBase = getMemberBaseDecl( memberExpr );330 // if ( ! memberBase ) return memberExpr;331 // DeclarationWithType *memberDecl = memberExpr->get_member();332 // std::list< Declaration* >::const_iterator baseIt = findDeclNamed( memberBase->get_members(), memberDecl->get_name() );333 // if ( baseIt == memberBase->get_members().end() ) return memberExpr;334 // DeclarationWithType *baseDecl = dynamic_cast< DeclarationWithType* >( *baseIt );335 // if ( ! baseDecl ) return memberExpr;336 //337 // // check if stated type of the member is not the type of the member's declaration; if so, need a cast338 // // this *SHOULD* be safe, I don't think anything but the void-replacements I put in for dtypes would make it past the typechecker339 // SymTab::Indexer dummy;340 // if ( ResolvExpr::typesCompatible( memberDecl->get_type(), baseDecl->get_type(), dummy ) ) return memberExpr;341 // else return new CastExpr( memberExpr, memberDecl->get_type() );342 // }343 290 344 291 void Instantiate::doBeginScope() { 345 292 DeclMutator::doBeginScope(); 293 // push a new concrete type scope 346 294 instantiations.beginScope(); 347 295 } … … 349 297 void Instantiate::doEndScope() { 350 298 DeclMutator::doEndScope(); 299 // pop the last concrete type scope 351 300 instantiations.endScope(); 352 301 }
Note:
See TracChangeset
for help on using the changeset viewer.