Changeset 8488c715
- Timestamp:
- Jan 22, 2016, 3:30:43 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 73a28e2
- Parents:
- 6d160d7
- Location:
- src/GenPoly
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r6d160d7 r8488c715 1290 1290 1291 1291 // get declaration for base struct, exiting early if not found 1292 VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate() ); 1292 int varDepth; 1293 VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth ); 1293 1294 if ( ! varExpr ) return memberExpr; 1294 1295 ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() ); … … 1296 1297 1297 1298 // only mutate member expressions for polymorphic types 1298 Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars ); 1299 int tyDepth; 1300 Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth ); 1299 1301 if ( ! objectType ) return memberExpr; 1300 1302 … … 1323 1325 1324 1326 // replace member expression with pointer to base plus offset 1325 // this is in a polymorphic context, so maybe keeping it as a void* is fine?1327 // get offset for field 1326 1328 std::stringstream offset_namer; 1327 1329 offset_namer << i; … … 1330 1332 fieldOffset->get_args().push_back( new NameExpr( offsetofName( objectType ) ) ); 1331 1333 fieldOffset->get_args().push_back( fieldIndex ); 1334 // build appropriately-dereferenced variable 1335 Expression *derefdVar = varExpr->clone(); 1336 for ( int i = 1; i < varDepth; ++i ) { 1337 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); 1338 derefExpr->get_args().push_back( derefdVar ); 1339 derefdVar = derefExpr; 1340 } 1341 // add offset to deref'd variable 1332 1342 UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) ); 1333 fieldLoc->get_args().push_back( memberExpr->get_aggregate());1343 fieldLoc->get_args().push_back( derefdVar ); 1334 1344 fieldLoc->get_args().push_back( fieldOffset ); 1335 1345 1336 memberExpr->set_aggregate( 0 );1337 1346 delete memberExpr; 1338 1347 return fieldLoc; -
src/GenPoly/GenPoly.cc
r6d160d7 r8488c715 127 127 } 128 128 129 Type *hasPolyBase( Type *type, const TypeSubstitution *env ) { 130 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { 131 return hasPolyBase( ptr->get_base(), env ); 132 } else if ( env ) { 133 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { 134 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 135 return hasPolyBase( newType, env ); 136 } // if 137 } // if 129 Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { 130 int dummy; 131 if ( ! levels ) { levels = &dummy; } 132 *levels = 0; 133 134 while ( true ) { 135 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { 136 type = ptr->get_base(); 137 ++(*levels); 138 } else if ( env ) { 139 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { 140 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 141 type = newType; 142 } else break; 143 } else break; 144 } else break; 138 145 } 139 146 … … 141 148 } 142 149 143 Type *hasPolyBase( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { 144 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { 145 return hasPolyBase( ptr->get_base(), tyVars, env ); 146 } else if ( env ) { 147 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { 148 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 149 return hasPolyBase( newType, tyVars, env ); 150 } // if 151 } // if 150 Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { 151 int dummy; 152 if ( ! levels ) { levels = &dummy; } 153 *levels = 0; 154 155 while ( true ) { 156 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { 157 type = ptr->get_base(); 158 ++(*levels); 159 } else if ( env ) { 160 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { 161 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 162 type = newType; 163 } else break; 164 } else break; 165 } else break; 152 166 } 153 167 … … 164 178 } 165 179 166 VariableExpr *getBaseVar( Expression *expr ) { 167 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { 168 // found the variable directly 169 return varExpr; 170 } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { 171 return getBaseVar( addressExpr->get_arg() ); 172 } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { 173 // look for compiler-inserted dereference operator 174 NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); 175 if ( ! fn || fn->get_name() != std::string("*?") ) return 0; 176 return getBaseVar( *untypedExpr->begin_args() ); 177 } else return 0; 180 VariableExpr * getBaseVar( Expression *expr, int *levels ) { 181 int dummy; 182 if ( ! levels ) { levels = &dummy; } 183 *levels = 0; 184 185 while ( true ) { 186 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { 187 return varExpr; 188 } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { 189 expr = addressExpr->get_arg(); 190 } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { 191 // look for compiler-inserted dereference operator 192 NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); 193 if ( ! fn || fn->get_name() != std::string("*?") ) return 0; 194 expr = *untypedExpr->begin_args(); 195 } else break; 196 197 ++(*levels); 198 } 199 200 return 0; 178 201 } 179 202 -
src/GenPoly/GenPoly.h
r6d160d7 r8488c715 20 20 #include <string> 21 21 #include <iostream> 22 #include <utility> 22 23 23 24 #include "SynTree/Declaration.h" … … 47 48 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 ); 48 49 49 /// returns polymorphic base type if the base type (after dereferencing any number of pointers) is a polymorphictype, NULL otherwise;50 /// will look up substitution in env if provided51 Type *hasPolyBase( Type *type, const TypeSubstitution *env = 0 );50 /// if the base type (after dereferencing N >= 0 pointers) is a polymorphic type, returns the base type, NULL otherwise; 51 /// N will be stored in levels, if provided, will look up substitution in env if provided 52 Type *hasPolyBase( Type *type, int *levels = 0, const TypeSubstitution *env = 0 ); 52 53 53 /// returns polymorphic base type if the base type (after dereferencing any number of pointers) is a polymorphic type in tyVars, NULL otherwise;54 /// will look up substitution in env if provided55 Type *hasPolyBase( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 );54 /// if the base type (after dereferencing N >= 0 pointers) is a polymorphic type in tyVars, returns the base type, NULL otherwise; 55 /// N will be stored in levels, if provided, will look up substitution in env if provided 56 Type *hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels = 0, const TypeSubstitution *env = 0 ); 56 57 57 58 /// Returns a pointer to the base FunctionType if ty is the type of a function (or pointer to one), NULL otherwise 58 59 FunctionType *getFunctionType( Type *ty ); 59 60 60 /// Returns the base variable (possibly repeatedly dereferenced) for an expression, NULL if none found 61 VariableExpr *getBaseVar( Expression *expr ); 61 /// If expr (after dereferencing N >= 0 pointers) is a variable expression, returns the variable expression, NULL otherwise; 62 /// N will be stored in levels, if provided 63 VariableExpr *getBaseVar( Expression *expr, int *levels = 0 ); 62 64 63 65 /// Adds the declarations in the forall list of type (and its pointed-to type if it's a pointer type) to `tyVarMap`
Note: See TracChangeset
for help on using the changeset viewer.