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
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.
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.
28 #ifndef V8_INTERFACE_H_
29 #define V8_INTERFACE_H_
31 #include "zone-inl.h" // For operator new.
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.
46 class Interface : public ZoneObject {
48 // ---------------------------------------------------------------------------
51 static Interface* NewValue() {
52 static Interface value_interface(VALUE + FROZEN); // Cached.
53 return &value_interface;
56 static Interface* NewUnknown() {
57 return new Interface(NONE);
60 static Interface* NewModule() {
61 return new Interface(MODULE);
64 // ---------------------------------------------------------------------------
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);
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);
77 // Determine this interface to be a value interface.
78 void MakeValue(bool* ok) {
80 if (*ok) Chase()->flags_ |= VALUE;
83 // Determine this interface to be a module interface.
84 void MakeModule(bool* ok) {
86 if (*ok) Chase()->flags_ |= MODULE;
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;
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;
101 // ---------------------------------------------------------------------------
104 // Check whether this is still a fully undetermined type.
105 bool IsUnknown() { return Chase()->flags_ == NONE; }
107 // Check whether this is a value type.
108 bool IsValue() { return Chase()->flags_ & VALUE; }
110 // Check whether this is a module type.
111 bool IsModule() { return Chase()->flags_ & MODULE; }
113 // Check whether this is closed (i.e. fully determined).
114 bool IsFrozen() { return Chase()->flags_ & FROZEN; }
116 Handle<JSModule> Instance() { return Chase()->instance_; }
118 // Look up an exported name. Returns NULL if not (yet) defined.
119 Interface* Lookup(Handle<String> name);
121 // ---------------------------------------------------------------------------
125 // for (auto it = interface->iterator(); !it.done(); it.Advance()) {
126 // ... it.name() ... it.interface() ...
130 bool done() const { return entry_ == NULL; }
131 Handle<String> name() const {
133 return Handle<String>(*static_cast<String**>(entry_->key));
135 Interface* interface() const {
137 return static_cast<Interface*>(entry_->value);
139 void Advance() { entry_ = exports_->Next(entry_); }
142 friend class Interface;
143 explicit Iterator(const ZoneHashMap* exports)
144 : exports_(exports), entry_(exports ? exports->Start() : NULL) {}
146 const ZoneHashMap* exports_;
147 ZoneHashMap::Entry* entry_;
150 Iterator iterator() const { return Iterator(this->exports_); }
152 // ---------------------------------------------------------------------------
155 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
158 // ---------------------------------------------------------------------------
161 enum Flags { // All flags are monotonic
163 VALUE = 1, // This type describes a value
164 MODULE = 2, // This type describes a module
165 FROZEN = 4 // This type is fully determined
169 Interface* forward_; // Unification link
170 ZoneHashMap* exports_; // Module exports and their types (allocated lazily)
171 Handle<JSModule> instance_;
173 explicit Interface(int flags)
178 if (FLAG_print_interface_details)
179 PrintF("# Creating %p\n", static_cast<void*>(this));
184 Interface* result = this;
185 while (result->forward_ != NULL) result = result->forward_;
186 if (result != this) forward_ = result; // On-the-fly path compression.
190 void DoAdd(void* name, uint32_t hash, Interface* interface, bool* ok);
191 void DoUnify(Interface* that, bool* ok);
194 } } // namespace v8::internal
196 #endif // V8_INTERFACE_H_