Home | History | Annotate | Download | only in Serialization
      1 //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines common functions that both ASTReader and ASTWriter use.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "ASTCommon.h"
     15 #include "clang/AST/DeclObjC.h"
     16 #include "clang/Basic/IdentifierTable.h"
     17 #include "clang/Serialization/ASTDeserializationListener.h"
     18 #include "llvm/ADT/StringExtras.h"
     19 
     20 using namespace clang;
     21 
     22 // Give ASTDeserializationListener's VTable a home.
     23 ASTDeserializationListener::~ASTDeserializationListener() { }
     24 
     25 serialization::TypeIdx
     26 serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
     27   unsigned ID = 0;
     28   switch (BT->getKind()) {
     29   case BuiltinType::Void:       ID = PREDEF_TYPE_VOID_ID;       break;
     30   case BuiltinType::Bool:       ID = PREDEF_TYPE_BOOL_ID;       break;
     31   case BuiltinType::Char_U:     ID = PREDEF_TYPE_CHAR_U_ID;     break;
     32   case BuiltinType::UChar:      ID = PREDEF_TYPE_UCHAR_ID;      break;
     33   case BuiltinType::UShort:     ID = PREDEF_TYPE_USHORT_ID;     break;
     34   case BuiltinType::UInt:       ID = PREDEF_TYPE_UINT_ID;       break;
     35   case BuiltinType::ULong:      ID = PREDEF_TYPE_ULONG_ID;      break;
     36   case BuiltinType::ULongLong:  ID = PREDEF_TYPE_ULONGLONG_ID;  break;
     37   case BuiltinType::UInt128:    ID = PREDEF_TYPE_UINT128_ID;    break;
     38   case BuiltinType::Char_S:     ID = PREDEF_TYPE_CHAR_S_ID;     break;
     39   case BuiltinType::SChar:      ID = PREDEF_TYPE_SCHAR_ID;      break;
     40   case BuiltinType::WChar_S:
     41   case BuiltinType::WChar_U:    ID = PREDEF_TYPE_WCHAR_ID;      break;
     42   case BuiltinType::Short:      ID = PREDEF_TYPE_SHORT_ID;      break;
     43   case BuiltinType::Int:        ID = PREDEF_TYPE_INT_ID;        break;
     44   case BuiltinType::Long:       ID = PREDEF_TYPE_LONG_ID;       break;
     45   case BuiltinType::LongLong:   ID = PREDEF_TYPE_LONGLONG_ID;   break;
     46   case BuiltinType::Int128:     ID = PREDEF_TYPE_INT128_ID;     break;
     47   case BuiltinType::Half:       ID = PREDEF_TYPE_HALF_ID;       break;
     48   case BuiltinType::Float:      ID = PREDEF_TYPE_FLOAT_ID;      break;
     49   case BuiltinType::Double:     ID = PREDEF_TYPE_DOUBLE_ID;     break;
     50   case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break;
     51   case BuiltinType::NullPtr:    ID = PREDEF_TYPE_NULLPTR_ID;    break;
     52   case BuiltinType::Char16:     ID = PREDEF_TYPE_CHAR16_ID;     break;
     53   case BuiltinType::Char32:     ID = PREDEF_TYPE_CHAR32_ID;     break;
     54   case BuiltinType::Overload:   ID = PREDEF_TYPE_OVERLOAD_ID;   break;
     55   case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER;  break;
     56   case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break;
     57   case BuiltinType::Dependent:  ID = PREDEF_TYPE_DEPENDENT_ID;  break;
     58   case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY;   break;
     59   case BuiltinType::ARCUnbridgedCast:
     60                                 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; break;
     61   case BuiltinType::ObjCId:     ID = PREDEF_TYPE_OBJC_ID;       break;
     62   case BuiltinType::ObjCClass:  ID = PREDEF_TYPE_OBJC_CLASS;    break;
     63   case BuiltinType::ObjCSel:    ID = PREDEF_TYPE_OBJC_SEL;      break;
     64   case BuiltinType::OCLImage1d:       ID = PREDEF_TYPE_IMAGE1D_ID;      break;
     65   case BuiltinType::OCLImage1dArray:  ID = PREDEF_TYPE_IMAGE1D_ARR_ID;  break;
     66   case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break;
     67   case BuiltinType::OCLImage2d:       ID = PREDEF_TYPE_IMAGE2D_ID;      break;
     68   case BuiltinType::OCLImage2dArray:  ID = PREDEF_TYPE_IMAGE2D_ARR_ID;  break;
     69   case BuiltinType::OCLImage3d:       ID = PREDEF_TYPE_IMAGE3D_ID;      break;
     70   case BuiltinType::OCLSampler:       ID = PREDEF_TYPE_SAMPLER_ID;      break;
     71   case BuiltinType::OCLEvent:         ID = PREDEF_TYPE_EVENT_ID;        break;
     72   case BuiltinType::BuiltinFn:
     73                                 ID = PREDEF_TYPE_BUILTIN_FN; break;
     74 
     75   }
     76 
     77   return TypeIdx(ID);
     78 }
     79 
     80 unsigned serialization::ComputeHash(Selector Sel) {
     81   unsigned N = Sel.getNumArgs();
     82   if (N == 0)
     83     ++N;
     84   unsigned R = 5381;
     85   for (unsigned I = 0; I != N; ++I)
     86     if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
     87       R = llvm::HashString(II->getName(), R);
     88   return R;
     89 }
     90 
     91 const DeclContext *
     92 serialization::getDefinitiveDeclContext(const DeclContext *DC) {
     93   switch (DC->getDeclKind()) {
     94   // These entities may have multiple definitions.
     95   case Decl::TranslationUnit:
     96   case Decl::Namespace:
     97   case Decl::LinkageSpec:
     98     return 0;
     99 
    100   // C/C++ tag types can only be defined in one place.
    101   case Decl::Enum:
    102   case Decl::Record:
    103     if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
    104       return Def;
    105     return 0;
    106 
    107   // FIXME: These can be defined in one place... except special member
    108   // functions and out-of-line definitions.
    109   case Decl::CXXRecord:
    110   case Decl::ClassTemplateSpecialization:
    111   case Decl::ClassTemplatePartialSpecialization:
    112     return 0;
    113 
    114   // Each function, method, and block declaration is its own DeclContext.
    115   case Decl::Function:
    116   case Decl::CXXMethod:
    117   case Decl::CXXConstructor:
    118   case Decl::CXXDestructor:
    119   case Decl::CXXConversion:
    120   case Decl::ObjCMethod:
    121   case Decl::Block:
    122     // Objective C categories, category implementations, and class
    123     // implementations can only be defined in one place.
    124   case Decl::ObjCCategory:
    125   case Decl::ObjCCategoryImpl:
    126   case Decl::ObjCImplementation:
    127     return DC;
    128 
    129   case Decl::ObjCProtocol:
    130     if (const ObjCProtocolDecl *Def
    131           = cast<ObjCProtocolDecl>(DC)->getDefinition())
    132       return Def;
    133     return 0;
    134 
    135   // FIXME: These are defined in one place, but properties in class extensions
    136   // end up being back-patched into the main interface. See
    137   // Sema::HandlePropertyInClassExtension for the offending code.
    138   case Decl::ObjCInterface:
    139     return 0;
    140 
    141   default:
    142     llvm_unreachable("Unhandled DeclContext in AST reader");
    143   }
    144 
    145   llvm_unreachable("Unhandled decl kind");
    146 }
    147 
    148 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
    149   switch (static_cast<Decl::Kind>(Kind)) {
    150   case Decl::TranslationUnit: // Special case of a "merged" declaration.
    151   case Decl::Namespace:
    152   case Decl::NamespaceAlias: // FIXME: Not yet redeclarable, but will be.
    153   case Decl::Typedef:
    154   case Decl::TypeAlias:
    155   case Decl::Enum:
    156   case Decl::Record:
    157   case Decl::CXXRecord:
    158   case Decl::ClassTemplateSpecialization:
    159   case Decl::ClassTemplatePartialSpecialization:
    160   case Decl::Function:
    161   case Decl::CXXMethod:
    162   case Decl::CXXConstructor:
    163   case Decl::CXXDestructor:
    164   case Decl::CXXConversion:
    165   case Decl::Var:
    166   case Decl::FunctionTemplate:
    167   case Decl::ClassTemplate:
    168   case Decl::TypeAliasTemplate:
    169   case Decl::ObjCProtocol:
    170   case Decl::ObjCInterface:
    171   case Decl::Empty:
    172     return true;
    173 
    174   // Never redeclarable.
    175   case Decl::UsingDirective:
    176   case Decl::Label:
    177   case Decl::UnresolvedUsingTypename:
    178   case Decl::TemplateTypeParm:
    179   case Decl::EnumConstant:
    180   case Decl::UnresolvedUsingValue:
    181   case Decl::IndirectField:
    182   case Decl::Field:
    183   case Decl::ObjCIvar:
    184   case Decl::ObjCAtDefsField:
    185   case Decl::ImplicitParam:
    186   case Decl::ParmVar:
    187   case Decl::NonTypeTemplateParm:
    188   case Decl::TemplateTemplateParm:
    189   case Decl::Using:
    190   case Decl::UsingShadow:
    191   case Decl::ObjCMethod:
    192   case Decl::ObjCCategory:
    193   case Decl::ObjCCategoryImpl:
    194   case Decl::ObjCImplementation:
    195   case Decl::ObjCProperty:
    196   case Decl::ObjCCompatibleAlias:
    197   case Decl::LinkageSpec:
    198   case Decl::ObjCPropertyImpl:
    199   case Decl::FileScopeAsm:
    200   case Decl::AccessSpec:
    201   case Decl::Friend:
    202   case Decl::FriendTemplate:
    203   case Decl::StaticAssert:
    204   case Decl::Block:
    205   case Decl::ClassScopeFunctionSpecialization:
    206   case Decl::Import:
    207     return false;
    208   }
    209 
    210   llvm_unreachable("Unhandled declaration kind");
    211 }
    212