[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / src / modules.h
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_MODULES_H_
6 #define V8_MODULES_H_
7
8 #include "src/zone.h"
9
10 namespace v8 {
11 namespace internal {
12
13
14 class AstRawString;
15
16
17 class ModuleDescriptor : public ZoneObject {
18  public:
19   // ---------------------------------------------------------------------------
20   // Factory methods.
21
22   static ModuleDescriptor* New(Zone* zone) {
23     return new (zone) ModuleDescriptor(zone);
24   }
25
26   // ---------------------------------------------------------------------------
27   // Mutators.
28
29   // Add a name to the list of exports. If it already exists, or this descriptor
30   // is frozen, that's an error.
31   void AddLocalExport(const AstRawString* export_name,
32                       const AstRawString* local_name, Zone* zone, bool* ok);
33
34   // Add module_specifier to the list of requested modules,
35   // if not already present.
36   void AddModuleRequest(const AstRawString* module_specifier, Zone* zone);
37
38   // Do not allow any further refinements, directly or through unification.
39   void Freeze() { frozen_ = true; }
40
41   // Assign an index.
42   void Allocate(int index) {
43     DCHECK(IsFrozen() && index_ == -1);
44     index_ = index;
45   }
46
47   // ---------------------------------------------------------------------------
48   // Accessors.
49
50   // Check whether this is closed (i.e. fully determined).
51   bool IsFrozen() { return frozen_; }
52
53   int Length() {
54     DCHECK(IsFrozen());
55     ZoneHashMap* exports = exports_;
56     return exports ? exports->occupancy() : 0;
57   }
58
59   // The context slot in the hosting script context pointing to this module.
60   int Index() {
61     DCHECK(IsFrozen());
62     return index_;
63   }
64
65   const AstRawString* LookupLocalExport(const AstRawString* export_name,
66                                         Zone* zone);
67
68   const ZoneList<const AstRawString*>& requested_modules() const {
69     return requested_modules_;
70   }
71
72   // ---------------------------------------------------------------------------
73   // Iterators.
74
75   // Use like:
76   //   for (auto it = descriptor->iterator(); !it.done(); it.Advance()) {
77   //     ... it.name() ...
78   //   }
79   class Iterator {
80    public:
81     bool done() const { return entry_ == NULL; }
82     const AstRawString* export_name() const {
83       DCHECK(!done());
84       return static_cast<const AstRawString*>(entry_->key);
85     }
86     const AstRawString* local_name() const {
87       DCHECK(!done());
88       return static_cast<const AstRawString*>(entry_->value);
89     }
90     void Advance() { entry_ = exports_->Next(entry_); }
91
92    private:
93     friend class ModuleDescriptor;
94     explicit Iterator(const ZoneHashMap* exports)
95         : exports_(exports), entry_(exports ? exports->Start() : NULL) {}
96
97     const ZoneHashMap* exports_;
98     ZoneHashMap::Entry* entry_;
99   };
100
101   Iterator iterator() const { return Iterator(this->exports_); }
102
103   // ---------------------------------------------------------------------------
104   // Implementation.
105  private:
106   explicit ModuleDescriptor(Zone* zone)
107       : frozen_(false),
108         exports_(NULL),
109         requested_modules_(1, zone),
110         index_(-1) {}
111
112   bool frozen_;
113   ZoneHashMap* exports_;   // Module exports and their types (allocated lazily)
114   ZoneList<const AstRawString*> requested_modules_;
115   int index_;
116 };
117
118 }  // namespace internal
119 }  // namespace v8
120
121 #endif  // V8_MODULES_H_