+ // Are we trying to change the function pointer value to a new type?
+ if (OpNum == 0) {
+ const PointerType *PTy = dyn_cast<PointerType>(Ty);
+ if (PTy == 0) return false; // Can't convert to a non-pointer type...
+ const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
+ if (FTy == 0) return false; // Can't convert to a non ptr to function...
+
+ // Do not allow converting to a call where all of the operands are ...'s
+ if (FTy->getNumParams() == 0 && FTy->isVarArg())
+ return false; // Do not permit this conversion!
+
+ // Perform sanity checks to make sure that new function type has the
+ // correct number of arguments...
+ //
+ unsigned NumArgs = I->getNumOperands()-1; // Don't include function ptr
+
+ // Cannot convert to a type that requires more fixed arguments than
+ // the call provides...
+ //
+ if (NumArgs < FTy->getNumParams()) return false;
+
+ // Unless this is a vararg function type, we cannot provide more arguments
+ // than are desired...
+ //
+ if (!FTy->isVarArg() && NumArgs > FTy->getNumParams())
+ return false;
+
+ // Okay, at this point, we know that the call and the function type match
+ // number of arguments. Now we see if we can convert the arguments
+ // themselves. Note that we do not require operands to be convertable,
+ // we can insert casts if they are convertible but not compatible. The
+ // reason for this is that we prefer to have resolved functions but casted
+ // arguments if possible.
+ //
+ const FunctionType::ParamTypes &PTs = FTy->getParamTypes();
+ for (unsigned i = 0, NA = PTs.size(); i < NA; ++i)
+ if (!PTs[i]->isLosslesslyConvertableTo(I->getOperand(i+1)->getType()))
+ return false; // Operands must have compatible types!
+
+ // Okay, at this point, we know that all of the arguments can be
+ // converted. We succeed if we can change the return type if
+ // neccesary...
+ //
+ return ValueConvertableToType(I, FTy->getReturnType(), CTMap);
+ }