- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/CandidateFinder.cpp
r5eb3f65 rd3aa55e9 698 698 void postvisit( const ast::CountExpr * countExpr ); 699 699 700 const ast::Expr * makeEnumOffsetCast( const ast::EnumInstType * src,701 const ast::EnumInstType * dst702 , const ast::Expr * expr, Cost minCost );703 704 700 void postvisit( const ast::InitExpr * ) { 705 701 assertf( false, "CandidateFinder should never see a resolved InitExpr." ); … … 1213 1209 } 1214 1210 1215 // src is a subset of dst1216 const ast::Expr * Finder::makeEnumOffsetCast( const ast::EnumInstType * src,1217 const ast::EnumInstType * dst,1218 const ast::Expr * expr,1219 Cost minCost ) {1220 1221 auto srcDecl = src->base;1222 auto dstDecl = dst->base;1223 1224 if (srcDecl->name == dstDecl->name) return expr;1225 1226 for (auto& dstChild: dstDecl->inlinedDecl) {1227 Cost c = castCost(src, dstChild, false, symtab, tenv);1228 ast::CastExpr * castToDst;1229 if (c<minCost) {1230 unsigned offset = dstDecl->calChildOffset(dstChild.get());1231 if (offset > 0) {1232 auto untyped = ast::UntypedExpr::createCall(1233 expr->location,1234 "?+?",1235 { new ast::CastExpr( expr, new ast::BasicType(ast::BasicKind::SignedInt) ),1236 ast::ConstantExpr::from_int(expr->location, offset)});1237 CandidateFinder finder(context, tenv);1238 finder.find( untyped );1239 CandidateList winners = findMinCost( finder.candidates );1240 CandidateRef & choice = winners.front();1241 // choice->expr = referenceToRvalueConversion( choice->expr, choice->cost );1242 choice->expr = new ast::CastExpr(expr->location, choice->expr, dstChild, ast::GeneratedFlag::ExplicitCast);1243 // castToDst = new ast::CastExpr(choice->expr, dstChild);1244 castToDst = new ast::CastExpr(1245 makeEnumOffsetCast( src, dstChild, choice->expr, minCost ),1246 dst);1247 1248 } else {1249 castToDst = new ast::CastExpr( expr, dst );1250 }1251 return castToDst;1252 }1253 }1254 SemanticError(expr, src->base->name + " is not a subtype of " + dst->base->name);1255 return nullptr;1256 }1257 1258 1211 void Finder::postvisit( const ast::CastExpr * castExpr ) { 1259 1212 ast::ptr< ast::Type > toType = castExpr->result; … … 1309 1262 auto argAsEnum = cand->expr->result.as<ast::EnumInstType>(); 1310 1263 auto toAsEnum = toType.as<ast::EnumInstType>(); 1311 if ( argAsEnum && toAsEnum && argAsEnum->name != toAsEnum->name ) { 1312 ast::ptr<ast::Expr> offsetExpr = makeEnumOffsetCast(argAsEnum, toAsEnum, cand->expr, thisCost); 1264 if ( argAsEnum && toAsEnum && argAsEnum->name != toAsEnum->name ) { 1265 CandidateFinder subFinder(context, tenv); 1266 ast::ptr<ast::Expr> offsetExpr = subFinder.makeEnumOffsetCast(argAsEnum, toAsEnum, cand->expr, thisCost); 1313 1267 cand->expr = offsetExpr; 1314 1268 } … … 2193 2147 expr->location, 2194 2148 "?+?", 2195 { new ast::CastExpr( expr, new ast::BasicType(ast::BasicKind::SignedInt) ), 2196 ast::ConstantExpr::from_int(expr->location, offset)}); 2149 { new ast::CastExpr( expr->location, 2150 expr, 2151 new ast::BasicType(ast::BasicKind::SignedInt), 2152 ast::GeneratedFlag::ExplicitCast ), 2153 ast::ConstantExpr::from_int(expr->location, offset)} ); 2197 2154 CandidateFinder finder(context, env); 2198 2155 finder.find( untyped );
Note: See TracChangeset
for help on using the changeset viewer.