modules.h 8.63 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
#ifndef V8_AST_MODULES_H_
#define V8_AST_MODULES_H_
7

8
#include "src/parsing/scanner.h"  // Only for Scanner::Location.
9
#include "src/zone/zone-containers.h"
10 11 12 13

namespace v8 {
namespace internal {

14

15
class AstRawString;
16
class ModuleInfo;
17
class ModuleInfoEntry;
marja's avatar
marja committed
18
class PendingCompilationErrorHandler;
19

20
class ModuleDescriptor : public ZoneObject {
21
 public:
22
  explicit ModuleDescriptor(Zone* zone)
23
      : module_requests_(zone),
24 25
        special_exports_(zone),
        namespace_imports_(zone),
26 27
        regular_exports_(zone),
        regular_imports_(zone) {}
28

29 30 31
  // The following Add* methods are high-level convenience functions for use by
  // the parser.

32 33 34
  // import x from "foo.js";
  // import {x} from "foo.js";
  // import {x as y} from "foo.js";
35 36 37 38 39
  void AddImport(const AstRawString* import_name,
                 const AstRawString* local_name,
                 const AstRawString* module_request,
                 const Scanner::Location loc,
                 const Scanner::Location specifier_loc, Zone* zone);
40 41

  // import * as x from "foo.js";
42 43 44 45
  void AddStarImport(const AstRawString* local_name,
                     const AstRawString* module_request,
                     const Scanner::Location loc,
                     const Scanner::Location specifier_loc, Zone* zone);
46 47 48 49

  // import "foo.js";
  // import {} from "foo.js";
  // export {} from "foo.js";  (sic!)
50 51
  void AddEmptyImport(const AstRawString* module_request,
                      const Scanner::Location specifier_loc);
52 53 54 55 56 57 58 59 60 61 62 63

  // export {x};
  // export {x as y};
  // export VariableStatement
  // export Declaration
  // export default ...
  void AddExport(
    const AstRawString* local_name, const AstRawString* export_name,
    const Scanner::Location loc, Zone* zone);

  // export {x} from "foo.js";
  // export {x as y} from "foo.js";
64 65 66 67 68
  void AddExport(const AstRawString* export_name,
                 const AstRawString* import_name,
                 const AstRawString* module_request,
                 const Scanner::Location loc,
                 const Scanner::Location specifier_loc, Zone* zone);
69 70

  // export * from "foo.js";
71 72 73
  void AddStarExport(const AstRawString* module_request,
                     const Scanner::Location loc,
                     const Scanner::Location specifier_loc, Zone* zone);
74 75

  // Check if module is well-formed and report error if not.
76
  // Also canonicalize indirect exports.
77
  bool Validate(ModuleScope* module_scope,
78
                PendingCompilationErrorHandler* error_handler, Zone* zone);
79

80
  struct Entry : public ZoneObject {
81
    Scanner::Location location;
82 83 84
    const AstRawString* export_name;
    const AstRawString* local_name;
    const AstRawString* import_name;
85

86 87 88 89 90
    // The module_request value records the order in which modules are
    // requested. It also functions as an index into the ModuleInfo's array of
    // module specifiers and into the Module's array of requested modules.  A
    // negative value means no module request.
    int module_request;
91

92
    // Import/export entries that are associated with a MODULE-allocated
93 94 95 96 97 98
    // variable (i.e. regular_imports and regular_exports after Validate) use
    // the cell_index value to encode the location of their cell.  During
    // variable allocation, this will be be copied into the variable's index
    // field.
    // Entries that are not associated with a MODULE-allocated variable have
    // GetCellIndexKind(cell_index) == kInvalid.
99 100
    int cell_index;

101
    // TODO(neis): Remove local_name component?
102
    explicit Entry(Scanner::Location loc)
103 104 105 106
        : location(loc),
          export_name(nullptr),
          local_name(nullptr),
          import_name(nullptr),
107 108
          module_request(-1),
          cell_index(0) {}
109

110 111 112 113
    // (De-)serialization support.
    // Note that the location value is not preserved as it's only needed by the
    // parser.  (A Deserialize'd entry has an invalid location.)
    Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const;
114
    static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory,
115
                              Handle<ModuleInfoEntry> entry);
116
  };
117

118 119 120
  enum CellIndexKind { kInvalid, kExport, kImport };
  static CellIndexKind GetCellIndexKind(int cell_index);

121 122 123 124 125 126
  struct ModuleRequest {
    int index;
    int position;
    ModuleRequest(int index, int position) : index(index), position(position) {}
  };

127
  // Module requests.
128
  const ZoneMap<const AstRawString*, ModuleRequest>& module_requests() const {
129 130 131
    return module_requests_;
  }

132
  // Namespace imports.
133
  const ZoneVector<const Entry*>& namespace_imports() const {
134
    return namespace_imports_;
135 136 137
  }

