summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/NameAnalysisTypes.h
blob: d39e177fba1a33f7efba8209f378ad0fc30b5152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef frontend_NameAnalysis_h
#define frontend_NameAnalysis_h

#include "jsopcode.h"

#include "vm/Scope.h"

namespace js {

// An "environment coordinate" describes how to get from head of the
// environment chain to a given lexically-enclosing variable. An environment
// coordinate has two dimensions:
//  - hops: the number of environment objects on the scope chain to skip
//  - slot: the slot on the environment object holding the variable's value
class EnvironmentCoordinate
{
    uint32_t hops_;
    uint32_t slot_;

    // Technically, hops_/slot_ are ENVCOORD_(HOPS|SLOT)_BITS wide.  Since
    // EnvironmentCoordinate is a temporary value, don't bother with a bitfield as
    // this only adds overhead.
    static_assert(ENVCOORD_HOPS_BITS <= 32, "We have enough bits below");
    static_assert(ENVCOORD_SLOT_BITS <= 32, "We have enough bits below");

  public:
    explicit inline EnvironmentCoordinate(jsbytecode* pc)
      : hops_(GET_ENVCOORD_HOPS(pc)), slot_(GET_ENVCOORD_SLOT(pc + ENVCOORD_HOPS_LEN))
    {
        MOZ_ASSERT(JOF_OPTYPE(JSOp(*pc)) == JOF_ENVCOORD);
    }

    EnvironmentCoordinate() {}

    void setHops(uint32_t hops) {
        MOZ_ASSERT(hops < ENVCOORD_HOPS_LIMIT);
        hops_ = hops;
    }

    void setSlot(uint32_t slot) {
        MOZ_ASSERT(slot < ENVCOORD_SLOT_LIMIT);
        slot_ = slot;
    }

    uint32_t hops() const {
        MOZ_ASSERT(hops_ < ENVCOORD_HOPS_LIMIT);
        return hops_;
    }

    uint32_t slot() const {
        MOZ_ASSERT(slot_ < ENVCOORD_SLOT_LIMIT);
        return slot_;
    }

    bool operator==(const EnvironmentCoordinate& rhs) const {
        return hops() == rhs.hops() && slot() == rhs.slot();
    }
};

namespace frontend {

// A detailed kind used for tracking declarations in the Parser. Used for
// specific early error semantics and better error messages.
enum class DeclarationKind : uint8_t
{
    PositionalFormalParameter,
    FormalParameter,
    CoverArrowParameter,
    Var,
    ForOfVar,
    Let,
    Const,
    Import,
    BodyLevelFunction,
    LexicalFunction,
    VarForAnnexBLexicalFunction,
    SimpleCatchParameter,
    CatchParameter
};

static inline BindingKind
DeclarationKindToBindingKind(DeclarationKind kind)
{
    switch (kind) {
      case DeclarationKind::PositionalFormalParameter:
      case DeclarationKind::FormalParameter:
      case DeclarationKind::CoverArrowParameter:
        return BindingKind::FormalParameter;

      case DeclarationKind::Var:
      case DeclarationKind::BodyLevelFunction:
      case DeclarationKind::VarForAnnexBLexicalFunction:
      case DeclarationKind::ForOfVar:
        return BindingKind::Var;

      case DeclarationKind::Let:
      case DeclarationKind::LexicalFunction:
      case DeclarationKind::SimpleCatchParameter:
      case DeclarationKind::CatchParameter:
        return BindingKind::Let;

      case DeclarationKind::Const:
        return BindingKind::Const;

      case DeclarationKind::Import:
        return BindingKind::Import;
    }

    MOZ_CRASH("Bad DeclarationKind");
}

static inline bool
DeclarationKindIsLexical(DeclarationKind kind)
{
    return BindingKindIsLexical(DeclarationKindToBindingKind(kind));
}

// Used in Parser to track declared names.
class DeclaredNameInfo
{
    DeclarationKind kind_;

    // If the declared name is a binding, whether the binding is closed
    // over. Its value is meaningless if the declared name is not a binding
    // (i.e., a 'var' declared name in a non-var scope).
    bool closedOver_;

  public:
    explicit DeclaredNameInfo(DeclarationKind kind)
      : kind_(kind),
        closedOver_(false)
    { }

    // Needed for InlineMap.
    DeclaredNameInfo() = default;

    DeclarationKind kind() const {
        return kind_;
    }

    void alterKind(DeclarationKind kind) {
        kind_ = kind;
    }

    void setClosedOver() {
        closedOver_ = true;
    }

    bool closedOver() const {
        return closedOver_;
    }
};

// Used in BytecodeEmitter to map names to locations.
class NameLocation
{
  public:
    enum class Kind : uint8_t
    {
        // Cannot statically determine where the name lives. Needs to walk the
        // environment chain to search for the name.
        Dynamic,

        // The name lives on the global or is a global lexical binding. Search
        // for the name on the global scope.
        Global,

        // Special mode used only when emitting self-hosted scripts. See
        // BytecodeEmitter::lookupName.
        Intrinsic,

        // In a named lambda, the name is the callee itself.
        NamedLambdaCallee,

        // The name is a positional formal parameter name and can be retrieved
        // directly from the stack using slot_.
        ArgumentSlot,

        // The name is not closed over and lives on the frame in slot_.
        FrameSlot,

        // The name is closed over and lives on an environment hops_ away in slot_.
        EnvironmentCoordinate,

        // An imported name in a module.
        Import,

        // Cannot statically determine where the synthesized var for Annex
        // B.3.3 lives.
        DynamicAnnexBVar
    };

  private:
    // Where the name lives.
    Kind kind_;

