1 //===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/ClangASTContext.h"
16 // Other libraries and framework includes
18 // Clang headers like to use NDEBUG inside of them to enable/disable debug
19 // releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
20 // or another. This is bad because it means that if clang was built in release
21 // mode, it assumes that you are building in release mode which is not always
22 // the case. You can end up with functions that are defined as empty in header
23 // files when NDEBUG is not defined, and this can cause link errors with the
24 // clang .a files that you have since you might be missing functions in the .a
25 // file. So we have to define NDEBUG when including clang headers to avoid any
26 // mismatches. This is covered by rdar://problem/8691220
28 #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
29 #define LLDB_DEFINED_NDEBUG_FOR_CLANG
31 // Need to include assert.h so it is as clang would expect it to be (disabled)
35 #include "clang/AST/ASTContext.h"
36 #include "clang/AST/ASTImporter.h"
37 #include "clang/AST/Attr.h"
38 #include "clang/AST/CXXInheritance.h"
39 #include "clang/AST/DeclObjC.h"
40 #include "clang/AST/DeclTemplate.h"
41 #include "clang/AST/RecordLayout.h"
42 #include "clang/AST/Type.h"
43 #include "clang/Basic/Builtins.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/FileManager.h"
46 #include "clang/Basic/FileSystemOptions.h"
47 #include "clang/Basic/SourceManager.h"
48 #include "clang/Basic/TargetInfo.h"
49 #include "clang/Basic/TargetOptions.h"
50 #include "clang/Frontend/FrontendOptions.h"
51 #include "clang/Frontend/LangStandard.h"
53 #ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
55 #undef LLDB_DEFINED_NDEBUG_FOR_CLANG
56 // Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
60 #include "lldb/Core/ArchSpec.h"
61 #include "lldb/Core/dwarf.h"
62 #include "lldb/Core/Flags.h"
63 #include "lldb/Core/Log.h"
64 #include "lldb/Core/RegularExpression.h"
65 #include "lldb/Core/UniqueCStringMap.h"
66 #include "lldb/Expression/ASTDumper.h"
67 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
68 #include "lldb/Symbol/VerifyDecl.h"
69 #include "lldb/Target/ExecutionContext.h"
70 #include "lldb/Target/Process.h"
71 #include "lldb/Target/ObjCLanguageRuntime.h"
78 using namespace lldb_private;
80 using namespace clang;
82 clang::AccessSpecifier
83 ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
88 case eAccessNone: return AS_none;
89 case eAccessPublic: return AS_public;
90 case eAccessPrivate: return AS_private;
91 case eAccessProtected: return AS_protected;
104 // FIXME: Cleanup per-file based stuff.
106 // Set some properties which depend soley on the input kind; it would be nice
107 // to move these to the language standard, and have the driver resolve the
108 // input kind + language standard.
110 Opts.AsmPreprocessor = 1;
111 } else if (IK == IK_ObjC ||
113 IK == IK_PreprocessedObjC ||
114 IK == IK_PreprocessedObjCXX) {
115 Opts.ObjC1 = Opts.ObjC2 = 1;
118 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
120 if (LangStd == LangStandard::lang_unspecified) {
121 // Based on the base language, pick one.
126 assert (!"Invalid input kind!");
128 LangStd = LangStandard::lang_opencl;
131 LangStd = LangStandard::lang_cuda;
135 case IK_PreprocessedC:
137 case IK_PreprocessedObjC:
138 LangStd = LangStandard::lang_gnu99;
141 case IK_PreprocessedCXX:
143 case IK_PreprocessedObjCXX:
144 LangStd = LangStandard::lang_gnucxx98;
149 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
150 Opts.LineComment = Std.hasLineComments();
151 Opts.C99 = Std.isC99();
152 Opts.CPlusPlus = Std.isCPlusPlus();
153 Opts.CPlusPlus11 = Std.isCPlusPlus11();
154 Opts.Digraphs = Std.hasDigraphs();
155 Opts.GNUMode = Std.isGNUMode();
156 Opts.GNUInline = !Std.isC99();
157 Opts.HexFloats = Std.hasHexFloats();
158 Opts.ImplicitInt = Std.hasImplicitInt();
162 // OpenCL has some additional defaults.
163 if (LangStd == LangStandard::lang_opencl) {
166 Opts.CXXOperatorNames = 1;
167 Opts.LaxVectorConversions = 1;
170 // OpenCL and C++ both have bool, true, false keywords.
171 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
173 // if (Opts.CPlusPlus)
174 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
176 // if (Args.hasArg(OPT_fobjc_gc_only))
177 // Opts.setGCMode(LangOptions::GCOnly);
178 // else if (Args.hasArg(OPT_fobjc_gc))
179 // Opts.setGCMode(LangOptions::HybridGC);
181 // if (Args.hasArg(OPT_print_ivar_layout))
182 // Opts.ObjCGCBitmapPrint = 1;
184 // if (Args.hasArg(OPT_faltivec))
187 // if (Args.hasArg(OPT_pthread))
188 // Opts.POSIXThreads = 1;
190 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
192 // if (Vis == "default")
193 Opts.setValueVisibilityMode(DefaultVisibility);
194 // else if (Vis == "hidden")
195 // Opts.setVisibilityMode(LangOptions::Hidden);
196 // else if (Vis == "protected")
197 // Opts.setVisibilityMode(LangOptions::Protected);
199 // Diags.Report(diag::err_drv_invalid_value)
200 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
202 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
204 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
205 // is specified, or -std is set to a conforming mode.
206 Opts.Trigraphs = !Opts.GNUMode;
207 // if (Args.hasArg(OPT_trigraphs))
208 // Opts.Trigraphs = 1;
210 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
211 // OPT_fno_dollars_in_identifiers,
212 // !Opts.AsmPreprocessor);
213 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
214 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
215 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
216 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
217 // Opts.LaxVectorConversions = 0;
218 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
219 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
220 // Opts.Blocks = Args.hasArg(OPT_fblocks);
221 // Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
222 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
223 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
224 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
225 // Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
226 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
227 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
228 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
229 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
230 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
232 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
233 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
234 // OPT_fconstant_string_class);
235 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
236 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
237 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
238 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
239 // Opts.Static = Args.hasArg(OPT_static_define);
240 Opts.OptimizeSize = 0;
242 // FIXME: Eliminate this dependency.
244 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
245 // Opts.Optimize = Opt != 0;
248 // This is the __NO_INLINE__ define, which just depends on things like the
249 // optimization level and -fno-inline, not actually whether the backend has
252 // FIXME: This is affected by other options (-fno-inline).
253 Opts.NoInlineDefine = !Opt;
255 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
258 // Diags.Report(diag::err_drv_invalid_value)
259 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
261 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
262 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
263 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
268 ClangASTContext::ClangASTContext (const char *target_triple) :
271 m_language_options_ap(),
272 m_source_manager_ap(),
273 m_diagnostics_engine_ap(),
274 m_target_options_rp(),
276 m_identifier_table_ap(),
277 m_selector_table_ap(),
279 m_callback_tag_decl (NULL),
280 m_callback_objc_decl (NULL),
281 m_callback_baton (NULL),
282 m_pointer_byte_size (0)
285 if (target_triple && target_triple[0])
286 SetTargetTriple (target_triple);
289 //----------------------------------------------------------------------
291 //----------------------------------------------------------------------
292 ClangASTContext::~ClangASTContext()
294 m_builtins_ap.reset();
295 m_selector_table_ap.reset();
296 m_identifier_table_ap.reset();
297 m_target_info_ap.reset();
298 m_target_options_rp.reset();
299 m_diagnostics_engine_ap.reset();
300 m_source_manager_ap.reset();
301 m_language_options_ap.reset();
307 ClangASTContext::Clear()
310 m_language_options_ap.reset();
311 m_source_manager_ap.reset();
312 m_diagnostics_engine_ap.reset();
313 m_target_options_rp.reset();
314 m_target_info_ap.reset();
315 m_identifier_table_ap.reset();
316 m_selector_table_ap.reset();
317 m_builtins_ap.reset();
318 m_pointer_byte_size = 0;
322 ClangASTContext::GetTargetTriple ()
324 return m_target_triple.c_str();
328 ClangASTContext::SetTargetTriple (const char *target_triple)
331 m_target_triple.assign(target_triple);
335 ClangASTContext::SetArchitecture (const ArchSpec &arch)
337 SetTargetTriple(arch.GetTriple().str().c_str());
341 ClangASTContext::HasExternalSource ()
343 ASTContext *ast = getASTContext();
345 return ast->getExternalSource () != NULL;
350 ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
352 ASTContext *ast = getASTContext();
355 ast->setExternalSource (ast_source_ap);
356 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
357 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
362 ClangASTContext::RemoveExternalSource ()
364 ASTContext *ast = getASTContext();
368 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
369 ast->setExternalSource (empty_ast_source_ap);
370 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
371 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
378 ClangASTContext::getASTContext()
380 if (m_ast_ap.get() == NULL)
382 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
385 *getIdentifierTable(),
387 *getBuiltinContext(),
390 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
392 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
393 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
396 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
398 return m_ast_ap.get();
402 ClangASTContext::getBuiltinContext()
404 if (m_builtins_ap.get() == NULL)
405 m_builtins_ap.reset (new Builtin::Context());
406 return m_builtins_ap.get();
410 ClangASTContext::getIdentifierTable()
412 if (m_identifier_table_ap.get() == NULL)
413 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
414 return m_identifier_table_ap.get();
418 ClangASTContext::getLanguageOptions()
420 if (m_language_options_ap.get() == NULL)
422 m_language_options_ap.reset(new LangOptions());
423 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
424 // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
426 return m_language_options_ap.get();
430 ClangASTContext::getSelectorTable()
432 if (m_selector_table_ap.get() == NULL)
433 m_selector_table_ap.reset (new SelectorTable());
434 return m_selector_table_ap.get();
438 ClangASTContext::getFileManager()
440 if (m_file_manager_ap.get() == NULL)
442 clang::FileSystemOptions file_system_options;
443 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
445 return m_file_manager_ap.get();
448 clang::SourceManager *
449 ClangASTContext::getSourceManager()
451 if (m_source_manager_ap.get() == NULL)
452 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
453 return m_source_manager_ap.get();
456 clang::DiagnosticsEngine *
457 ClangASTContext::getDiagnosticsEngine()
459 if (m_diagnostics_engine_ap.get() == NULL)
461 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
462 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
464 return m_diagnostics_engine_ap.get();
467 class NullDiagnosticConsumer : public DiagnosticConsumer
470 NullDiagnosticConsumer ()
472 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
475 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
479 llvm::SmallVector<char, 32> diag_str(10);
480 info.FormatDiagnostic(diag_str);
481 diag_str.push_back('\0');
482 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
486 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
488 return new NullDiagnosticConsumer ();
495 ClangASTContext::getDiagnosticConsumer()
497 if (m_diagnostic_consumer_ap.get() == NULL)
498 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
500 return m_diagnostic_consumer_ap.get();
504 ClangASTContext::getTargetOptions()
506 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
508 m_target_options_rp.reset ();
509 m_target_options_rp = new TargetOptions();
510 if (m_target_options_rp.getPtr() != NULL)
511 m_target_options_rp->Triple = m_target_triple;
513 return m_target_options_rp.getPtr();
518 ClangASTContext::getTargetInfo()
520 // target_triple should be something like "x86_64-apple-macosx"
521 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
522 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
523 return m_target_info_ap.get();
526 #pragma mark Basic Types
529 QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
531 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
532 if (qual_type_bit_size == bit_size)
537 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
539 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
543 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
546 return ClangASTType();
550 case eEncodingInvalid:
551 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
552 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr());
556 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
557 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
558 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
559 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
560 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
561 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
562 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
563 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
564 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
565 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
566 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
567 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
571 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
572 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
573 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
574 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
575 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
576 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
577 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
578 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
579 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
580 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
581 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
582 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
585 case eEncodingIEEE754:
586 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
587 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr());
588 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
589 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr());
590 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
591 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr());
594 case eEncodingVector:
595 // Sanity check that bit_size is a multiple of 8's.
596 if (bit_size && !(bit_size & 0x7u))
597 return ClangASTType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr());
601 return ClangASTType();
607 ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
611 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
612 static TypeNameToBasicTypeMap g_type_map;
613 static std::once_flag g_once_flag;
614 std::call_once(g_once_flag, [](){
616 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
619 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
620 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
621 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
622 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
623 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
624 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
626 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
627 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
628 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
629 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
632 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
633 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
634 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
635 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
638 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
639 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
640 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
641 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
644 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
645 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
646 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
647 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
650 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
651 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
654 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
655 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
656 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
657 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
658 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
659 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
660 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
664 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
666 return eBasicTypeInvalid;
670 ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
674 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
675 return ClangASTContext::GetBasicType (ast, basic_type);
677 return ClangASTType();
681 ClangASTContext::GetPointerByteSize ()
683 if (m_pointer_byte_size == 0)
684 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize();
685 return m_pointer_byte_size;
689 ClangASTContext::GetBasicType (lldb::BasicType basic_type)
691 return GetBasicType (getASTContext(), basic_type);
695 ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
699 clang_type_t clang_type = NULL;
703 case eBasicTypeInvalid:
704 case eBasicTypeOther:
707 clang_type = ast->VoidTy.getAsOpaquePtr();
710 clang_type = ast->CharTy.getAsOpaquePtr();
712 case eBasicTypeSignedChar:
713 clang_type = ast->SignedCharTy.getAsOpaquePtr();
715 case eBasicTypeUnsignedChar:
716 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
718 case eBasicTypeWChar:
719 clang_type = ast->getWCharType().getAsOpaquePtr();
721 case eBasicTypeSignedWChar:
722 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
724 case eBasicTypeUnsignedWChar:
725 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
727 case eBasicTypeChar16:
728 clang_type = ast->Char16Ty.getAsOpaquePtr();
730 case eBasicTypeChar32:
731 clang_type = ast->Char32Ty.getAsOpaquePtr();
733 case eBasicTypeShort:
734 clang_type = ast->ShortTy.getAsOpaquePtr();
736 case eBasicTypeUnsignedShort:
737 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
740 clang_type = ast->IntTy.getAsOpaquePtr();
742 case eBasicTypeUnsignedInt:
743 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
746 clang_type = ast->LongTy.getAsOpaquePtr();
748 case eBasicTypeUnsignedLong:
749 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
751 case eBasicTypeLongLong:
752 clang_type = ast->LongLongTy.getAsOpaquePtr();
754 case eBasicTypeUnsignedLongLong:
755 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
757 case eBasicTypeInt128:
758 clang_type = ast->Int128Ty.getAsOpaquePtr();
760 case eBasicTypeUnsignedInt128:
761 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
764 clang_type = ast->BoolTy.getAsOpaquePtr();
767 clang_type = ast->HalfTy.getAsOpaquePtr();
769 case eBasicTypeFloat:
770 clang_type = ast->FloatTy.getAsOpaquePtr();
772 case eBasicTypeDouble:
773 clang_type = ast->DoubleTy.getAsOpaquePtr();
775 case eBasicTypeLongDouble:
776 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
778 case eBasicTypeFloatComplex:
779 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
781 case eBasicTypeDoubleComplex:
782 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
784 case eBasicTypeLongDoubleComplex:
785 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
787 case eBasicTypeObjCID:
788 clang_type = ast->getObjCIdType().getAsOpaquePtr();
790 case eBasicTypeObjCClass:
791 clang_type = ast->getObjCClassType().getAsOpaquePtr();
793 case eBasicTypeObjCSel:
794 clang_type = ast->getObjCSelType().getAsOpaquePtr();
796 case eBasicTypeNullPtr:
797 clang_type = ast->NullPtrTy.getAsOpaquePtr();
802 return ClangASTType (ast, clang_type);
804 return ClangASTType();
809 ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
811 ASTContext *ast = getASTContext();
813 #define streq(a,b) strcmp(a,b) == 0
814 assert (ast != NULL);
823 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
824 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr());
828 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
829 return ClangASTType (ast, ast->BoolTy.getAsOpaquePtr());
830 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
831 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
832 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
833 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
834 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
835 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
839 // This has been seen to mean DW_AT_complex_integer
842 if (::strstr(type_name, "complex"))
844 ClangASTType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
845 return ClangASTType (ast, ast->getComplexType (complex_int_clang_type.GetQualType()).getAsOpaquePtr());
850 case DW_ATE_complex_float:
851 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
852 return ClangASTType (ast, ast->FloatComplexTy.getAsOpaquePtr());
853 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
854 return ClangASTType (ast, ast->DoubleComplexTy.getAsOpaquePtr());
855 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
856 return ClangASTType (ast, ast->LongDoubleComplexTy.getAsOpaquePtr());
859 ClangASTType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
860 return ClangASTType (ast, ast->getComplexType (complex_float_clang_type.GetQualType()).getAsOpaquePtr());
865 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
866 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr());
867 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
868 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr());
869 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
870 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr());
876 if (streq(type_name, "wchar_t") &&
877 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
878 return ClangASTType (ast, ast->WCharTy.getAsOpaquePtr());
879 if (streq(type_name, "void") &&
880 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
881 return ClangASTType (ast, ast->VoidTy.getAsOpaquePtr());
882 if (strstr(type_name, "long long") &&
883 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
884 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
885 if (strstr(type_name, "long") &&
886 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
887 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
888 if (strstr(type_name, "short") &&
889 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
890 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
891 if (strstr(type_name, "char"))
893 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
894 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
895 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
896 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
898 if (strstr(type_name, "int"))
900 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
901 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
902 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
903 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
906 // We weren't able to match up a type name, just search by size
907 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
908 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
909 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
910 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
911 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
912 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
913 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
914 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
915 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
916 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
917 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
918 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
921 case DW_ATE_signed_char:
924 if (streq(type_name, "signed char"))
926 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
927 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
930 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
931 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
932 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
933 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
936 case DW_ATE_unsigned:
939 if (strstr(type_name, "long long"))
941 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
942 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
944 else if (strstr(type_name, "long"))
946 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
947 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
949 else if (strstr(type_name, "short"))
951 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
952 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
954 else if (strstr(type_name, "char"))
956 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
957 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
959 else if (strstr(type_name, "int"))
961 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
962 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
963 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
964 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
967 // We weren't able to match up a type name, just search by size
968 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
969 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
970 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
971 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
972 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
973 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
974 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
975 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
976 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
977 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
978 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
979 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
982 case DW_ATE_unsigned_char:
983 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
984 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
985 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
986 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
989 case DW_ATE_imaginary_float:
995 if (streq(type_name, "char16_t"))
997 return ClangASTType (ast, ast->Char16Ty.getAsOpaquePtr());
999 else if (streq(type_name, "char32_t"))
1001 return ClangASTType (ast, ast->Char32Ty.getAsOpaquePtr());
1007 // This assert should fire for anything that we don't catch above so we know
1008 // to fix any issues we run into.
1011 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size);
1015 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size);
1017 return ClangASTType ();
1021 ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1024 return ClangASTType (ast, ast->UnknownAnyTy.getAsOpaquePtr());
1025 return ClangASTType();
1029 ClangASTContext::GetCStringType (bool is_const)
1031 ASTContext *ast = getASTContext();
1032 QualType char_type(ast->CharTy);
1035 char_type.addConst();
1037 return ClangASTType (ast, ast->getPointerType(char_type).getAsOpaquePtr());
1040 clang::DeclContext *
1041 ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1043 return ast->getTranslationUnitDecl();
1047 ClangASTContext::CopyType (ASTContext *dst_ast,
1050 FileSystemOptions file_system_options;
1051 ASTContext *src_ast = src.GetASTContext();
1052 FileManager file_manager (file_system_options);
1053 ASTImporter importer(*dst_ast, file_manager,
1054 *src_ast, file_manager,
1057 QualType dst (importer.Import(src.GetQualType()));
1059 return ClangASTType (dst_ast, dst.getAsOpaquePtr());
1064 ClangASTContext::CopyDecl (ASTContext *dst_ast,
1065 ASTContext *src_ast,
1066 clang::Decl *source_decl)
1068 FileSystemOptions file_system_options;
1069 FileManager file_manager (file_system_options);
1070 ASTImporter importer(*dst_ast, file_manager,
1071 *src_ast, file_manager,
1074 return importer.Import(source_decl);
1078 ClangASTContext::AreTypesSame (ClangASTType type1,
1080 bool ignore_qualifiers)
1082 ASTContext *ast = type1.GetASTContext();
1083 if (ast != type2.GetASTContext())
1086 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1089 QualType type1_qual = type1.GetQualType();
1090 QualType type2_qual = type2.GetQualType();
1092 if (ignore_qualifiers)
1094 type1_qual = type1_qual.getUnqualifiedType();
1095 type2_qual = type2_qual.getUnqualifiedType();
1098 return ast->hasSameType (type1_qual, type2_qual);
1103 ClangASTContext::GetTypeForDecl (TagDecl *decl)
1105 // No need to call the getASTContext() accessor (which can create the AST
1106 // if it isn't created yet, because we can't have created a decl in this
1107 // AST if our AST didn't already exist...
1108 ASTContext *ast = m_ast_ap.get();
1110 return ClangASTType (ast, ast->getTagDeclType(decl).getAsOpaquePtr());
1111 return ClangASTType();
1115 ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1117 // No need to call the getASTContext() accessor (which can create the AST
1118 // if it isn't created yet, because we can't have created a decl in this
1119 // AST if our AST didn't already exist...
1120 ASTContext *ast = m_ast_ap.get();
1122 return ClangASTType (ast, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
1123 return ClangASTType();
1126 #pragma mark Structure, Unions, Classes
1129 ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1130 AccessType access_type,
1133 LanguageType language,
1134 ClangASTMetadata *metadata)
1136 ASTContext *ast = getASTContext();
1137 assert (ast != NULL);
1139 if (decl_ctx == NULL)
1140 decl_ctx = ast->getTranslationUnitDecl();
1143 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
1145 bool isForwardDecl = true;
1146 bool isInternal = false;
1147 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
1150 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1151 // we will need to update this code. I was told to currently always use
1152 // the CXXRecordDecl class since we often don't know from debug information
1153 // if something is struct or a class, so we default to always use the more
1154 // complete definition just in case.
1155 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1156 (TagDecl::TagKind)kind,
1160 name && name[0] ? &ast->Idents.get(name) : NULL);
1165 SetMetadata(ast, decl, *metadata);
1167 if (access_type != eAccessNone)
1168 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1171 decl_ctx->addDecl (decl);
1173 return ClangASTType(ast, ast->getTagDeclType(decl).getAsOpaquePtr());
1175 return ClangASTType();
1178 static TemplateParameterList *
1179 CreateTemplateParameterList (ASTContext *ast,
1180 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1181 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1183 const bool parameter_pack = false;
1184 const bool is_typename = false;
1185 const unsigned depth = 0;
1186 const size_t num_template_params = template_param_infos.GetSize();
1187 for (size_t i=0; i<num_template_params; ++i)
1189 const char *name = template_param_infos.names[i];
1191 IdentifierInfo *identifier_info = NULL;
1192 if (name && name[0])
1193 identifier_info = &ast->Idents.get(name);
1194 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
1196 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1197 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1203 template_param_infos.args[i].getIntegralType(),
1210 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1211 ast->getTranslationUnitDecl(), // Is this the right decl context?
1222 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1225 &template_param_decls.front(),
1226 template_param_decls.size(),
1228 return template_param_list;
1231 clang::FunctionTemplateDecl *
1232 ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1233 clang::FunctionDecl *func_decl,
1235 const TemplateParameterInfos &template_param_infos)
1237 // /// \brief Create a function template node.
1238 ASTContext *ast = getASTContext();
1240 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1242 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1243 template_param_infos,
1244 template_param_decls);
1245 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1247 func_decl->getLocation(),
1248 func_decl->getDeclName(),
1249 template_param_list,
1252 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1253 i < template_param_decl_count;
1256 // TODO: verify which decl context we should put template_param_decls into..
1257 template_param_decls[i]->setDeclContext (func_decl);
1260 return func_tmpl_decl;
1264 ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1265 clang::FunctionTemplateDecl *func_tmpl_decl,
1266 const TemplateParameterInfos &infos)
1268 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1272 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1279 ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
1280 lldb::AccessType access_type,
1281 const char *class_name,
1283 const TemplateParameterInfos &template_param_infos)
1285 ASTContext *ast = getASTContext();
1287 ClassTemplateDecl *class_template_decl = NULL;
1288 if (decl_ctx == NULL)
1289 decl_ctx = ast->getTranslationUnitDecl();
1291 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1292 DeclarationName decl_name (&identifier_info);
1294 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1296 for (NamedDecl *decl : result)
1298 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1299 if (class_template_decl)
1300 return class_template_decl;
1303 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1305 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1306 template_param_infos,
1307 template_param_decls);
1309 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1310 (TagDecl::TagKind)kind,
1311 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1316 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1317 i < template_param_decl_count;
1320 template_param_decls[i]->setDeclContext (template_cxx_decl);
1323 // With templated classes, we say that a class is templated with
1324 // specializations, but that the bare class has no functions.
1325 //template_cxx_decl->startDefinition();
1326 //template_cxx_decl->completeDefinition();
1328 class_template_decl = ClassTemplateDecl::Create (*ast,
1329 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1332 template_param_list,
1336 if (class_template_decl)
1338 if (access_type != eAccessNone)
1339 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1341 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1342 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1344 decl_ctx->addDecl (class_template_decl);
1346 #ifdef LLDB_CONFIGURATION_DEBUG
1347 VerifyDecl(class_template_decl);
1351 return class_template_decl;
1355 ClassTemplateSpecializationDecl *
1356 ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1357 ClassTemplateDecl *class_template_decl,
1359 const TemplateParameterInfos &template_param_infos)
1361 ASTContext *ast = getASTContext();
1362 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1363 (TagDecl::TagKind)kind,
1367 class_template_decl,
1368 &template_param_infos.args.front(),
1369 template_param_infos.args.size(),
1372 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1374 return class_template_specialization_decl;
1378 ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1380 if (class_template_specialization_decl)
1382 ASTContext *ast = getASTContext();
1384 return ClangASTType(ast, ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr());
1386 return ClangASTType();
1390 IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1392 if (name == NULL || name[0] == '\0')
1395 #define OPERATOR_PREFIX "operator"
1396 #define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
1398 const char *post_op_name = NULL;
1400 bool no_space = true;
1402 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
1405 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1407 if (post_op_name[0] == ' ')
1413 #undef OPERATOR_PREFIX
1414 #undef OPERATOR_PREFIX_LENGTH
1416 // This is an operator, set the overloaded operator kind to invalid
1417 // in case this is a conversion operator...
1418 op_kind = NUM_OVERLOADED_OPERATORS;
1420 switch (post_op_name[0])
1429 if (strcmp (post_op_name, "new") == 0)
1431 else if (strcmp (post_op_name, "new[]") == 0)
1432 op_kind = OO_Array_New;
1438 if (strcmp (post_op_name, "delete") == 0)
1439 op_kind = OO_Delete;
1440 else if (strcmp (post_op_name, "delete[]") == 0)
1441 op_kind = OO_Array_Delete;
1445 if (post_op_name[1] == '\0')
1447 else if (post_op_name[2] == '\0')
1449 if (post_op_name[1] == '=')
1450 op_kind = OO_PlusEqual;
1451 else if (post_op_name[1] == '+')
1452 op_kind = OO_PlusPlus;
1457 if (post_op_name[1] == '\0')
1459 else if (post_op_name[2] == '\0')
1461 switch (post_op_name[1])
1463 case '=': op_kind = OO_MinusEqual; break;
1464 case '-': op_kind = OO_MinusMinus; break;
1465 case '>': op_kind = OO_Arrow; break;
1468 else if (post_op_name[3] == '\0')
1470 if (post_op_name[2] == '*')
1471 op_kind = OO_ArrowStar; break;
1476 if (post_op_name[1] == '\0')
1478 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1479 op_kind = OO_StarEqual;
1483 if (post_op_name[1] == '\0')
1485 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1486 op_kind = OO_SlashEqual;
1490 if (post_op_name[1] == '\0')
1491 op_kind = OO_Percent;
1492 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1493 op_kind = OO_PercentEqual;
1498 if (post_op_name[1] == '\0')
1500 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1501 op_kind = OO_CaretEqual;
1505 if (post_op_name[1] == '\0')
1507 else if (post_op_name[2] == '\0')
1509 switch (post_op_name[1])
1511 case '=': op_kind = OO_AmpEqual; break;
1512 case '&': op_kind = OO_AmpAmp; break;
1518 if (post_op_name[1] == '\0')
1520 else if (post_op_name[2] == '\0')
1522 switch (post_op_name[1])
1524 case '=': op_kind = OO_PipeEqual; break;
1525 case '|': op_kind = OO_PipePipe; break;
1531 if (post_op_name[1] == '\0')
1536 if (post_op_name[1] == '\0')
1537 op_kind = OO_Exclaim;
1538 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1539 op_kind = OO_ExclaimEqual;
1543 if (post_op_name[1] == '\0')
1545 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1546 op_kind = OO_EqualEqual;
1550 if (post_op_name[1] == '\0')
1552 else if (post_op_name[2] == '\0')
1554 switch (post_op_name[1])
1556 case '<': op_kind = OO_LessLess; break;
1557 case '=': op_kind = OO_LessEqual; break;
1560 else if (post_op_name[3] == '\0')
1562 if (post_op_name[2] == '=')
1563 op_kind = OO_LessLessEqual;
1568 if (post_op_name[1] == '\0')
1569 op_kind = OO_Greater;
1570 else if (post_op_name[2] == '\0')
1572 switch (post_op_name[1])
1574 case '>': op_kind = OO_GreaterGreater; break;
1575 case '=': op_kind = OO_GreaterEqual; break;
1578 else if (post_op_name[1] == '>' &&
1579 post_op_name[2] == '=' &&
1580 post_op_name[3] == '\0')
1582 op_kind = OO_GreaterGreaterEqual;
1587 if (post_op_name[1] == '\0')
1592 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1597 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1598 op_kind = OO_Subscript;
1606 check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
1608 // Special-case call since it can take any number of operands
1609 if(op_kind == OO_Call)
1612 // The parameter count doens't include "this"
1613 if (num_params == 0)
1615 if (num_params == 1)
1622 ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1628 // C++ standard allows any number of arguments to new/delete
1632 case OO_Array_Delete:
1636 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
1639 #include "clang/Basic/OperatorKinds.def"
1645 clang::AccessSpecifier
1646 ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
1648 clang::AccessSpecifier ret = lhs;
1650 // Make the access equal to the stricter of the field and the nested field's access
1653 case clang::AS_none:
1655 case clang::AS_private:
1657 case clang::AS_protected:
1658 if (rhs == AS_private)
1661 case clang::AS_public:
1670 ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1672 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1676 ClangASTContext::FieldIsBitfield
1680 uint32_t& bitfield_bit_size
1683 if (ast == NULL || field == NULL)
1686 if (field->isBitField())
1688 Expr* bit_width_expr = field->getBitWidth();
1691 llvm::APSInt bit_width_apsint;
1692 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
1694 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1703 ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1705 if (record_decl == NULL)
1708 if (!record_decl->field_empty())
1711 // No fields, lets check this is a CXX record and check the base classes
1712 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1713 if (cxx_record_decl)
1715 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1716 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1717 base_class != base_class_end;
1720 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1721 if (RecordHasFields(base_class_decl))
1728 #pragma mark Objective C Classes
1731 ClangASTContext::CreateObjCClass
1734 DeclContext *decl_ctx,
1737 ClangASTMetadata *metadata
1740 ASTContext *ast = getASTContext();
1741 assert (ast != NULL);
1742 assert (name && name[0]);
1743 if (decl_ctx == NULL)
1744 decl_ctx = ast->getTranslationUnitDecl();
1746 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
1749 &ast->Idents.get(name),
1755 if (decl && metadata)
1756 SetMetadata(ast, decl, *metadata);
1758 return ClangASTType (ast, ast->getObjCInterfaceType(decl));
1762 BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1764 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
1768 ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
1770 uint32_t num_bases = 0;
1771 if (cxx_record_decl)
1773 if (omit_empty_base_classes)
1775 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1776 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1777 base_class != base_class_end;
1780 // Skip empty base classes
1781 if (omit_empty_base_classes)
1783 if (BaseSpecifierIsEmpty (base_class))
1790 num_bases = cxx_record_decl->getNumBases();
1796 #pragma mark Namespace Declarations
1799 ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
1801 NamespaceDecl *namespace_decl = NULL;
1802 ASTContext *ast = getASTContext();
1803 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
1804 if (decl_ctx == NULL)
1805 decl_ctx = translation_unit_decl;
1809 IdentifierInfo &identifier_info = ast->Idents.get(name);
1810 DeclarationName decl_name (&identifier_info);
1811 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1812 for (NamedDecl *decl : result)
1814 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1816 return namespace_decl;
1819 namespace_decl = NamespaceDecl::Create(*ast,
1827 decl_ctx->addDecl (namespace_decl);
1831 if (decl_ctx == translation_unit_decl)
1833 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1835 return namespace_decl;
1837 namespace_decl = NamespaceDecl::Create(*ast,
1844 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1845 translation_unit_decl->addDecl (namespace_decl);
1846 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1850 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1851 if (parent_namespace_decl)
1853 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1855 return namespace_decl;
1856 namespace_decl = NamespaceDecl::Create(*ast,
1863 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1864 parent_namespace_decl->addDecl (namespace_decl);
1865 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1876 // If we make it here, we are creating the anonymous namespace decl
1877 // for the first time, so we need to do the using directive magic
1879 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
1883 NestedNameSpecifierLoc(),
1887 using_directive_decl->setImplicit();
1888 decl_ctx->addDecl(using_directive_decl);
1891 #ifdef LLDB_CONFIGURATION_DEBUG
1892 VerifyDecl(namespace_decl);
1894 return namespace_decl;
1898 #pragma mark Function Types
1901 ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1903 const ClangASTType &function_clang_type,
1907 FunctionDecl *func_decl = NULL;
1908 ASTContext *ast = getASTContext();
1909 if (decl_ctx == NULL)
1910 decl_ctx = ast->getTranslationUnitDecl();
1913 const bool hasWrittenPrototype = true;
1914 const bool isConstexprSpecified = false;
1916 if (name && name[0])
1918 func_decl = FunctionDecl::Create (*ast,
1922 DeclarationName (&ast->Idents.get(name)),
1923 function_clang_type.GetQualType(),
1925 (FunctionDecl::StorageClass)storage,
1927 hasWrittenPrototype,
1928 isConstexprSpecified);
1932 func_decl = FunctionDecl::Create (*ast,
1937 function_clang_type.GetQualType(),
1939 (FunctionDecl::StorageClass)storage,
1941 hasWrittenPrototype,
1942 isConstexprSpecified);
1945 decl_ctx->addDecl (func_decl);
1947 #ifdef LLDB_CONFIGURATION_DEBUG
1948 VerifyDecl(func_decl);
1955 ClangASTContext::CreateFunctionType (ASTContext *ast,
1956 const ClangASTType& result_type,
1957 const ClangASTType *args,
1960 unsigned type_quals)
1962 assert (ast != NULL);
1963 std::vector<QualType> qual_type_args;
1964 for (unsigned i=0; i<num_args; ++i)
1965 qual_type_args.push_back (args[i].GetQualType());
1967 // TODO: Detect calling convention in DWARF?
1968 FunctionProtoType::ExtProtoInfo proto_info;
1969 proto_info.Variadic = is_variadic;
1970 proto_info.ExceptionSpecType = EST_None;
1971 proto_info.TypeQuals = type_quals;
1972 proto_info.RefQualifier = RQ_None;
1973 proto_info.NumExceptions = 0;
1974 proto_info.Exceptions = NULL;
1976 return ClangASTType (ast, ast->getFunctionType (result_type.GetQualType(),
1978 proto_info).getAsOpaquePtr());
1982 ClangASTContext::CreateParameterDeclaration (const char *name, const ClangASTType ¶m_type, int storage)
1984 ASTContext *ast = getASTContext();
1985 assert (ast != NULL);
1986 return ParmVarDecl::Create(*ast,
1987 ast->getTranslationUnitDecl(),
1990 name && name[0] ? &ast->Idents.get(name) : NULL,
1991 param_type.GetQualType(),
1993 (VarDecl::StorageClass)storage,
1998 ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2001 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
2005 #pragma mark Array Types
2008 ClangASTContext::CreateArrayType (const ClangASTType &element_type,
2009 size_t element_count,
2012 if (element_type.IsValid())
2014 ASTContext *ast = getASTContext();
2015 assert (ast != NULL);
2019 return ClangASTType (ast, ast->getExtVectorType(element_type.GetQualType(), element_count).getAsOpaquePtr());
2024 llvm::APInt ap_element_count (64, element_count);
2025 if (element_count == 0)
2027 return ClangASTType (ast, ast->getIncompleteArrayType (element_type.GetQualType(),
2029 0).getAsOpaquePtr());
2033 return ClangASTType (ast, ast->getConstantArrayType (element_type.GetQualType(),
2036 0).getAsOpaquePtr());
2040 return ClangASTType();
2045 #pragma mark Enumeration Types
2048 ClangASTContext::CreateEnumerationType
2051 DeclContext *decl_ctx,
2052 const Declaration &decl,
2053 const ClangASTType &integer_clang_type
2056 // TODO: Do something intelligent with the Declaration object passed in
2057 // like maybe filling in the SourceLocation with it...
2058 ASTContext *ast = getASTContext();
2060 // TODO: ask about these...
2061 // const bool IsScoped = false;
2062 // const bool IsFixed = false;
2064 EnumDecl *enum_decl = EnumDecl::Create (*ast,
2068 name && name[0] ? &ast->Idents.get(name) : NULL,
2071 false, // IsScopedUsingClassTag
2077 // TODO: check if we should be setting the promotion type too?
2078 enum_decl->setIntegerType(integer_clang_type.GetQualType());
2080 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2082 return ClangASTType (ast, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
2084 return ClangASTType();
2087 // Disable this for now since I can't seem to get a nicely formatted float
2088 // out of the APFloat class without just getting the float, double or quad
2089 // and then using a formatted print on it which defeats the purpose. We ideally
2090 // would like to get perfect string values for any kind of float semantics
2091 // so we can support remote targets. The code below also requires a patch to
2094 //ClangASTContext::ConvertFloatValueToString (ASTContext *ast, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str)
2096 // uint32_t count = 0;
2097 // bool is_complex = false;
2098 // if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2100 // unsigned num_bytes_per_float = byte_size / count;
2101 // unsigned num_bits_per_float = num_bytes_per_float * 8;
2103 // float_str.clear();
2105 // for (i=0; i<count; i++)
2107 // APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2108 // bool is_ieee = false;
2109 // APFloat ap_float(ap_int, is_ieee);
2111 // unsigned int hex_digits = 0;
2112 // bool upper_case = false;
2114 // if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2117 // float_str.append(", ");
2118 // float_str.append(s);
2119 // if (i == 1 && is_complex)
2120 // float_str.append(1, 'i');
2123 // return !float_str.empty();
2130 ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2135 if (bit_size == ast->getTypeSize(ast->FloatTy))
2136 return ClangASTType(ast, ast->FloatTy.getAsOpaquePtr());
2137 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
2138 return ClangASTType(ast, ast->DoubleTy.getAsOpaquePtr());
2139 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
2140 return ClangASTType(ast, ast->LongDoubleTy.getAsOpaquePtr());
2141 else if (bit_size == ast->getTypeSize(ast->HalfTy))
2142 return ClangASTType(ast, ast->HalfTy.getAsOpaquePtr());
2144 return ClangASTType();
2148 ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2154 ExternalASTSource *ast_source = ast->getExternalSource();
2159 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2161 if (tag_decl->isCompleteDefinition())
2164 if (!tag_decl->hasExternalLexicalStorage())
2167 ast_source->CompleteType(tag_decl);
2169 return !tag_decl->getTypeForDecl()->isIncompleteType();
2171 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2173 if (objc_interface_decl->getDefinition())
2176 if (!objc_interface_decl->hasExternalLexicalStorage())
2179 ast_source->CompleteType(objc_interface_decl);
2181 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2190 ClangASTContext::SetMetadataAsUserID (const void *object,
2193 ClangASTMetadata meta_data;
2194 meta_data.SetUserID (user_id);
2195 SetMetadata (object, meta_data);
2199 ClangASTContext::SetMetadata (clang::ASTContext *ast,
2201 ClangASTMetadata &metadata)
2203 ClangExternalASTSourceCommon *external_source =
2204 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
2206 if (external_source)
2207 external_source->SetMetadata(object, metadata);
2211 ClangASTContext::GetMetadata (clang::ASTContext *ast,
2214 ClangExternalASTSourceCommon *external_source =
2215 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
2217 if (external_source && external_source->HasMetadata(object))
2218 return external_source->GetMetadata(object);
2223 clang::DeclContext *
2224 ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2226 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2229 clang::DeclContext *
2230 ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2232 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2237 ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
2238 lldb::LanguageType &language,
2239 bool &is_instance_method,
2240 ConstString &language_object_name)
2242 language_object_name.Clear();
2243 language = eLanguageTypeUnknown;
2244 is_instance_method = false;
2248 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
2250 if (method_decl->isStatic())
2252 is_instance_method = false;
2256 language_object_name.SetCString("this");
2257 is_instance_method = true;
2259 language = eLanguageTypeC_plus_plus;
2262 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
2264 // Both static and instance methods have a "self" object in objective C
2265 language_object_name.SetCString("self");
2266 if (method_decl->isInstanceMethod())
2268 is_instance_method = true;
2272 is_instance_method = false;
2274 language = eLanguageTypeObjC;
2277 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
2279 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
2280 if (metadata && metadata->HasObjectPtr())
2282 language_object_name.SetCString (metadata->GetObjectPtrName());
2283 language = eLanguageTypeObjC;
2284 is_instance_method = true;