interface.h 6.45 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_INTERFACE_H_
#define V8_INTERFACE_H_

8
#include "src/ast-value-factory.h"
9
#include "src/zone-inl.h"  // For operator new.
10 11 12 13 14 15 16

namespace v8 {
namespace internal {


// This class implements the following abstract grammar of interfaces
// (i.e. module types):
17
//   interface ::= UNDETERMINED | VALUE | CONST | MODULE(exports)
18
//   exports ::= {name : interface, ...}
19 20 21
// A frozen type is one that is fully determined. Unification does not
// allow to turn non-const values into const, or adding additional exports to
// frozen interfaces. Otherwise, unifying modules merges their exports.
22
// Undetermined types are unification variables that can be unified freely.
23 24 25 26 27 28 29 30 31 32 33
// There is a natural subsort lattice that reflects the increase of knowledge:
//
//            undetermined
//           //     |    \\                                                    .
//       value  (frozen)  module
//      //   \\  /    \  //
//  const   fr.value  fr.module
//      \\    /
//     fr.const
//
// where the bold lines are the only transitions allowed.
34 35 36 37 38 39

class Interface : public ZoneObject {
 public:
  // ---------------------------------------------------------------------------
  // Factory methods.

40 41 42 43
  static Interface* NewUnknown(Zone* zone) {
    return new(zone) Interface(NONE);
  }

44
  static Interface* NewValue();
45

46
  static Interface* NewConst();
47

48 49
  static Interface* NewModule(Zone* zone) {
    return new(zone) Interface(MODULE);
50 51 52 53 54 55 56
  }

  // ---------------------------------------------------------------------------
  // Mutators.

  // Add a name to the list of exports. If it already exists, unify with
  // interface, otherwise insert unless this is closed.
57 58 59
  void Add(const AstRawString* name, Interface* interface, Zone* zone,
           bool* ok) {
    DoAdd(name, name->hash(), interface, zone, ok);
60 61 62 63
  }

  // Unify with another interface. If successful, both interface objects will
  // represent the same type, and changes to one are reflected in the other.
64
  void Unify(Interface* that, Zone* zone, bool* ok);
65 66 67 68 69 70 71

  // Determine this interface to be a value interface.
  void MakeValue(bool* ok) {
    *ok = !IsModule();
    if (*ok) Chase()->flags_ |= VALUE;
  }

72 73 74 75 76 77
  // Determine this interface to be an immutable interface.
  void MakeConst(bool* ok) {
    *ok = !IsModule() && (IsConst() || !IsFrozen());
    if (*ok) Chase()->flags_ |= VALUE + CONST;
  }

78 79 80 81 82 83 84 85 86 87 88 89
  // Determine this interface to be a module interface.
  void MakeModule(bool* ok) {
    *ok = !IsValue();
    if (*ok) Chase()->flags_ |= MODULE;
  }

  // Do not allow any further refinements, directly or through unification.
  void Freeze(bool* ok) {
    *ok = IsValue() || IsModule();
    if (*ok) Chase()->flags_ |= FROZEN;
  }

90 91
  // Assign an index.
  void Allocate(int index) {
92
    DCHECK(IsModule() && IsFrozen() && Chase()->index_ == -1);
93 94 95
    Chase()->index_ = index;
  }

96 97 98 99 100 101 102 103 104
  // ---------------------------------------------------------------------------
  // Accessors.

  // Check whether this is still a fully undetermined type.
  bool IsUnknown() { return Chase()->flags_ == NONE; }

  // Check whether this is a value type.
  bool IsValue() { return Chase()->flags_ & VALUE; }

105 106 107
  // Check whether this is a constant type.
  bool IsConst() { return Chase()->flags_ & CONST; }

108 109 110 111 112 113
  // Check whether this is a module type.
  bool IsModule() { return Chase()->flags_ & MODULE; }

  // Check whether this is closed (i.e. fully determined).
  bool IsFrozen() { return Chase()->flags_ & FROZEN; }

114 115 116 117 118 119 120
  bool IsUnified(Interface* that) {
    return Chase() == that->Chase()
        || (this->IsValue() == that->IsValue() &&
            this->IsConst() == that->IsConst());
  }

  int Length() {
121
    DCHECK(IsModule() && IsFrozen());
122 123 124 125 126 127
    ZoneHashMap* exports = Chase()->exports_;
    return exports ? exports->occupancy() : 0;
  }

  // The context slot in the hosting global context pointing to this module.
  int Index() {
128
    DCHECK(IsModule() && IsFrozen());
129 130
    return Chase()->index_;
  }
131 132

  // Look up an exported name. Returns NULL if not (yet) defined.
133
  Interface* Lookup(Handle<String> name, Zone* zone);
134 135 136 137 138 139 140 141 142 143 144

  // ---------------------------------------------------------------------------
  // Iterators.

  // Use like:
  //   for (auto it = interface->iterator(); !it.done(); it.Advance()) {
  //     ... it.name() ... it.interface() ...
  //   }
  class Iterator {
   public:
    bool done() const { return entry_ == NULL; }
145
    const AstRawString* name() const {
146
      DCHECK(!done());
147
      return static_cast<const AstRawString*>(entry_->key);
148 149
    }
    Interface* interface() const {
150
      DCHECK(!done());
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      return static_cast<Interface*>(entry_->value);
    }
    void Advance() { entry_ = exports_->Next(entry_); }

   private:
    friend class Interface;
    explicit Iterator(const ZoneHashMap* exports)
        : exports_(exports), entry_(exports ? exports->Start() : NULL) {}

    const ZoneHashMap* exports_;
    ZoneHashMap::Entry* entry_;
  };

  Iterator iterator() const { return Iterator(this->exports_); }

166 167 168 169 170 171 172 173 174
  // ---------------------------------------------------------------------------
  // Debugging.
#ifdef DEBUG
  void Print(int n = 0);  // n = indentation; n < 0 => don't print recursively
#endif

  // ---------------------------------------------------------------------------
  // Implementation.
 private:
175
  struct Cache;
176

177 178 179
  enum Flags {    // All flags are monotonic
    NONE = 0,
    VALUE = 1,    // This type describes a value
180 181 182
    CONST = 2,    // This type describes a constant
    MODULE = 4,   // This type describes a module
    FROZEN = 8    // This type is fully determined
183 184 185 186 187
  };

  int flags_;
  Interface* forward_;     // Unification link
  ZoneHashMap* exports_;   // Module exports and their types (allocated lazily)
188
  int index_;
189 190 191 192

  explicit Interface(int flags)
    : flags_(flags),
      forward_(NULL),
193 194
      exports_(NULL),
      index_(-1) {
195 196 197 198 199 200 201 202 203 204 205 206 207
#ifdef DEBUG
    if (FLAG_print_interface_details)
      PrintF("# Creating %p\n", static_cast<void*>(this));
#endif
  }

  Interface* Chase() {
    Interface* result = this;
    while (result->forward_ != NULL) result = result->forward_;
    if (result != this) forward_ = result;  // On-the-fly path compression.
    return result;
  }

208
  void DoAdd(const void* name, uint32_t hash, Interface* interface, Zone* zone,
209 210
             bool* ok);
  void DoUnify(Interface* that, bool* ok, Zone* zone);
211 212 213 214 215
};

} }  // namespace v8::internal

#endif  // V8_INTERFACE_H_