modules.h 8.51 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
    Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const;
111
  };
112

113 114 115
  enum CellIndexKind { kInvalid, kExport, kImport };
  static CellIndexKind GetCellIndexKind(int cell_index);

116 117 118 119 120 121
  struct ModuleRequest {
    int index;
    int position;
    ModuleRequest(int index, int position) : index(index), position(position) {}
  };

122 123 124 125 126 127 128 129 130 131 132 133 134
  // Custom content-based comparer for the below maps, to keep them stable
  // across parses.
  struct AstRawStringComparer {
    bool operator()(const AstRawString* lhs, const AstRawString* rhs) const;
  };

  typedef ZoneMap<const AstRawString*, ModuleRequest, AstRawStringComparer>
      ModuleRequestMap;
  typedef ZoneMultimap<const AstRawString*, Entry*, AstRawStringComparer>
      RegularExportMap;
  typedef ZoneMap<const AstRawString*, Entry*, AstRawStringComparer>
      RegularImportMap;

135
  // Module requests.
136
  const ModuleRequestMap& module_requests() const { return module_requests_; }
137

138
  // Namespace imports.
139
  const ZoneVector<const Entry*>& namespace_imports() const {
140
    return namespace_imports_;
141 142 143
  }

  // All the remaining imports, indexed by local name.
144
  const RegularImportMap& regular_imports() const { return regular_imports_; }
145

146
  // Star exports and explicitly indirect exports.
147
  const ZoneVector<const Entry*>& special_exports() const {
148 149 150 151
    return special_exports_;
  }

  // All the remaining exports, indexed by local name.
152
  // After canonicalization (see Validate), these are exactly the local exports.
153
  const RegularExportMap& regular_exports() const { return regular_exports_; }
154

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

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

169
  void AddRegularImport(Entry* entry) {
170 171 172
    DCHECK_NOT_NULL(entry->import_name);
    DCHECK_NOT_NULL(entry->local_name);
    DCHECK_NULL(entry->export_name);
173
    DCHECK_LE(0, entry->module_request);
174 175 176 177 178
    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.
  }

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

187 188 189
  Handle<FixedArray> SerializeRegularExports(Isolate* isolate,
                                             Zone* zone) const;

190
 private:
191
  ModuleRequestMap module_requests_;
192 193
  ZoneVector<const Entry*> special_exports_;
  ZoneVector<const Entry*> namespace_imports_;
194 195
  RegularExportMap regular_exports_;
  RegularImportMap 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_