[LLVMdev] Mutability of constant initializers

Joel Stanley jstanley at cs.uiuc.edu
Wed Apr 16 14:02:00 PDT 2003


It seems that the contents of Constant{Array,Struct,etc} are immutable
after construction.  The only available accessor function is
getValues(), which returns a constant reference to a vector of uses.

I have the following situation. I am building global (static) array of
structs. Hence, I build a vector of ConstantStruct pointers to give to
the ConstantArray constructor, etc. However, I have fields in the
structs themselves which contain indices into the enclosing array (i.e.
links within the structs to other structs in the global array).  Since
only the const accessor is available to me, I can either

a) force access (i.e. const_cast or some such thing) to the underlying
vector of struct values, and modify them. This seems gross.

or

b) go out of my way to build a temporary map which contains the proper
links, and use the map to fill in the fields upon construction.

Which would be the preferred 'LLVM way'? Presumably, (b) is the right
answer here, but I want to make sure that there isn't a better way to
get write access to the ConstantStruct initializer contents after
construction.

Furthermore, if the answer is (b), I'd like to know why the values are
immutable. Presumably, there's a Constant-instance-caching mechanism
behind the scenes, so that'd be my guess as to why I can't modify these
values after I construct a Constant-derived instance.

Thanks.

-j

-- 
Joel Stanley <jstanley at cs.uiuc.edu>




More information about the llvm-dev mailing list