    // If the name is not Dynamic or DynamicAnnexBVar, the kind of the
    // binding.
    BindingKind bindingKind_;

    // If the name is closed over and accessed via EnvironmentCoordinate, the
    // number of dynamic environments to skip.
    //
    // Otherwise UINT8_MAX.
    uint8_t hops_;

    // If the name lives on the frame, the slot frame.
    //
    // If the name is closed over and accessed via EnvironmentCoordinate, the
    // slot on the environment.
    //
    // Otherwise LOCALNO_LIMIT/ENVCOORD_SLOT_LIMIT.
    uint32_t slot_ : ENVCOORD_SLOT_BITS;

    static_assert(LOCALNO_BITS == ENVCOORD_SLOT_BITS,
                  "Frame and environment slots must be same sized.");

    NameLocation(Kind kind, BindingKind bindingKind,
                 uint8_t hops = UINT8_MAX, uint32_t slot = ENVCOORD_SLOT_LIMIT)
      : kind_(kind),
        bindingKind_(bindingKind),
        hops_(hops),
        slot_(slot)
    { }

  public:
    // Default constructor for InlineMap.
    NameLocation() = default;

    static NameLocation Dynamic() {
        return NameLocation();
    }

    static NameLocation Global(BindingKind bindKind) {
        MOZ_ASSERT(bindKind != BindingKind::FormalParameter);
        return NameLocation(Kind::Global, bindKind);
    }

    static NameLocation Intrinsic() {
        return NameLocation(Kind::Intrinsic, BindingKind::Var);
    }

    static NameLocation NamedLambdaCallee() {
        return NameLocation(Kind::NamedLambdaCallee, BindingKind::NamedLambdaCallee);
    }

    static NameLocation ArgumentSlot(uint16_t slot) {
        return NameLocation(Kind::ArgumentSlot, BindingKind::FormalParameter, 0, slot);
    }

    static NameLocation FrameSlot(BindingKind bindKind, uint32_t slot) {
        MOZ_ASSERT(slot < LOCALNO_LIMIT);
        return NameLocation(Kind::FrameSlot, bindKind, 0, slot);
    }

    static NameLocation EnvironmentCoordinate(BindingKind bindKind, uint8_t hops, uint32_t slot) {
        MOZ_ASSERT(slot < ENVCOORD_SLOT_LIMIT);
        return NameLocation(Kind::EnvironmentCoordinate, bindKind, hops, slot);
    }

    static NameLocation Import() {
        return NameLocation(Kind::Import, BindingKind::Import);
    }

    static NameLocation DynamicAnnexBVar() {
        return NameLocation(Kind::DynamicAnnexBVar, BindingKind::Var);
    }

    static NameLocation fromBinding(BindingKind bindKind, const BindingLocation& bl) {
        switch (bl.kind()) {
          case BindingLocation::Kind::Global:
            return Global(bindKind);
          case BindingLocation::Kind::Argument:
            return ArgumentSlot(bl.argumentSlot());
          case BindingLocation::Kind::Frame:
            return FrameSlot(bindKind, bl.slot());
          case BindingLocation::Kind::Environment:
            return EnvironmentCoordinate(bindKind, 0, bl.slot());
          case BindingLocation::Kind::Import:
            return Import();
          case BindingLocation::Kind::NamedLambdaCallee:
            return NamedLambdaCallee();
        }
        MOZ_CRASH("Bad BindingKind");
    }

    bool operator==(const NameLocation& other) const {
        return kind_ == other.kind_ && bindingKind_ == other.bindingKind_ &&
               hops_ == other.hops_ && slot_ == other.slot_;
    }

    bool operator!=(const NameLocation& other) const {
        return !(*this == other);
    }

    Kind kind() const {
        return kind_;
    }

    uint16_t argumentSlot() const {
        MOZ_ASSERT(kind_ == Kind::ArgumentSlot);
        return mozilla::AssertedCast<uint16_t>(slot_);
    }

    uint32_t frameSlot() const {
        MOZ_ASSERT(kind_ == Kind::FrameSlot);
        return slot_;
    }

    NameLocation addHops(uint8_t more) {
        MOZ_ASSERT(hops_ < ENVCOORD_HOPS_LIMIT - more);
        MOZ_ASSERT(kind_ == Kind::EnvironmentCoordinate);
        return NameLocation(kind_, bindingKind_, hops_ + more, slot_);
    }

    class EnvironmentCoordinate environmentCoordinate() const {
        MOZ_ASSERT(kind_ == Kind::EnvironmentCoordinate);
        class EnvironmentCoordinate coord;
        coord.setHops(hops_);
        coord.setSlot(slot_);
        return coord;
    }

    BindingKind bindingKind() const {
        MOZ_ASSERT(kind_ != Kind::Dynamic);
        return bindingKind_;
    }

    bool isLexical() const {
        return BindingKindIsLexical(bindingKind());
    }

    bool isConst() const {
        return bindingKind() == BindingKind::Const;
    }

    bool hasKnownSlot() const {
        return kind_ == Kind::ArgumentSlot ||
               kind_ == Kind::FrameSlot ||
               kind_ == Kind::EnvironmentCoordinate;
    }
};

// This type is declared here for LazyScript::Create.
using AtomVector = Vector<JSAtom*, 24, SystemAllocPolicy>;

} // namespace frontend
} // namespace js

namespace mozilla {

template <>
struct IsPod<js::frontend::DeclaredNameInfo> : TrueType {};

template <>
struct IsPod<js::frontend::NameLocation> : TrueType {};

} // namespace mozilla

#endif // frontend_NameAnalysis_h