StructureSet.h 5.12 KB
Newer Older
1
/*
2
 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

26 27
#ifndef StructureSet_h
#define StructureSet_h
28

29
#include "ArrayProfile.h"
30
#include "SpeculatedType.h"
31
#include "Structure.h"
32
#include "DumpContext.h"
33
#include <wtf/CommaPrinter.h>
34 35 36 37 38
#include <wtf/Vector.h>

namespace JSC {

namespace DFG {
39
class StructureAbstractValue;
40
}
41

42 43 44 45 46 47 48 49 50
class StructureSet {
public:
    StructureSet() { }
    
    StructureSet(Structure* structure)
    {
        m_structures.append(structure);
    }
    
51 52 53 54 55
    void clear()
    {
        m_structures.clear();
    }
    
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    void add(Structure* structure)
    {
        ASSERT(!contains(structure));
        m_structures.append(structure);
    }
    
    bool addAll(const StructureSet& other)
    {
        bool changed = false;
        for (size_t i = 0; i < other.size(); ++i) {
            if (contains(other[i]))
                continue;
            add(other[i]);
            changed = true;
        }
        return changed;
    }
    
    void remove(Structure* structure)
    {
        for (size_t i = 0; i < m_structures.size(); ++i) {
            if (m_structures[i] != structure)
                continue;
            
            m_structures[i] = m_structures.last();
            m_structures.removeLast();
            return;
        }
    }
    
    bool contains(Structure* structure) const
    {
        for (size_t i = 0; i < m_structures.size(); ++i) {
            if (m_structures[i] == structure)
                return true;
        }
        return false;
    }
    
95 96 97 98 99 100 101
    bool containsOnly(Structure* structure) const
    {
        if (size() != 1)
            return false;
        return singletonStructure() == structure;
    }
    
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    bool isSubsetOf(const StructureSet& other) const
    {
        for (size_t i = 0; i < m_structures.size(); ++i) {
            if (!other.contains(m_structures[i]))
                return false;
        }
        return true;
    }
    
    bool isSupersetOf(const StructureSet& other) const
    {
        return other.isSubsetOf(*this);
    }
    
    size_t size() const { return m_structures.size(); }
    
118 119 120 121 122 123 124 125
    // Call this if you know that the structure set must consist of exactly
    // one structure.
    Structure* singletonStructure() const
    {
        ASSERT(m_structures.size() == 1);
        return m_structures[0];
    }
    
126 127 128 129 130 131
    Structure* at(size_t i) const { return m_structures.at(i); }
    
    Structure* operator[](size_t i) const { return at(i); }
    
    Structure* last() const { return m_structures.last(); }

132
    SpeculatedType speculationFromStructures() const
133
    {
134
        SpeculatedType result = SpecNone;
135 136
        
        for (size_t i = 0; i < m_structures.size(); ++i)
137
            mergeSpeculation(result, speculationFromStructure(m_structures[i]));
138 139 140 141
        
        return result;
    }
    
142 143 144 145 146 147 148 149 150 151
    ArrayModes arrayModesFromStructures() const
    {
        ArrayModes result = 0;
        
        for (size_t i = 0; i < m_structures.size(); ++i)
            mergeArrayModes(result, asArrayModes(m_structures[i]->indexingType()));
        
        return result;
    }
    
152 153 154 155 156 157 158 159 160 161 162 163 164
    bool operator==(const StructureSet& other) const
    {
        if (m_structures.size() != other.m_structures.size())
            return false;
        
        for (size_t i = 0; i < m_structures.size(); ++i) {
            if (!other.contains(m_structures[i]))
                return false;
        }
        
        return true;
    }
    
165
    void dumpInContext(PrintStream& out, DumpContext* context) const
166
    {
167 168 169
        CommaPrinter comma;
        out.print("[");
        for (size_t i = 0; i < m_structures.size(); ++i)
170
            out.print(comma, inContext(*m_structures[i], context));
171
        out.print("]");
172 173
    }
    
174 175 176 177 178
    void dump(PrintStream& out) const
    {
        dumpInContext(out, 0);
    }
    
179
private:
180
    friend class DFG::StructureAbstractValue;
181
    
182 183 184
    Vector<Structure*, 2> m_structures;
};

185
} // namespace JSC
186

187
#endif // StructureSet_h