Rename some GC classes so that their roll will hopefully be clearer.
[oota-llvm.git] / include / llvm / Bitcode / BitstreamReader.h
index e34dd0921947e721bc8b1998566b206a610d5412..3b7a9b61e6aaab5d8a14ae85cb9e20e08b2f83ad 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Chris Lattner and is distributed under
-// the University of Illinois Open Source License.  See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 
 namespace llvm {
   
+  class Deserializer;
+  
 class BitstreamReader {
   const unsigned char *NextChar;
   const unsigned char *LastChar;
+  friend class Deserializer;
   
   /// CurWord - This is the current data we have pulled from the stream but have
   /// not returned to the client.
@@ -82,12 +85,15 @@ public:
   ~BitstreamReader() {
     // Abbrevs could still exist if the stream was broken.  If so, don't leak
     // them.
-    for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
+    for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
+         i != e; ++i)
       CurAbbrevs[i]->dropRef();
 
-    for (unsigned S = 0, e = BlockScope.size(); S != e; ++S) {
+    for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
+         S != e; ++S) {
       std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
-      for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
+      for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
+           i != e; ++i)
         Abbrevs[i]->dropRef();
     }
     
@@ -95,13 +101,16 @@ public:
     while (!BlockInfoRecords.empty()) {
       BlockInfo &Info = BlockInfoRecords.back();
       // Free blockinfo abbrev info.
-      for (unsigned i = 0, e = Info.Abbrevs.size(); i != e; ++i)
+      for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
+           i != e; ++i)
         Info.Abbrevs[i]->dropRef();
       BlockInfoRecords.pop_back();
     }
   }
   
-  bool AtEndOfStream() const { return NextChar == LastChar; }
+  bool AtEndOfStream() const {
+    return NextChar == LastChar && BitsInCurWord == 0;
+  }
   
   /// GetCurrentBitNo - Return the bit # of the bit we are reading.
   uint64_t GetCurrentBitNo() const {
@@ -110,18 +119,19 @@ public:
   
   /// JumpToBit - Reset the stream to the specified bit number.
   void JumpToBit(uint64_t BitNo) {
-    unsigned ByteNo = (BitNo/8) & ~3;
-    unsigned WordBitNo = BitNo & 31;
-    assert(ByteNo < (unsigned)(LastChar-FirstChar) && "Invalid location");
+    uintptr_t ByteNo = uintptr_t(BitNo/8) & ~3;
+    uintptr_t WordBitNo = uintptr_t(BitNo) & 31;
+    assert(ByteNo < (uintptr_t)(LastChar-FirstChar) && "Invalid location");
     
     // Move the cursor to the right word.
     NextChar = FirstChar+ByteNo;
     BitsInCurWord = 0;
+    CurWord = 0;
     
     // Skip over any bits that are already consumed.
     if (WordBitNo) {
       NextChar -= 4;
-      Read(WordBitNo);
+      Read(static_cast<unsigned>(WordBitNo));
     }
   }
   
@@ -231,7 +241,8 @@ private:
     if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
       return &BlockInfoRecords.back();
     
-    for (unsigned i = 0, e = BlockInfoRecords.size(); i != e; ++i)
+    for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
+         i != e; ++i)
       if (BlockInfoRecords[i].BlockID == BlockID)
         return &BlockInfoRecords[i];
     return 0;
@@ -267,8 +278,8 @@ public:
     return false;
   }
   
-  /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
-  /// the block, returning the BlockID of the block we just entered.
+  /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
+  /// the block, and return true if the block is valid.
   bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = 0) {
     // Save the current block's state on BlockScope.
     BlockScope.push_back(Block(CurCodeSize));
@@ -276,7 +287,8 @@ public:
     
     // Add the abbrevs specific to this block to the CurAbbrevs list.
     if (BlockInfo *Info = getBlockInfo(BlockID)) {
-      for (unsigned i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
+      for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
+           i != e; ++i) {
         CurAbbrevs.push_back(Info->Abbrevs[i]);
         CurAbbrevs.back()->addRef();
       }
@@ -301,17 +313,24 @@ public:
     // Block tail:
     //    [END_BLOCK, <align4bytes>]
     SkipToWord();
+    
+    PopBlockScope();
+    return false;
+  }
+  
+private:
+  void PopBlockScope() {
     CurCodeSize = BlockScope.back().PrevCodeSize;
     
     // Delete abbrevs from popped scope.
-    for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
+    for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
+         i != e; ++i)
       CurAbbrevs[i]->dropRef();
     
     BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
     BlockScope.pop_back();
-    return false;
-  }
-  
+  }  
+    
   //===--------------------------------------------------------------------===//
   // Record Processing
   //===--------------------------------------------------------------------===//
@@ -327,10 +346,10 @@ private:
       switch (Op.getEncoding()) {
       default: assert(0 && "Unknown encoding!");
       case BitCodeAbbrevOp::Fixed:
-        Vals.push_back(Read(Op.getEncodingData()));
+        Vals.push_back(Read((unsigned)Op.getEncodingData()));
         break;
       case BitCodeAbbrevOp::VBR:
-        Vals.push_back(ReadVBR64(Op.getEncodingData()));
+        Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
         break;
       case BitCodeAbbrevOp::Char6:
         Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
@@ -370,7 +389,7 @@ public:
       }
     }
     
-    unsigned Code = Vals[0];
+    unsigned Code = (unsigned)Vals[0];
     Vals.erase(Vals.begin());
     return Code;
   }
@@ -383,7 +402,7 @@ public:
     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
     unsigned NumOpInfo = ReadVBR(5);
     for (unsigned i = 0; i != NumOpInfo; ++i) {
-      bool IsLiteral = Read(1);
+      bool IsLiteral = Read(1) ? true : false;
       if (IsLiteral) {
         Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
         continue;
@@ -451,7 +470,7 @@ public:
       default: break;  // Default behavior, ignore unknown content.
       case bitc::BLOCKINFO_CODE_SETBID:
         if (Record.size() < 1) return true;
-        CurBlockInfo = &getOrCreateBlockInfo(Record[0]);
+        CurBlockInfo = &getOrCreateBlockInfo((unsigned)Record[0]);
         break;
       }
     }