summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/LexicalScopeEmitter.h
blob: 379ecc0a89baf729befa0daca2fee2e634b63ef1 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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_LexicalScopeEmitter_h
#define frontend_LexicalScopeEmitter_h

#include "mozilla/Attributes.h"  // MOZ_STACK_CLASS
#include "mozilla/Maybe.h"       // Maybe

#include "frontend/EmitterScope.h"   // EmitterScope
#include "frontend/TDZCheckCache.h"  // TDZCheckCache
#include "vm/Scope.h"                // ScopeKind, LexicalScope

namespace js {
namespace frontend {

struct BytecodeEmitter;

// Class for emitting bytecode for lexical scope.
//
// In addition to emitting code for entering and leaving a scope, this RAII
// guard affects the code emitted for `break` and other non-structured
// control flow. See NonLocalExitControl::prepareForNonLocalJump().
//
// Usage: (check for the return value is omitted for simplicity)
//
//   `{ ... }` -- lexical scope with no bindings
//     LexicalScopeEmitter lse(this);
//     lse.emitEmptyScope();
//     emit(scopeBody);
//     lse.emitEnd();
//
//   `{ let a; body }`
//     LexicalScopeEmitter lse(this);
//     lse.emitScope(ScopeKind::Lexical, scopeBinding);
//     emit(let_and_body);
//     lse.emitEnd();
//
//   `catch (e) { body }`
//     LexicalScopeEmitter lse(this);
//     lse.emitScope(ScopeKind::SimpleCatch, scopeBinding);
//     emit(body);
//     lse.emitEnd();
//
//   `catch ([a, b]) { body }`
//     LexicalScopeEmitter lse(this);
//     lse.emitScope(ScopeKind::Catch, scopeBinding);
//     emit(body);
//     lse.emitEnd();
class MOZ_STACK_CLASS LexicalScopeEmitter {
  BytecodeEmitter* bce_;

  mozilla::Maybe<TDZCheckCache> tdzCache_;
  mozilla::Maybe<EmitterScope> emitterScope_;

#ifdef DEBUG
  // The state of this emitter.
  //
  // +-------+ emitScope  +-------+ emitEnd  +-----+
  // | Start |----------->| Scope |--------->| End |
  // +-------+            +-------+          +-----+
  enum class State {
    // The initial state.
    Start,

    // After calling emitScope/emitEmptyScope.
    Scope,

    // After calling emitEnd.
    End,
  };
  State state_ = State::Start;
#endif

 public:
  explicit LexicalScopeEmitter(BytecodeEmitter* bce);

  // Returns the scope object for non-empty scope.
  const EmitterScope& emitterScope() const { return *emitterScope_; }

  [[nodiscard]] bool emitScope(ScopeKind kind,
                               LexicalScope::ParserData* bindings);
  [[nodiscard]] bool emitEmptyScope();

  [[nodiscard]] bool emitEnd();
};

} /* namespace frontend */
} /* namespace js */

#endif /* frontend_LexicalScopeEmitter_h */