Home | History | Annotate | Download | only in parsing
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_PARSING_DUPLICATE_FINDER_H_
      6 #define V8_PARSING_DUPLICATE_FINDER_H_
      7 
      8 #include "src/base/hashmap.h"
      9 #include "src/collector.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 class UnicodeCache;
     15 
     16 // DuplicateFinder discovers duplicate symbols.
     17 class DuplicateFinder {
     18  public:
     19   explicit DuplicateFinder(UnicodeCache* constants)
     20       : unicode_constants_(constants), backing_store_(16), map_(&Match) {}
     21 
     22   int AddOneByteSymbol(Vector<const uint8_t> key, int value);
     23   int AddTwoByteSymbol(Vector<const uint16_t> key, int value);
     24   // Add a a number literal by converting it (if necessary)
     25   // to the string that ToString(ToNumber(literal)) would generate.
     26   // and then adding that string with AddOneByteSymbol.
     27   // This string is the actual value used as key in an object literal,
     28   // and the one that must be different from the other keys.
     29   int AddNumber(Vector<const uint8_t> key, int value);
     30 
     31  private:
     32   int AddSymbol(Vector<const uint8_t> key, bool is_one_byte, int value);
     33   // Backs up the key and its length in the backing store.
     34   // The backup is stored with a base 127 encoding of the
     35   // length (plus a bit saying whether the string is one byte),
     36   // followed by the bytes of the key.
     37   uint8_t* BackupKey(Vector<const uint8_t> key, bool is_one_byte);
     38 
     39   // Compare two encoded keys (both pointing into the backing store)
     40   // for having the same base-127 encoded lengths and representation.
     41   // and then having the same 'length' bytes following.
     42   static bool Match(void* first, void* second);
     43   // Creates a hash from a sequence of bytes.
     44   static uint32_t Hash(Vector<const uint8_t> key, bool is_one_byte);
     45   // Checks whether a string containing a JS number is its canonical
     46   // form.
     47   static bool IsNumberCanonical(Vector<const uint8_t> key);
     48 
     49   // Size of buffer. Sufficient for using it to call DoubleToCString in
     50   // from conversions.h.
     51   static const int kBufferSize = 100;
     52 
     53   UnicodeCache* unicode_constants_;
     54   // Backing store used to store strings used as hashmap keys.
     55   SequenceCollector<unsigned char> backing_store_;
     56   base::CustomMatcherHashMap map_;
     57   // Buffer used for string->number->canonical string conversions.
     58   char number_buffer_[kBufferSize];
     59 };
     60 
     61 }  // namespace internal
     62 }  // namespace v8
     63 
     64 #endif  // V8_PARSING_DUPLICATE_FINDER_H_
     65