comp.lang.ada
 help / color / mirror / Atom feed
* generic instantion as overriding primitive operation?
@ 2012-10-21 17:52 Stephen Leake
  2012-10-23 14:58 ` Adam Beneschan
  2012-10-24 10:24 ` AdaMagica
  0 siblings, 2 replies; 4+ messages in thread
From: Stephen Leake @ 2012-10-21 17:52 UTC (permalink / raw)


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?

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: generic instantion as overriding primitive operation?
  2012-10-21 17:52 generic instantion as overriding primitive operation? Stephen Leake
@ 2012-10-23 14:58 ` Adam Beneschan
  2012-10-24 10:11   ` Stephen Leake
  2012-10-24 10:24 ` AdaMagica
  1 sibling, 1 reply; 4+ messages in thread
From: Adam Beneschan @ 2012-10-23 14:58 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: generic instantion as overriding primitive operation?
  2012-10-23 14:58 ` Adam Beneschan
@ 2012-10-24 10:11   ` Stephen Leake
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen Leake @ 2012-10-24 10:11 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> 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

That's for sure! There's no point in making Gen_Procedure generic.

> (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.)

Sounds like I can just ignore this case in the indentation engine, since
it will never occur in practice. I wonder why it is in the Ada grammar?

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: generic instantion as overriding primitive operation?
  2012-10-21 17:52 generic instantion as overriding primitive operation? Stephen Leake
  2012-10-23 14:58 ` Adam Beneschan
@ 2012-10-24 10:24 ` AdaMagica
  1 sibling, 0 replies; 4+ messages in thread
From: AdaMagica @ 2012-10-24 10:24 UTC (permalink / raw)


Ada 2012 defines formal incomplete types:

RM 12.5(2.2/3), which, according to 12.5.1(1/3), do not freeze the type.

Thus these might provide what you want. (Haven't tried myself yet.)



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-10-24 10:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-21 17:52 generic instantion as overriding primitive operation? Stephen Leake
2012-10-23 14:58 ` Adam Beneschan
2012-10-24 10:11   ` Stephen Leake
2012-10-24 10:24 ` AdaMagica

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox