blob: 73a09eae24fc2b6b720b1dda60ecbe97fbe4f255 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Function.cpp --------------------------------------------*- 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#include "lldb/Symbol/Function.h"
11#include "lldb/Core/Module.h"
12#include "lldb/Core/Section.h"
Greg Clayton1674b122010-07-21 22:12:05 +000013#include "lldb/Symbol/ClangASTType.h"
14#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Symbol/CompileUnit.h"
16#include "lldb/Symbol/LineTable.h"
17#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "clang/AST/Type.h"
19#include "clang/AST/CanonicalType.h"
20
21using namespace lldb_private;
22
23//----------------------------------------------------------------------
24// Basic function information is contained in the FunctionInfo class.
25// It is designed to contain the name, linkage name, and declaration
26// location.
27//----------------------------------------------------------------------
28FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) :
29 m_name(name),
30 m_declaration(decl_ptr)
31{
32}
33
34
35FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) :
36 m_name(name),
37 m_declaration(decl_ptr)
38{
39}
40
41
42FunctionInfo::~FunctionInfo()
43{
44}
45
46void
47FunctionInfo::Dump(Stream *s) const
48{
49 if (m_name)
50 *s << ", name = \"" << m_name << "\"";
51 m_declaration.Dump(s);
52}
53
54
55int
56FunctionInfo::Compare(const FunctionInfo& a, const FunctionInfo& b)
57{
58 int result = ConstString::Compare(a.GetName(), b.GetName());
59 if (result)
60 return result;
61
62 return Declaration::Compare(a.m_declaration, b.m_declaration);
63}
64
65
66Declaration&
67FunctionInfo::GetDeclaration()
68{
69 return m_declaration;
70}
71
72const Declaration&
73FunctionInfo::GetDeclaration() const
74{
75 return m_declaration;
76}
77
78const ConstString&
79FunctionInfo::GetName() const
80{
81 return m_name;
82}
83
84size_t
85FunctionInfo::MemorySize() const
86{
87 return m_name.MemorySize() + m_declaration.MemorySize();
88}
89
90
91InlineFunctionInfo::InlineFunctionInfo
92(
93 const char *name,
94 const char *mangled,
95 const Declaration *decl_ptr,
96 const Declaration *call_decl_ptr
97) :
98 FunctionInfo(name, decl_ptr),
99 m_mangled(mangled, true),
100 m_call_decl (call_decl_ptr)
101{
102}
103
104InlineFunctionInfo::InlineFunctionInfo
105(
106 const ConstString& name,
107 const Mangled &mangled,
108 const Declaration *decl_ptr,
109 const Declaration *call_decl_ptr
110) :
111 FunctionInfo(name, decl_ptr),
112 m_mangled(mangled),
113 m_call_decl (call_decl_ptr)
114{
115}
116
117InlineFunctionInfo::~InlineFunctionInfo()
118{
119}
120
121int
122InlineFunctionInfo::Compare(const InlineFunctionInfo& a, const InlineFunctionInfo& b)
123{
124
125 int result = FunctionInfo::Compare(a, b);
126 if (result)
127 return result;
128 // only compare the mangled names if both have them
129 return Mangled::Compare(a.m_mangled, a.m_mangled);
130}
131
132void
133InlineFunctionInfo::Dump(Stream *s) const
134{
135 FunctionInfo::Dump(s);
136 if (m_mangled)
137 m_mangled.Dump(s);
138}
139
140void
141InlineFunctionInfo::DumpStopContext (Stream *s) const
142{
143// s->Indent("[inlined] ");
144 s->Indent();
145 if (m_mangled)
146 s->PutCString (m_mangled.GetName().AsCString());
147 else
148 s->PutCString (m_name.AsCString());
149}
150
151Declaration &
152InlineFunctionInfo::GetCallSite ()
153{
154 return m_call_decl;
155}
156
157const Declaration &
158InlineFunctionInfo::GetCallSite () const
159{
160 return m_call_decl;
161}
162
163
164Mangled&
165InlineFunctionInfo::GetMangled()
166{
167 return m_mangled;
168}
169
170const Mangled&
171InlineFunctionInfo::GetMangled() const
172{
173 return m_mangled;
174}
175
176size_t
177InlineFunctionInfo::MemorySize() const
178{
179 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
180}
181
182//----------------------------------------------------------------------
183//
184//----------------------------------------------------------------------
185Function::Function
186(
187 CompileUnit *comp_unit,
188 lldb::user_id_t func_uid,
189 lldb::user_id_t type_uid,
190 const Mangled &mangled,
191 Type * type,
192 const AddressRange& range
193) :
Greg Clayton75ccf502010-08-21 02:22:51 +0000194 UserID (func_uid),
195 m_comp_unit (comp_unit),
196 m_type_uid (type_uid),
197 m_type (type),
198 m_mangled (mangled),
199 m_block (func_uid),
200 m_range (range),
201 m_frame_base (),
202 m_flags (),
203 m_prologue_byte_size (0)
Chris Lattner24943d22010-06-08 16:52:24 +0000204{
Greg Clayton75ccf502010-08-21 02:22:51 +0000205 m_block.SetParentScope(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000206 assert(comp_unit != NULL);
207}
208
209Function::Function
210(
211 CompileUnit *comp_unit,
212 lldb::user_id_t func_uid,
213 lldb::user_id_t type_uid,
214 const char *mangled,
215 Type *type,
216 const AddressRange &range
217) :
Greg Clayton75ccf502010-08-21 02:22:51 +0000218 UserID (func_uid),
219 m_comp_unit (comp_unit),
220 m_type_uid (type_uid),
221 m_type (type),
222 m_mangled (mangled, true),
223 m_block (func_uid),
224 m_range (range),
225 m_frame_base (),
226 m_flags (),
227 m_prologue_byte_size (0)
Chris Lattner24943d22010-06-08 16:52:24 +0000228{
Greg Clayton75ccf502010-08-21 02:22:51 +0000229 m_block.SetParentScope(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000230 assert(comp_unit != NULL);
231}
232
233
234Function::~Function()
235{
236}
237
Jim Inghamb75b4662010-08-20 01:15:01 +0000238void
239Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
240{
241 line_no = 0;
242 source_file.Clear();
243
244 if (m_comp_unit == NULL)
245 return;
246
247 if (m_type != NULL && m_type->GetDeclaration().GetLine() != 0)
248 {
249 source_file = m_type->GetDeclaration().GetFile();
250 line_no = m_type->GetDeclaration().GetLine();
251 }
252 else
253 {
254 LineTable *line_table = m_comp_unit->GetLineTable();
255 if (line_table == NULL)
256 return;
257
258 LineEntry line_entry;
259 if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, NULL))
260 {
261 line_no = line_entry.line;
262 source_file = line_entry.file;
263 }
264 }
265}
266
267void
268Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
269{
270 line_no = 0;
271 source_file.Clear();
272
273 // The -1 is kind of cheesy, but I want to get the last line entry for the given function, not the
274 // first entry of the next.
275 Address scratch_addr(GetAddressRange().GetBaseAddress());
276 scratch_addr.SetOffset (scratch_addr.GetOffset() + GetAddressRange().GetByteSize() - 1);
277
278 LineTable *line_table = m_comp_unit->GetLineTable();
279 if (line_table == NULL)
280 return;
281
282 LineEntry line_entry;
283 if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, NULL))
284 {
285 line_no = line_entry.line;
286 source_file = line_entry.file;
287 }
288}
289
Greg Clayton75ccf502010-08-21 02:22:51 +0000290Block &
291Function::GetBlock (bool can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000292{
Greg Clayton75ccf502010-08-21 02:22:51 +0000293 if (!m_block.BlockInfoHasBeenParsed() && can_create)
Chris Lattner24943d22010-06-08 16:52:24 +0000294 {
295 SymbolContext sc;
296 CalculateSymbolContext(&sc);
297 assert(sc.module_sp);
298 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
Greg Clayton75ccf502010-08-21 02:22:51 +0000299 m_block.SetBlockInfoHasBeenParsed (true, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000300 }
Greg Clayton75ccf502010-08-21 02:22:51 +0000301 return m_block;
Chris Lattner24943d22010-06-08 16:52:24 +0000302}
303
304CompileUnit*
305Function::GetCompileUnit()
306{
307 return m_comp_unit;
308}
309
310const CompileUnit*
311Function::GetCompileUnit() const
312{
313 return m_comp_unit;
314}
315
Greg Clayton12bec712010-06-28 21:30:43 +0000316
317void
318Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Process *process)
319{
320 Type* func_type = GetType();
321 *s << '"' << func_type->GetName() << "\", id = " << (const UserID&)*this;
322 *s << ", range = ";
323 GetAddressRange().Dump(s, process, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
324}
325
Chris Lattner24943d22010-06-08 16:52:24 +0000326void
327Function::Dump(Stream *s, bool show_context) const
328{
329 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
330 s->Indent();
331 *s << "Function" << (const UserID&)*this;
332
333 m_mangled.Dump(s);
334
335// FunctionInfo::Dump(s);
336 if (m_type)
337 {
338 *s << ", type = " << (void*)m_type;
339 /// << " (";
340 ///m_type->DumpTypeName(s);
341 ///s->PutChar(')');
342 }
343 else if (m_type_uid != LLDB_INVALID_UID)
344 *s << ", type_uid = " << m_type_uid;
345
346 s->EOL();
347 // Dump the root object
Greg Clayton75ccf502010-08-21 02:22:51 +0000348 if (m_block.BlockInfoHasBeenParsed ())
349 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context);
Chris Lattner24943d22010-06-08 16:52:24 +0000350}
351
352
353void
354Function::CalculateSymbolContext(SymbolContext* sc)
355{
356 sc->function = this;
357 m_comp_unit->CalculateSymbolContext(sc);
358}
359
360void
361Function::DumpSymbolContext(Stream *s)
362{
363 m_comp_unit->DumpSymbolContext(s);
364 s->Printf(", Function{0x%8.8x}", GetID());
365}
366
367size_t
368Function::MemorySize () const
369{
Greg Clayton75ccf502010-08-21 02:22:51 +0000370 size_t mem_size = sizeof(Function) + m_block.MemorySize();
Chris Lattner24943d22010-06-08 16:52:24 +0000371 return mem_size;
372}
373
374Type*
375Function::GetType()
376{
377 return m_type;
378}
379
380const Type*
381Function::GetType() const
382{
383 return m_type;
384}
385
386Type
387Function::GetReturnType ()
388{
389 clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType()));
390 assert (clang_type->isFunctionType());
391 clang::FunctionType *function_type = dyn_cast<clang::FunctionType> (clang_type);
392 clang::QualType fun_return_qualtype = function_type->getResultType();
393
Greg Clayton1674b122010-07-21 22:12:05 +0000394 const ConstString fun_return_name(ClangASTType::GetClangTypeName(fun_return_qualtype.getAsOpaquePtr()));
Chris Lattner24943d22010-06-08 16:52:24 +0000395
396 SymbolContext sc;
397 CalculateSymbolContext (&sc);
398 // Null out everything below the CompUnit 'cause we don't actually know these.
399
Greg Clayton960d6a42010-08-03 00:35:52 +0000400 size_t bit_size = ClangASTType::GetClangTypeBitWidth ((GetType()->GetClangASTContext().getASTContext()), fun_return_qualtype.getAsOpaquePtr());
Chris Lattner24943d22010-06-08 16:52:24 +0000401 Type return_type (0, GetType()->GetSymbolFile(), fun_return_name, bit_size, sc.comp_unit, 0, Type::eTypeUIDSynthetic, Declaration(), fun_return_qualtype.getAsOpaquePtr());
402 return return_type;
403}
404
405int
406Function::GetArgumentCount ()
407{
408 clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType()));
409 assert (clang_type->isFunctionType());
410 if (!clang_type->isFunctionProtoType())
411 return -1;
412
413 const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type);
414 if (function_proto_type != NULL)
415 return function_proto_type->getNumArgs();
416
417 return 0;
418}
419
420const Type
421Function::GetArgumentTypeAtIndex (size_t idx)
422{
423 clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType()));
424 assert (clang_type->isFunctionType());
425 if (!clang_type->isFunctionProtoType())
426 return Type();
427
428 const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type);
429 if (function_proto_type != NULL)
430 {
431 unsigned num_args = function_proto_type->getNumArgs();
432 if (idx >= num_args)
433 return Type();
434 clang::QualType arg_qualtype = (function_proto_type->arg_type_begin())[idx];
435
Greg Clayton1674b122010-07-21 22:12:05 +0000436 const ConstString arg_return_name(ClangASTType::GetClangTypeName(arg_qualtype.getAsOpaquePtr()));
Chris Lattner24943d22010-06-08 16:52:24 +0000437 SymbolContext sc;
438 CalculateSymbolContext (&sc);
439 // Null out everything below the CompUnit 'cause we don't actually know these.
440
Greg Clayton960d6a42010-08-03 00:35:52 +0000441 size_t bit_size = ClangASTType::GetClangTypeBitWidth ((GetType()->GetClangASTContext().getASTContext()), arg_qualtype.getAsOpaquePtr());
Chris Lattner24943d22010-06-08 16:52:24 +0000442 Type arg_type (0, GetType()->GetSymbolFile(), arg_return_name, bit_size, sc.comp_unit, 0, Type::eTypeUIDSynthetic, Declaration(), arg_qualtype.getAsOpaquePtr());
443 return arg_type;
444 }
445
446 return Type();
447}
448
449const char *
450Function::GetArgumentNameAtIndex (size_t idx)
451{
452 clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetOpaqueClangQualType())->getTypePtr();
453 assert (clang_type->isFunctionType());
454 if (!clang_type->isFunctionProtoType())
455 return NULL;
456 return NULL;
457}
458
459bool
460Function::IsVariadic ()
461{
462 const clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetOpaqueClangQualType())->getTypePtr();
463 assert (clang_type->isFunctionType());
464 if (!clang_type->isFunctionProtoType())
465 return false;
466
467 const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type);
468 if (function_proto_type != NULL)
469 {
470 return function_proto_type->isVariadic();
471 }
472
473 return false;
474}
475
476uint32_t
477Function::GetPrologueByteSize ()
478{
479 if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize))
480 {
481 m_flags.Set(flagsCalculatedPrologueSize);
482 LineTable* line_table = m_comp_unit->GetLineTable ();
483 if (line_table)
484 {
485 LineEntry line_entry;
486 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), line_entry))
487 m_prologue_byte_size = line_entry.range.GetByteSize();
488 }
489 }
490 return m_prologue_byte_size;
491}
492
493
494