ASTCommon.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include "ASTCommon.h"
  14. #include "clang/AST/DeclCXX.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. using namespace clang;
  20. // Give ASTDeserializationListener's VTable a home.
  21. ASTDeserializationListener::~ASTDeserializationListener() { }
  22. serialization::TypeIdx
  23. serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
  24. unsigned ID = 0;
  25. switch (BT->getKind()) {
  26. case BuiltinType::Void:
  27. ID = PREDEF_TYPE_VOID_ID;
  28. break;
  29. case BuiltinType::Bool:
  30. ID = PREDEF_TYPE_BOOL_ID;
  31. break;
  32. case BuiltinType::Char_U:
  33. ID = PREDEF_TYPE_CHAR_U_ID;
  34. break;
  35. case BuiltinType::UChar:
  36. ID = PREDEF_TYPE_UCHAR_ID;
  37. break;
  38. case BuiltinType::UShort:
  39. ID = PREDEF_TYPE_USHORT_ID;
  40. break;
  41. case BuiltinType::UInt:
  42. ID = PREDEF_TYPE_UINT_ID;
  43. break;
  44. case BuiltinType::ULong:
  45. ID = PREDEF_TYPE_ULONG_ID;
  46. break;
  47. case BuiltinType::ULongLong:
  48. ID = PREDEF_TYPE_ULONGLONG_ID;
  49. break;
  50. case BuiltinType::UInt128:
  51. ID = PREDEF_TYPE_UINT128_ID;
  52. break;
  53. case BuiltinType::Char_S:
  54. ID = PREDEF_TYPE_CHAR_S_ID;
  55. break;
  56. case BuiltinType::SChar:
  57. ID = PREDEF_TYPE_SCHAR_ID;
  58. break;
  59. case BuiltinType::WChar_S:
  60. case BuiltinType::WChar_U:
  61. ID = PREDEF_TYPE_WCHAR_ID;
  62. break;
  63. case BuiltinType::Short:
  64. ID = PREDEF_TYPE_SHORT_ID;
  65. break;
  66. case BuiltinType::Int:
  67. ID = PREDEF_TYPE_INT_ID;
  68. break;
  69. case BuiltinType::Long:
  70. ID = PREDEF_TYPE_LONG_ID;
  71. break;
  72. case BuiltinType::LongLong:
  73. ID = PREDEF_TYPE_LONGLONG_ID;
  74. break;
  75. case BuiltinType::Int128:
  76. ID = PREDEF_TYPE_INT128_ID;
  77. break;
  78. case BuiltinType::Half:
  79. ID = PREDEF_TYPE_HALF_ID;
  80. break;
  81. case BuiltinType::Float:
  82. ID = PREDEF_TYPE_FLOAT_ID;
  83. break;
  84. case BuiltinType::Double:
  85. ID = PREDEF_TYPE_DOUBLE_ID;
  86. break;
  87. case BuiltinType::LongDouble:
  88. ID = PREDEF_TYPE_LONGDOUBLE_ID;
  89. break;
  90. case BuiltinType::Float16:
  91. ID = PREDEF_TYPE_FLOAT16_ID;
  92. break;
  93. case BuiltinType::Float128:
  94. ID = PREDEF_TYPE_FLOAT128_ID;
  95. break;
  96. case BuiltinType::NullPtr:
  97. ID = PREDEF_TYPE_NULLPTR_ID;
  98. break;
  99. case BuiltinType::Char16:
  100. ID = PREDEF_TYPE_CHAR16_ID;
  101. break;
  102. case BuiltinType::Char32:
  103. ID = PREDEF_TYPE_CHAR32_ID;
  104. break;
  105. case BuiltinType::Overload:
  106. ID = PREDEF_TYPE_OVERLOAD_ID;
  107. break;
  108. case BuiltinType::BoundMember:
  109. ID = PREDEF_TYPE_BOUND_MEMBER;
  110. break;
  111. case BuiltinType::PseudoObject:
  112. ID = PREDEF_TYPE_PSEUDO_OBJECT;
  113. break;
  114. case BuiltinType::Dependent:
  115. ID = PREDEF_TYPE_DEPENDENT_ID;
  116. break;
  117. case BuiltinType::UnknownAny:
  118. ID = PREDEF_TYPE_UNKNOWN_ANY;
  119. break;
  120. case BuiltinType::ARCUnbridgedCast:
  121. ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
  122. break;
  123. case BuiltinType::ObjCId:
  124. ID = PREDEF_TYPE_OBJC_ID;
  125. break;
  126. case BuiltinType::ObjCClass:
  127. ID = PREDEF_TYPE_OBJC_CLASS;
  128. break;
  129. case BuiltinType::ObjCSel:
  130. ID = PREDEF_TYPE_OBJC_SEL;
  131. break;
  132. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  133. case BuiltinType::Id: \
  134. ID = PREDEF_TYPE_##Id##_ID; \
  135. break;
  136. #include "clang/Basic/OpenCLImageTypes.def"
  137. case BuiltinType::OCLSampler:
  138. ID = PREDEF_TYPE_SAMPLER_ID;
  139. break;
  140. case BuiltinType::OCLEvent:
  141. ID = PREDEF_TYPE_EVENT_ID;
  142. break;
  143. case BuiltinType::OCLClkEvent:
  144. ID = PREDEF_TYPE_CLK_EVENT_ID;
  145. break;
  146. case BuiltinType::OCLQueue:
  147. ID = PREDEF_TYPE_QUEUE_ID;
  148. break;
  149. case BuiltinType::OCLReserveID:
  150. ID = PREDEF_TYPE_RESERVE_ID_ID;
  151. break;
  152. case BuiltinType::BuiltinFn:
  153. ID = PREDEF_TYPE_BUILTIN_FN;
  154. break;
  155. case BuiltinType::OMPArraySection:
  156. ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
  157. break;
  158. }
  159. return TypeIdx(ID);
  160. }
  161. unsigned serialization::ComputeHash(Selector Sel) {
  162. unsigned N = Sel.getNumArgs();
  163. if (N == 0)
  164. ++N;
  165. unsigned R = 5381;
  166. for (unsigned I = 0; I != N; ++I)
  167. if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
  168. R = llvm::HashString(II->getName(), R);
  169. return R;
  170. }
  171. const DeclContext *
  172. serialization::getDefinitiveDeclContext(const DeclContext *DC) {
  173. switch (DC->getDeclKind()) {
  174. // These entities may have multiple definitions.
  175. case Decl::TranslationUnit:
  176. case Decl::ExternCContext:
  177. case Decl::Namespace:
  178. case Decl::LinkageSpec:
  179. case Decl::Export:
  180. return nullptr;
  181. // C/C++ tag types can only be defined in one place.
  182. case Decl::Enum:
  183. case Decl::Record:
  184. if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
  185. return Def;
  186. return nullptr;
  187. // FIXME: These can be defined in one place... except special member
  188. // functions and out-of-line definitions.
  189. case Decl::CXXRecord:
  190. case Decl::ClassTemplateSpecialization:
  191. case Decl::ClassTemplatePartialSpecialization:
  192. return nullptr;
  193. // Each function, method, and block declaration is its own DeclContext.
  194. case Decl::Function:
  195. case Decl::CXXMethod:
  196. case Decl::CXXConstructor:
  197. case Decl::CXXDestructor:
  198. case Decl::CXXConversion:
  199. case Decl::ObjCMethod:
  200. case Decl::Block:
  201. case Decl::Captured:
  202. // Objective C categories, category implementations, and class
  203. // implementations can only be defined in one place.
  204. case Decl::ObjCCategory:
  205. case Decl::ObjCCategoryImpl:
  206. case Decl::ObjCImplementation:
  207. return DC;
  208. case Decl::ObjCProtocol:
  209. if (const ObjCProtocolDecl *Def
  210. = cast<ObjCProtocolDecl>(DC)->getDefinition())
  211. return Def;
  212. return nullptr;
  213. // FIXME: These are defined in one place, but properties in class extensions
  214. // end up being back-patched into the main interface. See
  215. // Sema::HandlePropertyInClassExtension for the offending code.
  216. case Decl::ObjCInterface:
  217. return nullptr;
  218. default:
  219. llvm_unreachable("Unhandled DeclContext in AST reader");
  220. }
  221. llvm_unreachable("Unhandled decl kind");
  222. }
  223. bool serialization::isRedeclarableDeclKind(unsigned Kind) {
  224. switch (static_cast<Decl::Kind>(Kind)) {
  225. case Decl::TranslationUnit:
  226. case Decl::ExternCContext:
  227. // Special case of a "merged" declaration.
  228. return true;
  229. case Decl::Namespace:
  230. case Decl::NamespaceAlias:
  231. case Decl::Typedef:
  232. case Decl::TypeAlias:
  233. case Decl::Enum:
  234. case Decl::Record:
  235. case Decl::CXXRecord:
  236. case Decl::ClassTemplateSpecialization:
  237. case Decl::ClassTemplatePartialSpecialization:
  238. case Decl::VarTemplateSpecialization:
  239. case Decl::VarTemplatePartialSpecialization:
  240. case Decl::Function:
  241. case Decl::CXXDeductionGuide:
  242. case Decl::CXXMethod:
  243. case Decl::CXXConstructor:
  244. case Decl::CXXDestructor:
  245. case Decl::CXXConversion:
  246. case Decl::UsingShadow:
  247. case Decl::ConstructorUsingShadow:
  248. case Decl::Var:
  249. case Decl::FunctionTemplate:
  250. case Decl::ClassTemplate:
  251. case Decl::VarTemplate:
  252. case Decl::TypeAliasTemplate:
  253. case Decl::ObjCProtocol:
  254. case Decl::ObjCInterface:
  255. case Decl::Empty:
  256. return true;
  257. // Never redeclarable.
  258. case Decl::UsingDirective:
  259. case Decl::Label:
  260. case Decl::UnresolvedUsingTypename:
  261. case Decl::TemplateTypeParm:
  262. case Decl::EnumConstant:
  263. case Decl::UnresolvedUsingValue:
  264. case Decl::IndirectField:
  265. case Decl::Field:
  266. case Decl::MSProperty:
  267. case Decl::ObjCIvar:
  268. case Decl::ObjCAtDefsField:
  269. case Decl::NonTypeTemplateParm:
  270. case Decl::TemplateTemplateParm:
  271. case Decl::Using:
  272. case Decl::UsingPack:
  273. case Decl::ObjCMethod:
  274. case Decl::ObjCCategory:
  275. case Decl::ObjCCategoryImpl:
  276. case Decl::ObjCImplementation:
  277. case Decl::ObjCProperty:
  278. case Decl::ObjCCompatibleAlias:
  279. case Decl::LinkageSpec:
  280. case Decl::Export:
  281. case Decl::ObjCPropertyImpl:
  282. case Decl::PragmaComment:
  283. case Decl::PragmaDetectMismatch:
  284. case Decl::FileScopeAsm:
  285. case Decl::AccessSpec:
  286. case Decl::Friend:
  287. case Decl::FriendTemplate:
  288. case Decl::StaticAssert:
  289. case Decl::Block:
  290. case Decl::Captured:
  291. case Decl::ClassScopeFunctionSpecialization:
  292. case Decl::Import:
  293. case Decl::OMPThreadPrivate:
  294. case Decl::OMPCapturedExpr:
  295. case Decl::OMPDeclareReduction:
  296. case Decl::BuiltinTemplate:
  297. case Decl::Decomposition:
  298. case Decl::Binding:
  299. return false;
  300. // These indirectly derive from Redeclarable<T> but are not actually
  301. // redeclarable.
  302. case Decl::ImplicitParam:
  303. case Decl::ParmVar:
  304. case Decl::ObjCTypeParam:
  305. return false;
  306. }
  307. llvm_unreachable("Unhandled declaration kind");
  308. }
  309. bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
  310. // Friend declarations in dependent contexts aren't anonymous in the usual
  311. // sense, but they cannot be found by name lookup in their semantic context
  312. // (or indeed in any context), so we treat them as anonymous.
  313. //
  314. // This doesn't apply to friend tag decls; Sema makes those available to name
  315. // lookup in the surrounding context.
  316. if (D->getFriendObjectKind() &&
  317. D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
  318. // For function templates and class templates, the template is numbered and
  319. // not its pattern.
  320. if (auto *FD = dyn_cast<FunctionDecl>(D))
  321. return !FD->getDescribedFunctionTemplate();
  322. if (auto *RD = dyn_cast<CXXRecordDecl>(D))
  323. return !RD->getDescribedClassTemplate();
  324. return true;
  325. }
  326. // Otherwise, we only care about anonymous class members.
  327. if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
  328. return false;
  329. return isa<TagDecl>(D) || isa<FieldDecl>(D);
  330. }