core::NodeAddress ForAll::apply(const core::NodeAddress& targetAddress) const { auto target = targetAddress.as<core::NodePtr>(); // generate list of target nodes vector<core::NodeAddress> targets = filter(target); if(targets.empty()) { return targetAddress; } // generate replacement map std::map<core::NodeAddress, core::NodePtr> replacements; for_each(targets, [&](const core::NodeAddress& cur) { replacements[cur] = getTransformation()->apply(cur.getAddressedNode()); }); // apply replacement auto mod = core::transform::replaceAll(target->getNodeManager(), replacements); return core::transform::replaceAddress(target->getNodeManager(), targetAddress, mod); }
core::NodeAddress Fixpoint::apply(const core::NodeAddress& targetAddress) const { auto target = targetAddress.as<core::NodePtr>(); // apply transformation until result represents a fix-point of the sub-transformation core::NodePtr cur = target; ; core::NodePtr last; unsigned counter = 0; do { last = cur; cur = getTransformation()->apply(last); counter++; } while(*cur != *last && counter <= maxIterations); // check whether fixpoint could be obtained if(!acceptNonFixpoint && *cur != *last) { // => no fixpoint found! throw InvalidTargetException("Fixpoint could not be obtained!"); } // return fixpoint (or approximation) return core::transform::replaceAddress(target->getNodeManager(), targetAddress, cur); }
core::NodeAddress ForEach::apply(const core::NodeAddress& targetAddress) const { auto target = targetAddress.as<core::NodePtr>(); auto mod = apply(target, maxDepth); return core::transform::replaceAddress(target->getNodeManager(), targetAddress, mod); }
bool isSubTypeOf(const TypePtr& subType, const TypePtr& superType) { // quick check - reflexivity if (*subType == *superType) { return true; } // check for recursive types if (subType->getNodeType() == NT_RecType || superType->getNodeType() == NT_RecType) { // since they are not equivalent we have to compare the unrolled version of the sub with the super type if (subType->getNodeType() == NT_RecType) { return isSubTypeOf(subType.as<RecTypePtr>()->unroll(), superType); } if (superType->getNodeType() == NT_RecType) { assert(subType->getNodeType() != NT_RecType); return isSubTypeOf(subType, superType.as<RecTypePtr>()->unroll()); } assert(false && "How could you get here?"); } // check whether the sub-type is generic if (subType->getNodeType() == NT_GenericType || subType->getNodeType() == NT_StructType) { // use the delta algorithm for computing all the super-types of the given sub-type return isSubTypeOfInternal(subType, superType); } // check for vector types if (subType.isa<VectorTypePtr>() ) { VectorTypePtr vector = static_pointer_cast<const VectorType>(subType); // potential super type is an array of the same element type IRBuilder builder(vector->getNodeManager()); return *superType == *builder.arrayType(vector->getElementType()); } // for all other relations, the node type has to be the same if (subType->getNodeType() != superType->getNodeType()) { return false; } // check function types if (subType->getNodeType() == NT_FunctionType) { FunctionTypePtr funTypeA = static_pointer_cast<const FunctionType>(subType); FunctionTypePtr funTypeB = static_pointer_cast<const FunctionType>(superType); // check kind of functions if (funTypeA->getKind() != funTypeB->getKind()) { // only closure to plain conversion is allowed if (!(funTypeB->isClosure() && funTypeA->isPlain())) { return false; } } bool res = true; res = res && funTypeA->getParameterTypes().size() == funTypeB->getParameterTypes().size(); res = res && isSubTypeOf(funTypeA->getReturnType(), funTypeB->getReturnType()); for(std::size_t i = 0; res && i<funTypeB->getParameterTypes().size(); i++) { res = res && isSubTypeOf(funTypeB->getParameterTypes()[i], funTypeA->getParameterTypes()[i]); } return res; } // check reference types if (subType->getNodeType() == NT_RefType) { const auto& basic = subType->getNodeManager().getLangBasic(); // check source / sink auto srcRef = subType.as<RefTypePtr>(); auto trgRef = superType.as<RefTypePtr>(); // check read/write privileges if (trgRef.isRead() && !srcRef.isRead()) return false; if (trgRef.isWrite() && !srcRef.isWrite()) return false; // check element type auto srcElement = subType.as<RefTypePtr>()->getElementType(); auto trgElement = superType.as<RefTypePtr>()->getElementType(); // if element types are identical => it is fine //if (srcElement == trgElement) return true; if(core::analysis::compareTypes(srcElement, trgElement)) return true; // if sub-type is ref<any> it is ok if (basic.isAny(trgElement)) { return true; } // support nested references if (srcElement.isa<RefTypePtr>() && trgElement.isa<RefTypePtr>()) { return isSubTypeOf(srcElement, trgElement); } // a ref<vector<X,Y>> is a sub-type of a ref<array<X,1>> if (trgElement.isa<ArrayTypePtr>() && srcElement.isa<VectorTypePtr>()) { IRBuilder builder(srcElement.getNodeManager()); auto one = builder.concreteIntTypeParam(1); // array needs to be 1-dimensional and both have to have the same element type return trgElement.as<ArrayTypePtr>()->getDimension() == one && trgElement.as<ArrayTypePtr>()->getElementType() == srcElement.as<VectorTypePtr>()->getElementType(); } // also support references of derived classes being passed to base-type pointer if (core::analysis::isObjectType(srcElement) && core::analysis::isObjectType(trgElement)) { if (isSubTypeOf(srcElement, trgElement)) { return true; } } } // no other relations are supported return false; }