Skip to content
Snippets Groups Projects
Commit fec5eddc authored by Julien Lopez's avatar Julien Lopez
Browse files

Fix in interaction between QIRRecordType and QIRSomeType

parent 3f301f61
No related branches found
No related tags found
No related merge requests found
......@@ -6,8 +6,6 @@ import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import qir.util.QIRException;
public class QIRRecordType extends QIRType {
private final Map<String, QIRType> fieldTypes;
private Optional<QIRType> globalRestriction;
......@@ -43,11 +41,11 @@ public class QIRRecordType extends QIRType {
return fieldTypes.get(id);
}
public final void unionWith(final QIRRecordType other) {
public final boolean unionWith(final QIRRecordType other) {
if (!globalRestriction.isPresent() || other.globalRestriction.isPresent() && other.globalRestriction.get().isSubtypeOf(globalRestriction.get()))
globalRestriction = other.globalRestriction;
else if (other.globalRestriction.isPresent() && !globalRestriction.get().isSubtypeOf(other.globalRestriction.get()))
throw new QIRException("Failed union between " + this + " and " + other);
return false;
for (Entry<String, QIRType> otherEntry : other.fieldTypes.entrySet()) {
final String otherKey = otherEntry.getKey();
final QIRType otherValue = otherEntry.getValue();
......@@ -56,10 +54,11 @@ public class QIRRecordType extends QIRType {
if (otherValue.isSubtypeOf(thisType))
fieldTypes.put(otherKey, otherValue);
else if (!thisType.isSubtypeOf(otherValue))
throw new QIRException("Failed union between " + this + " and " + other);
return false;
} else
fieldTypes.put(otherEntry.getKey(), otherEntry.getValue());
}
return true;
}
@Override
......
......@@ -2,8 +2,6 @@ package qir.types;
import java.util.Optional;
import qir.util.QIRException;
public class QIRSomeType extends QIRType {
private static int idGen = 0;
......@@ -25,18 +23,23 @@ public class QIRSomeType extends QIRType {
@Override
protected boolean isSubtypeOfSpecific(final QIRType other) {
if (other instanceof QIRRecordType) {
if (infered.isPresent()) {
if (infered.get() instanceof QIRRecordType) {
((QIRRecordType) infered.get()).unionWith((QIRRecordType) other);
return true;
} else
return false;
}
infered = Optional.of(other);
return true;
}
if (!infered.isPresent() || other.isSubtypeOf(infered.get())) {
infered = Optional.of(other);
return true;
}
if (infered.get().isSubtypeOf(other))
return true;
if (infered.get() instanceof QIRRecordType && other instanceof QIRRecordType)
try {
((QIRRecordType) infered.get()).unionWith((QIRRecordType) other);
return true;
} catch (final QIRException e) {
}
return false;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment