OSDN Git Service

2009-11-30 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_scil.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ S C I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --             Copyright (C) 2009, Free Software Foundation, Inc.           --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Einfo;    use Einfo;
27 with Namet;    use Namet;
28 with Nlists;   use Nlists;
29 with Opt;      use Opt;
30 with Rtsfind;  use Rtsfind;
31 with Sem;      use Sem;
32 with Sem_Aux;  use Sem_Aux;
33 with Sem_Util; use Sem_Util;
34 with Sinfo;    use Sinfo;
35 with Snames;   use Snames;
36 with Stand;    use Stand;
37
38 package body Sem_SCIL is
39
40    ----------------------
41    -- Adjust_SCIL_Node --
42    ----------------------
43
44    procedure Adjust_SCIL_Node (Old_Node : Node_Id; New_Node : Node_Id) is
45       SCIL_Node : Node_Id;
46
47    begin
48       pragma Assert (Generate_SCIL);
49
50       --  Check cases in which no action is required. Currently the only SCIL
51       --  nodes that may require adjustment are those of dispatching calls
52       --  internally generated by the frontend.
53
54       if Comes_From_Source (Old_Node)
55         or else not
56           Nkind_In (New_Node, N_Function_Call, N_Procedure_Call_Statement)
57       then
58          return;
59
60       --  Conditional expression associated with equality operator. Old_Node
61       --  may be part of the expansion of the predefined equality operator of
62       --  a tagged type and hence we need to check if it has a SCIL dispatching
63       --  node that needs adjustment.
64
65       elsif Nkind (Old_Node) = N_Conditional_Expression
66         and then (Nkind (Original_Node (Old_Node)) = N_Op_Eq
67                     or else
68                       (Nkind (Original_Node (Old_Node)) = N_Function_Call
69                         and then Chars (Name (Original_Node (Old_Node))) =
70                                                                  Name_Op_Eq))
71       then
72          null;
73
74       --  Type conversions may involve dispatching calls to functions whose
75       --  associated SCIL dispatching node needs adjustment.
76
77       elsif Nkind (Old_Node) = N_Type_Conversion then
78          null;
79
80       --  Relocated subprogram call
81
82       elsif Nkind (Old_Node) = Nkind (New_Node)
83         and then Original_Node (Old_Node) = Original_Node (New_Node)
84       then
85          null;
86
87       else
88          return;
89       end if;
90
91       --  Search for the SCIL node and update it (if found)
92
93       SCIL_Node := Find_SCIL_Node (Old_Node);
94
95       if Present (SCIL_Node) then
96          Set_SCIL_Related_Node (SCIL_Node, New_Node);
97       end if;
98    end Adjust_SCIL_Node;
99
100    ---------------------
101    -- Check_SCIL_Node --
102    ---------------------
103
104    function Check_SCIL_Node (N : Node_Id) return Traverse_Result is
105       Ctrl_Tag : Node_Id;
106       Ctrl_Typ : Entity_Id;
107
108    begin
109       if Nkind (N) = N_SCIL_Membership_Test then
110
111          --  Check contents of the boolean expression associated with the
112          --  membership test.
113
114          pragma Assert (Nkind (SCIL_Related_Node (N)) = N_Identifier
115            and then Etype (SCIL_Related_Node (N)) = Standard_Boolean);
116
117          --  Check the entity identifier of the associated tagged type (that
118          --  is, in testing for membership in T'Class, the entity id of the
119          --  specific type T).
120
121          --  Note: When the SCIL node is generated the private and full-view
122          --    of the tagged types may have been swapped and hence the node
123          --    referenced by attribute SCIL_Entity may be the private view.
124          --    Therefore, in order to uniformily locate the full-view we use
125          --    attribute Underlying_Type.
126
127          pragma Assert (Is_Tagged_Type (Underlying_Type (SCIL_Entity (N))));
128
129          --  Interface types are unsupported
130
131          pragma Assert (not Is_Interface (Underlying_Type (SCIL_Entity (N))));
132
133          --  Check the decoration of the expression that denotes the tag value
134          --  being tested
135
136          Ctrl_Tag := SCIL_Tag_Value (N);
137
138          case Nkind (Ctrl_Tag) is
139
140             --  For class-wide membership tests the SCIL tag value is the tag
141             --  of the tested object (i.e. Obj.Tag).
142
143             when N_Selected_Component =>
144                pragma Assert (Etype (Ctrl_Tag) = RTE (RE_Tag));
145                null;
146
147             when others =>
148                pragma Assert (False);
149                null;
150
151          end case;
152
153          return Skip;
154
155       elsif Nkind (N) = N_SCIL_Dispatching_Call then
156          Ctrl_Tag := SCIL_Controlling_Tag (N);
157
158          --  SCIL_Related_Node of SCIL dispatching call nodes MUST reference
159          --  subprogram calls.
160
161          if not Nkind_In (SCIL_Related_Node (N), N_Function_Call,
162                                                  N_Procedure_Call_Statement)
163          then
164             pragma Assert (False);
165             raise Program_Error;
166
167          --  In simple cases the controlling tag is the tag of the controlling
168          --  argument (i.e. Obj.Tag).
169
170          elsif Nkind (Ctrl_Tag) = N_Selected_Component then
171             Ctrl_Typ := Etype (Ctrl_Tag);
172
173             --  Interface types are unsupported
174
175             if Is_Interface (Ctrl_Typ)
176               or else (RTE_Available (RE_Interface_Tag)
177                          and then Ctrl_Typ = RTE (RE_Interface_Tag))
178             then
179                null;
180
181             else
182                pragma Assert (Ctrl_Typ = RTE (RE_Tag));
183                null;
184             end if;
185
186          --  When the controlling tag of a dispatching call is an identifier
187          --  the SCIL_Controlling_Tag attribute references the corresponding
188          --  object or parameter declaration. Interface types are still
189          --  unsupported.
190
191          elsif Nkind_In (Ctrl_Tag, N_Object_Declaration,
192                                    N_Parameter_Specification)
193          then
194             Ctrl_Typ := Etype (Defining_Identifier (Ctrl_Tag));
195
196             --  Interface types are unsupported.
197
198             if Is_Interface (Ctrl_Typ)
199               or else (RTE_Available (RE_Interface_Tag)
200                         and then Ctrl_Typ = RTE (RE_Interface_Tag))
201               or else (Is_Access_Type (Ctrl_Typ)
202                         and then
203                           Is_Interface
204                             (Available_View
205                               (Base_Type (Designated_Type (Ctrl_Typ)))))
206             then
207                null;
208
209             else
210                pragma Assert
211                  (Ctrl_Typ = RTE (RE_Tag)
212                     or else
213                       (Is_Access_Type (Ctrl_Typ)
214                         and then Available_View
215                                   (Base_Type (Designated_Type (Ctrl_Typ))) =
216                                                                 RTE (RE_Tag)));
217                null;
218             end if;
219
220          --  Interface types are unsupported
221
222          elsif Is_Interface (Etype (Ctrl_Tag)) then
223             null;
224
225          else
226             pragma Assert (False);
227             raise Program_Error;
228          end if;
229
230          return Skip;
231
232       --  Node is not N_SCIL_Dispatching_Call
233
234       else
235          return OK;
236       end if;
237    end Check_SCIL_Node;
238
239    --------------------
240    -- Find_SCIL_Node --
241    --------------------
242
243    function Find_SCIL_Node (Node : Node_Id) return Node_Id is
244       Found_Node : Node_Id;
245       --  This variable stores the last node found by the nested subprogram
246       --  Find_SCIL_Node.
247
248       function Find_SCIL_Node (L : List_Id) return Boolean;
249       --  Searches in list L for a SCIL node associated with a dispatching call
250       --  whose SCIL_Related_Node is Node. If found returns true and stores the
251       --  SCIL node in Found_Node; otherwise returns False and sets Found_Node
252       --  to Empty.
253
254       --------------------
255       -- Find_SCIL_Node --
256       --------------------
257
258       function Find_SCIL_Node (L : List_Id) return Boolean is
259          N : Node_Id;
260
261       begin
262          N := First (L);
263          while Present (N) loop
264             if Nkind (N) in N_SCIL_Node
265               and then SCIL_Related_Node (N) = Node
266             then
267                Found_Node := N;
268                return True;
269             end if;
270
271             Next (N);
272          end loop;
273
274          Found_Node := Empty;
275          return False;
276       end Find_SCIL_Node;
277
278       --  Local variables
279
280       P : Node_Id;
281
282    --  Start of processing for Find_SCIL_Node
283
284    begin
285       pragma Assert (Generate_SCIL);
286
287       --  Search for the SCIL node in list associated with a transient scope
288
289       if Scope_Is_Transient then
290          declare
291             SE : Scope_Stack_Entry
292                    renames Scope_Stack.Table (Scope_Stack.Last);
293          begin
294             if SE.Is_Transient
295               and then Present (SE.Actions_To_Be_Wrapped_Before)
296               and then Find_SCIL_Node (SE.Actions_To_Be_Wrapped_Before)
297             then
298                return Found_Node;
299             end if;
300          end;
301       end if;
302
303       --  Otherwise climb up the tree searching for the SCIL node analyzing
304       --  all the lists in which Insert_Actions may have inserted it
305
306       P := Node;
307       while Present (P) loop
308          case Nkind (P) is
309
310             --  Actions associated with AND THEN or OR ELSE
311
312             when N_Short_Circuit =>
313                if Present (Actions (P))
314                  and then Find_SCIL_Node (Actions (P))
315                then
316                   return Found_Node;
317                end if;
318
319             --  Actions of conditional expressions
320
321             when N_Conditional_Expression =>
322                if (Present (Then_Actions (P))
323                     and then Find_SCIL_Node (Actions (P)))
324                  or else
325                   (Present (Else_Actions (P))
326                     and then Find_SCIL_Node (Else_Actions (P)))
327                then
328                   return Found_Node;
329                end if;
330
331             --  Actions in handled sequence of statements
332
333             when
334                N_Handled_Sequence_Of_Statements =>
335                   if Find_SCIL_Node (Statements (P)) then
336                      return Found_Node;
337                   end if;
338
339             --  Conditions of while expression or elsif.
340
341             when N_Iteration_Scheme |
342                  N_Elsif_Part
343             =>
344                if Present (Condition_Actions (P))
345                  and then Find_SCIL_Node (Condition_Actions (P))
346                then
347                   return Found_Node;
348                end if;
349
350             --  Statements, declarations, pragmas, representation clauses
351
352             when
353                --  Statements
354
355                N_Procedure_Call_Statement               |
356                N_Statement_Other_Than_Procedure_Call    |
357
358                --  Pragmas
359
360                N_Pragma                                 |
361
362                --  Representation_Clause
363
364                N_At_Clause                              |
365                N_Attribute_Definition_Clause            |
366                N_Enumeration_Representation_Clause      |
367                N_Record_Representation_Clause           |
368
369                --  Declarations
370
371                N_Abstract_Subprogram_Declaration        |
372                N_Entry_Body                             |
373                N_Exception_Declaration                  |
374                N_Exception_Renaming_Declaration         |
375                N_Formal_Abstract_Subprogram_Declaration |
376                N_Formal_Concrete_Subprogram_Declaration |
377                N_Formal_Object_Declaration              |
378                N_Formal_Type_Declaration                |
379                N_Full_Type_Declaration                  |
380                N_Function_Instantiation                 |
381                N_Generic_Function_Renaming_Declaration  |
382                N_Generic_Package_Declaration            |
383                N_Generic_Package_Renaming_Declaration   |
384                N_Generic_Procedure_Renaming_Declaration |
385                N_Generic_Subprogram_Declaration         |
386                N_Implicit_Label_Declaration             |
387                N_Incomplete_Type_Declaration            |
388                N_Number_Declaration                     |
389                N_Object_Declaration                     |
390                N_Object_Renaming_Declaration            |
391                N_Package_Body                           |
392                N_Package_Body_Stub                      |
393                N_Package_Declaration                    |
394                N_Package_Instantiation                  |
395                N_Package_Renaming_Declaration           |
396                N_Private_Extension_Declaration          |
397                N_Private_Type_Declaration               |
398                N_Procedure_Instantiation                |
399                N_Protected_Body                         |
400                N_Protected_Body_Stub                    |
401                N_Protected_Type_Declaration             |
402                N_Single_Task_Declaration                |
403                N_Subprogram_Body                        |
404                N_Subprogram_Body_Stub                   |
405                N_Subprogram_Declaration                 |
406                N_Subprogram_Renaming_Declaration        |
407                N_Subtype_Declaration                    |
408                N_Task_Body                              |
409                N_Task_Body_Stub                         |
410                N_Task_Type_Declaration                  |
411
412                --  Freeze entity behaves like a declaration or statement
413
414                N_Freeze_Entity
415             =>
416                --  Do not search here if the item is not a list member
417
418                if not Is_List_Member (P) then
419                   null;
420
421                --  Do not search if parent of P is an N_Component_Association
422                --  node (i.e. we are in the context of an N_Aggregate or
423                --  N_Extension_Aggregate node). In this case the node should
424                --  have been added before the entire aggregate.
425
426                elsif Nkind (Parent (P)) = N_Component_Association then
427                   null;
428
429                --  Do not search if the parent of P is either an N_Variant
430                --  node or an N_Record_Definition node. In this case the node
431                --  should have been added before the entire record.
432
433                elsif Nkind (Parent (P)) = N_Variant
434                  or else Nkind (Parent (P)) = N_Record_Definition
435                then
436                   null;
437
438                --  Otherwise search it in the list containing this node
439
440                elsif Find_SCIL_Node (List_Containing (P)) then
441                   return Found_Node;
442                end if;
443
444             --  A special case, N_Raise_xxx_Error can act either as a statement
445             --  or a subexpression. We diferentiate them by looking at the
446             --  Etype. It is set to Standard_Void_Type in the statement case.
447
448             when
449                N_Raise_xxx_Error =>
450                   if Etype (P) = Standard_Void_Type then
451                      if Is_List_Member (P)
452                        and then Find_SCIL_Node (List_Containing (P))
453                      then
454                         return Found_Node;
455                      end if;
456
457                   --  In the subexpression case, keep climbing
458
459                   else
460                      null;
461                   end if;
462
463             --  If a component association appears within a loop created for
464             --  an array aggregate, check if the SCIL node was added to the
465             --  the list of nodes attached to the association.
466
467             when
468                N_Component_Association =>
469                   if Nkind (Parent (P)) = N_Aggregate
470                     and then Present (Loop_Actions (P))
471                     and then Find_SCIL_Node (Loop_Actions (P))
472                   then
473                      return Found_Node;
474                   end if;
475
476             --  Another special case, an attribute denoting a procedure call
477
478             when
479                N_Attribute_Reference =>
480                   if Is_Procedure_Attribute_Name (Attribute_Name (P))
481                     and then Find_SCIL_Node (List_Containing (P))
482                   then
483                      return Found_Node;
484
485                   --  In the subexpression case, keep climbing
486
487                   else
488                      null;
489                   end if;
490
491             --  SCIL nodes do not have subtrees and hence they can never be
492             --  found climbing tree
493
494             when
495                N_SCIL_Dispatch_Table_Object_Init        |
496                N_SCIL_Dispatch_Table_Tag_Init           |
497                N_SCIL_Dispatching_Call                  |
498                N_SCIL_Membership_Test                   |
499                N_SCIL_Tag_Init
500             =>
501                pragma Assert (False);
502                raise Program_Error;
503
504             --  For all other node types, keep climbing tree
505
506             when
507                N_Abortable_Part                         |
508                N_Accept_Alternative                     |
509                N_Access_Definition                      |
510                N_Access_Function_Definition             |
511                N_Access_Procedure_Definition            |
512                N_Access_To_Object_Definition            |
513                N_Aggregate                              |
514                N_Allocator                              |
515                N_Case_Statement_Alternative             |
516                N_Character_Literal                      |
517                N_Compilation_Unit                       |
518                N_Compilation_Unit_Aux                   |
519                N_Component_Clause                       |
520                N_Component_Declaration                  |
521                N_Component_Definition                   |
522                N_Component_List                         |
523                N_Constrained_Array_Definition           |
524                N_Decimal_Fixed_Point_Definition         |
525                N_Defining_Character_Literal             |
526                N_Defining_Identifier                    |
527                N_Defining_Operator_Symbol               |
528                N_Defining_Program_Unit_Name             |
529                N_Delay_Alternative                      |
530                N_Delta_Constraint                       |
531                N_Derived_Type_Definition                |
532                N_Designator                             |
533                N_Digits_Constraint                      |
534                N_Discriminant_Association               |
535                N_Discriminant_Specification             |
536                N_Empty                                  |
537                N_Entry_Body_Formal_Part                 |
538                N_Entry_Call_Alternative                 |
539                N_Entry_Declaration                      |
540                N_Entry_Index_Specification              |
541                N_Enumeration_Type_Definition            |
542                N_Error                                  |
543                N_Exception_Handler                      |
544                N_Expanded_Name                          |
545                N_Explicit_Dereference                   |
546                N_Extension_Aggregate                    |
547                N_Floating_Point_Definition              |
548                N_Formal_Decimal_Fixed_Point_Definition  |
549                N_Formal_Derived_Type_Definition         |
550                N_Formal_Discrete_Type_Definition        |
551                N_Formal_Floating_Point_Definition       |
552                N_Formal_Modular_Type_Definition         |
553                N_Formal_Ordinary_Fixed_Point_Definition |
554                N_Formal_Package_Declaration             |
555                N_Formal_Private_Type_Definition         |
556                N_Formal_Signed_Integer_Type_Definition  |
557                N_Function_Call                          |
558                N_Function_Specification                 |
559                N_Generic_Association                    |
560                N_Identifier                             |
561                N_In                                     |
562                N_Index_Or_Discriminant_Constraint       |
563                N_Indexed_Component                      |
564                N_Integer_Literal                        |
565                N_Itype_Reference                        |
566                N_Label                                  |
567                N_Loop_Parameter_Specification           |
568                N_Mod_Clause                             |
569                N_Modular_Type_Definition                |
570                N_Not_In                                 |
571                N_Null                                   |
572                N_Op_Abs                                 |
573                N_Op_Add                                 |
574                N_Op_And                                 |
575                N_Op_Concat                              |
576                N_Op_Divide                              |
577                N_Op_Eq                                  |
578                N_Op_Expon                               |
579                N_Op_Ge                                  |
580                N_Op_Gt                                  |
581                N_Op_Le                                  |
582                N_Op_Lt                                  |
583                N_Op_Minus                               |
584                N_Op_Mod                                 |
585                N_Op_Multiply                            |
586                N_Op_Ne                                  |
587                N_Op_Not                                 |
588                N_Op_Or                                  |
589                N_Op_Plus                                |
590                N_Op_Rem                                 |
591                N_Op_Rotate_Left                         |
592                N_Op_Rotate_Right                        |
593                N_Op_Shift_Left                          |
594                N_Op_Shift_Right                         |
595                N_Op_Shift_Right_Arithmetic              |
596                N_Op_Subtract                            |
597                N_Op_Xor                                 |
598                N_Operator_Symbol                        |
599                N_Ordinary_Fixed_Point_Definition        |
600                N_Others_Choice                          |
601                N_Package_Specification                  |
602                N_Parameter_Association                  |
603                N_Parameter_Specification                |
604                N_Pop_Constraint_Error_Label             |
605                N_Pop_Program_Error_Label                |
606                N_Pop_Storage_Error_Label                |
607                N_Pragma_Argument_Association            |
608                N_Procedure_Specification                |
609                N_Protected_Definition                   |
610                N_Push_Constraint_Error_Label            |
611                N_Push_Program_Error_Label               |
612                N_Push_Storage_Error_Label               |
613                N_Qualified_Expression                   |
614                N_Range                                  |
615                N_Range_Constraint                       |
616                N_Real_Literal                           |
617                N_Real_Range_Specification               |
618                N_Record_Definition                      |
619                N_Reference                              |
620                N_Selected_Component                     |
621                N_Signed_Integer_Type_Definition         |
622                N_Single_Protected_Declaration           |
623                N_Slice                                  |
624                N_String_Literal                         |
625                N_Subprogram_Info                        |
626                N_Subtype_Indication                     |
627                N_Subunit                                |
628                N_Task_Definition                        |
629                N_Terminate_Alternative                  |
630                N_Triggering_Alternative                 |
631                N_Type_Conversion                        |
632                N_Unchecked_Expression                   |
633                N_Unchecked_Type_Conversion              |
634                N_Unconstrained_Array_Definition         |
635                N_Unused_At_End                          |
636                N_Unused_At_Start                        |
637                N_Use_Package_Clause                     |
638                N_Use_Type_Clause                        |
639                N_Variant                                |
640                N_Variant_Part                           |
641                N_Validate_Unchecked_Conversion          |
642                N_With_Clause
643             =>
644                null;
645
646          end case;
647
648          --  If we fall through above tests, keep climbing tree
649
650          if Nkind (Parent (P)) = N_Subunit then
651
652             --  This is the proper body corresponding to a stub. Insertion done
653             --  at the point of the stub, which is in the declarative part of
654             --  the parent unit.
655
656             P := Corresponding_Stub (Parent (P));
657
658          else
659             P := Parent (P);
660          end if;
661       end loop;
662
663       --  SCIL node not found
664
665       return Empty;
666    end Find_SCIL_Node;
667
668    -------------------------
669    -- First_Non_SCIL_Node --
670    -------------------------
671
672    function First_Non_SCIL_Node (L : List_Id) return Node_Id is
673       N : Node_Id;
674
675    begin
676       N := First (L);
677       while Nkind (N) in N_SCIL_Node loop
678          Next (N);
679       end loop;
680
681       return N;
682    end First_Non_SCIL_Node;
683
684    ------------------------
685    -- Next_Non_SCIL_Node --
686    ------------------------
687
688    function Next_Non_SCIL_Node (N : Node_Id) return Node_Id is
689       Aux_N : Node_Id;
690
691    begin
692       Aux_N := Next (N);
693       while Nkind (Aux_N) in N_SCIL_Node loop
694          Next (Aux_N);
695       end loop;
696
697       return Aux_N;
698    end Next_Non_SCIL_Node;
699
700 end Sem_SCIL;