1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef XFA_FXFA_FM2JS_XFA_EXPRESSION_H_ 8 #define XFA_FXFA_FM2JS_XFA_EXPRESSION_H_ 9 10 #include <memory> 11 #include <vector> 12 13 #include "xfa/fxfa/fm2js/xfa_simpleexpression.h" 14 15 enum XFA_FM_EXPTYPE { 16 XFA_FM_EXPTYPE_UNKNOWN, 17 XFA_FM_EXPTYPE_FUNC, 18 XFA_FM_EXPTYPE_VAR, 19 XFA_FM_EXPTYPE_EXP, 20 XFA_FM_EXPTYPE_BLOCK, 21 XFA_FM_EXPTYPE_IF, 22 XFA_FM_EXPTYPE_BREAK, 23 XFA_FM_EXPTYPE_CONTINUE, 24 }; 25 26 class CXFA_FMExpression { 27 public: 28 explicit CXFA_FMExpression(uint32_t line); 29 CXFA_FMExpression(uint32_t line, XFA_FM_EXPTYPE type); 30 virtual ~CXFA_FMExpression() {} 31 virtual void ToJavaScript(CFX_WideTextBuf& javascript); 32 virtual void ToImpliedReturnJS(CFX_WideTextBuf&); 33 uint32_t GetLine() { return m_line; } 34 XFA_FM_EXPTYPE GetExpType() const { return m_type; } 35 36 protected: 37 XFA_FM_EXPTYPE m_type; 38 uint32_t m_line; 39 }; 40 41 class CXFA_FMFunctionDefinition : public CXFA_FMExpression { 42 public: 43 // Takes ownership of |arguments| and |expressions|. 44 CXFA_FMFunctionDefinition( 45 uint32_t line, 46 bool isGlobal, 47 const CFX_WideStringC& wsName, 48 std::vector<CFX_WideStringC>&& arguments, 49 std::vector<std::unique_ptr<CXFA_FMExpression>>&& expressions); 50 ~CXFA_FMFunctionDefinition() override; 51 52 void ToJavaScript(CFX_WideTextBuf& javascript) override; 53 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 54 55 private: 56 CFX_WideStringC m_wsName; 57 std::vector<CFX_WideStringC> m_pArguments; 58 std::vector<std::unique_ptr<CXFA_FMExpression>> m_pExpressions; 59 bool m_isGlobal; 60 }; 61 62 class CXFA_FMVarExpression : public CXFA_FMExpression { 63 public: 64 CXFA_FMVarExpression(uint32_t line, 65 const CFX_WideStringC& wsName, 66 std::unique_ptr<CXFA_FMExpression> pInit); 67 ~CXFA_FMVarExpression() override; 68 69 void ToJavaScript(CFX_WideTextBuf& javascript) override; 70 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 71 72 private: 73 CFX_WideStringC m_wsName; 74 std::unique_ptr<CXFA_FMExpression> m_pInit; 75 }; 76 77 class CXFA_FMExpExpression : public CXFA_FMExpression { 78 public: 79 CXFA_FMExpExpression(uint32_t line, 80 std::unique_ptr<CXFA_FMSimpleExpression> pExpression); 81 ~CXFA_FMExpExpression() override; 82 83 void ToJavaScript(CFX_WideTextBuf& javascript) override; 84 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 85 86 private: 87 std::unique_ptr<CXFA_FMSimpleExpression> m_pExpression; 88 }; 89 90 class CXFA_FMBlockExpression : public CXFA_FMExpression { 91 public: 92 CXFA_FMBlockExpression( 93 uint32_t line, 94 std::vector<std::unique_ptr<CXFA_FMExpression>>&& pExpressionList); 95 ~CXFA_FMBlockExpression() override; 96 97 void ToJavaScript(CFX_WideTextBuf& javascript) override; 98 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 99 100 private: 101 std::vector<std::unique_ptr<CXFA_FMExpression>> m_ExpressionList; 102 }; 103 104 class CXFA_FMDoExpression : public CXFA_FMExpression { 105 public: 106 CXFA_FMDoExpression(uint32_t line, std::unique_ptr<CXFA_FMExpression> pList); 107 ~CXFA_FMDoExpression() override; 108 109 void ToJavaScript(CFX_WideTextBuf& javascript) override; 110 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 111 112 private: 113 std::unique_ptr<CXFA_FMExpression> m_pList; 114 }; 115 116 class CXFA_FMIfExpression : public CXFA_FMExpression { 117 public: 118 CXFA_FMIfExpression(uint32_t line, 119 std::unique_ptr<CXFA_FMSimpleExpression> pExpression, 120 std::unique_ptr<CXFA_FMExpression> pIfExpression, 121 std::unique_ptr<CXFA_FMExpression> pElseExpression); 122 ~CXFA_FMIfExpression() override; 123 124 void ToJavaScript(CFX_WideTextBuf& javascript) override; 125 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 126 127 private: 128 std::unique_ptr<CXFA_FMSimpleExpression> m_pExpression; 129 std::unique_ptr<CXFA_FMExpression> m_pIfExpression; 130 std::unique_ptr<CXFA_FMExpression> m_pElseExpression; 131 }; 132 133 class CXFA_FMLoopExpression : public CXFA_FMExpression { 134 public: 135 explicit CXFA_FMLoopExpression(uint32_t line) : CXFA_FMExpression(line) {} 136 ~CXFA_FMLoopExpression() override; 137 void ToJavaScript(CFX_WideTextBuf& javascript) override; 138 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 139 }; 140 141 class CXFA_FMWhileExpression : public CXFA_FMLoopExpression { 142 public: 143 CXFA_FMWhileExpression(uint32_t line, 144 std::unique_ptr<CXFA_FMSimpleExpression> pCodition, 145 std::unique_ptr<CXFA_FMExpression> pExpression); 146 ~CXFA_FMWhileExpression() override; 147 148 void ToJavaScript(CFX_WideTextBuf& javascript) override; 149 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 150 151 private: 152 std::unique_ptr<CXFA_FMSimpleExpression> m_pCondition; 153 std::unique_ptr<CXFA_FMExpression> m_pExpression; 154 }; 155 156 class CXFA_FMBreakExpression : public CXFA_FMExpression { 157 public: 158 explicit CXFA_FMBreakExpression(uint32_t line); 159 ~CXFA_FMBreakExpression() override; 160 void ToJavaScript(CFX_WideTextBuf& javascript) override; 161 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 162 }; 163 164 class CXFA_FMContinueExpression : public CXFA_FMExpression { 165 public: 166 explicit CXFA_FMContinueExpression(uint32_t line); 167 ~CXFA_FMContinueExpression() override; 168 void ToJavaScript(CFX_WideTextBuf& javascript) override; 169 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 170 }; 171 172 class CXFA_FMForExpression : public CXFA_FMLoopExpression { 173 public: 174 CXFA_FMForExpression(uint32_t line, 175 const CFX_WideStringC& wsVariant, 176 std::unique_ptr<CXFA_FMSimpleExpression> pAssignment, 177 std::unique_ptr<CXFA_FMSimpleExpression> pAccessor, 178 int32_t iDirection, 179 std::unique_ptr<CXFA_FMSimpleExpression> pStep, 180 std::unique_ptr<CXFA_FMExpression> pList); 181 ~CXFA_FMForExpression() override; 182 183 void ToJavaScript(CFX_WideTextBuf& javascript) override; 184 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 185 186 private: 187 CFX_WideStringC m_wsVariant; 188 std::unique_ptr<CXFA_FMSimpleExpression> m_pAssignment; 189 std::unique_ptr<CXFA_FMSimpleExpression> m_pAccessor; 190 int32_t m_iDirection; 191 std::unique_ptr<CXFA_FMSimpleExpression> m_pStep; 192 std::unique_ptr<CXFA_FMExpression> m_pList; 193 }; 194 195 class CXFA_FMForeachExpression : public CXFA_FMLoopExpression { 196 public: 197 // Takes ownership of |pAccessors|. 198 CXFA_FMForeachExpression( 199 uint32_t line, 200 const CFX_WideStringC& wsIdentifier, 201 std::vector<std::unique_ptr<CXFA_FMSimpleExpression>>&& pAccessors, 202 std::unique_ptr<CXFA_FMExpression> pList); 203 ~CXFA_FMForeachExpression() override; 204 205 void ToJavaScript(CFX_WideTextBuf& javascript) override; 206 void ToImpliedReturnJS(CFX_WideTextBuf&) override; 207 208 private: 209 CFX_WideStringC m_wsIdentifier; 210 std::vector<std::unique_ptr<CXFA_FMSimpleExpression>> m_pAccessors; 211 std::unique_ptr<CXFA_FMExpression> m_pList; 212 }; 213 214 #endif // XFA_FXFA_FM2JS_XFA_EXPRESSION_H_ 215