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 nullptr;
     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 nullptr;
    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 nullptr;
    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   case Decl::Captured:
    123     // Objective C categories, category implementations, and class
    124     // implementations can only be defined in one place.
    125   case Decl::ObjCCategory:
    126   case Decl::ObjCCategoryImpl:
    127   case Decl::ObjCImplementation:
    128     return DC;
    129 
    130   case Decl::ObjCProtocol:
    131     if (const ObjCProtocolDecl *Def
    132           = cast<ObjCProtocolDecl>(DC)->getDefinition())
    133       return Def;
    134     return nullptr;
    135 
    136   // FIXME: These are defined in one place, but properties in class extensions
    137   // end up being back-patched into the main interface. See
    138   // Sema::HandlePropertyInClassExtension for the offending code.
    139   case Decl::ObjCInterface:
    140     return nullptr;
    141 
    142   default:
    143     llvm_unreachable("Unhandled DeclContext in AST reader");
    144   }
    145 
    146   llvm_unreachable("Unhandled decl kind");
    147 }
    148 
    149 bool serialization::isRedeclarableDeclKind(unsigned Kind) {
    150   switch (static_cast<Decl::Kind>(Kind)) {
    151   case Decl::TranslationUnit: // Special case of a "merged" declaration.
    152   case Decl::Namespace:
    153   case Decl::NamespaceAlias: // FIXME: Not yet redeclarable, but will be.
    154   case Decl::Typedef:
    155   case Decl::TypeAlias:
    156   case Decl::Enum:
    157   case Decl::Record:
    158   case Decl::CXXRecord:
    159   case Decl::ClassTemplateSpecialization:
    160   case Decl::ClassTemplatePartialSpecialization:
    161   case Decl::VarTemplateSpecialization:
    162   case Decl::VarTemplatePartialSpecialization:
    163   case Decl::Function:
    164   case Decl::CXXMethod:
    165   case Decl::CXXConstructor:
    166   case Decl::CXXDestructor:
    167   case Decl::CXXConversion:
    168   case Decl::UsingShadow:
    169   case Decl::Var:
    170   case Decl::FunctionTemplate:
    171   case Decl::ClassTemplate:
    172   case Decl::VarTemplate:
    173   case Decl::TypeAliasTemplate:
    174   case Decl::ObjCProtocol:
    175   case Decl::ObjCInterface:
    176   case Decl::Empty:
    177     return true;
    178 
    179   // Never redeclarable.
    180   case Decl::UsingDirective:
    181   case Decl::Label:
    182   case Decl::UnresolvedUsingTypename:
    183   case Decl::TemplateTypeParm:
    184   case Decl::EnumConstant:
    185   case Decl::UnresolvedUsingValue:
    186   case Decl::IndirectField:
    187   case Decl::Field:
    188   case Decl::MSProperty:
    189   case Decl::ObjCIvar:
    190   case Decl::ObjCAtDefsField:
    191   case Decl::ImplicitParam:
    192   case Decl::ParmVar:
    193   case Decl::NonTypeTemplateParm:
    194   case Decl::TemplateTemplateParm:
    195   case Decl::Using:
    196   case Decl::ObjCMethod:
    197   case Decl::ObjCCategory:
    198   case Decl::ObjCCategoryImpl:
    199   case Decl::ObjCImplementation:
    200   case Decl::ObjCProperty:
    201   case Decl::ObjCCompatibleAlias:
    202   case Decl::LinkageSpec:
    203   case Decl::ObjCPropertyImpl:
    204   case Decl::FileScopeAsm:
    205   case Decl::AccessSpec:
    206   case Decl::Friend:
    207   case Decl::FriendTemplate:
    208   case Decl::StaticAssert:
    209   case Decl::Block:
    210   case Decl::Captured:
    211   case Decl::ClassScopeFunctionSpecialization:
    212   case Decl::Import:
    213   case Decl::OMPThreadPrivate:
    214     return false;
    215   }
    216 
    217   llvm_unreachable("Unhandled declaration kind");
    218 }
    219