ClangASTContext.cpp revision 82f0746880b4a6b18bcf8666670140f5b4a56791
18b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===// 28b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// 38b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// The LLVM Compiler Infrastructure 48b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// 58b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// This file is distributed under the University of Illinois Open Source 68b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// License. See LICENSE.TXT for details. 78b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// 88b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project//===----------------------------------------------------------------------===// 98b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 108b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "lldb/Symbol/ClangASTContext.h" 118b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 128b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// C Includes 138b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// C++ Includes 148b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include <string> 158b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 168b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// Other libraries and framework includes 178b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 188b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// Clang headers like to use NDEBUG inside of them to enable/disable debug 198b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing 208b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// or another. This is bad because it means that if clang was built in release 218b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// mode, it assumes that you are building in release mode which is not always 228b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// the case. You can end up with functions that are defined as empty in header 238b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// files when NDEBUG is not defined, and this can cause link errors with the 248b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// clang .a files that you have since you might be missing functions in the .a 258b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// file. So we have to define NDEBUG when including clang headers to avoid any 268b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// mismatches. This is covered by rdar://problem/8691220 278b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 288b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifndef NDEBUG 298b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define LLDB_DEFINED_NDEBUG_FOR_CLANG 308b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define NDEBUG 318b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// Need to include assert.h so it is as clang would expect it to be (disabled) 328b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include <assert.h> 338b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif 348b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 358b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/AST/ASTContext.h" 368b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/AST/ASTImporter.h" 378b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/AST/CXXInheritance.h" 388b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/AST/DeclObjC.h" 398b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/AST/RecordLayout.h" 408b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/AST/Type.h" 418b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Basic/Builtins.h" 428b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Basic/FileManager.h" 438b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Basic/FileSystemOptions.h" 448b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Basic/SourceManager.h" 458b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Basic/TargetInfo.h" 468b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Basic/TargetOptions.h" 478b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Frontend/FrontendOptions.h" 488b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "clang/Frontend/LangStandard.h" 498b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 508b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG 518b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef NDEBUG 528b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef LLDB_DEFINED_NDEBUG_FOR_CLANG 538b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project// Need to re-include assert.h so it is as _we_ would expect it to be (enabled) 548b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include <assert.h> 558b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif 568b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 578b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "lldb/Core/ArchSpec.h" 588b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "lldb/Core/dwarf.h" 598b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "lldb/Core/Flags.h" 608b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include "lldb/Core/Log.h" 618b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 628b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#include <stdio.h> 638b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 648b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectusing namespace lldb; 658b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectusing namespace lldb_private; 668b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectusing namespace llvm; 678b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectusing namespace clang; 688b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 698b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 708b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectstatic bool 718b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source ProjectGetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type) 728b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project{ 738b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 748b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project switch (type_class) 758b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 768b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project case clang::Type::Record: 778b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project case clang::Type::Enum: 788b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 798b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr()); 808b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project if (tag_type) 818b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 828b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project clang::TagDecl *tag_decl = tag_type->getDecl(); 838b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project if (tag_decl) 848b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 858b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project if (tag_decl->getDefinition()) 868b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project return true; 878b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project 888b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project if (tag_decl->hasExternalLexicalStorage()) 898b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 908b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project if (ast) 918b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 928b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project ExternalASTSource *external_ast_source = ast->getExternalSource(); 938b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project if (external_ast_source) 948b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project { 958b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project external_ast_source->CompleteType(tag_decl); 968b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project return !tag_type->isIncompleteType(); 978b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project } 988b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project } 998b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project } 1008b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project return false; 1018b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project } 1028b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project } 103 104 } 105 break; 106 107 case clang::Type::ObjCObject: 108 case clang::Type::ObjCInterface: 109 { 110 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type); 111 if (objc_class_type) 112 { 113 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 114 // We currently can't complete objective C types through the newly added ASTContext 115 // because it only supports TagDecl objects right now... 116 bool is_forward_decl = class_interface_decl->isForwardDecl(); 117 if (is_forward_decl && class_interface_decl->hasExternalLexicalStorage()) 118 { 119 if (ast) 120 { 121 ExternalASTSource *external_ast_source = ast->getExternalSource(); 122 if (external_ast_source) 123 { 124 external_ast_source->CompleteType (class_interface_decl); 125 is_forward_decl = class_interface_decl->isForwardDecl(); 126 } 127 } 128 return is_forward_decl == false; 129 } 130 return true; 131 } 132 } 133 break; 134 135 case clang::Type::Typedef: 136 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()); 137 138 default: 139 break; 140 } 141 142 return true; 143} 144 145 146static AccessSpecifier 147ConvertAccessTypeToAccessSpecifier (AccessType access) 148{ 149 switch (access) 150 { 151 default: break; 152 case eAccessNone: return AS_none; 153 case eAccessPublic: return AS_public; 154 case eAccessPrivate: return AS_private; 155 case eAccessProtected: return AS_protected; 156 } 157 return AS_none; 158} 159 160static ObjCIvarDecl::AccessControl 161ConvertAccessTypeToObjCIvarAccessControl (AccessType access) 162{ 163 switch (access) 164 { 165 default: break; 166 case eAccessNone: return ObjCIvarDecl::None; 167 case eAccessPublic: return ObjCIvarDecl::Public; 168 case eAccessPrivate: return ObjCIvarDecl::Private; 169 case eAccessProtected: return ObjCIvarDecl::Protected; 170 case eAccessPackage: return ObjCIvarDecl::Package; 171 } 172 return ObjCIvarDecl::None; 173} 174 175 176static void 177ParseLangArgs 178( 179 LangOptions &Opts, 180 InputKind IK 181) 182{ 183 // FIXME: Cleanup per-file based stuff. 184 185 // Set some properties which depend soley on the input kind; it would be nice 186 // to move these to the language standard, and have the driver resolve the 187 // input kind + language standard. 188 if (IK == IK_Asm) { 189 Opts.AsmPreprocessor = 1; 190 } else if (IK == IK_ObjC || 191 IK == IK_ObjCXX || 192 IK == IK_PreprocessedObjC || 193 IK == IK_PreprocessedObjCXX) { 194 Opts.ObjC1 = Opts.ObjC2 = 1; 195 } 196 197 LangStandard::Kind LangStd = LangStandard::lang_unspecified; 198 199 if (LangStd == LangStandard::lang_unspecified) { 200 // Based on the base language, pick one. 201 switch (IK) { 202 case IK_None: 203 case IK_AST: 204 case IK_LLVM_IR: 205 assert (!"Invalid input kind!"); 206 case IK_OpenCL: 207 LangStd = LangStandard::lang_opencl; 208 break; 209 case IK_CUDA: 210 LangStd = LangStandard::lang_cuda; 211 break; 212 case IK_Asm: 213 case IK_C: 214 case IK_PreprocessedC: 215 case IK_ObjC: 216 case IK_PreprocessedObjC: 217 LangStd = LangStandard::lang_gnu99; 218 break; 219 case IK_CXX: 220 case IK_PreprocessedCXX: 221 case IK_ObjCXX: 222 case IK_PreprocessedObjCXX: 223 LangStd = LangStandard::lang_gnucxx98; 224 break; 225 } 226 } 227 228 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); 229 Opts.BCPLComment = Std.hasBCPLComments(); 230 Opts.C99 = Std.isC99(); 231 Opts.CPlusPlus = Std.isCPlusPlus(); 232 Opts.CPlusPlus0x = Std.isCPlusPlus0x(); 233 Opts.Digraphs = Std.hasDigraphs(); 234 Opts.GNUMode = Std.isGNUMode(); 235 Opts.GNUInline = !Std.isC99(); 236 Opts.HexFloats = Std.hasHexFloats(); 237 Opts.ImplicitInt = Std.hasImplicitInt(); 238 239 // OpenCL has some additional defaults. 240 if (LangStd == LangStandard::lang_opencl) { 241 Opts.OpenCL = 1; 242 Opts.AltiVec = 1; 243 Opts.CXXOperatorNames = 1; 244 Opts.LaxVectorConversions = 1; 245 } 246 247 // OpenCL and C++ both have bool, true, false keywords. 248 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; 249 250// if (Opts.CPlusPlus) 251// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names); 252// 253// if (Args.hasArg(OPT_fobjc_gc_only)) 254// Opts.setGCMode(LangOptions::GCOnly); 255// else if (Args.hasArg(OPT_fobjc_gc)) 256// Opts.setGCMode(LangOptions::HybridGC); 257// 258// if (Args.hasArg(OPT_print_ivar_layout)) 259// Opts.ObjCGCBitmapPrint = 1; 260// 261// if (Args.hasArg(OPT_faltivec)) 262// Opts.AltiVec = 1; 263// 264// if (Args.hasArg(OPT_pthread)) 265// Opts.POSIXThreads = 1; 266// 267// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility, 268// "default"); 269// if (Vis == "default") 270 Opts.setVisibilityMode(DefaultVisibility); 271// else if (Vis == "hidden") 272// Opts.setVisibilityMode(LangOptions::Hidden); 273// else if (Vis == "protected") 274// Opts.setVisibilityMode(LangOptions::Protected); 275// else 276// Diags.Report(diag::err_drv_invalid_value) 277// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis; 278 279// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv); 280 281 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs 282 // is specified, or -std is set to a conforming mode. 283 Opts.Trigraphs = !Opts.GNUMode; 284// if (Args.hasArg(OPT_trigraphs)) 285// Opts.Trigraphs = 1; 286// 287// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers, 288// OPT_fno_dollars_in_identifiers, 289// !Opts.AsmPreprocessor); 290// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings); 291// Opts.Microsoft = Args.hasArg(OPT_fms_extensions); 292// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings); 293// if (Args.hasArg(OPT_fno_lax_vector_conversions)) 294// Opts.LaxVectorConversions = 0; 295// Opts.Exceptions = Args.hasArg(OPT_fexceptions); 296// Opts.RTTI = !Args.hasArg(OPT_fno_rtti); 297// Opts.Blocks = Args.hasArg(OPT_fblocks); 298// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char); 299// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar); 300// Opts.Freestanding = Args.hasArg(OPT_ffreestanding); 301// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding; 302// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new); 303// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions); 304// Opts.AccessControl = Args.hasArg(OPT_faccess_control); 305// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors); 306// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno); 307// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99, 308// Diags); 309// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime); 310// Opts.ObjCConstantStringClass = getLastArgValue(Args, 311// OPT_fconstant_string_class); 312// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi); 313// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior); 314// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls); 315// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); 316// Opts.Static = Args.hasArg(OPT_static_define); 317 Opts.OptimizeSize = 0; 318 319 // FIXME: Eliminate this dependency. 320// unsigned Opt = 321// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); 322// Opts.Optimize = Opt != 0; 323 unsigned Opt = 0; 324 325 // This is the __NO_INLINE__ define, which just depends on things like the 326 // optimization level and -fno-inline, not actually whether the backend has 327 // inlining enabled. 328 // 329 // FIXME: This is affected by other options (-fno-inline). 330 Opts.NoInline = !Opt; 331 332// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags); 333// switch (SSP) { 334// default: 335// Diags.Report(diag::err_drv_invalid_value) 336// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP; 337// break; 338// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break; 339// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break; 340// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break; 341// } 342} 343 344 345ClangASTContext::ClangASTContext (const char *target_triple) : 346 m_target_triple(), 347 m_ast_ap(), 348 m_language_options_ap(), 349 m_source_manager_ap(), 350 m_diagnostic_ap(), 351 m_target_options_ap(), 352 m_target_info_ap(), 353 m_identifier_table_ap(), 354 m_selector_table_ap(), 355 m_builtins_ap(), 356 m_callback_tag_decl (NULL), 357 m_callback_objc_decl (NULL), 358 m_callback_baton (NULL) 359 360{ 361 if (target_triple && target_triple[0]) 362 m_target_triple.assign (target_triple); 363} 364 365//---------------------------------------------------------------------- 366// Destructor 367//---------------------------------------------------------------------- 368ClangASTContext::~ClangASTContext() 369{ 370 m_builtins_ap.reset(); 371 m_selector_table_ap.reset(); 372 m_identifier_table_ap.reset(); 373 m_target_info_ap.reset(); 374 m_target_options_ap.reset(); 375 m_diagnostic_ap.reset(); 376 m_source_manager_ap.reset(); 377 m_language_options_ap.reset(); 378 m_ast_ap.reset(); 379} 380 381 382void 383ClangASTContext::Clear() 384{ 385 m_ast_ap.reset(); 386 m_language_options_ap.reset(); 387 m_source_manager_ap.reset(); 388 m_diagnostic_ap.reset(); 389 m_target_options_ap.reset(); 390 m_target_info_ap.reset(); 391 m_identifier_table_ap.reset(); 392 m_selector_table_ap.reset(); 393 m_builtins_ap.reset(); 394} 395 396const char * 397ClangASTContext::GetTargetTriple () 398{ 399 return m_target_triple.c_str(); 400} 401 402void 403ClangASTContext::SetTargetTriple (const char *target_triple) 404{ 405 Clear(); 406 m_target_triple.assign(target_triple); 407} 408 409void 410ClangASTContext::SetArchitecture (const ArchSpec &arch) 411{ 412 Clear(); 413 m_target_triple.assign(arch.GetTriple().str()); 414} 415 416bool 417ClangASTContext::HasExternalSource () 418{ 419 ASTContext *ast = getASTContext(); 420 if (ast) 421 return ast->getExternalSource () != NULL; 422 return false; 423} 424 425void 426ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap) 427{ 428 ASTContext *ast = getASTContext(); 429 if (ast) 430 { 431 ast->setExternalSource (ast_source_ap); 432 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); 433 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true); 434 } 435} 436 437void 438ClangASTContext::RemoveExternalSource () 439{ 440 ASTContext *ast = getASTContext(); 441 442 if (ast) 443 { 444 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap; 445 ast->setExternalSource (empty_ast_source_ap); 446 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false); 447 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false); 448 } 449} 450 451 452 453ASTContext * 454ClangASTContext::getASTContext() 455{ 456 if (m_ast_ap.get() == NULL) 457 { 458 m_ast_ap.reset(new ASTContext (*getLanguageOptions(), 459 *getSourceManager(), 460 *getTargetInfo(), 461 *getIdentifierTable(), 462 *getSelectorTable(), 463 *getBuiltinContext(), 464 0)); 465 466 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) 467 { 468 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 469 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 470 } 471 472 m_ast_ap->getDiagnostics().setClient(getDiagnosticClient(), false); 473 } 474 return m_ast_ap.get(); 475} 476 477Builtin::Context * 478ClangASTContext::getBuiltinContext() 479{ 480 if (m_builtins_ap.get() == NULL) 481 m_builtins_ap.reset (new Builtin::Context(*getTargetInfo())); 482 return m_builtins_ap.get(); 483} 484 485IdentifierTable * 486ClangASTContext::getIdentifierTable() 487{ 488 if (m_identifier_table_ap.get() == NULL) 489 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL)); 490 return m_identifier_table_ap.get(); 491} 492 493LangOptions * 494ClangASTContext::getLanguageOptions() 495{ 496 if (m_language_options_ap.get() == NULL) 497 { 498 m_language_options_ap.reset(new LangOptions()); 499 ParseLangArgs(*m_language_options_ap, IK_ObjCXX); 500// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX); 501 } 502 return m_language_options_ap.get(); 503} 504 505SelectorTable * 506ClangASTContext::getSelectorTable() 507{ 508 if (m_selector_table_ap.get() == NULL) 509 m_selector_table_ap.reset (new SelectorTable()); 510 return m_selector_table_ap.get(); 511} 512 513clang::FileManager * 514ClangASTContext::getFileManager() 515{ 516 if (m_file_manager_ap.get() == NULL) 517 { 518 clang::FileSystemOptions file_system_options; 519 m_file_manager_ap.reset(new clang::FileManager(file_system_options)); 520 } 521 return m_file_manager_ap.get(); 522} 523 524clang::SourceManager * 525ClangASTContext::getSourceManager() 526{ 527 if (m_source_manager_ap.get() == NULL) 528 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnostic(), *getFileManager())); 529 return m_source_manager_ap.get(); 530} 531 532Diagnostic * 533ClangASTContext::getDiagnostic() 534{ 535 if (m_diagnostic_ap.get() == NULL) 536 { 537 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); 538 m_diagnostic_ap.reset(new Diagnostic(diag_id_sp)); 539 } 540 return m_diagnostic_ap.get(); 541} 542 543class NullDiagnosticClient : public DiagnosticClient 544{ 545public: 546 NullDiagnosticClient () 547 { 548 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); 549 } 550 551 void HandleDiagnostic (Diagnostic::Level DiagLevel, const DiagnosticInfo &info) 552 { 553 if (m_log) 554 { 555 llvm::SmallVectorImpl<char> diag_str(10); 556 info.FormatDiagnostic(diag_str); 557 diag_str.push_back('\0'); 558 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data()); 559 } 560 } 561private: 562 LogSP m_log; 563}; 564 565DiagnosticClient * 566ClangASTContext::getDiagnosticClient() 567{ 568 if (m_diagnostic_client_ap.get() == NULL) 569 m_diagnostic_client_ap.reset(new NullDiagnosticClient); 570 571 return m_diagnostic_client_ap.get(); 572} 573 574TargetOptions * 575ClangASTContext::getTargetOptions() 576{ 577 if (m_target_options_ap.get() == NULL && !m_target_triple.empty()) 578 { 579 m_target_options_ap.reset (new TargetOptions()); 580 if (m_target_options_ap.get()) 581 m_target_options_ap->Triple = m_target_triple; 582 } 583 return m_target_options_ap.get(); 584} 585 586 587TargetInfo * 588ClangASTContext::getTargetInfo() 589{ 590 // target_triple should be something like "x86_64-apple-darwin10" 591 if (m_target_info_ap.get() == NULL && !m_target_triple.empty()) 592 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnostic(), *getTargetOptions())); 593 return m_target_info_ap.get(); 594} 595 596#pragma mark Basic Types 597 598static inline bool 599QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type) 600{ 601 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); 602 if (qual_type_bit_size == bit_size) 603 return true; 604 return false; 605} 606 607clang_type_t 608ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size) 609{ 610 ASTContext *ast = getASTContext(); 611 612 assert (ast != NULL); 613 614 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size); 615} 616 617clang_type_t 618ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size) 619{ 620 if (!ast) 621 return NULL; 622 623 switch (encoding) 624 { 625 case eEncodingInvalid: 626 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy)) 627 return ast->VoidPtrTy.getAsOpaquePtr(); 628 break; 629 630 case eEncodingUint: 631 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 632 return ast->UnsignedCharTy.getAsOpaquePtr(); 633 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 634 return ast->UnsignedShortTy.getAsOpaquePtr(); 635 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 636 return ast->UnsignedIntTy.getAsOpaquePtr(); 637 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 638 return ast->UnsignedLongTy.getAsOpaquePtr(); 639 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 640 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 641 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 642 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 643 break; 644 645 case eEncodingSint: 646 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 647 return ast->CharTy.getAsOpaquePtr(); 648 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 649 return ast->ShortTy.getAsOpaquePtr(); 650 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 651 return ast->IntTy.getAsOpaquePtr(); 652 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 653 return ast->LongTy.getAsOpaquePtr(); 654 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 655 return ast->LongLongTy.getAsOpaquePtr(); 656 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 657 return ast->Int128Ty.getAsOpaquePtr(); 658 break; 659 660 case eEncodingIEEE754: 661 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy)) 662 return ast->FloatTy.getAsOpaquePtr(); 663 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy)) 664 return ast->DoubleTy.getAsOpaquePtr(); 665 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy)) 666 return ast->LongDoubleTy.getAsOpaquePtr(); 667 break; 668 669 case eEncodingVector: 670 default: 671 break; 672 } 673 674 return NULL; 675} 676 677clang_type_t 678ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size) 679{ 680 ASTContext *ast = getASTContext(); 681 682 #define streq(a,b) strcmp(a,b) == 0 683 assert (ast != NULL); 684 if (ast) 685 { 686 switch (dw_ate) 687 { 688 default: 689 break; 690 691 case DW_ATE_address: 692 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy)) 693 return ast->VoidPtrTy.getAsOpaquePtr(); 694 break; 695 696 case DW_ATE_boolean: 697 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy)) 698 return ast->BoolTy.getAsOpaquePtr(); 699 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 700 return ast->UnsignedCharTy.getAsOpaquePtr(); 701 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 702 return ast->UnsignedShortTy.getAsOpaquePtr(); 703 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 704 return ast->UnsignedIntTy.getAsOpaquePtr(); 705 break; 706 707 case DW_ATE_lo_user: 708 // This has been seen to mean DW_AT_complex_integer 709 if (::strstr(type_name, "complex")) 710 { 711 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2); 712 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr(); 713 } 714 break; 715 716 case DW_ATE_complex_float: 717 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy)) 718 return ast->FloatComplexTy.getAsOpaquePtr(); 719 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy)) 720 return ast->DoubleComplexTy.getAsOpaquePtr(); 721 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy)) 722 return ast->LongDoubleComplexTy.getAsOpaquePtr(); 723 else 724 { 725 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2); 726 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr(); 727 } 728 break; 729 730 case DW_ATE_float: 731 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy)) 732 return ast->FloatTy.getAsOpaquePtr(); 733 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy)) 734 return ast->DoubleTy.getAsOpaquePtr(); 735 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy)) 736 return ast->LongDoubleTy.getAsOpaquePtr(); 737 break; 738 739 case DW_ATE_signed: 740 if (type_name) 741 { 742 if (strstr(type_name, "long long")) 743 { 744 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 745 return ast->LongLongTy.getAsOpaquePtr(); 746 } 747 else if (strstr(type_name, "long")) 748 { 749 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 750 return ast->LongTy.getAsOpaquePtr(); 751 } 752 else if (strstr(type_name, "short")) 753 { 754 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 755 return ast->ShortTy.getAsOpaquePtr(); 756 } 757 else if (strstr(type_name, "char")) 758 { 759 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 760 return ast->CharTy.getAsOpaquePtr(); 761 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 762 return ast->SignedCharTy.getAsOpaquePtr(); 763 } 764 else if (strstr(type_name, "int")) 765 { 766 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 767 return ast->IntTy.getAsOpaquePtr(); 768 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 769 return ast->Int128Ty.getAsOpaquePtr(); 770 } 771 else if (streq(type_name, "wchar_t")) 772 { 773 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy)) 774 return ast->WCharTy.getAsOpaquePtr(); 775 } 776 else if (streq(type_name, "void")) 777 { 778 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy)) 779 return ast->VoidTy.getAsOpaquePtr(); 780 } 781 } 782 // We weren't able to match up a type name, just search by size 783 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 784 return ast->CharTy.getAsOpaquePtr(); 785 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 786 return ast->ShortTy.getAsOpaquePtr(); 787 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 788 return ast->IntTy.getAsOpaquePtr(); 789 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 790 return ast->LongTy.getAsOpaquePtr(); 791 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 792 return ast->LongLongTy.getAsOpaquePtr(); 793 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 794 return ast->Int128Ty.getAsOpaquePtr(); 795 break; 796 797 case DW_ATE_signed_char: 798 if (type_name) 799 { 800 if (streq(type_name, "signed char")) 801 { 802 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 803 return ast->SignedCharTy.getAsOpaquePtr(); 804 } 805 } 806 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 807 return ast->CharTy.getAsOpaquePtr(); 808 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 809 return ast->SignedCharTy.getAsOpaquePtr(); 810 break; 811 812 case DW_ATE_unsigned: 813 if (type_name) 814 { 815 if (strstr(type_name, "long long")) 816 { 817 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 818 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 819 } 820 else if (strstr(type_name, "long")) 821 { 822 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 823 return ast->UnsignedLongTy.getAsOpaquePtr(); 824 } 825 else if (strstr(type_name, "short")) 826 { 827 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 828 return ast->UnsignedShortTy.getAsOpaquePtr(); 829 } 830 else if (strstr(type_name, "char")) 831 { 832 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 833 return ast->UnsignedCharTy.getAsOpaquePtr(); 834 } 835 else if (strstr(type_name, "int")) 836 { 837 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 838 return ast->UnsignedIntTy.getAsOpaquePtr(); 839 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 840 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 841 } 842 } 843 // We weren't able to match up a type name, just search by size 844 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 845 return ast->UnsignedCharTy.getAsOpaquePtr(); 846 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 847 return ast->UnsignedShortTy.getAsOpaquePtr(); 848 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 849 return ast->UnsignedIntTy.getAsOpaquePtr(); 850 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 851 return ast->UnsignedLongTy.getAsOpaquePtr(); 852 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 853 return ast->UnsignedLongLongTy.getAsOpaquePtr(); 854 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 855 return ast->UnsignedInt128Ty.getAsOpaquePtr(); 856 break; 857 858 case DW_ATE_unsigned_char: 859 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 860 return ast->UnsignedCharTy.getAsOpaquePtr(); 861 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 862 return ast->UnsignedShortTy.getAsOpaquePtr(); 863 break; 864 865 case DW_ATE_imaginary_float: 866 break; 867 } 868 } 869 // This assert should fire for anything that we don't catch above so we know 870 // to fix any issues we run into. 871 if (type_name) 872 { 873 fprintf (stderr, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size); 874 } 875 else 876 { 877 fprintf (stderr, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size); 878 } 879 return NULL; 880} 881 882clang_type_t 883ClangASTContext::GetBuiltInType_void(ASTContext *ast) 884{ 885 return ast->VoidTy.getAsOpaquePtr(); 886} 887 888clang_type_t 889ClangASTContext::GetBuiltInType_bool() 890{ 891 return getASTContext()->BoolTy.getAsOpaquePtr(); 892} 893 894clang_type_t 895ClangASTContext::GetBuiltInType_objc_id() 896{ 897 return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinIdTy).getAsOpaquePtr(); 898} 899 900clang_type_t 901ClangASTContext::GetBuiltInType_objc_Class() 902{ 903 return getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr(); 904} 905 906clang_type_t 907ClangASTContext::GetBuiltInType_objc_selector() 908{ 909 return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinSelTy).getAsOpaquePtr(); 910} 911 912clang_type_t 913ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) 914{ 915 return ast->UnknownAnyTy.getAsOpaquePtr(); 916} 917 918clang_type_t 919ClangASTContext::GetCStringType (bool is_const) 920{ 921 QualType char_type(getASTContext()->CharTy); 922 923 if (is_const) 924 char_type.addConst(); 925 926 return getASTContext()->getPointerType(char_type).getAsOpaquePtr(); 927} 928 929clang_type_t 930ClangASTContext::GetVoidPtrType (bool is_const) 931{ 932 return GetVoidPtrType(getASTContext(), is_const); 933} 934 935clang_type_t 936ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const) 937{ 938 QualType void_ptr_type(ast->VoidPtrTy); 939 940 if (is_const) 941 void_ptr_type.addConst(); 942 943 return void_ptr_type.getAsOpaquePtr(); 944} 945 946clang_type_t 947ClangASTContext::CopyType (ASTContext *dst_ast, 948 ASTContext *src_ast, 949 clang_type_t clang_type) 950{ 951 FileSystemOptions file_system_options; 952 FileManager file_manager (file_system_options); 953 ASTImporter importer(*dst_ast, file_manager, 954 *src_ast, file_manager, 955 false); 956 957 QualType src (QualType::getFromOpaquePtr(clang_type)); 958 QualType dst (importer.Import(src)); 959 960 return dst.getAsOpaquePtr(); 961} 962 963 964clang::Decl * 965ClangASTContext::CopyDecl (ASTContext *dst_ast, 966 ASTContext *src_ast, 967 clang::Decl *source_decl) 968{ 969 FileSystemOptions file_system_options; 970 FileManager file_manager (file_system_options); 971 ASTImporter importer(*dst_ast, file_manager, 972 *src_ast, file_manager, 973 false); 974 975 return importer.Import(source_decl); 976} 977 978bool 979ClangASTContext::AreTypesSame(ASTContext *ast, 980 clang_type_t type1, 981 clang_type_t type2) 982{ 983 return ast->hasSameType (QualType::getFromOpaquePtr(type1), 984 QualType::getFromOpaquePtr(type2)); 985} 986 987#pragma mark CVR modifiers 988 989clang_type_t 990ClangASTContext::AddConstModifier (clang_type_t clang_type) 991{ 992 if (clang_type) 993 { 994 QualType result(QualType::getFromOpaquePtr(clang_type)); 995 result.addConst(); 996 return result.getAsOpaquePtr(); 997 } 998 return NULL; 999} 1000 1001clang_type_t 1002ClangASTContext::AddRestrictModifier (clang_type_t clang_type) 1003{ 1004 if (clang_type) 1005 { 1006 QualType result(QualType::getFromOpaquePtr(clang_type)); 1007 result.getQualifiers().setRestrict (true); 1008 return result.getAsOpaquePtr(); 1009 } 1010 return NULL; 1011} 1012 1013clang_type_t 1014ClangASTContext::AddVolatileModifier (clang_type_t clang_type) 1015{ 1016 if (clang_type) 1017 { 1018 QualType result(QualType::getFromOpaquePtr(clang_type)); 1019 result.getQualifiers().setVolatile (true); 1020 return result.getAsOpaquePtr(); 1021 } 1022 return NULL; 1023} 1024 1025 1026clang_type_t 1027ClangASTContext::GetTypeForDecl (TagDecl *decl) 1028{ 1029 // No need to call the getASTContext() accessor (which can create the AST 1030 // if it isn't created yet, because we can't have created a decl in this 1031 // AST if our AST didn't already exist... 1032 if (m_ast_ap.get()) 1033 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr(); 1034 return NULL; 1035} 1036 1037clang_type_t 1038ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl) 1039{ 1040 // No need to call the getASTContext() accessor (which can create the AST 1041 // if it isn't created yet, because we can't have created a decl in this 1042 // AST if our AST didn't already exist... 1043 if (m_ast_ap.get()) 1044 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr(); 1045 return NULL; 1046} 1047 1048#pragma mark Structure, Unions, Classes 1049 1050clang_type_t 1051ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx, LanguageType language) 1052{ 1053 ASTContext *ast = getASTContext(); 1054 assert (ast != NULL); 1055 1056 if (decl_ctx == NULL) 1057 decl_ctx = ast->getTranslationUnitDecl(); 1058 1059 1060 if (language == eLanguageTypeObjC) 1061 { 1062 bool isForwardDecl = true; 1063 bool isInternal = false; 1064 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal); 1065 } 1066 1067 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1068 // we will need to update this code. I was told to currently always use 1069 // the CXXRecordDecl class since we often don't know from debug information 1070 // if something is struct or a class, so we default to always use the more 1071 // complete definition just in case. 1072 CXXRecordDecl *decl = CXXRecordDecl::Create(*ast, 1073 (TagDecl::TagKind)kind, 1074 decl_ctx, 1075 SourceLocation(), 1076 SourceLocation(), 1077 name && name[0] ? &ast->Idents.get(name) : NULL); 1078 1079 return ast->getTagDeclType(decl).getAsOpaquePtr(); 1080} 1081 1082bool 1083ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern) 1084{ 1085 if (clang_type == NULL) 1086 return false; 1087 1088 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 1089 1090 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1091 switch (type_class) 1092 { 1093 case clang::Type::Record: 1094 { 1095 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 1096 if (cxx_record_decl) 1097 { 1098 cxx_record_decl->setHasExternalLexicalStorage (has_extern); 1099 cxx_record_decl->setHasExternalVisibleStorage (has_extern); 1100 return true; 1101 } 1102 } 1103 break; 1104 1105 case clang::Type::Enum: 1106 { 1107 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl(); 1108 if (enum_decl) 1109 { 1110 enum_decl->setHasExternalLexicalStorage (has_extern); 1111 enum_decl->setHasExternalVisibleStorage (has_extern); 1112 return true; 1113 } 1114 } 1115 break; 1116 1117 case clang::Type::ObjCObject: 1118 case clang::Type::ObjCInterface: 1119 { 1120 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 1121 assert (objc_class_type); 1122 if (objc_class_type) 1123 { 1124 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1125 1126 if (class_interface_decl) 1127 { 1128 class_interface_decl->setHasExternalLexicalStorage (has_extern); 1129 class_interface_decl->setHasExternalVisibleStorage (has_extern); 1130 return true; 1131 } 1132 } 1133 } 1134 break; 1135 1136 case clang::Type::Typedef: 1137 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern); 1138 1139 default: 1140 break; 1141 } 1142 return false; 1143} 1144 1145static bool 1146IsOperator (const char *name, OverloadedOperatorKind &op_kind) 1147{ 1148 if (name == NULL || name[0] == '\0') 1149 return false; 1150 1151#define OPERATOR_PREFIX "operator" 1152#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1) 1153 1154 const char *post_op_name = NULL; 1155 1156 bool no_space = true; 1157 1158 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH)) 1159 return false; 1160 1161 post_op_name = name + OPERATOR_PREFIX_LENGTH; 1162 1163 if (post_op_name[0] == ' ') 1164 { 1165 post_op_name++; 1166 no_space = false; 1167 } 1168 1169#undef OPERATOR_PREFIX 1170#undef OPERATOR_PREFIX_LENGTH 1171 1172 // This is an operator, set the overloaded operator kind to invalid 1173 // in case this is a conversion operator... 1174 op_kind = NUM_OVERLOADED_OPERATORS; 1175 1176 switch (post_op_name[0]) 1177 { 1178 default: 1179 if (no_space) 1180 return false; 1181 break; 1182 case 'n': 1183 if (no_space) 1184 return false; 1185 if (strcmp (post_op_name, "new") == 0) 1186 op_kind = OO_New; 1187 else if (strcmp (post_op_name, "new[]") == 0) 1188 op_kind = OO_Array_New; 1189 break; 1190 1191 case 'd': 1192 if (no_space) 1193 return false; 1194 if (strcmp (post_op_name, "delete") == 0) 1195 op_kind = OO_Delete; 1196 else if (strcmp (post_op_name, "delete[]") == 0) 1197 op_kind = OO_Array_Delete; 1198 break; 1199 1200 case '+': 1201 if (post_op_name[1] == '\0') 1202 op_kind = OO_Plus; 1203 else if (post_op_name[2] == '\0') 1204 { 1205 if (post_op_name[1] == '=') 1206 op_kind = OO_PlusEqual; 1207 else if (post_op_name[1] == '+') 1208 op_kind = OO_PlusPlus; 1209 } 1210 break; 1211 1212 case '-': 1213 if (post_op_name[1] == '\0') 1214 op_kind = OO_Minus; 1215 else if (post_op_name[2] == '\0') 1216 { 1217 switch (post_op_name[1]) 1218 { 1219 case '=': op_kind = OO_MinusEqual; break; 1220 case '-': op_kind = OO_MinusMinus; break; 1221 case '>': op_kind = OO_Arrow; break; 1222 } 1223 } 1224 else if (post_op_name[3] == '\0') 1225 { 1226 if (post_op_name[2] == '*') 1227 op_kind = OO_ArrowStar; break; 1228 } 1229 break; 1230 1231 case '*': 1232 if (post_op_name[1] == '\0') 1233 op_kind = OO_Star; 1234 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1235 op_kind = OO_StarEqual; 1236 break; 1237 1238 case '/': 1239 if (post_op_name[1] == '\0') 1240 op_kind = OO_Slash; 1241 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1242 op_kind = OO_SlashEqual; 1243 break; 1244 1245 case '%': 1246 if (post_op_name[1] == '\0') 1247 op_kind = OO_Percent; 1248 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1249 op_kind = OO_PercentEqual; 1250 break; 1251 1252 1253 case '^': 1254 if (post_op_name[1] == '\0') 1255 op_kind = OO_Caret; 1256 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1257 op_kind = OO_CaretEqual; 1258 break; 1259 1260 case '&': 1261 if (post_op_name[1] == '\0') 1262 op_kind = OO_Amp; 1263 else if (post_op_name[2] == '\0') 1264 { 1265 switch (post_op_name[1]) 1266 { 1267 case '=': op_kind = OO_AmpEqual; break; 1268 case '&': op_kind = OO_AmpAmp; break; 1269 } 1270 } 1271 break; 1272 1273 case '|': 1274 if (post_op_name[1] == '\0') 1275 op_kind = OO_Pipe; 1276 else if (post_op_name[2] == '\0') 1277 { 1278 switch (post_op_name[1]) 1279 { 1280 case '=': op_kind = OO_PipeEqual; break; 1281 case '|': op_kind = OO_PipePipe; break; 1282 } 1283 } 1284 break; 1285 1286 case '~': 1287 if (post_op_name[1] == '\0') 1288 op_kind = OO_Tilde; 1289 break; 1290 1291 case '!': 1292 if (post_op_name[1] == '\0') 1293 op_kind = OO_Exclaim; 1294 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1295 op_kind = OO_ExclaimEqual; 1296 break; 1297 1298 case '=': 1299 if (post_op_name[1] == '\0') 1300 op_kind = OO_Equal; 1301 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 1302 op_kind = OO_EqualEqual; 1303 break; 1304 1305 case '<': 1306 if (post_op_name[1] == '\0') 1307 op_kind = OO_Less; 1308 else if (post_op_name[2] == '\0') 1309 { 1310 switch (post_op_name[1]) 1311 { 1312 case '<': op_kind = OO_LessLess; break; 1313 case '=': op_kind = OO_LessEqual; break; 1314 } 1315 } 1316 else if (post_op_name[3] == '\0') 1317 { 1318 if (post_op_name[2] == '=') 1319 op_kind = OO_LessLessEqual; 1320 } 1321 break; 1322 1323 case '>': 1324 if (post_op_name[1] == '\0') 1325 op_kind = OO_Greater; 1326 else if (post_op_name[2] == '\0') 1327 { 1328 switch (post_op_name[1]) 1329 { 1330 case '>': op_kind = OO_GreaterGreater; break; 1331 case '=': op_kind = OO_GreaterEqual; break; 1332 } 1333 } 1334 else if (post_op_name[1] == '>' && 1335 post_op_name[2] == '=' && 1336 post_op_name[3] == '\0') 1337 { 1338 op_kind = OO_GreaterGreaterEqual; 1339 } 1340 break; 1341 1342 case ',': 1343 if (post_op_name[1] == '\0') 1344 op_kind = OO_Comma; 1345 break; 1346 1347 case '(': 1348 if (post_op_name[1] == ')' && post_op_name[2] == '\0') 1349 op_kind = OO_Call; 1350 break; 1351 1352 case '[': 1353 if (post_op_name[1] == ']' && post_op_name[2] == '\0') 1354 op_kind = OO_Subscript; 1355 break; 1356 } 1357 1358 return true; 1359} 1360 1361CXXMethodDecl * 1362ClangASTContext::AddMethodToCXXRecordType 1363( 1364 ASTContext *ast, 1365 clang_type_t record_opaque_type, 1366 const char *name, 1367 clang_type_t method_opaque_type, 1368 lldb::AccessType access, 1369 bool is_virtual, 1370 bool is_static, 1371 bool is_inline, 1372 bool is_explicit 1373) 1374{ 1375 if (!record_opaque_type || !method_opaque_type || !name) 1376 return NULL; 1377 1378 assert(ast); 1379 1380 IdentifierTable *identifier_table = &ast->Idents; 1381 1382 assert(identifier_table); 1383 1384 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type)); 1385 1386 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl(); 1387 1388 if (cxx_record_decl == NULL) 1389 return NULL; 1390 1391 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 1392 1393 CXXMethodDecl *cxx_method_decl = NULL; 1394 1395 DeclarationName decl_name (&identifier_table->get(name)); 1396 1397 const bool is_implicitly_declared = false; 1398 1399 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr()); 1400 1401 if (function_Type == NULL) 1402 return NULL; 1403 1404 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type)); 1405 1406 if (!method_function_prototype) 1407 return NULL; 1408 1409 unsigned int num_params = method_function_prototype->getNumArgs(); 1410 1411 if (name[0] == '~') 1412 { 1413 cxx_method_decl = CXXDestructorDecl::Create (*ast, 1414 cxx_record_decl, 1415 SourceLocation(), 1416 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()), 1417 method_qual_type, 1418 NULL, 1419 is_inline, 1420 is_implicitly_declared); 1421 } 1422 else if (decl_name == cxx_record_decl->getDeclName()) 1423 { 1424 cxx_method_decl = CXXConstructorDecl::Create (*ast, 1425 cxx_record_decl, 1426 SourceLocation(), 1427 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()), 1428 method_qual_type, 1429 NULL, // TypeSourceInfo * 1430 is_explicit, 1431 is_inline, 1432 is_implicitly_declared); 1433 } 1434 else 1435 { 1436 1437 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS; 1438 if (IsOperator (name, op_kind)) 1439 { 1440 if (op_kind != NUM_OVERLOADED_OPERATORS) 1441 { 1442 cxx_method_decl = CXXMethodDecl::Create (*ast, 1443 cxx_record_decl, 1444 SourceLocation(), 1445 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()), 1446 method_qual_type, 1447 NULL, // TypeSourceInfo * 1448 is_static, 1449 SC_None, 1450 is_inline, 1451 SourceLocation()); 1452 } 1453 else if (num_params == 0) 1454 { 1455 // Conversion operators don't take params... 1456 cxx_method_decl = CXXConversionDecl::Create (*ast, 1457 cxx_record_decl, 1458 SourceLocation(), 1459 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()), 1460 method_qual_type, 1461 NULL, // TypeSourceInfo * 1462 is_inline, 1463 is_explicit, 1464 SourceLocation()); 1465 } 1466 } 1467 1468 if (cxx_method_decl == NULL) 1469 { 1470 cxx_method_decl = CXXMethodDecl::Create (*ast, 1471 cxx_record_decl, 1472 SourceLocation(), 1473 DeclarationNameInfo (decl_name, SourceLocation()), 1474 method_qual_type, 1475 NULL, // TypeSourceInfo * 1476 is_static, 1477 SC_None, 1478 is_inline, 1479 SourceLocation()); 1480 } 1481 } 1482 1483 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access); 1484 1485 cxx_method_decl->setAccess (access_specifier); 1486 cxx_method_decl->setVirtualAsWritten (is_virtual); 1487 1488 // Populate the method decl with parameter decls 1489 1490 llvm::SmallVector<ParmVarDecl *, 12> params; 1491 1492 for (int param_index = 0; 1493 param_index < num_params; 1494 ++param_index) 1495 { 1496 params.push_back (ParmVarDecl::Create (*ast, 1497 cxx_method_decl, 1498 SourceLocation(), 1499 SourceLocation(), 1500 NULL, // anonymous 1501 method_function_prototype->getArgType(param_index), 1502 NULL, 1503 SC_None, 1504 SC_None, 1505 NULL)); 1506 } 1507 1508 cxx_method_decl->setParams (params.data(), num_params); 1509 1510 cxx_record_decl->addDecl (cxx_method_decl); 1511 1512// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic()); 1513// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate()); 1514// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD()); 1515// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty()); 1516// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract()); 1517// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor()); 1518// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor()); 1519// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment()); 1520// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor()); 1521 return cxx_method_decl; 1522} 1523 1524bool 1525ClangASTContext::AddFieldToRecordType 1526( 1527 ASTContext *ast, 1528 clang_type_t record_clang_type, 1529 const char *name, 1530 clang_type_t field_type, 1531 AccessType access, 1532 uint32_t bitfield_bit_size 1533) 1534{ 1535 if (record_clang_type == NULL || field_type == NULL) 1536 return false; 1537 1538 IdentifierTable *identifier_table = &ast->Idents; 1539 1540 assert (ast != NULL); 1541 assert (identifier_table != NULL); 1542 1543 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); 1544 1545 const clang::Type *clang_type = record_qual_type.getTypePtr(); 1546 if (clang_type) 1547 { 1548 const RecordType *record_type = dyn_cast<RecordType>(clang_type); 1549 1550 if (record_type) 1551 { 1552 RecordDecl *record_decl = record_type->getDecl(); 1553 1554 clang::Expr *bit_width = NULL; 1555 if (bitfield_bit_size != 0) 1556 { 1557 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size); 1558 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation()); 1559 } 1560 FieldDecl *field = FieldDecl::Create (*ast, 1561 record_decl, 1562 SourceLocation(), 1563 SourceLocation(), 1564 name ? &identifier_table->get(name) : NULL, // Identifier 1565 QualType::getFromOpaquePtr(field_type), // Field type 1566 NULL, // DeclaratorInfo * 1567 bit_width, // BitWidth 1568 false); // Mutable 1569 1570 field->setAccess (ConvertAccessTypeToAccessSpecifier (access)); 1571 1572 if (field) 1573 { 1574 record_decl->addDecl(field); 1575 } 1576 } 1577 else 1578 { 1579 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type); 1580 if (objc_class_type) 1581 { 1582 bool is_synthesized = false; 1583 ClangASTContext::AddObjCClassIVar (ast, 1584 record_clang_type, 1585 name, 1586 field_type, 1587 access, 1588 bitfield_bit_size, 1589 is_synthesized); 1590 } 1591 } 1592 } 1593 return false; 1594} 1595 1596bool 1597ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 1598{ 1599 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 1600} 1601 1602bool 1603ClangASTContext::FieldIsBitfield 1604( 1605 ASTContext *ast, 1606 FieldDecl* field, 1607 uint32_t& bitfield_bit_size 1608) 1609{ 1610 if (ast == NULL || field == NULL) 1611 return false; 1612 1613 if (field->isBitField()) 1614 { 1615 Expr* bit_width_expr = field->getBitWidth(); 1616 if (bit_width_expr) 1617 { 1618 llvm::APSInt bit_width_apsint; 1619 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) 1620 { 1621 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 1622 return true; 1623 } 1624 } 1625 } 1626 return false; 1627} 1628 1629bool 1630ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 1631{ 1632 if (record_decl == NULL) 1633 return false; 1634 1635 if (!record_decl->field_empty()) 1636 return true; 1637 1638 // No fields, lets check this is a CXX record and check the base classes 1639 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1640 if (cxx_record_decl) 1641 { 1642 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1643 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1644 base_class != base_class_end; 1645 ++base_class) 1646 { 1647 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1648 if (RecordHasFields(base_class_decl)) 1649 return true; 1650 } 1651 } 1652 return false; 1653} 1654 1655void 1656ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) 1657{ 1658 if (clang_type) 1659 { 1660 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1661 1662 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr()); 1663 if (record_type) 1664 { 1665 RecordDecl *record_decl = record_type->getDecl(); 1666 if (record_decl) 1667 { 1668 uint32_t field_idx; 1669 RecordDecl::field_iterator field, field_end; 1670 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; 1671 field != field_end; 1672 ++field, ++field_idx) 1673 { 1674 // If no accessibility was assigned, assign the correct one 1675 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) 1676 field->setAccess ((AccessSpecifier)default_accessibility); 1677 } 1678 } 1679 } 1680 } 1681} 1682 1683#pragma mark C++ Base Classes 1684 1685CXXBaseSpecifier * 1686ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class) 1687{ 1688 if (base_class_type) 1689 return new CXXBaseSpecifier (SourceRange(), 1690 is_virtual, 1691 base_of_class, 1692 ConvertAccessTypeToAccessSpecifier (access), 1693 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)), 1694 SourceLocation()); 1695 return NULL; 1696} 1697 1698void 1699ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes) 1700{ 1701 for (unsigned i=0; i<num_base_classes; ++i) 1702 { 1703 delete base_classes[i]; 1704 base_classes[i] = NULL; 1705 } 1706} 1707 1708bool 1709ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) 1710{ 1711 if (class_clang_type) 1712 { 1713 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl(); 1714 if (cxx_record_decl) 1715 { 1716 cxx_record_decl->setBases(base_classes, num_base_classes); 1717 return true; 1718 } 1719 } 1720 return false; 1721} 1722#pragma mark Objective C Classes 1723 1724clang_type_t 1725ClangASTContext::CreateObjCClass 1726( 1727 const char *name, 1728 DeclContext *decl_ctx, 1729 bool isForwardDecl, 1730 bool isInternal 1731) 1732{ 1733 ASTContext *ast = getASTContext(); 1734 assert (ast != NULL); 1735 assert (name && name[0]); 1736 if (decl_ctx == NULL) 1737 decl_ctx = ast->getTranslationUnitDecl(); 1738 1739 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1740 // we will need to update this code. I was told to currently always use 1741 // the CXXRecordDecl class since we often don't know from debug information 1742 // if something is struct or a class, so we default to always use the more 1743 // complete definition just in case. 1744 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast, 1745 decl_ctx, 1746 SourceLocation(), 1747 &ast->Idents.get(name), 1748 SourceLocation(), 1749 isForwardDecl, 1750 isInternal); 1751 1752 return ast->getObjCInterfaceType(decl).getAsOpaquePtr(); 1753} 1754 1755bool 1756ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type) 1757{ 1758 if (class_opaque_type && super_opaque_type) 1759 { 1760 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1761 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type)); 1762 const clang::Type *class_type = class_qual_type.getTypePtr(); 1763 const clang::Type *super_type = super_qual_type.getTypePtr(); 1764 if (class_type && super_type) 1765 { 1766 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1767 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type); 1768 if (objc_class_type && objc_super_type) 1769 { 1770 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1771 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface(); 1772 if (class_interface_decl && super_interface_decl) 1773 { 1774 class_interface_decl->setSuperClass(super_interface_decl); 1775 return true; 1776 } 1777 } 1778 } 1779 } 1780 return false; 1781} 1782 1783 1784bool 1785ClangASTContext::AddObjCClassIVar 1786( 1787 ASTContext *ast, 1788 clang_type_t class_opaque_type, 1789 const char *name, 1790 clang_type_t ivar_opaque_type, 1791 AccessType access, 1792 uint32_t bitfield_bit_size, 1793 bool is_synthesized 1794) 1795{ 1796 if (class_opaque_type == NULL || ivar_opaque_type == NULL) 1797 return false; 1798 1799 IdentifierTable *identifier_table = &ast->Idents; 1800 1801 assert (ast != NULL); 1802 assert (identifier_table != NULL); 1803 1804 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1805 1806 const clang::Type *class_type = class_qual_type.getTypePtr(); 1807 if (class_type) 1808 { 1809 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1810 1811 if (objc_class_type) 1812 { 1813 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1814 1815 if (class_interface_decl) 1816 { 1817 clang::Expr *bit_width = NULL; 1818 if (bitfield_bit_size != 0) 1819 { 1820 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size); 1821 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation()); 1822 } 1823 1824 ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast, 1825 class_interface_decl, 1826 SourceLocation(), 1827 SourceLocation(), 1828 &identifier_table->get(name), // Identifier 1829 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type 1830 NULL, // TypeSourceInfo * 1831 ConvertAccessTypeToObjCIvarAccessControl (access), 1832 bit_width, 1833 is_synthesized); 1834 1835 if (field) 1836 { 1837 class_interface_decl->addDecl(field); 1838 return true; 1839 } 1840 } 1841 } 1842 } 1843 return false; 1844} 1845 1846 1847bool 1848ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass) 1849{ 1850 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1851 1852 const clang::Type *class_type = class_qual_type.getTypePtr(); 1853 if (class_type) 1854 { 1855 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1856 1857 if (objc_class_type) 1858 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass); 1859 } 1860 return false; 1861} 1862 1863bool 1864ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass) 1865{ 1866 while (class_interface_decl) 1867 { 1868 if (class_interface_decl->ivar_size() > 0) 1869 return true; 1870 1871 if (check_superclass) 1872 class_interface_decl = class_interface_decl->getSuperClass(); 1873 else 1874 break; 1875 } 1876 return false; 1877} 1878 1879ObjCMethodDecl * 1880ClangASTContext::AddMethodToObjCObjectType 1881( 1882 ASTContext *ast, 1883 clang_type_t class_opaque_type, 1884 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 1885 clang_type_t method_opaque_type, 1886 lldb::AccessType access 1887) 1888{ 1889 if (class_opaque_type == NULL || method_opaque_type == NULL) 1890 return NULL; 1891 1892 IdentifierTable *identifier_table = &ast->Idents; 1893 1894 assert (ast != NULL); 1895 assert (identifier_table != NULL); 1896 1897 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1898 1899 const clang::Type *class_type = class_qual_type.getTypePtr(); 1900 if (class_type == NULL) 1901 return NULL; 1902 1903 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1904 1905 if (objc_class_type == NULL) 1906 return NULL; 1907 1908 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1909 1910 if (class_interface_decl == NULL) 1911 return NULL; 1912 1913 const char *selector_start = ::strchr (name, ' '); 1914 if (selector_start == NULL) 1915 return NULL; 1916 1917 selector_start++; 1918 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_')) 1919 return NULL; 1920 llvm::SmallVector<IdentifierInfo *, 12> selector_idents; 1921 1922 size_t len = 0; 1923 const char *start; 1924 //printf ("name = '%s'\n", name); 1925 1926 unsigned num_selectors_with_args = 0; 1927 for (start = selector_start; 1928 start && *start != '\0' && *start != ']'; 1929 start += len) 1930 { 1931 len = ::strcspn(start, ":]"); 1932 bool has_arg = (start[len] == ':'); 1933 if (has_arg) 1934 ++num_selectors_with_args; 1935 selector_idents.push_back (&identifier_table->get (StringRef (start, len))); 1936 if (has_arg) 1937 len += 1; 1938 } 1939 1940 1941 if (selector_idents.size() == 0) 1942 return 0; 1943 1944 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0, 1945 selector_idents.data()); 1946 1947 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 1948 1949 // Populate the method decl with parameter decls 1950 const clang::Type *method_type(method_qual_type.getTypePtr()); 1951 1952 if (method_type == NULL) 1953 return NULL; 1954 1955 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type)); 1956 1957 if (!method_function_prototype) 1958 return NULL; 1959 1960 1961 bool is_variadic = false; 1962 bool is_synthesized = false; 1963 bool is_defined = false; 1964 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None; 1965 1966 const unsigned num_args = method_function_prototype->getNumArgs(); 1967 1968 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast, 1969 SourceLocation(), // beginLoc, 1970 SourceLocation(), // endLoc, 1971 method_selector, 1972 method_function_prototype->getResultType(), 1973 NULL, // TypeSourceInfo *ResultTInfo, 1974 GetDeclContextForType (class_opaque_type), 1975 name[0] == '-', 1976 is_variadic, 1977 is_synthesized, 1978 is_defined, 1979 imp_control, 1980 num_args); 1981 1982 1983 if (objc_method_decl == NULL) 1984 return NULL; 1985 1986 if (num_args > 0) 1987 { 1988 llvm::SmallVector<ParmVarDecl *, 12> params; 1989 1990 for (int param_index = 0; param_index < num_args; ++param_index) 1991 { 1992 params.push_back (ParmVarDecl::Create (*ast, 1993 objc_method_decl, 1994 SourceLocation(), 1995 SourceLocation(), 1996 NULL, // anonymous 1997 method_function_prototype->getArgType(param_index), 1998 NULL, 1999 SC_Auto, 2000 SC_Auto, 2001 NULL)); 2002 } 2003 2004 objc_method_decl->setMethodParams(*ast, params.data(), params.size(), num_args); 2005 } 2006 2007 class_interface_decl->addDecl (objc_method_decl); 2008 2009 2010 return objc_method_decl; 2011} 2012 2013 2014uint32_t 2015ClangASTContext::GetTypeInfo 2016( 2017 clang_type_t clang_type, 2018 clang::ASTContext *ast, 2019 clang_type_t *pointee_or_element_clang_type 2020) 2021{ 2022 if (clang_type == NULL) 2023 return 0; 2024 2025 if (pointee_or_element_clang_type) 2026 *pointee_or_element_clang_type = NULL; 2027 2028 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2029 2030 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2031 switch (type_class) 2032 { 2033 case clang::Type::Builtin: 2034 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2035 { 2036 case clang::BuiltinType::ObjCId: 2037 case clang::BuiltinType::ObjCClass: 2038 if (ast && pointee_or_element_clang_type) 2039 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr(); 2040 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue; 2041 2042 default: 2043 break; 2044 } 2045 return eTypeIsBuiltIn | eTypeHasValue; 2046 2047 case clang::Type::BlockPointer: 2048 if (pointee_or_element_clang_type) 2049 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2050 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; 2051 2052 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue; 2053 2054 case clang::Type::ConstantArray: 2055 case clang::Type::DependentSizedArray: 2056 case clang::Type::IncompleteArray: 2057 case clang::Type::VariableArray: 2058 if (pointee_or_element_clang_type) 2059 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr(); 2060 return eTypeHasChildren | eTypeIsArray; 2061 2062 case clang::Type::DependentName: return 0; 2063 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector; 2064 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate; 2065 case clang::Type::Decltype: return 0; 2066 2067 case clang::Type::Enum: 2068 if (pointee_or_element_clang_type) 2069 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr(); 2070 return eTypeIsEnumeration | eTypeHasValue; 2071 2072 case clang::Type::Elaborated: return 0; 2073 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector; 2074 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue; 2075 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue; 2076 case clang::Type::InjectedClassName: return 0; 2077 2078 case clang::Type::LValueReference: 2079 case clang::Type::RValueReference: 2080 if (pointee_or_element_clang_type) 2081 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(); 2082 return eTypeHasChildren | eTypeIsReference | eTypeHasValue; 2083 2084 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue; 2085 2086 case clang::Type::ObjCObjectPointer: 2087 if (pointee_or_element_clang_type) 2088 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2089 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue; 2090 2091 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 2092 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; 2093 2094 case clang::Type::Pointer: 2095 if (pointee_or_element_clang_type) 2096 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr(); 2097 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; 2098 2099 case clang::Type::Record: 2100 if (qual_type->getAsCXXRecordDecl()) 2101 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; 2102 else 2103 return eTypeHasChildren | eTypeIsStructUnion; 2104 break; 2105 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate; 2106 case clang::Type::TemplateTypeParm: return eTypeIsTemplate; 2107 case clang::Type::TemplateSpecialization: return eTypeIsTemplate; 2108 2109 case clang::Type::Typedef: 2110 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2111 ast, 2112 pointee_or_element_clang_type); 2113 2114 case clang::Type::TypeOfExpr: return 0; 2115 case clang::Type::TypeOf: return 0; 2116 case clang::Type::UnresolvedUsing: return 0; 2117 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector; 2118 default: return 0; 2119 } 2120 return 0; 2121} 2122 2123 2124#pragma mark Aggregate Types 2125 2126bool 2127ClangASTContext::IsAggregateType (clang_type_t clang_type) 2128{ 2129 if (clang_type == NULL) 2130 return false; 2131 2132 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2133 2134 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2135 switch (type_class) 2136 { 2137 case clang::Type::IncompleteArray: 2138 case clang::Type::VariableArray: 2139 case clang::Type::ConstantArray: 2140 case clang::Type::ExtVector: 2141 case clang::Type::Vector: 2142 case clang::Type::Record: 2143 case clang::Type::ObjCObject: 2144 case clang::Type::ObjCInterface: 2145 return true; 2146 2147 case clang::Type::Typedef: 2148 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2149 2150 default: 2151 break; 2152 } 2153 // The clang type does have a value 2154 return false; 2155} 2156 2157uint32_t 2158ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes) 2159{ 2160 if (clang_type == NULL) 2161 return 0; 2162 2163 uint32_t num_children = 0; 2164 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2165 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2166 switch (type_class) 2167 { 2168 case clang::Type::Builtin: 2169 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2170 { 2171 case clang::BuiltinType::ObjCId: // child is Class 2172 case clang::BuiltinType::ObjCClass: // child is Class 2173 num_children = 1; 2174 break; 2175 2176 default: 2177 break; 2178 } 2179 break; 2180 2181 case clang::Type::Complex: return 0; 2182 2183 case clang::Type::Record: 2184 if (GetCompleteQualType (ast, qual_type)) 2185 { 2186 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2187 const RecordDecl *record_decl = record_type->getDecl(); 2188 assert(record_decl); 2189 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2190 if (cxx_record_decl) 2191 { 2192 if (omit_empty_base_classes) 2193 { 2194 // Check each base classes to see if it or any of its 2195 // base classes contain any fields. This can help 2196 // limit the noise in variable views by not having to 2197 // show base classes that contain no members. 2198 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2199 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2200 base_class != base_class_end; 2201 ++base_class) 2202 { 2203 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2204 2205 // Skip empty base classes 2206 if (RecordHasFields(base_class_decl) == false) 2207 continue; 2208 2209 num_children++; 2210 } 2211 } 2212 else 2213 { 2214 // Include all base classes 2215 num_children += cxx_record_decl->getNumBases(); 2216 } 2217 2218 } 2219 RecordDecl::field_iterator field, field_end; 2220 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 2221 ++num_children; 2222 } 2223 break; 2224 2225 case clang::Type::ObjCObject: 2226 case clang::Type::ObjCInterface: 2227 if (GetCompleteQualType (ast, qual_type)) 2228 { 2229 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2230 assert (objc_class_type); 2231 if (objc_class_type) 2232 { 2233 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2234 2235 if (class_interface_decl) 2236 { 2237 2238 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2239 if (superclass_interface_decl) 2240 { 2241 if (omit_empty_base_classes) 2242 { 2243 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true)) 2244 ++num_children; 2245 } 2246 else 2247 ++num_children; 2248 } 2249 2250 num_children += class_interface_decl->ivar_size(); 2251 } 2252 } 2253 } 2254 break; 2255 2256 case clang::Type::ObjCObjectPointer: 2257 { 2258 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr()); 2259 QualType pointee_type = pointer_type->getPointeeType(); 2260 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2261 pointee_type.getAsOpaquePtr(), 2262 omit_empty_base_classes); 2263 // If this type points to a simple type, then it has 1 child 2264 if (num_pointee_children == 0) 2265 num_children = 1; 2266 else 2267 num_children = num_pointee_children; 2268 } 2269 break; 2270 2271 case clang::Type::ConstantArray: 2272 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 2273 break; 2274 2275 case clang::Type::Pointer: 2276 { 2277 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2278 QualType pointee_type (pointer_type->getPointeeType()); 2279 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2280 pointee_type.getAsOpaquePtr(), 2281 omit_empty_base_classes); 2282 if (num_pointee_children == 0) 2283 { 2284 // We have a pointer to a pointee type that claims it has no children. 2285 // We will want to look at 2286 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr()); 2287 } 2288 else 2289 num_children = num_pointee_children; 2290 } 2291 break; 2292 2293 case clang::Type::LValueReference: 2294 case clang::Type::RValueReference: 2295 { 2296 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2297 QualType pointee_type = reference_type->getPointeeType(); 2298 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast, 2299 pointee_type.getAsOpaquePtr(), 2300 omit_empty_base_classes); 2301 // If this type points to a simple type, then it has 1 child 2302 if (num_pointee_children == 0) 2303 num_children = 1; 2304 else 2305 num_children = num_pointee_children; 2306 } 2307 break; 2308 2309 2310 case clang::Type::Typedef: 2311 num_children = ClangASTContext::GetNumChildren (ast, 2312 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2313 omit_empty_base_classes); 2314 break; 2315 2316 default: 2317 break; 2318 } 2319 return num_children; 2320} 2321 2322// If a pointer to a pointee type (the clang_type arg) says that it has no 2323// children, then we either need to trust it, or override it and return a 2324// different result. For example, an "int *" has one child that is an integer, 2325// but a function pointer doesn't have any children. Likewise if a Record type 2326// claims it has no children, then there really is nothing to show. 2327uint32_t 2328ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type) 2329{ 2330 if (clang_type == NULL) 2331 return 0; 2332 2333 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2334 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2335 switch (type_class) 2336 { 2337 case clang::Type::Builtin: 2338 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 2339 { 2340 case clang::BuiltinType::UnknownAny: 2341 case clang::BuiltinType::Void: 2342 case clang::BuiltinType::NullPtr: 2343 return 0; 2344 case clang::BuiltinType::Bool: 2345 case clang::BuiltinType::Char_U: 2346 case clang::BuiltinType::UChar: 2347 case clang::BuiltinType::WChar_U: 2348 case clang::BuiltinType::Char16: 2349 case clang::BuiltinType::Char32: 2350 case clang::BuiltinType::UShort: 2351 case clang::BuiltinType::UInt: 2352 case clang::BuiltinType::ULong: 2353 case clang::BuiltinType::ULongLong: 2354 case clang::BuiltinType::UInt128: 2355 case clang::BuiltinType::Char_S: 2356 case clang::BuiltinType::SChar: 2357 case clang::BuiltinType::WChar_S: 2358 case clang::BuiltinType::Short: 2359 case clang::BuiltinType::Int: 2360 case clang::BuiltinType::Long: 2361 case clang::BuiltinType::LongLong: 2362 case clang::BuiltinType::Int128: 2363 case clang::BuiltinType::Float: 2364 case clang::BuiltinType::Double: 2365 case clang::BuiltinType::LongDouble: 2366 case clang::BuiltinType::Dependent: 2367 case clang::BuiltinType::Overload: 2368 case clang::BuiltinType::ObjCId: 2369 case clang::BuiltinType::ObjCClass: 2370 case clang::BuiltinType::ObjCSel: 2371 case clang::BuiltinType::BoundMember: 2372 return 1; 2373 } 2374 break; 2375 2376 case clang::Type::Complex: return 1; 2377 case clang::Type::Pointer: return 1; 2378 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them 2379 case clang::Type::LValueReference: return 1; 2380 case clang::Type::RValueReference: return 1; 2381 case clang::Type::MemberPointer: return 0; 2382 case clang::Type::ConstantArray: return 0; 2383 case clang::Type::IncompleteArray: return 0; 2384 case clang::Type::VariableArray: return 0; 2385 case clang::Type::DependentSizedArray: return 0; 2386 case clang::Type::DependentSizedExtVector: return 0; 2387 case clang::Type::Vector: return 0; 2388 case clang::Type::ExtVector: return 0; 2389 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children... 2390 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children... 2391 case clang::Type::UnresolvedUsing: return 0; 2392 case clang::Type::Paren: return 0; 2393 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 2394 case clang::Type::TypeOfExpr: return 0; 2395 case clang::Type::TypeOf: return 0; 2396 case clang::Type::Decltype: return 0; 2397 case clang::Type::Record: return 0; 2398 case clang::Type::Enum: return 1; 2399 case clang::Type::Elaborated: return 1; 2400 case clang::Type::TemplateTypeParm: return 1; 2401 case clang::Type::SubstTemplateTypeParm: return 1; 2402 case clang::Type::TemplateSpecialization: return 1; 2403 case clang::Type::InjectedClassName: return 0; 2404 case clang::Type::DependentName: return 1; 2405 case clang::Type::DependentTemplateSpecialization: return 1; 2406 case clang::Type::ObjCObject: return 0; 2407 case clang::Type::ObjCInterface: return 0; 2408 case clang::Type::ObjCObjectPointer: return 1; 2409 default: 2410 break; 2411 } 2412 return 0; 2413} 2414 2415clang_type_t 2416ClangASTContext::GetChildClangTypeAtIndex 2417( 2418 const char *parent_name, 2419 clang_type_t parent_clang_type, 2420 uint32_t idx, 2421 bool transparent_pointers, 2422 bool omit_empty_base_classes, 2423 std::string& child_name, 2424 uint32_t &child_byte_size, 2425 int32_t &child_byte_offset, 2426 uint32_t &child_bitfield_bit_size, 2427 uint32_t &child_bitfield_bit_offset, 2428 bool &child_is_base_class, 2429 bool &child_is_deref_of_parent 2430) 2431{ 2432 if (parent_clang_type) 2433 2434 return GetChildClangTypeAtIndex (getASTContext(), 2435 parent_name, 2436 parent_clang_type, 2437 idx, 2438 transparent_pointers, 2439 omit_empty_base_classes, 2440 child_name, 2441 child_byte_size, 2442 child_byte_offset, 2443 child_bitfield_bit_size, 2444 child_bitfield_bit_offset, 2445 child_is_base_class, 2446 child_is_deref_of_parent); 2447 return NULL; 2448} 2449 2450clang_type_t 2451ClangASTContext::GetChildClangTypeAtIndex 2452( 2453 ASTContext *ast, 2454 const char *parent_name, 2455 clang_type_t parent_clang_type, 2456 uint32_t idx, 2457 bool transparent_pointers, 2458 bool omit_empty_base_classes, 2459 std::string& child_name, 2460 uint32_t &child_byte_size, 2461 int32_t &child_byte_offset, 2462 uint32_t &child_bitfield_bit_size, 2463 uint32_t &child_bitfield_bit_offset, 2464 bool &child_is_base_class, 2465 bool &child_is_deref_of_parent 2466) 2467{ 2468 if (parent_clang_type == NULL) 2469 return NULL; 2470 2471 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes)) 2472 { 2473 uint32_t bit_offset; 2474 child_bitfield_bit_size = 0; 2475 child_bitfield_bit_offset = 0; 2476 child_is_base_class = false; 2477 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); 2478 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass(); 2479 switch (parent_type_class) 2480 { 2481 case clang::Type::Builtin: 2482 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind()) 2483 { 2484 case clang::BuiltinType::ObjCId: 2485 case clang::BuiltinType::ObjCClass: 2486 child_name = "isa"; 2487 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT; 2488 return ast->ObjCBuiltinClassTy.getAsOpaquePtr(); 2489 2490 default: 2491 break; 2492 } 2493 break; 2494 2495 case clang::Type::Record: 2496 if (GetCompleteQualType (ast, parent_qual_type)) 2497 { 2498 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); 2499 const RecordDecl *record_decl = record_type->getDecl(); 2500 assert(record_decl); 2501 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl); 2502 uint32_t child_idx = 0; 2503 2504 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2505 if (cxx_record_decl) 2506 { 2507 // We might have base classes to print out first 2508 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2509 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2510 base_class != base_class_end; 2511 ++base_class) 2512 { 2513 const CXXRecordDecl *base_class_decl = NULL; 2514 2515 // Skip empty base classes 2516 if (omit_empty_base_classes) 2517 { 2518 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2519 if (RecordHasFields(base_class_decl) == false) 2520 continue; 2521 } 2522 2523 if (idx == child_idx) 2524 { 2525 if (base_class_decl == NULL) 2526 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2527 2528 2529 if (base_class->isVirtual()) 2530 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8; 2531 else 2532 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8; 2533 2534 // Base classes should be a multiple of 8 bits in size 2535 assert (bit_offset % 8 == 0); 2536 child_byte_offset = bit_offset/8; 2537 std::string base_class_type_name(base_class->getType().getAsString()); 2538 2539 child_name.assign(base_class_type_name.c_str()); 2540 2541 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType()); 2542 2543 // Base classes bit sizes should be a multiple of 8 bits in size 2544 assert (clang_type_info_bit_size % 8 == 0); 2545 child_byte_size = clang_type_info_bit_size / 8; 2546 child_is_base_class = true; 2547 return base_class->getType().getAsOpaquePtr(); 2548 } 2549 // We don't increment the child index in the for loop since we might 2550 // be skipping empty base classes 2551 ++child_idx; 2552 } 2553 } 2554 // Make sure index is in range... 2555 uint32_t field_idx = 0; 2556 RecordDecl::field_iterator field, field_end; 2557 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) 2558 { 2559 if (idx == child_idx) 2560 { 2561 // Print the member type if requested 2562 // Print the member name and equal sign 2563 child_name.assign(field->getNameAsString().c_str()); 2564 2565 // Figure out the type byte size (field_type_info.first) and 2566 // alignment (field_type_info.second) from the AST context. 2567 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType()); 2568 assert(field_idx < record_layout.getFieldCount()); 2569 2570 child_byte_size = field_type_info.first / 8; 2571 2572 // Figure out the field offset within the current struct/union/class type 2573 bit_offset = record_layout.getFieldOffset (field_idx); 2574 child_byte_offset = bit_offset / 8; 2575 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size)) 2576 child_bitfield_bit_offset = bit_offset % 8; 2577 2578 return field->getType().getAsOpaquePtr(); 2579 } 2580 } 2581 } 2582 break; 2583 2584 case clang::Type::ObjCObject: 2585 case clang::Type::ObjCInterface: 2586 if (GetCompleteQualType (ast, parent_qual_type)) 2587 { 2588 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr()); 2589 assert (objc_class_type); 2590 if (objc_class_type) 2591 { 2592 uint32_t child_idx = 0; 2593 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2594 2595 if (class_interface_decl) 2596 { 2597 2598 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl); 2599 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2600 if (superclass_interface_decl) 2601 { 2602 if (omit_empty_base_classes) 2603 { 2604 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0) 2605 { 2606 if (idx == 0) 2607 { 2608 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl)); 2609 2610 2611 child_name.assign(superclass_interface_decl->getNameAsString().c_str()); 2612 2613 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr()); 2614 2615 child_byte_size = ivar_type_info.first / 8; 2616 child_byte_offset = 0; 2617 child_is_base_class = true; 2618 2619 return ivar_qual_type.getAsOpaquePtr(); 2620 } 2621 2622 ++child_idx; 2623 } 2624 } 2625 else 2626 ++child_idx; 2627 } 2628 2629 const uint32_t superclass_idx = child_idx; 2630 2631 if (idx < (child_idx + class_interface_decl->ivar_size())) 2632 { 2633 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2634 2635 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 2636 { 2637 if (child_idx == idx) 2638 { 2639 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2640 2641 QualType ivar_qual_type(ivar_decl->getType()); 2642 2643 child_name.assign(ivar_decl->getNameAsString().c_str()); 2644 2645 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr()); 2646 2647 child_byte_size = ivar_type_info.first / 8; 2648 2649 // Figure out the field offset within the current struct/union/class type 2650 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx); 2651 child_byte_offset = bit_offset / 8; 2652 2653 return ivar_qual_type.getAsOpaquePtr(); 2654 } 2655 ++child_idx; 2656 } 2657 } 2658 } 2659 } 2660 } 2661 break; 2662 2663 case clang::Type::ObjCObjectPointer: 2664 { 2665 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr()); 2666 QualType pointee_type = pointer_type->getPointeeType(); 2667 2668 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2669 { 2670 child_is_deref_of_parent = false; 2671 bool tmp_child_is_deref_of_parent = false; 2672 return GetChildClangTypeAtIndex (ast, 2673 parent_name, 2674 pointer_type->getPointeeType().getAsOpaquePtr(), 2675 idx, 2676 transparent_pointers, 2677 omit_empty_base_classes, 2678 child_name, 2679 child_byte_size, 2680 child_byte_offset, 2681 child_bitfield_bit_size, 2682 child_bitfield_bit_offset, 2683 child_is_base_class, 2684 tmp_child_is_deref_of_parent); 2685 } 2686 else 2687 { 2688 child_is_deref_of_parent = true; 2689 if (parent_name) 2690 { 2691 child_name.assign(1, '*'); 2692 child_name += parent_name; 2693 } 2694 2695 // We have a pointer to an simple type 2696 if (idx == 0) 2697 { 2698 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 2699 assert(clang_type_info.first % 8 == 0); 2700 child_byte_size = clang_type_info.first / 8; 2701 child_byte_offset = 0; 2702 return pointee_type.getAsOpaquePtr(); 2703 } 2704 } 2705 } 2706 break; 2707 2708 case clang::Type::ConstantArray: 2709 { 2710 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 2711 const uint64_t element_count = array->getSize().getLimitedValue(); 2712 2713 if (idx < element_count) 2714 { 2715 if (GetCompleteQualType (ast, array->getElementType())) 2716 { 2717 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 2718 2719 char element_name[64]; 2720 ::snprintf (element_name, sizeof (element_name), "[%u]", idx); 2721 2722 child_name.assign(element_name); 2723 assert(field_type_info.first % 8 == 0); 2724 child_byte_size = field_type_info.first / 8; 2725 child_byte_offset = idx * child_byte_size; 2726 return array->getElementType().getAsOpaquePtr(); 2727 } 2728 } 2729 } 2730 break; 2731 2732 case clang::Type::Pointer: 2733 { 2734 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); 2735 QualType pointee_type = pointer_type->getPointeeType(); 2736 2737 // Don't dereference "void *" pointers 2738 if (pointee_type->isVoidType()) 2739 return NULL; 2740 2741 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2742 { 2743 child_is_deref_of_parent = false; 2744 bool tmp_child_is_deref_of_parent = false; 2745 return GetChildClangTypeAtIndex (ast, 2746 parent_name, 2747 pointer_type->getPointeeType().getAsOpaquePtr(), 2748 idx, 2749 transparent_pointers, 2750 omit_empty_base_classes, 2751 child_name, 2752 child_byte_size, 2753 child_byte_offset, 2754 child_bitfield_bit_size, 2755 child_bitfield_bit_offset, 2756 child_is_base_class, 2757 tmp_child_is_deref_of_parent); 2758 } 2759 else 2760 { 2761 child_is_deref_of_parent = true; 2762 2763 if (parent_name) 2764 { 2765 child_name.assign(1, '*'); 2766 child_name += parent_name; 2767 } 2768 2769 // We have a pointer to an simple type 2770 if (idx == 0) 2771 { 2772 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 2773 assert(clang_type_info.first % 8 == 0); 2774 child_byte_size = clang_type_info.first / 8; 2775 child_byte_offset = 0; 2776 return pointee_type.getAsOpaquePtr(); 2777 } 2778 } 2779 } 2780 break; 2781 2782 case clang::Type::LValueReference: 2783 case clang::Type::RValueReference: 2784 { 2785 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr()); 2786 QualType pointee_type(reference_type->getPointeeType()); 2787 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr(); 2788 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type)) 2789 { 2790 child_is_deref_of_parent = false; 2791 bool tmp_child_is_deref_of_parent = false; 2792 return GetChildClangTypeAtIndex (ast, 2793 parent_name, 2794 pointee_clang_type, 2795 idx, 2796 transparent_pointers, 2797 omit_empty_base_classes, 2798 child_name, 2799 child_byte_size, 2800 child_byte_offset, 2801 child_bitfield_bit_size, 2802 child_bitfield_bit_offset, 2803 child_is_base_class, 2804 tmp_child_is_deref_of_parent); 2805 } 2806 else 2807 { 2808 if (parent_name) 2809 { 2810 child_name.assign(1, '&'); 2811 child_name += parent_name; 2812 } 2813 2814 // We have a pointer to an simple type 2815 if (idx == 0) 2816 { 2817 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 2818 assert(clang_type_info.first % 8 == 0); 2819 child_byte_size = clang_type_info.first / 8; 2820 child_byte_offset = 0; 2821 return pointee_type.getAsOpaquePtr(); 2822 } 2823 } 2824 } 2825 break; 2826 2827 case clang::Type::Typedef: 2828 return GetChildClangTypeAtIndex (ast, 2829 parent_name, 2830 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 2831 idx, 2832 transparent_pointers, 2833 omit_empty_base_classes, 2834 child_name, 2835 child_byte_size, 2836 child_byte_offset, 2837 child_bitfield_bit_size, 2838 child_bitfield_bit_offset, 2839 child_is_base_class, 2840 child_is_deref_of_parent); 2841 break; 2842 2843 default: 2844 break; 2845 } 2846 } 2847 return NULL; 2848} 2849 2850static inline bool 2851BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 2852{ 2853 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false; 2854} 2855 2856static uint32_t 2857GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 2858{ 2859 uint32_t num_bases = 0; 2860 if (cxx_record_decl) 2861 { 2862 if (omit_empty_base_classes) 2863 { 2864 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2865 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2866 base_class != base_class_end; 2867 ++base_class) 2868 { 2869 // Skip empty base classes 2870 if (omit_empty_base_classes) 2871 { 2872 if (BaseSpecifierIsEmpty (base_class)) 2873 continue; 2874 } 2875 ++num_bases; 2876 } 2877 } 2878 else 2879 num_bases = cxx_record_decl->getNumBases(); 2880 } 2881 return num_bases; 2882} 2883 2884 2885static uint32_t 2886GetIndexForRecordBase 2887( 2888 const RecordDecl *record_decl, 2889 const CXXBaseSpecifier *base_spec, 2890 bool omit_empty_base_classes 2891) 2892{ 2893 uint32_t child_idx = 0; 2894 2895 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2896 2897// const char *super_name = record_decl->getNameAsCString(); 2898// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); 2899// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 2900// 2901 if (cxx_record_decl) 2902 { 2903 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2904 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2905 base_class != base_class_end; 2906 ++base_class) 2907 { 2908 if (omit_empty_base_classes) 2909 { 2910 if (BaseSpecifierIsEmpty (base_class)) 2911 continue; 2912 } 2913 2914// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, 2915// child_idx, 2916// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 2917// 2918// 2919 if (base_class == base_spec) 2920 return child_idx; 2921 ++child_idx; 2922 } 2923 } 2924 2925 return UINT32_MAX; 2926} 2927 2928 2929static uint32_t 2930GetIndexForRecordChild 2931( 2932 const RecordDecl *record_decl, 2933 NamedDecl *canonical_decl, 2934 bool omit_empty_base_classes 2935) 2936{ 2937 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); 2938 2939// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2940// 2941//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); 2942// if (cxx_record_decl) 2943// { 2944// CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2945// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2946// base_class != base_class_end; 2947// ++base_class) 2948// { 2949// if (omit_empty_base_classes) 2950// { 2951// if (BaseSpecifierIsEmpty (base_class)) 2952// continue; 2953// } 2954// 2955//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 2956//// record_decl->getNameAsCString(), 2957//// canonical_decl->getNameAsCString(), 2958//// child_idx, 2959//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 2960// 2961// 2962// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2963// if (curr_base_class_decl == canonical_decl) 2964// { 2965// return child_idx; 2966// } 2967// ++child_idx; 2968// } 2969// } 2970// 2971// const uint32_t num_bases = child_idx; 2972 RecordDecl::field_iterator field, field_end; 2973 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 2974 field != field_end; 2975 ++field, ++child_idx) 2976 { 2977// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", 2978// record_decl->getNameAsCString(), 2979// canonical_decl->getNameAsCString(), 2980// child_idx - num_bases, 2981// field->getNameAsCString()); 2982 2983 if (field->getCanonicalDecl() == canonical_decl) 2984 return child_idx; 2985 } 2986 2987 return UINT32_MAX; 2988} 2989 2990// Look for a child member (doesn't include base classes, but it does include 2991// their members) in the type hierarchy. Returns an index path into "clang_type" 2992// on how to reach the appropriate member. 2993// 2994// class A 2995// { 2996// public: 2997// int m_a; 2998// int m_b; 2999// }; 3000// 3001// class B 3002// { 3003// }; 3004// 3005// class C : 3006// public B, 3007// public A 3008// { 3009// }; 3010// 3011// If we have a clang type that describes "class C", and we wanted to looked 3012// "m_b" in it: 3013// 3014// With omit_empty_base_classes == false we would get an integer array back with: 3015// { 1, 1 } 3016// The first index 1 is the child index for "class A" within class C 3017// The second index 1 is the child index for "m_b" within class A 3018// 3019// With omit_empty_base_classes == true we would get an integer array back with: 3020// { 0, 1 } 3021// The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count) 3022// The second index 1 is the child index for "m_b" within class A 3023 3024size_t 3025ClangASTContext::GetIndexOfChildMemberWithName 3026( 3027 ASTContext *ast, 3028 clang_type_t clang_type, 3029 const char *name, 3030 bool omit_empty_base_classes, 3031 std::vector<uint32_t>& child_indexes 3032) 3033{ 3034 if (clang_type && name && name[0]) 3035 { 3036 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3037 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3038 switch (type_class) 3039 { 3040 case clang::Type::Record: 3041 if (GetCompleteQualType (ast, qual_type)) 3042 { 3043 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 3044 const RecordDecl *record_decl = record_type->getDecl(); 3045 3046 assert(record_decl); 3047 uint32_t child_idx = 0; 3048 3049 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3050 3051 // Try and find a field that matches NAME 3052 RecordDecl::field_iterator field, field_end; 3053 StringRef name_sref(name); 3054 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 3055 field != field_end; 3056 ++field, ++child_idx) 3057 { 3058 if (field->getName().equals (name_sref)) 3059 { 3060 // We have to add on the number of base classes to this index! 3061 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); 3062 return child_indexes.size(); 3063 } 3064 } 3065 3066 if (cxx_record_decl) 3067 { 3068 const RecordDecl *parent_record_decl = cxx_record_decl; 3069 3070 //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 3071 3072 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 3073 // Didn't find things easily, lets let clang do its thang... 3074 IdentifierInfo & ident_ref = ast->Idents.get(name, name + strlen (name)); 3075 DeclarationName decl_name(&ident_ref); 3076 3077 CXXBasePaths paths; 3078 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, 3079 decl_name.getAsOpaquePtr(), 3080 paths)) 3081 { 3082 CXXBasePaths::const_paths_iterator path, path_end = paths.end(); 3083 for (path = paths.begin(); path != path_end; ++path) 3084 { 3085 const size_t num_path_elements = path->size(); 3086 for (size_t e=0; e<num_path_elements; ++e) 3087 { 3088 CXXBasePathElement elem = (*path)[e]; 3089 3090 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); 3091 if (child_idx == UINT32_MAX) 3092 { 3093 child_indexes.clear(); 3094 return 0; 3095 } 3096 else 3097 { 3098 child_indexes.push_back (child_idx); 3099 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); 3100 } 3101 } 3102 DeclContext::lookup_iterator named_decl_pos; 3103 for (named_decl_pos = path->Decls.first; 3104 named_decl_pos != path->Decls.second && parent_record_decl; 3105 ++named_decl_pos) 3106 { 3107 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); 3108 3109 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); 3110 if (child_idx == UINT32_MAX) 3111 { 3112 child_indexes.clear(); 3113 return 0; 3114 } 3115 else 3116 { 3117 child_indexes.push_back (child_idx); 3118 } 3119 } 3120 } 3121 return child_indexes.size(); 3122 } 3123 } 3124 3125 } 3126 break; 3127 3128 case clang::Type::ObjCObject: 3129 case clang::Type::ObjCInterface: 3130 if (GetCompleteQualType (ast, qual_type)) 3131 { 3132 StringRef name_sref(name); 3133 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 3134 assert (objc_class_type); 3135 if (objc_class_type) 3136 { 3137 uint32_t child_idx = 0; 3138 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3139 3140 if (class_interface_decl) 3141 { 3142 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3143 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 3144 3145 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx) 3146 { 3147 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3148 3149 if (ivar_decl->getName().equals (name_sref)) 3150 { 3151 if ((!omit_empty_base_classes && superclass_interface_decl) || 3152 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 3153 ++child_idx; 3154 3155 child_indexes.push_back (child_idx); 3156 return child_indexes.size(); 3157 } 3158 } 3159 3160 if (superclass_interface_decl) 3161 { 3162 // The super class index is always zero for ObjC classes, 3163 // so we push it onto the child indexes in case we find 3164 // an ivar in our superclass... 3165 child_indexes.push_back (0); 3166 3167 if (GetIndexOfChildMemberWithName (ast, 3168 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), 3169 name, 3170 omit_empty_base_classes, 3171 child_indexes)) 3172 { 3173 // We did find an ivar in a superclass so just 3174 // return the results! 3175 return child_indexes.size(); 3176 } 3177 3178 // We didn't find an ivar matching "name" in our 3179 // superclass, pop the superclass zero index that 3180 // we pushed on above. 3181 child_indexes.pop_back(); 3182 } 3183 } 3184 } 3185 } 3186 break; 3187 3188 case clang::Type::ObjCObjectPointer: 3189 { 3190 return GetIndexOfChildMemberWithName (ast, 3191 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 3192 name, 3193 omit_empty_base_classes, 3194 child_indexes); 3195 } 3196 break; 3197 3198 3199 case clang::Type::ConstantArray: 3200 { 3201// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 3202// const uint64_t element_count = array->getSize().getLimitedValue(); 3203// 3204// if (idx < element_count) 3205// { 3206// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 3207// 3208// char element_name[32]; 3209// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 3210// 3211// child_name.assign(element_name); 3212// assert(field_type_info.first % 8 == 0); 3213// child_byte_size = field_type_info.first / 8; 3214// child_byte_offset = idx * child_byte_size; 3215// return array->getElementType().getAsOpaquePtr(); 3216// } 3217 } 3218 break; 3219 3220// case clang::Type::MemberPointerType: 3221// { 3222// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 3223// QualType pointee_type = mem_ptr_type->getPointeeType(); 3224// 3225// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3226// { 3227// return GetIndexOfChildWithName (ast, 3228// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 3229// name); 3230// } 3231// } 3232// break; 3233// 3234 case clang::Type::LValueReference: 3235 case clang::Type::RValueReference: 3236 { 3237 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3238 QualType pointee_type = reference_type->getPointeeType(); 3239 3240 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3241 { 3242 return GetIndexOfChildMemberWithName (ast, 3243 reference_type->getPointeeType().getAsOpaquePtr(), 3244 name, 3245 omit_empty_base_classes, 3246 child_indexes); 3247 } 3248 } 3249 break; 3250 3251 case clang::Type::Pointer: 3252 { 3253 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 3254 QualType pointee_type = pointer_type->getPointeeType(); 3255 3256 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3257 { 3258 return GetIndexOfChildMemberWithName (ast, 3259 pointer_type->getPointeeType().getAsOpaquePtr(), 3260 name, 3261 omit_empty_base_classes, 3262 child_indexes); 3263 } 3264 else 3265 { 3266// if (parent_name) 3267// { 3268// child_name.assign(1, '*'); 3269// child_name += parent_name; 3270// } 3271// 3272// // We have a pointer to an simple type 3273// if (idx == 0) 3274// { 3275// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3276// assert(clang_type_info.first % 8 == 0); 3277// child_byte_size = clang_type_info.first / 8; 3278// child_byte_offset = 0; 3279// return pointee_type.getAsOpaquePtr(); 3280// } 3281 } 3282 } 3283 break; 3284 3285 case clang::Type::Typedef: 3286 return GetIndexOfChildMemberWithName (ast, 3287 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 3288 name, 3289 omit_empty_base_classes, 3290 child_indexes); 3291 3292 default: 3293 break; 3294 } 3295 } 3296 return 0; 3297} 3298 3299 3300// Get the index of the child of "clang_type" whose name matches. This function 3301// doesn't descend into the children, but only looks one level deep and name 3302// matches can include base class names. 3303 3304uint32_t 3305ClangASTContext::GetIndexOfChildWithName 3306( 3307 ASTContext *ast, 3308 clang_type_t clang_type, 3309 const char *name, 3310 bool omit_empty_base_classes 3311) 3312{ 3313 if (clang_type && name && name[0]) 3314 { 3315 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3316 3317 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3318 3319 switch (type_class) 3320 { 3321 case clang::Type::Record: 3322 if (GetCompleteQualType (ast, qual_type)) 3323 { 3324 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 3325 const RecordDecl *record_decl = record_type->getDecl(); 3326 3327 assert(record_decl); 3328 uint32_t child_idx = 0; 3329 3330 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 3331 3332 if (cxx_record_decl) 3333 { 3334 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 3335 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 3336 base_class != base_class_end; 3337 ++base_class) 3338 { 3339 // Skip empty base classes 3340 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 3341 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) 3342 continue; 3343 3344 if (base_class->getType().getAsString().compare (name) == 0) 3345 return child_idx; 3346 ++child_idx; 3347 } 3348 } 3349 3350 // Try and find a field that matches NAME 3351 RecordDecl::field_iterator field, field_end; 3352 StringRef name_sref(name); 3353 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 3354 field != field_end; 3355 ++field, ++child_idx) 3356 { 3357 if (field->getName().equals (name_sref)) 3358 return child_idx; 3359 } 3360 3361 } 3362 break; 3363 3364 case clang::Type::ObjCObject: 3365 case clang::Type::ObjCInterface: 3366 if (GetCompleteQualType (ast, qual_type)) 3367 { 3368 StringRef name_sref(name); 3369 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 3370 assert (objc_class_type); 3371 if (objc_class_type) 3372 { 3373 uint32_t child_idx = 0; 3374 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3375 3376 if (class_interface_decl) 3377 { 3378 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 3379 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 3380 3381 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 3382 { 3383 const ObjCIvarDecl* ivar_decl = *ivar_pos; 3384 3385 if (ivar_decl->getName().equals (name_sref)) 3386 { 3387 if ((!omit_empty_base_classes && superclass_interface_decl) || 3388 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 3389 ++child_idx; 3390 3391 return child_idx; 3392 } 3393 } 3394 3395 if (superclass_interface_decl) 3396 { 3397 if (superclass_interface_decl->getName().equals (name_sref)) 3398 return 0; 3399 } 3400 } 3401 } 3402 } 3403 break; 3404 3405 case clang::Type::ObjCObjectPointer: 3406 { 3407 return GetIndexOfChildWithName (ast, 3408 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 3409 name, 3410 omit_empty_base_classes); 3411 } 3412 break; 3413 3414 case clang::Type::ConstantArray: 3415 { 3416// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 3417// const uint64_t element_count = array->getSize().getLimitedValue(); 3418// 3419// if (idx < element_count) 3420// { 3421// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType()); 3422// 3423// char element_name[32]; 3424// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 3425// 3426// child_name.assign(element_name); 3427// assert(field_type_info.first % 8 == 0); 3428// child_byte_size = field_type_info.first / 8; 3429// child_byte_offset = idx * child_byte_size; 3430// return array->getElementType().getAsOpaquePtr(); 3431// } 3432 } 3433 break; 3434 3435// case clang::Type::MemberPointerType: 3436// { 3437// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 3438// QualType pointee_type = mem_ptr_type->getPointeeType(); 3439// 3440// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3441// { 3442// return GetIndexOfChildWithName (ast, 3443// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 3444// name); 3445// } 3446// } 3447// break; 3448// 3449 case clang::Type::LValueReference: 3450 case clang::Type::RValueReference: 3451 { 3452 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3453 QualType pointee_type = reference_type->getPointeeType(); 3454 3455 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3456 { 3457 return GetIndexOfChildWithName (ast, 3458 reference_type->getPointeeType().getAsOpaquePtr(), 3459 name, 3460 omit_empty_base_classes); 3461 } 3462 } 3463 break; 3464 3465 case clang::Type::Pointer: 3466 { 3467 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 3468 QualType pointee_type = pointer_type->getPointeeType(); 3469 3470 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 3471 { 3472 return GetIndexOfChildWithName (ast, 3473 pointer_type->getPointeeType().getAsOpaquePtr(), 3474 name, 3475 omit_empty_base_classes); 3476 } 3477 else 3478 { 3479// if (parent_name) 3480// { 3481// child_name.assign(1, '*'); 3482// child_name += parent_name; 3483// } 3484// 3485// // We have a pointer to an simple type 3486// if (idx == 0) 3487// { 3488// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type); 3489// assert(clang_type_info.first % 8 == 0); 3490// child_byte_size = clang_type_info.first / 8; 3491// child_byte_offset = 0; 3492// return pointee_type.getAsOpaquePtr(); 3493// } 3494 } 3495 } 3496 break; 3497 3498 case clang::Type::Typedef: 3499 return GetIndexOfChildWithName (ast, 3500 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), 3501 name, 3502 omit_empty_base_classes); 3503 3504 default: 3505 break; 3506 } 3507 } 3508 return UINT32_MAX; 3509} 3510 3511#pragma mark TagType 3512 3513bool 3514ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind) 3515{ 3516 if (tag_clang_type) 3517 { 3518 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); 3519 const clang::Type *clang_type = tag_qual_type.getTypePtr(); 3520 if (clang_type) 3521 { 3522 const TagType *tag_type = dyn_cast<TagType>(clang_type); 3523 if (tag_type) 3524 { 3525 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); 3526 if (tag_decl) 3527 { 3528 tag_decl->setTagKind ((TagDecl::TagKind)kind); 3529 return true; 3530 } 3531 } 3532 } 3533 } 3534 return false; 3535} 3536 3537 3538#pragma mark DeclContext Functions 3539 3540DeclContext * 3541ClangASTContext::GetDeclContextForType (clang_type_t clang_type) 3542{ 3543 if (clang_type == NULL) 3544 return NULL; 3545 3546 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 3547 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3548 switch (type_class) 3549 { 3550 case clang::Type::FunctionNoProto: break; 3551 case clang::Type::FunctionProto: break; 3552 case clang::Type::IncompleteArray: break; 3553 case clang::Type::VariableArray: break; 3554 case clang::Type::ConstantArray: break; 3555 case clang::Type::DependentSizedArray: break; 3556 case clang::Type::ExtVector: break; 3557 case clang::Type::DependentSizedExtVector: break; 3558 case clang::Type::Vector: break; 3559 case clang::Type::Builtin: break; 3560 case clang::Type::BlockPointer: break; 3561 case clang::Type::Pointer: break; 3562 case clang::Type::LValueReference: break; 3563 case clang::Type::RValueReference: break; 3564 case clang::Type::MemberPointer: break; 3565 case clang::Type::Complex: break; 3566 case clang::Type::ObjCObject: break; 3567 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface(); 3568 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr()); 3569 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl(); 3570 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl(); 3571 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 3572 3573 case clang::Type::TypeOfExpr: break; 3574 case clang::Type::TypeOf: break; 3575 case clang::Type::Decltype: break; 3576 //case clang::Type::QualifiedName: break; 3577 case clang::Type::TemplateSpecialization: break; 3578 case clang::Type::DependentTemplateSpecialization: break; 3579 case clang::Type::TemplateTypeParm: break; 3580 case clang::Type::SubstTemplateTypeParm: break; 3581 case clang::Type::SubstTemplateTypeParmPack:break; 3582 case clang::Type::PackExpansion: break; 3583 case clang::Type::UnresolvedUsing: break; 3584 case clang::Type::Paren: break; 3585 case clang::Type::Elaborated: break; 3586 case clang::Type::Attributed: break; 3587 case clang::Type::Auto: break; 3588 case clang::Type::InjectedClassName: break; 3589 case clang::Type::DependentName: break; 3590 } 3591 // No DeclContext in this type... 3592 return NULL; 3593} 3594 3595#pragma mark Namespace Declarations 3596 3597NamespaceDecl * 3598ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx) 3599{ 3600 // TODO: Do something intelligent with the Declaration object passed in 3601 // like maybe filling in the SourceLocation with it... 3602 if (name) 3603 { 3604 ASTContext *ast = getASTContext(); 3605 if (decl_ctx == NULL) 3606 decl_ctx = ast->getTranslationUnitDecl(); 3607 return NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), &ast->Idents.get(name)); 3608 } 3609 return NULL; 3610} 3611 3612 3613#pragma mark Function Types 3614 3615FunctionDecl * 3616ClangASTContext::CreateFunctionDeclaration (const char *name, clang_type_t function_clang_type, int storage, bool is_inline) 3617{ 3618 if (name) 3619 { 3620 ASTContext *ast = getASTContext(); 3621 assert (ast != NULL); 3622 3623 if (name && name[0]) 3624 { 3625 return FunctionDecl::Create(*ast, 3626 ast->getTranslationUnitDecl(), 3627 SourceLocation(), 3628 SourceLocation(), 3629 DeclarationName (&ast->Idents.get(name)), 3630 QualType::getFromOpaquePtr(function_clang_type), 3631 NULL, 3632 (FunctionDecl::StorageClass)storage, 3633 (FunctionDecl::StorageClass)storage, 3634 is_inline); 3635 } 3636 else 3637 { 3638 return FunctionDecl::Create(*ast, 3639 ast->getTranslationUnitDecl(), 3640 SourceLocation(), 3641 SourceLocation(), 3642 DeclarationName (), 3643 QualType::getFromOpaquePtr(function_clang_type), 3644 NULL, 3645 (FunctionDecl::StorageClass)storage, 3646 (FunctionDecl::StorageClass)storage, 3647 is_inline); 3648 } 3649 } 3650 return NULL; 3651} 3652 3653clang_type_t 3654ClangASTContext::CreateFunctionType (ASTContext *ast, 3655 clang_type_t result_type, 3656 clang_type_t *args, 3657 unsigned num_args, 3658 bool is_variadic, 3659 unsigned type_quals) 3660{ 3661 assert (ast != NULL); 3662 std::vector<QualType> qual_type_args; 3663 for (unsigned i=0; i<num_args; ++i) 3664 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); 3665 3666 // TODO: Detect calling convention in DWARF? 3667 FunctionProtoType::ExtProtoInfo proto_info; 3668 proto_info.Variadic = is_variadic; 3669 proto_info.ExceptionSpecType = EST_None; 3670 proto_info.TypeQuals = type_quals; 3671 proto_info.RefQualifier = RQ_None; 3672 proto_info.NumExceptions = 0; 3673 proto_info.Exceptions = NULL; 3674 3675 return ast->getFunctionType(QualType::getFromOpaquePtr(result_type), 3676 qual_type_args.empty() ? NULL : &qual_type_args.front(), 3677 qual_type_args.size(), 3678 proto_info).getAsOpaquePtr(); // NoReturn); 3679} 3680 3681ParmVarDecl * 3682ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage) 3683{ 3684 ASTContext *ast = getASTContext(); 3685 assert (ast != NULL); 3686 return ParmVarDecl::Create(*ast, 3687 ast->getTranslationUnitDecl(), 3688 SourceLocation(), 3689 SourceLocation(), 3690 name && name[0] ? &ast->Idents.get(name) : NULL, 3691 QualType::getFromOpaquePtr(param_type), 3692 NULL, 3693 (VarDecl::StorageClass)storage, 3694 (VarDecl::StorageClass)storage, 3695 0); 3696} 3697 3698void 3699ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 3700{ 3701 if (function_decl) 3702 function_decl->setParams (params, num_params); 3703} 3704 3705 3706#pragma mark Array Types 3707 3708clang_type_t 3709ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride) 3710{ 3711 if (element_type) 3712 { 3713 ASTContext *ast = getASTContext(); 3714 assert (ast != NULL); 3715 llvm::APInt ap_element_count (64, element_count); 3716 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type), 3717 ap_element_count, 3718 ArrayType::Normal, 3719 0).getAsOpaquePtr(); // ElemQuals 3720 } 3721 return NULL; 3722} 3723 3724 3725#pragma mark TagDecl 3726 3727bool 3728ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type) 3729{ 3730 if (clang_type) 3731 { 3732 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3733 const clang::Type *t = qual_type.getTypePtr(); 3734 if (t) 3735 { 3736 const TagType *tag_type = dyn_cast<TagType>(t); 3737 if (tag_type) 3738 { 3739 TagDecl *tag_decl = tag_type->getDecl(); 3740 if (tag_decl) 3741 { 3742 tag_decl->startDefinition(); 3743 return true; 3744 } 3745 } 3746 } 3747 } 3748 return false; 3749} 3750 3751bool 3752ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type) 3753{ 3754 if (clang_type) 3755 { 3756 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3757 3758 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 3759 3760 if (cxx_record_decl) 3761 { 3762 cxx_record_decl->completeDefinition(); 3763 3764 return true; 3765 } 3766 3767 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type); 3768 3769 if (objc_class_type) 3770 { 3771 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 3772 3773 class_interface_decl->setForwardDecl(false); 3774 } 3775 3776 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr()); 3777 3778 if (enum_type) 3779 { 3780 EnumDecl *enum_decl = enum_type->getDecl(); 3781 3782 if (enum_decl) 3783 { 3784 /// TODO This really needs to be fixed. 3785 3786 unsigned NumPositiveBits = 1; 3787 unsigned NumNegativeBits = 0; 3788 3789 ASTContext *ast = getASTContext(); 3790 3791 QualType promotion_qual_type; 3792 // If the enum integer type is less than an integer in bit width, 3793 // then we must promote it to an integer size. 3794 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy)) 3795 { 3796 if (enum_decl->getIntegerType()->isSignedIntegerType()) 3797 promotion_qual_type = ast->IntTy; 3798 else 3799 promotion_qual_type = ast->UnsignedIntTy; 3800 } 3801 else 3802 promotion_qual_type = enum_decl->getIntegerType(); 3803 3804 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits); 3805 return true; 3806 } 3807 } 3808 } 3809 return false; 3810} 3811 3812 3813#pragma mark Enumeration Types 3814 3815clang_type_t 3816ClangASTContext::CreateEnumerationType 3817( 3818 const char *name, 3819 DeclContext *decl_ctx, 3820 const Declaration &decl, 3821 clang_type_t integer_qual_type 3822) 3823{ 3824 // TODO: Do something intelligent with the Declaration object passed in 3825 // like maybe filling in the SourceLocation with it... 3826 ASTContext *ast = getASTContext(); 3827 assert (ast != NULL); 3828 3829 // TODO: ask about these... 3830// const bool IsScoped = false; 3831// const bool IsFixed = false; 3832 3833 EnumDecl *enum_decl = EnumDecl::Create (*ast, 3834 decl_ctx, 3835 SourceLocation(), 3836 SourceLocation(), 3837 name && name[0] ? &ast->Idents.get(name) : NULL, 3838 NULL, 3839 false, // IsScoped 3840 false, // IsScopedUsingClassTag 3841 false); // IsFixed 3842 3843 3844 if (enum_decl) 3845 { 3846 // TODO: check if we should be setting the promotion type too? 3847 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type)); 3848 3849 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info 3850 3851 return ast->getTagDeclType(enum_decl).getAsOpaquePtr(); 3852 } 3853 return NULL; 3854} 3855 3856clang_type_t 3857ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type) 3858{ 3859 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 3860 3861 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 3862 if (clang_type) 3863 { 3864 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 3865 if (enum_type) 3866 { 3867 EnumDecl *enum_decl = enum_type->getDecl(); 3868 if (enum_decl) 3869 return enum_decl->getIntegerType().getAsOpaquePtr(); 3870 } 3871 } 3872 return NULL; 3873} 3874bool 3875ClangASTContext::AddEnumerationValueToEnumerationType 3876( 3877 clang_type_t enum_clang_type, 3878 clang_type_t enumerator_clang_type, 3879 const Declaration &decl, 3880 const char *name, 3881 int64_t enum_value, 3882 uint32_t enum_value_bit_size 3883) 3884{ 3885 if (enum_clang_type && enumerator_clang_type && name) 3886 { 3887 // TODO: Do something intelligent with the Declaration object passed in 3888 // like maybe filling in the SourceLocation with it... 3889 ASTContext *ast = getASTContext(); 3890 IdentifierTable *identifier_table = getIdentifierTable(); 3891 3892 assert (ast != NULL); 3893 assert (identifier_table != NULL); 3894 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 3895 3896 const clang::Type *clang_type = enum_qual_type.getTypePtr(); 3897 if (clang_type) 3898 { 3899 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 3900 3901 if (enum_type) 3902 { 3903 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); 3904 enum_llvm_apsint = enum_value; 3905 EnumConstantDecl *enumerator_decl = 3906 EnumConstantDecl::Create (*ast, 3907 enum_type->getDecl(), 3908 SourceLocation(), 3909 name ? &identifier_table->get(name) : NULL, // Identifier 3910 QualType::getFromOpaquePtr(enumerator_clang_type), 3911 NULL, 3912 enum_llvm_apsint); 3913 3914 if (enumerator_decl) 3915 { 3916 enum_type->getDecl()->addDecl(enumerator_decl); 3917 return true; 3918 } 3919 } 3920 } 3921 } 3922 return false; 3923} 3924 3925#pragma mark Pointers & References 3926 3927clang_type_t 3928ClangASTContext::CreatePointerType (clang_type_t clang_type) 3929{ 3930 return CreatePointerType (getASTContext(), clang_type); 3931} 3932 3933clang_type_t 3934ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type) 3935{ 3936 if (ast && clang_type) 3937 { 3938 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3939 3940 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3941 switch (type_class) 3942 { 3943 case clang::Type::ObjCObject: 3944 case clang::Type::ObjCInterface: 3945 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr(); 3946 3947 default: 3948 return ast->getPointerType(qual_type).getAsOpaquePtr(); 3949 } 3950 } 3951 return NULL; 3952} 3953 3954clang_type_t 3955ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast, 3956 clang_type_t clang_type) 3957{ 3958 if (clang_type) 3959 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 3960 return NULL; 3961} 3962 3963clang_type_t 3964ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast, 3965 clang_type_t clang_type) 3966{ 3967 if (clang_type) 3968 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 3969 return NULL; 3970} 3971 3972clang_type_t 3973ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type) 3974{ 3975 if (clang_pointee_type && clang_pointee_type) 3976 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), 3977 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); 3978 return NULL; 3979} 3980 3981uint32_t 3982ClangASTContext::GetPointerBitSize () 3983{ 3984 ASTContext *ast = getASTContext(); 3985 return ast->getTypeSize(ast->VoidPtrTy); 3986} 3987 3988bool 3989ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type) 3990{ 3991 QualType pointee_qual_type; 3992 if (clang_type) 3993 { 3994 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3995 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3996 bool success = false; 3997 switch (type_class) 3998 { 3999 case clang::Type::Pointer: 4000 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType(); 4001 success = true; 4002 break; 4003 4004 case clang::Type::LValueReference: 4005 case clang::Type::RValueReference: 4006 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType(); 4007 success = true; 4008 break; 4009 4010 case clang::Type::Typedef: 4011 return ClangASTContext::IsPossibleCPlusPlusDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type); 4012 4013 default: 4014 break; 4015 } 4016 4017 if (success) 4018 { 4019 // Check to make sure what we are pointing too is a possible dynamic C++ type 4020 // We currently accept any "void *" (in case we have a class that has been 4021 // watered down to an opaque pointer) and virtual C++ classes. 4022 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass(); 4023 switch (pointee_type_class) 4024 { 4025 case clang::Type::Builtin: 4026 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind()) 4027 { 4028 case clang::BuiltinType::UnknownAny: 4029 case clang::BuiltinType::Void: 4030 if (dynamic_pointee_type) 4031 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 4032 return true; 4033 4034 case clang::BuiltinType::NullPtr: 4035 case clang::BuiltinType::Bool: 4036 case clang::BuiltinType::Char_U: 4037 case clang::BuiltinType::UChar: 4038 case clang::BuiltinType::WChar_U: 4039 case clang::BuiltinType::Char16: 4040 case clang::BuiltinType::Char32: 4041 case clang::BuiltinType::UShort: 4042 case clang::BuiltinType::UInt: 4043 case clang::BuiltinType::ULong: 4044 case clang::BuiltinType::ULongLong: 4045 case clang::BuiltinType::UInt128: 4046 case clang::BuiltinType::Char_S: 4047 case clang::BuiltinType::SChar: 4048 case clang::BuiltinType::WChar_S: 4049 case clang::BuiltinType::Short: 4050 case clang::BuiltinType::Int: 4051 case clang::BuiltinType::Long: 4052 case clang::BuiltinType::LongLong: 4053 case clang::BuiltinType::Int128: 4054 case clang::BuiltinType::Float: 4055 case clang::BuiltinType::Double: 4056 case clang::BuiltinType::LongDouble: 4057 case clang::BuiltinType::Dependent: 4058 case clang::BuiltinType::Overload: 4059 case clang::BuiltinType::ObjCId: 4060 case clang::BuiltinType::ObjCClass: 4061 case clang::BuiltinType::ObjCSel: 4062 case clang::BuiltinType::BoundMember: 4063 break; 4064 } 4065 break; 4066 case clang::Type::Record: 4067 { 4068 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl(); 4069 if (cxx_record_decl) 4070 { 4071 if (GetCompleteQualType (ast, pointee_qual_type)) 4072 { 4073 success = cxx_record_decl->isPolymorphic() || cxx_record_decl->isAbstract(); 4074 } 4075 else 4076 { 4077 // We failed to get the complete type, so we have to 4078 // treat this as a void * which we might possibly be 4079 // able to complete 4080 success = true; 4081 } 4082 if (success) 4083 { 4084 if (dynamic_pointee_type) 4085 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr(); 4086 return true; 4087 } 4088 } 4089 } 4090 break; 4091 4092 default: 4093 break; 4094 } 4095 } 4096 } 4097 if (dynamic_pointee_type) 4098 *dynamic_pointee_type = NULL; 4099 return false; 4100} 4101 4102 4103bool 4104ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type) 4105{ 4106 if (clang_type == NULL) 4107 return false; 4108 4109 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4110 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4111 switch (type_class) 4112 { 4113 case clang::Type::Builtin: 4114 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 4115 { 4116 default: 4117 break; 4118 case clang::BuiltinType::ObjCId: 4119 case clang::BuiltinType::ObjCClass: 4120 return true; 4121 } 4122 return false; 4123 case clang::Type::ObjCObjectPointer: 4124 if (target_type) 4125 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4126 return true; 4127 case clang::Type::BlockPointer: 4128 if (target_type) 4129 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4130 return true; 4131 case clang::Type::Pointer: 4132 if (target_type) 4133 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4134 return true; 4135 case clang::Type::MemberPointer: 4136 if (target_type) 4137 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4138 return true; 4139 case clang::Type::LValueReference: 4140 if (target_type) 4141 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 4142 return true; 4143 case clang::Type::RValueReference: 4144 if (target_type) 4145 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 4146 return true; 4147 case clang::Type::Typedef: 4148 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 4149 default: 4150 break; 4151 } 4152 return false; 4153} 4154 4155bool 4156ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed) 4157{ 4158 if (!clang_type) 4159 return false; 4160 4161 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4162 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); 4163 4164 if (builtin_type) 4165 { 4166 if (builtin_type->isInteger()) 4167 is_signed = builtin_type->isSignedInteger(); 4168 4169 return true; 4170 } 4171 4172 return false; 4173} 4174 4175bool 4176ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t*target_type) 4177{ 4178 if (clang_type) 4179 { 4180 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4181 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4182 switch (type_class) 4183 { 4184 case clang::Type::Builtin: 4185 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 4186 { 4187 default: 4188 break; 4189 case clang::BuiltinType::ObjCId: 4190 case clang::BuiltinType::ObjCClass: 4191 return true; 4192 } 4193 return false; 4194 case clang::Type::ObjCObjectPointer: 4195 if (target_type) 4196 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4197 return true; 4198 case clang::Type::BlockPointer: 4199 if (target_type) 4200 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4201 return true; 4202 case clang::Type::Pointer: 4203 if (target_type) 4204 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4205 return true; 4206 case clang::Type::MemberPointer: 4207 if (target_type) 4208 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 4209 return true; 4210 case clang::Type::Typedef: 4211 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type); 4212 default: 4213 break; 4214 } 4215 } 4216 return false; 4217} 4218 4219bool 4220ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex) 4221{ 4222 if (clang_type) 4223 { 4224 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4225 4226 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) 4227 { 4228 clang::BuiltinType::Kind kind = BT->getKind(); 4229 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) 4230 { 4231 count = 1; 4232 is_complex = false; 4233 return true; 4234 } 4235 } 4236 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) 4237 { 4238 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) 4239 { 4240 count = 2; 4241 is_complex = true; 4242 return true; 4243 } 4244 } 4245 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) 4246 { 4247 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) 4248 { 4249 count = VT->getNumElements(); 4250 is_complex = false; 4251 return true; 4252 } 4253 } 4254 } 4255 return false; 4256} 4257 4258 4259bool 4260ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name) 4261{ 4262 if (clang_type) 4263 { 4264 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4265 4266 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 4267 if (cxx_record_decl) 4268 { 4269 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart()); 4270 return true; 4271 } 4272 } 4273 class_name.clear(); 4274 return false; 4275} 4276 4277 4278bool 4279ClangASTContext::IsCXXClassType (clang_type_t clang_type) 4280{ 4281 if (clang_type) 4282 { 4283 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4284 if (qual_type->getAsCXXRecordDecl() != NULL) 4285 return true; 4286 } 4287 return false; 4288} 4289 4290bool 4291ClangASTContext::IsObjCClassType (clang_type_t clang_type) 4292{ 4293 if (clang_type) 4294 { 4295 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4296 if (qual_type->isObjCObjectOrInterfaceType()) 4297 return true; 4298 } 4299 return false; 4300} 4301 4302 4303bool 4304ClangASTContext::IsCharType (clang_type_t clang_type) 4305{ 4306 if (clang_type) 4307 return QualType::getFromOpaquePtr(clang_type)->isCharType(); 4308 return false; 4309} 4310 4311bool 4312ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length) 4313{ 4314 clang_type_t pointee_or_element_clang_type = NULL; 4315 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type)); 4316 4317 if (pointee_or_element_clang_type == NULL) 4318 return false; 4319 4320 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer)) 4321 { 4322 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type)); 4323 4324 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType()) 4325 { 4326 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4327 if (type_flags.Test (eTypeIsArray)) 4328 { 4329 // We know the size of the array and it could be a C string 4330 // since it is an array of characters 4331 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 4332 return true; 4333 } 4334 else 4335 { 4336 length = 0; 4337 return true; 4338 } 4339 4340 } 4341 } 4342 return false; 4343} 4344 4345bool 4346ClangASTContext::IsFunctionPointerType (clang_type_t clang_type) 4347{ 4348 if (clang_type) 4349 { 4350 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4351 4352 if (qual_type->isFunctionPointerType()) 4353 return true; 4354 4355 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4356 switch (type_class) 4357 { 4358 default: 4359 break; 4360 case clang::Type::Typedef: 4361 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()); 4362 4363 case clang::Type::LValueReference: 4364 case clang::Type::RValueReference: 4365 { 4366 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 4367 if (reference_type) 4368 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr()); 4369 } 4370 break; 4371 } 4372 } 4373 return false; 4374} 4375 4376size_t 4377ClangASTContext::GetArraySize (clang_type_t clang_type) 4378{ 4379 if (clang_type) 4380 { 4381 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr()); 4382 if (array) 4383 return array->getSize().getLimitedValue(); 4384 } 4385 return 0; 4386} 4387 4388bool 4389ClangASTContext::IsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size) 4390{ 4391 if (!clang_type) 4392 return false; 4393 4394 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4395 4396 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 4397 switch (type_class) 4398 { 4399 default: 4400 break; 4401 case clang::Type::ConstantArray: 4402 if (member_type) 4403 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 4404 if (size) 4405 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX); 4406 return true; 4407 case clang::Type::IncompleteArray: 4408 if (member_type) 4409 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 4410 if (size) 4411 *size = 0; 4412 return true; 4413 case clang::Type::VariableArray: 4414 if (member_type) 4415 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 4416 if (size) 4417 *size = 0; 4418 return true; 4419 case clang::Type::DependentSizedArray: 4420 if (member_type) 4421 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 4422 if (size) 4423 *size = 0; 4424 return true; 4425 } 4426 return false; 4427} 4428 4429 4430#pragma mark Typedefs 4431 4432clang_type_t 4433ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx) 4434{ 4435 if (clang_type) 4436 { 4437 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4438 ASTContext *ast = getASTContext(); 4439 IdentifierTable *identifier_table = getIdentifierTable(); 4440 assert (ast != NULL); 4441 assert (identifier_table != NULL); 4442 if (decl_ctx == NULL) 4443 decl_ctx = ast->getTranslationUnitDecl(); 4444 TypedefDecl *decl = TypedefDecl::Create (*ast, 4445 decl_ctx, 4446 SourceLocation(), 4447 SourceLocation(), 4448 name ? &identifier_table->get(name) : NULL, // Identifier 4449 ast->CreateTypeSourceInfo(qual_type)); 4450 4451 decl->setAccess(AS_public); // TODO respect proper access specifier 4452 4453 // Get a uniqued QualType for the typedef decl type 4454 return ast->getTypedefType (decl).getAsOpaquePtr(); 4455 } 4456 return NULL; 4457} 4458 4459 4460std::string 4461ClangASTContext::GetTypeName (clang_type_t opaque_qual_type) 4462{ 4463 std::string return_name; 4464 4465 QualType qual_type(QualType::getFromOpaquePtr(opaque_qual_type)); 4466 4467 const TypedefType *typedef_type = qual_type->getAs<TypedefType>(); 4468 if (typedef_type) 4469 { 4470 const TypedefNameDecl *typedef_decl = typedef_type->getDecl(); 4471 return_name = typedef_decl->getQualifiedNameAsString(); 4472 } 4473 else 4474 { 4475 return_name = qual_type.getAsString(); 4476 } 4477 4478 return return_name; 4479} 4480 4481// Disable this for now since I can't seem to get a nicely formatted float 4482// out of the APFloat class without just getting the float, double or quad 4483// and then using a formatted print on it which defeats the purpose. We ideally 4484// would like to get perfect string values for any kind of float semantics 4485// so we can support remote targets. The code below also requires a patch to 4486// llvm::APInt. 4487//bool 4488//ClangASTContext::ConvertFloatValueToString (ASTContext *ast, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) 4489//{ 4490// uint32_t count = 0; 4491// bool is_complex = false; 4492// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 4493// { 4494// unsigned num_bytes_per_float = byte_size / count; 4495// unsigned num_bits_per_float = num_bytes_per_float * 8; 4496// 4497// float_str.clear(); 4498// uint32_t i; 4499// for (i=0; i<count; i++) 4500// { 4501// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 4502// bool is_ieee = false; 4503// APFloat ap_float(ap_int, is_ieee); 4504// char s[1024]; 4505// unsigned int hex_digits = 0; 4506// bool upper_case = false; 4507// 4508// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 4509// { 4510// if (i > 0) 4511// float_str.append(", "); 4512// float_str.append(s); 4513// if (i == 1 && is_complex) 4514// float_str.append(1, 'i'); 4515// } 4516// } 4517// return !float_str.empty(); 4518// } 4519// return false; 4520//} 4521 4522size_t 4523ClangASTContext::ConvertStringToFloatValue (ASTContext *ast, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size) 4524{ 4525 if (clang_type) 4526 { 4527 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4528 uint32_t count = 0; 4529 bool is_complex = false; 4530 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 4531 { 4532 // TODO: handle complex and vector types 4533 if (count != 1) 4534 return false; 4535 4536 StringRef s_sref(s); 4537 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref); 4538 4539 const uint64_t bit_size = ast->getTypeSize (qual_type); 4540 const uint64_t byte_size = bit_size / 8; 4541 if (dst_size >= byte_size) 4542 { 4543 if (bit_size == sizeof(float)*8) 4544 { 4545 float float32 = ap_float.convertToFloat(); 4546 ::memcpy (dst, &float32, byte_size); 4547 return byte_size; 4548 } 4549 else if (bit_size >= 64) 4550 { 4551 llvm::APInt ap_int(ap_float.bitcastToAPInt()); 4552 ::memcpy (dst, ap_int.getRawData(), byte_size); 4553 return byte_size; 4554 } 4555 } 4556 } 4557 } 4558 return 0; 4559} 4560 4561unsigned 4562ClangASTContext::GetTypeQualifiers(clang_type_t clang_type) 4563{ 4564 assert (clang_type); 4565 4566 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 4567 4568 return qual_type.getQualifiers().getCVRQualifiers(); 4569} 4570 4571bool 4572ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type) 4573{ 4574 if (clang_type == NULL) 4575 return false; 4576 4577 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type)); 4578} 4579 4580 4581bool 4582ClangASTContext::GetCompleteType (clang_type_t clang_type) 4583{ 4584 return ClangASTContext::GetCompleteType (getASTContext(), clang_type); 4585} 4586 4587