ConstantPointerRef and void*'s to functions

I have a Function* to a function that has a signature like:

void foo(double*, double*);

I need to write the address of this function into a global variable initializer (really, into the initializer of a particular structure field). The structure field type is void*, and must be.

Because of type constraints, doing something like:

Function* foo = …;
std::vector<const Type*> fields;

fields.push_back(ConstantPointerRef::get(foo));

will yield a type error because the type of the function foo (“void () (double, double*)”) does not match the structure field type.

How might I go about making the necessary cast-like step at the GlobalValue/Function level? I want to do something like:

Function* quasiCastThinger = foo->getAsType(myDesiredFunctionType);

TIA.

-j

As I thought about this more, I realized that the problem is not as easy as just transmuting the FunctionType from one FunctionType to another, since void* is not a FunctionType.

If the strict type-checking that is happening (the assert on Constants.cpp:233 in the ConstantStruct constructor) is the only problem here, and nothing more severe, perhaps the client should be able to set a flag that forces a the type of a ConstantPointerRef of a Function equal to what is returned by PointerType::get(Type::VoidTy), or some such thing. :slight_smile:

Thanks again,

-j

Function* foo = ...;
std::vector<const Type*> fields;
...
fields.push_back(ConstantPointerRef::get(foo));

will yield a type error because the type of the function foo ("void (*)
(double*, double*)") does not match the structure field type.
How might I go about making the necessary cast-like step at the
GlobalValue/Function level? I want to do something like:

ConstantExpr::getCast will return a constant cast from one constant to a
different type.

-Chris

As I thought about this more, I realized that the problem is not as easy
as just transmuting the FunctionType from one FunctionType to another,
since void* is not a FunctionType.

Sure, and the real type of the function is actual a
pointer_to(function_type) anyway.

If the strict type-checking that is happening (the assert on
Constants.cpp:233 in the ConstantStruct constructor) is the only problem
here, and nothing more severe, perhaps the client should be able to set
a flag that forces a the type of a ConstantPointerRef of a Function
equal to what is returned by PointerType::get(Type::VoidTy), or some
such thing. :slight_smile:

Ick ick ick. :slight_smile: No, just use the constant expr cast. You can see an
example if you do something like this with the C front-end:

void foo();
void *Ptr = &foo;

You generate ConstantExpr's programatically with the ConstantExpr::get*
methods.

-Chris