ClangASTContext.cpp revision abe0fed36d83e1c37af9dae90c2d25db742b4515
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                     The LLVM Compiler Infrastructure
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is distributed under the University of Illinois Open Source
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// License. See LICENSE.TXT for details.
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "lldb/Symbol/ClangASTContext.h"
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// C Includes
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// C++ Includes
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Other libraries and framework includes
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Clang headers like to use NDEBUG inside of them to enable/disable debug
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// or another. This is bad because it means that if clang was built in release
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// mode, it assumes that you are building in release mode which is not always
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// the case. You can end up with functions that are defined as empty in header
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// files when NDEBUG is not defined, and this can cause link errors with the
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// clang .a files that you have since you might be missing functions in the .a
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// file. So we have to define NDEBUG when including clang headers to avoid any
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// mismatches. This is covered by rdar://problem/8691220
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef NDEBUG
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define LLDB_DEFINED_NDEBUG_FOR_CLANG
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define NDEBUG
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Need to include assert.h so it is as clang would expect it to be (disabled)
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <assert.h>
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/AST/ASTContext.h"
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/AST/ASTImporter.h"
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/AST/CXXInheritance.h"
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/AST/DeclObjC.h"
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/AST/RecordLayout.h"
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/AST/Type.h"
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/Builtins.h"
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/FileManager.h"
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/FileSystemOptions.h"
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/SourceManager.h"
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/TargetInfo.h"
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Basic/TargetOptions.h"
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Frontend/FrontendOptions.h"
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "clang/Frontend/LangStandard.h"
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#undef NDEBUG
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <assert.h>
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "lldb/Core/ArchSpec.h"
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "lldb/Core/dwarf.h"
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "lldb/Core/Flags.h"
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "lldb/Core/Log.h"
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdio.h>
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace lldb;
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace lldb_private;
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace llvm;
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace clang;
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic bool
713c827367444ee418f129b2c238299f49d3264554Jarkko PoyryGetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type)
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    switch (type_class)
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case clang::Type::Record:
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case clang::Type::Enum:
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        {
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            if (tag_type)
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            {
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                clang::TagDecl *tag_decl = tag_type->getDecl();
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                if (tag_decl)
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                {
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    if (tag_decl->getDefinition())
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        return true;
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    if (tag_decl->hasExternalLexicalStorage())
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    {
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        ExternalASTSource *external_ast_source = ast->getExternalSource();
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        if (external_ast_source)
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        {
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                            external_ast_source->CompleteType(tag_decl);
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                            return !tag_type->isIncompleteType();
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        }
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    }
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    return false;
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                }
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            }
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        break;
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case clang::Type::ObjCObject:
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case clang::Type::ObjCInterface:
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        {
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            if (objc_class_type)
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            {
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                // We currently can't complete objective C types through the newly added ASTContext
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                // because it only supports TagDecl objects right now...
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                bool is_forward_decl = class_interface_decl->isForwardDecl();
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                if (is_forward_decl && class_interface_decl->hasExternalLexicalStorage())
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                {
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    ExternalASTSource *external_ast_source = ast->getExternalSource();
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    if (external_ast_source)
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    {
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        external_ast_source->CompleteType (class_interface_decl);
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                        is_forward_decl = class_interface_decl->isForwardDecl();
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    }
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    return is_forward_decl == false;
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                }
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                return true;
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            }
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        break;
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case clang::Type::Typedef:
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType());
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    default:
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        break;
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return true;
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic AccessSpecifier
1413c827367444ee418f129b2c238299f49d3264554Jarkko PoyryConvertAccessTypeToAccessSpecifier (AccessType access)
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    switch (access)
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    default:               break;
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessNone:      return AS_none;
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessPublic:    return AS_public;
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessPrivate:   return AS_private;
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessProtected: return AS_protected;
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return AS_none;
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic ObjCIvarDecl::AccessControl
1553c827367444ee418f129b2c238299f49d3264554Jarkko PoyryConvertAccessTypeToObjCIvarAccessControl (AccessType access)
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    switch (access)
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    default:               break;
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessNone:      return ObjCIvarDecl::None;
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessPublic:    return ObjCIvarDecl::Public;
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessPrivate:   return ObjCIvarDecl::Private;
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessProtected: return ObjCIvarDecl::Protected;
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    case eAccessPackage:   return ObjCIvarDecl::Package;
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return ObjCIvarDecl::None;
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void
1713c827367444ee418f129b2c238299f49d3264554Jarkko PoyryParseLangArgs
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry(
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    LangOptions &Opts,
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    InputKind IK
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry)
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: Cleanup per-file based stuff.
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Set some properties which depend soley on the input kind; it would be nice
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // to move these to the language standard, and have the driver resolve the
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // input kind + language standard.
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (IK == IK_Asm) {
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.AsmPreprocessor = 1;
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    } else if (IK == IK_ObjC ||
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               IK == IK_ObjCXX ||
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               IK == IK_PreprocessedObjC ||
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               IK == IK_PreprocessedObjCXX) {
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.ObjC1 = Opts.ObjC2 = 1;
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    LangStandard::Kind LangStd = LangStandard::lang_unspecified;
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (LangStd == LangStandard::lang_unspecified) {
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        // Based on the base language, pick one.
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        switch (IK) {
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_None:
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_AST:
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_LLVM_IR:
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                assert (!"Invalid input kind!");
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_OpenCL:
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                LangStd = LangStandard::lang_opencl;
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                break;
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_CUDA:
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                LangStd = LangStandard::lang_cuda;
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                break;
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_Asm:
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_C:
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_PreprocessedC:
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_ObjC:
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_PreprocessedObjC:
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                LangStd = LangStandard::lang_gnu99;
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                break;
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_CXX:
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_PreprocessedCXX:
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_ObjCXX:
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            case IK_PreprocessedObjCXX:
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                LangStd = LangStandard::lang_gnucxx98;
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                break;
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.BCPLComment = Std.hasBCPLComments();
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.C99 = Std.isC99();
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.CPlusPlus = Std.isCPlusPlus();
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.CPlusPlus0x = Std.isCPlusPlus0x();
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.Digraphs = Std.hasDigraphs();
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.GNUMode = Std.isGNUMode();
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.GNUInline = !Std.isC99();
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.HexFloats = Std.hasHexFloats();
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.ImplicitInt = Std.hasImplicitInt();
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // OpenCL has some additional defaults.
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (LangStd == LangStandard::lang_opencl) {
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.OpenCL = 1;
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.AltiVec = 1;
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.CXXOperatorNames = 1;
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.LaxVectorConversions = 1;
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // OpenCL and C++ both have bool, true, false keywords.
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Opts.CPlusPlus)
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Args.hasArg(OPT_fobjc_gc_only))
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.setGCMode(LangOptions::GCOnly);
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    else if (Args.hasArg(OPT_fobjc_gc))
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.setGCMode(LangOptions::HybridGC);
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Args.hasArg(OPT_print_ivar_layout))
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.ObjCGCBitmapPrint = 1;
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Args.hasArg(OPT_faltivec))
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.AltiVec = 1;
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Args.hasArg(OPT_pthread))
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.POSIXThreads = 1;
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                                          "default");
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Vis == "default")
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Opts.setVisibilityMode(DefaultVisibility);
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    else if (Vis == "hidden")
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.setVisibilityMode(LangOptions::Hidden);
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    else if (Vis == "protected")
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.setVisibilityMode(LangOptions::Protected);
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    else
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Diags.Report(diag::err_drv_invalid_value)
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // is specified, or -std is set to a conforming mode.
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.Trigraphs = !Opts.GNUMode;
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Args.hasArg(OPT_trigraphs))
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.Trigraphs = 1;
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                                     OPT_fno_dollars_in_identifiers,
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                                     !Opts.AsmPreprocessor);
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    if (Args.hasArg(OPT_fno_lax_vector_conversions))
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        Opts.LaxVectorConversions = 0;
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.Exceptions = Args.hasArg(OPT_fexceptions);
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.Blocks = Args.hasArg(OPT_fblocks);
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.AccessControl = Args.hasArg(OPT_faccess_control);
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                                                 Diags);
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.ObjCConstantStringClass = getLastArgValue(Args,
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                                                   OPT_fconstant_string_class);
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.Static = Args.hasArg(OPT_static_define);
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.OptimizeSize = 0;
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: Eliminate this dependency.
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    unsigned Opt =
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    Opts.Optimize = Opt != 0;
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    unsigned Opt = 0;
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // This is the __NO_INLINE__ define, which just depends on things like the
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // optimization level and -fno-inline, not actually whether the backend has
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // inlining enabled.
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    //
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // FIXME: This is affected by other options (-fno-inline).
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Opts.NoInline = !Opt;
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    switch (SSP) {
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        default:
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//            Diags.Report(diag::err_drv_invalid_value)
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//            << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//            break;
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        case 1: Opts.setStackProtectorMode(LangOptions::SSPOn);  break;
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//    }
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3393c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::ClangASTContext (const char *target_triple) :
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_triple(),
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_ast_ap(),
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_language_options_ap(),
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_source_manager_ap(),
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_diagnostic_ap(),
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_options_ap(),
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_info_ap(),
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_identifier_table_ap(),
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_selector_table_ap(),
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_builtins_ap(),
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_callback_tag_decl (NULL),
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_callback_objc_decl (NULL),
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_callback_baton (NULL)
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (target_triple && target_triple[0])
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_target_triple.assign (target_triple);
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//----------------------------------------------------------------------
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Destructor
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//----------------------------------------------------------------------
3623c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::~ClangASTContext()
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_builtins_ap.reset();
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_selector_table_ap.reset();
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_identifier_table_ap.reset();
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_info_ap.reset();
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_options_ap.reset();
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_diagnostic_ap.reset();
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_source_manager_ap.reset();
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_language_options_ap.reset();
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_ast_ap.reset();
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
3773c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::Clear()
3783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_ast_ap.reset();
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_language_options_ap.reset();
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_source_manager_ap.reset();
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_diagnostic_ap.reset();
3833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_options_ap.reset();
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_info_ap.reset();
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_identifier_table_ap.reset();
3863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_selector_table_ap.reset();
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_builtins_ap.reset();
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst char *
3913c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::GetTargetTriple ()
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_target_triple.c_str();
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3963c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
3973c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::SetTargetTriple (const char *target_triple)
3983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Clear();
4003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_triple.assign(target_triple);
4013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
4043c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::SetArchitecture (const ArchSpec &arch)
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Clear();
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    m_target_triple.assign(arch.GetTriple().str());
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool
4113c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::HasExternalSource ()
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    ASTContext *ast = getASTContext();
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (ast)
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        return ast->getExternalSource () != NULL;
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
4203c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
4213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    ASTContext *ast = getASTContext();
4233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (ast)
4243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
4253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        ast->setExternalSource (ast_source_ap);
4263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
4273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
4283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4313c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
4323c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::RemoveExternalSource ()
4333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    ASTContext *ast = getASTContext();
4353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (ast)
4373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
4383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
4393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        ast->setExternalSource (empty_ast_source_ap);
4403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
4413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
4423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4473c827367444ee418f129b2c238299f49d3264554Jarkko PoyryASTContext *
4483c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getASTContext()
4493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_ast_ap.get() == NULL)
4513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
4523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
4533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       *getSourceManager(),
4543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       *getTargetInfo(),
4553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       *getIdentifierTable(),
4563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       *getSelectorTable(),
4573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       *getBuiltinContext(),
4583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       0));
4593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
4613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        {
4623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
4633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
4643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
4653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_ast_ap->getDiagnostics().setClient(getDiagnosticClient(), false);
4673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_ast_ap.get();
4693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4713c827367444ee418f129b2c238299f49d3264554Jarkko PoyryBuiltin::Context *
4723c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getBuiltinContext()
4733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_builtins_ap.get() == NULL)
4753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_builtins_ap.reset (new Builtin::Context(*getTargetInfo()));
4763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_builtins_ap.get();
4773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4793c827367444ee418f129b2c238299f49d3264554Jarkko PoyryIdentifierTable *
4803c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getIdentifierTable()
4813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_identifier_table_ap.get() == NULL)
4833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
4843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_identifier_table_ap.get();
4853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4873c827367444ee418f129b2c238299f49d3264554Jarkko PoyryLangOptions *
4883c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getLanguageOptions()
4893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_language_options_ap.get() == NULL)
4913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
4923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_language_options_ap.reset(new LangOptions());
4933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
4943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//        InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
4953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_language_options_ap.get();
4973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4993c827367444ee418f129b2c238299f49d3264554Jarkko PoyrySelectorTable *
5003c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getSelectorTable()
5013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_selector_table_ap.get() == NULL)
5033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_selector_table_ap.reset (new SelectorTable());
5043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_selector_table_ap.get();
5053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5073c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclang::FileManager *
5083c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getFileManager()
5093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_file_manager_ap.get() == NULL)
5113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
5123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        clang::FileSystemOptions file_system_options;
5133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_file_manager_ap.reset(new clang::FileManager(file_system_options));
5143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
5153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_file_manager_ap.get();
5163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5183c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclang::SourceManager *
5193c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getSourceManager()
5203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_source_manager_ap.get() == NULL)
5223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_source_manager_ap.reset(new clang::SourceManager(*getDiagnostic(), *getFileManager()));
5233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_source_manager_ap.get();
5243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5263c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDiagnostic *
5273c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getDiagnostic()
5283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_diagnostic_ap.get() == NULL)
5303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
5313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
5323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_diagnostic_ap.reset(new Diagnostic(diag_id_sp));
5333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
5343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_diagnostic_ap.get();
5353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass NullDiagnosticClient : public DiagnosticClient
5383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
5403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    NullDiagnosticClient ()
5413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
5423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
5433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
5443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    void HandleDiagnostic (Diagnostic::Level DiagLevel, const DiagnosticInfo &info)
5463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
5473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (m_log)
5483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        {
5493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            llvm::SmallVectorImpl<char> diag_str(10);
5503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            info.FormatDiagnostic(diag_str);
5513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            diag_str.push_back('\0');
5523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
5533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
5543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
5553c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
5563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    LogSP m_log;
5573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
5583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5593c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDiagnosticClient *
5603c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getDiagnosticClient()
5613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_diagnostic_client_ap.get() == NULL)
5633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_diagnostic_client_ap.reset(new NullDiagnosticClient);
5643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_diagnostic_client_ap.get();
5663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5683c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTargetOptions *
5693c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getTargetOptions()
5703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_target_options_ap.get() == NULL && !m_target_triple.empty())
5723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    {
5733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_target_options_ap.reset (new TargetOptions());
5743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (m_target_options_ap.get())
5753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry            m_target_options_ap->Triple = m_target_triple;
5763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
5773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_target_options_ap.get();
5783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5813c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTargetInfo *
5823c827367444ee418f129b2c238299f49d3264554Jarkko PoyryClangASTContext::getTargetInfo()
5833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // target_triple should be something like "x86_64-apple-darwin10"
5853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
5863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnostic(), *getTargetOptions()));
5873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return m_target_info_ap.get();
5883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#pragma mark Basic Types
5913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5923c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic inline bool
5933c827367444ee418f129b2c238299f49d3264554Jarkko PoyryQualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
5943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
5963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (qual_type_bit_size == bit_size)
5973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        return true;
5983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
599}
600
601clang_type_t
602ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
603{
604    ASTContext *ast = getASTContext();
605
606    assert (ast != NULL);
607
608    return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
609}
610
611clang_type_t
612ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
613{
614    if (!ast)
615        return NULL;
616
617    switch (encoding)
618    {
619    case eEncodingInvalid:
620        if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
621            return ast->VoidPtrTy.getAsOpaquePtr();
622        break;
623
624    case eEncodingUint:
625        if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
626            return ast->UnsignedCharTy.getAsOpaquePtr();
627        if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
628            return ast->UnsignedShortTy.getAsOpaquePtr();
629        if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
630            return ast->UnsignedIntTy.getAsOpaquePtr();
631        if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
632            return ast->UnsignedLongTy.getAsOpaquePtr();
633        if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
634            return ast->UnsignedLongLongTy.getAsOpaquePtr();
635        if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
636            return ast->UnsignedInt128Ty.getAsOpaquePtr();
637        break;
638
639    case eEncodingSint:
640        if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
641            return ast->CharTy.getAsOpaquePtr();
642        if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
643            return ast->ShortTy.getAsOpaquePtr();
644        if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
645            return ast->IntTy.getAsOpaquePtr();
646        if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
647            return ast->LongTy.getAsOpaquePtr();
648        if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
649            return ast->LongLongTy.getAsOpaquePtr();
650        if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
651            return ast->Int128Ty.getAsOpaquePtr();
652        break;
653
654    case eEncodingIEEE754:
655        if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
656            return ast->FloatTy.getAsOpaquePtr();
657        if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
658            return ast->DoubleTy.getAsOpaquePtr();
659        if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
660            return ast->LongDoubleTy.getAsOpaquePtr();
661        break;
662
663    case eEncodingVector:
664    default:
665        break;
666    }
667
668    return NULL;
669}
670
671clang_type_t
672ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
673{
674    ASTContext *ast = getASTContext();
675
676    #define streq(a,b) strcmp(a,b) == 0
677    assert (ast != NULL);
678    if (ast)
679    {
680        switch (dw_ate)
681        {
682        default:
683            break;
684
685        case DW_ATE_address:
686            if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
687                return ast->VoidPtrTy.getAsOpaquePtr();
688            break;
689
690        case DW_ATE_boolean:
691            if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
692                return ast->BoolTy.getAsOpaquePtr();
693            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
694                return ast->UnsignedCharTy.getAsOpaquePtr();
695            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
696                return ast->UnsignedShortTy.getAsOpaquePtr();
697            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
698                return ast->UnsignedIntTy.getAsOpaquePtr();
699            break;
700
701        case DW_ATE_lo_user:
702            // This has been seen to mean DW_AT_complex_integer
703            if (::strstr(type_name, "complex"))
704            {
705                clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
706                return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
707            }
708            break;
709
710        case DW_ATE_complex_float:
711            if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
712                return ast->FloatComplexTy.getAsOpaquePtr();
713            else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
714                return ast->DoubleComplexTy.getAsOpaquePtr();
715            else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
716                return ast->LongDoubleComplexTy.getAsOpaquePtr();
717            else
718            {
719                clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
720                return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
721            }
722            break;
723
724        case DW_ATE_float:
725            if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
726                return ast->FloatTy.getAsOpaquePtr();
727            if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
728                return ast->DoubleTy.getAsOpaquePtr();
729            if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
730                return ast->LongDoubleTy.getAsOpaquePtr();
731            break;
732
733        case DW_ATE_signed:
734            if (type_name)
735            {
736                if (strstr(type_name, "long long"))
737                {
738                    if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
739                        return ast->LongLongTy.getAsOpaquePtr();
740                }
741                else if (strstr(type_name, "long"))
742                {
743                    if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
744                        return ast->LongTy.getAsOpaquePtr();
745                }
746                else if (strstr(type_name, "short"))
747                {
748                    if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
749                        return ast->ShortTy.getAsOpaquePtr();
750                }
751                else if (strstr(type_name, "char"))
752                {
753                    if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
754                        return ast->CharTy.getAsOpaquePtr();
755                    if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
756                        return ast->SignedCharTy.getAsOpaquePtr();
757                }
758                else if (strstr(type_name, "int"))
759                {
760                    if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
761                        return ast->IntTy.getAsOpaquePtr();
762                    if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
763                        return ast->Int128Ty.getAsOpaquePtr();
764                }
765                else if (streq(type_name, "wchar_t"))
766                {
767                    if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
768                        return ast->WCharTy.getAsOpaquePtr();
769                }
770                else if (streq(type_name, "void"))
771                {
772                    if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
773                        return ast->VoidTy.getAsOpaquePtr();
774                }
775            }
776            // We weren't able to match up a type name, just search by size
777            if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
778                return ast->CharTy.getAsOpaquePtr();
779            if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
780                return ast->ShortTy.getAsOpaquePtr();
781            if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
782                return ast->IntTy.getAsOpaquePtr();
783            if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
784                return ast->LongTy.getAsOpaquePtr();
785            if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
786                return ast->LongLongTy.getAsOpaquePtr();
787            if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
788                return ast->Int128Ty.getAsOpaquePtr();
789            break;
790
791        case DW_ATE_signed_char:
792            if (type_name)
793            {
794                if (streq(type_name, "signed char"))
795                {
796                    if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
797                        return ast->SignedCharTy.getAsOpaquePtr();
798                }
799            }
800            if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
801                return ast->CharTy.getAsOpaquePtr();
802            if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
803                return ast->SignedCharTy.getAsOpaquePtr();
804            break;
805
806        case DW_ATE_unsigned:
807            if (type_name)
808            {
809                if (strstr(type_name, "long long"))
810                {
811                    if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
812                        return ast->UnsignedLongLongTy.getAsOpaquePtr();
813                }
814                else if (strstr(type_name, "long"))
815                {
816                    if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
817                        return ast->UnsignedLongTy.getAsOpaquePtr();
818                }
819                else if (strstr(type_name, "short"))
820                {
821                    if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
822                        return ast->UnsignedShortTy.getAsOpaquePtr();
823                }
824                else if (strstr(type_name, "char"))
825                {
826                    if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
827                        return ast->UnsignedCharTy.getAsOpaquePtr();
828                }
829                else if (strstr(type_name, "int"))
830                {
831                    if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
832                        return ast->UnsignedIntTy.getAsOpaquePtr();
833                    if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
834                        return ast->UnsignedInt128Ty.getAsOpaquePtr();
835                }
836            }
837            // We weren't able to match up a type name, just search by size
838            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
839                return ast->UnsignedCharTy.getAsOpaquePtr();
840            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
841                return ast->UnsignedShortTy.getAsOpaquePtr();
842            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
843                return ast->UnsignedIntTy.getAsOpaquePtr();
844            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
845                return ast->UnsignedLongTy.getAsOpaquePtr();
846            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
847                return ast->UnsignedLongLongTy.getAsOpaquePtr();
848            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
849                return ast->UnsignedInt128Ty.getAsOpaquePtr();
850            break;
851
852        case DW_ATE_unsigned_char:
853            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
854                return ast->UnsignedCharTy.getAsOpaquePtr();
855            if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
856                return ast->UnsignedShortTy.getAsOpaquePtr();
857            break;
858
859        case DW_ATE_imaginary_float:
860            break;
861        }
862    }
863    // This assert should fire for anything that we don't catch above so we know
864    // to fix any issues we run into.
865    assert (!"error: ClangASTContext::GetClangTypeForDWARFEncodingAndSize() contains an unhandled encoding. Fix this ASAP!");
866    return NULL;
867}
868
869clang_type_t
870ClangASTContext::GetBuiltInType_void(ASTContext *ast)
871{
872    return ast->VoidTy.getAsOpaquePtr();
873}
874
875clang_type_t
876ClangASTContext::GetBuiltInType_bool()
877{
878    return getASTContext()->BoolTy.getAsOpaquePtr();
879}
880
881clang_type_t
882ClangASTContext::GetBuiltInType_objc_id()
883{
884    return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinIdTy).getAsOpaquePtr();
885}
886
887clang_type_t
888ClangASTContext::GetBuiltInType_objc_Class()
889{
890    return getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr();
891}
892
893clang_type_t
894ClangASTContext::GetBuiltInType_objc_selector()
895{
896    return getASTContext()->getPointerType(getASTContext()->ObjCBuiltinSelTy).getAsOpaquePtr();
897}
898
899clang_type_t
900ClangASTContext::GetCStringType (bool is_const)
901{
902    QualType char_type(getASTContext()->CharTy);
903
904    if (is_const)
905        char_type.addConst();
906
907    return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
908}
909
910clang_type_t
911ClangASTContext::GetVoidPtrType (bool is_const)
912{
913    return GetVoidPtrType(getASTContext(), is_const);
914}
915
916clang_type_t
917ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
918{
919    QualType void_ptr_type(ast->VoidPtrTy);
920
921    if (is_const)
922        void_ptr_type.addConst();
923
924    return void_ptr_type.getAsOpaquePtr();
925}
926
927clang_type_t
928ClangASTContext::CopyType (ASTContext *dst_ast,
929                           ASTContext *src_ast,
930                           clang_type_t clang_type)
931{
932    FileSystemOptions file_system_options;
933    FileManager file_manager (file_system_options);
934    ASTImporter importer(*dst_ast, file_manager,
935                         *src_ast, file_manager,
936                         false);
937
938    QualType src (QualType::getFromOpaquePtr(clang_type));
939    QualType dst (importer.Import(src));
940
941    return dst.getAsOpaquePtr();
942}
943
944
945clang::Decl *
946ClangASTContext::CopyDecl (ASTContext *dst_ast,
947                           ASTContext *src_ast,
948                           clang::Decl *source_decl)
949{
950    FileSystemOptions file_system_options;
951    FileManager file_manager (file_system_options);
952    ASTImporter importer(*dst_ast, file_manager,
953                         *src_ast, file_manager,
954                         false);
955
956    return importer.Import(source_decl);
957}
958
959bool
960ClangASTContext::AreTypesSame(ASTContext *ast,
961             clang_type_t type1,
962             clang_type_t type2)
963{
964    return ast->hasSameType (QualType::getFromOpaquePtr(type1),
965                             QualType::getFromOpaquePtr(type2));
966}
967
968#pragma mark CVR modifiers
969
970clang_type_t
971ClangASTContext::AddConstModifier (clang_type_t clang_type)
972{
973    if (clang_type)
974    {
975        QualType result(QualType::getFromOpaquePtr(clang_type));
976        result.addConst();
977        return result.getAsOpaquePtr();
978    }
979    return NULL;
980}
981
982clang_type_t
983ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
984{
985    if (clang_type)
986    {
987        QualType result(QualType::getFromOpaquePtr(clang_type));
988        result.getQualifiers().setRestrict (true);
989        return result.getAsOpaquePtr();
990    }
991    return NULL;
992}
993
994clang_type_t
995ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
996{
997    if (clang_type)
998    {
999        QualType result(QualType::getFromOpaquePtr(clang_type));
1000        result.getQualifiers().setVolatile (true);
1001        return result.getAsOpaquePtr();
1002    }
1003    return NULL;
1004}
1005
1006
1007clang_type_t
1008ClangASTContext::GetTypeForDecl (TagDecl *decl)
1009{
1010    // No need to call the getASTContext() accessor (which can create the AST
1011    // if it isn't created yet, because we can't have created a decl in this
1012    // AST if our AST didn't already exist...
1013    if (m_ast_ap.get())
1014        return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1015    return NULL;
1016}
1017
1018clang_type_t
1019ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1020{
1021    // No need to call the getASTContext() accessor (which can create the AST
1022    // if it isn't created yet, because we can't have created a decl in this
1023    // AST if our AST didn't already exist...
1024    if (m_ast_ap.get())
1025        return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1026    return NULL;
1027}
1028
1029#pragma mark Structure, Unions, Classes
1030
1031clang_type_t
1032ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx, LanguageType language)
1033{
1034    ASTContext *ast = getASTContext();
1035    assert (ast != NULL);
1036
1037    if (decl_ctx == NULL)
1038        decl_ctx = ast->getTranslationUnitDecl();
1039
1040
1041    if (language == eLanguageTypeObjC)
1042    {
1043        bool isForwardDecl = true;
1044        bool isInternal = false;
1045        return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal);
1046    }
1047
1048    // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1049    // we will need to update this code. I was told to currently always use
1050    // the CXXRecordDecl class since we often don't know from debug information
1051    // if something is struct or a class, so we default to always use the more
1052    // complete definition just in case.
1053    CXXRecordDecl *decl = CXXRecordDecl::Create(*ast,
1054                                                (TagDecl::TagKind)kind,
1055                                                decl_ctx,
1056                                                SourceLocation(),
1057                                                SourceLocation(),
1058                                                name && name[0] ? &ast->Idents.get(name) : NULL);
1059
1060    return ast->getTagDeclType(decl).getAsOpaquePtr();
1061}
1062
1063bool
1064ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1065{
1066    if (clang_type == NULL)
1067        return false;
1068
1069    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1070
1071    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1072    switch (type_class)
1073    {
1074    case clang::Type::Record:
1075        {
1076            CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1077            if (cxx_record_decl)
1078            {
1079                cxx_record_decl->setHasExternalLexicalStorage (has_extern);
1080                cxx_record_decl->setHasExternalVisibleStorage (has_extern);
1081                return true;
1082            }
1083        }
1084        break;
1085
1086    case clang::Type::Enum:
1087        {
1088            EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1089            if (enum_decl)
1090            {
1091                enum_decl->setHasExternalLexicalStorage (has_extern);
1092                enum_decl->setHasExternalVisibleStorage (has_extern);
1093                return true;
1094            }
1095        }
1096        break;
1097
1098    case clang::Type::ObjCObject:
1099    case clang::Type::ObjCInterface:
1100        {
1101            const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
1102            assert (objc_class_type);
1103            if (objc_class_type)
1104            {
1105                ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1106
1107                if (class_interface_decl)
1108                {
1109                    class_interface_decl->setHasExternalLexicalStorage (has_extern);
1110                    class_interface_decl->setHasExternalVisibleStorage (has_extern);
1111                    return true;
1112                }
1113            }
1114        }
1115        break;
1116
1117    case clang::Type::Typedef:
1118        return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
1119
1120    default:
1121        break;
1122    }
1123    return false;
1124}
1125
1126static bool
1127IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1128{
1129    if (name == NULL || name[0] == '\0')
1130        return false;
1131
1132#define OPERATOR_PREFIX "operator"
1133#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
1134
1135    const char *post_op_name = NULL;
1136
1137    bool no_space = true;
1138
1139    if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
1140        return false;
1141
1142    post_op_name = name + OPERATOR_PREFIX_LENGTH;
1143
1144    if (post_op_name[0] == ' ')
1145    {
1146        post_op_name++;
1147        no_space = false;
1148    }
1149
1150#undef OPERATOR_PREFIX
1151#undef OPERATOR_PREFIX_LENGTH
1152
1153    // This is an operator, set the overloaded operator kind to invalid
1154    // in case this is a conversion operator...
1155    op_kind = NUM_OVERLOADED_OPERATORS;
1156
1157    switch (post_op_name[0])
1158    {
1159    default:
1160        if (no_space)
1161            return false;
1162        break;
1163    case 'n':
1164        if (no_space)
1165            return false;
1166        if  (strcmp (post_op_name, "new") == 0)
1167            op_kind = OO_New;
1168        else if (strcmp (post_op_name, "new[]") == 0)
1169            op_kind = OO_Array_New;
1170        break;
1171
1172    case 'd':
1173        if (no_space)
1174            return false;
1175        if (strcmp (post_op_name, "delete") == 0)
1176            op_kind = OO_Delete;
1177        else if (strcmp (post_op_name, "delete[]") == 0)
1178            op_kind = OO_Array_Delete;
1179        break;
1180
1181    case '+':
1182        if (post_op_name[1] == '\0')
1183            op_kind = OO_Plus;
1184        else if (post_op_name[2] == '\0')
1185        {
1186            if (post_op_name[1] == '=')
1187                op_kind = OO_PlusEqual;
1188            else if (post_op_name[1] == '+')
1189                op_kind = OO_PlusPlus;
1190        }
1191        break;
1192
1193    case '-':
1194        if (post_op_name[1] == '\0')
1195            op_kind = OO_Minus;
1196        else if (post_op_name[2] == '\0')
1197        {
1198            switch (post_op_name[1])
1199            {
1200            case '=': op_kind = OO_MinusEqual; break;
1201            case '-': op_kind = OO_MinusMinus; break;
1202            case '>': op_kind = OO_Arrow; break;
1203            }
1204        }
1205        else if (post_op_name[3] == '\0')
1206        {
1207            if (post_op_name[2] == '*')
1208                op_kind = OO_ArrowStar; break;
1209        }
1210        break;
1211
1212    case '*':
1213        if (post_op_name[1] == '\0')
1214            op_kind = OO_Star;
1215        else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1216            op_kind = OO_StarEqual;
1217        break;
1218
1219    case '/':
1220        if (post_op_name[1] == '\0')
1221            op_kind = OO_Slash;
1222        else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1223            op_kind = OO_SlashEqual;
1224        break;
1225
1226    case '%':
1227        if (post_op_name[1] == '\0')
1228            op_kind = OO_Percent;
1229        else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1230            op_kind = OO_PercentEqual;
1231        break;
1232
1233
1234    case '^':
1235        if (post_op_name[1] == '\0')
1236            op_kind = OO_Caret;
1237        else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1238            op_kind = OO_CaretEqual;
1239        break;
1240
1241    case '&':
1242        if (post_op_name[1] == '\0')
1243            op_kind = OO_Amp;
1244        else if (post_op_name[2] == '\0')
1245        {
1246            switch (post_op_name[1])
1247            {
1248            case '=': op_kind = OO_AmpEqual; break;
1249            case '&': op_kind = OO_AmpAmp; break;
1250            }
1251        }
1252        break;
1253
1254    case '|':
1255        if (post_op_name[1] == '\0')
1256            op_kind = OO_Pipe;
1257        else if (post_op_name[2] == '\0')
1258        {
1259            switch (post_op_name[1])
1260            {
1261            case '=': op_kind = OO_PipeEqual; break;
1262            case '|': op_kind = OO_PipePipe; break;
1263            }
1264        }
1265        break;
1266
1267    case '~':
1268        if (post_op_name[1] == '\0')
1269            op_kind = OO_Tilde;
1270        break;
1271
1272    case '!':
1273        if (post_op_name[1] == '\0')
1274            op_kind = OO_Exclaim;
1275        else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1276            op_kind = OO_ExclaimEqual;
1277        break;
1278
1279    case '=':
1280        if (post_op_name[1] == '\0')
1281            op_kind = OO_Equal;
1282        else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1283            op_kind = OO_EqualEqual;
1284        break;
1285
1286    case '<':
1287        if (post_op_name[1] == '\0')
1288            op_kind = OO_Less;
1289        else if (post_op_name[2] == '\0')
1290        {
1291            switch (post_op_name[1])
1292            {
1293            case '<': op_kind = OO_LessLess; break;
1294            case '=': op_kind = OO_LessEqual; break;
1295            }
1296        }
1297        else if (post_op_name[3] == '\0')
1298        {
1299            if (post_op_name[2] == '=')
1300                op_kind = OO_LessLessEqual;
1301        }
1302        break;
1303
1304    case '>':
1305        if (post_op_name[1] == '\0')
1306            op_kind = OO_Greater;
1307        else if (post_op_name[2] == '\0')
1308        {
1309            switch (post_op_name[1])
1310            {
1311            case '>': op_kind = OO_GreaterGreater; break;
1312            case '=': op_kind = OO_GreaterEqual; break;
1313            }
1314        }
1315        else if (post_op_name[1] == '>' &&
1316                 post_op_name[2] == '=' &&
1317                 post_op_name[3] == '\0')
1318        {
1319                op_kind = OO_GreaterGreaterEqual;
1320        }
1321        break;
1322
1323    case ',':
1324        if (post_op_name[1] == '\0')
1325            op_kind = OO_Comma;
1326        break;
1327
1328    case '(':
1329        if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1330            op_kind = OO_Call;
1331        break;
1332
1333    case '[':
1334        if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1335            op_kind = OO_Subscript;
1336        break;
1337    }
1338
1339    return true;
1340}
1341
1342CXXMethodDecl *
1343ClangASTContext::AddMethodToCXXRecordType
1344(
1345    ASTContext *ast,
1346    clang_type_t record_opaque_type,
1347    const char *name,
1348    clang_type_t method_opaque_type,
1349    lldb::AccessType access,
1350    bool is_virtual,
1351    bool is_static,
1352    bool is_inline,
1353    bool is_explicit
1354)
1355{
1356    if (!record_opaque_type || !method_opaque_type || !name)
1357        return NULL;
1358
1359    assert(ast);
1360
1361    IdentifierTable *identifier_table = &ast->Idents;
1362
1363    assert(identifier_table);
1364
1365    QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
1366
1367    CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
1368
1369    if (cxx_record_decl == NULL)
1370        return NULL;
1371
1372    QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
1373
1374    CXXMethodDecl *cxx_method_decl = NULL;
1375
1376    DeclarationName decl_name (&identifier_table->get(name));
1377
1378    const bool is_implicitly_declared = false;
1379
1380    const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
1381
1382    if (function_Type == NULL)
1383        return NULL;
1384
1385    const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
1386
1387    if (!method_function_prototype)
1388        return NULL;
1389
1390    unsigned int num_params = method_function_prototype->getNumArgs();
1391
1392    if (name[0] == '~')
1393    {
1394        cxx_method_decl = CXXDestructorDecl::Create (*ast,
1395                                                     cxx_record_decl,
1396                                                     SourceLocation(),
1397                                                     DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1398                                                     method_qual_type,
1399                                                     NULL,
1400                                                     is_inline,
1401                                                     is_implicitly_declared);
1402    }
1403    else if (decl_name == cxx_record_decl->getDeclName())
1404    {
1405        cxx_method_decl = CXXConstructorDecl::Create (*ast,
1406                                                      cxx_record_decl,
1407                                                      SourceLocation(),
1408                                                      DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1409                                                      method_qual_type,
1410                                                      NULL, // TypeSourceInfo *
1411                                                      is_explicit,
1412                                                      is_inline,
1413                                                      is_implicitly_declared);
1414    }
1415    else
1416    {
1417
1418        OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1419        if (IsOperator (name, op_kind))
1420        {
1421            if (op_kind != NUM_OVERLOADED_OPERATORS)
1422            {
1423                cxx_method_decl = CXXMethodDecl::Create (*ast,
1424                                                         cxx_record_decl,
1425                                                         SourceLocation(),
1426                                                         DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
1427                                                         method_qual_type,
1428                                                         NULL, // TypeSourceInfo *
1429                                                         is_static,
1430                                                         SC_None,
1431                                                         is_inline,
1432                                                         SourceLocation());
1433            }
1434            else if (num_params == 0)
1435            {
1436                // Conversion operators don't take params...
1437                cxx_method_decl = CXXConversionDecl::Create (*ast,
1438                                                             cxx_record_decl,
1439                                                             SourceLocation(),
1440                                                             DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
1441                                                             method_qual_type,
1442                                                             NULL, // TypeSourceInfo *
1443                                                             is_inline,
1444                                                             is_explicit,
1445                                                             SourceLocation());
1446            }
1447        }
1448
1449        if (cxx_method_decl == NULL)
1450        {
1451            cxx_method_decl = CXXMethodDecl::Create (*ast,
1452                                                     cxx_record_decl,
1453                                                     SourceLocation(),
1454                                                     DeclarationNameInfo (decl_name, SourceLocation()),
1455                                                     method_qual_type,
1456                                                     NULL, // TypeSourceInfo *
1457                                                     is_static,
1458                                                     SC_None,
1459                                                     is_inline,
1460                                                     SourceLocation());
1461        }
1462    }
1463
1464    AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
1465
1466    cxx_method_decl->setAccess (access_specifier);
1467    cxx_method_decl->setVirtualAsWritten (is_virtual);
1468
1469    // Populate the method decl with parameter decls
1470
1471    ParmVarDecl *params[num_params];
1472
1473    for (int param_index = 0;
1474         param_index < num_params;
1475         ++param_index)
1476    {
1477        params[param_index] = ParmVarDecl::Create (*ast,
1478                                                   cxx_method_decl,
1479                                                   SourceLocation(),
1480                                                   SourceLocation(),
1481                                                   NULL, // anonymous
1482                                                   method_function_prototype->getArgType(param_index),
1483                                                   NULL,
1484                                                   SC_None,
1485                                                   SC_None,
1486                                                   NULL);
1487    }
1488
1489    cxx_method_decl->setParams (params, num_params);
1490
1491    cxx_record_decl->addDecl (cxx_method_decl);
1492
1493//    printf ("decl->isPolymorphic()             = %i\n", cxx_record_decl->isPolymorphic());
1494//    printf ("decl->isAggregate()               = %i\n", cxx_record_decl->isAggregate());
1495//    printf ("decl->isPOD()                     = %i\n", cxx_record_decl->isPOD());
1496//    printf ("decl->isEmpty()                   = %i\n", cxx_record_decl->isEmpty());
1497//    printf ("decl->isAbstract()                = %i\n", cxx_record_decl->isAbstract());
1498//    printf ("decl->hasTrivialConstructor()     = %i\n", cxx_record_decl->hasTrivialConstructor());
1499//    printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1500//    printf ("decl->hasTrivialCopyAssignment()  = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1501//    printf ("decl->hasTrivialDestructor()      = %i\n", cxx_record_decl->hasTrivialDestructor());
1502    return cxx_method_decl;
1503}
1504
1505bool
1506ClangASTContext::AddFieldToRecordType
1507(
1508    ASTContext *ast,
1509    clang_type_t record_clang_type,
1510    const char *name,
1511    clang_type_t field_type,
1512    AccessType access,
1513    uint32_t bitfield_bit_size
1514)
1515{
1516    if (record_clang_type == NULL || field_type == NULL)
1517        return false;
1518
1519    IdentifierTable *identifier_table = &ast->Idents;
1520
1521    assert (ast != NULL);
1522    assert (identifier_table != NULL);
1523
1524    QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1525
1526    const clang::Type *clang_type = record_qual_type.getTypePtr();
1527    if (clang_type)
1528    {
1529        const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1530
1531        if (record_type)
1532        {
1533            RecordDecl *record_decl = record_type->getDecl();
1534
1535            clang::Expr *bit_width = NULL;
1536            if (bitfield_bit_size != 0)
1537            {
1538                APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1539                bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
1540            }
1541            FieldDecl *field = FieldDecl::Create (*ast,
1542                                                  record_decl,
1543                                                  SourceLocation(),
1544                                                  SourceLocation(),
1545                                                  name ? &identifier_table->get(name) : NULL, // Identifier
1546                                                  QualType::getFromOpaquePtr(field_type), // Field type
1547                                                  NULL,       // DeclaratorInfo *
1548                                                  bit_width,  // BitWidth
1549                                                  false);     // Mutable
1550
1551            field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
1552
1553            if (field)
1554            {
1555                record_decl->addDecl(field);
1556            }
1557        }
1558        else
1559        {
1560            const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
1561            if (objc_class_type)
1562            {
1563                bool is_synthesized = false;
1564                ClangASTContext::AddObjCClassIVar (ast,
1565                                                   record_clang_type,
1566                                                   name,
1567                                                   field_type,
1568                                                   access,
1569                                                   bitfield_bit_size,
1570                                                   is_synthesized);
1571            }
1572        }
1573    }
1574    return false;
1575}
1576
1577bool
1578ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1579{
1580    return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1581}
1582
1583bool
1584ClangASTContext::FieldIsBitfield
1585(
1586    ASTContext *ast,
1587    FieldDecl* field,
1588    uint32_t& bitfield_bit_size
1589)
1590{
1591    if (ast == NULL || field == NULL)
1592        return false;
1593
1594    if (field->isBitField())
1595    {
1596        Expr* bit_width_expr = field->getBitWidth();
1597        if (bit_width_expr)
1598        {
1599            llvm::APSInt bit_width_apsint;
1600            if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
1601            {
1602                bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1603                return true;
1604            }
1605        }
1606    }
1607    return false;
1608}
1609
1610bool
1611ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1612{
1613    if (record_decl == NULL)
1614        return false;
1615
1616    if (!record_decl->field_empty())
1617        return true;
1618
1619    // No fields, lets check this is a CXX record and check the base classes
1620    const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1621    if (cxx_record_decl)
1622    {
1623        CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1624        for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1625             base_class != base_class_end;
1626             ++base_class)
1627        {
1628            const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1629            if (RecordHasFields(base_class_decl))
1630                return true;
1631        }
1632    }
1633    return false;
1634}
1635
1636void
1637ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
1638{
1639    if (clang_type)
1640    {
1641        QualType qual_type(QualType::getFromOpaquePtr(clang_type));
1642
1643        const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
1644        if (record_type)
1645        {
1646            RecordDecl *record_decl = record_type->getDecl();
1647            if (record_decl)
1648            {
1649                uint32_t field_idx;
1650                RecordDecl::field_iterator field, field_end;
1651                for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
1652                     field != field_end;
1653                     ++field, ++field_idx)
1654                {
1655                    // If no accessibility was assigned, assign the correct one
1656                    if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
1657                        field->setAccess ((AccessSpecifier)default_accessibility);
1658                }
1659            }
1660        }
1661    }
1662}
1663
1664#pragma mark C++ Base Classes
1665
1666CXXBaseSpecifier *
1667ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
1668{
1669    if (base_class_type)
1670        return new CXXBaseSpecifier (SourceRange(),
1671                                     is_virtual,
1672                                     base_of_class,
1673                                     ConvertAccessTypeToAccessSpecifier (access),
1674                                     getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
1675                                     SourceLocation());
1676    return NULL;
1677}
1678
1679void
1680ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
1681{
1682    for (unsigned i=0; i<num_base_classes; ++i)
1683    {
1684        delete base_classes[i];
1685        base_classes[i] = NULL;
1686    }
1687}
1688
1689bool
1690ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
1691{
1692    if (class_clang_type)
1693    {
1694        CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
1695        if (cxx_record_decl)
1696        {
1697            cxx_record_decl->setBases(base_classes, num_base_classes);
1698            return true;
1699        }
1700    }
1701    return false;
1702}
1703#pragma mark Objective C Classes
1704
1705clang_type_t
1706ClangASTContext::CreateObjCClass
1707(
1708    const char *name,
1709    DeclContext *decl_ctx,
1710    bool isForwardDecl,
1711    bool isInternal
1712)
1713{
1714    ASTContext *ast = getASTContext();
1715    assert (ast != NULL);
1716    assert (name && name[0]);
1717    if (decl_ctx == NULL)
1718        decl_ctx = ast->getTranslationUnitDecl();
1719
1720    // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1721    // we will need to update this code. I was told to currently always use
1722    // the CXXRecordDecl class since we often don't know from debug information
1723    // if something is struct or a class, so we default to always use the more
1724    // complete definition just in case.
1725    ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
1726                                                         decl_ctx,
1727                                                         SourceLocation(),
1728                                                         &ast->Idents.get(name),
1729                                                         SourceLocation(),
1730                                                         isForwardDecl,
1731                                                         isInternal);
1732
1733    return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
1734}
1735
1736bool
1737ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
1738{
1739    if (class_opaque_type && super_opaque_type)
1740    {
1741        QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1742        QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
1743        const clang::Type *class_type = class_qual_type.getTypePtr();
1744        const clang::Type *super_type = super_qual_type.getTypePtr();
1745        if (class_type && super_type)
1746        {
1747            const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1748            const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
1749            if (objc_class_type && objc_super_type)
1750            {
1751                ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1752                ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
1753                if (class_interface_decl && super_interface_decl)
1754                {
1755                    class_interface_decl->setSuperClass(super_interface_decl);
1756                    return true;
1757                }
1758            }
1759        }
1760    }
1761    return false;
1762}
1763
1764
1765bool
1766ClangASTContext::AddObjCClassIVar
1767(
1768    ASTContext *ast,
1769    clang_type_t class_opaque_type,
1770    const char *name,
1771    clang_type_t ivar_opaque_type,
1772    AccessType access,
1773    uint32_t bitfield_bit_size,
1774    bool is_synthesized
1775)
1776{
1777    if (class_opaque_type == NULL || ivar_opaque_type == NULL)
1778        return false;
1779
1780    IdentifierTable *identifier_table = &ast->Idents;
1781
1782    assert (ast != NULL);
1783    assert (identifier_table != NULL);
1784
1785    QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1786
1787    const clang::Type *class_type = class_qual_type.getTypePtr();
1788    if (class_type)
1789    {
1790        const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1791
1792        if (objc_class_type)
1793        {
1794            ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1795
1796            if (class_interface_decl)
1797            {
1798                clang::Expr *bit_width = NULL;
1799                if (bitfield_bit_size != 0)
1800                {
1801                    APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1802                    bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
1803                }
1804
1805                ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast,
1806                                                            class_interface_decl,
1807                                                            SourceLocation(),
1808                                                            SourceLocation(),
1809                                                            &identifier_table->get(name), // Identifier
1810                                                            QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
1811                                                            NULL, // TypeSourceInfo *
1812                                                            ConvertAccessTypeToObjCIvarAccessControl (access),
1813                                                            bit_width,
1814                                                            is_synthesized);
1815
1816                if (field)
1817                {
1818                    class_interface_decl->addDecl(field);
1819                    return true;
1820                }
1821            }
1822        }
1823    }
1824    return false;
1825}
1826
1827
1828bool
1829ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
1830{
1831    QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1832
1833    const clang::Type *class_type = class_qual_type.getTypePtr();
1834    if (class_type)
1835    {
1836        const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1837
1838        if (objc_class_type)
1839            return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
1840    }
1841    return false;
1842}
1843
1844bool
1845ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
1846{
1847    while (class_interface_decl)
1848    {
1849        if (class_interface_decl->ivar_size() > 0)
1850            return true;
1851
1852        if (check_superclass)
1853            class_interface_decl = class_interface_decl->getSuperClass();
1854        else
1855            break;
1856    }
1857    return false;
1858}
1859
1860ObjCMethodDecl *
1861ClangASTContext::AddMethodToObjCObjectType
1862(
1863    ASTContext *ast,
1864    clang_type_t class_opaque_type,
1865    const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
1866    clang_type_t method_opaque_type,
1867    lldb::AccessType access
1868)
1869{
1870    if (class_opaque_type == NULL || method_opaque_type == NULL)
1871        return NULL;
1872
1873    IdentifierTable *identifier_table = &ast->Idents;
1874
1875    assert (ast != NULL);
1876    assert (identifier_table != NULL);
1877
1878    QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1879
1880    const clang::Type *class_type = class_qual_type.getTypePtr();
1881    if (class_type == NULL)
1882        return NULL;
1883
1884    const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1885
1886    if (objc_class_type == NULL)
1887        return NULL;
1888
1889    ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1890
1891    if (class_interface_decl == NULL)
1892        return NULL;
1893
1894    const char *selector_start = ::strchr (name, ' ');
1895    if (selector_start == NULL)
1896        return NULL;
1897
1898    selector_start++;
1899    if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
1900        return NULL;
1901    llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
1902
1903    size_t len = 0;
1904    const char *start;
1905    //printf ("name = '%s'\n", name);
1906
1907    unsigned num_selectors_with_args = 0;
1908    for (start = selector_start;
1909         start && *start != '\0' && *start != ']';
1910         start += len)
1911    {
1912        len = ::strcspn(start, ":]");
1913        bool has_arg = (start[len] == ':');
1914        if (has_arg)
1915            ++num_selectors_with_args;
1916        selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
1917        if (has_arg)
1918            len += 1;
1919    }
1920
1921
1922    if (selector_idents.size() == 0)
1923        return 0;
1924
1925    clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
1926                                                                          selector_idents.data());
1927
1928    QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
1929
1930    // Populate the method decl with parameter decls
1931    const clang::Type *method_type(method_qual_type.getTypePtr());
1932
1933    if (method_type == NULL)
1934        return NULL;
1935
1936    const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
1937
1938    if (!method_function_prototype)
1939        return NULL;
1940
1941
1942    bool is_variadic = false;
1943    bool is_synthesized = false;
1944    bool is_defined = false;
1945    ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
1946
1947    const unsigned num_args = method_function_prototype->getNumArgs();
1948
1949    ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
1950                                                               SourceLocation(), // beginLoc,
1951                                                               SourceLocation(), // endLoc,
1952                                                               method_selector,
1953                                                               method_function_prototype->getResultType(),
1954                                                               NULL, // TypeSourceInfo *ResultTInfo,
1955                                                               GetDeclContextForType (class_opaque_type),
1956                                                               name[0] == '-',
1957                                                               is_variadic,
1958                                                               is_synthesized,
1959                                                               is_defined,
1960                                                               imp_control,
1961                                                               num_args);
1962
1963
1964    if (objc_method_decl == NULL)
1965        return NULL;
1966
1967    if (num_args > 0)
1968    {
1969        llvm::SmallVector<ParmVarDecl *, 12> params;
1970
1971        for (int param_index = 0; param_index < num_args; ++param_index)
1972        {
1973            params.push_back (ParmVarDecl::Create (*ast,
1974                                                   objc_method_decl,
1975                                                   SourceLocation(),
1976                                                   SourceLocation(),
1977                                                   NULL, // anonymous
1978                                                   method_function_prototype->getArgType(param_index),
1979                                                   NULL,
1980                                                   SC_Auto,
1981                                                   SC_Auto,
1982                                                   NULL));
1983        }
1984
1985        objc_method_decl->setMethodParams(*ast, params.data(), params.size(), num_args);
1986    }
1987
1988    class_interface_decl->addDecl (objc_method_decl);
1989
1990
1991    return objc_method_decl;
1992}
1993
1994
1995uint32_t
1996ClangASTContext::GetTypeInfo
1997(
1998    clang_type_t clang_type,
1999    clang::ASTContext *ast,
2000    clang_type_t *pointee_or_element_clang_type
2001)
2002{
2003    if (clang_type == NULL)
2004        return 0;
2005
2006    if (pointee_or_element_clang_type)
2007        *pointee_or_element_clang_type = NULL;
2008
2009    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2010
2011    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2012    switch (type_class)
2013    {
2014    case clang::Type::Builtin:
2015        switch (cast<clang::BuiltinType>(qual_type)->getKind())
2016        {
2017        case clang::BuiltinType::ObjCId:
2018        case clang::BuiltinType::ObjCClass:
2019            if (ast && pointee_or_element_clang_type)
2020                *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
2021            return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
2022
2023        default:
2024            break;
2025        }
2026        return eTypeIsBuiltIn | eTypeHasValue;
2027
2028    case clang::Type::BlockPointer:
2029        if (pointee_or_element_clang_type)
2030            *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2031        return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2032
2033    case clang::Type::Complex:                          return eTypeIsBuiltIn | eTypeHasValue;
2034
2035    case clang::Type::ConstantArray:
2036    case clang::Type::DependentSizedArray:
2037    case clang::Type::IncompleteArray:
2038    case clang::Type::VariableArray:
2039        if (pointee_or_element_clang_type)
2040            *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2041        return eTypeHasChildren | eTypeIsArray;
2042
2043    case clang::Type::DependentName:                    return 0;
2044    case clang::Type::DependentSizedExtVector:          return eTypeHasChildren | eTypeIsVector;
2045    case clang::Type::DependentTemplateSpecialization:  return eTypeIsTemplate;
2046    case clang::Type::Decltype:                         return 0;
2047
2048    case clang::Type::Enum:
2049        if (pointee_or_element_clang_type)
2050            *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2051        return eTypeIsEnumeration | eTypeHasValue;
2052
2053    case clang::Type::Elaborated:                       return 0;
2054    case clang::Type::ExtVector:                        return eTypeHasChildren | eTypeIsVector;
2055    case clang::Type::FunctionProto:                    return eTypeIsFuncPrototype | eTypeHasValue;
2056    case clang::Type::FunctionNoProto:                  return eTypeIsFuncPrototype | eTypeHasValue;
2057    case clang::Type::InjectedClassName:                return 0;
2058
2059    case clang::Type::LValueReference:
2060    case clang::Type::RValueReference:
2061        if (pointee_or_element_clang_type)
2062            *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2063        return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2064
2065    case clang::Type::MemberPointer:                    return eTypeIsPointer   | eTypeIsMember | eTypeHasValue;
2066
2067    case clang::Type::ObjCObjectPointer:
2068        if (pointee_or_element_clang_type)
2069            *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2070        return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2071
2072    case clang::Type::ObjCObject:                       return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2073    case clang::Type::ObjCInterface:                    return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2074
2075    case clang::Type::Pointer:
2076        if (pointee_or_element_clang_type)
2077            *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2078        return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2079
2080    case clang::Type::Record:
2081        if (qual_type->getAsCXXRecordDecl())
2082            return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2083        else
2084            return eTypeHasChildren | eTypeIsStructUnion;
2085        break;
2086    case clang::Type::SubstTemplateTypeParm:            return eTypeIsTemplate;
2087    case clang::Type::TemplateTypeParm:                 return eTypeIsTemplate;
2088    case clang::Type::TemplateSpecialization:           return eTypeIsTemplate;
2089
2090    case clang::Type::Typedef:
2091        return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2092                                                                  ast,
2093                                                                  pointee_or_element_clang_type);
2094
2095    case clang::Type::TypeOfExpr:                       return 0;
2096    case clang::Type::TypeOf:                           return 0;
2097    case clang::Type::UnresolvedUsing:                  return 0;
2098    case clang::Type::Vector:                           return eTypeHasChildren | eTypeIsVector;
2099    default:                                            return 0;
2100    }
2101    return 0;
2102}
2103
2104
2105#pragma mark Aggregate Types
2106
2107bool
2108ClangASTContext::IsAggregateType (clang_type_t clang_type)
2109{
2110    if (clang_type == NULL)
2111        return false;
2112
2113    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2114
2115    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2116    switch (type_class)
2117    {
2118    case clang::Type::IncompleteArray:
2119    case clang::Type::VariableArray:
2120    case clang::Type::ConstantArray:
2121    case clang::Type::ExtVector:
2122    case clang::Type::Vector:
2123    case clang::Type::Record:
2124    case clang::Type::ObjCObject:
2125    case clang::Type::ObjCInterface:
2126        return true;
2127
2128    case clang::Type::Typedef:
2129        return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2130
2131    default:
2132        break;
2133    }
2134    // The clang type does have a value
2135    return false;
2136}
2137
2138uint32_t
2139ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
2140{
2141    if (clang_type == NULL)
2142        return 0;
2143
2144    uint32_t num_children = 0;
2145    QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2146    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2147    switch (type_class)
2148    {
2149    case clang::Type::Builtin:
2150        switch (cast<clang::BuiltinType>(qual_type)->getKind())
2151        {
2152        case clang::BuiltinType::ObjCId:    // child is Class
2153        case clang::BuiltinType::ObjCClass: // child is Class
2154            num_children = 1;
2155            break;
2156
2157        default:
2158            break;
2159        }
2160        break;
2161
2162    case clang::Type::Complex: return 0;
2163
2164    case clang::Type::Record:
2165        if (GetCompleteQualType (ast, qual_type))
2166        {
2167            const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2168            const RecordDecl *record_decl = record_type->getDecl();
2169            assert(record_decl);
2170            const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2171            if (cxx_record_decl)
2172            {
2173                if (omit_empty_base_classes)
2174                {
2175                    // Check each base classes to see if it or any of its
2176                    // base classes contain any fields. This can help
2177                    // limit the noise in variable views by not having to
2178                    // show base classes that contain no members.
2179                    CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2180                    for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2181                         base_class != base_class_end;
2182                         ++base_class)
2183                    {
2184                        const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2185
2186                        // Skip empty base classes
2187                        if (RecordHasFields(base_class_decl) == false)
2188                            continue;
2189
2190                        num_children++;
2191                    }
2192                }
2193                else
2194                {
2195                    // Include all base classes
2196                    num_children += cxx_record_decl->getNumBases();
2197                }
2198
2199            }
2200            RecordDecl::field_iterator field, field_end;
2201            for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
2202                ++num_children;
2203        }
2204        break;
2205
2206    case clang::Type::ObjCObject:
2207    case clang::Type::ObjCInterface:
2208        if (GetCompleteQualType (ast, qual_type))
2209        {
2210            const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2211            assert (objc_class_type);
2212            if (objc_class_type)
2213            {
2214                ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2215
2216                if (class_interface_decl)
2217                {
2218
2219                    ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2220                    if (superclass_interface_decl)
2221                    {
2222                        if (omit_empty_base_classes)
2223                        {
2224                            if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
2225                                ++num_children;
2226                        }
2227                        else
2228                            ++num_children;
2229                    }
2230
2231                    num_children += class_interface_decl->ivar_size();
2232                }
2233            }
2234        }
2235        break;
2236
2237    case clang::Type::ObjCObjectPointer:
2238        {
2239            const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
2240            QualType pointee_type = pointer_type->getPointeeType();
2241            uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
2242                                                                             pointee_type.getAsOpaquePtr(),
2243                                                                             omit_empty_base_classes);
2244            // If this type points to a simple type, then it has 1 child
2245            if (num_pointee_children == 0)
2246                num_children = 1;
2247            else
2248                num_children = num_pointee_children;
2249        }
2250        break;
2251
2252    case clang::Type::ConstantArray:
2253        num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
2254        break;
2255
2256    case clang::Type::Pointer:
2257        {
2258            const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
2259            QualType pointee_type (pointer_type->getPointeeType());
2260            uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
2261                                                                             pointee_type.getAsOpaquePtr(),
2262                                                                             omit_empty_base_classes);
2263            if (num_pointee_children == 0)
2264            {
2265                // We have a pointer to a pointee type that claims it has no children.
2266                // We will want to look at
2267                num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
2268            }
2269            else
2270                num_children = num_pointee_children;
2271        }
2272        break;
2273
2274    case clang::Type::LValueReference:
2275    case clang::Type::RValueReference:
2276        {
2277            const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
2278            QualType pointee_type = reference_type->getPointeeType();
2279            uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
2280                                                                             pointee_type.getAsOpaquePtr(),
2281                                                                             omit_empty_base_classes);
2282            // If this type points to a simple type, then it has 1 child
2283            if (num_pointee_children == 0)
2284                num_children = 1;
2285            else
2286                num_children = num_pointee_children;
2287        }
2288        break;
2289
2290
2291    case clang::Type::Typedef:
2292        num_children = ClangASTContext::GetNumChildren (ast,
2293                                                        cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2294                                                        omit_empty_base_classes);
2295        break;
2296
2297    default:
2298        break;
2299    }
2300    return num_children;
2301}
2302
2303// If a pointer to a pointee type (the clang_type arg) says that it has no
2304// children, then we either need to trust it, or override it and return a
2305// different result. For example, an "int *" has one child that is an integer,
2306// but a function pointer doesn't have any children. Likewise if a Record type
2307// claims it has no children, then there really is nothing to show.
2308uint32_t
2309ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
2310{
2311    if (clang_type == NULL)
2312        return 0;
2313
2314    QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2315    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2316    switch (type_class)
2317    {
2318    case clang::Type::Builtin:
2319        switch (cast<clang::BuiltinType>(qual_type)->getKind())
2320        {
2321        case clang::BuiltinType::UnknownAny:
2322        case clang::BuiltinType::Void:
2323        case clang::BuiltinType::NullPtr:
2324            return 0;
2325        case clang::BuiltinType::Bool:
2326        case clang::BuiltinType::Char_U:
2327        case clang::BuiltinType::UChar:
2328        case clang::BuiltinType::WChar_U:
2329        case clang::BuiltinType::Char16:
2330        case clang::BuiltinType::Char32:
2331        case clang::BuiltinType::UShort:
2332        case clang::BuiltinType::UInt:
2333        case clang::BuiltinType::ULong:
2334        case clang::BuiltinType::ULongLong:
2335        case clang::BuiltinType::UInt128:
2336        case clang::BuiltinType::Char_S:
2337        case clang::BuiltinType::SChar:
2338        case clang::BuiltinType::WChar_S:
2339        case clang::BuiltinType::Short:
2340        case clang::BuiltinType::Int:
2341        case clang::BuiltinType::Long:
2342        case clang::BuiltinType::LongLong:
2343        case clang::BuiltinType::Int128:
2344        case clang::BuiltinType::Float:
2345        case clang::BuiltinType::Double:
2346        case clang::BuiltinType::LongDouble:
2347        case clang::BuiltinType::Dependent:
2348        case clang::BuiltinType::Overload:
2349        case clang::BuiltinType::ObjCId:
2350        case clang::BuiltinType::ObjCClass:
2351        case clang::BuiltinType::ObjCSel:
2352            return 1;
2353        }
2354        break;
2355
2356    case clang::Type::Complex:                  return 1;
2357    case clang::Type::Pointer:                  return 1;
2358    case clang::Type::BlockPointer:             return 0;   // If block pointers don't have debug info, then no children for them
2359    case clang::Type::LValueReference:          return 1;
2360    case clang::Type::RValueReference:          return 1;
2361    case clang::Type::MemberPointer:            return 0;
2362    case clang::Type::ConstantArray:            return 0;
2363    case clang::Type::IncompleteArray:          return 0;
2364    case clang::Type::VariableArray:            return 0;
2365    case clang::Type::DependentSizedArray:      return 0;
2366    case clang::Type::DependentSizedExtVector:  return 0;
2367    case clang::Type::Vector:                   return 0;
2368    case clang::Type::ExtVector:                return 0;
2369    case clang::Type::FunctionProto:            return 0;   // When we function pointers, they have no children...
2370    case clang::Type::FunctionNoProto:          return 0;   // When we function pointers, they have no children...
2371    case clang::Type::UnresolvedUsing:          return 0;
2372    case clang::Type::Paren:                    return 0;
2373    case clang::Type::Typedef:                  return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2374    case clang::Type::TypeOfExpr:               return 0;
2375    case clang::Type::TypeOf:                   return 0;
2376    case clang::Type::Decltype:                 return 0;
2377    case clang::Type::Record:                   return 0;
2378    case clang::Type::Enum:                     return 1;
2379    case clang::Type::Elaborated:               return 1;
2380    case clang::Type::TemplateTypeParm:         return 1;
2381    case clang::Type::SubstTemplateTypeParm:    return 1;
2382    case clang::Type::TemplateSpecialization:   return 1;
2383    case clang::Type::InjectedClassName:        return 0;
2384    case clang::Type::DependentName:            return 1;
2385    case clang::Type::DependentTemplateSpecialization:  return 1;
2386    case clang::Type::ObjCObject:               return 0;
2387    case clang::Type::ObjCInterface:            return 0;
2388    case clang::Type::ObjCObjectPointer:        return 1;
2389    default:
2390        break;
2391    }
2392    return 0;
2393}
2394
2395clang_type_t
2396ClangASTContext::GetChildClangTypeAtIndex
2397(
2398    const char *parent_name,
2399    clang_type_t parent_clang_type,
2400    uint32_t idx,
2401    bool transparent_pointers,
2402    bool omit_empty_base_classes,
2403    std::string& child_name,
2404    uint32_t &child_byte_size,
2405    int32_t &child_byte_offset,
2406    uint32_t &child_bitfield_bit_size,
2407    uint32_t &child_bitfield_bit_offset,
2408    bool &child_is_base_class,
2409    bool &child_is_deref_of_parent
2410)
2411{
2412    if (parent_clang_type)
2413
2414        return GetChildClangTypeAtIndex (getASTContext(),
2415                                         parent_name,
2416                                         parent_clang_type,
2417                                         idx,
2418                                         transparent_pointers,
2419                                         omit_empty_base_classes,
2420                                         child_name,
2421                                         child_byte_size,
2422                                         child_byte_offset,
2423                                         child_bitfield_bit_size,
2424                                         child_bitfield_bit_offset,
2425                                         child_is_base_class,
2426                                         child_is_deref_of_parent);
2427    return NULL;
2428}
2429
2430clang_type_t
2431ClangASTContext::GetChildClangTypeAtIndex
2432(
2433    ASTContext *ast,
2434    const char *parent_name,
2435    clang_type_t parent_clang_type,
2436    uint32_t idx,
2437    bool transparent_pointers,
2438    bool omit_empty_base_classes,
2439    std::string& child_name,
2440    uint32_t &child_byte_size,
2441    int32_t &child_byte_offset,
2442    uint32_t &child_bitfield_bit_size,
2443    uint32_t &child_bitfield_bit_offset,
2444    bool &child_is_base_class,
2445    bool &child_is_deref_of_parent
2446)
2447{
2448    if (parent_clang_type == NULL)
2449        return NULL;
2450
2451    if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes))
2452    {
2453        uint32_t bit_offset;
2454        child_bitfield_bit_size = 0;
2455        child_bitfield_bit_offset = 0;
2456        child_is_base_class = false;
2457        QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
2458        const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
2459        switch (parent_type_class)
2460        {
2461        case clang::Type::Builtin:
2462            switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
2463            {
2464            case clang::BuiltinType::ObjCId:
2465            case clang::BuiltinType::ObjCClass:
2466                child_name = "isa";
2467                child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
2468                return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
2469
2470            default:
2471                break;
2472            }
2473            break;
2474
2475        case clang::Type::Record:
2476            if (GetCompleteQualType (ast, parent_qual_type))
2477            {
2478                const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
2479                const RecordDecl *record_decl = record_type->getDecl();
2480                assert(record_decl);
2481                const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
2482                uint32_t child_idx = 0;
2483
2484                const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2485                if (cxx_record_decl)
2486                {
2487                    // We might have base classes to print out first
2488                    CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2489                    for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2490                         base_class != base_class_end;
2491                         ++base_class)
2492                    {
2493                        const CXXRecordDecl *base_class_decl = NULL;
2494
2495                        // Skip empty base classes
2496                        if (omit_empty_base_classes)
2497                        {
2498                            base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2499                            if (RecordHasFields(base_class_decl) == false)
2500                                continue;
2501                        }
2502
2503                        if (idx == child_idx)
2504                        {
2505                            if (base_class_decl == NULL)
2506                                base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2507
2508
2509                            if (base_class->isVirtual())
2510                                bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
2511                            else
2512                                bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
2513
2514                            // Base classes should be a multiple of 8 bits in size
2515                            assert (bit_offset % 8 == 0);
2516                            child_byte_offset = bit_offset/8;
2517                            std::string base_class_type_name(base_class->getType().getAsString());
2518
2519                            child_name.assign(base_class_type_name.c_str());
2520
2521                            uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
2522
2523                            // Base classes bit sizes should be a multiple of 8 bits in size
2524                            assert (clang_type_info_bit_size % 8 == 0);
2525                            child_byte_size = clang_type_info_bit_size / 8;
2526                            child_is_base_class = true;
2527                            return base_class->getType().getAsOpaquePtr();
2528                        }
2529                        // We don't increment the child index in the for loop since we might
2530                        // be skipping empty base classes
2531                        ++child_idx;
2532                    }
2533                }
2534                // Make sure index is in range...
2535                uint32_t field_idx = 0;
2536                RecordDecl::field_iterator field, field_end;
2537                for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
2538                {
2539                    if (idx == child_idx)
2540                    {
2541                        // Print the member type if requested
2542                        // Print the member name and equal sign
2543                        child_name.assign(field->getNameAsString().c_str());
2544
2545                        // Figure out the type byte size (field_type_info.first) and
2546                        // alignment (field_type_info.second) from the AST context.
2547                        std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
2548                        assert(field_idx < record_layout.getFieldCount());
2549
2550                        child_byte_size = field_type_info.first / 8;
2551
2552                        // Figure out the field offset within the current struct/union/class type
2553                        bit_offset = record_layout.getFieldOffset (field_idx);
2554                        child_byte_offset = bit_offset / 8;
2555                        if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
2556                            child_bitfield_bit_offset = bit_offset % 8;
2557
2558                        return field->getType().getAsOpaquePtr();
2559                    }
2560                }
2561            }
2562            break;
2563
2564        case clang::Type::ObjCObject:
2565        case clang::Type::ObjCInterface:
2566            if (GetCompleteQualType (ast, parent_qual_type))
2567            {
2568                const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
2569                assert (objc_class_type);
2570                if (objc_class_type)
2571                {
2572                    uint32_t child_idx = 0;
2573                    ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2574
2575                    if (class_interface_decl)
2576                    {
2577
2578                        const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
2579                        ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2580                        if (superclass_interface_decl)
2581                        {
2582                            if (omit_empty_base_classes)
2583                            {
2584                                if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
2585                                {
2586                                    if (idx == 0)
2587                                    {
2588                                        QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
2589
2590
2591                                        child_name.assign(superclass_interface_decl->getNameAsString().c_str());
2592
2593                                        std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
2594
2595                                        child_byte_size = ivar_type_info.first / 8;
2596                                        child_byte_offset = 0;
2597                                        child_is_base_class = true;
2598
2599                                        return ivar_qual_type.getAsOpaquePtr();
2600                                    }
2601
2602                                    ++child_idx;
2603                                }
2604                            }
2605                            else
2606                                ++child_idx;
2607                        }
2608
2609                        const uint32_t superclass_idx = child_idx;
2610
2611                        if (idx < (child_idx + class_interface_decl->ivar_size()))
2612                        {
2613                            ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
2614
2615                            for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
2616                            {
2617                                if (child_idx == idx)
2618                                {
2619                                    const ObjCIvarDecl* ivar_decl = *ivar_pos;
2620
2621                                    QualType ivar_qual_type(ivar_decl->getType());
2622
2623                                    child_name.assign(ivar_decl->getNameAsString().c_str());
2624
2625                                    std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
2626
2627                                    child_byte_size = ivar_type_info.first / 8;
2628
2629                                    // Figure out the field offset within the current struct/union/class type
2630                                    bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
2631                                    child_byte_offset = bit_offset / 8;
2632
2633                                    return ivar_qual_type.getAsOpaquePtr();
2634                                }
2635                                ++child_idx;
2636                            }
2637                        }
2638                    }
2639                }
2640            }
2641            break;
2642
2643        case clang::Type::ObjCObjectPointer:
2644            {
2645                const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
2646                QualType pointee_type = pointer_type->getPointeeType();
2647
2648                if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2649                {
2650                    child_is_deref_of_parent = false;
2651                    bool tmp_child_is_deref_of_parent = false;
2652                    return GetChildClangTypeAtIndex (ast,
2653                                                     parent_name,
2654                                                     pointer_type->getPointeeType().getAsOpaquePtr(),
2655                                                     idx,
2656                                                     transparent_pointers,
2657                                                     omit_empty_base_classes,
2658                                                     child_name,
2659                                                     child_byte_size,
2660                                                     child_byte_offset,
2661                                                     child_bitfield_bit_size,
2662                                                     child_bitfield_bit_offset,
2663                                                     child_is_base_class,
2664                                                     tmp_child_is_deref_of_parent);
2665                }
2666                else
2667                {
2668                    child_is_deref_of_parent = true;
2669                    if (parent_name)
2670                    {
2671                        child_name.assign(1, '*');
2672                        child_name += parent_name;
2673                    }
2674
2675                    // We have a pointer to an simple type
2676                    if (idx == 0)
2677                    {
2678                        std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
2679                        assert(clang_type_info.first % 8 == 0);
2680                        child_byte_size = clang_type_info.first / 8;
2681                        child_byte_offset = 0;
2682                        return pointee_type.getAsOpaquePtr();
2683                    }
2684                }
2685            }
2686            break;
2687
2688        case clang::Type::ConstantArray:
2689            {
2690                const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
2691                const uint64_t element_count = array->getSize().getLimitedValue();
2692
2693                if (idx < element_count)
2694                {
2695                    if (GetCompleteQualType (ast, array->getElementType()))
2696                    {
2697                        std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
2698
2699                        char element_name[64];
2700                        ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
2701
2702                        child_name.assign(element_name);
2703                        assert(field_type_info.first % 8 == 0);
2704                        child_byte_size = field_type_info.first / 8;
2705                        child_byte_offset = idx * child_byte_size;
2706                        return array->getElementType().getAsOpaquePtr();
2707                    }
2708                }
2709            }
2710            break;
2711
2712        case clang::Type::Pointer:
2713            {
2714                const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
2715                QualType pointee_type = pointer_type->getPointeeType();
2716
2717                // Don't dereference "void *" pointers
2718                if (pointee_type->isVoidType())
2719                    return NULL;
2720
2721                if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2722                {
2723                    child_is_deref_of_parent = false;
2724                    bool tmp_child_is_deref_of_parent = false;
2725                    return GetChildClangTypeAtIndex (ast,
2726                                                     parent_name,
2727                                                     pointer_type->getPointeeType().getAsOpaquePtr(),
2728                                                     idx,
2729                                                     transparent_pointers,
2730                                                     omit_empty_base_classes,
2731                                                     child_name,
2732                                                     child_byte_size,
2733                                                     child_byte_offset,
2734                                                     child_bitfield_bit_size,
2735                                                     child_bitfield_bit_offset,
2736                                                     child_is_base_class,
2737                                                     tmp_child_is_deref_of_parent);
2738                }
2739                else
2740                {
2741                    child_is_deref_of_parent = true;
2742
2743                    if (parent_name)
2744                    {
2745                        child_name.assign(1, '*');
2746                        child_name += parent_name;
2747                    }
2748
2749                    // We have a pointer to an simple type
2750                    if (idx == 0)
2751                    {
2752                        std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
2753                        assert(clang_type_info.first % 8 == 0);
2754                        child_byte_size = clang_type_info.first / 8;
2755                        child_byte_offset = 0;
2756                        return pointee_type.getAsOpaquePtr();
2757                    }
2758                }
2759            }
2760            break;
2761
2762        case clang::Type::LValueReference:
2763        case clang::Type::RValueReference:
2764            {
2765                const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
2766                QualType pointee_type(reference_type->getPointeeType());
2767                clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
2768                if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
2769                {
2770                    child_is_deref_of_parent = false;
2771                    bool tmp_child_is_deref_of_parent = false;
2772                    return GetChildClangTypeAtIndex (ast,
2773                                                     parent_name,
2774                                                     pointee_clang_type,
2775                                                     idx,
2776                                                     transparent_pointers,
2777                                                     omit_empty_base_classes,
2778                                                     child_name,
2779                                                     child_byte_size,
2780                                                     child_byte_offset,
2781                                                     child_bitfield_bit_size,
2782                                                     child_bitfield_bit_offset,
2783                                                     child_is_base_class,
2784                                                     tmp_child_is_deref_of_parent);
2785                }
2786                else
2787                {
2788                    if (parent_name)
2789                    {
2790                        child_name.assign(1, '&');
2791                        child_name += parent_name;
2792                    }
2793
2794                    // We have a pointer to an simple type
2795                    if (idx == 0)
2796                    {
2797                        std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
2798                        assert(clang_type_info.first % 8 == 0);
2799                        child_byte_size = clang_type_info.first / 8;
2800                        child_byte_offset = 0;
2801                        return pointee_type.getAsOpaquePtr();
2802                    }
2803                }
2804            }
2805            break;
2806
2807        case clang::Type::Typedef:
2808            return GetChildClangTypeAtIndex (ast,
2809                                             parent_name,
2810                                             cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2811                                             idx,
2812                                             transparent_pointers,
2813                                             omit_empty_base_classes,
2814                                             child_name,
2815                                             child_byte_size,
2816                                             child_byte_offset,
2817                                             child_bitfield_bit_size,
2818                                             child_bitfield_bit_offset,
2819                                             child_is_base_class,
2820                                             child_is_deref_of_parent);
2821            break;
2822
2823        default:
2824            break;
2825        }
2826    }
2827    return NULL;
2828}
2829
2830static inline bool
2831BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
2832{
2833    return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
2834}
2835
2836static uint32_t
2837GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
2838{
2839    uint32_t num_bases = 0;
2840    if (cxx_record_decl)
2841    {
2842        if (omit_empty_base_classes)
2843        {
2844            CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2845            for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2846                 base_class != base_class_end;
2847                 ++base_class)
2848            {
2849                // Skip empty base classes
2850                if (omit_empty_base_classes)
2851                {
2852                    if (BaseSpecifierIsEmpty (base_class))
2853                        continue;
2854                }
2855                ++num_bases;
2856            }
2857        }
2858        else
2859            num_bases = cxx_record_decl->getNumBases();
2860    }
2861    return num_bases;
2862}
2863
2864
2865static uint32_t
2866GetIndexForRecordBase
2867(
2868    const RecordDecl *record_decl,
2869    const CXXBaseSpecifier *base_spec,
2870    bool omit_empty_base_classes
2871)
2872{
2873    uint32_t child_idx = 0;
2874
2875    const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2876
2877//    const char *super_name = record_decl->getNameAsCString();
2878//    const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
2879//    printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
2880//
2881    if (cxx_record_decl)
2882    {
2883        CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2884        for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2885             base_class != base_class_end;
2886             ++base_class)
2887        {
2888            if (omit_empty_base_classes)
2889            {
2890                if (BaseSpecifierIsEmpty (base_class))
2891                    continue;
2892            }
2893
2894//            printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
2895//                    child_idx,
2896//                    base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
2897//
2898//
2899            if (base_class == base_spec)
2900                return child_idx;
2901            ++child_idx;
2902        }
2903    }
2904
2905    return UINT32_MAX;
2906}
2907
2908
2909static uint32_t
2910GetIndexForRecordChild
2911(
2912    const RecordDecl *record_decl,
2913    NamedDecl *canonical_decl,
2914    bool omit_empty_base_classes
2915)
2916{
2917    uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
2918
2919//    const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2920//
2921////    printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
2922//    if (cxx_record_decl)
2923//    {
2924//        CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2925//        for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2926//             base_class != base_class_end;
2927//             ++base_class)
2928//        {
2929//            if (omit_empty_base_classes)
2930//            {
2931//                if (BaseSpecifierIsEmpty (base_class))
2932//                    continue;
2933//            }
2934//
2935////            printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
2936////                    record_decl->getNameAsCString(),
2937////                    canonical_decl->getNameAsCString(),
2938////                    child_idx,
2939////                    base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
2940//
2941//
2942//            CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2943//            if (curr_base_class_decl == canonical_decl)
2944//            {
2945//                return child_idx;
2946//            }
2947//            ++child_idx;
2948//        }
2949//    }
2950//
2951//    const uint32_t num_bases = child_idx;
2952    RecordDecl::field_iterator field, field_end;
2953    for (field = record_decl->field_begin(), field_end = record_decl->field_end();
2954         field != field_end;
2955         ++field, ++child_idx)
2956    {
2957//            printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
2958//                    record_decl->getNameAsCString(),
2959//                    canonical_decl->getNameAsCString(),
2960//                    child_idx - num_bases,
2961//                    field->getNameAsCString());
2962
2963        if (field->getCanonicalDecl() == canonical_decl)
2964            return child_idx;
2965    }
2966
2967    return UINT32_MAX;
2968}
2969
2970// Look for a child member (doesn't include base classes, but it does include
2971// their members) in the type hierarchy. Returns an index path into "clang_type"
2972// on how to reach the appropriate member.
2973//
2974//    class A
2975//    {
2976//    public:
2977//        int m_a;
2978//        int m_b;
2979//    };
2980//
2981//    class B
2982//    {
2983//    };
2984//
2985//    class C :
2986//        public B,
2987//        public A
2988//    {
2989//    };
2990//
2991// If we have a clang type that describes "class C", and we wanted to looked
2992// "m_b" in it:
2993//
2994// With omit_empty_base_classes == false we would get an integer array back with:
2995// { 1,  1 }
2996// The first index 1 is the child index for "class A" within class C
2997// The second index 1 is the child index for "m_b" within class A
2998//
2999// With omit_empty_base_classes == true we would get an integer array back with:
3000// { 0,  1 }
3001// The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count)
3002// The second index 1 is the child index for "m_b" within class A
3003
3004size_t
3005ClangASTContext::GetIndexOfChildMemberWithName
3006(
3007    ASTContext *ast,
3008    clang_type_t clang_type,
3009    const char *name,
3010    bool omit_empty_base_classes,
3011    std::vector<uint32_t>& child_indexes
3012)
3013{
3014    if (clang_type && name && name[0])
3015    {
3016        QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3017        const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3018        switch (type_class)
3019        {
3020        case clang::Type::Record:
3021            if (GetCompleteQualType (ast, qual_type))
3022            {
3023                const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3024                const RecordDecl *record_decl = record_type->getDecl();
3025
3026                assert(record_decl);
3027                uint32_t child_idx = 0;
3028
3029                const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3030
3031                // Try and find a field that matches NAME
3032                RecordDecl::field_iterator field, field_end;
3033                StringRef name_sref(name);
3034                for (field = record_decl->field_begin(), field_end = record_decl->field_end();
3035                     field != field_end;
3036                     ++field, ++child_idx)
3037                {
3038                    if (field->getName().equals (name_sref))
3039                    {
3040                        // We have to add on the number of base classes to this index!
3041                        child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
3042                        return child_indexes.size();
3043                    }
3044                }
3045
3046                if (cxx_record_decl)
3047                {
3048                    const RecordDecl *parent_record_decl = cxx_record_decl;
3049
3050                    //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
3051
3052                    //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
3053                    // Didn't find things easily, lets let clang do its thang...
3054                    IdentifierInfo & ident_ref = ast->Idents.get(name, name + strlen (name));
3055                    DeclarationName decl_name(&ident_ref);
3056
3057                    CXXBasePaths paths;
3058                    if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
3059                                                       decl_name.getAsOpaquePtr(),
3060                                                       paths))
3061                    {
3062                        CXXBasePaths::const_paths_iterator path, path_end = paths.end();
3063                        for (path = paths.begin(); path != path_end; ++path)
3064                        {
3065                            const size_t num_path_elements = path->size();
3066                            for (size_t e=0; e<num_path_elements; ++e)
3067                            {
3068                                CXXBasePathElement elem = (*path)[e];
3069
3070                                child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
3071                                if (child_idx == UINT32_MAX)
3072                                {
3073                                    child_indexes.clear();
3074                                    return 0;
3075                                }
3076                                else
3077                                {
3078                                    child_indexes.push_back (child_idx);
3079                                    parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
3080                                }
3081                            }
3082                            DeclContext::lookup_iterator named_decl_pos;
3083                            for (named_decl_pos = path->Decls.first;
3084                                 named_decl_pos != path->Decls.second && parent_record_decl;
3085                                 ++named_decl_pos)
3086                            {
3087                                //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString());
3088
3089                                child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
3090                                if (child_idx == UINT32_MAX)
3091                                {
3092                                    child_indexes.clear();
3093                                    return 0;
3094                                }
3095                                else
3096                                {
3097                                    child_indexes.push_back (child_idx);
3098                                }
3099                            }
3100                        }
3101                        return child_indexes.size();
3102                    }
3103                }
3104
3105            }
3106            break;
3107
3108        case clang::Type::ObjCObject:
3109        case clang::Type::ObjCInterface:
3110            if (GetCompleteQualType (ast, qual_type))
3111            {
3112                StringRef name_sref(name);
3113                const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3114                assert (objc_class_type);
3115                if (objc_class_type)
3116                {
3117                    uint32_t child_idx = 0;
3118                    ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3119
3120                    if (class_interface_decl)
3121                    {
3122                        ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3123                        ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3124
3125                        for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
3126                        {
3127                            const ObjCIvarDecl* ivar_decl = *ivar_pos;
3128
3129                            if (ivar_decl->getName().equals (name_sref))
3130                            {
3131                                if ((!omit_empty_base_classes && superclass_interface_decl) ||
3132                                    ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
3133                                    ++child_idx;
3134
3135                                child_indexes.push_back (child_idx);
3136                                return child_indexes.size();
3137                            }
3138                        }
3139
3140                        if (superclass_interface_decl)
3141                        {
3142                            // The super class index is always zero for ObjC classes,
3143                            // so we push it onto the child indexes in case we find
3144                            // an ivar in our superclass...
3145                            child_indexes.push_back (0);
3146
3147                            if (GetIndexOfChildMemberWithName (ast,
3148                                                               ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
3149                                                               name,
3150                                                               omit_empty_base_classes,
3151                                                               child_indexes))
3152                            {
3153                                // We did find an ivar in a superclass so just
3154                                // return the results!
3155                                return child_indexes.size();
3156                            }
3157
3158                            // We didn't find an ivar matching "name" in our
3159                            // superclass, pop the superclass zero index that
3160                            // we pushed on above.
3161                            child_indexes.pop_back();
3162                        }
3163                    }
3164                }
3165            }
3166            break;
3167
3168        case clang::Type::ObjCObjectPointer:
3169            {
3170                return GetIndexOfChildMemberWithName (ast,
3171                                                      cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
3172                                                      name,
3173                                                      omit_empty_base_classes,
3174                                                      child_indexes);
3175            }
3176            break;
3177
3178
3179        case clang::Type::ConstantArray:
3180            {
3181//                const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
3182//                const uint64_t element_count = array->getSize().getLimitedValue();
3183//
3184//                if (idx < element_count)
3185//                {
3186//                    std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
3187//
3188//                    char element_name[32];
3189//                    ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
3190//
3191//                    child_name.assign(element_name);
3192//                    assert(field_type_info.first % 8 == 0);
3193//                    child_byte_size = field_type_info.first / 8;
3194//                    child_byte_offset = idx * child_byte_size;
3195//                    return array->getElementType().getAsOpaquePtr();
3196//                }
3197            }
3198            break;
3199
3200//        case clang::Type::MemberPointerType:
3201//            {
3202//                MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
3203//                QualType pointee_type = mem_ptr_type->getPointeeType();
3204//
3205//                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3206//                {
3207//                    return GetIndexOfChildWithName (ast,
3208//                                                    mem_ptr_type->getPointeeType().getAsOpaquePtr(),
3209//                                                    name);
3210//                }
3211//            }
3212//            break;
3213//
3214        case clang::Type::LValueReference:
3215        case clang::Type::RValueReference:
3216            {
3217                const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
3218                QualType pointee_type = reference_type->getPointeeType();
3219
3220                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3221                {
3222                    return GetIndexOfChildMemberWithName (ast,
3223                                                          reference_type->getPointeeType().getAsOpaquePtr(),
3224                                                          name,
3225                                                          omit_empty_base_classes,
3226                                                          child_indexes);
3227                }
3228            }
3229            break;
3230
3231        case clang::Type::Pointer:
3232            {
3233                const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
3234                QualType pointee_type = pointer_type->getPointeeType();
3235
3236                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3237                {
3238                    return GetIndexOfChildMemberWithName (ast,
3239                                                          pointer_type->getPointeeType().getAsOpaquePtr(),
3240                                                          name,
3241                                                          omit_empty_base_classes,
3242                                                          child_indexes);
3243                }
3244                else
3245                {
3246//                    if (parent_name)
3247//                    {
3248//                        child_name.assign(1, '*');
3249//                        child_name += parent_name;
3250//                    }
3251//
3252//                    // We have a pointer to an simple type
3253//                    if (idx == 0)
3254//                    {
3255//                        std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
3256//                        assert(clang_type_info.first % 8 == 0);
3257//                        child_byte_size = clang_type_info.first / 8;
3258//                        child_byte_offset = 0;
3259//                        return pointee_type.getAsOpaquePtr();
3260//                    }
3261                }
3262            }
3263            break;
3264
3265        case clang::Type::Typedef:
3266            return GetIndexOfChildMemberWithName (ast,
3267                                                  cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3268                                                  name,
3269                                                  omit_empty_base_classes,
3270                                                  child_indexes);
3271
3272        default:
3273            break;
3274        }
3275    }
3276    return 0;
3277}
3278
3279
3280// Get the index of the child of "clang_type" whose name matches. This function
3281// doesn't descend into the children, but only looks one level deep and name
3282// matches can include base class names.
3283
3284uint32_t
3285ClangASTContext::GetIndexOfChildWithName
3286(
3287    ASTContext *ast,
3288    clang_type_t clang_type,
3289    const char *name,
3290    bool omit_empty_base_classes
3291)
3292{
3293    if (clang_type && name && name[0])
3294    {
3295        QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3296
3297        const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3298
3299        switch (type_class)
3300        {
3301        case clang::Type::Record:
3302            if (GetCompleteQualType (ast, qual_type))
3303            {
3304                const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3305                const RecordDecl *record_decl = record_type->getDecl();
3306
3307                assert(record_decl);
3308                uint32_t child_idx = 0;
3309
3310                const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3311
3312                if (cxx_record_decl)
3313                {
3314                    CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3315                    for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3316                         base_class != base_class_end;
3317                         ++base_class)
3318                    {
3319                        // Skip empty base classes
3320                        CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3321                        if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
3322                            continue;
3323
3324                        if (base_class->getType().getAsString().compare (name) == 0)
3325                            return child_idx;
3326                        ++child_idx;
3327                    }
3328                }
3329
3330                // Try and find a field that matches NAME
3331                RecordDecl::field_iterator field, field_end;
3332                StringRef name_sref(name);
3333                for (field = record_decl->field_begin(), field_end = record_decl->field_end();
3334                     field != field_end;
3335                     ++field, ++child_idx)
3336                {
3337                    if (field->getName().equals (name_sref))
3338                        return child_idx;
3339                }
3340
3341            }
3342            break;
3343
3344        case clang::Type::ObjCObject:
3345        case clang::Type::ObjCInterface:
3346            if (GetCompleteQualType (ast, qual_type))
3347            {
3348                StringRef name_sref(name);
3349                const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3350                assert (objc_class_type);
3351                if (objc_class_type)
3352                {
3353                    uint32_t child_idx = 0;
3354                    ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3355
3356                    if (class_interface_decl)
3357                    {
3358                        ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3359                        ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3360
3361                        for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
3362                        {
3363                            const ObjCIvarDecl* ivar_decl = *ivar_pos;
3364
3365                            if (ivar_decl->getName().equals (name_sref))
3366                            {
3367                                if ((!omit_empty_base_classes && superclass_interface_decl) ||
3368                                    ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
3369                                    ++child_idx;
3370
3371                                return child_idx;
3372                            }
3373                        }
3374
3375                        if (superclass_interface_decl)
3376                        {
3377                            if (superclass_interface_decl->getName().equals (name_sref))
3378                                return 0;
3379                        }
3380                    }
3381                }
3382            }
3383            break;
3384
3385        case clang::Type::ObjCObjectPointer:
3386            {
3387                return GetIndexOfChildWithName (ast,
3388                                                cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
3389                                                name,
3390                                                omit_empty_base_classes);
3391            }
3392            break;
3393
3394        case clang::Type::ConstantArray:
3395            {
3396//                const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
3397//                const uint64_t element_count = array->getSize().getLimitedValue();
3398//
3399//                if (idx < element_count)
3400//                {
3401//                    std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
3402//
3403//                    char element_name[32];
3404//                    ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
3405//
3406//                    child_name.assign(element_name);
3407//                    assert(field_type_info.first % 8 == 0);
3408//                    child_byte_size = field_type_info.first / 8;
3409//                    child_byte_offset = idx * child_byte_size;
3410//                    return array->getElementType().getAsOpaquePtr();
3411//                }
3412            }
3413            break;
3414
3415//        case clang::Type::MemberPointerType:
3416//            {
3417//                MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
3418//                QualType pointee_type = mem_ptr_type->getPointeeType();
3419//
3420//                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3421//                {
3422//                    return GetIndexOfChildWithName (ast,
3423//                                                    mem_ptr_type->getPointeeType().getAsOpaquePtr(),
3424//                                                    name);
3425//                }
3426//            }
3427//            break;
3428//
3429        case clang::Type::LValueReference:
3430        case clang::Type::RValueReference:
3431            {
3432                const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
3433                QualType pointee_type = reference_type->getPointeeType();
3434
3435                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3436                {
3437                    return GetIndexOfChildWithName (ast,
3438                                                    reference_type->getPointeeType().getAsOpaquePtr(),
3439                                                    name,
3440                                                    omit_empty_base_classes);
3441                }
3442            }
3443            break;
3444
3445        case clang::Type::Pointer:
3446            {
3447                const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
3448                QualType pointee_type = pointer_type->getPointeeType();
3449
3450                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3451                {
3452                    return GetIndexOfChildWithName (ast,
3453                                                    pointer_type->getPointeeType().getAsOpaquePtr(),
3454                                                    name,
3455                                                    omit_empty_base_classes);
3456                }
3457                else
3458                {
3459//                    if (parent_name)
3460//                    {
3461//                        child_name.assign(1, '*');
3462//                        child_name += parent_name;
3463//                    }
3464//
3465//                    // We have a pointer to an simple type
3466//                    if (idx == 0)
3467//                    {
3468//                        std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
3469//                        assert(clang_type_info.first % 8 == 0);
3470//                        child_byte_size = clang_type_info.first / 8;
3471//                        child_byte_offset = 0;
3472//                        return pointee_type.getAsOpaquePtr();
3473//                    }
3474                }
3475            }
3476            break;
3477
3478        case clang::Type::Typedef:
3479            return GetIndexOfChildWithName (ast,
3480                                            cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3481                                            name,
3482                                            omit_empty_base_classes);
3483
3484        default:
3485            break;
3486        }
3487    }
3488    return UINT32_MAX;
3489}
3490
3491#pragma mark TagType
3492
3493bool
3494ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
3495{
3496    if (tag_clang_type)
3497    {
3498        QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
3499        const clang::Type *clang_type = tag_qual_type.getTypePtr();
3500        if (clang_type)
3501        {
3502            const TagType *tag_type = dyn_cast<TagType>(clang_type);
3503            if (tag_type)
3504            {
3505                TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
3506                if (tag_decl)
3507                {
3508                    tag_decl->setTagKind ((TagDecl::TagKind)kind);
3509                    return true;
3510                }
3511            }
3512        }
3513    }
3514    return false;
3515}
3516
3517
3518#pragma mark DeclContext Functions
3519
3520DeclContext *
3521ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
3522{
3523    if (clang_type == NULL)
3524        return NULL;
3525
3526    QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3527    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3528    switch (type_class)
3529    {
3530    case clang::Type::FunctionNoProto:          break;
3531    case clang::Type::FunctionProto:            break;
3532    case clang::Type::IncompleteArray:          break;
3533    case clang::Type::VariableArray:            break;
3534    case clang::Type::ConstantArray:            break;
3535    case clang::Type::DependentSizedArray:      break;
3536    case clang::Type::ExtVector:                break;
3537    case clang::Type::DependentSizedExtVector:  break;
3538    case clang::Type::Vector:                   break;
3539    case clang::Type::Builtin:                  break;
3540    case clang::Type::BlockPointer:             break;
3541    case clang::Type::Pointer:                  break;
3542    case clang::Type::LValueReference:          break;
3543    case clang::Type::RValueReference:          break;
3544    case clang::Type::MemberPointer:            break;
3545    case clang::Type::Complex:                  break;
3546    case clang::Type::ObjCObject:               break;
3547    case clang::Type::ObjCInterface:            return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
3548    case clang::Type::ObjCObjectPointer:        return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
3549    case clang::Type::Record:                   return cast<RecordType>(qual_type)->getDecl();
3550    case clang::Type::Enum:                     return cast<EnumType>(qual_type)->getDecl();
3551    case clang::Type::Typedef:                  return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3552
3553    case clang::Type::TypeOfExpr:               break;
3554    case clang::Type::TypeOf:                   break;
3555    case clang::Type::Decltype:                 break;
3556    //case clang::Type::QualifiedName:          break;
3557    case clang::Type::TemplateSpecialization:   break;
3558    case clang::Type::DependentTemplateSpecialization:  break;
3559    case clang::Type::TemplateTypeParm:         break;
3560    case clang::Type::SubstTemplateTypeParm:    break;
3561    case clang::Type::SubstTemplateTypeParmPack:break;
3562    case clang::Type::PackExpansion:            break;
3563    case clang::Type::UnresolvedUsing:          break;
3564    case clang::Type::Paren:                    break;
3565    case clang::Type::Elaborated:               break;
3566    case clang::Type::Attributed:               break;
3567    case clang::Type::Auto:                     break;
3568    case clang::Type::InjectedClassName:        break;
3569    case clang::Type::DependentName:            break;
3570    }
3571    // No DeclContext in this type...
3572    return NULL;
3573}
3574
3575#pragma mark Namespace Declarations
3576
3577NamespaceDecl *
3578ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx)
3579{
3580    // TODO: Do something intelligent with the Declaration object passed in
3581    // like maybe filling in the SourceLocation with it...
3582    if (name)
3583    {
3584        ASTContext *ast = getASTContext();
3585        if (decl_ctx == NULL)
3586            decl_ctx = ast->getTranslationUnitDecl();
3587        return NamespaceDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), &ast->Idents.get(name));
3588    }
3589    return NULL;
3590}
3591
3592
3593#pragma mark Function Types
3594
3595FunctionDecl *
3596ClangASTContext::CreateFunctionDeclaration (const char *name, clang_type_t function_clang_type, int storage, bool is_inline)
3597{
3598    if (name)
3599    {
3600        ASTContext *ast = getASTContext();
3601        assert (ast != NULL);
3602
3603        if (name && name[0])
3604        {
3605            return FunctionDecl::Create(*ast,
3606                                        ast->getTranslationUnitDecl(),
3607                                        SourceLocation(),
3608                                        SourceLocation(),
3609                                        DeclarationName (&ast->Idents.get(name)),
3610                                        QualType::getFromOpaquePtr(function_clang_type),
3611                                        NULL,
3612                                        (FunctionDecl::StorageClass)storage,
3613                                        (FunctionDecl::StorageClass)storage,
3614                                        is_inline);
3615        }
3616        else
3617        {
3618            return FunctionDecl::Create(*ast,
3619                                        ast->getTranslationUnitDecl(),
3620                                        SourceLocation(),
3621                                        SourceLocation(),
3622                                        DeclarationName (),
3623                                        QualType::getFromOpaquePtr(function_clang_type),
3624                                        NULL,
3625                                        (FunctionDecl::StorageClass)storage,
3626                                        (FunctionDecl::StorageClass)storage,
3627                                        is_inline);
3628        }
3629    }
3630    return NULL;
3631}
3632
3633clang_type_t
3634ClangASTContext::CreateFunctionType (ASTContext *ast,
3635                                     clang_type_t result_type,
3636                                     clang_type_t *args,
3637                                     unsigned num_args,
3638                                     bool is_variadic,
3639                                     unsigned type_quals)
3640{
3641    assert (ast != NULL);
3642    std::vector<QualType> qual_type_args;
3643    for (unsigned i=0; i<num_args; ++i)
3644        qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
3645
3646    // TODO: Detect calling convention in DWARF?
3647    FunctionProtoType::ExtProtoInfo proto_info;
3648    proto_info.Variadic = is_variadic;
3649    proto_info.ExceptionSpecType = EST_None;
3650    proto_info.TypeQuals = type_quals;
3651    proto_info.RefQualifier = RQ_None;
3652    proto_info.NumExceptions = 0;
3653    proto_info.Exceptions = NULL;
3654
3655    return ast->getFunctionType(QualType::getFromOpaquePtr(result_type),
3656                                        qual_type_args.empty() ? NULL : &qual_type_args.front(),
3657                                        qual_type_args.size(),
3658                                        proto_info).getAsOpaquePtr();    // NoReturn);
3659}
3660
3661ParmVarDecl *
3662ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
3663{
3664    ASTContext *ast = getASTContext();
3665    assert (ast != NULL);
3666    return ParmVarDecl::Create(*ast,
3667                                ast->getTranslationUnitDecl(),
3668                                SourceLocation(),
3669                                SourceLocation(),
3670                                name && name[0] ? &ast->Idents.get(name) : NULL,
3671                                QualType::getFromOpaquePtr(param_type),
3672                                NULL,
3673                                (VarDecl::StorageClass)storage,
3674                                (VarDecl::StorageClass)storage,
3675                                0);
3676}
3677
3678void
3679ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
3680{
3681    if (function_decl)
3682        function_decl->setParams (params, num_params);
3683}
3684
3685
3686#pragma mark Array Types
3687
3688clang_type_t
3689ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride)
3690{
3691    if (element_type)
3692    {
3693        ASTContext *ast = getASTContext();
3694        assert (ast != NULL);
3695        llvm::APInt ap_element_count (64, element_count);
3696        return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
3697                                                 ap_element_count,
3698                                                 ArrayType::Normal,
3699                                                 0).getAsOpaquePtr(); // ElemQuals
3700    }
3701    return NULL;
3702}
3703
3704
3705#pragma mark TagDecl
3706
3707bool
3708ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
3709{
3710    if (clang_type)
3711    {
3712        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3713        const clang::Type *t = qual_type.getTypePtr();
3714        if (t)
3715        {
3716            const TagType *tag_type = dyn_cast<TagType>(t);
3717            if (tag_type)
3718            {
3719                TagDecl *tag_decl = tag_type->getDecl();
3720                if (tag_decl)
3721                {
3722                    tag_decl->startDefinition();
3723                    return true;
3724                }
3725            }
3726        }
3727    }
3728    return false;
3729}
3730
3731bool
3732ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
3733{
3734    if (clang_type)
3735    {
3736        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3737
3738        CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3739
3740        if (cxx_record_decl)
3741        {
3742            cxx_record_decl->completeDefinition();
3743
3744            return true;
3745        }
3746
3747        const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type);
3748
3749        if (objc_class_type)
3750        {
3751            ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3752
3753            class_interface_decl->setForwardDecl(false);
3754        }
3755
3756        const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
3757
3758        if (enum_type)
3759        {
3760            EnumDecl *enum_decl = enum_type->getDecl();
3761
3762            if (enum_decl)
3763            {
3764                /// TODO This really needs to be fixed.
3765
3766                unsigned NumPositiveBits = 1;
3767                unsigned NumNegativeBits = 0;
3768
3769                ASTContext *ast = getASTContext();
3770
3771                QualType promotion_qual_type;
3772                // If the enum integer type is less than an integer in bit width,
3773                // then we must promote it to an integer size.
3774                if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
3775                {
3776                    if (enum_decl->getIntegerType()->isSignedIntegerType())
3777                        promotion_qual_type = ast->IntTy;
3778                    else
3779                        promotion_qual_type = ast->UnsignedIntTy;
3780                }
3781                else
3782                    promotion_qual_type = enum_decl->getIntegerType();
3783
3784                enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
3785                return true;
3786            }
3787        }
3788    }
3789    return false;
3790}
3791
3792
3793#pragma mark Enumeration Types
3794
3795clang_type_t
3796ClangASTContext::CreateEnumerationType
3797(
3798    const char *name,
3799    DeclContext *decl_ctx,
3800    const Declaration &decl,
3801    clang_type_t integer_qual_type
3802)
3803{
3804    // TODO: Do something intelligent with the Declaration object passed in
3805    // like maybe filling in the SourceLocation with it...
3806    ASTContext *ast = getASTContext();
3807    assert (ast != NULL);
3808
3809    // TODO: ask about these...
3810//    const bool IsScoped = false;
3811//    const bool IsFixed = false;
3812
3813    EnumDecl *enum_decl = EnumDecl::Create (*ast,
3814                                            decl_ctx,
3815                                            SourceLocation(),
3816                                            SourceLocation(),
3817                                            name && name[0] ? &ast->Idents.get(name) : NULL,
3818                                            NULL,
3819                                            false,  // IsScoped
3820                                            false,  // IsScopedUsingClassTag
3821                                            false); // IsFixed
3822
3823
3824    if (enum_decl)
3825    {
3826        // TODO: check if we should be setting the promotion type too?
3827        enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
3828
3829        enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
3830
3831        return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
3832    }
3833    return NULL;
3834}
3835
3836clang_type_t
3837ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
3838{
3839    QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
3840
3841    const clang::Type *clang_type = enum_qual_type.getTypePtr();
3842    if (clang_type)
3843    {
3844        const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
3845        if (enum_type)
3846        {
3847            EnumDecl *enum_decl = enum_type->getDecl();
3848            if (enum_decl)
3849                return enum_decl->getIntegerType().getAsOpaquePtr();
3850        }
3851    }
3852    return NULL;
3853}
3854bool
3855ClangASTContext::AddEnumerationValueToEnumerationType
3856(
3857    clang_type_t enum_clang_type,
3858    clang_type_t enumerator_clang_type,
3859    const Declaration &decl,
3860    const char *name,
3861    int64_t enum_value,
3862    uint32_t enum_value_bit_size
3863)
3864{
3865    if (enum_clang_type && enumerator_clang_type && name)
3866    {
3867        // TODO: Do something intelligent with the Declaration object passed in
3868        // like maybe filling in the SourceLocation with it...
3869        ASTContext *ast = getASTContext();
3870        IdentifierTable *identifier_table = getIdentifierTable();
3871
3872        assert (ast != NULL);
3873        assert (identifier_table != NULL);
3874        QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
3875
3876        const clang::Type *clang_type = enum_qual_type.getTypePtr();
3877        if (clang_type)
3878        {
3879            const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
3880
3881            if (enum_type)
3882            {
3883                llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
3884                enum_llvm_apsint = enum_value;
3885                EnumConstantDecl *enumerator_decl =
3886                    EnumConstantDecl::Create (*ast,
3887                                              enum_type->getDecl(),
3888                                              SourceLocation(),
3889                                              name ? &identifier_table->get(name) : NULL,    // Identifier
3890                                              QualType::getFromOpaquePtr(enumerator_clang_type),
3891                                              NULL,
3892                                              enum_llvm_apsint);
3893
3894                if (enumerator_decl)
3895                {
3896                    enum_type->getDecl()->addDecl(enumerator_decl);
3897                    return true;
3898                }
3899            }
3900        }
3901    }
3902    return false;
3903}
3904
3905#pragma mark Pointers & References
3906
3907clang_type_t
3908ClangASTContext::CreatePointerType (clang_type_t clang_type)
3909{
3910    return CreatePointerType (getASTContext(), clang_type);
3911}
3912
3913clang_type_t
3914ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
3915{
3916    if (ast && clang_type)
3917    {
3918        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3919
3920        const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3921        switch (type_class)
3922        {
3923        case clang::Type::ObjCObject:
3924        case clang::Type::ObjCInterface:
3925            return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
3926
3927        default:
3928            return ast->getPointerType(qual_type).getAsOpaquePtr();
3929        }
3930    }
3931    return NULL;
3932}
3933
3934clang_type_t
3935ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
3936                                            clang_type_t clang_type)
3937{
3938    if (clang_type)
3939        return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
3940    return NULL;
3941}
3942
3943clang_type_t
3944ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
3945                                            clang_type_t clang_type)
3946{
3947    if (clang_type)
3948        return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
3949    return NULL;
3950}
3951
3952clang_type_t
3953ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
3954{
3955    if (clang_pointee_type && clang_pointee_type)
3956        return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
3957                                                     QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
3958    return NULL;
3959}
3960
3961uint32_t
3962ClangASTContext::GetPointerBitSize ()
3963{
3964    ASTContext *ast = getASTContext();
3965    return ast->getTypeSize(ast->VoidPtrTy);
3966}
3967
3968bool
3969ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
3970{
3971    if (clang_type == NULL)
3972        return false;
3973
3974    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3975    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3976    switch (type_class)
3977    {
3978    case clang::Type::Builtin:
3979        switch (cast<clang::BuiltinType>(qual_type)->getKind())
3980        {
3981        default:
3982            break;
3983        case clang::BuiltinType::ObjCId:
3984        case clang::BuiltinType::ObjCClass:
3985            return true;
3986        }
3987        return false;
3988    case clang::Type::ObjCObjectPointer:
3989        if (target_type)
3990            *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3991        return true;
3992    case clang::Type::BlockPointer:
3993        if (target_type)
3994            *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3995        return true;
3996    case clang::Type::Pointer:
3997        if (target_type)
3998            *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3999        return true;
4000    case clang::Type::MemberPointer:
4001        if (target_type)
4002            *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
4003        return true;
4004    case clang::Type::LValueReference:
4005        if (target_type)
4006            *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
4007        return true;
4008    case clang::Type::RValueReference:
4009        if (target_type)
4010            *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
4011        return true;
4012    case clang::Type::Typedef:
4013        return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
4014    default:
4015        break;
4016    }
4017    return false;
4018}
4019
4020bool
4021ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
4022{
4023    if (!clang_type)
4024        return false;
4025
4026    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4027    const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
4028
4029    if (builtin_type)
4030    {
4031        if (builtin_type->isInteger())
4032            is_signed = builtin_type->isSignedInteger();
4033
4034        return true;
4035    }
4036
4037    return false;
4038}
4039
4040bool
4041ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t*target_type)
4042{
4043    if (clang_type)
4044    {
4045        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4046        const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4047        switch (type_class)
4048        {
4049        case clang::Type::Builtin:
4050            switch (cast<clang::BuiltinType>(qual_type)->getKind())
4051            {
4052            default:
4053                break;
4054            case clang::BuiltinType::ObjCId:
4055            case clang::BuiltinType::ObjCClass:
4056                return true;
4057            }
4058            return false;
4059        case clang::Type::ObjCObjectPointer:
4060            if (target_type)
4061                *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
4062            return true;
4063        case clang::Type::BlockPointer:
4064            if (target_type)
4065                *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
4066            return true;
4067        case clang::Type::Pointer:
4068            if (target_type)
4069                *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
4070            return true;
4071        case clang::Type::MemberPointer:
4072            if (target_type)
4073                *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
4074            return true;
4075        case clang::Type::Typedef:
4076            return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
4077        default:
4078            break;
4079        }
4080    }
4081    return false;
4082}
4083
4084bool
4085ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
4086{
4087    if (clang_type)
4088    {
4089        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4090
4091        if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
4092        {
4093            clang::BuiltinType::Kind kind = BT->getKind();
4094            if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
4095            {
4096                count = 1;
4097                is_complex = false;
4098                return true;
4099            }
4100        }
4101        else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
4102        {
4103            if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
4104            {
4105                count = 2;
4106                is_complex = true;
4107                return true;
4108            }
4109        }
4110        else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
4111        {
4112            if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
4113            {
4114                count = VT->getNumElements();
4115                is_complex = false;
4116                return true;
4117            }
4118        }
4119    }
4120    return false;
4121}
4122
4123
4124bool
4125ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
4126{
4127    if (clang_type)
4128    {
4129        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4130
4131        CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
4132        if (cxx_record_decl)
4133        {
4134            class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
4135            return true;
4136        }
4137    }
4138    class_name.clear();
4139    return false;
4140}
4141
4142
4143bool
4144ClangASTContext::IsCXXClassType (clang_type_t clang_type)
4145{
4146    if (clang_type)
4147    {
4148        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4149        if (qual_type->getAsCXXRecordDecl() != NULL)
4150            return true;
4151    }
4152    return false;
4153}
4154
4155bool
4156ClangASTContext::IsObjCClassType (clang_type_t clang_type)
4157{
4158    if (clang_type)
4159    {
4160        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4161        if (qual_type->isObjCObjectOrInterfaceType())
4162            return true;
4163    }
4164    return false;
4165}
4166
4167
4168bool
4169ClangASTContext::IsCharType (clang_type_t clang_type)
4170{
4171    if (clang_type)
4172        return QualType::getFromOpaquePtr(clang_type)->isCharType();
4173    return false;
4174}
4175
4176bool
4177ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
4178{
4179    clang_type_t pointee_or_element_clang_type = NULL;
4180    Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
4181
4182    if (pointee_or_element_clang_type == NULL)
4183        return false;
4184
4185    if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
4186    {
4187        QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
4188
4189        if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
4190        {
4191            QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4192            if (type_flags.Test (eTypeIsArray))
4193            {
4194                // We know the size of the array and it could be a C string
4195                // since it is an array of characters
4196                length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4197                return true;
4198            }
4199            else
4200            {
4201                length = 0;
4202                return true;
4203            }
4204
4205        }
4206    }
4207    return false;
4208}
4209
4210bool
4211ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
4212{
4213    if (clang_type)
4214    {
4215        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4216
4217        if (qual_type->isFunctionPointerType())
4218            return true;
4219
4220        const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4221        switch (type_class)
4222        {
4223        default:
4224            break;
4225        case clang::Type::Typedef:
4226            return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
4227
4228        case clang::Type::LValueReference:
4229        case clang::Type::RValueReference:
4230            {
4231                const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
4232                if (reference_type)
4233                    return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
4234            }
4235            break;
4236        }
4237    }
4238    return false;
4239}
4240
4241size_t
4242ClangASTContext::GetArraySize (clang_type_t clang_type)
4243{
4244    if (clang_type)
4245    {
4246        const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
4247        if (array)
4248            return array->getSize().getLimitedValue();
4249    }
4250    return 0;
4251}
4252
4253bool
4254ClangASTContext::IsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size)
4255{
4256    if (!clang_type)
4257        return false;
4258
4259    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4260
4261    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4262    switch (type_class)
4263    {
4264    default:
4265        break;
4266    case clang::Type::ConstantArray:
4267        if (member_type)
4268            *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
4269        if (size)
4270            *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
4271        return true;
4272    case clang::Type::IncompleteArray:
4273        if (member_type)
4274            *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
4275        if (size)
4276            *size = 0;
4277        return true;
4278    case clang::Type::VariableArray:
4279        if (member_type)
4280            *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
4281        if (size)
4282            *size = 0;
4283        return true;
4284    case clang::Type::DependentSizedArray:
4285        if (member_type)
4286            *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
4287        if (size)
4288            *size = 0;
4289        return true;
4290    }
4291    return false;
4292}
4293
4294
4295#pragma mark Typedefs
4296
4297clang_type_t
4298ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
4299{
4300    if (clang_type)
4301    {
4302        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4303        ASTContext *ast = getASTContext();
4304        IdentifierTable *identifier_table = getIdentifierTable();
4305        assert (ast != NULL);
4306        assert (identifier_table != NULL);
4307        if (decl_ctx == NULL)
4308            decl_ctx = ast->getTranslationUnitDecl();
4309        TypedefDecl *decl = TypedefDecl::Create (*ast,
4310                                                 decl_ctx,
4311                                                 SourceLocation(),
4312                                                 SourceLocation(),
4313                                                 name ? &identifier_table->get(name) : NULL, // Identifier
4314                                                 ast->CreateTypeSourceInfo(qual_type));
4315
4316        decl->setAccess(AS_public); // TODO respect proper access specifier
4317
4318        // Get a uniqued QualType for the typedef decl type
4319        return ast->getTypedefType (decl).getAsOpaquePtr();
4320    }
4321    return NULL;
4322}
4323
4324
4325std::string
4326ClangASTContext::GetTypeName (clang_type_t opaque_qual_type)
4327{
4328    std::string return_name;
4329
4330    QualType qual_type(QualType::getFromOpaquePtr(opaque_qual_type));
4331
4332    const TypedefType *typedef_type = qual_type->getAs<TypedefType>();
4333    if (typedef_type)
4334    {
4335        const TypedefDecl *typedef_decl = typedef_type->getDecl();
4336        return_name = typedef_decl->getQualifiedNameAsString();
4337    }
4338    else
4339    {
4340        return_name = qual_type.getAsString();
4341    }
4342
4343    return return_name;
4344}
4345
4346// Disable this for now since I can't seem to get a nicely formatted float
4347// out of the APFloat class without just getting the float, double or quad
4348// and then using a formatted print on it which defeats the purpose. We ideally
4349// would like to get perfect string values for any kind of float semantics
4350// so we can support remote targets. The code below also requires a patch to
4351// llvm::APInt.
4352//bool
4353//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)
4354//{
4355//  uint32_t count = 0;
4356//  bool is_complex = false;
4357//  if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
4358//  {
4359//      unsigned num_bytes_per_float = byte_size / count;
4360//      unsigned num_bits_per_float = num_bytes_per_float * 8;
4361//
4362//      float_str.clear();
4363//      uint32_t i;
4364//      for (i=0; i<count; i++)
4365//      {
4366//          APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
4367//          bool is_ieee = false;
4368//          APFloat ap_float(ap_int, is_ieee);
4369//          char s[1024];
4370//          unsigned int hex_digits = 0;
4371//          bool upper_case = false;
4372//
4373//          if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
4374//          {
4375//              if (i > 0)
4376//                  float_str.append(", ");
4377//              float_str.append(s);
4378//              if (i == 1 && is_complex)
4379//                  float_str.append(1, 'i');
4380//          }
4381//      }
4382//      return !float_str.empty();
4383//  }
4384//  return false;
4385//}
4386
4387size_t
4388ClangASTContext::ConvertStringToFloatValue (ASTContext *ast, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size)
4389{
4390    if (clang_type)
4391    {
4392        QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4393        uint32_t count = 0;
4394        bool is_complex = false;
4395        if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
4396        {
4397            // TODO: handle complex and vector types
4398            if (count != 1)
4399                return false;
4400
4401            StringRef s_sref(s);
4402            APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
4403
4404            const uint64_t bit_size = ast->getTypeSize (qual_type);
4405            const uint64_t byte_size = bit_size / 8;
4406            if (dst_size >= byte_size)
4407            {
4408                if (bit_size == sizeof(float)*8)
4409                {
4410                    float float32 = ap_float.convertToFloat();
4411                    ::memcpy (dst, &float32, byte_size);
4412                    return byte_size;
4413                }
4414                else if (bit_size >= 64)
4415                {
4416                    llvm::APInt ap_int(ap_float.bitcastToAPInt());
4417                    ::memcpy (dst, ap_int.getRawData(), byte_size);
4418                    return byte_size;
4419                }
4420            }
4421        }
4422    }
4423    return 0;
4424}
4425
4426unsigned
4427ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
4428{
4429    assert (clang_type);
4430
4431    QualType qual_type (QualType::getFromOpaquePtr(clang_type));
4432
4433    return qual_type.getQualifiers().getCVRQualifiers();
4434}
4435
4436bool
4437ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
4438{
4439    if (clang_type == NULL)
4440        return false;
4441
4442    return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
4443}
4444
4445
4446bool
4447ClangASTContext::GetCompleteType (clang_type_t clang_type)
4448{
4449    return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
4450}
4451
4452