示例#1
0
bool Constraints::unify(Exp* x, Exp* y, ConstraintMap& extra) {
LOG << "Unifying " << x << " with " << y << " result ";
	assert(x->isTypeVal());
	assert(y->isTypeVal());
	Type* xtype = ((TypeVal*)x)->getType();
	Type* ytype = ((TypeVal*)y)->getType();
	if (xtype->isPointer() && ytype->isPointer()) {
		Type* xPointsTo = ((PointerType*)xtype)->getPointsTo();
		Type* yPointsTo = ((PointerType*)ytype)->getPointsTo();
		if (((PointerType*)xtype)->pointsToAlpha() ||
			((PointerType*)ytype)->pointsToAlpha()) {
			// A new constraint: xtype must be equal to ytype; at least
			// one of these is a variable type
			if (((PointerType*)xtype)->pointsToAlpha())
				extra.constrain(xPointsTo, yPointsTo);
			else
				extra.constrain(yPointsTo, xPointsTo);
LOG << "true\n";
			return true;
		}
LOG << (*xPointsTo == *yPointsTo) << "\n";
		return *xPointsTo == *yPointsTo;
	} else if (xtype->isSize()) {
		if (ytype->getSize() == 0) {   // Assume size=0 means unknown
LOG << "true\n";
			return true;
		} else {
LOG << (xtype->getSize() == ytype->getSize()) << "\n";
			return xtype->getSize() == ytype->getSize();
		}
	} else if (ytype->isSize()) {
		if (xtype->getSize() == 0) {   // Assume size=0 means unknown
LOG << "true\n";
			return true;
		} else {
LOG << (xtype->getSize() == ytype->getSize()) << "\n";
			return xtype->getSize() == ytype->getSize();
		}
	}
	// Otherwise, just compare the sizes
LOG << (*xtype == *ytype) << "\n";
	return *xtype == *ytype;
}