BitcodeWriter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. //===-- BitcodeWriter.cpp - ClangDoc Bitcode Writer ------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "BitcodeWriter.h"
  9. #include "llvm/ADT/IndexedMap.h"
  10. #include <initializer_list>
  11. namespace clang {
  12. namespace doc {
  13. // Empty SymbolID for comparison, so we don't have to construct one every time.
  14. static const SymbolID EmptySID = SymbolID();
  15. // Since id enums are not zero-indexed, we need to transform the given id into
  16. // its associated index.
  17. struct BlockIdToIndexFunctor {
  18. using argument_type = unsigned;
  19. unsigned operator()(unsigned ID) const { return ID - BI_FIRST; }
  20. };
  21. struct RecordIdToIndexFunctor {
  22. using argument_type = unsigned;
  23. unsigned operator()(unsigned ID) const { return ID - RI_FIRST; }
  24. };
  25. using AbbrevDsc = void (*)(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev);
  26. static void AbbrevGen(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev,
  27. const std::initializer_list<llvm::BitCodeAbbrevOp> Ops) {
  28. for (const auto &Op : Ops)
  29. Abbrev->Add(Op);
  30. }
  31. static void BoolAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
  32. AbbrevGen(Abbrev,
  33. {// 0. Boolean
  34. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  35. BitCodeConstants::BoolSize)});
  36. }
  37. static void IntAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
  38. AbbrevGen(Abbrev,
  39. {// 0. Fixed-size integer
  40. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  41. BitCodeConstants::IntSize)});
  42. }
  43. static void SymbolIDAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
  44. AbbrevGen(Abbrev,
  45. {// 0. Fixed-size integer (length of the sha1'd USR)
  46. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  47. BitCodeConstants::USRLengthSize),
  48. // 1. Fixed-size array of Char6 (USR)
  49. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Array),
  50. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  51. BitCodeConstants::USRBitLengthSize)});
  52. }
  53. static void StringAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
  54. AbbrevGen(Abbrev,
  55. {// 0. Fixed-size integer (length of the following string)
  56. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  57. BitCodeConstants::StringLengthSize),
  58. // 1. The string blob
  59. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)});
  60. }
  61. // Assumes that the file will not have more than 65535 lines.
  62. static void LocationAbbrev(std::shared_ptr<llvm::BitCodeAbbrev> &Abbrev) {
  63. AbbrevGen(
  64. Abbrev,
  65. {// 0. Fixed-size integer (line number)
  66. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  67. BitCodeConstants::LineNumberSize),
  68. // 1. Boolean (IsFileInRootDir)
  69. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  70. BitCodeConstants::BoolSize),
  71. // 2. Fixed-size integer (length of the following string (filename))
  72. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed,
  73. BitCodeConstants::StringLengthSize),
  74. // 3. The string blob
  75. llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)});
  76. }
  77. struct RecordIdDsc {
  78. llvm::StringRef Name;
  79. AbbrevDsc Abbrev = nullptr;
  80. RecordIdDsc() = default;
  81. RecordIdDsc(llvm::StringRef Name, AbbrevDsc Abbrev)
  82. : Name(Name), Abbrev(Abbrev) {}
  83. // Is this 'description' valid?
  84. operator bool() const {
  85. return Abbrev != nullptr && Name.data() != nullptr && !Name.empty();
  86. }
  87. };
  88. static const llvm::IndexedMap<llvm::StringRef, BlockIdToIndexFunctor>
  89. BlockIdNameMap = []() {
  90. llvm::IndexedMap<llvm::StringRef, BlockIdToIndexFunctor> BlockIdNameMap;
  91. BlockIdNameMap.resize(BlockIdCount);
  92. // There is no init-list constructor for the IndexedMap, so have to
  93. // improvise
  94. static const std::vector<std::pair<BlockId, const char *const>> Inits = {
  95. {BI_VERSION_BLOCK_ID, "VersionBlock"},
  96. {BI_NAMESPACE_BLOCK_ID, "NamespaceBlock"},
  97. {BI_ENUM_BLOCK_ID, "EnumBlock"},
  98. {BI_TYPE_BLOCK_ID, "TypeBlock"},
  99. {BI_FIELD_TYPE_BLOCK_ID, "FieldTypeBlock"},
  100. {BI_MEMBER_TYPE_BLOCK_ID, "MemberTypeBlock"},
  101. {BI_RECORD_BLOCK_ID, "RecordBlock"},
  102. {BI_BASE_RECORD_BLOCK_ID, "BaseRecordBlock"},
  103. {BI_FUNCTION_BLOCK_ID, "FunctionBlock"},
  104. {BI_COMMENT_BLOCK_ID, "CommentBlock"},
  105. {BI_REFERENCE_BLOCK_ID, "ReferenceBlock"}};
  106. assert(Inits.size() == BlockIdCount);
  107. for (const auto &Init : Inits)
  108. BlockIdNameMap[Init.first] = Init.second;
  109. assert(BlockIdNameMap.size() == BlockIdCount);
  110. return BlockIdNameMap;
  111. }();
  112. static const llvm::IndexedMap<RecordIdDsc, RecordIdToIndexFunctor>
  113. RecordIdNameMap = []() {
  114. llvm::IndexedMap<RecordIdDsc, RecordIdToIndexFunctor> RecordIdNameMap;
  115. RecordIdNameMap.resize(RecordIdCount);
  116. // There is no init-list constructor for the IndexedMap, so have to
  117. // improvise
  118. static const std::vector<std::pair<RecordId, RecordIdDsc>> Inits = {
  119. {VERSION, {"Version", &IntAbbrev}},
  120. {COMMENT_KIND, {"Kind", &StringAbbrev}},
  121. {COMMENT_TEXT, {"Text", &StringAbbrev}},
  122. {COMMENT_NAME, {"Name", &StringAbbrev}},
  123. {COMMENT_DIRECTION, {"Direction", &StringAbbrev}},
  124. {COMMENT_PARAMNAME, {"ParamName", &StringAbbrev}},
  125. {COMMENT_CLOSENAME, {"CloseName", &StringAbbrev}},
  126. {COMMENT_SELFCLOSING, {"SelfClosing", &BoolAbbrev}},
  127. {COMMENT_EXPLICIT, {"Explicit", &BoolAbbrev}},
  128. {COMMENT_ATTRKEY, {"AttrKey", &StringAbbrev}},
  129. {COMMENT_ATTRVAL, {"AttrVal", &StringAbbrev}},
  130. {COMMENT_ARG, {"Arg", &StringAbbrev}},
  131. {FIELD_TYPE_NAME, {"Name", &StringAbbrev}},
  132. {MEMBER_TYPE_NAME, {"Name", &StringAbbrev}},
  133. {MEMBER_TYPE_ACCESS, {"Access", &IntAbbrev}},
  134. {NAMESPACE_USR, {"USR", &SymbolIDAbbrev}},
  135. {NAMESPACE_NAME, {"Name", &StringAbbrev}},
  136. {NAMESPACE_PATH, {"Path", &StringAbbrev}},
  137. {ENUM_USR, {"USR", &SymbolIDAbbrev}},
  138. {ENUM_NAME, {"Name", &StringAbbrev}},
  139. {ENUM_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
  140. {ENUM_LOCATION, {"Location", &LocationAbbrev}},
  141. {ENUM_MEMBER, {"Member", &StringAbbrev}},
  142. {ENUM_SCOPED, {"Scoped", &BoolAbbrev}},
  143. {RECORD_USR, {"USR", &SymbolIDAbbrev}},
  144. {RECORD_NAME, {"Name", &StringAbbrev}},
  145. {RECORD_PATH, {"Path", &StringAbbrev}},
  146. {RECORD_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
  147. {RECORD_LOCATION, {"Location", &LocationAbbrev}},
  148. {RECORD_TAG_TYPE, {"TagType", &IntAbbrev}},
  149. {RECORD_IS_TYPE_DEF, {"IsTypeDef", &BoolAbbrev}},
  150. {BASE_RECORD_USR, {"USR", &SymbolIDAbbrev}},
  151. {BASE_RECORD_NAME, {"Name", &StringAbbrev}},
  152. {BASE_RECORD_PATH, {"Path", &StringAbbrev}},
  153. {BASE_RECORD_TAG_TYPE, {"TagType", &IntAbbrev}},
  154. {BASE_RECORD_IS_VIRTUAL, {"IsVirtual", &BoolAbbrev}},
  155. {BASE_RECORD_ACCESS, {"Access", &IntAbbrev}},
  156. {BASE_RECORD_IS_PARENT, {"IsParent", &BoolAbbrev}},
  157. {FUNCTION_USR, {"USR", &SymbolIDAbbrev}},
  158. {FUNCTION_NAME, {"Name", &StringAbbrev}},
  159. {FUNCTION_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
  160. {FUNCTION_LOCATION, {"Location", &LocationAbbrev}},
  161. {FUNCTION_ACCESS, {"Access", &IntAbbrev}},
  162. {FUNCTION_IS_METHOD, {"IsMethod", &BoolAbbrev}},
  163. {REFERENCE_USR, {"USR", &SymbolIDAbbrev}},
  164. {REFERENCE_NAME, {"Name", &StringAbbrev}},
  165. {REFERENCE_TYPE, {"RefType", &IntAbbrev}},
  166. {REFERENCE_PATH, {"Path", &StringAbbrev}},
  167. {REFERENCE_IS_IN_GLOBAL_NAMESPACE,
  168. {"IsInGlobalNamespace", &BoolAbbrev}},
  169. {REFERENCE_FIELD, {"Field", &IntAbbrev}}};
  170. assert(Inits.size() == RecordIdCount);
  171. for (const auto &Init : Inits) {
  172. RecordIdNameMap[Init.first] = Init.second;
  173. assert((Init.second.Name.size() + 1) <= BitCodeConstants::RecordSize);
  174. }
  175. assert(RecordIdNameMap.size() == RecordIdCount);
  176. return RecordIdNameMap;
  177. }();
  178. static const std::vector<std::pair<BlockId, std::vector<RecordId>>>
  179. RecordsByBlock{
  180. // Version Block
  181. {BI_VERSION_BLOCK_ID, {VERSION}},
  182. // Comment Block
  183. {BI_COMMENT_BLOCK_ID,
  184. {COMMENT_KIND, COMMENT_TEXT, COMMENT_NAME, COMMENT_DIRECTION,
  185. COMMENT_PARAMNAME, COMMENT_CLOSENAME, COMMENT_SELFCLOSING,
  186. COMMENT_EXPLICIT, COMMENT_ATTRKEY, COMMENT_ATTRVAL, COMMENT_ARG}},
  187. // Type Block
  188. {BI_TYPE_BLOCK_ID, {}},
  189. // FieldType Block
  190. {BI_FIELD_TYPE_BLOCK_ID, {FIELD_TYPE_NAME}},
  191. // MemberType Block
  192. {BI_MEMBER_TYPE_BLOCK_ID, {MEMBER_TYPE_NAME, MEMBER_TYPE_ACCESS}},
  193. // Enum Block
  194. {BI_ENUM_BLOCK_ID,
  195. {ENUM_USR, ENUM_NAME, ENUM_DEFLOCATION, ENUM_LOCATION, ENUM_MEMBER,
  196. ENUM_SCOPED}},
  197. // Namespace Block
  198. {BI_NAMESPACE_BLOCK_ID,
  199. {NAMESPACE_USR, NAMESPACE_NAME, NAMESPACE_PATH}},
  200. // Record Block
  201. {BI_RECORD_BLOCK_ID,
  202. {RECORD_USR, RECORD_NAME, RECORD_PATH, RECORD_DEFLOCATION,
  203. RECORD_LOCATION, RECORD_TAG_TYPE, RECORD_IS_TYPE_DEF}},
  204. // BaseRecord Block
  205. {BI_BASE_RECORD_BLOCK_ID,
  206. {BASE_RECORD_USR, BASE_RECORD_NAME, BASE_RECORD_PATH,
  207. BASE_RECORD_TAG_TYPE, BASE_RECORD_IS_VIRTUAL, BASE_RECORD_ACCESS,
  208. BASE_RECORD_IS_PARENT}},
  209. // Function Block
  210. {BI_FUNCTION_BLOCK_ID,
  211. {FUNCTION_USR, FUNCTION_NAME, FUNCTION_DEFLOCATION, FUNCTION_LOCATION,
  212. FUNCTION_ACCESS, FUNCTION_IS_METHOD}},
  213. // Reference Block
  214. {BI_REFERENCE_BLOCK_ID,
  215. {REFERENCE_USR, REFERENCE_NAME, REFERENCE_TYPE, REFERENCE_PATH,
  216. REFERENCE_IS_IN_GLOBAL_NAMESPACE, REFERENCE_FIELD}}};
  217. // AbbreviationMap
  218. constexpr unsigned char BitCodeConstants::Signature[];
  219. void ClangDocBitcodeWriter::AbbreviationMap::add(RecordId RID,
  220. unsigned AbbrevID) {
  221. assert(RecordIdNameMap[RID] && "Unknown RecordId.");
  222. assert(Abbrevs.find(RID) == Abbrevs.end() && "Abbreviation already added.");
  223. Abbrevs[RID] = AbbrevID;
  224. }
  225. unsigned ClangDocBitcodeWriter::AbbreviationMap::get(RecordId RID) const {
  226. assert(RecordIdNameMap[RID] && "Unknown RecordId.");
  227. assert(Abbrevs.find(RID) != Abbrevs.end() && "Unknown abbreviation.");
  228. return Abbrevs.lookup(RID);
  229. }
  230. // Validation and Overview Blocks
  231. /// Emits the magic number header to check that its the right format,
  232. /// in this case, 'DOCS'.
  233. void ClangDocBitcodeWriter::emitHeader() {
  234. for (char C : BitCodeConstants::Signature)
  235. Stream.Emit((unsigned)C, BitCodeConstants::SignatureBitSize);
  236. }
  237. void ClangDocBitcodeWriter::emitVersionBlock() {
  238. StreamSubBlockGuard Block(Stream, BI_VERSION_BLOCK_ID);
  239. emitRecord(VersionNumber, VERSION);
  240. }
  241. /// Emits a block ID and the block name to the BLOCKINFO block.
  242. void ClangDocBitcodeWriter::emitBlockID(BlockId BID) {
  243. const auto &BlockIdName = BlockIdNameMap[BID];
  244. assert(BlockIdName.data() && BlockIdName.size() && "Unknown BlockId.");
  245. Record.clear();
  246. Record.push_back(BID);
  247. Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
  248. Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME,
  249. ArrayRef<unsigned char>(BlockIdName.bytes_begin(),
  250. BlockIdName.bytes_end()));
  251. }
  252. /// Emits a record name to the BLOCKINFO block.
  253. void ClangDocBitcodeWriter::emitRecordID(RecordId ID) {
  254. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  255. prepRecordData(ID);
  256. Record.append(RecordIdNameMap[ID].Name.begin(),
  257. RecordIdNameMap[ID].Name.end());
  258. Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
  259. }
  260. // Abbreviations
  261. void ClangDocBitcodeWriter::emitAbbrev(RecordId ID, BlockId Block) {
  262. assert(RecordIdNameMap[ID] && "Unknown abbreviation.");
  263. auto Abbrev = std::make_shared<llvm::BitCodeAbbrev>();
  264. Abbrev->Add(llvm::BitCodeAbbrevOp(ID));
  265. RecordIdNameMap[ID].Abbrev(Abbrev);
  266. Abbrevs.add(ID, Stream.EmitBlockInfoAbbrev(Block, std::move(Abbrev)));
  267. }
  268. // Records
  269. void ClangDocBitcodeWriter::emitRecord(const SymbolID &Sym, RecordId ID) {
  270. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  271. assert(RecordIdNameMap[ID].Abbrev == &SymbolIDAbbrev &&
  272. "Abbrev type mismatch.");
  273. if (!prepRecordData(ID, Sym != EmptySID))
  274. return;
  275. assert(Sym.size() == 20);
  276. Record.push_back(Sym.size());
  277. Record.append(Sym.begin(), Sym.end());
  278. Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
  279. }
  280. void ClangDocBitcodeWriter::emitRecord(llvm::StringRef Str, RecordId ID) {
  281. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  282. assert(RecordIdNameMap[ID].Abbrev == &StringAbbrev &&
  283. "Abbrev type mismatch.");
  284. if (!prepRecordData(ID, !Str.empty()))
  285. return;
  286. assert(Str.size() < (1U << BitCodeConstants::StringLengthSize));
  287. Record.push_back(Str.size());
  288. Stream.EmitRecordWithBlob(Abbrevs.get(ID), Record, Str);
  289. }
  290. void ClangDocBitcodeWriter::emitRecord(const Location &Loc, RecordId ID) {
  291. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  292. assert(RecordIdNameMap[ID].Abbrev == &LocationAbbrev &&
  293. "Abbrev type mismatch.");
  294. if (!prepRecordData(ID, true))
  295. return;
  296. // FIXME: Assert that the line number is of the appropriate size.
  297. Record.push_back(Loc.LineNumber);
  298. assert(Loc.Filename.size() < (1U << BitCodeConstants::StringLengthSize));
  299. Record.push_back(Loc.IsFileInRootDir);
  300. Record.push_back(Loc.Filename.size());
  301. Stream.EmitRecordWithBlob(Abbrevs.get(ID), Record, Loc.Filename);
  302. }
  303. void ClangDocBitcodeWriter::emitRecord(bool Val, RecordId ID) {
  304. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  305. assert(RecordIdNameMap[ID].Abbrev == &BoolAbbrev && "Abbrev type mismatch.");
  306. if (!prepRecordData(ID, Val))
  307. return;
  308. Record.push_back(Val);
  309. Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
  310. }
  311. void ClangDocBitcodeWriter::emitRecord(int Val, RecordId ID) {
  312. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  313. assert(RecordIdNameMap[ID].Abbrev == &IntAbbrev && "Abbrev type mismatch.");
  314. if (!prepRecordData(ID, Val))
  315. return;
  316. // FIXME: Assert that the integer is of the appropriate size.
  317. Record.push_back(Val);
  318. Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
  319. }
  320. void ClangDocBitcodeWriter::emitRecord(unsigned Val, RecordId ID) {
  321. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  322. assert(RecordIdNameMap[ID].Abbrev == &IntAbbrev && "Abbrev type mismatch.");
  323. if (!prepRecordData(ID, Val))
  324. return;
  325. assert(Val < (1U << BitCodeConstants::IntSize));
  326. Record.push_back(Val);
  327. Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
  328. }
  329. bool ClangDocBitcodeWriter::prepRecordData(RecordId ID, bool ShouldEmit) {
  330. assert(RecordIdNameMap[ID] && "Unknown RecordId.");
  331. if (!ShouldEmit)
  332. return false;
  333. Record.clear();
  334. Record.push_back(ID);
  335. return true;
  336. }
  337. // BlockInfo Block
  338. void ClangDocBitcodeWriter::emitBlockInfoBlock() {
  339. Stream.EnterBlockInfoBlock();
  340. for (const auto &Block : RecordsByBlock) {
  341. assert(Block.second.size() < (1U << BitCodeConstants::SubblockIDSize));
  342. emitBlockInfo(Block.first, Block.second);
  343. }
  344. Stream.ExitBlock();
  345. }
  346. void ClangDocBitcodeWriter::emitBlockInfo(BlockId BID,
  347. const std::vector<RecordId> &RIDs) {
  348. assert(RIDs.size() < (1U << BitCodeConstants::SubblockIDSize));
  349. emitBlockID(BID);
  350. for (RecordId RID : RIDs) {
  351. emitRecordID(RID);
  352. emitAbbrev(RID, BID);
  353. }
  354. }
  355. // Block emission
  356. void ClangDocBitcodeWriter::emitBlock(const Reference &R, FieldId Field) {
  357. if (R.USR == EmptySID && R.Name.empty())
  358. return;
  359. StreamSubBlockGuard Block(Stream, BI_REFERENCE_BLOCK_ID);
  360. emitRecord(R.USR, REFERENCE_USR);
  361. emitRecord(R.Name, REFERENCE_NAME);
  362. emitRecord((unsigned)R.RefType, REFERENCE_TYPE);
  363. emitRecord(R.Path, REFERENCE_PATH);
  364. emitRecord(R.IsInGlobalNamespace, REFERENCE_IS_IN_GLOBAL_NAMESPACE);
  365. emitRecord((unsigned)Field, REFERENCE_FIELD);
  366. }
  367. void ClangDocBitcodeWriter::emitBlock(const TypeInfo &T) {
  368. StreamSubBlockGuard Block(Stream, BI_TYPE_BLOCK_ID);
  369. emitBlock(T.Type, FieldId::F_type);
  370. }
  371. void ClangDocBitcodeWriter::emitBlock(const FieldTypeInfo &T) {
  372. StreamSubBlockGuard Block(Stream, BI_FIELD_TYPE_BLOCK_ID);
  373. emitBlock(T.Type, FieldId::F_type);
  374. emitRecord(T.Name, FIELD_TYPE_NAME);
  375. }
  376. void ClangDocBitcodeWriter::emitBlock(const MemberTypeInfo &T) {
  377. StreamSubBlockGuard Block(Stream, BI_MEMBER_TYPE_BLOCK_ID);
  378. emitBlock(T.Type, FieldId::F_type);
  379. emitRecord(T.Name, MEMBER_TYPE_NAME);
  380. emitRecord(T.Access, MEMBER_TYPE_ACCESS);
  381. }
  382. void ClangDocBitcodeWriter::emitBlock(const CommentInfo &I) {
  383. StreamSubBlockGuard Block(Stream, BI_COMMENT_BLOCK_ID);
  384. for (const auto &L : std::vector<std::pair<llvm::StringRef, RecordId>>{
  385. {I.Kind, COMMENT_KIND},
  386. {I.Text, COMMENT_TEXT},
  387. {I.Name, COMMENT_NAME},
  388. {I.Direction, COMMENT_DIRECTION},
  389. {I.ParamName, COMMENT_PARAMNAME},
  390. {I.CloseName, COMMENT_CLOSENAME}})
  391. emitRecord(L.first, L.second);
  392. emitRecord(I.SelfClosing, COMMENT_SELFCLOSING);
  393. emitRecord(I.Explicit, COMMENT_EXPLICIT);
  394. for (const auto &A : I.AttrKeys)
  395. emitRecord(A, COMMENT_ATTRKEY);
  396. for (const auto &A : I.AttrValues)
  397. emitRecord(A, COMMENT_ATTRVAL);
  398. for (const auto &A : I.Args)
  399. emitRecord(A, COMMENT_ARG);
  400. for (const auto &C : I.Children)
  401. emitBlock(*C);
  402. }
  403. void ClangDocBitcodeWriter::emitBlock(const NamespaceInfo &I) {
  404. StreamSubBlockGuard Block(Stream, BI_NAMESPACE_BLOCK_ID);
  405. emitRecord(I.USR, NAMESPACE_USR);
  406. emitRecord(I.Name, NAMESPACE_NAME);
  407. emitRecord(I.Path, NAMESPACE_PATH);
  408. for (const auto &N : I.Namespace)
  409. emitBlock(N, FieldId::F_namespace);
  410. for (const auto &CI : I.Description)
  411. emitBlock(CI);
  412. for (const auto &C : I.ChildNamespaces)
  413. emitBlock(C, FieldId::F_child_namespace);
  414. for (const auto &C : I.ChildRecords)
  415. emitBlock(C, FieldId::F_child_record);
  416. for (const auto &C : I.ChildFunctions)
  417. emitBlock(C);
  418. for (const auto &C : I.ChildEnums)
  419. emitBlock(C);
  420. }
  421. void ClangDocBitcodeWriter::emitBlock(const EnumInfo &I) {
  422. StreamSubBlockGuard Block(Stream, BI_ENUM_BLOCK_ID);
  423. emitRecord(I.USR, ENUM_USR);
  424. emitRecord(I.Name, ENUM_NAME);
  425. for (const auto &N : I.Namespace)
  426. emitBlock(N, FieldId::F_namespace);
  427. for (const auto &CI : I.Description)
  428. emitBlock(CI);
  429. if (I.DefLoc)
  430. emitRecord(I.DefLoc.getValue(), ENUM_DEFLOCATION);
  431. for (const auto &L : I.Loc)
  432. emitRecord(L, ENUM_LOCATION);
  433. emitRecord(I.Scoped, ENUM_SCOPED);
  434. for (const auto &N : I.Members)
  435. emitRecord(N, ENUM_MEMBER);
  436. }
  437. void ClangDocBitcodeWriter::emitBlock(const RecordInfo &I) {
  438. StreamSubBlockGuard Block(Stream, BI_RECORD_BLOCK_ID);
  439. emitRecord(I.USR, RECORD_USR);
  440. emitRecord(I.Name, RECORD_NAME);
  441. emitRecord(I.Path, RECORD_PATH);
  442. for (const auto &N : I.Namespace)
  443. emitBlock(N, FieldId::F_namespace);
  444. for (const auto &CI : I.Description)
  445. emitBlock(CI);
  446. if (I.DefLoc)
  447. emitRecord(I.DefLoc.getValue(), RECORD_DEFLOCATION);
  448. for (const auto &L : I.Loc)
  449. emitRecord(L, RECORD_LOCATION);
  450. emitRecord(I.TagType, RECORD_TAG_TYPE);
  451. emitRecord(I.IsTypeDef, RECORD_IS_TYPE_DEF);
  452. for (const auto &N : I.Members)
  453. emitBlock(N);
  454. for (const auto &P : I.Parents)
  455. emitBlock(P, FieldId::F_parent);
  456. for (const auto &P : I.VirtualParents)
  457. emitBlock(P, FieldId::F_vparent);
  458. for (const auto &PB : I.Bases)
  459. emitBlock(PB);
  460. for (const auto &C : I.ChildRecords)
  461. emitBlock(C, FieldId::F_child_record);
  462. for (const auto &C : I.ChildFunctions)
  463. emitBlock(C);
  464. for (const auto &C : I.ChildEnums)
  465. emitBlock(C);
  466. }
  467. void ClangDocBitcodeWriter::emitBlock(const BaseRecordInfo &I) {
  468. StreamSubBlockGuard Block(Stream, BI_BASE_RECORD_BLOCK_ID);
  469. emitRecord(I.USR, BASE_RECORD_USR);
  470. emitRecord(I.Name, BASE_RECORD_NAME);
  471. emitRecord(I.Path, BASE_RECORD_PATH);
  472. emitRecord(I.TagType, BASE_RECORD_TAG_TYPE);
  473. emitRecord(I.IsVirtual, BASE_RECORD_IS_VIRTUAL);
  474. emitRecord(I.Access, BASE_RECORD_ACCESS);
  475. emitRecord(I.IsParent, BASE_RECORD_IS_PARENT);
  476. for (const auto &M : I.Members)
  477. emitBlock(M);
  478. for (const auto &C : I.ChildFunctions)
  479. emitBlock(C);
  480. }
  481. void ClangDocBitcodeWriter::emitBlock(const FunctionInfo &I) {
  482. StreamSubBlockGuard Block(Stream, BI_FUNCTION_BLOCK_ID);
  483. emitRecord(I.USR, FUNCTION_USR);
  484. emitRecord(I.Name, FUNCTION_NAME);
  485. for (const auto &N : I.Namespace)
  486. emitBlock(N, FieldId::F_namespace);
  487. for (const auto &CI : I.Description)
  488. emitBlock(CI);
  489. emitRecord(I.Access, FUNCTION_ACCESS);
  490. emitRecord(I.IsMethod, FUNCTION_IS_METHOD);
  491. if (I.DefLoc)
  492. emitRecord(I.DefLoc.getValue(), FUNCTION_DEFLOCATION);
  493. for (const auto &L : I.Loc)
  494. emitRecord(L, FUNCTION_LOCATION);
  495. emitBlock(I.Parent, FieldId::F_parent);
  496. emitBlock(I.ReturnType);
  497. for (const auto &N : I.Params)
  498. emitBlock(N);
  499. }
  500. bool ClangDocBitcodeWriter::dispatchInfoForWrite(Info *I) {
  501. switch (I->IT) {
  502. case InfoType::IT_namespace:
  503. emitBlock(*static_cast<clang::doc::NamespaceInfo *>(I));
  504. break;
  505. case InfoType::IT_record:
  506. emitBlock(*static_cast<clang::doc::RecordInfo *>(I));
  507. break;
  508. case InfoType::IT_enum:
  509. emitBlock(*static_cast<clang::doc::EnumInfo *>(I));
  510. break;
  511. case InfoType::IT_function:
  512. emitBlock(*static_cast<clang::doc::FunctionInfo *>(I));
  513. break;
  514. default:
  515. llvm::errs() << "Unexpected info, unable to write.\n";
  516. return true;
  517. }
  518. return false;
  519. }
  520. } // namespace doc
  521. } // namespace clang