Commit 76d49f49 by Ed Schonberg Committed by Arnaud Charlet

sem_ch13.adb (Check_Indexing_Functions): The return type of an indexing function…

sem_ch13.adb (Check_Indexing_Functions): The return type of an indexing function can be the default element type...

2011-12-20  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch13.adb (Check_Indexing_Functions): The return type of an
	indexing function can be the default element type, and does not
	need to be a reference type.
	* sem_ch4.adb (Try_Container_Indexing): Ditto.

From-SVN: r182536
parent a68d415b
2011-12-20 Ed Schonberg <schonberg@adacore.com>
* sem_ch13.adb (Check_Indexing_Functions): The return type of an
indexing function can be the default element type, and does not
need to be a reference type.
* sem_ch4.adb (Try_Container_Indexing): Ditto.
2011-12-20 Robert Dewar <dewar@adacore.com> 2011-12-20 Robert Dewar <dewar@adacore.com>
* a-cdlili.ads, sem_cat.adb, sem_ch10.adb: Minor reformatting. * a-cdlili.ads, sem_cat.adb, sem_ch10.adb: Minor reformatting.
......
...@@ -1867,6 +1867,11 @@ package body Sem_Ch13 is ...@@ -1867,6 +1867,11 @@ package body Sem_Ch13 is
------------------------ ------------------------
procedure Check_One_Function (Subp : Entity_Id) is procedure Check_One_Function (Subp : Entity_Id) is
Default_Element : constant Node_Id :=
Find_Aspect
(Etype (First_Formal (Subp)),
Aspect_Iterator_Element);
begin begin
if not Check_Primitive_Function (Subp) then if not Check_Primitive_Function (Subp) then
Error_Msg_NE Error_Msg_NE
...@@ -1874,6 +1879,21 @@ package body Sem_Ch13 is ...@@ -1874,6 +1879,21 @@ package body Sem_Ch13 is
Subp, Ent); Subp, Ent);
end if; end if;
-- An indexing function must return either the default element of
-- the container, or a reference type.
if Present (Default_Element) then
Analyze (Default_Element);
if Is_Entity_Name (Default_Element)
and then
Covers (Entity (Default_Element), Etype (Subp))
then
return;
end if;
end if;
-- Otherwise the return type must be a reference type.
if not Has_Implicit_Dereference (Etype (Subp)) then if not Has_Implicit_Dereference (Etype (Subp)) then
Error_Msg_N Error_Msg_N
("function for indexing must return a reference type", Subp); ("function for indexing must return a reference type", Subp);
......
...@@ -6491,18 +6491,22 @@ package body Sem_Ch4 is ...@@ -6491,18 +6491,22 @@ package body Sem_Ch4 is
Rewrite (N, Indexing); Rewrite (N, Indexing);
Analyze (N); Analyze (N);
-- The return type of the indexing function is a reference type, so -- If the return type of the indexing function is a reference type,
-- add the dereference as a possible interpretation. -- add the dereference as a possible interpretation. Note that the
-- indexing aspect may be a function that returns the element type
Disc := First_Discriminant (Etype (Func)); -- with no intervening implicit dereference.
while Present (Disc) loop
if Has_Implicit_Dereference (Disc) then if Has_Discriminants (Etype (Func)) then
Add_One_Interp (N, Disc, Designated_Type (Etype (Disc))); Disc := First_Discriminant (Etype (Func));
exit; while Present (Disc) loop
end if; if Has_Implicit_Dereference (Disc) then
Add_One_Interp (N, Disc, Designated_Type (Etype (Disc)));
exit;
end if;
Next_Discriminant (Disc); Next_Discriminant (Disc);
end loop; end loop;
end if;
else else
Indexing := Make_Function_Call (Loc, Indexing := Make_Function_Call (Loc,
...@@ -6528,16 +6532,18 @@ package body Sem_Ch4 is ...@@ -6528,16 +6532,18 @@ package body Sem_Ch4 is
-- Add implicit dereference interpretation -- Add implicit dereference interpretation
Disc := First_Discriminant (Etype (It.Nam)); if Has_Discriminants (Etype (It.Nam)) then
while Present (Disc) loop Disc := First_Discriminant (Etype (It.Nam));
if Has_Implicit_Dereference (Disc) then while Present (Disc) loop
Add_One_Interp if Has_Implicit_Dereference (Disc) then
(N, Disc, Designated_Type (Etype (Disc))); Add_One_Interp
exit; (N, Disc, Designated_Type (Etype (Disc)));
end if; exit;
end if;
Next_Discriminant (Disc); Next_Discriminant (Disc);
end loop; end loop;
end if;
exit; exit;
end if; end if;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment