tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ExpressionStatementEmitter.h (2030B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef frontend_ExpressionStatementEmitter_h
      8 #define frontend_ExpressionStatementEmitter_h
      9 
     10 #include "mozilla/Attributes.h"
     11 
     12 #include <stdint.h>
     13 
     14 #include "frontend/ValueUsage.h"
     15 
     16 namespace js {
     17 namespace frontend {
     18 
     19 struct BytecodeEmitter;
     20 
     21 // Class for emitting bytecode for expression statement.
     22 //
     23 // Usage: (check for the return value is omitted for simplicity)
     24 //
     25 //   `expr;`
     26 //     // IgnoreValue if this is in normal script.
     27 //     // WantValue if this is in eval script.
     28 //     ValueUsage valueUsage = ...;
     29 //
     30 //     ExpressionStatementEmitter ese(this, valueUsage);
     31 //     ese.prepareForExpr(offset_of_expr);
     32 //     emit(expr);
     33 //     ese.emitEnd();
     34 //
     35 class MOZ_STACK_CLASS ExpressionStatementEmitter {
     36  BytecodeEmitter* bce_;
     37 
     38 #ifdef DEBUG
     39  // The stack depth before emitting expression.
     40  int32_t depth_;
     41 #endif
     42 
     43  // The usage of the value of the expression.
     44  ValueUsage valueUsage_;
     45 
     46 #ifdef DEBUG
     47  // The state of this emitter.
     48  //
     49  // +-------+ prepareForExpr +------+ emitEnd +-----+
     50  // | Start |--------------->| Expr |-------->| End |
     51  // +-------+                +------+         +-----+
     52  enum class State {
     53    // The initial state.
     54    Start,
     55 
     56    // After calling prepareForExpr.
     57    Expr,
     58 
     59    // After calling emitEnd.
     60    End
     61  };
     62  State state_ = State::Start;
     63 #endif
     64 
     65 public:
     66  ExpressionStatementEmitter(BytecodeEmitter* bce, ValueUsage valueUsage);
     67 
     68  // Parameters are the offset in the source code for each character below:
     69  //
     70  //   expr;
     71  //   ^
     72  //   |
     73  //   beginPos
     74  [[nodiscard]] bool prepareForExpr(uint32_t beginPos);
     75  [[nodiscard]] bool emitEnd();
     76 };
     77 
     78 }  // namespace frontend
     79 }  // namespace js
     80 
     81 #endif /* frontend_ExpressionStatementEmitter_h */