1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              X R E F _ L I B                             --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1998-2018, 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--  Miscellaneous utilities for the cross-referencing tool
27
28with Hostparm;
29with Xr_Tabls;  use Xr_Tabls;
30
31with GNAT.Directory_Operations; use GNAT.Directory_Operations;
32with GNAT.OS_Lib;               use GNAT.OS_Lib;
33with GNAT.Dynamic_Tables;
34with GNAT.Regexp;               use GNAT.Regexp;
35
36package Xref_Lib is
37
38   subtype File_Name_String is String (1 .. Hostparm.Max_Name_Length);
39   subtype Line_String      is String (1 .. Hostparm.Max_Line_Length);
40
41   type ALI_File is limited private;
42
43   ---------------------
44   -- Directory Input --
45   ---------------------
46
47   type Rec_DIR is limited private;
48   --  This one is used for recursive search of .ali files
49
50   procedure Find_ALI_Files;
51   --  Find all the ali files that we will have to parse, and have them to
52   --  the file list
53
54   ---------------------
55   -- Search patterns --
56   ---------------------
57
58   type Search_Pattern is private;
59   type Search_Pattern_Ptr is access all Search_Pattern;
60
61   procedure Add_Entity
62     (Pattern : in out Search_Pattern;
63      Entity  : String;
64      Glob    : Boolean := False);
65   --  Add a new entity to the search pattern (the entity should have the
66   --  form pattern[:file[:line[:column]]], and it is parsed entirely in
67   --  this procedure. Glob indicates if we should use the 'globbing
68   --  patterns' (True) or the full regular expressions (False)
69
70   procedure Add_Xref_File (File : String);
71   --  Add a new file in the list of files to search for references. File
72   --  is interpreted as a globbing regular expression, which is expanded.
73
74   Invalid_Argument : exception;
75   --  Exception raised when there is a syntax error in the command line
76
77   -----------------------
78   -- Output Algorithms --
79   -----------------------
80
81   procedure Print_Gnatfind
82     (References     : Boolean;
83      Full_Path_Name : Boolean);
84   procedure Print_Unused (Full_Path_Name : Boolean);
85   procedure Print_Vi     (Full_Path_Name : Boolean);
86   procedure Print_Xref   (Full_Path_Name : Boolean);
87   --  The actual print procedures. These functions step through the symbol
88   --  table and print all the symbols if they match the files given on the
89   --  command line (they already match the entities if they are in the
90   --  symbol table)
91
92   ------------------------
93   -- General Algorithms --
94   ------------------------
95
96   function Default_Project_File (Dir_Name : String) return String;
97   --  Returns the default Project file name for the directory Dir_Name
98
99   procedure Search
100     (Pattern       : Search_Pattern;
101      Local_Symbols : Boolean;
102      Wide_Search   : Boolean;
103      Read_Only     : Boolean;
104      Der_Info      : Boolean;
105      Type_Tree     : Boolean);
106   --  Search every ALI file for entities matching Pattern, and add
107   --  these entities to the internal symbol tables.
108   --
109   --  If Wide_Search is True, all ALI files found in the object path
110   --  are searched.
111   --
112   --  If Read_Only is True, read-only ALI files will also be parsed,
113   --  similar to gnatmake -a.
114   --
115   --  If Der_Info is true, then the derived type information will be
116   --  processed.
117   --
118   --  If Type_Tree is true, then the type hierarchy will be searched
119   --  going from the pattern to the parent type.
120
121   procedure Search_Xref
122     (Local_Symbols : Boolean;
123      Read_Only     : Boolean;
124      Der_Info      : Boolean);
125   --  Search every ali file given in the command line and all their
126   --  dependencies. If Read_Only is True, we parse the read-only ali
127   --  files too. If Der_Mode is true then the derived type information will
128   --  be processed
129
130private
131   type Rec_DIR is limited record
132      Dir : GNAT.Directory_Operations.Dir_Type;
133   end record;
134
135   package Dependencies_Tables is new GNAT.Dynamic_Tables
136     (Table_Component_Type => Xr_Tabls.File_Reference,
137      Table_Index_Type     => Natural,
138      Table_Low_Bound      => 1,
139      Table_Initial        => 400,
140      Table_Increment      => 100);
141   use Dependencies_Tables;
142
143   type Dependencies is new Dependencies_Tables.Instance;
144
145   type ALI_File is limited record
146      Buffer : String_Access := null;
147      --  Buffer used to read the whole file at once
148
149      Current_Line : Positive;
150      --  Start of the current line in Buffer
151
152      Xref_Line : Positive;
153      --  Start of the xref lines in Buffer
154
155      X_File : Xr_Tabls.File_Reference;
156      --  Stores the cross-referencing file-name ("X..." lines), as an
157      --  index into the dependencies table
158
159      Dep : Dependencies;
160      --  Store file name associated with each number ("D..." lines)
161   end record;
162
163   --  The following record type stores all the patterns that are searched for
164
165   type Search_Pattern is record
166      Entity : GNAT.Regexp.Regexp;
167      --  A regular expression matching the entities we are looking for.
168      --  File is a list of the places where the declaration of the entities
169      --  has to be. When the user enters a file:line:column on the command
170      --  line, it is stored as "Entity_Name Declaration_File:line:column"
171
172      File_Ref : Xr_Tabls.File_Reference;
173      --  A reference to the source file, if any
174
175      Initialized : Boolean := False;
176      --  Set to True when Entity has been initialized
177   end record;
178
179end Xref_Lib;
180