|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include "hermes/AST/RecursiveVisitor.h" |
| 11 | +#include "hermes/Parser/JSLexer.h" |
| 12 | +#include "llvh/ADT/StringRef.h" |
| 13 | + |
| 14 | +namespace { |
| 15 | +using namespace hermes; |
| 16 | + |
| 17 | +/// Mutable vector that helps dealing with arrays of nodes safely. |
| 18 | +/// Once done with the vector, it can create an ESTree::NodeList |
| 19 | +/// representation which is used by the ESTree API in several places. |
| 20 | +class NodeVector { |
| 21 | + public: |
| 22 | + using Storage = llvh::SmallVector<ESTree::Node *, 8>; |
| 23 | + |
| 24 | + NodeVector() = default; |
| 25 | + NodeVector(std::initializer_list<ESTree::Node *> nodes) { |
| 26 | + for (auto &node : nodes) { |
| 27 | + _storage.push_back(node); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + NodeVector(ESTree::NodeList &list) { |
| 32 | + for (auto &node : list) { |
| 33 | + _storage.push_back(&node); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + ~NodeVector() = default; |
| 38 | + |
| 39 | + size_t size() const { |
| 40 | + return _storage.size(); |
| 41 | + } |
| 42 | + |
| 43 | + Storage::const_iterator begin() const { |
| 44 | + return _storage.begin(); |
| 45 | + } |
| 46 | + |
| 47 | + Storage::const_iterator end() const { |
| 48 | + return _storage.end(); |
| 49 | + } |
| 50 | + |
| 51 | + void append(ESTree::Node *node) { |
| 52 | + _storage.emplace_back(node); |
| 53 | + } |
| 54 | + |
| 55 | + void prepend(ESTree::Node *node) { |
| 56 | + _storage.insert(_storage.begin(), node); |
| 57 | + } |
| 58 | + |
| 59 | + ESTree::NodeList toNodeList() const { |
| 60 | + ESTree::NodeList nodeList; |
| 61 | + for (auto &node : _storage) { |
| 62 | + nodeList.push_back(*node); |
| 63 | + } |
| 64 | + return nodeList; |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 68 | + Storage _storage; |
| 69 | +}; |
| 70 | + |
| 71 | +class TransformationsBase |
| 72 | + : public ESTree::RecursionDepthTracker<TransformationsBase> { |
| 73 | + public: |
| 74 | + static constexpr bool kEnableNodeListMutation = true; |
| 75 | + |
| 76 | + TransformationsBase(Context &context) |
| 77 | + : context_(context), |
| 78 | + identVar_(context.getIdentifier("var").getUnderlyingPointer()) {} |
| 79 | + |
| 80 | + void recursionDepthExceeded(ESTree::Node *n) { |
| 81 | + context_.getSourceErrorManager().error( |
| 82 | + n->getEndLoc(), "Too many nested expressions/statements/declarations"); |
| 83 | + } |
| 84 | + |
| 85 | + protected: |
| 86 | + Context &context_; |
| 87 | + UniqueString *const identVar_; |
| 88 | + |
| 89 | + void doCopyLocation(ESTree::Node *src, ESTree::Node *dest) { |
| 90 | + if (src != nullptr) { |
| 91 | + dest->setStartLoc(src->getStartLoc()); |
| 92 | + dest->setEndLoc(src->getEndLoc()); |
| 93 | + dest->setDebugLoc(src->getDebugLoc()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + template <typename T> |
| 98 | + T *copyLocation(ESTree::Node *src, T *dest) { |
| 99 | + doCopyLocation(src, dest); |
| 100 | + return dest; |
| 101 | + } |
| 102 | + |
| 103 | + template <typename T, typename... Args> |
| 104 | + T *createTransformedNode(ESTree::Node *src, Args &&...args) { |
| 105 | + auto *node = new (context_) T(std::forward<Args>(args)...); |
| 106 | + return copyLocation(src, node); |
| 107 | + } |
| 108 | + |
| 109 | + ESTree::IdentifierNode *makeIdentifierNode( |
| 110 | + ESTree::Node *srcNode, |
| 111 | + UniqueString *name) { |
| 112 | + return createTransformedNode<ESTree::IdentifierNode>( |
| 113 | + srcNode, name, nullptr, false); |
| 114 | + } |
| 115 | + |
| 116 | + ESTree::IdentifierNode *makeIdentifierNode( |
| 117 | + ESTree::Node *srcNode, |
| 118 | + llvh::StringRef name) { |
| 119 | + return makeIdentifierNode( |
| 120 | + srcNode, context_.getIdentifier(name).getUnderlyingPointer()); |
| 121 | + } |
| 122 | + |
| 123 | + ESTree::Node *makeSingleVarDecl( |
| 124 | + ESTree::Node *srcNode, |
| 125 | + ESTree::Node *identifier, |
| 126 | + ESTree::Node *value) { |
| 127 | + auto *variableDeclarator = |
| 128 | + createTransformedNode<ESTree::VariableDeclaratorNode>( |
| 129 | + srcNode, value, identifier); |
| 130 | + ESTree::NodeList variableList; |
| 131 | + variableList.push_back(*variableDeclarator); |
| 132 | + return createTransformedNode<ESTree::VariableDeclarationNode>( |
| 133 | + srcNode, identVar_, std::move(variableList)); |
| 134 | + } |
| 135 | + |
| 136 | + ESTree::Node *makeHermesInternalCall( |
| 137 | + ESTree::Node *srcNode, |
| 138 | + llvh::StringRef methodName, |
| 139 | + const NodeVector ¶meters) { |
| 140 | + auto hermesInternalIdentifier = getHermesInternalIdentifier(srcNode); |
| 141 | + auto methodIdentifier = makeIdentifierNode(srcNode, methodName); |
| 142 | + |
| 143 | + auto *getPropertyNode = createTransformedNode<ESTree::MemberExpressionNode>( |
| 144 | + srcNode, hermesInternalIdentifier, methodIdentifier, false); |
| 145 | + return createTransformedNode<ESTree::CallExpressionNode>( |
| 146 | + srcNode, getPropertyNode, nullptr, parameters.toNodeList()); |
| 147 | + } |
| 148 | + |
| 149 | + virtual ESTree::Node *getHermesInternalIdentifier(ESTree::Node *srcNode) { |
| 150 | + return nullptr; |
| 151 | + }; |
| 152 | + |
| 153 | + virtual ~TransformationsBase() = default; |
| 154 | +}; |
| 155 | +} // namespace |
0 commit comments