1------------------------------------------------------------------------------
2--                                                                          --
3--                 ASIS-for-GNAT IMPLEMENTATION COMPONENTS                  --
4--                                                                          --
5--                          A 4 G . C O N T T . T T                         --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--            Copyright (C) 1995-2011, Free Software Foundation, Inc.       --
10--                                                                          --
11-- ASIS-for-GNAT is free software; you can redistribute it and/or modify it --
12-- under terms of the  GNU General Public License  as published by the Free --
13-- Software Foundation;  either version 2,  or  (at your option)  any later --
14-- version. ASIS-for-GNAT is distributed  in the hope  that it will be use- --
15-- ful, but WITHOUT ANY WARRANTY; without even the implied warranty of MER- --
16-- CHANTABILITY or  FITNESS FOR A  PARTICULAR PURPOSE.  See the GNU General --
17-- Public License for more details.  You should have received a copy of the --
18-- GNU  General  Public  License  distributed with  ASIS-for-GNAT; see file --
19-- COPYING.  If not,  write  to the  Free Software Foundation,  51 Franklin --
20-- Street, Fifth Floor, Boston, MA 02110-1301, USA.                         --
21--                                                                          --
22--                                                                          --
23--                                                                          --
24--                                                                          --
25--                                                                          --
26--                                                                          --
27--                                                                          --
28--                                                                          --
29-- ASIS-for-GNAT was originally developed  by the ASIS-for-GNAT team at the --
30-- Software  Engineering  Laboratory  of  the Swiss  Federal  Institute  of --
31-- Technology (LGL-EPFL) in Lausanne,  Switzerland, in cooperation with the --
32-- Scientific  Research  Computer  Center of  Moscow State University (SRCC --
33-- MSU), Russia,  with funding partially provided  by grants from the Swiss --
34-- National  Science  Foundation  and  the  Swiss  Academy  of  Engineering --
35-- Sciences.  ASIS-for-GNAT is now maintained by  AdaCore.                  --
36-- (http://www.adacore.com).                                                --
37--                                                                          --
38------------------------------------------------------------------------------
39
40--  This package defines for each ASIS Context the corresponding Tree Table,
41--  which contains the information about the tree output files needed for
42--  handling and swapping the ASTs represented by the tree output files
43--  accessed by ASIS.
44
45with Asis;
46
47package A4G.Contt.TT is              --  Context_Table.Tree_Tables
48
49   ----------------
50   -- Tree Table --
51   ----------------
52
53   --  The tree table has an entry for each AST ( = tree output file)
54   --  created and read at least once for this run of ASIS application.
55
56   --  The entries in the table are accessing using a Tree_Id which
57   --  ranges from Nil_Tree (a special value using for initializing
58   --  ASIS Nil_Element and ASIS Nil_Compilation_Unit) to Last_Tree.
59   --  Each entry has the following fields:
60
61   ---------------------
62   -- Tree Name Table --
63   ---------------------
64
65   procedure Get_Name_String (C : Context_Id; Id : Tree_Id);
66   --  Get_Name_String is used to retrieve the string associated with
67   --  an entry in the name table. The resulting string is stored in
68   --  Name_Buffer and Name_Len is set.
69
70   function Get_Tree_Name (C : Context_Id; Id : Tree_Id) return String;
71   --  Returns the full name of the tree file.
72
73   function Allocate_Tree_Entry return Tree_Id; --  #####
74   --  Allocates the new entry in the Tree Table for the tree output file
75   --  name stored in the A_Name_Buffer (A_Name_Len should be set
76   --  in a proper way).
77
78   ------------------------------
79   -- Internal Tree Attributes --
80   ------------------------------
81
82   --  Each Tree entry contains the following fields, representing the Tree
83   --  attributes needed to organize tree processing inside ASIS
84   --  implementation:
85
86   --  Enclosing_Lib : Context_Id;                          --##
87   --    Context Id of the ASIS Context for which the tree has been
88   --    created.
89
90   --  Main_Unit_Id : Unit_Id;
91   --    The ASIS Compilation Unit, corresponding to the main unit in
92   --    the tree
93
94   --  Main_Top : Node_Id;
95   --    The top node (having N_Compilation_Unit Node Kind) of Main_Unit
96
97   --   Units : Elist_Id;
98   --     The list of all the Units (or all the Units except Main_Unit?)
99   --     which may be processed on the base of this tree, [each Unit
100   --     is accompanied by its top node, which it has in the given tree
101   --     ??? Not implemented for now!]
102
103   ---------------------------------------------------------------
104   --  Internal Tree Unit Attributes Access and Update Routines --
105   ---------------------------------------------------------------
106
107   function Main_Unit_Id (T : Tree_Id) return Unit_Id;
108   function Main_Unit_Id               return Unit_Id;
109   --  Returns the Id of the main unit in Current_Tree
110
111   procedure Set_Main_Unit_Id (T : Tree_Id; U : Unit_Id);
112   procedure Set_Main_Top     (T : Tree_Id; N : Node_Id);
113   --  Do we really need Set procedures having a Tree (and its "enclosing"
114   --  Context) as a parameter? Now it seems, that all settings will be
115   --  done for the currently accessing Tree only.
116
117   procedure Set_Main_Unit_Id (U : Unit_Id);
118   procedure Set_Main_Top     (N : Node_Id);
119
120   -----------------------------------
121   -- Subprograms for Tree Swapping --
122   -----------------------------------
123
124   function Unit_In_Current_Tree (C : Context_Id; U : Unit_Id) return Boolean;
125   --  Checks if the subtree for a given Unit defined by C and U, is
126   --  contained in the currently accessed tree.
127
128   procedure Reset_Tree (Context : Context_Id; Tree : Tree_Id);
129   --  Resets the currently accessed tree to the tree identified by
130   --  the Context and Tree parameters
131
132   procedure Reset_Tree_For_Unit (C : Context_Id; U : Unit_Id);
133   procedure Reset_Tree_For_Unit (Unit : Asis.Compilation_Unit);
134   --  Resets the currently accessed tree to some tree containing
135   --  the subtree for a given unit. For now, there is no special
136   --  strategy for choosing the tree among all the trees containing
137   --  the given unit
138
139   procedure Reset_Tree_For_Element (E : Asis.Element);
140   --  Resets the currently accessed tree to the tree containing the node(s)
141   --  of the argument Element.
142
143   procedure Reset_Instance_Tree
144     (Lib_Level_Instance : Asis.Compilation_Unit;
145      Decl_Node          : in out Node_Id);
146   --  Given Lib_Level_Instance as ASIS Compilation Unit being a library-level
147   --  instantiation, or a package or generic package containing
148   --  an instantiation of some library-level generic unit, and Decl_Node as
149   --  the node representing some declaration in the corresponding spec (which
150   --  can be either expanded generics spec if Lib_Level_Instance is a library-
151   --  level instantiation or a normal spec in case of a (generic) package);
152   --  it is an error to call this procedure with other arguments), this
153   --  procedure resets the currently accessed tree to the main tree for
154   --  Lib_Level_Instance (it may be the tree created for the body of
155   --  Lib_Level_Instance in case if Lib_Level_Instance is a package
156   --  declaration) and resets Decl_Node to point to the same construct in
157   --  this tree.
158   --
159   --  If the corresponding ASIS Context does not contain the main tree for
160   --  this library-level instantiation, the procedure does nothing.
161   --  Also does nothing if Lib_Level_Instance is a package body
162
163   function Restore_Node_From_Trace
164     (In_Body : Boolean               := False;
165      CU      : Asis.Compilation_Unit := Asis.Nil_Compilation_Unit)
166      return    Node_Id;
167   --  Taking the node trace stored in Node_Trace table, tries to find the
168   --  construct corresponding to the beginning of the trace in the currently
169   --  accessed tree. By default we consider that we are in the package spec,
170   --  unless In_Body is set ON.
171
172   procedure Append_Full_View_Tree_To_Unit    (C : Context_Id; U : Unit_Id);
173   procedure Append_Limited_View_Tree_To_Unit (C : Context_Id; U : Unit_Id);
174   --  Appends the currently accessed tree to the list of the (consistent)
175   --  trees containing a given Unit (this tree list belongs to the unit U).
176
177   procedure Reorder_Trees (C : Context_Id);
178   --  This procedure is called in the very end of opening the context C, when
179   --  all the information is already set in the Context Unit table. It
180   --  reorders the tree lists associated with units according to the
181   --  following rules (note, that currently the first tree in the tree list
182   --  is used by Element gateway queries to get into the unit structure:
183   --
184   --  (1) for a subunit, the tree for its ancestor body is moved into the
185   --      first position in the tree list;
186   --
187   --  (2) for a package declaration or generic package declaration, if this
188   --      package requires a body, the tree for the body is moved into the
189   --      first position in the tree list;
190   --
191   --  (3) for package or generic package declaration which does not require a
192   --      body, the tree created for the given (generic) package is moved
193   --      into the first position in the tree list;
194   --
195   --  (4) for a library-level instantiation, the tree created for the
196   --      instantiation is moved into the first position in the tree list;
197   --
198   --  (5) for a (generic) subprogram declaration, the tree for the
199   --      corresponding body is moved into the first position in the tree
200   --      list;
201   --
202   --  (6) for the bodies, we may also need to set the main tree first, because
203   --      according to Lib (h), the body may be compiled as being needed for
204   --      some spec (or other body unit)
205   --
206   --  For -CA Context, if the tree to be moved into the first position in
207   --  the tree list does not exist, the corresponding warning is generated,
208   --  except if the corresponding unit is of A_Predefined_Unit or
209   --  An_Implementation_Unit origin
210
211   ---------------------------------
212   -- General-Purpose Subprograms --
213   ---------------------------------
214
215   function Present (Tree : Tree_Id) return Boolean;
216   --  Tests given Tree Id for non-equality with No_Tree_Name.
217   --  This allows  notations like "if Present (Tree)" as opposed to
218   --  "if Tree /= No_Tree_Name"
219
220   function No (Tree : Tree_Id) return Boolean;
221   --  Tests given Tree Id for equality with No_Tree_Name. This allows
222   --  notations like "if No (Tree)" as opposed to
223   --  "if Tree = No_Tree_Name"
224
225   function Last_Tree (C : Context_Id) return Tree_Id;
226   --  Returns the Tree_Id of the last tree which has been allocated
227   --  in the Tree Table.
228
229   procedure Output_Tree (C : Context_Id; Tree : Tree_Id);
230   --  Produces the debug output of the Tree Table entry corresponding
231   --  to Tree
232
233   procedure Print_Trees (C : Context_Id);
234   --  Produces the debug output from the Tree table for the Context C.
235
236   function Tree_Consistent_With_Sources
237     (E :    Asis.Element)
238      return Boolean;
239
240   function Tree_Consistent_With_Sources
241     (CU :   Asis.Compilation_Unit)
242      return Boolean;
243   --  These functions are supposed to be used for Incremental Context mode.
244   --  They check that the tree from which their argument Element or Unit has
245   --  been obtained is still consistent with all the sources from which
246   --  the tree was generated (and that all these sources are available)
247   --  This function supposes that its argument is not null and that  the tree
248   --  to check is available.
249
250   function Current_Tree_Consistent_With_Sources return Boolean;
251   --  Checks that for the current tree all the sources from which it has been
252   --  obtained are still available and that the tree is consistent with
253   --  these sources. The caller is responsible for setting as the current
254   --  tree the tree he would like to check
255
256end A4G.Contt.TT;
257