Skip to content

Commit

Permalink
Minor fixes in the conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
lucteo committed Nov 28, 2019
1 parent b914d05 commit 157d946
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/SparrowFrontend/Helpers/CommonCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ NodeHandle SprFrontend::createTmpForRef(NodeHandle src, TypeWithStorage destType
src.location(), StringRef("$tmpForRef"), TypeNode::create(src.location(), srcT));
auto varRef = VarRefExp::create(src.location(), var);
auto store = MemStoreExp::create(src.location(), src, varRef);
auto cast = BitcastExp::create(src.location(), TypeNode::create(src.location(), destType), varRef);
auto cast =
BitcastExp::create(src.location(), TypeNode::create(src.location(), destType), varRef);
return NodeList::create(src.location(), fromIniList({var, store, cast}));
}

Expand Down
5 changes: 2 additions & 3 deletions src/SparrowFrontend/Helpers/SprTypeTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ Type SprFrontend::commonType(CompilationContext* context, Type t1, Type t2) {

// If there is one conversion from one type to another (and not vice-versa) take the less
// specialized type
ConversionFlags flags = flagDontAddReference;
ConversionResult c1 = g_ConvertService->checkConversion(context, t1, t2, flags);
ConversionResult c2 = g_ConvertService->checkConversion(context, t2, t1, flags);
ConversionResult c1 = g_ConvertService->checkConversion(context, t1, t2);
ConversionResult c2 = g_ConvertService->checkConversion(context, t2, t1);
if (c1 && !c2) {
return t2;
}
Expand Down
17 changes: 6 additions & 11 deletions src/SparrowFrontend/Services/Convert/ConvertServiceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ bool ConvertServiceImpl::checkConversionToConcept(ConversionResult& res,
// Case 1: concept -> concept
if (isConceptType(srcBase)) {
// Check wrapper types
bool canAddRef = (flags & flagDontAddReference) == 0;
if (!checkWrapperTypes(res, src, dest, canAddRef))
if (!checkWrapperTypes(res, src, dest))
return false;

// Iteratively search the base concept to find our dest type
Expand All @@ -183,8 +182,7 @@ bool ConvertServiceImpl::checkConversionToConcept(ConversionResult& res,
destTypeKind = typeKindData;

// Check wrapper types
bool canAddRef = (flags & flagDontAddReference) == 0;
if (!checkWrapperTypes(res, src, dest, canAddRef))
if (!checkWrapperTypes(res, src, dest))
return false;

bool isOk = false;
Expand Down Expand Up @@ -221,8 +219,7 @@ bool ConvertServiceImpl::checkDataConversion(ConversionResult& res, CompilationC
// Case 1: The datatypes have the same decl
if (dest.referredNode() == src.referredNode()) {
// Check wrapper types
bool canAddRef = (flags & flagDontAddReference) == 0;
if (!checkWrapperTypes(res, src, dest, canAddRef))
if (!checkWrapperTypes(res, src, dest))
return false;

res.addConversion(convDirect);
Expand Down Expand Up @@ -268,8 +265,7 @@ bool ConvertServiceImpl::checkDataConversion(ConversionResult& res, CompilationC
res.addConversion(convCustom, ConvAction(ActionType::customCvt, resType), sourceCode);

// Finally, check the wrapper types
bool canAddRef = (flags & flagDontAddReference) == 0;
if (!checkWrapperTypes(res, resType, dest, canAddRef))
if (!checkWrapperTypes(res, resType, dest))
return false;

return true;
Expand Down Expand Up @@ -419,7 +415,7 @@ int setPlainIfKindMissing(int kind) { return kind == 0 ? typeKindData : kind; }
} // namespace

bool ConvertServiceImpl::checkWrapperTypes(
ConversionResult& res, TypeWithStorage src, TypeWithStorage dest, bool canAddRef) {
ConversionResult& res, TypeWithStorage src, TypeWithStorage dest) {

// Analyze the two types: figure our their base type and all the wrappers
static KindStack srcKinds;
Expand Down Expand Up @@ -555,11 +551,10 @@ bool ConvertServiceImpl::checkWrapperTypes(
src = removeRef(src);
res.addConversion(convImplicit, ConvAction(ActionType::dereference, src));
}
// TODO: remove this
// Handle cases like T mut -> T or T ptr mut -> T ptr
if (Feather::isCategoryType(src) && src.numReferences() > dest.numReferences()) {
src = removeCategoryIfPresent(src);
res.addConversion(convDirect, ConvAction(ActionType::dereference, src));
// TODO (now): Should we make this convImplicit?
}
if (src != dest) {
if (needsCast)
Expand Down
3 changes: 1 addition & 2 deletions src/SparrowFrontend/Services/Convert/ConvertServiceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ struct ConvertServiceImpl : IConvertService {
//! The base types are assumed to be the same and just the wrapper types differ.
//! Add all the conversions to 'res'. Returns false if conversion is impossible.
//! The source type must be a data-like type.
bool checkWrapperTypes(
ConversionResult& res, TypeWithStorage src, TypeWithStorage dest, bool canAddRef);
bool checkWrapperTypes(ConversionResult& res, TypeWithStorage src, TypeWithStorage dest);
};

} // namespace SprFrontend
1 change: 0 additions & 1 deletion src/SparrowFrontend/Services/IConvertService.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace SprFrontend {
enum ConversionFlags {
flagsDefault = 0,
flagDontCallConversionCtor = 1,
flagDontAddReference = 2,
};

//! The interface for the service that deals with conversions between types.
Expand Down
2 changes: 1 addition & 1 deletion unittests/SparrowFrontend/TestConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ TEST_CASE_METHOD(ConvertFixture, "Conversion rules") {
RC_PRE(srcPtr != dest);
RC_LOG() << srcPtr << " -> " << dest << endl;

int flags = flagDontAddReference | flagDontCallConversionCtor;
int flags = flagDontCallConversionCtor;
auto c1 = getConvType(src, dest, ConversionFlags(flags));
RC_LOG() << " " << src << " -> " << dest << " = " << int(c1) << endl;
if (c1)
Expand Down

0 comments on commit 157d946

Please sign in to comment.