modules.h revision f91f0611dbaf29ca0f1d4aecb357ce243a19d2fa
1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_AST_MODULES_H_
6#define V8_AST_MODULES_H_
7
8#include "src/parsing/scanner.h"  // Only for Scanner::Location.
9#include "src/pending-compilation-error-handler.h"
10#include "src/zone-containers.h"
11
12namespace v8 {
13namespace internal {
14
15
16class AstRawString;
17
18
19class ModuleDescriptor : public ZoneObject {
20 public:
21  explicit ModuleDescriptor(Zone* zone)
22      : exports_(1, zone), special_imports_(1, zone), regular_imports_(zone) {}
23
24  // import x from "foo.js";
25  // import {x} from "foo.js";
26  // import {x as y} from "foo.js";
27  void AddImport(
28    const AstRawString* import_name, const AstRawString* local_name,
29    const AstRawString* module_request, const Scanner::Location loc,
30    Zone* zone);
31
32  // import * as x from "foo.js";
33  void AddStarImport(
34    const AstRawString* local_name, const AstRawString* module_request,
35    const Scanner::Location loc, Zone* zone);
36
37  // import "foo.js";
38  // import {} from "foo.js";
39  // export {} from "foo.js";  (sic!)
40  void AddEmptyImport(
41      const AstRawString* module_request, const Scanner::Location loc,
42      Zone* zone);
43
44  // export {x};
45  // export {x as y};
46  // export VariableStatement
47  // export Declaration
48  // export default ...
49  void AddExport(
50    const AstRawString* local_name, const AstRawString* export_name,
51    const Scanner::Location loc, Zone* zone);
52
53  // export {x} from "foo.js";
54  // export {x as y} from "foo.js";
55  void AddExport(
56    const AstRawString* export_name, const AstRawString* import_name,
57    const AstRawString* module_request, const Scanner::Location loc,
58    Zone* zone);
59
60  // export * from "foo.js";
61  void AddStarExport(
62    const AstRawString* module_request, const Scanner::Location loc,
63    Zone* zone);
64
65  // Check if module is well-formed and report error if not.
66  // Also canonicalize indirect exports.
67  bool Validate(ModuleScope* module_scope,
68                PendingCompilationErrorHandler* error_handler, Zone* zone);
69
70  struct ModuleEntry : public ZoneObject {
71    const Scanner::Location location;
72    const AstRawString* export_name;
73    const AstRawString* local_name;
74    const AstRawString* import_name;
75    const AstRawString* module_request;
76
77    explicit ModuleEntry(Scanner::Location loc)
78        : location(loc),
79          export_name(nullptr),
80          local_name(nullptr),
81          import_name(nullptr),
82          module_request(nullptr) {}
83  };
84
85  const ZoneList<ModuleEntry*>& exports() const { return exports_; }
86
87  // Empty imports and namespace imports.
88  const ZoneList<const ModuleEntry*>& special_imports() const {
89    return special_imports_;
90  }
91
92  // All the remaining imports, indexed by local name.
93  const ZoneMap<const AstRawString*, const ModuleEntry*>& regular_imports()
94      const {
95    return regular_imports_;
96  }
97
98 private:
99  ZoneList<ModuleEntry*> exports_;
100  ZoneList<const ModuleEntry*> special_imports_;
101  ZoneMap<const AstRawString*, const ModuleEntry*> regular_imports_;
102
103  // Find any implicitly indirect exports and make them explicit.
104  //
105  // An explicitly indirect export is an export entry arising from an export
106  // statement of the following form:
107  //   export {a as c} from "X";
108  // An implicitly indirect export corresponds to
109  //   export {b as c};
110  // in the presence of an import statement of the form
111  //   import {a as b} from "X";
112  // This function finds such implicitly indirect export entries and rewrites
113  // them by filling in the import name and module request, as well as nulling
114  // out the local name.  Effectively, it turns
115  //   import {a as b} from "X"; export {b as c};
116  // into:
117  //   import {a as b} from "X"; export {a as c} from "X";
118  // (The import entry is never deleted.)
119  void MakeIndirectExportsExplicit();
120};
121
122}  // namespace internal
123}  // namespace v8
124
125#endif  // V8_AST_MODULES_H_
126