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 #include "mozilla/Maybe.h" 12 13 #include <stdint.h> 14 15 #include "frontend/ValueUsage.h" 16 17 namespace js { 18 namespace frontend { 19 20 struct BytecodeEmitter; 21 22 // Class for emitting bytecode for expression statement. 23 // 24 // Usage: (check for the return value is omitted for simplicity) 25 // 26 // `expr;` 27 // // IgnoreValue if this is in normal script. 28 // // WantValue if this is in eval script. 29 // ValueUsage valueUsage = ...; 30 // 31 // ExpressionStatementEmitter ese(this, valueUsage); 32 // ese.prepareForExpr(Some(offset_of_expr)); 33 // emit(expr); 34 // ese.emitEnd(); 35 // 36 class MOZ_STACK_CLASS ExpressionStatementEmitter { 37 BytecodeEmitter* bce_; 38 39 #ifdef DEBUG 40 // The stack depth before emitting expression. 41 int32_t depth_; 42 #endif 43 44 // The usage of the value of the expression. 45 ValueUsage valueUsage_; 46 47 #ifdef DEBUG 48 // The state of this emitter. 49 // 50 // +-------+ prepareForExpr +------+ emitEnd +-----+ 51 // | Start |--------------->| Expr |-------->| End | 52 // +-------+ +------+ +-----+ 53 enum class State { 54 // The initial state. 55 Start, 56 57 // After calling prepareForExpr. 58 Expr, 59 60 // After calling emitEnd. 61 End 62 }; 63 State state_ = State::Start; 64 #endif 65 66 public: 67 ExpressionStatementEmitter(BytecodeEmitter* bce, ValueUsage valueUsage); 68 69 // Parameters are the offset in the source code for each character below: 70 // 71 // expr; 72 // ^ 73 // | 74 // beginPos 75 // 76 // Can be Nothing() if not available. 77 MOZ_MUST_USE bool prepareForExpr(const mozilla::Maybe<uint32_t>& beginPos); 78 MOZ_MUST_USE bool emitEnd(); 79 }; 80 81 } // namespace frontend 82 } // namespace js 83 84 #endif /* frontend_ExpressionStatementEmitter_h */ 85