  // All the remaining imports, indexed by local name.
138
  const ZoneMap<const AstRawString*, Entry*>& regular_imports() const {
139 140
    return regular_imports_;
  }
141

142
  // Star exports and explicitly indirect exports.
143
  const ZoneVector<const Entry*>& special_exports() const {
144 145 146 147
    return special_exports_;
  }

  // All the remaining exports, indexed by local name.
148
  // After canonicalization (see Validate), these are exactly the local exports.
149
  const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const {
150 151 152
    return regular_exports_;
  }

153 154 155 156
  void AddRegularExport(Entry* entry) {
    DCHECK_NOT_NULL(entry->export_name);
    DCHECK_NOT_NULL(entry->local_name);
    DCHECK_NULL(entry->import_name);
157
    DCHECK_LT(entry->module_request, 0);
158 159 160 161
    regular_exports_.insert(std::make_pair(entry->local_name, entry));
  }

  void AddSpecialExport(const Entry* entry, Zone* zone) {
neis's avatar
neis committed
162
    DCHECK_NULL(entry->local_name);
163
    DCHECK_LE(0, entry->module_request);
164
    special_exports_.push_back(entry);
165 166
  }

167
  void AddRegularImport(Entry* entry) {
168 169 170
    DCHECK_NOT_NULL(entry->import_name);
    DCHECK_NOT_NULL(entry->local_name);
    DCHECK_NULL(entry->export_name);
171
    DCHECK_LE(0, entry->module_request);
172 173 174 175 176
    regular_imports_.insert(std::make_pair(entry->local_name, entry));
    // We don't care if there's already an entry for this local name, as in that
    // case we will report an error when declaring the variable.
  }

177 178
  void AddNamespaceImport(const Entry* entry, Zone* zone) {
    DCHECK_NULL(entry->import_name);
179
    DCHECK_NULL(entry->export_name);
180
    DCHECK_NOT_NULL(entry->local_name);
181
    DCHECK_LE(0, entry->module_request);
182
    namespace_imports_.push_back(entry);
183 184
  }

185 186 187
  Handle<FixedArray> SerializeRegularExports(Isolate* isolate,
                                             Zone* zone) const;
  void DeserializeRegularExports(Isolate* isolate, AstValueFactory* avfactory,
188
                                 Handle<ModuleInfo> module_info);
189

190
 private:
191
  ZoneMap<const AstRawString*, ModuleRequest> module_requests_;
192 193
  ZoneVector<const Entry*> special_exports_;
  ZoneVector<const Entry*> namespace_imports_;
194
  ZoneMultimap<const AstRawString*, Entry*> regular_exports_;
195
  ZoneMap<const AstRawString*, Entry*> regular_imports_;
196

197 198
  // If there are multiple export entries with the same export name, return the
  // last of them (in source order).  Otherwise return nullptr.
199
  const Entry* FindDuplicateExport(Zone* zone) const;
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  // Find any implicitly indirect exports and make them explicit.
  //
  // An explicitly indirect export is an export entry arising from an export
  // statement of the following form:
  //   export {a as c} from "X";
  // An implicitly indirect export corresponds to
  //   export {b as c};
  // in the presence of an import statement of the form
  //   import {a as b} from "X";
  // This function finds such implicitly indirect export entries and rewrites
  // them by filling in the import name and module request, as well as nulling
  // out the local name.  Effectively, it turns
  //   import {a as b} from "X"; export {b as c};
  // into:
  //   import {a as b} from "X"; export {a as c} from "X";
  // (The import entry is never deleted.)
217
  void MakeIndirectExportsExplicit(Zone* zone);
218

219 220 221 222 223
  // Assign a cell_index of -1,-2,... to regular imports.
  // Assign a cell_index of +1,+2,... to regular (local) exports.
  // Assign a cell_index of 0 to anything else.
  void AssignCellIndices();

224 225
  int AddModuleRequest(const AstRawString* specifier,
                       Scanner::Location specifier_loc) {
226
    DCHECK_NOT_NULL(specifier);
227
    int module_requests_count = static_cast<int>(module_requests_.size());
228
    auto it = module_requests_
229 230 231
                  .insert(std::make_pair(specifier,
                                         ModuleRequest(module_requests_count,
                                                       specifier_loc.beg_pos)))
232
                  .first;
233
    return it->second.index;
234
  }
235 236
};

237 238
}  // namespace internal
}  // namespace v8
239

240
#endif  // V8_AST_MODULES_H_