Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does generic instantiation occur (i.e., what does the word mean)?

Tags:

generics

ada

This question is based on a discussion (now deleted) in response to this question. It's more about terminology than actual programming semantics.

What exactly is generic "instantiation"? Does it occur at compile time or at run time?

I'll refer to the latest draft of the Ada 2012 standard.

The meaning of the term generic_instantiation is clear; it's a syntactic construct, something like

package Inst is new Generic_Package(This => That);

My question is about the verb "instantiate".

I've always thought of "instantiation" as something that occurs at compile time. It's the expansion of a generic template, and it occurs when the compiler encounters a generic_instantiation in a compilation unit.

But other sources refer to "instantiation" taking place, at least optionally, at run time.

This Wikipedia article says:

To instantiate a generic unit, the programmer passes actual parameters for each formal. The generic instance then behaves just like any other unit. It is possible to instantiate generic units at run-time, for example inside a loop.

which seems to imply that instantiation can occur either at compile time or at run time. I think this is referring to the elaboration of the generic_instantiation, which does occur at run time -- but then, all elaboration occurs at run time, yes?

I found it surprisingly difficult to get a definitive answer to this from the Ada RM. There is no glossary entry (Annex N) for "instantiation" or "instantiate".

The closest I could find was this statement in 12.2 (Generic Bodies) paragraph 2, under "Dynamic Semantics":

The elaboration of a generic body has no other effect than to establish that the generic unit can from then on be instantiated without failing the Elaboration_Check.

which indirectly implies that instantiation is a run-time event.

Does the verb "instantiate" properly refer to a compile-time event (expanding a generic template)? To a run-time event (elaborating a generic_instantiation)? If the latter, what do we call the former? Is the Ada RM as clear as it needs to be on this point?

like image 273
Keith Thompson Avatar asked Mar 09 '12 22:03

Keith Thompson


1 Answers

According to the generic instantiation in the Static Semantics part in the rules 12, 13 and 14, it seems to be done at compile time.

But the Gnat documentation, in its elaboration order control part, describe that a Program Error would be raised under certain circumstances. Such exception is only thrown when running the program.

So the point is, as far as I understood, that at compile time, the compiler checks that arguments support operations and ranges pushed by your generic but that real code is done at runtime.

like image 134
Frédéric Praca Avatar answered Oct 05 '22 09:10

Frédéric Praca