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 // related 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 typedef llvm::DenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
87 static ClangASTMap g_map;
92 clang::AccessSpecifier
93 ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
98 case eAccessNone: return AS_none;
99 case eAccessPublic: return AS_public;
100 case eAccessPrivate: return AS_private;
101 case eAccessProtected: return AS_protected;
114 // FIXME: Cleanup per-file based stuff.
116 // Set some properties which depend solely on the input kind; it would be nice
117 // to move these to the language standard, and have the driver resolve the
118 // input kind + language standard.
120 Opts.AsmPreprocessor = 1;
121 } else if (IK == IK_ObjC ||
123 IK == IK_PreprocessedObjC ||
124 IK == IK_PreprocessedObjCXX) {
125 Opts.ObjC1 = Opts.ObjC2 = 1;
128 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
130 if (LangStd == LangStandard::lang_unspecified) {
131 // Based on the base language, pick one.
136 assert (!"Invalid input kind!");
138 LangStd = LangStandard::lang_opencl;
141 LangStd = LangStandard::lang_cuda;
145 case IK_PreprocessedC:
147 case IK_PreprocessedObjC:
148 LangStd = LangStandard::lang_gnu99;
151 case IK_PreprocessedCXX:
153 case IK_PreprocessedObjCXX:
154 LangStd = LangStandard::lang_gnucxx98;
159 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
160 Opts.LineComment = Std.hasLineComments();
161 Opts.C99 = Std.isC99();
162 Opts.CPlusPlus = Std.isCPlusPlus();
163 Opts.CPlusPlus11 = Std.isCPlusPlus11();
164 Opts.Digraphs = Std.hasDigraphs();
165 Opts.GNUMode = Std.isGNUMode();
166 Opts.GNUInline = !Std.isC99();
167 Opts.HexFloats = Std.hasHexFloats();
168 Opts.ImplicitInt = Std.hasImplicitInt();
172 // OpenCL has some additional defaults.
173 if (LangStd == LangStandard::lang_opencl) {
176 Opts.CXXOperatorNames = 1;
177 Opts.LaxVectorConversions = 1;
180 // OpenCL and C++ both have bool, true, false keywords.
181 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
183 // if (Opts.CPlusPlus)
184 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
186 // if (Args.hasArg(OPT_fobjc_gc_only))
187 // Opts.setGCMode(LangOptions::GCOnly);
188 // else if (Args.hasArg(OPT_fobjc_gc))
189 // Opts.setGCMode(LangOptions::HybridGC);
191 // if (Args.hasArg(OPT_print_ivar_layout))
192 // Opts.ObjCGCBitmapPrint = 1;
194 // if (Args.hasArg(OPT_faltivec))
197 // if (Args.hasArg(OPT_pthread))
198 // Opts.POSIXThreads = 1;
200 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
202 // if (Vis == "default")
203 Opts.setValueVisibilityMode(DefaultVisibility);
204 // else if (Vis == "hidden")
205 // Opts.setVisibilityMode(LangOptions::Hidden);
206 // else if (Vis == "protected")
207 // Opts.setVisibilityMode(LangOptions::Protected);
209 // Diags.Report(diag::err_drv_invalid_value)
210 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
212 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
214 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
215 // is specified, or -std is set to a conforming mode.
216 Opts.Trigraphs = !Opts.GNUMode;
217 // if (Args.hasArg(OPT_trigraphs))
218 // Opts.Trigraphs = 1;
220 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
221 // OPT_fno_dollars_in_identifiers,
222 // !Opts.AsmPreprocessor);
223 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
224 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
225 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
226 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
227 // Opts.LaxVectorConversions = 0;
228 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
229 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
230 // Opts.Blocks = Args.hasArg(OPT_fblocks);
231 // Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
232 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
233 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
234 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
235 // Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
236 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
237 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
238 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
239 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
240 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
242 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
243 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
244 // OPT_fconstant_string_class);
245 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
246 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
247 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
248 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
249 // Opts.Static = Args.hasArg(OPT_static_define);
250 Opts.OptimizeSize = 0;
252 // FIXME: Eliminate this dependency.
254 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
255 // Opts.Optimize = Opt != 0;
258 // This is the __NO_INLINE__ define, which just depends on things like the
259 // optimization level and -fno-inline, not actually whether the backend has
262 // FIXME: This is affected by other options (-fno-inline).
263 Opts.NoInlineDefine = !Opt;
265 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
268 // Diags.Report(diag::err_drv_invalid_value)
269 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
271 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
272 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
273 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
278 ClangASTContext::ClangASTContext (const char *target_triple) :
281 m_language_options_ap(),
282 m_source_manager_ap(),
283 m_diagnostics_engine_ap(),
284 m_target_options_rp(),
286 m_identifier_table_ap(),
287 m_selector_table_ap(),
289 m_callback_tag_decl (nullptr),
290 m_callback_objc_decl (nullptr),
291 m_callback_baton (nullptr),
292 m_pointer_byte_size (0)
295 if (target_triple && target_triple[0])
296 SetTargetTriple (target_triple);
299 //----------------------------------------------------------------------
301 //----------------------------------------------------------------------
302 ClangASTContext::~ClangASTContext()
306 GetASTMap().erase(m_ast_ap.get());
309 m_builtins_ap.reset();
310 m_selector_table_ap.reset();
311 m_identifier_table_ap.reset();
312 m_target_info_ap.reset();
313 m_target_options_rp.reset();
314 m_diagnostics_engine_ap.reset();
315 m_source_manager_ap.reset();
316 m_language_options_ap.reset();
322 ClangASTContext::Clear()
325 m_language_options_ap.reset();
326 m_source_manager_ap.reset();
327 m_diagnostics_engine_ap.reset();
328 m_target_options_rp.reset();
329 m_target_info_ap.reset();
330 m_identifier_table_ap.reset();
331 m_selector_table_ap.reset();
332 m_builtins_ap.reset();
333 m_pointer_byte_size = 0;
337 ClangASTContext::GetTargetTriple ()
339 return m_target_triple.c_str();
343 ClangASTContext::SetTargetTriple (const char *target_triple)
346 m_target_triple.assign(target_triple);
350 ClangASTContext::SetArchitecture (const ArchSpec &arch)
352 SetTargetTriple(arch.GetTriple().str().c_str());
356 ClangASTContext::HasExternalSource ()
358 ASTContext *ast = getASTContext();
360 return ast->getExternalSource () != nullptr;
365 ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
367 ASTContext *ast = getASTContext();
370 ast->setExternalSource (ast_source_ap);
371 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
372 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
377 ClangASTContext::RemoveExternalSource ()
379 ASTContext *ast = getASTContext();
383 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
384 ast->setExternalSource (empty_ast_source_ap);
385 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
386 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
393 ClangASTContext::getASTContext()
395 if (m_ast_ap.get() == nullptr)
397 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
399 *getIdentifierTable(),
401 *getBuiltinContext()));
402 m_ast_ap->InitBuiltinTypes(*getTargetInfo());
404 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
406 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
407 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
410 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
412 GetASTMap().insert(std::make_pair(m_ast_ap.get(), this));
414 return m_ast_ap.get();
418 ClangASTContext::GetASTContext (clang::ASTContext* ast)
420 ClangASTContext *clang_ast = GetASTMap().lookup(ast);
425 ClangASTContext::getBuiltinContext()
427 if (m_builtins_ap.get() == nullptr)
428 m_builtins_ap.reset (new Builtin::Context());
429 return m_builtins_ap.get();
433 ClangASTContext::getIdentifierTable()
435 if (m_identifier_table_ap.get() == nullptr)
436 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
437 return m_identifier_table_ap.get();
441 ClangASTContext::getLanguageOptions()
443 if (m_language_options_ap.get() == nullptr)
445 m_language_options_ap.reset(new LangOptions());
446 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
447 // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
449 return m_language_options_ap.get();
453 ClangASTContext::getSelectorTable()
455 if (m_selector_table_ap.get() == nullptr)
456 m_selector_table_ap.reset (new SelectorTable());
457 return m_selector_table_ap.get();
461 ClangASTContext::getFileManager()
463 if (m_file_manager_ap.get() == nullptr)
465 clang::FileSystemOptions file_system_options;
466 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
468 return m_file_manager_ap.get();
471 clang::SourceManager *
472 ClangASTContext::getSourceManager()
474 if (m_source_manager_ap.get() == nullptr)
475 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
476 return m_source_manager_ap.get();
479 clang::DiagnosticsEngine *
480 ClangASTContext::getDiagnosticsEngine()
482 if (m_diagnostics_engine_ap.get() == nullptr)
484 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
485 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
487 return m_diagnostics_engine_ap.get();
490 class NullDiagnosticConsumer : public DiagnosticConsumer
493 NullDiagnosticConsumer ()
495 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
498 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
502 llvm::SmallVector<char, 32> diag_str(10);
503 info.FormatDiagnostic(diag_str);
504 diag_str.push_back('\0');
505 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
509 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
511 return new NullDiagnosticConsumer ();
518 ClangASTContext::getDiagnosticConsumer()
520 if (m_diagnostic_consumer_ap.get() == nullptr)
521 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
523 return m_diagnostic_consumer_ap.get();
526 std::shared_ptr<TargetOptions> &
527 ClangASTContext::getTargetOptions() {
528 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
530 m_target_options_rp = std::make_shared<TargetOptions>();
531 if (m_target_options_rp.get() != nullptr)
532 m_target_options_rp->Triple = m_target_triple;
534 return m_target_options_rp;
539 ClangASTContext::getTargetInfo()
541 // target_triple should be something like "x86_64-apple-macosx"
542 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
543 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
544 return m_target_info_ap.get();
547 #pragma mark Basic Types
550 QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
552 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
553 if (qual_type_bit_size == bit_size)
558 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
560 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
564 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
567 return ClangASTType();
571 case eEncodingInvalid:
572 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
573 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr());
577 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
578 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
579 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
580 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
581 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
582 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
583 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
584 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
585 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
586 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
587 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
588 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
592 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
593 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
594 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
595 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
596 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
597 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
598 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
599 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
600 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
601 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
602 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
603 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
606 case eEncodingIEEE754:
607 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
608 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr());
609 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
610 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr());
611 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
612 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr());
615 case eEncodingVector:
616 // Sanity check that bit_size is a multiple of 8's.
617 if (bit_size && !(bit_size & 0x7u))
618 return ClangASTType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr());
622 return ClangASTType();
628 ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
632 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
633 static TypeNameToBasicTypeMap g_type_map;
634 static std::once_flag g_once_flag;
635 std::call_once(g_once_flag, [](){
637 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
640 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
641 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
642 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
643 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
644 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
645 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
647 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
648 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
649 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
650 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
653 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
654 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
655 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
656 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
659 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
660 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
661 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
662 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
665 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
666 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
667 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
668 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
671 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
672 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
675 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
676 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
677 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
678 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
679 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
680 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
681 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
685 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
687 return eBasicTypeInvalid;
691 ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
695 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
696 return ClangASTContext::GetBasicType (ast, basic_type);
698 return ClangASTType();
702 ClangASTContext::GetPointerByteSize ()
704 if (m_pointer_byte_size == 0)
705 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize();
706 return m_pointer_byte_size;
710 ClangASTContext::GetBasicType (lldb::BasicType basic_type)
712 return GetBasicType (getASTContext(), basic_type);
716 ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
720 clang_type_t clang_type = nullptr;
724 case eBasicTypeInvalid:
725 case eBasicTypeOther:
728 clang_type = ast->VoidTy.getAsOpaquePtr();
731 clang_type = ast->CharTy.getAsOpaquePtr();
733 case eBasicTypeSignedChar:
734 clang_type = ast->SignedCharTy.getAsOpaquePtr();
736 case eBasicTypeUnsignedChar:
737 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
739 case eBasicTypeWChar:
740 clang_type = ast->getWCharType().getAsOpaquePtr();
742 case eBasicTypeSignedWChar:
743 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
745 case eBasicTypeUnsignedWChar:
746 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
748 case eBasicTypeChar16:
749 clang_type = ast->Char16Ty.getAsOpaquePtr();
751 case eBasicTypeChar32:
752 clang_type = ast->Char32Ty.getAsOpaquePtr();
754 case eBasicTypeShort:
755 clang_type = ast->ShortTy.getAsOpaquePtr();
757 case eBasicTypeUnsignedShort:
758 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
761 clang_type = ast->IntTy.getAsOpaquePtr();
763 case eBasicTypeUnsignedInt:
764 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
767 clang_type = ast->LongTy.getAsOpaquePtr();
769 case eBasicTypeUnsignedLong:
770 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
772 case eBasicTypeLongLong:
773 clang_type = ast->LongLongTy.getAsOpaquePtr();
775 case eBasicTypeUnsignedLongLong:
776 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
778 case eBasicTypeInt128:
779 clang_type = ast->Int128Ty.getAsOpaquePtr();
781 case eBasicTypeUnsignedInt128:
782 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
785 clang_type = ast->BoolTy.getAsOpaquePtr();
788 clang_type = ast->HalfTy.getAsOpaquePtr();
790 case eBasicTypeFloat:
791 clang_type = ast->FloatTy.getAsOpaquePtr();
793 case eBasicTypeDouble:
794 clang_type = ast->DoubleTy.getAsOpaquePtr();
796 case eBasicTypeLongDouble:
797 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
799 case eBasicTypeFloatComplex:
800 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
802 case eBasicTypeDoubleComplex:
803 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
805 case eBasicTypeLongDoubleComplex:
806 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
808 case eBasicTypeObjCID:
809 clang_type = ast->getObjCIdType().getAsOpaquePtr();
811 case eBasicTypeObjCClass:
812 clang_type = ast->getObjCClassType().getAsOpaquePtr();
814 case eBasicTypeObjCSel:
815 clang_type = ast->getObjCSelType().getAsOpaquePtr();
817 case eBasicTypeNullPtr:
818 clang_type = ast->NullPtrTy.getAsOpaquePtr();
823 return ClangASTType (ast, clang_type);
825 return ClangASTType();
830 ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
832 ASTContext *ast = getASTContext();
834 #define streq(a,b) strcmp(a,b) == 0
835 assert (ast != nullptr);
844 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
845 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr());
849 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
850 return ClangASTType (ast, ast->BoolTy.getAsOpaquePtr());
851 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
852 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
853 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
854 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
855 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
856 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
860 // This has been seen to mean DW_AT_complex_integer
863 if (::strstr(type_name, "complex"))
865 ClangASTType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
866 return ClangASTType (ast, ast->getComplexType (complex_int_clang_type.GetQualType()).getAsOpaquePtr());
871 case DW_ATE_complex_float:
872 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
873 return ClangASTType (ast, ast->FloatComplexTy.getAsOpaquePtr());
874 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
875 return ClangASTType (ast, ast->DoubleComplexTy.getAsOpaquePtr());
876 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
877 return ClangASTType (ast, ast->LongDoubleComplexTy.getAsOpaquePtr());
880 ClangASTType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
881 return ClangASTType (ast, ast->getComplexType (complex_float_clang_type.GetQualType()).getAsOpaquePtr());
886 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
887 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr());
888 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
889 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr());
890 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
891 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr());
897 if (streq(type_name, "wchar_t") &&
898 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
899 return ClangASTType (ast, ast->WCharTy.getAsOpaquePtr());
900 if (streq(type_name, "void") &&
901 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
902 return ClangASTType (ast, ast->VoidTy.getAsOpaquePtr());
903 if (strstr(type_name, "long long") &&
904 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
905 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
906 if (strstr(type_name, "long") &&
907 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
908 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
909 if (strstr(type_name, "short") &&
910 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
911 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
912 if (strstr(type_name, "char"))
914 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
915 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
916 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
917 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
919 if (strstr(type_name, "int"))
921 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
922 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
923 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
924 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
927 // We weren't able to match up a type name, just search by size
928 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
929 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
930 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
931 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
932 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
933 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
934 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
935 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
936 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
937 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
938 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
939 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
942 case DW_ATE_signed_char:
945 if (streq(type_name, "signed char"))
947 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
948 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
951 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
952 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
953 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
954 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
957 case DW_ATE_unsigned:
960 if (strstr(type_name, "long long"))
962 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
963 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
965 else if (strstr(type_name, "long"))
967 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
968 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
970 else if (strstr(type_name, "short"))
972 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
973 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
975 else if (strstr(type_name, "char"))
977 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
978 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
980 else if (strstr(type_name, "int"))
982 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
983 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
984 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
985 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
988 // We weren't able to match up a type name, just search by size
989 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
990 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
991 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
992 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
993 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
994 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
995 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
996 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
997 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
998 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
999 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
1000 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
1003 case DW_ATE_unsigned_char:
1004 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
1005 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
1006 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
1007 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
1010 case DW_ATE_imaginary_float:
1016 if (streq(type_name, "char16_t"))
1018 return ClangASTType (ast, ast->Char16Ty.getAsOpaquePtr());
1020 else if (streq(type_name, "char32_t"))
1022 return ClangASTType (ast, ast->Char32Ty.getAsOpaquePtr());
1028 // This assert should fire for anything that we don't catch above so we know
1029 // to fix any issues we run into.
1032 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);
1036 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);
1038 return ClangASTType ();
1042 ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1045 return ClangASTType (ast, ast->UnknownAnyTy.getAsOpaquePtr());
1046 return ClangASTType();
1050 ClangASTContext::GetCStringType (bool is_const)
1052 ASTContext *ast = getASTContext();
1053 QualType char_type(ast->CharTy);
1056 char_type.addConst();
1058 return ClangASTType (ast, ast->getPointerType(char_type).getAsOpaquePtr());
1061 clang::DeclContext *
1062 ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1064 return ast->getTranslationUnitDecl();
1068 ClangASTContext::CopyType (ASTContext *dst_ast,
1071 FileSystemOptions file_system_options;
1072 ASTContext *src_ast = src.GetASTContext();
1073 FileManager file_manager (file_system_options);
1074 ASTImporter importer(*dst_ast, file_manager,
1075 *src_ast, file_manager,
1078 QualType dst (importer.Import(src.GetQualType()));
1080 return ClangASTType (dst_ast, dst.getAsOpaquePtr());
1085 ClangASTContext::CopyDecl (ASTContext *dst_ast,
1086 ASTContext *src_ast,
1087 clang::Decl *source_decl)
1089 FileSystemOptions file_system_options;
1090 FileManager file_manager (file_system_options);
1091 ASTImporter importer(*dst_ast, file_manager,
1092 *src_ast, file_manager,
1095 return importer.Import(source_decl);
1099 ClangASTContext::AreTypesSame (ClangASTType type1,
1101 bool ignore_qualifiers)
1103 ASTContext *ast = type1.GetASTContext();
1104 if (ast != type2.GetASTContext())
1107 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1110 QualType type1_qual = type1.GetQualType();
1111 QualType type2_qual = type2.GetQualType();
1113 if (ignore_qualifiers)
1115 type1_qual = type1_qual.getUnqualifiedType();
1116 type2_qual = type2_qual.getUnqualifiedType();
1119 return ast->hasSameType (type1_qual, type2_qual);
1124 ClangASTContext::GetTypeForDecl (TagDecl *decl)
1126 // No need to call the getASTContext() accessor (which can create the AST
1127 // if it isn't created yet, because we can't have created a decl in this
1128 // AST if our AST didn't already exist...
1129 ASTContext *ast = m_ast_ap.get();
1131 return ClangASTType (ast, ast->getTagDeclType(decl).getAsOpaquePtr());
1132 return ClangASTType();
1136 ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1138 // No need to call the getASTContext() accessor (which can create the AST
1139 // if it isn't created yet, because we can't have created a decl in this
1140 // AST if our AST didn't already exist...
1141 ASTContext *ast = m_ast_ap.get();
1143 return ClangASTType (ast, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
1144 return ClangASTType();
1147 #pragma mark Structure, Unions, Classes
1150 ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1151 AccessType access_type,
1154 LanguageType language,
1155 ClangASTMetadata *metadata)
1157 ASTContext *ast = getASTContext();
1158 assert (ast != nullptr);
1160 if (decl_ctx == nullptr)
1161 decl_ctx = ast->getTranslationUnitDecl();
1164 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
1166 bool isForwardDecl = true;
1167 bool isInternal = false;
1168 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
1171 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1172 // we will need to update this code. I was told to currently always use
1173 // the CXXRecordDecl class since we often don't know from debug information
1174 // if something is struct or a class, so we default to always use the more
1175 // complete definition just in case.
1177 bool is_anonymous = (!name) || (!name[0]);
1179 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1180 (TagDecl::TagKind)kind,
1184 is_anonymous ? nullptr : &ast->Idents.get(name));
1187 decl->setAnonymousStructOrUnion(true);
1192 SetMetadata(ast, decl, *metadata);
1194 if (access_type != eAccessNone)
1195 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1198 decl_ctx->addDecl (decl);
1200 return ClangASTType(ast, ast->getTagDeclType(decl).getAsOpaquePtr());
1202 return ClangASTType();
1205 static TemplateParameterList *
1206 CreateTemplateParameterList (ASTContext *ast,
1207 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1208 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1210 const bool parameter_pack = false;
1211 const bool is_typename = false;
1212 const unsigned depth = 0;
1213 const size_t num_template_params = template_param_infos.GetSize();
1214 for (size_t i=0; i<num_template_params; ++i)
1216 const char *name = template_param_infos.names[i];
1218 IdentifierInfo *identifier_info = nullptr;
1219 if (name && name[0])
1220 identifier_info = &ast->Idents.get(name);
1221 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
1223 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1224 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1230 template_param_infos.args[i].getIntegralType(),
1237 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1238 ast->getTranslationUnitDecl(), // Is this the right decl context?
1249 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1252 &template_param_decls.front(),
1253 template_param_decls.size(),
1255 return template_param_list;
1258 clang::FunctionTemplateDecl *
1259 ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1260 clang::FunctionDecl *func_decl,
1262 const TemplateParameterInfos &template_param_infos)
1264 // /// \brief Create a function template node.
1265 ASTContext *ast = getASTContext();
1267 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1269 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1270 template_param_infos,
1271 template_param_decls);
1272 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1274 func_decl->getLocation(),
1275 func_decl->getDeclName(),
1276 template_param_list,
1279 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1280 i < template_param_decl_count;
1283 // TODO: verify which decl context we should put template_param_decls into..
1284 template_param_decls[i]->setDeclContext (func_decl);
1287 return func_tmpl_decl;
1291 ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1292 clang::FunctionTemplateDecl *func_tmpl_decl,
1293 const TemplateParameterInfos &infos)
1295 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1299 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1306 ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
1307 lldb::AccessType access_type,
1308 const char *class_name,
1310 const TemplateParameterInfos &template_param_infos)
1312 ASTContext *ast = getASTContext();
1314 ClassTemplateDecl *class_template_decl = nullptr;
1315 if (decl_ctx == nullptr)
1316 decl_ctx = ast->getTranslationUnitDecl();
1318 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1319 DeclarationName decl_name (&identifier_info);
1321 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1323 for (NamedDecl *decl : result)
1325 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1326 if (class_template_decl)
1327 return class_template_decl;
1330 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1332 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1333 template_param_infos,
1334 template_param_decls);
1336 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1337 (TagDecl::TagKind)kind,
1338 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1343 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1344 i < template_param_decl_count;
1347 template_param_decls[i]->setDeclContext (template_cxx_decl);
1350 // With templated classes, we say that a class is templated with
1351 // specializations, but that the bare class has no functions.
1352 //template_cxx_decl->startDefinition();
1353 //template_cxx_decl->completeDefinition();
1355 class_template_decl = ClassTemplateDecl::Create (*ast,
1356 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1359 template_param_list,
1363 if (class_template_decl)
1365 if (access_type != eAccessNone)
1366 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1368 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1369 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1371 decl_ctx->addDecl (class_template_decl);
1373 #ifdef LLDB_CONFIGURATION_DEBUG
1374 VerifyDecl(class_template_decl);
1378 return class_template_decl;
1382 ClassTemplateSpecializationDecl *
1383 ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1384 ClassTemplateDecl *class_template_decl,
1386 const TemplateParameterInfos &template_param_infos)
1388 ASTContext *ast = getASTContext();
1389 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1390 (TagDecl::TagKind)kind,
1394 class_template_decl,
1395 &template_param_infos.args.front(),
1396 template_param_infos.args.size(),
1399 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1401 return class_template_specialization_decl;
1405 ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1407 if (class_template_specialization_decl)
1409 ASTContext *ast = getASTContext();
1411 return ClangASTType(ast, ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr());
1413 return ClangASTType();
1417 check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
1419 // Special-case call since it can take any number of operands
1420 if(op_kind == OO_Call)
1423 // The parameter count doesn't include "this"
1424 if (num_params == 0)
1426 if (num_params == 1)
1433 ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1439 // C++ standard allows any number of arguments to new/delete
1443 case OO_Array_Delete:
1447 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
1450 #include "clang/Basic/OperatorKinds.def"
1456 clang::AccessSpecifier
1457 ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
1459 clang::AccessSpecifier ret = lhs;
1461 // Make the access equal to the stricter of the field and the nested field's access
1464 case clang::AS_none:
1466 case clang::AS_private:
1468 case clang::AS_protected:
1469 if (rhs == AS_private)
1472 case clang::AS_public:
1481 ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1483 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1487 ClangASTContext::FieldIsBitfield
1491 uint32_t& bitfield_bit_size
1494 if (ast == nullptr || field == nullptr)
1497 if (field->isBitField())
1499 Expr* bit_width_expr = field->getBitWidth();
1502 llvm::APSInt bit_width_apsint;
1503 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
1505 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1514 ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1516 if (record_decl == nullptr)
1519 if (!record_decl->field_empty())
1522 // No fields, lets check this is a CXX record and check the base classes
1523 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1524 if (cxx_record_decl)
1526 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1527 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1528 base_class != base_class_end;
1531 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1532 if (RecordHasFields(base_class_decl))
1539 #pragma mark Objective C Classes
1542 ClangASTContext::CreateObjCClass
1545 DeclContext *decl_ctx,
1548 ClangASTMetadata *metadata
1551 ASTContext *ast = getASTContext();
1552 assert (ast != nullptr);
1553 assert (name && name[0]);
1554 if (decl_ctx == nullptr)
1555 decl_ctx = ast->getTranslationUnitDecl();
1557 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
1560 &ast->Idents.get(name),
1566 if (decl && metadata)
1567 SetMetadata(ast, decl, *metadata);
1569 return ClangASTType (ast, ast->getObjCInterfaceType(decl));
1573 BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1575 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
1579 ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
1581 uint32_t num_bases = 0;
1582 if (cxx_record_decl)
1584 if (omit_empty_base_classes)
1586 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1587 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1588 base_class != base_class_end;
1591 // Skip empty base classes
1592 if (omit_empty_base_classes)
1594 if (BaseSpecifierIsEmpty (base_class))
1601 num_bases = cxx_record_decl->getNumBases();
1607 #pragma mark Namespace Declarations
1610 ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
1612 NamespaceDecl *namespace_decl = nullptr;
1613 ASTContext *ast = getASTContext();
1614 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
1615 if (decl_ctx == nullptr)
1616 decl_ctx = translation_unit_decl;
1620 IdentifierInfo &identifier_info = ast->Idents.get(name);
1621 DeclarationName decl_name (&identifier_info);
1622 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1623 for (NamedDecl *decl : result)
1625 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1627 return namespace_decl;
1630 namespace_decl = NamespaceDecl::Create(*ast,
1638 decl_ctx->addDecl (namespace_decl);
1642 if (decl_ctx == translation_unit_decl)
1644 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1646 return namespace_decl;
1648 namespace_decl = NamespaceDecl::Create(*ast,
1655 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1656 translation_unit_decl->addDecl (namespace_decl);
1657 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1661 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1662 if (parent_namespace_decl)
1664 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1666 return namespace_decl;
1667 namespace_decl = NamespaceDecl::Create(*ast,
1674 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1675 parent_namespace_decl->addDecl (namespace_decl);
1676 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1687 // If we make it here, we are creating the anonymous namespace decl
1688 // for the first time, so we need to do the using directive magic
1690 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
1694 NestedNameSpecifierLoc(),
1698 using_directive_decl->setImplicit();
1699 decl_ctx->addDecl(using_directive_decl);
1702 #ifdef LLDB_CONFIGURATION_DEBUG
1703 VerifyDecl(namespace_decl);
1705 return namespace_decl;
1709 #pragma mark Function Types
1712 ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1714 const ClangASTType &function_clang_type,
1718 FunctionDecl *func_decl = nullptr;
1719 ASTContext *ast = getASTContext();
1720 if (decl_ctx == nullptr)
1721 decl_ctx = ast->getTranslationUnitDecl();
1724 const bool hasWrittenPrototype = true;
1725 const bool isConstexprSpecified = false;
1727 if (name && name[0])
1729 func_decl = FunctionDecl::Create (*ast,
1733 DeclarationName (&ast->Idents.get(name)),
1734 function_clang_type.GetQualType(),
1736 (FunctionDecl::StorageClass)storage,
1738 hasWrittenPrototype,
1739 isConstexprSpecified);
1743 func_decl = FunctionDecl::Create (*ast,
1748 function_clang_type.GetQualType(),
1750 (FunctionDecl::StorageClass)storage,
1752 hasWrittenPrototype,
1753 isConstexprSpecified);
1756 decl_ctx->addDecl (func_decl);
1758 #ifdef LLDB_CONFIGURATION_DEBUG
1759 VerifyDecl(func_decl);
1766 ClangASTContext::CreateFunctionType (ASTContext *ast,
1767 const ClangASTType& result_type,
1768 const ClangASTType *args,
1771 unsigned type_quals)
1773 assert (ast != nullptr);
1774 std::vector<QualType> qual_type_args;
1775 for (unsigned i=0; i<num_args; ++i)
1776 qual_type_args.push_back (args[i].GetQualType());
1778 // TODO: Detect calling convention in DWARF?
1779 FunctionProtoType::ExtProtoInfo proto_info;
1780 proto_info.Variadic = is_variadic;
1781 proto_info.ExceptionSpecType = EST_None;
1782 proto_info.TypeQuals = type_quals;
1783 proto_info.RefQualifier = RQ_None;
1784 proto_info.NumExceptions = 0;
1785 proto_info.Exceptions = nullptr;
1787 return ClangASTType (ast, ast->getFunctionType (result_type.GetQualType(),
1789 proto_info).getAsOpaquePtr());
1793 ClangASTContext::CreateParameterDeclaration (const char *name, const ClangASTType ¶m_type, int storage)
1795 ASTContext *ast = getASTContext();
1796 assert (ast != nullptr);
1797 return ParmVarDecl::Create(*ast,
1798 ast->getTranslationUnitDecl(),
1801 name && name[0] ? &ast->Idents.get(name) : nullptr,
1802 param_type.GetQualType(),
1804 (VarDecl::StorageClass)storage,
1809 ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
1812 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
1816 #pragma mark Array Types
1819 ClangASTContext::CreateArrayType (const ClangASTType &element_type,
1820 size_t element_count,
1823 if (element_type.IsValid())
1825 ASTContext *ast = getASTContext();
1826 assert (ast != nullptr);
1830 return ClangASTType (ast, ast->getExtVectorType(element_type.GetQualType(), element_count).getAsOpaquePtr());
1835 llvm::APInt ap_element_count (64, element_count);
1836 if (element_count == 0)
1838 return ClangASTType (ast, ast->getIncompleteArrayType (element_type.GetQualType(),
1840 0).getAsOpaquePtr());
1844 return ClangASTType (ast, ast->getConstantArrayType (element_type.GetQualType(),
1847 0).getAsOpaquePtr());
1851 return ClangASTType();
1856 #pragma mark Enumeration Types
1859 ClangASTContext::CreateEnumerationType
1862 DeclContext *decl_ctx,
1863 const Declaration &decl,
1864 const ClangASTType &integer_clang_type
1867 // TODO: Do something intelligent with the Declaration object passed in
1868 // like maybe filling in the SourceLocation with it...
1869 ASTContext *ast = getASTContext();
1871 // TODO: ask about these...
1872 // const bool IsScoped = false;
1873 // const bool IsFixed = false;
1875 EnumDecl *enum_decl = EnumDecl::Create (*ast,
1879 name && name[0] ? &ast->Idents.get(name) : nullptr,
1882 false, // IsScopedUsingClassTag
1888 // TODO: check if we should be setting the promotion type too?
1889 enum_decl->setIntegerType(integer_clang_type.GetQualType());
1891 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
1893 return ClangASTType (ast, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
1895 return ClangASTType();
1898 // Disable this for now since I can't seem to get a nicely formatted float
1899 // out of the APFloat class without just getting the float, double or quad
1900 // and then using a formatted print on it which defeats the purpose. We ideally
1901 // would like to get perfect string values for any kind of float semantics
1902 // so we can support remote targets. The code below also requires a patch to
1905 //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)
1907 // uint32_t count = 0;
1908 // bool is_complex = false;
1909 // if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
1911 // unsigned num_bytes_per_float = byte_size / count;
1912 // unsigned num_bits_per_float = num_bytes_per_float * 8;
1914 // float_str.clear();
1916 // for (i=0; i<count; i++)
1918 // APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
1919 // bool is_ieee = false;
1920 // APFloat ap_float(ap_int, is_ieee);
1922 // unsigned int hex_digits = 0;
1923 // bool upper_case = false;
1925 // if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
1928 // float_str.append(", ");
1929 // float_str.append(s);
1930 // if (i == 1 && is_complex)
1931 // float_str.append(1, 'i');
1934 // return !float_str.empty();
1940 ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
1941 size_t bit_size, bool is_signed)
1947 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
1948 return ClangASTType(ast, ast->SignedCharTy.getAsOpaquePtr());
1950 if (bit_size == ast->getTypeSize(ast->ShortTy))
1951 return ClangASTType(ast, ast->ShortTy.getAsOpaquePtr());
1953 if (bit_size == ast->getTypeSize(ast->IntTy))
1954 return ClangASTType(ast, ast->IntTy.getAsOpaquePtr());
1956 if (bit_size == ast->getTypeSize(ast->LongTy))
1957 return ClangASTType(ast, ast->LongTy.getAsOpaquePtr());
1959 if (bit_size == ast->getTypeSize(ast->LongLongTy))
1960 return ClangASTType(ast, ast->LongLongTy.getAsOpaquePtr());
1962 if (bit_size == ast->getTypeSize(ast->Int128Ty))
1963 return ClangASTType(ast, ast->Int128Ty.getAsOpaquePtr());
1967 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
1968 return ClangASTType(ast, ast->UnsignedCharTy.getAsOpaquePtr());
1970 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
1971 return ClangASTType(ast, ast->UnsignedShortTy.getAsOpaquePtr());
1973 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
1974 return ClangASTType(ast, ast->UnsignedIntTy.getAsOpaquePtr());
1976 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
1977 return ClangASTType(ast, ast->UnsignedLongTy.getAsOpaquePtr());
1979 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
1980 return ClangASTType(ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
1982 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
1983 return ClangASTType(ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
1986 return ClangASTType();
1990 ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
1993 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
1994 return ClangASTType();
1998 ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2003 if (bit_size == ast->getTypeSize(ast->FloatTy))
2004 return ClangASTType(ast, ast->FloatTy.getAsOpaquePtr());
2005 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
2006 return ClangASTType(ast, ast->DoubleTy.getAsOpaquePtr());
2007 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
2008 return ClangASTType(ast, ast->LongDoubleTy.getAsOpaquePtr());
2009 else if (bit_size == ast->getTypeSize(ast->HalfTy))
2010 return ClangASTType(ast, ast->HalfTy.getAsOpaquePtr());
2012 return ClangASTType();
2016 ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2022 ExternalASTSource *ast_source = ast->getExternalSource();
2027 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2029 if (tag_decl->isCompleteDefinition())
2032 if (!tag_decl->hasExternalLexicalStorage())
2035 ast_source->CompleteType(tag_decl);
2037 return !tag_decl->getTypeForDecl()->isIncompleteType();
2039 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2041 if (objc_interface_decl->getDefinition())
2044 if (!objc_interface_decl->hasExternalLexicalStorage())
2047 ast_source->CompleteType(objc_interface_decl);
2049 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2058 ClangASTContext::SetMetadataAsUserID (const void *object,
2061 ClangASTMetadata meta_data;
2062 meta_data.SetUserID (user_id);
2063 SetMetadata (object, meta_data);
2067 ClangASTContext::SetMetadata (clang::ASTContext *ast,
2069 ClangASTMetadata &metadata)
2071 ClangExternalASTSourceCommon *external_source =
2072 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
2074 if (external_source)
2075 external_source->SetMetadata(object, metadata);
2079 ClangASTContext::GetMetadata (clang::ASTContext *ast,
2082 ClangExternalASTSourceCommon *external_source =
2083 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
2085 if (external_source && external_source->HasMetadata(object))
2086 return external_source->GetMetadata(object);
2091 clang::DeclContext *
2092 ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2094 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2097 clang::DeclContext *
2098 ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2100 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2105 ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
2106 lldb::LanguageType &language,
2107 bool &is_instance_method,
2108 ConstString &language_object_name)
2110 language_object_name.Clear();
2111 language = eLanguageTypeUnknown;
2112 is_instance_method = false;
2116 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
2118 if (method_decl->isStatic())
2120 is_instance_method = false;
2124 language_object_name.SetCString("this");
2125 is_instance_method = true;
2127 language = eLanguageTypeC_plus_plus;
2130 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
2132 // Both static and instance methods have a "self" object in objective C
2133 language_object_name.SetCString("self");
2134 if (method_decl->isInstanceMethod())
2136 is_instance_method = true;
2140 is_instance_method = false;
2142 language = eLanguageTypeObjC;
2145 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
2147 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
2148 if (metadata && metadata->HasObjectPtr())
2150 language_object_name.SetCString (metadata->GetObjectPtrName());
2151 language = eLanguageTypeObjC;
2152 is_instance_method = true;