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 | ) : |
| 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_blocks(this, range), |
| 200 | m_frame_base(), |
| 201 | m_flags(), |
| 202 | m_prologue_byte_size(0) |
| 203 | { |
| 204 | assert(comp_unit != NULL); |
| 205 | } |
| 206 | |
| 207 | Function::Function |
| 208 | ( |
| 209 | CompileUnit *comp_unit, |
| 210 | lldb::user_id_t func_uid, |
| 211 | lldb::user_id_t type_uid, |
| 212 | const char *mangled, |
| 213 | Type *type, |
| 214 | const AddressRange &range |
| 215 | ) : |
| 216 | UserID(func_uid), |
| 217 | m_comp_unit(comp_unit), |
| 218 | m_type_uid(type_uid), |
| 219 | m_type(type), |
| 220 | m_mangled(mangled, true), |
| 221 | m_blocks(this, range), |
| 222 | m_frame_base(), |
| 223 | m_flags(), |
| 224 | m_prologue_byte_size(0) |
| 225 | { |
| 226 | assert(comp_unit != NULL); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | Function::~Function() |
| 231 | { |
| 232 | } |
| 233 | |
| 234 | const AddressRange & |
| 235 | Function::GetAddressRange() |
| 236 | { |
| 237 | return GetBlocks(true).GetAddressRange(); |
| 238 | } |
| 239 | |
| 240 | BlockList & |
| 241 | Function::GetBlocks(bool can_create) |
| 242 | { |
| 243 | if (m_blocks.IsEmpty() && can_create) |
| 244 | { |
| 245 | SymbolContext sc; |
| 246 | CalculateSymbolContext(&sc); |
| 247 | assert(sc.module_sp); |
| 248 | sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); |
| 249 | } |
| 250 | return m_blocks; |
| 251 | } |
| 252 | |
| 253 | CompileUnit* |
| 254 | Function::GetCompileUnit() |
| 255 | { |
| 256 | return m_comp_unit; |
| 257 | } |
| 258 | |
| 259 | const CompileUnit* |
| 260 | Function::GetCompileUnit() const |
| 261 | { |
| 262 | return m_comp_unit; |
| 263 | } |
| 264 | |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 265 | |
| 266 | void |
| 267 | Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Process *process) |
| 268 | { |
| 269 | Type* func_type = GetType(); |
| 270 | *s << '"' << func_type->GetName() << "\", id = " << (const UserID&)*this; |
| 271 | *s << ", range = "; |
| 272 | GetAddressRange().Dump(s, process, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress); |
| 273 | } |
| 274 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | void |
| 276 | Function::Dump(Stream *s, bool show_context) const |
| 277 | { |
| 278 | s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
| 279 | s->Indent(); |
| 280 | *s << "Function" << (const UserID&)*this; |
| 281 | |
| 282 | m_mangled.Dump(s); |
| 283 | |
| 284 | // FunctionInfo::Dump(s); |
| 285 | if (m_type) |
| 286 | { |
| 287 | *s << ", type = " << (void*)m_type; |
| 288 | /// << " ("; |
| 289 | ///m_type->DumpTypeName(s); |
| 290 | ///s->PutChar(')'); |
| 291 | } |
| 292 | else if (m_type_uid != LLDB_INVALID_UID) |
| 293 | *s << ", type_uid = " << m_type_uid; |
| 294 | |
| 295 | s->EOL(); |
| 296 | // Dump the root object |
| 297 | if (!m_blocks.IsEmpty()) |
| 298 | m_blocks.Dump(s, Block::RootID, INT_MAX, show_context); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | void |
| 303 | Function::CalculateSymbolContext(SymbolContext* sc) |
| 304 | { |
| 305 | sc->function = this; |
| 306 | m_comp_unit->CalculateSymbolContext(sc); |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | Function::DumpSymbolContext(Stream *s) |
| 311 | { |
| 312 | m_comp_unit->DumpSymbolContext(s); |
| 313 | s->Printf(", Function{0x%8.8x}", GetID()); |
| 314 | } |
| 315 | |
| 316 | size_t |
| 317 | Function::MemorySize () const |
| 318 | { |
| 319 | size_t mem_size = sizeof(Function) + m_blocks.MemorySize(); |
| 320 | return mem_size; |
| 321 | } |
| 322 | |
| 323 | Type* |
| 324 | Function::GetType() |
| 325 | { |
| 326 | return m_type; |
| 327 | } |
| 328 | |
| 329 | const Type* |
| 330 | Function::GetType() const |
| 331 | { |
| 332 | return m_type; |
| 333 | } |
| 334 | |
| 335 | Type |
| 336 | Function::GetReturnType () |
| 337 | { |
| 338 | clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType())); |
| 339 | assert (clang_type->isFunctionType()); |
| 340 | clang::FunctionType *function_type = dyn_cast<clang::FunctionType> (clang_type); |
| 341 | clang::QualType fun_return_qualtype = function_type->getResultType(); |
| 342 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame^] | 343 | const ConstString fun_return_name(ClangASTType::GetClangTypeName(fun_return_qualtype.getAsOpaquePtr())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 344 | |
| 345 | SymbolContext sc; |
| 346 | CalculateSymbolContext (&sc); |
| 347 | // Null out everything below the CompUnit 'cause we don't actually know these. |
| 348 | |
| 349 | size_t bit_size = ClangASTContext::GetTypeBitSize ((GetType()->GetClangASTContext().getASTContext()), &fun_return_qualtype); |
| 350 | Type return_type (0, GetType()->GetSymbolFile(), fun_return_name, bit_size, sc.comp_unit, 0, Type::eTypeUIDSynthetic, Declaration(), fun_return_qualtype.getAsOpaquePtr()); |
| 351 | return return_type; |
| 352 | } |
| 353 | |
| 354 | int |
| 355 | Function::GetArgumentCount () |
| 356 | { |
| 357 | clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType())); |
| 358 | assert (clang_type->isFunctionType()); |
| 359 | if (!clang_type->isFunctionProtoType()) |
| 360 | return -1; |
| 361 | |
| 362 | const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type); |
| 363 | if (function_proto_type != NULL) |
| 364 | return function_proto_type->getNumArgs(); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | const Type |
| 370 | Function::GetArgumentTypeAtIndex (size_t idx) |
| 371 | { |
| 372 | clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType())); |
| 373 | assert (clang_type->isFunctionType()); |
| 374 | if (!clang_type->isFunctionProtoType()) |
| 375 | return Type(); |
| 376 | |
| 377 | const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type); |
| 378 | if (function_proto_type != NULL) |
| 379 | { |
| 380 | unsigned num_args = function_proto_type->getNumArgs(); |
| 381 | if (idx >= num_args) |
| 382 | return Type(); |
| 383 | clang::QualType arg_qualtype = (function_proto_type->arg_type_begin())[idx]; |
| 384 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame^] | 385 | const ConstString arg_return_name(ClangASTType::GetClangTypeName(arg_qualtype.getAsOpaquePtr())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | SymbolContext sc; |
| 387 | CalculateSymbolContext (&sc); |
| 388 | // Null out everything below the CompUnit 'cause we don't actually know these. |
| 389 | |
| 390 | size_t bit_size = ClangASTContext::GetTypeBitSize ((GetType()->GetClangASTContext().getASTContext()), &arg_qualtype); |
| 391 | Type arg_type (0, GetType()->GetSymbolFile(), arg_return_name, bit_size, sc.comp_unit, 0, Type::eTypeUIDSynthetic, Declaration(), arg_qualtype.getAsOpaquePtr()); |
| 392 | return arg_type; |
| 393 | } |
| 394 | |
| 395 | return Type(); |
| 396 | } |
| 397 | |
| 398 | const char * |
| 399 | Function::GetArgumentNameAtIndex (size_t idx) |
| 400 | { |
| 401 | clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetOpaqueClangQualType())->getTypePtr(); |
| 402 | assert (clang_type->isFunctionType()); |
| 403 | if (!clang_type->isFunctionProtoType()) |
| 404 | return NULL; |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | bool |
| 409 | Function::IsVariadic () |
| 410 | { |
| 411 | const clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetOpaqueClangQualType())->getTypePtr(); |
| 412 | assert (clang_type->isFunctionType()); |
| 413 | if (!clang_type->isFunctionProtoType()) |
| 414 | return false; |
| 415 | |
| 416 | const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type); |
| 417 | if (function_proto_type != NULL) |
| 418 | { |
| 419 | return function_proto_type->isVariadic(); |
| 420 | } |
| 421 | |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | uint32_t |
| 426 | Function::GetPrologueByteSize () |
| 427 | { |
| 428 | if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize)) |
| 429 | { |
| 430 | m_flags.Set(flagsCalculatedPrologueSize); |
| 431 | LineTable* line_table = m_comp_unit->GetLineTable (); |
| 432 | if (line_table) |
| 433 | { |
| 434 | LineEntry line_entry; |
| 435 | if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), line_entry)) |
| 436 | m_prologue_byte_size = line_entry.range.GetByteSize(); |
| 437 | } |
| 438 | } |
| 439 | return m_prologue_byte_size; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | |