1139f7f9bSDimitry Andric //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
2139f7f9bSDimitry Andric //
3139f7f9bSDimitry Andric //                     The LLVM Compiler Infrastructure
4139f7f9bSDimitry Andric //
5139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7139f7f9bSDimitry Andric //
8139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9139f7f9bSDimitry Andric 
10139f7f9bSDimitry Andric #include "llvm/Bitcode/BitstreamReader.h"
11*d88c1a5aSDimitry Andric #include "llvm/ADT/StringRef.h"
12*d88c1a5aSDimitry Andric #include <cassert>
13*d88c1a5aSDimitry Andric #include <string>
14139f7f9bSDimitry Andric 
15139f7f9bSDimitry Andric using namespace llvm;
16139f7f9bSDimitry Andric 
17139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
18139f7f9bSDimitry Andric //  BitstreamCursor implementation
19139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
20139f7f9bSDimitry Andric 
21139f7f9bSDimitry Andric /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
22139f7f9bSDimitry Andric /// the block, and return true if the block has an error.
23139f7f9bSDimitry Andric bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
24139f7f9bSDimitry Andric   // Save the current block's state on BlockScope.
25139f7f9bSDimitry Andric   BlockScope.push_back(Block(CurCodeSize));
26139f7f9bSDimitry Andric   BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
27139f7f9bSDimitry Andric 
28139f7f9bSDimitry Andric   // Add the abbrevs specific to this block to the CurAbbrevs list.
29*d88c1a5aSDimitry Andric   if (BlockInfo) {
30*d88c1a5aSDimitry Andric     if (const BitstreamBlockInfo::BlockInfo *Info =
31*d88c1a5aSDimitry Andric             BlockInfo->getBlockInfo(BlockID)) {
3239d628a0SDimitry Andric       CurAbbrevs.insert(CurAbbrevs.end(), Info->Abbrevs.begin(),
3339d628a0SDimitry Andric                         Info->Abbrevs.end());
34139f7f9bSDimitry Andric     }
35*d88c1a5aSDimitry Andric   }
36139f7f9bSDimitry Andric 
37139f7f9bSDimitry Andric   // Get the codesize of this block.
38139f7f9bSDimitry Andric   CurCodeSize = ReadVBR(bitc::CodeLenWidth);
39ff0cc061SDimitry Andric   // We can't read more than MaxChunkSize at a time
40ff0cc061SDimitry Andric   if (CurCodeSize > MaxChunkSize)
41ff0cc061SDimitry Andric     return true;
42ff0cc061SDimitry Andric 
43139f7f9bSDimitry Andric   SkipToFourByteBoundary();
44139f7f9bSDimitry Andric   unsigned NumWords = Read(bitc::BlockSizeWidth);
45139f7f9bSDimitry Andric   if (NumWordsP) *NumWordsP = NumWords;
46139f7f9bSDimitry Andric 
47139f7f9bSDimitry Andric   // Validate that this block is sane.
48ff0cc061SDimitry Andric   return CurCodeSize == 0 || AtEndOfStream();
49139f7f9bSDimitry Andric }
50139f7f9bSDimitry Andric 
5139d628a0SDimitry Andric static uint64_t readAbbreviatedField(BitstreamCursor &Cursor,
5239d628a0SDimitry Andric                                      const BitCodeAbbrevOp &Op) {
5339d628a0SDimitry Andric   assert(!Op.isLiteral() && "Not to be used with literals!");
54139f7f9bSDimitry Andric 
55139f7f9bSDimitry Andric   // Decode the value as we are commanded.
56139f7f9bSDimitry Andric   switch (Op.getEncoding()) {
57139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Array:
58139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Blob:
5991bc56edSDimitry Andric     llvm_unreachable("Should not reach here");
60139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Fixed:
61ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
6239d628a0SDimitry Andric     return Cursor.Read((unsigned)Op.getEncodingData());
63139f7f9bSDimitry Andric   case BitCodeAbbrevOp::VBR:
64ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
6539d628a0SDimitry Andric     return Cursor.ReadVBR64((unsigned)Op.getEncodingData());
66139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Char6:
6739d628a0SDimitry Andric     return BitCodeAbbrevOp::DecodeChar6(Cursor.Read(6));
68139f7f9bSDimitry Andric   }
6939d628a0SDimitry Andric   llvm_unreachable("invalid abbreviation encoding");
70139f7f9bSDimitry Andric }
71139f7f9bSDimitry Andric 
7239d628a0SDimitry Andric static void skipAbbreviatedField(BitstreamCursor &Cursor,
7339d628a0SDimitry Andric                                  const BitCodeAbbrevOp &Op) {
7439d628a0SDimitry Andric   assert(!Op.isLiteral() && "Not to be used with literals!");
75139f7f9bSDimitry Andric 
76139f7f9bSDimitry Andric   // Decode the value as we are commanded.
77139f7f9bSDimitry Andric   switch (Op.getEncoding()) {
78139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Array:
79139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Blob:
8091bc56edSDimitry Andric     llvm_unreachable("Should not reach here");
81139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Fixed:
82ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
8339d628a0SDimitry Andric     Cursor.Read((unsigned)Op.getEncodingData());
84139f7f9bSDimitry Andric     break;
85139f7f9bSDimitry Andric   case BitCodeAbbrevOp::VBR:
86ff0cc061SDimitry Andric     assert((unsigned)Op.getEncodingData() <= Cursor.MaxChunkSize);
8739d628a0SDimitry Andric     Cursor.ReadVBR64((unsigned)Op.getEncodingData());
88139f7f9bSDimitry Andric     break;
89139f7f9bSDimitry Andric   case BitCodeAbbrevOp::Char6:
9039d628a0SDimitry Andric     Cursor.Read(6);
91139f7f9bSDimitry Andric     break;
92139f7f9bSDimitry Andric   }
93139f7f9bSDimitry Andric }
94139f7f9bSDimitry Andric 
95139f7f9bSDimitry Andric /// skipRecord - Read the current record and discard it.
96139f7f9bSDimitry Andric void BitstreamCursor::skipRecord(unsigned AbbrevID) {
97139f7f9bSDimitry Andric   // Skip unabbreviated records by reading past their entries.
98139f7f9bSDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
99139f7f9bSDimitry Andric     unsigned Code = ReadVBR(6);
100139f7f9bSDimitry Andric     (void)Code;
101139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
102139f7f9bSDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
103139f7f9bSDimitry Andric       (void)ReadVBR64(6);
104139f7f9bSDimitry Andric     return;
105139f7f9bSDimitry Andric   }
106139f7f9bSDimitry Andric 
107139f7f9bSDimitry Andric   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
108139f7f9bSDimitry Andric 
109139f7f9bSDimitry Andric   for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
110139f7f9bSDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
111139f7f9bSDimitry Andric     if (Op.isLiteral())
112139f7f9bSDimitry Andric       continue;
113139f7f9bSDimitry Andric 
114139f7f9bSDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
115139f7f9bSDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
11639d628a0SDimitry Andric       skipAbbreviatedField(*this, Op);
117139f7f9bSDimitry Andric       continue;
118139f7f9bSDimitry Andric     }
119139f7f9bSDimitry Andric 
120139f7f9bSDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
121139f7f9bSDimitry Andric       // Array case.  Read the number of elements as a vbr6.
122139f7f9bSDimitry Andric       unsigned NumElts = ReadVBR(6);
123139f7f9bSDimitry Andric 
124139f7f9bSDimitry Andric       // Get the element encoding.
125139f7f9bSDimitry Andric       assert(i+2 == e && "array op not second to last?");
126139f7f9bSDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
127139f7f9bSDimitry Andric 
128139f7f9bSDimitry Andric       // Read all the elements.
1293ca95b02SDimitry Andric       // Decode the value as we are commanded.
1303ca95b02SDimitry Andric       switch (EltEnc.getEncoding()) {
1313ca95b02SDimitry Andric       default:
1323ca95b02SDimitry Andric         report_fatal_error("Array element type can't be an Array or a Blob");
1333ca95b02SDimitry Andric       case BitCodeAbbrevOp::Fixed:
134*d88c1a5aSDimitry Andric         assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
135*d88c1a5aSDimitry Andric         JumpToBit(GetCurrentBitNo() + NumElts * EltEnc.getEncodingData());
1363ca95b02SDimitry Andric         break;
1373ca95b02SDimitry Andric       case BitCodeAbbrevOp::VBR:
138*d88c1a5aSDimitry Andric         assert((unsigned)EltEnc.getEncodingData() <= MaxChunkSize);
1393ca95b02SDimitry Andric         for (; NumElts; --NumElts)
1403ca95b02SDimitry Andric           ReadVBR64((unsigned)EltEnc.getEncodingData());
1413ca95b02SDimitry Andric         break;
1423ca95b02SDimitry Andric       case BitCodeAbbrevOp::Char6:
143*d88c1a5aSDimitry Andric         JumpToBit(GetCurrentBitNo() + NumElts * 6);
1443ca95b02SDimitry Andric         break;
1453ca95b02SDimitry Andric       }
146139f7f9bSDimitry Andric       continue;
147139f7f9bSDimitry Andric     }
148139f7f9bSDimitry Andric 
149139f7f9bSDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
150139f7f9bSDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
151139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
152139f7f9bSDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
153139f7f9bSDimitry Andric 
154139f7f9bSDimitry Andric     // Figure out where the end of this blob will be including tail padding.
155139f7f9bSDimitry Andric     size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
156139f7f9bSDimitry Andric 
157139f7f9bSDimitry Andric     // If this would read off the end of the bitcode file, just set the
158139f7f9bSDimitry Andric     // record to empty and return.
159139f7f9bSDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
1603ca95b02SDimitry Andric       skipToEnd();
161139f7f9bSDimitry Andric       break;
162139f7f9bSDimitry Andric     }
163139f7f9bSDimitry Andric 
164139f7f9bSDimitry Andric     // Skip over the blob.
165139f7f9bSDimitry Andric     JumpToBit(NewEnd);
166139f7f9bSDimitry Andric   }
167139f7f9bSDimitry Andric }
168139f7f9bSDimitry Andric 
169139f7f9bSDimitry Andric unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
170139f7f9bSDimitry Andric                                      SmallVectorImpl<uint64_t> &Vals,
171139f7f9bSDimitry Andric                                      StringRef *Blob) {
172139f7f9bSDimitry Andric   if (AbbrevID == bitc::UNABBREV_RECORD) {
173139f7f9bSDimitry Andric     unsigned Code = ReadVBR(6);
174139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
175139f7f9bSDimitry Andric     for (unsigned i = 0; i != NumElts; ++i)
176139f7f9bSDimitry Andric       Vals.push_back(ReadVBR64(6));
177139f7f9bSDimitry Andric     return Code;
178139f7f9bSDimitry Andric   }
179139f7f9bSDimitry Andric 
180139f7f9bSDimitry Andric   const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
181139f7f9bSDimitry Andric 
182f785676fSDimitry Andric   // Read the record code first.
183f785676fSDimitry Andric   assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
184f785676fSDimitry Andric   const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
18539d628a0SDimitry Andric   unsigned Code;
186f785676fSDimitry Andric   if (CodeOp.isLiteral())
18739d628a0SDimitry Andric     Code = CodeOp.getLiteralValue();
188ff0cc061SDimitry Andric   else {
189ff0cc061SDimitry Andric     if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array ||
190ff0cc061SDimitry Andric         CodeOp.getEncoding() == BitCodeAbbrevOp::Blob)
191ff0cc061SDimitry Andric       report_fatal_error("Abbreviation starts with an Array or a Blob");
19239d628a0SDimitry Andric     Code = readAbbreviatedField(*this, CodeOp);
193ff0cc061SDimitry Andric   }
194f785676fSDimitry Andric 
195f785676fSDimitry Andric   for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
196139f7f9bSDimitry Andric     const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
197139f7f9bSDimitry Andric     if (Op.isLiteral()) {
19839d628a0SDimitry Andric       Vals.push_back(Op.getLiteralValue());
199139f7f9bSDimitry Andric       continue;
200139f7f9bSDimitry Andric     }
201139f7f9bSDimitry Andric 
202139f7f9bSDimitry Andric     if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
203139f7f9bSDimitry Andric         Op.getEncoding() != BitCodeAbbrevOp::Blob) {
20439d628a0SDimitry Andric       Vals.push_back(readAbbreviatedField(*this, Op));
205139f7f9bSDimitry Andric       continue;
206139f7f9bSDimitry Andric     }
207139f7f9bSDimitry Andric 
208139f7f9bSDimitry Andric     if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
209139f7f9bSDimitry Andric       // Array case.  Read the number of elements as a vbr6.
210139f7f9bSDimitry Andric       unsigned NumElts = ReadVBR(6);
211139f7f9bSDimitry Andric 
212139f7f9bSDimitry Andric       // Get the element encoding.
213ff0cc061SDimitry Andric       if (i + 2 != e)
214ff0cc061SDimitry Andric         report_fatal_error("Array op not second to last");
215139f7f9bSDimitry Andric       const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
216ff0cc061SDimitry Andric       if (!EltEnc.isEncoding())
217ff0cc061SDimitry Andric         report_fatal_error(
218ff0cc061SDimitry Andric             "Array element type has to be an encoding of a type");
219139f7f9bSDimitry Andric 
220139f7f9bSDimitry Andric       // Read all the elements.
2213ca95b02SDimitry Andric       switch (EltEnc.getEncoding()) {
2223ca95b02SDimitry Andric       default:
2233ca95b02SDimitry Andric         report_fatal_error("Array element type can't be an Array or a Blob");
2243ca95b02SDimitry Andric       case BitCodeAbbrevOp::Fixed:
225139f7f9bSDimitry Andric         for (; NumElts; --NumElts)
2263ca95b02SDimitry Andric           Vals.push_back(Read((unsigned)EltEnc.getEncodingData()));
2273ca95b02SDimitry Andric         break;
2283ca95b02SDimitry Andric       case BitCodeAbbrevOp::VBR:
2293ca95b02SDimitry Andric         for (; NumElts; --NumElts)
2303ca95b02SDimitry Andric           Vals.push_back(ReadVBR64((unsigned)EltEnc.getEncodingData()));
2313ca95b02SDimitry Andric         break;
2323ca95b02SDimitry Andric       case BitCodeAbbrevOp::Char6:
2333ca95b02SDimitry Andric         for (; NumElts; --NumElts)
2343ca95b02SDimitry Andric           Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
2353ca95b02SDimitry Andric       }
236139f7f9bSDimitry Andric       continue;
237139f7f9bSDimitry Andric     }
238139f7f9bSDimitry Andric 
239139f7f9bSDimitry Andric     assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
240139f7f9bSDimitry Andric     // Blob case.  Read the number of bytes as a vbr6.
241139f7f9bSDimitry Andric     unsigned NumElts = ReadVBR(6);
242139f7f9bSDimitry Andric     SkipToFourByteBoundary();  // 32-bit alignment
243139f7f9bSDimitry Andric 
244139f7f9bSDimitry Andric     // Figure out where the end of this blob will be including tail padding.
245139f7f9bSDimitry Andric     size_t CurBitPos = GetCurrentBitNo();
246139f7f9bSDimitry Andric     size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
247139f7f9bSDimitry Andric 
248139f7f9bSDimitry Andric     // If this would read off the end of the bitcode file, just set the
249139f7f9bSDimitry Andric     // record to empty and return.
250139f7f9bSDimitry Andric     if (!canSkipToPos(NewEnd/8)) {
251139f7f9bSDimitry Andric       Vals.append(NumElts, 0);
2523ca95b02SDimitry Andric       skipToEnd();
253139f7f9bSDimitry Andric       break;
254139f7f9bSDimitry Andric     }
255139f7f9bSDimitry Andric 
2563ca95b02SDimitry Andric     // Otherwise, inform the streamer that we need these bytes in memory.  Skip
2573ca95b02SDimitry Andric     // over tail padding first, in case jumping to NewEnd invalidates the Blob
2583ca95b02SDimitry Andric     // pointer.
2593ca95b02SDimitry Andric     JumpToBit(NewEnd);
2603ca95b02SDimitry Andric     const char *Ptr = (const char *)getPointerToBit(CurBitPos, NumElts);
261139f7f9bSDimitry Andric 
262139f7f9bSDimitry Andric     // If we can return a reference to the data, do so to avoid copying it.
263139f7f9bSDimitry Andric     if (Blob) {
264139f7f9bSDimitry Andric       *Blob = StringRef(Ptr, NumElts);
265139f7f9bSDimitry Andric     } else {
266139f7f9bSDimitry Andric       // Otherwise, unpack into Vals with zero extension.
267139f7f9bSDimitry Andric       for (; NumElts; --NumElts)
268139f7f9bSDimitry Andric         Vals.push_back((unsigned char)*Ptr++);
269139f7f9bSDimitry Andric     }
270139f7f9bSDimitry Andric   }
271139f7f9bSDimitry Andric 
272139f7f9bSDimitry Andric   return Code;
273139f7f9bSDimitry Andric }
274139f7f9bSDimitry Andric 
275139f7f9bSDimitry Andric void BitstreamCursor::ReadAbbrevRecord() {
276139f7f9bSDimitry Andric   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
277139f7f9bSDimitry Andric   unsigned NumOpInfo = ReadVBR(5);
278139f7f9bSDimitry Andric   for (unsigned i = 0; i != NumOpInfo; ++i) {
279ff0cc061SDimitry Andric     bool IsLiteral = Read(1);
280139f7f9bSDimitry Andric     if (IsLiteral) {
281139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
282139f7f9bSDimitry Andric       continue;
283139f7f9bSDimitry Andric     }
284139f7f9bSDimitry Andric 
285139f7f9bSDimitry Andric     BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
286139f7f9bSDimitry Andric     if (BitCodeAbbrevOp::hasEncodingData(E)) {
287ff0cc061SDimitry Andric       uint64_t Data = ReadVBR64(5);
288139f7f9bSDimitry Andric 
289139f7f9bSDimitry Andric       // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
290139f7f9bSDimitry Andric       // and vbr(0) as a literal zero.  This is decoded the same way, and avoids
291139f7f9bSDimitry Andric       // a slow path in Read() to have to handle reading zero bits.
292139f7f9bSDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
293139f7f9bSDimitry Andric           Data == 0) {
294139f7f9bSDimitry Andric         Abbv->Add(BitCodeAbbrevOp(0));
295139f7f9bSDimitry Andric         continue;
296139f7f9bSDimitry Andric       }
297139f7f9bSDimitry Andric 
298ff0cc061SDimitry Andric       if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
299ff0cc061SDimitry Andric           Data > MaxChunkSize)
300ff0cc061SDimitry Andric         report_fatal_error(
301ff0cc061SDimitry Andric             "Fixed or VBR abbrev record with size > MaxChunkData");
302ff0cc061SDimitry Andric 
303139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E, Data));
304139f7f9bSDimitry Andric     } else
305139f7f9bSDimitry Andric       Abbv->Add(BitCodeAbbrevOp(E));
306139f7f9bSDimitry Andric   }
307ff0cc061SDimitry Andric 
308ff0cc061SDimitry Andric   if (Abbv->getNumOperandInfos() == 0)
309ff0cc061SDimitry Andric     report_fatal_error("Abbrev record with no operands");
310139f7f9bSDimitry Andric   CurAbbrevs.push_back(Abbv);
311139f7f9bSDimitry Andric }
312139f7f9bSDimitry Andric 
313*d88c1a5aSDimitry Andric Optional<BitstreamBlockInfo>
314*d88c1a5aSDimitry Andric BitstreamCursor::ReadBlockInfoBlock(bool ReadBlockInfoNames) {
315*d88c1a5aSDimitry Andric   if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return None;
316139f7f9bSDimitry Andric 
317*d88c1a5aSDimitry Andric   BitstreamBlockInfo NewBlockInfo;
318139f7f9bSDimitry Andric 
319139f7f9bSDimitry Andric   SmallVector<uint64_t, 64> Record;
320*d88c1a5aSDimitry Andric   BitstreamBlockInfo::BlockInfo *CurBlockInfo = nullptr;
321139f7f9bSDimitry Andric 
322139f7f9bSDimitry Andric   // Read all the records for this module.
323*d88c1a5aSDimitry Andric   while (true) {
324139f7f9bSDimitry Andric     BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
325139f7f9bSDimitry Andric 
326139f7f9bSDimitry Andric     switch (Entry.Kind) {
327139f7f9bSDimitry Andric     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
328139f7f9bSDimitry Andric     case llvm::BitstreamEntry::Error:
329*d88c1a5aSDimitry Andric       return None;
330139f7f9bSDimitry Andric     case llvm::BitstreamEntry::EndBlock:
331*d88c1a5aSDimitry Andric       return std::move(NewBlockInfo);
332139f7f9bSDimitry Andric     case llvm::BitstreamEntry::Record:
333139f7f9bSDimitry Andric       // The interesting case.
334139f7f9bSDimitry Andric       break;
335139f7f9bSDimitry Andric     }
336139f7f9bSDimitry Andric 
337139f7f9bSDimitry Andric     // Read abbrev records, associate them with CurBID.
338139f7f9bSDimitry Andric     if (Entry.ID == bitc::DEFINE_ABBREV) {
339*d88c1a5aSDimitry Andric       if (!CurBlockInfo) return None;
340139f7f9bSDimitry Andric       ReadAbbrevRecord();
341139f7f9bSDimitry Andric 
342139f7f9bSDimitry Andric       // ReadAbbrevRecord installs the abbrev in CurAbbrevs.  Move it to the
343139f7f9bSDimitry Andric       // appropriate BlockInfo.
34439d628a0SDimitry Andric       CurBlockInfo->Abbrevs.push_back(std::move(CurAbbrevs.back()));
345139f7f9bSDimitry Andric       CurAbbrevs.pop_back();
346139f7f9bSDimitry Andric       continue;
347139f7f9bSDimitry Andric     }
348139f7f9bSDimitry Andric 
349139f7f9bSDimitry Andric     // Read a record.
350139f7f9bSDimitry Andric     Record.clear();
351139f7f9bSDimitry Andric     switch (readRecord(Entry.ID, Record)) {
352139f7f9bSDimitry Andric       default: break;  // Default behavior, ignore unknown content.
353139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_SETBID:
354*d88c1a5aSDimitry Andric         if (Record.size() < 1) return None;
355*d88c1a5aSDimitry Andric         CurBlockInfo = &NewBlockInfo.getOrCreateBlockInfo((unsigned)Record[0]);
356139f7f9bSDimitry Andric         break;
357139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_BLOCKNAME: {
358*d88c1a5aSDimitry Andric         if (!CurBlockInfo) return None;
359*d88c1a5aSDimitry Andric         if (!ReadBlockInfoNames)
3603ca95b02SDimitry Andric           break; // Ignore name.
361139f7f9bSDimitry Andric         std::string Name;
362139f7f9bSDimitry Andric         for (unsigned i = 0, e = Record.size(); i != e; ++i)
363139f7f9bSDimitry Andric           Name += (char)Record[i];
364139f7f9bSDimitry Andric         CurBlockInfo->Name = Name;
365139f7f9bSDimitry Andric         break;
366139f7f9bSDimitry Andric       }
367139f7f9bSDimitry Andric       case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
368*d88c1a5aSDimitry Andric         if (!CurBlockInfo) return None;
369*d88c1a5aSDimitry Andric         if (!ReadBlockInfoNames)
3703ca95b02SDimitry Andric           break; // Ignore name.
371139f7f9bSDimitry Andric         std::string Name;
372139f7f9bSDimitry Andric         for (unsigned i = 1, e = Record.size(); i != e; ++i)
373139f7f9bSDimitry Andric           Name += (char)Record[i];
374139f7f9bSDimitry Andric         CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
375139f7f9bSDimitry Andric                                                            Name));
376139f7f9bSDimitry Andric         break;
377139f7f9bSDimitry Andric       }
378139f7f9bSDimitry Andric     }
379139f7f9bSDimitry Andric   }
380139f7f9bSDimitry Andric }
381