1 //===-- CompileUnit.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_CompUnit_h_
11 #define liblldb_CompUnit_h_
12 
13 #include "lldb/Symbol/Function.h"
14 #include "lldb/Core/FileSpecList.h"
15 #include "lldb/Core/Language.h"
16 #include "lldb/Core/ModuleChild.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/UserID.h"
19 
20 namespace lldb_private {
21 //----------------------------------------------------------------------
22 /// @class CompileUnit CompileUnit.h "lldb/Symbol/CompileUnit.h"
23 /// @brief A class that describes a compilation unit.
24 ///
25 /// A representation of a compilation unit, or compiled source file.
26 /// The UserID of the compile unit is specified by the SymbolFile
27 /// plug-in and can have any value as long as the value is unique
28 /// within the Module that owns this compile units.
29 ///
30 /// Each compile unit has a list of functions, global and static
31 /// variables, support file list (include files and inlined source
32 /// files), and a line table.
33 //----------------------------------------------------------------------
34 class CompileUnit :
35     public ModuleChild,
36     public FileSpec,
37     public UserID,
38     public Language,
39     public SymbolContextScope
40 {
41 public:
42     //------------------------------------------------------------------
43     /// Construct with a module, path, UID and language.
44     ///
45     /// Initialize the compile unit given the owning \a module, a path
46     /// to convert into a FileSpec, the SymbolFile plug-in supplied
47     /// \a uid, and the source language type.
48     ///
49     /// @param[in] module
50     ///     The parent module that owns this compile unit. This value
51     ///     must be a valid pointer value.
52     ///
53     /// @param[in] user_data
54     ///     User data where the SymbolFile parser can store data.
55     ///
56     /// @param[in] pathname
57     ///     The path to the source file for this compile unit.
58     ///
59     /// @param[in] uid
60     ///     The user ID of the compile unit. This value is supplied by
61     ///     the SymbolFile plug-in and should be a value that allows
62     ///     the SymbolFile plug-in to easily locate and parse additional
63     ///     information for the compile unit.
64     ///
65     /// @param[in] language
66     ///     A language enumeration type that describes the main language
67     ///     of this compile unit.
68     ///
69     /// @see lldb::LanguageType
70     //------------------------------------------------------------------
71     CompileUnit(Module *module, void *user_data, const char *pathname, lldb::user_id_t uid, lldb::LanguageType language);
72 
73     //------------------------------------------------------------------
74     /// Construct with a module, file spec, UID and language.
75     ///
76     /// Initialize the compile unit given the owning \a module, a path
77     /// to convert into a FileSpec, the SymbolFile plug-in supplied
78     /// \a uid, and the source language type.
79     ///
80     /// @param[in] module
81     ///     The parent module that owns this compile unit. This value
82     ///     must be a valid pointer value.
83     ///
84     /// @param[in] user_data
85     ///     User data where the SymbolFile parser can store data.
86     ///
87     /// @param[in] file_spec
88     ///     The file specification for the source file of this compile
89     ///     unit.
90     ///
91     /// @param[in] uid
92     ///     The user ID of the compile unit. This value is supplied by
93     ///     the SymbolFile plug-in and should be a value that allows
94     ///     the plug-in to easily locate and parse
95     ///     additional information for the compile unit.
96     ///
97     /// @param[in] language
98     ///     A language enumeration type that describes the main language
99     ///     of this compile unit.
100     ///
101     /// @see lldb::LanguageType
102     //------------------------------------------------------------------
103     CompileUnit(Module *module, void *user_data, const FileSpec &file_spec, lldb::user_id_t uid, lldb::LanguageType language);
104 
105     //------------------------------------------------------------------
106     /// Destructor
107     //------------------------------------------------------------------
108     ~CompileUnit();
109 
110     //------------------------------------------------------------------
111     /// Add a function to this compile unit.
112     ///
113     /// Typically called by the SymbolFile plug-ins as they partially
114     /// parse the debug information.
115     ///
116     /// @param[in] function_sp
117     ///     A shared pointer to the a Function object.
118     //------------------------------------------------------------------
119     void
120     AddFunction(lldb::FunctionSP& function_sp);
121 
122     //------------------------------------------------------------------
123     /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
124     ///
125     /// @see SymbolContextScope
126     //------------------------------------------------------------------
127     virtual void
128     CalculateSymbolContext(SymbolContext* sc);
129 
130     virtual Module *
131     CalculateSymbolContextModule ();
132 
133     virtual CompileUnit *
134     CalculateSymbolContextCompileUnit ();
135 
136     //------------------------------------------------------------------
137     /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
138     ///
139     /// @see SymbolContextScope
140     //------------------------------------------------------------------
141     virtual void
142     DumpSymbolContext(Stream *s);
143 
144 
145     void
146     GetDescription(Stream *s, lldb::DescriptionLevel level) const;
147 
148     //------------------------------------------------------------------
149     /// Get a shared pointer to a function in this compile unit by
150     /// index.
151     ///
152     /// Typically called when iterating though all functions in a
153     /// compile unit after all functions have been parsed. This provides
154     /// raw access to the function shared pointer list and will not
155     /// cause the SymbolFile plug-in to parse any unparsed functions.
156     ///
157     /// @param[in] idx
158     ///     An index into the function list.
159     ///
160     /// @return
161     ///     A shared pointer to a function that might contain a NULL
162     ///     Function class pointer.
163     //------------------------------------------------------------------
164     lldb::FunctionSP
165     GetFunctionAtIndex (size_t idx);
166 
167     //------------------------------------------------------------------
168     /// Dump the compile unit contents to the stream \a s.
169     ///
170     /// @param[in] s
171     ///     The stream to which to dump the object descripton.
172     ///
173     /// @param[in] show_context
174     ///     If \b true, variables will dump their symbol context
175     ///     information.
176     //------------------------------------------------------------------
177     void
178     Dump (Stream *s, bool show_context) const;
179 
180     //------------------------------------------------------------------
181     /// Find the line entry by line and optional inlined file spec.
182     ///
183     /// Finds the first line entry that has an index greater than
184     /// \a start_idx that matches \a line. If \a file_spec_ptr
185     /// is NULL, then the search matches line entries whose file matches
186     /// the file for the compile unit. If \a file_spec_ptr is
187     /// not NULL, line entries must match the specified file spec (for
188     /// inlined line table entries).
189     ///
190     /// Multiple calls to this function can find all entries that match
191     /// a given file and line by starting with \a start_idx equal to zero,
192     /// and calling this function back with the return valeu + 1.
193     ///
194     /// @param[in] start_idx
195     ///     The zero based index at which to start looking for matches.
196     ///
197     /// @param[in] line
198     ///     The line number to search for.
199     ///
200     /// @param[in] file_spec_ptr
201     ///     If non-NULL search for entries that match this file spec,
202     ///     else if NULL, search for line entries that match the compile
203     ///     unit file.
204     ///
205     /// @param[out] line_entry
206     ///     If non-NULL, a copy of the line entry that was found.
207     ///
208     /// @return
209     ///     The zero based index of a matching line entry, or UINT32_MAX
210     ///     if no matching line entry is found.
211     //------------------------------------------------------------------
212     uint32_t
213     FindLineEntry (uint32_t start_idx,
214                    uint32_t line,
215                    const FileSpec* file_spec_ptr,
216                    LineEntry *line_entry);
217 
218     //------------------------------------------------------------------
219     /// Get the line table for the compile unit.
220     ///
221     /// Called by clients and the SymbolFile plug-in. The SymbolFile
222     /// plug-ins use this function to determine if the line table has
223     /// be parsed yet. Clients use this function to get the line table
224     /// from a compile unit.
225     ///
226     /// @return
227     ///     The line table object pointer, or NULL if this line table
228     ///     hasn't been parsed yet.
229     //------------------------------------------------------------------
230     LineTable*
231     GetLineTable ();
232 
233     //------------------------------------------------------------------
234     /// Get the compile unit's support file list.
235     ///
236     /// The support file list is used by the line table, and any objects
237     /// that have valid Declaration objects.
238     ///
239     /// @return
240     ///     A support file list object.
241     //------------------------------------------------------------------
242     FileSpecList&
243     GetSupportFiles ();
244 
245     //------------------------------------------------------------------
246     /// Get the SymbolFile plug-in user data.
247     ///
248     /// SymbolFile plug-ins can store user data to internal state or
249     /// objects to quickly allow them to parse more information for a
250     /// given object.
251     ///
252     /// @return
253     ///     The user data stored with the CompileUnit when it was
254     ///     constructed.
255     //------------------------------------------------------------------
256     void *
257     GetUserData () const;
258 
259     //------------------------------------------------------------------
260     /// Get the variable list for a compile unit.
261     ///
262     /// Called by clients to get the variable list for a compile unit.
263     /// The variable list will contain all global and static variables
264     /// that were defined at the compile unit level.
265     ///
266     /// @param[in] can_create
267     ///     If \b true, the variable list will be parsed on demand. If
268     ///     \b false, the current variable list will be returned even
269     ///     if it contains a NULL VariableList object (typically
270     ///     called by dumping routines that want to display only what
271     ///     has currently been parsed).
272     ///
273     /// @return
274     ///     A shared pointer to a variable list, that can contain NULL
275     ///     VariableList pointer if there are no global or static
276     ///     variables.
277     //------------------------------------------------------------------
278     lldb::VariableListSP
279     GetVariableList (bool can_create);
280 
281     //------------------------------------------------------------------
282     /// Finds a function by user ID.
283     ///
284     /// Typically used by SymbolFile plug-ins when partially parsing
285     /// the debug information to see if the function has been parsed
286     /// yet.
287     ///
288     /// @param[in] uid
289     ///     The user ID of the function to find. This value is supplied
290     ///     by the SymbolFile plug-in and should be a value that
291     ///     allows the plug-in to easily locate and parse additional
292     ///     information in the function.
293     ///
294     /// @return
295     ///     A shared pointer to the function object that might contain
296     ///     a NULL Function pointer.
297     //------------------------------------------------------------------
298     lldb::FunctionSP
299     FindFunctionByUID (lldb::user_id_t uid);
300 
301     //------------------------------------------------------------------
302     /// Set the line table for the compile unit.
303     ///
304     /// Called by the SymbolFile plug-in when if first parses the line
305     /// table and hands ownership of the line table to this object. The
306     /// compile unit owns the line table object and will delete the
307     /// object when it is deleted.
308     ///
309     /// @param[in] line_table
310     ///     A line table object pointer that this object now owns.
311     //------------------------------------------------------------------
312     void
313     SetLineTable(LineTable* line_table);
314 
315     //------------------------------------------------------------------
316     /// Set accessor for the variable list.
317     ///
318     /// Called by the SymbolFile plug-ins after they have parsed the
319     /// variable lists and are ready to hand ownership of the list over
320     /// to this object.
321     ///
322     /// @param[in] variable_list_sp
323     ///     A shared pointer to a VariableList.
324     //------------------------------------------------------------------
325     void
326     SetVariableList (lldb::VariableListSP& variable_list_sp);
327 
328     //------------------------------------------------------------------
329     /// Resolve symbol contexts by file and line.
330     ///
331     /// Given a file in \a file_spec, and a line number, find all
332     /// instances and append them to the supplied symbol context list
333     /// \a sc_list.
334     ///
335     /// @param[in] file_spec
336     ///     A file specification. If \a file_spec contains no directory
337     ///     information, only the basename will be used when matching
338     ///     contexts. If the directory in \a file_spec is valid, a
339     ///     complete file specification match will be performed.
340     ///
341     /// @param[in] line
342     ///     The line number to match against the compile unit's line
343     ///     tables.
344     ///
345     /// @param[in] check_inlines
346     ///     If \b true this function will also match any inline
347     ///     file and line matches. If \b false, the compile unit's
348     ///     file specification must match \a file_spec for any matches
349     ///     to be returned.
350     ///
351     /// @param[in] exact
352     ///     If true, only resolve the context if \a line exists in the line table.
353     ///     If false, resolve the context to the closest line greater than \a line
354     ///     in the line table.
355     ///
356     /// @param[in] resolve_scope
357     ///     For each matching line entry, this bitfield indicates what
358     ///     values within each SymbolContext that gets added to \a
359     ///     sc_list will be resolved. See the SymbolContext::Scope
360     ///     enumeration for a list of all available bits that can be
361     ///     resolved. Only SymbolContext entries that can be resolved
362     ///     using a LineEntry base address will be able to be resolved.
363     ///
364     /// @param[out] sc_list
365     ///     A SymbolContext list class that willl get any matching
366     ///     entries appended to.
367     ///
368     /// @return
369     ///     The number of new matches that were added to \a sc_list.
370     ///
371     /// @see enum SymbolContext::Scope
372     //------------------------------------------------------------------
373     uint32_t
374     ResolveSymbolContext (const FileSpec& file_spec,
375                           uint32_t line,
376                           bool check_inlines,
377                           bool exact,
378                           uint32_t resolve_scope,
379                           SymbolContextList &sc_list);
380 
381 
382 protected:
383     void *m_user_data; ///< User data for the SymbolFile parser to store information into.
384     Flags m_flags; ///< Compile unit flags that help with partial parsing.
385     std::vector<lldb::FunctionSP> m_functions; ///< The sparsely populated list of shared pointers to functions
386                                          ///< that gets populated as functions get partially parsed.
387     FileSpecList m_support_files; ///< Files associated with this compile unit's line table and declarations.
388     std::auto_ptr<LineTable> m_line_table_ap; ///< Line table that will get parsed on demand.
389     lldb::VariableListSP m_variables; ///< Global and static variable list that will get parsed on demand.
390 
391 private:
392     enum
393     {
394         flagsParsedAllFunctions = (1 << 0), ///< Have we already parsed all our functions
395         flagsParsedVariables    = (1 << 1), ///< Have we already parsed globals and statics?
396         flagsParsedSupportFiles = (1 << 2), ///< Have we already parsed the support files for this compile unit?
397         flagsParsedLineTable    = (1 << 3)  ///< Have we parsed the line table already?
398     };
399 
400     DISALLOW_COPY_AND_ASSIGN (CompileUnit);
401 };
402 
403 } // namespace lldb_private
404 
405 #endif  // liblldb_CompUnit_h_
406