[V8] Introduce a QML compilation mode
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / interface.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_INTERFACE_H_
29 #define V8_INTERFACE_H_
30
31 #include "zone-inl.h"  // For operator new.
32
33 namespace v8 {
34 namespace internal {
35
36
37 // This class implements the following abstract grammar of interfaces
38 // (i.e. module types):
39 //   interface ::= UNDETERMINED | VALUE | MODULE(exports)
40 //   exports ::= {name : interface, ...}
41 // A frozen module type is one that is fully determined. Unification does not
42 // allow adding additional exports to frozen interfaces.
43 // Otherwise, unifying modules merges their exports.
44 // Undetermined types are unification variables that can be unified freely.
45
46 class Interface : public ZoneObject {
47  public:
48   // ---------------------------------------------------------------------------
49   // Factory methods.
50
51   static Interface* NewValue() {
52     static Interface value_interface(VALUE + FROZEN);  // Cached.
53     return &value_interface;
54   }
55
56   static Interface* NewUnknown() {
57     return new Interface(NONE);
58   }
59
60   static Interface* NewModule() {
61     return new Interface(MODULE);
62   }
63
64   // ---------------------------------------------------------------------------
65   // Mutators.
66
67   // Add a name to the list of exports. If it already exists, unify with
68   // interface, otherwise insert unless this is closed.
69   void Add(Handle<String> name, Interface* interface, bool* ok) {
70     DoAdd(name.location(), name->Hash(), interface, ok);
71   }
72
73   // Unify with another interface. If successful, both interface objects will
74   // represent the same type, and changes to one are reflected in the other.
75   void Unify(Interface* that, bool* ok);
76
77   // Determine this interface to be a value interface.
78   void MakeValue(bool* ok) {
79     *ok = !IsModule();
80     if (*ok) Chase()->flags_ |= VALUE;
81   }
82
83   // Determine this interface to be a module interface.
84   void MakeModule(bool* ok) {
85     *ok = !IsValue();
86     if (*ok) Chase()->flags_ |= MODULE;
87   }
88
89   // Set associated instance object.
90   void MakeSingleton(Handle<JSModule> instance, bool* ok) {
91     *ok = IsModule() && Chase()->instance_.is_null();
92     if (*ok) Chase()->instance_ = instance;
93   }
94
95   // Do not allow any further refinements, directly or through unification.
96   void Freeze(bool* ok) {
97     *ok = IsValue() || IsModule();
98     if (*ok) Chase()->flags_ |= FROZEN;
99   }
100
101   // ---------------------------------------------------------------------------
102   // Accessors.
103
104   // Check whether this is still a fully undetermined type.
105   bool IsUnknown() { return Chase()->flags_ == NONE; }
106
107   // Check whether this is a value type.
108   bool IsValue() { return Chase()->flags_ & VALUE; }
109
110   // Check whether this is a module type.
111   bool IsModule() { return Chase()->flags_ & MODULE; }
112
113   // Check whether this is closed (i.e. fully determined).
114   bool IsFrozen() { return Chase()->flags_ & FROZEN; }
115
116   Handle<JSModule> Instance() { return Chase()->instance_; }
117
118   // Look up an exported name. Returns NULL if not (yet) defined.
119   Interface* Lookup(Handle<String> name);
120
121   // ---------------------------------------------------------------------------
122   // Iterators.
123
124   // Use like:
125   //   for (auto it = interface->iterator(); !it.done(); it.Advance()) {
126   //     ... it.name() ... it.interface() ...
127   //   }
128   class Iterator {
129    public:
130     bool done() const { return entry_ == NULL; }
131     Handle<String> name() const {
132       ASSERT(!done());
133       return Handle<String>(*static_cast<String**>(entry_->key));
134     }
135     Interface* interface() const {
136       ASSERT(!done());
137       return static_cast<Interface*>(entry_->value);
138     }
139     void Advance() { entry_ = exports_->Next(entry_); }
140
141    private:
142     friend class Interface;
143     explicit Iterator(const ZoneHashMap* exports)
144         : exports_(exports), entry_(exports ? exports->Start() : NULL) {}
145
146     const ZoneHashMap* exports_;
147     ZoneHashMap::Entry* entry_;
148   };
149
150   Iterator iterator() const { return Iterator(this->exports_); }
151
152   // ---------------------------------------------------------------------------
153   // Debugging.
154 #ifdef DEBUG
155   void Print(int n = 0);  // n = indentation; n < 0 => don't print recursively
156 #endif
157
158   // ---------------------------------------------------------------------------
159   // Implementation.
160  private:
161   enum Flags {    // All flags are monotonic
162     NONE = 0,
163     VALUE = 1,    // This type describes a value
164     MODULE = 2,   // This type describes a module
165     FROZEN = 4    // This type is fully determined
166   };
167
168   int flags_;
169   Interface* forward_;     // Unification link
170   ZoneHashMap* exports_;   // Module exports and their types (allocated lazily)
171   Handle<JSModule> instance_;
172
173   explicit Interface(int flags)
174     : flags_(flags),
175       forward_(NULL),
176       exports_(NULL) {
177 #ifdef DEBUG
178     if (FLAG_print_interface_details)
179       PrintF("# Creating %p\n", static_cast<void*>(this));
180 #endif
181   }
182
183   Interface* Chase() {
184     Interface* result = this;
185     while (result->forward_ != NULL) result = result->forward_;
186     if (result != this) forward_ = result;  // On-the-fly path compression.
187     return result;
188   }
189
190   void DoAdd(void* name, uint32_t hash, Interface* interface, bool* ok);
191   void DoUnify(Interface* that, bool* ok);
192 };
193
194 } }  // namespace v8::internal
195
196 #endif  // V8_INTERFACE_H_