Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/InstantiateGeneric.cc

    r63db3d76 r5bf4712  
    7777                std::list< Type* > params;  ///< Instantiation parameters
    7878        };
    79        
     79
    8080        /// Maps a concrete type to the instantiated struct type, accounting for scope
    8181        class InstantiationMap {
     
    143143        /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
    144144        class Instantiate : public DeclMutator {
    145                 /// Map of (generic type, parameter list) pairs to concrete type instantiations
    146145                InstantiationMap instantiations;
    147                 /// Namer for concrete types
    148146                UniqueName typeNamer;
    149147
     
    151149                Instantiate() : DeclMutator(), instantiations(), typeNamer("_conc_") {}
    152150
     151//              virtual Declaration* mutate( StructDecl *aggregateDecl );
     152//              virtual Declaration* mutate( UnionDecl *aggregateDecl );
     153
    153154                virtual Type* mutate( StructInstType *inst );
    154155                virtual Type* mutate( UnionInstType *inst );
    155 
    156 //              virtual Expression* mutate( MemberExpr *memberExpr );
    157156               
    158157                virtual void doBeginScope();
     
    167166        /// Makes substitutions of params into baseParams; returns true if all parameters substituted for a concrete type
    168167        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 concrete
     168                bool allConcrete = true;  // will finish the substitution list even if they're not all concrete
    170169
    171170                // substitute concrete types for given parameters, and incomplete types for placeholders
     
    173172                std::list< Expression* >::const_iterator param = params.begin();
    174173                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 generics
     174                        switch ( (*baseParam)->get_kind() ) {
     175                        case TypeDecl::Any: {   // any type is a valid substitution here; complete types can be used to instantiate generics
    177176                                TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
    178177                                assert(paramType && "Aggregate parameters should be type expressions");
     
    180179                                // check that the substituted type isn't a type variable itself
    181180                                if ( dynamic_cast< TypeInstType* >( paramType->get_type() ) ) {
    182                                         allConcrete = false;
     181                                        allConcrete = false;
    183182                                }
    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       
    214211        /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out
    215212        void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs,
     
    291288                return newInst;
    292289        }
    293 
    294 //      /// Gets the base struct or union declaration for a member expression; NULL if not applicable
    295 //      AggregateDecl* getMemberBaseDecl( MemberExpr *memberExpr ) {
    296 //              // get variable for member aggregate
    297 //              VariableExpr *varExpr = dynamic_cast< VariableExpr* >( memberExpr->get_aggregate() );
    298 //              if ( ! varExpr ) return NULL;
    299 //
    300 //              // get object for variable
    301 //              ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() );
    302 //              if ( ! objectDecl ) return NULL;
    303 //
    304 //              // get base declaration from object type
    305 //              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 such
    315 //      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 MemberExpr
    324 //              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 found
    329 //              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 cast
    338 //              // this *SHOULD* be safe, I don't think anything but the void-replacements I put in for dtypes would make it past the typechecker
    339 //              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 //      }
    343290       
    344291        void Instantiate::doBeginScope() {
    345292                DeclMutator::doBeginScope();
     293                // push a new concrete type scope
    346294                instantiations.beginScope();
    347295        }
     
    349297        void Instantiate::doEndScope() {
    350298                DeclMutator::doEndScope();
     299                // pop the last concrete type scope
    351300                instantiations.endScope();
    352301        }
Note: See TracChangeset for help on using the changeset viewer.