From: Adam Beneschan <adam@irvine.com>
Subject: Re: generic instantion as overriding primitive operation?
Date: Tue, 23 Oct 2012 07:58:37 -0700 (PDT)
Date: 2012-10-23T07:58:37-07:00 [thread overview]
Message-ID: <4be3f29b-d2f2-4cd4-b7d3-78063b7779b1@googlegroups.com> (raw)
In-Reply-To: <858vazhfuv.fsf@stephe-leake.org>
On Sunday, October 21, 2012 10:52:57 AM UTC-7, Stephen Leake wrote:
> The syntax for generic_instantiation allows an overriding_indicator:
>
> generic_instantiation ::=
> [overriding_indicator]
> procedure defining_program_unit_name is
> new generic_procedure_name [generic_actual_part]
> [aspect_specification];
>
> So I'm trying to construct an example of that, as a test for the Ada
> mode 5.0 indentation engine.
>
> So far I have:
>
> package Ada_Mode.Nominal is
> type Parent_Type_1 is tagged null record;
> procedure Procedure_1a (Item : in out Parent_Type_1);
> function Function_2a (Param : in Parent_Type_1) return Float;
> end Ada_Mode.Nominal;
>
> generic
> package Ada_Mode.Generic_Parent is
>
> -- These match some of the primitive operations of
> -- Ada_Mode.Nominal.Parent_Type_1.
>
>
> generic
> type Tagged_Type is abstract tagged limited private;
> function Gen_Function_2a (Item : in Tagged_Type) return Float;
>
> generic
> type Tagged_Type is abstract tagged limited private;
> procedure Gen_Procedure_1b (Item : in out Tagged_Type);
>
> end Ada_Mode.Generic_Parent;
>
>
>
> with Ada_Mode.Generic_Parent;
> with Ada_Mode.Nominal;
> package Ada_Mode.Generic_Instantiation is
>
> package Instance is new Ada_Mode.Generic_Parent;
>
> type Child_Type_1 is new Ada_Mode.Nominal.Parent_Type_1 with null record;
>
> overriding
> procedure Procedure_1b is new Instance.Gen_Procedure_1b (Child_Type_1);
>
> type Child_Type_2 is new Ada_Mode.Nominal.Parent_Type_1 with null record;
>
> overriding
> function Function_2a is new Instance.Gen_Function_2a (Child_Type_2);
>
> end Ada_Mode.Generic_Instantiation;
>
> But GNAT 7.0.1 doesn't like this; it says:
>
> ada_mode-generic_instantiation.ads:32:04: warning: no primitive operations for "Child_Type_1" after this line
> ada_mode-generic_instantiation.ads:32:14: this primitive operation is declared too late
>
> where 'line 32' is the line declaring Procedure_1b
>
> Can anyone rearrange this to make it compile? Or is this a compiler bug?
>
> Does anyone have any real examples of this?
The compiler is correct. A generic instantiation freezes all identifiers used in the instantiation; thus, you can't use Child_Type_1 as an actual parameter to a generic subprogram instantiation if the resulting subprogram will be a primitive operation, since the primitive operation would be declared after the type is frozen.
On top of that, even without that rule, your program would be illegal because you're creating a new Procedure_1b and saying it's "overriding" but there's no Procedure_1b anywhere else in your program. I'm assuming this is a typo.
Here's a way to create a generic instantiation with an "overriding" indicator legally:
with Ada_Mode.Generic_Parent;
with Ada_Mode.Nominal;
package Ada_Mode.Generic_Instantiation is
type Child_Type_1 is new Ada_Mode.Nominal.Parent_Type_1 with null record;
generic
procedure Gen_Procedure (Item : in out Child_Type_1);
overriding
procedure Procedure_1a is new Gen_Procedure;
end Ada_Mode.Generic_Instantiation;
It may not be terribly useful in practice (and I'm not even sure you can create an example that won't raise Program_Error due to a violation of elaboration rules). But if your goal is to create a legal program that has "overriding" on a generic instantiation to test your indentation engine, this should serve. (Then again, if all you're doing is testing an indentation engine, I don't know why you'd care whether the program will compile or not.)
-- Adam
next prev parent reply other threads:[~2012-10-23 14:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-21 17:52 generic instantion as overriding primitive operation? Stephen Leake
2012-10-23 14:58 ` Adam Beneschan [this message]
2012-10-24 10:11 ` Stephen Leake
2012-10-24 10:24 ` AdaMagica
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox