DeclSerialization.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. //===--- DeclSerialization.cpp - Serialization of Decls ---------*- 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 methods that implement bitcode serialization for Decls.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/Expr.h"
  16. #include "llvm/Bitcode/Serialize.h"
  17. #include "llvm/Bitcode/Deserialize.h"
  18. using llvm::Serializer;
  19. using llvm::Deserializer;
  20. using llvm::SerializedPtrID;
  21. using namespace clang;
  22. //===----------------------------------------------------------------------===//
  23. // Decl Serialization: Dispatch code to handle specialized decl types.
  24. //===----------------------------------------------------------------------===//
  25. void Decl::Emit(Serializer& S) const {
  26. S.EmitInt(getKind());
  27. EmitImpl(S);
  28. }
  29. Decl* Decl::Create(Deserializer& D, ASTContext& C) {
  30. Kind k = static_cast<Kind>(D.ReadInt());
  31. switch (k) {
  32. default:
  33. assert (false && "Not implemented.");
  34. break;
  35. case Var:
  36. return VarDecl::CreateImpl(D, C);
  37. case Enum:
  38. return EnumDecl::CreateImpl(D, C);
  39. case EnumConstant:
  40. return EnumConstantDecl::CreateImpl(D, C);
  41. case Field:
  42. return FieldDecl::CreateImpl(D, C);
  43. case ParmVar:
  44. return ParmVarDecl::CreateImpl(D, C);
  45. case Function:
  46. return FunctionDecl::CreateImpl(D, C);
  47. case Union:
  48. case Struct:
  49. return RecordDecl::CreateImpl(k, D, C);
  50. case Typedef:
  51. return TypedefDecl::CreateImpl(D, C);
  52. case FileScopeAsm:
  53. return FileScopeAsmDecl::CreateImpl(D, C);
  54. }
  55. }
  56. //===----------------------------------------------------------------------===//
  57. // Common serialization logic for subclasses of Decl.
  58. //===----------------------------------------------------------------------===//
  59. void Decl::EmitInRec(Serializer& S) const {
  60. S.Emit(getLocation()); // From Decl.
  61. }
  62. void Decl::ReadInRec(Deserializer& D, ASTContext& C) {
  63. Loc = SourceLocation::ReadVal(D); // From Decl.
  64. }
  65. //===----------------------------------------------------------------------===//
  66. // Common serialization logic for subclasses of NamedDecl.
  67. //===----------------------------------------------------------------------===//
  68. void NamedDecl::EmitInRec(Serializer& S) const {
  69. Decl::EmitInRec(S);
  70. S.EmitPtr(getIdentifier()); // From NamedDecl.
  71. }
  72. void NamedDecl::ReadInRec(Deserializer& D, ASTContext& C) {
  73. Decl::ReadInRec(D, C);
  74. D.ReadPtr(Identifier); // From NamedDecl.
  75. }
  76. //===----------------------------------------------------------------------===//
  77. // Common serialization logic for subclasses of ScopedDecl.
  78. //===----------------------------------------------------------------------===//
  79. void ScopedDecl::EmitInRec(Serializer& S) const {
  80. NamedDecl::EmitInRec(S);
  81. S.EmitPtr(getNext()); // From ScopedDecl.
  82. S.EmitPtr(cast_or_null<Decl>(getDeclContext())); // From ScopedDecl.
  83. }
  84. void ScopedDecl::ReadInRec(Deserializer& D, ASTContext& C) {
  85. NamedDecl::ReadInRec(D, C);
  86. D.ReadPtr(Next); // From ScopedDecl.
  87. Decl *TmpD;
  88. D.ReadPtr(TmpD); // From ScopedDecl.
  89. CtxDecl = cast_or_null<DeclContext>(TmpD);
  90. }
  91. //===------------------------------------------------------------===//
  92. // NOTE: Not all subclasses of ScopedDecl will use the "OutRec" //
  93. // methods. This is because owned pointers are usually "batched" //
  94. // together for efficiency. //
  95. //===------------------------------------------------------------===//
  96. void ScopedDecl::EmitOutRec(Serializer& S) const {
  97. S.EmitOwnedPtr(getNextDeclarator()); // From ScopedDecl.
  98. }
  99. void ScopedDecl::ReadOutRec(Deserializer& D, ASTContext& C) {
  100. NextDeclarator =
  101. cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>(C)); // From ScopedDecl.
  102. }
  103. //===----------------------------------------------------------------------===//
  104. // Common serialization logic for subclasses of ValueDecl.
  105. //===----------------------------------------------------------------------===//
  106. void ValueDecl::EmitInRec(Serializer& S) const {
  107. ScopedDecl::EmitInRec(S);
  108. S.Emit(getType()); // From ValueDecl.
  109. }
  110. void ValueDecl::ReadInRec(Deserializer& D, ASTContext& C) {
  111. ScopedDecl::ReadInRec(D, C);
  112. DeclType = QualType::ReadVal(D); // From ValueDecl.
  113. }
  114. //===----------------------------------------------------------------------===//
  115. // Common serialization logic for subclasses of VarDecl.
  116. //===----------------------------------------------------------------------===//
  117. void VarDecl::EmitInRec(Serializer& S) const {
  118. ValueDecl::EmitInRec(S);
  119. S.EmitInt(getStorageClass()); // From VarDecl.
  120. }
  121. void VarDecl::ReadInRec(Deserializer& D, ASTContext& C) {
  122. ValueDecl::ReadInRec(D, C);
  123. SClass = static_cast<StorageClass>(D.ReadInt()); // From VarDecl.
  124. }
  125. //===------------------------------------------------------------===//
  126. // NOTE: VarDecl has its own "OutRec" methods that doesn't use //
  127. // the one define in ScopedDecl. This is to batch emit the //
  128. // owned pointers, which results in a smaller output.
  129. //===------------------------------------------------------------===//
  130. void VarDecl::EmitOutRec(Serializer& S) const {
  131. // Emit these last because they will create records of their own.
  132. S.BatchEmitOwnedPtrs(getInit(), // From VarDecl.
  133. getNextDeclarator()); // From ScopedDecl.
  134. }
  135. void VarDecl::ReadOutRec(Deserializer& D, ASTContext& C) {
  136. Decl* next_declarator;
  137. D.BatchReadOwnedPtrs(Init, // From VarDecl.
  138. next_declarator, // From ScopedDecl.
  139. C);
  140. setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
  141. }
  142. void VarDecl::EmitImpl(Serializer& S) const {
  143. VarDecl::EmitInRec(S);
  144. VarDecl::EmitOutRec(S);
  145. }
  146. void VarDecl::ReadImpl(Deserializer& D, ASTContext& C) {
  147. ReadInRec(D, C);
  148. ReadOutRec(D, C);
  149. }
  150. //===----------------------------------------------------------------------===//
  151. // VarDecl Serialization.
  152. //===----------------------------------------------------------------------===//
  153. VarDecl* VarDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  154. void *Mem = C.getAllocator().Allocate<VarDecl>();
  155. VarDecl* decl =
  156. new (Mem) VarDecl(Var, 0, SourceLocation(), NULL, QualType(), None, NULL);
  157. decl->VarDecl::ReadImpl(D, C);
  158. return decl;
  159. }
  160. //===----------------------------------------------------------------------===//
  161. // ParmVarDecl Serialization.
  162. //===----------------------------------------------------------------------===//
  163. void ParmVarDecl::EmitImpl(llvm::Serializer& S) const {
  164. VarDecl::EmitImpl(S);
  165. S.EmitInt(getObjCDeclQualifier()); // From ParmVarDecl.
  166. S.EmitOwnedPtr(getDefaultArg()); // From ParmVarDecl.
  167. }
  168. ParmVarDecl* ParmVarDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  169. void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
  170. ParmVarDecl* decl = new (Mem)
  171. ParmVarDecl(0, SourceLocation(), NULL, QualType(), None, NULL, NULL);
  172. decl->VarDecl::ReadImpl(D, C);
  173. decl->objcDeclQualifier = static_cast<ObjCDeclQualifier>(D.ReadInt());
  174. decl->DefaultArg = D.ReadOwnedPtr<Expr>(C);
  175. return decl;
  176. }
  177. //===----------------------------------------------------------------------===//
  178. // EnumDecl Serialization.
  179. //===----------------------------------------------------------------------===//
  180. void EnumDecl::EmitImpl(Serializer& S) const {
  181. ScopedDecl::EmitInRec(S);
  182. S.EmitBool(isDefinition());
  183. S.Emit(IntegerType);
  184. S.BatchEmitOwnedPtrs(ElementList,getNextDeclarator());
  185. }
  186. EnumDecl* EnumDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  187. void *Mem = C.getAllocator().Allocate<EnumDecl>();
  188. EnumDecl* decl = new (Mem) EnumDecl(0, SourceLocation(), NULL, NULL);
  189. decl->ScopedDecl::ReadInRec(D, C);
  190. decl->setDefinition(D.ReadBool());
  191. decl->IntegerType = QualType::ReadVal(D);
  192. Decl* next_declarator;
  193. Decl* Elist;
  194. D.BatchReadOwnedPtrs(Elist, next_declarator, C);
  195. decl->ElementList = cast_or_null<EnumConstantDecl>(Elist);
  196. decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
  197. return decl;
  198. }
  199. //===----------------------------------------------------------------------===//
  200. // EnumConstantDecl Serialization.
  201. //===----------------------------------------------------------------------===//
  202. void EnumConstantDecl::EmitImpl(Serializer& S) const {
  203. S.Emit(Val);
  204. ValueDecl::EmitInRec(S);
  205. S.BatchEmitOwnedPtrs(getNextDeclarator(),Init);
  206. }
  207. EnumConstantDecl* EnumConstantDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  208. llvm::APSInt val(1);
  209. D.Read(val);
  210. void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
  211. EnumConstantDecl* decl = new (Mem)
  212. EnumConstantDecl(0, SourceLocation(), NULL, QualType(), NULL, val, NULL);
  213. decl->ValueDecl::ReadInRec(D, C);
  214. Decl* next_declarator;
  215. D.BatchReadOwnedPtrs(next_declarator, decl->Init, C);
  216. decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
  217. return decl;
  218. }
  219. //===----------------------------------------------------------------------===//
  220. // FieldDecl Serialization.
  221. //===----------------------------------------------------------------------===//
  222. void FieldDecl::EmitImpl(Serializer& S) const {
  223. S.Emit(getType());
  224. NamedDecl::EmitInRec(S);
  225. S.EmitOwnedPtr(BitWidth);
  226. }
  227. FieldDecl* FieldDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  228. void *Mem = C.getAllocator().Allocate<FieldDecl>();
  229. FieldDecl* decl = new (Mem) FieldDecl(SourceLocation(), NULL, QualType(), 0);
  230. decl->DeclType.ReadBackpatch(D);
  231. decl->ReadInRec(D, C);
  232. decl->BitWidth = D.ReadOwnedPtr<Expr>(C);
  233. return decl;
  234. }
  235. //===----------------------------------------------------------------------===//
  236. // FunctionDecl Serialization.
  237. //===----------------------------------------------------------------------===//
  238. void FunctionDecl::EmitImpl(Serializer& S) const {
  239. S.EmitInt(SClass); // From FunctionDecl.
  240. S.EmitBool(IsInline); // From FunctionDecl.
  241. ValueDecl::EmitInRec(S);
  242. S.EmitPtr(DeclChain);
  243. // NOTE: We do not need to serialize out the number of parameters, because
  244. // that is encoded in the type (accessed via getNumParams()).
  245. if (ParamInfo != NULL) {
  246. S.EmitBool(true);
  247. S.BatchEmitOwnedPtrs(getNumParams(),&ParamInfo[0], Body,
  248. getNextDeclarator());
  249. }
  250. else {
  251. S.EmitBool(false);
  252. S.BatchEmitOwnedPtrs(Body,getNextDeclarator());
  253. }
  254. }
  255. FunctionDecl* FunctionDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  256. StorageClass SClass = static_cast<StorageClass>(D.ReadInt());
  257. bool IsInline = D.ReadBool();
  258. void *Mem = C.getAllocator().Allocate<FunctionDecl>();
  259. FunctionDecl* decl = new (Mem)
  260. FunctionDecl(0, SourceLocation(), NULL, QualType(), SClass, IsInline, 0);
  261. decl->ValueDecl::ReadInRec(D, C);
  262. D.ReadPtr(decl->DeclChain);
  263. Decl* next_declarator;
  264. bool hasParamDecls = D.ReadBool();
  265. decl->ParamInfo = hasParamDecls
  266. ? new ParmVarDecl*[decl->getNumParams()]
  267. : NULL;
  268. if (hasParamDecls)
  269. D.BatchReadOwnedPtrs(decl->getNumParams(),
  270. reinterpret_cast<Decl**>(&decl->ParamInfo[0]),
  271. decl->Body, next_declarator, C);
  272. else
  273. D.BatchReadOwnedPtrs(decl->Body, next_declarator, C);
  274. decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
  275. return decl;
  276. }
  277. //===----------------------------------------------------------------------===//
  278. // RecordDecl Serialization.
  279. //===----------------------------------------------------------------------===//
  280. void RecordDecl::EmitImpl(Serializer& S) const {
  281. ScopedDecl::EmitInRec(S);
  282. S.EmitBool(isDefinition());
  283. S.EmitBool(hasFlexibleArrayMember());
  284. S.EmitSInt(getNumMembers());
  285. if (getNumMembers() > 0) {
  286. assert (Members);
  287. S.BatchEmitOwnedPtrs((unsigned) getNumMembers(),
  288. (Decl**) &Members[0],getNextDeclarator());
  289. }
  290. else
  291. ScopedDecl::EmitOutRec(S);
  292. }
  293. RecordDecl* RecordDecl::CreateImpl(Decl::Kind DK, Deserializer& D,
  294. ASTContext& C) {
  295. void *Mem = C.getAllocator().Allocate<RecordDecl>();
  296. RecordDecl* decl = new (Mem) RecordDecl(DK, 0, SourceLocation(), NULL, NULL);
  297. decl->ScopedDecl::ReadInRec(D, C);
  298. decl->setDefinition(D.ReadBool());
  299. decl->setHasFlexibleArrayMember(D.ReadBool());
  300. decl->NumMembers = D.ReadSInt();
  301. if (decl->getNumMembers() > 0) {
  302. Decl* next_declarator;
  303. decl->Members = new FieldDecl*[(unsigned) decl->getNumMembers()];
  304. D.BatchReadOwnedPtrs((unsigned) decl->getNumMembers(),
  305. (Decl**) &decl->Members[0],
  306. next_declarator, C);
  307. decl->setNextDeclarator(cast_or_null<ScopedDecl>(next_declarator));
  308. }
  309. else
  310. decl->ScopedDecl::ReadOutRec(D, C);
  311. return decl;
  312. }
  313. //===----------------------------------------------------------------------===//
  314. // TypedefDecl Serialization.
  315. //===----------------------------------------------------------------------===//
  316. void TypedefDecl::EmitImpl(Serializer& S) const {
  317. S.Emit(UnderlyingType);
  318. ScopedDecl::EmitInRec(S);
  319. ScopedDecl::EmitOutRec(S);
  320. }
  321. TypedefDecl* TypedefDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  322. QualType T = QualType::ReadVal(D);
  323. void *Mem = C.getAllocator().Allocate<TypedefDecl>();
  324. TypedefDecl* decl = new (Mem) TypedefDecl(0, SourceLocation(), NULL, T, NULL);
  325. decl->ScopedDecl::ReadInRec(D, C);
  326. decl->ScopedDecl::ReadOutRec(D, C);
  327. return decl;
  328. }
  329. //===----------------------------------------------------------------------===//
  330. // LinkageSpec Serialization.
  331. //===----------------------------------------------------------------------===//
  332. void LinkageSpecDecl::EmitInRec(Serializer& S) const {
  333. Decl::EmitInRec(S);
  334. S.EmitInt(getLanguage());
  335. S.EmitPtr(D);
  336. }
  337. void LinkageSpecDecl::ReadInRec(Deserializer& D, ASTContext& C) {
  338. Decl::ReadInRec(D, C);
  339. Language = static_cast<LanguageIDs>(D.ReadInt());
  340. D.ReadPtr(this->D);
  341. }
  342. //===----------------------------------------------------------------------===//
  343. // FileScopeAsm Serialization.
  344. //===----------------------------------------------------------------------===//
  345. void FileScopeAsmDecl::EmitImpl(llvm::Serializer& S) const
  346. {
  347. Decl::EmitInRec(S);
  348. S.EmitOwnedPtr(AsmString);
  349. }
  350. FileScopeAsmDecl* FileScopeAsmDecl::CreateImpl(Deserializer& D, ASTContext& C) {
  351. void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
  352. FileScopeAsmDecl* decl = new (Mem) FileScopeAsmDecl(SourceLocation(), 0);
  353. decl->Decl::ReadInRec(D, C);
  354. decl->AsmString = cast<StringLiteral>(D.ReadOwnedPtr<Expr>(C));
  355. // D.ReadOwnedPtr(D.ReadOwnedPtr<StringLiteral>())<#T * * Ptr#>, <#bool AutoRegister#>)(decl->AsmString);
  356. return decl;
  357. }