Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 13 | #include "lldb/Host/Host.h" |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/ClangASTType.h" |
| 15 | #include "lldb/Symbol/ClangASTContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/CompileUnit.h" |
| 17 | #include "lldb/Symbol/LineTable.h" |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/SymbolFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/SymbolVendor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
| 21 | #include "clang/AST/CanonicalType.h" |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Casting.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 24 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | using namespace lldb_private; |
| 26 | |
| 27 | //---------------------------------------------------------------------- |
| 28 | // Basic function information is contained in the FunctionInfo class. |
| 29 | // It is designed to contain the name, linkage name, and declaration |
| 30 | // location. |
| 31 | //---------------------------------------------------------------------- |
| 32 | FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) : |
| 33 | m_name(name), |
| 34 | m_declaration(decl_ptr) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | |
| 39 | FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) : |
| 40 | m_name(name), |
| 41 | m_declaration(decl_ptr) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | |
| 46 | FunctionInfo::~FunctionInfo() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 51 | FunctionInfo::Dump(Stream *s, bool show_fullpaths) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | { |
| 53 | if (m_name) |
| 54 | *s << ", name = \"" << m_name << "\""; |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 55 | m_declaration.Dump(s, show_fullpaths); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
| 59 | int |
| 60 | FunctionInfo::Compare(const FunctionInfo& a, const FunctionInfo& b) |
| 61 | { |
| 62 | int result = ConstString::Compare(a.GetName(), b.GetName()); |
| 63 | if (result) |
| 64 | return result; |
| 65 | |
| 66 | return Declaration::Compare(a.m_declaration, b.m_declaration); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | Declaration& |
| 71 | FunctionInfo::GetDeclaration() |
| 72 | { |
| 73 | return m_declaration; |
| 74 | } |
| 75 | |
| 76 | const Declaration& |
| 77 | FunctionInfo::GetDeclaration() const |
| 78 | { |
| 79 | return m_declaration; |
| 80 | } |
| 81 | |
| 82 | const ConstString& |
| 83 | FunctionInfo::GetName() const |
| 84 | { |
| 85 | return m_name; |
| 86 | } |
| 87 | |
| 88 | size_t |
| 89 | FunctionInfo::MemorySize() const |
| 90 | { |
| 91 | return m_name.MemorySize() + m_declaration.MemorySize(); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | InlineFunctionInfo::InlineFunctionInfo |
| 96 | ( |
| 97 | const char *name, |
| 98 | const char *mangled, |
| 99 | const Declaration *decl_ptr, |
| 100 | const Declaration *call_decl_ptr |
| 101 | ) : |
| 102 | FunctionInfo(name, decl_ptr), |
| 103 | m_mangled(mangled, true), |
| 104 | m_call_decl (call_decl_ptr) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | InlineFunctionInfo::InlineFunctionInfo |
| 109 | ( |
| 110 | const ConstString& name, |
| 111 | const Mangled &mangled, |
| 112 | const Declaration *decl_ptr, |
| 113 | const Declaration *call_decl_ptr |
| 114 | ) : |
| 115 | FunctionInfo(name, decl_ptr), |
| 116 | m_mangled(mangled), |
| 117 | m_call_decl (call_decl_ptr) |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | InlineFunctionInfo::~InlineFunctionInfo() |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | InlineFunctionInfo::Compare(const InlineFunctionInfo& a, const InlineFunctionInfo& b) |
| 127 | { |
| 128 | |
| 129 | int result = FunctionInfo::Compare(a, b); |
| 130 | if (result) |
| 131 | return result; |
| 132 | // only compare the mangled names if both have them |
| 133 | return Mangled::Compare(a.m_mangled, a.m_mangled); |
| 134 | } |
| 135 | |
| 136 | void |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 137 | InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | { |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 139 | FunctionInfo::Dump(s, show_fullpaths); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | if (m_mangled) |
| 141 | m_mangled.Dump(s); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | InlineFunctionInfo::DumpStopContext (Stream *s) const |
| 146 | { |
| 147 | // s->Indent("[inlined] "); |
| 148 | s->Indent(); |
| 149 | if (m_mangled) |
| 150 | s->PutCString (m_mangled.GetName().AsCString()); |
| 151 | else |
| 152 | s->PutCString (m_name.AsCString()); |
| 153 | } |
| 154 | |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 155 | |
| 156 | const ConstString & |
| 157 | InlineFunctionInfo::GetName () const |
| 158 | { |
| 159 | if (m_mangled) |
| 160 | return m_mangled.GetName(); |
| 161 | return m_name; |
| 162 | } |
| 163 | |
| 164 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | Declaration & |
| 166 | InlineFunctionInfo::GetCallSite () |
| 167 | { |
| 168 | return m_call_decl; |
| 169 | } |
| 170 | |
| 171 | const Declaration & |
| 172 | InlineFunctionInfo::GetCallSite () const |
| 173 | { |
| 174 | return m_call_decl; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | Mangled& |
| 179 | InlineFunctionInfo::GetMangled() |
| 180 | { |
| 181 | return m_mangled; |
| 182 | } |
| 183 | |
| 184 | const Mangled& |
| 185 | InlineFunctionInfo::GetMangled() const |
| 186 | { |
| 187 | return m_mangled; |
| 188 | } |
| 189 | |
| 190 | size_t |
| 191 | InlineFunctionInfo::MemorySize() const |
| 192 | { |
| 193 | return FunctionInfo::MemorySize() + m_mangled.MemorySize(); |
| 194 | } |
| 195 | |
| 196 | //---------------------------------------------------------------------- |
| 197 | // |
| 198 | //---------------------------------------------------------------------- |
| 199 | Function::Function |
| 200 | ( |
| 201 | CompileUnit *comp_unit, |
| 202 | lldb::user_id_t func_uid, |
| 203 | lldb::user_id_t type_uid, |
| 204 | const Mangled &mangled, |
| 205 | Type * type, |
| 206 | const AddressRange& range |
| 207 | ) : |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 208 | UserID (func_uid), |
| 209 | m_comp_unit (comp_unit), |
| 210 | m_type_uid (type_uid), |
| 211 | m_type (type), |
| 212 | m_mangled (mangled), |
| 213 | m_block (func_uid), |
| 214 | m_range (range), |
| 215 | m_frame_base (), |
| 216 | m_flags (), |
| 217 | m_prologue_byte_size (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 219 | m_block.SetParentScope(this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | assert(comp_unit != NULL); |
| 221 | } |
| 222 | |
| 223 | Function::Function |
| 224 | ( |
| 225 | CompileUnit *comp_unit, |
| 226 | lldb::user_id_t func_uid, |
| 227 | lldb::user_id_t type_uid, |
| 228 | const char *mangled, |
| 229 | Type *type, |
| 230 | const AddressRange &range |
| 231 | ) : |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 232 | UserID (func_uid), |
| 233 | m_comp_unit (comp_unit), |
| 234 | m_type_uid (type_uid), |
| 235 | m_type (type), |
| 236 | m_mangled (mangled, true), |
| 237 | m_block (func_uid), |
| 238 | m_range (range), |
| 239 | m_frame_base (), |
| 240 | m_flags (), |
| 241 | m_prologue_byte_size (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 243 | m_block.SetParentScope(this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | assert(comp_unit != NULL); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | Function::~Function() |
| 249 | { |
| 250 | } |
| 251 | |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 252 | void |
| 253 | Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no) |
| 254 | { |
| 255 | line_no = 0; |
| 256 | source_file.Clear(); |
| 257 | |
| 258 | if (m_comp_unit == NULL) |
| 259 | return; |
| 260 | |
| 261 | if (m_type != NULL && m_type->GetDeclaration().GetLine() != 0) |
| 262 | { |
| 263 | source_file = m_type->GetDeclaration().GetFile(); |
| 264 | line_no = m_type->GetDeclaration().GetLine(); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | LineTable *line_table = m_comp_unit->GetLineTable(); |
| 269 | if (line_table == NULL) |
| 270 | return; |
| 271 | |
| 272 | LineEntry line_entry; |
| 273 | if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, NULL)) |
| 274 | { |
| 275 | line_no = line_entry.line; |
| 276 | source_file = line_entry.file; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no) |
| 283 | { |
| 284 | line_no = 0; |
| 285 | source_file.Clear(); |
| 286 | |
| 287 | // The -1 is kind of cheesy, but I want to get the last line entry for the given function, not the |
| 288 | // first entry of the next. |
| 289 | Address scratch_addr(GetAddressRange().GetBaseAddress()); |
| 290 | scratch_addr.SetOffset (scratch_addr.GetOffset() + GetAddressRange().GetByteSize() - 1); |
| 291 | |
| 292 | LineTable *line_table = m_comp_unit->GetLineTable(); |
| 293 | if (line_table == NULL) |
| 294 | return; |
| 295 | |
| 296 | LineEntry line_entry; |
| 297 | if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, NULL)) |
| 298 | { |
| 299 | line_no = line_entry.line; |
| 300 | source_file = line_entry.file; |
| 301 | } |
| 302 | } |
| 303 | |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 304 | Block & |
| 305 | Function::GetBlock (bool can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 306 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 307 | if (!m_block.BlockInfoHasBeenParsed() && can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | { |
| 309 | SymbolContext sc; |
| 310 | CalculateSymbolContext(&sc); |
Greg Clayton | 17cc8b9 | 2011-06-24 03:47:23 +0000 | [diff] [blame] | 311 | if (sc.module_sp) |
| 312 | { |
| 313 | sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); |
| 314 | } |
| 315 | else |
| 316 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 317 | Host::SystemLog (Host::eSystemLogError, |
| 318 | "error: unable to find module shared pointer for function '%s' in %s%s%s\n", |
| 319 | GetName().GetCString(), |
| 320 | m_comp_unit->GetDirectory().GetCString(), |
| 321 | m_comp_unit->GetDirectory() ? "/" : "", |
| 322 | m_comp_unit->GetFilename().GetCString()); |
Greg Clayton | 17cc8b9 | 2011-06-24 03:47:23 +0000 | [diff] [blame] | 323 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 324 | m_block.SetBlockInfoHasBeenParsed (true, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 326 | return m_block; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | CompileUnit* |
| 330 | Function::GetCompileUnit() |
| 331 | { |
| 332 | return m_comp_unit; |
| 333 | } |
| 334 | |
| 335 | const CompileUnit* |
| 336 | Function::GetCompileUnit() const |
| 337 | { |
| 338 | return m_comp_unit; |
| 339 | } |
| 340 | |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 341 | |
| 342 | void |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 343 | Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 344 | { |
| 345 | Type* func_type = GetType(); |
Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 346 | *s << "id = " << (const UserID&)*this << ", name = \"" << func_type->GetName() << "\", range = "; |
| 347 | |
| 348 | Address::DumpStyle fallback_style; |
| 349 | if (level == eDescriptionLevelVerbose) |
| 350 | fallback_style = Address::DumpStyleModuleWithFileAddress; |
| 351 | else |
| 352 | fallback_style = Address::DumpStyleFileAddress; |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 353 | GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, fallback_style); |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 356 | void |
| 357 | Function::Dump(Stream *s, bool show_context) const |
| 358 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 359 | s->Printf("%p: ", this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | s->Indent(); |
| 361 | *s << "Function" << (const UserID&)*this; |
| 362 | |
| 363 | m_mangled.Dump(s); |
| 364 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 365 | if (m_type) |
| 366 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 367 | s->Printf(", type = %p", m_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | } |
| 369 | else if (m_type_uid != LLDB_INVALID_UID) |
Greg Clayton | cbc9eb4 | 2011-08-16 18:19:31 +0000 | [diff] [blame] | 370 | { |
Greg Clayton | 81c22f6 | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 371 | s->Printf(", type_uid = 0x%8.8llx", m_type_uid); |
Greg Clayton | cbc9eb4 | 2011-08-16 18:19:31 +0000 | [diff] [blame] | 372 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | |
| 374 | s->EOL(); |
| 375 | // Dump the root object |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 376 | if (m_block.BlockInfoHasBeenParsed ()) |
| 377 | m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | |
| 381 | void |
| 382 | Function::CalculateSymbolContext(SymbolContext* sc) |
| 383 | { |
| 384 | sc->function = this; |
| 385 | m_comp_unit->CalculateSymbolContext(sc); |
| 386 | } |
| 387 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 388 | ModuleSP |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 389 | Function::CalculateSymbolContextModule () |
| 390 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 391 | SectionSP section_sp (m_range.GetBaseAddress().GetSection()); |
| 392 | if (section_sp) |
Jim Ingham | 881ec85 | 2011-10-07 22:20:35 +0000 | [diff] [blame] | 393 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 394 | SectionSP linked_section_sp (section_sp->GetLinkedSection()); |
| 395 | if (linked_section_sp) |
| 396 | return linked_section_sp->GetModule(); |
Jim Ingham | 881ec85 | 2011-10-07 22:20:35 +0000 | [diff] [blame] | 397 | else |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 398 | return section_sp->GetModule(); |
Jim Ingham | 881ec85 | 2011-10-07 22:20:35 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 401 | return this->GetCompileUnit()->GetModule(); |
| 402 | } |
| 403 | |
| 404 | CompileUnit * |
| 405 | Function::CalculateSymbolContextCompileUnit () |
| 406 | { |
| 407 | return this->GetCompileUnit(); |
| 408 | } |
| 409 | |
| 410 | Function * |
| 411 | Function::CalculateSymbolContextFunction () |
| 412 | { |
| 413 | return this; |
| 414 | } |
| 415 | |
| 416 | //Symbol * |
| 417 | //Function::CalculateSymbolContextSymbol () |
| 418 | //{ |
| 419 | // return // TODO: find the symbol for the function??? |
| 420 | //} |
| 421 | |
| 422 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 423 | void |
| 424 | Function::DumpSymbolContext(Stream *s) |
| 425 | { |
| 426 | m_comp_unit->DumpSymbolContext(s); |
Greg Clayton | 81c22f6 | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 427 | s->Printf(", Function{0x%8.8llx}", GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | size_t |
| 431 | Function::MemorySize () const |
| 432 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 433 | size_t mem_size = sizeof(Function) + m_block.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 434 | return mem_size; |
| 435 | } |
| 436 | |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 437 | clang::DeclContext * |
| 438 | Function::GetClangDeclContext() |
| 439 | { |
| 440 | SymbolContext sc; |
| 441 | |
| 442 | CalculateSymbolContext (&sc); |
| 443 | |
| 444 | if (!sc.module_sp) |
| 445 | return NULL; |
| 446 | |
| 447 | SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); |
| 448 | |
| 449 | if (!sym_vendor) |
| 450 | return NULL; |
| 451 | |
| 452 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 453 | |
| 454 | if (!sym_file) |
| 455 | return NULL; |
| 456 | |
| 457 | return sym_file->GetClangDeclContextForTypeUID (sc, m_uid); |
| 458 | } |
| 459 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | Type* |
| 461 | Function::GetType() |
| 462 | { |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 463 | if (m_type == NULL) |
| 464 | { |
| 465 | SymbolContext sc; |
| 466 | |
| 467 | CalculateSymbolContext (&sc); |
| 468 | |
| 469 | if (!sc.module_sp) |
| 470 | return NULL; |
| 471 | |
| 472 | SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); |
| 473 | |
| 474 | if (sym_vendor == NULL) |
| 475 | return NULL; |
| 476 | |
| 477 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 478 | |
| 479 | if (sym_file == NULL) |
| 480 | return NULL; |
| 481 | |
Greg Clayton | 5cf58b9 | 2011-10-05 22:22:08 +0000 | [diff] [blame] | 482 | m_type = sym_file->ResolveTypeUID(m_type_uid); |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 483 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | return m_type; |
| 485 | } |
| 486 | |
| 487 | const Type* |
| 488 | Function::GetType() const |
| 489 | { |
| 490 | return m_type; |
| 491 | } |
| 492 | |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 493 | clang_type_t |
| 494 | Function::GetReturnClangType () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 495 | { |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 496 | clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetClangFullType())); |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 497 | const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType> (clang_type); |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 498 | if (function_type) |
| 499 | return function_type->getResultType().getAsOpaquePtr(); |
| 500 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | int |
| 504 | Function::GetArgumentCount () |
| 505 | { |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 506 | clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetClangFullType())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | assert (clang_type->isFunctionType()); |
| 508 | if (!clang_type->isFunctionProtoType()) |
| 509 | return -1; |
| 510 | |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 511 | const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(clang_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 512 | if (function_proto_type != NULL) |
| 513 | return function_proto_type->getNumArgs(); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 518 | clang_type_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 519 | Function::GetArgumentTypeAtIndex (size_t idx) |
| 520 | { |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 521 | clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetClangFullType())); |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 522 | const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(clang_type); |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 523 | if (function_proto_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 524 | { |
| 525 | unsigned num_args = function_proto_type->getNumArgs(); |
| 526 | if (idx >= num_args) |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | |
| 529 | return (function_proto_type->arg_type_begin())[idx].getAsOpaquePtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 530 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | bool |
| 535 | Function::IsVariadic () |
| 536 | { |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 537 | const clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetClangFullType())->getTypePtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 538 | assert (clang_type->isFunctionType()); |
| 539 | if (!clang_type->isFunctionProtoType()) |
| 540 | return false; |
| 541 | |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 542 | const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(clang_type); |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 543 | if (function_proto_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 544 | return function_proto_type->isVariadic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 545 | |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | uint32_t |
| 550 | Function::GetPrologueByteSize () |
| 551 | { |
| 552 | if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize)) |
| 553 | { |
| 554 | m_flags.Set(flagsCalculatedPrologueSize); |
| 555 | LineTable* line_table = m_comp_unit->GetLineTable (); |
| 556 | if (line_table) |
| 557 | { |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame^] | 558 | LineEntry first_line_entry; |
| 559 | uint32_t first_line_entry_idx = UINT32_MAX; |
| 560 | if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), first_line_entry, &first_line_entry_idx)) |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 561 | { |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame^] | 562 | // Make sure the first line entry isn't already the end of the prologue |
| 563 | addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS; |
| 564 | if (first_line_entry.is_prologue_end) |
| 565 | { |
| 566 | prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress(); |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | // Check the first few instructions and look for one that has |
| 571 | // is_prologue_end set to true. |
| 572 | const uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
| 573 | LineEntry line_entry; |
| 574 | for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx) |
| 575 | { |
| 576 | if (line_table->GetLineEntryAtIndex (idx, line_entry)) |
| 577 | { |
| 578 | if (line_entry.is_prologue_end) |
| 579 | { |
| 580 | prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress(); |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // If we didn't find the end of the prologue in the line tables, |
| 588 | // then just use the end address of the first line table entry |
| 589 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) |
| 590 | { |
| 591 | prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress() + first_line_entry.range.GetByteSize(); |
| 592 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 593 | const addr_t func_start_file_addr = m_range.GetBaseAddress().GetFileAddress(); |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame^] | 594 | const addr_t func_end_file_addr = func_start_file_addr + m_range.GetByteSize(); |
| 595 | |
| 596 | // Verify that this prologue end file address in the function's |
| 597 | // address range just to be sure |
| 598 | if (func_start_file_addr < prologue_end_file_addr && prologue_end_file_addr < func_end_file_addr) |
| 599 | { |
| 600 | m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr; |
| 601 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | return m_prologue_byte_size; |
| 606 | } |
| 607 | |
| 608 | |
| 609 | |