attribute "unchecked_access" cannot be applied to a subprogram

28
Jan

In Ada, you can get the pointer to a function or procedure to later call it dynamically. That is, a pointer variable of that type could be set to any one function or procedure that matches the definition and thus the call would be to that one function you selected.

To get the pointer of a function or a procedure you simply use the 'Access attribute. Unfortunately, you cannot pass such a pointer around as easily as you might expect (especially if you are a C programmer, or even better: an assembler code guy!)

Reading the documentation about the 'Access attribute, you are not unlikely going to find the 'Unchecked_Access attribute. One might think that if the compiler tells you that 'Access is not acceptable in a certain case because of a constrain, then 'Unchecked_Access would just work. Not the case. It is a bit complicated to explain why it is like that, but you can trust the fact that the people who wrote the Ada language know that it makes sense.

Yet, there are situation when you perfectly know that the function or procedure pointer is simply safe. In such circumstances, you'd still like to be able to grab that pointer. Well... That's a bit complicated, but more or less you need to use the Unchecked_Conversion feature. For a complete example, look at the AAda Test Package and any one test (i.e. AAda Test VStrings). The tests defines an array of procedure pointers that are defined inside the test and passed down to the AAda Test Package which is at a different level (and thus the 'Access is refused.) As you will see, you create a new version of the unchecked_conversion function that converts whatever you have on the left to whatever you want on the right. This is a generic implementation so you have to instantiate it to use it. Plus, it is intrinsic (which means that it happens inline and there isn't an actual anything in Ada language to make this work... it makes sense since there wouldn't be a way to make it work!)

0
Your rating: None