clang API Documentation
00001 //===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines common functions that both ASTReader and ASTWriter use. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "ASTCommon.h" 00015 #include "clang/AST/DeclCXX.h" 00016 #include "clang/AST/DeclObjC.h" 00017 #include "clang/Basic/IdentifierTable.h" 00018 #include "clang/Serialization/ASTDeserializationListener.h" 00019 #include "llvm/ADT/StringExtras.h" 00020 00021 using namespace clang; 00022 00023 // Give ASTDeserializationListener's VTable a home. 00024 ASTDeserializationListener::~ASTDeserializationListener() { } 00025 00026 serialization::TypeIdx 00027 serialization::TypeIdxFromBuiltin(const BuiltinType *BT) { 00028 unsigned ID = 0; 00029 switch (BT->getKind()) { 00030 case BuiltinType::Void: ID = PREDEF_TYPE_VOID_ID; break; 00031 case BuiltinType::Bool: ID = PREDEF_TYPE_BOOL_ID; break; 00032 case BuiltinType::Char_U: ID = PREDEF_TYPE_CHAR_U_ID; break; 00033 case BuiltinType::UChar: ID = PREDEF_TYPE_UCHAR_ID; break; 00034 case BuiltinType::UShort: ID = PREDEF_TYPE_USHORT_ID; break; 00035 case BuiltinType::UInt: ID = PREDEF_TYPE_UINT_ID; break; 00036 case BuiltinType::ULong: ID = PREDEF_TYPE_ULONG_ID; break; 00037 case BuiltinType::ULongLong: ID = PREDEF_TYPE_ULONGLONG_ID; break; 00038 case BuiltinType::UInt128: ID = PREDEF_TYPE_UINT128_ID; break; 00039 case BuiltinType::Char_S: ID = PREDEF_TYPE_CHAR_S_ID; break; 00040 case BuiltinType::SChar: ID = PREDEF_TYPE_SCHAR_ID; break; 00041 case BuiltinType::WChar_S: 00042 case BuiltinType::WChar_U: ID = PREDEF_TYPE_WCHAR_ID; break; 00043 case BuiltinType::Short: ID = PREDEF_TYPE_SHORT_ID; break; 00044 case BuiltinType::Int: ID = PREDEF_TYPE_INT_ID; break; 00045 case BuiltinType::Long: ID = PREDEF_TYPE_LONG_ID; break; 00046 case BuiltinType::LongLong: ID = PREDEF_TYPE_LONGLONG_ID; break; 00047 case BuiltinType::Int128: ID = PREDEF_TYPE_INT128_ID; break; 00048 case BuiltinType::Half: ID = PREDEF_TYPE_HALF_ID; break; 00049 case BuiltinType::Float: ID = PREDEF_TYPE_FLOAT_ID; break; 00050 case BuiltinType::Double: ID = PREDEF_TYPE_DOUBLE_ID; break; 00051 case BuiltinType::LongDouble: ID = PREDEF_TYPE_LONGDOUBLE_ID; break; 00052 case BuiltinType::NullPtr: ID = PREDEF_TYPE_NULLPTR_ID; break; 00053 case BuiltinType::Char16: ID = PREDEF_TYPE_CHAR16_ID; break; 00054 case BuiltinType::Char32: ID = PREDEF_TYPE_CHAR32_ID; break; 00055 case BuiltinType::Overload: ID = PREDEF_TYPE_OVERLOAD_ID; break; 00056 case BuiltinType::BoundMember:ID = PREDEF_TYPE_BOUND_MEMBER; break; 00057 case BuiltinType::PseudoObject:ID = PREDEF_TYPE_PSEUDO_OBJECT;break; 00058 case BuiltinType::Dependent: ID = PREDEF_TYPE_DEPENDENT_ID; break; 00059 case BuiltinType::UnknownAny: ID = PREDEF_TYPE_UNKNOWN_ANY; break; 00060 case BuiltinType::ARCUnbridgedCast: 00061 ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST; break; 00062 case BuiltinType::ObjCId: ID = PREDEF_TYPE_OBJC_ID; break; 00063 case BuiltinType::ObjCClass: ID = PREDEF_TYPE_OBJC_CLASS; break; 00064 case BuiltinType::ObjCSel: ID = PREDEF_TYPE_OBJC_SEL; break; 00065 case BuiltinType::OCLImage1d: ID = PREDEF_TYPE_IMAGE1D_ID; break; 00066 case BuiltinType::OCLImage1dArray: ID = PREDEF_TYPE_IMAGE1D_ARR_ID; break; 00067 case BuiltinType::OCLImage1dBuffer: ID = PREDEF_TYPE_IMAGE1D_BUFF_ID; break; 00068 case BuiltinType::OCLImage2d: ID = PREDEF_TYPE_IMAGE2D_ID; break; 00069 case BuiltinType::OCLImage2dArray: ID = PREDEF_TYPE_IMAGE2D_ARR_ID; break; 00070 case BuiltinType::OCLImage3d: ID = PREDEF_TYPE_IMAGE3D_ID; break; 00071 case BuiltinType::OCLSampler: ID = PREDEF_TYPE_SAMPLER_ID; break; 00072 case BuiltinType::OCLEvent: ID = PREDEF_TYPE_EVENT_ID; break; 00073 case BuiltinType::BuiltinFn: 00074 ID = PREDEF_TYPE_BUILTIN_FN; break; 00075 00076 } 00077 00078 return TypeIdx(ID); 00079 } 00080 00081 unsigned serialization::ComputeHash(Selector Sel) { 00082 unsigned N = Sel.getNumArgs(); 00083 if (N == 0) 00084 ++N; 00085 unsigned R = 5381; 00086 for (unsigned I = 0; I != N; ++I) 00087 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) 00088 R = llvm::HashString(II->getName(), R); 00089 return R; 00090 } 00091 00092 const DeclContext * 00093 serialization::getDefinitiveDeclContext(const DeclContext *DC) { 00094 switch (DC->getDeclKind()) { 00095 // These entities may have multiple definitions. 00096 case Decl::TranslationUnit: 00097 case Decl::Namespace: 00098 case Decl::LinkageSpec: 00099 return nullptr; 00100 00101 // C/C++ tag types can only be defined in one place. 00102 case Decl::Enum: 00103 case Decl::Record: 00104 if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition()) 00105 return Def; 00106 return nullptr; 00107 00108 // FIXME: These can be defined in one place... except special member 00109 // functions and out-of-line definitions. 00110 case Decl::CXXRecord: 00111 case Decl::ClassTemplateSpecialization: 00112 case Decl::ClassTemplatePartialSpecialization: 00113 return nullptr; 00114 00115 // Each function, method, and block declaration is its own DeclContext. 00116 case Decl::Function: 00117 case Decl::CXXMethod: 00118 case Decl::CXXConstructor: 00119 case Decl::CXXDestructor: 00120 case Decl::CXXConversion: 00121 case Decl::ObjCMethod: 00122 case Decl::Block: 00123 case Decl::Captured: 00124 // Objective C categories, category implementations, and class 00125 // implementations can only be defined in one place. 00126 case Decl::ObjCCategory: 00127 case Decl::ObjCCategoryImpl: 00128 case Decl::ObjCImplementation: 00129 return DC; 00130 00131 case Decl::ObjCProtocol: 00132 if (const ObjCProtocolDecl *Def 00133 = cast<ObjCProtocolDecl>(DC)->getDefinition()) 00134 return Def; 00135 return nullptr; 00136 00137 // FIXME: These are defined in one place, but properties in class extensions 00138 // end up being back-patched into the main interface. See 00139 // Sema::HandlePropertyInClassExtension for the offending code. 00140 case Decl::ObjCInterface: 00141 return nullptr; 00142 00143 default: 00144 llvm_unreachable("Unhandled DeclContext in AST reader"); 00145 } 00146 00147 llvm_unreachable("Unhandled decl kind"); 00148 } 00149 00150 bool serialization::isRedeclarableDeclKind(unsigned Kind) { 00151 switch (static_cast<Decl::Kind>(Kind)) { 00152 case Decl::TranslationUnit: // Special case of a "merged" declaration. 00153 case Decl::Namespace: 00154 case Decl::NamespaceAlias: 00155 case Decl::Typedef: 00156 case Decl::TypeAlias: 00157 case Decl::Enum: 00158 case Decl::Record: 00159 case Decl::CXXRecord: 00160 case Decl::ClassTemplateSpecialization: 00161 case Decl::ClassTemplatePartialSpecialization: 00162 case Decl::VarTemplateSpecialization: 00163 case Decl::VarTemplatePartialSpecialization: 00164 case Decl::Function: 00165 case Decl::CXXMethod: 00166 case Decl::CXXConstructor: 00167 case Decl::CXXDestructor: 00168 case Decl::CXXConversion: 00169 case Decl::UsingShadow: 00170 case Decl::Var: 00171 case Decl::FunctionTemplate: 00172 case Decl::ClassTemplate: 00173 case Decl::VarTemplate: 00174 case Decl::TypeAliasTemplate: 00175 case Decl::ObjCProtocol: 00176 case Decl::ObjCInterface: 00177 case Decl::Empty: 00178 return true; 00179 00180 // Never redeclarable. 00181 case Decl::UsingDirective: 00182 case Decl::Label: 00183 case Decl::UnresolvedUsingTypename: 00184 case Decl::TemplateTypeParm: 00185 case Decl::EnumConstant: 00186 case Decl::UnresolvedUsingValue: 00187 case Decl::IndirectField: 00188 case Decl::Field: 00189 case Decl::MSProperty: 00190 case Decl::ObjCIvar: 00191 case Decl::ObjCAtDefsField: 00192 case Decl::NonTypeTemplateParm: 00193 case Decl::TemplateTemplateParm: 00194 case Decl::Using: 00195 case Decl::ObjCMethod: 00196 case Decl::ObjCCategory: 00197 case Decl::ObjCCategoryImpl: 00198 case Decl::ObjCImplementation: 00199 case Decl::ObjCProperty: 00200 case Decl::ObjCCompatibleAlias: 00201 case Decl::LinkageSpec: 00202 case Decl::ObjCPropertyImpl: 00203 case Decl::FileScopeAsm: 00204 case Decl::AccessSpec: 00205 case Decl::Friend: 00206 case Decl::FriendTemplate: 00207 case Decl::StaticAssert: 00208 case Decl::Block: 00209 case Decl::Captured: 00210 case Decl::ClassScopeFunctionSpecialization: 00211 case Decl::Import: 00212 case Decl::OMPThreadPrivate: 00213 return false; 00214 00215 // These indirectly derive from Redeclarable<T> but are not actually 00216 // redeclarable. 00217 case Decl::ImplicitParam: 00218 case Decl::ParmVar: 00219 return false; 00220 } 00221 00222 llvm_unreachable("Unhandled declaration kind"); 00223 } 00224 00225 bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) { 00226 if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext())) 00227 return false; 00228 return isa<TagDecl>(D) || isa<FieldDecl>(D); 00229 } 00230