Chris Lattner | 24943d2 | 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 | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 13 | #include "lldb/Symbol/ClangASTType.h" |
| 14 | #include "lldb/Symbol/ClangASTContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/CompileUnit.h" |
| 16 | #include "lldb/Symbol/LineTable.h" |
| 17 | #include "lldb/Symbol/SymbolVendor.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
| 19 | #include "clang/AST/CanonicalType.h" |
| 20 | |
| 21 | using 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 | //---------------------------------------------------------------------- |
| 28 | FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) : |
| 29 | m_name(name), |
| 30 | m_declaration(decl_ptr) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) : |
| 36 | m_name(name), |
| 37 | m_declaration(decl_ptr) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | |
| 42 | FunctionInfo::~FunctionInfo() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | FunctionInfo::Dump(Stream *s) const |
| 48 | { |
| 49 | if (m_name) |
| 50 | *s << ", name = \"" << m_name << "\""; |
| 51 | m_declaration.Dump(s); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int |
| 56 | FunctionInfo::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 | |
| 66 | Declaration& |
| 67 | FunctionInfo::GetDeclaration() |
| 68 | { |
| 69 | return m_declaration; |
| 70 | } |
| 71 | |
| 72 | const Declaration& |
| 73 | FunctionInfo::GetDeclaration() const |
| 74 | { |
| 75 | return m_declaration; |
| 76 | } |
| 77 | |
| 78 | const ConstString& |
| 79 | FunctionInfo::GetName() const |
| 80 | { |
| 81 | return m_name; |
| 82 | } |
| 83 | |
| 84 | size_t |
| 85 | FunctionInfo::MemorySize() const |
| 86 | { |
| 87 | return m_name.MemorySize() + m_declaration.MemorySize(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | InlineFunctionInfo::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 | |
| 104 | InlineFunctionInfo::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 | |
| 117 | InlineFunctionInfo::~InlineFunctionInfo() |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | int |
| 122 | InlineFunctionInfo::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 | |
| 132 | void |
| 133 | InlineFunctionInfo::Dump(Stream *s) const |
| 134 | { |
| 135 | FunctionInfo::Dump(s); |
| 136 | if (m_mangled) |
| 137 | m_mangled.Dump(s); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | InlineFunctionInfo::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 | |
| 151 | Declaration & |
| 152 | InlineFunctionInfo::GetCallSite () |
| 153 | { |
| 154 | return m_call_decl; |
| 155 | } |
| 156 | |
| 157 | const Declaration & |
| 158 | InlineFunctionInfo::GetCallSite () const |
| 159 | { |
| 160 | return m_call_decl; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | Mangled& |
| 165 | InlineFunctionInfo::GetMangled() |
| 166 | { |
| 167 | return m_mangled; |
| 168 | } |
| 169 | |
| 170 | const Mangled& |
| 171 | InlineFunctionInfo::GetMangled() const |
| 172 | { |
| 173 | return m_mangled; |
| 174 | } |
| 175 | |
| 176 | size_t |
| 177 | InlineFunctionInfo::MemorySize() const |
| 178 | { |
| 179 | return FunctionInfo::MemorySize() + m_mangled.MemorySize(); |
| 180 | } |
| 181 | |
| 182 | //---------------------------------------------------------------------- |
| 183 | // |
| 184 | //---------------------------------------------------------------------- |
| 185 | Function::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 Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 194 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | { |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 205 | m_block.SetParentScope(this); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | assert(comp_unit != NULL); |
| 207 | } |
| 208 | |
| 209 | Function::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 Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 218 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | { |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 229 | m_block.SetParentScope(this); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | assert(comp_unit != NULL); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | Function::~Function() |
| 235 | { |
| 236 | } |
| 237 | |
Jim Ingham | b75b466 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 238 | void |
| 239 | Function::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 | |
| 267 | void |
| 268 | Function::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 Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 290 | Block & |
| 291 | Function::GetBlock (bool can_create) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | { |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 293 | if (!m_block.BlockInfoHasBeenParsed() && can_create) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | { |
| 295 | SymbolContext sc; |
| 296 | CalculateSymbolContext(&sc); |
| 297 | assert(sc.module_sp); |
| 298 | sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 299 | m_block.SetBlockInfoHasBeenParsed (true, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 300 | } |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 301 | return m_block; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | CompileUnit* |
| 305 | Function::GetCompileUnit() |
| 306 | { |
| 307 | return m_comp_unit; |
| 308 | } |
| 309 | |
| 310 | const CompileUnit* |
| 311 | Function::GetCompileUnit() const |
| 312 | { |
| 313 | return m_comp_unit; |
| 314 | } |
| 315 | |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 316 | |
| 317 | void |
| 318 | Function::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | void |
| 327 | Function::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 Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 348 | if (m_block.BlockInfoHasBeenParsed ()) |
| 349 | m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | void |
| 354 | Function::CalculateSymbolContext(SymbolContext* sc) |
| 355 | { |
| 356 | sc->function = this; |
| 357 | m_comp_unit->CalculateSymbolContext(sc); |
| 358 | } |
| 359 | |
| 360 | void |
| 361 | Function::DumpSymbolContext(Stream *s) |
| 362 | { |
| 363 | m_comp_unit->DumpSymbolContext(s); |
| 364 | s->Printf(", Function{0x%8.8x}", GetID()); |
| 365 | } |
| 366 | |
| 367 | size_t |
| 368 | Function::MemorySize () const |
| 369 | { |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 370 | size_t mem_size = sizeof(Function) + m_block.MemorySize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 371 | return mem_size; |
| 372 | } |
| 373 | |
| 374 | Type* |
| 375 | Function::GetType() |
| 376 | { |
| 377 | return m_type; |
| 378 | } |
| 379 | |
| 380 | const Type* |
| 381 | Function::GetType() const |
| 382 | { |
| 383 | return m_type; |
| 384 | } |
| 385 | |
| 386 | Type |
| 387 | Function::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 Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 394 | const ConstString fun_return_name(ClangASTType::GetClangTypeName(fun_return_qualtype.getAsOpaquePtr())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | |
| 396 | SymbolContext sc; |
| 397 | CalculateSymbolContext (&sc); |
| 398 | // Null out everything below the CompUnit 'cause we don't actually know these. |
| 399 | |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 400 | size_t bit_size = ClangASTType::GetClangTypeBitWidth ((GetType()->GetClangASTContext().getASTContext()), fun_return_qualtype.getAsOpaquePtr()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | 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 | |
| 405 | int |
| 406 | Function::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 | |
| 420 | const Type |
| 421 | Function::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 Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 436 | const ConstString arg_return_name(ClangASTType::GetClangTypeName(arg_qualtype.getAsOpaquePtr())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 437 | SymbolContext sc; |
| 438 | CalculateSymbolContext (&sc); |
| 439 | // Null out everything below the CompUnit 'cause we don't actually know these. |
| 440 | |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 441 | size_t bit_size = ClangASTType::GetClangTypeBitWidth ((GetType()->GetClangASTContext().getASTContext()), arg_qualtype.getAsOpaquePtr()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | 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 | |
| 449 | const char * |
| 450 | Function::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 | |
| 459 | bool |
| 460 | Function::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 | |
| 476 | uint32_t |
| 477 | Function::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 | |