]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/llvm/tools/lldb/source/Expression/ClangExpressionDeclMap.cpp
MFC r258054: Update LLDB to upstream r194122 snapshot
[FreeBSD/stable/10.git] / contrib / llvm / tools / lldb / source / Expression / ClangExpressionDeclMap.cpp
1 //===-- ClangExpressionDeclMap.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
10 #include "lldb/Expression/ClangExpressionDeclMap.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Decl.h"
19 #include "lldb/lldb-private.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/Error.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Core/RegisterValue.h"
26 #include "lldb/Core/ValueObjectConstResult.h"
27 #include "lldb/Core/ValueObjectVariable.h"
28 #include "lldb/Expression/ASTDumper.h"
29 #include "lldb/Expression/ClangASTSource.h"
30 #include "lldb/Expression/ClangPersistentVariables.h"
31 #include "lldb/Expression/Materializer.h"
32 #include "lldb/Host/Endian.h"
33 #include "lldb/Symbol/ClangASTContext.h"
34 #include "lldb/Symbol/ClangNamespaceDecl.h"
35 #include "lldb/Symbol/CompileUnit.h"
36 #include "lldb/Symbol/Function.h"
37 #include "lldb/Symbol/ObjectFile.h"
38 #include "lldb/Symbol/SymbolContext.h"
39 #include "lldb/Symbol/SymbolVendor.h"
40 #include "lldb/Symbol/Type.h"
41 #include "lldb/Symbol/TypeList.h"
42 #include "lldb/Symbol/Variable.h"
43 #include "lldb/Symbol/VariableList.h"
44 #include "lldb/Target/ExecutionContext.h"
45 #include "lldb/Target/ObjCLanguageRuntime.h"
46 #include "lldb/Target/Process.h"
47 #include "lldb/Target/RegisterContext.h"
48 #include "lldb/Target/StackFrame.h"
49 #include "lldb/Target/Target.h"
50 #include "lldb/Target/Thread.h"
51
52 using namespace lldb;
53 using namespace lldb_private;
54 using namespace clang;
55
56 ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, ExecutionContext &exe_ctx) :
57     ClangASTSource (exe_ctx.GetTargetSP()),
58     m_found_entities (),
59     m_struct_members (),
60     m_keep_result_in_memory (keep_result_in_memory),
61     m_parser_vars (),
62     m_struct_vars ()
63 {
64     EnableStructVars();
65 }
66
67 ClangExpressionDeclMap::~ClangExpressionDeclMap()
68 {
69     // Note: The model is now that the parser's AST context and all associated
70     //   data does not vanish until the expression has been executed.  This means
71     //   that valuable lookup data (like namespaces) doesn't vanish, but 
72     
73     DidParse();
74     DisableStructVars();
75 }
76
77 bool 
78 ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
79                                   Materializer *materializer)
80 {
81     ClangASTMetrics::ClearLocalCounters();
82     
83     EnableParserVars();
84     m_parser_vars->m_exe_ctx = exe_ctx;
85     
86     Target *target = exe_ctx.GetTargetPtr();
87     if (exe_ctx.GetFramePtr())
88         m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
89     else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0))
90         m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything);
91     else if (exe_ctx.GetProcessPtr())
92     {
93         m_parser_vars->m_sym_ctx.Clear(true);
94         m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
95     }
96     else if (target)
97     {
98         m_parser_vars->m_sym_ctx.Clear(true);
99         m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
100     }
101     
102     if (target)
103     {
104         m_parser_vars->m_persistent_vars = &target->GetPersistentVariables();
105     
106         if (!target->GetScratchClangASTContext())
107             return false;
108     }
109     
110     m_parser_vars->m_target_info = GetTargetInfo();
111     m_parser_vars->m_materializer = materializer;
112     
113     return true;
114 }
115
116 void
117 ClangExpressionDeclMap::DidParse()
118 {
119     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
120
121     if (log)
122         ClangASTMetrics::DumpCounters(log);
123     
124     if (m_parser_vars.get())
125     {
126         for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
127              entity_index < num_entities;
128              ++entity_index)
129         {
130             ClangExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index));
131             if (var_sp)
132                 var_sp->DisableParserVars(GetParserID());
133         }
134         
135         for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize();
136              pvar_index < num_pvars;
137              ++pvar_index)
138         {
139             ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
140             if (pvar_sp)
141                 pvar_sp->DisableParserVars(GetParserID());
142         }
143         
144         DisableParserVars();
145     }
146 }
147
148 // Interface for IRForTarget
149
150 ClangExpressionDeclMap::TargetInfo 
151 ClangExpressionDeclMap::GetTargetInfo()
152 {
153     assert (m_parser_vars.get());
154     
155     TargetInfo ret;
156     
157     ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
158
159     Process *process = exe_ctx.GetProcessPtr();
160     if (process)
161     {
162         ret.byte_order = process->GetByteOrder();
163         ret.address_byte_size = process->GetAddressByteSize();
164     }
165     else 
166     {
167         Target *target = exe_ctx.GetTargetPtr();
168         if (target)
169         {
170             ret.byte_order = target->GetArchitecture().GetByteOrder();
171             ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
172         }
173     }
174
175     return ret;
176 }
177
178 bool 
179 ClangExpressionDeclMap::AddPersistentVariable 
180 (
181     const NamedDecl *decl, 
182     const ConstString &name, 
183     TypeFromParser parser_type,
184     bool is_result,
185     bool is_lvalue
186 )
187 {
188     assert (m_parser_vars.get());
189     
190     if (m_parser_vars->m_materializer && is_result)
191     {
192         Error err;
193         
194         ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
195         Target *target = exe_ctx.GetTargetPtr();
196         if (target == NULL)
197             return false;
198         
199         ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
200         
201         TypeFromUser user_type(m_ast_importer->DeportType(context,
202                                                           parser_type.GetASTContext(),
203                                                           parser_type.GetOpaqueQualType()),
204                                context);
205         
206         uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, is_lvalue, m_keep_result_in_memory, err);
207         
208         ClangExpressionVariableSP var_sp = m_found_entities.CreateVariable(exe_ctx.GetBestExecutionContextScope(),
209                                                                            name,
210                                                                            user_type,
211                                                                            m_parser_vars->m_target_info.byte_order,
212                                                                            m_parser_vars->m_target_info.address_byte_size);
213         
214         if (!var_sp)
215             return false;
216         
217         var_sp->EnableParserVars(GetParserID());
218         
219         ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
220
221         parser_vars->m_named_decl = decl;
222         parser_vars->m_parser_type = parser_type;
223         
224         var_sp->EnableJITVars(GetParserID());
225         
226         ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
227         
228         jit_vars->m_offset = offset;
229         
230         return true;
231     }
232     
233     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
234     ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
235     Target *target = exe_ctx.GetTargetPtr();
236     if (target == NULL)
237         return false;
238
239     ASTContext *context(target->GetScratchClangASTContext()->getASTContext());
240     
241     TypeFromUser user_type(m_ast_importer->DeportType(context, 
242                                                       parser_type.GetASTContext(),
243                                                       parser_type.GetOpaqueQualType()),
244                            context);
245     
246     if (!user_type.GetOpaqueQualType())
247     {
248         if (log)
249             log->Printf("Persistent variable's type wasn't copied successfully");
250         return false;
251     }
252         
253     if (!m_parser_vars->m_target_info.IsValid())
254         return false;
255     
256     ClangExpressionVariableSP var_sp = m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (),
257                                                                                                    name,
258                                                                                                    user_type,
259                                                                                                    m_parser_vars->m_target_info.byte_order,
260                                                                                                    m_parser_vars->m_target_info.address_byte_size);
261     
262     if (!var_sp)
263         return false;
264     
265     var_sp->m_frozen_sp->SetHasCompleteType();
266     
267     if (is_result)
268         var_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
269     else
270         var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist
271     
272     if (is_lvalue)
273     {
274         var_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
275     }
276     else
277     {
278         var_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
279         var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
280     }
281     
282     if (m_keep_result_in_memory)
283     {
284         var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
285     }
286     
287     if (log)
288         log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
289     
290     var_sp->EnableParserVars(GetParserID());
291     
292     ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
293     
294     parser_vars->m_named_decl = decl;
295     parser_vars->m_parser_type = parser_type;
296     
297     return true;
298 }
299
300 bool 
301 ClangExpressionDeclMap::AddValueToStruct 
302 (
303     const NamedDecl *decl,
304     const ConstString &name,
305     llvm::Value *value,
306     size_t size,
307     off_t alignment
308 )
309 {
310     assert (m_struct_vars.get());
311     assert (m_parser_vars.get());
312     
313     bool is_persistent_variable = false;
314     
315     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
316     
317     m_struct_vars->m_struct_laid_out = false;
318     
319     if (m_struct_members.GetVariable(decl, GetParserID()))
320         return true;
321     
322     ClangExpressionVariableSP var_sp (m_found_entities.GetVariable(decl, GetParserID()));
323     
324     if (!var_sp)
325     {
326         var_sp = m_parser_vars->m_persistent_vars->GetVariable(decl, GetParserID());
327         is_persistent_variable = true;
328     }
329     
330     if (!var_sp)
331         return false;
332     
333     if (log)
334         log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure",
335                     decl,
336                     name.GetCString(),
337                     var_sp->GetName().GetCString());
338     
339     // We know entity->m_parser_vars is valid because we used a parser variable
340     // to find it
341     
342     ClangExpressionVariable::ParserVars *parser_vars = var_sp->GetParserVars(GetParserID());
343
344     parser_vars->m_llvm_value = value;
345     
346     if (ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID()))
347     {
348         // We already laid this out; do not touch
349         
350         if (log)
351             log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset);
352     }
353     
354     var_sp->EnableJITVars(GetParserID());
355     
356     ClangExpressionVariable::JITVars *jit_vars = var_sp->GetJITVars(GetParserID());
357
358     jit_vars->m_alignment = alignment;
359     jit_vars->m_size = size;
360     
361     m_struct_members.AddVariable(var_sp);
362     
363     if (m_parser_vars->m_materializer)
364     {
365         uint32_t offset = 0;
366
367         Error err;
368
369         if (is_persistent_variable)
370         {
371             offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, err);
372         }
373         else
374         {
375             if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
376                 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
377             else if (const RegisterInfo *reg_info = var_sp->GetRegisterInfo())
378                 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
379             else if (parser_vars->m_lldb_var)
380                 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err);
381         }
382         
383         if (!err.Success())
384             return false;
385         
386         if (log)
387             log->Printf("Placed at 0x%llx", (unsigned long long)offset);
388         
389         jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this.
390     }
391     
392     return true;
393 }
394
395 bool
396 ClangExpressionDeclMap::DoStructLayout ()
397 {
398     assert (m_struct_vars.get());
399     
400     if (m_struct_vars->m_struct_laid_out)
401         return true;
402     
403     if (!m_parser_vars->m_materializer)
404         return false;
405     
406     m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment();
407     m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize();
408     m_struct_vars->m_struct_laid_out = true;
409     return true;
410 }
411
412 bool ClangExpressionDeclMap::GetStructInfo 
413 (
414     uint32_t &num_elements,
415     size_t &size,
416     off_t &alignment
417 )
418 {
419     assert (m_struct_vars.get());
420     
421     if (!m_struct_vars->m_struct_laid_out)
422         return false;
423     
424     num_elements = m_struct_members.GetSize();
425     size = m_struct_vars->m_struct_size;
426     alignment = m_struct_vars->m_struct_alignment;
427     
428     return true;
429 }
430
431 bool 
432 ClangExpressionDeclMap::GetStructElement 
433 (
434     const NamedDecl *&decl,
435     llvm::Value *&value,
436     off_t &offset,
437     ConstString &name,
438     uint32_t index
439 )
440 {
441     assert (m_struct_vars.get());
442     
443     if (!m_struct_vars->m_struct_laid_out)
444         return false;
445     
446     if (index >= m_struct_members.GetSize())
447         return false;
448     
449     ClangExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
450     
451     if (!member_sp)
452         return false;
453     
454     ClangExpressionVariable::ParserVars *parser_vars = member_sp->GetParserVars(GetParserID());
455     ClangExpressionVariable::JITVars *jit_vars = member_sp->GetJITVars(GetParserID());
456     
457     if (!parser_vars ||
458         !jit_vars ||
459         !member_sp->GetValueObject())
460         return false;
461     
462     decl = parser_vars->m_named_decl;
463     value = parser_vars->m_llvm_value;
464     offset = jit_vars->m_offset;
465     name = member_sp->GetName();
466         
467     return true;
468 }
469
470 bool
471 ClangExpressionDeclMap::GetFunctionInfo 
472 (
473     const NamedDecl *decl, 
474     uint64_t &ptr
475 )
476 {
477     ClangExpressionVariableSP entity_sp(m_found_entities.GetVariable(decl, GetParserID()));
478
479     if (!entity_sp)
480         return false;
481     
482     // We know m_parser_vars is valid since we searched for the variable by
483     // its NamedDecl
484     
485     ClangExpressionVariable::ParserVars *parser_vars = entity_sp->GetParserVars(GetParserID());
486
487     ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
488     
489     return true;
490 }
491
492 static void
493 FindCodeSymbolInContext
494 (
495     const ConstString &name,
496     SymbolContext &sym_ctx,
497     SymbolContextList &sc_list
498 )
499 {
500     SymbolContextList temp_sc_list;
501     if (sym_ctx.module_sp)
502         sym_ctx.module_sp->FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
503     
504     if (!sc_list.GetSize() && sym_ctx.target_sp)
505         sym_ctx.target_sp->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, temp_sc_list);
506
507     unsigned temp_sc_list_size = temp_sc_list.GetSize();
508     for (unsigned i = 0; i < temp_sc_list_size; i++)
509     {
510         SymbolContext sym_ctx;
511         temp_sc_list.GetContextAtIndex(i, sym_ctx);
512         if (sym_ctx.symbol)
513         {
514             switch (sym_ctx.symbol->GetType())
515             {
516                 case eSymbolTypeCode:
517                 case eSymbolTypeResolver:
518                 case eSymbolTypeReExported:
519                     sc_list.Append(sym_ctx);
520                     break;
521
522                 default:
523                     break;
524             }
525         }
526     }
527 }
528
529 bool
530 ClangExpressionDeclMap::GetFunctionAddress 
531 (
532     const ConstString &name,
533     uint64_t &func_addr
534 )
535 {
536     assert (m_parser_vars.get());
537     
538     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
539     ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
540     Target *target = exe_ctx.GetTargetPtr();
541     // Back out in all cases where we're not fully initialized
542     if (target == NULL)
543         return false;
544     if (!m_parser_vars->m_sym_ctx.target_sp)
545         return false;
546
547     SymbolContextList sc_list;
548     
549     FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, sc_list);
550
551     uint32_t sc_list_size = sc_list.GetSize();
552     if (sc_list_size == 0)
553     {
554         // We occasionally get debug information in which a const function is reported 
555         // as non-const, so the mangled name is wrong.  This is a hack to compensate.
556         
557         if (!strncmp(name.GetCString(), "_ZN", 3) &&
558             strncmp(name.GetCString(), "_ZNK", 4))
559         {
560             std::string fixed_scratch("_ZNK");
561             fixed_scratch.append(name.GetCString() + 3);
562             ConstString fixed_name(fixed_scratch.c_str());
563             
564             if (log)
565                 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString());
566             
567             FindCodeSymbolInContext(fixed_name, m_parser_vars->m_sym_ctx, sc_list);
568             sc_list_size = sc_list.GetSize();
569         }
570     }
571
572     for (uint32_t i=0; i<sc_list_size; ++i)
573     {
574         SymbolContext sym_ctx;
575         sc_list.GetContextAtIndex(i, sym_ctx);
576
577         const Address *func_so_addr = NULL;
578         bool is_indirect_function = false;
579         if (sym_ctx.function)
580             func_so_addr = &sym_ctx.function->GetAddressRange().GetBaseAddress();
581         else if (sym_ctx.symbol)
582         {
583             if (sym_ctx.symbol->GetType() == eSymbolTypeReExported)
584             {
585                 Symbol *reexported_symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
586                 if (reexported_symbol)
587                 {
588                     func_so_addr = &reexported_symbol->GetAddress();
589                     is_indirect_function = reexported_symbol->IsIndirect();
590                 }
591             }
592             else
593             {
594                 func_so_addr = &sym_ctx.symbol->GetAddress();
595                 is_indirect_function = sym_ctx.symbol->IsIndirect();
596             }
597         }
598
599         if (func_so_addr && func_so_addr->IsValid())
600         {
601             lldb::addr_t load_addr = func_so_addr->GetCallableLoadAddress (target, is_indirect_function);
602             
603             if (load_addr != LLDB_INVALID_ADDRESS)
604             {
605                 func_addr = load_addr;
606                 return true;
607             }
608         }
609     }
610     return false;
611 }
612
613 addr_t
614 ClangExpressionDeclMap::GetSymbolAddress (Target &target,
615                                           Process *process,
616                                           const ConstString &name,
617                                           lldb::SymbolType symbol_type,
618                                           lldb_private::Module *module)
619 {
620     SymbolContextList sc_list;
621     
622     if (module)
623         module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
624     else
625         target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
626     
627     const uint32_t num_matches = sc_list.GetSize();
628     addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
629
630     for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
631     {
632         SymbolContext sym_ctx;
633         sc_list.GetContextAtIndex(i, sym_ctx);
634     
635         const Address *sym_address = &sym_ctx.symbol->GetAddress();
636         
637         if (!sym_address || !sym_address->IsValid())
638             continue;
639         
640         if (sym_address)
641         {
642             switch (sym_ctx.symbol->GetType())
643             {
644                 case eSymbolTypeCode:
645                 case eSymbolTypeTrampoline:
646                     symbol_load_addr = sym_address->GetCallableLoadAddress (&target);
647                     break;
648
649                 case eSymbolTypeResolver:
650                     symbol_load_addr = sym_address->GetCallableLoadAddress (&target, true);
651                     break;
652
653                 case eSymbolTypeReExported:
654                     {
655                         ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
656                         if (reexport_name)
657                         {
658                             ModuleSP reexport_module_sp;
659                             ModuleSpec reexport_module_spec;
660                             reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
661                             if (reexport_module_spec.GetPlatformFileSpec())
662                             {
663                                 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
664                                 if (!reexport_module_sp)
665                                 {
666                                     reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
667                                     reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
668                                 }
669                             }
670                             symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get());
671                         }
672                     }
673                     break;
674
675                 case eSymbolTypeData:
676                 case eSymbolTypeRuntime:
677                 case eSymbolTypeVariable:
678                 case eSymbolTypeLocal:
679                 case eSymbolTypeParam:
680                 case eSymbolTypeInvalid:
681                 case eSymbolTypeAbsolute:
682                 case eSymbolTypeException:
683                 case eSymbolTypeSourceFile:
684                 case eSymbolTypeHeaderFile:
685                 case eSymbolTypeObjectFile:
686                 case eSymbolTypeCommonBlock:
687                 case eSymbolTypeBlock:
688                 case eSymbolTypeVariableType:
689                 case eSymbolTypeLineEntry:
690                 case eSymbolTypeLineHeader:
691                 case eSymbolTypeScopeBegin:
692                 case eSymbolTypeScopeEnd:
693                 case eSymbolTypeAdditional:
694                 case eSymbolTypeCompiler:
695                 case eSymbolTypeInstrumentation:
696                 case eSymbolTypeUndefined:
697                 case eSymbolTypeObjCClass:
698                 case eSymbolTypeObjCMetaClass:
699                 case eSymbolTypeObjCIVar:
700                     symbol_load_addr = sym_address->GetLoadAddress (&target);
701                     break;
702             }
703         }
704     }
705     
706     if (symbol_load_addr == LLDB_INVALID_ADDRESS && process)
707     {
708         ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
709         
710         if (runtime)
711         {
712             symbol_load_addr = runtime->LookupRuntimeSymbol(name);
713         }
714     }
715     
716     return symbol_load_addr;
717 }
718
719 addr_t
720 ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type)
721 {
722     assert (m_parser_vars.get());
723     
724     if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
725         return false;
726     
727     return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type);
728 }
729
730 const Symbol *
731 ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target,
732                                               const ConstString &name,
733                                               lldb_private::Module *module)
734 {
735     SymbolContextList sc_list;
736     
737     if (module)
738         module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
739     else
740         target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list);
741     
742     const uint32_t matches = sc_list.GetSize();
743     for (uint32_t i=0; i<matches; ++i)
744     {
745         SymbolContext sym_ctx;
746         sc_list.GetContextAtIndex(i, sym_ctx);
747         if (sym_ctx.symbol)
748         {
749             const Symbol *symbol = sym_ctx.symbol;
750             const Address *sym_address = &symbol->GetAddress();
751             
752             if (sym_address && sym_address->IsValid())
753             {
754                 switch (symbol->GetType())
755                 {
756                     case eSymbolTypeData:
757                     case eSymbolTypeRuntime:
758                     case eSymbolTypeAbsolute:
759                     case eSymbolTypeObjCClass:
760                     case eSymbolTypeObjCMetaClass:
761                     case eSymbolTypeObjCIVar:
762                         if (symbol->GetDemangledNameIsSynthesized())
763                         {
764                             // If the demangled name was synthesized, then don't use it
765                             // for expressions. Only let the symbol match if the mangled
766                             // named matches for these symbols.
767                             if (symbol->GetMangled().GetMangledName() != name)
768                                 break;
769                         }
770                         return symbol;
771
772                     case eSymbolTypeReExported:
773                         {
774                             ConstString reexport_name = symbol->GetReExportedSymbolName();
775                             if (reexport_name)
776                             {
777                                 ModuleSP reexport_module_sp;
778                                 ModuleSpec reexport_module_spec;
779                                 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary();
780                                 if (reexport_module_spec.GetPlatformFileSpec())
781                                 {
782                                     reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
783                                     if (!reexport_module_sp)
784                                     {
785                                         reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
786                                         reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec);
787                                     }
788                                 }
789                                 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get());
790                             }
791                         }
792                         break;
793                         
794                     case eSymbolTypeCode: // We already lookup functions elsewhere
795                     case eSymbolTypeVariable:
796                     case eSymbolTypeLocal:
797                     case eSymbolTypeParam:
798                     case eSymbolTypeTrampoline:
799                     case eSymbolTypeInvalid:
800                     case eSymbolTypeException:
801                     case eSymbolTypeSourceFile:
802                     case eSymbolTypeHeaderFile:
803                     case eSymbolTypeObjectFile:
804                     case eSymbolTypeCommonBlock:
805                     case eSymbolTypeBlock:
806                     case eSymbolTypeVariableType:
807                     case eSymbolTypeLineEntry:
808                     case eSymbolTypeLineHeader:
809                     case eSymbolTypeScopeBegin:
810                     case eSymbolTypeScopeEnd:
811                     case eSymbolTypeAdditional:
812                     case eSymbolTypeCompiler:
813                     case eSymbolTypeInstrumentation:
814                     case eSymbolTypeUndefined:
815                     case eSymbolTypeResolver:
816                         break;
817                 }
818             }
819         }
820     }
821     
822     return NULL;
823 }
824
825 lldb::VariableSP
826 ClangExpressionDeclMap::FindGlobalVariable
827 (
828     Target &target,
829     ModuleSP &module,
830     const ConstString &name,
831     ClangNamespaceDecl *namespace_decl,
832     TypeFromUser *type
833 )
834 {
835     VariableList vars;
836     
837     if (module && namespace_decl)
838         module->FindGlobalVariables (name, namespace_decl, true, -1, vars);
839     else
840         target.GetImages().FindGlobalVariables(name, true, -1, vars);
841     
842     if (vars.GetSize())
843     {
844         if (type)
845         {
846             for (size_t i = 0; i < vars.GetSize(); ++i)
847             {
848                 VariableSP var_sp = vars.GetVariableAtIndex(i);
849                 
850                 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetClangFullType()))
851                     return var_sp;
852             }
853         }
854         else
855         {
856             return vars.GetVariableAtIndex(0);
857         }
858     }
859     
860     return VariableSP();
861 }
862
863 // Interface for ClangASTSource
864
865 void
866 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context)
867 {
868     assert (m_ast_context);
869     
870     ClangASTMetrics::RegisterVisibleQuery();
871     
872     const ConstString name(context.m_decl_name.getAsString().c_str());
873     
874     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
875     
876     if (GetImportInProgress())
877     {
878         if (log && log->GetVerbose())
879             log->Printf("Ignoring a query during an import");
880         return;
881     }
882     
883     static unsigned int invocation_id = 0;
884     unsigned int current_id = invocation_id++;
885     
886     if (log)
887     {
888         if (!context.m_decl_context)
889             log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
890         else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
891             log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
892         else
893             log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
894     }
895             
896     if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
897     {
898         ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
899         
900         if (log && log->GetVerbose())
901             log->Printf("  CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)", 
902                         current_id, 
903                         namespace_map.get(), 
904                         (int)namespace_map->size());
905         
906         if (!namespace_map)
907             return;
908         
909         for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
910              i != e;
911              ++i)
912         {
913             if (log)
914                 log->Printf("  CEDM::FEVD[%u] Searching namespace %s in module %s",
915                             current_id,
916                             i->second.GetNamespaceDecl()->getNameAsString().c_str(),
917                             i->first->GetFileSpec().GetFilename().GetCString());
918                 
919             FindExternalVisibleDecls(context,
920                                      i->first,
921                                      i->second,
922                                      current_id);
923         }
924     }
925     else if (isa<TranslationUnitDecl>(context.m_decl_context))
926     {
927         ClangNamespaceDecl namespace_decl;
928         
929         if (log)
930             log->Printf("  CEDM::FEVD[%u] Searching the root namespace", current_id);
931         
932         FindExternalVisibleDecls(context,
933                                  lldb::ModuleSP(),
934                                  namespace_decl,
935                                  current_id);
936     }
937     
938     if (!context.m_found.variable)
939         ClangASTSource::FindExternalVisibleDecls(context);
940 }
941
942 void 
943 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context, 
944                                                   lldb::ModuleSP module_sp,
945                                                   ClangNamespaceDecl &namespace_decl,
946                                                   unsigned int current_id)
947 {
948     assert (m_ast_context);
949     
950     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
951     
952     SymbolContextList sc_list;
953     
954     const ConstString name(context.m_decl_name.getAsString().c_str());
955     
956     const char *name_unique_cstr = name.GetCString();
957     
958     if (name_unique_cstr == NULL)
959         return;
960     
961     static ConstString id_name("id");
962     static ConstString Class_name("Class");
963     
964     if (name == id_name || name == Class_name)
965         return;
966     
967     // Only look for functions by name out in our symbols if the function 
968     // doesn't start with our phony prefix of '$'
969     Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
970     StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
971     if (name_unique_cstr[0] == '$' && !namespace_decl)
972     {
973         static ConstString g_lldb_class_name ("$__lldb_class");
974         
975         if (name == g_lldb_class_name)
976         {
977             // Clang is looking for the type of "this"
978                         
979             if (frame == NULL)
980                 return;
981             
982             SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
983             
984             if (!sym_ctx.function)
985                 return;
986             
987             // Get the block that defines the function
988             Block *function_block = sym_ctx.GetFunctionBlock();
989
990             if (!function_block)
991                 return;
992
993             clang::DeclContext *decl_context = function_block->GetClangDeclContext();
994             
995             if (!decl_context)
996                 return;
997             
998             clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context);
999             
1000             if (method_decl)
1001             {
1002                 clang::CXXRecordDecl *class_decl = method_decl->getParent();
1003                 
1004                 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
1005                 
1006                 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(),
1007                                               &class_decl->getASTContext());
1008                 
1009                 if (log)
1010                 {
1011                     ASTDumper ast_dumper(class_qual_type);
1012                     log->Printf("  CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString());
1013                 }
1014                 
1015                 TypeFromParser class_type = CopyClassType(class_user_type, current_id);
1016                 
1017                 if (!class_type.IsValid())
1018                     return;
1019                 
1020                 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(class_type.GetOpaqueQualType()));
1021                 
1022                 if (!type_source_info)
1023                     return;
1024                 
1025                 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context,
1026                                                                 m_ast_context->getTranslationUnitDecl(),
1027                                                                 SourceLocation(),
1028                                                                 SourceLocation(),
1029                                                                 context.m_decl_name.getAsIdentifierInfo(),
1030                                                                 type_source_info);
1031                 
1032                 
1033                 if (!typedef_decl)
1034                     return;
1035                 
1036                 context.AddNamedDecl(typedef_decl);
1037                 
1038                 if (method_decl->isInstance())
1039                 {
1040                     // self is a pointer to the object
1041                     
1042                     QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type);
1043                     
1044                     TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1045                                                 &method_decl->getASTContext());
1046                     
1047                     m_struct_vars->m_object_pointer_type = self_user_type;
1048                 }
1049             }
1050             else
1051             {
1052                 // This branch will get hit if we are executing code in the context of a function that
1053                 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1054                 // method of the class.  In that case, just look up the "this" variable in the the current
1055                 // scope and use its type.
1056                 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer
1057                 // for C++ so it hasn't actually been tested.
1058                 
1059                 VariableList *vars = frame->GetVariableList(false);
1060                 
1061                 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
1062                 
1063                 if (this_var &&
1064                     this_var->IsInScope(frame) &&
1065                     this_var->LocationIsValidForFrame (frame))
1066                 {
1067                     Type *this_type = this_var->GetType();
1068                     
1069                     if (!this_type)
1070                         return;
1071                     
1072                     ClangASTType pointee_type = this_type->GetClangForwardType().GetPointeeType();
1073                     
1074                     if (pointee_type.IsValid())
1075                     {
1076                         if (log)
1077                         {
1078                             ASTDumper ast_dumper(this_type->GetClangFullType());
1079                             log->Printf("  FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1080                         }
1081                         
1082                         TypeFromUser class_user_type(pointee_type);
1083                         AddOneType(context, class_user_type, current_id);
1084                                     
1085                                     
1086                         TypeFromUser this_user_type(this_type->GetClangFullType());
1087                         m_struct_vars->m_object_pointer_type = this_user_type;
1088                         return;
1089                     }
1090                 }
1091             }
1092             
1093             return;
1094         }
1095         
1096         static ConstString g_lldb_objc_class_name ("$__lldb_objc_class");
1097         if (name == g_lldb_objc_class_name)
1098         {
1099             // Clang is looking for the type of "*self"
1100             
1101             if (!frame)
1102                 return;
1103          
1104             SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
1105             
1106             if (!sym_ctx.function)
1107                 return;
1108             
1109             // Get the block that defines the function
1110             Block *function_block = sym_ctx.GetFunctionBlock();
1111             
1112             if (!function_block)
1113                 return;
1114             
1115             clang::DeclContext *decl_context = function_block->GetClangDeclContext();
1116             
1117             if (!decl_context)
1118                 return;
1119             
1120             clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context);
1121             
1122             if (method_decl)
1123             {
1124                 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface();
1125                 
1126                 if (!self_interface)
1127                     return;
1128                 
1129                 const clang::Type *interface_type = self_interface->getTypeForDecl();
1130                 
1131                 if (!interface_type)
1132                     return; // This is unlikely, but we have seen crashes where this occurred
1133                         
1134                 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
1135                                              &method_decl->getASTContext());
1136                 
1137                 if (log)
1138                 {
1139                     ASTDumper ast_dumper(interface_type);
1140                     log->Printf("  FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1141                 }
1142                     
1143                 AddOneType(context, class_user_type, current_id);
1144                                 
1145                 if (method_decl->isInstanceMethod())
1146                 {
1147                     // self is a pointer to the object
1148                     
1149                     QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0));
1150                 
1151                     TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
1152                                                 &method_decl->getASTContext());
1153                 
1154                     m_struct_vars->m_object_pointer_type = self_user_type;
1155                 }
1156                 else
1157                 {
1158                     // self is a Class pointer
1159                     QualType class_type = method_decl->getASTContext().getObjCClassType();
1160                     
1161                     TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
1162                                                 &method_decl->getASTContext());
1163                     
1164                     m_struct_vars->m_object_pointer_type = self_user_type;
1165                 }
1166
1167                 return;
1168             }
1169             else
1170             {
1171                 // This branch will get hit if we are executing code in the context of a function that
1172                 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a
1173                 // method of the class.  In that case, just look up the "self" variable in the the current
1174                 // scope and use its type.
1175                 
1176                 VariableList *vars = frame->GetVariableList(false);
1177                 
1178                 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
1179                 
1180                 if (self_var &&
1181                     self_var->IsInScope(frame) && 
1182                     self_var->LocationIsValidForFrame (frame))
1183                 {
1184                     Type *self_type = self_var->GetType();
1185                     
1186                     if (!self_type)
1187                         return;
1188                     
1189                     ClangASTType self_clang_type = self_type->GetClangFullType();
1190                     
1191                     if (self_clang_type.IsObjCClassType())
1192                     {
1193                         return;
1194                     }
1195                     else if (self_clang_type.IsObjCObjectPointerType())
1196                     {
1197                         self_clang_type = self_clang_type.GetPointeeType();
1198
1199                         if (!self_clang_type)
1200                             return;
1201                         
1202                         if (log)
1203                         {
1204                             ASTDumper ast_dumper(self_type->GetClangFullType());
1205                             log->Printf("  FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString());
1206                         }
1207                         
1208                         TypeFromUser class_user_type (self_clang_type);
1209                         
1210                         AddOneType(context, class_user_type, current_id);
1211                                     
1212                         TypeFromUser self_user_type(self_type->GetClangFullType());
1213                         
1214                         m_struct_vars->m_object_pointer_type = self_user_type;
1215                         return;
1216                     }
1217                 }
1218             }
1219
1220             return;
1221         }
1222         
1223         // any other $__lldb names should be weeded out now
1224         if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
1225             return;
1226         
1227         do
1228         {
1229             if (!target)
1230                 break;
1231             
1232             ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
1233             
1234             if (!scratch_clang_ast_context)
1235                 break;
1236             
1237             ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
1238             
1239             if (!scratch_ast_context)
1240                 break;
1241             
1242             TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
1243             
1244             if (!ptype_type_decl)
1245                 break;
1246             
1247             Decl *parser_ptype_decl = m_ast_importer->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
1248             
1249             if (!parser_ptype_decl)
1250                 break;
1251             
1252             TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
1253             
1254             if (!parser_ptype_type_decl)
1255                 break;
1256             
1257             if (log)
1258                 log->Printf("  CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
1259             
1260             context.AddNamedDecl(parser_ptype_type_decl);
1261         } while (0);
1262         
1263         ClangExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
1264         
1265         if (pvar_sp)
1266         {
1267             AddOneVariable(context, pvar_sp, current_id);
1268             return;
1269         }
1270         
1271         const char *reg_name(&name.GetCString()[1]);
1272         
1273         if (m_parser_vars->m_exe_ctx.GetRegisterContext())
1274         {
1275             const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name));
1276             
1277             if (reg_info)
1278             {
1279                 if (log)
1280                     log->Printf("  CEDM::FEVD[%u] Found register %s", current_id, reg_info->name);
1281                 
1282                 AddOneRegister(context, reg_info, current_id);
1283             }
1284         }
1285     }
1286     else
1287     {
1288         ValueObjectSP valobj;
1289         VariableSP var;
1290         Error err;
1291         
1292         if (frame && !namespace_decl)
1293         {
1294             valobj = frame->GetValueForVariableExpressionPath(name_unique_cstr, 
1295                                                               eNoDynamicValues, 
1296                                                               StackFrame::eExpressionPathOptionCheckPtrVsMember ||
1297                                                               StackFrame::eExpressionPathOptionsAllowDirectIVarAccess ||
1298                                                               StackFrame::eExpressionPathOptionsNoFragileObjcIvar ||
1299                                                               StackFrame::eExpressionPathOptionsNoSyntheticChildren ||
1300                                                               StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
1301                                                               var,
1302                                                               err);
1303             
1304             // If we found a variable in scope, no need to pull up function names
1305             if (err.Success() && var)
1306             {
1307                 AddOneVariable(context, var, valobj, current_id);
1308                 context.m_found.variable = true;
1309                 return;
1310             }
1311         }
1312         
1313         if (target)
1314         {
1315             var = FindGlobalVariable (*target,
1316                                       module_sp,
1317                                       name,
1318                                       &namespace_decl,
1319                                       NULL);
1320             
1321             if (var)
1322             {
1323                 valobj = ValueObjectVariable::Create(target, var);
1324                 AddOneVariable(context, var, valobj, current_id);
1325                 context.m_found.variable = true;
1326                 return;
1327             }
1328         }
1329         
1330         if (!context.m_found.variable)
1331         {
1332             const bool include_inlines = false;
1333             const bool append = false;
1334             
1335             if (namespace_decl && module_sp)
1336             {
1337                 const bool include_symbols = false;
1338
1339                 module_sp->FindFunctions(name,
1340                                          &namespace_decl,
1341                                          eFunctionNameTypeBase, 
1342                                          include_symbols,
1343                                          include_inlines,
1344                                          append,
1345                                          sc_list);
1346             }
1347             else if (target && !namespace_decl)
1348             {
1349                 const bool include_symbols = true;
1350                 
1351                 // TODO Fix FindFunctions so that it doesn't return
1352                 //   instance methods for eFunctionNameTypeBase.
1353                 
1354                 target->GetImages().FindFunctions(name,
1355                                                   eFunctionNameTypeFull,
1356                                                   include_symbols,
1357                                                   include_inlines,
1358                                                   append, 
1359                                                   sc_list);
1360             }
1361             
1362             if (sc_list.GetSize())
1363             {
1364                 Symbol *extern_symbol = NULL;
1365                 Symbol *non_extern_symbol = NULL;
1366                 
1367                 for (uint32_t index = 0, num_indices = sc_list.GetSize();
1368                      index < num_indices;
1369                      ++index)
1370                 {
1371                     SymbolContext sym_ctx;
1372                     sc_list.GetContextAtIndex(index, sym_ctx);
1373                     
1374                     if (sym_ctx.function)
1375                     {
1376                         clang::DeclContext *decl_ctx = sym_ctx.function->GetClangDeclContext();
1377                         
1378                         if (!decl_ctx)
1379                             continue;
1380                         
1381                         // Filter out class/instance methods.
1382                         if (dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
1383                             continue;
1384                         if (dyn_cast<clang::CXXMethodDecl>(decl_ctx))
1385                             continue;
1386                         
1387                         AddOneFunction(context, sym_ctx.function, NULL, current_id);
1388                         context.m_found.function_with_type_info = true;
1389                         context.m_found.function = true;
1390                     }
1391                     else if (sym_ctx.symbol)
1392                     {
1393                         if (sym_ctx.symbol->GetType() == eSymbolTypeReExported)
1394                         {
1395                             sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1396                             if (sym_ctx.symbol == NULL)
1397                                 continue;
1398                         }
1399                         
1400                         if (sym_ctx.symbol->IsExternal())
1401                             extern_symbol = sym_ctx.symbol;
1402                         else
1403                             non_extern_symbol = sym_ctx.symbol;
1404                     }
1405                 }
1406                 
1407                 if (!context.m_found.function_with_type_info)
1408                 {
1409                     if (extern_symbol)
1410                     {
1411                         AddOneFunction (context, NULL, extern_symbol, current_id);
1412                         context.m_found.function = true;
1413                     }
1414                     else if (non_extern_symbol)
1415                     {
1416                         AddOneFunction (context, NULL, non_extern_symbol, current_id);
1417                         context.m_found.function = true;
1418                     }
1419                 }
1420             }
1421             
1422             if (target && !context.m_found.variable && !namespace_decl)
1423             {
1424                 // We couldn't find a non-symbol variable for this.  Now we'll hunt for a generic 
1425                 // data symbol, and -- if it is found -- treat it as a variable.
1426                 
1427                 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name);
1428                 
1429                 if (data_symbol)
1430                 {
1431                     AddOneGenericVariable(context, *data_symbol, current_id);
1432                     context.m_found.variable = true;
1433                 }
1434             }
1435         }
1436     }
1437 }
1438
1439 static clang_type_t
1440 MaybePromoteToBlockPointerType
1441 (
1442     ASTContext *ast_context,
1443     clang_type_t candidate_type
1444 )
1445 {
1446     if (!candidate_type)
1447         return candidate_type;
1448     
1449     QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type);
1450     
1451     const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type);
1452     
1453     if (!candidate_pointer_type)
1454         return candidate_type;
1455     
1456     QualType pointee_qual_type = candidate_pointer_type->getPointeeType();
1457     
1458     const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type);
1459     
1460     if (!pointee_record_type)
1461         return candidate_type;
1462     
1463     RecordDecl *pointee_record_decl = pointee_record_type->getDecl();
1464     
1465     if (!pointee_record_decl->isRecord())
1466         return candidate_type;
1467     
1468     if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_")))
1469         return candidate_type;
1470     
1471     QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy);
1472     QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type);
1473     
1474     return block_pointer_type.getAsOpaquePtr();
1475 }
1476
1477 bool
1478 ClangExpressionDeclMap::GetVariableValue (VariableSP &var,
1479                                           lldb_private::Value &var_location,
1480                                           TypeFromUser *user_type,
1481                                           TypeFromParser *parser_type)
1482 {
1483     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1484     
1485     Type *var_type = var->GetType();
1486     
1487     if (!var_type) 
1488     {
1489         if (log)
1490             log->PutCString("Skipped a definition because it has no type");
1491         return false;
1492     }
1493     
1494     ClangASTType var_clang_type = var_type->GetClangFullType();
1495     
1496     if (!var_clang_type)
1497     {
1498         if (log)
1499             log->PutCString("Skipped a definition because it has no Clang type");
1500         return false;
1501     }
1502     
1503     ASTContext *ast = var_type->GetClangASTContext().getASTContext();
1504
1505     if (!ast)
1506     {
1507         if (log)
1508             log->PutCString("There is no AST context for the current execution context");
1509         return false;
1510     }
1511     //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type);
1512     
1513     DWARFExpression &var_location_expr = var->LocationExpression();
1514     
1515     lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
1516     
1517     Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1518
1519     if (var_location_expr.IsLocationList())
1520     {
1521         SymbolContext var_sc;
1522         var->CalculateSymbolContext (&var_sc);
1523         loclist_base_load_addr = var_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
1524     }
1525     Error err;
1526     
1527     if (var->GetLocationIsConstantValueData())
1528     {
1529         DataExtractor const_value_extractor;
1530         
1531         if (var_location_expr.GetExpressionData(const_value_extractor))
1532         {
1533             var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize());
1534             var_location.SetValueType(Value::eValueTypeHostAddress);
1535         }
1536         else
1537         {
1538             if (log)
1539                 log->Printf("Error evaluating constant variable: %s", err.AsCString());
1540             return false;
1541         }
1542     }
1543     
1544     ClangASTType type_to_use = GuardedCopyType(var_clang_type);
1545     
1546     if (!type_to_use)
1547     {
1548         if (log)
1549             log->Printf("Couldn't copy a variable's type into the parser's AST context");
1550         
1551         return false;
1552     }
1553     
1554     if (parser_type)
1555         *parser_type = TypeFromParser(type_to_use);
1556     
1557     if (var_location.GetContextType() == Value::eContextTypeInvalid)
1558         var_location.SetClangType(type_to_use);
1559     
1560     if (var_location.GetValueType() == Value::eValueTypeFileAddress)
1561     {
1562         SymbolContext var_sc;
1563         var->CalculateSymbolContext(&var_sc);
1564         
1565         if (!var_sc.module_sp)
1566             return false;
1567
1568         Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList());
1569         
1570         lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1571         
1572         if (load_addr != LLDB_INVALID_ADDRESS)
1573         {
1574             var_location.GetScalar() = load_addr;
1575             var_location.SetValueType(Value::eValueTypeLoadAddress);
1576         }
1577     }
1578     
1579     if (user_type)
1580         *user_type = TypeFromUser(var_clang_type);
1581     
1582     return true;
1583 }
1584
1585 void
1586 ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id)
1587 {
1588     assert (m_parser_vars.get());
1589     
1590     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1591         
1592     TypeFromUser ut;
1593     TypeFromParser pt;
1594     Value var_location;
1595     
1596     if (!GetVariableValue (var, var_location, &ut, &pt))
1597         return;
1598     
1599     clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1600     
1601     if (parser_opaque_type.isNull())
1602         return;
1603     
1604     if (const clang::Type *parser_type = parser_opaque_type.getTypePtr())
1605     {
1606         if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1607             CompleteType(tag_type->getDecl());
1608     }
1609     
1610     
1611     bool is_reference = pt.IsReferenceType();
1612
1613     NamedDecl *var_decl = NULL;
1614     if (is_reference)
1615         var_decl = context.AddVarDecl(pt);
1616     else
1617         var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1618         
1619     std::string decl_name(context.m_decl_name.getAsString());
1620     ConstString entity_name(decl_name.c_str());
1621     ClangExpressionVariableSP entity(m_found_entities.CreateVariable (valobj));
1622     
1623     assert (entity.get());
1624     entity->EnableParserVars(GetParserID());
1625     ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1626     parser_vars->m_parser_type = pt;
1627     parser_vars->m_named_decl  = var_decl;
1628     parser_vars->m_llvm_value  = NULL;
1629     parser_vars->m_lldb_value  = var_location;
1630     parser_vars->m_lldb_var    = var;
1631     
1632     if (is_reference)
1633         entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1634     
1635     if (log)
1636     {
1637         ASTDumper orig_dumper(ut.GetOpaqueQualType());
1638         ASTDumper ast_dumper(var_decl);        
1639         log->Printf("  CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString());
1640     }
1641 }
1642
1643 void
1644 ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1645                                        ClangExpressionVariableSP &pvar_sp, 
1646                                        unsigned int current_id)
1647 {
1648     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1649     
1650     TypeFromUser user_type (pvar_sp->GetTypeFromUser());
1651     
1652     TypeFromParser parser_type (GuardedCopyType(user_type));
1653     
1654     if (!parser_type.GetOpaqueQualType())
1655     {
1656         if (log)
1657             log->Printf("  CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString());
1658         return;
1659     }
1660     
1661     NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType());
1662     
1663     pvar_sp->EnableParserVars(GetParserID());
1664     ClangExpressionVariable::ParserVars *parser_vars = pvar_sp->GetParserVars(GetParserID());
1665     parser_vars->m_parser_type = parser_type;
1666     parser_vars->m_named_decl = var_decl;
1667     parser_vars->m_llvm_value = NULL;
1668     parser_vars->m_lldb_value.Clear();
1669     
1670     if (log)
1671     {
1672         ASTDumper ast_dumper(var_decl);
1673         log->Printf("  CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString());
1674     }
1675 }
1676
1677 void
1678 ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context, 
1679                                               const Symbol &symbol,
1680                                               unsigned int current_id)
1681 {
1682     assert(m_parser_vars.get());
1683     
1684     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1685     
1686     Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1687
1688     if (target == NULL)
1689         return;
1690
1691     ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1692     
1693     TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1694     TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType());
1695     NamedDecl *var_decl = context.AddVarDecl(parser_type);
1696     
1697     std::string decl_name(context.m_decl_name.getAsString());
1698     ConstString entity_name(decl_name.c_str());
1699     ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1700                                                                       entity_name, 
1701                                                                       user_type,
1702                                                                       m_parser_vars->m_target_info.byte_order,
1703                                                                       m_parser_vars->m_target_info.address_byte_size));
1704     assert (entity.get());
1705     
1706     entity->EnableParserVars(GetParserID());
1707     ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1708
1709     const Address &symbol_address = symbol.GetAddress();
1710     lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1711     
1712     //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1713     parser_vars->m_lldb_value.SetClangType(user_type);
1714     parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1715     parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1716     
1717     parser_vars->m_parser_type = parser_type;
1718     parser_vars->m_named_decl  = var_decl;
1719     parser_vars->m_llvm_value  = NULL;
1720     parser_vars->m_lldb_sym    = &symbol;
1721     
1722     if (log)
1723     {
1724         ASTDumper ast_dumper(var_decl);
1725         
1726         log->Printf("  CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString());
1727     }
1728 }
1729
1730 bool 
1731 ClangExpressionDeclMap::ResolveUnknownTypes()
1732 {
1733     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1734     Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1735
1736     ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext();
1737
1738     for (size_t index = 0, num_entities = m_found_entities.GetSize();
1739          index < num_entities;
1740          ++index)
1741     {
1742         ClangExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index);
1743         
1744         ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1745         
1746         if (entity->m_flags & ClangExpressionVariable::EVUnknownType)
1747         {
1748             const NamedDecl *named_decl = parser_vars->m_named_decl;
1749             const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl);
1750             
1751             if (!var_decl)
1752             {
1753                 if (log)
1754                     log->Printf("Entity of unknown type does not have a VarDecl");
1755                 return false;
1756             }
1757             
1758             if (log)
1759             {
1760                 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl));
1761                 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString());
1762             }
1763                 
1764             QualType var_type = var_decl->getType();
1765             TypeFromParser parser_type(var_type.getAsOpaquePtr(), &var_decl->getASTContext());
1766             
1767             lldb::clang_type_t copied_type = m_ast_importer->CopyType(scratch_ast_context, &var_decl->getASTContext(), var_type.getAsOpaquePtr());
1768             
1769             if (!copied_type)
1770             {                
1771                 if (log)
1772                     log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable");
1773                 
1774                 return (bool) lldb::ClangExpressionVariableSP();
1775             }
1776             
1777             TypeFromUser user_type(copied_type, scratch_ast_context);
1778                         
1779 //            parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType());
1780             parser_vars->m_lldb_value.SetClangType(user_type);
1781             parser_vars->m_parser_type = parser_type;
1782             
1783             entity->SetClangType(user_type);
1784             
1785             entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType);
1786         }
1787     }
1788             
1789     return true;
1790 }
1791
1792 void
1793 ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context,
1794                                         const RegisterInfo *reg_info, 
1795                                         unsigned int current_id)
1796 {
1797     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1798     
1799     ClangASTType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context,
1800                                                                                     reg_info->encoding,
1801                                                                                     reg_info->byte_size * 8);
1802     
1803     if (!clang_type)
1804     {
1805         if (log)
1806             log->Printf("  Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str());
1807         return;
1808     }
1809     
1810     TypeFromParser parser_clang_type (clang_type);
1811     
1812     NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1813     
1814     ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1815                                                                       m_parser_vars->m_target_info.byte_order,
1816                                                                       m_parser_vars->m_target_info.address_byte_size));
1817     assert (entity.get());
1818     
1819     std::string decl_name(context.m_decl_name.getAsString());
1820     entity->SetName (ConstString (decl_name.c_str()));
1821     entity->SetRegisterInfo (reg_info);
1822     entity->EnableParserVars(GetParserID());
1823     ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1824     parser_vars->m_parser_type = parser_clang_type;
1825     parser_vars->m_named_decl = var_decl;
1826     parser_vars->m_llvm_value = NULL;
1827     parser_vars->m_lldb_value.Clear();
1828     entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1829     
1830     if (log)
1831     {
1832         ASTDumper ast_dumper(var_decl);
1833         log->Printf("  CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString());
1834     }
1835 }
1836
1837 void
1838 ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context,
1839                                         Function* function,
1840                                         Symbol* symbol,
1841                                         unsigned int current_id)
1842 {
1843     assert (m_parser_vars.get());
1844     
1845     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1846     
1847     NamedDecl *function_decl = NULL;
1848     const Address *fun_address = NULL;
1849     ClangASTType function_clang_type;
1850
1851     bool is_indirect_function = false;
1852
1853     if (function)
1854     {
1855         Type *function_type = function->GetType();
1856         
1857         if (!function_type)
1858         {
1859             if (log)
1860                 log->PutCString("  Skipped a function because it has no type");
1861             return;
1862         }
1863         
1864         function_clang_type = function_type->GetClangFullType();
1865         
1866         if (!function_clang_type)
1867         {
1868             if (log)
1869                 log->PutCString("  Skipped a function because it has no Clang type");
1870             return;
1871         }
1872         
1873         fun_address = &function->GetAddressRange().GetBaseAddress();
1874         
1875         ClangASTType copied_function_type = GuardedCopyType(function_clang_type);
1876         if (copied_function_type)
1877         {
1878             function_decl = context.AddFunDecl(copied_function_type);
1879             
1880             if (!function_decl)
1881             {
1882                 if (log)
1883                 {
1884                     log->Printf ("  Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}",
1885                                  function_type->GetName().GetCString(),
1886                                  function_type->GetID());
1887                 }
1888                 
1889                 return;
1890             }
1891         }
1892         else
1893         {
1894             // We failed to copy the type we found
1895             if (log)
1896             {
1897                 log->Printf ("  Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt",
1898                              function_type->GetName().GetCString(),
1899                              function_type->GetID());
1900             }
1901             
1902             return;
1903         }
1904     }
1905     else if (symbol)
1906     {
1907         fun_address = &symbol->GetAddress();
1908         function_decl = context.AddGenericFunDecl();
1909         is_indirect_function = symbol->IsIndirect();
1910     }
1911     else
1912     {
1913         if (log)
1914             log->PutCString("  AddOneFunction called with no function and no symbol");
1915         return;
1916     }
1917     
1918     Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1919
1920     lldb::addr_t load_addr = fun_address->GetCallableLoadAddress(target, is_indirect_function);
1921     
1922     ClangExpressionVariableSP entity(m_found_entities.CreateVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (),
1923                                                                       m_parser_vars->m_target_info.byte_order,
1924                                                                       m_parser_vars->m_target_info.address_byte_size));
1925     assert (entity.get());
1926
1927     std::string decl_name(context.m_decl_name.getAsString());
1928     entity->SetName(ConstString(decl_name.c_str()));
1929     entity->SetClangType (function_clang_type);
1930     entity->EnableParserVars(GetParserID());
1931
1932     ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID());
1933     
1934     if (load_addr != LLDB_INVALID_ADDRESS)
1935     {
1936         parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress);
1937         parser_vars->m_lldb_value.GetScalar() = load_addr;
1938     }
1939     else
1940     {
1941         // We have to try finding a file address.
1942         
1943         lldb::addr_t file_addr = fun_address->GetFileAddress();
1944         
1945         parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress);
1946         parser_vars->m_lldb_value.GetScalar() = file_addr;
1947     }
1948     
1949
1950     parser_vars->m_named_decl  = function_decl;
1951     parser_vars->m_llvm_value  = NULL;
1952     
1953     if (log)
1954     {
1955         ASTDumper ast_dumper(function_decl);
1956         
1957         StreamString ss;
1958         
1959         fun_address->Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription);
1960         
1961         log->Printf("  CEDM::FEVD[%u] Found %s function %s (description %s), returned %s",
1962                     current_id,
1963                     (function ? "specific" : "generic"),
1964                     decl_name.c_str(),
1965                     ss.GetData(),
1966                     ast_dumper.GetCString());
1967     }
1968 }
1969
1970 TypeFromParser
1971 ClangExpressionDeclMap::CopyClassType(TypeFromUser &ut,
1972                                       unsigned int current_id)
1973 {
1974     ClangASTType copied_clang_type = GuardedCopyType(ut);
1975     
1976     if (!copied_clang_type)
1977     {
1978         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1979         
1980         if (log)
1981             log->Printf("ClangExpressionDeclMap::CopyClassType - Couldn't import the type");
1982         
1983         return TypeFromParser();
1984     }
1985
1986     if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ())
1987     {
1988         ClangASTType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid);
1989         ClangASTType void_ptr_clang_type = void_clang_type.GetPointerType();
1990         
1991         ClangASTType method_type = ClangASTContext::CreateFunctionType (m_ast_context,
1992                                                                         void_clang_type,
1993                                                                         &void_ptr_clang_type,
1994                                                                         1,
1995                                                                         false,
1996                                                                         copied_clang_type.GetTypeQualifiers());
1997         
1998         const bool is_virtual = false;
1999         const bool is_static = false;
2000         const bool is_inline = false;
2001         const bool is_explicit = false;
2002         const bool is_attr_used = true;
2003         const bool is_artificial = false;
2004         
2005         copied_clang_type.AddMethodToCXXRecordType ("$__lldb_expr",
2006                                                     method_type,
2007                                                     lldb::eAccessPublic,
2008                                                     is_virtual,
2009                                                     is_static,
2010                                                     is_inline,
2011                                                     is_explicit,
2012                                                     is_attr_used,
2013                                                     is_artificial);
2014     }
2015     
2016     return TypeFromParser(copied_clang_type);
2017 }
2018
2019 void 
2020 ClangExpressionDeclMap::AddOneType(NameSearchContext &context, 
2021                                    TypeFromUser &ut,
2022                                    unsigned int current_id)
2023 {
2024     ClangASTType copied_clang_type = GuardedCopyType(ut);
2025     
2026     if (!copied_clang_type)
2027     {
2028         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2029
2030         if (log)
2031             log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2032         
2033         return;
2034     }
2035     
2036     context.AddTypeDecl(copied_clang_type);
2037 }