blob: aeba626190c2b5318dec547384f67a27f940ede3 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000017
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
27
Sean Callanan3b1d4f62011-10-26 17:46:51 +000028#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000029#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000030#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000031// Need to include assert.h so it is as clang would expect it to be (disabled)
32#include <assert.h>
33#endif
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "clang/AST/ASTContext.h"
36#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000037#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000039#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000040#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "clang/AST/RecordLayout.h"
42#include "clang/AST/Type.h"
43#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000044#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000046#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#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"
Greg Clayton6beaaa62011-01-17 03:46:26 +000052
53#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000054#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000055#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
56// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
57#include <assert.h>
58#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Clayton514487e2011-02-15 21:59:32 +000060#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000062#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000063#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000064#include "lldb/Core/RegularExpression.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000065#include "lldb/Core/UniqueCStringMap.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000066#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000067#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000068#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000069#include "lldb/Target/ExecutionContext.h"
70#include "lldb/Target/Process.h"
71#include "lldb/Target/ObjCLanguageRuntime.h"
72
Eli Friedman932197d2010-06-13 19:06:42 +000073#include <stdio.h>
74
Greg Clayton1341baf2013-07-11 23:36:31 +000075#include <mutex>
76
Greg Claytonc86103d2010-08-05 01:57:25 +000077using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078using namespace lldb_private;
79using namespace llvm;
80using namespace clang;
81
Greg Clayton57ee3062013-07-11 22:46:58 +000082clang::AccessSpecifier
83ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +000084{
85 switch (access)
86 {
Greg Claytonc86103d2010-08-05 01:57:25 +000087 default: break;
88 case eAccessNone: return AS_none;
89 case eAccessPublic: return AS_public;
90 case eAccessPrivate: return AS_private;
91 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +000092 }
93 return AS_none;
94}
95
Greg Clayton8cf05932010-07-22 18:30:50 +000096
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097static void
98ParseLangArgs
99(
100 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000101 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102)
103{
104 // FIXME: Cleanup per-file based stuff.
105
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.
Greg Clayton94e5d782010-06-13 17:34:29 +0000109 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000111 } else if (IK == IK_ObjC ||
112 IK == IK_ObjCXX ||
113 IK == IK_PreprocessedObjC ||
114 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 Opts.ObjC1 = Opts.ObjC2 = 1;
116 }
117
118 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
119
120 if (LangStd == LangStandard::lang_unspecified) {
121 // Based on the base language, pick one.
122 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000123 case IK_None:
124 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000125 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000126 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000127 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 LangStd = LangStandard::lang_opencl;
129 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000130 case IK_CUDA:
131 LangStd = LangStandard::lang_cuda;
132 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000133 case IK_Asm:
134 case IK_C:
135 case IK_PreprocessedC:
136 case IK_ObjC:
137 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 LangStd = LangStandard::lang_gnu99;
139 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000140 case IK_CXX:
141 case IK_PreprocessedCXX:
142 case IK_ObjCXX:
143 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 LangStd = LangStandard::lang_gnucxx98;
145 break;
146 }
147 }
148
149 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000150 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 Opts.C99 = Std.isC99();
152 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000153 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 Opts.Digraphs = Std.hasDigraphs();
155 Opts.GNUMode = Std.isGNUMode();
156 Opts.GNUInline = !Std.isC99();
157 Opts.HexFloats = Std.hasHexFloats();
158 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000159
160 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161
162 // OpenCL has some additional defaults.
163 if (LangStd == LangStandard::lang_opencl) {
164 Opts.OpenCL = 1;
165 Opts.AltiVec = 1;
166 Opts.CXXOperatorNames = 1;
167 Opts.LaxVectorConversions = 1;
168 }
169
170 // OpenCL and C++ both have bool, true, false keywords.
171 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
172
173// if (Opts.CPlusPlus)
174// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
175//
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);
180//
181// if (Args.hasArg(OPT_print_ivar_layout))
182// Opts.ObjCGCBitmapPrint = 1;
183//
184// if (Args.hasArg(OPT_faltivec))
185// Opts.AltiVec = 1;
186//
187// if (Args.hasArg(OPT_pthread))
188// Opts.POSIXThreads = 1;
189//
190// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
191// "default");
192// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000193 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194// else if (Vis == "hidden")
195// Opts.setVisibilityMode(LangOptions::Hidden);
196// else if (Vis == "protected")
197// Opts.setVisibilityMode(LangOptions::Protected);
198// else
199// Diags.Report(diag::err_drv_invalid_value)
200// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
201
202// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
203
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;
209//
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,
231// Diags);
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;
241
242 // FIXME: Eliminate this dependency.
243// unsigned Opt =
244// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
245// Opts.Optimize = Opt != 0;
246 unsigned Opt = 0;
247
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
250 // inlining enabled.
251 //
252 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000253 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254
255// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
256// switch (SSP) {
257// default:
258// Diags.Report(diag::err_drv_invalid_value)
259// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
260// break;
261// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
262// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
263// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
264// }
265}
266
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
Greg Clayton6beaaa62011-01-17 03:46:26 +0000268ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000270 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271 m_language_options_ap(),
272 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000273 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000274 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 m_target_info_ap(),
276 m_identifier_table_ap(),
277 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000278 m_builtins_ap(),
279 m_callback_tag_decl (NULL),
280 m_callback_objc_decl (NULL),
Greg Clayton57ee3062013-07-11 22:46:58 +0000281 m_callback_baton (NULL),
282 m_pointer_byte_size (0)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000283
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284{
285 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000286 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287}
288
289//----------------------------------------------------------------------
290// Destructor
291//----------------------------------------------------------------------
292ClangASTContext::~ClangASTContext()
293{
294 m_builtins_ap.reset();
295 m_selector_table_ap.reset();
296 m_identifier_table_ap.reset();
297 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000298 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000299 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 m_source_manager_ap.reset();
301 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000302 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303}
304
305
306void
307ClangASTContext::Clear()
308{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000309 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 m_language_options_ap.reset();
311 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000312 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000313 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 m_target_info_ap.reset();
315 m_identifier_table_ap.reset();
316 m_selector_table_ap.reset();
317 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000318 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
321const char *
322ClangASTContext::GetTargetTriple ()
323{
324 return m_target_triple.c_str();
325}
326
327void
328ClangASTContext::SetTargetTriple (const char *target_triple)
329{
330 Clear();
331 m_target_triple.assign(target_triple);
332}
333
Greg Clayton514487e2011-02-15 21:59:32 +0000334void
335ClangASTContext::SetArchitecture (const ArchSpec &arch)
336{
Greg Clayton880cbb02011-07-30 01:26:02 +0000337 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000338}
339
Greg Clayton6beaaa62011-01-17 03:46:26 +0000340bool
341ClangASTContext::HasExternalSource ()
342{
343 ASTContext *ast = getASTContext();
344 if (ast)
345 return ast->getExternalSource () != NULL;
346 return false;
347}
348
349void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000350ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000351{
352 ASTContext *ast = getASTContext();
353 if (ast)
354 {
355 ast->setExternalSource (ast_source_ap);
356 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
357 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
358 }
359}
360
361void
362ClangASTContext::RemoveExternalSource ()
363{
364 ASTContext *ast = getASTContext();
365
366 if (ast)
367 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000368 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000369 ast->setExternalSource (empty_ast_source_ap);
370 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
371 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
372 }
373}
374
375
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376
377ASTContext *
378ClangASTContext::getASTContext()
379{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000380 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000382 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
383 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000384 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000385 *getIdentifierTable(),
386 *getSelectorTable(),
387 *getBuiltinContext(),
388 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000389
Greg Clayton6beaaa62011-01-17 03:46:26 +0000390 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
391 {
392 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
393 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
394 }
395
Sean Callanan880e6802011-10-07 23:18:13 +0000396 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000398 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
401Builtin::Context *
402ClangASTContext::getBuiltinContext()
403{
404 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000405 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 return m_builtins_ap.get();
407}
408
409IdentifierTable *
410ClangASTContext::getIdentifierTable()
411{
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();
415}
416
417LangOptions *
418ClangASTContext::getLanguageOptions()
419{
420 if (m_language_options_ap.get() == NULL)
421 {
422 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000423 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
424// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 }
426 return m_language_options_ap.get();
427}
428
429SelectorTable *
430ClangASTContext::getSelectorTable()
431{
432 if (m_selector_table_ap.get() == NULL)
433 m_selector_table_ap.reset (new SelectorTable());
434 return m_selector_table_ap.get();
435}
436
Sean Callanan79439e82010-11-18 02:56:27 +0000437clang::FileManager *
438ClangASTContext::getFileManager()
439{
440 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000441 {
442 clang::FileSystemOptions file_system_options;
443 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
444 }
Sean Callanan79439e82010-11-18 02:56:27 +0000445 return m_file_manager_ap.get();
446}
447
Greg Claytone1a916a2010-07-21 22:12:05 +0000448clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449ClangASTContext::getSourceManager()
450{
451 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000452 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 return m_source_manager_ap.get();
454}
455
Sean Callanan880e6802011-10-07 23:18:13 +0000456clang::DiagnosticsEngine *
457ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
Sean Callanan880e6802011-10-07 23:18:13 +0000459 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000460 {
461 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000462 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000463 }
Sean Callanan880e6802011-10-07 23:18:13 +0000464 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465}
466
Sean Callanan880e6802011-10-07 23:18:13 +0000467class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000468{
469public:
Sean Callanan880e6802011-10-07 23:18:13 +0000470 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000471 {
472 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
473 }
474
Sean Callanan880e6802011-10-07 23:18:13 +0000475 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000476 {
477 if (m_log)
478 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000479 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000480 info.FormatDiagnostic(diag_str);
481 diag_str.push_back('\0');
482 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
483 }
484 }
Sean Callanan880e6802011-10-07 23:18:13 +0000485
486 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
487 {
488 return new NullDiagnosticConsumer ();
489 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000490private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000491 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000492};
493
Sean Callanan880e6802011-10-07 23:18:13 +0000494DiagnosticConsumer *
495ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000496{
Sean Callanan880e6802011-10-07 23:18:13 +0000497 if (m_diagnostic_consumer_ap.get() == NULL)
498 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000499
Sean Callanan880e6802011-10-07 23:18:13 +0000500 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000501}
502
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503TargetOptions *
504ClangASTContext::getTargetOptions()
505{
Sean Callananc5069ad2012-10-17 22:11:14 +0000506 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000508 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000513 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514}
515
516
517TargetInfo *
518ClangASTContext::getTargetInfo()
519{
Greg Clayton70512312012-05-08 01:45:38 +0000520 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000522 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 return m_target_info_ap.get();
524}
525
526#pragma mark Basic Types
527
528static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000529QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000531 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 if (qual_type_bit_size == bit_size)
533 return true;
534 return false;
535}
Greg Clayton57ee3062013-07-11 22:46:58 +0000536ClangASTType
Greg Claytonc86103d2010-08-05 01:57:25 +0000537ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538{
Greg Clayton57ee3062013-07-11 22:46:58 +0000539 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540}
541
Greg Clayton57ee3062013-07-11 22:46:58 +0000542ClangASTType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000543ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000545 if (!ast)
Greg Clayton57ee3062013-07-11 22:46:58 +0000546 return ClangASTType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547
548 switch (encoding)
549 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000550 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000551 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000552 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 break;
554
Greg Claytonc86103d2010-08-05 01:57:25 +0000555 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000556 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000557 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000558 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000559 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000560 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000561 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000562 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000563 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000564 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000565 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000566 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Clayton57ee3062013-07-11 22:46:58 +0000567 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 break;
569
Greg Claytonc86103d2010-08-05 01:57:25 +0000570 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000571 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000572 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000573 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000574 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000575 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000576 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000577 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000578 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000579 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000580 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000581 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Clayton57ee3062013-07-11 22:46:58 +0000582 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 break;
584
Greg Claytonc86103d2010-08-05 01:57:25 +0000585 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000586 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000587 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000588 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000589 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000590 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000591 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 break;
593
Greg Claytonc86103d2010-08-05 01:57:25 +0000594 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000595 // Sanity check that bit_size is a multiple of 8's.
596 if (bit_size && !(bit_size & 0x7u))
Greg Clayton57ee3062013-07-11 22:46:58 +0000597 return ClangASTType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr());
Johnny Chenc79c93a2012-03-07 01:12:24 +0000598 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 }
600
Greg Clayton57ee3062013-07-11 22:46:58 +0000601 return ClangASTType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Greg Clayton57ee3062013-07-11 22:46:58 +0000604
605
606lldb::BasicType
607ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
608{
609 if (name)
610 {
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, [](){
615 // "void"
616 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
617
618 // "char"
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);
625 // "short"
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);
630
631 // "int"
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);
636
637 // "long"
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);
642
643 // "long long"
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);
648
649 // "int128"
650 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
651 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
652
653 // Miscelaneous
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);
661 g_type_map.Sort();
662 });
663
664 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
665 }
666 return eBasicTypeInvalid;
667}
668
669ClangASTType
670ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
671{
672 if (ast)
673 {
674 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
675 return ClangASTContext::GetBasicType (ast, basic_type);
676 }
677 return ClangASTType();
678}
679
680uint32_t
681ClangASTContext::GetPointerByteSize ()
682{
683 if (m_pointer_byte_size == 0)
684 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize();
685 return m_pointer_byte_size;
686}
687
688ClangASTType
689ClangASTContext::GetBasicType (lldb::BasicType basic_type)
690{
691 return GetBasicType (getASTContext(), basic_type);
692}
693
694ClangASTType
695ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
696{
697 if (ast)
698 {
699 clang_type_t clang_type = NULL;
700
701 switch (basic_type)
702 {
703 case eBasicTypeInvalid:
704 case eBasicTypeOther:
705 break;
706 case eBasicTypeVoid:
707 clang_type = ast->VoidTy.getAsOpaquePtr();
708 break;
709 case eBasicTypeChar:
710 clang_type = ast->CharTy.getAsOpaquePtr();
711 break;
712 case eBasicTypeSignedChar:
713 clang_type = ast->SignedCharTy.getAsOpaquePtr();
714 break;
715 case eBasicTypeUnsignedChar:
716 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
717 break;
718 case eBasicTypeWChar:
719 clang_type = ast->getWCharType().getAsOpaquePtr();
720 break;
721 case eBasicTypeSignedWChar:
722 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
723 break;
724 case eBasicTypeUnsignedWChar:
725 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
726 break;
727 case eBasicTypeChar16:
728 clang_type = ast->Char16Ty.getAsOpaquePtr();
729 break;
730 case eBasicTypeChar32:
731 clang_type = ast->Char32Ty.getAsOpaquePtr();
732 break;
733 case eBasicTypeShort:
734 clang_type = ast->ShortTy.getAsOpaquePtr();
735 break;
736 case eBasicTypeUnsignedShort:
737 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
738 break;
739 case eBasicTypeInt:
740 clang_type = ast->IntTy.getAsOpaquePtr();
741 break;
742 case eBasicTypeUnsignedInt:
743 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
744 break;
745 case eBasicTypeLong:
746 clang_type = ast->LongTy.getAsOpaquePtr();
747 break;
748 case eBasicTypeUnsignedLong:
749 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
750 break;
751 case eBasicTypeLongLong:
752 clang_type = ast->LongLongTy.getAsOpaquePtr();
753 break;
754 case eBasicTypeUnsignedLongLong:
755 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
756 break;
757 case eBasicTypeInt128:
758 clang_type = ast->Int128Ty.getAsOpaquePtr();
759 break;
760 case eBasicTypeUnsignedInt128:
761 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
762 break;
763 case eBasicTypeBool:
764 clang_type = ast->BoolTy.getAsOpaquePtr();
765 break;
766 case eBasicTypeHalf:
767 clang_type = ast->HalfTy.getAsOpaquePtr();
768 break;
769 case eBasicTypeFloat:
770 clang_type = ast->FloatTy.getAsOpaquePtr();
771 break;
772 case eBasicTypeDouble:
773 clang_type = ast->DoubleTy.getAsOpaquePtr();
774 break;
775 case eBasicTypeLongDouble:
776 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
777 break;
778 case eBasicTypeFloatComplex:
779 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
780 break;
781 case eBasicTypeDoubleComplex:
782 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
783 break;
784 case eBasicTypeLongDoubleComplex:
785 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
786 break;
787 case eBasicTypeObjCID:
788 clang_type = ast->getObjCIdType().getAsOpaquePtr();
789 break;
790 case eBasicTypeObjCClass:
791 clang_type = ast->getObjCClassType().getAsOpaquePtr();
792 break;
793 case eBasicTypeObjCSel:
794 clang_type = ast->getObjCSelType().getAsOpaquePtr();
795 break;
796 case eBasicTypeNullPtr:
797 clang_type = ast->NullPtrTy.getAsOpaquePtr();
798 break;
799 }
800
801 if (clang_type)
802 return ClangASTType (ast, clang_type);
803 }
804 return ClangASTType();
805}
806
807
808ClangASTType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
810{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000811 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000812
813#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000814 assert (ast != NULL);
815 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 {
817 switch (dw_ate)
818 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000819 default:
820 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000821
Sean Callanan38d4df52012-04-03 01:10:10 +0000822 case DW_ATE_address:
823 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000824 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000825 break;
826
827 case DW_ATE_boolean:
828 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000829 return ClangASTType (ast, ast->BoolTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000830 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000831 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000832 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000833 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000834 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000835 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000836 break;
837
838 case DW_ATE_lo_user:
839 // This has been seen to mean DW_AT_complex_integer
840 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000841 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000842 if (::strstr(type_name, "complex"))
843 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000844 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());
Sean Callanan38d4df52012-04-03 01:10:10 +0000846 }
Greg Clayton605684e2011-10-28 23:06:08 +0000847 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000848 break;
849
850 case DW_ATE_complex_float:
851 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000852 return ClangASTType (ast, ast->FloatComplexTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000853 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000854 return ClangASTType (ast, ast->DoubleComplexTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000855 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000856 return ClangASTType (ast, ast->LongDoubleComplexTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000857 else
Greg Clayton605684e2011-10-28 23:06:08 +0000858 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000859 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());
Greg Clayton605684e2011-10-28 23:06:08 +0000861 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000862 break;
863
864 case DW_ATE_float:
865 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000866 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000867 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000868 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000869 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000870 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000871 break;
872
873 case DW_ATE_signed:
874 if (type_name)
875 {
876 if (streq(type_name, "wchar_t") &&
877 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000878 return ClangASTType (ast, ast->WCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000879 if (streq(type_name, "void") &&
880 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000881 return ClangASTType (ast, ast->VoidTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000882 if (strstr(type_name, "long long") &&
883 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000884 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000885 if (strstr(type_name, "long") &&
886 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000887 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000888 if (strstr(type_name, "short") &&
889 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000890 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000891 if (strstr(type_name, "char"))
892 {
893 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000894 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000895 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000896 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000897 }
898 if (strstr(type_name, "int"))
899 {
900 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000901 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000902 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Clayton57ee3062013-07-11 22:46:58 +0000903 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000904 }
905 }
906 // We weren't able to match up a type name, just search by size
907 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000908 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000909 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000910 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000911 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000912 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000913 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000914 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000915 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000916 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000917 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Clayton57ee3062013-07-11 22:46:58 +0000918 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000919 break;
920
921 case DW_ATE_signed_char:
922 if (type_name)
923 {
924 if (streq(type_name, "signed char"))
925 {
926 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000927 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000928 }
929 }
930 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000931 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000932 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000933 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000934 break;
935
936 case DW_ATE_unsigned:
937 if (type_name)
938 {
939 if (strstr(type_name, "long long"))
940 {
941 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000942 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000943 }
944 else if (strstr(type_name, "long"))
945 {
946 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000947 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000948 }
949 else if (strstr(type_name, "short"))
950 {
951 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000952 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000953 }
954 else if (strstr(type_name, "char"))
955 {
956 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000957 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000958 }
959 else if (strstr(type_name, "int"))
960 {
961 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000962 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000963 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Clayton57ee3062013-07-11 22:46:58 +0000964 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000965 }
966 }
967 // We weren't able to match up a type name, just search by size
968 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000969 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000970 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000971 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000972 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000973 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000974 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000975 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000976 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000977 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000978 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Clayton57ee3062013-07-11 22:46:58 +0000979 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000980 break;
981
982 case DW_ATE_unsigned_char:
983 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000984 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000985 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Clayton57ee3062013-07-11 22:46:58 +0000986 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000987 break;
988
989 case DW_ATE_imaginary_float:
990 break;
991
992 case DW_ATE_UTF:
993 if (type_name)
994 {
995 if (streq(type_name, "char16_t"))
996 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000997 return ClangASTType (ast, ast->Char16Ty.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +0000998 }
999 else if (streq(type_name, "char32_t"))
1000 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001001 return ClangASTType (ast, ast->Char32Ty.getAsOpaquePtr());
Sean Callanan38d4df52012-04-03 01:10:10 +00001002 }
1003 }
1004 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005 }
1006 }
1007 // This assert should fire for anything that we don't catch above so we know
1008 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001009 if (type_name)
1010 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001011 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);
Greg Claytondc968d12011-05-17 18:15:05 +00001012 }
1013 else
1014 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001015 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);
Greg Claytondc968d12011-05-17 18:15:05 +00001016 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001017 return ClangASTType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018}
1019
Greg Clayton57ee3062013-07-11 22:46:58 +00001020ClangASTType
Sean Callanan77502262011-05-12 23:54:16 +00001021ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1022{
Greg Clayton57ee3062013-07-11 22:46:58 +00001023 if (ast)
1024 return ClangASTType (ast, ast->UnknownAnyTy.getAsOpaquePtr());
1025 return ClangASTType();
Sean Callanan77502262011-05-12 23:54:16 +00001026}
1027
Greg Clayton57ee3062013-07-11 22:46:58 +00001028ClangASTType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029ClangASTContext::GetCStringType (bool is_const)
1030{
Greg Clayton57ee3062013-07-11 22:46:58 +00001031 ASTContext *ast = getASTContext();
1032 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001033
1034 if (is_const)
1035 char_type.addConst();
1036
Greg Clayton57ee3062013-07-11 22:46:58 +00001037 return ClangASTType (ast, ast->getPointerType(char_type).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038}
1039
Sean Callanan09ab4b72011-11-30 22:11:59 +00001040clang::DeclContext *
1041ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1042{
1043 return ast->getTranslationUnitDecl();
1044}
1045
Greg Clayton57ee3062013-07-11 22:46:58 +00001046ClangASTType
Greg Clayton38a61402010-12-02 23:20:03 +00001047ClangASTContext::CopyType (ASTContext *dst_ast,
Greg Clayton57ee3062013-07-11 22:46:58 +00001048 ClangASTType src)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049{
Sean Callanan79439e82010-11-18 02:56:27 +00001050 FileSystemOptions file_system_options;
Greg Clayton57ee3062013-07-11 22:46:58 +00001051 ASTContext *src_ast = src.GetASTContext();
Greg Clayton38a61402010-12-02 23:20:03 +00001052 FileManager file_manager (file_system_options);
1053 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001054 *src_ast, file_manager,
1055 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001056
Greg Clayton57ee3062013-07-11 22:46:58 +00001057 QualType dst (importer.Import(src.GetQualType()));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001058
Greg Clayton57ee3062013-07-11 22:46:58 +00001059 return ClangASTType (dst_ast, dst.getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001060}
1061
Greg Clayton526e5af2010-11-13 03:52:47 +00001062
1063clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001064ClangASTContext::CopyDecl (ASTContext *dst_ast,
1065 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001066 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001067{
Sean Callanan79439e82010-11-18 02:56:27 +00001068 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001069 FileManager file_manager (file_system_options);
1070 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001071 *src_ast, file_manager,
1072 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001073
1074 return importer.Import(source_decl);
1075}
1076
Sean Callanan23a30272010-07-16 00:00:27 +00001077bool
Greg Clayton57ee3062013-07-11 22:46:58 +00001078ClangASTContext::AreTypesSame (ClangASTType type1,
1079 ClangASTType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001080 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001081{
Greg Clayton57ee3062013-07-11 22:46:58 +00001082 ASTContext *ast = type1.GetASTContext();
1083 if (ast != type2.GetASTContext())
1084 return false;
1085
1086 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001087 return true;
1088
Greg Clayton57ee3062013-07-11 22:46:58 +00001089 QualType type1_qual = type1.GetQualType();
1090 QualType type2_qual = type2.GetQualType();
Sean Callanan5056ab02012-02-18 02:01:03 +00001091
1092 if (ignore_qualifiers)
1093 {
1094 type1_qual = type1_qual.getUnqualifiedType();
1095 type2_qual = type2_qual.getUnqualifiedType();
1096 }
1097
Greg Clayton57ee3062013-07-11 22:46:58 +00001098 return ast->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099}
1100
Greg Clayton6beaaa62011-01-17 03:46:26 +00001101
Greg Clayton57ee3062013-07-11 22:46:58 +00001102ClangASTType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001103ClangASTContext::GetTypeForDecl (TagDecl *decl)
1104{
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...
Greg Clayton57ee3062013-07-11 22:46:58 +00001108 ASTContext *ast = m_ast_ap.get();
1109 if (ast)
1110 return ClangASTType (ast, ast->getTagDeclType(decl).getAsOpaquePtr());
1111 return ClangASTType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001112}
1113
Greg Clayton57ee3062013-07-11 22:46:58 +00001114ClangASTType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001115ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1116{
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...
Greg Clayton57ee3062013-07-11 22:46:58 +00001120 ASTContext *ast = m_ast_ap.get();
1121 if (ast)
1122 return ClangASTType (ast, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
1123 return ClangASTType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001124}
1125
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126#pragma mark Structure, Unions, Classes
1127
Greg Clayton57ee3062013-07-11 22:46:58 +00001128ClangASTType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001129ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1130 AccessType access_type,
1131 const char *name,
1132 int kind,
1133 LanguageType language,
1134 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001136 ASTContext *ast = getASTContext();
1137 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001140 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141
Greg Clayton9e409562010-07-28 02:04:09 +00001142
Greg Claytone1be9962011-08-24 23:50:00 +00001143 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001144 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001145 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001146 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001147 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001148 }
1149
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150 // 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.
Sean Callanan11e32d32014-02-18 00:31:38 +00001155
1156 bool is_anonymous = (!name) || (!name[0]);
1157
Greg Claytonf0705c82011-10-22 03:33:13 +00001158 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1159 (TagDecl::TagKind)kind,
1160 decl_ctx,
1161 SourceLocation(),
1162 SourceLocation(),
Sean Callanan11e32d32014-02-18 00:31:38 +00001163 is_anonymous ? NULL : &ast->Idents.get(name));
1164
1165 if (is_anonymous)
1166 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001167
Greg Claytonc4ffd662013-03-08 01:37:30 +00001168 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001169 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001170 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001171 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001172
Greg Clayton55561e92011-10-26 03:31:36 +00001173 if (access_type != eAccessNone)
1174 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001175
1176 if (decl_ctx)
1177 decl_ctx->addDecl (decl);
1178
Greg Clayton57ee3062013-07-11 22:46:58 +00001179 return ClangASTType(ast, ast->getTagDeclType(decl).getAsOpaquePtr());
Greg Clayton55561e92011-10-26 03:31:36 +00001180 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001181 return ClangASTType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001182}
1183
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001184static TemplateParameterList *
1185CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001186 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001187 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1188{
1189 const bool parameter_pack = false;
1190 const bool is_typename = false;
1191 const unsigned depth = 0;
1192 const size_t num_template_params = template_param_infos.GetSize();
1193 for (size_t i=0; i<num_template_params; ++i)
1194 {
1195 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001196
1197 IdentifierInfo *identifier_info = NULL;
1198 if (name && name[0])
1199 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001200 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001201 {
1202 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1203 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1204 SourceLocation(),
1205 SourceLocation(),
1206 depth,
1207 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001208 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001209 template_param_infos.args[i].getIntegralType(),
1210 parameter_pack,
1211 NULL));
1212
1213 }
1214 else
1215 {
1216 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1217 ast->getTranslationUnitDecl(), // Is this the right decl context?
1218 SourceLocation(),
1219 SourceLocation(),
1220 depth,
1221 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001222 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001223 is_typename,
1224 parameter_pack));
1225 }
1226 }
1227
1228 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1229 SourceLocation(),
1230 SourceLocation(),
1231 &template_param_decls.front(),
1232 template_param_decls.size(),
1233 SourceLocation());
1234 return template_param_list;
1235}
1236
1237clang::FunctionTemplateDecl *
1238ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1239 clang::FunctionDecl *func_decl,
1240 const char *name,
1241 const TemplateParameterInfos &template_param_infos)
1242{
1243// /// \brief Create a function template node.
1244 ASTContext *ast = getASTContext();
1245
1246 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1247
1248 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1249 template_param_infos,
1250 template_param_decls);
1251 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1252 decl_ctx,
1253 func_decl->getLocation(),
1254 func_decl->getDeclName(),
1255 template_param_list,
1256 func_decl);
1257
1258 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1259 i < template_param_decl_count;
1260 ++i)
1261 {
1262 // TODO: verify which decl context we should put template_param_decls into..
1263 template_param_decls[i]->setDeclContext (func_decl);
1264 }
1265
1266 return func_tmpl_decl;
1267}
1268
1269void
1270ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1271 clang::FunctionTemplateDecl *func_tmpl_decl,
1272 const TemplateParameterInfos &infos)
1273{
1274 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1275 infos.args.data(),
1276 infos.args.size());
1277
1278 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1279 &template_args,
1280 NULL);
1281}
1282
1283
Greg Claytonf0705c82011-10-22 03:33:13 +00001284ClassTemplateDecl *
1285ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001286 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001287 const char *class_name,
1288 int kind,
1289 const TemplateParameterInfos &template_param_infos)
1290{
1291 ASTContext *ast = getASTContext();
1292
1293 ClassTemplateDecl *class_template_decl = NULL;
1294 if (decl_ctx == NULL)
1295 decl_ctx = ast->getTranslationUnitDecl();
1296
1297 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1298 DeclarationName decl_name (&identifier_info);
1299
1300 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001301
1302 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001303 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001304 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001305 if (class_template_decl)
1306 return class_template_decl;
1307 }
1308
1309 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001310
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001311 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1312 template_param_infos,
1313 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001314
1315 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1316 (TagDecl::TagKind)kind,
1317 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1318 SourceLocation(),
1319 SourceLocation(),
1320 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001321
1322 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1323 i < template_param_decl_count;
1324 ++i)
1325 {
1326 template_param_decls[i]->setDeclContext (template_cxx_decl);
1327 }
1328
Sean Callananb5c79622011-11-19 01:35:08 +00001329 // With templated classes, we say that a class is templated with
1330 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001331 //template_cxx_decl->startDefinition();
1332 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001333
Greg Claytonf0705c82011-10-22 03:33:13 +00001334 class_template_decl = ClassTemplateDecl::Create (*ast,
1335 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1336 SourceLocation(),
1337 decl_name,
1338 template_param_list,
1339 template_cxx_decl,
1340 NULL);
1341
1342 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001343 {
Greg Clayton55561e92011-10-26 03:31:36 +00001344 if (access_type != eAccessNone)
1345 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001346
1347 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1348 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1349
Greg Claytonf0705c82011-10-22 03:33:13 +00001350 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001351
1352#ifdef LLDB_CONFIGURATION_DEBUG
1353 VerifyDecl(class_template_decl);
1354#endif
1355 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001356
1357 return class_template_decl;
1358}
1359
1360
1361ClassTemplateSpecializationDecl *
1362ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1363 ClassTemplateDecl *class_template_decl,
1364 int kind,
1365 const TemplateParameterInfos &template_param_infos)
1366{
1367 ASTContext *ast = getASTContext();
1368 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1369 (TagDecl::TagKind)kind,
1370 decl_ctx,
1371 SourceLocation(),
1372 SourceLocation(),
1373 class_template_decl,
1374 &template_param_infos.args.front(),
1375 template_param_infos.args.size(),
1376 NULL);
1377
Sean Callananfa4fab72013-02-01 06:55:48 +00001378 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1379
Greg Claytonf0705c82011-10-22 03:33:13 +00001380 return class_template_specialization_decl;
1381}
1382
Greg Clayton57ee3062013-07-11 22:46:58 +00001383ClangASTType
Greg Claytonf0705c82011-10-22 03:33:13 +00001384ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1385{
1386 if (class_template_specialization_decl)
1387 {
1388 ASTContext *ast = getASTContext();
1389 if (ast)
Greg Clayton57ee3062013-07-11 22:46:58 +00001390 return ClangASTType(ast, ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr());
Greg Claytonf0705c82011-10-22 03:33:13 +00001391 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001392 return ClangASTType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393}
1394
Greg Claytona3c444a2010-10-01 23:13:49 +00001395static bool
1396IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1397{
1398 if (name == NULL || name[0] == '\0')
1399 return false;
1400
Sean Callanana43f20d2010-12-10 19:51:54 +00001401#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001402#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001403
1404 const char *post_op_name = NULL;
1405
Sean Callanana43f20d2010-12-10 19:51:54 +00001406 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001407
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001408 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001409 return false;
1410
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001411 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1412
Sean Callanana43f20d2010-12-10 19:51:54 +00001413 if (post_op_name[0] == ' ')
1414 {
1415 post_op_name++;
1416 no_space = false;
1417 }
1418
1419#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001420#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001421
Greg Claytona3c444a2010-10-01 23:13:49 +00001422 // This is an operator, set the overloaded operator kind to invalid
1423 // in case this is a conversion operator...
1424 op_kind = NUM_OVERLOADED_OPERATORS;
1425
1426 switch (post_op_name[0])
1427 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001428 default:
1429 if (no_space)
1430 return false;
1431 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001432 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001433 if (no_space)
1434 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001435 if (strcmp (post_op_name, "new") == 0)
1436 op_kind = OO_New;
1437 else if (strcmp (post_op_name, "new[]") == 0)
1438 op_kind = OO_Array_New;
1439 break;
1440
1441 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001442 if (no_space)
1443 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001444 if (strcmp (post_op_name, "delete") == 0)
1445 op_kind = OO_Delete;
1446 else if (strcmp (post_op_name, "delete[]") == 0)
1447 op_kind = OO_Array_Delete;
1448 break;
1449
1450 case '+':
1451 if (post_op_name[1] == '\0')
1452 op_kind = OO_Plus;
1453 else if (post_op_name[2] == '\0')
1454 {
1455 if (post_op_name[1] == '=')
1456 op_kind = OO_PlusEqual;
1457 else if (post_op_name[1] == '+')
1458 op_kind = OO_PlusPlus;
1459 }
1460 break;
1461
1462 case '-':
1463 if (post_op_name[1] == '\0')
1464 op_kind = OO_Minus;
1465 else if (post_op_name[2] == '\0')
1466 {
1467 switch (post_op_name[1])
1468 {
1469 case '=': op_kind = OO_MinusEqual; break;
1470 case '-': op_kind = OO_MinusMinus; break;
1471 case '>': op_kind = OO_Arrow; break;
1472 }
1473 }
1474 else if (post_op_name[3] == '\0')
1475 {
1476 if (post_op_name[2] == '*')
1477 op_kind = OO_ArrowStar; break;
1478 }
1479 break;
1480
1481 case '*':
1482 if (post_op_name[1] == '\0')
1483 op_kind = OO_Star;
1484 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1485 op_kind = OO_StarEqual;
1486 break;
1487
1488 case '/':
1489 if (post_op_name[1] == '\0')
1490 op_kind = OO_Slash;
1491 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1492 op_kind = OO_SlashEqual;
1493 break;
1494
1495 case '%':
1496 if (post_op_name[1] == '\0')
1497 op_kind = OO_Percent;
1498 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1499 op_kind = OO_PercentEqual;
1500 break;
1501
1502
1503 case '^':
1504 if (post_op_name[1] == '\0')
1505 op_kind = OO_Caret;
1506 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1507 op_kind = OO_CaretEqual;
1508 break;
1509
1510 case '&':
1511 if (post_op_name[1] == '\0')
1512 op_kind = OO_Amp;
1513 else if (post_op_name[2] == '\0')
1514 {
1515 switch (post_op_name[1])
1516 {
1517 case '=': op_kind = OO_AmpEqual; break;
1518 case '&': op_kind = OO_AmpAmp; break;
1519 }
1520 }
1521 break;
1522
1523 case '|':
1524 if (post_op_name[1] == '\0')
1525 op_kind = OO_Pipe;
1526 else if (post_op_name[2] == '\0')
1527 {
1528 switch (post_op_name[1])
1529 {
1530 case '=': op_kind = OO_PipeEqual; break;
1531 case '|': op_kind = OO_PipePipe; break;
1532 }
1533 }
1534 break;
1535
1536 case '~':
1537 if (post_op_name[1] == '\0')
1538 op_kind = OO_Tilde;
1539 break;
1540
1541 case '!':
1542 if (post_op_name[1] == '\0')
1543 op_kind = OO_Exclaim;
1544 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1545 op_kind = OO_ExclaimEqual;
1546 break;
1547
1548 case '=':
1549 if (post_op_name[1] == '\0')
1550 op_kind = OO_Equal;
1551 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1552 op_kind = OO_EqualEqual;
1553 break;
1554
1555 case '<':
1556 if (post_op_name[1] == '\0')
1557 op_kind = OO_Less;
1558 else if (post_op_name[2] == '\0')
1559 {
1560 switch (post_op_name[1])
1561 {
1562 case '<': op_kind = OO_LessLess; break;
1563 case '=': op_kind = OO_LessEqual; break;
1564 }
1565 }
1566 else if (post_op_name[3] == '\0')
1567 {
1568 if (post_op_name[2] == '=')
1569 op_kind = OO_LessLessEqual;
1570 }
1571 break;
1572
1573 case '>':
1574 if (post_op_name[1] == '\0')
1575 op_kind = OO_Greater;
1576 else if (post_op_name[2] == '\0')
1577 {
1578 switch (post_op_name[1])
1579 {
1580 case '>': op_kind = OO_GreaterGreater; break;
1581 case '=': op_kind = OO_GreaterEqual; break;
1582 }
1583 }
1584 else if (post_op_name[1] == '>' &&
1585 post_op_name[2] == '=' &&
1586 post_op_name[3] == '\0')
1587 {
1588 op_kind = OO_GreaterGreaterEqual;
1589 }
1590 break;
1591
1592 case ',':
1593 if (post_op_name[1] == '\0')
1594 op_kind = OO_Comma;
1595 break;
1596
1597 case '(':
1598 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1599 op_kind = OO_Call;
1600 break;
1601
1602 case '[':
1603 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1604 op_kind = OO_Subscript;
1605 break;
1606 }
1607
1608 return true;
1609}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001610
Greg Clayton090d0982011-06-19 03:43:27 +00001611static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001612check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001613{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001614 // Special-case call since it can take any number of operands
1615 if(op_kind == OO_Call)
1616 return true;
1617
Greg Clayton090d0982011-06-19 03:43:27 +00001618 // The parameter count doens't include "this"
1619 if (num_params == 0)
1620 return unary;
1621 if (num_params == 1)
1622 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001623 else
Greg Clayton090d0982011-06-19 03:43:27 +00001624 return false;
1625}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001626
Greg Clayton090d0982011-06-19 03:43:27 +00001627bool
1628ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1629{
Sean Callanan5b26f272012-02-04 08:49:35 +00001630 switch (op_kind)
1631 {
1632 default:
1633 break;
1634 // C++ standard allows any number of arguments to new/delete
1635 case OO_New:
1636 case OO_Array_New:
1637 case OO_Delete:
1638 case OO_Array_Delete:
1639 return true;
1640 }
1641
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001642#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
Greg Clayton090d0982011-06-19 03:43:27 +00001643 switch (op_kind)
1644 {
1645#include "clang/Basic/OperatorKinds.def"
1646 default: break;
1647 }
1648 return false;
1649}
1650
Greg Clayton57ee3062013-07-11 22:46:58 +00001651clang::AccessSpecifier
1652ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001653{
1654 clang::AccessSpecifier ret = lhs;
1655
1656 // Make the access equal to the stricter of the field and the nested field's access
1657 switch (ret)
1658 {
1659 case clang::AS_none:
1660 break;
1661 case clang::AS_private:
1662 break;
1663 case clang::AS_protected:
1664 if (rhs == AS_private)
1665 ret = AS_private;
1666 break;
1667 case clang::AS_public:
1668 ret = rhs;
1669 break;
1670 }
1671
1672 return ret;
1673}
1674
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675bool
1676ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1677{
1678 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1679}
1680
1681bool
1682ClangASTContext::FieldIsBitfield
1683(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001684 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685 FieldDecl* field,
1686 uint32_t& bitfield_bit_size
1687)
1688{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001689 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 return false;
1691
1692 if (field->isBitField())
1693 {
1694 Expr* bit_width_expr = field->getBitWidth();
1695 if (bit_width_expr)
1696 {
1697 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001698 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 {
1700 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1701 return true;
1702 }
1703 }
1704 }
1705 return false;
1706}
1707
1708bool
1709ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1710{
1711 if (record_decl == NULL)
1712 return false;
1713
1714 if (!record_decl->field_empty())
1715 return true;
1716
1717 // No fields, lets check this is a CXX record and check the base classes
1718 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1719 if (cxx_record_decl)
1720 {
1721 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1722 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1723 base_class != base_class_end;
1724 ++base_class)
1725 {
1726 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1727 if (RecordHasFields(base_class_decl))
1728 return true;
1729 }
1730 }
1731 return false;
1732}
1733
Greg Clayton8cf05932010-07-22 18:30:50 +00001734#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735
Greg Clayton57ee3062013-07-11 22:46:58 +00001736ClangASTType
Sean Callananad880762012-04-18 01:06:17 +00001737ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001738(
1739 const char *name,
1740 DeclContext *decl_ctx,
1741 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001742 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001743 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001744)
1745{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001746 ASTContext *ast = getASTContext();
1747 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00001748 assert (name && name[0]);
1749 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001750 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001751
Greg Clayton6beaaa62011-01-17 03:46:26 +00001752 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001753 decl_ctx,
1754 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001755 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00001756 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00001757 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001758 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001759 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001760
Jim Ingham379397632012-10-27 02:54:13 +00001761 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001762 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001763
Greg Clayton57ee3062013-07-11 22:46:58 +00001764 return ClangASTType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765}
1766
1767static inline bool
1768BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1769{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001770 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771}
1772
Greg Clayton57ee3062013-07-11 22:46:58 +00001773uint32_t
1774ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775{
1776 uint32_t num_bases = 0;
1777 if (cxx_record_decl)
1778 {
1779 if (omit_empty_base_classes)
1780 {
1781 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1782 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1783 base_class != base_class_end;
1784 ++base_class)
1785 {
1786 // Skip empty base classes
1787 if (omit_empty_base_classes)
1788 {
1789 if (BaseSpecifierIsEmpty (base_class))
1790 continue;
1791 }
1792 ++num_bases;
1793 }
1794 }
1795 else
1796 num_bases = cxx_record_decl->getNumBases();
1797 }
1798 return num_bases;
1799}
1800
1801
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802#pragma mark Namespace Declarations
1803
1804NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001805ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001806{
Greg Clayton030a2042011-10-14 21:34:45 +00001807 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001808 ASTContext *ast = getASTContext();
1809 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
1810 if (decl_ctx == NULL)
1811 decl_ctx = translation_unit_decl;
1812
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813 if (name)
1814 {
Greg Clayton030a2042011-10-14 21:34:45 +00001815 IdentifierInfo &identifier_info = ast->Idents.get(name);
1816 DeclarationName decl_name (&identifier_info);
1817 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001818 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001819 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001820 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001821 if (namespace_decl)
1822 return namespace_decl;
1823 }
1824
Sean Callanan5b26f272012-02-04 08:49:35 +00001825 namespace_decl = NamespaceDecl::Create(*ast,
1826 decl_ctx,
1827 false,
1828 SourceLocation(),
1829 SourceLocation(),
1830 &identifier_info,
1831 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00001832
Greg Clayton9d3d6882011-10-31 23:51:19 +00001833 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001834 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001835 else
1836 {
1837 if (decl_ctx == translation_unit_decl)
1838 {
1839 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1840 if (namespace_decl)
1841 return namespace_decl;
1842
Sean Callanan5b26f272012-02-04 08:49:35 +00001843 namespace_decl = NamespaceDecl::Create(*ast,
1844 decl_ctx,
1845 false,
1846 SourceLocation(),
1847 SourceLocation(),
1848 NULL,
1849 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001850 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1851 translation_unit_decl->addDecl (namespace_decl);
1852 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1853 }
1854 else
1855 {
1856 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1857 if (parent_namespace_decl)
1858 {
1859 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1860 if (namespace_decl)
1861 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001862 namespace_decl = NamespaceDecl::Create(*ast,
1863 decl_ctx,
1864 false,
1865 SourceLocation(),
1866 SourceLocation(),
1867 NULL,
1868 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001869 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1870 parent_namespace_decl->addDecl (namespace_decl);
1871 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1872 }
1873 else
1874 {
1875 // BAD!!!
1876 }
1877 }
1878
1879
1880 if (namespace_decl)
1881 {
1882 // If we make it here, we are creating the anonymous namespace decl
1883 // for the first time, so we need to do the using directive magic
1884 // like SEMA does
1885 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
1886 decl_ctx,
1887 SourceLocation(),
1888 SourceLocation(),
1889 NestedNameSpecifierLoc(),
1890 SourceLocation(),
1891 namespace_decl,
1892 decl_ctx);
1893 using_directive_decl->setImplicit();
1894 decl_ctx->addDecl(using_directive_decl);
1895 }
1896 }
1897#ifdef LLDB_CONFIGURATION_DEBUG
1898 VerifyDecl(namespace_decl);
1899#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001900 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001901}
1902
1903
1904#pragma mark Function Types
1905
1906FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001907ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1908 const char *name,
1909 const ClangASTType &function_clang_type,
1910 int storage,
1911 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912{
Greg Clayton147e1fa2011-10-14 22:47:18 +00001913 FunctionDecl *func_decl = NULL;
1914 ASTContext *ast = getASTContext();
1915 if (decl_ctx == NULL)
1916 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917
Greg Clayton0d551042013-06-28 21:08:47 +00001918
1919 const bool hasWrittenPrototype = true;
1920 const bool isConstexprSpecified = false;
1921
Greg Clayton147e1fa2011-10-14 22:47:18 +00001922 if (name && name[0])
1923 {
1924 func_decl = FunctionDecl::Create (*ast,
1925 decl_ctx,
1926 SourceLocation(),
1927 SourceLocation(),
1928 DeclarationName (&ast->Idents.get(name)),
Greg Clayton57ee3062013-07-11 22:46:58 +00001929 function_clang_type.GetQualType(),
Greg Clayton147e1fa2011-10-14 22:47:18 +00001930 NULL,
1931 (FunctionDecl::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001932 is_inline,
1933 hasWrittenPrototype,
1934 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001936 else
1937 {
1938 func_decl = FunctionDecl::Create (*ast,
1939 decl_ctx,
1940 SourceLocation(),
1941 SourceLocation(),
1942 DeclarationName (),
Greg Clayton57ee3062013-07-11 22:46:58 +00001943 function_clang_type.GetQualType(),
Greg Clayton147e1fa2011-10-14 22:47:18 +00001944 NULL,
1945 (FunctionDecl::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001946 is_inline,
1947 hasWrittenPrototype,
1948 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001949 }
1950 if (func_decl)
1951 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001952
1953#ifdef LLDB_CONFIGURATION_DEBUG
1954 VerifyDecl(func_decl);
1955#endif
1956
Greg Clayton147e1fa2011-10-14 22:47:18 +00001957 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001958}
1959
Greg Clayton57ee3062013-07-11 22:46:58 +00001960ClangASTType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001961ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton57ee3062013-07-11 22:46:58 +00001962 const ClangASTType& result_type,
1963 const ClangASTType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00001964 unsigned num_args,
1965 bool is_variadic,
1966 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001967{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001968 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969 std::vector<QualType> qual_type_args;
1970 for (unsigned i=0; i<num_args; ++i)
Greg Clayton57ee3062013-07-11 22:46:58 +00001971 qual_type_args.push_back (args[i].GetQualType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972
1973 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00001974 FunctionProtoType::ExtProtoInfo proto_info;
1975 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00001976 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001977 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00001978 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001979 proto_info.NumExceptions = 0;
1980 proto_info.Exceptions = NULL;
1981
Greg Clayton57ee3062013-07-11 22:46:58 +00001982 return ClangASTType (ast, ast->getFunctionType (result_type.GetQualType(),
1983 qual_type_args,
1984 proto_info).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985}
1986
1987ParmVarDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001988ClangASTContext::CreateParameterDeclaration (const char *name, const ClangASTType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001990 ASTContext *ast = getASTContext();
1991 assert (ast != NULL);
1992 return ParmVarDecl::Create(*ast,
1993 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001995 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001996 name && name[0] ? &ast->Idents.get(name) : NULL,
Greg Clayton57ee3062013-07-11 22:46:58 +00001997 param_type.GetQualType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998 NULL,
1999 (VarDecl::StorageClass)storage,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002000 0);
2001}
2002
2003void
2004ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2005{
2006 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00002007 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002008}
2009
2010
2011#pragma mark Array Types
2012
Greg Clayton57ee3062013-07-11 22:46:58 +00002013ClangASTType
2014ClangASTContext::CreateArrayType (const ClangASTType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00002015 size_t element_count,
2016 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002017{
Greg Clayton57ee3062013-07-11 22:46:58 +00002018 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002020 ASTContext *ast = getASTContext();
2021 assert (ast != NULL);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002022
Greg Clayton1c8ef472013-04-05 23:27:21 +00002023 if (is_vector)
2024 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002025 return ClangASTType (ast, ast->getExtVectorType(element_type.GetQualType(), element_count).getAsOpaquePtr());
Greg Clayton4ef877f2012-12-06 02:33:54 +00002026 }
2027 else
2028 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002029
2030 llvm::APInt ap_element_count (64, element_count);
2031 if (element_count == 0)
2032 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002033 return ClangASTType (ast, ast->getIncompleteArrayType (element_type.GetQualType(),
2034 ArrayType::Normal,
2035 0).getAsOpaquePtr());
Greg Clayton1c8ef472013-04-05 23:27:21 +00002036 }
2037 else
2038 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002039 return ClangASTType (ast, ast->getConstantArrayType (element_type.GetQualType(),
2040 ap_element_count,
2041 ArrayType::Normal,
2042 0).getAsOpaquePtr());
Greg Clayton1c8ef472013-04-05 23:27:21 +00002043 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00002044 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002045 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002046 return ClangASTType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047}
2048
2049
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002050
2051#pragma mark Enumeration Types
2052
Greg Clayton57ee3062013-07-11 22:46:58 +00002053ClangASTType
Greg Claytonca512b32011-01-14 04:54:56 +00002054ClangASTContext::CreateEnumerationType
2055(
2056 const char *name,
2057 DeclContext *decl_ctx,
2058 const Declaration &decl,
Greg Clayton57ee3062013-07-11 22:46:58 +00002059 const ClangASTType &integer_clang_type
Greg Claytonca512b32011-01-14 04:54:56 +00002060)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061{
2062 // TODO: Do something intelligent with the Declaration object passed in
2063 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002064 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00002065
2066 // TODO: ask about these...
2067// const bool IsScoped = false;
2068// const bool IsFixed = false;
2069
Greg Clayton6beaaa62011-01-17 03:46:26 +00002070 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002071 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002072 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002073 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002074 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00002075 NULL,
2076 false, // IsScoped
2077 false, // IsScopedUsingClassTag
2078 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002079
2080
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002081 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002082 {
2083 // TODO: check if we should be setting the promotion type too?
Greg Clayton57ee3062013-07-11 22:46:58 +00002084 enum_decl->setIntegerType(integer_clang_type.GetQualType());
Sean Callanan2652ad22011-01-18 01:03:44 +00002085
2086 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2087
Greg Clayton57ee3062013-07-11 22:46:58 +00002088 return ClangASTType (ast, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
Greg Clayton83ff3892010-09-12 23:17:56 +00002089 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002090 return ClangASTType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002091}
2092
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002093// Disable this for now since I can't seem to get a nicely formatted float
2094// out of the APFloat class without just getting the float, double or quad
2095// and then using a formatted print on it which defeats the purpose. We ideally
2096// would like to get perfect string values for any kind of float semantics
2097// so we can support remote targets. The code below also requires a patch to
2098// llvm::APInt.
2099//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00002100//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)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002101//{
2102// uint32_t count = 0;
2103// bool is_complex = false;
2104// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2105// {
2106// unsigned num_bytes_per_float = byte_size / count;
2107// unsigned num_bits_per_float = num_bytes_per_float * 8;
2108//
2109// float_str.clear();
2110// uint32_t i;
2111// for (i=0; i<count; i++)
2112// {
2113// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2114// bool is_ieee = false;
2115// APFloat ap_float(ap_int, is_ieee);
2116// char s[1024];
2117// unsigned int hex_digits = 0;
2118// bool upper_case = false;
2119//
2120// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2121// {
2122// if (i > 0)
2123// float_str.append(", ");
2124// float_str.append(s);
2125// if (i == 1 && is_complex)
2126// float_str.append(1, 'i');
2127// }
2128// }
2129// return !float_str.empty();
2130// }
2131// return false;
2132//}
2133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134
Greg Clayton57ee3062013-07-11 22:46:58 +00002135ClangASTType
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002136ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2137 size_t bit_size)
2138{
2139 if (ast)
2140 {
2141 if (bit_size == ast->getTypeSize(ast->FloatTy))
Greg Clayton57ee3062013-07-11 22:46:58 +00002142 return ClangASTType(ast, ast->FloatTy.getAsOpaquePtr());
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002143 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
Greg Clayton57ee3062013-07-11 22:46:58 +00002144 return ClangASTType(ast, ast->DoubleTy.getAsOpaquePtr());
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002145 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
Greg Clayton57ee3062013-07-11 22:46:58 +00002146 return ClangASTType(ast, ast->LongDoubleTy.getAsOpaquePtr());
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002147 else if (bit_size == ast->getTypeSize(ast->HalfTy))
Greg Clayton57ee3062013-07-11 22:46:58 +00002148 return ClangASTType(ast, ast->HalfTy.getAsOpaquePtr());
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002149 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002150 return ClangASTType();
Enrico Granata86027e92012-03-24 01:11:14 +00002151}
2152
2153bool
Greg Claytona2721472011-06-25 00:44:06 +00002154ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2155 clang::Decl *decl)
2156{
2157 if (!decl)
2158 return false;
2159
2160 ExternalASTSource *ast_source = ast->getExternalSource();
2161
2162 if (!ast_source)
2163 return false;
2164
2165 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2166 {
Greg Clayton219cf312012-03-30 00:51:13 +00002167 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002168 return true;
2169
2170 if (!tag_decl->hasExternalLexicalStorage())
2171 return false;
2172
2173 ast_source->CompleteType(tag_decl);
2174
2175 return !tag_decl->getTypeForDecl()->isIncompleteType();
2176 }
2177 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2178 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002179 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002180 return true;
2181
2182 if (!objc_interface_decl->hasExternalLexicalStorage())
2183 return false;
2184
2185 ast_source->CompleteType(objc_interface_decl);
2186
Sean Callanan5b26f272012-02-04 08:49:35 +00002187 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002188 }
2189 else
2190 {
2191 return false;
2192 }
2193}
2194
Sean Callanan60217122012-04-13 00:10:03 +00002195void
Greg Claytond0029442013-03-27 01:48:02 +00002196ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002197 user_id_t user_id)
2198{
2199 ClangASTMetadata meta_data;
2200 meta_data.SetUserID (user_id);
2201 SetMetadata (object, meta_data);
2202}
2203
2204void
Sean Callanan60217122012-04-13 00:10:03 +00002205ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002206 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002207 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002208{
2209 ClangExternalASTSourceCommon *external_source =
2210 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
2211
2212 if (external_source)
2213 external_source->SetMetadata(object, metadata);
2214}
2215
Jim Ingham379397632012-10-27 02:54:13 +00002216ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002217ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002218 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002219{
2220 ClangExternalASTSourceCommon *external_source =
2221 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
2222
2223 if (external_source && external_source->HasMetadata(object))
2224 return external_source->GetMetadata(object);
2225 else
Jim Ingham379397632012-10-27 02:54:13 +00002226 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00002227}
2228
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002229clang::DeclContext *
2230ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2231{
Sean Callanana87bee82011-08-19 06:19:25 +00002232 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002233}
2234
2235clang::DeclContext *
2236ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2237{
Sean Callanana87bee82011-08-19 06:19:25 +00002238 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002239}
2240
Greg Clayton685c88c2012-07-14 00:53:55 +00002241
2242bool
2243ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
2244 lldb::LanguageType &language,
2245 bool &is_instance_method,
2246 ConstString &language_object_name)
2247{
2248 language_object_name.Clear();
2249 language = eLanguageTypeUnknown;
2250 is_instance_method = false;
2251
2252 if (decl_ctx)
2253 {
2254 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
2255 {
2256 if (method_decl->isStatic())
2257 {
2258 is_instance_method = false;
2259 }
2260 else
2261 {
2262 language_object_name.SetCString("this");
2263 is_instance_method = true;
2264 }
2265 language = eLanguageTypeC_plus_plus;
2266 return true;
2267 }
2268 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
2269 {
2270 // Both static and instance methods have a "self" object in objective C
2271 language_object_name.SetCString("self");
2272 if (method_decl->isInstanceMethod())
2273 {
2274 is_instance_method = true;
2275 }
2276 else
2277 {
2278 is_instance_method = false;
2279 }
2280 language = eLanguageTypeObjC;
2281 return true;
2282 }
Jim Ingham379397632012-10-27 02:54:13 +00002283 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
2284 {
Greg Claytond0029442013-03-27 01:48:02 +00002285 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00002286 if (metadata && metadata->HasObjectPtr())
2287 {
2288 language_object_name.SetCString (metadata->GetObjectPtrName());
2289 language = eLanguageTypeObjC;
2290 is_instance_method = true;
2291 }
2292 return true;
2293 }
Greg Clayton685c88c2012-07-14 00:53:55 +00002294 }
2295 return false;
2296}
2297