Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eli Friedman | f05633b | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 10 | #include "lldb/Symbol/ClangASTContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | #include <string> |
| 15 | |
| 16 | // Other libraries and framework includes |
Sean Callanan | bc4f0f5 | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 17 | #define NDEBUG |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/ASTImporter.h" |
| 20 | #include "clang/AST/CXXInheritance.h" |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
| 23 | #include "clang/AST/Type.h" |
| 24 | #include "clang/Basic/Builtins.h" |
| 25 | #include "clang/Basic/FileManager.h" |
| 26 | #include "clang/Basic/SourceManager.h" |
| 27 | #include "clang/Basic/TargetInfo.h" |
| 28 | #include "clang/Basic/TargetOptions.h" |
| 29 | #include "clang/Frontend/FrontendOptions.h" |
| 30 | #include "clang/Frontend/LangStandard.h" |
Sean Callanan | bc4f0f5 | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 31 | #undef NDEBUG |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Core/dwarf.h" |
| 34 | |
Eli Friedman | f05633b | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 37 | using namespace lldb; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | using namespace lldb_private; |
| 39 | using namespace llvm; |
| 40 | using namespace clang; |
| 41 | |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 42 | static AccessSpecifier |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 43 | ConvertAccessTypeToAccessSpecifier (AccessType access) |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 44 | { |
| 45 | switch (access) |
| 46 | { |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 47 | default: break; |
| 48 | case eAccessNone: return AS_none; |
| 49 | case eAccessPublic: return AS_public; |
| 50 | case eAccessPrivate: return AS_private; |
| 51 | case eAccessProtected: return AS_protected; |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 52 | } |
| 53 | return AS_none; |
| 54 | } |
| 55 | |
| 56 | static ObjCIvarDecl::AccessControl |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 57 | ConvertAccessTypeToObjCIvarAccessControl (AccessType access) |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 58 | { |
| 59 | switch (access) |
| 60 | { |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 61 | default: break; |
| 62 | case eAccessNone: return ObjCIvarDecl::None; |
| 63 | case eAccessPublic: return ObjCIvarDecl::Public; |
| 64 | case eAccessPrivate: return ObjCIvarDecl::Private; |
| 65 | case eAccessProtected: return ObjCIvarDecl::Protected; |
| 66 | case eAccessPackage: return ObjCIvarDecl::Package; |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 67 | } |
| 68 | return ObjCIvarDecl::None; |
| 69 | } |
| 70 | |
| 71 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | static void |
| 73 | ParseLangArgs |
| 74 | ( |
| 75 | LangOptions &Opts, |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 76 | InputKind IK |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | ) |
| 78 | { |
| 79 | // FIXME: Cleanup per-file based stuff. |
| 80 | |
| 81 | // Set some properties which depend soley on the input kind; it would be nice |
| 82 | // to move these to the language standard, and have the driver resolve the |
| 83 | // input kind + language standard. |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 84 | if (IK == IK_Asm) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | Opts.AsmPreprocessor = 1; |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 86 | } else if (IK == IK_ObjC || |
| 87 | IK == IK_ObjCXX || |
| 88 | IK == IK_PreprocessedObjC || |
| 89 | IK == IK_PreprocessedObjCXX) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | Opts.ObjC1 = Opts.ObjC2 = 1; |
| 91 | } |
| 92 | |
| 93 | LangStandard::Kind LangStd = LangStandard::lang_unspecified; |
| 94 | |
| 95 | if (LangStd == LangStandard::lang_unspecified) { |
| 96 | // Based on the base language, pick one. |
| 97 | switch (IK) { |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 98 | case IK_None: |
| 99 | case IK_AST: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | assert(0 && "Invalid input kind!"); |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 101 | case IK_OpenCL: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | LangStd = LangStandard::lang_opencl; |
| 103 | break; |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 104 | case IK_Asm: |
| 105 | case IK_C: |
| 106 | case IK_PreprocessedC: |
| 107 | case IK_ObjC: |
| 108 | case IK_PreprocessedObjC: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | LangStd = LangStandard::lang_gnu99; |
| 110 | break; |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 111 | case IK_CXX: |
| 112 | case IK_PreprocessedCXX: |
| 113 | case IK_ObjCXX: |
| 114 | case IK_PreprocessedObjCXX: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | LangStd = LangStandard::lang_gnucxx98; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); |
| 121 | Opts.BCPLComment = Std.hasBCPLComments(); |
| 122 | Opts.C99 = Std.isC99(); |
| 123 | Opts.CPlusPlus = Std.isCPlusPlus(); |
| 124 | Opts.CPlusPlus0x = Std.isCPlusPlus0x(); |
| 125 | Opts.Digraphs = Std.hasDigraphs(); |
| 126 | Opts.GNUMode = Std.isGNUMode(); |
| 127 | Opts.GNUInline = !Std.isC99(); |
| 128 | Opts.HexFloats = Std.hasHexFloats(); |
| 129 | Opts.ImplicitInt = Std.hasImplicitInt(); |
| 130 | |
| 131 | // OpenCL has some additional defaults. |
| 132 | if (LangStd == LangStandard::lang_opencl) { |
| 133 | Opts.OpenCL = 1; |
| 134 | Opts.AltiVec = 1; |
| 135 | Opts.CXXOperatorNames = 1; |
| 136 | Opts.LaxVectorConversions = 1; |
| 137 | } |
| 138 | |
| 139 | // OpenCL and C++ both have bool, true, false keywords. |
| 140 | Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; |
| 141 | |
| 142 | // if (Opts.CPlusPlus) |
| 143 | // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names); |
| 144 | // |
| 145 | // if (Args.hasArg(OPT_fobjc_gc_only)) |
| 146 | // Opts.setGCMode(LangOptions::GCOnly); |
| 147 | // else if (Args.hasArg(OPT_fobjc_gc)) |
| 148 | // Opts.setGCMode(LangOptions::HybridGC); |
| 149 | // |
| 150 | // if (Args.hasArg(OPT_print_ivar_layout)) |
| 151 | // Opts.ObjCGCBitmapPrint = 1; |
| 152 | // |
| 153 | // if (Args.hasArg(OPT_faltivec)) |
| 154 | // Opts.AltiVec = 1; |
| 155 | // |
| 156 | // if (Args.hasArg(OPT_pthread)) |
| 157 | // Opts.POSIXThreads = 1; |
| 158 | // |
| 159 | // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility, |
| 160 | // "default"); |
| 161 | // if (Vis == "default") |
| 162 | Opts.setVisibilityMode(LangOptions::Default); |
| 163 | // else if (Vis == "hidden") |
| 164 | // Opts.setVisibilityMode(LangOptions::Hidden); |
| 165 | // else if (Vis == "protected") |
| 166 | // Opts.setVisibilityMode(LangOptions::Protected); |
| 167 | // else |
| 168 | // Diags.Report(diag::err_drv_invalid_value) |
| 169 | // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis; |
| 170 | |
| 171 | // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv); |
| 172 | |
| 173 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs |
| 174 | // is specified, or -std is set to a conforming mode. |
| 175 | Opts.Trigraphs = !Opts.GNUMode; |
| 176 | // if (Args.hasArg(OPT_trigraphs)) |
| 177 | // Opts.Trigraphs = 1; |
| 178 | // |
| 179 | // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers, |
| 180 | // OPT_fno_dollars_in_identifiers, |
| 181 | // !Opts.AsmPreprocessor); |
| 182 | // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings); |
| 183 | // Opts.Microsoft = Args.hasArg(OPT_fms_extensions); |
| 184 | // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings); |
| 185 | // if (Args.hasArg(OPT_fno_lax_vector_conversions)) |
| 186 | // Opts.LaxVectorConversions = 0; |
| 187 | // Opts.Exceptions = Args.hasArg(OPT_fexceptions); |
| 188 | // Opts.RTTI = !Args.hasArg(OPT_fno_rtti); |
| 189 | // Opts.Blocks = Args.hasArg(OPT_fblocks); |
| 190 | // Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char); |
| 191 | // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar); |
| 192 | // Opts.Freestanding = Args.hasArg(OPT_ffreestanding); |
| 193 | // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding; |
| 194 | // Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new); |
| 195 | // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions); |
| 196 | // Opts.AccessControl = Args.hasArg(OPT_faccess_control); |
| 197 | // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors); |
| 198 | // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno); |
| 199 | // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99, |
| 200 | // Diags); |
| 201 | // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime); |
| 202 | // Opts.ObjCConstantStringClass = getLastArgValue(Args, |
| 203 | // OPT_fconstant_string_class); |
| 204 | // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi); |
| 205 | // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior); |
| 206 | // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls); |
| 207 | // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); |
| 208 | // Opts.Static = Args.hasArg(OPT_static_define); |
| 209 | Opts.OptimizeSize = 0; |
| 210 | |
| 211 | // FIXME: Eliminate this dependency. |
| 212 | // unsigned Opt = |
| 213 | // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); |
| 214 | // Opts.Optimize = Opt != 0; |
| 215 | unsigned Opt = 0; |
| 216 | |
| 217 | // This is the __NO_INLINE__ define, which just depends on things like the |
| 218 | // optimization level and -fno-inline, not actually whether the backend has |
| 219 | // inlining enabled. |
| 220 | // |
| 221 | // FIXME: This is affected by other options (-fno-inline). |
| 222 | Opts.NoInline = !Opt; |
| 223 | |
| 224 | // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags); |
| 225 | // switch (SSP) { |
| 226 | // default: |
| 227 | // Diags.Report(diag::err_drv_invalid_value) |
| 228 | // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP; |
| 229 | // break; |
| 230 | // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break; |
| 231 | // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break; |
| 232 | // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break; |
| 233 | // } |
| 234 | } |
| 235 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | ClangASTContext::ClangASTContext(const char *target_triple) : |
| 238 | m_target_triple(), |
| 239 | m_ast_context_ap(), |
| 240 | m_language_options_ap(), |
| 241 | m_source_manager_ap(), |
| 242 | m_diagnostic_ap(), |
| 243 | m_target_options_ap(), |
| 244 | m_target_info_ap(), |
| 245 | m_identifier_table_ap(), |
| 246 | m_selector_table_ap(), |
| 247 | m_builtins_ap() |
| 248 | { |
| 249 | if (target_triple && target_triple[0]) |
| 250 | m_target_triple.assign (target_triple); |
| 251 | } |
| 252 | |
| 253 | //---------------------------------------------------------------------- |
| 254 | // Destructor |
| 255 | //---------------------------------------------------------------------- |
| 256 | ClangASTContext::~ClangASTContext() |
| 257 | { |
| 258 | m_builtins_ap.reset(); |
| 259 | m_selector_table_ap.reset(); |
| 260 | m_identifier_table_ap.reset(); |
| 261 | m_target_info_ap.reset(); |
| 262 | m_target_options_ap.reset(); |
| 263 | m_diagnostic_ap.reset(); |
| 264 | m_source_manager_ap.reset(); |
| 265 | m_language_options_ap.reset(); |
| 266 | m_ast_context_ap.reset(); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | void |
| 271 | ClangASTContext::Clear() |
| 272 | { |
| 273 | m_ast_context_ap.reset(); |
| 274 | m_language_options_ap.reset(); |
| 275 | m_source_manager_ap.reset(); |
| 276 | m_diagnostic_ap.reset(); |
| 277 | m_target_options_ap.reset(); |
| 278 | m_target_info_ap.reset(); |
| 279 | m_identifier_table_ap.reset(); |
| 280 | m_selector_table_ap.reset(); |
| 281 | m_builtins_ap.reset(); |
| 282 | } |
| 283 | |
| 284 | const char * |
| 285 | ClangASTContext::GetTargetTriple () |
| 286 | { |
| 287 | return m_target_triple.c_str(); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | ClangASTContext::SetTargetTriple (const char *target_triple) |
| 292 | { |
| 293 | Clear(); |
| 294 | m_target_triple.assign(target_triple); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | ASTContext * |
| 299 | ClangASTContext::getASTContext() |
| 300 | { |
| 301 | if (m_ast_context_ap.get() == NULL) |
| 302 | { |
| 303 | m_ast_context_ap.reset( |
| 304 | new ASTContext( |
| 305 | *getLanguageOptions(), |
| 306 | *getSourceManager(), |
| 307 | *getTargetInfo(), |
| 308 | *getIdentifierTable(), |
| 309 | *getSelectorTable(), |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 310 | *getBuiltinContext(), |
| 311 | 0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 312 | } |
| 313 | return m_ast_context_ap.get(); |
| 314 | } |
| 315 | |
| 316 | Builtin::Context * |
| 317 | ClangASTContext::getBuiltinContext() |
| 318 | { |
| 319 | if (m_builtins_ap.get() == NULL) |
| 320 | m_builtins_ap.reset (new Builtin::Context(*getTargetInfo())); |
| 321 | return m_builtins_ap.get(); |
| 322 | } |
| 323 | |
| 324 | IdentifierTable * |
| 325 | ClangASTContext::getIdentifierTable() |
| 326 | { |
| 327 | if (m_identifier_table_ap.get() == NULL) |
| 328 | m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL)); |
| 329 | return m_identifier_table_ap.get(); |
| 330 | } |
| 331 | |
| 332 | LangOptions * |
| 333 | ClangASTContext::getLanguageOptions() |
| 334 | { |
| 335 | if (m_language_options_ap.get() == NULL) |
| 336 | { |
| 337 | m_language_options_ap.reset(new LangOptions()); |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 338 | ParseLangArgs(*m_language_options_ap, IK_ObjCXX); |
| 339 | // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 340 | } |
| 341 | return m_language_options_ap.get(); |
| 342 | } |
| 343 | |
| 344 | SelectorTable * |
| 345 | ClangASTContext::getSelectorTable() |
| 346 | { |
| 347 | if (m_selector_table_ap.get() == NULL) |
| 348 | m_selector_table_ap.reset (new SelectorTable()); |
| 349 | return m_selector_table_ap.get(); |
| 350 | } |
| 351 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 352 | clang::SourceManager * |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | ClangASTContext::getSourceManager() |
| 354 | { |
| 355 | if (m_source_manager_ap.get() == NULL) |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 356 | m_source_manager_ap.reset(new clang::SourceManager(*getDiagnostic())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | return m_source_manager_ap.get(); |
| 358 | } |
| 359 | |
| 360 | Diagnostic * |
| 361 | ClangASTContext::getDiagnostic() |
| 362 | { |
| 363 | if (m_diagnostic_ap.get() == NULL) |
| 364 | m_diagnostic_ap.reset(new Diagnostic()); |
| 365 | return m_diagnostic_ap.get(); |
| 366 | } |
| 367 | |
| 368 | TargetOptions * |
| 369 | ClangASTContext::getTargetOptions() |
| 370 | { |
| 371 | if (m_target_options_ap.get() == NULL && !m_target_triple.empty()) |
| 372 | { |
| 373 | m_target_options_ap.reset (new TargetOptions()); |
| 374 | if (m_target_options_ap.get()) |
| 375 | m_target_options_ap->Triple = m_target_triple; |
| 376 | } |
| 377 | return m_target_options_ap.get(); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | TargetInfo * |
| 382 | ClangASTContext::getTargetInfo() |
| 383 | { |
| 384 | // target_triple should be something like "x86_64-apple-darwin10" |
| 385 | if (m_target_info_ap.get() == NULL && !m_target_triple.empty()) |
| 386 | m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnostic(), *getTargetOptions())); |
| 387 | return m_target_info_ap.get(); |
| 388 | } |
| 389 | |
| 390 | #pragma mark Basic Types |
| 391 | |
| 392 | static inline bool |
| 393 | QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast_context, QualType qual_type) |
| 394 | { |
| 395 | uint64_t qual_type_bit_size = ast_context->getTypeSize(qual_type); |
| 396 | if (qual_type_bit_size == bit_size) |
| 397 | return true; |
| 398 | return false; |
| 399 | } |
| 400 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 401 | clang_type_t |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 402 | ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 403 | { |
| 404 | ASTContext *ast_context = getASTContext(); |
| 405 | |
| 406 | assert (ast_context != NULL); |
| 407 | |
| 408 | return GetBuiltinTypeForEncodingAndBitSize (ast_context, encoding, bit_size); |
| 409 | } |
| 410 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 411 | clang_type_t |
| 412 | ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast_context, Encoding encoding, uint32_t bit_size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 413 | { |
| 414 | if (!ast_context) |
| 415 | return NULL; |
| 416 | |
| 417 | switch (encoding) |
| 418 | { |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 419 | case eEncodingInvalid: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 420 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy)) |
| 421 | return ast_context->VoidPtrTy.getAsOpaquePtr(); |
| 422 | break; |
| 423 | |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 424 | case eEncodingUint: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) |
| 426 | return ast_context->UnsignedCharTy.getAsOpaquePtr(); |
| 427 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) |
| 428 | return ast_context->UnsignedShortTy.getAsOpaquePtr(); |
| 429 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) |
| 430 | return ast_context->UnsignedIntTy.getAsOpaquePtr(); |
| 431 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) |
| 432 | return ast_context->UnsignedLongTy.getAsOpaquePtr(); |
| 433 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) |
| 434 | return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); |
| 435 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) |
| 436 | return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); |
| 437 | break; |
| 438 | |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 439 | case eEncodingSint: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 440 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) |
| 441 | return ast_context->CharTy.getAsOpaquePtr(); |
| 442 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) |
| 443 | return ast_context->ShortTy.getAsOpaquePtr(); |
| 444 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) |
| 445 | return ast_context->IntTy.getAsOpaquePtr(); |
| 446 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) |
| 447 | return ast_context->LongTy.getAsOpaquePtr(); |
| 448 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) |
| 449 | return ast_context->LongLongTy.getAsOpaquePtr(); |
| 450 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) |
| 451 | return ast_context->Int128Ty.getAsOpaquePtr(); |
| 452 | break; |
| 453 | |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 454 | case eEncodingIEEE754: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy)) |
| 456 | return ast_context->FloatTy.getAsOpaquePtr(); |
| 457 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy)) |
| 458 | return ast_context->DoubleTy.getAsOpaquePtr(); |
| 459 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy)) |
| 460 | return ast_context->LongDoubleTy.getAsOpaquePtr(); |
| 461 | break; |
| 462 | |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 463 | case eEncodingVector: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 464 | default: |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | return NULL; |
| 469 | } |
| 470 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 471 | clang_type_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 472 | ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size) |
| 473 | { |
| 474 | ASTContext *ast_context = getASTContext(); |
| 475 | |
| 476 | #define streq(a,b) strcmp(a,b) == 0 |
| 477 | assert (ast_context != NULL); |
| 478 | if (ast_context) |
| 479 | { |
| 480 | switch (dw_ate) |
| 481 | { |
| 482 | default: |
| 483 | break; |
| 484 | |
| 485 | case DW_ATE_address: |
| 486 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy)) |
| 487 | return ast_context->VoidPtrTy.getAsOpaquePtr(); |
| 488 | break; |
| 489 | |
| 490 | case DW_ATE_boolean: |
| 491 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->BoolTy)) |
| 492 | return ast_context->BoolTy.getAsOpaquePtr(); |
| 493 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) |
| 494 | return ast_context->UnsignedCharTy.getAsOpaquePtr(); |
| 495 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) |
| 496 | return ast_context->UnsignedShortTy.getAsOpaquePtr(); |
| 497 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) |
| 498 | return ast_context->UnsignedIntTy.getAsOpaquePtr(); |
| 499 | break; |
| 500 | |
| 501 | case DW_ATE_complex_float: |
| 502 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatComplexTy)) |
| 503 | return ast_context->FloatComplexTy.getAsOpaquePtr(); |
| 504 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleComplexTy)) |
| 505 | return ast_context->DoubleComplexTy.getAsOpaquePtr(); |
| 506 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleComplexTy)) |
| 507 | return ast_context->LongDoubleComplexTy.getAsOpaquePtr(); |
| 508 | break; |
| 509 | |
| 510 | case DW_ATE_float: |
| 511 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy)) |
| 512 | return ast_context->FloatTy.getAsOpaquePtr(); |
| 513 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy)) |
| 514 | return ast_context->DoubleTy.getAsOpaquePtr(); |
| 515 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy)) |
| 516 | return ast_context->LongDoubleTy.getAsOpaquePtr(); |
| 517 | break; |
| 518 | |
| 519 | case DW_ATE_signed: |
| 520 | if (type_name) |
| 521 | { |
| 522 | if (streq(type_name, "int") || |
| 523 | streq(type_name, "signed int")) |
| 524 | { |
| 525 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) |
| 526 | return ast_context->IntTy.getAsOpaquePtr(); |
| 527 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) |
| 528 | return ast_context->Int128Ty.getAsOpaquePtr(); |
| 529 | } |
| 530 | |
| 531 | if (streq(type_name, "long int") || |
| 532 | streq(type_name, "long long int") || |
| 533 | streq(type_name, "signed long long")) |
| 534 | { |
| 535 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) |
| 536 | return ast_context->LongTy.getAsOpaquePtr(); |
| 537 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) |
| 538 | return ast_context->LongLongTy.getAsOpaquePtr(); |
| 539 | } |
| 540 | |
| 541 | if (streq(type_name, "short") || |
| 542 | streq(type_name, "short int") || |
| 543 | streq(type_name, "signed short") || |
| 544 | streq(type_name, "short signed int")) |
| 545 | { |
| 546 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) |
| 547 | return ast_context->ShortTy.getAsOpaquePtr(); |
| 548 | } |
| 549 | |
| 550 | if (streq(type_name, "char") || |
| 551 | streq(type_name, "signed char")) |
| 552 | { |
| 553 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) |
| 554 | return ast_context->CharTy.getAsOpaquePtr(); |
| 555 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) |
| 556 | return ast_context->SignedCharTy.getAsOpaquePtr(); |
| 557 | } |
| 558 | |
| 559 | if (streq(type_name, "wchar_t")) |
| 560 | { |
| 561 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->WCharTy)) |
| 562 | return ast_context->WCharTy.getAsOpaquePtr(); |
| 563 | } |
| 564 | |
| 565 | } |
| 566 | // We weren't able to match up a type name, just search by size |
| 567 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) |
| 568 | return ast_context->CharTy.getAsOpaquePtr(); |
| 569 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) |
| 570 | return ast_context->ShortTy.getAsOpaquePtr(); |
| 571 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) |
| 572 | return ast_context->IntTy.getAsOpaquePtr(); |
| 573 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) |
| 574 | return ast_context->LongTy.getAsOpaquePtr(); |
| 575 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) |
| 576 | return ast_context->LongLongTy.getAsOpaquePtr(); |
| 577 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) |
| 578 | return ast_context->Int128Ty.getAsOpaquePtr(); |
| 579 | break; |
| 580 | |
| 581 | case DW_ATE_signed_char: |
| 582 | if (type_name) |
| 583 | { |
| 584 | if (streq(type_name, "signed char")) |
| 585 | { |
| 586 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) |
| 587 | return ast_context->SignedCharTy.getAsOpaquePtr(); |
| 588 | } |
| 589 | } |
| 590 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) |
| 591 | return ast_context->CharTy.getAsOpaquePtr(); |
| 592 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) |
| 593 | return ast_context->SignedCharTy.getAsOpaquePtr(); |
| 594 | break; |
| 595 | |
| 596 | case DW_ATE_unsigned: |
| 597 | if (type_name) |
| 598 | { |
| 599 | if (streq(type_name, "unsigned int")) |
| 600 | { |
| 601 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) |
| 602 | return ast_context->UnsignedIntTy.getAsOpaquePtr(); |
| 603 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) |
| 604 | return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); |
| 605 | } |
| 606 | |
| 607 | if (streq(type_name, "unsigned int") || |
| 608 | streq(type_name, "long unsigned int") || |
| 609 | streq(type_name, "unsigned long long")) |
| 610 | { |
| 611 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) |
| 612 | return ast_context->UnsignedLongTy.getAsOpaquePtr(); |
| 613 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) |
| 614 | return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); |
| 615 | } |
| 616 | |
| 617 | if (streq(type_name, "unsigned short") || |
| 618 | streq(type_name, "short unsigned int")) |
| 619 | { |
| 620 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) |
| 621 | return ast_context->UnsignedShortTy.getAsOpaquePtr(); |
| 622 | } |
| 623 | if (streq(type_name, "unsigned char")) |
| 624 | { |
| 625 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) |
| 626 | return ast_context->UnsignedCharTy.getAsOpaquePtr(); |
| 627 | } |
| 628 | |
| 629 | } |
| 630 | // We weren't able to match up a type name, just search by size |
| 631 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) |
| 632 | return ast_context->UnsignedCharTy.getAsOpaquePtr(); |
| 633 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) |
| 634 | return ast_context->UnsignedShortTy.getAsOpaquePtr(); |
| 635 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) |
| 636 | return ast_context->UnsignedIntTy.getAsOpaquePtr(); |
| 637 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) |
| 638 | return ast_context->UnsignedLongTy.getAsOpaquePtr(); |
| 639 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) |
| 640 | return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); |
| 641 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) |
| 642 | return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); |
| 643 | break; |
| 644 | |
| 645 | case DW_ATE_unsigned_char: |
| 646 | if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) |
| 647 | return ast_context->UnsignedCharTy.getAsOpaquePtr(); |
| 648 | break; |
| 649 | |
| 650 | case DW_ATE_imaginary_float: |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | // This assert should fire for anything that we don't catch above so we know |
| 655 | // to fix any issues we run into. |
| 656 | assert (!"error: ClangASTContext::GetClangTypeForDWARFEncodingAndSize() contains an unhandled encoding. Fix this ASAP!"); |
| 657 | return NULL; |
| 658 | } |
| 659 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 660 | clang_type_t |
| 661 | ClangASTContext::GetBuiltInType_void(ASTContext *ast_context) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 662 | { |
Sean Callanan | a751f7b | 2010-09-17 02:24:29 +0000 | [diff] [blame] | 663 | return ast_context->VoidTy.getAsOpaquePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 666 | clang_type_t |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 667 | ClangASTContext::GetBuiltInType_objc_id() |
| 668 | { |
| 669 | return getASTContext()->getObjCIdType().getAsOpaquePtr(); |
| 670 | } |
| 671 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 672 | clang_type_t |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 673 | ClangASTContext::GetBuiltInType_objc_Class() |
| 674 | { |
| 675 | return getASTContext()->getObjCClassType().getAsOpaquePtr(); |
| 676 | } |
| 677 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 678 | clang_type_t |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 679 | ClangASTContext::GetBuiltInType_objc_selector() |
| 680 | { |
| 681 | return getASTContext()->getObjCSelType().getAsOpaquePtr(); |
| 682 | } |
| 683 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 684 | clang_type_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | ClangASTContext::GetCStringType (bool is_const) |
| 686 | { |
| 687 | QualType char_type(getASTContext()->CharTy); |
| 688 | |
| 689 | if (is_const) |
| 690 | char_type.addConst(); |
| 691 | |
| 692 | return getASTContext()->getPointerType(char_type).getAsOpaquePtr(); |
| 693 | } |
| 694 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 695 | clang_type_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 696 | ClangASTContext::GetVoidPtrType (bool is_const) |
| 697 | { |
| 698 | return GetVoidPtrType(getASTContext(), is_const); |
| 699 | } |
| 700 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 701 | clang_type_t |
| 702 | ClangASTContext::GetVoidPtrType (ASTContext *ast_context, bool is_const) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 703 | { |
| 704 | QualType void_ptr_type(ast_context->VoidPtrTy); |
| 705 | |
| 706 | if (is_const) |
| 707 | void_ptr_type.addConst(); |
| 708 | |
| 709 | return void_ptr_type.getAsOpaquePtr(); |
| 710 | } |
| 711 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 712 | clang_type_t |
| 713 | ClangASTContext::CopyType (ASTContext *dest_context, |
| 714 | ASTContext *source_context, |
| 715 | clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 716 | { |
| 717 | Diagnostic diagnostics; |
| 718 | FileManager file_manager; |
| 719 | ASTImporter importer(diagnostics, |
| 720 | *dest_context, file_manager, |
| 721 | *source_context, file_manager); |
| 722 | QualType ret = importer.Import(QualType::getFromOpaquePtr(clang_type)); |
| 723 | return ret.getAsOpaquePtr(); |
| 724 | } |
| 725 | |
Sean Callanan | 8d82506 | 2010-07-16 00:00:27 +0000 | [diff] [blame] | 726 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 727 | ClangASTContext::AreTypesSame(ASTContext *ast_context, |
| 728 | clang_type_t type1, |
| 729 | clang_type_t type2) |
Sean Callanan | 5510ddd | 2010-07-15 22:30:52 +0000 | [diff] [blame] | 730 | { |
| 731 | return ast_context->hasSameType(QualType::getFromOpaquePtr(type1), |
| 732 | QualType::getFromOpaquePtr(type2)); |
| 733 | } |
| 734 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 735 | #pragma mark CVR modifiers |
| 736 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 737 | clang_type_t |
| 738 | ClangASTContext::AddConstModifier (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 739 | { |
| 740 | if (clang_type) |
| 741 | { |
| 742 | QualType result(QualType::getFromOpaquePtr(clang_type)); |
| 743 | result.addConst(); |
| 744 | return result.getAsOpaquePtr(); |
| 745 | } |
| 746 | return NULL; |
| 747 | } |
| 748 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 749 | clang_type_t |
| 750 | ClangASTContext::AddRestrictModifier (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | { |
| 752 | if (clang_type) |
| 753 | { |
| 754 | QualType result(QualType::getFromOpaquePtr(clang_type)); |
| 755 | result.getQualifiers().setRestrict (true); |
| 756 | return result.getAsOpaquePtr(); |
| 757 | } |
| 758 | return NULL; |
| 759 | } |
| 760 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 761 | clang_type_t |
| 762 | ClangASTContext::AddVolatileModifier (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 763 | { |
| 764 | if (clang_type) |
| 765 | { |
| 766 | QualType result(QualType::getFromOpaquePtr(clang_type)); |
| 767 | result.getQualifiers().setVolatile (true); |
| 768 | return result.getAsOpaquePtr(); |
| 769 | } |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | #pragma mark Structure, Unions, Classes |
| 774 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 775 | clang_type_t |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 776 | ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx, LanguageType language) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 777 | { |
| 778 | ASTContext *ast_context = getASTContext(); |
| 779 | assert (ast_context != NULL); |
| 780 | |
| 781 | if (decl_ctx == NULL) |
| 782 | decl_ctx = ast_context->getTranslationUnitDecl(); |
| 783 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 784 | |
Greg Clayton | 585660c | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 785 | if (language == eLanguageTypeObjC) |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 786 | { |
| 787 | bool isForwardDecl = false; |
| 788 | bool isInternal = false; |
| 789 | return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal); |
| 790 | } |
| 791 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 792 | // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and |
| 793 | // we will need to update this code. I was told to currently always use |
| 794 | // the CXXRecordDecl class since we often don't know from debug information |
| 795 | // if something is struct or a class, so we default to always use the more |
| 796 | // complete definition just in case. |
| 797 | CXXRecordDecl *decl = CXXRecordDecl::Create(*ast_context, |
| 798 | (TagDecl::TagKind)kind, |
| 799 | decl_ctx, |
| 800 | SourceLocation(), |
| 801 | name && name[0] ? &ast_context->Idents.get(name) : NULL); |
| 802 | |
| 803 | return ast_context->getTagDeclType(decl).getAsOpaquePtr(); |
| 804 | } |
| 805 | |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 806 | CXXMethodDecl * |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 807 | ClangASTContext::AddMethodToCXXRecordType |
| 808 | ( |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 809 | ASTContext *ast_context, |
| 810 | clang_type_t record_opaque_type, |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 811 | const char *name, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 812 | clang_type_t method_opaque_type, |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 813 | lldb::AccessType access, |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 814 | bool is_virtual, |
| 815 | bool is_static, |
| 816 | bool is_inline |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 817 | ) |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 818 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 819 | if (!record_opaque_type || !method_opaque_type || !name) |
Johnny Chen | 974fddb | 2010-09-28 16:10:54 +0000 | [diff] [blame] | 820 | return NULL; |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 821 | |
| 822 | assert(ast_context); |
| 823 | |
| 824 | IdentifierTable *identifier_table = &ast_context->Idents; |
| 825 | |
| 826 | assert(identifier_table); |
| 827 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 828 | QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type)); |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 829 | |
| 830 | clang::Type *clang_type(record_qual_type.getTypePtr()); |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 831 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 832 | if (clang_type == NULL) |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 833 | return NULL; |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 834 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 835 | RecordType *record_clang_type(dyn_cast<RecordType>(clang_type)); |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 836 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 837 | if (record_clang_type == NULL) |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 838 | return NULL; |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 839 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 840 | RecordDecl *record_decl = record_clang_type->getDecl(); |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 841 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 842 | if (record_decl == NULL) |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 843 | return NULL; |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 844 | |
| 845 | CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 846 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 847 | if (cxx_record_decl == NULL) |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 848 | return NULL; |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 849 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 850 | QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 851 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 852 | CXXMethodDecl *cxx_method_decl = CXXMethodDecl::Create (*ast_context, |
| 853 | cxx_record_decl, |
| 854 | DeclarationNameInfo (DeclarationName (&identifier_table->get(name)), SourceLocation()), |
| 855 | method_qual_type, |
| 856 | NULL, // TypeSourceInfo * |
| 857 | is_static, |
| 858 | SC_None, |
| 859 | is_inline); |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 860 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 861 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 862 | AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access); |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 863 | |
| 864 | cxx_method_decl->setAccess (access_specifier); |
| 865 | cxx_method_decl->setVirtualAsWritten (is_virtual); |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 866 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 867 | // Populate the method decl with parameter decls |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 868 | clang::Type *method_type(method_qual_type.getTypePtr()); |
| 869 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 870 | if (method_type == NULL) |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 871 | return NULL; |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 872 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 873 | FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type)); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 874 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 875 | if (!method_function_prototype) |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 876 | return NULL; |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 877 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 878 | unsigned int num_params = method_function_prototype->getNumArgs(); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 879 | |
| 880 | ParmVarDecl *params[num_params]; |
| 881 | |
| 882 | for (int param_index = 0; |
| 883 | param_index < num_params; |
| 884 | ++param_index) |
| 885 | { |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 886 | params[param_index] = ParmVarDecl::Create (*ast_context, |
| 887 | cxx_method_decl, |
| 888 | SourceLocation(), |
| 889 | NULL, // anonymous |
| 890 | method_function_prototype->getArgType(param_index), |
| 891 | NULL, |
| 892 | SC_None, |
| 893 | SC_None, |
| 894 | NULL); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 897 | cxx_method_decl->setParams (params, num_params); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 898 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 899 | cxx_record_decl->addDecl (cxx_method_decl); |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 900 | |
Greg Clayton | 412440a | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 901 | return cxx_method_decl; |
Sean Callanan | 7952300 | 2010-09-17 02:58:26 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | bool |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 905 | ClangASTContext::AddFieldToRecordType |
| 906 | ( |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 907 | ASTContext *ast_context, |
| 908 | clang_type_t record_clang_type, |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 909 | const char *name, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 910 | clang_type_t field_type, |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 911 | AccessType access, |
| 912 | uint32_t bitfield_bit_size |
| 913 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 914 | { |
| 915 | if (record_clang_type == NULL || field_type == NULL) |
| 916 | return false; |
| 917 | |
Sean Callanan | 60a0ced | 2010-09-16 20:01:08 +0000 | [diff] [blame] | 918 | IdentifierTable *identifier_table = &ast_context->Idents; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 919 | |
| 920 | assert (ast_context != NULL); |
| 921 | assert (identifier_table != NULL); |
| 922 | |
| 923 | QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); |
| 924 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 925 | clang::Type *clang_type = record_qual_type.getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 926 | if (clang_type) |
| 927 | { |
| 928 | const RecordType *record_type = dyn_cast<RecordType>(clang_type); |
| 929 | |
| 930 | if (record_type) |
| 931 | { |
| 932 | RecordDecl *record_decl = record_type->getDecl(); |
| 933 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 934 | clang::Expr *bit_width = NULL; |
| 935 | if (bitfield_bit_size != 0) |
| 936 | { |
| 937 | APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 938 | bit_width = new (*ast_context)IntegerLiteral (*ast_context, bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 939 | } |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 940 | FieldDecl *field = FieldDecl::Create (*ast_context, |
| 941 | record_decl, |
| 942 | SourceLocation(), |
| 943 | name ? &identifier_table->get(name) : NULL, // Identifier |
| 944 | QualType::getFromOpaquePtr(field_type), // Field type |
| 945 | NULL, // DeclaratorInfo * |
| 946 | bit_width, // BitWidth |
| 947 | false); // Mutable |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 948 | |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 949 | field->setAccess (ConvertAccessTypeToAccessSpecifier (access)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 950 | |
| 951 | if (field) |
| 952 | { |
| 953 | record_decl->addDecl(field); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 956 | else |
| 957 | { |
| 958 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type); |
| 959 | if (objc_class_type) |
| 960 | { |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 961 | bool is_synthesized = false; |
Sean Callanan | 60a0ced | 2010-09-16 20:01:08 +0000 | [diff] [blame] | 962 | ClangASTContext::AddObjCClassIVar (ast_context, |
| 963 | record_clang_type, |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 964 | name, |
| 965 | field_type, |
| 966 | access, |
| 967 | bitfield_bit_size, |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 968 | is_synthesized); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 971 | } |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | bool |
| 976 | ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) |
| 977 | { |
| 978 | return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); |
| 979 | } |
| 980 | |
| 981 | bool |
| 982 | ClangASTContext::FieldIsBitfield |
| 983 | ( |
| 984 | ASTContext *ast_context, |
| 985 | FieldDecl* field, |
| 986 | uint32_t& bitfield_bit_size |
| 987 | ) |
| 988 | { |
| 989 | if (ast_context == NULL || field == NULL) |
| 990 | return false; |
| 991 | |
| 992 | if (field->isBitField()) |
| 993 | { |
| 994 | Expr* bit_width_expr = field->getBitWidth(); |
| 995 | if (bit_width_expr) |
| 996 | { |
| 997 | llvm::APSInt bit_width_apsint; |
| 998 | if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast_context)) |
| 999 | { |
| 1000 | bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); |
| 1001 | return true; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | bool |
| 1009 | ClangASTContext::RecordHasFields (const RecordDecl *record_decl) |
| 1010 | { |
| 1011 | if (record_decl == NULL) |
| 1012 | return false; |
| 1013 | |
| 1014 | if (!record_decl->field_empty()) |
| 1015 | return true; |
| 1016 | |
| 1017 | // No fields, lets check this is a CXX record and check the base classes |
| 1018 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1019 | if (cxx_record_decl) |
| 1020 | { |
| 1021 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1022 | for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 1023 | base_class != base_class_end; |
| 1024 | ++base_class) |
| 1025 | { |
| 1026 | const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1027 | if (RecordHasFields(base_class_decl)) |
| 1028 | return true; |
| 1029 | } |
| 1030 | } |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | void |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1035 | ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_qual_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1036 | { |
| 1037 | if (clang_qual_type) |
| 1038 | { |
| 1039 | QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1040 | clang::Type *clang_type = qual_type.getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1041 | if (clang_type) |
| 1042 | { |
| 1043 | RecordType *record_type = dyn_cast<RecordType>(clang_type); |
| 1044 | if (record_type) |
| 1045 | { |
| 1046 | RecordDecl *record_decl = record_type->getDecl(); |
| 1047 | if (record_decl) |
| 1048 | { |
| 1049 | uint32_t field_idx; |
| 1050 | RecordDecl::field_iterator field, field_end; |
| 1051 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; |
| 1052 | field != field_end; |
| 1053 | ++field, ++field_idx) |
| 1054 | { |
| 1055 | // If no accessibility was assigned, assign the correct one |
| 1056 | if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) |
| 1057 | field->setAccess ((AccessSpecifier)default_accessibility); |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | #pragma mark C++ Base Classes |
| 1066 | |
| 1067 | CXXBaseSpecifier * |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1068 | ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1069 | { |
| 1070 | if (base_class_type) |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 1071 | return new CXXBaseSpecifier (SourceRange(), |
| 1072 | is_virtual, |
| 1073 | base_of_class, |
| 1074 | ConvertAccessTypeToAccessSpecifier (access), |
| 1075 | getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | return NULL; |
| 1077 | } |
| 1078 | |
Greg Clayton | e9d0df4 | 2010-07-02 01:29:13 +0000 | [diff] [blame] | 1079 | void |
| 1080 | ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes) |
| 1081 | { |
| 1082 | for (unsigned i=0; i<num_base_classes; ++i) |
| 1083 | { |
| 1084 | delete base_classes[i]; |
| 1085 | base_classes[i] = NULL; |
| 1086 | } |
| 1087 | } |
| 1088 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1089 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1090 | ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1091 | { |
| 1092 | if (class_clang_type) |
| 1093 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1094 | clang::Type *clang_type = QualType::getFromOpaquePtr(class_clang_type).getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1095 | if (clang_type) |
| 1096 | { |
| 1097 | RecordType *record_type = dyn_cast<RecordType>(clang_type); |
| 1098 | if (record_type) |
| 1099 | { |
| 1100 | CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_type->getDecl()); |
| 1101 | if (cxx_record_decl) |
| 1102 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1103 | cxx_record_decl->setBases(base_classes, num_base_classes); |
| 1104 | return true; |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | return false; |
| 1110 | } |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1111 | #pragma mark Objective C Classes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1112 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1113 | clang_type_t |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1114 | ClangASTContext::CreateObjCClass |
| 1115 | ( |
| 1116 | const char *name, |
| 1117 | DeclContext *decl_ctx, |
| 1118 | bool isForwardDecl, |
| 1119 | bool isInternal |
| 1120 | ) |
| 1121 | { |
| 1122 | ASTContext *ast_context = getASTContext(); |
| 1123 | assert (ast_context != NULL); |
| 1124 | assert (name && name[0]); |
| 1125 | if (decl_ctx == NULL) |
| 1126 | decl_ctx = ast_context->getTranslationUnitDecl(); |
| 1127 | |
| 1128 | // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and |
| 1129 | // we will need to update this code. I was told to currently always use |
| 1130 | // the CXXRecordDecl class since we often don't know from debug information |
| 1131 | // if something is struct or a class, so we default to always use the more |
| 1132 | // complete definition just in case. |
| 1133 | ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast_context, |
| 1134 | decl_ctx, |
| 1135 | SourceLocation(), |
| 1136 | &ast_context->Idents.get(name), |
| 1137 | SourceLocation(), |
| 1138 | isForwardDecl, |
| 1139 | isInternal); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1140 | |
| 1141 | return ast_context->getObjCInterfaceType(decl).getAsOpaquePtr(); |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1145 | ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type) |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1146 | { |
| 1147 | if (class_opaque_type && super_opaque_type) |
| 1148 | { |
| 1149 | QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); |
| 1150 | QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type)); |
| 1151 | clang::Type *class_type = class_qual_type.getTypePtr(); |
| 1152 | clang::Type *super_type = super_qual_type.getTypePtr(); |
| 1153 | if (class_type && super_type) |
| 1154 | { |
| 1155 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); |
| 1156 | ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type); |
| 1157 | if (objc_class_type && objc_super_type) |
| 1158 | { |
| 1159 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 1160 | ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface(); |
| 1161 | if (class_interface_decl && super_interface_decl) |
| 1162 | { |
| 1163 | class_interface_decl->setSuperClass(super_interface_decl); |
| 1164 | return true; |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | return false; |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | bool |
| 1174 | ClangASTContext::AddObjCClassIVar |
| 1175 | ( |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1176 | ASTContext *ast_context, |
| 1177 | clang_type_t class_opaque_type, |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1178 | const char *name, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1179 | clang_type_t ivar_opaque_type, |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1180 | AccessType access, |
| 1181 | uint32_t bitfield_bit_size, |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1182 | bool is_synthesized |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1183 | ) |
| 1184 | { |
| 1185 | if (class_opaque_type == NULL || ivar_opaque_type == NULL) |
| 1186 | return false; |
| 1187 | |
Sean Callanan | 60a0ced | 2010-09-16 20:01:08 +0000 | [diff] [blame] | 1188 | IdentifierTable *identifier_table = &ast_context->Idents; |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1189 | |
| 1190 | assert (ast_context != NULL); |
| 1191 | assert (identifier_table != NULL); |
| 1192 | |
| 1193 | QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); |
| 1194 | |
| 1195 | clang::Type *class_type = class_qual_type.getTypePtr(); |
| 1196 | if (class_type) |
| 1197 | { |
| 1198 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); |
| 1199 | |
| 1200 | if (objc_class_type) |
| 1201 | { |
| 1202 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 1203 | |
| 1204 | if (class_interface_decl) |
| 1205 | { |
| 1206 | clang::Expr *bit_width = NULL; |
| 1207 | if (bitfield_bit_size != 0) |
| 1208 | { |
| 1209 | APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 1210 | bit_width = new (*ast_context)IntegerLiteral (*ast_context, bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1213 | ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast_context, |
| 1214 | class_interface_decl, |
| 1215 | SourceLocation(), |
| 1216 | &identifier_table->get(name), // Identifier |
| 1217 | QualType::getFromOpaquePtr(ivar_opaque_type), // Field type |
| 1218 | NULL, // TypeSourceInfo * |
| 1219 | ConvertAccessTypeToObjCIvarAccessControl (access), |
| 1220 | bit_width, |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1221 | is_synthesized); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (field) |
| 1224 | { |
| 1225 | class_interface_decl->addDecl(field); |
| 1226 | return true; |
| 1227 | } |
Greg Clayton | 84f8075 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | return false; |
| 1232 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1233 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1234 | |
| 1235 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1236 | ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass) |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1237 | { |
| 1238 | QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); |
| 1239 | |
| 1240 | clang::Type *class_type = class_qual_type.getTypePtr(); |
| 1241 | if (class_type) |
| 1242 | { |
| 1243 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); |
| 1244 | |
| 1245 | if (objc_class_type) |
| 1246 | return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass); |
| 1247 | } |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | bool |
| 1252 | ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass) |
| 1253 | { |
| 1254 | while (class_interface_decl) |
| 1255 | { |
| 1256 | if (class_interface_decl->ivar_size() > 0) |
| 1257 | return true; |
| 1258 | |
| 1259 | if (check_superclass) |
| 1260 | class_interface_decl = class_interface_decl->getSuperClass(); |
| 1261 | else |
| 1262 | break; |
| 1263 | } |
| 1264 | return false; |
| 1265 | } |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1266 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1267 | ObjCMethodDecl * |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1268 | ClangASTContext::AddMethodToObjCObjectType |
| 1269 | ( |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1270 | ASTContext *ast_context, |
| 1271 | clang_type_t class_opaque_type, |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1272 | const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1273 | clang_type_t method_opaque_type, |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1274 | lldb::AccessType access |
| 1275 | ) |
| 1276 | { |
| 1277 | if (class_opaque_type == NULL || method_opaque_type == NULL) |
| 1278 | return NULL; |
| 1279 | |
| 1280 | IdentifierTable *identifier_table = &ast_context->Idents; |
| 1281 | |
| 1282 | assert (ast_context != NULL); |
| 1283 | assert (identifier_table != NULL); |
| 1284 | |
| 1285 | QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); |
| 1286 | |
| 1287 | clang::Type *class_type = class_qual_type.getTypePtr(); |
| 1288 | if (class_type == NULL) |
| 1289 | return NULL; |
| 1290 | |
| 1291 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); |
| 1292 | |
| 1293 | if (objc_class_type == NULL) |
| 1294 | return NULL; |
| 1295 | |
| 1296 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 1297 | |
| 1298 | if (class_interface_decl == NULL) |
| 1299 | return NULL; |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1300 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 1301 | const char *selector_start = ::strchr (name, ' '); |
| 1302 | if (selector_start == NULL) |
| 1303 | return NULL; |
| 1304 | |
| 1305 | selector_start++; |
| 1306 | if (!(::isalpha (selector_start[0]) || selector_start[0] == '_')) |
| 1307 | return NULL; |
| 1308 | llvm::SmallVector<IdentifierInfo *, 12> selector_idents; |
| 1309 | |
| 1310 | size_t len; |
| 1311 | const char *start; |
| 1312 | for (start = selector_start, len = ::strcspn(start, ":]"); |
| 1313 | start && *start != '\0' && *start != ']'; |
| 1314 | start += len + 1) |
| 1315 | { |
| 1316 | selector_idents.push_back (&identifier_table->get (StringRef (start, len))); |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | if (selector_idents.size() == 0) |
| 1321 | return 0; |
| 1322 | |
| 1323 | clang::Selector method_selector = ast_context->Selectors.getSelector (selector_idents.size(), |
| 1324 | selector_idents.data()); |
| 1325 | |
| 1326 | QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); |
| 1327 | |
| 1328 | // Populate the method decl with parameter decls |
| 1329 | clang::Type *method_type(method_qual_type.getTypePtr()); |
| 1330 | |
| 1331 | if (method_type == NULL) |
| 1332 | return NULL; |
| 1333 | |
| 1334 | FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type)); |
| 1335 | |
| 1336 | if (!method_function_prototype) |
| 1337 | return NULL; |
| 1338 | |
| 1339 | |
| 1340 | bool is_variadic = false; |
| 1341 | bool is_synthesized = false; |
| 1342 | bool is_defined = false; |
| 1343 | ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None; |
| 1344 | |
| 1345 | const unsigned num_args = method_function_prototype->getNumArgs(); |
| 1346 | |
| 1347 | ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast_context, |
| 1348 | SourceLocation(), // beginLoc, |
| 1349 | SourceLocation(), // endLoc, |
| 1350 | method_selector, |
| 1351 | method_function_prototype->getResultType(), |
| 1352 | NULL, // TypeSourceInfo *ResultTInfo, |
| 1353 | GetDeclContextForType (class_opaque_type), |
| 1354 | name[0] == '-', |
| 1355 | is_variadic, |
| 1356 | is_synthesized, |
| 1357 | is_defined, |
| 1358 | imp_control, |
| 1359 | num_args); |
| 1360 | |
| 1361 | |
| 1362 | if (objc_method_decl == NULL) |
| 1363 | return NULL; |
| 1364 | |
| 1365 | if (num_args > 0) |
| 1366 | { |
| 1367 | llvm::SmallVector<ParmVarDecl *, 12> params; |
| 1368 | |
| 1369 | for (int param_index = 0; param_index < num_args; ++param_index) |
| 1370 | { |
| 1371 | params.push_back (ParmVarDecl::Create (*ast_context, |
| 1372 | objc_method_decl, |
| 1373 | SourceLocation(), |
| 1374 | NULL, // anonymous |
| 1375 | method_function_prototype->getArgType(param_index), |
| 1376 | NULL, |
| 1377 | SC_Auto, |
| 1378 | SC_Auto, |
| 1379 | NULL)); |
| 1380 | } |
| 1381 | |
| 1382 | objc_method_decl->setMethodParams(*ast_context, params.data(), params.size(), num_args); |
| 1383 | } |
| 1384 | |
| 1385 | class_interface_decl->addDecl (objc_method_decl); |
| 1386 | |
| 1387 | |
| 1388 | return objc_method_decl; |
| 1389 | } |
| 1390 | |
| 1391 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1393 | #pragma mark Aggregate Types |
| 1394 | |
| 1395 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1396 | ClangASTContext::IsAggregateType (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1397 | { |
| 1398 | if (clang_type == NULL) |
| 1399 | return false; |
| 1400 | |
| 1401 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 1402 | |
| 1403 | if (qual_type->isAggregateType ()) |
| 1404 | return true; |
| 1405 | |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 1406 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 1407 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1408 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1409 | case clang::Type::IncompleteArray: |
| 1410 | case clang::Type::VariableArray: |
| 1411 | case clang::Type::ConstantArray: |
| 1412 | case clang::Type::ExtVector: |
| 1413 | case clang::Type::Vector: |
| 1414 | case clang::Type::Record: |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1415 | case clang::Type::ObjCObject: |
| 1416 | case clang::Type::ObjCInterface: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1417 | return true; |
| 1418 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1419 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1420 | return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); |
| 1421 | |
| 1422 | default: |
| 1423 | break; |
| 1424 | } |
| 1425 | // The clang type does have a value |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | uint32_t |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1430 | ClangASTContext::GetNumChildren (clang_type_t clang_qual_type, bool omit_empty_base_classes) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1431 | { |
| 1432 | if (clang_qual_type == NULL) |
| 1433 | return 0; |
| 1434 | |
| 1435 | uint32_t num_children = 0; |
| 1436 | QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1437 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 1438 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1439 | { |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1440 | case clang::Type::Builtin: |
| 1441 | switch (cast<clang::BuiltinType>(qual_type)->getKind()) |
| 1442 | { |
| 1443 | case clang::BuiltinType::ObjCId: // Child is Class |
| 1444 | case clang::BuiltinType::ObjCClass: // child is Class |
| 1445 | case clang::BuiltinType::ObjCSel: // child is const char * |
| 1446 | num_children = 1; |
| 1447 | |
| 1448 | default: |
| 1449 | break; |
| 1450 | } |
| 1451 | break; |
| 1452 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1453 | case clang::Type::Record: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1454 | { |
| 1455 | const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); |
| 1456 | const RecordDecl *record_decl = record_type->getDecl(); |
| 1457 | assert(record_decl); |
| 1458 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1459 | if (cxx_record_decl) |
| 1460 | { |
| 1461 | if (omit_empty_base_classes) |
| 1462 | { |
| 1463 | // Check each base classes to see if it or any of its |
| 1464 | // base classes contain any fields. This can help |
| 1465 | // limit the noise in variable views by not having to |
| 1466 | // show base classes that contain no members. |
| 1467 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1468 | for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 1469 | base_class != base_class_end; |
| 1470 | ++base_class) |
| 1471 | { |
| 1472 | const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1473 | |
| 1474 | // Skip empty base classes |
| 1475 | if (RecordHasFields(base_class_decl) == false) |
| 1476 | continue; |
| 1477 | |
| 1478 | num_children++; |
| 1479 | } |
| 1480 | } |
| 1481 | else |
| 1482 | { |
| 1483 | // Include all base classes |
| 1484 | num_children += cxx_record_decl->getNumBases(); |
| 1485 | } |
| 1486 | |
| 1487 | } |
| 1488 | RecordDecl::field_iterator field, field_end; |
| 1489 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) |
| 1490 | ++num_children; |
| 1491 | } |
| 1492 | break; |
| 1493 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1494 | case clang::Type::ObjCObject: |
| 1495 | case clang::Type::ObjCInterface: |
| 1496 | { |
| 1497 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); |
| 1498 | assert (objc_class_type); |
| 1499 | if (objc_class_type) |
| 1500 | { |
| 1501 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 1502 | |
| 1503 | if (class_interface_decl) |
| 1504 | { |
| 1505 | |
| 1506 | ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); |
| 1507 | if (superclass_interface_decl) |
| 1508 | { |
| 1509 | if (omit_empty_base_classes) |
| 1510 | { |
| 1511 | if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true)) |
| 1512 | ++num_children; |
| 1513 | } |
| 1514 | else |
| 1515 | ++num_children; |
| 1516 | } |
| 1517 | |
| 1518 | num_children += class_interface_decl->ivar_size(); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | break; |
| 1523 | |
| 1524 | case clang::Type::ObjCObjectPointer: |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1525 | { |
| 1526 | ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr()); |
| 1527 | QualType pointee_type = pointer_type->getPointeeType(); |
| 1528 | uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), |
| 1529 | omit_empty_base_classes); |
| 1530 | // If this type points to a simple type, then it has 1 child |
| 1531 | if (num_pointee_children == 0) |
| 1532 | num_children = 1; |
| 1533 | else |
| 1534 | num_children = num_pointee_children; |
| 1535 | } |
| 1536 | break; |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1537 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1538 | case clang::Type::ConstantArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1539 | num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); |
| 1540 | break; |
| 1541 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1542 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1543 | { |
| 1544 | PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); |
| 1545 | QualType pointee_type = pointer_type->getPointeeType(); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1546 | uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), |
| 1547 | omit_empty_base_classes); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1548 | // If this type points to a simple type, then it has 1 child |
| 1549 | if (num_pointee_children == 0) |
| 1550 | num_children = 1; |
| 1551 | else |
| 1552 | num_children = num_pointee_children; |
| 1553 | } |
| 1554 | break; |
| 1555 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1556 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1557 | num_children = ClangASTContext::GetNumChildren (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), omit_empty_base_classes); |
| 1558 | break; |
| 1559 | |
| 1560 | default: |
| 1561 | break; |
| 1562 | } |
| 1563 | return num_children; |
| 1564 | } |
| 1565 | |
| 1566 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1567 | clang_type_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1568 | ClangASTContext::GetChildClangTypeAtIndex |
| 1569 | ( |
| 1570 | const char *parent_name, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1571 | clang_type_t parent_clang_type, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1572 | uint32_t idx, |
| 1573 | bool transparent_pointers, |
| 1574 | bool omit_empty_base_classes, |
| 1575 | std::string& child_name, |
| 1576 | uint32_t &child_byte_size, |
| 1577 | int32_t &child_byte_offset, |
| 1578 | uint32_t &child_bitfield_bit_size, |
| 1579 | uint32_t &child_bitfield_bit_offset |
| 1580 | ) |
| 1581 | { |
| 1582 | if (parent_clang_type) |
| 1583 | |
| 1584 | return GetChildClangTypeAtIndex (getASTContext(), |
| 1585 | parent_name, |
| 1586 | parent_clang_type, |
| 1587 | idx, |
| 1588 | transparent_pointers, |
| 1589 | omit_empty_base_classes, |
| 1590 | child_name, |
| 1591 | child_byte_size, |
| 1592 | child_byte_offset, |
| 1593 | child_bitfield_bit_size, |
| 1594 | child_bitfield_bit_offset); |
| 1595 | return NULL; |
| 1596 | } |
| 1597 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1598 | clang_type_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1599 | ClangASTContext::GetChildClangTypeAtIndex |
| 1600 | ( |
| 1601 | ASTContext *ast_context, |
| 1602 | const char *parent_name, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 1603 | clang_type_t parent_clang_type, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1604 | uint32_t idx, |
| 1605 | bool transparent_pointers, |
| 1606 | bool omit_empty_base_classes, |
| 1607 | std::string& child_name, |
| 1608 | uint32_t &child_byte_size, |
| 1609 | int32_t &child_byte_offset, |
| 1610 | uint32_t &child_bitfield_bit_size, |
| 1611 | uint32_t &child_bitfield_bit_offset |
| 1612 | ) |
| 1613 | { |
| 1614 | if (parent_clang_type == NULL) |
| 1615 | return NULL; |
| 1616 | |
| 1617 | if (idx < ClangASTContext::GetNumChildren (parent_clang_type, omit_empty_base_classes)) |
| 1618 | { |
| 1619 | uint32_t bit_offset; |
| 1620 | child_bitfield_bit_size = 0; |
| 1621 | child_bitfield_bit_offset = 0; |
| 1622 | QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 1623 | const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass(); |
| 1624 | switch (parent_type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1625 | { |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1626 | case clang::Type::Builtin: |
| 1627 | switch (cast<clang::BuiltinType>(parent_qual_type)->getKind()) |
| 1628 | { |
| 1629 | case clang::BuiltinType::ObjCId: |
| 1630 | case clang::BuiltinType::ObjCClass: |
| 1631 | return ast_context->ObjCBuiltinClassTy.getAsOpaquePtr(); |
| 1632 | |
| 1633 | case clang::BuiltinType::ObjCSel: |
| 1634 | { |
| 1635 | QualType char_type(ast_context->CharTy); |
| 1636 | char_type.addConst(); |
| 1637 | return ast_context->getPointerType(char_type).getAsOpaquePtr(); |
| 1638 | } |
| 1639 | break; |
| 1640 | |
| 1641 | default: |
| 1642 | break; |
| 1643 | } |
| 1644 | break; |
| 1645 | |
| 1646 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1647 | case clang::Type::Record: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1648 | { |
| 1649 | const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); |
| 1650 | const RecordDecl *record_decl = record_type->getDecl(); |
| 1651 | assert(record_decl); |
| 1652 | const ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl); |
| 1653 | uint32_t child_idx = 0; |
| 1654 | |
| 1655 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1656 | if (cxx_record_decl) |
| 1657 | { |
| 1658 | // We might have base classes to print out first |
| 1659 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1660 | for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 1661 | base_class != base_class_end; |
| 1662 | ++base_class) |
| 1663 | { |
| 1664 | const CXXRecordDecl *base_class_decl = NULL; |
| 1665 | |
| 1666 | // Skip empty base classes |
| 1667 | if (omit_empty_base_classes) |
| 1668 | { |
| 1669 | base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1670 | if (RecordHasFields(base_class_decl) == false) |
| 1671 | continue; |
| 1672 | } |
| 1673 | |
| 1674 | if (idx == child_idx) |
| 1675 | { |
| 1676 | if (base_class_decl == NULL) |
| 1677 | base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1678 | |
| 1679 | |
| 1680 | if (base_class->isVirtual()) |
| 1681 | bit_offset = record_layout.getVBaseClassOffset(base_class_decl); |
| 1682 | else |
| 1683 | bit_offset = record_layout.getBaseClassOffset(base_class_decl); |
| 1684 | |
| 1685 | // Base classes should be a multiple of 8 bits in size |
| 1686 | assert (bit_offset % 8 == 0); |
| 1687 | child_byte_offset = bit_offset/8; |
| 1688 | std::string base_class_type_name(base_class->getType().getAsString()); |
| 1689 | |
| 1690 | child_name.assign(base_class_type_name.c_str()); |
| 1691 | |
| 1692 | uint64_t clang_type_info_bit_size = ast_context->getTypeSize(base_class->getType()); |
| 1693 | |
| 1694 | // Base classes biut sizes should be a multiple of 8 bits in size |
| 1695 | assert (clang_type_info_bit_size % 8 == 0); |
| 1696 | child_byte_size = clang_type_info_bit_size / 8; |
| 1697 | return base_class->getType().getAsOpaquePtr(); |
| 1698 | } |
| 1699 | // We don't increment the child index in the for loop since we might |
| 1700 | // be skipping empty base classes |
| 1701 | ++child_idx; |
| 1702 | } |
| 1703 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1704 | // Make sure index is in range... |
| 1705 | uint32_t field_idx = 0; |
| 1706 | RecordDecl::field_iterator field, field_end; |
| 1707 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) |
| 1708 | { |
| 1709 | if (idx == child_idx) |
| 1710 | { |
| 1711 | // Print the member type if requested |
| 1712 | // Print the member name and equal sign |
| 1713 | child_name.assign(field->getNameAsString().c_str()); |
| 1714 | |
| 1715 | // Figure out the type byte size (field_type_info.first) and |
| 1716 | // alignment (field_type_info.second) from the AST context. |
| 1717 | std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field->getType()); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1718 | assert(field_idx < record_layout.getFieldCount()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1719 | |
| 1720 | child_byte_size = field_type_info.first / 8; |
| 1721 | |
| 1722 | // Figure out the field offset within the current struct/union/class type |
| 1723 | bit_offset = record_layout.getFieldOffset (field_idx); |
| 1724 | child_byte_offset = bit_offset / 8; |
| 1725 | if (ClangASTContext::FieldIsBitfield (ast_context, *field, child_bitfield_bit_size)) |
| 1726 | child_bitfield_bit_offset = bit_offset % 8; |
| 1727 | |
| 1728 | return field->getType().getAsOpaquePtr(); |
| 1729 | } |
| 1730 | } |
| 1731 | } |
| 1732 | break; |
| 1733 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1734 | case clang::Type::ObjCObject: |
| 1735 | case clang::Type::ObjCInterface: |
| 1736 | { |
| 1737 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr()); |
| 1738 | assert (objc_class_type); |
| 1739 | if (objc_class_type) |
| 1740 | { |
| 1741 | uint32_t child_idx = 0; |
| 1742 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 1743 | |
| 1744 | if (class_interface_decl) |
| 1745 | { |
| 1746 | |
| 1747 | const ASTRecordLayout &interface_layout = ast_context->getASTObjCInterfaceLayout(class_interface_decl); |
| 1748 | ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); |
| 1749 | if (superclass_interface_decl) |
| 1750 | { |
| 1751 | if (omit_empty_base_classes) |
| 1752 | { |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1753 | if (ClangASTContext::GetNumChildren(ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0) |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1754 | { |
| 1755 | if (idx == 0) |
| 1756 | { |
| 1757 | QualType ivar_qual_type(ast_context->getObjCInterfaceType(superclass_interface_decl)); |
| 1758 | |
| 1759 | |
| 1760 | child_name.assign(superclass_interface_decl->getNameAsString().c_str()); |
| 1761 | |
| 1762 | std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 1763 | |
| 1764 | child_byte_size = ivar_type_info.first / 8; |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1765 | child_byte_offset = 0; |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1766 | |
| 1767 | return ivar_qual_type.getAsOpaquePtr(); |
| 1768 | } |
| 1769 | |
| 1770 | ++child_idx; |
| 1771 | } |
| 1772 | } |
| 1773 | else |
| 1774 | ++child_idx; |
| 1775 | } |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1776 | |
| 1777 | const uint32_t superclass_idx = child_idx; |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1778 | |
| 1779 | if (idx < (child_idx + class_interface_decl->ivar_size())) |
| 1780 | { |
| 1781 | ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); |
| 1782 | |
| 1783 | for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) |
| 1784 | { |
| 1785 | if (child_idx == idx) |
| 1786 | { |
| 1787 | const ObjCIvarDecl* ivar_decl = *ivar_pos; |
| 1788 | |
| 1789 | QualType ivar_qual_type(ivar_decl->getType()); |
| 1790 | |
| 1791 | child_name.assign(ivar_decl->getNameAsString().c_str()); |
| 1792 | |
| 1793 | std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 1794 | |
| 1795 | child_byte_size = ivar_type_info.first / 8; |
| 1796 | |
| 1797 | // Figure out the field offset within the current struct/union/class type |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1798 | bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1799 | child_byte_offset = bit_offset / 8; |
| 1800 | |
| 1801 | return ivar_qual_type.getAsOpaquePtr(); |
| 1802 | } |
| 1803 | ++child_idx; |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | break; |
| 1810 | |
| 1811 | case clang::Type::ObjCObjectPointer: |
| 1812 | { |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1813 | ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr()); |
| 1814 | QualType pointee_type = pointer_type->getPointeeType(); |
| 1815 | |
| 1816 | if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 1817 | { |
| 1818 | return GetChildClangTypeAtIndex (ast_context, |
| 1819 | parent_name, |
| 1820 | pointer_type->getPointeeType().getAsOpaquePtr(), |
| 1821 | idx, |
| 1822 | transparent_pointers, |
| 1823 | omit_empty_base_classes, |
| 1824 | child_name, |
| 1825 | child_byte_size, |
| 1826 | child_byte_offset, |
| 1827 | child_bitfield_bit_size, |
| 1828 | child_bitfield_bit_offset); |
| 1829 | } |
| 1830 | else |
| 1831 | { |
| 1832 | if (parent_name) |
| 1833 | { |
| 1834 | child_name.assign(1, '*'); |
| 1835 | child_name += parent_name; |
| 1836 | } |
| 1837 | |
| 1838 | // We have a pointer to an simple type |
| 1839 | if (idx == 0) |
| 1840 | { |
| 1841 | std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); |
| 1842 | assert(clang_type_info.first % 8 == 0); |
| 1843 | child_byte_size = clang_type_info.first / 8; |
| 1844 | child_byte_offset = 0; |
| 1845 | return pointee_type.getAsOpaquePtr(); |
| 1846 | } |
| 1847 | } |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1848 | } |
| 1849 | break; |
| 1850 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1851 | case clang::Type::ConstantArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1852 | { |
| 1853 | const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 1854 | const uint64_t element_count = array->getSize().getLimitedValue(); |
| 1855 | |
| 1856 | if (idx < element_count) |
| 1857 | { |
| 1858 | std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); |
| 1859 | |
| 1860 | char element_name[32]; |
| 1861 | ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); |
| 1862 | |
| 1863 | child_name.assign(element_name); |
| 1864 | assert(field_type_info.first % 8 == 0); |
| 1865 | child_byte_size = field_type_info.first / 8; |
| 1866 | child_byte_offset = idx * child_byte_size; |
| 1867 | return array->getElementType().getAsOpaquePtr(); |
| 1868 | } |
| 1869 | } |
| 1870 | break; |
| 1871 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1872 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1873 | { |
| 1874 | PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); |
| 1875 | QualType pointee_type = pointer_type->getPointeeType(); |
| 1876 | |
| 1877 | if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 1878 | { |
| 1879 | return GetChildClangTypeAtIndex (ast_context, |
| 1880 | parent_name, |
| 1881 | pointer_type->getPointeeType().getAsOpaquePtr(), |
| 1882 | idx, |
| 1883 | transparent_pointers, |
| 1884 | omit_empty_base_classes, |
| 1885 | child_name, |
| 1886 | child_byte_size, |
| 1887 | child_byte_offset, |
| 1888 | child_bitfield_bit_size, |
| 1889 | child_bitfield_bit_offset); |
| 1890 | } |
| 1891 | else |
| 1892 | { |
| 1893 | if (parent_name) |
| 1894 | { |
| 1895 | child_name.assign(1, '*'); |
| 1896 | child_name += parent_name; |
| 1897 | } |
| 1898 | |
| 1899 | // We have a pointer to an simple type |
| 1900 | if (idx == 0) |
| 1901 | { |
| 1902 | std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); |
| 1903 | assert(clang_type_info.first % 8 == 0); |
| 1904 | child_byte_size = clang_type_info.first / 8; |
| 1905 | child_byte_offset = 0; |
| 1906 | return pointee_type.getAsOpaquePtr(); |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | break; |
| 1911 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 1912 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1913 | return GetChildClangTypeAtIndex (ast_context, |
| 1914 | parent_name, |
| 1915 | cast<TypedefType>(parent_qual_type)->LookThroughTypedefs().getAsOpaquePtr(), |
| 1916 | idx, |
| 1917 | transparent_pointers, |
| 1918 | omit_empty_base_classes, |
| 1919 | child_name, |
| 1920 | child_byte_size, |
| 1921 | child_byte_offset, |
| 1922 | child_bitfield_bit_size, |
| 1923 | child_bitfield_bit_offset); |
| 1924 | break; |
| 1925 | |
| 1926 | default: |
| 1927 | break; |
| 1928 | } |
| 1929 | } |
Greg Clayton | f8e98a6 | 2010-07-23 15:37:46 +0000 | [diff] [blame] | 1930 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | static inline bool |
| 1934 | BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) |
| 1935 | { |
| 1936 | return ClangASTContext::RecordHasFields(cast<CXXRecordDecl>(b->getType()->getAs<RecordType>()->getDecl())) == false; |
| 1937 | } |
| 1938 | |
| 1939 | static uint32_t |
| 1940 | GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) |
| 1941 | { |
| 1942 | uint32_t num_bases = 0; |
| 1943 | if (cxx_record_decl) |
| 1944 | { |
| 1945 | if (omit_empty_base_classes) |
| 1946 | { |
| 1947 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1948 | for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 1949 | base_class != base_class_end; |
| 1950 | ++base_class) |
| 1951 | { |
| 1952 | // Skip empty base classes |
| 1953 | if (omit_empty_base_classes) |
| 1954 | { |
| 1955 | if (BaseSpecifierIsEmpty (base_class)) |
| 1956 | continue; |
| 1957 | } |
| 1958 | ++num_bases; |
| 1959 | } |
| 1960 | } |
| 1961 | else |
| 1962 | num_bases = cxx_record_decl->getNumBases(); |
| 1963 | } |
| 1964 | return num_bases; |
| 1965 | } |
| 1966 | |
| 1967 | |
| 1968 | static uint32_t |
| 1969 | GetIndexForRecordBase |
| 1970 | ( |
| 1971 | const RecordDecl *record_decl, |
| 1972 | const CXXBaseSpecifier *base_spec, |
| 1973 | bool omit_empty_base_classes |
| 1974 | ) |
| 1975 | { |
| 1976 | uint32_t child_idx = 0; |
| 1977 | |
| 1978 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1979 | |
| 1980 | // const char *super_name = record_decl->getNameAsCString(); |
| 1981 | // const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); |
| 1982 | // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); |
| 1983 | // |
| 1984 | if (cxx_record_decl) |
| 1985 | { |
| 1986 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1987 | for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 1988 | base_class != base_class_end; |
| 1989 | ++base_class) |
| 1990 | { |
| 1991 | if (omit_empty_base_classes) |
| 1992 | { |
| 1993 | if (BaseSpecifierIsEmpty (base_class)) |
| 1994 | continue; |
| 1995 | } |
| 1996 | |
| 1997 | // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, |
| 1998 | // child_idx, |
| 1999 | // base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); |
| 2000 | // |
| 2001 | // |
| 2002 | if (base_class == base_spec) |
| 2003 | return child_idx; |
| 2004 | ++child_idx; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | return UINT32_MAX; |
| 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | static uint32_t |
| 2013 | GetIndexForRecordChild |
| 2014 | ( |
| 2015 | const RecordDecl *record_decl, |
| 2016 | NamedDecl *canonical_decl, |
| 2017 | bool omit_empty_base_classes |
| 2018 | ) |
| 2019 | { |
| 2020 | uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); |
| 2021 | |
| 2022 | // const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 2023 | // |
| 2024 | //// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); |
| 2025 | // if (cxx_record_decl) |
| 2026 | // { |
| 2027 | // CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 2028 | // for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 2029 | // base_class != base_class_end; |
| 2030 | // ++base_class) |
| 2031 | // { |
| 2032 | // if (omit_empty_base_classes) |
| 2033 | // { |
| 2034 | // if (BaseSpecifierIsEmpty (base_class)) |
| 2035 | // continue; |
| 2036 | // } |
| 2037 | // |
| 2038 | //// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", |
| 2039 | //// record_decl->getNameAsCString(), |
| 2040 | //// canonical_decl->getNameAsCString(), |
| 2041 | //// child_idx, |
| 2042 | //// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); |
| 2043 | // |
| 2044 | // |
| 2045 | // CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); |
| 2046 | // if (curr_base_class_decl == canonical_decl) |
| 2047 | // { |
| 2048 | // return child_idx; |
| 2049 | // } |
| 2050 | // ++child_idx; |
| 2051 | // } |
| 2052 | // } |
| 2053 | // |
| 2054 | // const uint32_t num_bases = child_idx; |
| 2055 | RecordDecl::field_iterator field, field_end; |
| 2056 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); |
| 2057 | field != field_end; |
| 2058 | ++field, ++child_idx) |
| 2059 | { |
| 2060 | // printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", |
| 2061 | // record_decl->getNameAsCString(), |
| 2062 | // canonical_decl->getNameAsCString(), |
| 2063 | // child_idx - num_bases, |
| 2064 | // field->getNameAsCString()); |
| 2065 | |
| 2066 | if (field->getCanonicalDecl() == canonical_decl) |
| 2067 | return child_idx; |
| 2068 | } |
| 2069 | |
| 2070 | return UINT32_MAX; |
| 2071 | } |
| 2072 | |
| 2073 | // Look for a child member (doesn't include base classes, but it does include |
| 2074 | // their members) in the type hierarchy. Returns an index path into "clang_type" |
| 2075 | // on how to reach the appropriate member. |
| 2076 | // |
| 2077 | // class A |
| 2078 | // { |
| 2079 | // public: |
| 2080 | // int m_a; |
| 2081 | // int m_b; |
| 2082 | // }; |
| 2083 | // |
| 2084 | // class B |
| 2085 | // { |
| 2086 | // }; |
| 2087 | // |
| 2088 | // class C : |
| 2089 | // public B, |
| 2090 | // public A |
| 2091 | // { |
| 2092 | // }; |
| 2093 | // |
| 2094 | // If we have a clang type that describes "class C", and we wanted to looked |
| 2095 | // "m_b" in it: |
| 2096 | // |
| 2097 | // With omit_empty_base_classes == false we would get an integer array back with: |
| 2098 | // { 1, 1 } |
| 2099 | // The first index 1 is the child index for "class A" within class C |
| 2100 | // The second index 1 is the child index for "m_b" within class A |
| 2101 | // |
| 2102 | // With omit_empty_base_classes == true we would get an integer array back with: |
| 2103 | // { 0, 1 } |
| 2104 | // 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) |
| 2105 | // The second index 1 is the child index for "m_b" within class A |
| 2106 | |
| 2107 | size_t |
| 2108 | ClangASTContext::GetIndexOfChildMemberWithName |
| 2109 | ( |
| 2110 | ASTContext *ast_context, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2111 | clang_type_t clang_type, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2112 | const char *name, |
| 2113 | bool omit_empty_base_classes, |
| 2114 | std::vector<uint32_t>& child_indexes |
| 2115 | ) |
| 2116 | { |
| 2117 | if (clang_type && name && name[0]) |
| 2118 | { |
| 2119 | QualType qual_type(QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 2120 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2121 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2122 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2123 | case clang::Type::Record: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2124 | { |
| 2125 | const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); |
| 2126 | const RecordDecl *record_decl = record_type->getDecl(); |
| 2127 | |
| 2128 | assert(record_decl); |
| 2129 | uint32_t child_idx = 0; |
| 2130 | |
| 2131 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 2132 | |
| 2133 | // Try and find a field that matches NAME |
| 2134 | RecordDecl::field_iterator field, field_end; |
| 2135 | StringRef name_sref(name); |
| 2136 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); |
| 2137 | field != field_end; |
| 2138 | ++field, ++child_idx) |
| 2139 | { |
| 2140 | if (field->getName().equals (name_sref)) |
| 2141 | { |
| 2142 | // We have to add on the number of base classes to this index! |
| 2143 | child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); |
| 2144 | return child_indexes.size(); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | if (cxx_record_decl) |
| 2149 | { |
| 2150 | const RecordDecl *parent_record_decl = cxx_record_decl; |
| 2151 | |
| 2152 | //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); |
| 2153 | |
| 2154 | //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); |
| 2155 | // Didn't find things easily, lets let clang do its thang... |
| 2156 | IdentifierInfo & ident_ref = ast_context->Idents.get(name, name + strlen (name)); |
| 2157 | DeclarationName decl_name(&ident_ref); |
| 2158 | |
| 2159 | CXXBasePaths paths; |
| 2160 | if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, |
| 2161 | decl_name.getAsOpaquePtr(), |
| 2162 | paths)) |
| 2163 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2164 | CXXBasePaths::const_paths_iterator path, path_end = paths.end(); |
| 2165 | for (path = paths.begin(); path != path_end; ++path) |
| 2166 | { |
| 2167 | const size_t num_path_elements = path->size(); |
| 2168 | for (size_t e=0; e<num_path_elements; ++e) |
| 2169 | { |
| 2170 | CXXBasePathElement elem = (*path)[e]; |
| 2171 | |
| 2172 | child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); |
| 2173 | if (child_idx == UINT32_MAX) |
| 2174 | { |
| 2175 | child_indexes.clear(); |
| 2176 | return 0; |
| 2177 | } |
| 2178 | else |
| 2179 | { |
| 2180 | child_indexes.push_back (child_idx); |
| 2181 | parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); |
| 2182 | } |
| 2183 | } |
| 2184 | DeclContext::lookup_iterator named_decl_pos; |
| 2185 | for (named_decl_pos = path->Decls.first; |
| 2186 | named_decl_pos != path->Decls.second && parent_record_decl; |
| 2187 | ++named_decl_pos) |
| 2188 | { |
| 2189 | //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); |
| 2190 | |
| 2191 | child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); |
| 2192 | if (child_idx == UINT32_MAX) |
| 2193 | { |
| 2194 | child_indexes.clear(); |
| 2195 | return 0; |
| 2196 | } |
| 2197 | else |
| 2198 | { |
| 2199 | child_indexes.push_back (child_idx); |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | return child_indexes.size(); |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | } |
| 2208 | break; |
| 2209 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2210 | case clang::Type::ObjCObject: |
| 2211 | case clang::Type::ObjCInterface: |
| 2212 | { |
| 2213 | StringRef name_sref(name); |
| 2214 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); |
| 2215 | assert (objc_class_type); |
| 2216 | if (objc_class_type) |
| 2217 | { |
| 2218 | uint32_t child_idx = 0; |
| 2219 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 2220 | |
| 2221 | if (class_interface_decl) |
| 2222 | { |
| 2223 | ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); |
| 2224 | ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); |
| 2225 | |
Greg Clayton | 823533e | 2010-09-18 02:11:07 +0000 | [diff] [blame] | 2226 | for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx) |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2227 | { |
| 2228 | const ObjCIvarDecl* ivar_decl = *ivar_pos; |
| 2229 | |
| 2230 | if (ivar_decl->getName().equals (name_sref)) |
| 2231 | { |
| 2232 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 2233 | ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) |
| 2234 | ++child_idx; |
| 2235 | |
| 2236 | child_indexes.push_back (child_idx); |
| 2237 | return child_indexes.size(); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | if (superclass_interface_decl) |
| 2242 | { |
| 2243 | // The super class index is always zero for ObjC classes, |
| 2244 | // so we push it onto the child indexes in case we find |
| 2245 | // an ivar in our superclass... |
| 2246 | child_indexes.push_back (0); |
| 2247 | |
| 2248 | if (GetIndexOfChildMemberWithName (ast_context, |
| 2249 | ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), |
| 2250 | name, |
| 2251 | omit_empty_base_classes, |
| 2252 | child_indexes)) |
| 2253 | { |
| 2254 | // We did find an ivar in a superclass so just |
| 2255 | // return the results! |
| 2256 | return child_indexes.size(); |
| 2257 | } |
| 2258 | |
| 2259 | // We didn't find an ivar matching "name" in our |
| 2260 | // superclass, pop the superclass zero index that |
| 2261 | // we pushed on above. |
| 2262 | child_indexes.pop_back(); |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | break; |
| 2268 | |
| 2269 | case clang::Type::ObjCObjectPointer: |
| 2270 | { |
| 2271 | return GetIndexOfChildMemberWithName (ast_context, |
| 2272 | cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), |
| 2273 | name, |
| 2274 | omit_empty_base_classes, |
| 2275 | child_indexes); |
| 2276 | } |
| 2277 | break; |
| 2278 | |
| 2279 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2280 | case clang::Type::ConstantArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2281 | { |
| 2282 | // const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 2283 | // const uint64_t element_count = array->getSize().getLimitedValue(); |
| 2284 | // |
| 2285 | // if (idx < element_count) |
| 2286 | // { |
| 2287 | // std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); |
| 2288 | // |
| 2289 | // char element_name[32]; |
| 2290 | // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); |
| 2291 | // |
| 2292 | // child_name.assign(element_name); |
| 2293 | // assert(field_type_info.first % 8 == 0); |
| 2294 | // child_byte_size = field_type_info.first / 8; |
| 2295 | // child_byte_offset = idx * child_byte_size; |
| 2296 | // return array->getElementType().getAsOpaquePtr(); |
| 2297 | // } |
| 2298 | } |
| 2299 | break; |
| 2300 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2301 | // case clang::Type::MemberPointerType: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2302 | // { |
| 2303 | // MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); |
| 2304 | // QualType pointee_type = mem_ptr_type->getPointeeType(); |
| 2305 | // |
| 2306 | // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 2307 | // { |
| 2308 | // return GetIndexOfChildWithName (ast_context, |
| 2309 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 2310 | // name); |
| 2311 | // } |
| 2312 | // } |
| 2313 | // break; |
| 2314 | // |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2315 | case clang::Type::LValueReference: |
| 2316 | case clang::Type::RValueReference: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2317 | { |
| 2318 | ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); |
| 2319 | QualType pointee_type = reference_type->getPointeeType(); |
| 2320 | |
| 2321 | if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 2322 | { |
| 2323 | return GetIndexOfChildMemberWithName (ast_context, |
| 2324 | reference_type->getPointeeType().getAsOpaquePtr(), |
| 2325 | name, |
| 2326 | omit_empty_base_classes, |
| 2327 | child_indexes); |
| 2328 | } |
| 2329 | } |
| 2330 | break; |
| 2331 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2332 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2333 | { |
| 2334 | PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); |
| 2335 | QualType pointee_type = pointer_type->getPointeeType(); |
| 2336 | |
| 2337 | if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 2338 | { |
| 2339 | return GetIndexOfChildMemberWithName (ast_context, |
| 2340 | pointer_type->getPointeeType().getAsOpaquePtr(), |
| 2341 | name, |
| 2342 | omit_empty_base_classes, |
| 2343 | child_indexes); |
| 2344 | } |
| 2345 | else |
| 2346 | { |
| 2347 | // if (parent_name) |
| 2348 | // { |
| 2349 | // child_name.assign(1, '*'); |
| 2350 | // child_name += parent_name; |
| 2351 | // } |
| 2352 | // |
| 2353 | // // We have a pointer to an simple type |
| 2354 | // if (idx == 0) |
| 2355 | // { |
| 2356 | // std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); |
| 2357 | // assert(clang_type_info.first % 8 == 0); |
| 2358 | // child_byte_size = clang_type_info.first / 8; |
| 2359 | // child_byte_offset = 0; |
| 2360 | // return pointee_type.getAsOpaquePtr(); |
| 2361 | // } |
| 2362 | } |
| 2363 | } |
| 2364 | break; |
| 2365 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2366 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2367 | return GetIndexOfChildMemberWithName (ast_context, |
| 2368 | cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), |
| 2369 | name, |
| 2370 | omit_empty_base_classes, |
| 2371 | child_indexes); |
| 2372 | |
| 2373 | default: |
| 2374 | break; |
| 2375 | } |
| 2376 | } |
| 2377 | return 0; |
| 2378 | } |
| 2379 | |
| 2380 | |
| 2381 | // Get the index of the child of "clang_type" whose name matches. This function |
| 2382 | // doesn't descend into the children, but only looks one level deep and name |
| 2383 | // matches can include base class names. |
| 2384 | |
| 2385 | uint32_t |
| 2386 | ClangASTContext::GetIndexOfChildWithName |
| 2387 | ( |
| 2388 | ASTContext *ast_context, |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2389 | clang_type_t clang_type, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2390 | const char *name, |
| 2391 | bool omit_empty_base_classes |
| 2392 | ) |
| 2393 | { |
| 2394 | if (clang_type && name && name[0]) |
| 2395 | { |
| 2396 | QualType qual_type(QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2397 | |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 2398 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2399 | |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 2400 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2401 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2402 | case clang::Type::Record: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2403 | { |
| 2404 | const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); |
| 2405 | const RecordDecl *record_decl = record_type->getDecl(); |
| 2406 | |
| 2407 | assert(record_decl); |
| 2408 | uint32_t child_idx = 0; |
| 2409 | |
| 2410 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 2411 | |
| 2412 | if (cxx_record_decl) |
| 2413 | { |
| 2414 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 2415 | for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); |
| 2416 | base_class != base_class_end; |
| 2417 | ++base_class) |
| 2418 | { |
| 2419 | // Skip empty base classes |
| 2420 | CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); |
| 2421 | if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) |
| 2422 | continue; |
| 2423 | |
| 2424 | if (base_class->getType().getAsString().compare (name) == 0) |
| 2425 | return child_idx; |
| 2426 | ++child_idx; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | // Try and find a field that matches NAME |
| 2431 | RecordDecl::field_iterator field, field_end; |
| 2432 | StringRef name_sref(name); |
| 2433 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); |
| 2434 | field != field_end; |
| 2435 | ++field, ++child_idx) |
| 2436 | { |
| 2437 | if (field->getName().equals (name_sref)) |
| 2438 | return child_idx; |
| 2439 | } |
| 2440 | |
| 2441 | } |
| 2442 | break; |
| 2443 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2444 | case clang::Type::ObjCObject: |
| 2445 | case clang::Type::ObjCInterface: |
| 2446 | { |
| 2447 | StringRef name_sref(name); |
| 2448 | ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); |
| 2449 | assert (objc_class_type); |
| 2450 | if (objc_class_type) |
| 2451 | { |
| 2452 | uint32_t child_idx = 0; |
| 2453 | ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); |
| 2454 | |
| 2455 | if (class_interface_decl) |
| 2456 | { |
| 2457 | ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); |
| 2458 | ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); |
| 2459 | |
| 2460 | for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) |
| 2461 | { |
| 2462 | const ObjCIvarDecl* ivar_decl = *ivar_pos; |
| 2463 | |
| 2464 | if (ivar_decl->getName().equals (name_sref)) |
| 2465 | { |
| 2466 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 2467 | ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) |
| 2468 | ++child_idx; |
| 2469 | |
| 2470 | return child_idx; |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | if (superclass_interface_decl) |
| 2475 | { |
| 2476 | if (superclass_interface_decl->getName().equals (name_sref)) |
| 2477 | return 0; |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | break; |
| 2483 | |
| 2484 | case clang::Type::ObjCObjectPointer: |
| 2485 | { |
| 2486 | return GetIndexOfChildWithName (ast_context, |
| 2487 | cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), |
| 2488 | name, |
| 2489 | omit_empty_base_classes); |
| 2490 | } |
| 2491 | break; |
| 2492 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2493 | case clang::Type::ConstantArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2494 | { |
| 2495 | // const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 2496 | // const uint64_t element_count = array->getSize().getLimitedValue(); |
| 2497 | // |
| 2498 | // if (idx < element_count) |
| 2499 | // { |
| 2500 | // std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); |
| 2501 | // |
| 2502 | // char element_name[32]; |
| 2503 | // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); |
| 2504 | // |
| 2505 | // child_name.assign(element_name); |
| 2506 | // assert(field_type_info.first % 8 == 0); |
| 2507 | // child_byte_size = field_type_info.first / 8; |
| 2508 | // child_byte_offset = idx * child_byte_size; |
| 2509 | // return array->getElementType().getAsOpaquePtr(); |
| 2510 | // } |
| 2511 | } |
| 2512 | break; |
| 2513 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2514 | // case clang::Type::MemberPointerType: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2515 | // { |
| 2516 | // MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); |
| 2517 | // QualType pointee_type = mem_ptr_type->getPointeeType(); |
| 2518 | // |
| 2519 | // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 2520 | // { |
| 2521 | // return GetIndexOfChildWithName (ast_context, |
| 2522 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 2523 | // name); |
| 2524 | // } |
| 2525 | // } |
| 2526 | // break; |
| 2527 | // |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2528 | case clang::Type::LValueReference: |
| 2529 | case clang::Type::RValueReference: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2530 | { |
| 2531 | ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); |
| 2532 | QualType pointee_type = reference_type->getPointeeType(); |
| 2533 | |
| 2534 | if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 2535 | { |
| 2536 | return GetIndexOfChildWithName (ast_context, |
| 2537 | reference_type->getPointeeType().getAsOpaquePtr(), |
| 2538 | name, |
| 2539 | omit_empty_base_classes); |
| 2540 | } |
| 2541 | } |
| 2542 | break; |
| 2543 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2544 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2545 | { |
| 2546 | PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); |
| 2547 | QualType pointee_type = pointer_type->getPointeeType(); |
| 2548 | |
| 2549 | if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) |
| 2550 | { |
| 2551 | return GetIndexOfChildWithName (ast_context, |
| 2552 | pointer_type->getPointeeType().getAsOpaquePtr(), |
| 2553 | name, |
| 2554 | omit_empty_base_classes); |
| 2555 | } |
| 2556 | else |
| 2557 | { |
| 2558 | // if (parent_name) |
| 2559 | // { |
| 2560 | // child_name.assign(1, '*'); |
| 2561 | // child_name += parent_name; |
| 2562 | // } |
| 2563 | // |
| 2564 | // // We have a pointer to an simple type |
| 2565 | // if (idx == 0) |
| 2566 | // { |
| 2567 | // std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); |
| 2568 | // assert(clang_type_info.first % 8 == 0); |
| 2569 | // child_byte_size = clang_type_info.first / 8; |
| 2570 | // child_byte_offset = 0; |
| 2571 | // return pointee_type.getAsOpaquePtr(); |
| 2572 | // } |
| 2573 | } |
| 2574 | } |
| 2575 | break; |
| 2576 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2577 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2578 | return GetIndexOfChildWithName (ast_context, |
| 2579 | cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), |
| 2580 | name, |
| 2581 | omit_empty_base_classes); |
| 2582 | |
| 2583 | default: |
| 2584 | break; |
| 2585 | } |
| 2586 | } |
| 2587 | return UINT32_MAX; |
| 2588 | } |
| 2589 | |
| 2590 | #pragma mark TagType |
| 2591 | |
| 2592 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2593 | ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2594 | { |
| 2595 | if (tag_clang_type) |
| 2596 | { |
| 2597 | QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2598 | clang::Type *clang_type = tag_qual_type.getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2599 | if (clang_type) |
| 2600 | { |
| 2601 | TagType *tag_type = dyn_cast<TagType>(clang_type); |
| 2602 | if (tag_type) |
| 2603 | { |
| 2604 | TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); |
| 2605 | if (tag_decl) |
| 2606 | { |
| 2607 | tag_decl->setTagKind ((TagDecl::TagKind)kind); |
| 2608 | return true; |
| 2609 | } |
| 2610 | } |
| 2611 | } |
| 2612 | } |
| 2613 | return false; |
| 2614 | } |
| 2615 | |
| 2616 | |
| 2617 | #pragma mark DeclContext Functions |
| 2618 | |
| 2619 | DeclContext * |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2620 | ClangASTContext::GetDeclContextForType (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2621 | { |
| 2622 | if (clang_type == NULL) |
| 2623 | return NULL; |
| 2624 | |
| 2625 | QualType qual_type(QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 2626 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2627 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2628 | { |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2629 | case clang::Type::FunctionNoProto: break; |
| 2630 | case clang::Type::FunctionProto: break; |
| 2631 | case clang::Type::IncompleteArray: break; |
| 2632 | case clang::Type::VariableArray: break; |
| 2633 | case clang::Type::ConstantArray: break; |
| 2634 | case clang::Type::ExtVector: break; |
| 2635 | case clang::Type::Vector: break; |
| 2636 | case clang::Type::Builtin: break; |
| 2637 | case clang::Type::BlockPointer: break; |
| 2638 | case clang::Type::Pointer: break; |
| 2639 | case clang::Type::LValueReference: break; |
| 2640 | case clang::Type::RValueReference: break; |
| 2641 | case clang::Type::MemberPointer: break; |
| 2642 | case clang::Type::Complex: break; |
| 2643 | case clang::Type::ObjCObject: break; |
| 2644 | case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface(); |
| 2645 | case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr()); |
| 2646 | case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl(); |
| 2647 | case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl(); |
| 2648 | case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2649 | |
Greg Clayton | 9488b74 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 2650 | case clang::Type::TypeOfExpr: break; |
| 2651 | case clang::Type::TypeOf: break; |
| 2652 | case clang::Type::Decltype: break; |
| 2653 | //case clang::Type::QualifiedName: break; |
| 2654 | case clang::Type::TemplateSpecialization: break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2655 | } |
| 2656 | // No DeclContext in this type... |
| 2657 | return NULL; |
| 2658 | } |
| 2659 | |
| 2660 | #pragma mark Namespace Declarations |
| 2661 | |
| 2662 | NamespaceDecl * |
| 2663 | ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx) |
| 2664 | { |
| 2665 | // TODO: Do something intelligent with the Declaration object passed in |
| 2666 | // like maybe filling in the SourceLocation with it... |
| 2667 | if (name) |
| 2668 | { |
| 2669 | ASTContext *ast_context = getASTContext(); |
| 2670 | if (decl_ctx == NULL) |
| 2671 | decl_ctx = ast_context->getTranslationUnitDecl(); |
| 2672 | return NamespaceDecl::Create(*ast_context, decl_ctx, SourceLocation(), &ast_context->Idents.get(name)); |
| 2673 | } |
| 2674 | return NULL; |
| 2675 | } |
| 2676 | |
| 2677 | |
| 2678 | #pragma mark Function Types |
| 2679 | |
| 2680 | FunctionDecl * |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2681 | ClangASTContext::CreateFunctionDeclaration (const char *name, clang_type_t function_clang_type, int storage, bool is_inline) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2682 | { |
| 2683 | if (name) |
| 2684 | { |
| 2685 | ASTContext *ast_context = getASTContext(); |
| 2686 | assert (ast_context != NULL); |
| 2687 | |
| 2688 | if (name && name[0]) |
| 2689 | { |
| 2690 | return FunctionDecl::Create(*ast_context, |
| 2691 | ast_context->getTranslationUnitDecl(), |
| 2692 | SourceLocation(), |
| 2693 | DeclarationName (&ast_context->Idents.get(name)), |
| 2694 | QualType::getFromOpaquePtr(function_clang_type), |
| 2695 | NULL, |
| 2696 | (FunctionDecl::StorageClass)storage, |
| 2697 | (FunctionDecl::StorageClass)storage, |
| 2698 | is_inline); |
| 2699 | } |
| 2700 | else |
| 2701 | { |
| 2702 | return FunctionDecl::Create(*ast_context, |
| 2703 | ast_context->getTranslationUnitDecl(), |
| 2704 | SourceLocation(), |
| 2705 | DeclarationName (), |
| 2706 | QualType::getFromOpaquePtr(function_clang_type), |
| 2707 | NULL, |
| 2708 | (FunctionDecl::StorageClass)storage, |
| 2709 | (FunctionDecl::StorageClass)storage, |
| 2710 | is_inline); |
| 2711 | } |
| 2712 | } |
| 2713 | return NULL; |
| 2714 | } |
| 2715 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2716 | clang_type_t |
| 2717 | ClangASTContext::CreateFunctionType (ASTContext *ast_context, |
| 2718 | clang_type_t result_type, |
| 2719 | clang_type_t *args, |
Sean Callanan | 2ea8f27 | 2010-09-16 20:40:25 +0000 | [diff] [blame] | 2720 | unsigned num_args, |
| 2721 | bool is_variadic, |
| 2722 | unsigned type_quals) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2723 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2724 | assert (ast_context != NULL); |
| 2725 | std::vector<QualType> qual_type_args; |
| 2726 | for (unsigned i=0; i<num_args; ++i) |
| 2727 | qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); |
| 2728 | |
| 2729 | // TODO: Detect calling convention in DWARF? |
| 2730 | return ast_context->getFunctionType(QualType::getFromOpaquePtr(result_type), |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 2731 | qual_type_args.empty() ? NULL : &qual_type_args.front(), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2732 | qual_type_args.size(), |
Sean Callanan | 2ea8f27 | 2010-09-16 20:40:25 +0000 | [diff] [blame] | 2733 | is_variadic, |
| 2734 | type_quals, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2735 | false, // hasExceptionSpec |
| 2736 | false, // hasAnyExceptionSpec, |
| 2737 | 0, // NumExs |
| 2738 | 0, // const QualType *ExArray |
| 2739 | FunctionType::ExtInfo ()).getAsOpaquePtr(); // NoReturn); |
| 2740 | } |
| 2741 | |
| 2742 | ParmVarDecl * |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2743 | ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2744 | { |
| 2745 | ASTContext *ast_context = getASTContext(); |
| 2746 | assert (ast_context != NULL); |
| 2747 | return ParmVarDecl::Create(*ast_context, |
| 2748 | ast_context->getTranslationUnitDecl(), |
| 2749 | SourceLocation(), |
| 2750 | name && name[0] ? &ast_context->Idents.get(name) : NULL, |
Sean Callanan | 2ea8f27 | 2010-09-16 20:40:25 +0000 | [diff] [blame] | 2751 | QualType::getFromOpaquePtr(param_type), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2752 | NULL, |
| 2753 | (VarDecl::StorageClass)storage, |
| 2754 | (VarDecl::StorageClass)storage, |
| 2755 | 0); |
| 2756 | } |
| 2757 | |
| 2758 | void |
| 2759 | ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) |
| 2760 | { |
| 2761 | if (function_decl) |
| 2762 | function_decl->setParams (params, num_params); |
| 2763 | } |
| 2764 | |
| 2765 | |
| 2766 | #pragma mark Array Types |
| 2767 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2768 | clang_type_t |
| 2769 | ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2770 | { |
| 2771 | if (element_type) |
| 2772 | { |
| 2773 | ASTContext *ast_context = getASTContext(); |
| 2774 | assert (ast_context != NULL); |
| 2775 | llvm::APInt ap_element_count (64, element_count); |
| 2776 | return ast_context->getConstantArrayType(QualType::getFromOpaquePtr(element_type), |
| 2777 | ap_element_count, |
| 2778 | ArrayType::Normal, |
| 2779 | 0).getAsOpaquePtr(); // ElemQuals |
| 2780 | } |
| 2781 | return NULL; |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | #pragma mark TagDecl |
| 2786 | |
| 2787 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2788 | ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2789 | { |
| 2790 | if (clang_type) |
| 2791 | { |
| 2792 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2793 | clang::Type *t = qual_type.getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2794 | if (t) |
| 2795 | { |
| 2796 | TagType *tag_type = dyn_cast<TagType>(t); |
| 2797 | if (tag_type) |
| 2798 | { |
| 2799 | TagDecl *tag_decl = tag_type->getDecl(); |
| 2800 | if (tag_decl) |
| 2801 | { |
| 2802 | tag_decl->startDefinition(); |
| 2803 | return true; |
| 2804 | } |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | return false; |
| 2809 | } |
| 2810 | |
| 2811 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2812 | ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2813 | { |
| 2814 | if (clang_type) |
| 2815 | { |
| 2816 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 55b6c53 | 2010-09-29 03:44:17 +0000 | [diff] [blame^] | 2817 | |
| 2818 | CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 2819 | |
| 2820 | if (cxx_record_decl) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2821 | { |
Greg Clayton | 55b6c53 | 2010-09-29 03:44:17 +0000 | [diff] [blame^] | 2822 | cxx_record_decl->completeDefinition(); |
| 2823 | |
| 2824 | return true; |
| 2825 | } |
| 2826 | |
| 2827 | const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr()); |
| 2828 | |
| 2829 | if (enum_type) |
| 2830 | { |
| 2831 | EnumDecl *enum_decl = enum_type->getDecl(); |
| 2832 | |
| 2833 | if (enum_decl) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2834 | { |
Greg Clayton | 55b6c53 | 2010-09-29 03:44:17 +0000 | [diff] [blame^] | 2835 | /// TODO This really needs to be fixed. |
| 2836 | |
| 2837 | unsigned NumPositiveBits = 1; |
| 2838 | unsigned NumNegativeBits = 0; |
| 2839 | |
| 2840 | enum_decl->completeDefinition(enum_decl->getIntegerType(), enum_decl->getIntegerType(), NumPositiveBits, NumNegativeBits); |
| 2841 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | } |
| 2845 | return false; |
| 2846 | } |
| 2847 | |
| 2848 | |
| 2849 | #pragma mark Enumeration Types |
| 2850 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2851 | clang_type_t |
| 2852 | ClangASTContext::CreateEnumerationType (const Declaration &decl, const char *name, clang_type_t integer_qual_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2853 | { |
| 2854 | // TODO: Do something intelligent with the Declaration object passed in |
| 2855 | // like maybe filling in the SourceLocation with it... |
| 2856 | ASTContext *ast_context = getASTContext(); |
| 2857 | assert (ast_context != NULL); |
| 2858 | EnumDecl *enum_decl = EnumDecl::Create(*ast_context, |
| 2859 | ast_context->getTranslationUnitDecl(), |
| 2860 | SourceLocation(), |
| 2861 | name && name[0] ? &ast_context->Idents.get(name) : NULL, |
| 2862 | SourceLocation(), |
| 2863 | NULL); |
| 2864 | if (enum_decl) |
Greg Clayton | e37f23c | 2010-09-12 23:17:56 +0000 | [diff] [blame] | 2865 | { |
| 2866 | // TODO: check if we should be setting the promotion type too? |
| 2867 | enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2868 | return ast_context->getTagDeclType(enum_decl).getAsOpaquePtr(); |
Greg Clayton | e37f23c | 2010-09-12 23:17:56 +0000 | [diff] [blame] | 2869 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2870 | return NULL; |
| 2871 | } |
| 2872 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2873 | clang_type_t |
| 2874 | ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type) |
| 2875 | { |
| 2876 | QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); |
| 2877 | |
| 2878 | clang::Type *clang_type = enum_qual_type.getTypePtr(); |
| 2879 | if (clang_type) |
| 2880 | { |
| 2881 | const EnumType *enum_type = dyn_cast<EnumType>(clang_type); |
| 2882 | if (enum_type) |
| 2883 | { |
| 2884 | EnumDecl *enum_decl = enum_type->getDecl(); |
| 2885 | if (enum_decl) |
| 2886 | return enum_decl->getIntegerType().getAsOpaquePtr(); |
| 2887 | } |
| 2888 | } |
| 2889 | return NULL; |
| 2890 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2891 | bool |
| 2892 | ClangASTContext::AddEnumerationValueToEnumerationType |
| 2893 | ( |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2894 | clang_type_t enum_clang_type, |
| 2895 | clang_type_t enumerator_clang_type, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2896 | const Declaration &decl, |
| 2897 | const char *name, |
| 2898 | int64_t enum_value, |
| 2899 | uint32_t enum_value_bit_size |
| 2900 | ) |
| 2901 | { |
| 2902 | if (enum_clang_type && enumerator_clang_type && name) |
| 2903 | { |
| 2904 | // TODO: Do something intelligent with the Declaration object passed in |
| 2905 | // like maybe filling in the SourceLocation with it... |
| 2906 | ASTContext *ast_context = getASTContext(); |
| 2907 | IdentifierTable *identifier_table = getIdentifierTable(); |
| 2908 | |
| 2909 | assert (ast_context != NULL); |
| 2910 | assert (identifier_table != NULL); |
| 2911 | QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); |
| 2912 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 2913 | clang::Type *clang_type = enum_qual_type.getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2914 | if (clang_type) |
| 2915 | { |
| 2916 | const EnumType *enum_type = dyn_cast<EnumType>(clang_type); |
| 2917 | |
| 2918 | if (enum_type) |
| 2919 | { |
| 2920 | llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); |
| 2921 | enum_llvm_apsint = enum_value; |
| 2922 | EnumConstantDecl *enumerator_decl = |
| 2923 | EnumConstantDecl::Create(*ast_context, |
| 2924 | enum_type->getDecl(), |
| 2925 | SourceLocation(), |
| 2926 | name ? &identifier_table->get(name) : NULL, // Identifier |
| 2927 | QualType::getFromOpaquePtr(enumerator_clang_type), |
| 2928 | NULL, |
| 2929 | enum_llvm_apsint); |
| 2930 | |
| 2931 | if (enumerator_decl) |
| 2932 | { |
| 2933 | enum_type->getDecl()->addDecl(enumerator_decl); |
| 2934 | return true; |
| 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | } |
| 2939 | return false; |
| 2940 | } |
| 2941 | |
| 2942 | #pragma mark Pointers & References |
| 2943 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2944 | clang_type_t |
| 2945 | ClangASTContext::CreatePointerType (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2946 | { |
| 2947 | if (clang_type) |
Greg Clayton | 7b54103 | 2010-07-29 20:06:32 +0000 | [diff] [blame] | 2948 | { |
| 2949 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 2950 | |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 2951 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2952 | switch (type_class) |
Greg Clayton | 7b54103 | 2010-07-29 20:06:32 +0000 | [diff] [blame] | 2953 | { |
| 2954 | case clang::Type::ObjCObject: |
| 2955 | case clang::Type::ObjCInterface: |
Greg Clayton | 7b54103 | 2010-07-29 20:06:32 +0000 | [diff] [blame] | 2956 | return getASTContext()->getObjCObjectPointerType(qual_type).getAsOpaquePtr(); |
| 2957 | |
Greg Clayton | 7b54103 | 2010-07-29 20:06:32 +0000 | [diff] [blame] | 2958 | default: |
| 2959 | return getASTContext()->getPointerType(qual_type).getAsOpaquePtr(); |
| 2960 | } |
| 2961 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2962 | return NULL; |
| 2963 | } |
| 2964 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2965 | clang_type_t |
| 2966 | ClangASTContext::CreateLValueReferenceType (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2967 | { |
| 2968 | if (clang_type) |
| 2969 | return getASTContext()->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); |
| 2970 | return NULL; |
| 2971 | } |
| 2972 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2973 | clang_type_t |
| 2974 | ClangASTContext::CreateRValueReferenceType (clang_type_t clang_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2975 | { |
| 2976 | if (clang_type) |
| 2977 | return getASTContext()->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2981 | clang_type_t |
| 2982 | ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type) |
Greg Clayton | fa97069 | 2010-06-12 01:20:30 +0000 | [diff] [blame] | 2983 | { |
| 2984 | if (clang_pointee_type && clang_pointee_type) |
| 2985 | return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), |
| 2986 | QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2990 | size_t |
| 2991 | ClangASTContext::GetPointerBitSize () |
| 2992 | { |
| 2993 | ASTContext *ast_context = getASTContext(); |
| 2994 | return ast_context->getTypeSize(ast_context->VoidPtrTy); |
| 2995 | } |
| 2996 | |
| 2997 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 2998 | ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2999 | { |
| 3000 | if (clang_type == NULL) |
| 3001 | return false; |
| 3002 | |
| 3003 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 3004 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3005 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3006 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3007 | case clang::Type::ObjCObjectPointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3008 | if (target_type) |
| 3009 | *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3010 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3011 | case clang::Type::BlockPointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3012 | if (target_type) |
| 3013 | *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3014 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3015 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3016 | if (target_type) |
| 3017 | *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3018 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3019 | case clang::Type::MemberPointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3020 | if (target_type) |
| 3021 | *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3022 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3023 | case clang::Type::LValueReference: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3024 | if (target_type) |
| 3025 | *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); |
| 3026 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3027 | case clang::Type::RValueReference: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3028 | if (target_type) |
| 3029 | *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); |
| 3030 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3031 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3032 | return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); |
| 3033 | default: |
| 3034 | break; |
| 3035 | } |
| 3036 | return false; |
| 3037 | } |
| 3038 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3039 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3040 | ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3041 | { |
| 3042 | if (!clang_type) |
| 3043 | return false; |
| 3044 | |
| 3045 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3046 | const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); |
| 3047 | |
| 3048 | if (builtin_type) |
| 3049 | { |
| 3050 | if (builtin_type->isInteger()) |
| 3051 | is_signed = builtin_type->isSignedInteger(); |
| 3052 | |
| 3053 | return true; |
| 3054 | } |
| 3055 | |
| 3056 | return false; |
| 3057 | } |
| 3058 | |
| 3059 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3060 | ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t*target_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3061 | { |
| 3062 | if (clang_type) |
| 3063 | { |
| 3064 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 3065 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3066 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3067 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3068 | case clang::Type::ObjCObjectPointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3069 | if (target_type) |
| 3070 | *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3071 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3072 | case clang::Type::BlockPointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3073 | if (target_type) |
| 3074 | *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3075 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3076 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3077 | if (target_type) |
| 3078 | *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3079 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3080 | case clang::Type::MemberPointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3081 | if (target_type) |
| 3082 | *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); |
| 3083 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3084 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3085 | return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), target_type); |
| 3086 | default: |
| 3087 | break; |
| 3088 | } |
| 3089 | } |
| 3090 | return false; |
| 3091 | } |
| 3092 | |
| 3093 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3094 | ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3095 | { |
| 3096 | if (clang_type) |
| 3097 | { |
| 3098 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3099 | |
| 3100 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) |
| 3101 | { |
| 3102 | clang::BuiltinType::Kind kind = BT->getKind(); |
| 3103 | if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) |
| 3104 | { |
| 3105 | count = 1; |
| 3106 | is_complex = false; |
| 3107 | return true; |
| 3108 | } |
| 3109 | } |
| 3110 | else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) |
| 3111 | { |
| 3112 | if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) |
| 3113 | { |
| 3114 | count = 2; |
| 3115 | is_complex = true; |
| 3116 | return true; |
| 3117 | } |
| 3118 | } |
| 3119 | else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) |
| 3120 | { |
| 3121 | if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) |
| 3122 | { |
| 3123 | count = VT->getNumElements(); |
| 3124 | is_complex = false; |
| 3125 | return true; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
| 3129 | return false; |
| 3130 | } |
| 3131 | |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 3132 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3133 | ClangASTContext::IsCXXClassType (clang_type_t clang_type) |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 3134 | { |
| 3135 | if (clang_type) |
| 3136 | { |
| 3137 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3138 | if (qual_type->getAsCXXRecordDecl() != NULL) |
| 3139 | return true; |
| 3140 | } |
| 3141 | return false; |
| 3142 | } |
| 3143 | |
| 3144 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3145 | ClangASTContext::IsObjCClassType (clang_type_t clang_type) |
Greg Clayton | 1d8173f | 2010-09-24 05:15:53 +0000 | [diff] [blame] | 3146 | { |
| 3147 | if (clang_type) |
| 3148 | { |
| 3149 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3150 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 3151 | return true; |
| 3152 | } |
| 3153 | return false; |
| 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3158 | |
| 3159 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3160 | ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3161 | { |
| 3162 | if (clang_type) |
| 3163 | { |
| 3164 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 3165 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3166 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3167 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3168 | case clang::Type::ConstantArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3169 | { |
| 3170 | ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr()); |
| 3171 | QualType element_qual_type = array->getElementType(); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3172 | clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3173 | if (canonical_type && canonical_type->isCharType()) |
| 3174 | { |
| 3175 | // We know the size of the array and it could be a C string |
| 3176 | // since it is an array of characters |
| 3177 | length = array->getSize().getLimitedValue(); |
| 3178 | return true; |
| 3179 | } |
| 3180 | } |
| 3181 | break; |
| 3182 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3183 | case clang::Type::Pointer: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3184 | { |
| 3185 | PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3186 | clang::Type *pointee_type_ptr = pointer_type->getPointeeType().getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3187 | if (pointee_type_ptr) |
| 3188 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3189 | clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3190 | length = 0; // No length info, read until a NULL terminator is received |
| 3191 | if (canonical_type_ptr) |
| 3192 | return canonical_type_ptr->isCharType(); |
| 3193 | else |
| 3194 | return pointee_type_ptr->isCharType(); |
| 3195 | } |
| 3196 | } |
| 3197 | break; |
| 3198 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3199 | case clang::Type::Typedef: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3200 | return ClangASTContext::IsCStringType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), length); |
| 3201 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3202 | case clang::Type::LValueReference: |
| 3203 | case clang::Type::RValueReference: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3204 | { |
| 3205 | ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3206 | clang::Type *pointee_type_ptr = reference_type->getPointeeType().getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3207 | if (pointee_type_ptr) |
| 3208 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3209 | clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3210 | length = 0; // No length info, read until a NULL terminator is received |
| 3211 | if (canonical_type_ptr) |
| 3212 | return canonical_type_ptr->isCharType(); |
| 3213 | else |
| 3214 | return pointee_type_ptr->isCharType(); |
| 3215 | } |
| 3216 | } |
| 3217 | break; |
| 3218 | } |
| 3219 | } |
| 3220 | return false; |
| 3221 | } |
| 3222 | |
| 3223 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3224 | ClangASTContext::IsFunctionPointerType (clang_type_t clang_type) |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 3225 | { |
| 3226 | if (clang_type) |
| 3227 | { |
| 3228 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3229 | |
| 3230 | if (qual_type->isFunctionPointerType()) |
| 3231 | return true; |
| 3232 | |
| 3233 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3234 | switch (type_class) |
| 3235 | { |
| 3236 | case clang::Type::Typedef: |
| 3237 | return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); |
| 3238 | |
| 3239 | case clang::Type::LValueReference: |
| 3240 | case clang::Type::RValueReference: |
| 3241 | { |
| 3242 | ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); |
| 3243 | if (reference_type) |
| 3244 | return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr()); |
| 3245 | } |
| 3246 | break; |
| 3247 | } |
| 3248 | } |
| 3249 | return false; |
| 3250 | } |
| 3251 | |
| 3252 | |
| 3253 | |
| 3254 | |
| 3255 | bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3256 | ClangASTContext::IsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3257 | { |
| 3258 | if (!clang_type) |
| 3259 | return false; |
| 3260 | |
| 3261 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3262 | |
Greg Clayton | 03e0f97 | 2010-09-13 03:32:57 +0000 | [diff] [blame] | 3263 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3264 | switch (type_class) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3265 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3266 | case clang::Type::ConstantArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3267 | if (member_type) |
| 3268 | *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); |
| 3269 | if (size) |
| 3270 | *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULONG_LONG_MAX); |
| 3271 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3272 | case clang::Type::IncompleteArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3273 | if (member_type) |
| 3274 | *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); |
| 3275 | if (size) |
| 3276 | *size = 0; |
| 3277 | return true; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3278 | case clang::Type::VariableArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3279 | if (member_type) |
| 3280 | *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); |
| 3281 | if (size) |
| 3282 | *size = 0; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 3283 | case clang::Type::DependentSizedArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3284 | if (member_type) |
| 3285 | *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); |
| 3286 | if (size) |
| 3287 | *size = 0; |
| 3288 | return true; |
| 3289 | } |
| 3290 | return false; |
| 3291 | } |
| 3292 | |
| 3293 | |
| 3294 | #pragma mark Typedefs |
| 3295 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3296 | clang_type_t |
| 3297 | ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3298 | { |
| 3299 | if (clang_type) |
| 3300 | { |
| 3301 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3302 | ASTContext *ast_context = getASTContext(); |
| 3303 | IdentifierTable *identifier_table = getIdentifierTable(); |
| 3304 | assert (ast_context != NULL); |
| 3305 | assert (identifier_table != NULL); |
| 3306 | if (decl_ctx == NULL) |
| 3307 | decl_ctx = ast_context->getTranslationUnitDecl(); |
| 3308 | TypedefDecl *decl = TypedefDecl::Create(*ast_context, |
| 3309 | decl_ctx, |
| 3310 | SourceLocation(), |
| 3311 | name ? &identifier_table->get(name) : NULL, // Identifier |
| 3312 | ast_context->CreateTypeSourceInfo(qual_type)); |
| 3313 | |
| 3314 | // Get a uniqued QualType for the typedef decl type |
| 3315 | return ast_context->getTypedefType (decl).getAsOpaquePtr(); |
| 3316 | } |
| 3317 | return NULL; |
| 3318 | } |
| 3319 | |
| 3320 | |
| 3321 | std::string |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3322 | ClangASTContext::GetTypeName (clang_type_t opaque_qual_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3323 | { |
| 3324 | std::string return_name; |
| 3325 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3326 | QualType qual_type(QualType::getFromOpaquePtr(opaque_qual_type)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3327 | |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3328 | const TypedefType *typedef_type = qual_type->getAs<TypedefType>(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3329 | if (typedef_type) |
| 3330 | { |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3331 | const TypedefDecl *typedef_decl = typedef_type->getDecl(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3332 | return_name = typedef_decl->getQualifiedNameAsString(); |
| 3333 | } |
| 3334 | else |
| 3335 | { |
| 3336 | return_name = qual_type.getAsString(); |
| 3337 | } |
| 3338 | |
| 3339 | return return_name; |
| 3340 | } |
| 3341 | |
| 3342 | // Disable this for now since I can't seem to get a nicely formatted float |
| 3343 | // out of the APFloat class without just getting the float, double or quad |
| 3344 | // and then using a formatted print on it which defeats the purpose. We ideally |
| 3345 | // would like to get perfect string values for any kind of float semantics |
| 3346 | // so we can support remote targets. The code below also requires a patch to |
| 3347 | // llvm::APInt. |
| 3348 | //bool |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3349 | //ClangASTContext::ConvertFloatValueToString (ASTContext *ast_context, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3350 | //{ |
| 3351 | // uint32_t count = 0; |
| 3352 | // bool is_complex = false; |
| 3353 | // if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) |
| 3354 | // { |
| 3355 | // unsigned num_bytes_per_float = byte_size / count; |
| 3356 | // unsigned num_bits_per_float = num_bytes_per_float * 8; |
| 3357 | // |
| 3358 | // float_str.clear(); |
| 3359 | // uint32_t i; |
| 3360 | // for (i=0; i<count; i++) |
| 3361 | // { |
| 3362 | // APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); |
| 3363 | // bool is_ieee = false; |
| 3364 | // APFloat ap_float(ap_int, is_ieee); |
| 3365 | // char s[1024]; |
| 3366 | // unsigned int hex_digits = 0; |
| 3367 | // bool upper_case = false; |
| 3368 | // |
| 3369 | // if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) |
| 3370 | // { |
| 3371 | // if (i > 0) |
| 3372 | // float_str.append(", "); |
| 3373 | // float_str.append(s); |
| 3374 | // if (i == 1 && is_complex) |
| 3375 | // float_str.append(1, 'i'); |
| 3376 | // } |
| 3377 | // } |
| 3378 | // return !float_str.empty(); |
| 3379 | // } |
| 3380 | // return false; |
| 3381 | //} |
| 3382 | |
| 3383 | size_t |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3384 | ClangASTContext::ConvertStringToFloatValue (ASTContext *ast_context, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3385 | { |
| 3386 | if (clang_type) |
| 3387 | { |
| 3388 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3389 | uint32_t count = 0; |
| 3390 | bool is_complex = false; |
| 3391 | if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) |
| 3392 | { |
| 3393 | // TODO: handle complex and vector types |
| 3394 | if (count != 1) |
| 3395 | return false; |
| 3396 | |
| 3397 | StringRef s_sref(s); |
| 3398 | APFloat ap_float(ast_context->getFloatTypeSemantics(qual_type), s_sref); |
| 3399 | |
| 3400 | const uint64_t bit_size = ast_context->getTypeSize (qual_type); |
| 3401 | const uint64_t byte_size = bit_size / 8; |
| 3402 | if (dst_size >= byte_size) |
| 3403 | { |
| 3404 | if (bit_size == sizeof(float)*8) |
| 3405 | { |
| 3406 | float float32 = ap_float.convertToFloat(); |
| 3407 | ::memcpy (dst, &float32, byte_size); |
| 3408 | return byte_size; |
| 3409 | } |
| 3410 | else if (bit_size >= 64) |
| 3411 | { |
| 3412 | llvm::APInt ap_int(ap_float.bitcastToAPInt()); |
| 3413 | ::memcpy (dst, ap_int.getRawData(), byte_size); |
| 3414 | return byte_size; |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | return 0; |
| 3420 | } |
Sean Callanan | a751f7b | 2010-09-17 02:24:29 +0000 | [diff] [blame] | 3421 | |
| 3422 | unsigned |
Greg Clayton | 462d414 | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 3423 | ClangASTContext::GetTypeQualifiers(clang_type_t clang_type) |
Sean Callanan | a751f7b | 2010-09-17 02:24:29 +0000 | [diff] [blame] | 3424 | { |
| 3425 | assert (clang_type); |
| 3426 | |
| 3427 | QualType qual_type (QualType::getFromOpaquePtr(clang_type)); |
| 3428 | |
| 3429 | return qual_type.getQualifiers().getCVRQualifiers(); |
| 3430 | } |