b5f9402a19695c7113d8190eff520539b727623c
[oota-llvm.git] / include / llvm / Debugger / ProgramInfo.h
1 //===- ProgramInfo.h - Information about the loaded program -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines various pieces of information about the currently loaded
11 // program.  One instance of this object is created every time a program is
12 // loaded, and destroyed every time it is unloaded.
13 //
14 // The various pieces of information gathered about the source program are all
15 // designed to be extended by various SourceLanguage implementations.  This
16 // allows source languages to keep any extended information that they support in
17 // the derived class portions of the class.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_DEBUGGER_PROGRAMINFO_H
22 #define LLVM_DEBUGGER_PROGRAMINFO_H
23
24 #include "llvm/System/TimeValue.h"
25 #include <string>
26 #include <map>
27 #include <vector>
28
29 namespace llvm {
30   class GlobalVariable;
31   class Module;
32   class SourceFile;
33   class SourceLanguage;
34   class ProgramInfo;
35
36   /// SourceLanguageCache - SourceLanguage implementations are allowed to cache
37   /// stuff in the ProgramInfo object.  The only requirement we have on these
38   /// instances is that they are destroyable.
39   struct SourceLanguageCache {
40     virtual ~SourceLanguageCache() {}
41   };
42
43   /// SourceFileInfo - One instance of this structure is created for each
44   /// source file in the program.
45   ///
46   class SourceFileInfo {
47     /// BaseName - The filename of the source file.
48     std::string BaseName;
49
50     /// Directory - The working directory of this source file when it was
51     /// compiled.
52     std::string Directory;
53
54     /// Version - The version of the LLVM debug information that this file was
55     /// compiled with.
56     unsigned Version;
57
58     /// Language - The source language that the file was compiled with.  This
59     /// pointer is never null.
60     ///
61     const SourceLanguage *Language;
62
63     /// Descriptor - The LLVM Global Variable which describes the source file.
64     ///
65     const GlobalVariable *Descriptor;
66
67     /// SourceText - The body of this source file, or null if it has not yet
68     /// been loaded.
69     mutable SourceFile *SourceText;
70   public:
71     SourceFileInfo(const GlobalVariable *Desc, const SourceLanguage &Lang);
72     ~SourceFileInfo();
73
74     const std::string &getBaseName() const { return BaseName; }
75     const std::string &getDirectory() const { return Directory; }
76     unsigned getDebugVersion() const { return Version; }
77     const GlobalVariable *getDescriptor() const { return Descriptor; }
78     SourceFile &getSourceText() const;
79
80     const SourceLanguage &getLanguage() const { return *Language; }
81   };
82
83
84   /// SourceFunctionInfo - An instance of this class is used to represent each
85   /// source function in the program.
86   ///
87   class SourceFunctionInfo {
88     /// Name - This contains an abstract name that is potentially useful to the
89     /// end-user.  If there is no explicit support for the current language,
90     /// then this string is used to identify the function.
91     std::string Name;
92
93     /// Descriptor - The descriptor for this function.
94     ///
95     const GlobalVariable *Descriptor;
96
97     /// SourceFile - The file that this function is defined in.
98     ///
99     const SourceFileInfo *SourceFile;
100
101     /// LineNo, ColNo - The location of the first stop-point in the function.
102     /// These are computed on demand.
103     mutable unsigned LineNo, ColNo;
104
105   public:
106     SourceFunctionInfo(ProgramInfo &PI, const GlobalVariable *Desc);
107     virtual ~SourceFunctionInfo() {}
108
109     /// getSymbolicName - Return a human-readable symbolic name to identify the
110     /// function (for example, in stack traces).
111     virtual std::string getSymbolicName() const { return Name; }
112
113     /// getDescriptor - This returns the descriptor for the function.
114     ///
115     const GlobalVariable *getDescriptor() const { return Descriptor; }
116
117     /// getSourceFile - This returns the source file that defines the function.
118     ///
119     const SourceFileInfo &getSourceFile() const { return *SourceFile; }
120
121     /// getSourceLocation - This method returns the location of the first
122     /// stopping point in the function.  If the body of the function cannot be
123     /// found, this returns zeros for both values.
124     void getSourceLocation(unsigned &LineNo, unsigned &ColNo) const;
125   };
126
127
128   /// ProgramInfo - This object contains information about the loaded program.
129   /// When a new program is loaded, an instance of this class is created.  When
130   /// the program is unloaded, the instance is destroyed.  This object basically
131   /// manages the lazy computation of information useful for the debugger.
132   class ProgramInfo {
133     Module *M;
134
135     /// ProgramTimeStamp - This is the timestamp of the executable file that we
136     /// currently have loaded into the debugger.
137     sys::TimeValue ProgramTimeStamp;
138
139     /// SourceFiles - This map is used to transform source file descriptors into
140     /// their corresponding SourceFileInfo objects.  This mapping owns the
141     /// memory for the SourceFileInfo objects.
142     ///
143     bool SourceFilesIsComplete;
144     std::map<const GlobalVariable*, SourceFileInfo*> SourceFiles;
145
146     /// SourceFileIndex - Mapping from source file basenames to the information
147     /// about the file.  Note that there can be filename collisions, so this is
148     /// a multimap.  This map is populated incrementally as the user interacts
149     /// with the program, through the getSourceFileFromDesc method.  If ALL of
150     /// the source files are needed, the getSourceFiles() method scans the
151     /// entire program looking for them.
152     ///
153     std::multimap<std::string, SourceFileInfo*> SourceFileIndex;
154
155     /// SourceFunctions - This map contains entries functions in the source
156     /// program.  If SourceFunctionsIsComplete is true, then this is ALL of the
157     /// functions in the program are in this map.
158     bool SourceFunctionsIsComplete;
159     std::map<const GlobalVariable*, SourceFunctionInfo*> SourceFunctions;
160
161     /// LanguageCaches - Each source language is permitted to keep a per-program
162     /// cache of information specific to whatever it needs.  This vector is
163     /// effectively a small map from the languages that are active in the
164     /// program to their caches.  This can be accessed by the language by the
165     /// "getLanguageCache" method.
166     std::vector<std::pair<const SourceLanguage*,
167                           SourceLanguageCache*> > LanguageCaches;
168   public:
169     ProgramInfo(Module *m);
170     ~ProgramInfo();
171
172     /// getProgramTimeStamp - Return the time-stamp of the program when it was
173     /// loaded.
174     sys::TimeValue getProgramTimeStamp() const { return ProgramTimeStamp; }
175
176     //===------------------------------------------------------------------===//
177     // Interfaces to the source code files that make up the program.
178     //
179
180     /// getSourceFile - Return source file information for the specified source
181     /// file descriptor object, adding it to the collection as needed.  This
182     /// method always succeeds (is unambiguous), and is always efficient.
183     ///
184     const SourceFileInfo &getSourceFile(const GlobalVariable *Desc);
185
186     /// getSourceFile - Look up the file with the specified name.  If there is
187     /// more than one match for the specified filename, prompt the user to pick
188     /// one.  If there is no source file that matches the specified name, throw
189     /// an exception indicating that we can't find the file.  Otherwise, return
190     /// the file information for that file.
191     ///
192     /// If the source file hasn't been discovered yet in the program, this
193     /// method might have to index the whole program by calling the
194     /// getSourceFiles() method.
195     ///
196     const SourceFileInfo &getSourceFile(const std::string &Filename);
197
198     /// getSourceFiles - Index all of the source files in the program and return
199     /// them.  This information is lazily computed the first time that it is
200     /// requested.  Since this information can take a long time to compute, the
201     /// user is given a chance to cancel it.  If this occurs, an exception is
202     /// thrown.
203     const std::map<const GlobalVariable*, SourceFileInfo*> &
204     getSourceFiles(bool RequiresCompleteMap = true);
205
206     //===------------------------------------------------------------------===//
207     // Interfaces to the functions that make up the program.
208     //
209
210     /// getFunction - Return source function information for the specified
211     /// function descriptor object, adding it to the collection as needed.  This
212     /// method always succeeds (is unambiguous), and is always efficient.
213     ///
214     const SourceFunctionInfo &getFunction(const GlobalVariable *Desc);
215
216     /// getSourceFunctions - Index all of the functions in the program and
217     /// return them.  This information is lazily computed the first time that it
218     /// is requested.  Since this information can take a long time to compute,
219     /// the user is given a chance to cancel it.  If this occurs, an exception
220     /// is thrown.
221     const std::map<const GlobalVariable*, SourceFunctionInfo*> &
222     getSourceFunctions(bool RequiresCompleteMap = true);
223
224     /// addSourceFunctionsRead - Return true if the source functions map is
225     /// complete: that is, all functions in the program have been read in.
226     bool allSourceFunctionsRead() const { return SourceFunctionsIsComplete; }
227
228     /// getLanguageCache - This method is used to build per-program caches of
229     /// information, such as the functions or types visible to the program.
230     /// This can be used by SourceLanguage implementations because it requires
231     /// an accessible [sl]::CacheType typedef, where [sl] is the C++ type of the
232     /// source-language subclass.
233     template<typename SL>
234     typename SL::CacheType &getLanguageCache(const SL *L) {
235       for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
236         if (LanguageCaches[i].first == L)
237           return *(typename SL::CacheType*)LanguageCaches[i].second;
238       typename SL::CacheType *NewCache = L->createSourceLanguageCache(*this);
239       LanguageCaches.push_back(std::make_pair(L, NewCache));
240       return *NewCache;
241     }
242   };
243
244 } // end namespace llvm
245
246 #endif